diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/error.c | 40 | ||||
-rw-r--r-- | src/ioport.c | 105 | ||||
-rw-r--r-- | src/timer.c | 156 | ||||
-rw-r--r-- | src/utils/buffer.c | 103 |
4 files changed, 227 insertions, 177 deletions
diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..1c75382 --- /dev/null +++ b/src/error.c @@ -0,0 +1,40 @@ +#include <error.h> +#ifdef CONFIG_ERRORS + +#ifdef CONFIG_EH_LED +#include "ioport.h" +#endif /* CONFIG_EH_LED */ + +#ifdef CONFIG_ERROR_MESSAGES +void _error(enum ErrorCodes ec, const char *msg) { +#ifdef CONFIG_ERROR_CALLBACK + error_callback(ec, msg); +#endif +#else /* CONFIG_ERROR_MESSAGES */ +void _error(enum ErrorCodes ec) { +#ifdef CONFIG_ERROR_CALLBACK + error_callback(ec); +#endif +#endif /* CONFIG_ERROR_MESSAGES */ + + // OK error code means no error. + if (ec == EC_OK) + return; + +#ifdef CONFIG_EH_LED + // Set led + io_setout(CONFIG_EH_LED_IOPIN); + io_hight(CONFIG_EH_LED_IOPIN); +#endif /* CONFIG_EH_LED */ + +#ifdef CONFIG_EH_HANGS + // TODO +#endif /* CONFIG_EH_HANGS */ + +#ifdef CONFIG_EH_RESTART + // TODO +#endif /* CONFIG_EH_RESTART */ +} + + +#endif /* CONFIG_ERRORS */ diff --git a/src/ioport.c b/src/ioport.c index a77e564..3cc9406 100644 --- a/src/ioport.c +++ b/src/ioport.c @@ -3,9 +3,9 @@ #ifdef CONFIG_PCINT struct PCIW { - uint8_t flags; - uint8_t mask; - void (*change) (uint8_t group, uint8_t mask); + uint8_t flags; + uint8_t mask; + void (*change) (uint8_t group, uint8_t mask); }; // Exploiting implementation of libc malloc @@ -14,8 +14,8 @@ struct PCIW { #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) + 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 @@ -28,84 +28,85 @@ struct PCIW { 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; + 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) { + 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; + case IO_B: + PCICR |= (1<<0); + PCMSK0 |= mask; + break; #endif #ifdef MCUSUPPORT_PCINT1 - case IO_C: - PCICR |= (1<<1); - PCMSK1 |= mask; - break; + case IO_C: + PCICR |= (1<<1); + PCMSK1 |= mask; + break; #endif #ifdef MCUSUPPORT_PCINT2 - case IO_D: - PCICR |= (1<<2); - PCMSK2 |= mask; - break + 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) { + 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) { + // TODO } // 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); + pci(IO_B, MCUSUPPORT_PCINT0); } #endif /* MCUSUPPORT_PCINT0 */ #ifdef MCUSUPPORT_PCINT1 ISR(PCINT1_vect, ISR_BLOCK) { - pci(IO_C, MCUSUPPORT_PCINT1); + pci(IO_C, MCUSUPPORT_PCINT1); } #endif /* MCUSUPPORT_PCINT1 */ #ifdef MCUSUPPORT_PCINT2 ISR(PCINT2_vect, ISR_BLOCK) { - pci(IO_D, MCUSUPPORT_PCINT2); + pci(IO_D, MCUSUPPORT_PCINT2); } #endif /* MCUSUPPORT_PCINT2 */ diff --git a/src/timer.c b/src/timer.c index 1175bad..967a2fa 100644 --- a/src/timer.c +++ b/src/timer.c @@ -2,100 +2,100 @@ #ifdef CONFIG_IOE_TIMERS void timer_init(uint8_t timer, enum timerDivider div) { - switch (timer) { + 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; + 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; + case COUNTER1_16PWM: + break; #endif /* COUNTER1_16PWM */ #ifdef COUNTER2_PWM_ASYNC - case COUNTER2_PWM_ASYNC: - break; + case COUNTER2_PWM_ASYNC: + break; #endif /* COUNTER2_PWM_ASYNC */ - } + } } void timer_disable(uint8_t timer) { - switch (timer) { + switch (timer) { #ifdef COUNTER0_PWM - case COUNTER0_PWM: - TCCR0B = 0; - break; + case COUNTER0_PWM: + TCCR0B = 0; + break; #endif /* COUNTER0_PWM */ #ifdef COUNTER1_16PWM - case COUNTER1_16PWM: - break; + case COUNTER1_16PWM: + break; #endif /* COUNTER1_16PWM */ #ifdef COUNTER2_PWM_ASYNC - case COUNTER2_PWM_ASYNC: - break; + case COUNTER2_PWM_ASYNC: + break; #endif /* COUNTER2_PWM_ASYNC */ - } + } } void timer_reset(uint8_t timer) { - switch (timer) { + switch (timer) { #ifdef COUNTER0_PWM - case COUNTER0_PWM: - TCNT0 = 0; - break; + case COUNTER0_PWM: + TCNT0 = 0; + break; #endif /* COUNTER0_PWM */ #ifdef COUNTER1_16PWM - case COUNTER1_16PWM: - break; + case COUNTER1_16PWM: + break; #endif /* COUNTER1_16PWM */ #ifdef COUNTER2_PWM_ASYNC - case COUNTER2_PWM_ASYNC: - break; + case COUNTER2_PWM_ASYNC: + break; #endif /* COUNTER2_PWM_ASYNC */ - } + } } uint16_t timer_value(uint8_t timer) { - switch (timer) { + switch (timer) { #ifdef COUNTER0_PWM - case COUNTER0_PWM: - return TCNT0; - break; + case COUNTER0_PWM: + return TCNT0; + break; #endif /* COUNTER0_PWM */ #ifdef COUNTER1_16PWM - case COUNTER1_16PWM: - break; + case COUNTER1_16PWM: + break; #endif /* COUNTER1_16PWM */ #ifdef COUNTER2_PWM_ASYNC - case COUNTER2_PWM_ASYNC: - break; + case COUNTER2_PWM_ASYNC: + break; #endif /* COUNTER2_PWM_ASYNC */ - default: - return 0; - } + default: + return 0; + } } #ifdef COUNTER0_PWM @@ -109,43 +109,43 @@ static void (*timer_2_pwm_overflow) (void); #endif /* COUNTER2_PWM_ASYNC */ void timer_sethook(uint8_t timer, void (*fnc) (void)) { - switch (timer) { + switch (timer) { #ifdef COUNTER0_PWM - case COUNTER0_PWM: - timer_0_pwm_overflow = fnc; - break; + case COUNTER0_PWM: + timer_0_pwm_overflow = fnc; + break; #endif /* COUNTER0_PWM */ #ifdef COUNTER1_16PWM - case COUNTER1_16PWM: - timer_1_16pwm_overflow = fnc; - break; + 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; + 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) - timer_0_pwm_overflow(); + if (timer_0_pwm_overflow) + timer_0_pwm_overflow(); } #endif /* COUNTER0_PWM */ #ifdef COUNTER0_PWM ISR(TIMER1_OVF_vect, ISR_BLOCK) { - if (timer_1_16pwm_overflow) - timer_1_16pwm_overflow(); + if (timer_1_16pwm_overflow) + timer_1_16pwm_overflow(); } #endif /* COUNTER0_PWM */ #ifdef COUNTER0_PWM ISR(TIMER2_OVF_vect, ISR_BLOCK) { - if (timer_2_pwm_overflow) - timer_2_pwm_overflow(); + if (timer_2_pwm_overflow) + timer_2_pwm_overflow(); } #endif /* COUNTER0_PWM */ diff --git a/src/utils/buffer.c b/src/utils/buffer.c index 3a5c15e..644a599 100644 --- a/src/utils/buffer.c +++ b/src/utils/buffer.c @@ -1,70 +1,79 @@ #include <utils/buffer.h> +#include <error.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 *)); + 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); + 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; + uint8_t mode = buf->flags & 0x3; +#ifdef CONFIG_CHECK_ARGUMENTS + switch(mode) { + case IOEBUFFER_F_MODE_BLOCK: + + } +#endif /* CONFIG_CHECK_ARGUMENTS */ + // First we check if we shouldn't block + 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; + 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; + 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; + 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 */ |