Refactor and add folder .cache to .gitignore
This commit is contained in:
parent
27b2f0bd87
commit
44b2ca3888
2
.gitignore
vendored
2
.gitignore
vendored
@ -55,3 +55,5 @@ dkms.conf
|
||||
.cproject
|
||||
.project
|
||||
.settings/
|
||||
|
||||
/.cache
|
||||
|
162
compile_commands.json
Normal file
162
compile_commands.json
Normal 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"
|
||||
}
|
||||
]
|
132
src/bridge.c
132
src/bridge.c
@ -2,102 +2,66 @@
|
||||
#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,
|
||||
.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); }
|
||||
|
299
src/commands.c
299
src/commands.c
@ -1,189 +1,170 @@
|
||||
#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) {
|
||||
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;
|
||||
}
|
@ -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);
|
||||
|
@ -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);
|
||||
int cmd_handle_switch_timer(void);
|
||||
|
||||
|
@ -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);
|
||||
void setPin(volatile uint8_t *port, unsigned int pin);
|
||||
|
||||
|
@ -2,4 +2,5 @@
|
||||
|
||||
void pwm_init(void);
|
||||
void pwm_worker(void);
|
||||
void pwm_interrupt(void);
|
||||
void pwm_interrupt(void);
|
||||
|
||||
|
@ -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);
|
||||
void sm_handleTransition(sm_t *statemachine, sm_state_t to);
|
||||
|
||||
|
@ -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);
|
||||
uint16_t tmr_getT1Top(void);
|
||||
|
||||
|
@ -2,4 +2,5 @@
|
||||
|
||||
void uart_init(unsigned int baud);
|
||||
void uart_sendByte(uint8_t byte);
|
||||
int uart_printf(char var, FILE *stream);
|
||||
int uart_printf(char var, FILE *stream);
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include "pins.h"
|
||||
#include "pwm.h"
|
||||
|
||||
int main()
|
||||
int main(void)
|
||||
{
|
||||
sei();
|
||||
|
||||
|
16
src/pins.c
16
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);
|
||||
}
|
@ -13,7 +13,7 @@ void pwm_init(void)
|
||||
tmr_subToT1Overflow(pwm_interrupt);
|
||||
}
|
||||
|
||||
void pwm_worker()
|
||||
void pwm_worker(void)
|
||||
{
|
||||
if (!interrupt_received)
|
||||
return;
|
||||
|
@ -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) {}
|
||||
|
||||
|
133
src/timer.c
133
src/timer.c
@ -1,103 +1,82 @@
|
||||
#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) {
|
||||
/*
|
||||
* 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++;
|
||||
}
|
||||
}
|
63
src/uart.c
63
src/uart.c
@ -1,52 +1,47 @@
|
||||
#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)
|
||||
{
|
||||
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<<RXEN1)|(1<<TXEN1);
|
||||
/* Set baud rate */
|
||||
UBRR1H = (unsigned char)(baud >> 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<<UCSZ10); // 8 data bits
|
||||
UCSR1C |= (1<<USBS1); // 2 stop bits
|
||||
/*Enable the receive interrupt*/
|
||||
UCSR1B |= (1 << RXCIE1);
|
||||
|
||||
/* Set frame format */
|
||||
UCSR1C |= (3 << UCSZ10); // 8 data bits
|
||||
UCSR1C |= (1 << USBS1); // 2 stop bits
|
||||
}
|
||||
|
||||
void uart_sendByte(uint8_t byte)
|
||||
{
|
||||
/* Wait for empty transmit buffer */
|
||||
while (!(UCSR1A & (1<<UDRE1)))
|
||||
{
|
||||
}
|
||||
void uart_sendByte(uint8_t byte) {
|
||||
/* Wait for empty transmit buffer */
|
||||
while (!(UCSR1A & (1 << UDRE1))) {
|
||||
}
|
||||
|
||||
/* Put data into buffer, sends the data */
|
||||
UDR1 = byte;
|
||||
/* Put data into buffer, sends the data */
|
||||
UDR1 = byte;
|
||||
}
|
||||
|
||||
int uart_printf(char var, FILE *stream)
|
||||
{
|
||||
uart_sendByte(var);
|
||||
return 0;
|
||||
int uart_printf(char var, FILE *stream) {
|
||||
uart_sendByte(var);
|
||||
return 0;
|
||||
}
|
||||
|
||||
ISR(USART1_RX_vect)
|
||||
{
|
||||
char c = UDR1;
|
||||
ISR(USART1_RX_vect) {
|
||||
char c = UDR1;
|
||||
|
||||
cmd_collect_char(c);
|
||||
uart_sendByte(c);
|
||||
}
|
||||
cmd_collect_char(c);
|
||||
uart_sendByte(c);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user