SX1509 16 Output I/O Expander / LED Driver Library
SX1509.h@3:ba95b8e850f1, 2014-10-21 (annotated)
- Committer:
- jjones646
- Date:
- Tue Oct 21 14:10:35 2014 +0000
- Revision:
- 3:ba95b8e850f1
- Parent:
- 2:330355f546b0
- Child:
- 5:c64016ec73d3
Adding documentation.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jjones646 | 0:893f387bda9f | 1 | |
jjones646 | 0:893f387bda9f | 2 | |
jjones646 | 0:893f387bda9f | 3 | #ifndef SX1509_H |
jjones646 | 0:893f387bda9f | 4 | #define SX1509_H |
jjones646 | 0:893f387bda9f | 5 | |
jjones646 | 0:893f387bda9f | 6 | #include "mbed.h" |
jjones646 | 0:893f387bda9f | 7 | |
jjones646 | 0:893f387bda9f | 8 | // DEFINES - SX1509 Configuration |
jjones646 | 0:893f387bda9f | 9 | // ==================================================== |
jjones646 | 0:893f387bda9f | 10 | #define SX1509_FREQUENCY 400000 |
jjones646 | 0:893f387bda9f | 11 | #define SX1509_ADDRESS_1 0x3E |
jjones646 | 0:893f387bda9f | 12 | #define SX1509_ADDRESS_2 0x3F |
jjones646 | 0:893f387bda9f | 13 | #define SX1509_ADDRESS_3 0x70 |
jjones646 | 0:893f387bda9f | 14 | #define SX1509_ADDRESS_4 0x71 |
jjones646 | 0:893f387bda9f | 15 | |
jjones646 | 0:893f387bda9f | 16 | // DEFINES - Register Addresses |
jjones646 | 0:893f387bda9f | 17 | // ==================================================== |
jjones646 | 0:893f387bda9f | 18 | #define REGINPUTDISABLEB (0x00) |
jjones646 | 0:893f387bda9f | 19 | #define REGINPUTDISABLEA (0x01) |
jjones646 | 0:893f387bda9f | 20 | #define REGLONGSLEWB (0x02) |
jjones646 | 0:893f387bda9f | 21 | #define REGLONGSLEWA (0x03) |
jjones646 | 0:893f387bda9f | 22 | #define REGLOWDRIVEB (0x04) |
jjones646 | 0:893f387bda9f | 23 | #define REGLOWDRIVEA (0x05) |
jjones646 | 0:893f387bda9f | 24 | #define REGPULLUPB (0x06) |
jjones646 | 0:893f387bda9f | 25 | #define REGPULLUPA (0x07) |
jjones646 | 0:893f387bda9f | 26 | #define REGPULLDOWNB (0x08) |
jjones646 | 0:893f387bda9f | 27 | #define REGPULLDOWNA (0x09) |
jjones646 | 0:893f387bda9f | 28 | #define REGOPENDRAINB (0x0A) |
jjones646 | 0:893f387bda9f | 29 | #define REGOPENDRAINA (0x0B) |
jjones646 | 0:893f387bda9f | 30 | #define REGPOLARITYB (0x0C) |
jjones646 | 0:893f387bda9f | 31 | #define REGPOLARITYA (0x0D) |
jjones646 | 0:893f387bda9f | 32 | #define REGDIRB (0x0E) |
jjones646 | 0:893f387bda9f | 33 | #define REGDIRA (0x0F) |
jjones646 | 0:893f387bda9f | 34 | #define REGDATAB (0x10) |
jjones646 | 0:893f387bda9f | 35 | #define REGDATAA (0x11) |
jjones646 | 0:893f387bda9f | 36 | #define REGINTERRUPTMASKB (0x12) |
jjones646 | 0:893f387bda9f | 37 | #define REGINTERRUPTMASKA (0x13) |
jjones646 | 0:893f387bda9f | 38 | #define REGSENSEHIGHB (0x14) |
jjones646 | 0:893f387bda9f | 39 | #define REGSENSELOWB (0x15) |
jjones646 | 0:893f387bda9f | 40 | #define REGSENSEHIGHA (0x16) |
jjones646 | 0:893f387bda9f | 41 | #define REGSENSELOWA (0x17) |
jjones646 | 0:893f387bda9f | 42 | #define REGINTERRUPTSOURCEB (0x18) |
jjones646 | 0:893f387bda9f | 43 | #define REGINTERRUPTSOURCEA (0x19) |
jjones646 | 0:893f387bda9f | 44 | #define REGEVENTSTATUSB (0x1A) |
jjones646 | 0:893f387bda9f | 45 | #define REGEVENTSTATUSA (0x1B) |
jjones646 | 0:893f387bda9f | 46 | #define REGLEVELSHIFTER1 (0x1C) |
jjones646 | 0:893f387bda9f | 47 | #define REGLEVELSHIFTER2 (0x1D) |
jjones646 | 0:893f387bda9f | 48 | #define REGCLOCK (0x1E) |
jjones646 | 0:893f387bda9f | 49 | #define REGMISC (0x1F) |
jjones646 | 0:893f387bda9f | 50 | #define REGLEDDRIVERENABLEB (0x20) |
jjones646 | 0:893f387bda9f | 51 | #define REGLEDDRIVERENABLEA (0x21) |
jjones646 | 0:893f387bda9f | 52 | // Debounce |
jjones646 | 0:893f387bda9f | 53 | #define REGDEBOUNCECONFIG (0x22) |
jjones646 | 0:893f387bda9f | 54 | #define REGDEBOUNCEENABLEB (0x23) |
jjones646 | 0:893f387bda9f | 55 | #define REGDEBOUNCEENABLEA (0x24) |
jjones646 | 0:893f387bda9f | 56 | #define REGKEYCONFIG1 (0x25) |
jjones646 | 0:893f387bda9f | 57 | #define REGKEYCONFIG2 (0x26) |
jjones646 | 0:893f387bda9f | 58 | #define REGKEYDATA1 (0x27) |
jjones646 | 0:893f387bda9f | 59 | #define REGKEYDATA2 (0x28) |
jjones646 | 0:893f387bda9f | 60 | // LED Driver |
jjones646 | 0:893f387bda9f | 61 | #define REGTON0 (0x29) |
jjones646 | 0:893f387bda9f | 62 | #define REGION0 (0x2A) |
jjones646 | 0:893f387bda9f | 63 | #define REGOFF0 (0x2B) |
jjones646 | 0:893f387bda9f | 64 | #define REGTON1 (0x2C) |
jjones646 | 0:893f387bda9f | 65 | #define REGION1 (0x2D) |
jjones646 | 0:893f387bda9f | 66 | #define REGOFF1 (0x2E) |
jjones646 | 0:893f387bda9f | 67 | #define REGTON2 (0x2F) |
jjones646 | 0:893f387bda9f | 68 | #define REGION2 (0x30) |
jjones646 | 0:893f387bda9f | 69 | #define REGOFF2 (0x31) |
jjones646 | 0:893f387bda9f | 70 | #define REGTON3 (0x32) |
jjones646 | 0:893f387bda9f | 71 | #define REGION3 (0x33) |
jjones646 | 0:893f387bda9f | 72 | #define REGOFF3 (0x34) |
jjones646 | 0:893f387bda9f | 73 | #define REGTON4 (0x35) |
jjones646 | 0:893f387bda9f | 74 | #define REGION4 (0x36) |
jjones646 | 0:893f387bda9f | 75 | #define REGOFF4 (0x37) |
jjones646 | 0:893f387bda9f | 76 | #define REGTRISE4 (0x38) |
jjones646 | 0:893f387bda9f | 77 | #define REGTFALL4 (0x39) |
jjones646 | 0:893f387bda9f | 78 | #define REGTON5 (0x3A) |
jjones646 | 0:893f387bda9f | 79 | #define REGION5 (0x3B) |
jjones646 | 0:893f387bda9f | 80 | #define REGOFF5 (0x3C) |
jjones646 | 0:893f387bda9f | 81 | #define REGTRISE5 (0x3D) |
jjones646 | 0:893f387bda9f | 82 | #define REGTFALL5 (0x3E) |
jjones646 | 0:893f387bda9f | 83 | #define REGTON6 (0x3F) |
jjones646 | 0:893f387bda9f | 84 | #define REGION6 (0x40) |
jjones646 | 0:893f387bda9f | 85 | #define REGOFF6 (0x41) |
jjones646 | 0:893f387bda9f | 86 | #define REGTRISE6 (0x42) |
jjones646 | 0:893f387bda9f | 87 | #define REGTFALL6 (0x43) |
jjones646 | 0:893f387bda9f | 88 | #define REGTON7 (0x44) |
jjones646 | 0:893f387bda9f | 89 | #define REGION7 (0x45) |
jjones646 | 0:893f387bda9f | 90 | #define REGOFF7 (0x46) |
jjones646 | 0:893f387bda9f | 91 | #define REGTRISE7 (0x47) |
jjones646 | 0:893f387bda9f | 92 | #define REGTFALL7 (0x48) |
jjones646 | 0:893f387bda9f | 93 | #define REGTON8 (0x49) |
jjones646 | 0:893f387bda9f | 94 | #define REGION8 (0x4A) |
jjones646 | 0:893f387bda9f | 95 | #define REGOFF8 (0x4B) |
jjones646 | 0:893f387bda9f | 96 | #define REGTON9 (0x4C) |
jjones646 | 0:893f387bda9f | 97 | #define REGION9 (0x4D) |
jjones646 | 0:893f387bda9f | 98 | #define REGOFF9 (0x4E) |
jjones646 | 0:893f387bda9f | 99 | #define REGTON10 (0x4F) |
jjones646 | 0:893f387bda9f | 100 | #define REGION10 (0x50) |
jjones646 | 0:893f387bda9f | 101 | #define REGOFF10 (0x51) |
jjones646 | 0:893f387bda9f | 102 | #define REGTON11 (0x52) |
jjones646 | 0:893f387bda9f | 103 | #define REGION11 (0x53) |
jjones646 | 0:893f387bda9f | 104 | #define REGOFF11 (0x54) |
jjones646 | 0:893f387bda9f | 105 | #define REGTON12 (0x55) |
jjones646 | 0:893f387bda9f | 106 | #define REGION12 (0x56) |
jjones646 | 0:893f387bda9f | 107 | #define REGOFF12 (0x57) |
jjones646 | 0:893f387bda9f | 108 | #define REGTRISE12 (0x58) |
jjones646 | 0:893f387bda9f | 109 | #define REGTFALL12 (0x59) |
jjones646 | 0:893f387bda9f | 110 | #define REGTON13 (0x5A) |
jjones646 | 0:893f387bda9f | 111 | #define REGION13 (0x5B) |
jjones646 | 0:893f387bda9f | 112 | #define REGOFF13 (0x5C) |
jjones646 | 0:893f387bda9f | 113 | #define REGTRISE13 (0x5D) |
jjones646 | 0:893f387bda9f | 114 | #define REGTFALL13 (0x5E) |
jjones646 | 0:893f387bda9f | 115 | #define REGTON14 (0x5F) |
jjones646 | 0:893f387bda9f | 116 | #define REGION14 (0x60) |
jjones646 | 0:893f387bda9f | 117 | #define REGOFF14 (0x61) |
jjones646 | 0:893f387bda9f | 118 | #define REGTRISE14 (0x62) |
jjones646 | 0:893f387bda9f | 119 | #define REGTFALL14 (0x63) |
jjones646 | 0:893f387bda9f | 120 | #define REGTON15 (0x64) |
jjones646 | 0:893f387bda9f | 121 | #define REGION15 (0x65) |
jjones646 | 0:893f387bda9f | 122 | #define REGOFF15 (0x66) |
jjones646 | 0:893f387bda9f | 123 | #define REGTRISE15 (0x67) |
jjones646 | 0:893f387bda9f | 124 | #define REGTFALL15 (0x68) |
jjones646 | 0:893f387bda9f | 125 | // Misc |
jjones646 | 0:893f387bda9f | 126 | #define REGHIGHINPUTB (0x69) |
jjones646 | 0:893f387bda9f | 127 | #define REGHIGHINPUTA (0x6A) |
jjones646 | 0:893f387bda9f | 128 | // Software Reset |
jjones646 | 0:893f387bda9f | 129 | #define REGRESET (0x7D) |
jjones646 | 0:893f387bda9f | 130 | // Test Registers (do not write) |
jjones646 | 0:893f387bda9f | 131 | #define REGTEST1 (0x7E) |
jjones646 | 0:893f387bda9f | 132 | #define REGTEST2 (0x7F) |
jjones646 | 0:893f387bda9f | 133 | |
jjones646 | 0:893f387bda9f | 134 | |
jjones646 | 0:893f387bda9f | 135 | // DEFINES - Class Specific |
jjones646 | 0:893f387bda9f | 136 | // ==================================================== |
jjones646 | 0:893f387bda9f | 137 | #define BUFFER_SIZE (8) |
jjones646 | 0:893f387bda9f | 138 | |
jjones646 | 1:9ab20d13c44e | 139 | |
jjones646 | 1:9ab20d13c44e | 140 | // ENUMERATIONS |
jjones646 | 1:9ab20d13c44e | 141 | // ==================================================== |
jjones646 | 0:893f387bda9f | 142 | enum Direction_t { |
jjones646 | 0:893f387bda9f | 143 | OUT = 0, |
jjones646 | 0:893f387bda9f | 144 | IN |
jjones646 | 0:893f387bda9f | 145 | }; |
jjones646 | 0:893f387bda9f | 146 | |
jjones646 | 0:893f387bda9f | 147 | enum Side_t { |
jjones646 | 0:893f387bda9f | 148 | A = 0, |
jjones646 | 0:893f387bda9f | 149 | B |
jjones646 | 0:893f387bda9f | 150 | }; |
jjones646 | 0:893f387bda9f | 151 | |
jjones646 | 0:893f387bda9f | 152 | enum Transfer_t { |
jjones646 | 0:893f387bda9f | 153 | WRITE = 0, |
jjones646 | 0:893f387bda9f | 154 | READ |
jjones646 | 0:893f387bda9f | 155 | }; |
jjones646 | 0:893f387bda9f | 156 | |
jjones646 | 0:893f387bda9f | 157 | enum State_t { |
jjones646 | 0:893f387bda9f | 158 | OFF = 0x00, |
jjones646 | 0:893f387bda9f | 159 | ON = 0xFF |
jjones646 | 0:893f387bda9f | 160 | }; |
jjones646 | 0:893f387bda9f | 161 | |
jjones646 | 0:893f387bda9f | 162 | enum Pull_t { |
jjones646 | 0:893f387bda9f | 163 | PULLUP = 0, |
jjones646 | 0:893f387bda9f | 164 | PULLDOWN |
jjones646 | 0:893f387bda9f | 165 | }; |
jjones646 | 0:893f387bda9f | 166 | |
jjones646 | 0:893f387bda9f | 167 | enum Freq_t { |
jjones646 | 0:893f387bda9f | 168 | SLOW = 0, |
jjones646 | 0:893f387bda9f | 169 | MED = 1, |
jjones646 | 0:893f387bda9f | 170 | FAST = 2 |
jjones646 | 0:893f387bda9f | 171 | }; |
jjones646 | 0:893f387bda9f | 172 | |
jjones646 | 0:893f387bda9f | 173 | enum LED_t { |
jjones646 | 0:893f387bda9f | 174 | IO4 = REGTON4, |
jjones646 | 0:893f387bda9f | 175 | IO5 = REGTON5, |
jjones646 | 0:893f387bda9f | 176 | IO6 = REGTON6, |
jjones646 | 0:893f387bda9f | 177 | IO7 = REGTON7, |
jjones646 | 0:893f387bda9f | 178 | IO12 = REGTON12, |
jjones646 | 0:893f387bda9f | 179 | IO13 = REGTON13, |
jjones646 | 0:893f387bda9f | 180 | IO14 = REGTON14, |
jjones646 | 0:893f387bda9f | 181 | IO15 = REGTON15 |
jjones646 | 0:893f387bda9f | 182 | }; |
jjones646 | 0:893f387bda9f | 183 | |
jjones646 | 0:893f387bda9f | 184 | // CLASS DEFINITION |
jjones646 | 0:893f387bda9f | 185 | // ==================================================== |
jjones646 | 3:ba95b8e850f1 | 186 | /** Class definition for the SX1509 I/O expander IC |
jjones646 | 3:ba95b8e850f1 | 187 | */ |
jjones646 | 0:893f387bda9f | 188 | class SX1509 |
jjones646 | 0:893f387bda9f | 189 | { |
jjones646 | 0:893f387bda9f | 190 | private: |
jjones646 | 0:893f387bda9f | 191 | Direction_t dirA, dirB; |
jjones646 | 0:893f387bda9f | 192 | char buf[BUFFER_SIZE]; |
jjones646 | 0:893f387bda9f | 193 | |
jjones646 | 0:893f387bda9f | 194 | protected: |
jjones646 | 0:893f387bda9f | 195 | DigitalIn *_intN; |
jjones646 | 0:893f387bda9f | 196 | DigitalOut *_resetN; |
jjones646 | 0:893f387bda9f | 197 | I2C *_i2c; |
jjones646 | 0:893f387bda9f | 198 | bool hasInt, hasReset; |
jjones646 | 0:893f387bda9f | 199 | |
jjones646 | 0:893f387bda9f | 200 | void init(PinName, PinName, PinName, PinName); |
jjones646 | 0:893f387bda9f | 201 | void direction(Direction_t, Side_t); |
jjones646 | 0:893f387bda9f | 202 | int transfer_data(char const, char const, Transfer_t); |
jjones646 | 3:ba95b8e850f1 | 203 | void write_register(char const, char const); |
jjones646 | 3:ba95b8e850f1 | 204 | int read_register(char const); |
jjones646 | 3:ba95b8e850f1 | 205 | void setBuffer(Side_t, State_t); |
jjones646 | 3:ba95b8e850f1 | 206 | void setPull(Side_t, Pull_t, State_t); |
jjones646 | 3:ba95b8e850f1 | 207 | void setOpenDrain(Side_t, State_t); |
jjones646 | 3:ba95b8e850f1 | 208 | void setClock(State_t); |
jjones646 | 3:ba95b8e850f1 | 209 | void setFreq(Freq_t); |
jjones646 | 3:ba95b8e850f1 | 210 | void setLedDrive(Side_t, State_t); |
jjones646 | 0:893f387bda9f | 211 | |
jjones646 | 0:893f387bda9f | 212 | public: |
jjones646 | 0:893f387bda9f | 213 | |
jjones646 | 0:893f387bda9f | 214 | // Constructors |
jjones646 | 0:893f387bda9f | 215 | SX1509(); |
jjones646 | 0:893f387bda9f | 216 | SX1509(PinName sda); |
jjones646 | 2:330355f546b0 | 217 | |
jjones646 | 2:330355f546b0 | 218 | /** Create a new SX1509 object |
jjones646 | 2:330355f546b0 | 219 | */ |
jjones646 | 0:893f387bda9f | 220 | SX1509(PinName sda, PinName scl, PinName intN = NC, PinName resetN = NC); |
jjones646 | 0:893f387bda9f | 221 | |
jjones646 | 3:ba95b8e850f1 | 222 | /// Set the direction for pins 0 to 7 |
jjones646 | 0:893f387bda9f | 223 | void directionA(Direction_t dir); |
jjones646 | 3:ba95b8e850f1 | 224 | |
jjones646 | 3:ba95b8e850f1 | 225 | /// Set the direction for pins 8 to 15 |
jjones646 | 0:893f387bda9f | 226 | void directionB(Direction_t dir); |
jjones646 | 3:ba95b8e850f1 | 227 | |
jjones646 | 3:ba95b8e850f1 | 228 | /// Write an 8-bit value to the pins corresponding to 0 to 7 |
jjones646 | 0:893f387bda9f | 229 | void setA(char const); |
jjones646 | 3:ba95b8e850f1 | 230 | |
jjones646 | 3:ba95b8e850f1 | 231 | /// Write an 8-bit value to the pins corresponding to 8 to 15 |
jjones646 | 0:893f387bda9f | 232 | void setB(char const); |
jjones646 | 3:ba95b8e850f1 | 233 | |
jjones646 | 3:ba95b8e850f1 | 234 | /// Read the 8-bit value for pins corresponding to 0 to 7 |
jjones646 | 0:893f387bda9f | 235 | int readA(); |
jjones646 | 3:ba95b8e850f1 | 236 | |
jjones646 | 3:ba95b8e850f1 | 237 | /// Read the 8-bit value for pins corresponding to 0 to 7 |
jjones646 | 0:893f387bda9f | 238 | int readB(); |
jjones646 | 3:ba95b8e850f1 | 239 | |
jjones646 | 3:ba95b8e850f1 | 240 | /// Read a single bit from a pin's state |
jjones646 | 0:893f387bda9f | 241 | bool get(int const); |
jjones646 | 3:ba95b8e850f1 | 242 | |
jjones646 | 3:ba95b8e850f1 | 243 | /// Set a single bit for a pin's state |
jjones646 | 0:893f387bda9f | 244 | void set(int const); |
jjones646 | 3:ba95b8e850f1 | 245 | |
jjones646 | 3:ba95b8e850f1 | 246 | /// Clear a single bit for a pin's state |
jjones646 | 0:893f387bda9f | 247 | void clear(int const); |
jjones646 | 3:ba95b8e850f1 | 248 | |
jjones646 | 3:ba95b8e850f1 | 249 | /// Toggle the state of a pin |
jjones646 | 0:893f387bda9f | 250 | void toggle(int const); |
jjones646 | 3:ba95b8e850f1 | 251 | |
jjones646 | 3:ba95b8e850f1 | 252 | /// Returns the direction that the pins for A or B are set to (if unamious) |
jjones646 | 0:893f387bda9f | 253 | Direction_t getDirection(Side_t); |
jjones646 | 3:ba95b8e850f1 | 254 | |
jjones646 | 3:ba95b8e850f1 | 255 | /// Set the state for the pins for A or B to off |
jjones646 | 0:893f387bda9f | 256 | void clearAll(Side_t); |
jjones646 | 3:ba95b8e850f1 | 257 | |
jjones646 | 3:ba95b8e850f1 | 258 | /// Used for configuring outputs for driving LEDs on pins for A or B |
jjones646 | 0:893f387bda9f | 259 | void enableLED(Side_t); |
jjones646 | 3:ba95b8e850f1 | 260 | |
jjones646 | 3:ba95b8e850f1 | 261 | /// The method used for controlling how an LED is controlled by the SX1509 |
jjones646 | 0:893f387bda9f | 262 | void setupLED(LED_t); |
jjones646 | 1:9ab20d13c44e | 263 | |
jjones646 | 0:893f387bda9f | 264 | }; |
jjones646 | 1:9ab20d13c44e | 265 | |
jjones646 | 0:893f387bda9f | 266 | #endif |