From 8e79e24ea4f427230c85ddf1004f34a6114ff633 Mon Sep 17 00:00:00 2001 From: adabreuti <76965454+adabreuti@users.noreply.github.com> Date: Fri, 19 Apr 2024 14:32:21 -0500 Subject: [PATCH] [TI] Update Number of LWIP Buffers, Fix ICD GPIO build error (#33053) * Update Number of LWIP Buffers, Fix ICD GPIO build error Update memory interface to support realloc, decrease num message buffers Fix ICD LED Build failures * Restyled by whitespace * Restyled by clang-format * Keep old implementation of realloc for CC3220 --------- Co-authored-by: Restyled.io --- .../cc13x4_26x4/main/AppTask.cpp | 7 - .../cc13x4_26x4/main/Globals.cpp | 2 + .../cc13x4_26x4/main/include/Globals.h | 7 + .../lighting-app/cc13x4_26x4/src/AppTask.cpp | 3 +- examples/lock-app/cc13x4_26x4/src/AppTask.cpp | 3 +- .../project_include/OpenThreadConfig.h | 1 + .../pump-app/cc13x4_26x4/main/AppTask.cpp | 3 +- .../cc13x4_26x4/main/AppTask.cpp | 3 +- src/lwip/cc13xx_26xx/lwipopts.h | 2 +- src/platform/cc13xx_26xx/TI_heap_wrapper.c | 75 ++++ src/platform/cc13xx_26xx/TI_heap_wrapper.h | 10 + src/platform/cc13xx_26xx/memory.c | 398 ++++++++++++++++++ .../ti_simplelink_sdk/ti_simplelink_sdk.gni | 4 +- 13 files changed, 504 insertions(+), 14 deletions(-) create mode 100644 src/platform/cc13xx_26xx/TI_heap_wrapper.c create mode 100644 src/platform/cc13xx_26xx/TI_heap_wrapper.h create mode 100644 src/platform/cc13xx_26xx/memory.c diff --git a/examples/all-clusters-app/cc13x4_26x4/main/AppTask.cpp b/examples/all-clusters-app/cc13x4_26x4/main/AppTask.cpp index 3e33847483..6fc9642b8c 100644 --- a/examples/all-clusters-app/cc13x4_26x4/main/AppTask.cpp +++ b/examples/all-clusters-app/cc13x4_26x4/main/AppTask.cpp @@ -54,12 +54,6 @@ #define APP_TASK_STACK_SIZE (5000) #define APP_TASK_PRIORITY 4 #define APP_EVENT_QUEUE_SIZE 10 - -#if (CHIP_CONFIG_ENABLE_ICD_SERVER == 1) -#define LED_ENABLE 0 -#else -#define LED_ENABLE 1 -#endif #define BUTTON_ENABLE 1 using namespace ::chip; @@ -68,7 +62,6 @@ using namespace ::chip::DeviceLayer; static TaskHandle_t sAppTaskHandle; static QueueHandle_t sAppEventQueue; - static Button_Handle sAppLeftHandle; static Button_Handle sAppRightHandle; static DeviceInfoProviderImpl sExampleDeviceInfoProvider; diff --git a/examples/all-clusters-app/cc13x4_26x4/main/Globals.cpp b/examples/all-clusters-app/cc13x4_26x4/main/Globals.cpp index 01a945e1a3..00fd64e312 100644 --- a/examples/all-clusters-app/cc13x4_26x4/main/Globals.cpp +++ b/examples/all-clusters-app/cc13x4_26x4/main/Globals.cpp @@ -18,5 +18,7 @@ #include "Globals.h" +#if (LED_ENABLE == 1) LED_Handle sAppRedHandle; LED_Handle sAppGreenHandle; +#endif diff --git a/examples/all-clusters-app/cc13x4_26x4/main/include/Globals.h b/examples/all-clusters-app/cc13x4_26x4/main/include/Globals.h index 4cdc0b6917..8320b01ac7 100644 --- a/examples/all-clusters-app/cc13x4_26x4/main/include/Globals.h +++ b/examples/all-clusters-app/cc13x4_26x4/main/include/Globals.h @@ -31,5 +31,12 @@ void cc13xx_26xxLog(const char * aFormat, ...); #ifdef __cplusplus } #endif + +#if (CHIP_CONFIG_ENABLE_ICD_SERVER == 1) +#define LED_ENABLE 0 +#else +#define LED_ENABLE 1 +#endif + extern LED_Handle sAppRedHandle; extern LED_Handle sAppGreenHandle; diff --git a/examples/lighting-app/cc13x4_26x4/src/AppTask.cpp b/examples/lighting-app/cc13x4_26x4/src/AppTask.cpp index bad302bb6b..5b5bb7d7df 100644 --- a/examples/lighting-app/cc13x4_26x4/src/AppTask.cpp +++ b/examples/lighting-app/cc13x4_26x4/src/AppTask.cpp @@ -81,9 +81,10 @@ using namespace ::chip::DeviceLayer; static TaskHandle_t sAppTaskHandle; static QueueHandle_t sAppEventQueue; - +#if (LED_ENABLE == 1) static LED_Handle sAppRedHandle; static LED_Handle sAppGreenHandle; +#endif static Button_Handle sAppLeftHandle; static Button_Handle sAppRightHandle; static DeviceInfoProviderImpl sExampleDeviceInfoProvider; diff --git a/examples/lock-app/cc13x4_26x4/src/AppTask.cpp b/examples/lock-app/cc13x4_26x4/src/AppTask.cpp index ec6a1d048c..2a202ce432 100644 --- a/examples/lock-app/cc13x4_26x4/src/AppTask.cpp +++ b/examples/lock-app/cc13x4_26x4/src/AppTask.cpp @@ -72,9 +72,10 @@ using namespace ::chip::app::Clusters::DoorLock; static TaskHandle_t sAppTaskHandle; static QueueHandle_t sAppEventQueue; - +#if (LED_ENABLE == 1) static LED_Handle sAppRedHandle; static LED_Handle sAppGreenHandle; +#endif static Button_Handle sAppLeftHandle; static Button_Handle sAppRightHandle; diff --git a/examples/platform/cc13x4_26x4/project_include/OpenThreadConfig.h b/examples/platform/cc13x4_26x4/project_include/OpenThreadConfig.h index 795d77b646..c4087578b9 100644 --- a/examples/platform/cc13x4_26x4/project_include/OpenThreadConfig.h +++ b/examples/platform/cc13x4_26x4/project_include/OpenThreadConfig.h @@ -23,6 +23,7 @@ #pragma once +#define OPENTHREAD_CONFIG_NUM_MESSAGE_BUFFERS 22 #define OPENTHREAD_CONFIG_DHCP6_SERVER_ENABLE 0 #define OPENTHREAD_CONFIG_BORDER_ROUTER_ENABLE 0 #define OPENTHREAD_CONFIG_DIAG_ENABLE 0 diff --git a/examples/pump-app/cc13x4_26x4/main/AppTask.cpp b/examples/pump-app/cc13x4_26x4/main/AppTask.cpp index 4450b09bd9..0aaa1c6b40 100644 --- a/examples/pump-app/cc13x4_26x4/main/AppTask.cpp +++ b/examples/pump-app/cc13x4_26x4/main/AppTask.cpp @@ -79,9 +79,10 @@ using namespace chip::app::Clusters; static TaskHandle_t sAppTaskHandle; static QueueHandle_t sAppEventQueue; - +#if (LED_ENABLE == 1) static LED_Handle sAppRedHandle; static LED_Handle sAppGreenHandle; +#endif static Button_Handle sAppLeftHandle; static Button_Handle sAppRightHandle; diff --git a/examples/pump-controller-app/cc13x4_26x4/main/AppTask.cpp b/examples/pump-controller-app/cc13x4_26x4/main/AppTask.cpp index 6c84954934..fd44a3df45 100644 --- a/examples/pump-controller-app/cc13x4_26x4/main/AppTask.cpp +++ b/examples/pump-controller-app/cc13x4_26x4/main/AppTask.cpp @@ -68,9 +68,10 @@ using namespace ::chip::DeviceLayer; static TaskHandle_t sAppTaskHandle; static QueueHandle_t sAppEventQueue; - +#if (LED_ENABLE == 1) static LED_Handle sAppRedHandle; static LED_Handle sAppGreenHandle; +#endif static Button_Handle sAppLeftHandle; static Button_Handle sAppRightHandle; diff --git a/src/lwip/cc13xx_26xx/lwipopts.h b/src/lwip/cc13xx_26xx/lwipopts.h index 925a06d261..d89c33afb5 100644 --- a/src/lwip/cc13xx_26xx/lwipopts.h +++ b/src/lwip/cc13xx_26xx/lwipopts.h @@ -89,7 +89,7 @@ #define MEMP_SEPARATE_POOLS (1) #define LWIP_PBUF_FROM_CUSTOM_POOLS (0) #define MEMP_USE_CUSTOM_POOLS (0) -#define PBUF_POOL_SIZE (6) +#define PBUF_POOL_SIZE (12) #define PBUF_POOL_BUFSIZE (1280) #define PBUF_CUSTOM_POOL_IDX_START (MEMP_PBUF_POOL_SMALL) #define PBUF_CUSTOM_POOL_IDX_END (MEMP_PBUF_POOL_LARGE) diff --git a/src/platform/cc13xx_26xx/TI_heap_wrapper.c b/src/platform/cc13xx_26xx/TI_heap_wrapper.c new file mode 100644 index 0000000000..a749884660 --- /dev/null +++ b/src/platform/cc13xx_26xx/TI_heap_wrapper.c @@ -0,0 +1,75 @@ +#include "bget.h" +#include +#include + +typedef unsigned int dpl_CSState; + +typedef union _dpl_cs_state_union_t +{ + /** critical section variable as declared in the interface */ + dpl_CSState state; + /** @internal field used to access internal data */ + struct _dpl_cs_state_aggr_t + { + /** field to store Swi_disable() return value */ + uint_least16_t swikey; + /** field to store Hwi_disable() return value */ + uint_least16_t hwikey; + } each; +} dpl_CSStateUnion; + +/* This is enter critical section for DPL supported devices */ +dpl_CSState dpl_enterCSImpl(void) +{ + + dpl_CSStateUnion cu; + cu.each.swikey = (uint_least16_t) SwiP_disable(); + cu.each.hwikey = (uint_least16_t) HwiP_disable(); + return cu.state; +} + +/* This is exit critical section for DPL supported devices */ +void dpl_leaveCSImpl(dpl_CSState key) +{ + dpl_CSStateUnion * cu = (dpl_CSStateUnion *) &key; + HwiP_restore((uint32_t) cu->each.hwikey); + SwiP_restore((uint32_t) cu->each.swikey); +} + +/* Protected allocation */ +void * pvPortMalloc(size_t xWantedSize) +{ + void * retVal = NULL; + + dpl_CSState state; + state = dpl_enterCSImpl(); + + retVal = bget(xWantedSize); + + dpl_leaveCSImpl(state); + return retVal; +} + +/* Protected Deallocation */ +void vPortFree(void * pv) +{ + dpl_CSState state; + state = dpl_enterCSImpl(); + + brel(pv); + + dpl_leaveCSImpl(state); +} + +void * pvPortRealloc(void * pv, size_t size) +{ + void * retVal = NULL; + + dpl_CSState state; + state = dpl_enterCSImpl(); + + retVal = bgetr(pv, size); + + dpl_leaveCSImpl(state); + return retVal; +} diff --git a/src/platform/cc13xx_26xx/TI_heap_wrapper.h b/src/platform/cc13xx_26xx/TI_heap_wrapper.h new file mode 100644 index 0000000000..11df56fddb --- /dev/null +++ b/src/platform/cc13xx_26xx/TI_heap_wrapper.h @@ -0,0 +1,10 @@ +/* Protected allocation + malloc/ICall_heapMalloc --> ti_heap_wrapper --> bget protected by critical section +*/ +void * pvPortMalloc(size_t xWantedSize); + +/* Protected Deallocation + Free/ICall_heapFree --> ti_heap_wrapper --> brel protected by critical section + */ +void vPortFree(void * pv); +void * pvPortRealloc(void * pv, size_t size); diff --git a/src/platform/cc13xx_26xx/memory.c b/src/platform/cc13xx_26xx/memory.c new file mode 100644 index 0000000000..2bd85363e7 --- /dev/null +++ b/src/platform/cc13xx_26xx/memory.c @@ -0,0 +1,398 @@ +/* + * Copyright (c) 2016-2022 Texas Instruments Incorporated - http://www.ti.com + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * * Neither the name of Texas Instruments Incorporated nor the names of + * its contributors may be used to endorse or promote products derived + * from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, + * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + */ + +/* + * ======== memory.c ======== + */ + +#if defined(__ti__) && !defined(__clang__) + +#pragma FUNC_EXT_CALLED(malloc); +#pragma FUNC_EXT_CALLED(memalign); +#pragma FUNC_EXT_CALLED(free); +#pragma FUNC_EXT_CALLED(calloc); +#pragma FUNC_EXT_CALLED(realloc); +#pragma FUNC_EXT_CALLED(aligned_alloc); + +#define ATTRIBUTE + +#elif defined(__IAR_SYSTEMS_ICC__) + +#define ATTRIBUTE + +#else + +#define ATTRIBUTE __attribute__((used)) + +#endif + +#include +#include +#include +#include + +#include +#include + +#if defined(__GNUC__) && !defined(__ti__) + +#include + +#endif + +extern void * pvPortRealloc(void * pv, size_t size); + +/* + * This Header is only needed to support advanced memory services - namely + * realloc() and memalign(). If the user doesn't require those features, they + * can remove the overhead and save both code and data. + */ +#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV) +/* + * Header is a union to make sure that the size is a power of 2. + * + * On the MSP430 small model (MSP430X), size_t is 2 bytes, which makes + * the size of this struct 6 bytes. + */ +typedef union Header +{ + struct + { + void * actualBuf; + size_t size; + } header; + int pad[2]; /* 4 words on 28L, 8 bytes on most others */ +} Header; +#endif + +/* + * ======== malloc ======== + */ +void ATTRIBUTE * malloc(size_t size) +{ +#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV) + Header * packet; + size_t allocSize; + + allocSize = size + sizeof(Header); + + /* + * If size is very large and allocSize overflows, the result will be + * smaller than size. In this case, don't try to allocate. + */ + if ((size == 0) || (allocSize < size)) + { + errno = EINVAL; + return (NULL); + } + + packet = (Header *) pvPortMalloc(allocSize); + + if (packet == NULL) + { + errno = ENOMEM; + return (NULL); + } + + packet->header.actualBuf = (void *) packet; + packet->header.size = allocSize; + + return (packet + 1); +#else + void * packet; + + if (size == 0) + { + errno = EINVAL; + return (NULL); + } + + packet = pvPortMalloc(size); + + if (packet == NULL) + { + errno = ENOMEM; + return (NULL); + } + + return (packet); +#endif +} + +/* + * ======== calloc ======== + */ +void ATTRIBUTE * calloc(size_t nmemb, size_t size) +{ + size_t nbytes; + void * retval; + + /* guard against divide by zero exception below */ + if (nmemb == 0) + { + errno = EINVAL; + return (NULL); + } + + nbytes = nmemb * size; + + /* return NULL if there's an overflow */ + if (nmemb && size != (nbytes / nmemb)) + { + errno = EOVERFLOW; + return (NULL); + } + + retval = malloc(nbytes); + if (retval != NULL) + { + (void) memset(retval, (int) '\0', nbytes); + } + + return (retval); +} +#ifndef DeviceFamily_CC3220 +/* + * ======== realloc ======== + */ +void ATTRIBUTE * realloc(void * ptr, size_t size) +{ +#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV) + void * retval; + Header * packet; + size_t oldSize; + + if (ptr == NULL) + { + retval = malloc(size); + } + else if (size == 0) + { + errno = EINVAL; + retval = NULL; + } + else + { + packet = (Header *) ptr - 1; + retval = malloc(size); + if (retval != NULL) + { + oldSize = packet->header.size - sizeof(Header); + (void) memcpy(retval, ptr, (size < oldSize) ? size : oldSize); + free(ptr); + } + } + + return (retval); +#else + void * packet; + + if (size == 0) + { + errno = EINVAL; + return (NULL); + } + + packet = pvPortRealloc(ptr, size); + + if (packet == NULL) + { + errno = ENOMEM; + return (NULL); + } + + return (packet); +#endif +} + +#else +/* + * ======== realloc ======== + */ +void ATTRIBUTE * realloc(void * ptr, size_t size) +{ +#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV) + void * retval; + Header * packet; + size_t oldSize; + + if (ptr == NULL) + { + retval = malloc(size); + } + else if (size == 0) + { + errno = EINVAL; + retval = NULL; + } + else + { + packet = (Header *) ptr - 1; + retval = malloc(size); + if (retval != NULL) + { + oldSize = packet->header.size - sizeof(Header); + (void) memcpy(retval, ptr, (size < oldSize) ? size : oldSize); + free(ptr); + } + } + + return (retval); +#else + /* Unsupported implementation */ + return (NULL); +#endif +} +#endif + +/* + * ======== free ======== + */ +void ATTRIBUTE free(void * ptr) +{ +#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV) + Header * packet; + + if (ptr != NULL) + { + packet = ((Header *) ptr) - 1; + vPortFree(packet->header.actualBuf); + } +#else + vPortFree(ptr); +#endif +} + +/* + * ======== memalign ======== + */ +void ATTRIBUTE * memalign(size_t boundary, size_t size) +{ +#if defined(TI_POSIX_FREERTOS_MEMORY_ENABLEADV) + Header * packet; + void * tmp; + + /* return NULL if size is 0, or alignment is not a power-of-2 */ + if (size == 0 || (boundary & (boundary - 1))) + { + errno = EINVAL; + return (NULL); + } + + if (boundary < sizeof(Header)) + { + boundary = sizeof(Header); + } + + /* + * Allocate 'align + size + sizeof(Header)' so that we have room for + * the 'packet' and can return an aligned buffer. + */ + tmp = pvPortMalloc(boundary + size + sizeof(Header)); + + if (tmp == NULL) + { + errno = ENOMEM; + return (NULL); + } + + if ((unsigned int) tmp & (boundary - 1)) + { + /* tmp is not already aligned */ + packet = (Header *) (((unsigned int) tmp + boundary) & ~(boundary - 1)) - 1; + if (packet < (Header *) tmp) + { + /* don't have room for Header before aligned address */ + packet = (Header *) ((unsigned int) packet + boundary); + } + } + else + { + /* tmp is already aligned to boundary (by chance) */ + packet = ((Header *) (((unsigned int) tmp + boundary))) - 1; + } + + packet->header.actualBuf = tmp; + packet->header.size = size + sizeof(Header); + + return (packet + 1); +#else + /* user called an unsupported function */ + return (NULL); +#endif +} + +/* + * ======== aligned_alloc ======== + */ +void ATTRIBUTE * aligned_alloc(size_t alignment, size_t size) +{ + /* use existing implementation to allocate buffer */ + return (memalign(alignment, size)); +} + +#if defined(__GNUC__) && !defined(__ti__) + +/* + * ======== _malloc_r ======== + */ +void ATTRIBUTE * _malloc_r(struct _reent * rptr, size_t size) +{ + return malloc(size); +} + +/* + * ======== _calloc_r ======== + */ +void ATTRIBUTE * _calloc_r(struct _reent * rptr, size_t nmemb, size_t size) +{ + return calloc(nmemb, size); +} + +/* + * ======== _free_r ======== + */ +void ATTRIBUTE _free_r(struct _reent * rptr, void * ptr) +{ + free(ptr); +} + +/* + * ======== _realloc_r ======== + */ +void ATTRIBUTE * _realloc_r(struct _reent * rptr, void * ptr, size_t size) +{ + return realloc(ptr, size); +} + +#endif diff --git a/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni b/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni index 0698af1653..af3e810d67 100644 --- a/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni +++ b/third_party/ti_simplelink_sdk/ti_simplelink_sdk.gni @@ -305,8 +305,8 @@ template("ti_simplelink_sdk") { configs -= [ "${build_root}/config/compiler:std_default" ] configs += [ ":${sdk_target_name}_posix_config" ] sources = [ + "${chip_root}/src/platform/cc13xx_26xx/memory.c", "${ti_simplelink_sdk_root}/source/ti/posix/freertos/clock.c", - "${ti_simplelink_sdk_root}/source/ti/posix/freertos/memory.c", "${ti_simplelink_sdk_root}/source/ti/posix/freertos/mqueue.c", "${ti_simplelink_sdk_root}/source/ti/posix/freertos/pthread.c", "${ti_simplelink_sdk_root}/source/ti/posix/freertos/pthread_barrier.c", @@ -521,8 +521,8 @@ template("ti_simplelink_sdk") { ] sources = [ + "${chip_root}/src/platform/cc13xx_26xx/TI_heap_wrapper.c", "${chip_root}/src/platform/cc13xx_26xx/chipOBleProfile.c", - "${ti_simplelink_sdk_root}/source/ti/ble5stack/common/cc26xx/freertos/TI_heap_wrapper.c", "${ti_simplelink_sdk_root}/source/ti/ble5stack/common/cc26xx/freertos/bget.c", "${ti_simplelink_sdk_root}/source/ti/ble5stack/common/cc26xx/rcosc/rcosc_calibration.c", "${ti_simplelink_sdk_root}/source/ti/dmm/apps/common/ble_remote_display/stack/osal_icall_ble.c",