2020-02-08 17:01:02 +00:00
|
|
|
#include <FreeRTOS.h>
|
2019-12-07 16:11:50 +00:00
|
|
|
#include <hal/nrf_gpio.h>
|
2020-01-19 18:47:49 +00:00
|
|
|
#include <hal/nrf_spim.h>
|
2019-12-07 16:11:50 +00:00
|
|
|
#include "SpiMaster.h"
|
2020-01-19 18:47:49 +00:00
|
|
|
#include <algorithm>
|
2020-02-08 17:01:02 +00:00
|
|
|
#include <task.h>
|
2020-08-22 15:59:59 +00:00
|
|
|
#include <nrfx_log.h>
|
2020-02-08 17:01:02 +00:00
|
|
|
|
2019-12-07 16:11:50 +00:00
|
|
|
using namespace Pinetime::Drivers;
|
|
|
|
|
2020-01-18 19:53:32 +00:00
|
|
|
SpiMaster::SpiMaster(const SpiMaster::SpiModule spi, const SpiMaster::Parameters ¶ms) :
|
|
|
|
spi{spi}, params{params} {
|
2020-05-11 16:50:37 +00:00
|
|
|
mutex = xSemaphoreCreateBinary();
|
|
|
|
ASSERT(mutex != NULL);
|
2020-01-18 19:53:32 +00:00
|
|
|
}
|
2020-01-17 21:16:45 +00:00
|
|
|
|
2020-01-18 19:53:32 +00:00
|
|
|
bool SpiMaster::Init() {
|
2019-12-07 16:11:50 +00:00
|
|
|
/* Configure GPIO pins used for pselsck, pselmosi, pselmiso and pselss for SPI0 */
|
2020-01-19 18:47:49 +00:00
|
|
|
nrf_gpio_pin_set(params.pinSCK);
|
2019-12-07 16:11:50 +00:00
|
|
|
nrf_gpio_cfg_output(params.pinSCK);
|
2020-01-19 18:47:49 +00:00
|
|
|
nrf_gpio_pin_clear(params.pinMOSI);
|
2019-12-07 16:11:50 +00:00
|
|
|
nrf_gpio_cfg_output(params.pinMOSI);
|
|
|
|
nrf_gpio_cfg_input(params.pinMISO, NRF_GPIO_PIN_NOPULL);
|
2020-05-07 17:53:51 +00:00
|
|
|
// nrf_gpio_cfg_output(params.pinCSN);
|
|
|
|
// pinCsn = params.pinCSN;
|
2019-12-07 16:11:50 +00:00
|
|
|
|
|
|
|
switch(spi) {
|
2020-01-19 18:47:49 +00:00
|
|
|
case SpiModule::SPI0: spiBaseAddress = NRF_SPIM0; break;
|
|
|
|
case SpiModule::SPI1: spiBaseAddress = NRF_SPIM1; break;
|
2019-12-07 16:11:50 +00:00
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Configure pins, frequency and mode */
|
2020-01-26 14:35:18 +00:00
|
|
|
spiBaseAddress->PSELSCK = params.pinSCK;
|
|
|
|
spiBaseAddress->PSELMOSI = params.pinMOSI;
|
|
|
|
spiBaseAddress->PSELMISO = params.pinMISO;
|
2019-12-07 16:11:50 +00:00
|
|
|
|
|
|
|
uint32_t frequency;
|
|
|
|
switch(params.Frequency) {
|
|
|
|
case Frequencies::Freq8Mhz: frequency = 0x80000000; break;
|
|
|
|
default: return false;
|
|
|
|
}
|
2020-01-26 14:35:18 +00:00
|
|
|
spiBaseAddress->FREQUENCY = frequency;
|
2019-12-07 16:11:50 +00:00
|
|
|
|
|
|
|
uint32_t regConfig = 0;
|
|
|
|
switch(params.bitOrder) {
|
|
|
|
case BitOrder::Msb_Lsb: break;
|
|
|
|
case BitOrder::Lsb_Msb: regConfig = 1;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
switch(params.mode) {
|
|
|
|
case Modes::Mode0: break;
|
|
|
|
case Modes::Mode1: regConfig |= (0x01 << 1); break;
|
|
|
|
case Modes::Mode2: regConfig |= (0x02 << 1); break;
|
|
|
|
case Modes::Mode3: regConfig |= (0x03 << 1); break;
|
|
|
|
default: return false;
|
|
|
|
}
|
|
|
|
|
2020-01-26 14:35:18 +00:00
|
|
|
spiBaseAddress->CONFIG = regConfig;
|
|
|
|
spiBaseAddress->EVENTS_ENDRX = 0;
|
|
|
|
spiBaseAddress->EVENTS_ENDTX = 0;
|
|
|
|
spiBaseAddress->EVENTS_END = 0;
|
2020-01-19 18:47:49 +00:00
|
|
|
|
2020-01-26 14:35:18 +00:00
|
|
|
spiBaseAddress->INTENSET = ((unsigned)1 << (unsigned)6);
|
|
|
|
spiBaseAddress->INTENSET = ((unsigned)1 << (unsigned)1);
|
|
|
|
spiBaseAddress->INTENSET = ((unsigned)1 << (unsigned)19);
|
2019-12-07 16:11:50 +00:00
|
|
|
|
2020-01-26 14:35:18 +00:00
|
|
|
spiBaseAddress->ENABLE = (SPIM_ENABLE_ENABLE_Enabled << SPIM_ENABLE_ENABLE_Pos);
|
2019-12-07 16:11:50 +00:00
|
|
|
|
2020-01-26 14:35:18 +00:00
|
|
|
NRFX_IRQ_PRIORITY_SET(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn,2);
|
2020-01-22 18:45:53 +00:00
|
|
|
NRFX_IRQ_ENABLE(SPIM0_SPIS0_TWIM0_TWIS0_SPI0_TWI0_IRQn);
|
2020-05-11 16:50:37 +00:00
|
|
|
|
|
|
|
xSemaphoreGive(mutex);
|
2019-12-07 16:11:50 +00:00
|
|
|
return true;
|
|
|
|
}
|
2020-01-17 21:16:45 +00:00
|
|
|
|
2020-01-22 18:45:53 +00:00
|
|
|
|
2020-01-26 14:35:18 +00:00
|
|
|
void SpiMaster::SetupWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel) {
|
2020-01-19 18:47:49 +00:00
|
|
|
// Create an event when SCK toggles.
|
|
|
|
NRF_GPIOTE->CONFIG[gpiote_channel] = (GPIOTE_CONFIG_MODE_Event << GPIOTE_CONFIG_MODE_Pos) |
|
|
|
|
(spim->PSEL.SCK << GPIOTE_CONFIG_PSEL_Pos) |
|
|
|
|
(GPIOTE_CONFIG_POLARITY_Toggle << GPIOTE_CONFIG_POLARITY_Pos);
|
|
|
|
|
|
|
|
// Stop the spim instance when SCK toggles.
|
|
|
|
NRF_PPI->CH[ppi_channel].EEP = (uint32_t) &NRF_GPIOTE->EVENTS_IN[gpiote_channel];
|
|
|
|
NRF_PPI->CH[ppi_channel].TEP = (uint32_t) &spim->TASKS_STOP;
|
|
|
|
NRF_PPI->CHENSET = 1U << ppi_channel;
|
2020-05-17 07:35:01 +00:00
|
|
|
spiBaseAddress->EVENTS_END = 0;
|
2020-01-26 14:35:18 +00:00
|
|
|
|
|
|
|
// Disable IRQ
|
|
|
|
spim->INTENCLR = (1<<6);
|
|
|
|
spim->INTENCLR = (1<<1);
|
|
|
|
spim->INTENCLR = (1<<19);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SpiMaster::DisableWorkaroundForFtpan58(NRF_SPIM_Type *spim, uint32_t ppi_channel, uint32_t gpiote_channel) {
|
|
|
|
NRF_GPIOTE->CONFIG[gpiote_channel] = 0;
|
|
|
|
NRF_PPI->CH[ppi_channel].EEP = 0;
|
|
|
|
NRF_PPI->CH[ppi_channel].TEP = 0;
|
|
|
|
NRF_PPI->CHENSET = ppi_channel;
|
2020-05-11 16:50:37 +00:00
|
|
|
spiBaseAddress->EVENTS_END = 0;
|
2020-01-26 14:35:18 +00:00
|
|
|
spim->INTENSET = (1<<6);
|
|
|
|
spim->INTENSET = (1<<1);
|
|
|
|
spim->INTENSET = (1<<19);
|
2020-01-19 18:47:49 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 17:01:02 +00:00
|
|
|
void SpiMaster::OnEndEvent() {
|
2020-05-11 16:50:37 +00:00
|
|
|
if(currentBufferAddr == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-01-26 12:37:10 +00:00
|
|
|
|
|
|
|
auto s = currentBufferSize;
|
|
|
|
if(s > 0) {
|
|
|
|
auto currentSize = std::min((size_t) 255, s);
|
2020-01-26 14:35:18 +00:00
|
|
|
PrepareTx(currentBufferAddr, currentSize);
|
2020-01-26 12:37:10 +00:00
|
|
|
currentBufferAddr += currentSize;
|
|
|
|
currentBufferSize -= currentSize;
|
|
|
|
|
2020-01-26 14:35:18 +00:00
|
|
|
spiBaseAddress->TASKS_START = 1;
|
2020-01-26 12:37:10 +00:00
|
|
|
} else {
|
2020-05-17 07:35:01 +00:00
|
|
|
if(taskToNotify != nullptr) {
|
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
vTaskNotifyGiveFromISR(taskToNotify, &xHigherPriorityTaskWoken);
|
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
|
|
|
}
|
|
|
|
|
|
|
|
nrf_gpio_pin_set(this->pinCsn);
|
|
|
|
currentBufferAddr = 0;
|
2020-02-08 17:01:02 +00:00
|
|
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
2020-05-17 07:35:01 +00:00
|
|
|
xSemaphoreGiveFromISR(mutex, &xHigherPriorityTaskWoken);
|
2020-02-08 17:01:02 +00:00
|
|
|
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
|
2020-01-22 18:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 17:01:02 +00:00
|
|
|
void SpiMaster::OnStartedEvent() {
|
2020-01-26 12:37:10 +00:00
|
|
|
}
|
2020-01-22 20:08:53 +00:00
|
|
|
|
2020-01-26 14:35:18 +00:00
|
|
|
void SpiMaster::PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size) {
|
|
|
|
spiBaseAddress->TXD.PTR = bufferAddress;
|
|
|
|
spiBaseAddress->TXD.MAXCNT = size;
|
|
|
|
spiBaseAddress->TXD.LIST = 0;
|
|
|
|
spiBaseAddress->RXD.PTR = 0;
|
|
|
|
spiBaseAddress->RXD.MAXCNT = 0;
|
|
|
|
spiBaseAddress->RXD.LIST = 0;
|
|
|
|
spiBaseAddress->EVENTS_END = 0;
|
|
|
|
}
|
|
|
|
|
2020-05-11 16:50:37 +00:00
|
|
|
void SpiMaster::PrepareRx(const volatile uint32_t cmdAddress, const volatile size_t cmdSize, const volatile uint32_t bufferAddress, const volatile size_t size) {
|
2020-05-07 17:53:51 +00:00
|
|
|
spiBaseAddress->TXD.PTR = 0;
|
|
|
|
spiBaseAddress->TXD.MAXCNT = 0;
|
|
|
|
spiBaseAddress->TXD.LIST = 0;
|
|
|
|
spiBaseAddress->RXD.PTR = bufferAddress;
|
|
|
|
spiBaseAddress->RXD.MAXCNT = size;
|
|
|
|
spiBaseAddress->RXD.LIST = 0;
|
|
|
|
spiBaseAddress->EVENTS_END = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool SpiMaster::Write(uint8_t pinCsn, const uint8_t *data, size_t size) {
|
2020-01-19 18:47:49 +00:00
|
|
|
if(data == nullptr) return false;
|
2020-05-11 16:50:37 +00:00
|
|
|
auto ok = xSemaphoreTake(mutex, portMAX_DELAY);
|
|
|
|
ASSERT(ok == true);
|
2020-02-08 17:01:02 +00:00
|
|
|
taskToNotify = xTaskGetCurrentTaskHandle();
|
2020-05-11 16:50:37 +00:00
|
|
|
|
2020-01-22 18:45:53 +00:00
|
|
|
|
2020-05-07 17:53:51 +00:00
|
|
|
this->pinCsn = pinCsn;
|
|
|
|
|
2020-01-19 18:47:49 +00:00
|
|
|
if(size == 1) {
|
2020-01-26 14:35:18 +00:00
|
|
|
SetupWorkaroundForFtpan58(spiBaseAddress, 0,0);
|
2020-01-19 18:47:49 +00:00
|
|
|
} else {
|
2020-01-26 14:35:18 +00:00
|
|
|
DisableWorkaroundForFtpan58(spiBaseAddress, 0, 0);
|
2020-01-19 18:47:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 17:53:51 +00:00
|
|
|
nrf_gpio_pin_clear(this->pinCsn);
|
2020-01-19 18:47:49 +00:00
|
|
|
|
2020-01-26 12:37:10 +00:00
|
|
|
currentBufferAddr = (uint32_t)data;
|
|
|
|
currentBufferSize = size;
|
2020-01-22 18:45:53 +00:00
|
|
|
|
2020-01-26 12:37:10 +00:00
|
|
|
auto currentSize = std::min((size_t)255, (size_t)currentBufferSize);
|
2020-01-26 14:35:18 +00:00
|
|
|
PrepareTx(currentBufferAddr, currentSize);
|
2020-01-22 20:08:53 +00:00
|
|
|
currentBufferSize -= currentSize;
|
|
|
|
currentBufferAddr += currentSize;
|
2020-01-26 14:35:18 +00:00
|
|
|
spiBaseAddress->TASKS_START = 1;
|
2020-01-19 18:47:49 +00:00
|
|
|
|
2020-01-22 20:08:53 +00:00
|
|
|
if(size == 1) {
|
2020-01-26 14:35:18 +00:00
|
|
|
while (spiBaseAddress->EVENTS_END == 0);
|
2020-05-24 15:46:43 +00:00
|
|
|
nrf_gpio_pin_set(this->pinCsn);
|
|
|
|
currentBufferAddr = 0;
|
2020-05-11 16:50:37 +00:00
|
|
|
xSemaphoreGive(mutex);
|
2020-01-22 20:08:53 +00:00
|
|
|
}
|
2020-01-19 18:47:49 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-05-11 16:50:37 +00:00
|
|
|
bool SpiMaster::Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize) {
|
2020-05-24 14:51:50 +00:00
|
|
|
xSemaphoreTake(mutex, portMAX_DELAY);
|
2020-05-11 16:50:37 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
taskToNotify = nullptr;
|
2020-05-07 17:53:51 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
this->pinCsn = pinCsn;
|
|
|
|
DisableWorkaroundForFtpan58(spiBaseAddress, 0,0);
|
|
|
|
spiBaseAddress->INTENCLR = (1<<6);
|
|
|
|
spiBaseAddress->INTENCLR = (1<<1);
|
|
|
|
spiBaseAddress->INTENCLR = (1<<19);
|
2020-05-07 17:53:51 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
nrf_gpio_pin_clear(this->pinCsn);
|
2020-05-07 17:53:51 +00:00
|
|
|
|
2020-05-11 16:50:37 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
currentBufferAddr = 0;
|
|
|
|
currentBufferSize = 0;
|
2020-05-07 17:53:51 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
PrepareTx((uint32_t)cmd, cmdSize);
|
|
|
|
spiBaseAddress->TASKS_START = 1;
|
|
|
|
while (spiBaseAddress->EVENTS_END == 0);
|
2020-05-11 16:50:37 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
PrepareRx((uint32_t)cmd, cmdSize, (uint32_t)data, dataSize);
|
|
|
|
spiBaseAddress->TASKS_START = 1;
|
2020-05-07 17:53:51 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
while (spiBaseAddress->EVENTS_END == 0);
|
|
|
|
nrf_gpio_pin_set(this->pinCsn);
|
2020-05-07 17:53:51 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
xSemaphoreGive(mutex);
|
2020-05-07 17:53:51 +00:00
|
|
|
|
2020-05-24 14:51:50 +00:00
|
|
|
return true;
|
2020-05-07 17:53:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-17 21:16:45 +00:00
|
|
|
void SpiMaster::Sleep() {
|
2020-01-26 14:35:18 +00:00
|
|
|
while(spiBaseAddress->ENABLE != 0) {
|
|
|
|
spiBaseAddress->ENABLE = (SPIM_ENABLE_ENABLE_Disabled << SPIM_ENABLE_ENABLE_Pos);
|
2020-01-17 21:16:45 +00:00
|
|
|
}
|
2020-01-18 19:53:32 +00:00
|
|
|
nrf_gpio_cfg_default(params.pinSCK);
|
|
|
|
nrf_gpio_cfg_default(params.pinMOSI);
|
|
|
|
nrf_gpio_cfg_default(params.pinMISO);
|
2020-08-22 15:59:59 +00:00
|
|
|
|
|
|
|
NRF_LOG_INFO("[SPIMASTER] sleep")
|
2020-01-17 21:16:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpiMaster::Wakeup() {
|
2020-01-18 19:53:32 +00:00
|
|
|
Init();
|
2020-08-22 15:59:59 +00:00
|
|
|
NRF_LOG_INFO("[SPIMASTER] Wakeup");
|
2020-01-17 21:16:45 +00:00
|
|
|
}
|
2020-01-22 20:08:53 +00:00
|
|
|
|
2020-06-01 16:32:46 +00:00
|
|
|
bool SpiMaster::WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t *cmd, size_t cmdSize, const uint8_t *data, size_t dataSize) {
|
2020-05-11 16:50:37 +00:00
|
|
|
xSemaphoreTake(mutex, portMAX_DELAY);
|
|
|
|
|
|
|
|
taskToNotify = nullptr;
|
|
|
|
|
|
|
|
this->pinCsn = pinCsn;
|
|
|
|
DisableWorkaroundForFtpan58(spiBaseAddress, 0,0);
|
|
|
|
spiBaseAddress->INTENCLR = (1<<6);
|
|
|
|
spiBaseAddress->INTENCLR = (1<<1);
|
|
|
|
spiBaseAddress->INTENCLR = (1<<19);
|
|
|
|
|
|
|
|
nrf_gpio_pin_clear(this->pinCsn);
|
|
|
|
|
|
|
|
|
|
|
|
currentBufferAddr = 0;
|
|
|
|
currentBufferSize = 0;
|
|
|
|
|
|
|
|
PrepareTx((uint32_t)cmd, cmdSize);
|
|
|
|
spiBaseAddress->TASKS_START = 1;
|
|
|
|
while (spiBaseAddress->EVENTS_END == 0);
|
|
|
|
|
|
|
|
PrepareTx((uint32_t)data, dataSize);
|
|
|
|
spiBaseAddress->TASKS_START = 1;
|
|
|
|
|
|
|
|
while (spiBaseAddress->EVENTS_END == 0);
|
|
|
|
nrf_gpio_pin_set(this->pinCsn);
|
|
|
|
|
|
|
|
xSemaphoreGive(mutex);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-26 12:37:10 +00:00
|
|
|
|
2020-05-07 17:53:51 +00:00
|
|
|
|