diff --git a/.gitignore b/.gitignore index f8776f9..1b83b98 100644 --- a/.gitignore +++ b/.gitignore @@ -55,3 +55,5 @@ dkms.conf .cproject .project .settings/ + +/.cache diff --git a/compile_commands.json b/compile_commands.json new file mode 100644 index 0000000..1f67695 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1,162 @@ +[ + { + "arguments": [ + "/usr/bin/avr-gcc", + "-c", + "-DF_CPU=16000000UL", + "-Wall", + "-Wpedantic", + "-Wextra", + "-Os", + "-mmcu=atmega32u4", + "-Isrc/include", + "-I/usr/avr/include", + "-o", + "build/togglePin.elf", + "src/bridge.c" + ], + "directory": "/home/m/dev/electronics/inverter/sw", + "file": "/home/m/dev/electronics/inverter/sw/src/bridge.c", + "output": "/home/m/dev/electronics/inverter/sw/build/togglePin.elf" + }, + { + "arguments": [ + "/usr/bin/avr-gcc", + "-c", + "-DF_CPU=16000000UL", + "-Wall", + "-Wpedantic", + "-Wextra", + "-Os", + "-mmcu=atmega32u4", + "-Isrc/include", + "-I/usr/avr/include", + "-o", + "build/togglePin.elf", + "src/commands.c" + ], + "directory": "/home/m/dev/electronics/inverter/sw", + "file": "/home/m/dev/electronics/inverter/sw/src/commands.c", + "output": "/home/m/dev/electronics/inverter/sw/build/togglePin.elf" + }, + { + "arguments": [ + "/usr/bin/avr-gcc", + "-c", + "-DF_CPU=16000000UL", + "-Wall", + "-Wpedantic", + "-Wextra", + "-Os", + "-mmcu=atmega32u4", + "-Isrc/include", + "-I/usr/avr/include", + "-o", + "build/togglePin.elf", + "src/inverter.c" + ], + "directory": "/home/m/dev/electronics/inverter/sw", + "file": "/home/m/dev/electronics/inverter/sw/src/inverter.c", + "output": "/home/m/dev/electronics/inverter/sw/build/togglePin.elf" + }, + { + "arguments": [ + "/usr/bin/avr-gcc", + "-c", + "-DF_CPU=16000000UL", + "-Wall", + "-Wpedantic", + "-Wextra", + "-Os", + "-mmcu=atmega32u4", + "-Isrc/include", + "-I/usr/avr/include", + "-o", + "build/togglePin.elf", + "src/pins.c" + ], + "directory": "/home/m/dev/electronics/inverter/sw", + "file": "/home/m/dev/electronics/inverter/sw/src/pins.c", + "output": "/home/m/dev/electronics/inverter/sw/build/togglePin.elf" + }, + { + "arguments": [ + "/usr/bin/avr-gcc", + "-c", + "-DF_CPU=16000000UL", + "-Wall", + "-Wpedantic", + "-Wextra", + "-Os", + "-mmcu=atmega32u4", + "-Isrc/include", + "-I/usr/avr/include", + "-o", + "build/togglePin.elf", + "src/pwm.c" + ], + "directory": "/home/m/dev/electronics/inverter/sw", + "file": "/home/m/dev/electronics/inverter/sw/src/pwm.c", + "output": "/home/m/dev/electronics/inverter/sw/build/togglePin.elf" + }, + { + "arguments": [ + "/usr/bin/avr-gcc", + "-c", + "-DF_CPU=16000000UL", + "-Wall", + "-Wpedantic", + "-Wextra", + "-Os", + "-mmcu=atmega32u4", + "-Isrc/include", + "-I/usr/avr/include", + "-o", + "build/togglePin.elf", + "src/statemachine.c" + ], + "directory": "/home/m/dev/electronics/inverter/sw", + "file": "/home/m/dev/electronics/inverter/sw/src/statemachine.c", + "output": "/home/m/dev/electronics/inverter/sw/build/togglePin.elf" + }, + { + "arguments": [ + "/usr/bin/avr-gcc", + "-c", + "-DF_CPU=16000000UL", + "-Wall", + "-Wpedantic", + "-Wextra", + "-Os", + "-mmcu=atmega32u4", + "-Isrc/include", + "-I/usr/avr/include", + "-o", + "build/togglePin.elf", + "src/timer.c" + ], + "directory": "/home/m/dev/electronics/inverter/sw", + "file": "/home/m/dev/electronics/inverter/sw/src/timer.c", + "output": "/home/m/dev/electronics/inverter/sw/build/togglePin.elf" + }, + { + "arguments": [ + "/usr/bin/avr-gcc", + "-c", + "-DF_CPU=16000000UL", + "-Wall", + "-Wpedantic", + "-Wextra", + "-Os", + "-mmcu=atmega32u4", + "-Isrc/include", + "-I/usr/avr/include", + "-o", + "build/togglePin.elf", + "src/uart.c" + ], + "directory": "/home/m/dev/electronics/inverter/sw", + "file": "/home/m/dev/electronics/inverter/sw/src/uart.c", + "output": "/home/m/dev/electronics/inverter/sw/build/togglePin.elf" + } +] diff --git a/src/bridge.c b/src/bridge.c index bd67ad0..47b7378 100644 --- a/src/bridge.c +++ b/src/bridge.c @@ -2,102 +2,66 @@ #include #include "bridge.h" -#include "statemachine.h" #include "pins.h" +#include "statemachine.h" bdg_sm_state_t bridge_state; -sm_trans_t bridge_sm_transitions[] = -{ - { - .from_state = BRIDGE_SM_START, - .to_state = BRIDGE_SM_INIT_IO, - .trans_fct = brdg_BothGndOn - }, - { - .from_state = BRIDGE_SM_INIT_IO, - .to_state = HB1_PWR_ON, - .trans_fct = brdg_Hb1PowerOn - }, - { - .from_state = HB1_GND_ON, - .to_state = HB1_PWR_ON, - .trans_fct = brdg_Hb1PowerOn - }, - { - .from_state = HB1_PWR_ON, - .to_state = HB1_PWR_OFF, - .trans_fct = brdg_Hb1PowerOff - }, - { - .from_state = HB1_PWR_OFF, - .to_state = HB2_GND_ON, - .trans_fct = brdg_Hb2GndOn - }, - { - .from_state = HB2_GND_ON, - .to_state = HB2_PWR_ON, - .trans_fct = brdg_Hb2PowerOn - }, - { - .from_state = HB2_PWR_ON, - .to_state = HB2_PWR_OFF, - .trans_fct = brdg_Hb2PowerOff - }, - { - .from_state = HB2_PWR_OFF, - .to_state = HB2_GND_ON, - .trans_fct = brdg_Hb2GndOn - } -}; +sm_trans_t bridge_sm_transitions[] = {{.from_state = BRIDGE_SM_START, + .to_state = BRIDGE_SM_INIT_IO, + .trans_fct = brdg_BothGndOn}, + {.from_state = BRIDGE_SM_INIT_IO, + .to_state = HB1_PWR_ON, + .trans_fct = brdg_Hb1PowerOn}, + {.from_state = HB1_GND_ON, + .to_state = HB1_PWR_ON, + .trans_fct = brdg_Hb1PowerOn}, + {.from_state = HB1_PWR_ON, + .to_state = HB1_PWR_OFF, + .trans_fct = brdg_Hb1PowerOff}, + {.from_state = HB1_PWR_OFF, + .to_state = HB2_GND_ON, + .trans_fct = brdg_Hb2GndOn}, + {.from_state = HB2_GND_ON, + .to_state = HB2_PWR_ON, + .trans_fct = brdg_Hb2PowerOn}, + {.from_state = HB2_PWR_ON, + .to_state = HB2_PWR_OFF, + .trans_fct = brdg_Hb2PowerOff}, + {.from_state = HB2_PWR_OFF, + .to_state = HB2_GND_ON, + .trans_fct = brdg_Hb2GndOn}}; -sm_t bridge_statemachine = { - .current_state = BRIDGE_SM_START, - .transitions = bridge_sm_transitions -}; +sm_t bridge_statemachine = {.current_state = BRIDGE_SM_START, + .transitions = bridge_sm_transitions}; -void brdg_BothGndOn(void) -{ - /* Turn both GND transistors on */ - setPin(&PORTB, PB6); - setPin(&PORTB, PB4); +void brdg_BothGndOn(void) { + /* Turn both GND transistors on */ + setPin(&PORTB, PB6); + setPin(&PORTB, PB4); - /* Turn both PWR transistors off */ - unsetPin(&PORTB, PB7); - unsetPin(&PORTB, PB5); + /* Turn both PWR transistors off */ + unsetPin(&PORTB, PB7); + unsetPin(&PORTB, PB5); } -void brdg_Hb1GndOn(void) -{ - /* We turn GND of half-bridge 1 on */ - setPin(&PORTB, PB6); +void brdg_Hb1GndOn(void) { + /* We turn GND of half-bridge 1 on */ + setPin(&PORTB, PB6); - /* And then we turn GND of half-bridge 2 off */ - unsetPin(&PORTB, PB4); + /* And then we turn GND of half-bridge 2 off */ + unsetPin(&PORTB, PB4); } -void brdg_Hb1PowerOn(void) -{ - setPin(&PORTB, PB7); +void brdg_Hb1PowerOn(void) { setPin(&PORTB, PB7); } + +void brdg_Hb1PowerOff(void) { unsetPin(&PORTB, PB7); } + +void brdg_Hb2GndOn(void) { + setPin(&PORTB, PB4); + unsetPin(&PORTB, PB6); } -void brdg_Hb1PowerOff(void) -{ - unsetPin(&PORTB, PB7); -} +void brdg_Hb2PowerOn(void) { setPin(&PORTB, PB5); } -void brdg_Hb2GndOn(void) -{ - setPin(&PORTB, PB4); - unsetPin(&PORTB, PB6); -} - -void brdg_Hb2PowerOn(void) -{ - setPin(&PORTB, PB5); -} - -void brdg_Hb2PowerOff(void) -{ - unsetPin(&PORTB, PB5); -} +void brdg_Hb2PowerOff(void) { unsetPin(&PORTB, PB5); } diff --git a/src/commands.c b/src/commands.c index 2b948e7..d690bfe 100644 --- a/src/commands.c +++ b/src/commands.c @@ -1,189 +1,170 @@ +#include #include #include #include -#include #include "commands.h" #include "pins.h" -#include "uart.h" #include "timer.h" +#include "uart.h" - -cmd_t command = { - .buf = {0}, - .index = 0 -}; +cmd_t command = {.buf = {0}, .index = 0}; int cmd_terminated = 0; -void cmd_worker(void) -{ - cmd_handle(&command); +void cmd_worker(void) { cmd_handle(&command); } + +void cmd_collect_char(char c) { + command.buf[command.index] = c; + command.index++; + + if (c == 0x3) { + /* React on Ctl-C */ + command.index = 0; + printf("\r\n"); + } + + if (c == 0xD) { + /* Null-terminate the string */ + command.buf[command.index - 1] = '\0'; + + command.index = 0; + cmd_terminated = 1; + } + + /* We are switching the timer off here because serial + * line handling is iffy with timer on */ + if (c == 0x18) + printf("T1: %d\r\n", cmd_handle_switch_timer()); } -void cmd_collect_char(char c) -{ - command.buf[command.index] = c; - command.index++; +void cmd_handle(cmd_t *command) { + if (!cmd_terminated) + return; - if (c == 0x3) { - /* React on Ctl-C */ - command.index = 0; - printf("\r\n"); - } + printf("\n\r"); - if (c == 0xD) { - /* Null-terminate the string */ - command.buf[command.index-1] = '\0'; + char *tok1 = strtok(command->buf, " "); + char *tok2 = strtok(NULL, " "); + char *tok3 = strtok(NULL, " "); - command.index = 0; - cmd_terminated = 1; - } + if (0 == strcmp(tok1, "set")) + cmd_handle_set(tok2, tok3); - /* We are switching the timer off here because serial - * line handling is iffy with timer on */ - if (c == 0x18) - printf("T1: %d\r\n",cmd_handle_switch_timer()); + if (0 == strcmp(tok1, "switch") || 0 == strcmp(tok1, "sw")) + cmd_handle_switch(tok2, tok3); + + cmd_terminated = 0; } -void cmd_handle(cmd_t *command) -{ - if (!cmd_terminated) - return; +void cmd_handle_set(char *op1, char *op2) { + long int val; + char *end; - printf("\n\r"); - - char *tok1 = strtok(command->buf, " "); - char *tok2 = strtok(NULL, " "); - char *tok3 = strtok(NULL, " "); - - if (0 == strcmp(tok1, "set")) - cmd_handle_set(tok2, tok3); - - if (0 == strcmp(tok1, "switch") || 0 == strcmp(tok1, "sw")) - cmd_handle_switch(tok2, tok3); - - - cmd_terminated = 0; + if (0 == strcmp(op1, "t0_top")) { + val = strtol(op2, &end, 10); + if (op2 != end) { + OCR0A = val; + printf("Setting Timer0 TOP to %li\r\n", val); + } else + printf("No valid value for t0_top given\n\r"); + } else if (0 == strcmp(op1, "t1_top")) { + val = strtol(op2, &end, 10); + if (op2 != end) { + OCR1A = val; + printf("Setting Timer1 TOP to %li\r\n", val); + } else + printf("No valid value for t1_top given\n\r"); + } else if (0 == strcmp(op1, "t1_cm")) { + val = strtol(op2, &end, 10); + if (op2 != end) { + OCR1B = val; + printf("Setting Timer1 Compare Match to %li\r\n", val); + } else + printf("No valid value for t1_cm given\n\r"); + } else if (0 == strcmp(op1, "t0_ps")) { + val = strtol(op2, &end, 10); + TCCR0B &= ~((1 << CS00) | (1 << CS01) | (1 << CS02)); + switch (val) { + case 0: + TCCR0B |= (1 << CS00); + break; + case 8: + TCCR0B |= (1 << CS01); + break; + case 64: + TCCR0B |= (1 << CS00) | (1 << CS01); + break; + case 256: + TCCR0B |= (1 << CS02); + break; + case 1024: + TCCR0B |= (1 << CS02) | (1 << CS00); + break; + default: + printf("No valid prescaler value.\n\r"); + break; + } + } } -void cmd_handle_set(char *op1, char *op2) -{ - long int val; - char *end; +void cmd_handle_switch(char *op1, char *op2) { + if (0 == strcmp(op1, "pd6")) { + if (0 == strcmp(op2, "on")) + setPin(&PORTD, PD6); + else if (0 == strcmp(op2, "off")) + unsetPin(&PORTD, PD6); + else + togglePin(&PORTD, PD6); + } - if (0 == strcmp(op1, "t0_top")) { - val = strtol(op2, &end, 10); - if (op2 != end) { - OCR0A = val; - printf("Setting Timer0 TOP to %li\r\n", val); - } - else - printf("No valid value for t0_top given\n\r"); - } - else if (0 == strcmp(op1, "t1_top")) { - val = strtol(op2, &end, 10); - if (op2 != end) { - OCR1A = val; - printf("Setting Timer1 TOP to %li\r\n", val); - } - else - printf("No valid value for t1_top given\n\r"); - } - else if (0 == strcmp(op1, "t1_cm")) { - val = strtol(op2, &end, 10); - if (op2 != end) { - OCR1B = val; - printf("Setting Timer1 Compare Match to %li\r\n", val); - } - else - printf("No valid value for t1_cm given\n\r"); - } - else if (0 == strcmp(op1, "t0_ps")) { - val = strtol(op2, &end, 10); - TCCR0B &= ~((1 << CS00) | (1 << CS01) | (1 << CS02)); - switch (val) - { - case 0: - TCCR0B |= (1 << CS00); - break; - case 8: - TCCR0B |= (1 << CS01); - break; - case 64: - TCCR0B |= (1 << CS00) | (1 << CS01); - break; - case 256: - TCCR0B |= (1 << CS02); - break; - case 1024: - TCCR0B |= (1 << CS02) | (1 << CS00); - break; - default: - printf("No valid prescaler value.\n\r"); - break; - } - } + if (0 == strcmp(op1, "pb6")) { + if (0 == strcmp(op2, "on")) + setPin(&PORTB, PB6); + else if (0 == strcmp(op2, "off")) + unsetPin(&PORTB, PB6); + else + togglePin(&PORTB, PB6); + } + + if (0 == strcmp(op1, "pc7")) { + if (0 == strcmp(op2, "on")) + setPin(&PORTC, PC7); + else if (0 == strcmp(op2, "off")) + unsetPin(&PORTC, PC7); + else + togglePin(&PORTC, PC7); + } + + if (0 == strcmp(op1, "pd1")) { + if (0 == strcmp(op2, "on")) + setPin(&PORTD, PD1); + else if (0 == strcmp(op2, "off")) + unsetPin(&PORTD, PD1); + else + togglePin(&PORTD, PD1); + } + + if (0 == strcmp(op1, "pd0")) { + if (0 == strcmp(op2, "on")) + setPin(&PORTD, PD0); + else if (0 == strcmp(op2, "off")) + unsetPin(&PORTD, PD0); + else + togglePin(&PORTD, PD0); + } } -void cmd_handle_switch(char *op1, char *op2) -{ - if (0 == strcmp(op1, "pd6")) { - if (0 == strcmp(op2, "on")) - setPin(&PORTD, PD6); - else if (0 == strcmp(op2, "off")) - unsetPin(&PORTD, PD6); - else - togglePin(&PORTD, PD6); - } +int cmd_handle_switch_timer(void) { + static int timer1_on = 1; - if (0 == strcmp(op1, "pb6")) { - if (0 == strcmp(op2, "on")) - setPin(&PORTB, PB6); - else if (0 == strcmp(op2, "off")) - unsetPin(&PORTB, PB6); - else - togglePin(&PORTB, PB6); - } + if (timer1_on) + tmr_disableT1(); + else + tmr_enableT1(); - if (0 == strcmp(op1, "pc7")) { - if (0 == strcmp(op2, "on")) - setPin(&PORTC, PC7); - else if (0 == strcmp(op2, "off")) - unsetPin(&PORTC, PC7); - else - togglePin(&PORTC, PC7); - } + timer1_on = !timer1_on; - if (0 == strcmp(op1, "pd1")) { - if (0 == strcmp(op2, "on")) - setPin(&PORTD, PD1); - else if (0 == strcmp(op2, "off")) - unsetPin(&PORTD, PD1); - else - togglePin(&PORTD, PD1); - } - - if (0 == strcmp(op1, "pd0")) { - if (0 == strcmp(op2, "on")) - setPin(&PORTD, PD0); - else if (0 == strcmp(op2, "off")) - unsetPin(&PORTD, PD0); - else - togglePin(&PORTD, PD0); - } + return timer1_on; } -int cmd_handle_switch_timer(void) -{ - static int timer1_on = 1; - - if (timer1_on) - tmr_disableT1(); - else - tmr_enableT1(); - - timer1_on = !timer1_on; - - return timer1_on; -} \ No newline at end of file diff --git a/src/include/bridge.h b/src/include/bridge.h index dfa4a03..e742c6f 100644 --- a/src/include/bridge.h +++ b/src/include/bridge.h @@ -1,16 +1,14 @@ -typedef enum -{ - BRIDGE_SM_START, - BRIDGE_SM_INIT_IO, - HB1_GND_ON, - HB1_PWR_ON, - HB1_PWR_OFF, - HB2_GND_ON, - HB2_PWR_ON, - HB2_PWR_OFF +typedef enum { + BRIDGE_SM_START, + BRIDGE_SM_INIT_IO, + HB1_GND_ON, + HB1_PWR_ON, + HB1_PWR_OFF, + HB2_GND_ON, + HB2_PWR_ON, + HB2_PWR_OFF } bdg_sm_state_t; - void brdg_BothGndOn(void); void brdg_Hb1GndOn(void); diff --git a/src/include/commands.h b/src/include/commands.h index 8378439..b712132 100644 --- a/src/include/commands.h +++ b/src/include/commands.h @@ -1,6 +1,6 @@ typedef struct { - char buf[100]; - int index; + char buf[100]; + int index; } cmd_t; void cmd_worker(void); @@ -9,4 +9,5 @@ void cmd_collect_char(char c); void cmd_handle(cmd_t *command); void cmd_handle_set(char *op1, char *op2); void cmd_handle_switch(char *op1, char *op2); -int cmd_handle_switch_timer(void); \ No newline at end of file +int cmd_handle_switch_timer(void); + diff --git a/src/include/pins.h b/src/include/pins.h index 5194449..42c429d 100644 --- a/src/include/pins.h +++ b/src/include/pins.h @@ -2,4 +2,5 @@ void togglePin(volatile uint8_t *port, unsigned int pin); void unsetPin(volatile uint8_t *port, unsigned int pin); -void setPin(volatile uint8_t *port, unsigned int pin); \ No newline at end of file +void setPin(volatile uint8_t *port, unsigned int pin); + diff --git a/src/include/pwm.h b/src/include/pwm.h index 26430be..c326970 100644 --- a/src/include/pwm.h +++ b/src/include/pwm.h @@ -2,4 +2,5 @@ void pwm_init(void); void pwm_worker(void); -void pwm_interrupt(void); \ No newline at end of file +void pwm_interrupt(void); + diff --git a/src/include/statemachine.h b/src/include/statemachine.h index d1bbb59..add736c 100644 --- a/src/include/statemachine.h +++ b/src/include/statemachine.h @@ -1,16 +1,15 @@ typedef int sm_state_t; -typedef struct -{ - sm_state_t from_state; - sm_state_t to_state; - void (*trans_fct)(void); +typedef struct { + sm_state_t from_state; + sm_state_t to_state; + void (*trans_fct)(void); } sm_trans_t; -typedef struct -{ - sm_state_t current_state; - sm_trans_t *transitions; +typedef struct { + sm_state_t current_state; + sm_trans_t *transitions; } sm_t; -void sm_handleTransition(sm_t *statemachine, sm_state_t to); \ No newline at end of file +void sm_handleTransition(sm_t *statemachine, sm_state_t to); + diff --git a/src/include/timer.h b/src/include/timer.h index b70df7e..a780edd 100644 --- a/src/include/timer.h +++ b/src/include/timer.h @@ -2,19 +2,18 @@ #define NOTIFY_ARRAY_SIZE 10 -typedef struct -{ - void (*callOnInterrupt[NOTIFY_ARRAY_SIZE])(); - int index; +typedef struct { + void (*callOnInterrupt[NOTIFY_ARRAY_SIZE])(void); + int index; } timer_notify_t; - void tmr_initT1(void); void tmr_initT0(void); -void tmr_subToT1Overflow(void (*)()); +void tmr_subToT1Overflow(void (*)(void)); void tmr_enableT1(void); void tmr_disableT1(void); void tmr_setT1CompareMatch(uint16_t cm); -uint16_t tmr_getT1Top(void); \ No newline at end of file +uint16_t tmr_getT1Top(void); + diff --git a/src/include/uart.h b/src/include/uart.h index edbfb28..91b1709 100644 --- a/src/include/uart.h +++ b/src/include/uart.h @@ -2,4 +2,5 @@ void uart_init(unsigned int baud); void uart_sendByte(uint8_t byte); -int uart_printf(char var, FILE *stream); \ No newline at end of file +int uart_printf(char var, FILE *stream); + diff --git a/src/inverter.c b/src/inverter.c index 58b206e..1cdf6c5 100644 --- a/src/inverter.c +++ b/src/inverter.c @@ -8,7 +8,7 @@ #include "pins.h" #include "pwm.h" -int main() +int main(void) { sei(); diff --git a/src/pins.c b/src/pins.c index 7bdd4ac..246ed28 100644 --- a/src/pins.c +++ b/src/pins.c @@ -3,17 +3,13 @@ #include "pins.h" -void setPin(volatile uint8_t *port, unsigned int pin) -{ - *port |= (1 << pin); +void setPin(volatile uint8_t *port, unsigned int pin) { *port |= (1 << pin); } + +void unsetPin(volatile uint8_t *port, unsigned int pin) { + *port &= ~(1 << pin); } -void unsetPin(volatile uint8_t *port, unsigned int pin) -{ - *port &= ~(1 << pin); +void togglePin(volatile uint8_t *port, unsigned int pin) { + *port ^= (1 << pin); } -void togglePin(volatile uint8_t *port, unsigned int pin) -{ - *port ^= (1 << pin); -} \ No newline at end of file diff --git a/src/pwm.c b/src/pwm.c index 2f5ae85..bb59271 100644 --- a/src/pwm.c +++ b/src/pwm.c @@ -13,7 +13,7 @@ void pwm_init(void) tmr_subToT1Overflow(pwm_interrupt); } -void pwm_worker() +void pwm_worker(void) { if (!interrupt_received) return; diff --git a/src/statemachine.c b/src/statemachine.c index 2076ecf..da3ecff 100644 --- a/src/statemachine.c +++ b/src/statemachine.c @@ -1,6 +1,4 @@ #include "statemachine.h" -void sm_handleTransition(sm_t *statemachine, sm_state_t to) -{ - -} \ No newline at end of file +void sm_handleTransition(sm_t *statemachine, sm_state_t to) {} + diff --git a/src/timer.c b/src/timer.c index c28082d..6eadf14 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,103 +1,82 @@ -#include #include +#include #include #include #include "timer.h" +timer_notify_t timer1 = {.index = 0, .callOnInterrupt = {0}}; -timer_notify_t timer1 = -{ - .index = 0, - .callOnInterrupt = {0} -}; +void tmr_initT1(void) { + /* + * We are using Timer 1 Compare Match Unit B. + * OC1B is on Pin PB6. Let's make it an output. + */ + DDRB |= (1 << DDB6); + /* Enable Fast PWM with TOP in OCR1A */ + TCCR1A |= (1 << WGM11) | (1 << WGM10); + TCCR1B |= (1 << WGM13) | (1 << WGM12); -void tmr_initT1(void) -{ - /* - * We are using Timer 1 Compare Match Unit B. - * OC1B is on Pin PB6. Let's make it an output. - */ - DDRB |= (1 << DDB6); + /* Set OC1B on compare match, clear OC1B at TOP */ + TCCR1A |= (1 << COM1B1); - /* Enable Fast PWM with TOP in OCR1A */ - TCCR1A |= (1 << WGM11) | (1 << WGM10); - TCCR1B |= (1 << WGM13) | (1 << WGM12); + /* Enable Counter1 Compare Match B Interrupt */ + TIMSK1 |= (1 << OCIE1B); - /* Set OC1B on compare match, clear OC1B at TOP */ - TCCR1A |= (1 << COM1B1); + /* Enable Counter1 Overflow Interrupt */ + TIMSK1 |= (1 << TOIE1); - /* Enable Counter1 Compare Match B Interrupt */ - TIMSK1 |= (1 << OCIE1B); + /* TOP value */ + OCR1A = 40000; - /* Enable Counter1 Overflow Interrupt */ - TIMSK1 |= (1 << TOIE1); - - /* TOP value */ - OCR1A = 40000; - - /* Compare Match with OCR1B */ - OCR1B = 20000; + /* Compare Match with OCR1B */ + OCR1B = 20000; } -void tmr_enableT1(void) -{ - /* Clock prescaler of 8 */ - TCCR1B |= (1 << CS11); +void tmr_enableT1(void) { + /* Clock prescaler of 8 */ + TCCR1B |= (1 << CS11); } -void tmr_disableT1(void) -{ - TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12)); +void tmr_disableT1(void) { + TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12)); } -void tmr_setT1CompareMatch(uint16_t cm) -{ - OCR1B = cm; +void tmr_setT1CompareMatch(uint16_t cm) { OCR1B = cm; } + +uint16_t tmr_getT1Top(void) { return OCR1A; } + +void tmr_initT0(void) { + /* Initialize counter 0 */ + TCNT0 = 0; + + /* Enable Counter0 Compare Match A Interrupt */ + TIMSK0 |= (1 << OCIE0A); + + /* Select clock. Prescaler of 1024 */ + TCCR0B |= (1 << CS02) | (1 << CS00); + + /* Use CTC Mode */ + TCCR0A |= (1 << WGM01); + + /* + * OCR0A contains TOP value for counter: + */ + OCR0A = 255; } -uint16_t tmr_getT1Top(void) -{ - return OCR1A; +ISR(TIMER1_OVF_vect) { + for (int i = 0; i < timer1.index; i++) + timer1.callOnInterrupt[i](); } -void tmr_initT0(void) -{ - /* Initialize counter 0 */ - TCNT0 = 0; +ISR(TIMER1_COMPB_vect) { PORTD ^= (1 << PD7); } - /* Enable Counter0 Compare Match A Interrupt */ - TIMSK0 |= (1 << OCIE0A); - - /* Select clock. Prescaler of 1024 */ - TCCR0B |= (1 << CS02) | (1 << CS00); - - /* Use CTC Mode */ - TCCR0A |= (1 << WGM01); - - /* - * OCR0A contains TOP value for counter: - */ - OCR0A = 255; +void tmr_subToT1Overflow(void (*call)(void)) { + if (timer1.index < NOTIFY_ARRAY_SIZE) { + timer1.callOnInterrupt[timer1.index] = call; + timer1.index++; + } } - -ISR(TIMER1_OVF_vect) -{ - for(int i = 0; i < timer1.index; i++) - timer1.callOnInterrupt[i](); -} - -ISR(TIMER1_COMPB_vect) -{ - PORTD ^= (1 << PD7); -} - -void tmr_subToT1Overflow(void (*call)()) -{ - if (timer1.index < NOTIFY_ARRAY_SIZE) { - timer1.callOnInterrupt[timer1.index] = call; - timer1.index++; - } -} \ No newline at end of file diff --git a/src/uart.c b/src/uart.c index 73e0045..48496ed 100644 --- a/src/uart.c +++ b/src/uart.c @@ -1,52 +1,47 @@ -#include #include +#include -#include "include/uart.h" #include "include/commands.h" +#include "include/uart.h" /* Make uart_printf be called whenever printf() is invoked */ static FILE stream = FDEV_SETUP_STREAM(uart_printf, NULL, _FDEV_SETUP_WRITE); -void uart_init(unsigned int baud) -{ - stdout = &stream; +void uart_init(unsigned int baud) { + stdout = &stream; - /* Set baud rate */ - UBRR1H = (unsigned char)(baud>>8); - UBRR1L = (unsigned char)baud; - - /* Enable receiver and transmitter */ - UCSR1B = (1<> 8); + UBRR1L = (unsigned char)baud; - /*Enable the receive interrupt*/ - UCSR1B |= (1 << RXCIE1); + /* Enable receiver and transmitter */ + UCSR1B = (1 << RXEN1) | (1 << TXEN1); - /* Set frame format */ - UCSR1C |= (3<