From fbb77baa3b6e487f4c68e08770499cb893c4e035 Mon Sep 17 00:00:00 2001 From: jlukanc <27705324+jlukanc1@users.noreply.github.com> Date: Fri, 15 Jan 2021 22:11:53 -0500 Subject: [PATCH] add non-blocking motor controller --- src/CMakeLists.txt | 2 + src/components/motor/MotorController.cpp | 40 +++++++++++++++++ src/components/motor/MotorController.h | 57 ++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 src/components/motor/MotorController.cpp create mode 100644 src/components/motor/MotorController.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fde1f586..b5669414 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -36,10 +36,12 @@ set(SDK_SOURCE_FILES # Base SDK "${NRF5_SDK_PATH}/components/boards/boards.c" "${NRF5_SDK_PATH}/integration/nrfx/legacy/nrf_drv_clock.c" + "${NRF5_SDK_PATH}/integration/nrfx/legacy/nrf_drv_clock.h" "${NRF5_SDK_PATH}/modules/nrfx/drivers/src/nrfx_clock.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/drivers/src/nrfx_saadc.c" + "${NRF5_SDK_PATH}/components/libraries/timer/app_timer.h" # FreeRTOS ${NRF5_SDK_PATH}/external/freertos/source/croutine.c diff --git a/src/components/motor/MotorController.cpp b/src/components/motor/MotorController.cpp new file mode 100644 index 00000000..1885f7bd --- /dev/null +++ b/src/components/motor/MotorController.cpp @@ -0,0 +1,40 @@ +#include "MotorController.h" +#include +#include "systemtask/SystemTask.h" +#include "app_timer.h" +#include "nrf_drv_clock.h" + +APP_TIMER_DEF(vibTimer); + +using namespace Pinetime::Controllers; + +static void lfclk_request(void) //get the low freq. clock +{ + nrf_drv_clock_init(); + nrf_drv_clock_lfclk_request(NULL); +} + +void vibrateTimer(void * p_context) +{ + nrf_gpio_pin_set(pinMotor); +} + +static void create_timers() +{ + //create timer, single shot, re-armable + app_timer_create(&vibTimer, APP_TIMER_MODE_SINGLE_SHOT, vibrateTimer); +} + +void MotorController::Init() { + nrf_gpio_cfg_output(pinMotor); // set the motor pin as an output + nrf_gpio_pin_set(pinMotor); + lfclk_request(); //get lfclock ready + app_timer_init(); //start app timers to make calls + create_timers(); +} + +void MotorController::SetDuration(uint8_t motorDuration) { + nrf_gpio_pin_clear(pinMotor); + //start timer for motorDuration miliseconds + app_timer_start(vibTimer, APP_TIMER_TICKS(motorDuration), NULL); //timers trigger at end of duration? +} \ No newline at end of file diff --git a/src/components/motor/MotorController.h b/src/components/motor/MotorController.h new file mode 100644 index 00000000..6712d933 --- /dev/null +++ b/src/components/motor/MotorController.h @@ -0,0 +1,57 @@ +#pragma once + +#include + +namespace Pinetime { + namespace Controllers { + static constexpr uint8_t pinMotor = 16; + + class MotorController { + public: + void Init(); + void SetDuration(uint8_t motorDuration); + #ifndef NRF_CLOCK_ENABLED + #define NRF_CLOCK_ENABLED 1 + #endif + + #ifndef CLOCK_CONFIG_LF_SRC + #define CLOCK_CONFIG_LF_SRC 1 + #endif + + #ifndef CLOCK_CONFIG_IRQ_PRIORITY + #define CLOCK_CONFIG_IRQ_PRIORITY 6 + #endif + + #define APP_TIMER_ENABLED 1 + #define APP_TIMER_CONFIG_RTC_FREQUENCY 15 //2048hz + #define APP_TIMER_CONFIG_IRQ_PRIORITY 6 + + #ifndef APP_TIMER_CONFIG_OP_QUEUE_SIZE + #define APP_TIMER_CONFIG_OP_QUEUE_SIZE 10 + #endif + + #ifndef APP_TIMER_CONFIG_USE_SCHEDULER + #define APP_TIMER_CONFIG_USE_SCHEDULER 0 + #endif + + #ifndef APP_TIMER_KEEPS_RTC_ACTIVE + #define APP_TIMER_KEEPS_RTC_ACTIVE 0 + #endif + + #ifndef APP_TIMER_SAFE_WINDOW_MS + #define APP_TIMER_SAFE_WINDOW_MS 300000 + #endif + + #ifndef APP_TIMER_WITH_PROFILER + #define APP_TIMER_WITH_PROFILER 0 + #endif + + #ifndef APP_TIMER_CONFIG_SWI_NUMBER + #define APP_TIMER_CONFIG_SWI_NUMBER 0 + #endif + + private: + + }; + } +} \ No newline at end of file