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.

Timer for CC2530

Other Parts Discussed in Thread: CC2530, CC2538, Z-STACK

Hey,

I am using CC2530 zigbee development kit, I need to ask: I took a sample example of toggling LED 1, but when it toggle, it couldn't appear in the EB, because the for loop is very fast, So I need a delay or timer after toggling.

here is the sample code:

#include <ioCC2530.h>

int main(void)

{

// Set P1.0 of CC2530 as output

P1DIR |= 0x01;

// Toggle P1.0

for(;;)

  {

     P1_0 ^= 1;

  }

again what I need is a timer or delay after P1_0^=1; 

 

another question: I need to toggle LED 2, but I dont know the pin.

Thanks,

SI

}

 

  • Hey,

    One more thing, I am using IAR embedded workbenchversion 7.60

    Thanks 

    S I

  • If the only thing you want to do is toggle the LED then you could use:

     

    uint32 delay;

    while (true)

    {

    P1_0 ^= 1;


    for ( delay = 0; delay < 50000; delay++);

    }

     

    You could play around with the 50000 value to find something suitable.

    If you want a timed value you need to use a counter with a know oscillator speed with a interrupt or polling mechanism.

    If you are using the ZStack, then you can create a task and use the OSAL timer functions to set timed events and then use the HAL LED API to set, toggle or blink the LEDs.  You can also configure which pins the LEDs are on in hal_baord_cfg.h, though if you are using an EB then this is probably all setup by default anyway.

    iR.

  • Thanks for your reply,

    One more thing, I want to write in Zstack, to toggle the LEDs. what I am going to do ? I am going to use the APIs of the Zstack to make my own application?

    S I

  • The best way is to create a task and add it to the OSAL initialization file.  I will assume you are using SampleApp that comes with the CC2530 ZStack download, but the methodology follows for any project using AF.h, the ZStack application framework.

    First, I would create a new task in new .c and .h files.

    In the header file, say new_task.h, I would add the events, the public prototypes and public variables:

    //////
    /// Events.
    //////
    #define EVENT_new_task_start 0x0001
    #define EVENT_new_task_stop 0x0002
    #if  ( HAL_LED == TRUE  )
    #define EVENT_new_task_blink 0x0004
    #endif
    
    
    //////
    /// Public variables.
    //////
    extern uint8 PUBLIC_task_id_new_task;
    
    
    //////
    /// Public prototypes.
    //////
    void new_task_init ( uint8 task_id );
    uint16 new_task_process_event ( uint8 task_id, uint16 events );
    

     

    Then, in new_task.c, I would add local variables, the initialization function and the event process function.

    //////
    /// Includes.
    //////
    
    #include "AF.h"
    
    #if (HAL_LED == TRUE)
    #include "hal_led.h"
    #endif
    
    #include "new_task.h"
    
    
    //////
    /// Variables.
    //////
    
    // The task ID.
    T_uint8 PUBLIC_task_id_new_task;
    
    
    /**
     * Initialize the task.
     */
    void new_task_init ( T_uint8 task_id )
    {
    
    	PUBLIC_task_id_new_task = task_id;
    
    }
    
    /**
     * Event handler.
     */
    T_uint16 new_task_process_event ( T_uint8 task_id, T_uint16 events )
    {
    
    
    	/**
    	 * Start event.
    	 */
    	if ( events & EVENT_new_task_start )
    	{
    
    		// Start a periodic blink event at 1Hz.
    		osal_start_reload_timer ( PUBLIC_task_id_new_task, EVENT_new_task_blink, 1000 );
    
    		// Clear event and return.
    		return ( events ^ EVENT_new_task_start);
    
    	}
    
    
    	/**
    	 * Stop event.
    	 */
    	if ( events & EVENT_new_task_stop )
    	{
    
    		// 10ms blink, I think.
    		osal_stop_timerEx ( PUBLIC_task_id_new_task, EVENT_new_task_blink );
    
    		// Clear event and return.
    		return ( events ^ EVENT_new_task_stop);
    
    
    	}
    
    
    	/**
    	 * Blink event.
    	 */
    	#if ( HAL_LED == TRUE )
    	if ( events & EVENT_new_task_blink )
    	{
    
    		// 10ms blink, I think.
    		HalLedBlink(HAL_LED_1, 1, 10, 1000);
    
    		// Clear event and return.
    		return ( events ^ EVENT_new_task_blink);
    
    
    	}
    	#endif
    
    	// Return unrecognized events.
    	return (0);
    
    }
    

    Finally, you will need to register the task wil the OSAL, which is done in OSAL_SampleApp.c by adding the initialization function and process_event function references.  I have copied in the code from OSAL_SampleApp.c and highlighted the lines added to register new_task in red:

     

    /*********************************************************************
     * INCLUDES
     */
    
    #include "ZComDef.h"
    #include "hal_drivers.h"
    #include "OSAL.h"
    #include "OSAL_Tasks.h"
    
    #if defined ( MT_TASK )
      #include "MT.h"
      #include "MT_TASK.h"
    #endif
    
    #include "nwk.h"
    #include "APS.h"
    #include "ZDApp.h"
    #if defined ( ZIGBEE_FREQ_AGILITY ) || defined ( ZIGBEE_PANID_CONFLICT )
      #include "ZDNwkMgr.h"
    #endif
    #if defined ( ZIGBEE_FRAGMENTATION )
      #include "aps_frag.h"
    #endif
    
    #include "SampleApp.h"
    #include "new_task.h"
    
    /*********************************************************************
     * GLOBAL VARIABLES
     */
    
    // The order in this table must be identical to the task initialization calls below in osalInitTask.
    const pTaskEventHandlerFn tasksArr[] = {
      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
      new_task_process_event,
      SampleApp_ProcessEvent
    };
    
    const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
    uint16 *tasksEvents;
    
    /*********************************************************************
     * FUNCTIONS
     *********************************************************************/
    
    /*********************************************************************
     * @fn      osalInitTasks
     *
     * @brief   This function invokes the initialization function for each task.
     *
     * @param   void
     *
     * @return  none
     */
    void osalInitTasks( void )
    {
      uint8 taskID = 0;
    
      tasksEvents = (uint16 *)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
      osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
    
      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
      new_task_init (taskID++);
      SampleApp_Init( taskID );
    }

    You need to call osal_set_event ( PUBLIC_task_id_new_task, EVENT_new_task_start ), in SampleApp.c you could do that in state change:

     

           case ZDO_STATE_CHANGE:
              SampleApp_NwkState = (devStates_t)(MSGpkt->hdr.status);
              if ( (SampleApp_NwkState == DEV_ZB_COORD)
                  || (SampleApp_NwkState == DEV_ROUTER)
                  || (SampleApp_NwkState == DEV_END_DEVICE) )
              {
    
                // Start new_tast.
                osal_set_event( PUBLIC_task_id_new_task,
                                  EVENT_new_task_start );
    
                // Start sending the periodic message in a regular interval.
                osal_start_timerEx( SampleApp_TaskID,
                                  SAMPLEAPP_SEND_PERIODIC_MSG_EVT,
                                  SAMPLEAPP_SEND_PERIODIC_MSG_TIMEOUT );
    
              }
    

    To be more interactive, you could set the start and stop events using key handler.  Add osal_set_event ( PUBLIC_task_id_new_task, EVENT_new_task_start ) to one key, and osal_set_event ( PUBLIC_task_id_new_task, EVENT_new_task_stop ) to another.  Despite such limited functionality, I be there's something deeply satisfying about it.  :)

    I hope this helps.

    iR.

  • I should just point out that you could just drop the HalLedBlink(HAL_LED_1, 1, 10, 1000) into the existing event handler for SAMPLEAPP_SEND_PERIODIC_MSG_EVT in SampleApp but I am assuming that you want to do more than just blink an LED, in the long run.  Indeed, you might not be using the SampleApp, in which case I hope that this translates meaningfully into whatever application context you are working in.  By building a new task I think you get a much better and more useful feel for using the ZStack Application Framework.

    I hope I didn't scare you off with the large post and that you find it helpful.

    iR.

  • Thank you very much, I got it :)

    IR

  • Hey,

       I need to ask for something, I want to make my own monitor application ( like the Zigbee sensor monitor), to monitor the sensors and so on, first I will define the sensors inside my code, and then I want the sensors to send me the temperature, and I will do the GUI.. Shall I use the IAR workbench program to write the code or  use another program such as Eclipse or leonardo?? and how could I define the sensors inside my code??

     

    Thanks,

    S I

  • Hi SI,

    I would use IAR, I think that it has the best compiler support, including library files and linker scripts, and most, if not all, the example projects are using IAR.  I think most forum users and TI support will assume you are using IAR and might be lost otherwise.

    I'm not sure what you mean here by sensors.  Can you describe the project in more detail?   What kind of sensor are you using.  What kind of data do you want to send back to the GUI?

    iR.

  • Hey iR

    I searched for how to make a zigbee monitor, actually I want to make a zigbee monitor, the data I want to send back to the GUI is temperature or anything else.

    but when I searched for how to make a zigbee monitor I found that I should install MS visual studio and qt for GUI, and build the qt with the visual studio, and I worked for that today but I failed !! because something isn't correct in the configuration.

    S I

  • Hi SI,

    You want to make a Windows application to handle the data coming in off your network?  What is 'qt'?

    The easiest proof-of-concept way would be to use the UART so you can hook up the serial port of the PC and report data in ASCII, which you can do with the EB because it has an RS232 driver and serial port.

    You can access the UART functionality using the functions in hal_uart.h.  I think you would need to define HAL_UART=TRUE.  There may be more to it, I've never accessed the UART directly myself.

    If you want to build an application to read network messages and provide a GUI then the best way is to use the MT interface.  For this you will need to understand the MT packet format which is described in the file Z-Stack Monitor and Test API.pdf that is included with the ZStack download.  Typically, the coordinator is hooked up to the PC and doubles as a gateway to the application.  In this case, all nodes send their data to address 0x0000, which is always the coordinator, and your application can get all the data.

    Typically, the following definitions are defined for MT-aware nodes:

    ZTOOL_P1

    MT_TASK

    MT_SYS_FUNC

    MT_ZDO_FUNC

    MS Visual Studio with .NET is a good platform to build the app on because it provides access to Serial Ports as well as fast GUI development.

    iR.

  • Hi all,

              I am using ZStack-CC2530-2.3.1-1.4.0 code for developing the Sample application on My own Board design. I fallowed the above method to do the Communication between two devices. one as Coordinator and another one as End device. But After downloading the code both devices LED start to Blink  and its never stopped. Even i used the Switch 1 to send the command as KEY_CHANGE Sample_process_event function. But its not working fine. Please can any one help me to solve this Problem.

           My requirement is to do the Switching Operation between the Coordinator and End device. in such a way that i will use two switches.  When switch/button 1 is pressed in the in Coordinator the END device LED should get ON and when Switch/button 2 is pressed the LED should get OFF.

         Please help me to solve this problem.

  • Hi,

    I am learning CC2530 Development Kit too. I am using SmartRF05 Evaluation Board.

    This is how I toggle all of the four LEDs. You could take a reference. :)

    I changed based on blinky.c.

     

    #include <ioCC2530.h>

    int main(void)
    {
      // Set P1.0 P1.1 P1.4 of CC2530 as output
      P1DIR |= 0x13; // Binary:  0001 0011
                             // PIN #:   7654 3210
      // Set P0.1 of CC2530 as output 
      P0DIR |= 0x02; // Binary:  0000 0010
                             // PIN #:   7654 3210
      for(;;)
      {
        // Toggle P1.0 P1.1 P1.4
        P1_0 ^= 1;
        P1_1 ^= 1;
        P1_4 ^= 1;
        // Toggle P0.1
        P0_1 ^= 1;
        for(long i=0;i<50000;i++);
      }
    }

  • Hi,

        Thanks for your reply, but I need the code sample to develop the Zstack sample application to control the Switching Operation. The above code is only for the normal processor operation.

  • Hi,

    Here is my code to blink a led on P1_0 pin. I think it is easy and works.


    #include <ioCC2530.h>

    int main( void )
    {
    volatile long i;
    P1DIR |= 0x01;
    for(;;)

    {

    P1_0 ^= 1;

    for (i = 0; i<10000; i++)
    {
    ;
    }

    }

    }

    You can modify the speed by change the number of "i". i<1000 is a very fast blinking. 10000 is slower, and 100000 is very slow.

  • Hi,

    Can you tell me, here which one is idle task

    thank you 

  • Hi Shibin,

    Don't understand you question well. Could you specify more clearly?

  • Hi YiKai Chen ,

    As we are making sensor based projects using cc2538. I have seen some task making mechanism in your page using cc2530. could you please tell me how to make tasks using cc2538 and task switching 

    thank you

  • Hi shibin,

    Do you use Z-Stack on CC2538? There is no RTOS on Z-Stack. Z-Stack has a tiny OS which allows you to create event. If you can describe your application more specific, I can give you suggest.

  • Hi YiKai Chen,

    we are not using any RTOS

    As per our project we need three tasks one is for rf send and receive operation , one for system operation and one idle task can you tell me how to make tasks for cc2538 and how to do task switching from idle task to other tasks as per interrupt call

    thank you

    shibin

  • Hi shibin,

    Do you use TI Z-Stack or not?

  • Hi YiKai Chen,

    No

    thank you

  • Hi Shibin,

    From SW point of view, you need to have OS running on CC2538 to create task if you want to do it. You can either choose one already exist (Contiki for example) or build it from scratch by yourself (this is not recommend ).

  • Hi YiKai Chen,

    If I use z-stack how can I make tasks and how to do task switching 

    thank you