cancel
Showing results for 
Search instead for 
Did you mean: 

Problem adding USART3 functionality to LwIP_HTTP_Server_Netconn_RTOS demo

debug
Associate III

Hi,

 

I'm having troubles to add USART communication to the LwIP_HTTP_Server_Netconn_RTOS demo from ST. I need to add communications using USART1 & USART3 to the example but have been stuck with HAL_UART_Transmit() returning 3. I came up with a little example to show the issue I'm facing:

Can someone help out?

I want to use pins PD8 for Tx and PD9 for Rx (STM32F439ZI) but have not found where this would be set and I suspect that PB10 & PB11 are being used by mistake (and may conflict with ETH_MII_RX & ETH_MII_TX).

Thanks!

int uart_test(void)
{
int rv = OK;
char cmd[ZABER_STR_LEN]={0};
enum zaber_dev dev = ZAB_BOTH;

huart3.Instance = USART3;
huart3.Init.BaudRate = 115200;
huart3.Init.WordLength = UART_WORDLENGTH_8B;
huart3.Init.StopBits = UART_STOPBITS_1;
huart3.Init.Parity = UART_PARITY_NONE;
huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; // No hardware flow control
huart3.Init.Mode = UART_MODE_TX_RX; // Full-duplex mode
if (HAL_UART_Init(&huart3) != HAL_OK)
{
Error_Handler();
}
if (dev==ZAB_BOTH)
sprintf(cmd, "/%s\n", ZABER_CMD_HOME);
else
sprintf(cmd, "/%d %s\n", dev, ZABER_CMD_HOME);

rv = HAL_UART_Transmit(&huart3, (uint8_t*)cmd, strlen(cmd), 1000);
return rv;
}

int main(void)
{
HAL_Init();
SystemClock_Config();
uart_test();

 

1 ACCEPTED SOLUTION

Accepted Solutions

Ok, so there's a stm32xyz_hal_msp.c file where it should enable clocks, and pins, associating the correct AF settings, etc.

/**
  * @brief UART MSP Initialization
  *        This function configures the hardware resources used in this example:
  *           - Peripheral's clock enable
  *           - Peripheral's GPIO Configuration
  * @PAram huart: UART handle pointer
  * @retval None
  */
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  if (huart == USARTx)// identify the one being configured/initialized
  {
    /*##-1- Enable peripherals and GPIO Clocks #################################*/
    /* Enable GPIO TX/RX clock */
    USARTx_TX_GPIO_CLK_ENABLE();
    USARTx_RX_GPIO_CLK_ENABLE();

    /* Enable USARTx clock */
    USARTx_CLK_ENABLE();

    /*##-2- Configure peripheral GPIO ##########################################*/
    /* UART TX GPIO pin configuration  */
    GPIO_InitStruct.Pin       = USARTx_TX_PIN;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = USARTx_TX_AF;

    HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);

    /* UART RX GPIO pin configuration  */
    GPIO_InitStruct.Pin = USARTx_RX_PIN;
    GPIO_InitStruct.Alternate = USARTx_RX_AF;

    HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
  }
}
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

8 REPLIES 8

HAL_TIMEOUT = 0x03U

Are you sure the clock is enabled, show the MSP code initializing the UART clocks and GPIOs

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
SofLit
ST Employee

Hello,

Most probably USART clock is not enabled.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

That was my first suspicion, too but my SystemClock_Config() sets up the divider for APB1 (which is routed to USART3), i.e. I suspect the problem is somewhere else:

static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;

/* Enable Power Control clock */
__HAL_RCC_PWR_CLK_ENABLE();

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

/* Enable HSE Oscillator and activate PLL with HSE as source */
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 4;//8;
RCC_OscInitStruct.PLL.PLLN = 168;//360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;//7;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
while(1) {};
}
/*
if(HAL_PWREx_EnableOverDrive() != HAL_OK)
{
while(1) {};
}*/

/* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
clocks dividers */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
{
while(1) {};
}
}

 

 

No. Apart of that you need to enable USART clock:

__HAL_RCC_USART3_CLK_ENABLE()
To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

Recommend clearing these auto/local variables for predictable operation

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};

The UART must have it's clock enabled in the APBx ENR register. CubeMX normally hides this in a call back in the MSP file, and you need to decode USART3 being initialized and supply the appropriate code.

Your code in the top-post is not where the issue lays

Use Error_Handler(__FILE__, __LINE__) methods over while(1) loops which will silently die

 

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

Okay Thanks everybody who has dropped in their comments, I've upgraded the test function (see below) but still can not measure any data on the line with my scope. I'm probing on pin PD8 and it measures 0V when the program is running, I'd expect it to be at 3.3V if it was configured to be USART3_TX, right?). I think I might be missing to start other clocks as well but which ones, the GPIO clocks are already being started:

 __HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();


The upgraded functions:

int uart_test(void)
{
int rv = OK;
char cmd[ZABER_STR_LEN]={0};
enum zaber_dev dev = ZAB_BOTH;


__HAL_RCC_USART3_CLK_ENABLE(); // enable USART3 clock


    huart3.Instance = USART3;
    huart3.Init.BaudRate = 115200;
    huart3.Init.WordLength = UART_WORDLENGTH_8B;
    huart3.Init.StopBits = UART_STOPBITS_1;
    huart3.Init.Parity = UART_PARITY_NONE;
    huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE; // No hardware flow control
    huart3.Init.Mode = UART_MODE_TX_RX; // Full-duplex mode
    if (HAL_UART_Init(&huart3) != HAL_OK)
    {
      Error_Handler();
    }


if (dev==ZAB_BOTH)
sprintf(cmd,  "/%s\n", ZABER_CMD_HOME);
else
sprintf(cmd, "/%d %s\n", dev, ZABER_CMD_HOME);


while(1) {
HAL_Delay(100);


rv = HAL_UART_Transmit(&huart3, (uint8_t*)cmd, strlen(cmd), 1000);
}


return rv;
}
I also initialize the clock declarations to 0 explicitly:
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 

Ok, so there's a stm32xyz_hal_msp.c file where it should enable clocks, and pins, associating the correct AF settings, etc.

/**
  * @brief UART MSP Initialization
  *        This function configures the hardware resources used in this example:
  *           - Peripheral's clock enable
  *           - Peripheral's GPIO Configuration
  * @PAram huart: UART handle pointer
  * @retval None
  */
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};

  if (huart == USARTx)// identify the one being configured/initialized
  {
    /*##-1- Enable peripherals and GPIO Clocks #################################*/
    /* Enable GPIO TX/RX clock */
    USARTx_TX_GPIO_CLK_ENABLE();
    USARTx_RX_GPIO_CLK_ENABLE();

    /* Enable USARTx clock */
    USARTx_CLK_ENABLE();

    /*##-2- Configure peripheral GPIO ##########################################*/
    /* UART TX GPIO pin configuration  */
    GPIO_InitStruct.Pin       = USARTx_TX_PIN;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = USARTx_TX_AF;

    HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);

    /* UART RX GPIO pin configuration  */
    GPIO_InitStruct.Pin = USARTx_RX_PIN;
    GPIO_InitStruct.Alternate = USARTx_RX_AF;

    HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
  }
}
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Hi,

 

Great, Thank you. I'm looking at a project where I have a working USART1 & USART3 and basically copied that file into the example workspace and that appears to have solved it.