monoZ docs
|
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.
monoZ Click tool user
Incase using tool, this variable is created and placed in "MZ_hardware_config.c"
Co2 Sample code implementation
extern MZ_UART_INIT_ST co2_lpuart1_instance; uart_enableuart_enable_cfg = { .u3 = MZI_UART3, .u3p = 0, .lu1 = MZI_LPUART1, .lu1p = &co2_lpuart1_instance, }; i2c_enable i2c_enable_cfg = { .i2 = MZI_I2C2, .i2p = 0, .i4 = MZI_I2C4, .i4p = 0, };
b. LPUART1 Peripheral context variable
User must create the peripheral context variable for the desired interface and use it in Main context variable. In case of the Co2 Addon board, it uses LPUART1 interface for sensor communication. monoZ_Lib takes all UART related initialization context through “uart_enable_cfg“ context variable. For the co2 application, we need to define a peripheral context variable. Let’s say “co2_lpuart1_instance”.
monoZ Click tool user
Incase using tool, this variable is created and placed in "MZ_Addon_board_CO2.c"
Co2 Sample code implementation
MZ_UART_INIT_ST co2_lpuart1_instance = { .Instance = MZ_ADDONCO2_INSTANCE, .Init.BaudRate = MZ_ADDONCO2_INIT_BAUDRATE, .Init.WordLength = MZ_ADDONCO2_INIT_WORDLENGTH, .Init.StopBits = MZ_ADDONCO2_INIT_STOPBITS, .Init.Parity = MZ_ADDONCO2_INIT_PARITY, .Init.Mode = MZ_ADDONCO2_INIT_MODE, .Init.HwFlowCtl = MZ_ADDONCO2_INIT_HWFLOWCTL, .Init.OverSampling = MZ_ADDONCO2_INIT_OVERSAMPLING, .Init.OneBitSampling = MZ_ADDONCO2_INIT_ONEBITSAMPLING, .AdvancedInit.AdvFeatureInit = MZ_ADDONCO2_ADVANCEDINIT_ADVFEATUREINIT };
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:
Note: User must define the desired AT commands need to be executed during these actions.
monoZ Click tool user
Incase using tool, these functions are created and placed in "MZ_modem_config.c"
Co2 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(); }
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.
monoZ Click tool user
Incase using tool, monoZ Library initialization code is placed inside main() API present in "main.c"
Co2 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(); }
The Co2 Application initialization is done before the Main FreeRTOS kernel starts. User must create a user application using the monoZ Library API’s.
monoZ Click tool user
Incase using tool, the code is generated and placed in MZ_Addon_board_CO2. c and the code is called within main in "main.c"
Co2 Sample code implementation
Static void co2_app_thread(void * arg) { … }
mz_error_tco2_app_init(void) { … /* Create the Co2 application thread */ if(!mz_thread_create(&co2_thread_id,"co2 Scheduler",co2_app_thread,NULL,osPriorityNormal,co2_stack,CO2_APP_STACK_SIZE,&co2_cb_mem,sizeof(co2_cb_mem))) { _ret = MZ_THREAD_CREATE_FAIL; } … }
/* creation of user task */ if(MZ_OK != uv_app_init()) { Error_Handler(); }
User must design the application based on requirements. Following is the High-level requirement considered for building the Addon-board Co2 example.
Co2 Sample code implementation
A. Application should read co2 sensor data periodically (every 90 sec)
if(MZ_OK == mz_tm_create_start_recursive("Uart Read timer",CO2_SENSOR_READ_TIME,co2_sensor_read_timer_cb)) { mz_puts("co2 sensor reading timer started\r\n"); }
static void co2_sensor_read_timer_cb(TimerHandle_txTimer) { /* Set the co2 sensor timer expire flag */ co2_read_timer_expire_flag = CO2_READ_TIMER_EXPIRE_SET; }
if(CO2_READ_TIMER_EXPIRE_SET == co2_read_timer_expire_flag) { /* Process the co2 sensor read timer expire event */ … }
B. Application should send the sensor data to LWM2M server after successfully reading the data
/* Set the Object 19/0/0 Data and send the Notify request to monoZ_Lib */ mz_error_t status = mz_set_and_notify_Ob19_0_0(payload_string);
C. Application should monitor LWM2M server status and reconfigure the client-server connection incase, the following case happens
i. If server did not send a Observe request within 3 mins of the device starts.
lwm2m_server_monitoring_timer_id =mz_tm_create_one("Server monitoring timer",LWM2M_SERVER_MONITORING_TIME,lwm2m_server_monitoring_timer_cb); (void)mz_tm_start(lwm2m_server_monitoring_timer_id);
static void lwm2m_server_monitoring_timer_cb(TimerHandle_txTimer) { /* Check if observe has not started */ if(OBSERVE_COMPLETE_CLEAR == observe_receive_flag) { /* Then set the server re-registration flag */ lwm2m_server_rereg_flag = SERVER_REG_NEED_SET; } }
ii. If client turned offline due to connection failure.
All the protocols related events are passed from monoZ_Libto a predefined callback API name “mz_pro_default_callback”.User must implement this callback API to get real time post processing events from Lwm2m client present inside the monoZ Library. A dummy implementation is as follows.
void mz_pro_default_callback(void * evnt) { st_lw_event * e = event; if(LW_EV_CLIENT_OFF == event) { /* set the server re-registration flag */ lwm2m_server_rereg_flag = SERVER_REG_NEED_SET; } if(LW_EV_OBSERVE == event) { if(e->lw_Obj_id == 19 && e->lw_Obj_Ins_id == 0 && e->lw_Res_id == 0) { /* Set the observed received flag */ observe_receive_flag = OBSERVE_COMPLETE_SET; } } }
a. How to use UART in interrupt mode.
User must register uart event callback to use uart in interrupt mode.
MZ_UART_register_intr_cb_rx(MZ_ADDONCO2_UART_INSTANCE,co2_lpuart1_rx_intr);
The callback API co2_lpuart1_rx_intr () will be called by monoZ_Lib after uart receive interrupt is generated by MCU. User must first set the uart in receive interrupt mode when expecting data to be received on interface.
MZ_UART_Receive_IT(MZ_ADDONCO2_UART_INSTANCE,(uint8_t*)&resp21,sizeof(resp21));For more details user can check "MZ_uart.h"
b. How to use UART in polling mode.
MZ_UART_Transmit(MZ_ADDONCO2_UART_INSTANCE,(uint8_t *)&cmd21,sizeof(cmd21),1000);
c. How to perform modem reconfiguration with for reconnecting lwm2m server.
if(MZ_OK == MZ_modem_config_set(mz_reboot_sequence)) { /* print the success in the CLI */ mz_puts("Server re-reg sequence started\r\n"); }