Skip to content

Commit f895da8

Browse files
committed
Merge branch 'Riksu9000-twimaster_rework' into develop
2 parents 969de9a + 45e7638 commit f895da8

4 files changed

Lines changed: 49 additions & 88 deletions

File tree

src/drivers/TwiMaster.cpp

Lines changed: 35 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,45 +8,39 @@ using namespace Pinetime::Drivers;
88
// TODO use shortcut to automatically send STOP when receive LastTX, for example
99
// TODO use DMA/IRQ
1010

11-
TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module {module}, params {params} {
11+
TwiMaster::TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl)
12+
: module {module}, frequency {frequency}, pinSda {pinSda}, pinScl {pinScl} {
13+
}
14+
15+
void TwiMaster::ConfigurePins() const {
16+
NRF_GPIO->PIN_CNF[pinScl] =
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_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
20+
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
21+
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
22+
23+
NRF_GPIO->PIN_CNF[pinSda] =
24+
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
25+
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
26+
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
27+
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
28+
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
1229
}
1330

1431
void TwiMaster::Init() {
15-
if(mutex == nullptr)
32+
if (mutex == nullptr) {
1633
mutex = xSemaphoreCreateBinary();
17-
18-
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);
22-
23-
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);
27-
28-
switch (module) {
29-
case Modules::TWIM1:
30-
twiBaseAddress = NRF_TWIM1;
31-
break;
32-
default:
33-
return;
3434
}
3535

36-
switch (static_cast<Frequencies>(params.frequency)) {
37-
case Frequencies::Khz100:
38-
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100;
39-
break;
40-
case Frequencies::Khz250:
41-
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K250;
42-
break;
43-
case Frequencies::Khz400:
44-
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400;
45-
break;
46-
}
36+
ConfigurePins();
4737

48-
twiBaseAddress->PSEL.SCL = params.pinScl;
49-
twiBaseAddress->PSEL.SDA = params.pinSda;
38+
twiBaseAddress = module;
39+
40+
twiBaseAddress->FREQUENCY = frequency;
41+
42+
twiBaseAddress->PSEL.SCL = pinScl;
43+
twiBaseAddress->PSEL.SDA = pinSda;
5044
twiBaseAddress->EVENTS_LASTRX = 0;
5145
twiBaseAddress->EVENTS_STOPPED = 0;
5246
twiBaseAddress->EVENTS_LASTTX = 0;
@@ -57,29 +51,27 @@ void TwiMaster::Init() {
5751

5852
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
5953

60-
/* // IRQ
61-
NVIC_ClearPendingIRQ(_IRQn);
62-
NVIC_SetPriority(_IRQn, 2);
63-
NVIC_EnableIRQ(_IRQn);
64-
*/
65-
6654
xSemaphoreGive(mutex);
6755
}
6856

6957
TwiMaster::ErrorCodes TwiMaster::Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* data, size_t size) {
7058
xSemaphoreTake(mutex, portMAX_DELAY);
59+
Wakeup();
7160
auto ret = Write(deviceAddress, &registerAddress, 1, false);
7261
ret = Read(deviceAddress, data, size, true);
62+
Sleep();
7363
xSemaphoreGive(mutex);
7464
return ret;
7565
}
7666

7767
TwiMaster::ErrorCodes TwiMaster::Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size) {
7868
ASSERT(size <= maxDataSize);
7969
xSemaphoreTake(mutex, portMAX_DELAY);
70+
Wakeup();
8071
internalBuffer[0] = registerAddress;
8172
std::memcpy(internalBuffer + 1, data, size);
8273
auto ret = Write(deviceAddress, internalBuffer, size + 1, true);
74+
Sleep();
8375
xSemaphoreGive(mutex);
8476
return ret;
8577
}
@@ -170,17 +162,11 @@ TwiMaster::ErrorCodes TwiMaster::Write(uint8_t deviceAddress, const uint8_t* dat
170162
}
171163

172164
void TwiMaster::Sleep() {
173-
while (twiBaseAddress->ENABLE != 0) {
174-
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos);
175-
}
176-
nrf_gpio_cfg_default(6);
177-
nrf_gpio_cfg_default(7);
178-
NRF_LOG_INFO("[TWIMASTER] Sleep");
165+
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Disabled << TWIM_ENABLE_ENABLE_Pos);
179166
}
180167

181168
void TwiMaster::Wakeup() {
182-
Init();
183-
NRF_LOG_INFO("[TWIMASTER] Wakeup");
169+
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
184170
}
185171

186172
/* Sometimes, the TWIM device just freeze and never set the event EVENTS_LASTTX.
@@ -190,20 +176,10 @@ void TwiMaster::Wakeup() {
190176
* */
191177
void TwiMaster::FixHwFreezed() {
192178
NRF_LOG_INFO("I2C device frozen, reinitializing it!");
193-
// Disable I²C
194-
uint32_t twi_state = NRF_TWI1->ENABLE;
195-
twiBaseAddress->ENABLE = TWIM_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
196179

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);
180+
uint32_t twi_state = NRF_TWI1->ENABLE;
201181

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);
182+
Sleep();
206183

207-
// Re-enable I²C
208184
twiBaseAddress->ENABLE = twi_state;
209-
}
185+
}

src/drivers/TwiMaster.h

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,9 @@ namespace Pinetime {
88
namespace Drivers {
99
class TwiMaster {
1010
public:
11-
enum class Modules { TWIM1 };
12-
enum class Frequencies { Khz100, Khz250, Khz400 };
1311
enum class ErrorCodes { NoError, TransactionFailed };
14-
struct Parameters {
15-
uint32_t frequency;
16-
uint8_t pinSda;
17-
uint8_t pinScl;
18-
};
1912

20-
TwiMaster(const Modules module, const Parameters& params);
13+
TwiMaster(NRF_TWIM_Type* module, uint32_t frequency, uint8_t pinSda, uint8_t pinScl);
2114

2215
void Init();
2316
ErrorCodes Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size);
@@ -30,15 +23,19 @@ namespace Pinetime {
3023
ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop);
3124
ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop);
3225
void FixHwFreezed();
26+
void ConfigurePins() const;
27+
3328
NRF_TWIM_Type* twiBaseAddress;
3429
SemaphoreHandle_t mutex = nullptr;
35-
const Modules module;
36-
const Parameters params;
30+
NRF_TWIM_Type* module;
31+
uint32_t frequency;
32+
uint8_t pinSda;
33+
uint8_t pinScl;
3734
static constexpr uint8_t maxDataSize {16};
3835
static constexpr uint8_t registerSize {1};
3936
uint8_t internalBuffer[maxDataSize + registerSize];
4037
uint32_t txStartedCycleCount = 0;
4138
static constexpr uint32_t HwFreezedDelay {161000};
4239
};
4340
}
44-
}
41+
}

src/main.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi};
8484
// respecting correct timings. According to erratas heet, this magic value makes it run
8585
// at ~390Khz with correct timings.
8686
static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug {0x06200000};
87-
Pinetime::Drivers::TwiMaster twiMaster {Pinetime::Drivers::TwiMaster::Modules::TWIM1,
88-
Pinetime::Drivers::TwiMaster::Parameters {MaxTwiFrequencyWithoutHardwareBug, pinTwiSda, pinTwiScl}};
87+
Pinetime::Drivers::TwiMaster twiMaster {NRF_TWIM1, MaxTwiFrequencyWithoutHardwareBug, pinTwiSda, pinTwiScl};
8988
Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress};
9089
#ifdef PINETIME_IS_RECOVERY
9190
static constexpr bool isFactory = true;

src/systemtask/SystemTask.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,6 @@ void SystemTask::Work() {
220220
break;
221221
case Messages::GoToRunning:
222222
spi.Wakeup();
223-
twiMaster.Wakeup();
224223

225224
// Double Tap needs the touch screen to be in normal mode
226225
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
@@ -241,16 +240,12 @@ void SystemTask::Work() {
241240
isDimmed = false;
242241
break;
243242
case Messages::TouchWakeUp: {
244-
twiMaster.Wakeup();
245-
touchHandler.GetNewTouchInfo();
246-
auto gesture = touchHandler.GestureGet();
247-
if ((gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap &&
248-
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) ||
249-
(gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap &&
250-
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap))) {
243+
auto touchInfo = touchPanel.GetTouchInfo();
244+
if (touchInfo.isTouch and ((touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::DoubleTap and
245+
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) or
246+
(touchInfo.gesture == Pinetime::Drivers::Cst816S::Gestures::SingleTap and
247+
settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap)))) {
251248
GoToRunning();
252-
} else {
253-
twiMaster.Sleep();
254249
}
255250
} break;
256251
case Messages::GoToSleep:
@@ -320,7 +315,6 @@ void SystemTask::Work() {
320315
if (!settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
321316
touchPanel.Sleep();
322317
}
323-
twiMaster.Sleep();
324318

325319
isSleeping = true;
326320
isGoingToSleep = false;
@@ -372,17 +366,12 @@ void SystemTask::UpdateMotion() {
372366
if (isSleeping && !settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist))
373367
return;
374368

375-
if (isSleeping)
376-
twiMaster.Wakeup();
377-
378369
if (stepCounterMustBeReset) {
379370
motionSensor.ResetStepCounter();
380371
stepCounterMustBeReset = false;
381372
}
382373

383374
auto motionValues = motionSensor.Process();
384-
if (isSleeping)
385-
twiMaster.Sleep();
386375

387376
motionController.IsSensorOk(motionSensor.IsOk());
388377
motionController.Update(motionValues.x, motionValues.y, motionValues.z, motionValues.steps);

0 commit comments

Comments
 (0)