From f8bbd0e754ec7cfa2e99792ee0cb16fe43813785 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sun, 24 Apr 2022 21:47:05 -0700 Subject: [PATCH 1/6] use family independent main.h to support l1.. --- board/hal_init.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/board/hal_init.c b/board/hal_init.c index 26dfde1..28588dd 100644 --- a/board/hal_init.c +++ b/board/hal_init.c @@ -1,4 +1,4 @@ -#include +#include #include From ce1ca7d634e00c667b7e93463920a21dd0f3baf5 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Sun, 24 Apr 2022 21:47:39 -0700 Subject: [PATCH 2/6] comment which pins for programming to save me looking at the pdf.. --- LORA-IRR.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/LORA-IRR.md b/LORA-IRR.md index c49153b..cbeb421 100644 --- a/LORA-IRR.md +++ b/LORA-IRR.md @@ -126,6 +126,8 @@ Pins The [pinout guide for the Node151](https://resource.heltec.cn/download/LoRa_Node_151/LoRa_Node_151_Pinout_Diagram.pdf). +The pins PA13(SWDIO)/14(SWCLK) are used for programming. + The default pins PB5-7,9 are used as active low controls for the relays. They are mapped to channels 0 through 3 respectively. The LED on PB8 is mapped to channel 4. This is useful for testing if a command works From f6515c235530e44c713476bbcdd709f2f92cad74 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Mon, 25 Apr 2022 11:35:47 -0700 Subject: [PATCH 3/6] switch to main.h so it pulls in the correct hal headers.. --- hal_generic.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hal_generic.c b/hal_generic.c index 5b07228..060da2f 100644 --- a/hal_generic.c +++ b/hal_generic.c @@ -1,4 +1,4 @@ -#include +#include void SysTick_Handler(void) From cf78ec45317988c2e91b06ec07a65b406bf52d5b Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Mon, 25 Apr 2022 11:39:01 -0700 Subject: [PATCH 4/6] add target to generate list of sysinits used This is to help make sure everything you think is starting is.. --- mk/mu.progs.mk | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mk/mu.progs.mk b/mk/mu.progs.mk index 75dc553..65ec284 100644 --- a/mk/mu.progs.mk +++ b/mk/mu.progs.mk @@ -34,7 +34,7 @@ PROGEXT = .elf DEPENDS += .arm_deps .for i in $(PROGS) -ALLTGTS+= $(i)$(PROGEXT) $(i).list +ALLTGTS+= $(i)$(PROGEXT) $(i).list $(i).sysinit ASRCS.$(i) = $(SRCS) $(SRCS.$(i)) OBJS.$(i) = $(ASRCS.$(i):C/.c$/.o/) @@ -43,6 +43,9 @@ ARM_DEP_SRCS+= $(ASRCS.$(i)) $(i)$(PROGEXT) $(i).map: $(OBJS.$(i)) $(ARMCC) $(ARMTARGET) -o $(i)$(PROGEXT) $(.ALLSRC) -T$(LINKER_SCRIPT) --specs=nosys.specs -Wl,-Map="$(i).map" -Wl,--gc-sections -static --specs=nano.specs -Wl,--start-group -lc -lm -Wl,--end-group +$(i).sysinit: $(ASRCS.$(i)) + grep ^SYSINIT $(.ALLSRC) | sort -k 2 -k 3 > $@ || (rm -f $@ && false) + $(i).list: $(i)$(PROGEXT) $(ARMOBJDUMP) -h -S $(.ALLSRC) > $@ || (rm -f $@ && false) .endfor From d78bc3510ec12a592bc714d5dbed72bf72ed6756 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Mon, 25 Apr 2022 20:09:35 -0700 Subject: [PATCH 5/6] make generic, but turns out unneeded on NODE151.. --- si_usb.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/si_usb.c b/si_usb.c index e8521b8..9494520 100644 --- a/si_usb.c +++ b/si_usb.c @@ -7,8 +7,7 @@ #include -#include - +#include #include static void From e70b76111fd772fc257e814c5d28b1c35f0f5e27 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Mon, 25 Apr 2022 20:18:08 -0700 Subject: [PATCH 6/6] convert the irrigation project to sysinits... minor improvement to make sysinit_run a constructor, so don't need to manually call it anymore... This required updating the linker script for the l1 part to include the linkerset section.. --- Makefile | 7 ++++- board.c | 13 ++------ .../strobe_l1_rng_init.c | 4 +++ irr_main.c | 24 +++++++-------- main.c | 30 +++++++++++++++---- mk/boards.mk | 8 ++++- rs485hid/rs485gw.c | 2 -- rs485hid/rs485hid.c | 2 -- stm32/l151ccux/STM32L151CCUX_FLASH.ld | 6 ++++ stm32/l151ccux/stm32l1xx_it.c | 14 --------- sysinit.c | 2 +- 11 files changed, 64 insertions(+), 48 deletions(-) rename strobe_rng_init.c => board/strobe_l1_rng_init.c (94%) diff --git a/Makefile b/Makefile index 5e892c9..fa55bdc 100644 --- a/Makefile +++ b/Makefile @@ -38,10 +38,11 @@ PROGS = lora.gw lora.irr SRCS.lora.gw = main.c SRCS.lora.gw+= $(SRCS.NODE151) +SRCS.lora.gw+= $(SRCS.USB_CDC) +SRCS.lora.gw+= $(SRCS.USB) SRCS.lora.irr = irr_main.c SRCS.lora.irr+= comms.c -SRCS.lora.irr+= strobe_rng_init.c SRCS.lora.irr+= $(STROBE_SRCS) SRCS.lora.irr+= $(SRCS.NODE151) @@ -52,6 +53,10 @@ CFLAGS+= -I$(.OBJDIR) # for shared_key.h WITH_STROBE=yes +# XXX - bsd.mkopt.mk only allows an option to have one upstream, I want +# more. +WITH_HAL_INIT=yes + WITH_SX1276=yes WITH_NODE151=yes diff --git a/board.c b/board.c index e1449c8..f9a3150 100644 --- a/board.c +++ b/board.c @@ -44,6 +44,8 @@ #endif #include "board.h" +#include + #include /*! @@ -123,8 +125,6 @@ void BoardInitMcu( void ) { if( McuInitialized == false ) { - HAL_Init( ); - #if 0 // LEDs GpioInit( &Led1, LED_1, PIN_OUTPUT, PIN_PUSH_PULL, PIN_NO_PULL, 0 ); @@ -193,8 +193,6 @@ void BoardInitMcu( void ) SystemClockReConfig( ); } - MX_USB_DEVICE_Init(); - AdcInit( &Adc, PB_15 ); // Just initialize ADC /* setup routing */ @@ -233,6 +231,7 @@ void BoardInitMcu( void ) #endif } } +SYSINIT_VF(initmcu, SI_SUB_HAL, SI_ORDER_SECOND, BoardInitMcu); void BoardResetMcu( void ) { @@ -476,12 +475,6 @@ void SystemClockReConfig( void ) } } -void SysTick_Handler( void ) -{ - HAL_IncTick( ); - HAL_SYSTICK_IRQHandler( ); -} - uint8_t GetBoardPowerSource( void ) { if( UsbIsConnected == false ) diff --git a/strobe_rng_init.c b/board/strobe_l1_rng_init.c similarity index 94% rename from strobe_rng_init.c rename to board/strobe_l1_rng_init.c index 9009906..a989b87 100644 --- a/strobe_rng_init.c +++ b/board/strobe_l1_rng_init.c @@ -33,6 +33,8 @@ #include +#include + #define nitems(x) (sizeof(x) / sizeof *(x)) #define DEFINE_RNG_SAVE 1 #if DEFINE_RNG_SAVE @@ -58,6 +60,7 @@ strobe_rng_init(void) strobe_seed_prng(sbrk(0), 2*1024); } +SYSINIT_VF(rng_init, SI_SUB_HAL, SI_ORDER_LAST, strobe_rng_init); void strobe_rng_save(void) @@ -85,3 +88,4 @@ strobe_rng_save(void) __set_PRIMASK(primask); } +SYSINIT_VF(rng_save, SI_SUB_LAST, SI_ORDER_LAST, strobe_rng_save); diff --git a/irr_main.c b/irr_main.c index b1379ad..8057313 100644 --- a/irr_main.c +++ b/irr_main.c @@ -38,6 +38,8 @@ #include #include +#include + enum { CMD_TERMINATE = 1, CMD_WAITFOR = 2, @@ -134,6 +136,7 @@ radio_seed_rng(void) } #endif } +SYSINIT_VF(radio_seed_rng, SI_SUB_STANDARD, SI_ORDER_ANY, radio_seed_rng); static void analog_seed_rng(void) @@ -155,6 +158,7 @@ analog_seed_rng(void) } #endif } +SYSINIT_VF(analog_seed_rng, SI_SUB_STANDARD, SI_ORDER_ANY, analog_seed_rng); static inline uint32_t letoh_32(uint8_t *v) @@ -206,6 +210,7 @@ setup_gpio() set_chan(i, chans[i].init); } } +SYSINIT_VF(setup_gpio, SI_SUB_HAL, SI_ORDER_LAST, setup_gpio); static struct sched { uint32_t cmd; @@ -339,23 +344,18 @@ procmsg(struct pktbuf inbuf, struct pktbuf *outbuf) outbuf->pktlen = 1; } -int -main() +void +radio_init(void) { - strobe_rng_init(); - - BoardInitMcu(); - Radio.Init(&revents); +} - analog_seed_rng(); - - radio_seed_rng(); - - strobe_rng_save(); +SYSINIT_VF(radio_init, SI_SUB_HAL, SI_ORDER_MIDDLE, radio_init); - setup_gpio(); +int +main() +{ /* turn on LED */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET); diff --git a/main.c b/main.c index a2c829c..33db9eb 100644 --- a/main.c +++ b/main.c @@ -24,6 +24,7 @@ * */ +#include #include #include @@ -37,6 +38,20 @@ /* lora-irr headers */ #include +#include + +/* Start up USB */ +SYSINIT_VF(usb_cdc, SI_SUB_USB, SI_ORDER_MIDDLE, MX_USB_DEVICE_Init); + +/* XXX - where's a better place? */ +extern PCD_HandleTypeDef hpcd_USB_FS; + +void +USB_LP_IRQHandler(void) +{ + + HAL_PCD_IRQHandler(&hpcd_USB_FS); +} char * findeol(char *pos, size_t len) @@ -187,16 +202,21 @@ process_line(char *start, char *end) fflush(vcp_usb); } +void +radio_init(void) +{ + + Radio.Init(&revents); +} + +SYSINIT_VF(radio_init, SI_SUB_STANDARD, SI_ORDER_ANY, radio_init); + int main(void) { debug_printf("starting...\n"); - BoardInitMcu(); - - Radio.Init(&revents); - /* turn on LED */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET); @@ -210,7 +230,7 @@ main(void) * STM32 Core USB library: * https://github.com/STMicroelectronics/STM32CubeL1/issues/10 */ - DelayMs(50); + DelayMs(100); usb_printf("starting...\r\n"); #endif diff --git a/mk/boards.mk b/mk/boards.mk index d41e7a3..e13b897 100644 --- a/mk/boards.mk +++ b/mk/boards.mk @@ -40,6 +40,11 @@ STROBE_SRCS+= strobe.c \ STROBE_SRCS+= strobe_f1_rng_init.c .endif +.if ${MK_STROBE} == "yes" && ${MK_NODE151} == "yes" +.PATH: $(SRCTOP)/board +STROBE_SRCS+= strobe_l1_rng_init.c +.endif + # LoRamac (SX1276) radio code .if ${MK_SX1276} == "yes" LORAMAC_SRC = $(SRCTOP)/loramac/src @@ -98,6 +103,7 @@ ARMTARGET?= -mcpu=cortex-m3 -mthumb LINKER_SCRIPT=$(STM32)/l151ccux/STM32L151CCUX_FLASH.ld SRCS+= \ + hal_generic.c \ startup_stm32l151ccux.s \ stm32l1xx_hal.c \ stm32l1xx_hal_adc.c \ @@ -174,7 +180,7 @@ SRCS.USB+= \ .endif .if ${MK_USB} == "yes" && ${MK_NODE151} == "yes" +# note that the Node151 does not need the USB reset hack in si_usb.c SRCS.USB_CDC+= \ - si_usb.c \ stm32l1xx_ll_usb.c .endif diff --git a/rs485hid/rs485gw.c b/rs485hid/rs485gw.c index 69a93e0..d7cb3c2 100644 --- a/rs485hid/rs485gw.c +++ b/rs485hid/rs485gw.c @@ -227,8 +227,6 @@ int main(void) { - sysinit_run(); - debug_printf("starting...\n"); #if 0 diff --git a/rs485hid/rs485hid.c b/rs485hid/rs485hid.c index 17d417b..42e51e1 100644 --- a/rs485hid/rs485hid.c +++ b/rs485hid/rs485hid.c @@ -413,8 +413,6 @@ main() debug_printf("starting..."); - sysinit_run(); - #if 0 /* turn on LED */ HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET); diff --git a/stm32/l151ccux/STM32L151CCUX_FLASH.ld b/stm32/l151ccux/STM32L151CCUX_FLASH.ld index fd3ff19..7b83eee 100755 --- a/stm32/l151ccux/STM32L151CCUX_FLASH.ld +++ b/stm32/l151ccux/STM32L151CCUX_FLASH.ld @@ -3,6 +3,9 @@ ** ** File : LinkerScript.ld ** + +XXX + ** Author : Auto-generated by STM32CubeIDE ** ** Abstract : Linker script for STM32L151CCUx Device from stm32l1 series @@ -101,6 +104,9 @@ SECTIONS *(.rodata) /* .rodata sections (constants, strings, etc.) */ *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ . = ALIGN(4); + __start_set_sysinit_set = .; + KEEP(*(set_sysinit_set)) /* sysinit linker sets */ + __stop_set_sysinit_set = .; } >FLASH .ARM.extab : { diff --git a/stm32/l151ccux/stm32l1xx_it.c b/stm32/l151ccux/stm32l1xx_it.c index 42b9928..6902a8a 100755 --- a/stm32/l151ccux/stm32l1xx_it.c +++ b/stm32/l151ccux/stm32l1xx_it.c @@ -184,20 +184,6 @@ void PendSV_Handler(void) /* please refer to the startup file (startup_stm32l1xx.s). */ /******************************************************************************/ -/** - * @brief This function handles USB low priority interrupt. - */ -void USB_LP_IRQHandler(void) -{ - /* USER CODE BEGIN USB_LP_IRQn 0 */ - - /* USER CODE END USB_LP_IRQn 0 */ - HAL_PCD_IRQHandler(&hpcd_USB_FS); - /* USER CODE BEGIN USB_LP_IRQn 1 */ - - /* USER CODE END USB_LP_IRQn 1 */ -} - /** * @brief This function handles TIM4 global interrupt. */ diff --git a/sysinit.c b/sysinit.c index bb81f13..0b8bdef 100644 --- a/sysinit.c +++ b/sysinit.c @@ -31,7 +31,7 @@ SET_DECLARE(sysinit_set, struct sysinit); -void +void __attribute__ ((constructor)) sysinit_run(void) { const int cnt = SET_COUNT(sysinit_set);