2020-07-19 14:30:44 -04:00
|
|
|
#pragma once
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <semphr.h>
|
2020-11-15 11:45:43 -05:00
|
|
|
#include <drivers/include/nrfx_twi.h> // NRF_TWIM_Type
|
|
|
|
#include <cstdint>
|
2020-07-19 14:30:44 -04:00
|
|
|
|
|
|
|
namespace Pinetime {
|
|
|
|
namespace Drivers {
|
|
|
|
class TwiMaster {
|
|
|
|
public:
|
|
|
|
enum class Modules { TWIM1 };
|
2021-04-10 14:09:33 -04:00
|
|
|
enum class Frequencies {Khz100, Khz250, Khz400};
|
2020-10-23 16:25:37 -04:00
|
|
|
enum class ErrorCodes {NoError, TransactionFailed};
|
2020-07-19 14:30:44 -04:00
|
|
|
struct Parameters {
|
|
|
|
uint32_t frequency;
|
|
|
|
uint8_t pinSda;
|
|
|
|
uint8_t pinScl;
|
|
|
|
};
|
|
|
|
|
|
|
|
TwiMaster(const Modules module, const Parameters& params);
|
|
|
|
|
|
|
|
void Init();
|
2020-10-23 16:25:37 -04:00
|
|
|
ErrorCodes Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size);
|
|
|
|
ErrorCodes Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size);
|
2020-07-19 14:30:44 -04:00
|
|
|
|
2020-08-22 11:59:59 -04:00
|
|
|
void Sleep();
|
|
|
|
void Wakeup();
|
|
|
|
|
2020-07-19 14:30:44 -04:00
|
|
|
private:
|
2021-04-10 14:09:33 -04:00
|
|
|
|
|
|
|
ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop);
|
|
|
|
ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop);
|
|
|
|
void FixHwFreezed();
|
|
|
|
NRF_TWIM_Type* twiBaseAddress;
|
|
|
|
SemaphoreHandle_t mutex;
|
2020-07-19 14:30:44 -04:00
|
|
|
const Modules module;
|
|
|
|
const Parameters params;
|
2021-04-10 14:09:33 -04:00
|
|
|
static constexpr uint8_t maxDataSize{16};
|
2020-07-19 14:30:44 -04:00
|
|
|
static constexpr uint8_t registerSize{1};
|
|
|
|
uint8_t internalBuffer[maxDataSize + registerSize];
|
2021-04-10 14:09:33 -04:00
|
|
|
uint32_t txStartedCycleCount = 0;
|
|
|
|
static constexpr uint32_t HwFreezedDelay{161000};
|
2020-07-19 14:30:44 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|