2015-02-14 04:46 AM
What is the best way to implement chip select in software? I've tried doing it this way:
HAL_GPIO_WritePin(NSS_PORT, NSS_PIN, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1, out , 1); HAL_GPIO_WritePin(NSS_PORT, NSS_PIN, GPIO_PIN_SET); But the problem is that HAL_SPI_Transmit() exits before data is actually physically sent. I've also tried using interrupts:HAL_GPIO_WritePin(NSS_PORT, NSS_PIN, GPIO_PIN_RESET);
HAL_SPI_Transmit_IT(&hspi1, out , 1);static
void SPI_TxISR_8BIT( struct __SPI_HandleTypeDef *hspi) { *(__IO uint8_t *)&hspi->Instance->DR = (*hspi->pTxBuffPtr++); hspi->TxXferCount--; if (hspi->TxXferCount == 0) { if (hspi->Init.CRCCalculation == SPI_CRCCALCULATION_ENABLED) { /* Enable CRC Transmission */ hspi->Instance->CR1 |= SPI_CR1_CRCNEXT; } SPI_CloseTx_ISR(hspi); HAL_GPIO_WritePin(NSS_PORT, NSS_PIN, GPIO_PIN_SET); } } But it has the same problem - interrupt occurs way before data is actually sent. Another downside here is that I'm modifying a library file, something I probably should be doing. #interrups #stm32 #spi #nss2015-02-14 06:21 AM
I've narrowed the problem down somewhat. As it turns out, the first method is appropriate in all cases except where the number of bytes being transmitted is no bigger than 1 (rarely happens when sending 2). In that particular problematic case one obvious solution is to use a delay function.
2015-02-17 08:21 AM
Yeah, nevermind that, I don't think that the message length has any correlation with HAL_SPI_Transmit() prematurely ending.
Here's whats happening: Yellow line is hardware SPI_CLK, Red line is software SPI_NSS. Code is basically this:HAL_GPIO_WritePin(M25P80_NSS_PORT, M25P80_NSS_PIN, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi1,
out
, 4, TIMEOUT);
HAL_GPIO_WritePin(M25P80_NSS_PORT, M25P80_NSS_PIN, GPIO_PIN_SET);
2015-02-23 07:24 AM
Still wondering about robust ways of implementing software chips select.
2015-02-23 09:30 AM
2015-02-23 10:00 AM
What about using HAL_SPI_TransmitReceive, even if you do not care about the reception. The nice thing about reception is that when the last character arrives, then you are sure that the transmission is done.