Skip to content

Commit 04c405b

Browse files
committed
support for two wire via XWire library
1 parent 1eeec94 commit 04c405b

File tree

4 files changed

+1149
-37
lines changed

4 files changed

+1149
-37
lines changed

cores/xmega/wiring.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,12 +881,23 @@ void init()
881881
// DISABLE TWI (specifically TWI interrupts)
882882
// -----------------------------------------
883883

884+
#ifdef TWIC_CTRL
884885
TWIC_MASTER_CTRLA = 0;
885886
TWIC_SLAVE_CTRLA = 0;
886-
#if NUM_DIGITAL_PINS > 18 /* meaning there is a PORT E available */
887+
#endif // TWIC_CTRL
888+
#ifdef TWID_CTRL
889+
TWID_MASTER_CTRLA = 0;
890+
TWID_SLAVE_CTRLA = 0;
891+
#endif // TWID
892+
//#if NUM_DIGITAL_PINS > 18 /* meaning there is a PORT E available */
893+
#ifdef TWIE_CTRL
887894
TWIE_MASTER_CTRLA = 0;
888895
TWIE_SLAVE_CTRLA = 0;
889-
#endif // NUM_DIGITAL_PINS > 18
896+
#endif // TWIE_CTRL
897+
#ifdef TWIF_CTRL
898+
TWIF_MASTER_CTRLA = 0;
899+
TWIF_SLAVE_CTRLA = 0;
900+
#endif // TWIF
890901

891902
// --------------------
892903
// DISABLE SERIAL PORTS

variants/xmega128a1/pins_arduino.h

Lines changed: 51 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@
9696
//
9797
#define DIGITAL_IO_PIN_SHIFT /* UNCOMMENT THIS to shift digital pin assignments for Arduino shield compatibility */
9898

99-
//#define USE_AREF 0x2 /* see 28.16.3 in 'AU' manual - this is the REFCTRL bits for the reference select, AREF on PORTA (PA0) */
99+
//#define USE_AREF analogReference_PORTA0 /* see 28.16.3 in 'AU' manual - this is the REFCTRL bits for the reference select, AREF on PORTA (PA0) */
100100

101101
#define NUM_DIGITAL_PINS 62
102102

@@ -225,8 +225,27 @@
225225
// --------------------------------------------
226226

227227

228-
#define DEFAULT_TWI TWIC /* for now, later maybe TWIE? */
229-
#define TWIC_VECT_ENABLE /* use this to select the correct interrupt vectors */
228+
// TWI ports - default is TWIE
229+
#define DEFAULT_TWI TWIE
230+
231+
// the XMega128a1u has four TWI ports
232+
#define TWI_PORT0 TWIE
233+
#define TWI_VECTOR_S0 TWIE_TWIS_vect
234+
#define TWI_VECTOR_M0 TWIE_TWIM_vect
235+
#define TWI_PORT1 TWIC
236+
#define TWI_VECTOR_S1 TWIC_TWIS_vect
237+
#define TWI_VECTOR_M1 TWIC_TWIM_vect
238+
#define TWI_PORT2 TWID
239+
#define TWI_VECTOR_S2 TWID_TWIS_vect
240+
#define TWI_VECTOR_M2 TWID_TWIM_vect
241+
#define TWI_PORT3 TWIF
242+
#define TWI_VECTOR_S3 TWIF_TWIS_vect
243+
#define TWI_VECTOR_M3 TWIF_TWIM_vect
244+
#define TWI_INTERFACE_COUNT 4
245+
246+
247+
// obsolete - consider removal in all of them
248+
//#define TWIC_VECT_ENABLE /* use this to select the correct interrupt vectors for default */
230249

231250
// serial port 0
232251
#define SERIAL_0_PORT_NAME PORTD
@@ -348,12 +367,18 @@ static const uint8_t MOSI1 = 3;
348367
static const uint8_t MISO1 = 4;
349368
static const uint8_t SCK1 = 5;
350369

351-
// default 2-wire on PE0,PE1 - TWIE (for TWIC, you're on your own)
352-
// NOTE: TWIE appears it may be broken, so switch to TWIC?
370+
// default 2-wire on PE0,PE1 - TWIE
371+
// NOTE: this does NOT correspond to the mega2560, which uses 20 and 21 (need more remap work)
353372
static const uint8_t SDA = 14;
354373
static const uint8_t SCL = 15;
355-
356-
// TODO: alternate 2-wire ports
374+
static const uint8_t SDA0 = 14; // TWIE
375+
static const uint8_t SCL0 = 15;
376+
static const uint8_t SDA1 = 6; // TWIC
377+
static const uint8_t SCL1 = 7;
378+
static const uint8_t SDA2 = 59; // TWID
379+
static const uint8_t SCL2 = 60;
380+
static const uint8_t SDA3 = 22; // TWIF
381+
static const uint8_t SCL3 = 23;
357382

358383
// keep track of the indices for port R since its control register
359384
// settings may be slightly different
@@ -382,11 +407,18 @@ static const uint8_t MOSI1 = 5;
382407
static const uint8_t MISO1 = 6;
383408
static const uint8_t SCK1 = 7;
384409

385-
// default 2-wire on PE0,PE1 - TWIE (for TWIC, you're on your own)
386-
// NOTE: TWIE appears it may be broken, so switch to TWIC?
410+
// default 2-wire on PE0,PE1 - TWIE
387411
// NOTE: this does NOT correspond to the mega2560, which uses 20 and 21 (need more remap work)
388412
static const uint8_t SDA = 16;
389413
static const uint8_t SCL = 17;
414+
static const uint8_t SDA0 = 16;
415+
static const uint8_t SCL0 = 17;
416+
static const uint8_t SDA1 = 8;
417+
static const uint8_t SCL1 = 9;
418+
static const uint8_t SDA2 = 0;
419+
static const uint8_t SCL2 = 1;
420+
static const uint8_t SDA3 = 24;
421+
static const uint8_t SCL3 = 25;
390422

391423
// TODO: alternate 2-wire ports TWIC?
392424

@@ -522,25 +554,25 @@ const uint16_t PROGMEM port_to_input_PGM[] = {
522554

523555
const uint16_t PROGMEM digital_pin_to_control_PGM[] = {
524556
#ifndef DIGITAL_IO_PIN_SHIFT
525-
(uint16_t) &PORTD_PIN0CTRL, // PD 0 ** 0 **
526-
(uint16_t) &PORTD_PIN1CTRL, // PD 1 ** 1 **
557+
(uint16_t) &PORTD_PIN0CTRL, // PD 0 ** 0 ** SDA2
558+
(uint16_t) &PORTD_PIN1CTRL, // PD 1 ** 1 ** SCL2
527559
#endif // DIGITAL_IO_PIN_SHIFT
528560
(uint16_t) &PORTD_PIN2CTRL, // PD 2 ** 2 ** USARTD_RX ASYNC
529561
(uint16_t) &PORTD_PIN3CTRL, // PD 3 ** 3 ** USARTD_TX
530562
(uint16_t) &PORTD_PIN4CTRL, // PD 4 ** 4 **
531563
(uint16_t) &PORTD_PIN5CTRL, // PD 5 ** 5 **
532564
(uint16_t) &PORTD_PIN6CTRL, // PD 6 ** 6 **
533565
(uint16_t) &PORTD_PIN7CTRL, // PD 7 ** 7 **
534-
(uint16_t) &PORTC_PIN0CTRL, // PC 0 ** 8 **
535-
(uint16_t) &PORTC_PIN1CTRL, // PC 1 ** 9 **
566+
(uint16_t) &PORTC_PIN0CTRL, // PC 0 ** 8 ** SDA1
567+
(uint16_t) &PORTC_PIN1CTRL, // PC 1 ** 9 ** SCL1
536568
(uint16_t) &PORTC_PIN2CTRL, // PC 2 ** 10 ** ASYNC
537569
(uint16_t) &PORTC_PIN3CTRL, // PC 3 ** 11 **
538570
(uint16_t) &PORTC_PIN4CTRL, // PC 4 ** 12 ** SPI_SS
539571
(uint16_t) &PORTC_PIN5CTRL, // PC 5 ** 13 ** SPI_MOSI
540572
(uint16_t) &PORTC_PIN6CTRL, // PC 6 ** 14 ** SPI_MISO
541573
(uint16_t) &PORTC_PIN7CTRL, // PC 7 ** 15 ** SPI_SCK
542-
(uint16_t) &PORTE_PIN0CTRL, // PE 0 ** 16 ** SDA
543-
(uint16_t) &PORTE_PIN1CTRL, // PE 1 ** 17 ** SCL
574+
(uint16_t) &PORTE_PIN0CTRL, // PE 0 ** 16 ** SDA, SDA0
575+
(uint16_t) &PORTE_PIN1CTRL, // PE 1 ** 17 ** SCL, SCL0
544576
(uint16_t) &PORTE_PIN2CTRL, // PE 2 ** 18 ** ASYNC
545577
(uint16_t) &PORTE_PIN3CTRL, // PE 3 ** 19 **
546578

@@ -549,8 +581,8 @@ const uint16_t PROGMEM digital_pin_to_control_PGM[] = {
549581
(uint16_t) &PORTE_PIN6CTRL, // PE 3 ** 22 **
550582
(uint16_t) &PORTE_PIN7CTRL, // PE 3 ** 23 **
551583

552-
(uint16_t) &PORTF_PIN0CTRL, // PF 0 ** 24 **
553-
(uint16_t) &PORTF_PIN1CTRL, // PF 1 ** 25 **
584+
(uint16_t) &PORTF_PIN0CTRL, // PF 0 ** 24 ** SDA3
585+
(uint16_t) &PORTF_PIN1CTRL, // PF 1 ** 25 ** SCL3
554586
(uint16_t) &PORTF_PIN2CTRL, // PF 2 ** 26 ** ASYNC
555587
(uint16_t) &PORTF_PIN3CTRL, // PF 3 ** 27 **
556588
(uint16_t) &PORTF_PIN4CTRL, // PF 4 ** 28 **
@@ -593,8 +625,8 @@ const uint16_t PROGMEM digital_pin_to_control_PGM[] = {
593625
(uint16_t) &PORTR_PIN0CTRL, // PR 0 ** 60 **
594626
(uint16_t) &PORTR_PIN1CTRL, // PR 1 ** 61 ** default LED
595627
#ifdef DIGITAL_IO_PIN_SHIFT
596-
(uint16_t) &PORTD_PIN0CTRL, // PD 0 ** the new 60 **
597-
(uint16_t) &PORTD_PIN1CTRL, // PD 1 ** the new 61 **
628+
(uint16_t) &PORTD_PIN0CTRL, // PD 0 ** the new 60 ** SDA2
629+
(uint16_t) &PORTD_PIN1CTRL, // PD 1 ** the new 61 ** SCL2
598630
#endif // DIGITAL_IO_PIN_SHIFT
599631
(uint16_t) &PORTA_PIN0CTRL, // PA 0 ** 62 ** A0
600632
(uint16_t) &PORTA_PIN1CTRL, // PA 1 ** 63 ** A1

0 commit comments

Comments
 (0)