Command processor to access I2C and SPI Takes URI coded commands and returns JSON array
Fork of SerialInterface by
SerialInterface.cpp@5:9e27e2a46fa6, 2016-12-14 (annotated)
- Committer:
- switches
- Date:
- Wed Dec 14 19:52:31 2016 +0000
- Revision:
- 5:9e27e2a46fa6
- Parent:
- 4:0bd9ec504040
- Child:
- 6:c9b7256c8261
Added dio GPIO function and configuration options for SPI
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gsteiert | 0:828bfd94972b | 1 | /* Pmod Interface Library |
gsteiert | 0:828bfd94972b | 2 | * |
gsteiert | 0:828bfd94972b | 3 | */ |
gsteiert | 0:828bfd94972b | 4 | |
gsteiert | 0:828bfd94972b | 5 | #include "mbed.h" |
switches | 2:3f6a8ac111a9 | 6 | #include "SerialInterface.h" |
gsteiert | 0:828bfd94972b | 7 | |
switches | 2:3f6a8ac111a9 | 8 | SerialInterface::SerialInterface() |
gsteiert | 0:828bfd94972b | 9 | { |
gsteiert | 0:828bfd94972b | 10 | } |
gsteiert | 0:828bfd94972b | 11 | |
switches | 2:3f6a8ac111a9 | 12 | SerialInterface::~SerialInterface() |
gsteiert | 0:828bfd94972b | 13 | { |
gsteiert | 0:828bfd94972b | 14 | } |
gsteiert | 0:828bfd94972b | 15 | |
switches | 2:3f6a8ac111a9 | 16 | // Initialize SerialInterface |
switches | 5:9e27e2a46fa6 | 17 | void SerialInterface::init(I2C* i2c, SPI* spi, DigitalInOut* gpio) |
gsteiert | 0:828bfd94972b | 18 | { |
switches | 2:3f6a8ac111a9 | 19 | _i2c = i2c; // save pointer to I2C interface |
switches | 2:3f6a8ac111a9 | 20 | _spi = spi; // save pointer to SPI interface |
switches | 5:9e27e2a46fa6 | 21 | _gpio = gpio; // save pointer to GPIO pins |
switches | 5:9e27e2a46fa6 | 22 | } |
switches | 5:9e27e2a46fa6 | 23 | |
switches | 5:9e27e2a46fa6 | 24 | /* Digital I/O |
switches | 5:9e27e2a46fa6 | 25 | * /d read all |
switches | 5:9e27e2a46fa6 | 26 | * /d/[pin] read from pin |
switches | 5:9e27e2a46fa6 | 27 | * /d/[pin]/[cfg] write configuration to pin |
switches | 5:9e27e2a46fa6 | 28 | * [pin] = number (from 0 to 7) of the pin to access |
switches | 5:9e27e2a46fa6 | 29 | * [cfg] = pin configuration: |
switches | 5:9e27e2a46fa6 | 30 | * 0 - output low |
switches | 5:9e27e2a46fa6 | 31 | * 1 - output high |
switches | 5:9e27e2a46fa6 | 32 | * 2 - input pull down |
switches | 5:9e27e2a46fa6 | 33 | * 3 - input pull up |
switches | 5:9e27e2a46fa6 | 34 | * 4 - input pull none |
switches | 5:9e27e2a46fa6 | 35 | */ |
switches | 5:9e27e2a46fa6 | 36 | void SerialInterface::fnc_dio(char* resp) |
switches | 5:9e27e2a46fa6 | 37 | { |
switches | 5:9e27e2a46fa6 | 38 | int bdat; |
switches | 5:9e27e2a46fa6 | 39 | int bcnt; |
switches | 5:9e27e2a46fa6 | 40 | switch (_args[0]) { |
switches | 5:9e27e2a46fa6 | 41 | case 0: |
switches | 5:9e27e2a46fa6 | 42 | sprintf(resp, "["); |
switches | 5:9e27e2a46fa6 | 43 | resp +=1; |
switches | 5:9e27e2a46fa6 | 44 | for (bcnt = 0; bcnt < 8 ; bcnt++) { |
switches | 5:9e27e2a46fa6 | 45 | bdat = _gpio[bcnt].read(); |
switches | 5:9e27e2a46fa6 | 46 | sprintf(resp, "%d,", bdat); |
switches | 5:9e27e2a46fa6 | 47 | resp +=2; |
switches | 5:9e27e2a46fa6 | 48 | } |
switches | 5:9e27e2a46fa6 | 49 | sprintf((resp-1), "]"); |
switches | 5:9e27e2a46fa6 | 50 | break; |
switches | 5:9e27e2a46fa6 | 51 | case 1: |
switches | 5:9e27e2a46fa6 | 52 | if (_args[1] > 7) { |
switches | 5:9e27e2a46fa6 | 53 | sprintf(resp, "[-1]"); |
switches | 5:9e27e2a46fa6 | 54 | } else { |
switches | 5:9e27e2a46fa6 | 55 | sprintf(resp,"[%d]", _gpio[_args[1]].read()); |
switches | 5:9e27e2a46fa6 | 56 | } |
switches | 5:9e27e2a46fa6 | 57 | break; |
switches | 5:9e27e2a46fa6 | 58 | case 2: |
switches | 5:9e27e2a46fa6 | 59 | if (_args[1] > 7) { |
switches | 5:9e27e2a46fa6 | 60 | sprintf(resp, "[-1]"); |
switches | 5:9e27e2a46fa6 | 61 | } else { |
switches | 5:9e27e2a46fa6 | 62 | if (_args[2] < 2) { |
switches | 5:9e27e2a46fa6 | 63 | _gpio[_args[1]].write(_args[2]); |
switches | 5:9e27e2a46fa6 | 64 | _gpio[_args[1]].output(); |
switches | 5:9e27e2a46fa6 | 65 | } else { |
switches | 5:9e27e2a46fa6 | 66 | if (_args[2] == 2) { |
switches | 5:9e27e2a46fa6 | 67 | _gpio[_args[1]].mode(PullDown); |
switches | 5:9e27e2a46fa6 | 68 | } else if (_args[2] == 3) { |
switches | 5:9e27e2a46fa6 | 69 | _gpio[_args[1]].mode(PullUp); |
switches | 5:9e27e2a46fa6 | 70 | } else { |
switches | 5:9e27e2a46fa6 | 71 | _gpio[_args[1]].mode(PullNone); |
switches | 5:9e27e2a46fa6 | 72 | } |
switches | 5:9e27e2a46fa6 | 73 | _gpio[_args[1]].input(); |
switches | 5:9e27e2a46fa6 | 74 | } |
switches | 5:9e27e2a46fa6 | 75 | sprintf(resp,"[%d]", _args[2]); |
switches | 5:9e27e2a46fa6 | 76 | } |
switches | 5:9e27e2a46fa6 | 77 | break; |
switches | 5:9e27e2a46fa6 | 78 | default: |
switches | 5:9e27e2a46fa6 | 79 | sprintf(resp, "[-1]"); |
switches | 5:9e27e2a46fa6 | 80 | break; |
switches | 5:9e27e2a46fa6 | 81 | } |
gsteiert | 0:828bfd94972b | 82 | } |
gsteiert | 0:828bfd94972b | 83 | |
gsteiert | 0:828bfd94972b | 84 | /* I2C |
gsteiert | 0:828bfd94972b | 85 | * /i/[even]/[data]... write |
gsteiert | 0:828bfd94972b | 86 | * /i/[odd]/[cnt]/[data]... read |
gsteiert | 0:828bfd94972b | 87 | * [even] = even I2C address used for writes |
gsteiert | 0:828bfd94972b | 88 | * [odd] = odd I2C address used for reads |
gsteiert | 0:828bfd94972b | 89 | * [data] = data to be writen, if data is included with a read, the data |
gsteiert | 0:828bfd94972b | 90 | * will be written prior to the read to set the register address |
gsteiert | 0:828bfd94972b | 91 | * [cnt] = number of bytes to read |
switches | 3:601b78524967 | 92 | * returns data read in JSON array |
gsteiert | 0:828bfd94972b | 93 | */ |
switches | 2:3f6a8ac111a9 | 94 | void SerialInterface::fnc_i2c(char* resp) |
gsteiert | 0:828bfd94972b | 95 | { |
gsteiert | 0:828bfd94972b | 96 | int dcnt=0; |
gsteiert | 0:828bfd94972b | 97 | if (_args[IA_CNT] < 2) { |
switches | 2:3f6a8ac111a9 | 98 | sprintf(resp, "[-1]"); |
gsteiert | 0:828bfd94972b | 99 | } else { |
gsteiert | 0:828bfd94972b | 100 | if (_args[IA_ADD] & 1) { |
switches | 2:3f6a8ac111a9 | 101 | sprintf(resp, "["); |
switches | 2:3f6a8ac111a9 | 102 | resp +=1; |
gsteiert | 0:828bfd94972b | 103 | if (_args[IA_CNT] > 2) { |
gsteiert | 0:828bfd94972b | 104 | for (dcnt = 0; dcnt < (_args[IA_CNT] -2) ; dcnt++) { |
gsteiert | 0:828bfd94972b | 105 | _dbuf[dcnt] = _args[(dcnt +3)]; |
gsteiert | 0:828bfd94972b | 106 | } |
gsteiert | 0:828bfd94972b | 107 | if ((*_i2c).write(_args[IA_ADD], _dbuf, dcnt, true) != 0) { |
switches | 2:3f6a8ac111a9 | 108 | sprintf(resp, "-1,"); |
switches | 2:3f6a8ac111a9 | 109 | resp +=3; |
gsteiert | 0:828bfd94972b | 110 | } |
gsteiert | 0:828bfd94972b | 111 | } |
gsteiert | 0:828bfd94972b | 112 | if ((*_i2c).read(_args[IA_ADD], _dbuf, _args[IA_DATA])!=0) { |
switches | 2:3f6a8ac111a9 | 113 | sprintf(resp, "-1]"); |
gsteiert | 0:828bfd94972b | 114 | } else { |
gsteiert | 0:828bfd94972b | 115 | for (dcnt = 0; dcnt < _args[IA_DATA]; dcnt++) { |
switches | 2:3f6a8ac111a9 | 116 | sprintf(resp,"0x%02X,", _dbuf[dcnt]); |
gsteiert | 0:828bfd94972b | 117 | resp +=5; |
gsteiert | 0:828bfd94972b | 118 | } |
switches | 2:3f6a8ac111a9 | 119 | sprintf((resp-1), "]"); |
gsteiert | 0:828bfd94972b | 120 | } |
gsteiert | 0:828bfd94972b | 121 | } else { |
gsteiert | 0:828bfd94972b | 122 | for (dcnt = 0; dcnt < (_args[IA_CNT] -1) ; dcnt++) { |
gsteiert | 0:828bfd94972b | 123 | _dbuf[dcnt] = _args[(dcnt +2)]; |
gsteiert | 0:828bfd94972b | 124 | } |
gsteiert | 0:828bfd94972b | 125 | if ((*_i2c).write(_args[IA_ADD], _dbuf, dcnt) == 0) { |
switches | 2:3f6a8ac111a9 | 126 | sprintf(resp,"[%d]", dcnt); |
gsteiert | 0:828bfd94972b | 127 | } else { |
switches | 2:3f6a8ac111a9 | 128 | sprintf(resp, "[-1]"); |
gsteiert | 0:828bfd94972b | 129 | } |
gsteiert | 0:828bfd94972b | 130 | } |
gsteiert | 0:828bfd94972b | 131 | } |
gsteiert | 0:828bfd94972b | 132 | } |
gsteiert | 0:828bfd94972b | 133 | |
switches | 2:3f6a8ac111a9 | 134 | /* SPI |
switches | 5:9e27e2a46fa6 | 135 | * /s/[cfg]/[data]... read+write |
switches | 5:9e27e2a46fa6 | 136 | * [cfg] = SPI configuration specifies gpio to use for chip |
switches | 5:9e27e2a46fa6 | 137 | * select and other SPI parameters: |
switches | 5:9e27e2a46fa6 | 138 | * 0x[Byte3][Byte2][Byte1][Byte0] |
switches | 5:9e27e2a46fa6 | 139 | * Byte3 = SCK frequency in MHz (0 = no change) |
switches | 5:9e27e2a46fa6 | 140 | * Byte2 = Format: |
switches | 5:9e27e2a46fa6 | 141 | * 0 = no change |
switches | 5:9e27e2a46fa6 | 142 | * 1 = Mode 1 |
switches | 5:9e27e2a46fa6 | 143 | * 2 = Mode 2 |
switches | 5:9e27e2a46fa6 | 144 | * 3 = Mode 3 |
switches | 5:9e27e2a46fa6 | 145 | * 4 = Mode 0 |
switches | 5:9e27e2a46fa6 | 146 | * Byte1 = CS polarity 1 = active high, 0 = active low |
switches | 5:9e27e2a46fa6 | 147 | * Byte0 = GPIO to use for CS (0-7) |
switches | 5:9e27e2a46fa6 | 148 | * 0 = P5_3 |
switches | 5:9e27e2a46fa6 | 149 | * 1 = P5_4 |
switches | 5:9e27e2a46fa6 | 150 | * 2 = P5_5 |
switches | 5:9e27e2a46fa6 | 151 | * 3 = P5_6 |
switches | 5:9e27e2a46fa6 | 152 | * 4 = P3_0 |
switches | 5:9e27e2a46fa6 | 153 | * 5 = P3_1 |
switches | 5:9e27e2a46fa6 | 154 | * 6 = P3_2 |
switches | 5:9e27e2a46fa6 | 155 | * 7 = P3_3 |
switches | 2:3f6a8ac111a9 | 156 | * [data] = data to be writen |
switches | 3:601b78524967 | 157 | * this shifts out each data byte provided and |
switches | 3:601b78524967 | 158 | * returns each byte read in a JSON array |
gsteiert | 0:828bfd94972b | 159 | */ |
switches | 2:3f6a8ac111a9 | 160 | void SerialInterface::fnc_spi(char* resp) |
gsteiert | 0:828bfd94972b | 161 | { |
switches | 5:9e27e2a46fa6 | 162 | int dcnt=1; |
switches | 2:3f6a8ac111a9 | 163 | int dataIn; |
switches | 5:9e27e2a46fa6 | 164 | spiConfig_t spiCfg; |
switches | 2:3f6a8ac111a9 | 165 | if (_args[0] < 1) { |
switches | 2:3f6a8ac111a9 | 166 | sprintf(resp, "[-1]"); |
gsteiert | 0:828bfd94972b | 167 | } else { |
switches | 5:9e27e2a46fa6 | 168 | spiCfg.merged = _args[1]; |
switches | 5:9e27e2a46fa6 | 169 | if (spiCfg.freq) { |
switches | 5:9e27e2a46fa6 | 170 | (*_spi).frequency(spiCfg.freq * 1000000); |
switches | 5:9e27e2a46fa6 | 171 | } |
switches | 5:9e27e2a46fa6 | 172 | if (spiCfg.format) { |
switches | 5:9e27e2a46fa6 | 173 | (*_spi).format(8, (spiCfg.format & 3)); |
gsteiert | 0:828bfd94972b | 174 | } |
switches | 5:9e27e2a46fa6 | 175 | if (_args[0] > 1) { |
switches | 5:9e27e2a46fa6 | 176 | sprintf(resp, "["); |
switches | 5:9e27e2a46fa6 | 177 | resp++; |
switches | 5:9e27e2a46fa6 | 178 | _gpio[spiCfg.csPin] = (spiCfg.csPol); |
switches | 5:9e27e2a46fa6 | 179 | while(dcnt < _args[0]) { |
switches | 5:9e27e2a46fa6 | 180 | dcnt++; |
switches | 5:9e27e2a46fa6 | 181 | dataIn = (*_spi).write(_args[dcnt]); |
switches | 5:9e27e2a46fa6 | 182 | sprintf(resp, "0x%02X,", dataIn); |
switches | 5:9e27e2a46fa6 | 183 | resp += 5; |
switches | 5:9e27e2a46fa6 | 184 | } |
switches | 5:9e27e2a46fa6 | 185 | _gpio[spiCfg.csPin] = !(spiCfg.csPol); |
switches | 5:9e27e2a46fa6 | 186 | sprintf((resp-1), "]"); |
switches | 5:9e27e2a46fa6 | 187 | } |
gsteiert | 0:828bfd94972b | 188 | } |
gsteiert | 0:828bfd94972b | 189 | } |
gsteiert | 0:828bfd94972b | 190 | |
switches | 2:3f6a8ac111a9 | 191 | void SerialInterface::call(char* input, char* output) |
gsteiert | 0:828bfd94972b | 192 | { |
gsteiert | 0:828bfd94972b | 193 | char cmd; |
gsteiert | 0:828bfd94972b | 194 | _args[0] = 0; |
gsteiert | 0:828bfd94972b | 195 | if (*input == '/') { |
gsteiert | 0:828bfd94972b | 196 | input++; |
gsteiert | 0:828bfd94972b | 197 | cmd = *input; |
gsteiert | 0:828bfd94972b | 198 | input = strchr(input, '/'); |
gsteiert | 0:828bfd94972b | 199 | while (*input == '/') { |
gsteiert | 0:828bfd94972b | 200 | input++; |
gsteiert | 0:828bfd94972b | 201 | _args[(_args[0]+1)] = strtol(input, &input, 0); |
gsteiert | 0:828bfd94972b | 202 | if (input) { |
gsteiert | 0:828bfd94972b | 203 | _args[0]++; |
gsteiert | 0:828bfd94972b | 204 | } |
gsteiert | 0:828bfd94972b | 205 | } |
gsteiert | 0:828bfd94972b | 206 | switch (cmd) { |
switches | 5:9e27e2a46fa6 | 207 | case 'd': |
switches | 5:9e27e2a46fa6 | 208 | case 'D': |
switches | 5:9e27e2a46fa6 | 209 | fnc_dio(output); |
switches | 5:9e27e2a46fa6 | 210 | break; |
gsteiert | 0:828bfd94972b | 211 | case 'i': |
gsteiert | 0:828bfd94972b | 212 | case 'I': |
gsteiert | 0:828bfd94972b | 213 | fnc_i2c(output); |
gsteiert | 0:828bfd94972b | 214 | break; |
gsteiert | 0:828bfd94972b | 215 | case 's': |
gsteiert | 0:828bfd94972b | 216 | case 'S': |
gsteiert | 0:828bfd94972b | 217 | fnc_spi(output); |
gsteiert | 0:828bfd94972b | 218 | break; |
gsteiert | 0:828bfd94972b | 219 | default: |
switches | 5:9e27e2a46fa6 | 220 | sprintf(output, "!commands: dio i2c spi"); |
gsteiert | 0:828bfd94972b | 221 | break; |
gsteiert | 0:828bfd94972b | 222 | } |
gsteiert | 0:828bfd94972b | 223 | } else { |
gsteiert | 0:828bfd94972b | 224 | sprintf(output, "!format: /cmd/arg1/arg2..."); |
gsteiert | 0:828bfd94972b | 225 | |
gsteiert | 0:828bfd94972b | 226 | } |
gsteiert | 0:828bfd94972b | 227 | } |