diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile | 19 | ||||
-rw-r--r-- | bin/Makefile | 5 | ||||
-rw-r--r-- | bin/STM32L476RG.ld | 184 | ||||
-rw-r--r-- | bin/stm32l476rg.ld | 155 | ||||
-rwxr-xr-x[-rw-r--r--] | inc/actuators.h | 7 | ||||
-rwxr-xr-x[-rw-r--r--] | inc/ctrl.h | 9 | ||||
-rwxr-xr-x | inc/test.h | 67 | ||||
-rwxr-xr-x[-rw-r--r--] | src/actuators.c | 200 | ||||
-rwxr-xr-x[-rw-r--r--] | src/ctrl.c | 202 | ||||
-rwxr-xr-x[-rw-r--r--] | src/main.c | 118 | ||||
-rwxr-xr-x[-rw-r--r--] | src/startup_stm32l476xx.s | 2 | ||||
-rwxr-xr-x[-rw-r--r--] | src/stm32l4xx_it.c | 0 | ||||
-rwxr-xr-x | src/test.c | 141 | ||||
m--------- | stm32l4-drivers | 0 | ||||
m--------- | stm32l4-modules | 0 | ||||
-rwxr-xr-x[-rw-r--r--] | tmp/Makefile | 22 |
17 files changed, 768 insertions, 364 deletions
@@ -6,4 +6,3 @@ *.elf *.hex *.bin -bin/sistemas-embebidos* @@ -1,9 +1,10 @@ #!/usr/bin/make -f -VERSION = 0 -PATCHLEVEL = .5 +VERSION = 1 +PATCHLEVEL = .0 SUBLEVEL = EXTRAVERSION = +NAME = little-bird export VERSION export PATCHLEVEL @@ -131,7 +132,7 @@ export SIZE # CFLAGS CFLAGS_STD = -std=c11 -CFLAGS_OPT = -Os +#CFLAGS_OPT = -Os CFLAGS_MCU = -mlittle-endian CFLAGS_MCU += -march=armv7e-m @@ -177,9 +178,9 @@ export LDFLAGS ################################################################################ -.PHONY: all stm32l4-drivers libalx stm32l4-modules objects target flash erase reset clean mrproper +.PHONY: all stm32l4-drivers libalx stm32l4-modules main target flash erase reset clean mrproper -all: stm32l4-drivers libalx stm32l4-modules objects target +all: target stm32l4-drivers: @echo ' MAKE drivers' @@ -189,15 +190,15 @@ libalx: @echo ' MAKE libalx' $(Q)$(MAKE) base -C $(LIBALX_DIR) -stm32l4-modules: +stm32l4-modules: stm32l4-drivers libalx @echo ' MAKE modules' $(Q)$(MAKE) -C $(MODULES_DIR) -objects: - @echo " MAKE objects" +main: libalx stm32l4-modules + @echo " MAKE main" $(Q)$(MAKE) -C $(TMP_DIR) -target: +target: main @echo " MAKE target" $(Q)$(MAKE) -C $(BIN_DIR) diff --git a/bin/Makefile b/bin/Makefile index 80a970a..e22f2fb 100644 --- a/bin/Makefile +++ b/bin/Makefile @@ -17,7 +17,7 @@ ALL = $(PROJECT_NAME).elf $(PROJECT_NAME).hex $(PROJECT_NAME).lst size MAIN_OBJ_MODULES = \ system_stm32l4xx.o stm32l4xx_it.o main.o \ - ctrl.o actuators.o + ctrl.o actuators.o test.o MAIN_OBJS = $(patsubst %,$(TMP_DIR)/%,$(MAIN_OBJ_MODULES)) @@ -55,7 +55,7 @@ $(PROJECT_NAME).elf: $(MAIN_OBJS) $(MAIN_LIBS) @echo " CC $@" $(Q)$(CC) $(CFLAGS) $(LDFLAGS) $(MAIN_OBJS) -o $@ \ $(SRC_DIR)/startup_stm32l476xx.s\ - $(STATIC_LIBS) $(LIBS) -TSTM32L476RG.ld + $(STATIC_LIBS) $(LIBS) -Tstm32l476rg.ld $(PROJECT_NAME).hex: $(PROJECT_NAME).elf @echo " OBJCOPY $@" @@ -77,7 +77,6 @@ size: $(PROJECT_NAME).elf clean: @echo " RM $(ALL)" $(Q)rm -f $(ALL) - $(Q)rm -f *.map ################################################################################ ######## End of file ########################################################### diff --git a/bin/STM32L476RG.ld b/bin/STM32L476RG.ld deleted file mode 100644 index 4543da3..0000000 --- a/bin/STM32L476RG.ld +++ /dev/null @@ -1,184 +0,0 @@ -/*
-*****************************************************************************
-**
-
-** File : LinkerScript.ld
-**
-** Abstract : Linker script for STM32L476RGTx Device with
-** 1024KByte FLASH, 128KByte RAM
-**
-** Set heap size, stack size and stack location according
-** to application requirements.
-**
-** Set memory bank area and size if external memory is used.
-**
-** Target : STMicroelectronics STM32
-**
-**
-** Distribution: The file is distributed as is, without any warranty
-** of any kind.
-**
-** (c)Copyright Ac6.
-** You may use this file as-is or modify it according to the needs of your
-** project. Distribution of this file (unmodified or modified) is not
-** permitted. Ac6 permit registered System Workbench for MCU users the
-** rights to distribute the assembled, compiled & linked contents of this
-** file as part of an application binary file, provided that it is built
-** using the System Workbench for MCU toolchain.
-**
-*****************************************************************************
-*/
-
-/* Entry Point */
-ENTRY(Reset_Handler)
-
-/* Highest address of the user mode stack */
-_estack = 0x20018000; /* end of RAM */
-/* Generate a link error if heap and stack don't fit into RAM */
-_Min_Heap_Size = 0x200; /* required amount of heap */
-_Min_Stack_Size = 0x400; /* required amount of stack */
-
-/* Specify the memory areas */
-MEMORY
-{
-RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K
-RAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K
-FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K
-}
-
-/* Define output sections */
-SECTIONS
-{
- /* The startup code goes first into FLASH */
- .isr_vector :
- {
- . = ALIGN(8);
- KEEP(*(.isr_vector)) /* Startup code */
- . = ALIGN(8);
- } >FLASH
-
- /* The program code and other data goes into FLASH */
- .text :
- {
- . = ALIGN(8);
- *(.text) /* .text sections (code) */
- *(.text*) /* .text* sections (code) */
- *(.glue_7) /* glue arm to thumb code */
- *(.glue_7t) /* glue thumb to arm code */
- *(.eh_frame)
-
- KEEP (*(.init))
- KEEP (*(.fini))
-
- . = ALIGN(8);
- _etext = .; /* define a global symbols at end of code */
- } >FLASH
-
- /* Constant data goes into FLASH */
- .rodata :
- {
- . = ALIGN(8);
- *(.rodata) /* .rodata sections (constants, strings, etc.) */
- *(.rodata*) /* .rodata* sections (constants, strings, etc.) */
- . = ALIGN(8);
- } >FLASH
-
- .ARM.extab :
- {
- . = ALIGN(8);
- *(.ARM.extab* .gnu.linkonce.armextab.*)
- . = ALIGN(8);
- } >FLASH
- .ARM : {
- . = ALIGN(8);
- __exidx_start = .;
- *(.ARM.exidx*)
- __exidx_end = .;
- . = ALIGN(8);
- } >FLASH
-
- .preinit_array :
- {
- . = ALIGN(8);
- PROVIDE_HIDDEN (__preinit_array_start = .);
- KEEP (*(.preinit_array*))
- PROVIDE_HIDDEN (__preinit_array_end = .);
- . = ALIGN(8);
- } >FLASH
-
- .init_array :
- {
- . = ALIGN(8);
- PROVIDE_HIDDEN (__init_array_start = .);
- KEEP (*(SORT(.init_array.*)))
- KEEP (*(.init_array*))
- PROVIDE_HIDDEN (__init_array_end = .);
- . = ALIGN(8);
- } >FLASH
- .fini_array :
- {
- . = ALIGN(8);
- PROVIDE_HIDDEN (__fini_array_start = .);
- KEEP (*(SORT(.fini_array.*)))
- KEEP (*(.fini_array*))
- PROVIDE_HIDDEN (__fini_array_end = .);
- . = ALIGN(8);
- } >FLASH
-
- /* used by the startup to initialize data */
- _sidata = LOADADDR(.data);
-
- /* Initialized data sections goes into RAM, load LMA copy after code */
- .data :
- {
- . = ALIGN(8);
- _sdata = .; /* create a global symbol at data start */
- *(.data) /* .data sections */
- *(.data*) /* .data* sections */
-
- . = ALIGN(8);
- _edata = .; /* define a global symbol at data end */
- } >RAM AT> FLASH
-
-
- /* Uninitialized data section */
- . = ALIGN(4);
- .bss :
- {
- /* This is used by the startup in order to initialize the .bss secion */
- _sbss = .; /* define a global symbol at bss start */
- __bss_start__ = _sbss;
- *(.bss)
- *(.bss*)
- *(COMMON)
-
- . = ALIGN(4);
- _ebss = .; /* define a global symbol at bss end */
- __bss_end__ = _ebss;
- } >RAM
-
- /* User_heap_stack section, used to check that there is enough RAM left */
- ._user_heap_stack :
- {
- . = ALIGN(8);
- PROVIDE ( end = . );
- PROVIDE ( _end = . );
- . = . + _Min_Heap_Size;
- . = . + _Min_Stack_Size;
- . = ALIGN(8);
- } >RAM
-
-
-
- /* Remove information from the standard libraries */
- /DISCARD/ :
- {
- libc.a ( * )
- libm.a ( * )
- libgcc.a ( * )
- }
-
- .ARM.attributes 0 : { *(.ARM.attributes) }
-}
-
-
diff --git a/bin/stm32l476rg.ld b/bin/stm32l476rg.ld new file mode 100644 index 0000000..92ded50 --- /dev/null +++ b/bin/stm32l476rg.ld @@ -0,0 +1,155 @@ +/******************************************************************************
+ *
+ * STM32L476RGTx
+ * 1024KByte FLASH, 128KByte RAM
+ *
+ * Set heap size, stack size and stack location to application requirements.
+ *
+ * Set memory bank area and size if external memory is used.
+ *
+ ******************************************************************************/
+
+/* Entry Point */
+ENTRY(Reset_Handler)
+
+/* Highest address of the user mode stack */
+_estack = 0x20018000; /* end of RAM */
+/* Generate a link error if heap and stack don't fit into RAM */
+_Min_Heap_Size = 0x200; /* required amount of heap */
+_Min_Stack_Size = 0x400; /* required amount of stack */
+
+/* Specify the memory areas */
+MEMORY
+{
+ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 96K
+ RAM2 (xrw) : ORIGIN = 0x10000000, LENGTH = 32K
+ FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K
+}
+
+/* Define output sections */
+SECTIONS
+{
+ /* The startup code goes first into FLASH */
+ .isr_vector : {
+ . = ALIGN(8);
+ KEEP(*(.isr_vector)) /* Startup code */
+ . = ALIGN(8);
+ } >FLASH
+
+ /* The program code and other data goes into FLASH */
+ .text : {
+ . = ALIGN(8);
+ *(.text) /* .text sections (code) */
+ *(.text*) /* .text* sections (code) */
+ *(.glue_7) /* glue arm to thumb code */
+ *(.glue_7t) /* glue thumb to arm code */
+ *(.eh_frame)
+
+ KEEP (*(.init))
+ KEEP (*(.fini))
+
+ . = ALIGN(8);
+ _etext = .; /* define a global symbols at end of code */
+ } >FLASH
+
+ /* Constant data goes into FLASH */
+ .rodata : {
+ . = ALIGN(8);
+ *(.rodata) /* .rodata sections (constants, strings, etc.) */
+ *(.rodata*) /* .rodata* sections (constants, strings, etc.) */
+ . = ALIGN(8);
+ } >FLASH
+
+ .ARM.extab : {
+ . = ALIGN(8);
+ *(.ARM.extab* .gnu.linkonce.armextab.*)
+ . = ALIGN(8);
+ } >FLASH
+ .ARM : {
+ . = ALIGN(8);
+ __exidx_start = .;
+ *(.ARM.exidx*)
+ __exidx_end = .;
+ . = ALIGN(8);
+ } >FLASH
+
+ .preinit_array : {
+ . = ALIGN(8);
+ PROVIDE_HIDDEN (__preinit_array_start = .);
+ KEEP (*(.preinit_array*))
+ PROVIDE_HIDDEN (__preinit_array_end = .);
+ . = ALIGN(8);
+ } >FLASH
+
+ .init_array : {
+ . = ALIGN(8);
+ PROVIDE_HIDDEN (__init_array_start = .);
+ KEEP (*(SORT(.init_array.*)))
+ KEEP (*(.init_array*))
+ PROVIDE_HIDDEN (__init_array_end = .);
+ . = ALIGN(8);
+ } >FLASH
+ .fini_array : {
+ . = ALIGN(8);
+ PROVIDE_HIDDEN (__fini_array_start = .);
+ KEEP (*(SORT(.fini_array.*)))
+ KEEP (*(.fini_array*))
+ PROVIDE_HIDDEN (__fini_array_end = .);
+ . = ALIGN(8);
+ } >FLASH
+
+ /* used by the startup to initialize data */
+ _sidata = LOADADDR(.data);
+
+ /* Initialized data sections goes into RAM, load LMA copy after code */
+ .data : {
+ . = ALIGN(8);
+ _sdata = .; /* create a global symbol at data start */
+ *(.data) /* .data sections */
+ *(.data*) /* .data* sections */
+
+ . = ALIGN(8);
+ _edata = .; /* define a global symbol at data end */
+ } >RAM AT> FLASH
+
+
+ /* Uninitialized data section */
+ . = ALIGN(4);
+ .bss : {
+ /* This is used by the startup in order to initialize the .bss secion */
+ _sbss = .; /* define a global symbol at bss start */
+ __bss_start__ = _sbss;
+ *(.bss)
+ *(.bss*)
+ *(COMMON)
+
+ . = ALIGN(4);
+ _ebss = .; /* define a global symbol at bss end */
+ __bss_end__ = _ebss;
+ } >RAM
+
+ /* User_heap_stack section, used to check that there is enough RAM left */
+ ._user_heap_stack : {
+ . = ALIGN(8);
+ PROVIDE ( end = . );
+ PROVIDE ( _end = . );
+ . = . + _Min_Heap_Size;
+ . = . + _Min_Stack_Size;
+ . = ALIGN(8);
+ } >RAM
+
+
+
+ /* Remove information from the standard libraries */
+ /DISCARD/ : {
+ libc.a ( * )
+ libm.a ( * )
+ libgcc.a ( * )
+ }
+
+ .ARM.attributes 0 : {
+ *(.ARM.attributes)
+ }
+}
+
+/* EOF */
diff --git a/inc/actuators.h b/inc/actuators.h index 3283fbd..4baacc0 100644..100755 --- a/inc/actuators.h +++ b/inc/actuators.h @@ -33,7 +33,8 @@ /****************************************************************************** ******* macros *************************************************************** ******************************************************************************/ - # define REFRESH_FREQ (50u) + # define ACT_REFRESH_PERIOD_US (20000u) + # define ACT_TASK_MODE_IT (false) /****************************************************************************** @@ -55,8 +56,8 @@ ******* functions ************************************************************ ******************************************************************************/ int proc_actuators_init (void); -int proc_actuators_1 (void); -int proc_actuators_2 (void); +int proc_actuators_deinit (void); +int proc_actuators (void); /****************************************************************************** diff --git a/inc/ctrl.h b/inc/ctrl.h index 80bd956..acf1279 100644..100755 --- a/inc/ctrl.h +++ b/inc/ctrl.h @@ -33,7 +33,8 @@ /****************************************************************************** ******* macros *************************************************************** ******************************************************************************/ - # define REFRESH_FREQ (50u) + # define CTRL_REFRESH_PERIOD_US (20000u) + # define CTRL_TASK_MODE_IT (false) /****************************************************************************** @@ -54,9 +55,9 @@ /****************************************************************************** ******* functions ************************************************************ ******************************************************************************/ -int proc_ctrl_init (void); -int proc_ctrl_1 (void); -int proc_ctrl_2 (void); +int proc_ctrl_init (void); +int proc_ctrl_deinit (void); +int proc_ctrl (void); /****************************************************************************** diff --git a/inc/test.h b/inc/test.h new file mode 100755 index 0000000..7d82e73 --- /dev/null +++ b/inc/test.h @@ -0,0 +1,67 @@ +/****************************************************************************** + * Copyright (C) 2018 Colomar Andrés, Alejandro * + * Copyright (C) 2018 García Pedroche, Francisco Javier * + * SPDX-License-Identifier: GPL-2.0-only * + ******************************************************************************/ + +/** + * @file test.h + * @author Colomar Andrés, Alejandro + * @author García Pedroche, Francisco Javier + * @copyright GPL-2.0-only + * @date 2018/jan/02 + * @brief Test modules + */ + + +/****************************************************************************** + ******* include guard ******************************************************** + ******************************************************************************/ +# ifndef TEST_H + # define TEST_H + + +/****************************************************************************** + ******* headers ************************************************************** + ******************************************************************************/ +/* Standard C ----------------------------------------------------------------*/ +/* Drivers -------------------------------------------------------------------*/ +/* libalx --------------------------------------------------------------------*/ +/* STM32L4 modules -----------------------------------------------------------*/ + + +/****************************************************************************** + ******* macros *************************************************************** + ******************************************************************************/ + + +/****************************************************************************** + ******* enums **************************************************************** + ******************************************************************************/ + + +/****************************************************************************** + ******* structs ************************************************************** + ******************************************************************************/ + + +/****************************************************************************** + ******* variables ************************************************************ + ******************************************************************************/ + + +/****************************************************************************** + ******* functions ************************************************************ + ******************************************************************************/ +int test (void); + + +/****************************************************************************** + ******* include guard ******************************************************** + ******************************************************************************/ +# endif /* test.h */ + + +/****************************************************************************** + ******* end of file ********************************************************** + ******************************************************************************/ diff --git a/src/actuators.c b/src/actuators.c index d4c7e1d..3f5dc93 100644..100755 --- a/src/actuators.c +++ b/src/actuators.c @@ -33,7 +33,7 @@ #include "led.h" #include "servo.h" #include "tim.h" -/* Project -------------------------------------------------------------------*/ +/* project -------------------------------------------------------------------*/ #include "actuators.h" @@ -58,6 +58,7 @@ /* Volatile ------------------------------------------------------------------*/ /* Global --------------------------------------------------------------------*/ /* Static --------------------------------------------------------------------*/ +static bool init_pending = true; static float pitch; static float roll; static float yaw; @@ -67,9 +68,11 @@ static float yaw; ******* static functions (prototypes) **************************************** ******************************************************************************/ static int modules_init (void); -#if 0 +static int modules_deinit (void); static int proc_init (void); -#endif +static void proc_deinit (void); +static int proc_actuators_it (void); +static int proc_actuators_delay (void); static int proc_ref_read (void *data); static int proc_actuators_set (void *data); @@ -84,56 +87,76 @@ static int proc_actuators_set (void *data); */ int proc_actuators_init (void) { - if (modules_init()) { - return ERROR_NOK; + + if (init_pending) { + init_pending = false; + } else { + return ERROR_OK; } -#if 0 + if (modules_init()) { + goto err_mod; + } if (proc_init()) { - return ERROR_NOK; + goto err_proc; } -#endif return ERROR_OK; + + +err_proc: + modules_deinit(); +err_mod: + + return ERROR_NOK; } /** - * @brief Run actuators process (based on timer interrupts) + * @brief Deinitialize actuators process * @return Error * @note Sets global variable 'prj_error' */ -int proc_actuators_1 (void) +int proc_actuators_deinit (void) { - while (true) { - __WFE(); - if (tim_tim3_interrupt) { - if (tim_callback_exe()) { - prj_error_handle(); - return ERROR_NOK; - } - tim_tim3_interrupt = false; - } + int status; + + status = ERROR_OK; + + if (!init_pending) { + init_pending = true; + } else { + return status; } - return ERROR_OK; + proc_deinit(); + if (modules_deinit()) { + status = ERROR_NOK; + } + + return status; } /** - * @brief Run actuators process (based on delays) + * @brief Run actuators process * @return Error * @note Sets global variable 'prj_error' */ -int proc_actuators_2 (void) +int proc_actuators (void) { - delay_us(1000u); - while (true) { - delay_us(20000u); + if (init_pending) { + if (proc_actuators_init()) { + return ERROR_NOK; + } + } - if (!proc_ref_read(NULL)) { - if (proc_actuators_set(NULL)) { - return ERROR_NOK; - } + if (ACT_TASK_MODE_IT) { + if (proc_actuators_it()) { + return ERROR_NOK; + } + } else { + if (proc_actuators_delay()) { + return ERROR_NOK; } } @@ -146,37 +169,126 @@ int proc_actuators_2 (void) ******************************************************************************/ static int modules_init (void) { -#if 0 - if (tim_tim3_init(REFRESH_FREQ)) { - return ERROR_NOK; - } -#endif + + led_init(); if (delay_us_init()) { - return ERROR_NOK; + goto err_delay; + } + + if (ACT_TASK_MODE_IT) { + if (tim_tim3_init(ACT_REFRESH_PERIOD_US)) { + goto err_tim; + } } + if (can_init()) { - return ERROR_NOK; + goto err_can; } if (servo_init()) { - return ERROR_NOK; + goto err_servo; } return ERROR_OK; + + +err_servo: + can_deinit(); +err_can: + delay_us_deinit(); +err_delay: + led_deinit(); + tim_tim3_deinit(); +err_tim: + + return ERROR_NOK; +} + +static int modules_deinit (void) +{ + int status; + + status = ERROR_OK; + + if (servo_deinit()) { + status = ERROR_NOK; + } + if (can_deinit()) { + status = ERROR_NOK; + } + if (tim_tim3_deinit()) { + status = ERROR_NOK; + } + if (delay_us_deinit()) { + status = ERROR_NOK; + } + led_deinit(); + + return status; } -#if 0 static int proc_init (void) { - if (tim_callback_push(&proc_ref_read, NULL)) { - return ERROR_NOK; + + if (ACT_TASK_MODE_IT) { + if (tim_callback_push(&proc_ref_read, NULL)) { + goto err_push; + } + if (tim_callback_push(&proc_actuators_set, NULL)) { + goto err_push; + } + + return ERROR_OK; + } else { + return ERROR_OK; } - if (tim_callback_push(&proc_actuators_set, NULL)) { - return ERROR_NOK; + + +err_push: + proc_deinit(); + + return ERROR_NOK; +} + +static void proc_deinit (void) +{ + + if (ACT_TASK_MODE_IT) { + while (tim_callback_pop()) { + ; + } + } +} + +static int proc_actuators_it (void) +{ + + while (true) { + __WFE(); +// __WFI(); + + if (tim_tim3_interrupt) { + if (tim_callback_exe()) { + return ERROR_NOK; + } + tim_tim3_interrupt = false; + } } +} - return ERROR_OK; +static int proc_actuators_delay (void) +{ + + while (true) { + delay_us(ACT_REFRESH_PERIOD_US); + + if (!proc_ref_read(NULL)) { + if (proc_actuators_set(NULL)) { + return ERROR_NOK; + } + } +//prj_error_handle(); + } } -#endif static int proc_ref_read (void *data) { diff --git a/src/ctrl.c b/src/ctrl.c index 522c844..8856b64 100644..100755 --- a/src/ctrl.c +++ b/src/ctrl.c @@ -37,7 +37,7 @@ #include "led.h" #include "nunchuk.h" #include "tim.h" -/* Project -------------------------------------------------------------------*/ +/* project -------------------------------------------------------------------*/ #include "ctrl.h" @@ -59,8 +59,10 @@ /****************************************************************************** ******* variables ************************************************************ ******************************************************************************/ +/* Volatile ------------------------------------------------------------------*/ /* Global --------------------------------------------------------------------*/ /* Static --------------------------------------------------------------------*/ +static bool init_pending = true; static float pitch; static float roll; static float yaw; @@ -71,9 +73,11 @@ static bool level; ******* static functions (prototypes) **************************************** ******************************************************************************/ static int modules_init (void); -#if 0 +static int modules_deinit (void); static int proc_init (void); -#endif +static void proc_deinit (void); +static int proc_ctrl_it (void); +static int proc_ctrl_delay (void); static int proc_ctrl_read (void *data); static int proc_ctrl_report (void *data); @@ -86,63 +90,79 @@ static int proc_ctrl_report (void *data); * @return Error * @note Sets global variable 'prj_error' */ -int proc_ctrl_init (void) +int proc_ctrl_init (void) { - if (modules_init()) { - return ERROR_NOK; + + if (init_pending) { + init_pending = false; + } else { + return ERROR_OK; } -#if 0 + if (modules_init()) { + goto err_mod; + } if (proc_init()) { - return ERROR_NOK; + goto err_proc; } -#endif level = true; return ERROR_OK; + + +err_proc: + modules_deinit(); +err_mod: + + return ERROR_NOK; } /** - * @brief Run controller process (based on timer interrupts) + * @brief Deinitialize controller process * @return Error * @note Sets global variable 'prj_error' */ -int proc_ctrl_1 (void) +int proc_ctrl_deinit (void) { - while (true) { - __WFE(); - if (tim_tim3_interrupt) { - if (tim_callback_exe()) { - prj_error_handle(); - return ERROR_NOK; - } - tim_tim3_interrupt = false; - } + int status; + + status = ERROR_OK; + + if (!init_pending) { + init_pending = true; + } else { + return status; } - return ERROR_OK; + proc_deinit(); + if (modules_deinit()) { + status = ERROR_NOK; + } + + return status; } /** - * @brief Run controller process (based on delays) + * @brief Run controller process * @return Error * @note Sets global variable 'prj_error' */ -int proc_ctrl_2 (void) +int proc_ctrl (void) { - delay_us(1000u); - - while (true) { - delay_us(20000u); - if (proc_ctrl_read(NULL)) { - prj_error_handle(); + if (init_pending) { + if (proc_ctrl_init()) { return ERROR_NOK; } + } - if (proc_ctrl_report(NULL)) { - prj_error_handle(); + if (CTRL_TASK_MODE_IT) { + if (proc_ctrl_it()) { + return ERROR_NOK; + } + } else { + if (proc_ctrl_delay()) { return ERROR_NOK; } } @@ -156,39 +176,127 @@ int proc_ctrl_2 (void) ******************************************************************************/ static int modules_init (void) { -#if 0 - if (tim_tim3_init(REFRESH_FREQ)) { - return ERROR_NOK; - } -#endif + led_init(); if (delay_us_init()) { - return ERROR_NOK; + goto err_delay; } - if (can_init()) { - return ERROR_NOK; + + if (CTRL_TASK_MODE_IT) { + if (tim_tim3_init(CTRL_REFRESH_PERIOD_US)) { + goto err_tim; + } } + if (can_init()) { + goto err_can; + } if (nunchuk_init()) { - return ERROR_NOK; + goto err_nunchuk; } return ERROR_OK; + + +err_nunchuk: + can_deinit(); +err_can: + delay_us_deinit(); +err_delay: + led_deinit(); + tim_tim3_deinit(); +err_tim: + + return ERROR_NOK; +} + +static int modules_deinit (void) +{ + int status; + + status = ERROR_OK; + + if (nunchuk_deinit()) { + status = ERROR_NOK; + } + if (can_deinit()) { + status = ERROR_NOK; + } + if (tim_tim3_deinit()) { + status = ERROR_NOK; + } + if (delay_us_deinit()) { + status = ERROR_NOK; + } + led_deinit(); + + + return status; } -#if 0 static int proc_init (void) { - if (tim_callback_push(&proc_ctrl_read, NULL)) { - return ERROR_NOK; + + if (CTRL_TASK_MODE_IT) { + if (tim_callback_push(&proc_ctrl_read, NULL)) { + goto err_push; + } + if (tim_callback_push(&proc_ctrl_report, NULL)) { + goto err_push; + } + + return ERROR_OK; + } else { + return ERROR_OK; } - if (tim_callback_push(&proc_ctrl_report, NULL)) { - return ERROR_NOK; + + +err_push: + proc_deinit(); + + return ERROR_NOK; +} + +static void proc_deinit (void) +{ + + if (CTRL_TASK_MODE_IT) { + while (tim_callback_pop()) { + ; + } } +} - return ERROR_OK; +static int proc_ctrl_it (void) +{ + + while (true) { + __WFE(); +// __WFI(); + + if (tim_tim3_interrupt) { + if (tim_callback_exe()) { + return ERROR_NOK; + } + tim_tim3_interrupt = false; + } + } +} + +static int proc_ctrl_delay (void) +{ + + while (true) { + delay_us(CTRL_REFRESH_PERIOD_US); + + if (proc_ctrl_read(NULL)) { + return ERROR_NOK; + } + if (proc_ctrl_report(NULL)) { + return ERROR_NOK; + } + } } -#endif static int proc_ctrl_read (void *data) { diff --git a/src/main.c b/src/main.c index 0afb75b..0c3392d 100644..100755 --- a/src/main.c +++ b/src/main.c @@ -22,10 +22,8 @@ /* Standard C ----------------------------------------------------------------*/ #include <stdbool.h> #include <stdnoreturn.h> - /* Drivers -------------------------------------------------------------------*/ #include "stm32l4xx_hal.h" - /* libalx --------------------------------------------------------------------*/ /* STM32L4 modules -----------------------------------------------------------*/ #include "can.h" @@ -44,15 +42,19 @@ #include "nunchuk_test.h" #include "servo_test.h" #include "tim_test.h" - /* project -------------------------------------------------------------------*/ #include "ctrl.h" #include "actuators.h" + #include "test.h" /****************************************************************************** ******* macros *************************************************************** ******************************************************************************/ + # define DEBUG_MODE (false) + + # define PROJECT_CTRL (true) + # define PROJECT_ACT (!PROJECT_CTRL) /****************************************************************************** @@ -75,7 +77,8 @@ /****************************************************************************** ******* static functions (prototypes) **************************************** ******************************************************************************/ -static int test (void); +static int proc_init (void); +static int proc (void); static noreturn void stuck_forever (bool led); @@ -88,35 +91,23 @@ noreturn int main (void) sysclk_config(); prj_error = 0; -#if 0 - if (test()) { - stuck_forever(true); + if (DEBUG_MODE) { + if (test()) { + goto err; + } } -#else - (void)&test; - #if 01 - if (proc_actuators_init()) { - stuck_forever(true); + if (proc_init()) { + goto err; } - #elif 1 - if (proc_ctrl_init()) { - stuck_forever(true); - } - #endif - #if 01 - if (proc_actuators_2()) { - stuck_forever(true); - } - #elif 1 - if (proc_ctrl_2()) { - stuck_forever(true); + if (proc()) { + goto err; } - #endif -#endif +err: +/* prj_error_handle();*/ stuck_forever(true); } @@ -124,8 +115,41 @@ noreturn int main (void) /****************************************************************************** ******* static functions (definitions) *************************************** ******************************************************************************/ +static int proc_init (void) +{ + + if (PROJECT_ACT) { + if (proc_actuators_init()) { + return ERROR_NOK; + } + } else if (PROJECT_CTRL) { + if (proc_ctrl_init()) { + return ERROR_NOK; + } + } + + return ERROR_OK; +} + +static int proc (void) +{ + + if (PROJECT_ACT) { + if (proc_actuators()) { + return ERROR_NOK; + } + } else if (PROJECT_CTRL) { + if (proc_ctrl()) { + return ERROR_NOK; + } + } + + return ERROR_OK; +} + static noreturn void stuck_forever (bool led) { + if (led) { led_set(); } else { @@ -133,51 +157,11 @@ static noreturn void stuck_forever (bool led) } while (true) { - __WFI(); +// __WFI(); __NOP(); } } -static int test (void) -{ -#if 0 - if (led_test()) { - return ERROR_NOK; - } -#endif -#if 0 - can_r_test(); -#endif -#if 0 - can_w_test(); -#endif -#if 0 - if (servo_test_2()) { - return ERROR_NOK; - } -#endif -#if 0 - if (display_test()) { - return ERROR_NOK; - } -#endif -#if 0 - if (nunchuk_test_2()) { - return ERROR_NOK; - } -#endif -#if 0 - if (tim_test()) { - return ERROR_NOK; - } -#endif -#if 0 - prj_error_handle(); -#endif - - return ERROR_OK; -} - /****************************************************************************** ******* end of file ********************************************************** diff --git a/src/startup_stm32l476xx.s b/src/startup_stm32l476xx.s index f353700..56a0543 100644..100755 --- a/src/startup_stm32l476xx.s +++ b/src/startup_stm32l476xx.s @@ -7,7 +7,7 @@ * - Set the initial SP * - Set the initial PC == Reset_Handler, * - Set the vector table entries with the exceptions ISR address, - * - Configure the clock system + * - Configure the clock system * - Branches to main in the C library (which eventually * calls main()). * After Reset the Cortex-M4 processor is in Thread mode, diff --git a/src/stm32l4xx_it.c b/src/stm32l4xx_it.c index 6aade74..6aade74 100644..100755 --- a/src/stm32l4xx_it.c +++ b/src/stm32l4xx_it.c diff --git a/src/test.c b/src/test.c new file mode 100755 index 0000000..94fe40d --- /dev/null +++ b/src/test.c @@ -0,0 +1,141 @@ +/****************************************************************************** + * Copyright (C) 2018 Colomar Andrés, Alejandro * + * Copyright (C) 2018 García Pedroche, Francisco Javier * + * SPDX-License-Identifier: GPL-2.0-only * + ******************************************************************************/ + +/** + * @file test.c + * @author Colomar Andrés, Alejandro + * @author García Pedroche, Francisco Javier + * @copyright GPL-2.0-only + * @date 2018/jan/02 + * @brief Test modules + */ + + +/****************************************************************************** + ******* headers ************************************************************** + ******************************************************************************/ +/* Standard C ----------------------------------------------------------------*/ + #include <stdbool.h> + #include <stddef.h> + #include <stdint.h> + +/* Drivers -------------------------------------------------------------------*/ + #include "stm32l4xx_hal.h" + +/* libalx --------------------------------------------------------------------*/ +/* STM32L4 modules -----------------------------------------------------------*/ + #include "delay.h" + #include "errors.h" + #include "led.h" + + #include "can_test.h" + #include "display_test.h" + #include "led_test.h" + #include "nunchuk_test.h" + #include "servo_test.h" + #include "tim_test.h" + +/* project -------------------------------------------------------------------*/ + #include "test.h" + + +/****************************************************************************** + ******* macros *************************************************************** + ******************************************************************************/ + + +/****************************************************************************** + ******* enums **************************************************************** + ******************************************************************************/ + + +/****************************************************************************** + ******* structs ************************************************************** + ******************************************************************************/ + + +/****************************************************************************** + ******* variables ************************************************************ + ******************************************************************************/ +/* Volatile ------------------------------------------------------------------*/ +/* Global --------------------------------------------------------------------*/ +/* Static --------------------------------------------------------------------*/ + + +/****************************************************************************** + ******* static functions (prototypes) **************************************** + ******************************************************************************/ + + +/****************************************************************************** + ******* global functions ***************************************************** + ******************************************************************************/ + /** + * @brief Test modules + * @return Error + */ +int test (void) +{ + led_init(); + delay_us_init(); + +#if 0 + if (led_test()) { + return ERROR_NOK; + } +#endif +#if 0 + can_r_test(); +#endif +#if 0 + can_w_test(); +#endif +#if 1 + if (servo_test_0()) { + return ERROR_NOK; + } +#endif +#if 0 + if (servo_test_1()) { + return ERROR_NOK; + } +#endif +#if 0 + if (servo_test_2()) { + return ERROR_NOK; + } +#endif +#if 0 + if (display_test()) { + return ERROR_NOK; + } +#endif +#if 0 + if (nunchuk_test_2()) { + return ERROR_NOK; + } +#endif +#if 0 + if (tim_test()) { + return ERROR_NOK; + } +#endif +#if 0 + prj_error_handle(); +#endif + + return ERROR_OK; +} + + +/****************************************************************************** + ******* static functions (definitions) *************************************** + ******************************************************************************/ + + +/****************************************************************************** + ******* end of file ********************************************************** + ******************************************************************************/ diff --git a/stm32l4-drivers b/stm32l4-drivers -Subproject 4ce59057c85bbac81fa44f956bfc392fcb6f120 +Subproject 911c1d2e48f239753a011a922efe71de8bf0137 diff --git a/stm32l4-modules b/stm32l4-modules -Subproject f061888de747ef36615a396200b32d5a72ccdc3 +Subproject 83946ef2ec31ffdf8d9f07ae71028fa21e247d3 diff --git a/tmp/Makefile b/tmp/Makefile index f50f7eb..cc17390 100644..100755 --- a/tmp/Makefile +++ b/tmp/Makefile @@ -23,7 +23,7 @@ DEP_DIR = $(MAIN_DIR)/dep/ ALL = \ system_stm32l4xx.o stm32l4xx_it.o main.o \ - ctrl.o actuators.o + ctrl.o actuators.o test.o COMMON_INC_CMSIS = core_cm4.h cmsis_gcc.h COMMON_INC_CMSIS_L4 = stm32l4xx.h @@ -103,6 +103,22 @@ ACT_INC_DIRS = -I $(INC_DIR) \ $(COMMON_INC_DIRS) +TST_INC_BASE = delay.h errors.h led.h +TST_INC_TST = \ + can_test.h led_test.h tim_test.h \ + display_test.h nunchuk_test.h servo_test.h +TST_INC = test.h +TST_DEPS = $(SRC_DIR)/test.c \ + $(COMMON_DEPS) \ + $(patsubst %,$(MODULES_BASE_INC_DIR)/%,$(TST_INC_BASE)) \ + $(patsubst %,$(MODULES_TST_INC_DIR)/%,$(TST_INC_TST)) \ + $(patsubst %,$(INC_DIR)/%,$(TST_INC)) +TST_INC_DIRS = -I $(INC_DIR) \ + -I $(MODULES_BASE_INC_DIR) \ + -I $(MODULES_TST_INC_DIR) \ + $(COMMON_INC_DIRS) + + # target: dependencies # action @@ -129,6 +145,10 @@ actuators.o: $(ACT_DEPS) @echo " CC $@" $(Q)$(CC) $(CFLAGS) $(ACT_INC_DIRS) -c $< -o $@ +test.o: $(TST_DEPS) + @echo " CC $@" + $(Q)$(CC) $(CFLAGS) $(TST_INC_DIRS) -c $< -o $@ + clean: $(Q)rm -f *.o |