2019-12-07 16:11:50 +00:00
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
2020-11-15 04:04:22 +00:00
|
|
|
#include <cstdint>
|
2020-01-26 12:37:10 +00:00
|
|
|
|
2020-11-15 04:04:22 +00:00
|
|
|
#include <FreeRTOS.h>
|
2020-05-17 07:35:01 +00:00
|
|
|
#include <semphr.h>
|
2020-11-15 04:04:22 +00:00
|
|
|
#include <task.h>
|
2020-05-17 07:35:01 +00:00
|
|
|
|
2019-12-07 16:11:50 +00:00
|
|
|
namespace Pinetime {
|
|
|
|
namespace Drivers {
|
|
|
|
class SpiMaster {
|
2021-04-24 09:00:45 +00:00
|
|
|
public:
|
2021-04-18 17:28:14 +00:00
|
|
|
enum class SpiModule : uint8_t { SPI0, SPI1 };
|
|
|
|
enum class BitOrder : uint8_t { Msb_Lsb, Lsb_Msb };
|
|
|
|
enum class Modes : uint8_t { Mode0, Mode1, Mode2, Mode3 };
|
|
|
|
enum class Frequencies : uint8_t { Freq8Mhz };
|
2023-01-07 20:23:15 +00:00
|
|
|
|
2021-04-18 17:28:14 +00:00
|
|
|
struct Parameters {
|
|
|
|
BitOrder bitOrder;
|
|
|
|
Modes mode;
|
|
|
|
Frequencies Frequency;
|
|
|
|
uint8_t pinSCK;
|
|
|
|
uint8_t pinMOSI;
|
|
|
|
uint8_t pinMISO;
|
|
|
|
};
|
|
|
|
|
|
|
|
SpiMaster(const SpiModule spi, const Parameters& params);
|
|
|
|
SpiMaster(const SpiMaster&) = delete;
|
|
|
|
SpiMaster& operator=(const SpiMaster&) = delete;
|
|
|
|
SpiMaster(SpiMaster&&) = delete;
|
|
|
|
SpiMaster& operator=(SpiMaster&&) = delete;
|
|
|
|
|
|
|
|
bool Init();
|
|
|
|
bool Write(uint8_t pinCsn, const uint8_t* data, size_t size);
|
|
|
|
bool Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t* data, size_t dataSize);
|
|
|
|
|
|
|
|
bool WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmdSize, const uint8_t* data, size_t dataSize);
|
|
|
|
|
|
|
|
void OnStartedEvent();
|
|
|
|
void OnEndEvent();
|
|
|
|
|
|
|
|
void Sleep();
|
|
|
|
void Wakeup();
|
|
|
|
|
2021-04-24 09:00:45 +00:00
|
|
|
private:
|
2021-04-18 17:28:14 +00:00
|
|
|
void SetupWorkaroundForFtpan58(NRF_SPIM_Type* spim, uint32_t ppi_channel, uint32_t gpiote_channel);
|
|
|
|
void DisableWorkaroundForFtpan58(NRF_SPIM_Type* spim, uint32_t ppi_channel, uint32_t gpiote_channel);
|
|
|
|
void PrepareTx(const volatile uint32_t bufferAddress, const volatile size_t size);
|
2022-12-30 21:31:31 +00:00
|
|
|
void PrepareRx(const volatile uint32_t bufferAddress, const volatile size_t size);
|
2021-04-18 17:28:14 +00:00
|
|
|
|
|
|
|
NRF_SPIM_Type* spiBaseAddress;
|
|
|
|
uint8_t pinCsn;
|
|
|
|
|
|
|
|
SpiMaster::SpiModule spi;
|
|
|
|
SpiMaster::Parameters params;
|
|
|
|
|
|
|
|
volatile uint32_t currentBufferAddr = 0;
|
|
|
|
volatile size_t currentBufferSize = 0;
|
|
|
|
volatile TaskHandle_t taskToNotify;
|
2021-06-12 08:58:28 +00:00
|
|
|
SemaphoreHandle_t mutex = nullptr;
|
2019-12-07 16:11:50 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|