├── .github └── workflows │ ├── misra_check.yml │ ├── publish_docs.yml │ └── unit_tests.yml ├── .gitignore ├── AUTHORS.md ├── CMakeLists.txt ├── ChangeLog ├── LICENSE ├── README.md ├── docs ├── apiref.md ├── assertions.md ├── checksum.md ├── critsect.md ├── crypto.md ├── extra.md ├── heap.md ├── images │ ├── software_architecture.png │ └── software_architecture.svg ├── index.md ├── license.md ├── lists.md ├── mempools.md ├── misra.md ├── platform.md └── random.md ├── mkdocs.yml └── source ├── extra ├── cplusplus │ └── tbxcxx.cpp └── freertos │ ├── tbx_freertos.c │ └── tbx_freertos.h ├── microtbx.h ├── misra.cmake ├── misra.json ├── port ├── ARM_CORTEXM │ ├── GCC │ │ └── tbx_comp.s │ ├── IAR │ │ └── tbx_comp.s │ ├── Keil │ │ └── tbxcomp.s │ ├── tbx_port.c │ └── tbx_types.h ├── AVR │ ├── GCC │ │ └── tbx_port.c │ └── tbx_types.h ├── LINUX │ ├── tbx_port.c │ └── tbx_types.h └── RP2040 │ ├── tbx_port.c │ └── tbx_types.h ├── tbx_aes256.c ├── tbx_aes256.h ├── tbx_assert.c ├── tbx_assert.h ├── tbx_checksum.c ├── tbx_checksum.h ├── tbx_critsect.c ├── tbx_critsect.h ├── tbx_crypto.c ├── tbx_crypto.h ├── tbx_heap.c ├── tbx_heap.h ├── tbx_list.c ├── tbx_list.h ├── tbx_mempool.c ├── tbx_mempool.h ├── tbx_platform.c ├── tbx_platform.h ├── tbx_port.h ├── tbx_random.c ├── tbx_random.h ├── template └── tbx_conf.h └── tests ├── unittests.c └── unittests.h /.github/workflows/misra_check.yml: -------------------------------------------------------------------------------- 1 | name: MISRA Check 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - master 7 | - main 8 | jobs: 9 | test: 10 | runs-on: ubuntu-22.04 11 | steps: 12 | - name: Install dependencies 13 | run: sudo apt-get install cppcheck 14 | - name: Checkout tests repository 15 | uses: actions/checkout@v4 16 | with: 17 | repository: feaser/microtbx-tests 18 | submodules: true 19 | - name: Checkout repository 20 | uses: actions/checkout@v4 21 | with: 22 | path: 'third_party/microtbx' 23 | - name: Generate build environment 24 | working-directory: build 25 | run: cmake .. 26 | - name: Perform MISRA compliance check 27 | working-directory: build/tests/base/LINUX_PC 28 | run: make microtbx_MISRA 29 | 30 | -------------------------------------------------------------------------------- /.github/workflows/publish_docs.yml: -------------------------------------------------------------------------------- 1 | name: Publish Docs 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - master 7 | - main 8 | jobs: 9 | deploy: 10 | runs-on: ubuntu-22.04 11 | steps: 12 | - uses: actions/checkout@v4 13 | - uses: actions/setup-python@v4 14 | with: 15 | python-version: 3.x 16 | - run: pip install mkdocs 17 | - run: mkdocs gh-deploy --force 18 | -------------------------------------------------------------------------------- /.github/workflows/unit_tests.yml: -------------------------------------------------------------------------------- 1 | name: Unit Tests 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - master 7 | - main 8 | jobs: 9 | test: 10 | runs-on: ubuntu-22.04 11 | steps: 12 | - name: Checkout tests repository 13 | uses: actions/checkout@v4 14 | with: 15 | repository: feaser/microtbx-tests 16 | submodules: true 17 | - name: Checkout repository 18 | uses: actions/checkout@v4 19 | with: 20 | path: 'third_party/microtbx' 21 | - name: Generate build environment 22 | working-directory: build 23 | run: cmake .. 24 | - name: Build test application 25 | working-directory: build/tests/base/LINUX_PC 26 | run: make 27 | - name: Run test application 28 | working-directory: build/tests/base/LINUX_PC 29 | run: ./microtbx_tests 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Prerequisites 2 | *.d 3 | 4 | # Object files 5 | *.o 6 | *.ko 7 | *.obj 8 | *.elf 9 | 10 | # Linker output 11 | *.ilk 12 | *.map 13 | *.exp 14 | 15 | # Precompiled Headers 16 | *.gch 17 | *.pch 18 | 19 | # Libraries 20 | *.lib 21 | *.a 22 | *.la 23 | *.lo 24 | 25 | # Shared objects (inc. Windows DLLs) 26 | *.dll 27 | *.so 28 | *.so.* 29 | *.dylib 30 | 31 | # Executables 32 | *.exe 33 | *.out 34 | *.app 35 | *.i*86 36 | *.x86_64 37 | *.hex 38 | 39 | # Debug files 40 | *.dSYM/ 41 | *.su 42 | *.idb 43 | *.pdb 44 | 45 | # Kernel Module Compile Results 46 | *.mod* 47 | *.cmd 48 | .tmp_versions/ 49 | modules.order 50 | Module.symvers 51 | Mkfile.old 52 | dkms.conf 53 | -------------------------------------------------------------------------------- /AUTHORS.md: -------------------------------------------------------------------------------- 1 | # Authors of MicroTBX 2 | 3 | * Frank Voorburg designed and implemented the initial version of MicroTBX. 4 | 5 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # Create interface library for hardware independent MicroTBX sources. 2 | add_library(microtbx INTERFACE) 3 | 4 | target_sources(microtbx INTERFACE 5 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_aes256.c" 6 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_assert.c" 7 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_checksum.c" 8 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_critsect.c" 9 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_crypto.c" 10 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_heap.c" 11 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_list.c" 12 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_mempool.c" 13 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_platform.c" 14 | "${CMAKE_CURRENT_LIST_DIR}/source/tbx_random.c" 15 | ) 16 | 17 | target_include_directories(microtbx INTERFACE 18 | "${CMAKE_CURRENT_LIST_DIR}/source" 19 | ) 20 | 21 | # Create interface library for Cortex-M port specific sources. 22 | add_library(microtbx-cortexm INTERFACE) 23 | 24 | target_sources(microtbx-cortexm INTERFACE 25 | "${CMAKE_CURRENT_LIST_DIR}/source/port/ARM_CORTEXM/tbx_port.c" 26 | "${CMAKE_CURRENT_LIST_DIR}/source/port/ARM_CORTEXM/GCC/tbx_comp.s" 27 | ) 28 | 29 | target_include_directories(microtbx-cortexm INTERFACE 30 | "${CMAKE_CURRENT_LIST_DIR}/source/port/ARM_CORTEXM" 31 | "${CMAKE_CURRENT_LIST_DIR}/source/port/ARM_CORTEXM/GCC" 32 | ) 33 | 34 | # Create interface library for Raspberry PI Pico RP2040 port specific sources. 35 | add_library(microtbx-rp2040 INTERFACE) 36 | 37 | target_sources(microtbx-rp2040 INTERFACE 38 | "${CMAKE_CURRENT_LIST_DIR}/source/port/RP2040/tbx_port.c" 39 | ) 40 | 41 | target_include_directories(microtbx-rp2040 INTERFACE 42 | "${CMAKE_CURRENT_LIST_DIR}/source/port/RP2040" 43 | ) 44 | 45 | # Create interface library for AVR port specific sources. 46 | add_library(microtbx-avr INTERFACE) 47 | 48 | target_sources(microtbx-avr INTERFACE 49 | "${CMAKE_CURRENT_LIST_DIR}/source/port/AVR/GCC/tbx_port.c" 50 | ) 51 | 52 | target_include_directories(microtbx-avr INTERFACE 53 | "${CMAKE_CURRENT_LIST_DIR}/source/port/AVR" 54 | "${CMAKE_CURRENT_LIST_DIR}/source/port/AVR/GCC" 55 | ) 56 | 57 | # Create interface library for Linux port specific sources. 58 | add_library(microtbx-linux INTERFACE) 59 | 60 | target_sources(microtbx-linux INTERFACE 61 | "${CMAKE_CURRENT_LIST_DIR}/source/port/LINUX/tbx_port.c" 62 | ) 63 | 64 | target_include_directories(microtbx-linux INTERFACE 65 | "${CMAKE_CURRENT_LIST_DIR}/source/port/LINUX" 66 | ) 67 | 68 | # Create interface library for FreeRTOS extra sources. 69 | add_library(microtbx-extra-freertos INTERFACE) 70 | 71 | target_sources(microtbx-extra-freertos INTERFACE 72 | "${CMAKE_CURRENT_LIST_DIR}/source/extra/freertos/tbx_freertos.c" 73 | ) 74 | 75 | target_include_directories(microtbx-extra-freertos INTERFACE 76 | "${CMAKE_CURRENT_LIST_DIR}/source/extra/freertos" 77 | ) 78 | 79 | # Create interface library for C++ extra sources. 80 | add_library(microtbx-extra-cpp INTERFACE) 81 | 82 | target_sources(microtbx-extra-cpp INTERFACE 83 | "${CMAKE_CURRENT_LIST_DIR}/source/extra/cplusplus/tbxcxx.cpp" 84 | ) 85 | 86 | target_include_directories(microtbx-extra-cpp INTERFACE 87 | "${CMAKE_CURRENT_LIST_DIR}/source/extra/cplusplus" 88 | ) 89 | 90 | # Create interface library for the unit test specific sources. 91 | add_library(microtbx-tests INTERFACE) 92 | 93 | target_sources(microtbx-tests INTERFACE 94 | "${CMAKE_CURRENT_LIST_DIR}/source/tests/unittests.c" 95 | ) 96 | 97 | target_include_directories(microtbx-tests INTERFACE 98 | "${CMAKE_CURRENT_LIST_DIR}/source/tests" 99 | ) 100 | 101 | # Create interface library for the template. Only used for MISRA check. 102 | add_library(microtbx-template INTERFACE) 103 | 104 | target_include_directories(microtbx-template INTERFACE 105 | "${CMAKE_CURRENT_LIST_DIR}/source/template" 106 | ) 107 | 108 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | ----------------------------------------------------------------------------------------- 2 | 2023-03-26 Frank Voorburg 3 | 4 | * Renamed files. 5 | * Added CMake integration details to the user manual. 6 | * Resolved a compiler warning. 7 | * Public release of version 1.3.0. 8 | 9 | ----------------------------------------------------------------------------------------- 10 | 2023-03-11 Frank Voorburg 11 | 12 | * Added RP2040 multicore and AVR ports. 13 | * Added platform module for run-time endianness detection. 14 | * Added TBX_ON and TBX_OFF macros. 15 | * Coding style updates. 16 | * Public release of version 1.2.0. 17 | 18 | ----------------------------------------------------------------------------------------- 19 | 2023-02-03 Frank Voorburg 20 | 21 | * Added GitHub action workflow for automated MISRA compliance check. 22 | * Switch to CppCheck for MISRA compliance check and improved code quality some more. 23 | * Added CMake interface library for convenient integration in CMake based projects. 24 | * Added tbxcxx.cpp with C++ new/delete overloads that use MicroTBX memory pools. 25 | * Public release of version 1.1.0. 26 | 27 | ----------------------------------------------------------------------------------------- 28 | 2022-06-15 Frank Voorburg 29 | 30 | * Added unit tests. 31 | * Configured GitHub actions for building and running unit tests. 32 | * Added port for GNU/Linux. 33 | * Configured CRC16 as CRC16-CCITT-False and CRC32 as CRC32-MPEG2 34 | * Public release of version 1.0.0. 35 | 36 | ----------------------------------------------------------------------------------------- 37 | 2022-03-09 Frank Voorburg 38 | 39 | * Added complete API reference to the documentation. 40 | * Added extra functionality for usinig MicroTBX in combination with FreeRTOS. 41 | * Configured GitHub actions for building and publishing the documentation. 42 | * Public release of version 0.9.5. 43 | 44 | ----------------------------------------------------------------------------------------- 45 | 2021-10-29 Frank Voorburg 46 | 47 | * Moved demos to a separate git repository. 48 | * Moved tbx_conf.h to the "template" subdirectory. 49 | * Public release of version 0.9.4. 50 | 51 | ----------------------------------------------------------------------------------------- 52 | 2020-04-09 Frank Voorburg 53 | 54 | * Added linked list module. 55 | * Removed Windows and Linux ports and demos. 56 | * Public release of version 0.9.3. 57 | 58 | ----------------------------------------------------------------------------------------- 59 | 2020-03-23 Frank Voorburg 60 | 61 | * Moved aes256 sources to the main source directory. 62 | * Public release of version 0.9.2. 63 | 64 | ----------------------------------------------------------------------------------------- 65 | 2020-03-01 Frank Voorburg 66 | 67 | * Minor fixes and improvements. 68 | * Public release of version 0.9.1. 69 | 70 | ----------------------------------------------------------------------------------------- 71 | 2019-08-22 Frank Voorburg 72 | 73 | * Basic functionality implemented. 74 | * Public release of version 0.9.0. 75 | 76 | ----------------------------------------------------------------------------------------- 77 | 2019-07-06 Frank Voorburg 78 | 79 | * Setup of GitHub project and kick-off for development. 80 | 81 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MicroTBX 2 | [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) [![MISRA Check](https://github.com/feaser/microtbx/actions/workflows/misra_check.yml/badge.svg)](https://github.com/feaser/microtbx/actions/workflows/misra_check.yml) [![Unit Tests](https://github.com/feaser/microtbx/actions/workflows/unit_tests.yml/badge.svg)](https://github.com/feaser/microtbx/actions/workflows/unit_tests.yml) [![Publish Docs](https://github.com/feaser/microtbx/actions/workflows/publish_docs.yml/badge.svg)](https://github.com/feaser/microtbx/actions/workflows/publish_docs.yml) 3 | 4 | MicroTBX is an open source Microcontroller ToolBoX consisting of software components commonly needed in embedded software applications. Think of it as a Swiss army knife for your firmware. The following software components are included in MicroTBX: 5 | 6 | * Assertions - For checking situations that should never happen. 7 | * Critical Sections - For mutual exclusive access to shared resources. 8 | * Heap - For static memory pre-allocation on the heap. 9 | * Memory Pools - For pool based dynamic memory allocation on the heap. 10 | * Linked Lists - For dynamically sized lists of data items. 11 | * Random Numbers - For generating random numbers. 12 | * Checksums - For calculating data checksums. 13 | * Cryptography - For data encryption and decryption. 14 | * Platform - For platform specifics. 15 | 16 | MicroTBX is written in the C programming language (C99) with MISRA compliance in mind. It currently supports all microcontrollers based on an ARM Cortex-M core, for example: ST STM32, Infineon XMC, and NXP S32K. There is little effort involved with porting MicroTBX to different microcontroller families. 17 | 18 | While developing MicroTBX, special care was given to making the code threadsafe. Access to shared resources are serialized with the help of critical sections. This makes it safe to use MicroTBX in a multithreaded environment, for example when your embedded software program is built upon a pre-emptive RTOS. 19 | 20 | If you do not feel like reinventing the wheel, consider using MicroTBX. Ideally, MicroTBX is integrated into your embedded software program at the start of each new project. The following image illustrates where MicroTBX fits into your firmware's architecture: 21 | 22 | ![alt text](docs/images/software_architecture.png "Software architecture with MicroTBX") 23 | 24 | # Getting Started 25 | 26 | To get started with MicroTBX, it is recommended to download the latest stable release from the [releases](https://github.com/feaser/microtbx/releases/) page. Next, follow the integration steps outlined on the first page of the [user manual](https://feaser.github.io/microtbx). 27 | 28 | Additionally, an assortment of demo programs is available in a separate Git repository: 29 | 30 | * https://github.com/feaser/microtbx-demos 31 | 32 | # User manual 33 | 34 | The online user manual is located at the following location: 35 | 36 | * [https://feaser.github.io/microtbx](https://feaser.github.io/microtbx) 37 | 38 | # Development 39 | 40 | Development of MicroTBX takes place at GitHub. Feel free to contribute by submitting issues and pull requests. 41 | 42 | * [https://github.com/feaser/microtbx/](https://github.com/feaser/microtbx/) 43 | 44 | # Contact 45 | 46 | Development and maintenance of MicroTBX is sponsored by Feaser. Feaser also offers integration/customization services, and professional technical support on a commercial basis: 47 | 48 | * [https://www.feaser.com](https://www.feaser.com) 49 | -------------------------------------------------------------------------------- /docs/assertions.md: -------------------------------------------------------------------------------- 1 | # Assertions 2 | 3 | Assertions are statements used for checking situations that should never happen. It is a good coding practice to add such statements to your software program. For example to validate the values of function 4 | parameters, to check pointers for not being NULL, to check that an array index is not out of range, etc. 5 | 6 | ## Usage 7 | 8 | In MicroTBX, assertions are implemented with the [`TBX_ASSERT(argument)`](apiref.md#generic) macro. The argument must evaluate 9 | to boolean `true` for the program to continue its execution as expected. If the argument evaluates to 10 | boolean `false`, an error is triggered. 11 | 12 | In this error case, MicroTBX jumps to an internal function with an infinite loop to hang the program. This 13 | function is called with two important parameters for debugging purposes: the filename of the source file 14 | that caused the assertion to fail and the line number inside this file where the assertion occurred. 15 | 16 | Practically, this means that when debugging and testing your software program, you simply set a breakpoint 17 | inside the internal function `TbxAssertTrigger()`. If an assertion is triggered, the debugger stops at the 18 | breakpoint. At this point you can inspect the function parameters `file` and `line` to determine which 19 | assertion statement caused the problem. Once you know the root cause of the assertion error, you can start 20 | working on a solution to fix the problem from happening in the future. 21 | 22 | In case you do not like the default assertion error handling, MicroTBX also makes it possible for you to configure your own assertion handler with function [`TbxAssertSetHandler()`](apiref.md#tbxassertsethandler). For example to execute a 23 | `printf` statement, to turn on an LED, or to write assertion error info to a file an on SD-card to indicate that an assertion error was detected. 24 | 25 | ## Examples 26 | 27 | The following example demonstrates how assertions can be used to test that a function parameter has a 28 | valid value range: 29 | 30 | ```c 31 | uint8_t myArray[4] = { 1, 2, 3, 4 }; 32 | 33 | void IncrementArrayElement(int8_t index) 34 | { 35 | /* Make sure the array index is positive. */ 36 | TBX_ASSERT(index >= 0); 37 | /* Make sure the array index is not out of range. */ 38 | TBX_ASSERT(index < (sizeof(myArray)/sizeof(myArray[0]))); 39 | 40 | /* Increment the array element at the requested index. */ 41 | myArray[index]++; 42 | } 43 | ``` 44 | 45 | The next example shows how you can register your own assertion error handler, which overrides the 46 | internal error handling: 47 | 48 | ```c 49 | #include 50 | 51 | void MyAssertionHandler(const char * const file, uint32_t line) 52 | { 53 | /* Display the assertion error info. */ 54 | printf("[ERROR] Assertion in %s at line %lu", file, line); 55 | } 56 | 57 | int main(void) 58 | { 59 | /* Register the application specific assertion handler. */ 60 | TbxAssertSetHandler(MyAssertionHandler); 61 | 62 | /* TODO Implement your program here. */ 63 | 64 | return 0; 65 | } 66 | ``` 67 | 68 | ## Configuration 69 | 70 | The enable the assertion error handling, set the macro [`TBX_CONF_ASSERTIONS_ENABLE`](apiref.md#configuration) to a value of `1`: 71 | 72 | ```c 73 | /** \brief Enable/disable run-time assertions. */ 74 | #define TBX_CONF_ASSERTIONS_ENABLE (1U) 75 | ``` 76 | 77 | To disable the assertion error handling, set this macro to a value of `0`. Note that there is not need 78 | to remove the [`TBX_ASSERT()`](apiref.md#generic) macros from your code. MicroTBX automatically makes the [`TBX_ASSERT()`](apiref.md#generic) macro and empty macro without side effect, when assertion error handling was disabled. 79 | 80 | Assertions are typically enabled in a debug version of the software program and disabled in a 81 | release version of the software program. 82 | 83 | ## Extra tips 84 | 85 | Make sure to write your assertion statements such that you do not accidentally remove functionality 86 | from your software program, after disabling the assertion error handling. So instead of the following: 87 | 88 | ```c 89 | FATFS fs; 90 | 91 | void MountDrive(void) 92 | { 93 | /* Mount the drive and make sure the drive mounting succeeded. */ 94 | TBX_ASSERT(f_mount(&fs, "0:", 0) == FR_OK); 95 | } 96 | ``` 97 | 98 | Do the following: 99 | 100 | ```c 101 | FATFS fs; 102 | 103 | void MountDrive(void) 104 | { 105 | FRESULT fresult; 106 | 107 | /* Mount the drive. */ 108 | fresult = f_mount(&fs, "0:", 0); 109 | 110 | /* Make sure the drive mounting succeeded. */ 111 | TBX_ASSERT(fresult == FR_OK); 112 | } 113 | ``` 114 | 115 | To be sure that the before mentioned problem does not occur, one could make the argument that assertion error handling should simply never be disabled. 116 | -------------------------------------------------------------------------------- /docs/checksum.md: -------------------------------------------------------------------------------- 1 | # Checksums 2 | 3 | A checksum is an integer value that can verify the contents of a data block. In 4 | embedded software programs, the common purposes of a checksum are: to validate 5 | the contents of a data bock, and to detect accidental changes in a data block. 6 | 7 | A checksum could for example be calculated over the entire program code that is 8 | programmed into flash memory. The checksum could then stored at another 9 | location in flash memory, with the help of the debugger / flash programmer. At program startup, the initialization code could recalculate the checksum and 10 | compare it with the value programmed by the debugger / flash programmer. It the 11 | values do not match, you can conclude that somehow the flash contents got 12 | changed and the software program might not execute properly. 13 | 14 | Checksums are also often embedded in communication data streams. The sender 15 | calculates and adds the checksum to the communication data. Upon reception, 16 | the receiver recalculates the checksum and compares the calculated value with 17 | the value that was added by the sender. The communication data is deemed valid 18 | if both values match. 19 | 20 | ## Usage 21 | 22 | MicroTBX includes functionality to calculate both 16-bit and 32-bit checksum 23 | values over a fixed length byte array, using the following cyclic redundancy check (CRC) 24 | algorithms: 25 | 26 | * CRC16-CCITT-FALSE 27 | * CRC32-MPEG2 28 | 29 | To calculate the 16-bit checksum value over a number of bytes, function [`TbxChecksumCrc16Calculate()`](apiref.md#tbxchecksumcrc16calculate) is available. In case a 32-bit checksum value 30 | is preferred, function [`TbxChecksumCrc32Calculate()`](apiref.md#tbxchecksumcrc32calculate) can be called. 31 | 32 | ## Examples 33 | 34 | The following example declares a data block with communication data, consisting 35 | of 32 bytes (8 * 4). Function `CalculateChecksum()` demonstrates how a 32-bit 36 | checksum over the communication data is calculated. The sender of this data 37 | packet could send the checksum value together with the actual packet data. 38 | 39 | The receiver could read out the received packet data and packet checksum value. 40 | Next, it calls function `VerifyChecksum()` to check if the packet data is valid 41 | and no bits got changed during the communication transfer. 42 | 43 | ```c 44 | uint32_t communicationData[8] = 45 | { 46 | 0x8ef9c15f, 0x4323a3cb, 0xb12ba488, 0xb3f5ec04, 47 | 0xc1c7544f, 0x4140ec9d, 0xc5dd421a, 0x14d57e3d 48 | }; 49 | 50 | uint32_t CalculateChecksum(void) 51 | { 52 | uint8_t result; 53 | uint8_t * dataPtr; 54 | size_t dataLen; 55 | 56 | /* Collect data block settings. */ 57 | dataPtr = (uint8_t *)&communicationData[0]; 58 | dataLen = sizeof(communicationData)/sizeof(uint8_t); 59 | /* Calculate the checksum. */ 60 | result = TbxChecksumCrc32Calculate(dataPtr, dataLen); 61 | /* Return the result. */ 62 | return result; 63 | } 64 | 65 | uint8_t VerifyChecksum(uint32_t checksum) 66 | { 67 | uint8_t result = TBX_FALSE; 68 | uint32_t calculatedChecksum; 69 | 70 | /* Recalculate the checksum. */ 71 | calculatedChecksum = CalculateChecksum(); 72 | /* Verify that the data contents are still valid. */ 73 | if (calculatedChecksum == checksum) 74 | { 75 | result = TBX_TRUE; 76 | } 77 | /* Return the result. */ 78 | return result; 79 | } 80 | ``` 81 | 82 | ## Configuration 83 | 84 | The 16-bit and 32-bit CRC algorithms uses a specific polynomial value and are 85 | seeded with an initial value. The default configuration of these values work 86 | fine and there is no need to change these. 87 | 88 | If for some reason your application requires you to set different values for the 89 | initial and polynomial values, you can override the default configuration by 90 | adding the following macros to the `tbx_conf.h` configuration file and assign 91 | the values you prefer: 92 | 93 | ```c 94 | /** \brief Polynomial of the 16-bit CRC. */ 95 | #define TBX_CONF_CHECKSUM_CRC16_POLYNOM (0x1021U) 96 | 97 | /** \brief Initial value of the 6-bit CRC calculation. */ 98 | #define TBX_CONF_CHECKSUM_CRC16_INITIAL (0xFFFFU) 99 | 100 | /** \brief Polynomial of the 32-bit CRC. */ 101 | #define TBX_CONF_CHECKSUM_CRC32_POLYNOM (0x04C11DB7UL) 102 | 103 | /** \brief Initial value of the 32-bit CRC calculation. */ 104 | #define TBX_CONF_CHECKSUM_CRC32_INITIAL (0xFFFFFFFFUL) 105 | ``` 106 | -------------------------------------------------------------------------------- /docs/critsect.md: -------------------------------------------------------------------------------- 1 | # Critical sections 2 | 3 | In an interrupt driven embedded software program, problems can occur with global resource sharing. A global resource can for example be a global data structure, a peripheral, or a communication device. 4 | 5 | Imagine a situation where the software program is writing to a global variable and before the write operation is completed, an interrupt occurred. The associated interrupt handler could write to the same global variable. After the interrupt is completed, to initial write operation completes. The global variable now holds invalid data. 6 | 7 | Such data concurrency issues are resolved by performing the global resource access in a critical section. In a critical section, no interrupt can occur and you can therefore be certain that you have mutual exclusive access to the global resource. 8 | 9 | ## Usage 10 | 11 | In MicroTBX a critical section is entered by calling the function [`TbxCriticalSectionEnter()`](apiref.md#tbxcriticalsectionenter). After this the global resource can be safely accessed. Once done, the critical section is left by calling the function [`TbxCriticalSectionExit()`](apiref.md#tbxcriticalsectionexit). 12 | 13 | This means that both functions must always be used pair-wise. Each call to [`TbxCriticalSectionEnter()`](apiref.md#tbxcriticalsectionenter) must eventually be followed by a call to [`TbxCriticalSectionExit()`](apiref.md#tbxcriticalsectionexit). Note that MicroTBX supports nested critical sections. It is completely fine if your software program enters the critical section again, even if it is already in a critical section. 14 | 15 | ## Examples 16 | 17 | The following example contains a global variable `myMessage` and a function `TransmitMessage()`. Imagine that this function can be called both from the main program loop and from an interrupt. This makes variable `myMessage` a shared resource. A critical section is applied to obtain mutual exclusive access to the variable `myMessage`: 18 | 19 | ```c 20 | struct 21 | { 22 | uint32_t id; 23 | uint8_t len; 24 | uint8_t data[8]; 25 | } myMessage; 26 | 27 | void TransmitMessage(void) 28 | { 29 | /* Obtain mutual exclusive access to myMessage. */ 30 | TbxCriticalSectionEnter(); 31 | /* Prepare and send the message. */ 32 | myMessage.id++; 33 | myMessage.len = 1u; 34 | myMessage.data[0]++; 35 | SendMessage(&myMessage); 36 | /* Release mutual exclusive access to myMessage. */ 37 | TbxCriticalSectionExit(); 38 | } 39 | ``` 40 | -------------------------------------------------------------------------------- /docs/crypto.md: -------------------------------------------------------------------------------- 1 | # Cryptography 2 | 3 | The cryptography software component contains functionality to encrypt and 4 | decrypt data blocks. Encryption essentially changes the data contents such 5 | that its contents can no longer be interpreted by unwanted third parties. Only 6 | the party that has access to the so called key, with which the data was 7 | encrypted, can decrypt the data contents back into its original state. 8 | 9 | Encryption enables you to securely protect data that you don't want anyone else 10 | to have access to. Possible fields of application in an embedded software 11 | program are: securing communication data, securing parameters or other 12 | proprietary data stored in EEPROM, etc. 13 | 14 | The cryptography software component is based on 256-bit AES ECB. AES stands for 15 | Advanced Encryption Standard and ECB stands for Electronic CodeBook. The key 16 | needed to perform the actual encryption and decryption is 256-bit in size. In 17 | C code, this is an array of 32 bytes. 18 | 19 | The only restriction is that the data to encrypt or decrypt must always be a 20 | multiple of 16 bytes in size. If this is not the case, the data needs to be 21 | aligned to a multiple of 16 bytes prior to performing the encryption/decryption 22 | operation. 23 | 24 | ## Usage 25 | 26 | The first step is to define your 256-bit cryptography key. This is nothing more 27 | than an array of 32 bytes. You can decide on the contents yourself. The only 28 | important part to remember is that you need the same key for both the 29 | encryption and decryption steps, in order to get the original data back when 30 | decrypting the encrypted data. Here is an example of how to define a 31 | cryptography key in C: 32 | 33 | ```c 34 | /* The 256-bit key that will be used to encrypt and decrypt data. */ 35 | const uint8_t cryptoKey[32] = 36 | { 37 | 0x32, 0x72, 0x35, 0x75, 0x38, 0x78, 0x21, 0x41, 38 | 0x25, 0x44, 0x2A, 0x47, 0x2D, 0x4B, 0x61, 0x50, 39 | 0x64, 0x53, 0x67, 0x56, 0x6B, 0x59, 0x70, 0x33, 40 | 0x73, 0x36, 0x76, 0x39, 0x79, 0x24, 0x42, 0x3F 41 | }; 42 | ``` 43 | 44 | Note that you can use an [encryption key generator](https://www.allkeysgenerator.com/Random/Security-Encryption-Key-Generator.aspx) to 45 | assist with getting unique contents for your cryptography key. 46 | 47 | To encrypt a 16-byte aligned data block, the function [`TbxCryptoAes256Encrypt()`](apiref.md#tbxcryptoaes256encrypt) 48 | is available. To decrypt the data block back to its original state, the function 49 | [`TbxCryptoAes256Decrypt()`](apiref.md#tbxcryptoaes256decrypt) can be called. 50 | 51 | ## Examples 52 | 53 | The following code example first encrypts the contents of a data buffer. 54 | Afterwards, the encrypted data is decrypted again. Followed by a verification 55 | to test that the decrypted data actually resembles the original data. The 56 | `cryptoKey[]` presented above serves as the 256-bit cryptography key: 57 | 58 | ```c 59 | /* Original data. */ 60 | const uint8_t originalData[64] = 61 | { 62 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 63 | 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 64 | 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 65 | 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 66 | 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 67 | 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 68 | 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 69 | 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F 70 | }; 71 | 72 | /* Temporary buffer to storing the encrypted/decrypted data. */ 73 | uint8_t buffer[64]; 74 | 75 | /* Array indexer for looping through array data. */ 76 | size_t idx; 77 | 78 | /* Copy the original data to the buffer. */ 79 | for (idx = 0U; idx < 64U; idx++) 80 | { 81 | buffer[idx] = originalData[idx]; 82 | } 83 | 84 | /* Encrypt the data in the buffer. */ 85 | TbxCryptoAes256Encrypt(buffer, 64, cryptoKey); 86 | 87 | /* Decrypt the data in the buffer. */ 88 | TbxCryptoAes256Decrypt(buffer, 64, cryptoKey); 89 | 90 | /* Verify that the decrypted data is the same as the original data. */ 91 | for (idx = 0U; idx < 64U; idx++) 92 | { 93 | if (buffer[idx] != originalData[idx]) 94 | { 95 | TBX_ASSERT(TBX_ERROR); 96 | } 97 | } 98 | ``` 99 | -------------------------------------------------------------------------------- /docs/extra.md: -------------------------------------------------------------------------------- 1 | # Extra functionality 2 | 3 | You can find some optional and extra functionality in the directory `source/extra`. This section provides additional information regarding this functionality. 4 | 5 | ## MicroTBX for FreeRTOS 6 | 7 | The [FreeRTOS](https://www.freertos.org) real-time operating system is widely used in the embedded industry. No wonder, because it's free, open source, high quality, MISRA compliant and maintained by [AWS](https://aws.amazon.com/). 8 | 9 | Whenever you use both FreeRTOS and MicroTBX, a few pieces of functionality are redundant. Both MicroTBX and FreeRTOS offer heap management and assertion functionality. There is nothing wrong with using both. However, if you'd like to reap the benefits of MicroTBX, while also using FreeRTOS, consider using the files in directory: 10 | 11 | * `source/extra/freertos/` 12 | 13 | ### Heap management 14 | 15 | FreeRTOS ships with a few different examples for heap management. Some allow just one-time memory allocation, some feature functionality to release allocated memory again, with minimal memory fragmentation. You can find these examples in directory: 16 | 17 | * `FreeRTOS/Source/portable/MemMang/` 18 | 19 | The file `tbx_freertos.c` offers an alternative heap management implementation, using MicroTBX's memory pools. This allows you to dynamically allocate and release memory on the heap, for your application's FreeRTOS objects. 20 | 21 | To use this heap management solution, you just need to remove the `heap_x.c` source file from your project and compile and link `tbx_freertos.c` instead. 22 | 23 | ### Assertions 24 | 25 | In the FreeRTOS configuration header file `FreeRTOSConfig.h`, you can add and configure the `configASSERT` macro to enable assertions in the FreeRTOS code base. MicroTBX includes an assertion module that you can use for this. The easiest way to link the MicroTBX assertion `TBX_ASSERT` macro to the FreeRTOS `configASSERT` macro, is by including the `tbx_freertos.h` header file all the way at the end. Just before the last `#endif`: 26 | 27 | ```c 28 | #define INCLUDE_vTaskCleanUpResources 1 29 | #define INCLUDE_vTaskSuspend 1 30 | #define INCLUDE_vTaskDelayUntil 1 31 | #define INCLUDE_vTaskDelay 1 32 | #define INCLUDE_uxTaskGetStackHighWaterMark 1 33 | 34 | /* Use MicroTBX assertion in FreeRTOS. */ 35 | #include "tbx_freertos.h" /* <---- ADD THIS LINE */ 36 | 37 | #endif /* FREERTOS_CONFIG_H */ 38 | ``` 39 | 40 | Just make sure to add the directory, where the `tbx_freertos.h` resides, to your compiler's search path for included header files. 41 | 42 | Alternatively, you can directly add and configure the `configASSERT` macro as follows in `FreeRTOSConfig.h`: 43 | 44 | ```c 45 | #include "microtbx.h" 46 | #define configASSERT( x ) TBX_ASSERT( x ) 47 | ``` 48 | 49 | ## C++ new and delete using MicroTBX memory pools 50 | 51 | On a microcontroller it is totally fine to dynamically allocate memory on the heap using `new` (or `malloc`). It gets potentially troublesome when you also release it at run-time using `delete` (or `free`). Multiple allocation and release operations can cause memory fragmentation. In a worst case scenario this leads to memory allocation failures, because of running out of heap memory. 52 | 53 | A possible solution is by dynamically allocating memory using memory pools. This lowers the risk of memory fragmentation. With a carefully selected memory pool setup, you can even eliminate this risk completely. Allowing you to dynamically allocate and release memory during run-time. MicroTBX includes a ready-made memory pool module that can be used for this purpose. 54 | 55 | One way to approach this is by using C++ *placement new* instead of the usual *new*. This allows you the first allocate memory from a memory pool and then place the new object exactly at that memory, instead of having the `new` operator do the allocation. The only issue with this is that there is no *placement delete*. This means that to delete the object, you need to manually call its destructor and then give the allocated memory back to the memory pool. Definitely an option, it just requires a bit more work. 56 | 57 | Another approach is to overload the default `new` and `delete` operators to do all the memory allocation and release using memory pools automatically. The following source-file implements these operator overloads for the [GNU ARM Embedded](https://developer.arm.com/Tools%20and%20Software/GNU%20Toolchain) toolchain: 58 | 59 | * `source/extra/cplusplus/tbxcxx.cpp` 60 | 61 | By compiling and linking this source file with your project, the global `new` and `delete` operators are overloaded, such that they by default always use the memory pools module of MicroTBX. This also apply to objects created using smart pointers. 62 | 63 | -------------------------------------------------------------------------------- /docs/heap.md: -------------------------------------------------------------------------------- 1 | # Heap 2 | 3 | In an embedded software program, memory can be dynamically allocated. The heap is 4 | available for this, which is a part of RAM specifically reserved for dynamically 5 | allocated memory. 6 | 7 | The C standard library offers the functions `malloc()` and `free()` for allocating 8 | and releasing memory on the heap. Both functions treat the heap as a byte pool, which 9 | means there is always a fragmentation risk present. The fragmentation eventually 10 | leads to a situation where memory can no longer be allocated, resulting in lost 11 | functionality or a crash of the software program in the worst case. 12 | 13 | This fragmentation risk is therefore unacceptable and another solution needs to be 14 | found for handling dynamic allocation. The solution is two-fold: 15 | 16 | 1. Use static memory preallocation. This entails performing the memory 17 | allocation once during software program initialization and never releasing 18 | (free-ing) the allocated memory. This means that the lifetime of the allocated data spans the run-time of the entire software program. 19 | 2. Use memory pools. With a memory pool, memory is allocated as fixed-size memory 20 | blocks (partitions). The benefit of dynamic memory allocation using memory 21 | pools is that the memory can be released again without the before mentioned 22 | fragmentation risk. It is perfect in situations when temporary data storage is needed, and the lifetime of the data is longer than just one function. 23 | 24 | MicroTBX offers software components for both solutions. The heap software 25 | component described in this section covers solution (1) with static memory 26 | preallocation. The [memory pools](mempools.md) software component covers 27 | solution (2). 28 | 29 | ## Usage 30 | 31 | The heap software component offers the function [`TbxHeapAllocate()`](apiref.md#tbxheapallocate) for performing 32 | memory allocation on the heap. Because it is meant for static memory preallocation 33 | only, there is no function for free-ing the allocated memory. The idea is that 34 | function [`TbxHeapAllocate()`](apiref.md#tbxheapallocate) is only called during the initialization of the 35 | software program, so before the infinite program loop is entered. It is basically 36 | its own implementation of `malloc()`, without `free()`. 37 | 38 | To find out how many bytes of memory are still available on the heap, function 39 | [`TbxHeapGetFree()`](apiref.md#tbxheapgetfree) can be called. 40 | 41 | If a software program has a need to allocate and release memory during the 42 | infinite program loop, the memory allocation should be performed with the 43 | functionality present in the [memory pools](mempools.md) software component. 44 | 45 | ## Examples 46 | 47 | The following example demonstrates how to call the functions of the heap software 48 | component. Memory for a FIFO buffer is preallocated during the software program 49 | initialization. 50 | 51 | ```c 52 | /* A first-in first-out buffer of 32-bit values. */ 53 | uint32_t * fifoBuffer = NULL; 54 | uint32_t const fifoMaxSize = 32; 55 | uint8_t fifoIdx; 56 | 57 | /* Make sure there is enough space on the heap for the FIFO buffer. */ 58 | if (TbxHeapGetFree() < (fifoMaxSize * sizeof(uint32_t))) 59 | { 60 | TBX_ASSERT(TBX_FALSE); 61 | } 62 | 63 | /* Perform static preallocation of the FIFO buffer. */ 64 | fifoBuffer = TbxHeapAllocate(fifoMaxSize * sizeof(uint32_t)); 65 | 66 | /* Make sure the allocation was successful. */ 67 | TBX_ASSERT(fifoBuffer != NULL); 68 | 69 | /* Initialize the FIFO buffer. */ 70 | for (fifoIdx = 0; fifoIdx < fifoMaxSize; fifoIdx++) 71 | { 72 | fifoBuffer[fifoIdx] = 0; 73 | } 74 | ``` 75 | 76 | ## Configuration 77 | 78 | The maximum size of the heap is configured with macro [`TBX_CONF_HEAP_SIZE`](apiref.md#configuration): 79 | 80 | ```c 81 | /** \brief Configure the size of the heap in bytes. */ 82 | #define TBX_CONF_HEAP_SIZE (2048U) 83 | ``` 84 | -------------------------------------------------------------------------------- /docs/images/software_architecture.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/feaser/microtbx/3185f963036b412beedc8f7def5deef85deecfa6/docs/images/software_architecture.png -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | # Introduction to MicroTBX 2 | 3 | MicroTBX is an open source Microcontroller ToolBoX consisting of software components commonly needed in embedded software applications. Think of it as a Swiss army knife for your firmware. 4 | 5 | MicroTBX is written in the C programming language (C99). It currently supports all microcontrollers based on an ARM Cortex-M core, for example: ST STM32, Infineon XMC, and NXP S32K. There is little effort involved with porting MicroTBX to different microcontroller families. 6 | 7 | While developing MicroTBX, special care was given to making the code threadsafe. Access to shared resources are serialized with the help of [critical sections](critsect.md). This make it safe to use MicroTBX in a multithreaded environment, for example when your embedded software program is built upon a pre-emptive RTOS. 8 | 9 | If you do not feel like reinventing the wheel, consider using MicroTBX. Ideally, MicroTBX is integrated into your embedded software program at the start of each new project. The following image illustrates where MicroTBX fits into your firmware's architecture: 10 | 11 | ![alt text](images/software_architecture.png "Software architecture with MicroTBX") 12 | 13 | ## Components 14 | 15 | The following table presents an overview of the currently supported software components inside MicroTBX: 16 | 17 | | Name | Description | 18 | | :------------------------------------ | :---------- | 19 | | [Assertions](assertions.md) | For checking situations that should never happen. | 20 | | [Critical Sections](critsect.md) | For mutual exclusive access to shared resources. | 21 | | [Heap](heap.md) | For static memory pre-allocation on the heap. | 22 | | [Memory Pools](mempools.md) | For pool based dynamic memory allocation on the heap. | 23 | | [Linked Lists](lists.md) | For dynamically sized lists of data items. | 24 | | [Random Numbers](random.md) | For generating random numbers. | 25 | | [Checksums](checksum.md) | For calculating data checksums. | 26 | | [Cryptography](crypto.md) | For data encryption and decryption. | 27 | | [Platform](platform.md) | For platform specifics. | 28 | 29 | ## MISRA 30 | 31 | MicroTBX was developed with [MISRA-C compliance](misra.md) in mind. This is a software development standard to facilitate best practices for programming safety-critical software in road vehicles and other embedded systems. 32 | 33 | ## Integration 34 | 35 | Adding MicroTBX to your software project is a simple five step process: 36 | 37 | 1. Copy all the files from the `source` and the correct `source/port/XXX` port directory to your project. 38 | 2. Copy the `source/template/tbx_conf.h` template configuration file to your project 39 | 3. Configure your project such that the added `.c` files are compiled and linked during a build. 40 | 4. Add the directories that contain the `.h` files to your compiler's include search path. 41 | 5. Customize the MicroTBX configuration in `tbx_conf.h`, if desired. 42 | 43 | Alternatively, when you use [CMake](https://cmake.org/) to manage your project's the build environment, you can use `add_subdirectory()` to register the MicroTBX interface library and add the desired libraries to your project's `target_link_libraries()`. A minimal `CMakeLists.txt` example follows, which assumes that: 44 | 45 | * You copied MicroTBX to directory `third_party/microtbx` . 46 | * You copied `source/template/tbx_conf.h` to the same directory as where your `main.c` resides. 47 | * You are using an ARM Cortex-M based microcontroller. 48 | 49 | ```cmake 50 | project(MyProject) 51 | 52 | add_subdirectory(third_party/microtbx) 53 | 54 | add_executable(MyProject 55 | main.c 56 | ) 57 | 58 | target_link_libraries(MyProject 59 | microtbx 60 | microtbx-cortexm 61 | ) 62 | ``` 63 | 64 | 65 | 66 | ## Usage 67 | 68 | 1. Add the following line to each source-file, where you intend to make use of MicroTBX: 69 | ```c 70 | #include 71 | ``` 72 | 73 | ## Licensing 74 | 75 | MicroTBX is licensed under the [MIT license](license.md). This permissive license enables you to include and distribute MicroTBX with your firmware, even if it is proprietary. The only requirements are that you include a copy of the MIT license and do not change the copyright notice, when you distribute MicroTBX. 76 | -------------------------------------------------------------------------------- /docs/license.md: -------------------------------------------------------------------------------- 1 | # License 2 | 3 | MicroTBX is released under the MIT license. This permissive license enables you to include and distribute MicroTBX with your firmware, even if it is proprietary. Refer to the following table for answers to frequently asked questions regarding the MIT license. 4 | 5 | | Question | MIT license | 6 | | :------------------------------------------------------------------ | :---------: | 7 | | Is MicroTBX free? | yes | 8 | | Do I have the right to change the MicroTBX source code? | yes | 9 | | Can I use MicroTBX in my closed source product? | yes | 10 | | Do I have to open my source code? | no | 11 | | Do I have to open source my changes to MicroTBX? | no | 12 | | Do I have to offer the MicroTBX source code to users of my product? | no | 13 | | Do I have to document that my product uses MicroTBX? | no | 14 | | Can I redistribute MicroTBX in source code format? | yes | 15 | | Can I receive professional technical support on a commercial basis? | yes | 16 | 17 | The actual license text is as follows: 18 | 19 | ```c 20 | /*--------------------------------------------------------------------------------------- 21 | * C O P Y R I G H T 22 | *---------------------------------------------------------------------------------------- 23 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 24 | * 25 | *---------------------------------------------------------------------------------------- 26 | * L I C E N S E 27 | *---------------------------------------------------------------------------------------- 28 | * Permission is hereby granted, free of charge, to any person obtaining a copy 29 | * of this software and associated documentation files (the "Software"), to deal 30 | * in the Software without restriction, including without limitation the rights 31 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 32 | * copies of the Software, and to permit persons to whom the Software is 33 | * furnished to do so, subject to the following conditions: 34 | * 35 | * The above copyright notice and this permission notice shall be included in all 36 | * copies or substantial portions of the Software. 37 | * 38 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 39 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 40 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 41 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 42 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 43 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 44 | * SOFTWARE. 45 | */ 46 | ``` 47 | -------------------------------------------------------------------------------- /docs/lists.md: -------------------------------------------------------------------------------- 1 | # Linked lists 2 | 3 | This software component consists of an easy-to-use set of functions for managing and 4 | sorting items in a linked list. It builds upon the [memory pool](mempools.md) 5 | functionality, which makes is possible to dynamically create, delete and automatically 6 | grow linked lists in a convenient way. Think of a linked list as an array on steroids. 7 | You do not yet have to know its size upon creation and you can insert, remove and 8 | sort its items with ease. 9 | 10 | A linked list is a sequence of data items, where each data item contains a connection 11 | to the adjacent items. In this software component the definition of a data item is 12 | a pointer to the data. The only thing you as the programmer need to take care of, 13 | is that the data pointer actually points to valid data before inserting it into 14 | the linked list. 15 | 16 | Linked lists are commonly used data structures in software programs and they are 17 | perfect for scenarios where you need to store a sequence of data, but you do not 18 | yet know the maximum number of items to store ahead of time. An example would be a 19 | first-in-first-out (FIFO) buffer. 20 | 21 | ## Usage 22 | 23 | Linked list can be created and deleted at any time in the software program with 24 | functions [`TbxListCreate()`](apiref.md#tbxlistcreate) and [`TbxListDelete()`](apiref.md#tbxlistdelete). The [`TbxListCreate()`](apiref.md#tbxlistcreate) function 25 | returns a pointer to the new list. This list pointer serves as a handle to the list. 26 | You can pass this handle as a parameter to all the other functions in the software 27 | component to identify the list that should be operated on. 28 | 29 | Once the list is created, you can start adding items to the list with functions 30 | [`TbxListInsertItemFront()`](apiref.md#tbxlistinsertitemfront), [`TbxListInsertItemBack()`](apiref.md#tbxlistinsertitemback), [`TbxListInsertItemBefore()`](apiref.md#tbxlistinsertitembefore), 31 | and [`TbxListInsertItemAfter()`](apiref.md#tbxlistinsertitemafter). The function to call depends on where in the list 32 | you would like to add the item. 33 | 34 | For reading items and for iterating over items, the functions [`TbxListGetFirstItem()`](apiref.md#tbxlistgetfirstitem), 35 | [`TbxListGetLastItem()`](apiref.md#tbxlistgetlastitem), [`TbxListGetPreviousItem()`](apiref.md#tbxlistgetpreviousitem), and [`TbxListGetNextItem()`](apiref.md#tbxlistgetnextitem) are 36 | available. 37 | 38 | At any given time, you can obtain the number of items that are stored in the list 39 | with function [`TbxListGetSize()`](apiref.md#tbxlistgetsize). Call function [`TbxListRemoveItem()`](apiref.md#tbxlistremoveitem) to remove 40 | a single item from the list, or call [`TbxListClear()`](apiref.md#tbxlistclear) to remove all items at once. 41 | 42 | For editing the order of the items in the list, functions [`TbxListSwapItems()`](apiref.md#tbxlistswapitems) and 43 | [`TbxListSortItems()`](apiref.md#tbxlistsortitems) are available. When calling [`TbxListSortItems()`](apiref.md#tbxlistsortitems) you can 44 | specify your own function that will be called during the sort operation. In this 45 | callback function you can implement your own application specific logic for 46 | comparing two data items, therefore giving you full control and flexibility 47 | over how the sorting works. 48 | 49 | ## Examples 50 | 51 | This section contains a few examples to demonstrate how the linked list software 52 | component works. To keep the examples simple, some error checking of function 53 | return values was omitted. The examples assume that the following type for an 54 | arbitrary message is defined: 55 | 56 | ```c 57 | typedef struct 58 | { 59 | uint32_t id; 60 | uint8_t len; 61 | uint8_t data[8]; 62 | } tMsg; 63 | ``` 64 | 65 | ### Example 1 - FIFO buffer 66 | 67 | The first example demonstrates how quickly a first-in-first-out (FIFO) buffer 68 | can be created. Keep in mind that a linked list holds pointers to data items. It 69 | is up to the developer to allocate and release the memory for the data item. 70 | Luckily, this is easy with the help of a [memory pool](mempools.md). 71 | 72 | ```c 73 | tTbxList * msgBuffer; 74 | 75 | void MsgBufferInit(void) 76 | { 77 | /* Create the linked list. */ 78 | msgBuffer = TbxListCreate(); 79 | /* Create message memory pool with an initial size of 1. */ 80 | TbxMemPoolCreate(1, sizeof(tMsg)); 81 | } 82 | 83 | void MsgBufferAdd(tMsg const * msg) 84 | { 85 | tMsg * newMsg; 86 | 87 | /* Attempt to allocate memory to store the message. */ 88 | newMsg = TbxMemPoolAllocate(sizeof(tMsg)); 89 | /* Automatically increase the memory pool if it was too small. */ 90 | if (newMsg == NULL) 91 | { 92 | TbxMemPoolCreate(1, sizeof(tMsg)); 93 | newMsg = TbxMemPoolAllocate(sizeof(tMsg)); 94 | } 95 | /* Copy the message. */ 96 | *newMsg = *msg; 97 | /* Add the message at the end of the list. */ 98 | TbxListInsertItemBack(msgBuffer, newMsg); 99 | } 100 | 101 | uint8_t MsgBufferGet(tMsg * msg) 102 | { 103 | uint8_t result = TBX_ERROR; 104 | tMsg * oldMsg; 105 | uint8_t idx; 106 | 107 | /* Get the oldest message from the list, if any. */ 108 | oldMsg = TbxListGetFirstItem(msgBuffer); 109 | if (oldMsg != NULL) 110 | { 111 | /* Delete it from the list now that we read it. */ 112 | TbxListRemoveItem(msgBuffer, oldMsg); 113 | /* Copy the message contents. */ 114 | *msg = *oldMsg; 115 | /* Give the allocated memory for the message back to the pool. */ 116 | TbxMemPoolRelease(oldMsg); 117 | /* Update the result. */ 118 | result = TBX_OK; 119 | } 120 | return result; 121 | } 122 | ``` 123 | 124 | You now not only have a FIFO buffer for storing messages, but its implementation 125 | is such that it can automatically grow in size. Memory pools form the foundation 126 | for this, meaning that you do not have to worry about memory fragmentation. 127 | 128 | The following code demonstrates how messages can be stored and retrieved from this 129 | FIFO buffer: 130 | 131 | ```c 132 | /* Initialize the message FIFO buffer. */ 133 | MsgBufferInit(); 134 | 135 | /* Add a few messages. */ 136 | tMsg newMsg1 = { 0x200, 8, { 1, 2, 3, 4, 5, 6, 7, 8} }; 137 | tMsg newMsg2 = { 0x300, 8, { 1, 2, 3, 4, 5, 6, 7, 8} }; 138 | tMsg newMsg3 = { 0x100, 8, { 1, 2, 3, 4, 5, 6, 7, 8} }; 139 | MsgBufferAdd(&newMsg1); 140 | MsgBufferAdd(&newMsg2); 141 | MsgBufferAdd(&newMsg3); 142 | 143 | /* Extract messages from the FIFO buffer. */ 144 | tMsg msg; 145 | while (MsgBufferGet(&msg) == TBX_OK) 146 | { 147 | printf(" Item ID: 0x%x\n", msg.id); 148 | } 149 | ``` 150 | 151 | After running this code, the following is shown: 152 | 153 | ``` 154 | Item ID: 0x200 155 | Item ID: 0x300 156 | Item ID: 0x100 157 | ``` 158 | 159 | ### Example 2 - Iterate list 160 | 161 | The second example demonstrates how to iterate through contents of a linked list. 162 | The following function prints information about the list contents: 163 | 164 | ```c 165 | void MsgBufferPrint(void) 166 | { 167 | tMsg * msg; 168 | size_t cnt = 0; 169 | 170 | printf("Number of items: %d\n", TbxListGetSize(msgBuffer)); 171 | msg = TbxListGetFirstItem(msgBuffer); 172 | while (msg != NULL) 173 | { 174 | printf(" Item %d ID: 0x%x\n", ++cnt, msg->id); 175 | msg = TbxListGetNextItem(msgBuffer, msg); 176 | } 177 | } 178 | ``` 179 | 180 | After running this example code: 181 | 182 | ```c 183 | /* Initialize the message FIFO buffer. */ 184 | MsgBufferInit(); 185 | 186 | /* Add a few messages. */ 187 | tMsg newMsg1 = { 0x200, 8, { 1, 2, 3, 4, 5, 6, 7, 8} }; 188 | tMsg newMsg2 = { 0x300, 8, { 1, 2, 3, 4, 5, 6, 7, 8} }; 189 | tMsg newMsg3 = { 0x100, 8, { 1, 2, 3, 4, 5, 6, 7, 8} }; 190 | MsgBufferAdd(&newMsg1); 191 | MsgBufferAdd(&newMsg2); 192 | MsgBufferAdd(&newMsg3); 193 | 194 | /* Print current FIFO buffer contents. */ 195 | MsgBufferPrint(); 196 | ``` 197 | 198 | The following is shown: 199 | 200 | ``` 201 | Number of items: 3 202 | Item 1 ID: 0x200 203 | Item 2 ID: 0x300 204 | Item 3 ID: 0x100 205 | ``` 206 | 207 | ### Example 3 - Sort list 208 | 209 | The third example builds upon example 2, where there are three messages in the 210 | list. Let's assume you want to sort the list contents based on the message 211 | identifier. For this you could implement the following function for comparing 212 | two messages: 213 | 214 | ```c 215 | uint8_t CompareMessage(void const * item1, void const * item2) 216 | { 217 | uint8_t result = TBX_FALSE; 218 | tMsg const * msg1 = item1; 219 | tMsg const * msg2 = item2; 220 | 221 | if (msg1->id > msg2->id) 222 | { 223 | result = TBX_TRUE; 224 | } 225 | return result; 226 | } 227 | ``` 228 | 229 | To perform the actual sort operation, you could run this code: 230 | 231 | ```c 232 | /* Print FIFO buffer contents before sorting. */ 233 | printf("--- Before sorting ---\n"); 234 | MsgBufferPrint(); 235 | 236 | /* Sort the buffer based on the message identifier. */ 237 | TbxListSortItems(msgBuffer, CompareMessage); 238 | 239 | /* Print FIFO buffer contents after sorting. */ 240 | printf("--- After sorting ---\n"); 241 | MsgBufferPrint(); 242 | ``` 243 | 244 | The following is shown: 245 | 246 | ``` 247 | --- Before sorting --- 248 | Number of items: 3 249 | Item 1 ID: 0x200 250 | Item 2 ID: 0x300 251 | Item 3 ID: 0x100 252 | --- After sorting --- 253 | Number of items: 3 254 | Item 1 ID: 0x100 255 | Item 2 ID: 0x200 256 | Item 3 ID: 0x300 257 | ``` 258 | 259 | Note that the items in the list are now sorted based on the message identifier 260 | in ascending order. 261 | 262 | ## Configuration 263 | 264 | The linked list software component itself does not have to be configured. However, when 265 | creating a linked list and inserting items into it, the memory needed is dynamically 266 | allocated with the help of a memory pool. Because a memory pool takes memory from the 267 | heap, make sure the heap size is configured large enough with the help of macro 268 | [`TBX_CONF_HEAP_SIZE`](apiref.md#configuration): 269 | 270 | ```c 271 | /** \brief Configure the size of the heap in bytes. */ 272 | #define TBX_CONF_HEAP_SIZE (2048U) 273 | ``` 274 | -------------------------------------------------------------------------------- /docs/mempools.md: -------------------------------------------------------------------------------- 1 | # Memory pools 2 | 3 | Dynamic memory allocation with the help of a memory pool is required for data that has a lifetime longer than just one function, yet shorter than the run-time of the entire software program. This is typically the case in event driven software. 4 | 5 | Take for example an embedded software program that communicates via CAN. When a CAN message reception interrupt triggers, the contents of the newly received CAN message are typically stored somewhere for later processing at task level. In this case data storage for the CAN message can be dynamically allocated and freed when it is no longer needed. 6 | 7 | The C standard library functions `malloc()` and `free()` can unfortunately not be used for this in a RAM constrained microcontroller based system. Both functions treat the heap as 8 | a byte pool, which means there is always a fragmentation risk present. This risk is unacceptable for an embedded software program. 9 | 10 | Note that if dynamic memory allocation is needed, where the lifetime of the allocated data equals the run-time of the entire software program, it is better to use the method of static memory preallocation with the help of the [heap](heap.md) software component. The heap software component is also a part of MicroTBX. 11 | 12 | Each memory pool consists of a run-time configurable amount of fixed-sized data blocks. The software program has full control over how many memory pools it creates and how many bytes the data blocks. 13 | 14 | ## Usage 15 | 16 | A prerequisite to performing dynamic memory allocation, is that the software program first 17 | creates one or more memory pools. Function [`TbxMemPoolCreate()`](apiref.md#tbxmempoolcreate) creates a memory pool. Through the function parameters you can specify the number of bytes for each data block inside the pool and how many data blocks it should hold. 18 | 19 | It is recommended to create the memory pools during the software program initialization, before the infinite program loop is entered. However, it is safe to create memory pools at any point in the software program. Note that if you attempt to create a memory pool with a data block size that already exists, the memory pools software component is smart enough to 20 | extend the already existing memory pool, instead of creating a new one. 21 | 22 | 23 | The trick is to tune the memory pools to your specific software program needs. When unsure about the what memory pools to create, it is a good starting point to create memory pools with sizes that are powers of two. For example, 8, 16, 32, 64, 128, etc. 24 | 25 | Once the memory pools are created, memory allocation with the memory pool software component is actually quite similar to calling the C standard library functions. To allocate memory, call [`TbxMemPoolAllocate()`](apiref.md#tbxmempoolallocate) instead of `malloc()`. The best fitting memory pool for the data size requested, is automatically selected. Once the allocated data is no longer needed, call [`TbxMemPoolRelease()`](apiref.md#tbxmempoolrelease), instead of `free()`. 26 | 27 | ## Examples 28 | 29 | The following example program demonstrates how memory pools are created and proves that data from the memory pools can be dynamically allocated and released over and over again. It it also an example of how you can expand and 30 | existing memory pool at a later point in time. 31 | 32 | ```c 33 | void main(void) 34 | { 35 | size_t idx; 36 | uint8_t * dataPtr[16]; 37 | uint8_t firstLoop = TBX_TRUE; 38 | 39 | /* Create 2 memory pools with block sizes 16 and 32 that have 8 40 | * data blocks per pool. 41 | */ 42 | TbxMemPoolCreate(8U, 16U); 43 | TbxMemPoolCreate(8U, 32U); /* Not actually used in this example. */ 44 | 45 | /* Enter the infinite program loop. */ 46 | for (;;) 47 | { 48 | /* Allocate all blocks in the memory pool with size 16. A smaller 49 | * block size (12 in this case) is automatically matched to the 50 | * memory pool with the next size up (16 in this case). 51 | */ 52 | for (idx = 0U; idx < 8U; idx++) 53 | { 54 | dataPtr[idx] = TbxMemPoolAllocate(12U); 55 | TBX_ASSERT(dataPtr[idx] != NULL); 56 | } 57 | 58 | /* Note that when the memory pool with block sizes of 16 bytes is 59 | * exhausted, the memory pool module does NOT automatically switch 60 | * to a memory pool with larger sized blocks. This was done on 61 | * purpose by design. 62 | * 63 | * The reason for this is that it is now possible to expand an 64 | * existing memory pool when it is full. Assume a situation where 65 | * all blocks in the memory pool are already allocated. The next 66 | * call to TbxMemPoolAllocate() therefore fails. You could now 67 | * call TbxMemPoolCreate() again for the same block size and the 68 | * original memory pool is expanded automatically. 69 | * 70 | * This is actually a very powerful feature, as it enables you 71 | * to dynamically enlarge the memory pool whenever needed, 72 | * provided the heap is configured to be large enough. A 73 | * simplified demonstration of this follows here. 74 | */ 75 | if (firstLoop == TBX_TRUE) 76 | { 77 | /* Reset the flag because this part should only execute during 78 | * the first loop iteration. 79 | */ 80 | firstLoop = TBX_FALSE; 81 | /* During the first loop iteration, the next allocation should 82 | * fail because all 8 blocks in the memory pool with size 16 83 | * are already allocated. 84 | */ 85 | TBX_ASSERT(TbxMemPoolAllocate(16U) == NULL); 86 | /* Now expand the memory pool with block size 16 to add 87 | * another 8 blocks. 88 | */ 89 | TbxMemPoolCreate(8U, 16U); 90 | } 91 | 92 | /* The memory pool with block size of 16 should now have 16 93 | * blocks. 8 were allocated, so it should be possible to 94 | * allocate another 8. 95 | */ 96 | for (idx = 8U; idx < 16U; idx++) 97 | { 98 | dataPtr[idx] = TbxMemPoolAllocate(12U); 99 | TBX_ASSERT(dataPtr[idx] != NULL); 100 | } 101 | 102 | /* The memory pool with block size of 16 should now be 103 | * exhausted and it should therefore not be possible 104 | * to allocate another block it in. Let's test this. 105 | */ 106 | TBX_ASSERT(TbxMemPoolAllocate(16U) == NULL); 107 | 108 | /* Now release all the allocated data blocks again. */ 109 | for (idx = 0U; idx < 16U; idx++) 110 | { 111 | TbxMemPoolRelease(dataPtr[idx]); 112 | } 113 | } 114 | } 115 | ``` 116 | 117 | ## Configuration 118 | 119 | The memory pool software component itself does not have to be configured. However, when 120 | creating memory pools with function [`TbxMemPoolCreate()`](apiref.md#tbxmempoolcreate), the data blocks are statically preallocated on the heap with the help of the [heap](heap.md) module. In case the memory pool creation fails, it is likely that the heap size needs to be increased using the macro [`TBX_CONF_HEAP_SIZE`](apiref.md#configuration): 121 | 122 | ```c 123 | /** \brief Configure the size of the heap in bytes. */ 124 | #define TBX_CONF_HEAP_SIZE (2048U) 125 | ``` 126 | -------------------------------------------------------------------------------- /docs/misra.md: -------------------------------------------------------------------------------- 1 | # MISRA compliance 2 | 3 | Static code analysis was performed to verify compliance with MISRA-C 2012. This document lists the compliance exceptions: 4 | 5 | ## Global 6 | 7 | | Directive | Type | Rationale | 8 | | :-------: | :------: | :----------------------------------------------------------- | 9 | | 2.5 | advisory | Especially in reusable modules or peripheral drivers, macro definitions
can remain unused in the module or driver itself, but should be kept
for the end-user. For example version macros and configuration options. | 10 | | 11.5 | advisory | Conversions from pointer to void to pointer to other type. This is needed
after allocating memory from the heap and then initializing a pointer to
point to this allocated memory. Used for example when allocating memory
to build a linked list. | 11 | 12 | ## Incidental 13 | 14 | The exceptions listed in this section only apply to specific incidents in the code. 15 | 16 | ### External linkage for API functions 17 | 18 | A standalone library module such as this one contains API functions. They are meant to be called by the application that integrates and uses the module. As such, it can happen that an API function is not referenced outside the source-file (translation unit) that holds the function implementation. Yet, it should keep its external linkage, otherwise it won't serve its purpose as an API function anymore. 19 | 20 | For this reason, an exception was added regarding MISRA rule: 21 | 22 | | Directive | Type | Rationale | 23 | | :-------: | :------: | :----------------------------------------------------------- | 24 | | 8.7 | advisory | API functions should keep external linkage, even if they are not
referenced outside their translation unit. | 25 | 26 | Suppression of the messages related to this MISRA directive exception are marked with: 27 | 28 | ```c 29 | /* cppcheck-suppress [misra-c2012-8.7,unmatchedSuppression] 30 | * MISRA exception: External linkage for API functions. 31 | */ 32 | ``` 33 | 34 | -------------------------------------------------------------------------------- /docs/platform.md: -------------------------------------------------------------------------------- 1 | # Platform 2 | 3 | The platform software component contains functionality to determine platform and architecture specifics at run-time. This functionality eases cross-platform embedded software development. 4 | 5 | ## Usage 6 | 7 | The platform component currently contains a function to determine if the microcontroller's memory storage organization is little endian (Intel) or big endian (Motorola). To make this determination at run-time, call function `TbxPlatformLittleEndian()`. If it returns `TBX_TRUE` the code runs on a platform with little endian memory storage organization. If it returns `TBX_FALSE`, it is big endian. 8 | 9 | ## Examples 10 | 11 | Especially communication protocols typically require you to either report the endianness of the target or store data larger than one byte in a specific way. One example is on TCP/IP networks, which requires multi-byte numbers to be communicated in the big endian format. Most TCP/IP stacks offer a function named `htonl()` for this; An abbreviation for *host-to-network-long*. With the help of `TbxPlatformLittleEndian()`, you can implement a similar function: 12 | 13 | ```c 14 | uint32_t StoreU32BigEndian(uint32_t value) 15 | { 16 | uint32_t result = value; 17 | 18 | /* Little endian (Intel)? */ 19 | if (TbxPlatformLittleEndian() == TBX_TRUE) 20 | { 21 | /* Get byte pointer to value and result variables. */ 22 | uint8_t * leValuePtr = (uint8_t *)&value; 23 | uint8_t * beResultPtr = (uint8_t *)&result; 24 | /* Reverse byte ordering to store value in big endian (Motorola) format. */ 25 | beResultPtr[0] = leValuePtr[3]; 26 | beResultPtr[1] = leValuePtr[2]; 27 | beResultPtr[2] = leValuePtr[1]; 28 | beResultPtr[3] = leValuePtr[0]; 29 | } 30 | /* Give the result back to the caller. */ 31 | return result; 32 | } 33 | ``` 34 | -------------------------------------------------------------------------------- /docs/random.md: -------------------------------------------------------------------------------- 1 | # Random numbers 2 | 3 | The random number generator software component offers functionality for 4 | generating 32-bit random numbers. The generator algorithm is based on the linear 5 | feedback shift register approach ([LFSR](https://en.wikipedia.org/wiki/Linear-feedback_shift_register)), specifically the one presented in [application note 4400](https://www.maximintegrated.com/en/app-notes/index.mvp/id/4400) from Maxim Integrated. 6 | 7 | ## Usage 8 | 9 | Whenever a random number is to be obtained, call function [`TbxRandomNumberGet()`](apiref.md#tbxrandomnumberget). 10 | The internal generator algorithm needs to be properly seeded. By default, the 11 | same constant value is assigned to the seed. This means that each time function 12 | [`TbxRandomNumberGet()`](apiref.md#tbxrandomnumberget) is called, you get a different number, but the consecutive 13 | numbers will always be the same each time your program is restarted: 14 | 15 | | Reset #1 | Reset #2 | Reset #3 | 16 | | ------------:| ------------:| ------------:| 17 | | 3196210335 | 3196210335 | 3196210335 | 18 | | 2375684060 | 2375684060 | 2375684060 | 19 | | 3691277220 | 3691277220 | 3691277220 | 20 | | 3596136368 | 3596136368 | 3596136368 | 21 | 22 | For some applications, this behavior is acceptable. However, most applications 23 | require different random numbers to be generated each time the software program 24 | is started. This is achieved by calling [`TbxRandomSetSeedInitHandler()`](apiref.md#tbxrandomsetseedinithandler) to 25 | register your own seed initialization function. 26 | 27 | The ideal implementation of your seed initialization function is such that it 28 | returns a different seed value, each time the software program is newly started. 29 | Here are some possible approaches to realize such a changing seed value: 30 | 31 | 1. Keep one of the analog inputs on your microcontroller unconnected. Such a 32 | floating analog input picks up noise from the surrounding environment and 33 | readings will therefore always vary slightly. Configure your ADC peripheral 34 | to perform an analog to digital conversion of this analog input and use its 35 | result value to seed the generator. 36 | 2. If your microcontroller has EEPROM or a non-volatile register, you can use it 37 | to store a value that you use to seed the generator. Then simply increment 38 | its value by one upon software program startup. This way you have a different 39 | seed value every time your software program is started. 40 | * Keep in mind though that these data storage options have a limited amount 41 | of write cycles. A better option might be to use external FRAM. 42 | 3. If your microcontroller has access to an external file system such as an 43 | SD-card, you could store a value, which you use to seed the generator, in a 44 | file. Then simply increment its value by one upon software program startup. 45 | This way you have a different seed value every time your software program is 46 | started. 47 | 4. If your microcontroller based system features a battery backed real-time 48 | clock, you could use the current date/time value to seed the generator 49 | algorithm. 50 | 51 | Here are the results of a demo program that implements method (1) with the 52 | floating analog input to seed the generator algorithm: 53 | 54 | | Reset #1 | Reset #2 | Reset #3 | 55 | | ------------:| ------------:| ------------:| 56 | | 1193105914 | 99986390 | 2131584566 | 57 | | 3607630837 | 1266123007 | 2093232 | 58 | | 2184628642 | 3493368727 | 1164981103 | 59 | | 793839531 | 296055939 | 3318979929 | 60 | 61 | 62 | ## Examples 63 | 64 | To generate a new random number, call the function [`TbxRandomNumberGet()`](apiref.md#tbxrandomnumberget) and it 65 | will return a new 32-bit random number. Example: 66 | 67 | ```c 68 | uint32_t number; 69 | 70 | /* Generate a new random number. */ 71 | number = TbxRandomNumberGet(); 72 | ``` 73 | 74 | The following function is an example implementation of an application specific 75 | seed initialization. It is based on the above described method (1) where a 76 | floating analog input provides randomness upon each read of the analog pin: 77 | 78 | ```c 79 | uint32_t CustomSeedInitHandler(void) 80 | { 81 | /* Create a 32-bit seed value by combining two reads of the floating analog 82 | * pin. 83 | */ 84 | return (AnalogFloatGet() << 16u) | AnalogFloatGet(); 85 | } 86 | ``` 87 | 88 | The above example assumes that the application provides the function 89 | `AnalogFloatGet()` for performing an analog to digital conversion of the analog 90 | input pin that is left floating. 91 | 92 | To register the function `CustomSeedInitHandler()` with the random number 93 | generator software component, make the following function call during the 94 | initialization of your software program: 95 | 96 | ```c 97 | /* Register application specific seed initialization function. */ 98 | TbxRandomSetSeedInitHandler(CustomSeedInitHandler); 99 | ``` 100 | -------------------------------------------------------------------------------- /mkdocs.yml: -------------------------------------------------------------------------------- 1 | site_name: MicroTBX User Manual 2 | site_description: User Manual for MicroTBX, the Swiss army knife for your firmware. 3 | repo_url: https://github.com/feaser/microtbx 4 | theme: readthedocs 5 | nav: 6 | - Home: 'index.md' 7 | - API reference: 'apiref.md' 8 | - MISRA compliance: 'misra.md' 9 | - License: 'license.md' 10 | - Assertions: 'assertions.md' 11 | - Critical sections: 'critsect.md' 12 | - Heap: 'heap.md' 13 | - Memory pools: 'mempools.md' 14 | - Linked lists: 'lists.md' 15 | - Random numbers: 'random.md' 16 | - Checksums: 'checksum.md' 17 | - Cryptography: 'crypto.md' 18 | - Platform: 'platform.md' 19 | - Extra functionality: 'extra.md' 20 | -------------------------------------------------------------------------------- /source/extra/cplusplus/tbxcxx.cpp: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbxcxx.cpp 3 | * \brief Globally overloaded operators source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2022 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /* 38 | * On a microcontroller it is totally fine to dynamically allocate memory on the heap 39 | * using new (or malloc). It gets potentially troublesome when you also release it at 40 | * run-time using delelete (or free). Multiple allocation and release operations can 41 | * cause memory fragmentation. In a worst case scenario this leads to memory allocation 42 | * failures, because of running out of heap memory. 43 | * 44 | * A possible solution is by dynamically allocating memory using memory pools. This 45 | * lowers the risk of memory fragmentation. With a carefully selected memory pool setup, 46 | * you can even eliminate this risk completely. Allowing you to dynamically allocate and 47 | * release memory during run-time. MicroTBX includes a ready-made memory pool module that 48 | * can be used for this purpose. 49 | * 50 | * One way to approach this is by using C++ "placement new" instead of the usual "new". 51 | * This allows you the first allocate memory from a memory pool and then place the new 52 | * object exactly at that memory, instead of having the "new" operator do the allocation. 53 | * The only issue with this is that there is no "placement delete". This means that to 54 | * delete the object, you need to manually call its destructor and then give the 55 | * allocated memory back to the memory pool. Definitely an option, it just requires a bit 56 | * more work. 57 | * 58 | * Another approach is to overload the default "new" and "delete" operators to do all the 59 | * memory allocation and release using memory pools automatically. That's the purpose of 60 | * this file. By compiling and linking this source file with your project, the global new 61 | * and delete operators are overloaded, such that they by default always use the memory 62 | * pools module of MicroTBX. 63 | */ 64 | 65 | 66 | /**************************************************************************************** 67 | * Include files 68 | ****************************************************************************************/ 69 | #include 70 | #include "microtbx.h" 71 | 72 | 73 | /************************************************************************************//** 74 | ** \brief Overloading global new operator. 75 | ** \param size Size of the memory to allocate. 76 | ** 77 | ****************************************************************************************/ 78 | void * operator new(size_t size) 79 | { 80 | void * result; 81 | 82 | /* Attempt to allocate a block from the best fitting memory pool. */ 83 | result = TbxMemPoolAllocate(size); 84 | /* Was the allocation not successful? */ 85 | if (result == nullptr) 86 | { 87 | /* The allocation failed. This can have two reasons: 88 | * 1. A memory pool for the requested size hasn't yet been created. 89 | * 2. The memory pool for the requested size has no more free blocks. 90 | * Both situations can be solved by calling TbxMemPoolCreate(), as this 91 | * function automatically extends a memory pool, if it was created before. 92 | * Note that there is no need to check the return value, because we will 93 | * attempt to allocate again right afterwards. We can catch the error 94 | * there in case the allocation fails. 95 | */ 96 | (void)TbxMemPoolCreate(1U, size); 97 | 98 | /* Assuming sufficient heap was available, the memory pool was extended. 99 | * Attempt to allocate the block again. 100 | */ 101 | result = TbxMemPoolAllocate(size); 102 | } 103 | /* Verify the allocation result. */ 104 | if (result == nullptr) 105 | { 106 | /* Since exceptions aren't used, call abort directly to indicate an abnormal end to 107 | * the program, since new is not allowed to return a nullptr. 108 | */ 109 | std::abort(); 110 | } 111 | /* Give the result back to the caller. */ 112 | return result; 113 | } /*** end of operator new ***/ 114 | 115 | 116 | /************************************************************************************//** 117 | ** \brief Overloading global delete operator. 118 | ** \param mem Pointer of memory block to release. 119 | ** 120 | ****************************************************************************************/ 121 | void operator delete(void * mem) 122 | { 123 | /* Give the block back to the memory pool. */ 124 | if (mem != nullptr) 125 | { 126 | TbxMemPoolRelease(mem); 127 | } 128 | } /*** end of operator delete ***/ 129 | 130 | 131 | /************************************************************************************//** 132 | ** \brief Overloading global delete operator. 133 | ** \param mem Pointer of memory block to release. 134 | ** \param size Size of the block. 135 | ** 136 | ****************************************************************************************/ 137 | void operator delete(void * mem, unsigned int size) 138 | { 139 | /* Memory pool manager already knows the size of the block. */ 140 | TBX_UNUSED_ARG(size); 141 | 142 | /* Give the block back to the memory pool. */ 143 | return ::operator delete(mem); 144 | } /*** end of operator delete ***/ 145 | 146 | 147 | /************************************************************************************//** 148 | ** \brief Overloading global new[] operator. 149 | ** \param size Size of the memory to allocate. 150 | ** 151 | ****************************************************************************************/ 152 | void * operator new[](size_t size) 153 | { 154 | /* Allocate and return the block of memory. */ 155 | return ::operator new(size); 156 | } /*** end of operator new[] ***/ 157 | 158 | 159 | /************************************************************************************//** 160 | ** \brief Overloading global delete[] operator. 161 | ** \param mem Pointer of memory block to release. 162 | ** 163 | ****************************************************************************************/ 164 | void operator delete[](void * mem) 165 | { 166 | /* Give the block back to the memory pool. */ 167 | return ::operator delete(mem); 168 | } /*** end of operator delete[] ***/ 169 | 170 | 171 | /************************************************************************************//** 172 | ** \brief Overloading global delete[] operator. 173 | ** \param mem Pointer of memory block to release. 174 | ** \param size Size of the block. 175 | ** 176 | ****************************************************************************************/ 177 | void operator delete[](void * mem, unsigned int size) 178 | { 179 | /* Memory pool manager already knows the size of the block. */ 180 | TBX_UNUSED_ARG(size); 181 | 182 | /* Give the block back to the memory pool. */ 183 | return ::operator delete(mem); 184 | } /*** end of operator delete[] ***/ 185 | 186 | 187 | /*********************************** end of operators.cpp ******************************/ 188 | -------------------------------------------------------------------------------- /source/extra/freertos/tbx_freertos.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_freertos.c 3 | * \brief Heap management source file for FreeRTOS based on MicroTBX. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2022 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /* 38 | * An implementation of pvPortMalloc() and vPortFree() based on the memory pools module 39 | * of MicroTBX. Note that this implementation allows allocated memory to be freed again. 40 | * 41 | * See heap_1.c, heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the 42 | * memory management pages of http://www.FreeRTOS.org for more information. 43 | */ 44 | 45 | 46 | /**************************************************************************************** 47 | * Macro definitions 48 | ***************************************************************************************/ 49 | /** \brief Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining 50 | * all the API functions to use the MPU wrappers. That should only be done when 51 | * task.h is included from an application file. 52 | */ 53 | #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE 54 | 55 | 56 | /**************************************************************************************** 57 | * Include files 58 | ***************************************************************************************/ 59 | #include "microtbx.h" 60 | #include "FreeRTOS.h" 61 | #include "task.h" 62 | 63 | /**************************************************************************************** 64 | * Macro removal 65 | ***************************************************************************************/ 66 | /** \brief This macro is no longer needed after including task.h from a non-application 67 | * source file. 68 | */ 69 | #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE 70 | 71 | /**************************************************************************************** 72 | * Configuration check 73 | ***************************************************************************************/ 74 | #if (configSUPPORT_DYNAMIC_ALLOCATION == 0) 75 | #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0 76 | #endif 77 | 78 | 79 | /**************************************************************************************** 80 | * External function prototype 81 | ***************************************************************************************/ 82 | extern void vApplicationMallocFailedHook(void); 83 | 84 | 85 | /************************************************************************************//** 86 | ** \brief Dynamically allocated memory on the heap. 87 | ** \param xWantedSize Size of the memory to allocate in bytes. 88 | ****************************************************************************************/ 89 | void * pvPortMalloc(size_t xWantedSize) 90 | { 91 | void * result; 92 | 93 | /* Prevent the scheduler from performing a context switch, while allocating memory. */ 94 | vTaskSuspendAll(); 95 | 96 | /* Attempt to allocate a block from the best fitting memory pool. */ 97 | result = TbxMemPoolAllocate(xWantedSize); 98 | /* Was the allocation not successful? */ 99 | if (result == NULL) 100 | { 101 | /* The allocation failed. This can have two reasons: 102 | * 1. A memory pool for the requested size hasn't yet been created. 103 | * 2. The memory pool for the requested size has no more free blocks. 104 | * Both situations can be solved by calling TbxMemPoolCreate(), as this 105 | * function automatically extends a memory pool, if it was created before. 106 | * Note that ther is not need to check the return value, because we will 107 | * attempts to allocate again right afterwards. We can catch the error 108 | * there in case the allocation fails. 109 | */ 110 | (void)TbxMemPoolCreate(1U, xWantedSize); 111 | 112 | /* Assuming sufficient heap was available, the memory pool was extended. 113 | * Attempt to allocate the block again. 114 | */ 115 | result = TbxMemPoolAllocate(xWantedSize); 116 | } 117 | /* Allow memory allocation tracing. */ 118 | traceMALLOC( result, xWantedSize ); 119 | 120 | /* Allow the scheduler to context switch again. */ 121 | (void)xTaskResumeAll(); 122 | 123 | #if (configUSE_MALLOC_FAILED_HOOK == 1) 124 | /* Was the allocation not successful? */ 125 | if( result == NULL ) 126 | { 127 | /* Inform the application about this problem. Try increasing the heap size to 128 | * prevent this from happining. It's macro TBX_CONF_HEAP_SIZE in tbx_conf.h. 129 | */ 130 | vApplicationMallocFailedHook(); 131 | } 132 | #endif 133 | 134 | /* Give the result back to the caller. */ 135 | return result; 136 | } /*** end of pvPortMalloc ***/ 137 | 138 | 139 | /************************************************************************************//** 140 | ** \brief Releases previously allocated memory, allowing it to be reused. 141 | ** \param pv Pointer to the previously allocated memory. It must be a pointer value 142 | ** that was previously returned by pvPortMalloc(). 143 | ** 144 | ****************************************************************************************/ 145 | void vPortFree(void * pv) 146 | { 147 | /* Give the block back to the memory pool. */ 148 | TbxMemPoolRelease(pv); 149 | } /*** end of vPortFree ***/ 150 | 151 | 152 | /*********************************** end of tbx_freertos.c *****************************/ 153 | -------------------------------------------------------------------------------- /source/extra/freertos/tbx_freertos.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_freertos.h 3 | * \brief MicroTBX support for FreeRTOS. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2022 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_FREERTOS_H 37 | #define TBX_FREERTOS_H 38 | 39 | /* 40 | * Include this file at the end of FreeRTOSConfig.h to add support of MicroTBX to 41 | * FreeRTOS. The current implementation remamps the assertion macro to the one from 42 | * MicroTBX. 43 | */ 44 | 45 | /**************************************************************************************** 46 | * Include files 47 | ***************************************************************************************/ 48 | #include "microtbx.h" 49 | 50 | 51 | /**************************************************************************************** 52 | * Macro definitions 53 | ***************************************************************************************/ 54 | #ifdef configASSERT 55 | #undef configASSERT 56 | #endif 57 | 58 | /** \brief Map the MicroTBX assertions macro to FreeRTOS. */ 59 | #define configASSERT(x) TBX_ASSERT(x) 60 | 61 | 62 | #endif /* TBX_FREERTOS_H */ 63 | /*********************************** end of tbx_freertos.h *****************************/ 64 | -------------------------------------------------------------------------------- /source/microtbx.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file microtbx.h 3 | * \brief MicroTBX global header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef MICROTBX_H 37 | #define MICROTBX_H 38 | 39 | /**************************************************************************************** 40 | * Include files 41 | ****************************************************************************************/ 42 | /* Note that it is possible to override the standard tbx_conf.h configuration header 43 | * file with a project specific one that is defined in the IDE/makefile. For example, 44 | * the following define could be configured: PROJ_TBX_CONF_H="my_tbx_config.h". This can 45 | * be handy if you use MicroTBX in several projects with a different configuration, 46 | * and enables you to have just one MicroTBX source base. 47 | */ 48 | #include /* Standard integer types */ 49 | #include /* Standard definitions */ 50 | #ifdef PROJ_TBX_CONF_H 51 | #include PROJ_TBX_CONF_H /* Custom MicroTBX configuration file */ 52 | #else 53 | #include "tbx_conf.h" /* Standard MicroTBX configuration file */ 54 | #endif /* PROJ_TBX_CONF_H */ 55 | #include "tbx_platform.h" /* Platform specifics */ 56 | #include "tbx_port.h" /* MicroTBX port specifics */ 57 | #include "tbx_assert.h" /* Run-time assertions */ 58 | #include "tbx_critsect.h" /* Critical sections */ 59 | #include "tbx_heap.h" /* Heap memory allocation */ 60 | #include "tbx_list.h" /* Linked lists */ 61 | #include "tbx_mempool.h" /* Pool based heap memory manager */ 62 | #include "tbx_random.h" /* Random number generator */ 63 | #include "tbx_checksum.h" /* Checksum module */ 64 | #include "tbx_crypto.h" /* Cryptography module */ 65 | 66 | 67 | #ifdef __cplusplus 68 | extern "C" { 69 | #endif 70 | /**************************************************************************************** 71 | * Version definitions 72 | ****************************************************************************************/ 73 | /** \brief Main version number of MicroTBX. */ 74 | #define TBX_VERSION_MAIN (1U) 75 | 76 | /** \brief Minor version number of MicroTBX. */ 77 | #define TBX_VERSION_MINOR (3U) 78 | 79 | /** \brief Patch number of MicroTBX. */ 80 | #define TBX_VERSION_PATCH (0U) 81 | 82 | 83 | /**************************************************************************************** 84 | * Macro definitions 85 | ****************************************************************************************/ 86 | /** \brief Boolean true value. */ 87 | #define TBX_TRUE (1U) 88 | 89 | /** \brief Boolean false value. */ 90 | #define TBX_FALSE (0U) 91 | 92 | /** \brief Generic okay value. */ 93 | #define TBX_OK (1U) 94 | 95 | /** \brief Generic error value. */ 96 | #define TBX_ERROR (0U) 97 | 98 | /** \brief Generic on value. */ 99 | #define TBX_ON (1U) 100 | 101 | /** \brief Generic off value. */ 102 | #define TBX_OFF (0U) 103 | 104 | /** \brief Macro to flag a function parameter as unused, which allows the associated 105 | * lint message and/or compiler warning to be suppressed. 106 | */ 107 | #define TBX_UNUSED_ARG(x) (void)(x) 108 | 109 | 110 | #ifdef __cplusplus 111 | } 112 | #endif 113 | 114 | #endif /* MICROTBX_H */ 115 | /*********************************** end of microtbx.h *********************************/ 116 | -------------------------------------------------------------------------------- /source/misra.cmake: -------------------------------------------------------------------------------- 1 | # Create target for MISRA checking MicroTBX if cppcheck can be located. 2 | find_program(CMAKE_CXX_CPPCHECK NAMES cppcheck) 3 | if (CMAKE_CXX_CPPCHECK) 4 | # Collect MicroTBX include directories. 5 | get_target_property(microtbx_incs microtbx INTERFACE_INCLUDE_DIRECTORIES) 6 | # Collect MicroTBX Cortex-M port include directories. 7 | get_target_property(microtbx_cortexm_incs microtbx-cortexm INTERFACE_INCLUDE_DIRECTORIES) 8 | # Collect MicroTBX RP2040 port include directories. 9 | get_target_property(microtbx_rp2040_incs microtbx-rp2040 INTERFACE_INCLUDE_DIRECTORIES) 10 | # Collect MicroTBX AVR port include directories. 11 | get_target_property(microtbx_avr_incs microtbx-avr INTERFACE_INCLUDE_DIRECTORIES) 12 | # Collect MicroTBX template include directories. 13 | get_target_property(microtbx_template_incs microtbx-template INTERFACE_INCLUDE_DIRECTORIES) 14 | 15 | # Build list with search paths for include files and transform by adding a "-I". 16 | set(search_path_incs) 17 | list(APPEND search_path_incs ${microtbx_incs}) 18 | list(APPEND search_path_incs ${microtbx_cortexm_incs}) 19 | list(APPEND search_path_incs ${microtbx_rp2040_incs}) 20 | list(APPEND search_path_incs ${microtbx_avr_incs}) 21 | list(APPEND search_path_incs ${microtbx_template_incs}) 22 | list(TRANSFORM search_path_incs PREPEND "-I") 23 | 24 | # Collect MicroTBX sources. 25 | get_target_property(microtbx_srcs microtbx INTERFACE_SOURCES) 26 | # Collect MicroTBX Cortex-M port sources. 27 | get_target_property(microtbx_cortexm_srcs microtbx-cortexm INTERFACE_SOURCES) 28 | # Collect MicroTBX RP2040 port sources. 29 | get_target_property(microtbx_rp2040_srcs microtbx-rp2040 INTERFACE_SOURCES) 30 | # Collect MicroTBX AVR port sources. 31 | get_target_property(microtbx_avr_srcs microtbx-avr INTERFACE_SOURCES) 32 | 33 | # Build list with MicroTBX sources to check. 34 | set(check_srcs) 35 | list(APPEND check_srcs ${microtbx_srcs}) 36 | list(APPEND check_srcs ${microtbx_cortexm_srcs}) 37 | list(APPEND check_srcs ${microtbx_rp2040_srcs}) 38 | list(APPEND check_srcs ${microtbx_avr_srcs}) 39 | 40 | # Set variable pointing to the addon for configuring the MISRA checks. 41 | set(misra_addon "${CMAKE_CURRENT_LIST_DIR}/misra.json") 42 | 43 | # Build list with cppcheck commands, one for each given source file 44 | set(cppcheck_commands) 45 | foreach(sourcefile ${check_srcs}) 46 | # only include C-source files 47 | if( sourcefile MATCHES \\.c$ ) 48 | # create command line for running cppcheck on one source file and add it 49 | # to the list of commands 50 | list(APPEND cppcheck_commands 51 | COMMAND ${CMAKE_CXX_CPPCHECK} 52 | --platform=arm32-wchar_t2.xml 53 | --addon=${misra_addon} 54 | --relative-paths 55 | --error-exitcode=1 56 | --language=c 57 | --std=c11 58 | --inline-suppr 59 | --suppress=missingIncludeSystem 60 | --suppress=unmatchedSuppression 61 | --enable=warning,style,information,missingInclude 62 | -DDEBUG 63 | ${search_path_incs} 64 | ${sourcefile}) 65 | endif() 66 | endforeach(sourcefile) 67 | 68 | # add a custom target consisting of all the commands generated above 69 | add_custom_target(microtbx_MISRA ${cppcheck_commands} VERBATIM) 70 | endif() 71 | -------------------------------------------------------------------------------- /source/misra.json: -------------------------------------------------------------------------------- 1 | { 2 | "script": "misra.py", 3 | "args": [ 4 | "--suppress-rules 2.5,11.5" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /source/port/ARM_CORTEXM/GCC/tbx_comp.s: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/ARM_CORTEXM/GCC/tbx_comp.s 3 | * \brief GCC compiler specifics of the port source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | .syntax unified 37 | .thumb 38 | 39 | 40 | .global TbxPortInterruptsDisable 41 | .global TbxPortInterruptsRestore 42 | 43 | 44 | /************************************************************************************//** 45 | ** \brief Stores the current state of the CPU status register and then disables the 46 | ** generation of global interrupts. The status register contains information 47 | ** about the interrupts being disable/enabled before they get disabled. This 48 | ** is needed to later on restore the state. 49 | ** Prototype: 50 | ** tTbxPortCpuSR TbxPortInterruptsDisable(void); 51 | ** \return The current value of the CPU status register. 52 | ** 53 | ****************************************************************************************/ 54 | .section .text,"ax" 55 | .weak TbxPortInterruptsDisable 56 | .type TbxPortInterruptsDisable, %function 57 | TbxPortInterruptsDisable: 58 | /* Store state of the currently enabled/disabled interrupts in register 0. On the 59 | * ARM Cortex this is stored in CPU register PRIMASK. Note that register 0 is used by 60 | * GCC for storing the return value of a function. 61 | */ 62 | mrs r0, primask 63 | /* Disable the interrupts by setting all bits in the CPU register PRIMASK to a value 64 | * of 1, which means the actication of the exception is prevented. 65 | */ 66 | cpsid i 67 | /* Return from this function by branching back to the location stored in the link 68 | * register. 69 | */ 70 | bx lr 71 | 72 | 73 | /************************************************************************************//** 74 | ** \brief Restores the interrupts enabled/disabled state to the state it was when 75 | ** function TbxPortInterruptsDisable() was previously called. It does this 76 | ** by writing the value of the CPU status register that was returned by 77 | ** TbxPortInterruptsDisable(). 78 | ** Prototype: 79 | ** void TbxPortInterruptsRestore(tTbxPortCpuSR prevCpuSr); 80 | ** \param prevCpuSr The previous value of the CPU status register from right before 81 | ** the interrupts where disabled. This value is returned by function 82 | ** TbxPortInterruptsDisable(). 83 | ** 84 | ****************************************************************************************/ 85 | .section .text,"ax" 86 | .weak TbxPortInterruptsRestore 87 | .type TbxPortInterruptsRestore, %function 88 | TbxPortInterruptsRestore: 89 | /* Restore the state of the interrupts by storing the value of register 0 in the CPU 90 | * register PRIMASK. Note that register 0 is used by GCC for passing the first function 91 | * parameter. 92 | */ 93 | msr primask, r0 94 | /* Return from this function by branching back to the location stored in the link 95 | * register. 96 | */ 97 | bx lr 98 | 99 | 100 | /*********************************** end of tbx_comp.s *********************************/ 101 | 102 | -------------------------------------------------------------------------------- /source/port/ARM_CORTEXM/IAR/tbx_comp.s: -------------------------------------------------------------------------------- 1 | ;**************************************************************************************** 2 | ;* \file port/ARM_CORTEXM/IAR/tbx_comp.s 3 | ;* \brief IAR compiler specifics of the port source file. 4 | ;* \internal 5 | ;*--------------------------------------------------------------------------------------- 6 | ;* C O P Y R I G H T 7 | ;*--------------------------------------------------------------------------------------- 8 | ;* Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | ;* 10 | ;*--------------------------------------------------------------------------------------- 11 | ;* L I C E N S E 12 | ;*--------------------------------------------------------------------------------------- 13 | ;* 14 | ;* SPDX-License-Identifier: MIT 15 | ;* 16 | ;* Permission is hereby granted, free of charge, to any person obtaining a copy 17 | ;* of this software and associated documentation files (the "Software"), to deal 18 | ;* in the Software without restriction, including without limitation the rights 19 | ;* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | ;* copies of the Software, and to permit persons to whom the Software is 21 | ;* furnished to do so, subject to the following conditions: 22 | ;* 23 | ;* The above copyright notice and this permission notice shall be included in all 24 | ;* copies or substantial portions of the Software. 25 | ;* 26 | ;* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | ;* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | ;* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | ;* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | ;* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | ;* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | ;* SOFTWARE. 33 | ;* 34 | ;* \endinternal 35 | ;**************************************************************************************** 36 | 37 | 38 | PUBLIC TbxPortInterruptsDisable 39 | PUBLIC TbxPortInterruptsRestore 40 | 41 | 42 | SECTION .text:CODE:NOROOT(2) 43 | THUMB 44 | 45 | 46 | PUBWEAK TbxPortInterruptsDisable 47 | TbxPortInterruptsDisable 48 | ; Store state of the currently enabled/disabled interrupts in register 0. On the 49 | ; ARM Cortex this is stored in CPU register PRIMASK. Note that register 0 is used 50 | ; by GCC for storing the return value of a function. 51 | MRS R0, PRIMASK 52 | ; Disable the interrupts by setting all bits in the CPU register PRIMASK to a 53 | ; value of 1, which means the actication of the exception is prevented. 54 | CPSID I 55 | ; Return from this function by branching back to the location stored in the link 56 | ; register. 57 | BX LR 58 | 59 | 60 | PUBWEAK TbxPortInterruptsRestore 61 | THUMB 62 | TbxPortInterruptsRestore 63 | ; Restore the state of the interrupts by storing the value of register 0 in the 64 | ; CPU register PRIMASK. Note that register 0 is used by GCC for passing the first 65 | ; function parameter. 66 | MSR PRIMASK, R0 67 | ; Return from this function by branching back to the location stored in the link 68 | ; register. 69 | BX LR 70 | 71 | 72 | END 73 | ;*********************************** end of tbx_comp.s ********************************** 74 | -------------------------------------------------------------------------------- /source/port/ARM_CORTEXM/Keil/tbxcomp.s: -------------------------------------------------------------------------------- 1 | ;**************************************************************************************** 2 | ;* \file port/ARM_CORTEXM/Keil/tbx_comp.s 3 | ;* \brief Keil compiler specifics of the port source file. 4 | ;* \internal 5 | ;*--------------------------------------------------------------------------------------- 6 | ;* C O P Y R I G H T 7 | ;*--------------------------------------------------------------------------------------- 8 | ;* Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | ;* 10 | ;*--------------------------------------------------------------------------------------- 11 | ;* L I C E N S E 12 | ;*--------------------------------------------------------------------------------------- 13 | ;* 14 | ;* SPDX-License-Identifier: MIT 15 | ;* 16 | ;* Permission is hereby granted, free of charge, to any person obtaining a copy 17 | ;* of this software and associated documentation files (the "Software"), to deal 18 | ;* in the Software without restriction, including without limitation the rights 19 | ;* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | ;* copies of the Software, and to permit persons to whom the Software is 21 | ;* furnished to do so, subject to the following conditions: 22 | ;* 23 | ;* The above copyright notice and this permission notice shall be included in all 24 | ;* copies or substantial portions of the Software. 25 | ;* 26 | ;* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | ;* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | ;* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | ;* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | ;* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | ;* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | ;* SOFTWARE. 33 | ;* 34 | ;* \endinternal 35 | ;**************************************************************************************** 36 | AREA |.text|, CODE, READONLY 37 | PRESERVE8 38 | THUMB 39 | 40 | 41 | ;**************************************************************************************** 42 | ;** \brief Stores the current state of the CPU status register and then disables the 43 | ;** generation of global interrupts. The status register contains information 44 | ;** about the interrupts being disable/enabled before they get disabled. This 45 | ;** is needed to later on restore the state. 46 | ;** Prototype: 47 | ;** tTbxPortCpuSR TbxPortInterruptsDisable(void); 48 | ;** \return The current value of the CPU status register. 49 | ;** 50 | ;**************************************************************************************** 51 | TbxPortInterruptsDisable FUNCTION 52 | EXPORT TbxPortInterruptsDisable [WEAK] 53 | ; Store state of the currently enabled/disabled interrupts in register 0. On the 54 | ; ARM Cortex this is stored in CPU register PRIMASK. Note that register 0 is used by 55 | ; GCC for storing the return value of a function. 56 | MRS R0, PRIMASK 57 | ; Disable the interrupts by setting all bits in the CPU register PRIMASK to a value 58 | ; of 1, which means the actication of the exception is prevented. 59 | CPSID I 60 | ; Return from this function by branching back to the location stored in the link 61 | ; register. 62 | BX LR 63 | ENDFUNC 64 | 65 | 66 | ;**************************************************************************************** 67 | ;** \brief Restores the interrupts enabled/disabled state to the state it was when 68 | ;** function TbxPortInterruptsDisable() was previously called. It does this 69 | ;** by writing the value of the CPU status register that was returned by 70 | ;** TbxPortInterruptsDisable(). 71 | ;** Prototype: 72 | ;** void TbxPortInterruptsRestore(tTbxPortCpuSR prevCpuSr); 73 | ;** \param prevCpuSr The previous value of the CPU status register from right before 74 | ;** the interrupts where disabled. This value is returned by function 75 | ;** TbxPortInterruptsDisable(). 76 | ;** 77 | ;**************************************************************************************** 78 | TbxPortInterruptsRestore FUNCTION 79 | EXPORT TbxPortInterruptsRestore [WEAK] 80 | ; Restore the state of the interrupts by storing the value of register 0 in the CPU 81 | ; register PRIMASK. Note that register 0 is used by GCC for passing the first function 82 | ; parameter. 83 | MSR PRIMASK, R0 84 | ; Return from this function by branching back to the location stored in the link 85 | ; register. 86 | BX LR 87 | ENDFUNC 88 | 89 | 90 | END 91 | ;*********************************** end of tbx_comp.s ********************************** 92 | -------------------------------------------------------------------------------- /source/port/ARM_CORTEXM/tbx_port.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/ARM_CORTEXM/tbx_port.c 3 | * \brief Port specifics source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include "microtbx.h" /* MicroTBX global header */ 41 | 42 | 43 | /* At this point nothing is implemented here. The TbxPortInterruptsXxx functions were 44 | * implemented in assembly for MISRA compliance. MISRA requires that where assembly 45 | * language instructions are required, it is recommended that they be encapsulated and 46 | * isolated in either: (a) assembler functions, (b) C functions or (c) macros. 47 | * Recommendation (a) was chosen for the TbxPortInterruptsXxx functions. They are located 48 | * in the compiler specific part of the port. 49 | */ 50 | 51 | 52 | /*********************************** end of tbx_port.c *********************************/ 53 | -------------------------------------------------------------------------------- /source/port/ARM_CORTEXM/tbx_types.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/ARM_CORTEXM/tbx_types.h 3 | * \brief Port specific types header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_TYPES_H 37 | #define TBX_TYPES_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Type definitions 44 | ****************************************************************************************/ 45 | /** \brief The type for the CPU status register. This type should be configured such 46 | * that the CPU's status register can be fully stored in it. This is the register 47 | * with information about global interrupts being enabled/disabled, among other 48 | * things. 49 | */ 50 | typedef uint32_t tTbxPortCpuSR; 51 | 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* TBX_TYPES_H */ 58 | /*********************************** end of tbx_types.h ********************************/ 59 | -------------------------------------------------------------------------------- /source/port/AVR/GCC/tbx_port.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/AVR/GCC/tbx_port.c 3 | * \brief Port specifics source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2023 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include /* AVR IRQ management */ 41 | #include "microtbx.h" /* MicroTBX global header */ 42 | 43 | 44 | /************************************************************************************//** 45 | ** \brief Stores the current state of the CPU status register and then disables the 46 | ** generation of global interrupts. The status register contains information 47 | ** about the interrupts being disable/enabled before they get disabled. This 48 | ** is needed to later on restore the state. 49 | ** \return The current value of the CPU status register. 50 | ** 51 | ****************************************************************************************/ 52 | tTbxPortCpuSR TbxPortInterruptsDisable(void) 53 | { 54 | tTbxPortCpuSR result; 55 | 56 | /* Store the current status register. */ 57 | result = SREG; 58 | /* Request the compiler to complete the previous load/store before continuing. */ 59 | __asm__ volatile ("" ::: "memory"); 60 | /* Clear the I-bit to make sure the global interrupts are disabled. */ 61 | __asm__ __volatile__ ("cli" ::: "memory"); 62 | 63 | /* Give the result back to the caller. */ 64 | return result; 65 | } /*** end of TbxPortInterruptsDisable ***/ 66 | 67 | 68 | /************************************************************************************//** 69 | ** \brief Restores the interrupts enabled/disabled state to the state it was when 70 | ** function TbxPortInterruptsDisable() was previously called. It does this 71 | ** by writing the value of the CPU status register that was returned by 72 | ** TbxPortInterruptsDisable(). 73 | ** \param prevCpuSr The previous value of the CPU status register from right before 74 | ** the interrupts where disabled. This value is returned by function 75 | ** TbxPortInterruptsDisable(). 76 | ** 77 | ****************************************************************************************/ 78 | void TbxPortInterruptsRestore(tTbxPortCpuSR prevCpuSr) 79 | { 80 | /* Write the status register to restore its previous state. */ 81 | SREG = prevCpuSr; 82 | /* Request the compiler to complete the previous load/store before continuing. */ 83 | __asm__ volatile ("" ::: "memory"); 84 | } /*** end of TbxPortInterruptsRestore ***/ 85 | 86 | 87 | /*********************************** end of tbx_port.c *********************************/ 88 | -------------------------------------------------------------------------------- /source/port/AVR/tbx_types.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/AVR/tbx_types.h 3 | * \brief Port specific types header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2023 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_TYPES_H 37 | #define TBX_TYPES_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Type definitions 44 | ****************************************************************************************/ 45 | /** \brief The type for the CPU status register. This type should be configured such 46 | * that the CPU's status register can be fully stored in it. This is the register 47 | * with information about global interrupts being enabled/disabled, among other 48 | * things. 49 | */ 50 | typedef uint8_t tTbxPortCpuSR; 51 | 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* TBX_TYPES_H */ 58 | /*********************************** end of tbx_types.h ********************************/ 59 | -------------------------------------------------------------------------------- /source/port/LINUX/tbx_port.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/LINUX/tbx_port.c 3 | * \brief Port specifics source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2022 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include "microtbx.h" /* MicroTBX global header */ 41 | #include /* Posix thread utilities */ 42 | #include /* Boolean definitions */ 43 | #include /* Atomic operations */ 44 | 45 | 46 | /**************************************************************************************** 47 | * Macro definitions 48 | ****************************************************************************************/ 49 | /** \brief Value that indicates the the global interrupts are enabled in the simulated 50 | * CPU status register. 51 | */ 52 | #define TBX_PORT_CPU_SR_IRQ_EN (1U) 53 | 54 | 55 | /**************************************************************************************** 56 | * Local data declarations 57 | ****************************************************************************************/ 58 | /** \brief Flag that simulates the global interrupt enabled/disabled state. Assume 59 | * enabled by default. 60 | */ 61 | static atomic_bool interruptsDisabledFlag = ATOMIC_VAR_INIT(false); 62 | 63 | /** \brief Critial section mutex. */ 64 | static pthread_mutex_t critSectMutex = PTHREAD_MUTEX_INITIALIZER; 65 | 66 | 67 | /************************************************************************************//** 68 | ** \brief Stores the current state of the CPU status register and then disables the 69 | ** generation of global interrupts. The status register contains information 70 | ** about the interrupts being disable/enabled before they get disabled. This 71 | ** is needed to later on restore the state. 72 | ** \return The current value of the CPU status register. 73 | ** 74 | ****************************************************************************************/ 75 | tTbxPortCpuSR TbxPortInterruptsDisable(void) 76 | { 77 | tTbxPortCpuSR result = 0U; 78 | 79 | /* Disable the simulated global interrupts by setting its flag to true, and store its 80 | * previous flag value. Were the simulated global interrupts actually enabled, before 81 | * we just disabled them? 82 | */ 83 | if (atomic_flag_test_and_set(&interruptsDisabledFlag) == false) 84 | { 85 | /* Simulate disabling the global interrupts by locking out other threads. */ 86 | (void)pthread_mutex_lock(&critSectMutex); 87 | /* Update the result accordingly. */ 88 | result = TBX_PORT_CPU_SR_IRQ_EN; 89 | } 90 | /* Give the result back to the caller. */ 91 | return result; 92 | } /*** end of TbxPortInterruptsDisable ***/ 93 | 94 | 95 | /************************************************************************************//** 96 | ** \brief Restores the interrupts enabled/disabled state to the state it was when 97 | ** function TbxPortInterruptsDisable() was previously called. It does this 98 | ** by writing the value of the CPU status register that was returned by 99 | ** TbxPortInterruptsDisable(). 100 | ** \param prevCpuSr The previous value of the CPU status register from right before 101 | ** the interrupts where disabled. This value is returned by function 102 | ** TbxPortInterruptsDisable(). 103 | ** 104 | ****************************************************************************************/ 105 | void TbxPortInterruptsRestore(tTbxPortCpuSR prevCpuSr) 106 | { 107 | /* Should the simulated global interrupts be restored to the enabled state? */ 108 | if (prevCpuSr == TBX_PORT_CPU_SR_IRQ_EN) 109 | { 110 | /* Simulate enabling the global interrupts by no longer locking out other threads. */ 111 | (void)pthread_mutex_unlock(&critSectMutex); 112 | /* Enable the simulated global interrupts by setting its flag to false. */ 113 | atomic_flag_clear(&interruptsDisabledFlag); 114 | } 115 | } /*** end of TbxPortInterruptsRestore ***/ 116 | 117 | 118 | /*********************************** end of tbx_port.c *********************************/ 119 | -------------------------------------------------------------------------------- /source/port/LINUX/tbx_types.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/LINUX/tbx_types.h 3 | * \brief Port specific types header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2022 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_TYPES_H 37 | #define TBX_TYPES_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Type definitions 44 | ****************************************************************************************/ 45 | /** \brief The type for the CPU status register. This type should be configured such 46 | * that the CPU's status register can be fully stored in it. This is the register 47 | * with information about global interrupts being enabled/disabled, among other 48 | * things. 49 | */ 50 | typedef uint32_t tTbxPortCpuSR; 51 | 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* TBX_TYPES_H */ 58 | /*********************************** end of tbx_types.h ********************************/ 59 | -------------------------------------------------------------------------------- /source/port/RP2040/tbx_port.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/RP2040/tbx_port.c 3 | * \brief Port specifics source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2023 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include /* Hardware synchronzation library */ 41 | #include "microtbx.h" /* MicroTBX global header */ 42 | 43 | /* Only use this port on the Raspberry PI Pico (RP2040) microcontroller, if you actually 44 | * use multiple cores in your firmware. If you only use one core, the ARM_CORTEXM port is 45 | * the better choice from a run-time performance perspective. 46 | */ 47 | 48 | /**************************************************************************************** 49 | * Local data declarations 50 | ****************************************************************************************/ 51 | /** \brief Spin lock handle. */ 52 | static volatile spin_lock_t * spinLock = NULL; 53 | 54 | /** \brief Flags to keep track if a specific core has the spin lock. */ 55 | static volatile uint8_t coreHasLock[NUM_CORES] = { 0 }; 56 | 57 | 58 | /************************************************************************************//** 59 | ** \brief Stores the current state of the CPU status register and then disables the 60 | ** generation of global interrupts. The status register contains information 61 | ** about the interrupts being disable/enabled before they get disabled. This 62 | ** is needed to later on restore the state. 63 | ** \return The current value of the CPU status register. 64 | ** 65 | ****************************************************************************************/ 66 | tTbxPortCpuSR TbxPortInterruptsDisable(void) 67 | { 68 | tTbxPortCpuSR result = 0U; 69 | static uint8_t spinLockInitialized = TBX_FALSE; 70 | 71 | /* Make sure the spin lock handle is initialized. */ 72 | if (spinLockInitialized == TBX_FALSE) 73 | { 74 | /* Initialization only needs to be done once. */ 75 | spinLockInitialized = TBX_TRUE; 76 | /* Initialize the flags. */ 77 | for (uint8_t coreIdx = 0U; coreIdx < NUM_CORES; coreIdx++) 78 | { 79 | coreHasLock[coreIdx] = TBX_FALSE; 80 | } 81 | /* Initialize the spin lock handle. Start by getting a free lock number. */ 82 | int32_t claimResult = spin_lock_claim_unused(false); 83 | /* Make sure a spin lock could be claimed. */ 84 | TBX_ASSERT(claimResult != -1); 85 | /* Only continue with a valid spin lock number. */ 86 | if (claimResult != -1) 87 | { 88 | /* Convert the result to the lock number. */ 89 | uint32_t lockNum = (uint32_t)claimResult; 90 | /* Get handle to the spin lock instance. */ 91 | spinLock = spin_lock_instance(lockNum); 92 | /* Verify instance handle. */ 93 | TBX_ASSERT(spinLock != NULL); 94 | } 95 | } 96 | 97 | /* Only continue with a valid instance handle. */ 98 | if (spinLock != NULL) 99 | { 100 | /* This function is called upon every critical section entry, so it can be called 101 | * when the calling core already has the spin lock. In that case there is no need to 102 | * obtain it again. In fact, the code shouldn't, because it will deadlock. Therefore 103 | * only continue if we do not already have the spin lock. 104 | */ 105 | if (coreHasLock[get_core_num()] == TBX_FALSE) 106 | { 107 | /* Wait (spin) the calling core to obtain the spin lock. Basically a multicore 108 | * hardware mutex. Additionally, disable the interrupts on the calling core, while 109 | * storing the current state of its CPU status register. 110 | */ 111 | result = spin_lock_blocking(spinLock); 112 | /* Set flag that this core now has the lock. Needs to be done after the call to 113 | * spin_lock_blocking(), because it is a shared resource for each core. 114 | */ 115 | coreHasLock[get_core_num()] = TBX_TRUE; 116 | } 117 | } 118 | /* Give the result back to the caller. */ 119 | return result; 120 | } /*** end of TbxPortInterruptsDisable ***/ 121 | 122 | 123 | /************************************************************************************//** 124 | ** \brief Restores the interrupts enabled/disabled state to the state it was when 125 | ** function TbxPortInterruptsDisable() was previously called. It does this 126 | ** by writing the value of the CPU status register that was returned by 127 | ** TbxPortInterruptsDisable(). 128 | ** \param prevCpuSr The previous value of the CPU status register from right before 129 | ** the interrupts where disabled. This value is returned by function 130 | ** TbxPortInterruptsDisable(). 131 | ** 132 | ****************************************************************************************/ 133 | void TbxPortInterruptsRestore(tTbxPortCpuSR prevCpuSr) 134 | { 135 | /* Only continue with a valid instance handle. */ 136 | if (spinLock != NULL) 137 | { 138 | /* Reset the flag now that this core is about to release the lock. Needs to be done 139 | * before the call to spin_unlock(), because it is a shared resource for each core. 140 | */ 141 | coreHasLock[get_core_num()] = TBX_FALSE; 142 | /* Release the spin lock and restore the interrupts on the core that disabled the 143 | * interrupts previously. 144 | */ 145 | spin_unlock(spinLock, prevCpuSr); 146 | } 147 | } /*** end of TbxPortInterruptsRestore ***/ 148 | 149 | 150 | /*********************************** end of tbx_port.c *********************************/ 151 | -------------------------------------------------------------------------------- /source/port/RP2040/tbx_types.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file port/RP2040/tbx_types.h 3 | * \brief Port specific types header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2023 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_TYPES_H 37 | #define TBX_TYPES_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Type definitions 44 | ****************************************************************************************/ 45 | /** \brief The type for the CPU status register. This type should be configured such 46 | * that the CPU's status register can be fully stored in it. This is the register 47 | * with information about global interrupts being enabled/disabled, among other 48 | * things. 49 | */ 50 | typedef uint32_t tTbxPortCpuSR; 51 | 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* TBX_TYPES_H */ 58 | /*********************************** end of tbx_types.h ********************************/ 59 | -------------------------------------------------------------------------------- /source/tbx_aes256.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Byte-oriented AES-256 implementation. 3 | * All lookup tables replaced with 'on the fly' calculations. 4 | * 5 | * Copyright (c) 2007-2009 Ilya O. Levin, www.literatecode.com 6 | * Other contributors: Hal Finney, Frank Voorburg (MISRA compliance) 7 | * 8 | * Permission to use, copy, modify, and distribute this software for any 9 | * purpose with or without fee is hereby granted, provided that the above 10 | * copyright notice and this permission notice appear in all copies. 11 | * 12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 | */ 20 | 21 | #ifdef __cplusplus 22 | extern "C" { 23 | #endif 24 | 25 | typedef struct { 26 | uint8_t key[32]; 27 | uint8_t enckey[32]; 28 | uint8_t deckey[32]; 29 | } tbx_aes256_context; 30 | 31 | 32 | void tbx_aes256_init(tbx_aes256_context *ctx, uint8_t const *k); 33 | void tbx_aes256_done(tbx_aes256_context *ctx); 34 | void tbx_aes256_encrypt_ecb(tbx_aes256_context *ctx, uint8_t *buf); 35 | void tbx_aes256_decrypt_ecb(tbx_aes256_context *ctx, uint8_t *buf); 36 | 37 | #ifdef __cplusplus 38 | } 39 | #endif 40 | -------------------------------------------------------------------------------- /source/tbx_assert.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_assert.c 3 | * \brief Run-time assertions source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include "microtbx.h" /* MicroTBX global header */ 41 | 42 | 43 | /**************************************************************************************** 44 | * Local data declarations 45 | ****************************************************************************************/ 46 | #if (TBX_CONF_ASSERTIONS_ENABLE > 0U) 47 | /** \brief Pointer to the application provided assertion handler function that should be 48 | * used, whenever a run-time assertion is triggered. 49 | */ 50 | static tTbxAssertHandler tbxAssertHandlerPtr = NULL; 51 | #endif /* (TBX_ASSERTIONS_ENABLE > 0U) */ 52 | 53 | 54 | /************************************************************************************//** 55 | ** \brief Sets the application specific assertion handler. 56 | ** \param assertHandler Pointer to the application specific assertion handler to use 57 | ** instead of the default internal handler. 58 | ** 59 | ****************************************************************************************/ 60 | void TbxAssertSetHandler(tTbxAssertHandler assertHandler) 61 | { 62 | #if (TBX_CONF_ASSERTIONS_ENABLE > 0U) 63 | /* Verify parameter. */ 64 | TBX_ASSERT(assertHandler != NULL); 65 | 66 | /* Only continue if the parameters are valid. */ 67 | if (assertHandler != NULL) 68 | { 69 | /* Store the pointer to the application specific assertion handler. */ 70 | tbxAssertHandlerPtr = assertHandler; 71 | } 72 | #else 73 | TBX_UNUSED_ARG(assertHandler); 74 | #endif /* (TBX_CONF_ASSERTIONS_ENABLE > 0U) */ 75 | } /*** end of TbxAssertSetHandler ***/ 76 | 77 | 78 | #if (TBX_CONF_ASSERTIONS_ENABLE > 0U) 79 | /* cppcheck-suppress [misra-c2012-8.7,unmatchedSuppression] 80 | * MISRA exception: External linkage for API functions. 81 | */ 82 | /************************************************************************************//** 83 | ** \brief Triggers the run-time assertion. The default implementation is to enter an 84 | ** infinite loop, which halts the program and can be used for debugging 85 | ** purposes. Inspecting the values of the file and line parameters gives a 86 | ** clear indication where the run-time assertion occurred. Note that an 87 | ** alternative application specific assertion handler can be configured with 88 | ** function TbxAssertSetHandler(). 89 | ** \param file The filename of the source file where the assertion occurred in. 90 | ** \param line The line number inside the file where the assertion occurred. 91 | ** 92 | ****************************************************************************************/ 93 | void TbxAssertTrigger(char const * const file, 94 | uint32_t line) 95 | { 96 | /* Check if there is an application specific assertion handler configured. */ 97 | if (tbxAssertHandlerPtr != NULL) 98 | { 99 | /* Call the application specific assertion handler. */ 100 | tbxAssertHandlerPtr(file, line); 101 | } 102 | /* Use the default internal assertion handler which simple enters an infinite loop. */ 103 | else 104 | { 105 | for (;;) 106 | { 107 | ; 108 | } 109 | } 110 | } /*** end of TbxAssertTrigger ***/ 111 | #endif /* (TBX_ASSERTIONS_ENABLE > 0U) */ 112 | 113 | 114 | /*********************************** end of tbx_assert.c *******************************/ 115 | -------------------------------------------------------------------------------- /source/tbx_assert.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_assert.h 3 | * \brief Run-time assertions header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_ASSERT_H 37 | #define TBX_ASSERT_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Configuration macros 44 | ****************************************************************************************/ 45 | #ifndef TBX_CONF_ASSERTIONS_ENABLE 46 | /** \brief Enable/disable run-time assertions. Note that it is possible to override this 47 | * value by adding this macro definition to the configuration header file. 48 | */ 49 | #define TBX_CONF_ASSERTIONS_ENABLE (0U) 50 | #endif 51 | 52 | 53 | /**************************************************************************************** 54 | * Macro definitions 55 | ****************************************************************************************/ 56 | #if (TBX_CONF_ASSERTIONS_ENABLE > 0U) 57 | /** \brief Macro for run-time assertions. */ 58 | #define TBX_ASSERT(cond) { if(!(cond)) { TbxAssertTrigger(__FILE__, __LINE__); } } 59 | #else 60 | /** \brief Dummy macro for when assertions are disabled. */ 61 | #define TBX_ASSERT(cond) { ; } 62 | #endif /* (TBX_CONF_ASSERTIONS_ENABLE > 0U) */ 63 | 64 | 65 | /**************************************************************************************** 66 | * Type definitions 67 | ****************************************************************************************/ 68 | /** \brief Function type for a run-time assertion handler function. */ 69 | typedef void (* tTbxAssertHandler)(char const * const file, 70 | uint32_t line); 71 | 72 | 73 | /**************************************************************************************** 74 | * Function prototypes 75 | ****************************************************************************************/ 76 | void TbxAssertSetHandler(tTbxAssertHandler assertHandler); 77 | 78 | #if (TBX_CONF_ASSERTIONS_ENABLE > 0U) 79 | void TbxAssertTrigger (char const * const file, 80 | uint32_t line); 81 | #endif /* (TBX_CONF_ASSERTIONS_ENABLE > 0U) */ 82 | 83 | 84 | #ifdef __cplusplus 85 | } 86 | #endif 87 | 88 | #endif /* TBX_ASSERT_H */ 89 | /*********************************** end of tbx_assert.h *******************************/ 90 | -------------------------------------------------------------------------------- /source/tbx_checksum.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_checksum.c 3 | * \brief Checksum module source file. 4 | * \details The CRC algorithms are based on the examples presents in a how-to blog 5 | * article from the Barr Group. It can be found at: 6 | * barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code. 7 | * \internal 8 | *---------------------------------------------------------------------------------------- 9 | * C O P Y R I G H T 10 | *---------------------------------------------------------------------------------------- 11 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 12 | * 13 | *---------------------------------------------------------------------------------------- 14 | * L I C E N S E 15 | *---------------------------------------------------------------------------------------- 16 | * 17 | * SPDX-License-Identifier: MIT 18 | * 19 | * Permission is hereby granted, free of charge, to any person obtaining a copy 20 | * of this software and associated documentation files (the "Software"), to deal 21 | * in the Software without restriction, including without limitation the rights 22 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 23 | * copies of the Software, and to permit persons to whom the Software is 24 | * furnished to do so, subject to the following conditions: 25 | * 26 | * The above copyright notice and this permission notice shall be included in all 27 | * copies or substantial portions of the Software. 28 | * 29 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 32 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 33 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 34 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 35 | * SOFTWARE. 36 | * 37 | * \endinternal 38 | ****************************************************************************************/ 39 | 40 | /**************************************************************************************** 41 | * Include files 42 | ****************************************************************************************/ 43 | #include "microtbx.h" /* MicroTBX global header */ 44 | 45 | 46 | /**************************************************************************************** 47 | * Configuration macros 48 | ****************************************************************************************/ 49 | #ifndef TBX_CONF_CHECKSUM_CRC16_POLYNOM 50 | /** \brief Polynomial of the 16-bit CRC. Note that it is possible to override this value 51 | * by adding this macro definition to the configuration header file. 52 | */ 53 | #define TBX_CONF_CHECKSUM_CRC16_POLYNOM (0x1021U) 54 | #endif 55 | 56 | #ifndef TBX_CONF_CHECKSUM_CRC16_INITIAL 57 | /** \brief Initial value of the 16-bit CRC calculation. Note that it is possible to 58 | * override this value by adding this macro definition to the configuration 59 | * header file. 60 | */ 61 | #define TBX_CONF_CHECKSUM_CRC16_INITIAL (0xFFFFU) 62 | #endif 63 | 64 | #ifndef TBX_CONF_CHECKSUM_CRC32_POLYNOM 65 | /** \brief Polynomial of the 32-bit CRC. Note that it is possible to override this value 66 | * by adding this macro definition to the configuration header file. 67 | */ 68 | #define TBX_CONF_CHECKSUM_CRC32_POLYNOM (0x04C11DB7UL) 69 | #endif 70 | 71 | #ifndef TBX_CONF_CHECKSUM_CRC32_INITIAL 72 | /** \brief Initial value of the 32-bit CRC calculation. Note that it is possible to 73 | * override this value by adding this macro definition to the configuration 74 | * header file. 75 | */ 76 | #define TBX_CONF_CHECKSUM_CRC32_INITIAL (0xFFFFFFFFUL) 77 | #endif 78 | 79 | 80 | /************************************************************************************//** 81 | ** \brief Calculates a 16-bit CRC value over the specified data. 82 | ** \details It uses the CRC16_CCITT_FALSE algorithm: 83 | ** - Polynomal: 0x1021 84 | ** - Initial value: 0xFFFF 85 | ** - Final Xor value: 0x0000 86 | ** - Input reflected: no 87 | ** - Result reflected: no 88 | ** \param data Array with bytes over which the CRC16 should be calculated. 89 | ** \param len Number of bytes in the data array. 90 | ** \return The 16-bit CRC value. 91 | ** 92 | ****************************************************************************************/ 93 | uint16_t TbxChecksumCrc16Calculate(uint8_t const * data, 94 | size_t len) 95 | { 96 | uint16_t result = 0; 97 | 98 | /* Verify parameters. */ 99 | TBX_ASSERT((data != NULL) && (len > 0U)); 100 | 101 | /* Only continue if the parameters are valid. */ 102 | if ( (data != NULL) && (len > 0U) ) 103 | { 104 | /* Set the initial value. */ 105 | result = TBX_CONF_CHECKSUM_CRC16_INITIAL; 106 | 107 | /* Loop through all data bytes to perform modulo 2 division per byte. */ 108 | for (size_t byteIdx = 0U; byteIdx < len; byteIdx++) 109 | { 110 | /* Introduce the next byte into the remainder. */ 111 | result = result ^ ((uint16_t)data[byteIdx] << 8U); 112 | /* Loop through the bits to perform modulo 2 division per bit. */ 113 | for (uint8_t bitIdx = 0U; bitIdx <= 7U; bitIdx++) 114 | { 115 | /* Attempt to divide the current bit. */ 116 | if ((result & 0x8000U) != 0U) 117 | { 118 | result = ((uint16_t)(result << 1U)) ^ TBX_CONF_CHECKSUM_CRC16_POLYNOM; 119 | } 120 | else 121 | { 122 | result = (uint16_t)(result << 1U); 123 | } 124 | } 125 | } 126 | } 127 | 128 | /* Give the result back to the caller. */ 129 | return result; 130 | } /*** end of TbxChecksumCrc16Calculate ***/ 131 | 132 | 133 | /************************************************************************************//** 134 | ** \brief Calculates a 32-bit CRC value over the specified data. 135 | ** \details It uses the CRC32_MPEG2 algorithm: 136 | ** - Polynomal: 0x04C11DB7 137 | ** - Initial value: 0xFFFFFFFF 138 | ** - Final Xor value: 0x00000000 139 | ** - Input reflected: no 140 | ** - Result reflected: no 141 | ** \param data Array with bytes over which the CRC32 should be calculated. 142 | ** \param len Number of bytes in the data array. 143 | ** \return The 32-bit CRC value. 144 | ** 145 | ****************************************************************************************/ 146 | uint32_t TbxChecksumCrc32Calculate(uint8_t const * data, 147 | size_t len) 148 | { 149 | uint32_t result = 0; 150 | 151 | /* Verify parameters. */ 152 | TBX_ASSERT((data != NULL) && (len > 0U)); 153 | 154 | /* Only continue if the parameters are valid. */ 155 | if ( (data != NULL) && (len > 0U) ) 156 | { 157 | /* Set the initial value. */ 158 | result = TBX_CONF_CHECKSUM_CRC32_INITIAL; 159 | 160 | /* Loop through all data bytes to perform modulo 2 division per byte. */ 161 | for (size_t byteIdx = 0U; byteIdx < len; byteIdx++) 162 | { 163 | /* Introduce the next byte into the remainder. */ 164 | result = result ^ ((uint32_t)data[byteIdx] << 24U); 165 | /* Loop through the bits to perform modulo 2 division per bit. */ 166 | for (uint8_t bitIdx = 0U; bitIdx <= 7U; bitIdx++) 167 | { 168 | /* Attempt to divide the current bit. */ 169 | if ((result & 0x80000000UL) != 0U) 170 | { 171 | result = ((uint32_t)(result << 1U)) ^ TBX_CONF_CHECKSUM_CRC32_POLYNOM; 172 | } 173 | else 174 | { 175 | result = (uint32_t)(result << 1U); 176 | } 177 | } 178 | } 179 | } 180 | 181 | /* Give the result back to the caller. */ 182 | return result; 183 | } /*** end of TbxChecksumCrc32Calculate ***/ 184 | 185 | 186 | /*********************************** end of tbx_checksum.c *****************************/ 187 | -------------------------------------------------------------------------------- /source/tbx_checksum.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_checksum.h 3 | * \brief Checksum module header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_CHECKSUM_H 37 | #define TBX_CHECKSUM_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | 43 | 44 | /**************************************************************************************** 45 | * Function prototypes 46 | ****************************************************************************************/ 47 | uint16_t TbxChecksumCrc16Calculate(uint8_t const * data, 48 | size_t len); 49 | 50 | uint32_t TbxChecksumCrc32Calculate(uint8_t const * data, 51 | size_t len); 52 | 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif /* TBX_CHECKSUM_H */ 59 | /*********************************** end of tbx_checksum.h *****************************/ 60 | -------------------------------------------------------------------------------- /source/tbx_critsect.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_critsect.c 3 | * \brief Critical sections source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include "microtbx.h" /* MicroTBX global header */ 41 | 42 | 43 | /**************************************************************************************** 44 | * Local data declarations 45 | ****************************************************************************************/ 46 | /** \brief Counter that gets incremented each time a critical section is entered and 47 | * decremented each time a critical section is left. 48 | */ 49 | static volatile uint32_t tbxCritSectNestingCounter = 0U; 50 | 51 | /** \brief Holds the copy of the CPU status register from right before the interrupts 52 | * were disabled. Needed to restore the interrupts enabled/disabled status upon 53 | * exiting the critical section, 54 | */ 55 | static volatile tTbxPortCpuSR tbxCritSectCpuSR = 0U; 56 | 57 | 58 | /************************************************************************************//** 59 | ** \brief Enter a critical section. Critical sections are needed in an interrupt 60 | ** driven software program to obtain mutual exclusive access shared resources 61 | ** such as global data and certain peripherals. Note that each call to this 62 | ** function should always be followed by a call to TbxCriticalSectionExit(). 63 | ** For example: 64 | ** TbxCriticalSectionEnter(); 65 | ** ...access shared resource... 66 | ** TbxCriticalSectionExit(); 67 | ** 68 | ****************************************************************************************/ 69 | void TbxCriticalSectionEnter(void) 70 | { 71 | tTbxPortCpuSR cpuSR; 72 | 73 | /* Disable the interrupts and store the CPU status register value in a local variable. 74 | * Note that it should not write directly to tbxCritSectCpuSR yet, because the 75 | * tbxCritSectCpuSR variable is a shared resource and should only be accessed with 76 | * interrupts disabled. 77 | */ 78 | cpuSR = TbxPortInterruptsDisable(); 79 | 80 | /* It this the first time we enter the critical section, as opposed to a nested 81 | * entry? 82 | */ 83 | if (tbxCritSectNestingCounter == 0U) 84 | { 85 | /* Store the CPU status register value in tbxCritSectCpuSR, since it is now safe 86 | * to access it. It is needed to restore the interrupt status upon exiting the 87 | * critical section. 88 | */ 89 | tbxCritSectCpuSR = cpuSR; 90 | } 91 | /* Increment the nesting counter. */ 92 | tbxCritSectNestingCounter++; 93 | } /*** end of TbxCriticalSectionEnter ***/ 94 | 95 | 96 | /************************************************************************************//** 97 | ** \brief Exit a critical section. Critical sections are needed in an interrupt 98 | ** driven software program to obtain mutual exclusive access shared resources 99 | ** such as global data and certain peripherals. Note that each call to this 100 | ** function should always be preceded by a call to TbxCriticalSectionEnter(). 101 | ** For example: 102 | ** TbxCriticalSectionEnter(); 103 | ** ...access shared resource... 104 | ** TbxCriticalSectionExit(); 105 | ** 106 | ****************************************************************************************/ 107 | void TbxCriticalSectionExit(void) 108 | { 109 | /* A call to this function must always be preceeded by a call to 110 | * TbxCriticalSectionEnter(). This means the tbxCritSectNestingCounter must be > 0. 111 | */ 112 | TBX_ASSERT(tbxCritSectNestingCounter > 0U); 113 | 114 | /* Only continue if this function call was preceeded by a call to 115 | * TbxCriticalSectionEnter(). 116 | */ 117 | if (tbxCritSectNestingCounter > 0U) 118 | { 119 | /* Decrement the nesting counter. */ 120 | tbxCritSectNestingCounter--; 121 | 122 | /* Is this the final call meaning that it is time we exit the critical section by 123 | * actually restoring the interrupt status again? 124 | */ 125 | if (tbxCritSectNestingCounter == 0U) 126 | { 127 | /* Restore the interrupt status to the state it was right before the interrupts 128 | * were all disabled upon the first time the critical section was entered. 129 | */ 130 | TbxPortInterruptsRestore(tbxCritSectCpuSR); 131 | } 132 | } 133 | } /*** end of TbxCriticalSectionExit ***/ 134 | 135 | 136 | /*********************************** end of tbx_critsect.c *****************************/ 137 | -------------------------------------------------------------------------------- /source/tbx_critsect.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_critsect.h 3 | * \brief Critical sections header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_CRITSECT_H 37 | #define TBX_CRITSECT_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Function prototypes 44 | ****************************************************************************************/ 45 | void TbxCriticalSectionEnter(void); 46 | 47 | void TbxCriticalSectionExit (void); 48 | 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif /* TBX_CRITSECT_H */ 55 | /*********************************** end of tbx_critsect.h *****************************/ 56 | -------------------------------------------------------------------------------- /source/tbx_crypto.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_crypto.c 3 | * \brief Cryptography module source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include "microtbx.h" /* MicroTBX global header */ 41 | #include "tbx_aes256.h" /* AES256 cryptography */ 42 | 43 | 44 | /**************************************************************************************** 45 | * Macro definitions 46 | ****************************************************************************************/ 47 | /** \brief Size of an AES block. Decryption is performed in this block size. */ 48 | #define TBX_CRYPTO_AES_BLOCK_SIZE (16U) 49 | 50 | 51 | /************************************************************************************//** 52 | ** \brief Encrypts the len-bytes in the specified data-array, using the specified 53 | ** 256-bit (32 bytes) key. The results are written back into the same array. 54 | ** \param data Pointer to the byte array with data to encrypt. The encrypted bytes 55 | ** are stored in the same array. 56 | ** \param len The number of bytes in the data-array to encrypt. It must be a multiple 57 | ** of 16, as this is the AES256 minimal block size. 58 | ** \param key The 256-bit encryption key as a array of 32 bytes. 59 | ** 60 | ****************************************************************************************/ 61 | void TbxCryptoAes256Encrypt(uint8_t * data, 62 | size_t len, 63 | uint8_t const * key) 64 | { 65 | tbx_aes256_context ctx; 66 | 67 | /* Verify parameters. */ 68 | TBX_ASSERT(data != NULL); 69 | TBX_ASSERT(len > 0U); 70 | TBX_ASSERT(key != NULL); 71 | TBX_ASSERT((len % TBX_CRYPTO_AES_BLOCK_SIZE) == 0U); 72 | 73 | /* Only continue if the parameters are valid. */ 74 | if ( (data != NULL) && (len > 0U) && (key != NULL) && \ 75 | ((len % TBX_CRYPTO_AES_BLOCK_SIZE) == 0U) ) 76 | { 77 | /* Initialize the context. */ 78 | tbx_aes256_init(&ctx, key); 79 | /* Encrypt in blocks of 16 bytes. */ 80 | for (size_t idx = 0U; idx < (len / TBX_CRYPTO_AES_BLOCK_SIZE); idx++) 81 | { 82 | tbx_aes256_encrypt_ecb(&ctx, &data[idx * TBX_CRYPTO_AES_BLOCK_SIZE]); 83 | } 84 | /* Cleanup */ 85 | tbx_aes256_done(&ctx); 86 | } 87 | } /*** end of TbxCryptoAes256Encrypt ***/ 88 | 89 | 90 | /************************************************************************************//** 91 | ** \brief Decrypts the len-bytes in the specified data-array, using the specified 256- 92 | ** bit (32 bytes) key. The results are written back into the same array. 93 | ** \param data Pointer to the byte array with data to decrypt. The decrypted bytes 94 | ** are stored in the same array. 95 | ** \param len The number of bytes in the data-array to decrypt. It must be a multiple 96 | ** of 16, as this is the AES256 minimal block size. 97 | ** \param key The 256-bit decryption key as a array of 32 bytes. 98 | ** 99 | ****************************************************************************************/ 100 | void TbxCryptoAes256Decrypt(uint8_t * data, 101 | size_t len, 102 | uint8_t const * key) 103 | { 104 | tbx_aes256_context ctx; 105 | 106 | /* Verify parameters. */ 107 | TBX_ASSERT(data != NULL); 108 | TBX_ASSERT(len > 0U); 109 | TBX_ASSERT(key != NULL); 110 | TBX_ASSERT((len % TBX_CRYPTO_AES_BLOCK_SIZE) == 0U); 111 | 112 | /* Only continue if the parameters are valid. */ 113 | if ( (data != NULL) && (len > 0U) && (key != NULL) && \ 114 | ((len % TBX_CRYPTO_AES_BLOCK_SIZE) == 0U) ) 115 | { 116 | /* Initialize the context. */ 117 | tbx_aes256_init(&ctx, key); 118 | /* Decrypt in blocks of 16 bytes. */ 119 | for (size_t idx = 0U; idx < (len / TBX_CRYPTO_AES_BLOCK_SIZE); idx++) 120 | { 121 | tbx_aes256_decrypt_ecb(&ctx, &data[idx * TBX_CRYPTO_AES_BLOCK_SIZE]); 122 | } 123 | /* Cleanup */ 124 | tbx_aes256_done(&ctx); 125 | } 126 | } /*** end of TbxCryptoAes256Decrypt ***/ 127 | 128 | 129 | /*********************************** end of tbx_crypto.c *******************************/ 130 | -------------------------------------------------------------------------------- /source/tbx_crypto.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_crypto.h 3 | * \brief Cryptography module header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_CRYPTO_H 37 | #define TBX_CRYPTO_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Function prototypes 44 | ****************************************************************************************/ 45 | void TbxCryptoAes256Encrypt(uint8_t * data, 46 | size_t len, 47 | uint8_t const * key); 48 | 49 | void TbxCryptoAes256Decrypt(uint8_t * data, 50 | size_t len, 51 | uint8_t const * key); 52 | 53 | 54 | #ifdef __cplusplus 55 | } 56 | #endif 57 | 58 | #endif /* TBX_CRYPTO_H */ 59 | /*********************************** end of tbx_crypto.h *******************************/ 60 | -------------------------------------------------------------------------------- /source/tbx_heap.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_heap.c 3 | * \brief Heap memory allocation source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include "microtbx.h" /* MicroTBX global header */ 41 | 42 | 43 | /**************************************************************************************** 44 | * Configuration macros 45 | ****************************************************************************************/ 46 | #ifndef TBX_CONF_HEAP_SIZE 47 | /** \brief Configure the size of the heap in bytes. Note that it is possible to override 48 | * this value by adding this macro definition to the configuration header file. 49 | */ 50 | #define TBX_CONF_HEAP_SIZE (1024U) 51 | #endif 52 | 53 | 54 | /**************************************************************************************** 55 | * Local data declarations 56 | ****************************************************************************************/ 57 | /** \brief Keeps track of how many bytes have already been allocated on the heap. */ 58 | static size_t tbxHeapAllocated = 0U; 59 | 60 | 61 | /************************************************************************************//** 62 | ** \brief Allocates the desired number of bytes on the heap. It can be used instead 63 | ** of the compiler specific malloc() function. Note that free-ing of allocated 64 | ** memory is not supported to prevent memory fragmentation. 65 | ** \param size The number of bytes to allocate on the heap. 66 | ** \return Pointer to the start of the newly allocated heap memory if successful, 67 | ** NULL otherwise. 68 | ** 69 | ****************************************************************************************/ 70 | void * TbxHeapAllocate(size_t size) 71 | { 72 | /* cppcheck-suppress [unassignedVariable,unmatchedSuppression] 73 | * The actual heap buffer. Whenever memory needs to be dynamically allocated, it will 74 | * be taken from this buffer. As such, it is okay to not be initialized and therefore 75 | * the warning about no value being assigned to this variable can be ignored. 76 | */ 77 | static uint8_t tbxHeapBuffer[TBX_CONF_HEAP_SIZE]; 78 | void * result = NULL; 79 | 80 | /* Verify parameter. */ 81 | TBX_ASSERT(size > 0U); 82 | 83 | /* Only continue if the parameters are valid. */ 84 | if (size > 0U) 85 | { 86 | /* Align the desired size to the address size to make it work on all targets. */ 87 | size_t sizeWanted = (size + (sizeof(void *) - 1U)) & ~(sizeof(void *) - 1U); 88 | /* Obtain mutual exclusive access to tbxHeapAllocated. */ 89 | TbxCriticalSectionEnter(); 90 | /* Determine the number of still available bytes in the heap buffer. */ 91 | size_t sizeAvailable = TBX_CONF_HEAP_SIZE - tbxHeapAllocated; 92 | /* Is there enough space left on the heap for this allocation request? */ 93 | if (sizeAvailable >= sizeWanted) 94 | { 95 | /* Set the address for the newly allocated memory. */ 96 | result = &tbxHeapBuffer[tbxHeapAllocated]; 97 | /* Perform the actual allocation by incrementing the counter. */ 98 | tbxHeapAllocated += sizeWanted; 99 | } 100 | /* Release mutual exclusive access to tbxHeapAllocated. */ 101 | TbxCriticalSectionExit(); 102 | } 103 | 104 | /* Return the address of the allocated memory to the caller. */ 105 | return result; 106 | } /*** end of TbxHeapAllocate ***/ 107 | 108 | 109 | /************************************************************************************//** 110 | ** \brief Obtains the current amount of bytes that are still available on the heap. 111 | ** \return Number of free bytes on the heap. 112 | ** 113 | ****************************************************************************************/ 114 | size_t TbxHeapGetFree(void) 115 | { 116 | size_t result; 117 | 118 | /* Obtain mutual exclusive access to tbxHeapAllocated. */ 119 | TbxCriticalSectionEnter(); 120 | /* Determine the number of still available bytes in the heap buffer. */ 121 | result = TBX_CONF_HEAP_SIZE - tbxHeapAllocated; 122 | /* Release mutual exclusive access to tbxHeapAllocated. */ 123 | TbxCriticalSectionExit(); 124 | 125 | /* Give the result back to the caller. */ 126 | return result; 127 | } /*** end of TbxHeapGetFree ***/ 128 | 129 | 130 | /*********************************** end of tbx_heap.c *********************************/ 131 | -------------------------------------------------------------------------------- /source/tbx_heap.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_heap.h 3 | * \brief Heap memory allocation header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_HEAP_H 37 | #define TBX_HEAP_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Function prototypes 44 | ****************************************************************************************/ 45 | void * TbxHeapAllocate(size_t size); 46 | 47 | size_t TbxHeapGetFree (void); 48 | 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif /* TBX_HEAP_H */ 55 | /*********************************** end of tbx_heap.h *********************************/ 56 | -------------------------------------------------------------------------------- /source/tbx_list.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_list.h 3 | * \brief Linked lists header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2020 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_LIST_H 37 | #define TBX_LIST_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Type definitions 44 | ****************************************************************************************/ 45 | /** \brief Layout of a linked list node, which forms the building block of a linked list 46 | * internally. Note that its elements should be considered private and only be 47 | * accessed internally by this linked list module. 48 | */ 49 | typedef struct t_tbx_list_node 50 | { 51 | /** \brief Pointer to the actual item stored in this node. */ 52 | void * itemPtr; 53 | /** \brief Pointer to the previous node in the list or NULL if it is the list start. */ 54 | struct t_tbx_list_node * prevNodePtr; 55 | /** \brief Pointer to the next node in the list or NULL if it is the list end. */ 56 | struct t_tbx_list_node * nextNodePtr; 57 | } tTbxListNode; 58 | 59 | /** \brief Layout of a linked list. Its pointer serves as the handle to the linked list 60 | * which is obtained after creation of the list and which is needed in the other 61 | * functions of this module. Note that its elements should be considered private 62 | * and only be accessed internally by this linked list module. 63 | */ 64 | typedef struct 65 | { 66 | /** \brief Total number of nodes that are currently present in the linked list. */ 67 | size_t nodeCount; 68 | /** \brief Pointer to the first node of the linked list, also known as the head. */ 69 | tTbxListNode * firstNodePtr; 70 | /** \brief Pointer to the last node of the linked list, also known as the tail. */ 71 | tTbxListNode * lastNodePtr; 72 | } tTbxList; 73 | 74 | /** \brief Callback function to compare items. It is called during list sorting. The 75 | * return value of the callback function has the following meaning: TBX_TRUE if 76 | * item1's data is greater than item2's data, TBX_FALSE otherwise. 77 | */ 78 | typedef uint8_t (* tTbxListCompareItems)(void const * item1, 79 | void const * item2); 80 | 81 | 82 | /**************************************************************************************** 83 | * Function prototypes 84 | ****************************************************************************************/ 85 | tTbxList * TbxListCreate (void); 86 | 87 | void TbxListDelete (tTbxList * list); 88 | 89 | void TbxListClear (tTbxList * list); 90 | 91 | size_t TbxListGetSize (tTbxList const * list); 92 | 93 | uint8_t TbxListInsertItemFront(tTbxList * list, 94 | void * item); 95 | 96 | uint8_t TbxListInsertItemBack (tTbxList * list, 97 | void * item); 98 | 99 | uint8_t TbxListInsertItemBefore(tTbxList * list, 100 | void * item, 101 | void const * itemRef); 102 | 103 | uint8_t TbxListInsertItemAfter (tTbxList * list, 104 | void * item, 105 | void const * itemRef); 106 | 107 | void TbxListRemoveItem (tTbxList * list, 108 | void const * item); 109 | 110 | void * TbxListGetFirstItem (tTbxList const * list); 111 | 112 | void * TbxListGetLastItem (tTbxList const * list); 113 | 114 | void * TbxListGetPreviousItem (tTbxList const * list, 115 | void const * itemRef); 116 | 117 | void * TbxListGetNextItem (tTbxList const * list, 118 | void const * itemRef); 119 | 120 | void TbxListSwapItems (tTbxList const * list, 121 | void * item1, 122 | void * item2); 123 | 124 | void TbxListSortItems (tTbxList const * list, 125 | tTbxListCompareItems compareItemsFcn); 126 | 127 | 128 | #ifdef __cplusplus 129 | } 130 | #endif 131 | 132 | #endif /* TBX_LIST_H */ 133 | /*********************************** end of tbx_list.h *********************************/ 134 | -------------------------------------------------------------------------------- /source/tbx_mempool.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_mempool.h 3 | * \brief Pool based dynamic heap memory manager header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_MEMPOOL_H 37 | #define TBX_MEMPOOL_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Function prototypes 44 | ****************************************************************************************/ 45 | uint8_t TbxMemPoolCreate (size_t numBlocks, 46 | size_t blockSize); 47 | 48 | void * TbxMemPoolAllocate(size_t size); 49 | 50 | void TbxMemPoolRelease (void * memPtr); 51 | 52 | 53 | #ifdef __cplusplus 54 | } 55 | #endif 56 | 57 | #endif /* TBX_MEMPOOL_H */ 58 | /*********************************** end of tbx_mempool.h ******************************/ 59 | -------------------------------------------------------------------------------- /source/tbx_platform.c: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_platform.c 3 | * \brief Platform specifics module source file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2023 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | 37 | /**************************************************************************************** 38 | * Include files 39 | ****************************************************************************************/ 40 | #include "microtbx.h" /* MicroTBX global header */ 41 | 42 | 43 | /************************************************************************************//** 44 | ** \brief Utility function to determine if the targets memory storage organization 45 | ** is little endian (Intel) or big endian (Motorola). 46 | ** \return TBX_TRUE for little endian, TBX_FALSE for big endian. 47 | ** 48 | ****************************************************************************************/ 49 | uint8_t TbxPlatformLittleEndian(void) 50 | { 51 | uint8_t result = TBX_FALSE; 52 | volatile uint32_t endianTestVal = 0x01234567UL; 53 | volatile uint8_t const * firstBytePtr; 54 | 55 | /* Initialize the pointer to the first byte of the test value. */ 56 | firstBytePtr = (volatile uint8_t const *)&endianTestVal; 57 | 58 | /* Is the target little endian? In this case the LSB is stored first in memory. */ 59 | if (firstBytePtr[0] == 0x67U) 60 | { 61 | result = TBX_TRUE; 62 | } 63 | /* Give the result back to the caller. */ 64 | return result; 65 | } /*** end of TbxPlatformLittleEndian ***/ 66 | 67 | 68 | /*********************************** end of tbx_platform.c *****************************/ 69 | -------------------------------------------------------------------------------- /source/tbx_platform.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_platform.h 3 | * \brief Platform specifics module header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2023 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_PLATFORM_H 37 | #define TBX_PLATFORM_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Function prototypes 44 | ****************************************************************************************/ 45 | uint8_t TbxPlatformLittleEndian(void); 46 | 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | 52 | #endif /* TBX_PLATFORM_H */ 53 | /*********************************** end of tbx_platform.h *****************************/ 54 | -------------------------------------------------------------------------------- /source/tbx_port.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_port.h 3 | * \brief Port specifics header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_PORT_H 37 | #define TBX_PORT_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Include files 44 | ****************************************************************************************/ 45 | #include "tbx_types.h" /* MicroTBX port specific types */ 46 | 47 | 48 | /**************************************************************************************** 49 | * Function prototypes 50 | ****************************************************************************************/ 51 | tTbxPortCpuSR TbxPortInterruptsDisable(void); 52 | 53 | void TbxPortInterruptsRestore(tTbxPortCpuSR prevCpuSr); 54 | 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif /* TBX_PORT_H */ 61 | /*********************************** end of tbx_port.h *********************************/ 62 | -------------------------------------------------------------------------------- /source/tbx_random.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_random.h 3 | * \brief Random number generator header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_RANDOM_H 37 | #define TBX_RANDOM_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Type definitions 44 | ****************************************************************************************/ 45 | /** \brief Function type for an application specific seed initialization handler. */ 46 | typedef uint32_t (* tTbxRandomSeedInitHandler)(void); 47 | 48 | 49 | /**************************************************************************************** 50 | * Function prototypes 51 | ****************************************************************************************/ 52 | uint32_t TbxRandomNumberGet (void); 53 | 54 | void TbxRandomSetSeedInitHandler(tTbxRandomSeedInitHandler seedInitHandler); 55 | 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* TBX_RANDOM_H */ 62 | /*********************************** end of tbx_random.h *******************************/ 63 | -------------------------------------------------------------------------------- /source/template/tbx_conf.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file tbx_conf.h 3 | * \brief MicroTBX configuration header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2019 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef TBX_CONF_H 37 | #define TBX_CONF_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * A S S E R T I O N S M O D U L E C O N F I G U R A T I O N 44 | ****************************************************************************************/ 45 | /** \brief Enable/disable run-time assertions. */ 46 | #define TBX_CONF_ASSERTIONS_ENABLE (1U) 47 | 48 | 49 | /**************************************************************************************** 50 | * H E A P M O D U L E C O N F I G U R A T I O N 51 | ****************************************************************************************/ 52 | /** \brief Configure the size of the heap in bytes. */ 53 | #define TBX_CONF_HEAP_SIZE (2048U) 54 | 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif /* TBX_CONF_H */ 61 | /*********************************** end of tbx_conf.h *********************************/ 62 | -------------------------------------------------------------------------------- /source/tests/unittests.h: -------------------------------------------------------------------------------- 1 | /************************************************************************************//** 2 | * \file unittests.h 3 | * \brief Unit tests header file. 4 | * \internal 5 | *---------------------------------------------------------------------------------------- 6 | * C O P Y R I G H T 7 | *---------------------------------------------------------------------------------------- 8 | * Copyright (c) 2022 by Feaser www.feaser.com All rights reserved 9 | * 10 | *---------------------------------------------------------------------------------------- 11 | * L I C E N S E 12 | *---------------------------------------------------------------------------------------- 13 | * 14 | * SPDX-License-Identifier: MIT 15 | * 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in all 24 | * copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 | * SOFTWARE. 33 | * 34 | * \endinternal 35 | ****************************************************************************************/ 36 | #ifndef UNITTESTS_H 37 | #define UNITTESTS_H 38 | 39 | #ifdef __cplusplus 40 | extern "C" { 41 | #endif 42 | /**************************************************************************************** 43 | * Function prototypes 44 | ****************************************************************************************/ 45 | void initializeTests(void); 46 | 47 | int runTests(void); 48 | 49 | 50 | #ifdef __cplusplus 51 | } 52 | #endif 53 | 54 | #endif /* UNITTESTS_H */ 55 | /*********************************** end of unittests.h ********************************/ 56 | --------------------------------------------------------------------------------