Using SCons with GNU toolchain for ARM on Windows – part 2 : the code

[part 1 part 2 part 3]

After downloading and installing all the necessary tools (see my previous post), I will create a project for the discovery board using STM32CubeMX tool.
The application will just send some bytes through the serial port USART2 and light the LEDs on the discovery board.

 1. The STM32CubeMX project

So, let’s start creating the STM32CubeMX project. The STM32F4Discovery board I’m using is equipped with a STM32F407VGT6 MCU; so that’s the platform I’m going to choose in the new project wizard.

The various configuration steps are described in the attached figures, and I’m not going to detail every step since they are relatively straightforward.

a) Peripheral selection

Peripheral Configuration

b) GPIO configuration

Pin Configuration

c) clock configuration

Clock Configuration

d) Peripheral configuration: USART2
USART Configuration
e) Peripheral configuration: GPIOs
GPIO Configuration
f) Project settings: project
Project Settings - code generation
g) Project settings: code generator

Project Settings2. Code generation

Now the code can be generated by clicking on the Project > Generate Code menu. If requested, allow STM32CubeMX to download the necessary device libraries.

The generated project looks like this:

stm32cubemx_generatedcode

3. Code editing

The generated code (see Src/main.c) does nothing but configuring the MCU hardware, so we need to edit the code to send data through serial port and light up the leds.

Let’s modify the main.c file between the /* USER CODE BEGIN 3 */ and /* USER CODE END 3 */ as follows:

/* USER CODE BEGIN 3 */
  /* Infinite loop */
  uint8_t c = 'A';
  uint16_t cnt = 0;
  while (1)
  {
	  HAL_UART_Transmit(&huart2, &c, 1, 10);
	  c++;
	  if(c == ('Z'+1)) c = 'A';
	  switch(cnt) {
	  case 0:
		  // green led on
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_SET);
		  break;
	  case 100:
		  // green led off
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
		  break;
	  case 200:
		  // orange led on
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_SET);
		  break;
	  case 300:
		  // orange led off
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_13, GPIO_PIN_RESET);
		  break;
	  case 400:
		  // red led on
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_SET);
		  break;
	  case 500:
		  // red led off
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
		  break;
	  case 600:
		  // blue led on
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_SET);
		  break;
	  case 700:
		  // blue led off
		  HAL_GPIO_WritePin(GPIOD, GPIO_PIN_15, GPIO_PIN_RESET);
		  break;
	  }
	  cnt = (cnt+1)%799;
  }
  /* USER CODE END 3 */

This code just sends chars to the serial port and lights on and off the leds on the discovery board. In order to see the sent chars (from ‘A’ to ‘Z’) we will need a TTL to RS232 level converter or a LVTTL USB serial port interface. Let me stress that because of different voltage evels you cannot connect the discovery board directly to an RS232 interface.

What we still need is is the startup file. A working startup file can be downloaded from Coocox public repository. I admit, I didn’t check this file, but it works. So, download the file a put it into a new folder named sys in the Src folder of the project, and rename the file startup_stm32f4xx_fromCoocox.c (to remember from where we got it).

In the next post the SCons script will be created and used to build the application.

Advertisements

2 thoughts on “Using SCons with GNU toolchain for ARM on Windows – part 2 : the code

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s