I am trying to translate some code that was written for the UNO to my DUE. Almost everything is working fine, but I have very little understanding of assembler commands. So I thought maybe someone could help me to translate this little block of code to the Arduino Due?
This is regarding the SPI connection. I am trying to read from and write to the Wolfson 8737 ADC/DAC chip via SPI. The data I am trying to read/write are two 16bit words. The transmission looks like in the image I attached. So by pulling LRC (pin 10) high, I start the transmission from the chip. Then I'd like to read and write via SPI.
This is the working code for the UNO for this operation:
static inline void AudioCodec_data(int* _lin, int* _rin, int _lout, int _rout) {
int _out_temp = _lout;
PORTB |= (1<<PORTB2); // toggle ss pina
asm volatile ("out %0, %B1" : : "I" (_SFR_IO_ADDR(SPDR)), "r" (_out_temp) );
PORTB &= ~(1<<PORTB2); // toggle ss pin
while(!(SPSR & (1<<SPIF))){ // wait for data transfer to complete
}
asm volatile ("out %0, %A1" : : "I" (_SFR_IO_ADDR(SPDR)), "r" (_out_temp) );
asm volatile ("in r3, %0" : : "I" (_SFR_IO_ADDR(SPDR)) : "r3" );
_out_temp = _rout;
while(!(SPSR & (1<<SPIF))){ // wait for data transfer to complete
}
asm volatile ("out %0, %B1" : : "I" (_SFR_IO_ADDR(SPDR)), "r" (_out_temp) );
asm volatile ("in r2, %0" : : "I" (_SFR_IO_ADDR(SPDR)) : "r2" );
asm volatile ("movw %0, r2" : "=r" (*_lin) : : "r2", "r3" );
while(!(SPSR & (1<<SPIF))){ // wait for data transfer to complete
}
asm volatile ("out %0, %A1" : : "I" (_SFR_IO_ADDR(SPDR)), "r" (_out_temp) );
asm volatile ("in r3, %0" : : "I" (_SFR_IO_ADDR(SPDR)) : "r3" );
while(!(SPSR & (1<<SPIF))){ // wait for data transfer to complete
}
asm volatile ("in r2, %0" : : "I" (_SFR_IO_ADDR(SPDR)) : "r2" );
asm volatile ("movw %0, r2" : "=r" (*_rin) : : "r2", "r3" );
}
(source: http://www.openmusiclabs.com/)
And this is what I have for the Arduino DUE so far:
void TC3_Handler(){
TC_GetStatus(TC1, 0);
if(HIGH) g_APinDescription[10].pPort -> PIO_SODR = g_APinDescription[10].ulPin; //Setting LRC High
else g_APinDescription[10].pPort -> PIO_CODR = g_APinDescription[10].ulPin;
/// CODE MISSING
if(LOW) g_APinDescription[10].pPort -> PIO_SODR = g_APinDescription[10].ulPin; //Setting LRC LOW
else g_APinDescription[10].pPort -> PIO_CODR = g_APinDescription[10].ulPin;
/// CODE MISSING
}
Almost nothing.
I know the register to read from SPI would be REG_SPI0_RDR
(page 697, SAM3X/A datasheet), but how do I do this in assembler?
Most importantly I really need someone to help me to translate this (if this is even possible...). Thanks in advance!