Ok, I think I got it to work...
The code below transmits in parallel to a code that prints a long string. When it finishes transmitting the port fires an interrupt breaking the printing execution from the main thread and printing the messages from the interrupt routine in the middle. Maybe I am overlooking something but I believe that means that the two things are running in parallel. Next step, handling back an forth communication.
Feedback is welcome.
// Speed of the twi clock
#define TWI_CLOCK 100000
// size of the buffer to transmit
#define SIZE 32
// definition of the buffer
uint8_t buf[SIZE];
// address of the device we are trying to communicate with
uint8_t txAddress = 0x18;
// Two wire interface instance (for reference look at component_twi.h
// found in the arduino folder/Resources/Java/hardware/arduino/sam/system/
// CMSIS/Device/ATMEL/sam3xa/include/component/
Twi* twi;
void setup() {
// initializing serial for debug
Serial.begin(115200);
Serial.println("transfering...");
// filling the buffer with dummy data
for(uint8_t i=0; i<SIZE; i++){
buf[i] = i;
}
// function call to initialize the TWI (see details below)
initWire(0);
// configuring the two wire interface for master (that means arduino
// talks, device listens)
TWI_ConfigureMaster(twi, TWI_CLOCK, VARIANT_MCK);
// write data procedure (details below)
writeData();
// we print something to kill some time (and hopefully get interrupted)
Serial.println("1234567890abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+=");
delay(2000);
}
void loop() {
// repeat until boredom
writeData();
Serial.println("1234567890abcdefghijklmnopqrstuvwxyz!@#$%^&*()_+=");
delay(2000);
}
// function to initialize the two wire interface. I borrowed most of the code
// here from the Wire Library (Wire.h). Found in the arduino folder/Resources/
// Java/hardware/arduino/sam/libraries/Wire/Wire.h
void initWire(int i){
switch(i){
case 0: // TWI1 = SDA0, SCL0 on the Arduino board (sorry, I know this is confusing)
pmc_enable_periph_clk(WIRE_INTERFACE_ID);
PIO_Configure(g_APinDescription[PIN_WIRE_SDA].pPort,
g_APinDescription[PIN_WIRE_SDA].ulPinType,
g_APinDescription[PIN_WIRE_SDA].ulPin,
g_APinDescription[PIN_WIRE_SDA].ulPinConfiguration);
PIO_Configure(g_APinDescription[PIN_WIRE_SCL].pPort,
g_APinDescription[PIN_WIRE_SCL].ulPinType,
g_APinDescription[PIN_WIRE_SCL].ulPin,
g_APinDescription[PIN_WIRE_SCL].ulPinConfiguration);
NVIC_DisableIRQ(TWI1_IRQn);
NVIC_ClearPendingIRQ(TWI1_IRQn);
NVIC_SetPriority(TWI1_IRQn, 0);
NVIC_EnableIRQ(TWI1_IRQn);
twi = WIRE_INTERFACE;
break;
case 1: // TWI0 = SDA1, SCL1 on the Arduino board (sorry, I know this is confusing)
pmc_enable_periph_clk(WIRE1_INTERFACE_ID);
PIO_Configure(g_APinDescription[PIN_WIRE1_SDA].pPort,
g_APinDescription[PIN_WIRE1_SDA].ulPinType,
g_APinDescription[PIN_WIRE1_SDA].ulPin,
g_APinDescription[PIN_WIRE1_SDA].ulPinConfiguration);
PIO_Configure(g_APinDescription[PIN_WIRE1_SCL].pPort,
g_APinDescription[PIN_WIRE1_SCL].ulPinType,
g_APinDescription[PIN_WIRE1_SCL].ulPin,
g_APinDescription[PIN_WIRE1_SCL].ulPinConfiguration);
NVIC_DisableIRQ(TWI0_IRQn);
NVIC_ClearPendingIRQ(TWI0_IRQn);
NVIC_SetPriority(TWI0_IRQn, 0);
NVIC_EnableIRQ(TWI0_IRQn);
twi = WIRE1_INTERFACE;
break;
}
}
// simple function to send data and set the event for when that has finished sending.
void writeData(){
// StartWrite sends the data. More info on the TWI functions can be found in twi.h
// path: Arduino folder/Resources/Java/hardware/arduino/sam/system/libsam/include/twi.h
// parameters:
// 1=twi interfaces to send from/ 2=address of the device we are trying to reach/
// 3=internal address (of the arduino due)/ 4=size of the internal address/ 5=first byte
// of the buffer.
TWI_StartWrite(twi, txAddress, 0, 0, buf[0]);
// EnableIt enables the interrupts on the TWI interface. TWI_IER_TXCOMP is the event
// for when the sending is complete. This will call TWI0_Handler or TWI1_Handler depending
// which one we specify in the first parameter.
TWI_EnableIt(twi, TWI_IER_TXCOMP);
}
// Handler for the interrups on the TWI1 interface (SDA and SCL pins in Arduino Due)
// prints the status of the SR register (potentially you could detect errors, etc.)
// for reference on the TWI_SR look at the component_twi.h (path above)
void TWI1_Handler( ) {
Serial.println("done (TWI1)");
Serial.print("SR: "); Serial.println(twi->TWI_SR);
// disables the event until the next transfer
TWI_DisableIt(twi, TWI_IER_TXCOMP);
}
// Handler for the interrupts on the TWI1 interface (SDA and SCL pins in Arduino Due)
// prints the status of the SR register (potentially you could detect errors, etc.)
// for reference on the TWI_SR look at the component_twi.h (path above)
void TWI0_Handler( ) {
Serial.println("done (TWI0)");
Serial.print("SR: "); Serial.println(twi->TWI_SR);
// disables the event until the next transfer
TWI_DisableIt(twi, TWI_IER_TXCOMP);
}