contiki/examples/cc2538-common/test-uart.c
2016-12-01 12:45:10 +05:30

111 lines
3.7 KiB
C

/*
* Copyright (c) 2016, Indian Institute of Science <http://www.iisc.ernet.in>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* This file is part of the Contiki operating system.
*
*/
/**
* \addtogroup cc2538-examples
* @{
*
* \defgroup cc2538-test-uart Test the CC2538 UART
*
* Demonstrates the use of the CC2538 UART
*
* @{
*
* \file
* A quick program for testing the UART1 on the cc2538 based
* platform which can be used to interface with sensor with UART
* interface and to communicate with other development boards
* \author
* Akshay P M <akshaypm@ece.iisc.ernet.in>
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "sys/etimer.h"
#include "dev/leds.h"
#include "dev/uart.h"
#include "dev/serial-line.h"
#include <stdio.h>
#include <stdint.h>
/*---------------------------------------------------------------------------*/
static struct etimer et;
/*---------------------------------------------------------------------------*/
PROCESS(cc2538_uart_demo_process, "cc2538 uart demo");
AUTOSTART_PROCESSES(&cc2538_uart_demo_process);
/*---------------------------------------------------------------------------*/
unsigned int
uart1_send_bytes(const unsigned char *s, unsigned int len)
{
unsigned int i = 0;
while(s && *s != 0) {
if(i >= len) {
break;
}
uart_write_byte(1, *s++);
i++;
}
return i;
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(cc2538_uart_demo_process, ev, data)
{
char *rxdata;
static uint16_t count=0;
char string[20];
PROCESS_BEGIN();
uart_set_input(1, serial_line_input_byte);
etimer_set(&et, CLOCK_SECOND * 4);
leds_toggle(LEDS_GREEN);
while(1) {
PROCESS_WAIT_UNTIL(etimer_expired(&et));
sprintf(string, "sending string %u.\n", ++count);
uart1_send_bytes((uint8_t *)string, sizeof(string) - 1);
etimer_reset(&et);
printf("string is being sent\n");
PROCESS_YIELD();
if(ev == serial_line_event_message) {
leds_toggle(LEDS_RED);
rxdata = data;
printf("Data received over UART %s\n", rxdata);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
* @}
* @}
*/