2015-10-15 01:21 AM
I'd like some help narrowing down the origin of my problem withSTM32F030 and CubeMX generated code.
Just made some project using the newest version of CubeMX software, and it didn't work. I could simplify the code (see below) to focus on the problem. In short, the UART transfer isn't working for some reason. If I single-step throughHAL_UART_Transmit_IT() call, it works ok. However if I start in free-running mode, the code would never reach the while(1) loop. Instead, it would infinitely call error callback with huart->ErrorCode set to 4 (framing error) which is nonsense as it should not normally occur upon uart transmission I'm using Keil v.5 , optimization level set to 0. main codeint
main(
void
)
{
HAL_Init();
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
SystemClock_Config();
/* Configure the system clock */
MX_GPIO_Init();
/* Initialize all configured peripherals */
MX_USART1_UART_Init();
RxOK = 0;
TxOK = 0;
HAL_UART_Transmit_IT (&huart1, &TxByte[0], 1) ;
while
(1)
{
}
}
irq service routines
/*******************************************************/
void
SysTick_Handler(
void
)
{
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
}
/*******************************************************/
void
USART1_IRQHandler(
void
)
{
HAL_UART_IRQHandler(&huart1);
}
/*******************************************************/
void
HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)
{
TxOK = 1;
}
/*******************************************************/
void
HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)
{
RxOK = 1;
}
/*******************************************************/
void
HAL_UART_ErrorCallback(UART_HandleTypeDef *huart)
{
__NOP();
}
/*******************************************************/
init routines
/************************************************************************************************/
void
SystemClock_Config(
void
)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_PeriphCLKInitTypeDef PeriphClkInit;
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL4;
RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
}
/************************************************************************************************/
void
MX_USART1_UART_Init(
void
)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 4800;
huart1.Init.WordLength = UART_WORDLENGTH_9B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_EVEN;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT;
huart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
HAL_UART_Init(&huart1);
}
/************************************************************************************************/
void
MX_GPIO_Init(
void
)
{
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */
__GPIOF_CLK_ENABLE();
__GPIOA_CLK_ENABLE();
__GPIOB_CLK_ENABLE();
/*Configure GPIO pins : PF0 PF1 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
/*Configure GPIO pins : PA0 PA1 PA4 PA5
PA6 PA7 PA9 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5
|GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/*Configure GPIO pin : PB1 */
GPIO_InitStruct.Pin = GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
}
/************************************************************************************************/
#interrupt #cubemx-bug-report #stm32cubemx #uart #problem
2015-10-15 02:42 AM
p/s I could track the problem down to the autogenerated init code
The test project works with the following alternation to the uart init code (tx only rather than tx/rx) Apparently the alternation produce different settings though, so I'm still waiting for the solution of my problemvoid
MX_USART1_UART_Init(
void
)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 4800;
huart1.Init.WordLength = UART_WORDLENGTH_9B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_EVEN;
//huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.Mode = UART_MODE_TX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
huart1.Init.OneBitSampling = UART_ONEBIT_SAMPLING_DISABLED ;
huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT;
huart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
HAL_UART_Init(&huart1);
}
2015-10-15 04:42 AM
p/p/s
problem solved My application required swapping tx/rx pins For some reason, CubeMX didn't generate appropriate code. Rx pin , coupled wit transmitter circuitry, became pulled low, and never seen bus idle condition. This code corrected the issuehuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_TXINVERT_INIT | UART_ADVFEATURE_SWAP_INIT;
huart1.AdvancedInit.TxPinLevelInvert = UART_ADVFEATURE_TXINV_ENABLE;
huart1.AdvancedInit.Swap = UART_ADVFEATURE_SWAP_ENABLE;