@@ -92,10 +92,11 @@ static inline bool TWI_STATUS_NACK(uint32_t status) {
92
92
return (status & TWI_SR_NACK) == TWI_SR_NACK;
93
93
}
94
94
95
- TwoWire::TwoWire (Twi *_twi, void (*_beginCb)(void )) :
95
+ TwoWire::TwoWire (Twi *_twi, void (*_beginCb)(void ), void(*_endCb)( void ) ) :
96
96
twi(_twi), rxBufferIndex(0 ), rxBufferLength(0 ), txAddress(0 ),
97
97
txBufferLength(0 ), srvBufferIndex(0 ), srvBufferLength(0 ), status(
98
- UNINITIALIZED), onBeginCallback(_beginCb), twiClock(TWI_CLOCK) {
98
+ UNINITIALIZED), onBeginCallback(_beginCb),
99
+ onEndCallback(_endCb), twiClock(TWI_CLOCK) {
99
100
}
100
101
101
102
void TwoWire::begin (void ) {
@@ -128,6 +129,12 @@ void TwoWire::begin(int address) {
128
129
129
130
void TwoWire::end (void ) {
130
131
TWI_Disable (twi);
132
+
133
+ // Enable PDC channel
134
+ twi->TWI_PTCR &= ~(UART_PTCR_RXTDIS | UART_PTCR_TXTDIS);
135
+
136
+ if (onEndCallback)
137
+ onEndCallback ();
131
138
}
132
139
133
140
void TwoWire::setClock (uint32_t frequency) {
@@ -385,7 +392,21 @@ static void Wire_Init(void) {
385
392
NVIC_EnableIRQ (WIRE_ISR_ID);
386
393
}
387
394
388
- TwoWire Wire = TwoWire(WIRE_INTERFACE, Wire_Init);
395
+ static void Wire_Deinit (void ) {
396
+ NVIC_DisableIRQ (WIRE_ISR_ID);
397
+ NVIC_ClearPendingIRQ (WIRE_ISR_ID);
398
+
399
+ pmc_disable_periph_clk (WIRE_INTERFACE_ID);
400
+
401
+ // disable pull ups
402
+ pinMode (PIN_WIRE_SDA, OUTPUT);
403
+ pinMode (PIN_WIRE_SCL, OUTPUT);
404
+
405
+ digitalWrite (PIN_WIRE_SDA, LOW);
406
+ digitalWrite (PIN_WIRE_SCL, LOW);
407
+ }
408
+
409
+ TwoWire Wire = TwoWire(WIRE_INTERFACE, Wire_Init, Wire_Deinit);
389
410
390
411
void WIRE_ISR_HANDLER (void ) {
391
412
Wire.onService ();
@@ -412,7 +433,21 @@ static void Wire1_Init(void) {
412
433
NVIC_EnableIRQ (WIRE1_ISR_ID);
413
434
}
414
435
415
- TwoWire Wire1 = TwoWire(WIRE1_INTERFACE, Wire1_Init);
436
+ static void Wire1_Deinit (void ) {
437
+ NVIC_DisableIRQ (WIRE1_ISR_ID);
438
+ NVIC_ClearPendingIRQ (WIRE1_ISR_ID);
439
+
440
+ pmc_disable_periph_clk (WIRE1_INTERFACE_ID);
441
+
442
+ // disable pull ups
443
+ pinMode (PIN_WIRE1_SDA, OUTPUT);
444
+ pinMode (PIN_WIRE1_SCL, OUTPUT);
445
+
446
+ digitalWrite (PIN_WIRE1_SDA, LOW);
447
+ digitalWrite (PIN_WIRE1_SCL, LOW);
448
+ }
449
+
450
+ TwoWire Wire1 = TwoWire(WIRE1_INTERFACE, Wire1_Init, Wire1_Deinit);
416
451
417
452
void WIRE1_ISR_HANDLER (void ) {
418
453
Wire1.onService ();
0 commit comments