Library 58- Dynamic strings on STM32F4xx
Have you ever been in position where you need to store some string informations just for a small amount of time? Did you reserve ram memory before you worked with strings?
If you did it like that, than this library might be a solution for you.
This library was designed directly for a purpose where you need to store your information for a few time. For example, getting some string informations from your sensor, USART, etc.
It uses malloc and free functions to allocate a memory inside HEAP section of RAM. So, if you have in plain to use a biiig array of strings, first make sure that your HEAP section is big enough where malloc and free functions can operate.
Library
Features
- Dynamic string operation
- Allocation size depends on string size only, no preallocations
- Allows for dynamically search for new sections of memory if current preallocated memory is full
- String deletion allowed one by one or all at a time
Dependencies
- CMSIS
- STM32F4xx
- TM
- defines.h
- defines.h
- C
- string.h
- stdlib.h
As I said above, library uses HEAP memory. If you use Keil uVision and startup files provided from ST or if you downloaded it from my Github or default Keil uVision project, then HEAP memory is by default set to 0x200 Bytes = 512kB which can be quite small for something like strings can be in size.
So, in case of Keil uVision, you can open .s (startup file) inside your project and edit variable “Heap_Size” to a value you want to reserve for HEAP.
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
/** * @defgroup TM_STRING_Typedefs * @brief Library Typedefs * @{ */ /** * @brief Main string structure */ typedef struct { char** Strings; /*!< Pointer to pointers to strings */ uint32_t Count; /*!< Number of string elementsz */ uint32_t Size; /*!< Number of all allocated pointers for strings */ } TM_STRING_t; /** * @} */ /** * @defgroup TM_STRING_Functions * @brief Library Functions * @{ */ /** * @brief Creates and allocated string structure and memory for pointers for desired number of strings * @note Function uses @ref malloc() to allocate main structure and all pointers depending on count variable, * so make sure that you have enough HEAP memory reserved * @param count: Number of strings you will use. Set to 1, if you don't know how many of them will be used. * It's recommended that you select the number which is greater or equal to count of all strings * @retval Pointer to allocated @ref TM_STRING_t structure or NULL of malloc() fails */ TM_STRING_t* TM_STRING_Create(uint16_t count); /** * @brief Adds new string to main string structure * @note Function uses @ref malloc() to allocate memory for string where it will copy it. * Memory size for allocation is string length + 1 * @param *String: Pointer to @ref TM_STRING_t structure * @param *str: Pointer to string to be added to string * @retval String position in strings array */ uint16_t TM_STRING_AddString(TM_STRING_t* String, char* str); /** * @brief Replaces already added string with new string * @note If new string is larger than string before, new memory is allocated and old is free, * but if new string length is smaller than old, only new string is copied to already allocateed memory * @param *String: Pointer to @ref TM_STRING_t structure * @param pos: Position in array where to replace string * @param *str: Pointer to new string which will be applied to memory * @retval Pointer to @ref TM_STRING_t structure */ TM_STRING_t* TM_STRING_ReplaceString(TM_STRING_t* String, uint16_t pos, char* str); /** * @brief Deletes string from strings array * @param *String: Pointer to @ref TM_STRING_t structure * @param pos: Position number in string array which string will be deleted. * This number can be a value between 0 and number of strings - 1 * @retval Pointer to @ref TM_STRING_t structure */ TM_STRING_t* TM_STRING_DeleteString(TM_STRING_t* String, uint16_t pos); /** * @brief Gets pointer to string at desired position * @param *String: Pointer to @ref TM_STRING_t structure * @param pos: Position number in string array which string pointer will be returned. * This number can be a value between 0 and number of strings - 1 * @retval Pointer to string at desired position in string array */ char* TM_STRING_GetString(TM_STRING_t* String, uint16_t pos); /** * @brief Free all. It will free all strings, all pointers to strings and also main string structure * @note Use @ref TM_STRING_DeleteString() to delete only one string if needed * @param *String: Pointer to @ref TM_STRING_t structure * @retval None */ void TM_STRING_FreeAll(TM_STRING_t* String); /** * @brief Gets number of strings in a string array * @param *String: Pointer to @ref TM_STRING_t structure * @retval Number of strings in array * @note Defined as macro for faster execution */ #define TM_STRING_GetCount(str) ((str)->Count) /** * @brief Free main structure and pointer to all pointers. * @note This is not meant for public use unless you know what you are doing. * It will deallocate structure, but all strings will stay untouched in heap memory! * Use @ref TM_STRING_FreeAll() instead. * @param *String: Pointer to @ref TM_STRING_t structure */ void TM_STRING_Free(TM_STRING_t* String); /** * @} */ |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
/** * Keil project for STRINGS library * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx Keil packs version 2.4.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.5.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_usart.h" #include "tm_stm32f4_string.h" #include "stdio.h" /* String structure */ TM_STRING_t* String; int main(void) { uint16_t i = 0; /* Initialize system */ SystemInit(); /* Init USART2, TX: PA2, RX: PA3, 921600 bauds */ TM_USART_Init(USART2, TM_USART_PinsPack_1, 921600); /* Create string with 10 predefined locations for strings */ String = TM_STRING_Create(10); /* Add string to memory, allocated memory will be set depending on string length */ TM_STRING_AddString(String, "First string"); /* Add another string to memory, allocated memory will be set depending on string length */ TM_STRING_AddString(String, "Second string"); /* Send strings over USART */ for (i = 0; i < String->Count; i++) { /* Print string to user */ printf("%s\n", String->Strings[i]); } /* Delete string on position 1 = "Second string" */ TM_STRING_DeleteString(String, 1); /* Free entire string memory with all strings */ TM_STRING_FreeAll(String); while (1) { } } /* Printf handler */ int fputc(int ch, FILE* fil) { /* Send over USART */ TM_USART_Putc(USART2, ch); /* Return character */ return ch; } |
Project is available on my Github account, download library below.
Recent comments