Back to Getting Started

Back to Examples

This example demonstrates setting up and making use of the LED and Switch Board

This examples helps you in learning APIs and application firmware that can be used to

1. To figure out the SPIP headers on the peripheral board and how they are mapped to the I/O ports.

2. Hence, accessing the switch and LED through I/O ports and controlling them.

Fig. 1: TOP VIEW Fig. 2: BOTTOM VIEW

Before you proceed with this tutorial, you must have Koliada visual studio plugin and Koliada toolchain setup. For more information on the setup check out Koliada Getting started. This tutorial is currently based on the TI CC2541 which uses the 8051.

Before working on this example, make sure you have set the latest KoliadaESDK(TM). More information on setSDK setup and setting the path is shown here.

Note: After making sure SDK path is set, you must know that the board config must be flashed only if you are using any I/O pins. In this particular example there are I/O configurations for the LED and Switch and hence you should open the LEDSwitch config project in visual studio. You can find the project under koliadaSDK→examples→LEDSwitchConfig.

Select the SDK using the SDK Dropdown button at the top.

Now compile the config project and hit download to target.

You should see the following output on your serial terminal

Now since the I/O ports for the LED and the Switch are configured by downloading the config project we can move on to download the application firmware. You find the application firmware for LED and Switch examples under koliadaSDK→examples→LEDSwitch.

Now compile the project and hit download to target.

You should see the following output on your serial terminal

In this example we should be testing the operation of the LED and the Switch available on the LED and Switch peripheral board.

Note: SW2,SW3 and LED2,LED3 are the only the switches and LEDs configured in this particular project.

Hence you can test the functionality of these LEDs and Switches here

When you press the button2/SW2 on the board the LED2 should be toggled and you should the see the LED2 light up and you should also be seeing the following output on your serial monitor

When you press the button2/SW2 on the board again the LED2 should be toggled and you should the see the LED2 go off and you should also be seeing the following output on your serial monitor

When you press the button3/SW3 on the board the LED3 should be toggled and you should the see the LED3 light up and you should also be seeing the following output on your serial monitor

When you press the button3/SW3 on the board again the LED3 should be toggled and you should the see the LED3 go off and you should also be seeing the following output on your serial monitor

LEDSwitch Config

As mentioned above the LEDSwitchConfig.c file configures the I/O pins. Hence we configure the necessary LEDs and Switches that we intend to use. Here in this example SW2,SW3 and LED2,LED3 are used. Hence the respective port numbers are configured. Port and Bit specifies the port and mask used. The port and mask details for the LED and SWITCH board can be found in the map file here. The given five binary bits specify the direction of the port input/ output, the edge trigger rising/falling, reenable, inversion and initial high/low state. The name describes the I/O pin name that can be used in the application firmware.

PinDescriptor pinTable[] =
	{
// mark unused entries with GPIOX
// there is an implicit assumption that LEDs 1-4 and SW1-4 are first 8 entries
// single entry can describe multiple pins with same port/config (OR the bits together)
// for outputpins, REN is the init state (lo = 0, 1 = hi)
//
//                       +-------------- dir: 0 = input,   1 = output
//                       |  +----------- ies: 0 = rising,  1 = falling
//                       |  |  +-------- ren: 0 - pull,    1 - 3-state
//                       |  |  |  +----- inv: 0 - normal,  1 - invert
//                       |  |  |  |  +-- set: 0 = off,     1 = on
//                       |  |  |  |  |
//  pin    port   mask   |  |  |  |  |        Name
//-------|-------|-----|-----------------|------------------------------------------
/*   0 */ {PORT0, BIT3, {1, 0, 0, 1, 0}},	/*< LED1, SYSLED, OTA_READY, OTA_LOAD, OTA_DECODE, OTA_ERROR >*/
/*   1 */ {PORT1, BIT1, {0, 1, 0, 0, 0}},	/*< SW3 >*/
/*   2 */ {PORT0, BIT7, {0, 1, 0, 0, 0}},	/*< SW2>*/
/*   3 */ {PORT0, BIT2, {1, 0, 0, 1, 0}},	/*< LED2, TP0 >*/
/*   4 */ {PORT0, BIT5, {1, 0, 0, 1, 0}},	/*< LED3, TP1 >*/
 
	};

We can walkthrough ledSwitch.c now.

Application firmware(ledSwitch) app explanation

The main.c calls ledSwitch function in ledSwitch.c

  void ledSwitch()
	{
	printf(">>%s\n", __func__);
 
	// look up the i/o pins
        LED2 = GPIO.FIND("LED2");
	SW2 = GPIO.FIND("SW2");// deliberate switch!
	LED3 = GPIO.FIND("LED3");
	SW3 = GPIO.FIND("SW3");// deliberate switch!
 
	// create testButton event
        objectCreate(testButtonEvent2);
	objectCreate(testButtonEvent3);
 
 
	// connect a handler to the event
	OnEvent(testButtonEvent3, (HANDLER) testButtonEventHandler3);
        OnEvent(testButtonEvent2, (HANDLER) testButtonEventHandler2);
 
	// capture gpio interrupt to trigger the event
	// here we use the port interrupt for PB1 (BUTTON)
	// note: it is up to the developer to manage overlapping pin port IRQs!
        GPIO.SetIRQ(SW2, testButtonEvent2);
 
	GPIO.SetIRQ(SW3, testButtonEvent3);
 
 
	// enable interrupts for the button(s)
        GPIO.IFG(SW2, 0);
	GPIO.IEN(SW2, 1);
 
	GPIO.IFG(SW3, 0);
	GPIO.IEN(SW3, 1);
 
	// wait for system events
	printf("press button(s) ->\n");
	WaitEvent(0, 0);// never returns
	}
 
 

The GPIO.FIND("LED2") finds the port named under LED2 and its respective settings. Similarly the port and port settings of the other LED and Switches are found.

The objectCreate(testButtonEvent2); creates an event under the name testbuttonEvent2(which is supposed to happen when ever the button2/SW2 is triggered). On this event (OnEvent(testButtonEvent2, (HANDLER) testButtonEventHandler2)) the testbuttoneventhandler2 will be called. Similarly the objectCreate(testButtonEvent3); is set to call the (OnEvent(testButtonEvent2, (HANDLER) testButtonEventHandler3)). Also once the event is defined and eventhandlers are set for SW1, SW2 we need to enable the interrupt for these switches which is done using

// enable interrupts for the button(s)
   GPIO.IFG(SW2, 0);
   GPIO.IEN(SW2, 1);
 
   GPIO.IFG(SW3, 0);
   GPIO.IEN(SW3, 1);

The GPIO.IFG() sets the interrupt flag bit and GPIO.IEN()sets the Interrupt enable bit. Now since all the events are set the system goes to event listening mode. This is done by using WaitEvent(0, 0); which waits for any event(button trigger event in this case).

The testButtonvents are defined using DefineEvent()

Now let us take a look at the testbuttonhandlers.

The testButtonEventHandler2 debugs the event, port and mask details and then toggles the LED2 using GPIO.XOR(). Remember the Interrupt flag and the interrupt enable bit needs to be set again using GPIO.IFG() and GPIO.IEN(). Otherwise the button will not trigger an interrupt again.

DefineEvent(testButtonEvent2);
void testButtonEventHandler2(EVENT e, word port, word mask)
	{
	// called each time the BUTTON is pushed
 
	// print something
	printf("%s(%p, port=%u, mask=%8.8b)\n", __FUNC__, e, port, mask);
 
	// toggle LED2
	GPIO.XOR(LED2);
	// re-enable
	GPIO.IFG(SW2, 0);
	GPIO.IEN(SW2, 1);
	}

Similarly,

The testButtonEventHandler3 debugs the event, port and mask details and then toggles the LED3 using GPIO.XOR(). Remember the Interrupt flag and the interrupt enable bit needs to be set again using GPIO.IFG() and GPIO.IEN(). Otherwise the button will not trigger an interrupt again.

DefineEvent(testButtonEvent3);
void testButtonEventHandler3(EVENT e, word port, word mask)
	{
	// called each time the BUTTON is pushed
 
	// print something
	printf("%s(%p, port=%u, mask=%8.8b)\n", __FUNC__, e, port, mask);
 
	// toggle LED3
	GPIO.XOR(LED3);
 
	// re-enable
	GPIO.IFG(SW3, 0);
	GPIO.IEN(SW3, 1);
	}

We have now accomplished setting up and accessing the switch and LED through I/O ports and controlling them. The tutorial helped you setup two LEDs and two switches. Now you can try setting up and accessing the other switches and LEDs available on the board and observe your results.

More Examples

More Boards

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