Skip to content

Commit 81a36dc

Browse files
committed
Simplify parameters and cleanup
1 parent 1d341a7 commit 81a36dc

3 files changed

Lines changed: 19 additions & 48 deletions

File tree

src/drivers/TwiMaster.cpp

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,20 @@ 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} {
1213
mutex = xSemaphoreCreateBinary();
1314
}
1415

1516
void TwiMaster::ConfigurePins() const {
16-
NRF_GPIO->PIN_CNF[params.pinScl] =
17+
NRF_GPIO->PIN_CNF[pinScl] =
1718
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
1819
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
1920
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
2021
(GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos) |
2122
(GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
2223

23-
NRF_GPIO->PIN_CNF[params.pinSda] =
24+
NRF_GPIO->PIN_CNF[pinSda] =
2425
(GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos) |
2526
(GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos) |
2627
(GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) |
@@ -31,28 +32,12 @@ void TwiMaster::ConfigurePins() const {
3132
void TwiMaster::Init() {
3233
ConfigurePins();
3334

34-
switch (module) {
35-
case Modules::TWIM1:
36-
twiBaseAddress = NRF_TWIM1;
37-
break;
38-
default:
39-
return;
40-
}
35+
twiBaseAddress = module;
4136

42-
switch (static_cast<Frequencies>(params.frequency)) {
43-
case Frequencies::Khz100:
44-
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100;
45-
break;
46-
case Frequencies::Khz250:
47-
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K250;
48-
break;
49-
case Frequencies::Khz400:
50-
twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400;
51-
break;
52-
}
37+
twiBaseAddress->FREQUENCY = frequency;
5338

54-
twiBaseAddress->PSEL.SCL = params.pinScl;
55-
twiBaseAddress->PSEL.SDA = params.pinSda;
39+
twiBaseAddress->PSEL.SCL = pinScl;
40+
twiBaseAddress->PSEL.SDA = pinSda;
5641
twiBaseAddress->EVENTS_LASTRX = 0;
5742
twiBaseAddress->EVENTS_STOPPED = 0;
5843
twiBaseAddress->EVENTS_LASTTX = 0;
@@ -63,12 +48,6 @@ void TwiMaster::Init() {
6348

6449
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
6550

66-
/* // IRQ
67-
NVIC_ClearPendingIRQ(_IRQn);
68-
NVIC_SetPriority(_IRQn, 2);
69-
NVIC_EnableIRQ(_IRQn);
70-
*/
71-
7251
xSemaphoreGive(mutex);
7352
}
7453

@@ -194,12 +173,10 @@ void TwiMaster::Wakeup() {
194173
* */
195174
void TwiMaster::FixHwFreezed() {
196175
NRF_LOG_INFO("I2C device frozen, reinitializing it!");
197-
// Disable I²C
176+
198177
uint32_t twi_state = NRF_TWI1->ENABLE;
199-
twiBaseAddress->ENABLE = TWIM_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
200178

201-
ConfigurePins();
179+
Sleep();
202180

203-
// Re-enable I²C
204181
twiBaseAddress->ENABLE = twi_state;
205182
}

src/drivers/TwiMaster.h

Lines changed: 7 additions & 12 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);
@@ -26,16 +19,18 @@ namespace Pinetime {
2619
void Sleep();
2720
void Wakeup();
2821

29-
void ConfigurePins() const;
30-
3122
private:
3223
ErrorCodes Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop);
3324
ErrorCodes Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop);
3425
void FixHwFreezed();
26+
void ConfigurePins() const;
27+
3528
NRF_TWIM_Type* twiBaseAddress;
3629
SemaphoreHandle_t mutex = nullptr;
37-
const Modules module;
38-
const Parameters params;
30+
NRF_TWIM_Type* module;
31+
uint32_t frequency;
32+
uint8_t pinSda;
33+
uint8_t pinScl;
3934
static constexpr uint8_t maxDataSize {16};
4035
static constexpr uint8_t registerSize {1};
4136
uint8_t internalBuffer[maxDataSize + registerSize];

src/main.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,8 @@ Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi};
8181
// The TWI device should work @ up to 400Khz but there is a HW bug which prevent it from
8282
// respecting correct timings. According to erratas heet, this magic value makes it run
8383
// at ~390Khz with correct timings.
84-
static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug {0x06200000};
85-
Pinetime::Drivers::TwiMaster twiMaster {Pinetime::Drivers::TwiMaster::Modules::TWIM1,
86-
Pinetime::Drivers::TwiMaster::Parameters {MaxTwiFrequencyWithoutHardwareBug, pinTwiSda, pinTwiScl}};
84+
//static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug {0x06200000};
85+
Pinetime::Drivers::TwiMaster twiMaster {NRF_TWIM1, TWI_FREQUENCY_FREQUENCY_K250, pinTwiSda, pinTwiScl};
8786
Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress};
8887
#ifdef PINETIME_IS_RECOVERY
8988
static constexpr bool isFactory = true;

0 commit comments

Comments
 (0)