This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2640: Optimize Stack utilization for reducing the flash size consumption

Expert 1760 points
Part Number: CC2640


Tool/software:

Hi,

I'm using sdk version 2.2.8 for implementing secure channel in CC2640 taking reference project as simpleperipheral.

After enabling secure channel feature in build_config.opt (-DBLE_V42_FEATURES=SECURE_CONNS_CFG) and (-DGAP_BOND_MGR), I could observe the flash space consumption increased to 62970 bytes.

And also the simple_peripheral example application consume 40308 bytes.

On top of which we have the bootloader project which consumes almost 5kB of flash. 

And overall it seems consume 110kB of flash size out of available total 128kB of flash, which is almost 85%.

This consumption is without adding the application overhead (respective to our customized requirements). 

Is there any possibility of reducing or optimizing the memory utilized by stack or from the simple_peripheral example application.??

Thank you/

  • Hi, 

    Thank you for reaching out. 

    May I kindly ask how much Flash you would like to get freed up?

    I would suggest looking into the following document to see if some Flash savings can be identified: https://software-dl.ti.com/simplelink/esd/simplelink_cc2640r2_sdk/5.30.01.11/exports/docs/blestack/ble_user_guide/html/ble-stack-3.x/creating-a-custom-bluetooth-low-energy-application.html#optimizing-bluetooth-low-energy-flash-and-ram-memory-usage (it is not directly applicable to your case, but should give you some leads).

    Please let us know your progress.

    Best regards, 

  • Hi Clement,

    I would like to free around 5kB - 10kB if possible, so my application can fit properly with keeping a remaining buffer space of 10kB(90% consumption only after adding the full application code).

    I have tried all of the steps in above mentioned link.

    For our application we need to use Secure channel, OAD library (which includes Bootloader as well). 

    After adding the OAD library the total consumption of flash including Flash+App=113,994 bytes (this is without any customization). 

    And only 10kB is remaining to include the application which may not fit. 

  • Hi, 

    Thank you for telling me. 

    How much flash did you manage to save following the link I have shared? 

    Next step to identify savings in the files provided in the SDK. By default these files are meant to address a wide variety of use case, and you could get rid a unused code. 

    1. Remove the fxnTable of the drivers using one (applicable to I2C, SPI, UART, Watchdog - not applicable to I2S, PDM, PIN)
    Here is how to do this for one specific function of the I2C driver (the same is applicable for other drivers and other functions)

    • In I2C.c
      • Add #include <ti/drivers/i2c/I2CCC26XX.h>
      • Replace handle->fxnTablePtr->cancelFxn(handle); by I2CCC26XX_cancel(handle);
    • In I2CCC26XX.c
      • In the I2CCC26XX_fxnTable replace I2CCC26XX_cancel by NULL
      • If all the functions in the fxnTable are removed then the the full fxnTable can be set to NULL
    • In I2CCC26XX.h
      • Add extern void I2CCC26XX_cancel(I2C_Handle handle)

    => This will help assuming some driver functions are unused in your application 

    => For this to work, the application should be built using the source files of the driver (not the pre-compiled library). Ensure the source files of the driver you optimize are pulled in your project.

    2. Identify some features of the drivers that are not used. Good candidates could be drivers that are providing both blocking and callback mode (in general only one of the two modes is used - this is the case of UART, I2C, SPI drivers). Other possibilities for optimization are in drivers providing several sampling frequencies (such as I2C and PDM) or configurations (SPI).
    Sometimes, removing these features may lead to some RAM usage reduction by removing form the object some used elements (e.g. semaphores used in blocking mode)

    => Same remarks as before

    3. Optimize the Power driver by removing the unused modes

    4. PIN driver: remove the initialization for non-CC2640R2 devices

    5. Don't link the crypto drivers mode not required by the stack.

    • AES CBC => can be removed
    • AES CTR => can be removed
    • AES-ECB => can be  removed if the device is never paired and RPA is not used
    • ECDH => can be removed assuming only Legacy pairing is used

    6. The RF driver can be optimized. In the BLE3 example, RF single is used. RFCC26XX_singleMode.c provides code that is not used when in BLE mode only. 

    => Here a file I have modified, you may want to look into it RFCC26XX_singleMode_modified_not_tested.c

    7. Switch to HeapMem (instead of OSAL HeapMgr)
    In the applications's cfg file that changes HEAPMGR_CONFIG to 0x81

    8. Clean up devinfoservice.c  => verify if the functions DevInfo_GetParameter() and DevInfo_SetParameter() are called with different arguments (or if only one argument is used)

    9. If not used, make sure GATT_CLIENT is fully disabled.

    10. Remove unused branches in all the "switch" you can find

    I hope this will help,

    Best regards, 

  • Hi Clement, 

    Thanks for the cherry picking and giving suggestions.

    Tried the above steps, some of these steps we have already in place. We could only save 1kB additional.

    Would you please clarify the below points:

    - point 6, you are referring to the RFCC26XX_singleMode.c file in CC2640 or CC2640R2 sdk. Because I see a huge difference when I compare the file you have provided and one without modification.

    - point 7, do you mean to make changes in app_ble.cfg which inturn calls the cc2640.cfg file. But I don't see a mention of HEAPMGR_CONFIG in this file. 

    Additionally, regarding to RAM:

    - Can we utilize the Cache 8kB available to reduce the Flash or RAM consumption also ?

  • Hi, 

    - point 6, you are referring to the RFCC26XX_singleMode.c file in CC2640 or CC2640R2 sdk. Because I see a huge difference when I compare the file you have provided and one without modification.

    - point 7, do you mean to make changes in app_ble.cfg which inturn calls the cc2640.cfg file. But I don't see a mention of HEAPMGR_CONFIG in this file. 

    These two points were actually applicable only on CC2640R2 sorry for the confusion. 

    - Can we utilize the Cache 8kB available to reduce the Flash or RAM consumption also ?

    The Cache can be used as RAM - offering an extra 8kB of RAM. 
    The impact on the total RAM and Flash usages is expected to be limited. 

    To add on my previous suggestions, you can also look into changing the compiler settings to see if further optimizations are possible. 

    Best regards,