Switch to freertos timers (#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 <riksu9000@gmail.com>
Co-authored-by: NeroBurner <pyro4hell@gmail.com>
This commit is contained in:
Michele Bini 2022-06-06 17:47:43 +02:00 committed by GitHub
parent f95147cf0e
commit 35dcf8c860
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 32 additions and 43 deletions

View File

@ -106,7 +106,6 @@ macro(nRF5x_setup)
${NRF5_SDK_PATH}/external/freertos/source/stream_buffer.c ${NRF5_SDK_PATH}/external/freertos/source/stream_buffer.c
${NRF5_SDK_PATH}/external/freertos/source/tasks.c ${NRF5_SDK_PATH}/external/freertos/source/tasks.c
${NRF5_SDK_PATH}/external/freertos/source/timers.c ${NRF5_SDK_PATH}/external/freertos/source/timers.c
${NRF5_SDK_PATH}/components/libraries/timer/app_timer_freertos.c
) )
# freertos include # freertos include
@ -335,7 +334,6 @@ endmacro(nRF5x_addAppFIFO)
# adds app-level Timer libraries # adds app-level Timer libraries
macro(nRF5x_addAppTimer) macro(nRF5x_addAppTimer)
list(APPEND SDK_SOURCE_FILES list(APPEND SDK_SOURCE_FILES
"${NRF5_SDK_PATH}/components/libraries/timer/app_timer.c"
) )
endmacro(nRF5x_addAppTimer) endmacro(nRF5x_addAppTimer)

View File

@ -42,7 +42,6 @@ set(SDK_SOURCE_FILES
"${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_gpiote.c" "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_gpiote.c"
"${NRF5_SDK_PATH}/modules/nrfx/soc/nrfx_atomic.c" "${NRF5_SDK_PATH}/modules/nrfx/soc/nrfx_atomic.c"
"${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_saadc.c" "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_saadc.c"
"${NRF5_SDK_PATH}/components/libraries/timer/app_timer.h"
# FreeRTOS # FreeRTOS
${NRF5_SDK_PATH}/external/freertos/source/croutine.c ${NRF5_SDK_PATH}/external/freertos/source/croutine.c
@ -53,7 +52,6 @@ set(SDK_SOURCE_FILES
${NRF5_SDK_PATH}/external/freertos/source/stream_buffer.c ${NRF5_SDK_PATH}/external/freertos/source/stream_buffer.c
${NRF5_SDK_PATH}/external/freertos/source/tasks.c ${NRF5_SDK_PATH}/external/freertos/source/tasks.c
${NRF5_SDK_PATH}/external/freertos/source/timers.c ${NRF5_SDK_PATH}/external/freertos/source/timers.c
${NRF5_SDK_PATH}/components/libraries/timer/app_timer_freertos.c
# Libs # Libs
"${NRF5_SDK_PATH}/components/libraries/atomic/nrf_atomic.c" "${NRF5_SDK_PATH}/components/libraries/atomic/nrf_atomic.c"

View File

@ -17,7 +17,6 @@
*/ */
#include "components/alarm/AlarmController.h" #include "components/alarm/AlarmController.h"
#include "systemtask/SystemTask.h" #include "systemtask/SystemTask.h"
#include "app_timer.h"
#include "task.h" #include "task.h"
#include <chrono> #include <chrono>
@ -27,20 +26,16 @@ using namespace std::chrono_literals;
AlarmController::AlarmController(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} { AlarmController::AlarmController(Controllers::DateTime& dateTimeController) : dateTimeController {dateTimeController} {
} }
APP_TIMER_DEF(alarmAppTimer);
namespace { namespace {
void SetOffAlarm(void* p_context) { void SetOffAlarm(TimerHandle_t xTimer) {
auto* controller = static_cast<Pinetime::Controllers::AlarmController*>(p_context); auto controller = static_cast<Pinetime::Controllers::AlarmController*>(pvTimerGetTimerID(xTimer));
if (controller != nullptr) {
controller->SetOffAlarmNow(); controller->SetOffAlarmNow();
} }
}
} }
void AlarmController::Init(System::SystemTask* systemTask) { void AlarmController::Init(System::SystemTask* systemTask) {
app_timer_create(&alarmAppTimer, APP_TIMER_MODE_SINGLE_SHOT, SetOffAlarm);
this->systemTask = systemTask; this->systemTask = systemTask;
alarmTimer = xTimerCreate("Alarm", 1, pdFALSE, this, SetOffAlarm);
} }
void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
@ -49,8 +44,8 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) {
} }
void AlarmController::ScheduleAlarm() { void AlarmController::ScheduleAlarm() {
// Determine the next time the alarm needs to go off and set the app_timer // Determine the next time the alarm needs to go off and set the timer
app_timer_stop(alarmAppTimer); xTimerStop(alarmTimer, 0);
auto now = dateTimeController.CurrentDateTime(); auto now = dateTimeController.CurrentDateTime();
alarmTime = now; alarmTime = now;
@ -80,8 +75,9 @@ void AlarmController::ScheduleAlarm() {
// now can convert back to a time_point // now can convert back to a time_point
alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime)); alarmTime = std::chrono::system_clock::from_time_t(std::mktime(tmAlarmTime));
auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count(); auto secondsToAlarm = std::chrono::duration_cast<std::chrono::seconds>(alarmTime - now).count();
app_timer_start(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), this); xTimerChangePeriod(alarmTimer, secondsToAlarm * configTICK_RATE_HZ, 0);
xTimerStart(alarmTimer, 0);
state = AlarmState::Set; state = AlarmState::Set;
} }
@ -91,7 +87,7 @@ uint32_t AlarmController::SecondsToAlarm() {
} }
void AlarmController::DisableAlarm() { void AlarmController::DisableAlarm() {
app_timer_stop(alarmAppTimer); xTimerStop(alarmTimer, 0);
state = AlarmState::Not_Set; state = AlarmState::Not_Set;
} }
@ -101,14 +97,12 @@ void AlarmController::SetOffAlarmNow() {
} }
void AlarmController::StopAlerting() { void AlarmController::StopAlerting() {
systemTask->PushMessage(System::Messages::StopRinging);
// Alarm state is off unless this is a recurring alarm // Alarm state is off unless this is a recurring alarm
if (recurrence == RecurType::None) { if (recurrence == RecurType::None) {
state = AlarmState::Not_Set; state = AlarmState::Not_Set;
} else { } else {
state = AlarmState::Set;
// set next instance // set next instance
ScheduleAlarm(); ScheduleAlarm();
} }
systemTask->PushMessage(System::Messages::StopRinging);
} }

View File

@ -17,6 +17,8 @@
*/ */
#pragma once #pragma once
#include <FreeRTOS.h>
#include <timers.h>
#include <cstdint> #include <cstdint>
#include "components/datetime/DateTimeController.h" #include "components/datetime/DateTimeController.h"
@ -57,6 +59,7 @@ namespace Pinetime {
private: private:
Controllers::DateTime& dateTimeController; Controllers::DateTime& dateTimeController;
System::SystemTask* systemTask = nullptr; System::SystemTask* systemTask = nullptr;
TimerHandle_t alarmTimer;
uint8_t hours = 7; uint8_t hours = 7;
uint8_t minutes = 0; uint8_t minutes = 0;
std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> alarmTime; std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> alarmTime;

View File

@ -1,43 +1,39 @@
#include "components/motor/MotorController.h" #include "components/motor/MotorController.h"
#include <hal/nrf_gpio.h> #include <hal/nrf_gpio.h>
#include "systemtask/SystemTask.h" #include "systemtask/SystemTask.h"
#include "app_timer.h"
#include "drivers/PinMap.h" #include "drivers/PinMap.h"
APP_TIMER_DEF(shortVibTimer);
APP_TIMER_DEF(longVibTimer);
using namespace Pinetime::Controllers; using namespace Pinetime::Controllers;
void MotorController::Init() { void MotorController::Init() {
nrf_gpio_cfg_output(PinMap::Motor); nrf_gpio_cfg_output(PinMap::Motor);
nrf_gpio_pin_set(PinMap::Motor); nrf_gpio_pin_set(PinMap::Motor);
app_timer_init();
app_timer_create(&shortVibTimer, APP_TIMER_MODE_SINGLE_SHOT, StopMotor); shortVib = xTimerCreate("shortVib", 1, pdFALSE, nullptr, StopMotor);
app_timer_create(&longVibTimer, APP_TIMER_MODE_REPEATED, Ring); longVib = xTimerCreate("longVib", pdMS_TO_TICKS(1000), pdTRUE, this, Ring);
} }
void MotorController::Ring(void* p_context) { void MotorController::Ring(TimerHandle_t xTimer) {
auto* motorController = static_cast<MotorController*>(p_context); auto* motorController = static_cast<MotorController*>(pvTimerGetTimerID(xTimer));
motorController->RunForDuration(50); motorController->RunForDuration(50);
} }
void MotorController::RunForDuration(uint8_t motorDuration) { void MotorController::RunForDuration(uint8_t motorDuration) {
if (xTimerChangePeriod(shortVib, pdMS_TO_TICKS(motorDuration), 0) == pdPASS && xTimerStart(shortVib, 0) == pdPASS) {
nrf_gpio_pin_clear(PinMap::Motor); nrf_gpio_pin_clear(PinMap::Motor);
app_timer_start(shortVibTimer, APP_TIMER_TICKS(motorDuration), nullptr); }
} }
void MotorController::StartRinging() { void MotorController::StartRinging() {
Ring(this); RunForDuration(50);
app_timer_start(longVibTimer, APP_TIMER_TICKS(1000), this); xTimerStart(longVib, 0);
} }
void MotorController::StopRinging() { void MotorController::StopRinging() {
app_timer_stop(longVibTimer); xTimerStop(longVib, 0);
nrf_gpio_pin_set(PinMap::Motor); nrf_gpio_pin_set(PinMap::Motor);
} }
void MotorController::StopMotor(void* p_context) { void MotorController::StopMotor(TimerHandle_t xTimer) {
nrf_gpio_pin_set(PinMap::Motor); nrf_gpio_pin_set(PinMap::Motor);
} }

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include <FreeRTOS.h>
#include <timers.h>
#include <cstdint> #include <cstdint>
namespace Pinetime { namespace Pinetime {
@ -15,8 +17,10 @@ namespace Pinetime {
void StopRinging(); void StopRinging();
private: private:
static void Ring(void* p_context); static void Ring(TimerHandle_t xTimer);
static void StopMotor(void* p_context); static void StopMotor(TimerHandle_t xTimer);
TimerHandle_t shortVib;
TimerHandle_t longVib;
}; };
} }
} }

View File

@ -3,7 +3,6 @@
#include <hal/nrf_wdt.h> #include <hal/nrf_wdt.h>
#include <legacy/nrf_drv_clock.h> #include <legacy/nrf_drv_clock.h>
#include <libraries/gpiote/app_gpiote.h> #include <libraries/gpiote/app_gpiote.h>
#include <libraries/timer/app_timer.h>
#include <softdevice/common/nrf_sdh.h> #include <softdevice/common/nrf_sdh.h>
#include <nrf_delay.h> #include <nrf_delay.h>

View File

@ -6781,7 +6781,7 @@
// <e> APP_TIMER_ENABLED - app_timer - Application timer functionality // <e> APP_TIMER_ENABLED - app_timer - Application timer functionality
//========================================================== //==========================================================
#ifndef APP_TIMER_ENABLED #ifndef APP_TIMER_ENABLED
#define APP_TIMER_ENABLED 1 #define APP_TIMER_ENABLED 0
#endif #endif
// <o> APP_TIMER_CONFIG_RTC_FREQUENCY - Configure RTC prescaler. // <o> APP_TIMER_CONFIG_RTC_FREQUENCY - Configure RTC prescaler.

View File

@ -2,7 +2,6 @@
#include <hal/nrf_rtc.h> #include <hal/nrf_rtc.h>
#include <libraries/gpiote/app_gpiote.h> #include <libraries/gpiote/app_gpiote.h>
#include <libraries/log/nrf_log.h> #include <libraries/log/nrf_log.h>
#include <app_timer.h>
#include "BootloaderVersion.h" #include "BootloaderVersion.h"
#include "components/battery/BatteryController.h" #include "components/battery/BatteryController.h"
#include "components/ble/BleController.h" #include "components/ble/BleController.h"
@ -127,8 +126,6 @@ void SystemTask::Work() {
NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason())); NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason()));
APP_GPIOTE_INIT(2); APP_GPIOTE_INIT(2);
app_timer_init();
spi.Init(); spi.Init();
spiNorFlash.Init(); spiNorFlash.Init();
spiNorFlash.Wakeup(); spiNorFlash.Wakeup();