diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/adc.c | 4 | ||||
-rw-r--r-- | src/can/mcp2515.c | 52 | ||||
-rw-r--r-- | src/can_mcp2515.c | 0 | ||||
-rw-r--r-- | src/ioport.c | 113 | ||||
-rw-r--r-- | src/pcint.c | 32 | ||||
-rw-r--r-- | src/sensors/dht22.c (renamed from src/sensor_dht22.c) | 2 | ||||
-rw-r--r-- | src/spi.c | 24 | ||||
-rw-r--r-- | src/tasks.c | 64 | ||||
-rw-r--r-- | src/timer.c | 130 | ||||
-rw-r--r-- | src/usart.c | 131 | ||||
-rw-r--r-- | src/usi_spi.c | 4 | ||||
-rw-r--r-- | src/utils/buffer.c | 70 | ||||
-rw-r--r-- | src/utils/narray.c | 36 |
13 files changed, 535 insertions, 127 deletions
diff --git a/src/adc.c b/src/adc.c new file mode 100644 index 0000000..03e979e --- /dev/null +++ b/src/adc.c @@ -0,0 +1,4 @@ +#include <adc.h> + +int8_t ioe_adc(uint8_t input) { +} diff --git a/src/can/mcp2515.c b/src/can/mcp2515.c new file mode 100644 index 0000000..8c9915c --- /dev/null +++ b/src/can/mcp2515.c @@ -0,0 +1,52 @@ +#include <can/mcp2515.h> + +#ifdef CONFIG_IOE_CAN_MCP2515 +// TODO check +#define CAN_MCP2515_RESET 0xC0 +#define CAN_MCP2515_READ 0x03 +#define CAN_MCP2515_READRX 0x90 +#define CAN_MCP2515_WRITE 0x02 +#define CAN_MCP2515_LOADTX 0x40 +#define CAN_MCP2515_RTS 0x80 +#define CAN_MCP2515_RDSTAT 0xA0 +#define CAN_MCP2515_RXSTAT 0xB0 +#define CAN_MCP2515_BITMOD 0x05 + +inline void can_mcp2515_init(canMcp2515 * can, uint8_t group, uint8_t mask) { + can->group = group; + can->mask = mask; + can_mcp2515_reset(can); + // TODO + return 0; +}; + +inline void can_mcp2515_reset(canMcp2515 * can) { + take_mutex(spi_mutex); + spi_join(); + + release_mutex(spi_mutex); +}; + +inline uint8_t can_mcp2515_read(canMcp2515 * can, uint8_t address) { +}; + +inline uint8_t can_mcp2515_readrx(canMcp2515 * can, uint8_t buffer) { +}; + +inline void can_mcp2515_write(canMcp2515 * can, uint8_t address, uint8_t data) { +}; + +inline void can_mcp2515_loadrx(canMcp2515 * can, uint8_t buffer, uint8_t data) { +}; + +inline uint8_t can_mcp2515_rdstat(canMcp2515 * can) { +}; + +inline uint8_t can_mcp2515_rxstat(canMcp2515 * can) { +}; + +inline void can_mcp2515_bitmod(canMcp2515 * can, uint8_t address, uint8_t mask, + uint8_t data) { +}; + +#endif /* CONFIG_IOE_CAN_MCP2515 */ diff --git a/src/can_mcp2515.c b/src/can_mcp2515.c deleted file mode 100644 index e69de29..0000000 --- a/src/can_mcp2515.c +++ /dev/null diff --git a/src/ioport.c b/src/ioport.c new file mode 100644 index 0000000..a77e564 --- /dev/null +++ b/src/ioport.c @@ -0,0 +1,113 @@ +#include <ioport.h> +#ifdef CONFIG_IOPORTS +#ifdef CONFIG_PCINT + +struct PCIW { + uint8_t flags; + uint8_t mask; + void (*change) (uint8_t group, uint8_t mask); +}; + +// Exploiting implementation of libc malloc +#define PCIW_LEN(INPCIW) (*((uint16_t *) INPCIW - 1) / sizeof(struct PCIW) - 1) + +#if (defined MCUSUPPORT_PCINT0 && defined MCUSUPPORT_PCINT1 && defined MCUSUPPORT_PCINT2) +#define PCI_COUNT 3 +#elif (defined MCUSUPPORT_PCINT0 && defined MCUSUPPORT_PCINT1 || \ + defined MCUSUPPORT_PCINT1 && defined MCUSUPPORT_PCINT2 || \ + defined MCUSUPPORT_PCINT2 && defined MCUSUPPORT_PCINT0) +#define PCI_COUNT 2 +#elif (defined MCUSUPPORT_PCINT0 || defined MCUSUPPORT_PCINT1 || defined MCUSUPPORT_PCINT2) +#define PCI_COUNT 1 +#else +#define PCI_COUNT 0 +#endif + +#define GROUP2INDEX(GRP) (GRP - MCUSUPPORT_PCINT0) + +static struct PCIW *pciw[PCI_COUNT]; +static uint8_t pci_state[PCI_COUNT]; +static void pci(int8_t group, int8_t pinmax) { + int8_t i, y; + int8_t index = GROUP2INDEX(group); + int8_t len = (int8_t) PCIW_LEN(pciw[index]); + // WARN Is possible that PCINT0 is not corresponding with B group and so on? + uint8_t state = IOE_IO_PIN(group); + for (i = len; i >= 0; i--) { + uint8_t stA = state & pciw[index][i].mask; + uint8_t stB = pci_state[index] & pciw[index][i].mask; + if (((~stA & stB) && pciw[index][i].flags & IOE_IO_RISING) || + ((stA & ~stB) && pciw[index][i].flags & IOE_IO_FALLING)) { + pciw[index][i].change(IOE_IO_B, mask); + } + } + pci_state[index] = state; +} + +void io_change_sethook(uint8_t group, uint8_t mask, uint8_t edge, + void (*change) (uint8_t group, uint8_t mask)) { + int8_t index = (int8_t) GROUP2INDEX(group); + int8_t len = (int8_t) PCIW_LEN(pciw[index]); + pciw[index] = realloc(pciw[index], len + 1); + pciw[index][len].flags = edge; + pciw[index][len].mask = mask; + pciw[index][len].change = change; + switch (group) { +#ifdef MCUSUPPORT_PCINT0 // TODO this wont work with attiny4313 and meaby others + case IO_B: + PCICR |= (1<<0); + PCMSK0 |= mask; + break; +#endif +#ifdef MCUSUPPORT_PCINT1 + case IO_C: + PCICR |= (1<<1); + PCMSK1 |= mask; + break; +#endif +#ifdef MCUSUPPORT_PCINT2 + case IO_D: + PCICR |= (1<<2); + PCMSK2 |= mask; + break +#endif + } +} + +void io_change_remhook(void (*change) (uint8_t group, uint8_t mask)) { + int8_t i, y; + int8_t len; + for (i = PCI_COUNT - 1; i >= 0; i--) { + len = (int8_t)PCIW_LEN(pciw[index]); + for (y = len - 1; y >= 0; y--) { + if (pciw[i][y].change == change) { + + } + } + } +} + +void io_change_clearhooks(void) { +} + +// WARN This will work only if C is only defined on MCU with also B defined. +#ifdef MCUSUPPORT_PCINT0 +ISR(PCINT0_vect, ISR_BLOCK) { + pci(IO_B, MCUSUPPORT_PCINT0); +} +#endif /* MCUSUPPORT_PCINT0 */ + +#ifdef MCUSUPPORT_PCINT1 +ISR(PCINT1_vect, ISR_BLOCK) { + pci(IO_C, MCUSUPPORT_PCINT1); +} +#endif /* MCUSUPPORT_PCINT1 */ + +#ifdef MCUSUPPORT_PCINT2 +ISR(PCINT2_vect, ISR_BLOCK) { + pci(IO_D, MCUSUPPORT_PCINT2); +} +#endif /* MCUSUPPORT_PCINT2 */ + +#endif /* CONFIG_PCINT */ +#endif /* CONFIG_IOPORTS */ diff --git a/src/pcint.c b/src/pcint.c deleted file mode 100644 index 1dedb63..0000000 --- a/src/pcint.c +++ /dev/null @@ -1,32 +0,0 @@ -/* This file defines and handles Pin change interupts. - * This interupt can be used in different modules and sensors. Function call can - * be done from this interrupt handdler. - */ -#include <avr/interrupt.h> - -#if (defined CONFIG_IOE_SENSOR_DHT22) - -#ifdef CONFIG_IOE_SENSOR_DHT22 -extern void dht22_read_pcint(void); -#endif - - -ISR(PCINT0_vect, ISR_BLOCK) { -#ifdef CONFIG_IOE_SENSOR_DHT22 - void dht22_read_pcint(); -#endif -} - -ISR(PCINT1_vect, ISR_BLOCK) { -#ifdef CONFIG_IOE_SENSOR_DHT22 - void dht22_read_pcint(); -#endif -} - -ISR(PCINT2_vect, ISR_BLOCK) { -#ifdef CONFIG_IOE_SENSOR_DHT22 - void dht22_read_pcint(); -#endif -} - -#endif diff --git a/src/sensor_dht22.c b/src/sensors/dht22.c index 7791060..9653ba5 100644 --- a/src/sensor_dht22.c +++ b/src/sensors/dht22.c @@ -1,4 +1,4 @@ -#include "../sensor_dht22.h" +#include <sensors/dht22.h> #ifdef CONFIG_IOE_SENSOR_DHT22 @@ -1,23 +1,19 @@ -#ifdef CONFIG_IOE_SPI -#ifdef MCUSUPPORT_USART +#include <spi.h> + +#ifdef CONFIG_SPI volatile int8_t _spi_busy; +volatile Mutex spi_mutex; -////// Interrupts //////////////////////////////// void (*spi_receive) (uint8_t data) = 0; -#ifdef CONFIG_IOE_CAN_MCP2515 -extern void can_mcp2515_spi_hook(uint8_t); -#endif /* CONFIG_IOE_CAN_MCP2515 */ - ISR(SPI_STC_vect, ISR_BLOCK) { + void (*spir_w) (uint8_t data) = spi_receive; _spi_busy = 0; -#ifdef CONFIG_IOE_CAN_MCP2515 - can_mcp2515_spi_hook(SPDR); -#endif /* CONFIG_IOE_CAN_MCP2515 */ - if (spi_receive) - spi_receive(SPDR); + while (spir_w != NULL) { + spir_w(SPDR); + spir_w++; + } } -#endif /* MCUSUPPORT_SPI */ -#endif /* CONFIG_IOE_SPI */ +#endif /* CONFIG_SPI */ diff --git a/src/tasks.c b/src/tasks.c index 1d94eaa..abc2add 100644 --- a/src/tasks.c +++ b/src/tasks.c @@ -1,16 +1,62 @@ -#include "../tasks.h" - +#include <tasks.h> #ifdef CONFIG_IOE_TASKS +void tasks_run(void) { +} + +void task_init(Task * t, void (*func) (void)) { +} +void task_start(Task * t) { +} + +void task_delay(uint16_t us) { +} +void task_delay_ms(uint16_t ms) { +} +void task_delay_till(volatile int8_t * boolean, uint16_t timeout) { +} + +int8_t task_stack_init(TaskStack * ts, uint8_t stack_size, uint8_t flags) { + ts->stack = malloc(stack_size * sizeof(uint8_t)); + ts->flags = flags; + ts->running = NULL; + ts->pending = NULL; +} + +void task_stack_free(TaskStack * ts) { + free(ts->stack); +} + +void task_stack_reg(TaskStack * ts, Task * t) { +} + +void task_stack_unreg(TaskStack * ts, Task * t) { +} + +void init_mutex(Mutex * mtx, uint8_t flags) { +}; +void take_mutex(Mutex * mtx) { +}; +int8_t take_mutex_t(Mutex * mtx, uint16_t timeout) { +}; +void give_mutex(Mutex * mtx) { +}; + #else /* CONFIG_IOE_TASKS */ -int8_t taskDelayTillT(int8_t *condition, uint8_t timeout_10us) { - while(*condition == 0) { - if (timeout_10us == 0) - return 1; - timeout_10us--; - _delay_us(10); - } +int8_t task_delay_till(volatile int8_t * boolean, uint16_t timeout) { + if (timeout == 0) + while (!*boolean) + _delay_us(100); + else + // TODO this implementation should be improved to check boolean as often + // as possible + while (*boolean == 0) { + if (timeout == 0) + return 1; + timeout--; + _delay_ms(1); + } return 0; } diff --git a/src/timer.c b/src/timer.c index 68c6c06..1175bad 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,7 +1,133 @@ -#include "../timer.h" - +#include <timer.h> #ifdef CONFIG_IOE_TIMERS +void timer_init(uint8_t timer, enum timerDivider div) { + switch (timer) { +#ifdef COUNTER0_PWM + case COUNTER0_PWM: + // Null counting register + TCNT0 = 0; + // Set interrupt flag + TIMSK0 = _BV(TOIE0); + // Set division and start counting + switch (div) { + case TIMER_DIVIDER_1: + TCCR0B = _BV(CS00); + break; + case TIMER_DIVIDER_8: + TCCR0B = _BV(CS01); + break; + case TIMER_DIVIDER_64: + TCCR0B = _BV(CS01) | _BV(CS00); + break; + case TIMER_DIVIDER_256: + TCCR0B = _BV(CS02); + break; + case TIMER_DIVIDER_1024: + TCCR0B = _BV(CS02) | _BV(CS01); + break; + default: + return; + } + break; +#endif /* COUNTER0_PWM */ +#ifdef COUNTER1_16PWM + case COUNTER1_16PWM: + break; +#endif /* COUNTER1_16PWM */ +#ifdef COUNTER2_PWM_ASYNC + case COUNTER2_PWM_ASYNC: + break; +#endif /* COUNTER2_PWM_ASYNC */ + } +} + +void timer_disable(uint8_t timer) { + switch (timer) { +#ifdef COUNTER0_PWM + case COUNTER0_PWM: + TCCR0B = 0; + break; +#endif /* COUNTER0_PWM */ +#ifdef COUNTER1_16PWM + case COUNTER1_16PWM: + break; +#endif /* COUNTER1_16PWM */ +#ifdef COUNTER2_PWM_ASYNC + case COUNTER2_PWM_ASYNC: + break; +#endif /* COUNTER2_PWM_ASYNC */ + } +} + +void timer_reset(uint8_t timer) { + switch (timer) { +#ifdef COUNTER0_PWM + case COUNTER0_PWM: + TCNT0 = 0; + break; +#endif /* COUNTER0_PWM */ +#ifdef COUNTER1_16PWM + case COUNTER1_16PWM: + break; +#endif /* COUNTER1_16PWM */ +#ifdef COUNTER2_PWM_ASYNC + case COUNTER2_PWM_ASYNC: + break; +#endif /* COUNTER2_PWM_ASYNC */ + } +} + +uint16_t timer_value(uint8_t timer) { + switch (timer) { +#ifdef COUNTER0_PWM + case COUNTER0_PWM: + return TCNT0; + break; +#endif /* COUNTER0_PWM */ +#ifdef COUNTER1_16PWM + case COUNTER1_16PWM: + break; +#endif /* COUNTER1_16PWM */ +#ifdef COUNTER2_PWM_ASYNC + case COUNTER2_PWM_ASYNC: + break; +#endif /* COUNTER2_PWM_ASYNC */ + default: + return 0; + } +} + +#ifdef COUNTER0_PWM +static void *(*timer_0_pwm_overflow) (void); +#endif /* COUNTER0_PWM */ +#ifdef COUNTER1_16PWM +static void (*timer_1_16pwm_overflow) (void); +#endif /* COUNTER1_16PWM */ +#ifdef COUNTER2_PWM_ASYNC +static void (*timer_2_pwm_overflow) (void); +#endif /* COUNTER2_PWM_ASYNC */ + +void timer_sethook(uint8_t timer, void (*fnc) (void)) { + switch (timer) { +#ifdef COUNTER0_PWM + case COUNTER0_PWM: + timer_0_pwm_overflow = fnc; + break; +#endif /* COUNTER0_PWM */ +#ifdef COUNTER1_16PWM + case COUNTER1_16PWM: + timer_1_16pwm_overflow = fnc; + break; +#endif /* COUNTER1_16PWM */ +#ifdef COUNTER2_PWM_ASYNC + case COUNTER2_PWM_ASYNC: + timer_2_pwm_overflow = fnc; + break; +#endif /* COUNTER2_PWM_ASYNC */ + } +} + #ifdef COUNTER0_PWM ISR(TIMER0_OVF_vect, ISR_BLOCK) { if (timer_0_pwm_overflow) diff --git a/src/usart.c b/src/usart.c index bba83ac..6cfbc20 100644 --- a/src/usart.c +++ b/src/usart.c @@ -1,7 +1,5 @@ -#include "../usart.h" - -#ifdef CONFIG_IOE_USART -#ifdef MCUSUPPORT_USART +#include <usart.h> +#ifdef CONFIG_USART #define USART_PARITY_NONE 0 #define USART_PARITY_ODD 1 @@ -10,46 +8,46 @@ #define USART_STOPBIT_SINGLE 1 #define USART_STOPBIT_DOUBLE 2 -#ifndef CONFIG_IOE_USART_BAUD -#warning "CONFIG_IOE_USART_BAUD not defined. Setting default 9600." -#define CONFIG_IOE_USART_BAUD 9600 +#ifndef CONFIG_USART_BAUD +#warning "CONFIG_USART_BAUD not defined. Setting default 9600." +#define CONFIG_USART_BAUD 9600 #endif -#ifndef CONFIG_IOE_USART_PARITY -#warning "CONFIG_IOE_USART_PARITY not defined. Using default USART_PARITY_NONE." -#define CONFIG_IOE_USART_PARITY USART_PARITY_NONE +#ifndef CONFIG_USART_PARITY +#warning "CONFIG_USART_PARITY not defined. Using default USART_PARITY_NONE." +#define CONFIG_USART_PARITY USART_PARITY_NONE #endif -#ifndef CONFIG_IOE_USART_STOPBIT -#warning "CONFIG_IOE_USART_STOPBIT not defined. Using default USART_STOPBIT_SINGLE." -#define CONFIG_IOE_USART_STOPBIT USART_STOPBIT_SINGLE +#ifndef CONFIG_USART_STOPBIT +#warning "CONFIG_USART_STOPBIT not defined. Using default USART_STOPBIT_SINGLE." +#define CONFIG_USART_STOPBIT USART_STOPBIT_SINGLE #endif -#ifndef CONFIG_IOE_USART_DATABITS -#warning "CONFIG_IOE_USART_DATABITS not defined. Using default 8." -#define CONFIG_IOE_USART_DATABITS 8 +#ifndef CONFIG_USART_DATABITS +#warning "CONFIG_USART_DATABITS not defined. Using default 8." +#define CONFIG_USART_DATABITS 8 #endif -#if !((CONFIG_IOE_USART_PARITY == USART_PARITY_NONE) || \ - (CONFIG_IOE_USART_PARITY == USART_PARITY_ODD) || \ - (CONFIG_IOE_USART_PARITY == USART_PARITY_EVEN)) -#error "CONFIG_IOE_USART_PARITY has value, that is not allowed." +#if !((CONFIG_USART_PARITY == USART_PARITY_NONE) || \ + (CONFIG_USART_PARITY == USART_PARITY_ODD) || \ + (CONFIG_USART_PARITY == USART_PARITY_EVEN)) +#error "CONFIG_USART_PARITY has value, that is not allowed." #endif -#if !((CONFIG_IOE_USART_STOPBIT == USART_STOPBIT_SINGLE) || \ - (CONFIG_IOE_USART_STOPBIT == USART_STOPBIT_DOUBLE)) -#error "CONFIG_IOE_USART_STOPBIT has value, that is not allowed." +#if !((CONFIG_USART_STOPBIT == USART_STOPBIT_SINGLE) || \ + (CONFIG_USART_STOPBIT == USART_STOPBIT_DOUBLE)) +#error "CONFIG_USART_STOPBIT has value, that is not allowed." #endif -#if !((CONFIG_IOE_USART_DATABITS == 5) || \ - (CONFIG_IOE_USART_DATABITS == 6) || \ - (CONFIG_IOE_USART_DATABITS == 7) || \ - (CONFIG_IOE_USART_DATABITS == 8)) +#if !((CONFIG_USART_DATABITS == 5) || \ + (CONFIG_USART_DATABITS == 6) || \ + (CONFIG_USART_DATABITS == 7) || \ + (CONFIG_USART_DATABITS == 8)) // TODO DATABITS 9 is not supported -#error "CONFIG_IOE_USART_DATABITS has value, that is not allowed." +#error "CONFIG_USART_DATABITS has value, that is not allowed." #endif -#if (defined CONFIG_IOE_USART_INFILE) && (CONFIG_IOE_USART_INBUFFER_SIZE <= 0) +#if (defined CONFIG_USART_INFILE) && (CONFIG_USART_INBUFFER_SIZE <= 0) #error "USART Input file can't be enabled without input buffer" #endif -#if (defined CONFIG_IOE_USART_OUTFILE) && (CONFIG_IOE_USART_OUTBUFFER_SIZE <= 0) +#if (defined CONFIG_USART_OUTFILE) && (CONFIG_USART_OUTBUFFER_SIZE <= 0) #error "USART Input file can't be enabled without output buffer" #endif @@ -58,7 +56,7 @@ volatile int8_t _usart_busy; void usart_init_async(void) { _usart_busy = 0; -#define BAUD CONFIG_IOE_USART_BAUD +#define BAUD CONFIG_USART_BAUD #include <util/setbaud.h> UBRR0H = UBRRH_VALUE; UBRR0L = UBRRL_VALUE; @@ -70,95 +68,95 @@ void usart_init_async(void) { UCSR0C = 0 | // USART_PARITY_NONE are both UMP01 and UMP00 zero -#if CONFIG_IOE_USART_PARITY == USART_PARITY_ODD +#if CONFIG_USART_PARITY == USART_PARITY_ODD _BV(UPM01) | -#elif CONFIG_IOE_USART_PARITY == USART_PARITY_EVEN +#elif CONFIG_USART_PARITY == USART_PARITY_EVEN _BV(UPM00) | _BV(UPM01) | #endif // USART_STOPBIT_SINGLE is USBS0 zero -#if CONFIG_IOE_USART_STOPBIT == USART_STOPBIT_DOUBLE +#if CONFIG_USART_STOPBIT == USART_STOPBIT_DOUBLE _BV(USBS0) | #endif // For 5 databits are UCSZ00 and UCSZ01 zero -#if CONFIG_IOE_USART_DATABITS == 6 +#if CONFIG_USART_DATABITS == 6 _BV(UCSZ00) -#elif CONFIG_IOE_USART_DATABITS == 7 +#elif CONFIG_USART_DATABITS == 7 _BV(UCSZ01) -#elif CONFIG_IOE_USART_DATABITS == 8 +#elif CONFIG_USART_DATABITS == 8 _BV(UCSZ00) | _BV(UCSZ01) #endif ; // Enable receiver, transmitter and RX complete, // Data register empty interrupts UCSR0B = _BV(RXEN0) | _BV(TXEN0) | _BV(RXCIE0) | _BV(UDRIE0); -#ifdef _IOE_USART_INBUFFER - IOEBUFFER_INIT(_ioe_usart_inbuffer, CONFIG_IOE_USART_INBUFFER_SIZE); +#ifdef _USART_INBUFFER + IOEBUFFER_INIT(_ioe_usart_inbuffer, CONFIG_USART_INBUFFER_SIZE); #endif -#ifdef _IOE_USART_OUTBUFFER - IOEBUFFER_INIT(_ioe_usart_outbuffer, CONFIG_IOE_USART_OUTBUFFER_SIZE); +#ifdef _USART_OUTBUFFER + IOEBUFFER_INIT(_ioe_usart_outbuffer, CONFIG_USART_OUTBUFFER_SIZE); #endif } inline void usart_send(uint8_t data) { -#ifdef _IOE_USART_OUTBUFFER +#ifdef _USART_OUTBUFFER if (!_usart_busy) { _usart_busy = 1; UDR0 = data; } else { IOEBUFFER_PUT(_ioe_usart_outbuffer, - CONFIG_IOE_USART_OUTBUFFER_SIZE, data, - CONFIG_IOE_USART_OUTBUFFER_MODE); + CONFIG_USART_OUTBUFFER_SIZE, data, + CONFIG_USART_OUTBUFFER_MODE); } #else _usart_busy = 1; UDR0 = data; -#endif /* _IOE_USART_OUTBUFFER */ +#endif /* _USART_OUTBUFFER */ } -#ifdef _IOE_USART_OUTBUFFER +#ifdef _USART_OUTBUFFER inline void usart_send_str(char *str) { while (*str != '\0') { usart_send((uint8_t) * str); str++; } } -#endif /* _IOE_USART_OUTBUFFER */ +#endif /* _USART_OUTBUFFER */ -#ifdef _IOE_USART_INBUFFER +#ifdef _USART_INBUFFER uint8_t usart_get(void) { uint8_t rtn; - IOEBUFFER_GET(_ioe_usart_inbuffer, CONFIG_IOE_USART_INBUFFER_SIZE, + IOEBUFFER_GET(_ioe_usart_inbuffer, CONFIG_USART_INBUFFER_SIZE, rtn); return rtn; } #endif -#ifdef _IOE_USART_INBUFFER +#ifdef _USART_INBUFFER uint8_t usart_inbuffered(void) { uint8_t rtn; - IOEBUFFER_CNT(_ioe_usart_inbuffer, CONFIG_IOE_USART_INBUFFER_SIZE, + IOEBUFFER_CNT(_ioe_usart_inbuffer, CONFIG_USART_INBUFFER_SIZE, rtn); return rtn; } #endif -#ifdef _IOE_USART_OUTBUFFER +#ifdef _USART_OUTBUFFER uint8_t usart_outbuffered(void) { uint8_t rtn; - IOEBUFFER_CNT(_ioe_usart_outbuffer, CONFIG_IOE_USART_OUTBUFFER_SIZE, + IOEBUFFER_CNT(_ioe_usart_outbuffer, CONFIG_USART_OUTBUFFER_SIZE, rtn); return rtn; } #endif -#ifdef CONFIG_IOE_USART_OUTFILE +#ifdef CONFIG_USART_OUTFILE static int usartput(char c, FILE * f) { usart_send((uint8_t) c); return 0; } #endif -#ifdef CONFIG_IOE_USART_INBUFFER +#ifdef CONFIG_USART_INBUFFER static int usartget(FILE * f) { uint8_t v; while (!(v = usart_get())); @@ -166,11 +164,11 @@ static int usartget(FILE * f) { } #endif -#if (defined CONFIG_IOE_USART_INFILE) || (defined CONFIG_IOE_USART_OUTFILE) +#if (defined CONFIG_USART_INFILE) || (defined CONFIG_USART_OUTFILE) FILE *usart_async_open(void) { usart_init_async(); -#ifdef CONFIG_IOE_USART_OUTFILE -#ifdef CONFIG_IOE_USART_INFILE +#ifdef CONFIG_USART_OUTFILE +#ifdef CONFIG_USART_INFILE return fdevopen(usartput, usartget); #else return fdevopen(usartput, 0); @@ -186,19 +184,19 @@ void (*usart_receive) (uint8_t data) = 0; void (*usart_sent) (void) = 0; SIGNAL(USART_RX_vect) { -#ifdef _IOE_USART_INBUFFER +#ifdef _USART_INBUFFER uint8_t val = UDR0; - IOEBUFFER_PUT(_ioe_usart_inbuffer, CONFIG_IOE_USART_INBUFFER_SIZE, - val, CONFIG_IOE_USART_INBUFFER_MODE); -#endif /* _IOE_USART_INBUFFER */ + IOEBUFFER_PUT(_ioe_usart_inbuffer, CONFIG_USART_INBUFFER_SIZE, + val, CONFIG_USART_INBUFFER_MODE); +#endif /* _USART_INBUFFER */ if (usart_receive) usart_receive(UDR0); } SIGNAL(USART_UDRE_vect) { -#ifdef _IOE_USART_OUTBUFFER +#ifdef _USART_OUTBUFFER uint8_t val; - IOEBUFFER_GET(_ioe_usart_outbuffer, CONFIG_IOE_USART_OUTBUFFER_SIZE, + IOEBUFFER_GET(_ioe_usart_outbuffer, CONFIG_USART_OUTBUFFER_SIZE, val); if (val) UDR0 = val; @@ -206,10 +204,9 @@ SIGNAL(USART_UDRE_vect) { _usart_busy = 0; #else _usart_busy = 0; -#endif /* _IOE_USART_OUTBUFFER */ +#endif /* _USART_OUTBUFFER */ if (usart_sent) usart_sent(); } -#endif /* MCUSUPPORT_USART */ -#endif /* CONFIG_IOE_USART */ +#endif /* CONFIG_USART */ diff --git a/src/usi_spi.c b/src/usi_spi.c index 01c37f7..3291721 100644 --- a/src/usi_spi.c +++ b/src/usi_spi.c @@ -1,5 +1,5 @@ -#include "../usi_spi.h" -#include "../mcu/mcu_def.h" +#include <usi_spi.h> +#include <mcu/mcu_def.h> inline void usi_spi_init(enum spiMode mode) { if (mode == USI_SPI_MODE_MASTER) { diff --git a/src/utils/buffer.c b/src/utils/buffer.c new file mode 100644 index 0000000..3a5c15e --- /dev/null +++ b/src/utils/buffer.c @@ -0,0 +1,70 @@ +#include <utils/buffer.h> + +#ifdef CONFIG_IOE_BUFFER + +int8_t ioebuffer_init(IOEBuffer * buf, uint8_t size, uint8_t flags) { + buf->windex = 0; + buf->rindex = 0; + buf->size = size; + buf->flags = flags; + buf->data = malloc(size * sizeof(void *)); +} + +void ioebuffer_uninit(IOEBuffer * buf) { + free(buf->data); +} + +int8_t ioebuffer_put(IOEBuffer * buf, void *data) { + uint8_t mode = buf->flags & 0x3; + if (mode == IOEBUFFER_F_MODE_BLOCK) { + if (bud->windex == 0) { + while (bud->rindex == size - 1); + } else { + while (bud->rindex == bud->windex - 1); + } + } + bud->data[bud->windex] = data; + if (mode != IOEBUFFER_F_MODE_DROP || + (bud->windex == 0 && bud->rindex == size - 1) || + (bud->rindex + 1 == bud->windex)) { + if (bud->windex == 0) + bud->windex = size - 1; + else + bud->windex--; + } + if (mode == IOEBUFFER_F_MODE_OVERWRITE && bud->windex == bud->rindex) { + if (bud->windex == size - 1) + bud->windex = 0; + else + bud->windex++; + } else + return -1; + return 0; +} + +int8_t ioebuffer_get(IOEBuffer * buf, void **data) { + if (buf->rindex != buf->windex) { + *data = buf->data[buf->rindex]; + if (buf->rindex == 0) + buf->rindex = buf->size - 1; + else + buf->rindex--; + } else + *data = NULL; +} + +void ioebuffer_clean(IOEBuffer * buf) { + buf->windex = 0; + bud->rindex = 0; +} + +uint8_t ioebuffer_cnt(IOEBuffer * buf) { + if (buf->windex < buf->rindex) + return buf->rindex - buf->windex; + else if (buf->windex > buf->rindex) + return buf->size - buf->windex + buf->rindex; + else + return 0; +} + +#endif /* CONFIG_IOE_BUFFER */ diff --git a/src/utils/narray.c b/src/utils/narray.c new file mode 100644 index 0000000..f2cf0ed --- /dev/null +++ b/src/utils/narray.c @@ -0,0 +1,36 @@ +#include <utils/narray.h> + +#ifdef CONFIG_IOE_NARRAY + +void narray_add(void ***array, void *data) { + if (*array == NULL) { + *array = malloc(2 * sizeof(void *)); + (*array)[0] = data; + (*array)[1] = NULL; + } else { + uint8_t size = narray_size(array); + *array = realloc(*array, (size + 2) * sizeof(void *)); + (*array)[size] = data; + (*array)[size + 1] = NULL; + } +} + +void narray_remove(void ***array, void *data) { + uint8_t i = 0; + while ((*array)[i] != NULL || (*array)[i] != data) + i++; + while ((*array)[i + 1] != NULL) { + (*array)[i] = (*array)[i + 1]; + i++; + } + (*array)[i] = NULL; + *array = realloc(*array, (i + 1) * sizeof(void *)); +} + +uint8_t narray_size(void ***array) { + uint8_t size = 0; + while ((*array)[size++] != NULL); + return size; +} + +#endif /* CONFIG_IOE_NARRAY */ |