2024-12-18 02:05 AM - edited 2024-12-18 02:34 AM
Hello everyone
I need to implement a protocol for an external ADC. An SPI read with 22 bits should be triggered via a timer in a 4us cycle.
My procedure:
The protocol on hardware side is working fine:
My problem now is, that the DMA half complete or complete callback is never called (I used HAL_SPI_RxHalfCpltCallback and HAL_SPI_RxCpltCallback and overwrote them in the main). I have already read through some forum posts, the SPI seems to be a real pain. At least the write is now working via a DMA request from the timer, can anyone help me to get the read working?
Here is my code (a bit shortened), the configuration is generated with MXCube:
#include "main.h"
#include "string.h"
SPI_HandleTypeDef hspi3;
DMA_HandleTypeDef hdma_spi3_rx;
TIM_HandleTypeDef htim2;
TIM_HandleTypeDef htim6;
DMA_HandleTypeDef hdma_tim2_up;
#define RX_BUF_LEN (250)
#define RX_BUF_HALF_LEN (RX_BUF_LEN >> 1)
static uint32_t tx_buffer;
static uint32_t rx_buffer[RX_BUF_LEN];
uint32_t adc_value;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_SPI3_Init(void);
static void MX_TIM6_Init(void);
static void MX_TIM2_Init(void);
int main(void)
{
memset(&tx_buffer, 0x00, sizeof(tx_buffer));
memset(&rx_buffer, 0x00, sizeof(rx_buffer));
memset(&adc_value, 0x00, sizeof(adc_value));
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_ETH_Init();
MX_USART3_UART_Init();
MX_USB_OTG_HS_USB_Init();
MX_SPI3_Init();
MX_TIM2_Init();
if (HAL_DMA_Init(&hdma_tim2_up) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_Base_Start(&htim2) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1)!= HAL_OK)
{
Error_Handler();
}
// Prepare SPI for DMA transmit / receive
tx_buffer = 0x0000;
hspi3.Instance->CR2 = 0x0000; // Enable "auto-send"
hspi3.Instance->CR1 = (0x0000 | (1<<12) | (1<<0)); // Enable SPI, Set CS
hspi3.Instance->CR1 = (0x0000 | (1<<12) | (1<<9) | (1<<0)); // Enable CSTART, Set CS
htim2.Instance->CCR1 = (htim2.Init.Period - 14); // NCS / CVST should go high aprox. 50 ns before read on spi
if( HAL_SPI_Receive_DMA(&hspi3, (uint8_t*)rx_buffer, RX_BUF_LEN) != HAL_OK)
{
Error_Handler();
}
if (HAL_DMA_Start(&hdma_tim2_up, (uint32_t)&tx_buffer, (uint32_t)&hspi3.Instance->TXDR, 1) != HAL_OK)
{
Error_Handler();
}
__HAL_TIM_ENABLE_DMA(&htim2, TIM_DIER_UDE);
HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET);
while (1)
{
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void);
// deleted for post
/**
* @brief SPI3 Initialization Function
* None
* @retval None
*/
static void MX_SPI3_Init(void)
{
hspi3.Instance = SPI3;
hspi3.Init.Mode = SPI_MODE_MASTER;
hspi3.Init.Direction = SPI_DIRECTION_2LINES;
hspi3.Init.DataSize = SPI_DATASIZE_22BIT;
hspi3.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi3.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi3.Init.NSS = SPI_NSS_SOFT;
hspi3.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
hspi3.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi3.Init.TIMode = SPI_TIMODE_DISABLE;
hspi3.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi3.Init.CRCPolynomial = 0x0;
hspi3.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
hspi3.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
hspi3.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
hspi3.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi3.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi3.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_05CYCLE;
hspi3.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
hspi3.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
hspi3.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_ENABLE;
hspi3.Init.IOSwap = SPI_IO_SWAP_DISABLE;
if (HAL_SPI_Init(&hspi3) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief TIM2 Initialization Function
* None
* @retval None
*/
static void MX_TIM2_Init(void)
{
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim2.Instance = TIM2;
htim2.Init.Prescaler = 1-1;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 1375-1;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_PWM_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0xFFFFFFFF;
sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim2);
}
/**
* Enable DMA controller clock
*/
static void MX_DMA_Init(void)
{
/* DMA controller clock enable */
__HAL_RCC_DMA1_CLK_ENABLE();
}
/**
* @brief GPIO Initialization Function
* None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* USER CODE BEGIN MX_GPIO_Init_1 */
/* USER CODE END MX_GPIO_Init_1 */
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, LED_GREEN_Pin|LED_RED_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(USB_FS_PWR_EN_GPIO_Port, USB_FS_PWR_EN_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(LED_YELLOW_GPIO_Port, LED_YELLOW_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : B1_Pin */
GPIO_InitStruct.Pin = B1_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : LED_GREEN_Pin LED_RED_Pin */
GPIO_InitStruct.Pin = LED_GREEN_Pin|LED_RED_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/*Configure GPIO pin : USB_FS_PWR_EN_Pin */
GPIO_InitStruct.Pin = USB_FS_PWR_EN_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(USB_FS_PWR_EN_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : USB_FS_OVCR_Pin */
GPIO_InitStruct.Pin = USB_FS_OVCR_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(USB_FS_OVCR_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : USB_FS_VBUS_Pin */
GPIO_InitStruct.Pin = USB_FS_VBUS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(USB_FS_VBUS_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : USB_FS_ID_Pin */
GPIO_InitStruct.Pin = USB_FS_ID_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF10_OTG1_HS;
HAL_GPIO_Init(USB_FS_ID_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : LED_YELLOW_Pin */
GPIO_InitStruct.Pin = LED_YELLOW_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(LED_YELLOW_GPIO_Port, &GPIO_InitStruct);
/* USER CODE BEGIN MX_GPIO_Init_2 */
/* USER CODE END MX_GPIO_Init_2 */
}
void HAL_SPI_RxHalfCpltCallback(SPI_HandleTypeDef *hspi)
{
uint32_t mean = 0;
for(uint16_t i=0; i < RX_BUF_HALF_LEN; i++)
{
mean += ( (rx_buffer[i] & 0x003FFFFF) >> 6);
}
adc_value = mean / ((uint32_t)RX_BUF_HALF_LEN);
return;
}
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
uint32_t mean = 0;
for(uint16_t i=RX_BUF_HALF_LEN; i < RX_BUF_LEN; i++)
{
mean += ( (rx_buffer[i] & 0x003FFFFF) >> 6);
}
adc_value = mean / ((uint32_t)RX_BUF_HALF_LEN);
return;
}
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
{
Error_Handler();
}
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
HAL_GPIO_WritePin(LED_RED_GPIO_Port, LED_RED_Pin, GPIO_PIN_SET);
__disable_irq();
while (1)
{
}
}
The second thing i tried is:
int main(void)
{
...
tx_buffer = 0x0000;
hspi3.Instance->CR2 = 0x0000; // Enable "auto-send"
hspi3.Instance->CR1 = (0x0000 | (1<<12) | (1<<0)); // Enable SPI, Set CS
hspi3.Instance->CR1 = (0x0000 | (1<<12) | (1<<9) | (1<<0)); // Enable CSTART, Set CS
htim2.Instance->CCR1 = (htim2.Init.Period - 14); // NCS / CVST should go high aprox. 50 ns before read on spi
if( HAL_DMA_RegisterCallback(&hdma_spi3_rx, HAL_DMA_XFER_HALFCPLT_CB_ID, dma_half_complete_callback) )
{
Error_Handler();
}
if( HAL_DMA_RegisterCallback(&hdma_spi3_rx, HAL_DMA_XFER_CPLT_CB_ID, dma_complete_callback) )
{
Error_Handler();
}
if( HAL_DMA_Start_IT(&hdma_spi3_rx, (uint32_t)&hspi3.Instance->RXDR, (uint32_t)&rx_buffer, RX_BUF_LEN))
{
Error_Handler();
}
if (HAL_DMA_Start(&hdma_tim2_up, (uint32_t)&tx_buffer, (uint32_t)&hspi3.Instance->TXDR, 1) != HAL_OK)
{
Error_Handler();
}
__HAL_TIM_ENABLE_DMA(&htim2, TIM_DIER_UDE);
__HAL_SPI_ENABLE_IT(&hspi3, (SPI_IT_OVR | SPI_IT_FRE | SPI_IT_MODF | SPI_IT_RXP));
HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_SET);
while (1)
{
}
}
...
void dma_half_complete_callback(DMA_HandleTypeDef *dmaHandle)
{
uint32_t mean = 0;
for(uint16_t i=0; i < RX_BUF_HALF_LEN; i++)
{
mean += ( (rx_buffer[i] & 0x003FFFFF) >> 6);
}
adc_value = mean / ((uint32_t)RX_BUF_HALF_LEN);
return;
}
void dma_complete_callback(DMA_HandleTypeDef *dmaHandle)
{
uint32_t mean = 0;
for(uint16_t i=RX_BUF_HALF_LEN; i < RX_BUF_LEN; i++)
{
mean += ( (rx_buffer[i] & 0x003FFFFF) >> 6);
}
adc_value = mean / ((uint32_t)RX_BUF_HALF_LEN);
return;
}
But this didn't work either.