cancel
Showing results for 
Search instead for 
Did you mean: 

Serial communication via UART on SMT32L475 Discovery kit IoT

DNeet.1
Associate II

on the SMT32L475 Discovery kit IoT, I would like to implement the following commands from Arduino:

Serial1.begin(BPS, SERIAL_7N2);
Serial1.print(msg)
 
while (Serial1.available())
{
    t = Serial1.read();
    wmsg[ct] = t;
    ct++;
 
    *bufflen = ct;
}

According to the pinout, the pins PA0&PA1 are connected to Tx&Rx resp.That corresponsds to UART4. So I tried doing this with:

MX_USART4_UART_Init();
HAL_UART_Transmit(&huart4, msg, sizeof(msg), 1000);
huart4_status = HAL_UART_Receive(&huart4, pData, sizeof(pData), 1000);

But nothing is transmitted on the Tx/Rx wires, and nothing comes back.

How can I do Tx/Rx comms with an external board from the SMT32L475 Discovery kit IoT?

4 REPLIES 4

STM !!!

#include "stm32l4xx_hal.h"
 
//****************************************************************************
 
UART_HandleTypeDef huart4 = {0};
 
//****************************************************************************
 
void UART_Init(uint32_t BaudRate)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  __UART4_CLK_ENABLE();
  __GPIOA_CLK_ENABLE();
 
  /* UART RX/TX GPIO pin configuration  */
  GPIO_InitStruct.Pin       = GPIO_PIN_0 | GPIO_PIN_1; // D1/D0 Arduino PA0:AF8 UART4_TX,PA1:AF8 UART4_RX
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  /*## Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART1 configured as follow:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = NO parity
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance        = UART4;
 
  UartHandle.Init.BaudRate   = BaudRate;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
 
  UartHandle.Init.OverSampling   = UART_OVERSAMPLING_16;
#ifdef UART_ONE_BIT_SAMPLE_DISABLE
  UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
#endif
 
  if (HAL_UART_Init(&huart4) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
}
 
//******************************************************************************
 
void Error_Handler(void)
{
  while(1);
}
 
//****************************************************************************
 
void OutString(char *s)
{
  HAL_UART_Transmit(&huart4, s, strlen(s), 1000);
}
 
//****************************************************************************
 
void main(void)
{
  UART_Init();
 
  while(1)
    OutString("Hello World\r\n");
}
 
//****************************************************************************

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
DNeet.1
Associate II

Thx for answering, clive. I am getting the follwing errors:

HAL_UART_Transmit(&huart4, s, strlen(s), 1000);
 
invalid conversion from 'char*' to 'uint8_t* {aka unsigned char*}' [-fpermissive]
UartHandle.Instance        = UART4;
 
'UartHandle' was not declared in this scope

Is this the right way to receive data?

huart4_status = HAL_UART_Receive(&huart4, pData, sizeof(pData), 1000);

#include "stm32l4xx_hal.h"
 
//****************************************************************************
 
UART_HandleTypeDef UartHandle = {0};
 
//****************************************************************************
 
void UART_Init(uint32_t BaudRate)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  __UART4_CLK_ENABLE();
  __GPIOA_CLK_ENABLE();
 
  /* UART RX/TX GPIO pin configuration  */
  GPIO_InitStruct.Pin       = GPIO_PIN_0 | GPIO_PIN_1; // D1/D0 Arduino PA0:AF8 UART4_TX,PA1:AF8 UART4_RX
  GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull      = GPIO_PULLUP;
  GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
 
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
  /*## Configure the UART peripheral ######################################*/
  /* Put the USART peripheral in the Asynchronous mode (UART Mode) */
  /* UART1 configured as follow:
      - Word Length = 8 Bits
      - Stop Bit = One Stop bit
      - Parity = NO parity
      - BaudRate = 9600 baud
      - Hardware flow control disabled (RTS and CTS signals) */
  UartHandle.Instance        = UART4;
 
  UartHandle.Init.BaudRate   = BaudRate;
  UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  UartHandle.Init.StopBits   = UART_STOPBITS_1;
  UartHandle.Init.Parity     = UART_PARITY_NONE;
  UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;
  UartHandle.Init.Mode       = UART_MODE_TX_RX;
 
  UartHandle.Init.OverSampling   = UART_OVERSAMPLING_16;
#ifdef UART_ONE_BIT_SAMPLE_DISABLE
  UartHandle.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
#endif
 
  if (HAL_UART_Init(&UartHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
}
 
//******************************************************************************
 
void Error_Handler(void)
{
  while(1);
}
 
//****************************************************************************
 
void OutString(char *s)
{
  HAL_UART_Transmit(&UartHandle, (uint8_t *)s, strlen(s), 1000);
}
 
//****************************************************************************
 
void main(void)
{
  UART_Init();
 
  while(1)
    OutString("Hello World\r\n");
}
 
//****************************************************************************

Little too quick on the blind coding.

>> huart4_status = HAL_UART_Receive(&huart4, pData, sizeof(pData), 1000);

Yes, it will however wait for all characters to be received, or timeout, for it to complete. Might also error if the framing is wrong, or there is line noise.

It will also not pull data retrospectively, so only data after the call, or still in-flight.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
DNeet.1
Associate II

huart4_status is returning HAL_TIMEOUT:

huart4_status = HAL_UART_Receive(&huart4, pData, WYLER_MAX_MSG_SIZE, 1000);

Yes I think the blocking nature of this function HAL_UART_Receive makes it unsuitable for my usage. In Arduino I had the following code which worked perfectly. But I do not know how to implement this on the STM32 Discovery kit IoT node:

        char t;
        int ct = 0;
        memset(wmsg, 0, WYLER_MAX_MSG_SIZE);
        debugPrint("collectResponse:wmsg:", wmsg, this->VERBOSE);
 
        while (Serial1.available())
        {
            t = Serial1.read();
            wmsg[ct] = t;
            ct++;
 
            *bufflen = ct;
        }

Could you pls give me an idea where to start? Is it possible to use the HAL UART code generated by MX in a non-blocking way, or do I need another class like the mbed.os Serial class?

Another thing is I still do not know whether the Serial commands I am sending to the external device are actually going to the device or not. How can I read the UART4 on which I am sending the commands?