2020-07-19 20:30:44 +02:00
|
|
|
#include "TwiMaster.h"
|
2020-11-15 05:04:22 +01:00
|
|
|
#include <cstring>
|
|
|
|
#include <hal/nrf_gpio.h>
|
|
|
|
#include <nrfx_log.h>
|
2021-04-10 20:09:33 +02:00
|
|
|
|
2020-07-19 20:30:44 +02:00
|
|
|
using namespace Pinetime::Drivers;
|
|
|
|
|
|
|
|
// TODO use shortcut to automatically send STOP when receive LastTX, for example
|
|
|
|
// TODO use DMA/IRQ
|
|
|
|
|
2021-08-16 17:26:10 +02:00
|
|
|
TwiMaster::TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl)
|
|
|
|
: module {module}, frequency {frequency}, pinSda {pinSda}, pinScl {pinScl} {
|
2021-04-10 20:09:33 +02:00
|
|
|
}
|
|
|
|
|
2021-08-09 16:45:24 +02:00
|
|
|
void TwiMaster::ConfigurePins() const {
|
2021-08-16 17:26:10 +02:00
|
|
|
NRF_GPIO->PIN_CNF[pinScl] =
|
2021-08-09 16:45:24 +02:00
|
|
|
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
|
|
|
|
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
|
2021-08-10 10:52:20 +02:00
|
|
|
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
|
2021-08-09 16:45:24 +02:00
|
|
|
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
|
|
|
|
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-08-16 17:26:10 +02:00
|
|
|
NRF_GPIO->PIN_CNF[pinSda] =
|
2021-08-09 16:45:24 +02:00
|
|
|
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
|
|
|
|
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
|
2021-08-10 10:52:20 +02:00
|
|
|
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
|
2021-08-09 16:45:24 +02:00
|
|
|
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
|
|
|
|
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwiMaster::Init() {
|
2021-08-18 20:40:27 +02:00
|
|
|
if (mutex == nullptr) {
|
|
|
|
mutex = xSemaphoreCreateBinary();
|
|
|
|
}
|
|
|
|
|
2021-08-09 16:45:24 +02:00
|
|
|
ConfigurePins();
|
2021-04-18 19:28:14 +02:00
|
|
|
|
2021-08-16 17:26:10 +02:00
|
|
|
twiBaseAddress = module;
|
2020-07-19 20:30:44 +02:00
|
|
|
|
2021-08-16 17:26:10 +02:00
|
|
|
twiBaseAddress->FREQUENCY = frequency;
|
2021-04-10 20:09:33 +02:00
|
|
|
|
2021-08-16 17:26:10 +02:00
|
|
|
twiBaseAddress->PSEL.SCL = pinScl;
|
|
|
|
twiBaseAddress->PSEL.SDA = pinSda;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_LASTRX = 0;
|
|
|
|
twiBaseAddress->EVENTS_STOPPED = 0;
|
|
|
|
twiBaseAddress->EVENTS_LASTTX = 0;
|
|
|
|
twiBaseAddress->EVENTS_ERROR = 0;
|
|
|
|
twiBaseAddress->EVENTS_RXSTARTED = 0;
|
|
|
|
twiBaseAddress->EVENTS_SUSPENDED = 0;
|
|
|
|
twiBaseAddress->EVENTS_TXSTARTED = 0;
|
|
|
|
|
|
|
|
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
|
|
|
|
|
2020-07-19 20:30:44 +02:00
|
|
|
xSemaphoreGive(mutex);
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
TwiMaster::ErrorCodes TwiMaster::Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* data, size_t size) {
|
2020-07-19 20:30:44 +02:00
|
|
|
xSemaphoreTake(mutex, portMAX_DELAY);
|
2021-08-10 10:26:43 +02:00
|
|
|
Wakeup();
|
2021-04-10 20:09:33 +02:00
|
|
|
auto ret = Write(deviceAddress, ®isterAddress, 1, false);
|
|
|
|
ret = Read(deviceAddress, data, size, true);
|
2021-08-10 10:26:43 +02:00
|
|
|
Sleep();
|
2021-04-10 20:09:33 +02:00
|
|
|
xSemaphoreGive(mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
TwiMaster::ErrorCodes TwiMaster::Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size) {
|
2021-04-10 20:09:33 +02:00
|
|
|
ASSERT(size <= maxDataSize);
|
|
|
|
xSemaphoreTake(mutex, portMAX_DELAY);
|
2021-08-10 10:26:43 +02:00
|
|
|
Wakeup();
|
2021-04-10 20:09:33 +02:00
|
|
|
internalBuffer[0] = registerAddress;
|
|
|
|
std::memcpy(internalBuffer + 1, data, size);
|
|
|
|
auto ret = Write(deviceAddress, internalBuffer, size + 1, true);
|
2021-08-10 10:26:43 +02:00
|
|
|
Sleep();
|
2021-04-10 20:09:33 +02:00
|
|
|
xSemaphoreGive(mutex);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
TwiMaster::ErrorCodes TwiMaster::Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop) {
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->ADDRESS = deviceAddress;
|
|
|
|
twiBaseAddress->TASKS_RESUME = 0x1UL;
|
2021-04-18 19:28:14 +02:00
|
|
|
twiBaseAddress->RXD.PTR = (uint32_t) buffer;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->RXD.MAXCNT = size;
|
2021-04-08 20:07:24 +02:00
|
|
|
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->TASKS_STARTRX = 1;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
while (!twiBaseAddress->EVENTS_RXSTARTED && !twiBaseAddress->EVENTS_ERROR)
|
|
|
|
;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_RXSTARTED = 0x0UL;
|
|
|
|
|
|
|
|
txStartedCycleCount = DWT->CYCCNT;
|
|
|
|
uint32_t currentCycleCount;
|
2021-04-18 19:28:14 +02:00
|
|
|
while (!twiBaseAddress->EVENTS_LASTRX && !twiBaseAddress->EVENTS_ERROR) {
|
2021-04-10 20:09:33 +02:00
|
|
|
currentCycleCount = DWT->CYCCNT;
|
2021-04-18 19:28:14 +02:00
|
|
|
if ((currentCycleCount - txStartedCycleCount) > HwFreezedDelay) {
|
2021-04-10 20:09:33 +02:00
|
|
|
FixHwFreezed();
|
|
|
|
return ErrorCodes::TransactionFailed;
|
|
|
|
}
|
2021-04-08 20:07:24 +02:00
|
|
|
}
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_LASTRX = 0x0UL;
|
2021-04-08 20:07:24 +02:00
|
|
|
|
2021-04-10 20:09:33 +02:00
|
|
|
if (stop || twiBaseAddress->EVENTS_ERROR) {
|
|
|
|
twiBaseAddress->TASKS_STOP = 0x1UL;
|
2021-04-18 19:28:14 +02:00
|
|
|
while (!twiBaseAddress->EVENTS_STOPPED)
|
|
|
|
;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_STOPPED = 0x0UL;
|
2021-04-18 19:28:14 +02:00
|
|
|
} else {
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->TASKS_SUSPEND = 0x1UL;
|
2021-04-18 19:28:14 +02:00
|
|
|
while (!twiBaseAddress->EVENTS_SUSPENDED)
|
|
|
|
;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_SUSPENDED = 0x0UL;
|
2021-04-08 20:07:24 +02:00
|
|
|
}
|
2020-10-23 22:25:37 +02:00
|
|
|
|
2021-04-10 20:09:33 +02:00
|
|
|
if (twiBaseAddress->EVENTS_ERROR) {
|
|
|
|
twiBaseAddress->EVENTS_ERROR = 0x0UL;
|
|
|
|
}
|
|
|
|
return ErrorCodes::NoError;
|
2020-07-19 20:30:44 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
TwiMaster::ErrorCodes TwiMaster::Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop) {
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->ADDRESS = deviceAddress;
|
|
|
|
twiBaseAddress->TASKS_RESUME = 0x1UL;
|
2021-04-18 19:28:14 +02:00
|
|
|
twiBaseAddress->TXD.PTR = (uint32_t) data;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->TXD.MAXCNT = size;
|
2020-10-23 22:25:37 +02:00
|
|
|
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->TASKS_STARTTX = 1;
|
|
|
|
|
2021-04-18 19:28:14 +02:00
|
|
|
while (!twiBaseAddress->EVENTS_TXSTARTED && !twiBaseAddress->EVENTS_ERROR)
|
|
|
|
;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_TXSTARTED = 0x0UL;
|
|
|
|
|
|
|
|
txStartedCycleCount = DWT->CYCCNT;
|
|
|
|
uint32_t currentCycleCount;
|
2021-04-18 19:28:14 +02:00
|
|
|
while (!twiBaseAddress->EVENTS_LASTTX && !twiBaseAddress->EVENTS_ERROR) {
|
2021-04-10 20:09:33 +02:00
|
|
|
currentCycleCount = DWT->CYCCNT;
|
2021-04-18 19:28:14 +02:00
|
|
|
if ((currentCycleCount - txStartedCycleCount) > HwFreezedDelay) {
|
2021-04-10 20:09:33 +02:00
|
|
|
FixHwFreezed();
|
|
|
|
return ErrorCodes::TransactionFailed;
|
|
|
|
}
|
2020-07-19 20:30:44 +02:00
|
|
|
}
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_LASTTX = 0x0UL;
|
2020-07-19 20:30:44 +02:00
|
|
|
|
2021-04-10 20:09:33 +02:00
|
|
|
if (stop || twiBaseAddress->EVENTS_ERROR) {
|
|
|
|
twiBaseAddress->TASKS_STOP = 0x1UL;
|
2021-04-18 19:28:14 +02:00
|
|
|
while (!twiBaseAddress->EVENTS_STOPPED)
|
|
|
|
;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_STOPPED = 0x0UL;
|
2021-04-18 19:28:14 +02:00
|
|
|
} else {
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->TASKS_SUSPEND = 0x1UL;
|
2021-04-18 19:28:14 +02:00
|
|
|
while (!twiBaseAddress->EVENTS_SUSPENDED)
|
|
|
|
;
|
2021-04-10 20:09:33 +02:00
|
|
|
twiBaseAddress->EVENTS_SUSPENDED = 0x0UL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (twiBaseAddress->EVENTS_ERROR) {
|
|
|
|
twiBaseAddress->EVENTS_ERROR = 0x0UL;
|
|
|
|
uint32_t error = twiBaseAddress->ERRORSRC;
|
|
|
|
twiBaseAddress->ERRORSRC = error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ErrorCodes::NoError;
|
2020-08-22 17:59:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwiMaster::Sleep() {
|
2021-08-12 13:06:58 +02:00
|
|
|
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos);
|
2020-08-22 17:59:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwiMaster::Wakeup() {
|
2021-08-09 16:45:24 +02:00
|
|
|
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
|
2020-08-22 17:59:59 +02:00
|
|
|
}
|
2021-04-10 20:09:33 +02:00
|
|
|
|
|
|
|
/* Sometimes, the TWIM device just freeze and never set the event EVENTS_LASTTX.
|
|
|
|
* This method disable and re-enable the peripheral so that it works again.
|
|
|
|
* This is just a workaround, and it would be better if we could find a way to prevent
|
|
|
|
* this issue from happening.
|
|
|
|
* */
|
|
|
|
void TwiMaster::FixHwFreezed() {
|
|
|
|
NRF_LOG_INFO("I2C device frozen, reinitializing it!");
|
2021-08-16 17:26:10 +02:00
|
|
|
|
2021-04-10 20:09:33 +02:00
|
|
|
uint32_t twi_state = NRF_TWI1->ENABLE;
|
|
|
|
|
2021-08-16 17:26:10 +02:00
|
|
|
Sleep();
|
2021-04-10 20:09:33 +02:00
|
|
|
|
|
|
|
twiBaseAddress->ENABLE = twi_state;
|
2021-08-09 16:45:24 +02:00
|
|
|
}
|