2019-12-07 11:11:50 -05:00
|
|
|
#pragma once
|
|
|
|
#include <cstddef>
|
2020-11-14 23:04:22 -05:00
|
|
|
#include <cstdint>
|
2020-01-26 07:37:10 -05:00
|
|
|
|
2020-11-14 23:04:22 -05:00
|
|
|
#include <FreeRTOS.h>
|
2020-05-17 03:35:01 -04:00
|
|
|
#include <semphr.h>
|
2020-11-14 23:04:22 -05:00
|
|
|
#include <task.h>
|
2020-05-17 03:35:01 -04:00
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
namespace Pinetime {
|
|
|
|
namespace Drivers {
|
|
|
|
class SpiMaster {
|
|
|
|
public:;
|
|
|
|
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};
|
|
|
|
struct Parameters {
|
|
|
|
BitOrder bitOrder;
|
|
|
|
Modes mode;
|
|
|
|
Frequencies Frequency;
|
|
|
|
uint8_t pinSCK;
|
|
|
|
uint8_t pinMOSI;
|
|
|
|
uint8_t pinMISO;
|
|
|
|
};
|
2020-01-18 14:53:32 -05:00
|
|
|
|
|
|
|
SpiMaster(const SpiModule spi, const Parameters& params);
|
2020-03-08 16:38:11 -04:00
|
|
|
SpiMaster(const SpiMaster&) = delete;
|
|
|
|
SpiMaster& operator=(const SpiMaster&) = delete;
|
|
|
|
SpiMaster(SpiMaster&&) = delete;
|
|
|
|
SpiMaster& operator=(SpiMaster&&) = delete;
|
|
|
|
|
2020-01-18 14:53:32 -05:00
|
|
|
bool Init();
|
2020-05-07 13:53:51 -04:00
|
|
|
bool Write(uint8_t pinCsn, const uint8_t* data, size_t size);
|
2020-05-11 12:50:37 -04:00
|
|
|
bool Read(uint8_t pinCsn, uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize);
|
|
|
|
|
2020-06-01 12:32:46 -04:00
|
|
|
bool WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t* cmd, size_t cmdSize, const uint8_t *data, size_t dataSize);
|
2020-01-26 07:37:10 -05:00
|
|
|
|
2020-02-08 12:01:02 -05:00
|
|
|
void OnStartedEvent();
|
|
|
|
void OnEndEvent();
|
2019-12-07 11:11:50 -05:00
|
|
|
|
2020-01-17 16:16:45 -05:00
|
|
|
void Sleep();
|
|
|
|
void Wakeup();
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
private:
|
2020-01-26 09:35:18 -05: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);
|
2020-05-11 12:50:37 -04:00
|
|
|
void PrepareRx(const volatile uint32_t cmdAddress, const volatile size_t cmdSize, const volatile uint32_t bufferAddress, const volatile size_t size);
|
2020-01-26 07:37:10 -05:00
|
|
|
|
2020-01-19 13:47:49 -05:00
|
|
|
NRF_SPIM_Type * spiBaseAddress;
|
2019-12-07 11:11:50 -05:00
|
|
|
uint8_t pinCsn;
|
2020-01-17 16:16:45 -05:00
|
|
|
|
2020-01-18 14:53:32 -05:00
|
|
|
SpiMaster::SpiModule spi;
|
|
|
|
SpiMaster::Parameters params;
|
2020-01-22 13:45:53 -05:00
|
|
|
|
2020-01-22 15:08:53 -05:00
|
|
|
volatile uint32_t currentBufferAddr = 0;
|
|
|
|
volatile size_t currentBufferSize = 0;
|
2020-02-08 12:01:02 -05:00
|
|
|
volatile TaskHandle_t taskToNotify;
|
2020-05-11 12:50:37 -04:00
|
|
|
SemaphoreHandle_t mutex;
|
2019-12-07 11:11:50 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|