mixer-slider/firmware/slider/main.c

120 lines
2.1 KiB
C
Raw Normal View History

2019-02-06 23:22:59 +01:00
#include <avr/io.h>
#include <avr/interrupt.h>
2019-02-09 21:33:45 +01:00
#include "adc.h"
#include "led.h"
2019-02-10 19:12:40 +01:00
#include "button.h"
#include "main.h"
2019-02-06 23:22:59 +01:00
/* Communication driver */
#define SLAVE_ENABLE_DDR DDRA
#define SLAVE_ENABLE_REG PINA
#define SLAVE_ENABLE_PIN 1
2019-02-08 22:22:28 +01:00
#include <interface.h>
2019-02-06 23:22:59 +01:00
2019-02-10 19:12:40 +01:00
typedef struct {
iface_t iface;
} comm_t;
comm_t comm;
2019-02-06 23:22:59 +01:00
void comm_write(iface_t * iface, byte_t byte) {
2019-02-10 19:12:40 +01:00
// disable interrupt (temporarily)
2019-02-06 23:22:59 +01:00
uint8_t temp = USICR;
USICR &= ~(1<<USIOIE);
// write data register
USIDR = byte;
// wait for buffer overflow
while(!(USICR&(1<<USIOIE))) {}
// restore control register
USICR = temp;
}
void comm_init() {
// TODO: setup pins
// TODO: setup USART in synchronous mode
USICR = (1<<USIWM0)|(1<<USICS1);
2019-02-10 19:12:40 +01:00
// init interface
comm.iface.write = comm_write;
2019-02-06 23:22:59 +01:00
}
void comm_start() {
2019-02-10 19:12:40 +01:00
// don't start without interrupt handler
if (!comm.iface.on_read)
2019-02-09 21:33:45 +01:00
return;
2019-02-06 23:22:59 +01:00
// TODO: activate USART
USICR |= (1<<USIOIE);
}
ISR(USI_OVF_vect) {
2019-02-10 19:12:40 +01:00
// call interrupt handler
comm.iface.on_read(&comm.iface, USIBR, comm.iface.callback_data);
2019-02-09 21:33:45 +01:00
}
void on_read(iface_t * sender, byte_t b) {
if (!IS_EOM(b)) {
sender->write(sender, b);
return;
}
// strip EOM message
sender->write(sender, b&0x7F);
}
2019-02-10 19:12:40 +01:00
#include "main.h"
2019-02-09 21:33:45 +01:00
2019-02-10 19:12:40 +01:00
// the public instance of the app buffer
struct app app = {};
2019-02-09 21:33:45 +01:00
2019-02-10 19:12:40 +01:00
// initalizes the application modules
2019-02-06 23:22:59 +01:00
void init() {
2019-02-10 19:12:40 +01:00
// init button module (making the button light up on press)
// module also updates app.buffer.button for us
2019-02-06 23:22:59 +01:00
button_init();
2019-02-10 19:12:40 +01:00
// init adc module
// will sample 0x8F (CHANNEL 8 and 0-3)
// this module also updates app.buffer.poti and slider for us
2019-02-09 21:33:45 +01:00
adc_init((adc_buffer_t){
.ptr = app.adc_buffer,
.size = 5,
}, 0x8F);
2019-02-06 23:22:59 +01:00
2019-02-10 19:12:40 +01:00
// init communication module
comm_init();
// bind state encoder to communication module
slave_init(&app.state, &comm.iface);
// start all modules
adc_start();
comm_start();
// activate interrupts (effectivly starting the application)
2019-02-06 23:22:59 +01:00
sei();
}
2019-02-10 19:12:40 +01:00
// little debug helper function
// FIXME: remove when not needed
2019-02-09 21:33:45 +01:00
void delay(uint16_t d) {
while(d--) {
__asm("nop");
}
}
2019-02-06 23:22:59 +01:00
int main() {
init();
2019-02-10 19:12:40 +01:00
// start relevant periphials
2019-02-09 21:33:45 +01:00
adc_start();
while(1) {
2019-02-10 19:12:40 +01:00
// nothing to do here, the application is interrupt-driven
2019-02-09 21:33:45 +01:00
}
2019-02-06 23:22:59 +01:00
}