mixer-slider/firmware/slider/main.c

166 lines
2.9 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 {
2019-02-10 23:03:00 +01:00
byte_t * buffer_start, * buffer_end;
volatile byte_t * read;
volatile byte_t * write;
volatile byte_t count;
2019-02-10 19:12:40 +01:00
iface_t iface;
} comm_t;
2019-02-10 23:03:00 +01:00
comm_t comm;
2019-02-06 23:22:59 +01:00
2019-02-10 23:03:00 +01:00
void comm_write(iface_t * iface, byte_t byte) {
if ((comm.write == comm.read) && comm.count) {
// write overflow
return;
}
*comm.write = byte;
++comm.count;
// advance write pointer
if ((++comm.write) >= comm.buffer_end) {
comm.write = comm.buffer_start;
}
}
2019-02-06 23:22:59 +01:00
2019-02-10 23:03:00 +01:00
inline byte_t comm_read() {
if (!comm.count)
return 0x00;
byte_t result = *comm.read;
--comm.count;
if ((++comm.read) >= comm.buffer_end) {
comm.read = comm.buffer_start;
}
return result;
2019-02-06 23:22:59 +01:00
}
2019-02-10 23:03:00 +01:00
byte_t comm_buffer[10];
void comm_init()
{
comm.buffer_start = comm_buffer;
comm.buffer_end = comm_buffer + sizeof(comm_buffer);
// reset buffer
comm.write = comm.buffer_start;
comm.read = comm.buffer_start;
comm.count = 0;
2019-02-06 23:22:59 +01:00
// TODO: setup pins
2019-02-10 23:03:00 +01:00
DDRA |= (1<<PA5); // setup MOSI as output
2019-02-06 23:22:59 +01:00
// TODO: setup USART in synchronous mode
USICR = (1<<USIWM0)|(1<<USICS1);
2019-02-10 19:12:40 +01:00
// init interface
2019-02-10 23:03:00 +01:00
comm.iface.write = comm_write;
comm.iface.on_read = 0;
2019-02-06 23:22:59 +01:00
}
2019-02-10 23:03:00 +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);
}
2019-02-10 23:03:00 +01:00
ISR(USI_OVF_vect)
{
cli();
// write buffer out if there is data to be written
//if (!comm.count) {
// call interrupt handler
//comm.iface.on_read(&comm.iface, USIDR, comm.iface.callback_data);
//}
USIDR = 0x02;
sei();
//USIDR = comm_read();
2019-02-09 21:33:45 +01:00
}
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 23:03:00 +01:00
adc_sample_t * channel_map[] = {
// update app state (remap channels)
&app.state.state.poti[2],
&app.state.state.poti[3],
&app.state.state.poti[1],
&app.state.state.poti[0],
&app.state.state.slider
};
2019-02-10 19:12:40 +01:00
// initalizes the application modules
2019-02-10 23:03:00 +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){
2019-02-10 23:03:00 +01:00
.ptr = channel_map,
.size = sizeof(channel_map),
2019-02-09 21:33:45 +01:00
}, 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 23:03:00 +01:00
#include <util/delay.h>
2019-02-10 19:12:40 +01:00
// little debug helper function
// FIXME: remove when not needed
2019-02-10 23:03:00 +01:00
void delay(uint16_t d)
{
while(d--)
{
_delay_ms(1);
2019-02-09 21:33:45 +01:00
}
}
2019-02-10 23:03:00 +01:00
int main()
{
2019-02-06 23:22:59 +01:00
init();
2019-02-10 23:03:00 +01:00
2019-02-09 21:33:45 +01:00
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
}