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