diff --git a/unit-testing/stm32cube/.gitignore b/unit-testing/stm32cube/.gitignore new file mode 100644 index 0000000..5402c18 --- /dev/null +++ b/unit-testing/stm32cube/.gitignore @@ -0,0 +1,3 @@ +.pioenvs +.clang_complete +.gcc-flags.json diff --git a/unit-testing/stm32cube/.travis.yml b/unit-testing/stm32cube/.travis.yml new file mode 100644 index 0000000..593d7ef --- /dev/null +++ b/unit-testing/stm32cube/.travis.yml @@ -0,0 +1,65 @@ +# Continuous Integration (CI) is the practice, in software +# engineering, of merging all developer working copies with a shared mainline +# several times a day < http://docs.platformio.org/page/ci/index.html > +# +# Documentation: +# +# * Travis CI Embedded Builds with PlatformIO +# < https://docs.travis-ci.com/user/integration/platformio/ > +# +# * PlatformIO integration with Travis CI +# < http://docs.platformio.org/page/ci/travis.html > +# +# * User Guide for `platformio ci` command +# < http://docs.platformio.org/page/userguide/cmd_ci.html > +# +# +# Please choose one of the following templates (proposed below) and uncomment +# it (remove "# " before each line) or use own configuration according to the +# Travis CI documentation (see above). +# + + +# +# Template #1: General project. Test it using existing `platformio.ini`. +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# install: +# - pip install -U platformio +# +# script: +# - platformio run + + +# +# Template #2: The project is intended to by used as a library with examples +# + +# language: python +# python: +# - "2.7" +# +# sudo: false +# cache: +# directories: +# - "~/.platformio" +# +# env: +# - PLATFORMIO_CI_SRC=path/to/test/file.c +# - PLATFORMIO_CI_SRC=examples/file.ino +# - PLATFORMIO_CI_SRC=path/to/test/directory +# +# install: +# - pip install -U platformio +# +# script: +# - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N diff --git a/unit-testing/stm32cube/README.rst b/unit-testing/stm32cube/README.rst new file mode 100644 index 0000000..4e56fe5 --- /dev/null +++ b/unit-testing/stm32cube/README.rst @@ -0,0 +1,30 @@ +.. Copyright 2014-present PlatformIO + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +How to build PlatformIO based project +===================================== + +1. `Install PlatformIO Core `_ +2. Download `development platform with examples `_ +3. Extract ZIP archive +4. Run these commands: + +.. code-block:: bash + + # Change directory to example + > cd platformio-examples/unit-testing/stm32cube + + + # Test project + > platformio test + + # Test specific environment + > platformio test -e nucleo_f401re diff --git a/unit-testing/stm32cube/lib/readme.txt b/unit-testing/stm32cube/lib/readme.txt new file mode 100644 index 0000000..4b6209e --- /dev/null +++ b/unit-testing/stm32cube/lib/readme.txt @@ -0,0 +1,38 @@ + +This directory is intended for the project specific (private) libraries. +PlatformIO will compile them to static libraries and link to executable file. + +The source code of each library should be placed in separate directory, like +"lib/private_lib/[here are source files]". + +For example, see how can be organized `Foo` and `Bar` libraries: + +|--lib +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| |--Foo +| | |- Foo.c +| | |- Foo.h +| |- readme.txt --> THIS FILE +|- platformio.ini +|--src + |- main.c + +Then in `src/main.c` you should use: + +#include +#include + +// rest H/C/CPP code + +PlatformIO will find your libraries automatically, configure preprocessor's +include paths and build them. + +See additional options for PlatformIO Library Dependency Finder `lib_*`: + +http://docs.platformio.org/page/projectconf.html#lib-install + diff --git a/unit-testing/stm32cube/platformio.ini b/unit-testing/stm32cube/platformio.ini new file mode 100644 index 0000000..0ffa80d --- /dev/null +++ b/unit-testing/stm32cube/platformio.ini @@ -0,0 +1,16 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter, extra scripting +; Upload options: custom port, speed and extra flags +; Library options: dependencies, extra library storages +; +; Please visit documentation for the other options and examples +; http://docs.platformio.org/page/projectconf.html + + + +[env:nucleo_f401re] +platform = ststm32 +framework = stm32cube +board = nucleo_f401re +test_transport = custom diff --git a/unit-testing/stm32cube/src/main.c b/unit-testing/stm32cube/src/main.c new file mode 100644 index 0000000..aecde1a --- /dev/null +++ b/unit-testing/stm32cube/src/main.c @@ -0,0 +1,74 @@ +#include "main.h" + +void LED_Init() +{ + LED_GPIO_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct; + + GPIO_InitStruct.Pin = LED_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct); + +} + +#ifndef UNIT_TEST +int main(void) +#else +int app_main(void) +#endif +{ + HAL_Init(); + LED_Init(); + + while (1) + { + HAL_GPIO_TogglePin(LED_GPIO_PORT, LED_PIN); + HAL_Delay(1000); + } +} + +void SysTick_Handler(void) +{ + HAL_IncTick(); +} + +void NMI_Handler(void) +{ +} + +void HardFault_Handler(void) +{ + while (1) {} +} + + +void MemManage_Handler(void) +{ + while (1) {} +} + +void BusFault_Handler(void) +{ + while (1) {} +} + +void UsageFault_Handler(void) +{ + while (1) {} +} + +void SVC_Handler(void) +{ +} + + +void DebugMon_Handler(void) +{ +} + +void PendSV_Handler(void) +{ +} diff --git a/unit-testing/stm32cube/src/main.h b/unit-testing/stm32cube/src/main.h new file mode 100644 index 0000000..8b92380 --- /dev/null +++ b/unit-testing/stm32cube/src/main.h @@ -0,0 +1,10 @@ +#ifndef MAIN_H +#define MAIN_H + +#include "stm32f4xx_hal.h" + +#define LED_PIN GPIO_PIN_5 +#define LED_GPIO_PORT GPIOA +#define LED_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() + +#endif // MAIN_H \ No newline at end of file diff --git a/unit-testing/stm32cube/test/test_main.c b/unit-testing/stm32cube/test/test_main.c new file mode 100644 index 0000000..07a2ba5 --- /dev/null +++ b/unit-testing/stm32cube/test/test_main.c @@ -0,0 +1,54 @@ +#include +#include + +#ifdef UNIT_TEST + +void setUp(void) { + HAL_Init(); + + LED_GPIO_CLK_ENABLE(); + + GPIO_InitTypeDef GPIO_InitStruct; + GPIO_InitStruct.Pin = LED_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; + HAL_GPIO_Init(LED_GPIO_PORT, &GPIO_InitStruct); +} + +void tearDown(void) { + HAL_GPIO_DeInit(LED_GPIO_PORT, LED_PIN); +} + +void test_led_builtin_pin_number(void) { + TEST_ASSERT_EQUAL(LED_PIN, GPIO_PIN_5); +} + +void test_led_state_high(void) { + HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_SET); + TEST_ASSERT_EQUAL(HAL_GPIO_ReadPin(LED_GPIO_PORT, LED_PIN), GPIO_PIN_SET); +} + +void test_led_state_low(void) { + HAL_GPIO_WritePin(LED_GPIO_PORT, LED_PIN, GPIO_PIN_RESET); + TEST_ASSERT_EQUAL(HAL_GPIO_ReadPin(LED_GPIO_PORT, LED_PIN), GPIO_PIN_RESET); +} + +int main() { + UNITY_BEGIN(); + + RUN_TEST(test_led_builtin_pin_number); + for (unsigned int i = 0; i < 5; i++) + { + RUN_TEST(test_led_state_high); + HAL_Delay(500); + RUN_TEST(test_led_state_low); + HAL_Delay(500); + } + + UNITY_END(); // stop unit testing + + while(1){} +} + +#endif diff --git a/unit-testing/stm32cube/test/unittest_transport.c b/unit-testing/stm32cube/test/unittest_transport.c new file mode 100644 index 0000000..bed7c01 --- /dev/null +++ b/unit-testing/stm32cube/test/unittest_transport.c @@ -0,0 +1,72 @@ +#include "unittest_transport.h" +#include "stm32f4xx_hal.h" + +#define USARTx USART2 +#define USARTx_CLK_ENABLE() __HAL_RCC_USART2_CLK_ENABLE() +#define USARTx_CLK_DISABLE() __HAL_RCC_USART2_CLK_DISABLE() +#define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() +#define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE() +#define USARTx_RX_GPIO_CLK_DISABLE() __HAL_RCC_GPIOA_CLK_DISABLE() +#define USARTx_TX_GPIO_CLK_DISABLE() __HAL_RCC_GPIOA_CLK_DISABLE() + +#define USARTx_FORCE_RESET() __HAL_RCC_USART2_FORCE_RESET() +#define USARTx_RELEASE_RESET() __HAL_RCC_USART2_RELEASE_RESET() + +#define USARTx_TX_PIN GPIO_PIN_2 +#define USARTx_TX_GPIO_PORT GPIOA +#define USARTx_TX_AF GPIO_AF7_USART2 +#define USARTx_RX_PIN GPIO_PIN_3 +#define USARTx_RX_GPIO_PORT GPIOA +#define USARTx_RX_AF GPIO_AF7_USART2 + +static UART_HandleTypeDef UartHandle; + +void unittest_uart_begin() +{ + GPIO_InitTypeDef GPIO_InitStruct; + + USARTx_TX_GPIO_CLK_ENABLE(); + USARTx_RX_GPIO_CLK_ENABLE(); + + USARTx_CLK_ENABLE(); + + GPIO_InitStruct.Pin = USARTx_TX_PIN; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Speed = GPIO_SPEED_FAST; + GPIO_InitStruct.Alternate = USARTx_TX_AF; + + HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct); + + GPIO_InitStruct.Pin = USARTx_RX_PIN; + GPIO_InitStruct.Alternate = USARTx_RX_AF; + + HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct); + UartHandle.Instance = USARTx; + + UartHandle.Init.BaudRate = 9600; + UartHandle.Init.WordLength = UART_WORDLENGTH_8B; + UartHandle.Init.StopBits = UART_STOPBITS_1; + UartHandle.Init.Parity = UART_PARITY_NONE; + UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE; + UartHandle.Init.Mode = UART_MODE_TX_RX; + UartHandle.Init.OverSampling = UART_OVERSAMPLING_16; + + if(HAL_UART_Init(&UartHandle) != HAL_OK) { + while(1){} + } + +} + +void unittest_uart_putchar(char c) +{ + HAL_UART_Transmit(&UartHandle, (uint8_t*)(&c), 1, 1000); +} + +void unittest_uart_flush(){} + +void unittest_uart_end() { + USARTx_CLK_DISABLE(); + USARTx_RX_GPIO_CLK_DISABLE(); + USARTx_TX_GPIO_CLK_DISABLE(); +} diff --git a/unit-testing/stm32cube/test/unittest_transport.h b/unit-testing/stm32cube/test/unittest_transport.h new file mode 100644 index 0000000..e6da735 --- /dev/null +++ b/unit-testing/stm32cube/test/unittest_transport.h @@ -0,0 +1,17 @@ +#ifndef UNITEST_TRANSPORT_H +#define UNITEST_TRANSPORT_H + +#ifdef __cplusplus +extern "C" { +#endif + +void unittest_uart_begin(); +void unittest_uart_putchar(char c); +void unittest_uart_flush(); +void unittest_uart_end(); + +#ifdef __cplusplus +} +#endif + +#endif // UNITEST_TRANSPORT_H