monoZ docs
TIMER EXAMPLE

Objectives of Example

  1. Demonstrate how to create a Application Thread using monoZ_Lib
  2. Demonstrate how to create single-shot and recursive timers, and use them.
  3. Demonstrate how to use CLI for printing information.
  4. Demonstarte how to start timer and stop the timer with reference to the ID.

1. monoZ_Lib context Setup

There are certain context variables provided by user to initialize all the context required by the monoZ_Lib.
i. Hardware related context[Mandatory]
ii. Modem related context[Optional]

i. Hardware related context

The definitions of all hardware context variables are present in “MZ_public.h” header file. The sample template for using is also provided in the same header file.

a. Main context variable

User must create this context variable for successful use of the library. In case the context variable is not created by user, The monoZ_Lib will not initialize and a linker error will occur.

Timer Sample code implementation

TODO

ii. Modem related context

User must create the modem context functions if wish to initialize modem with specific configuration. There are 2 function definitions present to provide flexibility. The names of the function are fixed. User need to define the function with this exact name. Prototype of the functions are as follows:

  • void mz_reset_sequence (void * arg) This function will execute all ATcommands required to initialize modem during Power ON.
  • void mz_reboot_sequence (void * arg) This function will execute all ATcommands required to initialize modem during Wakeup.
  • Here we are setting the configurations like power saving mode,Device Configurations and its control settings.
  • Also the time out for a particular API can also be defined and even the duration between API consecutive hits can be specified.
  • Access point can be defined to which we need to use.
  • LWM2M configurations are been defined and set using the AT commands. LWM2M commands are been set with different input params.
  • Device has been rebooted using Z AT command and and board is reinitialized using AT command. Once the Device is reinitialized, set of AT commands like AT command initialization, registration and reading the Clock value.
  • LWM2M initialization is done and command is issued.

Note: User must define the desired AT commands need to be executed during these actions.

Sample code implementation

void mz_reset_sequence(void * arg)
{
    (void)arg;
    mz_power_config();
    mz_apn_set();
    mz_lwm2m_config();
    mz_device_reboot();
    mz_device_start();
    mz_lwm2m_start();
}

Void mz_reboot_sequence(void * arg)
{
    (void)arg;
    mz_device_reboot();
    mz_device_start();
    mz_lwm2m_start();
}
Note
  1. In case no context function is defined by user, a default context is taken by monoZ_Lib to initialize the modem.
  2. In case blank context function is defined by user, Modem will be on without any initialization.
  3. In case of user wish to use any protocol related feature, its mandatory to define the context by user.
  4. Incase using tool, these functions are created and placed in "MZ_modem_config.c"

2. monoZ_Lib Initialization and start

User must initialize the monoZ_Lib before it can start using the library features. The monoZ Library can be initialized using the API MZ_init (). “MZ_main. h” header file provides a sample use of how to initialize the library.

Sample code implementation

mz_version ver = {
        ._major = MZ_SW_VERSION_MAJOR,
        ._minor = MZ_SW_VERSION_MINOR,
        ._patch = MZ_SW_VERSION_PATCH
  };
if(MZ_OK != MZ_init(&ver))
{
    Error_Handler();
}
Note
  1. The initialization function should be called prior to FreeRTOS kernel initialization.
  2. Incase using tool, monoZ Library initialization code is placed inside main() API present in main.c

3. Application creation and start

The Timer Application initialization is done before the Main FreeRTOS kernel starts. User must create a user application using the monoZ Library API’s.

Timer Sample code implementation

  • The Timer main user application initialization function
      mz_error_t timer_app_init(void)
      {
          ...
          // Create the timer application threads
          if(!mz_thread_create(&timer_thread_id,"Timer Scheduler",timer_app,NULL,osPriorityNormal,timer_stack,TIMER_APP_STACK_SIZE,&timer_cb_mem,sizeof(timer_cb_mem)))
          {
              _ret = MZ_THREAD_CREATE_FAIL;
          }
          ...
      }
    


  • The Timer main user application initialization function called from main.c
      /* creation of user task */
      if(MZ_OK != uv_app_init())
      {
          Error_Handler();
      }
    
Note
  1. The user application must be created after FreeRTOS Kernel initialization and before FreeRTOS main scheduler start to avoid delay in application start. It will ensure the user application starts at the time of FreeRTOS scheduler starts.
  2. Incase using tool, the code is generated and placed in "MZ_timer_example.c" and the code is called within main in main.c .
  3. For building timer based applications using monoZ library, users can add upto a maximum of 5 timers.

4. Application process

User must design the application based on requirements.

Timer Sample code implementation

  • The timer main Application
      static void timer_app(void * arg)
      {
          ...
          if(MZ_OK != mz_tm_create_start_recursive("My timer",pdMS_TO_TICKS(5000),timer_cb))
          {
              __NOP(); //No action
          }
          while(1)
          {
              ...
          }
      }
    

source code

Click here to access the source code of this file.