|
|
@ -8,19 +8,20 @@ using namespace Pinetime::Drivers;
|
|
|
|
// TODO use shortcut to automatically send STOP when receive LastTX, for example
|
|
|
|
// TODO use shortcut to automatically send STOP when receive LastTX, for example
|
|
|
|
// TODO use DMA/IRQ
|
|
|
|
// TODO use DMA/IRQ
|
|
|
|
|
|
|
|
|
|
|
|
TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module {module}, params {params} {
|
|
|
|
TwiMaster::TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl)
|
|
|
|
|
|
|
|
: module {module}, frequency {frequency}, pinSda {pinSda}, pinScl {pinScl} {
|
|
|
|
mutex = xSemaphoreCreateBinary();
|
|
|
|
mutex = xSemaphoreCreateBinary();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void TwiMaster::ConfigurePins() const {
|
|
|
|
void TwiMaster::ConfigurePins() const {
|
|
|
|
NRF_GPIO->PIN_CNF[params.pinScl] =
|
|
|
|
NRF_GPIO->PIN_CNF[pinScl] =
|
|
|
|
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
|
|
|
|
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
|
|
|
|
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
|
|
|
|
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
|
|
|
|
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
|
|
|
|
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
|
|
|
|
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
|
|
|
|
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
|
|
|
|
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
|
|
|
|
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
|
|
|
|
|
|
|
|
|
|
|
|
NRF_GPIO->PIN_CNF[params.pinSda] =
|
|
|
|
NRF_GPIO->PIN_CNF[pinSda] =
|
|
|
|
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
|
|
|
|
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
|
|
|
|
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
|
|
|
|
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
|
|
|
|
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
|
|
|
|
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
|
|
|
@ -31,28 +32,12 @@ void TwiMaster::ConfigurePins() const {
|
|
|
|
void TwiMaster::Init() {
|
|
|
|
void TwiMaster::Init() {
|
|
|
|
ConfigurePins();
|
|
|
|
ConfigurePins();
|
|
|
|
|
|
|
|
|
|
|
|
switch (module) {
|
|
|
|
twiBaseAddress = module;
|
|
|
|
case Modules::TWIM1:
|
|
|
|
|
|
|
|
twiBaseAddress = NRF_TWIM1;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
switch (static_cast<Frequencies>(params.frequency)) {
|
|
|
|
twiBaseAddress->FREQUENCY = frequency;
|
|
|
|
case Frequencies::Khz100:
|
|
|
|
|
|
|
|
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Frequencies::Khz250:
|
|
|
|
|
|
|
|
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K250;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Frequencies::Khz400:
|
|
|
|
|
|
|
|
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400;
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
twiBaseAddress->PSEL.SCL = params.pinScl;
|
|
|
|
twiBaseAddress->PSEL.SCL = pinScl;
|
|
|
|
twiBaseAddress->PSEL.SDA = params.pinSda;
|
|
|
|
twiBaseAddress->PSEL.SDA = pinSda;
|
|
|
|
twiBaseAddress->EVENTS_LASTRX = 0;
|
|
|
|
twiBaseAddress->EVENTS_LASTRX = 0;
|
|
|
|
twiBaseAddress->EVENTS_STOPPED = 0;
|
|
|
|
twiBaseAddress->EVENTS_STOPPED = 0;
|
|
|
|
twiBaseAddress->EVENTS_LASTTX = 0;
|
|
|
|
twiBaseAddress->EVENTS_LASTTX = 0;
|
|
|
@ -63,12 +48,6 @@ void TwiMaster::Init() {
|
|
|
|
|
|
|
|
|
|
|
|
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
|
|
|
|
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
|
|
|
|
|
|
|
|
|
|
|
|
/* // IRQ
|
|
|
|
|
|
|
|
NVIC_ClearPendingIRQ(_IRQn);
|
|
|
|
|
|
|
|
NVIC_SetPriority(_IRQn, 2);
|
|
|
|
|
|
|
|
NVIC_EnableIRQ(_IRQn);
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
xSemaphoreGive(mutex);
|
|
|
|
xSemaphoreGive(mutex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -194,12 +173,10 @@ void TwiMaster::Wakeup() {
|
|
|
|
* */
|
|
|
|
* */
|
|
|
|
void TwiMaster::FixHwFreezed() {
|
|
|
|
void TwiMaster::FixHwFreezed() {
|
|
|
|
NRF_LOG_INFO("I2C device frozen, reinitializing it!");
|
|
|
|
NRF_LOG_INFO("I2C device frozen, reinitializing it!");
|
|
|
|
// Disable I²C
|
|
|
|
|
|
|
|
uint32_t twi_state = NRF_TWI1->ENABLE;
|
|
|
|
uint32_t twi_state = NRF_TWI1->ENABLE;
|
|
|
|
twiBaseAddress->ENABLE = TWIM_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ConfigurePins();
|
|
|
|
Sleep();
|
|
|
|
|
|
|
|
|
|
|
|
// Re-enable I²C
|
|
|
|
|
|
|
|
twiBaseAddress->ENABLE = twi_state;
|
|
|
|
twiBaseAddress->ENABLE = twi_state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|