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.

how to integrate user code in zstack

Other Parts Discussed in Thread: CC2538, Z-STACK

Hello All,

Greeting for the day!!!!

i want to implement something like this -> ( plz refer attached pic for more clarity )

1) i hav my application code with main() and in that under while(1) loop i am monitoring my sensors data and processing it..... for ex -

int main()

{.....

......

      while(1)

     {

      process sensors data...

      update the bits in flag register

      }

   return 0;

}

this application is for cc2538 only and i implemented this using cc2538 foundation firmware code..my this application is making use of HAL, BSP and driverlib provided in that codes

2) in this application i am creating one flag register and as per the sensors status i am modifying this flag register.

3) now what i want is that -> zigbee should periodically monitor status of my this flag register and  as soon as if any change is happened zstack should run ISR related to that bit and should start the n/w and send report to ZC over the air.... 

4) after sending the report zstack must monitor the flag register statud and if any further changes happening then must inform the ZC....

5) i dont want to register my main code in task array of zigbee like generally we are doing.....

6) zigbee is like a ISR for me whenever my flag bit is sent it will take my flag data and sent OTA and again return control to my main code....

my problem is ->

1) how can i integrate my application code into zstack HA code becoz in zstack the main fn (in zmain.c) is like this-

int main( void )
{
// Turn off interrupts
osal_int_disable( INTS_ALL );

// Initialization for board related stuff such as LEDs
HAL_BOARD_INIT();

.............

...........

osal_start_system(); // No Return from here

return 0; // Shouldn't get here.
} // main()

 so wher to keep my code ?

2) how to make zstack to periodically monitor my flag register . in zstack they are doing something like this ->

in fn ->

void osal_run_system( void )

{...............

process / monitor / check  task array

}

and under task array they are monitoring HAL, MAC, NWK and Applications related event....

so how zstack monitor my flag register status ? how can i define my own event monitoring task for flag register.

anyone plz help me and guide me how can i do this or how i can implement this....

Thanks & Regards,

Maneesh SIngh

  • You can't put a while(1) in Z-Stack. It will block everything.

  • hello YiKai Chen sir,

    thanks for replying...

    1) if u see in zstack in file OSAL.c they did something like this -

    void osal_start_system( void )
    {
    #if !defined ( ZBIT ) && !defined ( UBIT )
    for(;;) // Forever Loop           <============ if i am not wrong then for(;;) and while(1) works same 
    #endif
    {
    osal_run_system();
    }
    }

    2) actually in my code i am doing something like this -

    int main()

    {

    ;;;

    while(1)   //this while loop is needed so that continuously i can monitor sensor data

    {

    Monitor the sensor data      // this is my infinite loop which continuously monitor the sensor data                                                                              // using HAL and driverlib and if any sensors status changed then it will execute its ISR 

    }

    }

    it is something like task event processing in our zstack...

    what i want is that in the above code whenever status of any sensor changes then its ISR should run and status report must be sent over the air to ZC......

    3) so to accomplish above task what changes i need to do in zstack.

    Thanks & Regards,

    Maneesh

  • Hello YiKai Chen sir,

    plz reply sir,

    To accomplish above task mention in my previous post what changes i need to do in zstack.

    Thanks & Regards,

    Maneesh

  • The while(1) in osal_start_system is the only allowable while(1) in Z-ZStack because it is the kernel of Z-ZStack tiny os.  You can try to put your application in this while(1) but it is dangerous to have another while(1).

  • Hello Sir,

    Thanks for your reply...

    i was thinking if i'll do like this then is this fine ->

    ******* in OSAL<sample_code>.c file************

    const pTaskEventHandlerFn tasksArr[] = {

    my_flag_reg_event_loop,                                                                                                                                                 macEventLoop,
    nwk_event_loop,
    Hal_ProcessEvent,
    #if defined( MT_TASK )
    MT_ProcessEvent,
    #endif
    APS_event_loop,
    #if defined ( ZIGBEE_FRAGMENTATION )
    APSF_ProcessEvent,
    #endif
    ZDApp_event_loop,
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
    ZDNwkMgr_event_loop,
    #endif
    zcl_event_loop,
    <sample code even toop >   <<<<======here i'll use zigbee sample application code for n/w formation and other                                                                        purpose 
    };

    And 

    void osalInitTasks( void )
    {
    uint8 taskID = 0;

    tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
    osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));

    my_flag_reg_task_Init(taskID++); 

    macTaskInit( taskID++ );
    nwk_init( taskID++ );
    Hal_Init( taskID++ );
    #if defined( MT_TASK )
    MT_TaskInit( taskID++ );
    #endif
    APS_Init( taskID++ );
    #if defined ( ZIGBEE_FRAGMENTATION )
    APSF_Init( taskID++ );
    #endif
    ZDApp_Init( taskID++ );
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
    ZDNwkMgr_Init( taskID++ );
    #endif
    zcl_Init( taskID++ );
    <sample code init>   <<<<======here i'll use zigbee sample application code for n/w formation and other                                                                       purpose 
    }

    now it should work like this ->

    1) my_flag_reg_task has highest priority and in round robin fashion zstack will schedule this.....so all the content which i was earlier writing in while(1) loop now become just as a my_flag_reg_task_init fn call....

    (but i hav one doubt -> in my code i am using few timer and counters for monitoring the sensor code and sending the alert.....so my Question is while scheduling how my timer/counter will run becoz thats a critical section in my project......becoz as per the scheduling zstack  will schedule mac task also, HAL task also, nwk task also so on..... so while scheduling these task it will preempt earlier scheduled tasks.... so whether my tasks timer and counter will continue to run in between scheduling or not ?)

    here is my code snipped ->

    while (1)               
    {
    if (ui32PrevCount1 != timerIntCount)
    {
       ...............
    }
    if (timerIntCount >= TIMER0A_COUNT_FOR_15SEC)
    {
      ..............

      ..............

    sendAlert ();

    }
    if (timerHourIntCount >= TIMER0A_COUNT_FOR_30SEC)
    {

    ..............................

    }
    if (ui32PrevCount2 != timerIntCount)
    {
      ......................
    }
    }

    NOTE :- my intention is -> 1) continuously monitor the sensors and update its status (that's why i used while(1) loop) and in my code i implemented few timers also as per my requirement......

     2) now what i want is that whenever my code detect any changes in sensor data it will update the flag register status and zstack must check this register periodically but if any change happened in the flag register data then zstack must generate event  and  must send this data over the air to ZC...

    plz tell me how i can do this job....

    Thanks & Regards,

    Maneesh singh

  • If you put while(1) in my_flag_reg_task_Init(), your application will never go out of my_flag_reg_task_Init().

  • hello YiKai Chen sir,

    you are right sir,

    so what changes i need to do to accomplish following task ->

    NOTE :- my intention is -> 1) continuously monitor the sensors and update its status ( that's why i used while(1) loop) and in my code i implemented few timers also as per my requirement......

     2) now what i want is that whenever my code detect any changes in sensor data it will update the flag register status and zstack must check this register periodically but if any change happened in the flag register data then zstack must generate event  and  must send this data over the air to ZC...

    plz tell me how i can implement these tasks..

    Thanks & Regards,

    Maneesh singh

  • I have advise you not to use while(1) from beginning. You should use osal_start_timerEX to create periodic event to check your sensor status and send message by Z-stack. Why do you insist in using while(1) loop?