Skip to content

Commit 35dcf8c

Browse files
rev22Riksu9000NeroBurner
authored
Switch to freertos timers (InfiniTimeOrg#1095)
* Use FreeRTOS timer for AlarmController * Use FreeRTOS timer for MotorController * Remove app_timer component from compilation as we now solely use FreeROTS timer * Simplify variable and text names for AlarmController and MotorController timers * Call ScheduleAlarm directly from StopAlerting, for recurring timers Co-authored-by: Riku Isokoski <[email protected]> Co-authored-by: NeroBurner <[email protected]>
1 parent f95147c commit 35dcf8c

9 files changed

+32
-43
lines changed

cmake-nRF5x/CMake_nRF5x.cmake

-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ macro(nRF5x_setup)
106106
${NRF5_SDK_PATH}/external/freertos/source/stream_buffer.c
107107
${NRF5_SDK_PATH}/external/freertos/source/tasks.c
108108
${NRF5_SDK_PATH}/external/freertos/source/timers.c
109-
${NRF5_SDK_PATH}/components/libraries/timer/app_timer_freertos.c
110109
)
111110

112111
# freertos include
@@ -335,7 +334,6 @@ endmacro(nRF5x_addAppFIFO)
335334
# adds app-level Timer libraries
336335
macro(nRF5x_addAppTimer)
337336
list(APPEND SDK_SOURCE_FILES
338-
"${NRF5_SDK_PATH}/components/libraries/timer/app_timer.c"
339337
)
340338
endmacro(nRF5x_addAppTimer)
341339

src/CMakeLists.txt

-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ set(SDK_SOURCE_FILES
4242
"${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_gpiote.c"
4343
"${NRF5_SDK_PATH}/modules/nrfx/soc/nrfx_atomic.c"
4444
"${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_saadc.c"
45-
"${NRF5_SDK_PATH}/components/libraries/timer/app_timer.h"
4645

4746
# FreeRTOS
4847
${NRF5_SDK_PATH}/external/freertos/source/croutine.c
@@ -53,7 +52,6 @@ set(SDK_SOURCE_FILES
5352
${NRF5_SDK_PATH}/external/freertos/source/stream_buffer.c
5453
${NRF5_SDK_PATH}/external/freertos/source/tasks.c
5554
${NRF5_SDK_PATH}/external/freertos/source/timers.c
56-
${NRF5_SDK_PATH}/components/libraries/timer/app_timer_freertos.c
5755

5856
# Libs
5957
"${NRF5_SDK_PATH}/components/libraries/atomic/nrf_atomic.c"

src/components/alarm/AlarmController.cpp

+11-17
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
*/
1818
#include "components/alarm/AlarmController.h"
1919
#include "systemtask/SystemTask.h"
20-
#include "app_timer.h"
2120
#include "task.h"
2221
#include <chrono>
2322

@@ -27,20 +26,16 @@ using namespace std::chrono_literals;
2726
AlarmController::AlarmController(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} {
2827
}
2928

30-
APP_TIMER_DEF(alarmAppTimer);
31-
3229
namespace {
33-
void SetOffAlarm(void* p_context) {
34-
auto* controller = static_cast<Pinetime::Controllers::AlarmController*>(p_context);
35-
if (controller != nullptr) {
36-
controller->SetOffAlarmNow();
37-
}
30+
void SetOffAlarm(TimerHandle_t xTimer) {
31+
auto controller = static_cast<Pinetime::Controllers::AlarmController*>(pvTimerGetTimerID(xTimer));
32+
controller->SetOffAlarmNow();
3833
}
3934
}
4035

4136
void AlarmController::Init(System::SystemTask* systemTask) {
42-
app_timer_create(&alarmAppTimer, APP_TIMER_MODE_SINGLE_SHOT, SetOffAlarm);
4337
this->systemTask = systemTask;
38+
alarmTimer = xTimerCreate("Alarm", 1, pdFALSE, this, SetOffAlarm);
4439
}
4540

4641
void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
@@ -49,8 +44,8 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
4944
}
5045

5146
void AlarmController::ScheduleAlarm() {
52-
// Determine the next time the alarm needs to go off and set the app_timer
53-
app_timer_stop(alarmAppTimer);
47+
// Determine the next time the alarm needs to go off and set the timer
48+
xTimerStop(alarmTimer, 0);
5449

5550
auto now = dateTimeController.CurrentDateTime();
5651
alarmTime = now;
@@ -80,8 +75,9 @@ void AlarmController::ScheduleAlarm() {
8075

8176
// now can convert back to a time_point
8277
alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime));
83-
auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count();
84-
app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this);
78+
auto secondsToAlarm = std::chrono::duration_cast<std::chrono::seconds>(alarmTime - now).count();
79+
xTimerChangePeriod(alarmTimer, secondsToAlarm * configTICK_RATE_HZ, 0);
80+
xTimerStart(alarmTimer, 0);
8581

8682
state = AlarmState::Set;
8783
}
@@ -91,7 +87,7 @@ uint32_t AlarmController::SecondsToAlarm() {
9187
}
9288

9389
void AlarmController::DisableAlarm() {
94-
app_timer_stop(alarmAppTimer);
90+
xTimerStop(alarmTimer, 0);
9591
state = AlarmState::Not_Set;
9692
}
9793

@@ -101,14 +97,12 @@ void AlarmController::SetOffAlarmNow() {
10197
}
10298

10399
void AlarmController::StopAlerting() {
104-
systemTask->PushMessage(System::Messages::StopRinging);
105-
106100
// Alarm state is off unless this is a recurring alarm
107101
if (recurrence == RecurType::None) {
108102
state = AlarmState::Not_Set;
109103
} else {
110-
state = AlarmState::Set;
111104
// set next instance
112105
ScheduleAlarm();
113106
}
107+
systemTask->PushMessage(System::Messages::StopRinging);
114108
}

src/components/alarm/AlarmController.h

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
#pragma once
1919

20+
#include <FreeRTOS.h>
21+
#include <timers.h>
2022
#include <cstdint>
2123
#include "components/datetime/DateTimeController.h"
2224

@@ -57,6 +59,7 @@ namespace Pinetime {
5759
private:
5860
Controllers::DateTime& dateTimeController;
5961
System::SystemTask* systemTask = nullptr;
62+
TimerHandle_t alarmTimer;
6063
uint8_t hours = 7;
6164
uint8_t minutes = 0;
6265
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> alarmTime;
+11-15
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,39 @@
11
#include "components/motor/MotorController.h"
22
#include <hal/nrf_gpio.h>
33
#include "systemtask/SystemTask.h"
4-
#include "app_timer.h"
54
#include "drivers/PinMap.h"
65

7-
APP_TIMER_DEF(shortVibTimer);
8-
APP_TIMER_DEF(longVibTimer);
9-
106
using namespace Pinetime::Controllers;
117

128
void MotorController::Init() {
139
nrf_gpio_cfg_output(PinMap::Motor);
1410
nrf_gpio_pin_set(PinMap::Motor);
15-
app_timer_init();
1611

17-
app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor);
18-
app_timer_create(&longVibTimer, APP_TIMER_MODE_REPEATED, Ring);
12+
shortVib = xTimerCreate("shortVib", 1, pdFALSE, nullptr, StopMotor);
13+
longVib = xTimerCreate("longVib", pdMS_TO_TICKS(1000), pdTRUE, this, Ring);
1914
}
2015

21-
void MotorController::Ring(void* p_context) {
22-
auto* motorController = static_cast<MotorController*>(p_context);
16+
void MotorController::Ring(TimerHandle_t xTimer) {
17+
auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer));
2318
motorController->RunForDuration(50);
2419
}
2520

2621
void MotorController::RunForDuration(uint8_t motorDuration) {
27-
nrf_gpio_pin_clear(PinMap::Motor);
28-
app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr);
22+
if (xTimerChangePeriod(shortVib, pdMS_TO_TICKS(motorDuration), 0) == pdPASS && xTimerStart(shortVib, 0) == pdPASS) {
23+
nrf_gpio_pin_clear(PinMap::Motor);
24+
}
2925
}
3026

3127
void MotorController::StartRinging() {
32-
Ring(this);
33-
app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this);
28+
RunForDuration(50);
29+
xTimerStart(longVib, 0);
3430
}
3531

3632
void MotorController::StopRinging() {
37-
app_timer_stop(longVibTimer);
33+
xTimerStop(longVib, 0);
3834
nrf_gpio_pin_set(PinMap::Motor);
3935
}
4036

41-
void MotorController::StopMotor(void* p_context) {
37+
void MotorController::StopMotor(TimerHandle_t xTimer) {
4238
nrf_gpio_pin_set(PinMap::Motor);
4339
}

src/components/motor/MotorController.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#pragma once
22

3+
#include <FreeRTOS.h>
4+
#include <timers.h>
35
#include <cstdint>
46

57
namespace Pinetime {
@@ -15,8 +17,10 @@ namespace Pinetime {
1517
void StopRinging();
1618

1719
private:
18-
static void Ring(void* p_context);
19-
static void StopMotor(void* p_context);
20+
static void Ring(TimerHandle_t xTimer);
21+
static void StopMotor(TimerHandle_t xTimer);
22+
TimerHandle_t shortVib;
23+
TimerHandle_t longVib;
2024
};
2125
}
2226
}

src/main.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
#include <hal/nrf_wdt.h>
44
#include <legacy/nrf_drv_clock.h>
55
#include <libraries/gpiote/app_gpiote.h>
6-
#include <libraries/timer/app_timer.h>
76
#include <softdevice/common/nrf_sdh.h>
87
#include <nrf_delay.h>
98

src/sdk_config.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -6781,7 +6781,7 @@
67816781
// <e> APP_TIMER_ENABLED - app_timer - Application timer functionality
67826782
//==========================================================
67836783
#ifndef APP_TIMER_ENABLED
6784-
#define APP_TIMER_ENABLED 1
6784+
#define APP_TIMER_ENABLED 0
67856785
#endif
67866786
// <o> APP_TIMER_CONFIG_RTC_FREQUENCY - Configure RTC prescaler.
67876787

src/systemtask/SystemTask.cpp

-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <hal/nrf_rtc.h>
33
#include <libraries/gpiote/app_gpiote.h>
44
#include <libraries/log/nrf_log.h>
5-
#include <app_timer.h>
65
#include "BootloaderVersion.h"
76
#include "components/battery/BatteryController.h"
87
#include "components/ble/BleController.h"
@@ -127,8 +126,6 @@ void SystemTask::Work() {
127126
NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason()));
128127
APP_GPIOTE_INIT(2);
129128

130-
app_timer_init();
131-
132129
spi.Init();
133130
spiNorFlash.Init();
134131
spiNorFlash.Wakeup();

0 commit comments

Comments
 (0)