diff --git a/STM32/TSYS_controller/Makefile b/STM32/TSYS_controller/Makefile index 72e4685..0701667 100644 --- a/STM32/TSYS_controller/Makefile +++ b/STM32/TSYS_controller/Makefile @@ -13,6 +13,11 @@ DEFS := -DUSARTNUM=1 -DI2CPINS=67 # for example, if you have STM32F103VBT6, you should write: LDSCRIPT = stm32f042x6.ld +# autoincremental version & build date +VERSION_FILE := version.inc +NEXTVER := $(shell expr $$(awk '/#define BUILD_NUMBER/' $(VERSION_FILE) | tr -cd "[0-9]") + 1) +BUILDDATE := $(shell date +%Y-%m-%d) + INDEPENDENT_HEADERS= FP_FLAGS ?= -msoft-float @@ -56,7 +61,7 @@ LIB_DIR := $(INC_DIR)/ld ############################################################################### # C flags CFLAGS += -O2 -g -D__thumb2__=1 -CFLAGS += -Wall -Wextra -Wshadow -Wimplicit-function-declaration +CFLAGS += -Wall -Werror -Wextra -Wshadow -Wimplicit-function-declaration CFLAGS += -Wredundant-decls $(INCLUDE) # -Wmissing-prototypes -Wstrict-prototypes CFLAGS += -fno-common -ffunction-sections -fdata-sections @@ -72,8 +77,8 @@ LDFLAGS += -Wl,--gc-sections ############################################################################### # Used libraries -LDLIBS += -Wl,--start-group -lc -lgcc -Wl,--end-group -LDLIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) +#LDLIBS += -Wl,--start-group -lc -lgcc -Wl,--end-group +#LDLIBS += $(shell $(CC) $(CFLAGS) -print-libgcc-file-name) DEFS += -DSTM32$(FAMILY) -DSTM32$(MCU) @@ -103,8 +108,16 @@ $(OBJDIR): $(STARTUP): $(INC_DIR)/startup/vector.c $(CC) $(CFLAGS) $(DEFS) $(INCLUDE) $(ARCH_FLAGS) -o $@ -c $< +$(VERSION_FILE): *.[ch] + @echo " Generate version: $(NEXTVER) for date $(BUILDDATE)" + @sed -i "s/#define BUILD_NUMBER.*/#define BUILD_NUMBER \"$(NEXTVER)\"/" $(VERSION_FILE) + @sed -i "s/#define BUILD_DATE.*/#define BUILD_DATE \"$(BUILDDATE)\"/" $(VERSION_FILE) + +$(OBJDIR)/proto.o: proto.c $(VERSION_FILE) + $(OBJDIR)/%.o: %.c - @echo " CC $<" + @make $(VERSION_FILE) + @echo " CC $<" $(CC) $(CFLAGS) -MD $(DEFS) $(INCLUDE) $(ARCH_FLAGS) -o $@ -c $< $(BIN): $(ELF) @@ -120,7 +133,7 @@ $(LIST): $(ELF) $(OBJDUMP) -S $(ELF) > $(LIST) $(ELF): $(OBJDIR) $(OBJS) - @echo " LD $(ELF)" + @echo " LD $(ELF)" $(LD) $(LDFLAGS) $(ARCH_FLAGS) $(OBJS) $(LDLIBS) -o $(ELF) @size $(ELF) @@ -141,7 +154,4 @@ boot: $(BIN) @echo " LOAD $(BIN) through bootloader" $(STBOOT) -b$(BOOTSPEED) $(BOOTPORT) -w $(BIN) -gentags: - CFLAGS="$(CFLAGS) $(DEFS)" geany -g $(BINARY).c.tags *[hc] 2>/dev/null - -.PHONY: clean flash boot dfuboot gentags +.PHONY: clean flash boot dfuboot diff --git a/STM32/TSYS_controller/Readme.md b/STM32/TSYS_controller/Readme.md index cf659f9..0feff0f 100644 --- a/STM32/TSYS_controller/Readme.md +++ b/STM32/TSYS_controller/Readme.md @@ -89,16 +89,16 @@ First (number zero) byte of every sequence is command mark (0xA5) or data mark ( ### Sensors state data format - byte 3 - Sstate value: -- - `[SENS_INITING]` = "init" -- - `[SENS_RESETING]` = "reset" -- - `[SENS_GET_COEFFS]` = "getcoeff" -- - `[SENS_SLEEPING]` = "sleep" -- - `[SENS_START_MSRMNT]` = "startmeasure" -- - `[SENS_WAITING]` = "waitresults" -- - `[SENS_GATHERING]` = "collectdata" -- - `[SENS_OFF]` = "off" -- - `[SENS_OVERCURNT]` = "overcurrent" -- - `[SENS_OVERCURNT_OFF]` = "offbyovercurrent" + - `[SENS_INITING]` = "init" + - `[SENS_RESETING]` = "reset" + - `[SENS_GET_COEFFS]` = "getcoeff" + - `[SENS_SLEEPING]` = "sleep" + - `[SENS_START_MSRMNT]` = "startmeasure" + - `[SENS_WAITING]` = "waitresults" + - `[SENS_GATHERING]` = "collectdata" + - `[SENS_OFF]` = "off" + - `[SENS_OVERCURNT]` = "overcurrent" + - `[SENS_OVERCURNT_OFF]` = "offbyovercurrent" - byte 4 - `sens_present[0]` value - byte 5 - `sens_present[1]` value - byte 6 - `Nsens_present` value diff --git a/STM32/TSYS_controller/can.c b/STM32/TSYS_controller/can.c index b638715..561c367 100644 --- a/STM32/TSYS_controller/can.c +++ b/STM32/TSYS_controller/can.c @@ -20,11 +20,14 @@ * MA 02110-1301, USA. * */ + #include // memcpy + #include "can.h" #include "hardware.h" #include "proto.h" + // incoming message buffer size #define CAN_INMESSAGE_SIZE (6) @@ -237,7 +240,7 @@ static void can_process_fifo(uint8_t fifo_num){ uint32_t *dptr = (uint32_t*)msg.data; dptr[0] = dptr[1] = 0; } - uint8_t len = box->RDTR & 0x7; + uint8_t len = box->RDTR & 0x0f; msg.length = len; msg.ID = box->RIR >> 21; if(len){ // message can be without data diff --git a/STM32/TSYS_controller/can_process.c b/STM32/TSYS_controller/can_process.c index f9bea8a..e6aa695 100644 --- a/STM32/TSYS_controller/can_process.c +++ b/STM32/TSYS_controller/can_process.c @@ -100,7 +100,7 @@ void can_messages_proc(){ newline(); sendbuf(); } // don't process alien messages - if(can_mesg->ID != CANID || can_mesg->ID != BCAST_ID) return; + if(can_mesg->ID != CANID && can_mesg->ID != BCAST_ID) return; int16_t t; if(data[0] == COMMAND_MARK){ // process commands if(len < 2) return; diff --git a/STM32/TSYS_controller/main.c b/STM32/TSYS_controller/main.c index 8f745d0..affe161 100644 --- a/STM32/TSYS_controller/main.c +++ b/STM32/TSYS_controller/main.c @@ -75,23 +75,11 @@ int main(void){ adc_setup(); usart_setup(); i2c_setup(LOW_SPEED); - CAN_setup(0); // setup with default 250kbaud -/* - SEND("Greetings! My address is "); - printuhex(CANID); - newline(); - - if(RCC->CSR & RCC_CSR_IWDGRSTF){ // watchdog reset occured - SEND("WDGRESET=1\n"); - } - if(RCC->CSR & RCC_CSR_SFTRSTF){ // software reset occured - SEND("SOFTRESET=1\n"); - } - */ - RCC->CSR |= RCC_CSR_RMVF; // remove reset flags - USB_setup(); readCANID(); if(CANID == MASTER_ID) cansniffer = 1; // MASTER in sniffer mode by default + CAN_setup(0); // setup with default 250kbaud + RCC->CSR |= RCC_CSR_RMVF; // remove reset flags + USB_setup(); iwdg_setup(); while (1){ diff --git a/STM32/TSYS_controller/proto.c b/STM32/TSYS_controller/proto.c index f84bf55..716d530 100644 --- a/STM32/TSYS_controller/proto.c +++ b/STM32/TSYS_controller/proto.c @@ -20,6 +20,9 @@ * MA 02110-1301, USA. * */ + +#include + #include "adc.h" #include "can.h" #include "can_process.h" @@ -28,7 +31,7 @@ #include "sensors_manage.h" #include "usart.h" #include "usb.h" -#include // strlen, strcpy( +#include "version.inc" extern volatile uint8_t canerror; @@ -347,6 +350,7 @@ void cmd_parser(char *txt, uint8_t isUSB){ newline(); break; default: // help + SEND("https://github.com/eddyem/tsys01/tree/master/STM32/TSYS_controller build#" BUILD_NUMBER " @ " BUILD_DATE "\n"); SEND( "ALL little letters - without CAN messaging\n" "0..7 - send command to given controller (0 - this) instead of broadcast\n" @@ -388,8 +392,9 @@ void printu(uint32_t val){ *(--bufptr) = '0'; }else{ while(val){ - *(--bufptr) = val % 10 + '0'; + register uint32_t o = val; val /= 10; + *(--bufptr) = (o - 10*val) + '0'; } } addtobuf(bufptr); @@ -399,8 +404,13 @@ void printu(uint32_t val){ void printuhex(uint32_t val){ addtobuf("0x"); uint8_t *ptr = (uint8_t*)&val + 3; - int i, j; + int i, j, z = 1; for(i = 0; i < 4; ++i, --ptr){ + if(*ptr == 0){ // omit leading zeros + if(i == 3) z = 0; + if(z) continue; + } + else z = 0; for(j = 1; j > -1; --j){ uint8_t half = (*ptr >> (4*j)) & 0x0f; if(half < 10) bufputchar(half + '0'); diff --git a/STM32/TSYS_controller/sensors_manage.c b/STM32/TSYS_controller/sensors_manage.c index 6ba2656..63e485f 100644 --- a/STM32/TSYS_controller/sensors_manage.c +++ b/STM32/TSYS_controller/sensors_manage.c @@ -23,7 +23,7 @@ #include "sensors_manage.h" #include "can_process.h" #include "i2c.h" -#include "proto.h" // addtobuf, bufputchar +#include "proto.h" // addtobuf, bufputchar, memcpy extern volatile uint32_t Tms; uint8_t sensors_scan_mode = 0; // infinite scan mode @@ -61,6 +61,7 @@ const char *sensors_get_statename(SensorsState x){ return statenames[x]; } +const double mul[5] = {-1.5e-2, 1., -2., 4., -2.}; /** * Get temperature & calculate it by polinome * T = (-2) * k4 * 10^{-21} * ADC16^4 @@ -81,7 +82,6 @@ static uint16_t calc_t(uint32_t t, int i){ int j; double d = (double)t/256., tmp = 0.; // k0*(-1.5e-2) + 0.1*1e-5*val*(1*k1 + 1e-5*val*(-2.*k2 + 1e-5*val*(4*k3 + 1e-5*val*(-2*k4)))) - const double mul[5] = {-1.5e-2, 1., -2., 4., -2.}; for(j = 4; j > 0; --j){ tmp += mul[j] * (double)coeff[j]; tmp *= 1e-5*d; diff --git a/STM32/TSYS_controller/tsys01.bin b/STM32/TSYS_controller/tsys01.bin index d11a383..b5e88b7 100755 Binary files a/STM32/TSYS_controller/tsys01.bin and b/STM32/TSYS_controller/tsys01.bin differ diff --git a/STM32/TSYS_controller/usart.c b/STM32/TSYS_controller/usart.c index b79aa84..d1ccc0b 100644 --- a/STM32/TSYS_controller/usart.c +++ b/STM32/TSYS_controller/usart.c @@ -18,10 +18,13 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. */ + +#include + #include "stm32f0.h" #include "hardware.h" #include "usart.h" -#include + extern volatile uint32_t Tms; static int datalen[2] = {0,0}; // received data line length (including '\n') diff --git a/STM32/TSYS_controller/usb.c b/STM32/TSYS_controller/usb.c index f7aeb4c..03cd614 100644 --- a/STM32/TSYS_controller/usb.c +++ b/STM32/TSYS_controller/usb.c @@ -21,10 +21,11 @@ * */ +#include + +#include "usart.h" #include "usb.h" #include "usb_lib.h" -#include // memcpy, memmove -#include "usart.h" // incoming buffer size #define IDATASZ (256) diff --git a/STM32/TSYS_controller/usb_lib.c b/STM32/TSYS_controller/usb_lib.c index 3b1e3e8..4b1cbcb 100644 --- a/STM32/TSYS_controller/usb_lib.c +++ b/STM32/TSYS_controller/usb_lib.c @@ -20,11 +20,13 @@ * MA 02110-1301, USA. * */ -#include "proto.h" + +#include // memcpy + +#include "stm32f0.h" #include "usart.h" #include "usb_lib.h" -#include -#include // memcpy + #ifdef EBUG #undef EBUG @@ -146,17 +148,17 @@ _USB_STRING_(USB_StringProdDescriptor, u"USB-Serial Controller"); */ // SET_LINE_CODING void WEAK linecoding_handler(usb_LineCoding __attribute__((unused)) *lc){ - MSG("linecoding_handler\n"); + //MSG("linecoding_handler\n"); } // SET_CONTROL_LINE_STATE void WEAK clstate_handler(uint16_t __attribute__((unused)) val){ - MSG("clstate_handler\n"); + //MSG("clstate_handler\n"); } // SEND_BREAK void WEAK break_handler(){ - MSG("break_handler\n"); + //MSG("break_handler\n"); } // handler of vendor requests diff --git a/STM32/TSYS_controller/version.inc b/STM32/TSYS_controller/version.inc new file mode 100644 index 0000000..a6bd452 --- /dev/null +++ b/STM32/TSYS_controller/version.inc @@ -0,0 +1,2 @@ +#define BUILD_NUMBER "28" +#define BUILD_DATE "2022-01-31"