-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jarvis
committed
Nov 28, 2024
1 parent
537f682
commit 3f2b3ff
Showing
3 changed files
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
#include "stm32f10x_tim.h" | ||
#include "stm32f10x_rcc.h" | ||
#include "status.h" | ||
#include <stdint.h> | ||
#include <stdbool.h> | ||
#include <stddef.h> | ||
|
||
|
||
typedef void (*HardwareTimerCallback) (void); | ||
|
||
|
||
StatusCode hardware_timer_init_and_start(uint32_t duration_us, HardwareTimerCallback callback); | ||
|
||
StatusCode hardware_timer_interrupt(uint32_t duration_us); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <stdio.h> | ||
|
||
#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; | ||
} |