Skip to content

Commit 6773245

Browse files
committed
[zero] Bring more customization to Wire class
1 parent de9a555 commit 6773245

File tree

2 files changed

+12
-78
lines changed

2 files changed

+12
-78
lines changed

libraries/Wire/Wire.cpp

+8-77
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ extern "C" {
2626

2727
#include "Wire.h"
2828

29-
TwoWire::TwoWire(SERCOM * s)
29+
TwoWire::TwoWire(SERCOM * s, uint8_t pinSDA, uint8_t pinSCL)
3030
{
3131
this->sercom = s;
32+
this->_uc_pinSDA=pinSDA;
33+
this->_uc_pinSCL=pinSCL;
3234
transmissionBegun = false;
3335
}
3436

@@ -37,8 +39,8 @@ void TwoWire::begin(void) {
3739
sercom->initMasterWIRE(TWI_CLOCK);
3840
sercom->enableWIRE();
3941

40-
pinPeripheral(PIN_WIRE_SDA, g_APinDescription[PIN_WIRE_SDA].ulPinType);
41-
pinPeripheral(PIN_WIRE_SCL, g_APinDescription[PIN_WIRE_SCL].ulPinType);
42+
pinPeripheral(_uc_pinSDA, g_APinDescription[_uc_pinSDA].ulPinType);
43+
pinPeripheral(_uc_pinSCL, g_APinDescription[_uc_pinSCL].ulPinType);
4244
}
4345

4446
void TwoWire::begin(uint8_t address) {
@@ -254,77 +256,6 @@ void TwoWire::onService(void)
254256
}
255257
}
256258

257-
/*
258-
void TwoWire::onService(void)
259-
{
260-
// Retrieve interrupt status
261-
uint32_t sr = TWI_GetStatus(twi);
262-
263-
if (status == SLAVE_IDLE && TWI_STATUS_SVACC(sr)) {
264-
TWI_DisableIt(twi, TWI_IDR_SVACC);
265-
TWI_EnableIt(twi, TWI_IER_RXRDY | TWI_IER_GACC | TWI_IER_NACK
266-
| TWI_IER_EOSACC | TWI_IER_SCL_WS | TWI_IER_TXCOMP);
267-
268-
srvBufferLength = 0;
269-
srvBufferIndex = 0;
270-
271-
// Detect if we should go into RECV or SEND status
272-
// SVREAD==1 means *master* reading -> SLAVE_SEND
273-
if (!TWI_STATUS_SVREAD(sr)) {
274-
status = SLAVE_RECV;
275-
} else {
276-
status = SLAVE_SEND;
277-
278-
// Alert calling program to generate a response ASAP
279-
if (onRequestCallback)
280-
onRequestCallback();
281-
else
282-
// create a default 1-byte response
283-
write((uint8_t) 0);
284-
}
285-
}
286-
287-
if (status != SLAVE_IDLE) {
288-
if (TWI_STATUS_TXCOMP(sr) && TWI_STATUS_EOSACC(sr)) {
289-
if (status == SLAVE_RECV && onReceiveCallback) {
290-
// Copy data into rxBuffer
291-
// (allows to receive another packet while the
292-
// user program reads actual data)
293-
for (uint8_t i = 0; i < srvBufferLength; ++i)
294-
rxBuffer[i] = srvBuffer[i];
295-
rxBufferIndex = 0;
296-
rxBufferLength = srvBufferLength;
297-
298-
// Alert calling program
299-
onReceiveCallback( rxBufferLength);
300-
}
301-
302-
// Transfer completed
303-
TWI_EnableIt(twi, TWI_SR_SVACC);
304-
TWI_DisableIt(twi, TWI_IDR_RXRDY | TWI_IDR_GACC | TWI_IDR_NACK
305-
| TWI_IDR_EOSACC | TWI_IDR_SCL_WS | TWI_IER_TXCOMP);
306-
status = SLAVE_IDLE;
307-
}
308-
}
309-
310-
if (status == SLAVE_RECV) {
311-
if (TWI_STATUS_RXRDY(sr)) {
312-
if (srvBufferLength < BUFFER_LENGTH)
313-
srvBuffer[srvBufferLength++] = TWI_ReadByte(twi);
314-
}
315-
}
316-
317-
if (status == SLAVE_SEND) {
318-
if (TWI_STATUS_TXRDY(sr) && !TWI_STATUS_NACK(sr)) {
319-
uint8_t c = 'x';
320-
if (srvBufferIndex < srvBufferLength)
321-
c = srvBuffer[srvBufferIndex++];
322-
TWI_WriteByte(twi, c);
323-
}
324-
}
325-
}
326-
*/
327-
328259
#if WIRE_INTERFACES_COUNT > 0
329260
/*static void Wire_Init(void) {
330261
pmc_enable_periph_clk(WIRE_INTERFACE_ID);
@@ -346,10 +277,10 @@ void TwoWire::onService(void)
346277
}*/
347278

348279

349-
TwoWire Wire(&sercom3);
280+
TwoWire Wire(&PERIPH_WIRE, PIN_WIRE_SDA, PIN_WIRE_SCL);
350281

351-
void SERCOM3_Handler(void) {
282+
void WIRE_IT_HANDLER(void) {
352283
Wire.onService();
353284
}
354285

355-
#endif
286+
#endif // WIRE_INTERFACES_COUNT > 0

libraries/Wire/Wire.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
class TwoWire : public Stream
3232
{
3333
public:
34-
TwoWire(SERCOM *s);
34+
TwoWire(SERCOM *s, uint8_t pinSDA, uint8_t pinSCL);
3535
void begin();
3636
void begin(uint8_t);
3737
void setClock(uint32_t); // dummy function
@@ -59,6 +59,9 @@ class TwoWire : public Stream
5959

6060
private:
6161
SERCOM * sercom;
62+
uint8_t _uc_pinSDA;
63+
uint8_t _uc_pinSCL;
64+
6265
bool transmissionBegun;
6366

6467
// RX Buffer

0 commit comments

Comments
 (0)