Refactor and add folder .cache to .gitignore

This commit is contained in:
Marco 2023-11-09 00:05:28 +01:00
parent 27b2f0bd87
commit 44b2ca3888
17 changed files with 481 additions and 404 deletions

2
.gitignore vendored
View File

@ -55,3 +55,5 @@ dkms.conf
.cproject
.project
.settings/
/.cache

162
compile_commands.json Normal file
View File

@ -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"
}
]

View File

@ -2,62 +2,40 @@
#include <util/delay.h>
#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,
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,
.trans_fct = brdg_BothGndOn},
{.from_state = BRIDGE_SM_INIT_IO,
.to_state = HB1_PWR_ON,
.trans_fct = brdg_Hb1PowerOn
},
{
.from_state = HB1_GND_ON,
.trans_fct = brdg_Hb1PowerOn},
{.from_state = HB1_GND_ON,
.to_state = HB1_PWR_ON,
.trans_fct = brdg_Hb1PowerOn
},
{
.from_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,
.trans_fct = brdg_Hb1PowerOff},
{.from_state = HB1_PWR_OFF,
.to_state = HB2_GND_ON,
.trans_fct = brdg_Hb2GndOn
},
{
.from_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,
.trans_fct = brdg_Hb2PowerOn},
{.from_state = HB2_PWR_ON,
.to_state = HB2_PWR_OFF,
.trans_fct = brdg_Hb2PowerOff
},
{
.from_state = HB2_PWR_OFF,
.trans_fct = brdg_Hb2PowerOff},
{.from_state = HB2_PWR_OFF,
.to_state = HB2_GND_ON,
.trans_fct = brdg_Hb2GndOn
}
};
.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)
{
void brdg_BothGndOn(void) {
/* Turn both GND transistors on */
setPin(&PORTB, PB6);
setPin(&PORTB, PB4);
@ -67,8 +45,7 @@ void brdg_BothGndOn(void)
unsetPin(&PORTB, PB5);
}
void brdg_Hb1GndOn(void)
{
void brdg_Hb1GndOn(void) {
/* We turn GND of half-bridge 1 on */
setPin(&PORTB, PB6);
@ -76,28 +53,15 @@ void brdg_Hb1GndOn(void)
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_Hb1PowerOff(void) { unsetPin(&PORTB, PB7); }
void brdg_Hb2GndOn(void)
{
void brdg_Hb2GndOn(void) {
setPin(&PORTB, PB4);
unsetPin(&PORTB, PB6);
}
void brdg_Hb2PowerOn(void)
{
setPin(&PORTB, PB5);
}
void brdg_Hb2PowerOn(void) { setPin(&PORTB, PB5); }
void brdg_Hb2PowerOff(void)
{
unsetPin(&PORTB, PB5);
}
void brdg_Hb2PowerOff(void) { unsetPin(&PORTB, PB5); }

View File

@ -1,28 +1,20 @@
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <avr/io.h>
#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)
{
void cmd_collect_char(char c) {
command.buf[command.index] = c;
command.index++;
@ -34,7 +26,7 @@ void cmd_collect_char(char c)
if (c == 0xD) {
/* Null-terminate the string */
command.buf[command.index-1] = '\0';
command.buf[command.index - 1] = '\0';
command.index = 0;
cmd_terminated = 1;
@ -43,11 +35,10 @@ void cmd_collect_char(char c)
/* 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());
printf("T1: %d\r\n", cmd_handle_switch_timer());
}
void cmd_handle(cmd_t *command)
{
void cmd_handle(cmd_t *command) {
if (!cmd_terminated)
return;
@ -63,12 +54,10 @@ void cmd_handle(cmd_t *command)
if (0 == strcmp(tok1, "switch") || 0 == strcmp(tok1, "sw"))
cmd_handle_switch(tok2, tok3);
cmd_terminated = 0;
}
void cmd_handle_set(char *op1, char *op2)
{
void cmd_handle_set(char *op1, char *op2) {
long int val;
char *end;
@ -77,33 +66,26 @@ void cmd_handle_set(char *op1, char *op2)
if (op2 != end) {
OCR0A = val;
printf("Setting Timer0 TOP to %li\r\n", val);
}
else
} else
printf("No valid value for t0_top given\n\r");
}
else if (0 == strcmp(op1, "t1_top")) {
} 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
} else
printf("No valid value for t1_top given\n\r");
}
else if (0 == strcmp(op1, "t1_cm")) {
} 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
} else
printf("No valid value for t1_cm given\n\r");
}
else if (0 == strcmp(op1, "t0_ps")) {
} else if (0 == strcmp(op1, "t0_ps")) {
val = strtol(op2, &end, 10);
TCCR0B &= ~((1 << CS00) | (1 << CS01) | (1 << CS02));
switch (val)
{
switch (val) {
case 0:
TCCR0B |= (1 << CS00);
break;
@ -126,8 +108,7 @@ void cmd_handle_set(char *op1, char *op2)
}
}
void cmd_handle_switch(char *op1, char *op2)
{
void cmd_handle_switch(char *op1, char *op2) {
if (0 == strcmp(op1, "pd6")) {
if (0 == strcmp(op2, "on"))
setPin(&PORTD, PD6);
@ -174,8 +155,7 @@ void cmd_handle_switch(char *op1, char *op2)
}
}
int cmd_handle_switch_timer(void)
{
int cmd_handle_switch_timer(void) {
static int timer1_on = 1;
if (timer1_on)
@ -187,3 +167,4 @@ int cmd_handle_switch_timer(void)
return timer1_on;
}

View File

@ -1,5 +1,4 @@
typedef enum
{
typedef enum {
BRIDGE_SM_START,
BRIDGE_SM_INIT_IO,
HB1_GND_ON,
@ -10,7 +9,6 @@ typedef enum
HB2_PWR_OFF
} bdg_sm_state_t;
void brdg_BothGndOn(void);
void brdg_Hb1GndOn(void);

View File

@ -10,3 +10,4 @@ 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);

View File

@ -3,3 +3,4 @@
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);

View File

@ -3,3 +3,4 @@
void pwm_init(void);
void pwm_worker(void);
void pwm_interrupt(void);

View File

@ -1,16 +1,15 @@
typedef int sm_state_t;
typedef struct
{
typedef struct {
sm_state_t from_state;
sm_state_t to_state;
void (*trans_fct)(void);
} sm_trans_t;
typedef struct
{
typedef struct {
sm_state_t current_state;
sm_trans_t *transitions;
} sm_t;
void sm_handleTransition(sm_t *statemachine, sm_state_t to);

View File

@ -2,19 +2,18 @@
#define NOTIFY_ARRAY_SIZE 10
typedef struct
{
void (*callOnInterrupt[NOTIFY_ARRAY_SIZE])();
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);

View File

@ -3,3 +3,4 @@
void uart_init(unsigned int baud);
void uart_sendByte(uint8_t byte);
int uart_printf(char var, FILE *stream);

View File

@ -8,7 +8,7 @@
#include "pins.h"
#include "pwm.h"
int main()
int main(void)
{
sei();

View File

@ -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)
{
void unsetPin(volatile uint8_t *port, unsigned int pin) {
*port &= ~(1 << pin);
}
void togglePin(volatile uint8_t *port, unsigned int pin)
{
void togglePin(volatile uint8_t *port, unsigned int pin) {
*port ^= (1 << pin);
}

View File

@ -13,7 +13,7 @@ void pwm_init(void)
tmr_subToT1Overflow(pwm_interrupt);
}
void pwm_worker()
void pwm_worker(void)
{
if (!interrupt_received)
return;

View File

@ -1,6 +1,4 @@
#include "statemachine.h"
void sm_handleTransition(sm_t *statemachine, sm_state_t to)
{
void sm_handleTransition(sm_t *statemachine, sm_state_t to) {}
}

View File

@ -1,21 +1,14 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <math.h>
#include <stdio.h>
#include "timer.h"
timer_notify_t timer1 = {.index = 0, .callOnInterrupt = {0}};
timer_notify_t timer1 =
{
.index = 0,
.callOnInterrupt = {0}
};
void tmr_initT1(void)
{
void tmr_initT1(void) {
/*
* We are using Timer 1 Compare Match Unit B.
* OC1B is on Pin PB6. Let's make it an output.
@ -42,29 +35,20 @@ void tmr_initT1(void)
OCR1B = 20000;
}
void tmr_enableT1(void)
{
void tmr_enableT1(void) {
/* Clock prescaler of 8 */
TCCR1B |= (1 << CS11);
}
void tmr_disableT1(void)
{
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;
}
uint16_t tmr_getT1Top(void) { return OCR1A; }
void tmr_initT0(void)
{
void tmr_initT0(void) {
/* Initialize counter 0 */
TCNT0 = 0;
@ -83,19 +67,14 @@ void tmr_initT0(void)
OCR0A = 255;
}
ISR(TIMER1_OVF_vect)
{
for(int i = 0; i < timer1.index; i++)
ISR(TIMER1_OVF_vect) {
for (int i = 0; i < timer1.index; i++)
timer1.callOnInterrupt[i]();
}
ISR(TIMER1_COMPB_vect)
{
PORTD ^= (1 << PD7);
}
ISR(TIMER1_COMPB_vect) { PORTD ^= (1 << PD7); }
void tmr_subToT1Overflow(void (*call)())
{
void tmr_subToT1Overflow(void (*call)(void)) {
if (timer1.index < NOTIFY_ARRAY_SIZE) {
timer1.callOnInterrupt[timer1.index] = call;
timer1.index++;

View File

@ -1,50 +1,45 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#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)
{
void uart_init(unsigned int baud) {
stdout = &stream;
/* Set baud rate */
UBRR1H = (unsigned char)(baud>>8);
UBRR1H = (unsigned char)(baud >> 8);
UBRR1L = (unsigned char)baud;
/* Enable receiver and transmitter */
UCSR1B = (1<<RXEN1)|(1<<TXEN1);
UCSR1B = (1 << RXEN1) | (1 << TXEN1);
/*Enable the receive interrupt*/
UCSR1B |= (1 << RXCIE1);
/* Set frame format */
UCSR1C |= (3<<UCSZ10); // 8 data bits
UCSR1C |= (1<<USBS1); // 2 stop bits
UCSR1C |= (3 << UCSZ10); // 8 data bits
UCSR1C |= (1 << USBS1); // 2 stop bits
}
void uart_sendByte(uint8_t byte)
{
void uart_sendByte(uint8_t byte) {
/* Wait for empty transmit buffer */
while (!(UCSR1A & (1<<UDRE1)))
{
while (!(UCSR1A & (1 << UDRE1))) {
}
/* Put data into buffer, sends the data */
UDR1 = byte;
}
int uart_printf(char var, FILE *stream)
{
int uart_printf(char var, FILE *stream) {
uart_sendByte(var);
return 0;
}
ISR(USART1_RX_vect)
{
ISR(USART1_RX_vect) {
char c = UDR1;
cmd_collect_char(c);