
Typical controller demo program based on Seeed Arch Max. Features: - Multi-thread architecture - Inter-thread message communication - Independent command shell using thread - HTTPD with CGI, WS, RPC - Key & value pair configuration load/save
Dependencies: CMDB EthernetInterface HTTPD dconfig mbed-rpc mbed-rtos mbed storage_on_flash
util.cpp@0:2ffd10976643, 2015-03-25 (annotated)
- Committer:
- hillkim7
- Date:
- Wed Mar 25 21:56:51 2015 +0000
- Revision:
- 0:2ffd10976643
Typical controller demo program based on Seeed Arch Max.; Features:; - Multi-thread architecture; - Inter-thread message communication; - Independent command shell using thread; - HTTPD with CGI, WS, RPC; - Key & value pair configuration load/save
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hillkim7 | 0:2ffd10976643 | 1 | /** |
hillkim7 | 0:2ffd10976643 | 2 | * @file util.cpp |
hillkim7 | 0:2ffd10976643 | 3 | * |
hillkim7 | 0:2ffd10976643 | 4 | * @brief system wide utility function |
hillkim7 | 0:2ffd10976643 | 5 | * |
hillkim7 | 0:2ffd10976643 | 6 | */ |
hillkim7 | 0:2ffd10976643 | 7 | |
hillkim7 | 0:2ffd10976643 | 8 | #include <stdio.h> |
hillkim7 | 0:2ffd10976643 | 9 | #include <stdarg.h> |
hillkim7 | 0:2ffd10976643 | 10 | #include <stdlib.h> |
hillkim7 | 0:2ffd10976643 | 11 | #include <stdint.h> |
hillkim7 | 0:2ffd10976643 | 12 | #include <string.h> |
hillkim7 | 0:2ffd10976643 | 13 | |
hillkim7 | 0:2ffd10976643 | 14 | #if defined(__CC_ARM) && !defined(__MICROLIB) |
hillkim7 | 0:2ffd10976643 | 15 | // Keil MDK with microlib supports __heapstats(); |
hillkim7 | 0:2ffd10976643 | 16 | #define SUPPORT_HEAPSTATS |
hillkim7 | 0:2ffd10976643 | 17 | #endif |
hillkim7 | 0:2ffd10976643 | 18 | |
hillkim7 | 0:2ffd10976643 | 19 | #if defined(SUPPORT_HEAPSTATS) |
hillkim7 | 0:2ffd10976643 | 20 | |
hillkim7 | 0:2ffd10976643 | 21 | static void heap_printf(void *dummy, const char *fmt, ...) |
hillkim7 | 0:2ffd10976643 | 22 | { |
hillkim7 | 0:2ffd10976643 | 23 | va_list arg_ptr; |
hillkim7 | 0:2ffd10976643 | 24 | |
hillkim7 | 0:2ffd10976643 | 25 | if (strchr(fmt, '\n') != NULL) { |
hillkim7 | 0:2ffd10976643 | 26 | putchar('\r'); |
hillkim7 | 0:2ffd10976643 | 27 | } |
hillkim7 | 0:2ffd10976643 | 28 | va_start(arg_ptr, fmt); |
hillkim7 | 0:2ffd10976643 | 29 | vprintf(fmt, arg_ptr); |
hillkim7 | 0:2ffd10976643 | 30 | va_end(arg_ptr); |
hillkim7 | 0:2ffd10976643 | 31 | } |
hillkim7 | 0:2ffd10976643 | 32 | |
hillkim7 | 0:2ffd10976643 | 33 | void print_memstat(void) |
hillkim7 | 0:2ffd10976643 | 34 | { |
hillkim7 | 0:2ffd10976643 | 35 | __heapstats((__heapprt)heap_printf, NULL); |
hillkim7 | 0:2ffd10976643 | 36 | printf("\r\n"); |
hillkim7 | 0:2ffd10976643 | 37 | } |
hillkim7 | 0:2ffd10976643 | 38 | |
hillkim7 | 0:2ffd10976643 | 39 | #else |
hillkim7 | 0:2ffd10976643 | 40 | |
hillkim7 | 0:2ffd10976643 | 41 | /** |
hillkim7 | 0:2ffd10976643 | 42 | * Compute max consecutive memory chunk, by trying to allocate it. |
hillkim7 | 0:2ffd10976643 | 43 | */ |
hillkim7 | 0:2ffd10976643 | 44 | static uint32_t comput_free_heap(uint32_t resolution, uint32_t maximum) |
hillkim7 | 0:2ffd10976643 | 45 | { |
hillkim7 | 0:2ffd10976643 | 46 | int low = 0; |
hillkim7 | 0:2ffd10976643 | 47 | int high = maximum + 1; |
hillkim7 | 0:2ffd10976643 | 48 | |
hillkim7 | 0:2ffd10976643 | 49 | while (high - low > resolution) { |
hillkim7 | 0:2ffd10976643 | 50 | int mid = (low + high) / 2; |
hillkim7 | 0:2ffd10976643 | 51 | void* p = malloc(mid); |
hillkim7 | 0:2ffd10976643 | 52 | if (p == NULL) { |
hillkim7 | 0:2ffd10976643 | 53 | high = mid; |
hillkim7 | 0:2ffd10976643 | 54 | } else { |
hillkim7 | 0:2ffd10976643 | 55 | free(p); |
hillkim7 | 0:2ffd10976643 | 56 | low = mid; |
hillkim7 | 0:2ffd10976643 | 57 | } |
hillkim7 | 0:2ffd10976643 | 58 | } |
hillkim7 | 0:2ffd10976643 | 59 | |
hillkim7 | 0:2ffd10976643 | 60 | return low; |
hillkim7 | 0:2ffd10976643 | 61 | } |
hillkim7 | 0:2ffd10976643 | 62 | |
hillkim7 | 0:2ffd10976643 | 63 | void print_memstat(void) |
hillkim7 | 0:2ffd10976643 | 64 | { |
hillkim7 | 0:2ffd10976643 | 65 | printf("heap free %u\r\n", comput_free_heap(512, 192*1024)); |
hillkim7 | 0:2ffd10976643 | 66 | } |
hillkim7 | 0:2ffd10976643 | 67 | |
hillkim7 | 0:2ffd10976643 | 68 | #endif |
hillkim7 | 0:2ffd10976643 | 69 | |
hillkim7 | 0:2ffd10976643 | 70 |