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.

CC2538: NV read and write

Part Number: CC2538
Other Parts Discussed in Thread: Z-STACK

uint8 osal_nv_read( uint16 id, uint16 offset, uint16 len, void *buf );

uint8 osal_nv_write( uint16 id, uint16 offset, uint16 len, void *buf );

In this, I want to write and read the whole string in some buffer. What should be the value of offset to be set in both? 

  • Just set it to 0.
  • Write operations-
    x1=osal_nv_item_init(SSID_KEY,11, ssid1);
    x2=osal_nv_item_init(PSWRD_KEY, 9 , pswrd1);

    x3=osal_nv_write(SSID_KEY, 0,11, (void *) ssid1);
    x4=osal_nv_write(PSWRD_KEY, 0, 9, (void *)pswrd1);

    I am getting x1=x2=x3=00 which means success but x4=0A which means failed.
    Now read operation-
    char * ssid2, pswrd2;
    y1=osal_nv_item_init(SSID_KEY, 11, ssid1);
    y2=osal_nv_item_init(PSWRD_KEY, 9 , pswrd1);

    y3=osal_nv_read(SSID_KEY, 0, 11 , (void *)ssid2);
    y4=osal_nv_read(PSWRD_KEY, 0, 9 , (void *)pswrd2);
    y1=y2=y3=y4=0x00;
    I am getting ssid2=0x0; and pswrd2=0x0;
    Can you explain this
  • Do you allocate memory for ssid1, ssid2, pswrd1, and pswrd2?
  • sorry I did not aloocate memory for ssid2 and pswrd2. But now allocated and got the value of ssid2 only. In pswrd2, it is someything garbage.
    As x4=0x0A; is still. Why x4=0x0A?
  • What is your PSWRD_KEY?
  • #define SSID_KEY 0x0403
    #define PSWRD_KEY 0x0404
  • Should I change PSWRD_KEY ?
  • The define is OK. Can you post your complete code?
  • All others things are fine. Code is so vast.
  • Make sure the memory allocation is good.
  • x1=osal_nv_item_init(SSID_KEY,11, ssid1);
    x2=osal_nv_item_init(PSWRD_KEY, 9 , pswrd1);

    x3=osal_nv_write(SSID_KEY, 0,11, (void *) ssid1);
    x4=osal_nv_write(PSWRD_KEY, 0, 9, (void *)pswrd1);
    See in this, ssid1 and pswrd1are allocated perfectly and I can see their contents. These are right. Only problem I am getting the x4. x3 is right and all the things are same except size and pointer, Then why this returns failed?
  • Where do you put those code in your application?
  • I test it with the following red codes in SampleLight and everything works fine.

    #define SSID_KEY 0x0403
    #define PSWRD_KEY 0x0404
    uint8 x1,x2,x3,x4,y1,y2,y3,y4;
    uint8 ssid1[11]={0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x78,0x78};
    uint8 pswrd1[9]={0x87,0x87,0x87,0x87,0x87,0x87,0x87,0x87,0x87};
    uint8 ssid2[11]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
    uint8 pswrd2[9]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

    void zclSampleLight_Init( byte task_id )
    {
      zclSampleLight_TaskID = task_id;

      // Set destination address to indirect
      zclSampleLight_DstAddr.addrMode = (afAddrMode_t)AddrNotPresent;
      zclSampleLight_DstAddr.endPoint = 0;
      zclSampleLight_DstAddr.addr.shortAddr = 0;

      // This app is part of the Home Automation Profile
      zclHA_Init( &zclSampleLight_SimpleDesc );

      // Register the ZCL General Cluster Library callback functions
      zclGeneral_RegisterCmdCallbacks( SAMPLELIGHT_ENDPOINT, &zclSampleLight_CmdCallbacks );

      // Register the application's attribute list
      zcl_registerAttrList( SAMPLELIGHT_ENDPOINT, zclSampleLight_NumAttributes, zclSampleLight_Attrs );

      // Register the Application to receive the unprocessed Foundation command/response messages
      zcl_registerForMsg( zclSampleLight_TaskID );

    #ifdef ZCL_DISCOVER
      // Register the application's command list
      zcl_registerCmdList( SAMPLELIGHT_ENDPOINT, zclCmdsArraySize, zclSampleLight_Cmds );
    #endif

      // Register for all key events - This app will handle all key events
      RegisterForKeys( zclSampleLight_TaskID );

      // Register for a test endpoint
      afRegister( &sampleLight_TestEp );

    #ifdef ZCL_EZMODE
      // Register EZ-Mode
      zcl_RegisterEZMode( &zclSampleLight_RegisterEZModeData );

      // Register with the ZDO to receive Match Descriptor Responses
      ZDO_RegisterForZDOMsg(task_id, Match_Desc_rsp);
    #endif


    #if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)
      HalTimer1Init( 0 );
      halTimer1SetChannelDuty( WHITE_LED, 0 );
      halTimer1SetChannelDuty( RED_LED, 0 );
      halTimer1SetChannelDuty( BLUE_LED, 0 );
      halTimer1SetChannelDuty( GREEN_LED, 0 );

      // find if we are already on a network from NV_RESTORE
      uint8 state;
      NLME_GetRequest( nwkNwkState, 0, &state );

      if ( state < NWK_ENDDEVICE )
      {
        // Start EZMode on Start up to avoid button press
        osal_start_timerEx( zclSampleLight_TaskID, SAMPLELIGHT_START_EZMODE_EVT, 500 );
      }
    #if ZCL_LEVEL_CTRL
      zclSampleLight_DefaultMove();
    #endif
    #endif // #if (defined HAL_BOARD_ZLIGHT) || (defined HAL_PWM)

    #ifdef ZCL_DIAGNOSTIC
      // Register the application's callback function to read/write attribute data.
      // This is only required when the attribute data format is unknown to ZCL.
      zcl_registerReadWriteCB( SAMPLELIGHT_ENDPOINT, zclDiagnostic_ReadWriteAttrCB, NULL );

      if ( zclDiagnostic_InitStats() == ZSuccess )
      {
        // Here the user could start the timer to save Diagnostics to NV
      }
    #endif

    #ifdef LCD_SUPPORTED
      HalLcdWriteString ( (char *)sDeviceName, HAL_LCD_LINE_3 );
    #endif  // LCD_SUPPORTED

    #ifdef ZGP_AUTO_TT
      zgpTranslationTable_RegisterEP ( &zclSampleLight_SimpleDesc );
    #endif
      {
        x1=osal_nv_item_init(SSID_KEY,11, ssid1);
        x2=osal_nv_item_init(PSWRD_KEY, 9 , pswrd1);

        x3=osal_nv_write(SSID_KEY, 0,11, (void *)ssid1);
        x4=osal_nv_write(PSWRD_KEY, 0, 9, (void *)pswrd1);
        y3=osal_nv_read(SSID_KEY, 0, 11 , (void *)ssid2);
        y4=osal_nv_read(PSWRD_KEY, 0, 9 , (void *)pswrd2);
        y1=y2=y3=y4=0x00;

      }
    }

  • Actually I am using sample switch, cc2538, as a coordinator. I have written these lines in WIFI_UART.c file, new file created.
    I can read successfully from NV but as the write operation for pswrd1 is unsuccessful, I am getting garbage in pswrd2 during read.
  • I believe there is something wrong in your application code to cause this issue.
  • This is the memory related issue.
    Also, sometimes I face the problem that osal_mem_alloc is not able to allocate memory. It returns 00 to the pointer.
    Do all these memory related problems have common issues?

  • It sounds that you allocate lots of memory in your application. I would suggest you to use static array to keep information instead of using memory allocation.
  • Other than stack code, we have used fewmemory allocations of this type only-
    pMsg = (mtOSALSerialData_t *)osal_msg_allocate( sizeof ( mtOSALSerialData_t ) +
    MT_RPC_FRAME_HDR_SZ + LEN_Token );
    to send message to osal layer using osal send message.
  • What's the point?
  • OK. How much memery is allocated for NV? Because in NV, we are storing only few bytes of memory in application value range. Only 4 keys we have defined. 2 of them are ssid_key and pswrd_key.
  • I don't see Z-Stack uses memory allocate in osal_nv_xxxx related API.

  • It stores the binding table in NV.

    I am using mem allocation just to give some content to my ssid1 and pswrd1 and it is successful in that. I can use this ssid1 and pswrd1 in further uses.

  • I can only suggest you to use static array instead of memory allocate for this.
  • I dont know how but I changed the pswrd_key from 0x404 to 0x0408 and it is working now.