API  2.2
TSmarT Software Library
 All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
zigbee_receiver.c

This is an example about how to receive an API frame using the XBee Zigbee device.

/* @file main.c
* @author TST
* @version 0.1
* @date 18/06/2012
* @brief This is an example about how to receive an API frame using the XBee ZigBee device.
*
* This example uses ZigBee protocol to receive an API frame from other XBee(End-device or router).
* This application configures a Coordinator, for this reason be sure that user XBee has a proper
* API firmware: Coordinator API firmware.
* TSmarT can work only in API mode and this is defined by XBee firmware.
*
* @note to run this application it's necessary to set the other XBee ZigBee (it must be a
* end-device or router ZigBee device using API firmware) with the same configuration parameters:
* - kind of device: End-device or router (by firmware configures this parameter)
* - pan id: 0x3322
* The rest value can be default values.
*
* @note to use the debug UART, user must set the next configuration in the serial communication program:
* - Baud rate: 19200
* - Word length: 8 bits
* - Parity: none
* - Stop bit: 1
* - Flow control: none
*
*
*/
/* Platform headers */
#include "tsmart.h"
/* @brief vXBeezb_receiver task
*
* This task configures the XBee ZigBee and wait for a new API frame from another XBee.
*
* @param pvParameters
*/
void vXBeezb_receiver(void *pvParameters) {
/* Variables */
tsmart_xbee_zb_config_device_t xbee_zb_config_device;
uint8_t i;
/* Debug buffer */
uint8_t debug_buffer[50];
/* bytes received */
int32_t bytes_rcv = 0;
/* Set default configuration */
TSMART_XBEE_ZB_InitStructCoordinator(&xbee_zb_config_device);
/* Specific configuration for XBee ZigBee application */
xbee_zb_config_device.low_pan_id = 0x000003322;
xbee_zb_config_device.high_pan_id = 0x00000000;
bytes_rcv = TSMART_XBEE_ZB_Config(&tsmart_xbee_zb, &xbee_zb_config_device);
if (bytes_rcv != TSMART_PASS){
for(;;);
}
/* Wait to start */
for(;;){
status = TSMART_XBEE_ZB_ReadSatus(&tsmart_xbee_zb, 15000/portTICK_RATE_MS);
/* Now the network is created */
break;
}
}
for(;;){
/* Reset buffer */
memset(data_received.data, 0x00, sizeof(data_received.data));
/* Wait for new API frame */
bytes_rcv = TSMART_XBEE_ZB_ReceiveApiFrame(&tsmart_xbee_zb, &data_received, portMAX_DELAY);
if(bytes_rcv != 0 ){
/* Debug UART */
sprintf(debug_buffer, "from %x%x - %s", data_received.high_addr_source, data_received.low_addr_source, data_received.data);
TSMART_UART_Send(&tsmart_uart1, debug_buffer, strlen(debug_buffer), 1000/portTICK_RATE_MS);
}
}
}
/* @brief "Init()" function.
*
* This is the first thing that the user must do for using TSmarT.
* It initializes specific hardware resources (GPRS, GPS, AI, DIO, MODBUS, MSA...)
* and software resources (queues, mutex, tasks...) for the user application.
*
* The way to fill in this function properly is to initialize first
* hardware resources and after that software resources.
*
* This function must return: TSMART_PASS when every thing is OK or
* TSMART_FAIL when a failure happened.
*
* @return
* @arg TSMART_PASS
* @arg TSMART_FAIL
*/
int32_t init() {
/* Variables */
tsmart_xbee_zb_config_t xbee_zb_config;
tsmart_uart_config_t uart_config;
static uint8_t uart_buf[50];
portBASE_TYPE xReturn;
/* ************************************************************************* */
/* Debug Mode */
/* ************************************************************************* */
/* ************************************************************************* */
/* Initialize resources */
/* ************************************************************************* */
/* The specific hardware for this application: XBee ZigBee and UART.
* The baud rate and flow control for the XBee UART will be set. The priority
* for the reception task will be set to 14, avoiding lose packet.
*/
xbee_zb_config.baud_rate = 9600;
xbee_zb_config.priority = 14;
if (TSMART_XBEE_ZB_Init(&tsmart_xbee_zb, &xbee_zb_config) != TSMART_PASS){
return TSMART_FAIL;
}
/* Exit XBee from sleep mode */
/* UART configuration. */
/* Baud rate */
uart_config.baud_rate = 19200;
/* Flow control */
uart_config.flow_control = TSMART_UART_FC_NONE;
/* Parity bit */
uart_config.parity_bit = TSMART_UART_PARITY_NONE;
/* Word length */
uart_config.word_length = TSMART_UART_WORDLENGTH_8B;
/* Stop bit */
uart_config.stop_bit = TSMART_UART_STOPBITS_1;
/* Buffer size for our UART */
uart_config.rx_buf_sz = 50;
/* Buffer for our UART */
uart_config.rx_buf = uart_buf;
/* UART DMA */
uart_config.use_dma = TSMART_UART_DMA_RX_TX;
/* UART time out */
uart_config.use_tim = TSMART_UART_TIM_SW;
/* Initialize UART. UART 1 is selected */
TSMART_UART_Init(&tsmart_uart1, &uart_config);
/* ************************************************************************* */
/* Application task */
/* ************************************************************************* */
/* This examples will need one task:
*
* - XBeezb_receiver task: This task configures the device and wait for new
* message: Hello world!, this message will be showed
* using a debug UART.
*/
xReturn = xTaskCreate(vXBeezb_receiver, "XBeezb_receiver", 512, NULL, 7, NULL);
if (xReturn != pdPASS) {
return TSMART_FAIL;
}
/* Everything OK */
return TSMART_PASS;
}