From caa360625bc6da26f7776df96ec4a4b0d93f5996 Mon Sep 17 00:00:00 2001 From: tkl Date: Tue, 16 Aug 2016 13:08:49 +0200 Subject: [PATCH] kosmos threading running --- .../arch/stm32f4xx}/_initialize_hardware.c | 0 .../src => firmware/arch/stm32f4xx}/_write.c | 0 .../include/stm32f4-discovery.h | 24 +++--- .../stm32f4-discovery/stm32f4-discovery.c | 6 +- .../stm32f4-discovery/stm32f4-discovery.mk | 4 +- .../stm32f4xx/driver/include/stm32_sys_tick.h | 47 +++++++++++ .../{stm32_sys_tick._c => stm32_sys_tick.c} | 1 + .../arch/stm32f4xx}/stm32f4xx_hal_msp.c | 0 source/firmware/kernel/driver/timer.c | 3 +- source/firmware/kernel/include/sys_tick.h | 2 +- source/firmware/kernel/sys_tick.c | 5 +- source/test/src/BlinkLed.c | 29 ------- source/test/src/Timer.c | 64 -------------- source/test/src/include/BlinkLed.h | 84 ------------------- source/test/src/include/Timer.h | 27 ------ source/test/src/include/include.mk | 1 - source/test/src/main.c | 43 +++++----- 17 files changed, 93 insertions(+), 247 deletions(-) rename source/{test/src => firmware/arch/stm32f4xx}/_initialize_hardware.c (100%) rename source/{test/src => firmware/arch/stm32f4xx}/_write.c (100%) create mode 100644 source/firmware/arch/stm32f4xx/driver/include/stm32_sys_tick.h rename source/firmware/arch/stm32f4xx/driver/{stm32_sys_tick._c => stm32_sys_tick.c} (98%) rename source/{test/src => firmware/arch/stm32f4xx}/stm32f4xx_hal_msp.c (100%) delete mode 100644 source/test/src/BlinkLed.c delete mode 100644 source/test/src/Timer.c delete mode 100644 source/test/src/include/BlinkLed.h delete mode 100644 source/test/src/include/Timer.h delete mode 100644 source/test/src/include/include.mk diff --git a/source/test/src/_initialize_hardware.c b/source/firmware/arch/stm32f4xx/_initialize_hardware.c similarity index 100% rename from source/test/src/_initialize_hardware.c rename to source/firmware/arch/stm32f4xx/_initialize_hardware.c diff --git a/source/test/src/_write.c b/source/firmware/arch/stm32f4xx/_write.c similarity index 100% rename from source/test/src/_write.c rename to source/firmware/arch/stm32f4xx/_write.c diff --git a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h index 01260a3..e290263 100755 --- a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h +++ b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include/stm32f4-discovery.h @@ -9,16 +9,18 @@ #include #include #if 0 -#include "../../../../../kernel/include/sys_tick.h" -#include "../../../driver/include/stm32_sys_tick.h" #include "../../../driver/include/stm32f4_pwm.h" #include "../../../driver/include/stm32f4_uart.h" #include "pwm.h" -#include "timer.h" #include "uart.h" #include "ringbuffer.h" #endif #include "stm32f4xx.h" + +#include "timer.h" +#include "stm32_sys_tick.h" +#include "sys_tick.h" + #include "gpio.h" #include "stm32f4_gpio.h" #include "driver.h" @@ -41,27 +43,27 @@ static const struct gpio __gpio_d12 = { &gpio_fp }; -const struct driver gpio_d12 = { +static const struct driver gpio_d12 = { DRIVER_TYPE_GPIO, &__gpio_d12, }; -#if 0 // SYSTEM TICK static const enum stm32_sys_tick_time_base stm23_sys_tick_time_base = STM32_SYS_TICK_TIME_BASE_MS; + static const struct stm32_sys_tick stm32_sys_tick = { - &stm23_sys_tick_time_base, - NULL, - NULL + .tick_time_base = &stm23_sys_tick_time_base, + .sys_tick_cb = NULL, + .sys_tick_cb_param = NULL, }; static const struct loki_timer timer_1 = { - (void*)&stm32_sys_tick, - &timer_fp + .arch_dep_device = (void*)&stm32_sys_tick, + .fp = &timer_fp }; -#endif + #if 0 // PWM CHANNEL 4 /* apb1 clock = 84MHz */ diff --git a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/stm32f4-discovery.c b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/stm32f4-discovery.c index 9c0e390..15908f6 100755 --- a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/stm32f4-discovery.c +++ b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/stm32f4-discovery.c @@ -7,10 +7,12 @@ #include "board.h" -void board_init(void) { +void board_init(void) +{ +#if 0 NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); SysTick_CLKSourceConfig(RCC_SYSCLKSource_PLLCLK); - +#endif sys_tick_init(&timer_1); } diff --git a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/stm32f4-discovery.mk b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/stm32f4-discovery.mk index 5ee2e64..52f3b9c 100755 --- a/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/stm32f4-discovery.mk +++ b/source/firmware/arch/stm32f4xx/board/stm32f4-discovery/stm32f4-discovery.mk @@ -1,4 +1,2 @@ -CHECK_FOLDER += source/firmware/arch/stm32f4xx/board/stm32f4-discovery -SUB_FOLDER += source/firmware/arch/stm32f4xx/board/stm32f4-discovery +SRC_DIR += source/firmware/arch/stm32f4xx/board/stm32f4-discovery INCLUDES += source/firmware/arch/stm32f4xx/board/stm32f4-discovery/include -DOC_SRC += source/firmware/arch/stm32f4xx/board/stm32f4-discovery \ No newline at end of file diff --git a/source/firmware/arch/stm32f4xx/driver/include/stm32_sys_tick.h b/source/firmware/arch/stm32f4xx/driver/include/stm32_sys_tick.h new file mode 100644 index 0000000..a1864fc --- /dev/null +++ b/source/firmware/arch/stm32f4xx/driver/include/stm32_sys_tick.h @@ -0,0 +1,47 @@ +//! \file stm32_sys_tick.h +//! \author tkl +//! \date Feb 15, 2012 +//! \brief Header file of the stm32f10x architecture dependent sys tick timer implementation. +#ifndef STM32_SYS_TICK_H_ +#define STM32_SYS_TICK_H_ + +typedef void* (*stm32_sys_tick_cb_t)(void*); //!< callback for the external interrupt + +//! \brief Type of sys tick base time. +enum stm32_sys_tick_time_base { + STM32_SYS_TICK_TIME_BASE_NONE = 0, //!< Not init + STM32_SYS_TICK_TIME_BASE_US, //!< Tick time = 1 us. + STM32_SYS_TICK_TIME_BASE_MS //!< Tick time = 1 ms. +}; + +//! \brief The sys tick device. +struct stm32_sys_tick { + const enum stm32_sys_tick_time_base *tick_time_base; //!< time base for the system tick + stm32_sys_tick_cb_t sys_tick_cb; //!< callback for the sys tick interrupt + void *sys_tick_cb_param; //!< parameter for the callback +}; + +//! \brief Open a sys tick timer. +//! \param sys_tick The sys tick to open. Must be of type const stm32_sys_tick_t*. +//! \return -1 in error case. +int stm32_sys_tick_open(const void *sys_tick); + +//! \brief Close a sys tick timer. +//! \param sys_tick The sys tick to close. Must be of type const stm32_sys_tick_t*. +//! \return -1 in error case. +int stm32_sys_tick_close(const void *sys_tick); + +//! \brief Set the call back for a sys tick timer. +//! \param sys_tick The sys tick to open. Mus be of type const stm32_sys_tick_t*. +//! \param callback The function pointer of the call back function. +//! \param param The parameter for the call back function. +//! \return -1 in error case. +int stm32_sys_tick_set_cb(const void *sys_tick, const void *callback, const void *param); + +static const struct timer_fp timer_fp = { + stm32_sys_tick_open, + stm32_sys_tick_close, + stm32_sys_tick_set_cb +}; + +#endif /* STM32_SYS_TICK_H_ */ diff --git a/source/firmware/arch/stm32f4xx/driver/stm32_sys_tick._c b/source/firmware/arch/stm32f4xx/driver/stm32_sys_tick.c similarity index 98% rename from source/firmware/arch/stm32f4xx/driver/stm32_sys_tick._c rename to source/firmware/arch/stm32f4xx/driver/stm32_sys_tick.c index 209a097..4d00ec7 100755 --- a/source/firmware/arch/stm32f4xx/driver/stm32_sys_tick._c +++ b/source/firmware/arch/stm32f4xx/driver/stm32_sys_tick.c @@ -8,6 +8,7 @@ #include "stm32f4xx.h" +#include "timer.h" #include "stm32_sys_tick.h" #include "stm32f4xx_isr.h" diff --git a/source/test/src/stm32f4xx_hal_msp.c b/source/firmware/arch/stm32f4xx/stm32f4xx_hal_msp.c similarity index 100% rename from source/test/src/stm32f4xx_hal_msp.c rename to source/firmware/arch/stm32f4xx/stm32f4xx_hal_msp.c diff --git a/source/firmware/kernel/driver/timer.c b/source/firmware/kernel/driver/timer.c index 28f4583..c0ec760 100755 --- a/source/firmware/kernel/driver/timer.c +++ b/source/firmware/kernel/driver/timer.c @@ -2,9 +2,10 @@ //! \author tkl //! \date Jul 5, 2012 //! \brief Source file of the architecture independent timer implementation. +#include "include/timer.h" + #include -#include "timer.h" int timer_open(const struct loki_timer *device) { diff --git a/source/firmware/kernel/include/sys_tick.h b/source/firmware/kernel/include/sys_tick.h index 2a6d94f..31e15bf 100644 --- a/source/firmware/kernel/include/sys_tick.h +++ b/source/firmware/kernel/include/sys_tick.h @@ -8,6 +8,6 @@ #ifndef SYS_TICK_H_ #define SYS_TICK_H_ -//void sys_tick_init(const struct loki_timer *hw_timer); +void sys_tick_init(const struct loki_timer *hw_timer); #endif /* SYS_TICK_H_ */ diff --git a/source/firmware/kernel/sys_tick.c b/source/firmware/kernel/sys_tick.c index c3ec040..e5f7bc2 100644 --- a/source/firmware/kernel/sys_tick.c +++ b/source/firmware/kernel/sys_tick.c @@ -4,13 +4,12 @@ * Created on: Sep 25, 2015 * Author: tkl */ -#include "include/sys_tick.h" - #include #include -#include "irq.h" #include "timer.h" +#include "sys_tick.h" +#include "irq.h" #include "stack.h" #include "queue.h" #include "thread.h" diff --git a/source/test/src/BlinkLed.c b/source/test/src/BlinkLed.c deleted file mode 100644 index dae01d7..0000000 --- a/source/test/src/BlinkLed.c +++ /dev/null @@ -1,29 +0,0 @@ -// -// This file is part of the GNU ARM Eclipse distribution. -// Copyright (c) 2014 Liviu Ionescu. -// - -#include "BlinkLed.h" - -// ---------------------------------------------------------------------------- - -void -blink_led_init() -{ - // Enable GPIO Peripheral clock - RCC->AHB1ENR |= BLINK_RCC_MASKx(BLINK_PORT_NUMBER); - - GPIO_InitTypeDef GPIO_InitStructure; - - // Configure pin in output push/pull mode - GPIO_InitStructure.Pin = BLINK_PIN_MASK(BLINK_PIN_NUMBER); - GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP; - GPIO_InitStructure.Speed = GPIO_SPEED_FAST; - GPIO_InitStructure.Pull = GPIO_PULLUP; - HAL_GPIO_Init(BLINK_GPIOx(BLINK_PORT_NUMBER), &GPIO_InitStructure); - - // Start with led turned off - blink_led_off(); -} - -// ---------------------------------------------------------------------------- diff --git a/source/test/src/Timer.c b/source/test/src/Timer.c deleted file mode 100644 index 109f9be..0000000 --- a/source/test/src/Timer.c +++ /dev/null @@ -1,64 +0,0 @@ -// -// This file is part of the GNU ARM Eclipse distribution. -// Copyright (c) 2014 Liviu Ionescu. -// - -#include "Timer.h" -#include "cortexm/ExceptionHandlers.h" - -// ---------------------------------------------------------------------------- - -#if defined(USE_HAL_DRIVER) -void HAL_IncTick(void); -#endif - -// Forward declarations. - -void -timer_tick (void); - -// ---------------------------------------------------------------------------- - -volatile timer_ticks_t timer_delayCount; - -// ---------------------------------------------------------------------------- - -void -timer_start (void) -{ - // Use SysTick as reference for the delay loops. - SysTick_Config (SystemCoreClock / TIMER_FREQUENCY_HZ); -} - -void -timer_sleep (timer_ticks_t ticks) -{ - timer_delayCount = ticks; - - // Busy wait until the SysTick decrements the counter to zero. - while (timer_delayCount != 0u) - ; -} - -void -timer_tick (void) -{ - // Decrement to zero the counter used by the delay routine. - if (timer_delayCount != 0u) - { - --timer_delayCount; - } -} - -// ----- SysTick_Handler() ---------------------------------------------------- - -void -SysTick_Handler (void) -{ -#if defined(USE_HAL_DRIVER) - HAL_IncTick(); -#endif - timer_tick (); -} - -// ---------------------------------------------------------------------------- diff --git a/source/test/src/include/BlinkLed.h b/source/test/src/include/BlinkLed.h deleted file mode 100644 index b82bc88..0000000 --- a/source/test/src/include/BlinkLed.h +++ /dev/null @@ -1,84 +0,0 @@ -// -// This file is part of the GNU ARM Eclipse distribution. -// Copyright (c) 2014 Liviu Ionescu. -// - -#ifndef BLINKLED_H_ -#define BLINKLED_H_ - -#include "stm32f4xx.h" -#include "stm32f4xx_hal.h" - -// ----- LED definitions ------------------------------------------------------ - -// Adjust these definitions for your own board. - -#if defined(BOARD_OLIMEX_STM32_E407) - -// STM32-E407 definitions (the GREEN led, C13, active low) - -// Port numbers: 0=A, 1=B, 2=C, 3=D, 4=E, 5=F, 6=G, ... -#define BLINK_PORT_NUMBER (2) -#define BLINK_PIN_NUMBER (13) -#define BLINK_ACTIVE_LOW (1) - -#else - -// STM32F4DISCOVERY definitions (the GREEN led, D12, active high) -// (SEGGER J-Link device name: STM32F407VG). - -#define BLINK_PORT_NUMBER (3) -#define BLINK_PIN_NUMBER (12) -#define BLINK_ACTIVE_LOW (0) - -#endif - -#define BLINK_GPIOx(_N) ((GPIO_TypeDef *)(GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE)*(_N))) -#define BLINK_PIN_MASK(_N) (1 << (_N)) -#define BLINK_RCC_MASKx(_N) (RCC_AHB1ENR_GPIOAEN << (_N)) - -// ---------------------------------------------------------------------------- - -extern -void -blink_led_init(void); - -// ---------------------------------------------------------------------------- - -inline void -blink_led_on(void); - -inline void -blink_led_off(void); - -// ---------------------------------------------------------------------------- - -inline void -__attribute__((always_inline)) -blink_led_on(void) -{ -#if (BLINK_ACTIVE_LOW) - HAL_GPIO_WritePin(BLINK_GPIOx(BLINK_PORT_NUMBER), - BLINK_PIN_MASK(BLINK_PIN_NUMBER), GPIO_PIN_RESET); -#else - HAL_GPIO_WritePin(BLINK_GPIOx(BLINK_PORT_NUMBER), - BLINK_PIN_MASK(BLINK_PIN_NUMBER), GPIO_PIN_SET); -#endif -} - -inline void -__attribute__((always_inline)) -blink_led_off(void) -{ -#if (BLINK_ACTIVE_LOW) - HAL_GPIO_WritePin(BLINK_GPIOx(BLINK_PORT_NUMBER), - BLINK_PIN_MASK(BLINK_PIN_NUMBER), GPIO_PIN_SET); -#else - HAL_GPIO_WritePin(BLINK_GPIOx(BLINK_PORT_NUMBER), - BLINK_PIN_MASK(BLINK_PIN_NUMBER), GPIO_PIN_RESET); -#endif -} - -// ---------------------------------------------------------------------------- - -#endif // BLINKLED_H_ diff --git a/source/test/src/include/Timer.h b/source/test/src/include/Timer.h deleted file mode 100644 index 9ff2085..0000000 --- a/source/test/src/include/Timer.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// This file is part of the GNU ARM Eclipse distribution. -// Copyright (c) 2014 Liviu Ionescu. -// - -#ifndef TIMER_H_ -#define TIMER_H_ - -#include "cmsis_device.h" - -// ---------------------------------------------------------------------------- - -#define TIMER_FREQUENCY_HZ (1000u) - -typedef uint32_t timer_ticks_t; - -extern volatile timer_ticks_t timer_delayCount; - -extern void -timer_start (void); - -extern void -timer_sleep (timer_ticks_t ticks); - -// ---------------------------------------------------------------------------- - -#endif // TIMER_H_ diff --git a/source/test/src/include/include.mk b/source/test/src/include/include.mk deleted file mode 100644 index 4c58a42..0000000 --- a/source/test/src/include/include.mk +++ /dev/null @@ -1 +0,0 @@ -INCLUDES += include diff --git a/source/test/src/main.c b/source/test/src/main.c index 1cf1eba..d959f86 100644 --- a/source/test/src/main.c +++ b/source/test/src/main.c @@ -3,20 +3,16 @@ // Copyright (c) 2014 Liviu Ionescu. // +#include #include #include -#include "diag/Trace.h" - -#include "Timer.h" -#include "BlinkLed.h" +#include "stack.h" +#include "queue.h" +#include "kernel.h" #include "board.h" #include "driver.h" -// Keep the LED on for 2/3 of a second. -#define BLINK_ON_TICKS (TIMER_FREQUENCY_HZ * 3 / 4) -#define BLINK_OFF_TICKS (TIMER_FREQUENCY_HZ - BLINK_ON_TICKS) - // Sample pragmas to cope with warnings. Please note the related line at // the end of this function, used to pop the compiler diagnostics status. #pragma GCC diagnostic push @@ -24,25 +20,30 @@ #pragma GCC diagnostic ignored "-Wmissing-declarations" #pragma GCC diagnostic ignored "-Wreturn-type" -int main(int argc, char* argv[]) +#define TH_STACK_SIZE 256 +stack_t th_stack[TH_STACK_SIZE]; +struct thread_context th_ctx; +static void th_func(void *arg) { - trace_puts("Hello ARM World!"); - trace_printf("System clock: %u Hz\n", SystemCoreClock); - timer_start(); -// blink_led_init(); - uint32_t seconds = 0; drv_open(&gpio_d12); - drv_write(&gpio_d12, "1", 1); + drv_write(&gpio_d12, "0", 1); while(1) { -// blink_led_on(); + sleep_ms(1000); drv_write(&gpio_d12, "1", 1); - timer_sleep(seconds == 0 ? TIMER_FREQUENCY_HZ : BLINK_ON_TICKS); -// blink_led_off(); + sleep_ms(1000); drv_write(&gpio_d12, "0", 1); - timer_sleep(BLINK_OFF_TICKS); - ++seconds; - trace_printf("Second %u\n", seconds); } } + +int main(int argc, char* argv[]) +{ + board_init(); + thread_create(&th_ctx, th_stack, TH_STACK_SIZE, th_func, NULL, THREAD_PRIO_LOW); + + schedule_start(); + + return 0; +} + #pragma GCC diagnostic pop