2022-02-07 08:04 AM
Hello
I'm using an STM32L431 and trying to enable character match on usart2
MXcube does not manage "MODBUS" modes and the st HAL layer doesn't seem to allow this setting (it allows modbus timout but not character match)
So I try to modify USART_CR2 ADD with character code... impossible !
RM0394 Reference manual tells me : " This bit field can only be written when reception is disabled (RE = 0) or the USART is disabled (UE=0)"
ok I even tested a modification before the MXcube inits
/* USER CODE BEGIN USART2_Init 0 */
huart2.Instance = USART2;
huart2.ReceptionType = HAL_UART_RECEPTION_TOCHARMATCH ;
MODIFY_REG(huart2.Instance->CR2, USART_CR2_ADD , (char)0x0D);
/* USER CODE END USART2_Init 0 */
/* USER CODE BEGIN USART2_Init 1 */
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 1200;
huart2.Init.WordLength = UART_WORDLENGTH_7B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
C1, C2... all is to 0 RE=0, UE= 0. I do not understand anything anymore
How to modify this register CR2?
How to activate the character match?
Thanks in advance
2022-02-07 08:17 AM
Not sure, never used "MODIFY_REG" or the character match feature before, but try
MODIFY_REG(huart2.Instance->CR2, USART_CR2_ADD , (char)0x0D);
after
HAL_UART_Init(&huart2)
The Init might reset and set register values according to the init struct settings.
2022-02-07 08:24 AM
Hello
I already tried
I also tried without this macro with
huart2.Instance->CR2 = 218103808 ;
The problem is not that my edit is overwritten; is that it is impossible for me to modify the CR2 register.
CR2 is always and always = 0
2022-02-07 08:32 AM
Peripheral clock enabled?
And better set register in hex, in decimal it's absolutely unreadable (I guess not only for me). But okay, 0 is 0.
2022-02-07 08:34 AM
And for many bits in CR2:
These bits can only be written when reception is disabled (RE = 0) or the USART is disabled
(UE = 0).
2022-02-07 08:54 AM
before init by MXcube: no clock on the uart and CR1 = 0 (RE =0 and UE =0)
After ini by MAXcube : clock on the uart but UE!=0
whith this it's the same
/* USER CODE END USART2_Init 1 */
huart2.Instance = USART2;
huart2.Init.BaudRate = 1200;
huart2.Init.WordLength = UART_WORDLENGTH_7B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART2_Init 2 */
__HAL_UART_DISABLE(&huart2);
huart2.Instance->CR2 = 0x000000D ;
__HAL_UART_ENABLE(&huart2);
/* USER CODE END USART2_Init 2 */
HAL_UART_DISABLE => UE = 0
Clock on
But no modification of CR2
Thanks
2022-02-07 10:26 AM
A discovery :
__HAL_UART_DISABLE(&huart2);
huart2.Instance->CR2 = 0x000000D ;
no modification of CR2
__HAL_UART_DISABLE(&huart2);
MODIFY_REG(huart2.Instance->CR2, USART_CR2_ADD , (char)0x0D);
no modification of CR2
but this does set all the bits of ADD to 1 :
__HAL_UART_DISABLE(&huart2);
SET_BIT(huart2.Instance->CR2, USART_CR2_ADD);
Can you please explain to me why this is?
2022-02-07 10:54 AM
for my discharge I have a fever...
huart2.Instance->CR2 = 0xD000000 ; is much better
MODIFY_REG(huart2.Instance->CR2, USART_CR2_ADD , 0xD000000); is much better too
Please excuse my stupidity