/** * Transmit and Receive an amount of data in non-blocking mode with Interrupt. * * @param hspi pointer to a SPI_HandleTypeDef structure that contains * the configuration information for SPI module. * @param pTxData pointer to transmission data buffer * @param pRxData pointer to reception data buffer * @param Size amount of data to be sent and received * @retval HAL status */ HAL_StatusTypeDef HAL_SPI_TransmitReceive_IT_Custom(SPI_HandleTypeDef *hspi, uint8_t *pTxData, uint8_t *pRxData, uint16_t Size) { HAL_StatusTypeDef errorcode = HAL_OK; /* Process locked */ __HAL_LOCK(hspi); errorcode = HAL_SPI_Slave_Queue_TransmitReceive(hspi, pTxData, pRxData, Size); if (errorcode) { goto error; } /* Enable TXE, RXNE and ERR interrupt */ __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE | SPI_IT_RXNE | SPI_IT_ERR)); /* Check if the SPI is already enabled */ if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) { /* Enable SPI peripheral */ __HAL_SPI_ENABLE(hspi); } error: /* Process Unlocked */ __HAL_UNLOCK(hspi); return errorcode; }
/** * Transmit an amount of data in non-blocking mode with Interrupt. * * @param hspi pointer to a SPI_HandleTypeDef structure that contains * the configuration information for SPI module. * @param pData pointer to data buffer * @param Size amount of data to be sent * @retval HAL status */ HAL_StatusTypeDef HAL_SPI_Transmit_IT_Custom(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) { HAL_StatusTypeDef errorcode = HAL_OK; /* Process Locked */ __HAL_LOCK(hspi); errorcode = HAL_SPI_QueueTransmit(hspi, pData, Size); if (errorcode) { goto error; } /* Enable TXE interrupt */ __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE)); /* Check if the SPI is already enabled */ if ((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) { __HAL_SPI_ENABLE(hspi); } error : __HAL_UNLOCK(hspi); return errorcode; }
/** * @brief This function handles SPI1 global interrupt. */ void SPI1_IRQHandler(void) { __HAL_SPI_DISABLE_IT(&hspi1, SPI_IT_RXNE); HAL_SPI_Receive(&hspi1, &SPI_rx_buf[0], 3, 100); __HAL_SPI_ENABLE_IT(&hspi1, SPI_IT_RXNE); }
/* * GPIO interrupt when slave gets selected/deselected. */ static void spi_ss_isr(void *arg) { struct stm32_hal_spi *spi = (struct stm32_hal_spi *)arg; int ss; int len; uint16_t reg; spi_stat.ss_irq++; ss = hal_gpio_read(spi->cfg->ss_pin); if (ss == 0 && !spi->selected) { /* * We're now seeing chip select. Enable SPI, SPI interrupts. */ if (spi->tx_in_prog) { __HAL_SPI_ENABLE_IT(&spi->handle, SPI_IT_RXNE | SPI_IT_TXE | SPI_IT_ERR); } else { __HAL_SPI_ENABLE_IT(&spi->handle, SPI_IT_TXE | SPI_IT_ERR); } reg = spi->handle.Instance->CR1; reg &= ~SPI_CR1_SSI; reg |= SPI_CR1_SPE; spi->handle.Instance->CR1 = reg; spi->selected = 1; } if (ss == 1 && spi->selected) { /* * Chip select done. Check whether there's pending data to RX. */ if (spi->handle.Instance->SR & SPI_SR_RXNE && spi->handle.RxISR) { spi->handle.RxISR(&spi->handle); } /* * Disable SPI. */ reg = spi->handle.Instance->CR1; reg &= ~SPI_CR1_SPE; reg |= SPI_CR1_SSI; spi->handle.Instance->CR1 = reg; __HAL_SPI_DISABLE_IT(&spi->handle, SPI_IT_RXNE|SPI_IT_TXE|SPI_IT_ERR); len = spi->handle.RxXferSize - spi->handle.RxXferCount; if (len) { /* * If some data was clocked out, reset to start sending * default data and call callback, if user was waiting for * data. */ spi->handle.State = HAL_SPI_STATE_READY; HAL_SPI_QueueTransmit(&spi->handle, spi->def_char, 2); if (spi->tx_in_prog) { spi->tx_in_prog = 0; if (spi->txrx_cb_func) { spi->txrx_cb_func(spi->txrx_cb_arg, len); } } } spi->selected = 0; } }
HAL_StatusTypeDef HAL_SPI_QueueTransmit(SPI_HandleTypeDef *hspi, uint8_t *pData, uint16_t Size) { HAL_StatusTypeDef errorcode = HAL_OK; if((pData == NULL) || (Size == 0)) { errorcode = HAL_ERROR; goto error; } if(hspi->State != HAL_SPI_STATE_READY) { errorcode = HAL_BUSY; goto error; } /* Set the transaction information */ hspi->State = HAL_SPI_STATE_BUSY_TX; hspi->ErrorCode = HAL_SPI_ERROR_NONE; hspi->pTxBuffPtr = (uint8_t *)pData; hspi->TxXferSize = Size; hspi->TxXferCount = Size; /* Init field not used in handle to zero */ hspi->pRxBuffPtr = (uint8_t *)NULL; hspi->RxXferSize = 0U; hspi->RxXferCount = 0U; hspi->RxISR = NULL; /* Set the function for IT treatment */ if(hspi->Init.DataSize > SPI_DATASIZE_8BIT ) { hspi->TxISR = SPI_TxISR_16BIT; } else { hspi->TxISR = SPI_TxISR_8BIT; } #if 0 /* MYNEWT: TODO */ /* Enable TXE interrupt */ __HAL_SPI_ENABLE_IT(hspi, (SPI_IT_TXE)); #endif /* MYNEWT: in slave mode write 1st byte to DR */ if ((hspi->Instance->CR1 & SPI_CR1_MSTR) == 0) { hspi->TxISR(hspi); } #if 0 /* MYNEWT: TODO */ /* Check if the SPI is already enabled */ if((hspi->Instance->CR1 &SPI_CR1_SPE) != SPI_CR1_SPE) { /* Enable SPI peripheral */ __HAL_SPI_ENABLE(hspi); } #endif error : return errorcode; }