Hello all,
I'm emulating a SPI interface with the Synchronous interface of the XC164CS32F.
Here is what I do: #1. init SPI (direction, baudrate, frame format) #2. init sensor (direction, cs low, high) #3. accessing a register of the sensor
I'm using already this emulated SPI interface to control a DAC and a ADC, both are working fine but, I have noticed that the flag SSC0_TIC_IR set previously at 0, is set to 1 as soon as I have written in the transmit register. Which proves to be a problem as the microcontroller should be waiting until all the data are transmitted. I have written then a
for
, but I don't like that. Example of what I'm seeing on oscilloscope: First signal = chip select Second signal = clock
----__------------------- ----_-_-_-_-_-_-_-_------
As you can see, the CS is set to 1 before the data are transmitted. Normally it should be blocked because of the flag Transmit Interrupt SSC0_TIC_IR.
Code for initialisation:
void spi_Init(void) { // SPI 8Bit SSC0_CON = (0<<EN)|(1<<MS)|(0<<AREN)|(0<<BEN)|(0<<PEN)|(0<<REN)|(0<<TEN)|(0<<LB)| (0<<PO)|(1<<PH)|(1<<HB)|(1<<BM3)|(1<<BM2)|(1<<BM1)|(1<<BM0); SSC0_CON_EN = 1; SSC0_BR = (F_CPU /(SPI_BAUDRATE * 2L) - 1); ALTSEL0P3_P8 = 1; ALTSEL0P3_P9 = 1; ALTSEL0P3_P13 = 1; DP3_P8 = 0; // MISO Input DP3_P9 = 1; // MOSI Output DP3_P13 = 1; // Clock Output P3_P8 = 0; P3_P9 = 1; P3_P13 = 1; }
Routine to write something on SPI interface:
void spi_Write(unsigned int uiData) { SSC0_TIC_IR = 0; SSC0_RIC_IR = 0; SSC0_TB = uiData; while(SSC0_TIC_IR == 0){__asm{nop;}}; }
Dummy code using write function:
void test(void) { CS = 0; spi_Write(ADDRESS_REG_X); CS = 1; }
------------------------------------------ A solution could be to implement a loop (with a for i.e.), but I think its definitely not elegant, and I would like not to depend on the speed of the SPI interface. Anyone has an idea why the flag SSC0_TIC_IR is set right away to 1?!
Thanks !
Renaud
Don't wait for the transmit interrupt request flag but for the receive interrupt request flag.
Sauli
Thanks a lot Sauli! It's working perfectly now :)
I should have read better the documentation I guess, stupid mistake that drove me crazy!
Best regards, Renaud.