Skip to content

Commit d655bd5

Browse files
committed
Add end callback for deinitialization on end
1 parent 2737305 commit d655bd5

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

hardware/arduino/sam/libraries/Wire/Wire.cpp

+39-4
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,11 @@ static inline bool TWI_STATUS_NACK(uint32_t status) {
9292
return (status & TWI_SR_NACK) == TWI_SR_NACK;
9393
}
9494

95-
TwoWire::TwoWire(Twi *_twi, void(*_beginCb)(void)) :
95+
TwoWire::TwoWire(Twi *_twi, void(*_beginCb)(void), void(*_endCb)(void)) :
9696
twi(_twi), rxBufferIndex(0), rxBufferLength(0), txAddress(0),
9797
txBufferLength(0), srvBufferIndex(0), srvBufferLength(0), status(
98-
UNINITIALIZED), onBeginCallback(_beginCb), twiClock(TWI_CLOCK) {
98+
UNINITIALIZED), onBeginCallback(_beginCb),
99+
onEndCallback(_endCb), twiClock(TWI_CLOCK) {
99100
}
100101

101102
void TwoWire::begin(void) {
@@ -128,6 +129,12 @@ void TwoWire::begin(int address) {
128129

129130
void TwoWire::end(void) {
130131
TWI_Disable(twi);
132+
133+
// Enable PDC channel
134+
twi->TWI_PTCR &= ~(UART_PTCR_RXTDIS | UART_PTCR_TXTDIS);
135+
136+
if (onEndCallback)
137+
onEndCallback();
131138
}
132139

133140
void TwoWire::setClock(uint32_t frequency) {
@@ -385,7 +392,21 @@ static void Wire_Init(void) {
385392
NVIC_EnableIRQ(WIRE_ISR_ID);
386393
}
387394

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);
389410

390411
void WIRE_ISR_HANDLER(void) {
391412
Wire.onService();
@@ -412,7 +433,21 @@ static void Wire1_Init(void) {
412433
NVIC_EnableIRQ(WIRE1_ISR_ID);
413434
}
414435

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);
416451

417452
void WIRE1_ISR_HANDLER(void) {
418453
Wire1.onService();

hardware/arduino/sam/libraries/Wire/Wire.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
class TwoWire : public Stream {
3636
public:
37-
TwoWire(Twi *twi, void(*begin_cb)(void));
37+
TwoWire(Twi *twi, void(*begin_cb)(void), void(*end_cb)(void));
3838
void begin();
3939
void begin(uint8_t);
4040
void begin(int);
@@ -89,6 +89,9 @@ class TwoWire : public Stream {
8989
// Called before initialization
9090
void (*onBeginCallback)(void);
9191

92+
// Called after deinitialization
93+
void (*onEndCallback)(void);
94+
9295
// TWI instance
9396
Twi *twi;
9497

0 commit comments

Comments
 (0)