-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hw timer #259
Closed
Hw timer #259
Changes from 5 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
36c52ae
Delay function
b616720
removed unnesscary includes
f144ff6
Cleanup scons mess (#246)
ShiCheng-Lu f0b1804
Python CAN Parsing (#253)
Akashem06 13f3c48
Pd rev2 fixes (#258)
Akashem06 0a89ae6
updated smoke test
1db1902
formatted smoke project
0f7b3ed
added hw_timer call back functionality
48aee72
made revisions
e5dc97d
testing thread safe
Akashem06 2867af2
Delay function
e15e515
removed unnesscary includes
38396f1
updated smoke test
59163ac
formatted smoke project
e43e69a
added hw_timer call back functionality
4125ddd
made revisions
de653c5
rebased main
Akashem06 89e24a9
hw timer using compare registers
Akashem06 4a4ad8c
hw timer using compare registers
Akashem06 f0ae343
using compare registers for hardware timer
Akashem06 7bac614
formatting
Akashem06 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,19 @@ | ||
#pragma once | ||
|
||
#include <stdint.h> | ||
|
||
#include "status.h" | ||
#include "stm32f10x_rcc.h" | ||
#include "stm32f10x_tim.h" | ||
|
||
typedef void (*TimerCallback)(void); | ||
|
||
#define hw_timer_delay_ms(ms) hw_timer_delay_us((ms)*1000) | ||
#define hw_timer_callback_ms(ms, callback_function) \ | ||
hw_timer_delay_us((ms)*1000, (callback_function)) | ||
|
||
void hw_timer_init(void); | ||
|
||
void hw_timer_delay_us(uint32_t us); | ||
|
||
void hw_timer_callback_us(uint32_t us, TimerCallback callback_function); |
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,54 @@ | ||
#include "hw_timer.h" | ||
|
||
#include "log.h" | ||
#include "stm32f10x.h" | ||
|
||
#define hw_timer_delay_ms(ms) hw_timer_delay_us((ms)*1000) | ||
#define hw_timer_callback_ms(ms, callback_function) \ | ||
hw_timer_delay_us((ms)*1000, (callback_function)) | ||
|
||
void hw_timer_init(void) { | ||
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); | ||
|
||
TIM_Cmd(TIM2, DISABLE); | ||
TIM_ITConfig(TIM2, TIM_IT_CC1, DISABLE); | ||
TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE); | ||
|
||
RCC_ClocksTypeDef clocks; | ||
RCC_GetClocksFreq(&clocks); | ||
|
||
TIM_TimeBaseInitTypeDef timer_init = { | ||
.TIM_Prescaler = (clocks.PCLK1_Frequency / 1000000) - 1, // 1 Mhz | ||
.TIM_CounterMode = TIM_CounterMode_Up, | ||
.TIM_Period = UINT16_MAX, | ||
.TIM_ClockDivision = TIM_CKD_DIV1, | ||
}; | ||
TIM_TimeBaseInit(TIM2, &timer_init); | ||
TIM_Cmd(TIM2, ENABLE); | ||
} | ||
|
||
void hw_timer_delay_us(uint32_t us) { | ||
TIM_SetCounter(TIM2, 0); | ||
TIM_Cmd(TIM2, ENABLE); | ||
|
||
uint32_t start_count = TIM_GetCounter(TIM2); | ||
uint32_t elapsed_time = 0; | ||
|
||
while (elapsed_time < us) { | ||
uint32_t current_count = TIM_GetCounter(TIM2); | ||
if (current_count <= start_count) { | ||
elapsed_time += (UINT16_MAX + 1 - start_count) + current_count; | ||
} else { | ||
elapsed_time += current_count - start_count; | ||
} | ||
start_count = current_count; | ||
} | ||
|
||
TIM_Cmd(TIM2, DISABLE); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a function for init and enable the timer |
||
} | ||
|
||
void hw_timer_callback_us(uint32_t us, TimerCallback callback_function) { | ||
hw_timer_delay_us(us); | ||
|
||
if (callback_function != NULL) callback_function(); | ||
} |
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,16 @@ | ||
<!-- | ||
General guidelines | ||
These are just guidelines, not strict rules - document however seems best. | ||
A README for a firmware-only project (e.g. Babydriver, MPXE, bootloader, CAN explorer) should answer the following questions: | ||
- What is it? | ||
- What problem does it solve? | ||
- How do I use it? (with usage examples / example commands, etc) | ||
- How does it work? (architectural overview) | ||
A README for a board project (powering a hardware board, e.g. power distribution, centre console, charger, BMS carrier) should answer the following questions: | ||
- What is the purpose of the board? | ||
- What are all the things that the firmware needs to do? | ||
- How does it fit into the overall system? | ||
- How does it work? (architectural overview, e.g. what each module's purpose is or how data flows through the firmware) | ||
--> | ||
# adc | ||
|
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,6 @@ | ||
{ | ||
"libs": [ | ||
"FreeRTOS", | ||
"ms-common" | ||
] | ||
} |
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,45 @@ | ||
#include <stdio.h> | ||
|
||
#include "adc.h" | ||
#include "delay.h" | ||
#include "gpio.h" | ||
#include "hw_timer.h" | ||
#include "interrupt.h" | ||
#include "log.h" | ||
#include "tasks.h" | ||
|
||
static const GpioAddress leds[] = { | ||
{ .port = GPIO_PORT_B, .pin = 5 }, // | ||
{ .port = GPIO_PORT_B, .pin = 4 }, // | ||
{ .port = GPIO_PORT_B, .pin = 3 }, // | ||
{ .port = GPIO_PORT_A, .pin = 15 }, // | ||
}; | ||
|
||
void pre_loop_init() {} | ||
|
||
TASK(hw_timer_task, TASK_STACK_512) { | ||
for (uint8_t i = 0; i < SIZEOF_ARRAY(leds); i++) { | ||
gpio_init_pin(&leds[i], GPIO_OUTPUT_PUSH_PULL, GPIO_STATE_LOW); | ||
} | ||
|
||
while (true) { | ||
gpio_toggle_state(&leds[0]); | ||
gpio_toggle_state(&leds[3]); | ||
LOG_DEBUG("HW Delay"); | ||
hw_timer_delay_us(100); | ||
} | ||
} | ||
|
||
int main() { | ||
tasks_init(); | ||
interrupt_init(); | ||
gpio_init(); | ||
hw_timer_init(); | ||
log_init(); | ||
|
||
tasks_init_task(hw_timer_task, TASK_PRIORITY(2), NULL); | ||
|
||
tasks_start(); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, a possible issue with this is if a task delays for say 2ms, then gets pre empted by a task that delays for 1ms, the higher priority task would reset the timer, complete its delay, and then when the original task takes over, the timer would be disabled. I'm not 100% sure what the best fix for this is but I'll have to think about that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would it? TIM_GetCounter should be an atomic call. So the unless it happens before
start_count
, it wouldn't be an issue right?If that's the case, we could critical section this part until later