Skip to content

Commit baa7e1b

Browse files
committed
Optimize twiMaster
1 parent 514481e commit baa7e1b

2 files changed

Lines changed: 21 additions & 22 deletions

File tree

src/drivers/TwiMaster.cpp

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,25 @@ using namespace Pinetime::Drivers;
99
// TODO use DMA/IRQ
1010

1111
TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module {module}, params {params} {
12+
mutex = xSemaphoreCreateBinary();
1213
}
1314

14-
void TwiMaster::Init() {
15-
if(mutex == nullptr)
16-
mutex = xSemaphoreCreateBinary();
17-
15+
void TwiMaster::ConfigurePins() const {
1816
NRF_GPIO->PIN_CNF[params.pinScl] =
19-
((uint32_t) GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | ((uint32_t) GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
20-
((uint32_t) GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | ((uint32_t) GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
21-
((uint32_t) GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
17+
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
18+
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
19+
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
20+
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
2221

2322
NRF_GPIO->PIN_CNF[params.pinSda] =
24-
((uint32_t) GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | ((uint32_t) GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
25-
((uint32_t) GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | ((uint32_t) GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
26-
((uint32_t) GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
23+
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
24+
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
25+
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
26+
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
27+
}
28+
29+
void TwiMaster::Init() {
30+
ConfigurePins();
2731

2832
switch (module) {
2933
case Modules::TWIM1:
@@ -179,7 +183,8 @@ void TwiMaster::Sleep() {
179183
}
180184

181185
void TwiMaster::Wakeup() {
182-
Init();
186+
ConfigurePins();
187+
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
183188
NRF_LOG_INFO("[TWIMASTER] Wakeup");
184189
}
185190

@@ -194,16 +199,8 @@ void TwiMaster::FixHwFreezed() {
194199
uint32_t twi_state = NRF_TWI1->ENABLE;
195200
twiBaseAddress->ENABLE = TWIM_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
196201

197-
NRF_GPIO->PIN_CNF[params.pinScl] =
198-
((uint32_t) GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | ((uint32_t) GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
199-
((uint32_t) GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | ((uint32_t) GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
200-
((uint32_t) GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
201-
202-
NRF_GPIO->PIN_CNF[params.pinSda] =
203-
((uint32_t) GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) | ((uint32_t) GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
204-
((uint32_t) GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos) | ((uint32_t) GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) |
205-
((uint32_t) GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
202+
ConfigurePins();
206203

207204
// Re-enable I²C
208205
twiBaseAddress->ENABLE = twi_state;
209-
}
206+
}

src/drivers/TwiMaster.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ namespace Pinetime {
2626
void Sleep();
2727
void Wakeup();
2828

29+
void ConfigurePins() const;
30+
2931
private:
3032
ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop);
3133
ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop);
@@ -41,4 +43,4 @@ namespace Pinetime {
4143
static constexpr uint32_t HwFreezedDelay {161000};
4244
};
4345
}
44-
}
46+
}

0 commit comments

Comments
 (0)