2020-03-27 02:43 PM
I've tried many programs and configurations to get my board to printf text to my PC while running tera term to receive the text. But everything I've tried has failed. In the following program, pushing the blue button is supposed to turn on the green LED and printf a text message. It turns on the LED alright but doesn't print anything on my terminal.
I know the USB port works because I can download a program to the board and get LEDs to work. Following is the code from one of the programs I've tried. Sorry it's so long. Any help would be very welcome.
Thanks!
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under Ultimate Liberty license
* SLA0044, the "License"; You may not use this file except in compliance with
* the License. You may obtain a copy of the License at:
* www.st.com/SLA0044
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"
#include "usbd_cdc_if.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
void MX_GPIO_Init(void);
static int8_t CDC_Receive_FS (unsigned char [] , uint32_t Len);
USBD_HandleTypeDef hUsbDeviceFS;
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USB_DEVICE_Init();
unsigned char data_to_send[] = ("testing");
while (1)
{
if(HAL_GPIO_ReadPin(GPIOA, Buttton_Pin) == GPIO_PIN_SET){
HAL_GPIO_WritePin(GPIOD, LD3_Pin, GPIO_PIN_SET);
uint8_t returnValue = CDC_Receive_FS(data_to_send, sizeof(data_to_send));
printf("\n, %i", returnValue);
HAL_Delay(200);
HAL_GPIO_WritePin(GPIOD, LD3_Pin, GPIO_PIN_RESET);
}
}
}
static int8_t CDC_Receive_FS (unsigned char data_to_send[] , uint32_t Len)
{
/* USER CODE BEGIN 7 */
static
uint8_t buff_RX[256];
static
uint8_t buff_TX[256];
int
i = 0;
for
(i = 0; i < Len; i++)
buff_TX[i] = buff_RX[i];
USBD_CDC_SetTxBuffer(&hUsbDeviceFS, &buff_TX[0], Len);
USBD_CDC_TransmitPacket(&hUsbDeviceFS);
USBD_CDC_SetRxBuffer(&hUsbDeviceFS, &buff_RX[0]);
USBD_CDC_ReceivePacket(&hUsbDeviceFS);
return
(USBD_OK);
/* USER CODE END 7 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 15;
RCC_OscInitStruct.PLL.PLLN = 144;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 5;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, LD3_Pin|LD4_Pin|LD5_Pin|LD6_Pin, GPIO_PIN_RESET);
/*Configure GPIO pin : Buttton_Pin */
GPIO_InitStruct.Pin = Buttton_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(Buttton_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pins : LD3_Pin LD4_Pin LD5_Pin LD6_Pin */
GPIO_InitStruct.Pin = LD3_Pin|LD4_Pin|LD5_Pin|LD6_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
HAL_GPIO_WritePin(GPIOD, LD5_Pin, GPIO_PIN_SET);
/* USER CODE END Error_Handler_Debug */
}
#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 CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2020-03-27 03:44 PM
Debug your code and find out where it's crashing.
Have you implemented printf somewhere? The standard libraries have no way to know you want printf to send characters out over the USB.
2020-03-27 03:51 PM
Thank you for responding. I have a printf statement in the main. How would I go about implementing printf somewhere?
2020-03-27 04:08 PM
The STM32F407-DISC1 board doesn't have the ST-LINK VCP wired.
For printf() to work you need all the plumbing connected to associate it with some USART
2020-03-27 04:08 PM
Here are some resources you can look at:
http://www.emcu.eu/how-to-implement-printf-for-send-message-via-usb-on-stm32-nucleo-boards/
I don't believe STM32CubeMX provides this functionality out of the box.
2020-03-27 04:15 PM
@DBenn Your example code uses USB CDC device. But it isn't clear if your board has 2nd USB connector for your program.
STM32F407VG is microcontroller, not a board name.
The connector you use to download and debug the program is not the connector for your USB device.
So, either wire up a UART to external connector or virtual COM Port on the debugger if it is capable of that,
or get another USB port wired.
If you simply want printf going somewhere, use ITM , it will be visible in the debugger.
-- pa
2020-03-27 04:24 PM
Thank you for the response. I didn't know that I had to use the second USB port. The literature mentions it as a connection for a mouse or keyboard. But I'll get a cable and try it out.
Thanks again.