cancel
Showing results for 
Search instead for 
Did you mean: 

How to print "Hello ARM World!" by using (C++) gcc compiler in STM32F4 discovery board?

MetuPhysics
Associate II

For example:

#include <iostream>

using namespace std;

int

main()

{

 cout << "Hello Arm World!" << endl;

 return 0;

}

Can u give detailed info step by step?..

1 REPLY 1

>>Can u give detailed info step by step?

Sorry I'm more Fight Club than Kindergarten Cop..

You have to initialize the hardware and do the plumbing.

So clocks, pins, peripherals, for the UART you plan to use. The STM32F407-DISCO doesn't have a built in VCP with the ST-LINK/V2. You could perhaps use the SWV (SWO PB3) route, but you'd need to check the wiring/solder-bridge situation. I used the break-out board STM32F4DIS-BB, it had a RS232 DB9 via USART6

Have a newlib.c supporting _read() / _write(), in ST's typical approach these should call __io_putchar() and __io_getchar(), based on GNU's implementation of STDIO

Here an SPL implementation (circa 2017)

/** @addtogroup STM32401_DISCOVERY_LOW_LEVEL_COM
  * @{
  */
#define COMn                             1
 
/**
 * @brief Definition for COM port1, connected to USART6 (STM32F4DIS-BB)
 */
#define EVAL_COM1                        USART6
#define EVAL_COM1_CLK                    RCC_APB2Periph_USART6
#define EVAL_COM1_TX_PIN                 GPIO_Pin_6
#define EVAL_COM1_TX_GPIO_PORT           GPIOC
#define EVAL_COM1_TX_GPIO_CLK            RCC_AHB1Periph_GPIOC
#define EVAL_COM1_TX_SOURCE              GPIO_PinSource6
#define EVAL_COM1_TX_AF                  GPIO_AF_USART6
#define EVAL_COM1_RX_PIN                 GPIO_Pin_7
#define EVAL_COM1_RX_GPIO_PORT           GPIOC
#define EVAL_COM1_RX_GPIO_CLK            RCC_AHB1Periph_GPIOC
#define EVAL_COM1_RX_SOURCE              GPIO_PinSource7
#define EVAL_COM1_RX_AF                  GPIO_AF_USART6
#define EVAL_COM1_IRQn                   USART6_IRQn
 
...
 
USART_TypeDef* COM_USART[COMn] = {EVAL_COM1};
 
GPIO_TypeDef* COM_TX_PORT[COMn] = {EVAL_COM1_TX_GPIO_PORT};
 
GPIO_TypeDef* COM_RX_PORT[COMn] = {EVAL_COM1_RX_GPIO_PORT};
 
const uint32_t COM_USART_CLK[COMn] = {EVAL_COM1_CLK};
 
const uint32_t COM_TX_PORT_CLK[COMn] = {EVAL_COM1_TX_GPIO_CLK};
 
const uint32_t COM_RX_PORT_CLK[COMn] = {EVAL_COM1_RX_GPIO_CLK};
 
const uint16_t COM_TX_PIN[COMn] = {EVAL_COM1_TX_PIN};
 
const uint16_t COM_RX_PIN[COMn] = {EVAL_COM1_RX_PIN};
 
const uint16_t COM_TX_PIN_SOURCE[COMn] = {EVAL_COM1_TX_SOURCE};
 
const uint16_t COM_RX_PIN_SOURCE[COMn] = {EVAL_COM1_RX_SOURCE};
 
const uint16_t COM_TX_AF[COMn] = {EVAL_COM1_TX_AF};
 
const uint16_t COM_RX_AF[COMn] = {EVAL_COM1_RX_AF};
 
...
 
/**
  * @brief  Configures COM port.
  * @param  COM: Specifies the COM port to be configured.
  *   This parameter can be one of following parameters:
  *     @arg COM1
  *     @arg COM2
  * @param  USART_InitStruct: pointer to a USART_InitTypeDef structure that
  *   contains the configuration information for the specified USART peripheral.
  * @retval None
  */
void STM_EVAL_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* Enable GPIO clock */
  RCC_AHB1PeriphClockCmd(COM_TX_PORT_CLK[COM] | COM_RX_PORT_CLK[COM], ENABLE);
 
  if ((COM_USART[COM] == USART1) || (COM_USART[COM] == USART6))	/* APB2 USARTs */
  {
    /* Enable UART clock on APB2 */
    RCC_APB2PeriphClockCmd(COM_USART_CLK[COM], ENABLE);
  }
	else
  {
    /* Enable UART clock on APB1 */
    RCC_APB1PeriphClockCmd(COM_USART_CLK[COM], ENABLE);
  }
 
  /* Connect PXx to USARTx_Tx*/
  GPIO_PinAFConfig(COM_TX_PORT[COM], COM_TX_PIN_SOURCE[COM], COM_TX_AF[COM]);
 
  /* Connect PXx to USARTx_Rx*/
  GPIO_PinAFConfig(COM_RX_PORT[COM], COM_RX_PIN_SOURCE[COM], COM_RX_AF[COM]);
 
  /* Configure USART Tx as alternate function  */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 
  GPIO_InitStructure.GPIO_Pin = COM_TX_PIN[COM];
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(COM_TX_PORT[COM], &GPIO_InitStructure);
 
  /* Configure USART Rx as alternate function  */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = COM_RX_PIN[COM];
  GPIO_Init(COM_RX_PORT[COM], &GPIO_InitStructure);
 
  /* USART configuration */
  USART_Init(COM_USART[COM], USART_InitStruct);
 
  /* Enable USART */
  USART_Cmd(COM_USART[COM], ENABLE);
}
 
...
 
//******************************************************************************
 
void USART_Configuration(void)
{
	USART_InitTypeDef USART_InitStructure;
 
  /* USARTx configured as follow:
        - BaudRate = 115200 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
  */
  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
 
  STM_EVAL_COMInit(COM1, &USART_InitStructure);
}
 
//******************************************************************************
 
/**
  * @brief  Retargets the C library printf function to the USART (GNU)
  * @param  None
  * @retval None
  */
int __io_putchar(int ch)
{
  /* Place your implementation of fputc here */
  /* e.g. write a character to the USART */
 
	ITM_SendChar(ch); // SWV
 
  /* Loop until the transmit buffer empty */
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_TXE) == RESET)
  {}
 
  USART_SendData(EVAL_COM1, (uint8_t) ch);
 
  return(ch);
}
 
//******************************************************************************
 
/**
  * @brief  Retargets the C library scanf function to the USART (GNU)
  * @param  None
  * @retval None
  */
int __io_getchar(void)
{
  /* Place your implementation of fgetc here */
  /* e.g. read a character from the USART */
 
  /* Loop until the receive buffer not empty */
  while (USART_GetFlagStatus(EVAL_COM1, USART_FLAG_RXNE) == RESET)
  {}
 
  return((int)USART_ReceiveData(EVAL_COM1));
}
 
//******************************************************************************

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