Skip to content

Merge request #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions stm8/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*.lst
*.asm
*.sym
*.adb
*.cdb
.*.d
test_pwm_accuracy
test_adc_accuracy
Expand Down
19 changes: 14 additions & 5 deletions stm8/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SRC=main.c display.c uart.c eeprom.c outputs.c config.c fixedpoint.c parse.c adc.c
CFLAGS= -lstm8 -mstm8 --opt-code-size --std-c99
CFLAGS= -lstm8 -mstm8 --opt-code-size --std-c99 --fverbose-asm
OBJ=$(SRC:.c=.rel)
DEP=$(SRC:%.c=.%.c.d)

Expand All @@ -14,8 +14,12 @@ LINK_0 = @echo LINK $@; $(ACTUAL_SDCC)
LINK_1 = $(ACTUAL_SDCC)
LINK = $(LINK_$(V))

TESTUTILS=test_pwm_accuracy test_adc_accuracy test_parse

all: b3603.ihx check_size

test: $(TESTUTILS)

-include $(DEP)

check_size: b3603.ihx
Expand All @@ -24,12 +28,16 @@ check_size: b3603.ihx
else echo "Code fits the flash, it is $$CODESIZE"; \
fi

deploy: b3603.ihx
stm8flash -c stlinkv2 -p stm8s003f3 -w $<

b3603.ihx: $(OBJ)
$(LINK) --out-fmt-ihx --code-size 8192 -o $@ $^

.%.c.d: %.c
@$(ACTUAL_SDCC) -M -o $@ $<

%.rel: %.c
@$(SDCC) -M $< > .$<.tmp.d
@mv .$<.tmp.d .$<.d
$(SDCC) -c -o $@ $<

test_pwm_accuracy: test_pwm_accuracy.c outputs.c config.c fixedpoint.c
Expand All @@ -42,6 +50,7 @@ test_parse: test_parse.c parse.c
gcc -g -Wall -o $@ $< -DTEST=1

clean:
-rm -f *.rel *.ihx *.lk *.map *.rst *.lst *.asm *.sym .*.d
-rm -f *.rel *.ihx *.lk *.map *.rst *.lst *.asm *.sym *.adb *.cdb .*.d
-rm -f $(TESTUTILS)

.PHONY: all clean check_size
.PHONY: all clean check_size test deploy
9 changes: 7 additions & 2 deletions stm8/adc.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
#include "adc.h"
#include "stm8s.h"

// We only have a 10-bit ADC, so oversample for 3 bits of additional accuracy
#define OVERSAMPLE_BITS 3
#define OVERSAMPLE_DIVIDE (1 << OVERSAMPLE_BITS)
#define OVERSAMPLE_COUNT (OVERSAMPLE_DIVIDE << OVERSAMPLE_BITS)

static uint32_t sum;
static uint8_t count;

Expand Down Expand Up @@ -77,7 +82,7 @@ inline uint16_t _adc_read(void)

uint16_t adc_read(void)
{
return sum/8;
return sum / OVERSAMPLE_DIVIDE;
}

uint8_t adc_channel(void)
Expand All @@ -90,7 +95,7 @@ uint8_t adc_ready(void)
if (ADC1_CSR & 0x80) {
sum += _adc_read();
count++;
if (count < 64) {
if (count < OVERSAMPLE_COUNT) {
_adc_start();
return 0;
} else {
Expand Down
20 changes: 10 additions & 10 deletions stm8/calibrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@ def status(self):
part = line.split(':')
if part[0] == 'OUTPUT':
output = part[1].strip()
elif part[0] == 'VOLTAGE IN':
elif part[0] == 'VIN':
vin = float(part[1].strip())
elif part[0] == 'VOLTAGE OUT':
elif part[0] == 'VOUT':
vout = float(part[1].strip())
elif part[0] == 'CURRENT OUT':
elif part[0] == 'COUT':
cout = float(part[1].strip())
elif part[0] == 'CONSTANT':
constant = part[1].strip()
Expand All @@ -103,17 +103,17 @@ def rstatus(self):
part = line.split(':')
if part[0] == 'OUTPUT':
output = part[1].strip()
elif part[0] == 'VOLTAGE IN':
elif part[0] == 'VIN':
vin_calc = float(part[1].strip())
elif part[0] == 'VOLTAGE OUT':
elif part[0] == 'VOUT':
vout_calc = float(part[1].strip())
elif part[0] == 'CURRENT OUT':
elif part[0] == 'COUT':
cout_calc = float(part[1].strip())
elif part[0] == 'VOLTAGE IN ADC':
elif part[0] == 'VIN ADC':
vin = float(part[1].strip())
elif part[0] == 'VOLTAGE OUT ADC':
elif part[0] == 'VOUT ADC':
vout = float(part[1].strip())
elif part[0] == 'CURRENT OUT ADC':
elif part[0] == 'COUT ADC':
cout = float(part[1].strip())
elif part[0] == 'CONSTANT':
constant = part[1].strip()
Expand Down Expand Up @@ -316,7 +316,7 @@ def main():
auto_calibration()

if sys.argv[1] == '-m':
if len(sys.argv) != 2:
if len(sys.argv) != 3:
return usage()
else:
manual_calibration()
Expand Down
12 changes: 6 additions & 6 deletions stm8/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ void config_default_system(cfg_system_t *sys)
inline void validate_system_config(cfg_system_t *sys)
{
if (sys->version != SYSTEM_CFG_VERSION ||
sys->name[0] ||
sys->vin_adc.a ||
sys->vout_adc.a ||
sys->cout_adc.a ||
sys->vout_pwm.a ||
sys->cout_pwm.a
sys->name[0] == 0 ||
sys->vin_adc.a == 0 ||
sys->vout_adc.a == 0 ||
sys->cout_adc.a == 0 ||
sys->vout_pwm.a == 0 ||
sys->cout_pwm.a == 0
)
{
config_default_system(sys);
Expand Down
2 changes: 2 additions & 0 deletions stm8/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ typedef struct {
uint16_t vout; // mV
uint16_t cout; // mA
uint8_t constant_current; // If false, we are in constant voltage
#if DEBUG
uint8_t pc3;
#endif
} state_t;

void config_load_system(cfg_system_t *sys);
Expand Down
2 changes: 1 addition & 1 deletion stm8/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ uint8_t pending_display_data[4];
uint8_t pending_update;
uint16_t timer;

static const display_number[10] = {
static const uint8_t display_number[10] = {
0xFC, // '0'
0x60, // '1'
0xDA, // '2'
Expand Down
52 changes: 24 additions & 28 deletions stm8/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ cfg_system_t cfg_system;
cfg_output_t cfg_output;
state_t state;

inline iwatchdog_init(void)
inline void iwatchdog_init(void)
{
IWDG_KR = 0xCC; // Enable IWDG
// The default values give us about 15msec between pings
}

inline iwatchdog_tick(void)
inline void iwatchdog_tick(void)
{
IWDG_KR = 0xAA; // Reset the counter
}
Expand Down Expand Up @@ -307,7 +307,7 @@ void process_input()
uart_write_int32(cfg_system.cout_pwm.a);
uart_write_ch('/');
uart_write_int32(cfg_system.cout_pwm.b);
uart_write_ch('\r');
uart_write_str("\r\n");
} else if (strcmp(uart_read_buf, "LIMITS") == 0) {
uart_write_str("LIMITS:\r\n");
write_millivolt("VMIN: ", CAP_VMIN);
Expand Down Expand Up @@ -385,34 +385,26 @@ void process_input()
set_current(uart_read_buf + idx + 1);
} else if (strcmp(uart_read_buf, "AUTOCOMMIT") == 0) {
set_autocommit(uart_read_buf + idx + 1);
/* } else if (strcmp(uart_read_buf, "CALVIN1") == 0) {
calibrate_vin(1, parse_millinum(uart_read_buf+idx+1), state.vin_raw, &cfg_system.vin_adc);
} else if (strcmp(uart_read_buf, "CALVIN2") == 0) {
calibrate_vin(2, parse_millinum(uart_read_buf+idx+1), state.vin_raw, &cfg_system.vin_adc);
} else if (strcmp(uart_read_buf, "CALVOUT1") == 0) {
calibrate_vout(1, parse_millinum(uart_read_buf+idx+1), state.vout_raw, &cfg_system.vout_adc, &cfg_system.vout_pwm);
} else if (strcmp(uart_read_buf, "CALVOUT2") == 0) {
calibrate_vout(2, parse_millinum(uart_read_buf+idx+1), state.vout_raw, &cfg_system.vout_adc, &cfg_system.vout_pwm);
} else if (strcmp(uart_read_buf, "CALCOUT1") == 0) {
calibrate_cout(1, parse_millinum(uart_read_buf+idx+1), state.cout_raw, &cfg_system.cout_adc, &cfg_system.cout_pwm);
} else if (strcmp(uart_read_buf, "CALCOUT2") == 0) {
calibrate_cout(2, parse_millinum(uart_read_buf+idx+1), state.cout_raw, &cfg_system.cout_adc, &cfg_system.cout_pwm); */
} else if (strcmp(uart_read_buf, "CALVINADCA") == 0) {
parse_uint("VIN ADC A", &cfg_system.vin_adc.a, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALVINADCB") == 0) {
parse_uint("VIN ADC B", &cfg_system.vin_adc.b, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALVOUTADCA") == 0) {
parse_uint("ADC VOUT A", &cfg_system.vout_adc.a, uart_read_buf+idx+1);
parse_uint("VOUT ADC A", &cfg_system.vout_adc.a, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALVOUTADCB") == 0) {
parse_uint("ADC VOUT B", &cfg_system.vout_adc.b, uart_read_buf+idx+1);
parse_uint("VOUT ADC B", &cfg_system.vout_adc.b, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALVOUTPWMA") == 0) {
parse_uint("PWM VOUT A", &cfg_system.vout_pwm.a, uart_read_buf+idx+1);
parse_uint("VOUT PWM A", &cfg_system.vout_pwm.a, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALVOUTPWMB") == 0) {
parse_uint("PWM VOUT B", &cfg_system.vout_pwm.b, uart_read_buf+idx+1);
parse_uint("VOUT PWM B", &cfg_system.vout_pwm.b, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALCOUTADCA") == 0) {
parse_uint("ADC COUT A", &cfg_system.cout_adc.a, uart_read_buf+idx+1);
parse_uint("COUT ADC A", &cfg_system.cout_adc.a, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALCOUTADCB") == 0) {
parse_uint("ADC COUT B", &cfg_system.cout_adc.b, uart_read_buf+idx+1);
parse_uint("COUT ADC B", &cfg_system.cout_adc.b, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALCOUTPWMA") == 0) {
parse_uint("PWM COUT A", &cfg_system.cout_pwm.a, uart_read_buf+idx+1);
parse_uint("COUT PWM A", &cfg_system.cout_pwm.a, uart_read_buf+idx+1);
} else if (strcmp(uart_read_buf, "CALCOUTPWMB") == 0) {
parse_uint("PWM COUT B", &cfg_system.cout_pwm.b, uart_read_buf+idx+1);
parse_uint("COUT PWM B", &cfg_system.cout_pwm.b, uart_read_buf+idx+1);
} else {
uart_write_str("UNKNOWN COMMAND!\r\n");
}
Expand Down Expand Up @@ -454,7 +446,7 @@ inline void pinout_init()
// PC6 is Iout control, output
// PC7 is Button 1, input
PC_ODR = 0;
PC_DDR = (1<<5) || (1<<6);
PC_DDR = (1<<5) | (1<<6);
PC_CR1 = (1<<7); // For the button
PC_CR2 = (1<<5) | (1<<6);

Expand All @@ -477,20 +469,24 @@ void config_load(void)
else
cfg_system.output = 0;

#if DEBUG
state.pc3 = 1;
#endif
}

void read_state(void)
{
uint8_t tmp;

#if DEBUG
tmp = (PC_IDR & (1<<3)) ? 1 : 0;
if (state.pc3 != tmp) {
uart_write_str("PC3 is now ");
uart_write_ch('0' + tmp);
uart_write_str("\r\n");
state.pc3 = tmp;
}
#endif

tmp = (PB_IDR & (1<<5)) ? 1 : 0;
if (state.constant_current != tmp) {
Expand Down Expand Up @@ -526,10 +522,10 @@ void read_state(void)
uint8_t ch3;
uint8_t ch4;

ch1 = '0' + (val / 10000) % 10;
ch2 = '0' + (val / 1000) % 10;
ch3 = '0' + (val / 100) % 10;
ch4 = '0' + (val / 10 ) % 10;
ch1 = '0' + (state.vin / 10000) % 10;
ch2 = '0' + (state.vin / 1000) % 10;
ch3 = '0' + (state.vin / 100) % 10;
ch4 = '0' + (state.vin / 10 ) % 10;

display_show(ch1, 0, ch2, 1, ch3, 0, ch4, 0);
}
Expand Down