I need a hint on the SPI peripheral optimization. I am using a L6470 dSPIN device at the SPI bus attached to a STM32F0Discovery, which pretty much works. If I look at the lines, I see ugly stuff though and I want to get rid of that, maybe one has some advices for me. Please have a look at the following screenshot showing a 4byte long communication with a selected L6470. I see some quirks:
- - MOSI line flips around in between transmission of single bytes (looks the same on a scope)
- time between transmission of each byte takes very long
- timespan from sending byte done until ENABLE goes high is very long
The code looks like this
Code: Select all
/**
* SPI1 configuration structure.
* CPHA=0, CPOL=0, 8bits frames, MSb transmitted first.
*/
static SPIConfig dspin_spi_config = {
NULL,
/* HW dependent part.*/
DSPIN_CS_PORT,
DSPIN_CS_PIN,
SPI_CR1_BR_0 | SPI_CR1_BR_1
};
/**
* \brief Transmits/Receives one byte to/from dSPIN over SPI.
* \param byte to be transmitted
* \return Received byte
*/
/**
* Acquires SPI bus for further transmissions.\n
* Starts the SPI engine and selects a client -> dSPIN device
*/
void dSPIN_Acquire_Bus(void) {
spiStart(&SPID1, &dspin_spi_config);
spiSelect(&SPID1);
}
/**
* Releases SPI bus.
*/
void dSPIN_Release_Bus(void) {
spiUnselect(&SPID1);
spiStop(&SPID1);
}
uint8_t dSPIN_Write_Byte(uint8_t byte)
{
uint8_t spiBufferTx[1] = { 0 };
uint8_t spiBufferRx[1] = { 0 };
spiBufferTx[0] = byte;
dSPIN_Acquire_Bus();
spiExchange(&SPID1, 1, spiBufferTx, spiBufferRx);
dSPIN_Release_Bus();
return spiBufferRx[0];
}
int main(){
halInit();
chSysInit();
palSetPadMode( SPI_MISO_PORT, SPI_MISO_PIN,
PAL_MODE_ALTERNATE(0) | PAL_STM32_OSPEED_HIGHEST);
palSetPadMode( SPI_MOSI_PORT, SPI_MOSI_PIN,
PAL_MODE_ALTERNATE(0) | PAL_STM32_OSPEED_HIGHEST);
palSetPadMode( SPI_SCK_PORT, SPI_SCK_PIN,
PAL_MODE_ALTERNATE(0) | PAL_STM32_OSPEED_HIGHEST);
palSetPadMode( DSPIN_CS_PORT, DSPIN_CS_PIN,
PAL_MODE_OUTPUT_PUSHPULL | PAL_STM32_OSPEED_HIGHEST);
palSetPad( DSPIN_CS_PORT, DSPIN_CS_PIN);
spiObjectInit(&SPID1);
spiAcquireBus(&SPID1);
/* Send RUN operation code to dSPIN */
dSPIN_Write_Byte(dSPIN_Command_RUN | direction);
/* Send speed - byte 2 data dSPIN */
dSPIN_Write_Byte((uint8_t)(speed >> 16));
/* Send speed - byte 1 data dSPIN */
dSPIN_Write_Byte((uint8_t)(speed >> 8));
/* Send speed - byte 0 data dSPIN */
dSPIN_Write_Byte((uint8_t)(speed));
spiReleaseBus(&SPID1);
while(TRUE){
chThdSleepSeconds(2);
}
}
I tried to post as a attachement the conf headers, but nothing gets through. If desired I paste the content or upload the elsewhere.
I would be evry happy to optimize this behaviour and get to a datasheet-clean communication.
Thanks in any case!
Cheers
Stefan