small changes

This commit is contained in:
Julian Daube 2019-02-10 23:03:00 +01:00 committed by Julian Daube
parent f7270fd437
commit f67475e8e4
4 changed files with 91 additions and 52 deletions

View File

@ -20,13 +20,6 @@ static inline void adc_next() {
if (!adc.current_channel) { if (!adc.current_channel) {
adc.current_sample = adc.buffer.ptr-1; adc.current_sample = adc.buffer.ptr-1;
// update app state (remap channels)
app.state.state.slider = adc.buffer.ptr[4];
app.state.state.poti[0] = adc.buffer.ptr[3];
app.state.state.poti[1] = adc.buffer.ptr[2];
app.state.state.poti[2] = adc.buffer.ptr[0];
app.state.state.poti[3] = adc.buffer.ptr[1];
} }
} while(!(adc.channel_mask & (1<<adc.current_channel))); } while(!(adc.channel_mask & (1<<adc.current_channel)));
@ -55,7 +48,7 @@ void adc_init(adc_buffer_t buffer, uint8_t mask) {
} }
ADMUX = 0; ADMUX = 0;
ADCSRA = (1<<ADATE) | (1<<ADEN) ; // enable adc, maximum prescaler value ADCSRA = (1<<ADATE) | (1<<ADEN) | 5 ; // enable adc, maximum prescaler value
} }
void adc_start() { void adc_start() {
@ -66,8 +59,9 @@ void adc_stop( ){
ADCSRA &= ~(1<<ADIE); ADCSRA &= ~(1<<ADIE);
} }
#include "led.h"
ISR(ADC_vect) { ISR(ADC_vect) {
*adc.current_sample = ADC; **adc.current_sample = ADC;
adc_next(); adc_next();
} }

View File

@ -13,7 +13,7 @@
typedef uint16_t adc_sample_t; typedef uint16_t adc_sample_t;
typedef struct { typedef struct {
adc_sample_t * ptr; adc_sample_t ** ptr;
uint8_t size; uint8_t size;
} adc_buffer_t; } adc_buffer_t;
@ -21,7 +21,7 @@ typedef struct adc {
adc_buffer_t buffer; adc_buffer_t buffer;
uint8_t current_channel; uint8_t current_channel;
adc_sample_t * current_sample; adc_sample_t ** current_sample;
uint8_t channel_mask; uint8_t channel_mask;
} adc_t; } adc_t;

View File

@ -13,35 +13,71 @@
#include <interface.h> #include <interface.h>
typedef struct { typedef struct {
byte_t * buffer_start, * buffer_end;
volatile byte_t * read;
volatile byte_t * write;
volatile byte_t count;
iface_t iface; iface_t iface;
} comm_t; } comm_t;
comm_t comm; comm_t comm;
void comm_write(iface_t * iface, byte_t byte) { void comm_write(iface_t * iface, byte_t byte) {
// disable interrupt (temporarily) if ((comm.write == comm.read) && comm.count) {
uint8_t temp = USICR; // write overflow
USICR &= ~(1<<USIOIE); return;
}
// write data register
USIDR = byte; *comm.write = byte;
++comm.count;
// wait for buffer overflow
while(!(USICR&(1<<USIOIE))) {} // advance write pointer
if ((++comm.write) >= comm.buffer_end) {
// restore control register comm.write = comm.buffer_start;
USICR = temp; }
} }
void comm_init() { 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;
}
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;
// TODO: setup pins // TODO: setup pins
DDRA |= (1<<PA5); // setup MOSI as output
// TODO: setup USART in synchronous mode // TODO: setup USART in synchronous mode
USICR = (1<<USIWM0)|(1<<USICS1); USICR = (1<<USIWM0)|(1<<USICS1);
// init interface // init interface
comm.iface.write = comm_write; comm.iface.write = comm_write;
comm.iface.on_read = 0;
} }
void comm_start() { void comm_start()
{
// don't start without interrupt handler // don't start without interrupt handler
if (!comm.iface.on_read) if (!comm.iface.on_read)
return; return;
@ -50,19 +86,18 @@ void comm_start() {
USICR |= (1<<USIOIE); USICR |= (1<<USIOIE);
} }
ISR(USI_OVF_vect) { ISR(USI_OVF_vect)
// call interrupt handler {
comm.iface.on_read(&comm.iface, USIBR, comm.iface.callback_data); cli();
} // write buffer out if there is data to be written
//if (!comm.count) {
void on_read(iface_t * sender, byte_t b) { // call interrupt handler
if (!IS_EOM(b)) { //comm.iface.on_read(&comm.iface, USIDR, comm.iface.callback_data);
sender->write(sender, b); //}
return;
} USIDR = 0x02;
sei();
// strip EOM message //USIDR = comm_read();
sender->write(sender, b&0x7F);
} }
#include "main.h" #include "main.h"
@ -70,8 +105,18 @@ void on_read(iface_t * sender, byte_t b) {
// the public instance of the app buffer // the public instance of the app buffer
struct app app = {}; struct app app = {};
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
};
// initalizes the application modules // initalizes the application modules
void init() { void init()
{
// init button module (making the button light up on press) // init button module (making the button light up on press)
// module also updates app.buffer.button for us // module also updates app.buffer.button for us
button_init(); button_init();
@ -80,8 +125,8 @@ void init() {
// will sample 0x8F (CHANNEL 8 and 0-3) // will sample 0x8F (CHANNEL 8 and 0-3)
// this module also updates app.buffer.poti and slider for us // this module also updates app.buffer.poti and slider for us
adc_init((adc_buffer_t){ adc_init((adc_buffer_t){
.ptr = app.adc_buffer, .ptr = channel_map,
.size = 5, .size = sizeof(channel_map),
}, 0x8F); }, 0x8F);
// init communication module // init communication module
@ -98,21 +143,22 @@ void init() {
sei(); sei();
} }
#include <util/delay.h>
// little debug helper function // little debug helper function
// FIXME: remove when not needed // FIXME: remove when not needed
void delay(uint16_t d) { void delay(uint16_t d)
while(d--) { {
__asm("nop"); while(d--)
{
_delay_ms(1);
} }
} }
int main() { int main()
{
init(); init();
// start relevant periphials
adc_start();
while(1) { while(1) {
// nothing to do here, the application is interrupt-driven // nothing to do here, the application is interrupt-driven
} }

View File

@ -8,7 +8,6 @@
struct app { struct app {
slave_t state; slave_t state;
adc_sample_t adc_buffer[5];
}; };
extern struct app app; extern struct app app;