small changes
This commit is contained in:
		
							parent
							
								
									f7270fd437
								
							
						
					
					
						commit
						f67475e8e4
					
				@ -20,13 +20,6 @@ static inline void adc_next() {
 | 
			
		||||
 | 
			
		||||
		if (!adc.current_channel) {
 | 
			
		||||
			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)));
 | 
			
		||||
 | 
			
		||||
@ -55,7 +48,7 @@ void adc_init(adc_buffer_t buffer, uint8_t mask) {
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	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() {
 | 
			
		||||
@ -66,8 +59,9 @@ void adc_stop( ){
 | 
			
		||||
	ADCSRA &= ~(1<<ADIE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "led.h"
 | 
			
		||||
ISR(ADC_vect) {
 | 
			
		||||
	*adc.current_sample = ADC;
 | 
			
		||||
	**adc.current_sample = ADC;
 | 
			
		||||
	adc_next();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -13,7 +13,7 @@
 | 
			
		||||
typedef uint16_t adc_sample_t;
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
	adc_sample_t * ptr;
 | 
			
		||||
	adc_sample_t ** ptr;
 | 
			
		||||
	uint8_t size;
 | 
			
		||||
} adc_buffer_t;
 | 
			
		||||
 | 
			
		||||
@ -21,7 +21,7 @@ typedef struct adc {
 | 
			
		||||
	adc_buffer_t buffer;
 | 
			
		||||
 | 
			
		||||
	uint8_t current_channel;
 | 
			
		||||
	adc_sample_t * current_sample;
 | 
			
		||||
	adc_sample_t ** current_sample;
 | 
			
		||||
	uint8_t channel_mask;
 | 
			
		||||
} adc_t;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -13,35 +13,71 @@
 | 
			
		||||
#include <interface.h>
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
	byte_t * buffer_start, * buffer_end;
 | 
			
		||||
	
 | 
			
		||||
	volatile byte_t * read;
 | 
			
		||||
	volatile byte_t * write;
 | 
			
		||||
	volatile byte_t count; 
 | 
			
		||||
	
 | 
			
		||||
	iface_t iface;
 | 
			
		||||
} comm_t;
 | 
			
		||||
 | 
			
		||||
comm_t  comm;
 | 
			
		||||
comm_t comm;
 | 
			
		||||
 | 
			
		||||
void comm_write(iface_t * iface, byte_t byte) {
 | 
			
		||||
	// disable interrupt (temporarily)
 | 
			
		||||
	uint8_t temp = USICR;
 | 
			
		||||
	USICR &= ~(1<<USIOIE);
 | 
			
		||||
	if ((comm.write == comm.read) && comm.count) {
 | 
			
		||||
		// write overflow
 | 
			
		||||
		return;
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
	// write data register
 | 
			
		||||
	USIDR = byte;
 | 
			
		||||
	*comm.write = byte;
 | 
			
		||||
	++comm.count;
 | 
			
		||||
	
 | 
			
		||||
	// wait for buffer overflow
 | 
			
		||||
	while(!(USICR&(1<<USIOIE))) {}
 | 
			
		||||
 | 
			
		||||
	// restore control register
 | 
			
		||||
	USICR = temp;	
 | 
			
		||||
	// advance write pointer
 | 
			
		||||
	if ((++comm.write) >= comm.buffer_end) {
 | 
			
		||||
		comm.write = comm.buffer_start;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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
 | 
			
		||||
	DDRA |= (1<<PA5); // setup MOSI as output
 | 
			
		||||
	
 | 
			
		||||
	// TODO: setup USART in synchronous mode
 | 
			
		||||
	USICR = (1<<USIWM0)|(1<<USICS1);
 | 
			
		||||
 | 
			
		||||
	// 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
 | 
			
		||||
	if (!comm.iface.on_read)
 | 
			
		||||
		return;
 | 
			
		||||
@ -50,19 +86,18 @@ void comm_start() {
 | 
			
		||||
	USICR |= (1<<USIOIE);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
ISR(USI_OVF_vect) {
 | 
			
		||||
	// call interrupt handler
 | 
			
		||||
	comm.iface.on_read(&comm.iface, USIBR, comm.iface.callback_data);
 | 
			
		||||
}
 | 
			
		||||
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);
 | 
			
		||||
	//}
 | 
			
		||||
	
 | 
			
		||||
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);
 | 
			
		||||
	USIDR = 0x02;
 | 
			
		||||
	sei();
 | 
			
		||||
	//USIDR = comm_read();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include "main.h"
 | 
			
		||||
@ -70,8 +105,18 @@ void on_read(iface_t * sender, byte_t b) {
 | 
			
		||||
// the public instance of the app buffer
 | 
			
		||||
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
 | 
			
		||||
void init() {
 | 
			
		||||
void init() 
 | 
			
		||||
{
 | 
			
		||||
	// init button module (making the button light up on press)
 | 
			
		||||
	// module also updates app.buffer.button for us
 | 
			
		||||
	button_init();
 | 
			
		||||
@ -80,8 +125,8 @@ void init() {
 | 
			
		||||
	// will sample 0x8F (CHANNEL 8 and 0-3) 
 | 
			
		||||
	// this module also updates app.buffer.poti and slider for us
 | 
			
		||||
	adc_init((adc_buffer_t){
 | 
			
		||||
		.ptr  = app.adc_buffer,
 | 
			
		||||
		.size = 5,
 | 
			
		||||
		.ptr  = channel_map,
 | 
			
		||||
		.size = sizeof(channel_map),
 | 
			
		||||
	}, 0x8F);
 | 
			
		||||
 | 
			
		||||
	// init communication module
 | 
			
		||||
@ -98,21 +143,22 @@ void init() {
 | 
			
		||||
	sei();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#include <util/delay.h>
 | 
			
		||||
 | 
			
		||||
// little debug helper function
 | 
			
		||||
// FIXME: remove when not needed
 | 
			
		||||
void delay(uint16_t d) {
 | 
			
		||||
	while(d--) {
 | 
			
		||||
		__asm("nop");
 | 
			
		||||
void delay(uint16_t d) 
 | 
			
		||||
{
 | 
			
		||||
	while(d--) 
 | 
			
		||||
	{
 | 
			
		||||
		_delay_ms(1);
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main() {
 | 
			
		||||
int main() 
 | 
			
		||||
{
 | 
			
		||||
	init();
 | 
			
		||||
	
 | 
			
		||||
	// start relevant periphials
 | 
			
		||||
	adc_start();
 | 
			
		||||
 | 
			
		||||
	while(1) {
 | 
			
		||||
		// nothing to do here, the application is interrupt-driven
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
@ -8,7 +8,6 @@
 | 
			
		||||
 | 
			
		||||
struct app {
 | 
			
		||||
	slave_t state;
 | 
			
		||||
	adc_sample_t adc_buffer[5];
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
extern struct app app;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user