/* Includes ------------------------------------------------------------------*/
#include "spi.h"
/* USER CODE BEGIN 0 */
uint16_t sbuffer_tx[1];
uint8_t sbuffer_rx[SPI2_PAYLOAD_BYTES];
uint8_t adc1Value[3] = {0, 0, 0};
uint8_t adc2Value[3] = {0, 0, 0};
/* USER CODE END 0 */
SPI_HandleTypeDef hspi2;
DMA_HandleTypeDef hdma_spi2_rx;
DMA_HandleTypeDef hdma_spi2_tx;
/* SPI2 init function */
void MX_SPI2_Init(void)
{
/* USER CODE BEGIN SPI2_Init 0 */
/* USER CODE END SPI2_Init 0 */
/* USER CODE BEGIN SPI2_Init 1 */
/* USER CODE END SPI2_Init 1 */
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_SLAVE;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_HARD_INPUT;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 7;
hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI2_Init 2 */
/* USER CODE END SPI2_Init 2 */
}
void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN SPI2_MspInit 0 */
/* USER CODE END SPI2_MspInit 0 */
/* SPI2 clock enable */
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/**SPI2 GPIO Configuration
PB12 ------> SPI2_NSS
PB13 ------> SPI2_SCK
PB14 ------> SPI2_MISO
PB15 ------> SPI2_MOSI
*/
GPIO_InitStruct.Pin = SPI_Slave_CS_Pin|SPI_Slave_SCLK_Pin|SPI_Slave_MISO_Pin|SPI_Slave_MOSI_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* SPI2 DMA Init */
/* SPI2_RX Init */
hdma_spi2_rx.Instance = DMA1_Channel3;
hdma_spi2_rx.Init.Request = DMA_REQUEST_SPI2_RX;
hdma_spi2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_spi2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi2_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi2_rx.Init.Mode = DMA_NORMAL;
hdma_spi2_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
if (HAL_DMA_Init(&hdma_spi2_rx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(spiHandle,hdmarx,hdma_spi2_rx);
/* SPI2_TX Init */
hdma_spi2_tx.Instance = DMA1_Channel4;
hdma_spi2_tx.Init.Request = DMA_REQUEST_SPI2_TX;
hdma_spi2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_spi2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_spi2_tx.Init.MemInc = DMA_MINC_ENABLE;
hdma_spi2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
hdma_spi2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
hdma_spi2_tx.Init.Mode = DMA_NORMAL;
hdma_spi2_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
if (HAL_DMA_Init(&hdma_spi2_tx) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(spiHandle,hdmatx,hdma_spi2_tx);
/* SPI2 interrupt Init */
HAL_NVIC_SetPriority(SPI2_IRQn, 4, 0);
HAL_NVIC_EnableIRQ(SPI2_IRQn);
/* USER CODE BEGIN SPI2_MspInit 1 */
/* USER CODE END SPI2_MspInit 1 */
}
}
void HAL_SPI_MspDeInit(SPI_HandleTypeDef* spiHandle)
{
/* USER CODE BEGIN SPI2_MspDeInit 0 */
/* USER CODE END SPI2_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_SPI2_CLK_DISABLE();
/**SPI2 GPIO Configuration
PB12 ------> SPI2_NSS
PB13 ------> SPI2_SCK
PB14 ------> SPI2_MISO
PB15 ------> SPI2_MOSI
*/
HAL_GPIO_DeInit(GPIOB, SPI_Slave_CS_Pin|SPI_Slave_SCLK_Pin|SPI_Slave_MISO_Pin|SPI_Slave_MOSI_Pin);
/* SPI2 DMA DeInit */
HAL_DMA_DeInit(spiHandle->hdmarx);
HAL_DMA_DeInit(spiHandle->hdmatx);
/* SPI2 interrupt Deinit */
HAL_NVIC_DisableIRQ(SPI2_IRQn);
/* USER CODE BEGIN SPI2_MspDeInit 1 */
/* USER CODE END SPI2_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* ----------------------------------------------------------------------
* ----------------------------------------------------------------------
* ----------------------------SPI2 - SLAVE------------------------------
* ----------------------------------------------------------------------
* ----------------------------------------------------------------------
* ----------------------------------------------------------------------
*/
/**
* @brief Initialize the SPI2 Communication
*/
void Init_SPI_Slave(void) {
// __HAL_RCC_SPI2_FORCE_RESET();
// __HAL_RCC_SPI2_RELEASE_RESET();
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
My_Error_Handler ("The SPI2 (Slave) Receive Communication did not Start.");
}
if (HAL_SPI_Receive_IT(&hspi2, sbuffer_rx, SPI2_HEADER_BYTES) != HAL_OK) {
// Handle error if SPI2 (Slave) receive communication fails.
My_Error_Handler("The SPI2 (Slave) Receive Communication did not Start.");
}
}
/**
* @brief CallBack function of HAL_SPI_TransmitReceive_DMA.
* Gets called at the end of the communication.
*
@PAram *hspi: Pointer to the SPI object.
*/
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
if (hspi->Instance == SPI2) {
// Start the 10 microsecond timer for interrupt
if (HAL_TIM_Base_Start_IT(&htim6) != HAL_OK)
{
My_Error_Handler("The 10 microsecond Timer did not Start.");
}
/* ----------------------------------------------------------------------
* ---------------------------- SPI - READ -------------------------------
* -----------------------------------------------------------------------
*/
hspi2.Init.DataSize = SPI_DATASIZE_16BIT;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
My_Error_Handler ("The SPI2 (Slave) Receive Communication did not Start.");
}
// If the master wants to read the ADC channel 1
if (sbuffer_rx[0] == ADC1_MAX << 2){
// Need to Lock & Disable the DMA
__HAL_LOCK(hspi->hdmatx);
__HAL_DMA_DISABLE(hspi->hdmatx);
sbuffer_tx[0]= ((adc1Value[2] << 8) | adc1Value[1]);
// Unlock & Enable the DMA
__HAL_DMA_ENABLE(hspi->hdmatx);
__HAL_UNLOCK(hspi->hdmatx);
if (HAL_SPI_Transmit_IT(hspi, (uint8_t*)(sbuffer_tx), 1) != HAL_OK) {
// Handle error if SPI2 (Slave) transmit communication fails.
My_Error_Handler("The SPI2 (Slave) Transmit Communication did not Start.");
}
// If the master wants to read the ADC channel 2
} else if (sbuffer_rx[0] == ADC2_MAX << 2){
// Need to Lock & Disable the DMA
__HAL_LOCK(hspi->hdmatx);
__HAL_DMA_DISABLE(hspi->hdmatx);
// Upload the TX buffer values
sbuffer_tx[0]= ((adc2Value[2] << 8) | adc2Value[1]);
// Unlock & Enable the DMA
__HAL_DMA_ENABLE(hspi->hdmatx);
__HAL_UNLOCK(hspi->hdmatx);
if (HAL_SPI_Transmit_IT(hspi, (uint8_t*)(sbuffer_tx), 1) != HAL_OK) {
// Handle error if SPI2 (Slave) transmit communication fails.
My_Error_Handler("The SPI2 (Slave) Transmit Communication did not Start.");
}
} else if (sbuffer_rx[0] == RISING_LS << 2){
// Need to Lock & Disable the DMA
__HAL_LOCK(hspi->hdmatx);
__HAL_DMA_DISABLE(hspi->hdmatx);
// Do stuff
sbuffer_tx[0] = ((0x00 << 8) | (absDeadtimeValue_LS & 0xFF));
if (deadtimeValue_LS < 0){
sbuffer_tx[0]= ((0x01 << 8) | (absDeadtimeValue_LS & 0xFF));
}
// Unlock & Enable the DMA
__HAL_DMA_ENABLE(hspi->hdmatx);
__HAL_UNLOCK(hspi->hdmatx);
if (HAL_SPI_Transmit_IT(hspi, (uint8_t*)(sbuffer_tx), 1) != HAL_OK){
My_Error_Handler("The SPI2 (Slave) Transmit Communication did not Start.");
}
} else if (sbuffer_rx[0] == RISING_HS << 2){
// Need to Lock & Disable the DMA
__HAL_LOCK(hspi->hdmatx);
__HAL_DMA_DISABLE(hspi->hdmatx);
// Do stuff
sbuffer_tx[0] = ((0x00 << 8) | (absDeadtimeValue_HS & 0xFF));
if (deadtimeValue_HS < 0){
sbuffer_tx[0]= ((0x01 << 8) | (absDeadtimeValue_HS & 0xFF));
}
// Unlock & Enable the DMA
__HAL_DMA_ENABLE(hspi->hdmatx);
__HAL_UNLOCK(hspi->hdmatx);
if (HAL_SPI_Transmit_IT(hspi, (uint8_t*)(sbuffer_tx), 1) != HAL_OK){
My_Error_Handler("The SPI2 (Slave) Transmit Communication did not Start.");
}
} else if (sbuffer_rx[0] == FALLING_LS << 2){
// Need to Lock & Disable the DMA
__HAL_LOCK(hspi->hdmatx);
__HAL_DMA_DISABLE(hspi->hdmatx);
// Do stuff
sbuffer_tx[0] = ((0x00 << 8) | (absDeadtimeFallingAdjust_LS & 0xFF));
if (deadtimeFallingAdjust_LS < 0){
sbuffer_tx[0]= ((0x01 << 8) | (absDeadtimeFallingAdjust_LS & 0xFF));
}
// Unlock & Enable the DMA
__HAL_DMA_ENABLE(hspi->hdmatx);
__HAL_UNLOCK(hspi->hdmatx);
if (HAL_SPI_Transmit_IT(hspi, (uint8_t*)(sbuffer_tx), 1) != HAL_OK){
My_Error_Handler("The SPI2 (Slave) Transmit Communication did not Start.");
}
} else if (sbuffer_rx[0] == FALLING_HS << 2){
// Need to Lock & Disable the DMA
__HAL_LOCK(hspi->hdmatx);
__HAL_DMA_DISABLE(hspi->hdmatx);
// Do stuff
sbuffer_tx[0] = ((0x00 << 8) | (absDeadtimeFallingAdjust_HS & 0xFF));
if (deadtimeFallingAdjust_HS < 0){
sbuffer_tx[0]= ((0x01 << 8) | (absDeadtimeFallingAdjust_HS & 0xFF));
}
// Unlock & Enable the DMA
__HAL_DMA_ENABLE(hspi->hdmatx);
__HAL_UNLOCK(hspi->hdmatx);
if (HAL_SPI_Transmit_IT(hspi, (uint8_t*)(sbuffer_tx), 1) != HAL_OK){
My_Error_Handler("The SPI2 (Slave) Transmit Communication did not Start.");
}
}
}
/**
* @brief CallBack function of HAL_SPI_TransmitReceive_DMA.
* Gets called at the end of each communication.
*
@PAram *hspi: Pointer to the SPI object.
*/
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
// If it is SPI2, re-launch the communication
if (hspi->Instance == SPI2) {
Init_SPI_Slave();
}
}
/**
* @brief CallBack function of SPI Error.
* Gets called at the end of each communication.
*
@PAram *hspi: Pointer to the SPI object.
*/
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi) {
// If it is SPI2, re-launch the communication
if (hspi->Instance == SPI2) {
Init_SPI_Slave();
}
}
/* USER CODE END 1 */