From 3f2b3ff36a655c6f22762a33e2e1d06f70139bde Mon Sep 17 00:00:00 2001 From: Jarvis Date: Thu, 28 Nov 2024 00:41:15 +0000 Subject: [PATCH] smoke test --- libraries/ms-common/inc/hw_timer.h | 15 ++++++ libraries/ms-common/src/arm/hw_timer.c | 71 ++++++++++++++++++++++++++ smoke/hardware_timer/src/main.c | 41 +++++++++++++++ 3 files changed, 127 insertions(+) create mode 100644 libraries/ms-common/inc/hw_timer.h create mode 100644 libraries/ms-common/src/arm/hw_timer.c create mode 100644 smoke/hardware_timer/src/main.c diff --git a/libraries/ms-common/inc/hw_timer.h b/libraries/ms-common/inc/hw_timer.h new file mode 100644 index 00000000..5cf2a2ee --- /dev/null +++ b/libraries/ms-common/inc/hw_timer.h @@ -0,0 +1,15 @@ +#pragma once +#include "stm32f10x_tim.h" +#include "stm32f10x_rcc.h" +#include "status.h" +#include +#include +#include + + +typedef void (*HardwareTimerCallback) (void); + + +StatusCode hardware_timer_init_and_start(uint32_t duration_us, HardwareTimerCallback callback); + +StatusCode hardware_timer_interrupt(uint32_t duration_us); \ No newline at end of file diff --git a/libraries/ms-common/src/arm/hw_timer.c b/libraries/ms-common/src/arm/hw_timer.c new file mode 100644 index 00000000..25699196 --- /dev/null +++ b/libraries/ms-common/src/arm/hw_timer.c @@ -0,0 +1,71 @@ + +#include "hw_timer.h" + + +typedef struct { + TIM_TypeDef *TIMX; + bool state; + uint32_t ABP_periph; + HardwareTimerCallback callback; +} HWTimerData; + +//Timers configuration +static HWTimerData s_timer[3] = { + {TIM2, false, , NULL}, + {TIM3, false, RCC_APB1Periph_TIM3, NULL}, + {TIM4, false, RCC_APB1Periph_TIM4, NULL} +}; + +// HardwareTimerCallback htim2, htim3, htim4; +void TIM2_IRQHandler(void) { + s_timer[0].callback(); +} +void TIM3_IRQHandler(void) { + s_timer[1].callback(); +} +void TIM4_IRQHandler(void) { + s_timer[2].callback(); +} + + +StatusCode hardware_timer_init_and_start(uint32_t duration_us, HardwareTimerCallback callback){ + //Timer array for which timers are in use. + + // TIM_TypeDef *TIMX; + for (int i = 0; i < 3; i++){ + if (!s_timer[i].state){ + s_timer[i].state = true; + s_timer[i].callback = callback; + + + RCC_APB1PeriphClockCmd(s_timer[i].ABP_periph, ENABLE); + + + TIM_TimeBaseInitTypeDef config = { + 72 - 1, // replace this using clock speed / 1000000 + TIM_CounterMode_Up, + duration_us - 1, + TIM_CKD_DIV1, + 0 + }; + + TIM_TimeBaseInit(s_timer[i].TIMX, &config); // configs timer + + + // enable update interrupt for timer + TIM_ITConfig(s_timer[i].TIMX, TIM_IT_Update, ENABLE); + + + // enable timer + TIM_Cmd(s_timer[i].TIMX, ENABLE); + + return STATUS_CODE_OK; + + } else { + //All timers are in use + return STATUS_CODE_RESOURCE_EXHAUSTED; + } + } + return STATUS_CODE_OK; +} + diff --git a/smoke/hardware_timer/src/main.c b/smoke/hardware_timer/src/main.c new file mode 100644 index 00000000..6ffc7e4d --- /dev/null +++ b/smoke/hardware_timer/src/main.c @@ -0,0 +1,41 @@ +#include + +#include "log.h" +#include "tasks.h" +#include "hw_timer.h" + +void hardware_callback(void){ + LOG_DEBUG("STARTED"); +} + +TASK(HWTimer, TASK_STACK_512) { + LOG_DEBUG("STARTING TASKS \n"); + StatusCode status = hardware_timer_init_and_start(1000, hardware_callback); + LOG_DEBUG("code : %d\n", status); + if (hardware_timer_init_and_start(1000, hardware_callback) != STATUS_CODE_OK) LOG_DEBUG("ERROR 1 \n"); + vTaskDelay(1000); + if (hardware_timer_init_and_start(40,hardware_callback ) != STATUS_CODE_OK) LOG_DEBUG("ERROR 2\n"); + vTaskDelay(1000); + if (hardware_timer_init_and_start(1, hardware_callback) != 0) LOG_DEBUG("ERROR 3\n"); + vTaskDelay(1000); + if (hardware_timer_init_and_start(1, hardware_callback) == 0) LOG_DEBUG("ERROR 4\n"); + vTaskDelay(1000); +} + + + +// in callbacks do log debug +// time how long between the on/off takes for the led pin itself. +// use oscilloscope to test the timing, to see delay of the + +int main() { + tasks_init(); + log_init(); + LOG_DEBUG("Welcome to TEST!"); + tasks_init_task(HWTimer, 3, NULL); + + tasks_start(); + + LOG_DEBUG("exiting main?"); + return 0; +}