Skip to content

Commit f8a58f9

Browse files
committed
Add new example with unit testing and STM32Cube
1 parent 7799fbd commit f8a58f9

File tree

10 files changed

+379
-0
lines changed

10 files changed

+379
-0
lines changed

unit-testing/stm32cube/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.pioenvs
2+
.clang_complete
3+
.gcc-flags.json

unit-testing/stm32cube/.travis.yml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Continuous Integration (CI) is the practice, in software
2+
# engineering, of merging all developer working copies with a shared mainline
3+
# several times a day < http://docs.platformio.org/page/ci/index.html >
4+
#
5+
# Documentation:
6+
#
7+
# * Travis CI Embedded Builds with PlatformIO
8+
# < https://docs.travis-ci.com/user/integration/platformio/ >
9+
#
10+
# * PlatformIO integration with Travis CI
11+
# < http://docs.platformio.org/page/ci/travis.html >
12+
#
13+
# * User Guide for `platformio ci` command
14+
# < http://docs.platformio.org/page/userguide/cmd_ci.html >
15+
#
16+
#
17+
# Please choose one of the following templates (proposed below) and uncomment
18+
# it (remove "# " before each line) or use own configuration according to the
19+
# Travis CI documentation (see above).
20+
#
21+
22+
23+
#
24+
# Template #1: General project. Test it using existing `platformio.ini`.
25+
#
26+
27+
# language: python
28+
# python:
29+
# - "2.7"
30+
#
31+
# sudo: false
32+
# cache:
33+
# directories:
34+
# - "~/.platformio"
35+
#
36+
# install:
37+
# - pip install -U platformio
38+
#
39+
# script:
40+
# - platformio run
41+
42+
43+
#
44+
# Template #2: The project is intended to by used as a library with examples
45+
#
46+
47+
# language: python
48+
# python:
49+
# - "2.7"
50+
#
51+
# sudo: false
52+
# cache:
53+
# directories:
54+
# - "~/.platformio"
55+
#
56+
# env:
57+
# - PLATFORMIO_CI_SRC=path/to/test/file.c
58+
# - PLATFORMIO_CI_SRC=examples/file.ino
59+
# - PLATFORMIO_CI_SRC=path/to/test/directory
60+
#
61+
# install:
62+
# - pip install -U platformio
63+
#
64+
# script:
65+
# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N

unit-testing/stm32cube/README.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. Copyright 2014-present PlatformIO <[email protected]>
2+
Licensed under the Apache License, Version 2.0 (the "License");
3+
you may not use this file except in compliance with the License.
4+
You may obtain a copy of the License at
5+
http://www.apache.org/licenses/LICENSE-2.0
6+
Unless required by applicable law or agreed to in writing, software
7+
distributed under the License is distributed on an "AS IS" BASIS,
8+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
See the License for the specific language governing permissions and
10+
limitations under the License.
11+
12+
How to build PlatformIO based project
13+
=====================================
14+
15+
1. `Install PlatformIO Core <http://docs.platformio.org/page/core.html>`_
16+
2. Download `development platform with examples <https://github.com/platformio/platform-ststm32/archive/develop.zip>`_
17+
3. Extract ZIP archive
18+
4. Run these commands:
19+
20+
.. code-block:: bash
21+
22+
# Change directory to example
23+
> cd platformio-examples/unit-testing/stm32cube
24+
25+
26+
# Test project
27+
> platformio test
28+
29+
# Test specific environment
30+
> platformio test -e nucleo_f401re
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
This directory is intended for the project specific (private) libraries.
3+
PlatformIO will compile them to static libraries and link to executable file.
4+
5+
The source code of each library should be placed in separate directory, like
6+
"lib/private_lib/[here are source files]".
7+
8+
For example, see how can be organized `Foo` and `Bar` libraries:
9+
10+
|--lib
11+
| |--Bar
12+
| | |--docs
13+
| | |--examples
14+
| | |--src
15+
| | |- Bar.c
16+
| | |- Bar.h
17+
| |--Foo
18+
| | |- Foo.c
19+
| | |- Foo.h
20+
| |- readme.txt --> THIS FILE
21+
|- platformio.ini
22+
|--src
23+
|- main.c
24+
25+
Then in `src/main.c` you should use:
26+
27+
#include <Foo.h>
28+
#include <Bar.h>
29+
30+
// rest H/C/CPP code
31+
32+
PlatformIO will find your libraries automatically, configure preprocessor's
33+
include paths and build them.
34+
35+
See additional options for PlatformIO Library Dependency Finder `lib_*`:
36+
37+
http://docs.platformio.org/page/projectconf.html#lib-install
38+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
; PlatformIO Project Configuration File
2+
;
3+
; Build options: build flags, source filter, extra scripting
4+
; Upload options: custom port, speed and extra flags
5+
; Library options: dependencies, extra library storages
6+
;
7+
; Please visit documentation for the other options and examples
8+
; http://docs.platformio.org/page/projectconf.html
9+
10+
11+
12+
[env:nucleo_f401re]
13+
platform = ststm32
14+
framework = stm32cube
15+
board = nucleo_f401re
16+
test_transport = custom

unit-testing/stm32cube/src/main.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include "main.h"
2+
3+
void LED_Init()
4+
{
5+
LED_GPIO_CLK_ENABLE();
6+
7+
GPIO_InitTypeDef GPIO_InitStruct;
8+
9+
GPIO_InitStruct.Pin = LED_PIN;
10+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
11+
GPIO_InitStruct.Pull = GPIO_PULLUP;
12+
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
13+
HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
14+
15+
}
16+
17+
#ifndef UNIT_TEST
18+
int main(void)
19+
#else
20+
int app_main(void)
21+
#endif
22+
{
23+
HAL_Init();
24+
LED_Init();
25+
26+
while (1)
27+
{
28+
HAL_GPIO_TogglePin(LED_GPIO_PORT, LED_PIN);
29+
HAL_Delay(1000);
30+
}
31+
}
32+
33+
void SysTick_Handler(void)
34+
{
35+
HAL_IncTick();
36+
}
37+
38+
void NMI_Handler(void)
39+
{
40+
}
41+
42+
void HardFault_Handler(void)
43+
{
44+
while (1) {}
45+
}
46+
47+
48+
void MemManage_Handler(void)
49+
{
50+
while (1) {}
51+
}
52+
53+
void BusFault_Handler(void)
54+
{
55+
while (1) {}
56+
}
57+
58+
void UsageFault_Handler(void)
59+
{
60+
while (1) {}
61+
}
62+
63+
void SVC_Handler(void)
64+
{
65+
}
66+
67+
68+
void DebugMon_Handler(void)
69+
{
70+
}
71+
72+
void PendSV_Handler(void)
73+
{
74+
}

unit-testing/stm32cube/src/main.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef MAIN_H
2+
#define MAIN_H
3+
4+
#include "stm32f4xx_hal.h"
5+
6+
#define LED_PIN GPIO_PIN_5
7+
#define LED_GPIO_PORT GPIOA
8+
#define LED_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
9+
10+
#endif // MAIN_H
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <main.h>
2+
#include <unity.h>
3+
4+
#ifdef UNIT_TEST
5+
6+
void setUp(void) {
7+
HAL_Init();
8+
9+
LED_GPIO_CLK_ENABLE();
10+
11+
GPIO_InitTypeDef GPIO_InitStruct;
12+
GPIO_InitStruct.Pin = LED_PIN;
13+
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
14+
GPIO_InitStruct.Pull = GPIO_PULLUP;
15+
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
16+
HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct);
17+
}
18+
19+
void tearDown(void) {
20+
HAL_GPIO_DeInit(LED_GPIO_PORT, LED_PIN);
21+
}
22+
23+
void test_led_builtin_pin_number(void) {
24+
TEST_ASSERT_EQUAL(LED_PIN, GPIO_PIN_5);
25+
}
26+
27+
void test_led_state_high(void) {
28+
HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_SET);
29+
TEST_ASSERT_EQUAL(HAL_GPIO_ReadPin(LED_GPIO_PORT, LED_PIN), GPIO_PIN_SET);
30+
}
31+
32+
void test_led_state_low(void) {
33+
HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_RESET);
34+
TEST_ASSERT_EQUAL(HAL_GPIO_ReadPin(LED_GPIO_PORT, LED_PIN), GPIO_PIN_RESET);
35+
}
36+
37+
int main() {
38+
UNITY_BEGIN();
39+
40+
RUN_TEST(test_led_builtin_pin_number);
41+
for (unsigned int i = 0; i < 5; i++)
42+
{
43+
RUN_TEST(test_led_state_high);
44+
HAL_Delay(500);
45+
RUN_TEST(test_led_state_low);
46+
HAL_Delay(500);
47+
}
48+
49+
UNITY_END(); // stop unit testing
50+
51+
while(1){}
52+
}
53+
54+
#endif
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#include "unittest_transport.h"
2+
#include "stm32f4xx_hal.h"
3+
4+
#define USARTx USART2
5+
#define USARTx_CLK_ENABLE() __HAL_RCC_USART2_CLK_ENABLE()
6+
#define USARTx_CLK_DISABLE() __HAL_RCC_USART2_CLK_DISABLE()
7+
#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
8+
#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
9+
#define USARTx_RX_GPIO_CLK_DISABLE() __HAL_RCC_GPIOA_CLK_DISABLE()
10+
#define USARTx_TX_GPIO_CLK_DISABLE() __HAL_RCC_GPIOA_CLK_DISABLE()
11+
12+
#define USARTx_FORCE_RESET() __HAL_RCC_USART2_FORCE_RESET()
13+
#define USARTx_RELEASE_RESET() __HAL_RCC_USART2_RELEASE_RESET()
14+
15+
#define USARTx_TX_PIN GPIO_PIN_2
16+
#define USARTx_TX_GPIO_PORT GPIOA
17+
#define USARTx_TX_AF GPIO_AF7_USART2
18+
#define USARTx_RX_PIN GPIO_PIN_3
19+
#define USARTx_RX_GPIO_PORT GPIOA
20+
#define USARTx_RX_AF GPIO_AF7_USART2
21+
22+
static UART_HandleTypeDef UartHandle;
23+
24+
void unittest_uart_begin()
25+
{
26+
GPIO_InitTypeDef GPIO_InitStruct;
27+
28+
USARTx_TX_GPIO_CLK_ENABLE();
29+
USARTx_RX_GPIO_CLK_ENABLE();
30+
31+
USARTx_CLK_ENABLE();
32+
33+
GPIO_InitStruct.Pin = USARTx_TX_PIN;
34+
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
35+
GPIO_InitStruct.Pull = GPIO_PULLUP;
36+
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
37+
GPIO_InitStruct.Alternate = USARTx_TX_AF;
38+
39+
HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
40+
41+
GPIO_InitStruct.Pin = USARTx_RX_PIN;
42+
GPIO_InitStruct.Alternate = USARTx_RX_AF;
43+
44+
HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
45+
UartHandle.Instance = USARTx;
46+
47+
UartHandle.Init.BaudRate = 9600;
48+
UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
49+
UartHandle.Init.StopBits = UART_STOPBITS_1;
50+
UartHandle.Init.Parity = UART_PARITY_NONE;
51+
UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
52+
UartHandle.Init.Mode = UART_MODE_TX_RX;
53+
UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
54+
55+
if(HAL_UART_Init(&UartHandle) != HAL_OK) {
56+
while(1){}
57+
}
58+
59+
}
60+
61+
void unittest_uart_putchar(char c)
62+
{
63+
HAL_UART_Transmit(&UartHandle, (uint8_t*)(&c), 1, 1000);
64+
}
65+
66+
void unittest_uart_flush(){}
67+
68+
void unittest_uart_end() {
69+
USARTx_CLK_DISABLE();
70+
USARTx_RX_GPIO_CLK_DISABLE();
71+
USARTx_TX_GPIO_CLK_DISABLE();
72+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef UNITEST_TRANSPORT_H
2+
#define UNITEST_TRANSPORT_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
void unittest_uart_begin();
9+
void unittest_uart_putchar(char c);
10+
void unittest_uart_flush();
11+
void unittest_uart_end();
12+
13+
#ifdef __cplusplus
14+
}
15+
#endif
16+
17+
#endif // UNITEST_TRANSPORT_H

0 commit comments

Comments
 (0)