aboutsummaryrefslogtreecommitdiffstats
path: root/source/external-interrupts.rst
blob: bc9d6cd543bd5639b6b16d40890890c83f68a652 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
.. highlight:: cpp

.. _external-interrupts:

External Interrupts
===================

External interrupts can be used to trigger routines to run in response
to changes in voltage on a pin. Each GPIO pin on the Maple can be used
to detect transitions such as when the voltage goes from low to high,
or from high to low. This technique can be used to avoid unnecessary
polling of the state of a pin.

.. _contents: Contents
   :local:


Overview
--------

External interrupts are often used to detect when events happen
outside of the microcontroller. These can be used to tell the Maple
when events happen, such as when a sensor has data ready to be read,
or when a button has been pushed. When such an event happens, an
interrupt is raised and the Maple can react to it with a preset
*interrupt handler*.

Every GPIO pin on the Maple can be used as an external interrupt,
subject to certain constraints; there can be a maximum of 16 different
external interrupts set up at a time on the Maple. This is because the
external interrupt lines on the STM32 are multiplexed between GPIO
ports. In effect, this means that every pin on the Maple maps to a
certain EXTI line, and within that EXTI line, only one of the pins
that maps to it can be used as an external interrupt at a time.

The following table shows which pins can be used on which lines.

.. list-table::
   :widths: 1 1
   :header-rows: 1

   * - EXTI Line
     - Maple pins
   * - EXTI0
     - 2, 15, 27
   * - EXTI1
     - 3, 16, 28
   * - EXTI2
     - 1, 17, 25
   * - EXTI3
     - 0, 18
   * - EXTI4
     - 10, 19
   * - EXTI5
     - 4, 13, 20
   * - EXTI6
     - 5, 12, 35
   * - EXTI7
     - 9, 11, 36
   * - EXTI8
     - 6, 14, 37
   * - EXTI9
     - 7, 25, 28
   * - EXTI10
     - 8, 26, 29
   * - EXTI11
     - 30
   * - EXTI12
     - 31
   * - EXTI13
     - 21, 32
   * - EXTI14
     - 22, 33
   * - EXTI15
     - 23, 34

.. note::

   You should set the :ref:`pin mode <arduino-pinmode>` of your
   desired pin to an input mode (e.g ``INPUT`` or ``INPUT_FLOATING``,
   ``INPUT_PULLUP``, ``INPUT_PULLDOWN``).


Function Reference
------------------

.. doxygenfunction:: attachInterrupt

.. doxygenfunction:: detachInterrupt

.. doxygenenum:: ExtIntTriggerMode

.. doxygentypedef:: voidFuncPtr


Code example
------------

Blink the LED on every transition::

    int pin = 13;
    volatile int state = LOW;

    void setup() {
      pinMode(pin, OUTPUT);
      pinMode(0, INPUT_FLOATING);
      attachInterrupt(0, blink, CHANGE);
    }

    void loop() {
      digitalWrite(pin, state);
    }

    void blink() {
      state = !state;
    }


Recommended Reading
-------------------

* STMicro documentation for STM32F103RB microcontroller:

      * `All <http://www.st.com/mcu/devicedocs-STM32F103RB-110.html>`_
      * `Datasheet <http://www.st.com/stonline/products/literature/ds/13587.pdf>`_ (pdf)
      * `Reference Manual <http://www.st.com/stonline/products/literature/rm/13902.pdf>`_ (pdf)