From 2b3e6b1cde7151bea57d56f3909ff5f340c4610b Mon Sep 17 00:00:00 2001 From: JF Date: Sat, 18 Jan 2020 20:53:32 +0100 Subject: [PATCH] Refactor ctor() and Ini() of SpiMaster. --- src/DisplayApp/DisplayApp.cpp | 48 +++++++++++++++++------------------ src/DisplayApp/DisplayApp.h | 8 ++++++ src/drivers/SpiMaster.cpp | 17 +++++++------ src/drivers/SpiMaster.h | 8 +++--- 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/src/DisplayApp/DisplayApp.cpp b/src/DisplayApp/DisplayApp.cpp index 3fa0749e..3b7007af 100644 --- a/src/DisplayApp/DisplayApp.cpp +++ b/src/DisplayApp/DisplayApp.cpp @@ -17,8 +17,16 @@ using namespace Pinetime::Applications; DisplayApp::DisplayApp(Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController) : - spi{}, - lcd{new Drivers::St7789(spi, 18)}, + spi{Drivers::SpiMaster::SpiModule::SPI0, { + Drivers::SpiMaster::BitOrder::Msb_Lsb, + Drivers::SpiMaster::Modes::Mode3, + Drivers::SpiMaster::Frequencies::Freq8Mhz, + pinSpiSck, + pinSpiMosi, + pinSpiMiso, + pinSpiCsn + }}, + lcd{new Drivers::St7789(spi, pinLcdDataCommand)}, gfx{new Components::Gfx(*lcd.get()) }, batteryController{batteryController}, bleController{bleController}, @@ -44,26 +52,16 @@ void DisplayApp::Process(void *instance) { } void DisplayApp::InitHw() { - nrf_gpio_cfg_output(14); - nrf_gpio_cfg_output(22); - nrf_gpio_cfg_output(23); - nrf_gpio_pin_clear(14); - nrf_gpio_pin_clear(22); - nrf_gpio_pin_clear(23); + nrf_gpio_cfg_output(pinLcdBacklight1); + nrf_gpio_cfg_output(pinLcdBacklight2); + nrf_gpio_cfg_output(pinLcdBacklight3); + nrf_gpio_pin_clear(pinLcdBacklight1); + nrf_gpio_pin_clear(pinLcdBacklight2); + nrf_gpio_pin_clear(pinLcdBacklight3); - Drivers::SpiMaster::Parameters params; - params.bitOrder = Drivers::SpiMaster::BitOrder::Msb_Lsb; - params.mode = Drivers::SpiMaster::Modes::Mode3; - params.Frequency = Drivers::SpiMaster::Frequencies::Freq8Mhz; - params.pinCSN = 25; - params.pinMISO = 4; - params.pinMOSI = 3; - params.pinSCK = 2; - spi.Init(Drivers::SpiMaster::SpiModule::SPI0, params); + spi.Init(); gfx->Init(); - currentScreen->Refresh(true); - touchPanel.Init(); } @@ -84,11 +82,11 @@ void DisplayApp::Refresh() { if (xQueueReceive(msgQueue, &msg, queueTimeout)) { switch (msg) { case Messages::GoToSleep: - nrf_gpio_pin_set(23); + nrf_gpio_pin_set(pinLcdBacklight3); vTaskDelay(100); - nrf_gpio_pin_set(22); + nrf_gpio_pin_set(pinLcdBacklight2); vTaskDelay(100); - nrf_gpio_pin_set(14); + nrf_gpio_pin_set(pinLcdBacklight1); lcd->DisplayOff(); lcd->Sleep(); touchPanel.Sleep(); @@ -99,9 +97,9 @@ void DisplayApp::Refresh() { touchPanel.Wakeup(); lcd->DisplayOn(); - nrf_gpio_pin_clear(23); - nrf_gpio_pin_clear(22); - nrf_gpio_pin_clear(14); + nrf_gpio_pin_clear(pinLcdBacklight3); + nrf_gpio_pin_clear(pinLcdBacklight2); + nrf_gpio_pin_clear(pinLcdBacklight1); state = States::Running; break; case Messages::UpdateDateTime: diff --git a/src/DisplayApp/DisplayApp.h b/src/DisplayApp/DisplayApp.h index b2a4128d..5fb8f6f8 100644 --- a/src/DisplayApp/DisplayApp.h +++ b/src/DisplayApp/DisplayApp.h @@ -59,6 +59,14 @@ namespace Pinetime { Screens::Screen* currentScreen = nullptr; // Screens::Message messageScreen; // bool screenState = false; + static constexpr uint8_t pinSpiSck = 2; + static constexpr uint8_t pinSpiMosi = 3; + static constexpr uint8_t pinSpiMiso = 4; + static constexpr uint8_t pinSpiCsn = 25; + static constexpr uint8_t pinLcdDataCommand = 18; + static constexpr uint8_t pinLcdBacklight1 = 14; + static constexpr uint8_t pinLcdBacklight2 = 22; + static constexpr uint8_t pinLcdBacklight3 = 23; }; } } diff --git a/src/drivers/SpiMaster.cpp b/src/drivers/SpiMaster.cpp index 5e49f2a7..42d3d77b 100644 --- a/src/drivers/SpiMaster.cpp +++ b/src/drivers/SpiMaster.cpp @@ -3,10 +3,11 @@ using namespace Pinetime::Drivers; -bool SpiMaster::Init(const SpiMaster::SpiModule spi, const SpiMaster::Parameters ¶ms) { - configSpiModule = spi; - configParams = params; +SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters ¶ms) : + spi{spi}, params{params} { +} +bool SpiMaster::Init() { /* Configure GPIO pins used for pselsck, pselmosi, pselmiso and pselss for SPI0 */ nrf_gpio_cfg_output(params.pinSCK); nrf_gpio_cfg_output(params.pinMOSI); @@ -94,12 +95,12 @@ void SpiMaster::Sleep() { while(NRF_SPI0->ENABLE != 0) { NRF_SPI0->ENABLE = (SPIM_ENABLE_ENABLE_Disabled << SPIM_ENABLE_ENABLE_Pos); } - nrf_gpio_cfg_default(configParams.pinSCK); - nrf_gpio_cfg_default(configParams.pinMOSI); - nrf_gpio_cfg_default(configParams.pinMISO); - nrf_gpio_cfg_default(configParams.pinCSN); + nrf_gpio_cfg_default(params.pinSCK); + nrf_gpio_cfg_default(params.pinMOSI); + nrf_gpio_cfg_default(params.pinMISO); + nrf_gpio_cfg_default(params.pinCSN); } void SpiMaster::Wakeup() { - Init(configSpiModule, configParams); + Init(); } diff --git a/src/drivers/SpiMaster.h b/src/drivers/SpiMaster.h index 4f39dc39..073501a8 100644 --- a/src/drivers/SpiMaster.h +++ b/src/drivers/SpiMaster.h @@ -20,7 +20,9 @@ namespace Pinetime { uint8_t pinMISO; uint8_t pinCSN; }; - bool Init(const SpiModule spi, const Parameters& params); + + SpiMaster(const SpiModule spi, const Parameters& params); + bool Init(); bool Write(const uint8_t* data, size_t size); void Sleep(); @@ -30,8 +32,8 @@ namespace Pinetime { NRF_SPI_Type * spiBaseAddress; uint8_t pinCsn; - SpiMaster::SpiModule configSpiModule; - SpiMaster::Parameters configParams; + SpiMaster::SpiModule spi; + SpiMaster::Parameters params; }; } }