2020-07-19 14:30:44 -04:00
|
|
|
#include "TwiMaster.h"
|
2020-11-14 23:04:22 -05:00
|
|
|
#include <cstring>
|
|
|
|
#include <hal/nrf_gpio.h>
|
|
|
|
#include <nrfx_log.h>
|
2021-04-08 14:07:24 -04:00
|
|
|
#include <nrfx_twim.h>
|
|
|
|
#include <nrf_drv_twi.h>
|
2020-07-19 14:30:44 -04:00
|
|
|
using namespace Pinetime::Drivers;
|
|
|
|
|
|
|
|
// TODO use shortcut to automatically send STOP when receive LastTX, for example
|
|
|
|
// TODO use DMA/IRQ
|
|
|
|
|
2021-04-08 14:07:24 -04:00
|
|
|
TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module{module}, params{params}, mutex{xSemaphoreCreateBinary()} {
|
|
|
|
ASSERT(mutex != nullptr);
|
2020-07-19 14:30:44 -04:00
|
|
|
switch(module) {
|
2021-04-08 14:07:24 -04:00
|
|
|
case Modules::TWIM1:
|
2020-07-19 14:30:44 -04:00
|
|
|
default:
|
2021-04-08 14:07:24 -04:00
|
|
|
twim = NRFX_TWIM_INSTANCE(1);
|
|
|
|
break;
|
2020-07-19 14:30:44 -04:00
|
|
|
}
|
2021-04-08 14:07:24 -04:00
|
|
|
}
|
2020-07-19 14:30:44 -04:00
|
|
|
|
2021-04-08 14:07:24 -04:00
|
|
|
void TwiMaster::Init() {
|
|
|
|
nrfx_twim_config_t config;
|
|
|
|
config.frequency = static_cast<nrf_twim_frequency_t>(params.frequency);
|
|
|
|
config.hold_bus_uninit = false;
|
|
|
|
config.interrupt_priority = 0;
|
|
|
|
config.scl = params.pinScl;
|
|
|
|
config.sda = params.pinSda;
|
|
|
|
nrfx_twim_init(&twim,
|
|
|
|
&config,
|
|
|
|
nullptr,
|
|
|
|
nullptr);
|
|
|
|
nrfx_twim_enable(&twim);
|
2020-07-19 14:30:44 -04:00
|
|
|
|
|
|
|
xSemaphoreGive(mutex);
|
|
|
|
}
|
|
|
|
|
2020-10-23 16:25:37 -04:00
|
|
|
TwiMaster::ErrorCodes TwiMaster::Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t *data, size_t size) {
|
2020-07-19 14:30:44 -04:00
|
|
|
xSemaphoreTake(mutex, portMAX_DELAY);
|
2021-04-08 14:07:24 -04:00
|
|
|
TwiMaster::ErrorCodes ret;
|
|
|
|
|
|
|
|
auto err = nrfx_twim_tx(&twim, deviceAddress, ®isterAddress, 1, false);
|
|
|
|
if(err != 0) {
|
|
|
|
return TwiMaster::ErrorCodes::TransactionFailed;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = nrfx_twim_rx(&twim, deviceAddress, data, size);
|
|
|
|
if(err != 0) {
|
|
|
|
return TwiMaster::ErrorCodes::TransactionFailed;
|
|
|
|
}
|
2020-07-19 14:30:44 -04:00
|
|
|
xSemaphoreGive(mutex);
|
2020-10-23 16:25:37 -04:00
|
|
|
|
2021-04-08 14:07:24 -04:00
|
|
|
return TwiMaster::ErrorCodes::NoError;
|
2020-07-19 14:30:44 -04:00
|
|
|
}
|
|
|
|
|
2020-10-23 16:25:37 -04:00
|
|
|
TwiMaster::ErrorCodes TwiMaster::Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t *data, size_t size) {
|
2020-07-19 14:30:44 -04:00
|
|
|
ASSERT(size <= maxDataSize);
|
|
|
|
xSemaphoreTake(mutex, portMAX_DELAY);
|
2020-10-23 16:25:37 -04:00
|
|
|
TwiMaster::ErrorCodes ret;
|
|
|
|
|
2020-07-19 14:30:44 -04:00
|
|
|
internalBuffer[0] = registerAddress;
|
|
|
|
std::memcpy(internalBuffer+1, data, size);
|
2021-04-08 14:07:24 -04:00
|
|
|
auto err = nrfx_twim_tx(&twim, deviceAddress, internalBuffer , size+1, false);
|
|
|
|
if(err != 0){
|
|
|
|
return TwiMaster::ErrorCodes::TransactionFailed;
|
2020-07-19 14:30:44 -04:00
|
|
|
}
|
|
|
|
|
2021-04-08 14:07:24 -04:00
|
|
|
xSemaphoreGive(mutex);
|
|
|
|
return TwiMaster::ErrorCodes::NoError;
|
2020-08-22 11:59:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void TwiMaster::Sleep() {
|
2021-04-08 14:07:24 -04:00
|
|
|
nrfx_twim_disable(&twim);
|
|
|
|
nrfx_twim_uninit(&twim);
|
|
|
|
|
2020-08-22 11:59:59 -04:00
|
|
|
nrf_gpio_cfg_default(6);
|
|
|
|
nrf_gpio_cfg_default(7);
|
|
|
|
NRF_LOG_INFO("[TWIMASTER] Sleep");
|
|
|
|
}
|
|
|
|
|
|
|
|
void TwiMaster::Wakeup() {
|
|
|
|
Init();
|
|
|
|
NRF_LOG_INFO("[TWIMASTER] Wakeup");
|
|
|
|
}
|