Skip to content
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
wants to merge 21 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions libraries/ms-common/inc/hw_timer.h
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);
54 changes: 54 additions & 0 deletions libraries/ms-common/src/arm/hw_timer.c
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) {
Copy link
Collaborator

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

Copy link
Collaborator

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

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);
Copy link
Collaborator

Choose a reason for hiding this comment

The 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();
}
16 changes: 16 additions & 0 deletions smoke/hw_timer/README.md
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

6 changes: 6 additions & 0 deletions smoke/hw_timer/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"libs": [
"FreeRTOS",
"ms-common"
]
}
45 changes: 45 additions & 0 deletions smoke/hw_timer/src/main.c
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;
}
Loading