Refactor ctor() and Ini() of SpiMaster.

This commit is contained in:
JF 2020-01-18 20:53:32 +01:00
parent 86d9f6e6c8
commit 2b3e6b1cde
4 changed files with 45 additions and 36 deletions

View file

@ -17,8 +17,16 @@ using namespace Pinetime::Applications;
DisplayApp::DisplayApp(Controllers::Battery &batteryController, DisplayApp::DisplayApp(Controllers::Battery &batteryController,
Controllers::Ble &bleController, Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController) : Controllers::DateTime &dateTimeController) :
spi{}, spi{Drivers::SpiMaster::SpiModule::SPI0, {
lcd{new Drivers::St7789(spi, 18)}, 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()) }, gfx{new Components::Gfx(*lcd.get()) },
batteryController{batteryController}, batteryController{batteryController},
bleController{bleController}, bleController{bleController},
@ -44,26 +52,16 @@ void DisplayApp::Process(void *instance) {
} }
void DisplayApp::InitHw() { void DisplayApp::InitHw() {
nrf_gpio_cfg_output(14); nrf_gpio_cfg_output(pinLcdBacklight1);
nrf_gpio_cfg_output(22); nrf_gpio_cfg_output(pinLcdBacklight2);
nrf_gpio_cfg_output(23); nrf_gpio_cfg_output(pinLcdBacklight3);
nrf_gpio_pin_clear(14); nrf_gpio_pin_clear(pinLcdBacklight1);
nrf_gpio_pin_clear(22); nrf_gpio_pin_clear(pinLcdBacklight2);
nrf_gpio_pin_clear(23); nrf_gpio_pin_clear(pinLcdBacklight3);
Drivers::SpiMaster::Parameters params; spi.Init();
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);
gfx->Init(); gfx->Init();
currentScreen->Refresh(true); currentScreen->Refresh(true);
touchPanel.Init(); touchPanel.Init();
} }
@ -84,11 +82,11 @@ void DisplayApp::Refresh() {
if (xQueueReceive(msgQueue, &msg, queueTimeout)) { if (xQueueReceive(msgQueue, &msg, queueTimeout)) {
switch (msg) { switch (msg) {
case Messages::GoToSleep: case Messages::GoToSleep:
nrf_gpio_pin_set(23); nrf_gpio_pin_set(pinLcdBacklight3);
vTaskDelay(100); vTaskDelay(100);
nrf_gpio_pin_set(22); nrf_gpio_pin_set(pinLcdBacklight2);
vTaskDelay(100); vTaskDelay(100);
nrf_gpio_pin_set(14); nrf_gpio_pin_set(pinLcdBacklight1);
lcd->DisplayOff(); lcd->DisplayOff();
lcd->Sleep(); lcd->Sleep();
touchPanel.Sleep(); touchPanel.Sleep();
@ -99,9 +97,9 @@ void DisplayApp::Refresh() {
touchPanel.Wakeup(); touchPanel.Wakeup();
lcd->DisplayOn(); lcd->DisplayOn();
nrf_gpio_pin_clear(23); nrf_gpio_pin_clear(pinLcdBacklight3);
nrf_gpio_pin_clear(22); nrf_gpio_pin_clear(pinLcdBacklight2);
nrf_gpio_pin_clear(14); nrf_gpio_pin_clear(pinLcdBacklight1);
state = States::Running; state = States::Running;
break; break;
case Messages::UpdateDateTime: case Messages::UpdateDateTime:

View file

@ -59,6 +59,14 @@ namespace Pinetime {
Screens::Screen* currentScreen = nullptr; Screens::Screen* currentScreen = nullptr;
// Screens::Message messageScreen; // Screens::Message messageScreen;
// bool screenState = false; // 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;
}; };
} }
} }

View file

@ -3,10 +3,11 @@
using namespace Pinetime::Drivers; using namespace Pinetime::Drivers;
bool SpiMaster::Init(const SpiMaster::SpiModule spi, const SpiMaster::Parameters &params) { SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters &params) :
configSpiModule = spi; spi{spi}, params{params} {
configParams = params; }
bool SpiMaster::Init() {
/* Configure GPIO pins used for pselsck, pselmosi, pselmiso and pselss for SPI0 */ /* Configure GPIO pins used for pselsck, pselmosi, pselmiso and pselss for SPI0 */
nrf_gpio_cfg_output(params.pinSCK); nrf_gpio_cfg_output(params.pinSCK);
nrf_gpio_cfg_output(params.pinMOSI); nrf_gpio_cfg_output(params.pinMOSI);
@ -94,12 +95,12 @@ void SpiMaster::Sleep() {
while(NRF_SPI0->ENABLE != 0) { while(NRF_SPI0->ENABLE != 0) {
NRF_SPI0->ENABLE = (SPIM_ENABLE_ENABLE_Disabled << SPIM_ENABLE_ENABLE_Pos); NRF_SPI0->ENABLE = (SPIM_ENABLE_ENABLE_Disabled << SPIM_ENABLE_ENABLE_Pos);
} }
nrf_gpio_cfg_default(configParams.pinSCK); nrf_gpio_cfg_default(params.pinSCK);
nrf_gpio_cfg_default(configParams.pinMOSI); nrf_gpio_cfg_default(params.pinMOSI);
nrf_gpio_cfg_default(configParams.pinMISO); nrf_gpio_cfg_default(params.pinMISO);
nrf_gpio_cfg_default(configParams.pinCSN); nrf_gpio_cfg_default(params.pinCSN);
} }
void SpiMaster::Wakeup() { void SpiMaster::Wakeup() {
Init(configSpiModule, configParams); Init();
} }

View file

@ -20,7 +20,9 @@ namespace Pinetime {
uint8_t pinMISO; uint8_t pinMISO;
uint8_t pinCSN; 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); bool Write(const uint8_t* data, size_t size);
void Sleep(); void Sleep();
@ -30,8 +32,8 @@ namespace Pinetime {
NRF_SPI_Type * spiBaseAddress; NRF_SPI_Type * spiBaseAddress;
uint8_t pinCsn; uint8_t pinCsn;
SpiMaster::SpiModule configSpiModule; SpiMaster::SpiModule spi;
SpiMaster::Parameters configParams; SpiMaster::Parameters params;
}; };
} }
} }