Replacement for regular GPIO (DigitalIn, DigitalOut, DigitalInOut) classes which has superior speed.

Fork of FastIO by Erik -

Committer:
davidr99
Date:
Tue Oct 24 03:00:41 2017 +0000
Revision:
23:929068181dea
Parent:
21:a1dfa6c65400
Changed Ticker to only send 1 line at a time but interupt every 15us

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:d394ebd01052 1 #ifndef __FAST_IO_H
Sissors 0:d394ebd01052 2 #define __FAST_IO_H
Sissors 0:d394ebd01052 3
Sissors 0:d394ebd01052 4 #include "FastIO_LPC1768.h"
Sissors 0:d394ebd01052 5 #include "FastIO_LPC11UXX.h"
nameless129 18:c95920122b2e 6 #include "FastIO_LPC11U6X.h"
Sissors 1:85a4a54f15e3 7 #include "FastIO_LPC81X.h"
Sissors 0:d394ebd01052 8 #include "FastIO_KLXX.h"
Sissors 10:bb22a3dbedb4 9 #include "FastIO_K20D50M_KPSDK.h"
Sissors 13:0e21ffc6cb84 10 #include "FastIO_STM32F4.h"
viquiram 21:a1dfa6c65400 11 #include "FastIO_STM32L073XX.h"
Sissors 6:da3730030c07 12 #include "FastIO_NUCLEO_F030.h"
Sissors 7:1e784ae11fba 13 #include "FastIO_LPC11XX.h"
Sissors 15:e0c5a5216647 14 #include "FastIO_EFM32.h"
nameless129 19:89d6ae484a35 15 #include "FastIO_LPC43XX.h"
Sissors 20:ed72895a2402 16 #include "FastIO_NRF51822.h"
davidr99 23:929068181dea 17 #include "FastIO_STM32F767ZI.h"
Sissors 0:d394ebd01052 18
Sissors 0:d394ebd01052 19 #ifndef INIT_PIN
Sissors 2:1a6ed4b84590 20 #warning Target is not supported by FastIO
Sissors 2:1a6ed4b84590 21 #warning Reverting to regular DigitalInOut
Sissors 2:1a6ed4b84590 22 #include "FastIO_Unsupported.h"
Sissors 0:d394ebd01052 23 #endif
Sissors 0:d394ebd01052 24
Sissors 0:d394ebd01052 25 #include "mbed.h"
Sissors 0:d394ebd01052 26
Sissors 0:d394ebd01052 27 /**
Sissors 0:d394ebd01052 28 * Faster alternative compared to regular DigitalInOut
Sissors 0:d394ebd01052 29 *
Sissors 0:d394ebd01052 30 * Except the constructor it is compatible with regular DigitalInOut.
Sissors 0:d394ebd01052 31 * Code is based on Igor Skochinsky's code (http://mbed.org/users/igorsk/code/FastIO/)
Sissors 0:d394ebd01052 32 */
Sissors 0:d394ebd01052 33 template <PinName pin> class FastInOut
Sissors 0:d394ebd01052 34 {
Sissors 0:d394ebd01052 35 public:
Sissors 0:d394ebd01052 36 /**
Sissors 0:d394ebd01052 37 * Construct new FastInOut object
Sissors 0:d394ebd01052 38 *
Sissors 0:d394ebd01052 39 * @code
Sissors 0:d394ebd01052 40 * FastInOut<LED1> led1;
Sissors 0:d394ebd01052 41 * @endcode
Sissors 0:d394ebd01052 42 *
Sissors 0:d394ebd01052 43 * No initialization is done regarding input/output mode,
Sissors 0:d394ebd01052 44 * FastIn/FastOut can be used if that is required
Sissors 0:d394ebd01052 45 *
Sissors 0:d394ebd01052 46 * @param pin pin the FastOut object should be used for
Sissors 0:d394ebd01052 47 */
Sissors 0:d394ebd01052 48 FastInOut() {
Sissors 0:d394ebd01052 49 INIT_PIN;
Sissors 0:d394ebd01052 50 }
Sissors 2:1a6ed4b84590 51
Sissors 2:1a6ed4b84590 52 ~FastInOut() {
Sissors 2:1a6ed4b84590 53 DESTROY_PIN;
Sissors 2:1a6ed4b84590 54 }
Sissors 0:d394ebd01052 55
Sissors 0:d394ebd01052 56 void write(int value) {
Sissors 0:d394ebd01052 57 if ( value )
Sissors 0:d394ebd01052 58 WRITE_PIN_SET;
Sissors 0:d394ebd01052 59 else
Sissors 0:d394ebd01052 60 WRITE_PIN_CLR;
Sissors 0:d394ebd01052 61 }
Sissors 0:d394ebd01052 62 int read() {
Sissors 0:d394ebd01052 63 return READ_PIN;
Sissors 0:d394ebd01052 64 }
Sissors 0:d394ebd01052 65
Sissors 0:d394ebd01052 66 void mode(PinMode pull) {
Sissors 0:d394ebd01052 67 SET_MODE(pull);
Sissors 0:d394ebd01052 68 }
Sissors 0:d394ebd01052 69
Sissors 0:d394ebd01052 70 void output() {
Sissors 0:d394ebd01052 71 SET_DIR_OUTPUT;
Sissors 0:d394ebd01052 72 }
Sissors 0:d394ebd01052 73
Sissors 0:d394ebd01052 74 void input() {
Sissors 0:d394ebd01052 75 SET_DIR_INPUT;
Sissors 0:d394ebd01052 76 }
Sissors 0:d394ebd01052 77
Sissors 0:d394ebd01052 78 FastInOut& operator= (int value) {
Sissors 0:d394ebd01052 79 write(value);
Sissors 0:d394ebd01052 80 return *this;
Sissors 0:d394ebd01052 81 };
Sissors 0:d394ebd01052 82 FastInOut& operator= (FastInOut& rhs) {
Sissors 0:d394ebd01052 83 return write(rhs.read());
Sissors 0:d394ebd01052 84 };
Sissors 0:d394ebd01052 85 operator int() {
Sissors 0:d394ebd01052 86 return read();
Sissors 0:d394ebd01052 87 };
Sissors 0:d394ebd01052 88
Sissors 15:e0c5a5216647 89 protected:
Sissors 0:d394ebd01052 90 fastio_vars container;
Sissors 0:d394ebd01052 91 };
Sissors 0:d394ebd01052 92
Sissors 0:d394ebd01052 93 /**
Sissors 0:d394ebd01052 94 * Faster alternative compared to regular DigitalOut
Sissors 0:d394ebd01052 95 *
Sissors 0:d394ebd01052 96 * Except the constructor it is compatible with regular DigitalOut. Aditionally all
Sissors 0:d394ebd01052 97 * functions from DigitalInOut are also available (only initialization is different)
Sissors 0:d394ebd01052 98 * Code is based on Igor Skochinsky's code (http://mbed.org/users/igorsk/code/FastIO/)
Sissors 0:d394ebd01052 99 */
Sissors 0:d394ebd01052 100 template <PinName pin, int initial = 0> class FastOut : public FastInOut<pin>
Sissors 0:d394ebd01052 101 {
Sissors 0:d394ebd01052 102 public:
Sissors 0:d394ebd01052 103 /**
Sissors 0:d394ebd01052 104 * Construct new FastOut object
Sissors 0:d394ebd01052 105 *
Sissors 0:d394ebd01052 106 * @code
Sissors 0:d394ebd01052 107 * FastOut<LED1> led1;
Sissors 0:d394ebd01052 108 * @endcode
Sissors 0:d394ebd01052 109 *
Sissors 0:d394ebd01052 110 * @param pin pin the FastOut object should be used for
Sissors 0:d394ebd01052 111 * @param initial (optional) initial state of the pin after construction: default is 0 (low)
Sissors 0:d394ebd01052 112 */
Sissors 12:973e253323c9 113 FastOut() : FastInOut<pin>::FastInOut() {
Sissors 0:d394ebd01052 114 write(initial);
Sissors 0:d394ebd01052 115 SET_DIR_OUTPUT;
Sissors 0:d394ebd01052 116 }
Sissors 0:d394ebd01052 117
Sissors 0:d394ebd01052 118 FastOut& operator= (int value) {
Sissors 0:d394ebd01052 119 this->write(value);
Sissors 0:d394ebd01052 120 return *this;
Sissors 0:d394ebd01052 121 };
Sissors 0:d394ebd01052 122 FastOut& operator= (FastOut& rhs) {
Sissors 0:d394ebd01052 123 return this->write(rhs.read());
Sissors 0:d394ebd01052 124 };
Sissors 0:d394ebd01052 125 operator int() {
Sissors 0:d394ebd01052 126 return this->read();
Sissors 0:d394ebd01052 127 };
Sissors 0:d394ebd01052 128 };
Sissors 0:d394ebd01052 129
Sissors 0:d394ebd01052 130 /**
Sissors 0:d394ebd01052 131 * Faster alternative compared to regular DigitalIn
Sissors 0:d394ebd01052 132 *
Sissors 0:d394ebd01052 133 * Except the constructor it is compatible with regular DigitalIn. Aditionally all
Sissors 0:d394ebd01052 134 * functions from DigitalInOut are also available (only initialization is different)
Sissors 0:d394ebd01052 135 * Code is based on Igor Skochinsky's code (http://mbed.org/users/igorsk/code/FastIO/)
Sissors 0:d394ebd01052 136 */
Sissors 8:b0d725519c4f 137 template <PinName pin, PinMode pinmode = PullDefault> class FastIn : public FastInOut<pin>
Sissors 0:d394ebd01052 138 {
Sissors 0:d394ebd01052 139 public:
Sissors 0:d394ebd01052 140 /**
Sissors 0:d394ebd01052 141 * Construct new FastIn object
Sissors 0:d394ebd01052 142 *
Sissors 0:d394ebd01052 143 * @code
Sissors 0:d394ebd01052 144 * FastIn<LED1> led1;
Sissors 0:d394ebd01052 145 * @endcode
Sissors 0:d394ebd01052 146 *
Sissors 0:d394ebd01052 147 * @param pin pin the FastIn object should be used for
Sissors 8:b0d725519c4f 148 * @param pinmode (optional) initial mode of the pin after construction: default is PullDefault
Sissors 0:d394ebd01052 149 */
Sissors 12:973e253323c9 150 FastIn() : FastInOut<pin>::FastInOut() {
Sissors 8:b0d725519c4f 151 SET_MODE(pinmode);
Sissors 0:d394ebd01052 152 SET_DIR_INPUT;
Sissors 0:d394ebd01052 153 }
Sissors 0:d394ebd01052 154
Sissors 0:d394ebd01052 155 FastIn& operator= (int value) {
Sissors 0:d394ebd01052 156 this->write(value);
Sissors 0:d394ebd01052 157 return *this;
Sissors 0:d394ebd01052 158 };
Sissors 0:d394ebd01052 159 FastIn& operator= (FastIn& rhs) {
Sissors 0:d394ebd01052 160 return this->write(rhs.read());
Sissors 0:d394ebd01052 161 };
Sissors 0:d394ebd01052 162 operator int() {
Sissors 0:d394ebd01052 163 return this->read();
Sissors 0:d394ebd01052 164 };
Sissors 0:d394ebd01052 165 };
Sissors 0:d394ebd01052 166
Sissors 0:d394ebd01052 167 #endif