main.cpp 3.26 KB
Newer Older
Martin Horauer's avatar
Martin Horauer committed
1
2
3
4
5
6
7
8
9
10
11
12
/* mbed Example Program
 * Copyright (c) 2006-2014 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Martin Horauer's avatar
Martin Horauer committed
13
14
 * See the License for the specific language governing permissions limitations
 * and under the License.
Martin Horauer's avatar
Martin Horauer committed
15
16
17
18
19
20
21
 */

/******************************************************************* Includes */
#include "mbed.h"
#include "rtos.h"

/***************************************************************** Prototypes */
22
23
24
25
26
/* create an object of class InterruptIn: "sw"
   see drivers/InterruptIn.h
   SW2 is defined in
   targets/TARGET_Infineon/TARGET_XMC4XXX/TARGET_XMC4500/PinNames.h
*/
Martin Horauer's avatar
Martin Horauer committed
27
InterruptIn sw (SW2);       // BUTTON2 = P1.15
28
29
30
31
32
/* create an object of class DigitalOut: "led1"
   see drivers/DigitalOut.h
   LED1 is defined in
   targets/TARGET_Infineon/TARGET_XMC4XXX/TARGET_XMC4500/PinNames.h
*/
Martin Horauer's avatar
Martin Horauer committed
33
DigitalOut led1 (LED1);     // LED1 = P1.1
34
35
36
37
38
/* create an object of class Serial: "device"
   see drivers/Serial.h
   P0_1 and P0_0 are defined in
   targets/TARGET_Infineon/TARGET_XMC4XXX/TARGET_XMC4500/PinNames.h
*/
Martin Horauer's avatar
Martin Horauer committed
39
40
41
42
43
44
45
46
Serial device (P0_1, P0_0); // UART1 -> Tx = P0.1, Rx = P0.0

/****************************************************************** Callbacks */
/**
 * Callback if rising edge on P1.15 is detected
 */
void rise_handler (void)
{
47
48
49
50
	/* ovverride the = operator using "operator ="
	   see drivers/DigitalOut.h
	   -> toggle LED1
	*/
Martin Horauer's avatar
Martin Horauer committed
51
52
53
54
55
56
57
58
	led1 = !led1;
}

/**
 * Callback if falling edge on P1.15 is detected
 */
void fall_handler (void)
{
59
60
61
62
	/* output a message via UART1, in partzicular the thread id is output
	   using the .gettid() method of the current thread
	   see rtos/Thread.cpp
	*/
Martin Horauer's avatar
Martin Horauer committed
63
	device.printf ("fall_handler in context %p\r\n", Thread::gettid());
64
	/* toggle LED1 */
Martin Horauer's avatar
Martin Horauer committed
65
66
67
68
69
70
71
72
73
	led1 = !led1;
}

/****************************************************************** Functions */
/**
 * Main Function
 */
int main (void)
{
74
75
76
	/* request the shared queue to the OS
	   see events mbed_event_queue.cpp
	*/
Martin Horauer's avatar
Martin Horauer committed
77
	EventQueue *queue = mbed_event_queue();
78
79
80
81
82
	/* output a message via the device (here UART1) to that end the .printf()
	   method of platform/Stream.h is invoked, the function will output the
	   thread id of the main thread using the .gettid() method
	   see rtos/Thread.cpp
	*/
Martin Horauer's avatar
Martin Horauer committed
83
	device.printf ("Starting in context %p\r\n", Thread::gettid());
84
85
86
87
88
	/* the 'rise_handler()' callback function will execute in the IRQ context
	   it is installed using the method .rise() and activated when a rising edge
	   occurs on the input
	   see drivers/InterruptIn.h
	*/
Martin Horauer's avatar
Martin Horauer committed
89
	sw.rise (rise_handler);
90
91
92
93
94
95
96
	/* The 'fall_handler()' callback will execute in the context of the shared
	   queue thread - it is installed using the .fall() method see
	   drivers/InterruptIn.h and activated when a falling edge on the input
	   is detected - whenever this occurs it is put into a general queue to
	   the OS see events/mbed_shared_queues.h
	   the fall_handler() will execute as a separate thread
	*/
Martin Horauer's avatar
Martin Horauer committed
97
98
99
	sw.fall (queue->event (fall_handler));
}
/*! EOF */