@@ -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
1431void 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
6957TwiMaster::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, ®isterAddress, 1 , false );
7261 ret = Read (deviceAddress, data, size, true );
62+ Sleep ();
7363 xSemaphoreGive (mutex);
7464 return ret;
7565}
7666
7767TwiMaster::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
172164void 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
181168void 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 * */
191177void 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+ }
0 commit comments