InfiniTime/src/drivers/Spi.cpp

53 lines
1.1 KiB
C++
Raw Normal View History

#include "drivers/Spi.h"
2020-05-07 13:53:51 -04:00
#include <hal/nrf_gpio.h>
#include <nrfx_log.h>
2020-05-07 13:53:51 -04:00
using namespace Pinetime::Drivers;
Spi::Spi(SpiMaster& spiMaster, uint8_t pinCsn) : spiMaster {spiMaster}, pinCsn {pinCsn} {
nrf_gpio_cfg_output(pinCsn);
nrf_gpio_pin_set(pinCsn);
2020-05-07 13:53:51 -04:00
}
bool Spi::Write(const uint8_t* data, size_t size) {
2021-10-18 00:20:11 -04:00
if(!active){
Wakeup();
}
2020-05-07 13:53:51 -04:00
return spiMaster.Write(pinCsn, data, size);
}
bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize) {
2021-10-18 00:20:11 -04:00
if(!active){
Wakeup();
}
return spiMaster.Read(pinCsn, cmd, cmdSize, data, dataSize);
2020-05-07 13:53:51 -04:00
}
void Spi::Sleep() {
2021-10-18 00:20:11 -04:00
if(!active){
return;
}
active = false;
2020-05-07 13:53:51 -04:00
nrf_gpio_cfg_default(pinCsn);
NRF_LOG_INFO("[SPI] Sleep")
}
bool Spi::WriteCmdAndBuffer(const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize) {
return spiMaster.WriteCmdAndBuffer(pinCsn, cmd, cmdSize, data, dataSize);
2020-05-07 13:53:51 -04:00
}
bool Spi::Init() {
nrf_gpio_pin_set(pinCsn); /* disable Set slave select (inactive high) */
return true;
}
void Spi::Wakeup() {
2021-10-18 00:20:11 -04:00
if(active){
return;
}
nrf_gpio_cfg_output(pinCsn);
nrf_gpio_pin_set(pinCsn);
2021-10-18 00:20:11 -04:00
active=true;
NRF_LOG_INFO("[SPI] Wakeup")
}