2012-07-11 07:36 AM
Hi all,
I have a simple routine called in TIMER_IRQ event that set GPIO and than set a DAC on SPI BUS. This routine must be quick as possible. Regarding the GPIO setting there is no particular problem, while SPI has some trouble: 1) Obviously this code doesn't work because CS is rising during SPI Trasmission
SPI1_CS1_LOW();
SPI_I2S_SendData(SPI1, DacValue);
SPI1_CS1_HIGH();
2) Using a simple delay make things working but the routine in not quick now
SPI1_CS1_LOW();
SPI_I2S_SendData(SPI1, DacValue);
int i = 100;
while(i--);
SPI1_CS1_HIGH();
3) I tried to use SPI IRQ but unfortunately the routine doesn't work. Could enyone give me some code sample to rise the CS after the SPI trasmission and without using delay cicle?
For your information below the code that doens't work:SPI1_CS1_LOW();
if (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) != RESET)
SPI_I2S_SendData(SPI1, DacValue);
SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE);
void SPI1_IRQHandler(void){
if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) != RESET){
// SPI_I2S_ClearITPendingBit(SPI1, SPI_I2S_IT_TXE);
if(SPI_I2S_GetFlagStatus(SPI1,SPI_I2S_FLAG_BSY) == RESET){
SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE);
SPI1_CS1_HIGH();
}
}
Thanks in advance
Luca Lombardini2012-07-11 07:43 AM
Wait on RXNE, it will assert after last bit has left the shift register, TXE indicates the holding register is empty, which occurs proximately when the holding register moves to the shift register, and the first bit leaves. You'll need to do a scratch read of the data register to clear RXNE.
2012-07-12 01:11 AM
Hi Clive,
thanks for your suggestion. I modified the code waiting for RXNE before to rise the CS and up to now seems that the routine working: Here my IRQ Handler code: void SPI1_IRQHandler(void){ if (State == 0){ SPI2_CS1_LOW(); if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_TXE) != RESET){ //!< Send byte through the SPI1 peripheral // SPI_I2S_SendData(SPI1, 67); SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, DISABLE); State = 1; } } if (State == 1){ if (SPI_I2S_GetITStatus(SPI1, SPI_I2S_IT_RXNE) != RESET){ SPI_I2S_ReceiveData(SPI1); State = 0; SPI2_CS1_HIGH(); } } } In the routine where I want to write on the DAC I simple enable the TXE IRQ and I write the value in the IRQ Routine. SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_TXE, ENABLE); many thanks Luca Lombardini