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.

[参考译文] CC2642R:如何使用 noinit 在 CC2642中定义变量

Guru**** 2591290 points


请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/wireless-connectivity/bluetooth-group/bluetooth/f/bluetooth-forum/1367952/cc2642r-how-to-use-noinit-to-define-variables-in-cc2642

器件型号:CC2642R

工具与软件:

您好!

C2642芯片、我使用 IAR 环境并使用 noinit 定义变量。 在软件复位之前、我为 userResetReason 分配一个值1234。 为什么该变量在复位后仍然为0。 我经常尝试以下三种方法、但都不起作用。

//uint32_t userResetReason _attribute__((section (".noinit"));  

//__no_init uint32_t userResetReason; 
uint32_t userResetReason __attribute__(retain、noinit); 



IAR 9.32.1
SDK:simplelink_cc13xx_cc26xx_sdk_7_10_01_24
  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    属性"retain"意味着该变量应包含在最终编译中。 这可以防止编译器在任何代码未引用变量的情况下删除该变量。

    属性"noinit"意味着编译器在程序启动时不会初始化该变量。 因此、它可能包含未知值、直到由程序分配它。

    (顺便说一下,这里是一个很好的详细说明属性的资源: https://software-dl.ti.com/codegen/docs/tiarmclang/rel2_1_0_LTS/migration_guide/migrating_c_and_cpp_source/pragmas_and_attributes.html )

    似乎您甚至在器件复位之后也想要保存值。
    你在这里拥有的变量 userResetReason 很可能放置在 RAM 中、因此其值在复位后将不会保留。
    若要在复位后保持不变、必须将变量放置在闪存中。 如需相关参考、请尝试编译 nvsinternal 示例: https://dev.ti.com/tirex/explore/node?node=A__ANuUJDdHffNrvt7.ckoWLg__com.ti.SIMPLELINK_CC13XX_CC26XX_SDK__BSEc4rl__LATEST 

    此示例 展示了在器件内部闪存中分配非易失性存储的概念、包括初始化和对其进行读取/写入。
    同样的概念也适用于您的情况。
    在技术上、"读取"可以与正常 c 一样发生、例如 uint32_t ramVar_u32 = flashVar_u32。
    只能使用闪存 API (flash.h -> FlashProgram)进行"写入"。

    谢谢。
    托比

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    如果可以的话、我不想将闪存放入闪存中进行存储、因为闪存会频繁运行。 我看到这些文档使用了__attribute__(noinit))、按以下方式定义;通过软件复位后、其值仍然为0。 对于 IAR 进行配置是否需要执行其他操作?

    __attribute__(noinit) uint32_t userResetReason
    UserResetReason = 1234
    SystemReset();

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    我现在看到这个文档是针对 ticlang 编译器的、它在 IAR 中可能看起来有所不同。

    从 IAR 开发指南(wwwfiles.iar.com/.../EWARM_DevelopmentGuide.ENU.pdf)中、请见"将变量放置在指定段内的示例"。

    它还提供了其他变量放置方式、例如:

    __no_init int alpha @ "MY_NOINIT"; /* OK */

    #pragma location="MY_CONSTANTS"
    const int beta = 42; /* OK */
    

    const int gamma @ "MY_CONSTANTS" = 17; /* OK */
    int theta @ "MY_ZEROS"; /* OK */
    int phi @ "MY_INITED" = 4711; /* OK */

    然后在链接器文件(.icf)中、放置该变量的相关段必须具有"不初始化"指令。
    例如、空示例的 cc13x2_cc26x2_freertos.isf (C:\ti/simplelink_cc13xx_cc26xx_sdk_7_40_00_77\examples\rtos\CC26X2R1_LAUNCHXL\drivers\empty\freertos\IAR)就属于这种情况。
    它具有:  不初始化{section .stack、section .noinit};

    其他示例包括(来自 IAR 开发指南):

    __no_init volatile char alpha @ 0xFF2000;/* OK */

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

    我使用了工程 C:\ti/simplelink_cc13xx_cc26xx_sdk_7_40_00_77\examples\rtos\CC26X2R1_LAUNCHXL\drivers\empty 并按如下所示修改了文件、在调用软件复位后、alpha 仍为0。

    /*
     * Copyright (c) 2015-2019, Texas Instruments Incorporated
     * 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.
     */
    
    /*
     *  ======== empty.c ========
     */
    
    /* For usleep() */
    #include <unistd.h>
    #include <stdint.h>
    #include <stddef.h>
    
    /* Driver Header files */
    #include <ti/drivers/GPIO.h>
    // #include <ti/drivers/I2C.h>
    // #include <ti/drivers/SPI.h>
    // #include <ti/drivers/Watchdog.h>
    
    /* Driver configuration */
    
    #include "ti_drivers_config.h"
    #include <ti/display/Display.h>
    
    #include <ti/devices/DeviceFamily.h>
    #include DeviceFamily_constructPath(inc/hw_ccfg.h)
    #include DeviceFamily_constructPath(inc/hw_ccfg_simple_struct.h)
    #include DeviceFamily_constructPath(driverlib/sys_ctrl.h)
    
    Display_Handle dispHandle = NULL;
    
    
    //__no_init int alpha @ "MY_NOINIT"; 
    __no_init volatile char alpha;/* OK */
    //uint32_t alpha __attribute__ ((section (".noinit")));
    
    
    
    /*
     *  ======== mainThread ========
     */
    void *mainThread(void *arg0)
    {
      
        dispHandle = Display_open(Display_Type_ANY, NULL);
        Display_printf(dispHandle, 0, 0, "====================:%d",alpha);
      
        /* 1 second delay */
        uint32_t time = 1;
    
        /* Call driver init functions */
        GPIO_init();
        // I2C_init();
        // SPI_init();
        // Watchdog_init();
    
        /* Configure the LED pin */
        GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW);
    
        /* Turn on user LED */
        GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON);
    
        while (1)
        {
            sleep(time);
            GPIO_toggle(CONFIG_GPIO_LED_0);
            
            alpha = 123;
            SysCtrlSystemReset();
        }
    }
    

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    似乎您声明正确、但仍然看到它初始化为0。。。我建议与您最近的代码片段联系 IAR、了解相关信息。

    Unknown 说:
    在软件重置之前,我为 userResetReason 分配了一个1234值。 为什么该变量在复位后仍然为0。 我经常尝试以下三种方法,但它们都不起作用。

    回到您最初的问题、userResetReason 被放置在 RAM 中、因此如果复位后在复位前设置的任何值都将不会保留。
    在您的示例中、您有以下序列:

    1. UserResetReason = 1234;
    2. 复位
    3. temp = userResetReason

    在步骤3中、temp 不具有值1234。