2020-07-19 20:30:44 +02:00
|
|
|
#pragma once
|
|
|
|
#include <FreeRTOS.h>
|
|
|
|
#include <semphr.h>
|
2020-11-15 17:45:43 +01:00
|
|
|
#include <drivers/include/nrfx_twi.h> // NRF_TWIM_Type
|
|
|
|
#include <cstdint>
|
2020-07-19 20:30:44 +02:00
|
|
|
|
|
|
|
namespace Pinetime {
|
|
|
|
namespace Drivers {
|
|
|
|
class TwiMaster {
|
2021-04-24 11:00:45 +02:00
|
|
|
public:
|
2021-04-18 19:28:14 +02:00
|
|
|
enum class ErrorCodes { NoError, TransactionFailed };
|
2020-07-19 20:30:44 +02:00
|
|
|
|
2021-08-16 17:26:10 +02:00
|
|
|
TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl);
|
2020-07-19 20:30:44 +02:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
void Init();
|
|
|
|
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 20:30:44 +02:00
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
void Sleep();
|
|
|
|
void Wakeup();
|
2020-08-22 17:59:59 +02:00
|
|
|
|
2021-04-24 11:00:45 +02:00
|
|
|
private:
|
2021-04-18 19:28:14 +02: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();
|
2021-08-16 17:26:10 +02:00
|
|
|
void ConfigurePins() const;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
NRF_TWIM_Type* twiBaseAddress;
|
2021-06-12 10:58:28 +02:00
|
|
|
SemaphoreHandle_t mutex = nullptr;
|
2021-08-16 17:26:10 +02:00
|
|
|
NRF_TWIM_Type* module;
|
|
|
|
uint32_t frequency;
|
|
|
|
uint8_t pinSda;
|
|
|
|
uint8_t pinScl;
|
2021-04-18 19:28:14 +02:00
|
|
|
static constexpr uint8_t maxDataSize {16};
|
|
|
|
static constexpr uint8_t registerSize {1};
|
|
|
|
uint8_t internalBuffer[maxDataSize + registerSize];
|
|
|
|
uint32_t txStartedCycleCount = 0;
|
|
|
|
static constexpr uint32_t HwFreezedDelay {161000};
|
2020-07-19 20:30:44 +02:00
|
|
|
};
|
|
|
|
}
|
2021-08-09 16:45:24 +02:00
|
|
|
}
|