2025-02-07 08:53 AM
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
and uart config:
and what I would like to print:
let me know if you need more info.
Thanks,
2025-02-07 09:05 AM
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?
2025-02-07 09:20 AM
sizeof should go to line 110, and strlen to line 111. Where & how is message defined?
hth
KnarfB
2025-02-07 10:17 AM
/**
******************************************************************************
* @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
2025-02-07 10:51 AM
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
2025-02-07 11:02 AM
I'm not seeing a reason it should fail yet.
Show MSP code enabling UART and GPIO
Have something in Error_Handler() and HardFault_Handler() so can check it's not dying there.
Check SysTick() is working.
Stop in debugger once it fails, determine where it has stopped or is stuck.
2025-02-07 11:07 AM - edited 2025-02-07 11:08 AM
void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
GPIO_InitTypeDef GPIO_InitStruct;
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
/*##-1- Enable peripherals and GPIO Clocks #################################*/
/* Enable GPIO TX/RX clock */
USARTx_TX_GPIO_CLK_ENABLE();
USARTx_RX_GPIO_CLK_ENABLE();
/* Select SysClk as source of USART1 clocks */
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
RCC_PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_SYSCLK;
HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphClkInit);
/* 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_FREQ_VERY_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);
}
/**
* @brief UART MSP De-Initialization
* This function frees the hardware resources used in this example:
* - Disable the Peripheral's clock
* - Revert GPIO and NVIC configuration to their default state
* @param huart: UART handle pointer
* @retval None
*/
void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
{
/*##-1- Reset peripherals ##################################################*/
USARTx_FORCE_RESET();
USARTx_RELEASE_RESET();
/*##-2- Disable peripherals and GPIO Clocks #################################*/
/* Configure UART Tx as alternate function */
HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
/* Configure UART Rx as alternate function */
HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
}
@Tesla DeLorean here is the code for uart msp init
sometimes it stuck at:
tickstart = HAL_GetTick();
in HAL_UART_Transmit() function
2025-02-07 11:22 AM
I have found the issue. Issue was with adc initialization, I guess some kind of priority, I need to look into this, but if I comment out the adc() and adc_channel() init function uart works.
what could have caused it? is it systick interrupt priority vs adc interrupt priority?
/* 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";
uint8_t data[50];
uint8_t error = 0;
float k = 20.50;
while (1)
{
//temp_cel = read_temperature(&adc_handle);
snprintf(data, sizeof(data), "\n\r temperature:%0.2f \n\r", k);
HAL_UART_Transmit(&uart_handle, data, sizeof(data), HAL_MAX_DELAY);
}
}