working with leds now

This commit is contained in:
Julian Daube 2019-02-13 15:18:38 +01:00 committed by Julian Daube
parent 8ea404c3fa
commit b9a402d64b
8 changed files with 62 additions and 40 deletions

View File

@ -7,5 +7,5 @@ project(slider_firmware C)
add_subdirectory(SliderCommunication) add_subdirectory(SliderCommunication)
add_avr_executable(slider_firmware led.c main.c adc.c button.c) add_avr_executable(slider_firmware led.c main.c adc.c button.c comm.c)
target_link_libraries(slider_firmware DSPLAB_SliderCommunication) target_link_libraries(slider_firmware DSPLAB_SliderCommunication)

View File

@ -16,21 +16,9 @@ adc_t adc;
/* adc driver */ /* adc driver */
void adc_init(uint8_t * channels) { void adc_init(uint8_t * channels) {
adc.current_channel = 0; // adc.current_channel = 0;
adc.channel_map = channels; // adc.channel_map = channels;
ADMUX = channels[0]; ADMUX = channels[0];
ADCSRA = (1<<ADATE) | (1<<ADEN) | 7; // enable adc, maximum prescaler value ADCSRA = /*(1<<ADATE) | */ (1<<ADEN); // enable adc, maximum prescaler value
} }
ISR(ADC_vect) {
adc.buffer[adc.current_channel] = ADC;
adc.current_channel++;
if (adc.current_channel == ADC_BUFFER_SIZE) {
adc.current_channel = 0;
}
ADMUX = adc.channel_map[adc.current_channel];
}

View File

@ -19,10 +19,11 @@ typedef struct {
} adc_buffer_t; } adc_buffer_t;
typedef struct adc { typedef struct adc {
adc_sample_t buffer[ADC_BUFFER_SIZE]; //adc_sample_t buffer[ADC_BUFFER_SIZE];
volatile uint8_t current_channel; /*volatile uint8_t current_channel;
uint8_t * channel_map; uint8_t * channel_map;
*/
} adc_t; } adc_t;
// the adc instance // the adc instance
@ -30,7 +31,9 @@ extern adc_t adc;
void adc_init(uint8_t * channels); void adc_init(uint8_t * channels);
#define adc_start() ADCSRA |= (1<<ADIE) | (1<<ADSC) #define adc_start() do { ADCSRA |= (1<<ADSC); } while(0)
#define adc_stop() ADCSRA &= ~(1<<ADIE) #define adc_finished() (ADCSRA & (1<<ADIF))
#define adc_set_channel(channel) do { ADMUX = channel; } while(0)
#define adc_value() ADC
#endif /* ADC_H_ */ #endif /* ADC_H_ */

View File

@ -25,7 +25,7 @@ button_state_t button_is_pressed()
ISR(PCINT1_vect) { ISR(PCINT1_vect) {
if(!(BUTTON_REG&(1<<BUTTON_PIN))) { if(!(BUTTON_REG&(1<<BUTTON_PIN))) {
app.state.state.button = !app.state.state.button; app.button = !app.button;
led_toggle(); led_toggle();
} }
} }

14
firmware/slider/comm.c Normal file
View File

@ -0,0 +1,14 @@
/*
* comm.c
*
* Created on: 13.02.2019
* Author: julian
*/
#include <avr/io.h>
void comm_init()
{
DDRA |= (1<<PA5); // setup MOSI as output
USICR = (1<<USIWM0)|(1<<USICS1);
}

13
firmware/slider/comm.h Normal file
View File

@ -0,0 +1,13 @@
/*
* comm.h
*
* Created on: 13.02.2019
* Author: julian
*/
#ifndef COMM_H_
#define COMM_H_
void comm_init();
#endif /* COMM_H_ */

View File

@ -4,6 +4,7 @@
#include "led.h" #include "led.h"
#include "button.h" #include "button.h"
#include "main.h" #include "main.h"
#include "comm.h"
/* Communication driver */ /* Communication driver */
#define SLAVE_ENABLE_DDR DDRA #define SLAVE_ENABLE_DDR DDRA
@ -12,16 +13,12 @@
#include <interface.h> #include <interface.h>
#include "main.h"
void comm_init();
// the public instance of the app buffer // the public instance of the app buffer
struct app app = {}; struct app app = {};
uint8_t channel_map[ADC_BUFFER_SIZE] = { uint8_t channel_map[ADC_BUFFER_SIZE] = {
0,
1, 1,
0,
2, 2,
3, 3,
7 7
@ -49,12 +46,8 @@ void init()
sei(); sei();
} }
void comm_init()
{
DDRA |= (1<<PA5); // setup MOSI as output
USICR = (1<<USIWM0)|(1<<USICS1);
}
// All states of the transaction state machine
volatile enum { volatile enum {
COMM_WAIT = 0, COMM_WAIT = 0,
COMM_SEND_START , COMM_SEND_START ,
@ -69,25 +62,33 @@ static inline void comm_service()
if (USISR & (1<<USIOIF)) if (USISR & (1<<USIOIF))
{ {
led_on();
cli(); cli();
switch(comm_state) switch(comm_state)
{ {
case COMM_WAIT: case COMM_WAIT:
// only trigger on start of transmission
if (IS_EOM(USIDR)) { if (IS_EOM(USIDR)) {
// pass package through
USIDR &= ~0x80; USIDR &= ~0x80;
comm_state = COMM_SEND_START; comm_state = COMM_SEND_START;
adc_stop();
// sample first channel
adc_start();
} }
break; break;
case COMM_SEND_START: case COMM_SEND_START:
temp = adc.buffer[count]; // transmit LSByte of value
temp = adc_value();
// sample next channel
adc_set_channel(channel_map[count+1]);
adc_start();
USIDR = temp & 0x7F; USIDR = temp & 0x7F;
comm_state = COMM_SEND_WAIT; comm_state = COMM_SEND_WAIT;
break; break;
case COMM_SEND_WAIT: case COMM_SEND_WAIT:
// transmit MSByte
USIDR = (temp>>7)&0x7F; USIDR = (temp>>7)&0x7F;
if (++count == ADC_BUFFER_SIZE) { if (++count == ADC_BUFFER_SIZE) {
@ -97,20 +98,23 @@ static inline void comm_service()
} }
break; break;
case COMM_SEND_END: case COMM_SEND_END:
// cleanup the transaction
count = 0; count = 0;
USIDR = app.state.state.button | 0x80; // transmit the button state and end of transaction
USIDR = app.button | 0x80;
default: default:
comm_state = COMM_WAIT; comm_state = COMM_WAIT;
adc_start(); // reset channel of adc back to 0
adc_set_channel(channel_map[0]);
break; break;
} }
USISR |= (1<<USIOIF); USISR |= (1<<USIOIF);
sei(); sei();
led_off();
} }
} }
#include <util/delay.h> #include <util/delay.h>
// little debug helper function // little debug helper function

View File

@ -7,9 +7,9 @@
#include <slave/slave.h> #include <slave/slave.h>
struct app { struct app {
slave_t state; volatile uint8_t button;
}; };
extern struct app app; extern struct app app;
#endif #endif