Back to Getting Started

Back to Examples

Uart/USB Example

The Uart example tests the uart communiation available in the MCU.

Before you proceed with this tutorial, you must have the latest KoliadaESDK(TM) and KoliadaES environment setup. For more information on the setup check out Koliada Getting started. This example will run on any processor board that has UART serial communication and whereas this tutorial uses the PIEP CC2541, you can use any compatible processor board.

Fig. 1: TOP VIEW

Before working on this example, make sure you have set the latest KoliadaESDK(TM). For information on KoliadaESDK(TM) setup see here.

Note: After making sure the SDK is setup correctly, the BuildConfig needs be built & flashed only if you have changed any board configuration. You can find an example BoardConfig project here: KoliadaESDK(TM)/examples/BoardConfig.

In this particular example there are no I/O configuration changes, and we can move on to build and flash the TestUart application.

You find the source for TestUartEvent here KoliadaESDK(TM)/examples/TestUart.

Now build the project and hit 'Download to Target'.

You should see the following output on your serial terminal

Fig. 2: Output of TestUart Example

The output shows the TestUart program being loaded which prints out Hello World to the serial terminal and echoes out whatever is entered using the keyboard input into the serial terminal.

void TestUart()
	print("%s\n", __FUNC__);
	// set up the rxEvent
	objectCreate(rx.event = rxEvent);
	OnEvent(rxEvent, (HANDLER) rxEventHandler);
	// set up the uart to be used
	uart.init(UART_PORT, NULL, &rx, &tx);
	uart.puts("Hello World\n", UART_PORT);
	// stay awake to service uart interrupts
	// this is ONLY required for buffered reads
	// it is not required if your system only writes.
	// wait for events
	WaitEvent(0, 0);// never returns

We can walkthrough TestUart.c now.

The objectCreate(rx.event = rxEvent); creates a event under the name rxEvent which refers to the uart serial data receive event which is called eery time data is received through the uart port. On this event (OnEvent(rxEvent, (HANDLER) rxEventHandler);) the rxEventHandler will be called. The uart port receive event is routed to the rxevent handler defined by the user.

We then intialize and setup the uart port to be used using uart.init(UART_PORT, NULL, &rx, &tx). We print out hello world to the uart port setup using uart.puts(“Hello World\n”, UART_PORT).

Now since the event is set the system goes to event listening mode. This is done by using WaitEvent(0, 0); which waits for any event(uart input through the keyboard input from serial terminal).

void rxEventHandler(EVENT e, word c)
	// this handler is called whenever the rx event is posted
	// it is up to the developer to establish how to handle the data
	// here we simply echo the incoming chars to STDOUT.
	// you can stop/start data flow (if flow control is enabled) using
	//    - uart.wait() to claim the uart mutex (stop) and,
	//    - to release the uart mutex (start)
	// when using wait/post without flow control, any RX data sent
	// while you have the mutex will be lost
	while (rx.count)
		uart.putc(UART_PORT, uart.getc(UART_PORT));

The rxTimer Handler is called every time the uart recieves data i.e. user inputs data through keyboard and prints out the entered data (echoes it) on the serial terminal.

More Examples

More Boards

      Copyright © 2018-2019 Koliada, LLC
  • piep/examples/usb.txt
  • Last modified: 2019/04/16 00:50
  • by venkat