Skip to main content
Visitor II
February 7, 2025
Question

STM32F767 - HAL_UART_Transmit() is not printing the entire data on the hyperterminal

  • February 7, 2025
  • 2 replies
  • 1059 views

Hello there,

I have created my own example to print the data on HyperTerminal (putty) following the already given example "UART_Printf" but mine is only printing the first char of the data and then stuck forever. 

 

Here is the clock config code 

Sam4_0-1738947124905.png

and uart config:

Sam4_2-1738947206126.png

 

and what I would like to print:

Sam4_1-1738947174654.png

let me know if you need more info.

 

Thanks,

    This topic has been closed for replies.

    2 replies

    Graduate II
    February 7, 2025

    Please use the code pasting tool, see icon </>  rather than in-lining screen shots of code

    Not sure what "message" is defined at, nor how the pins are configured, etc.

    The fragment shown should be workable, perhaps look elsewhere for the actual issue.

    Like the MSP code, or where ever you enable the clocks and pins, and peripheral, etc.

    Is this a custom board?

    Does it work if you just run with the default clock?

    Sam4Author
    Visitor II
    February 7, 2025
    /**
     ******************************************************************************
     * @file Templates/Src/main.c 
     * @author MCD Application Team
     * @brief Main program body
     ******************************************************************************
     * @attention
     *
     * Copyright (c) 2016 STMicroelectronics.
     * All rights reserved.
     *
     * This software is licensed under terms that can be found in the LICENSE file
     * in the root directory of this software component.
     * If no LICENSE file comes with this software, it is provided AS-IS.
     *
     ******************************************************************************
     */
    
    /* Includes ------------------------------------------------------------------*/
    #include "main.h"
    
    
    #ifdef __GNUC__
    #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
    #endif
    
    
    
    ADC_HandleTypeDef	adc_handle;
    ADC_ChannelConfTypeDef	adc_channel_conf;
    __IO uint16_t adc_val = 0;
    __IO float temp_cel = 0.0;
    
    UART_HandleTypeDef uart_handle;
    
    
    /** @addtogroup STM32F7xx_HAL_Examples
     * @{
     */
    
    /** @addtogroup Templates
     * @{
     */
    
    /* Private typedef -----------------------------------------------------------*/
    /* Private define ------------------------------------------------------------*/
    /* Private macro -------------------------------------------------------------*/
    /* Private variables ---------------------------------------------------------*/
    /* Private function prototypes -----------------------------------------------*/
    static void MPU_Config(void);
    static void SystemClock_Config(void);
    static void Error_Handler(void);
    static void CPU_CACHE_Enable(void);
    
    static void init_adc(void);
    static void init_adc_channel(void);
    static void init_uart(void);
    
    float read_temperature(ADC_HandleTypeDef *adc_handle);
    
    /* Private functions ---------------------------------------------------------*/
    
    /**
     * @brief Main program
     * @param None
     * @retval None
     */
    int main(void)
    {
     /* This project template calls firstly CPU_CACHE_Enable() in order to enable the CPU Cache.
     This function is provided as template implementation that User may integrate 
     in his application, to enhance the performance in case of use of AXI interface 
     with several masters. */
    
     /* Configure the MPU attributes */
     MPU_Config();
    
     /* Enable the CPU Cache */
     CPU_CACHE_Enable();
    
     /* STM32F7xx HAL library initialization:
     - Configure the Flash ART accelerator on ITCM interface
     - Configure the Systick to generate an interrupt each 1 msec
     - Set NVIC Group Priority to 4
     - Low Level Initialization
     */
     HAL_Init();
    
     /* Configure the system clock to 216 MHz */
     SystemClock_Config();
    
    
     /* Add your application code here */
     init_uart();
     init_adc();
     init_adc_channel();
    
     //start the conversion process
     if(HAL_ADC_Start_IT(&adc_handle) != HAL_OK)
     {
    	 Error_Handler();
     }
    
     /* Infinite loop */
     uint8_t message[] = "UART!\r\n";
    
     while (1)
     {
    	 //temp_cel = read_temperature(&adc_handle);
    	 //snprintf(data, 30, "\n\r temperature: %f\n\r", temp_cel);
    	 HAL_UART_Transmit(&uart_handle, message, sizeof(message) - 1, HAL_MAX_DELAY);
    
     }
    }
    
    
    PUTCHAR_PROTOTYPE
    {
    	HAL_UART_Transmit(&uart_handle, (uint8_t *)&ch, 1, 0xFFFF);
    
    	return ch;
    }
    /**
     * @brief System Clock Configuration
     * The system Clock is configured as follow : 
     * System Clock source = PLL (HSE)
     * SYSCLK(Hz) = 216000000
     * HCLK(Hz) = 216000000
     * AHB Prescaler = 1
     * APB1 Prescaler = 4
     * APB2 Prescaler = 2
     * HSE Frequency(Hz) = 8000000
     * PLL_M = 8
     * PLL_N = 432
     * PLL_P = 2
     * PLL_Q = 9
     * PLL_R = 7
     * VDD(V) = 3.3
     * Main regulator output voltage = Scale1 mode
     * Flash Latency(WS) = 7
     * @param None
     * @retval None
     */
    void SystemClock_Config(void)
    {
     RCC_ClkInitTypeDef RCC_ClkInitStruct;
     RCC_OscInitTypeDef RCC_OscInitStruct;
    
     /* Enable HSE Oscillator and activate PLL with HSE as source */
     RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
     RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
     RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
     RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
     RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
     RCC_OscInitStruct.PLL.PLLM = 8;
     RCC_OscInitStruct.PLL.PLLN = 432; 
     RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
     RCC_OscInitStruct.PLL.PLLQ = 9;
     RCC_OscInitStruct.PLL.PLLR = 7;
     if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
     {
     while(1) {};
     }
     
     /* Activate the OverDrive to reach the 216 Mhz Frequency */
     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_7) != HAL_OK)
     {
     while(1) {};
     }
    }
    
    /**
     * @brief This function is executed in case of error occurrence.
     * @param None
     * @retval None
     */
    static void Error_Handler(void)
    {
     /* User may add here some code to deal with this error */
     while(1)
     {
     }
    }
    
    /**
     * @brief CPU L1-Cache enable.
     * @param None
     * @retval None
     */
    static void CPU_CACHE_Enable(void)
    {
     /* Enable I-Cache */
     SCB_EnableICache();
    
     /* Enable D-Cache */
     SCB_EnableDCache();
    }
    
    
    /**
     * @brief Configure the MPU attributes
     * @param None
     * @retval None
     */
    static void MPU_Config(void)
    {
     MPU_Region_InitTypeDef MPU_InitStruct;
    
     /* Disable the MPU */
     HAL_MPU_Disable();
    
     /* Configure the MPU as Strongly ordered for not defined regions */
     MPU_InitStruct.Enable = MPU_REGION_ENABLE;
     MPU_InitStruct.BaseAddress = 0x00;
     MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
     MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
     MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
     MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
     MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
     MPU_InitStruct.Number = MPU_REGION_NUMBER0;
     MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
     MPU_InitStruct.SubRegionDisable = 0x87;
     MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
    
     HAL_MPU_ConfigRegion(&MPU_InitStruct);
    
     /* Enable the MPU */
     HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
    }
    
    static void init_adc(void)
    {
    	adc_handle.Instance = ADCx;
    	if(HAL_ADC_DeInit(&adc_handle) != HAL_OK)
    	{
    		Error_Handler();
    	}
    	adc_handle.Init.Resolution = ADC_RESOLUTION_12B;
    	adc_handle.Init.ContinuousConvMode = ENABLE;
    	adc_handle.Init.DiscontinuousConvMode = DISABLE;
    	adc_handle.Init.NbrOfDiscConversion = 0;
    	adc_handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
    	adc_handle.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T1_CC1;
    	adc_handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
    	adc_handle.Init.NbrOfConversion = 1;
    	adc_handle.Init.ClockPrescaler = ADC_CLOCKPRESCALER_PCLK_DIV4;
    	adc_handle.Init.ScanConvMode = DISABLE;
    	adc_handle.Init.DMAContinuousRequests = DISABLE;
    	adc_handle.Init.EOCSelection = DISABLE;
    
    	if(HAL_ADC_Init(&adc_handle) != HAL_OK)
    	{
    		Error_Handler();
    	}
    
    }
    
    static void init_adc_channel(void)
    {
    	adc_channel_conf.Channel = ADC_CHANNEL_TEMPSENSOR;
    	adc_channel_conf.Rank = 1;
    	adc_channel_conf.SamplingTime = ADC_SAMPLETIME_15CYCLES;
    	adc_channel_conf.Offset = 0;
    
    	if(HAL_ADC_ConfigChannel(&adc_handle, &adc_channel_conf) != HAL_OK)
    	{
    		Error_Handler();
    	}
    }
    
    float read_temperature(ADC_HandleTypeDef *adc_handle)
    {
    	adc_val = HAL_ADC_GetValue(adc_handle);
    
    	float Vref = 3.3;
    	float Vsense = (adc_val * Vref)/4095.0;
    	float V25 = 0.76;
    	float avg_slope = 0.0025;
    
    	float temp = ((Vsense - V25)/avg_slope) + 25.0;
    
    	return temp;
    }
    
    static void init_uart(void)
    {
    	uart_handle.Instance = USARTx;
    
    	uart_handle.Init.BaudRate = 9600;
    	uart_handle.Init.WordLength = UART_WORDLENGTH_8B;
    	uart_handle.Init.StopBits = UART_STOPBITS_1;
    	uart_handle.Init.Parity = UART_PARITY_ODD;
    	uart_handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    	uart_handle.Init.Mode = UART_MODE_TX_RX;
    	uart_handle.Init.OverSampling = UART_OVERSAMPLING_16;
    
    	if(HAL_UART_Init(&uart_handle) != HAL_OK)
    	{
    		Error_Handler();
    	}
    }
    
    #ifdef USE_FULL_ASSERT
    
    /**
     * @brief Reports the name of the source file and the source line number
     * where the assert_param error has occurred.
     * @param file: pointer to the source file name
     * @param line: assert_param error line source number
     * @retval None
     */
    void assert_failed(uint8_t* file, uint32_t line)
    { 
     /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    
     /* Infinite loop */
     while (1)
     {
     }
    }
    #endif
    
    /**
     * @}
     */
    
    /**
     * @}
     */
    

    Hi Tesla,

    it is my first question here (so was not familiar with how you guys would like to see the code) but anyways here is the entire main.c. @KnarfB  you can see message[] is defined at line 105

    @Tesla DeLorean mpu and clock_config are same as in the "UART_Printf" example code. I following the same same example project.

    What I want to is achieve is printing the temperature on the terminal (so basically want to combine ADC temperature sensor and uart print example combined) 

    Note: Individual examples works but when I created my empty project (template) I am having the above mention isuse

     

    Thanks

    Super User
    February 7, 2025

    Check the return value of the HAL function call.

    Use the most precise clock (HSE?) at a high clock rate to generate precise timing on the serial line.

    Try another cable, terminal prog (TeraTerm) and/or computer.

    Step into the HAL code, it is open source and try to find out what's going on.

    Check the data on the wires using a cheap logic analyzer or scope.

    hth

    KnarfB

     

    Super User
    February 7, 2025

    sizeof should go to line 110, and strlen to line 111. Where & how is message defined?

    hth

    KnarfB