Gas Pressure Display Updated Power control for Pressure sensor added
Dependencies: UniGraphic mbed vt100
afLib/afLib.cpp@0:37c8ecde13c2, 2018-02-16 (annotated)
- Committer:
- Rhyme
- Date:
- Fri Feb 16 08:27:50 2018 +0000
- Revision:
- 0:37c8ecde13c2
control PSE530 power via PTC5 (pse530_en)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Rhyme | 0:37c8ecde13c2 | 1 | /** |
Rhyme | 0:37c8ecde13c2 | 2 | * Copyright 2015 Afero, Inc. |
Rhyme | 0:37c8ecde13c2 | 3 | * |
Rhyme | 0:37c8ecde13c2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Rhyme | 0:37c8ecde13c2 | 5 | * you may not use this file except in compliance with the License. |
Rhyme | 0:37c8ecde13c2 | 6 | * You may obtain a copy of the License at |
Rhyme | 0:37c8ecde13c2 | 7 | * |
Rhyme | 0:37c8ecde13c2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Rhyme | 0:37c8ecde13c2 | 9 | * |
Rhyme | 0:37c8ecde13c2 | 10 | * Unless required by applicable law or agreed to in writing, software |
Rhyme | 0:37c8ecde13c2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Rhyme | 0:37c8ecde13c2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Rhyme | 0:37c8ecde13c2 | 13 | * See the License for the specific language governing permissions and |
Rhyme | 0:37c8ecde13c2 | 14 | * limitations under the License. |
Rhyme | 0:37c8ecde13c2 | 15 | */ |
Rhyme | 0:37c8ecde13c2 | 16 | |
Rhyme | 0:37c8ecde13c2 | 17 | #include "mbed.h" |
Rhyme | 0:37c8ecde13c2 | 18 | #include "afLib.h" |
Rhyme | 0:37c8ecde13c2 | 19 | #include "afErrors.h" |
Rhyme | 0:37c8ecde13c2 | 20 | #include "msg_types.h" |
Rhyme | 0:37c8ecde13c2 | 21 | /* added by Motoo Tanaka on 26-Dec-2017 for watchdog */ |
Rhyme | 0:37c8ecde13c2 | 22 | #include "edge_reset_mgr.h" |
Rhyme | 0:37c8ecde13c2 | 23 | |
Rhyme | 0:37c8ecde13c2 | 24 | #define IS_MCU_ATTR(x) (x >= 0 && x < 1024) |
Rhyme | 0:37c8ecde13c2 | 25 | |
Rhyme | 0:37c8ecde13c2 | 26 | static iafLib *_iaflib = NULL; |
Rhyme | 0:37c8ecde13c2 | 27 | |
Rhyme | 0:37c8ecde13c2 | 28 | #define MAX_SYNC_RETRIES 10 |
Rhyme | 0:37c8ecde13c2 | 29 | static long lastSync = 0; |
Rhyme | 0:37c8ecde13c2 | 30 | static int syncRetries = 0; |
Rhyme | 0:37c8ecde13c2 | 31 | |
Rhyme | 0:37c8ecde13c2 | 32 | /** |
Rhyme | 0:37c8ecde13c2 | 33 | * create |
Rhyme | 0:37c8ecde13c2 | 34 | * |
Rhyme | 0:37c8ecde13c2 | 35 | * The public constructor for the afLib. This allows us to create the afLib object once and hold a reference to it. |
Rhyme | 0:37c8ecde13c2 | 36 | */ |
Rhyme | 0:37c8ecde13c2 | 37 | iafLib *iafLib::create(PinName mcuInterrupt, isr isrWrapper, |
Rhyme | 0:37c8ecde13c2 | 38 | onAttributeSet attrSet, onAttributeSetComplete attrSetComplete, afSPI *theSPI) |
Rhyme | 0:37c8ecde13c2 | 39 | { |
Rhyme | 0:37c8ecde13c2 | 40 | if (_iaflib == NULL) { |
Rhyme | 0:37c8ecde13c2 | 41 | _iaflib = new afLib( mcuInterrupt, isrWrapper, attrSet, attrSetComplete, theSPI); |
Rhyme | 0:37c8ecde13c2 | 42 | } |
Rhyme | 0:37c8ecde13c2 | 43 | |
Rhyme | 0:37c8ecde13c2 | 44 | return _iaflib; |
Rhyme | 0:37c8ecde13c2 | 45 | } |
Rhyme | 0:37c8ecde13c2 | 46 | |
Rhyme | 0:37c8ecde13c2 | 47 | void iafLib::destroy() |
Rhyme | 0:37c8ecde13c2 | 48 | { |
Rhyme | 0:37c8ecde13c2 | 49 | afLib *p = (afLib*)_iaflib; |
Rhyme | 0:37c8ecde13c2 | 50 | delete p; |
Rhyme | 0:37c8ecde13c2 | 51 | _iaflib = NULL; |
Rhyme | 0:37c8ecde13c2 | 52 | } |
Rhyme | 0:37c8ecde13c2 | 53 | |
Rhyme | 0:37c8ecde13c2 | 54 | /** |
Rhyme | 0:37c8ecde13c2 | 55 | * getRequestId |
Rhyme | 0:37c8ecde13c2 | 56 | * by Motoo Tanaka on 16-Nov-2017 |
Rhyme | 0:37c8ecde13c2 | 57 | */ |
Rhyme | 0:37c8ecde13c2 | 58 | int afLib::getRequestId(void) |
Rhyme | 0:37c8ecde13c2 | 59 | { |
Rhyme | 0:37c8ecde13c2 | 60 | return( _requestId ) ; |
Rhyme | 0:37c8ecde13c2 | 61 | } |
Rhyme | 0:37c8ecde13c2 | 62 | |
Rhyme | 0:37c8ecde13c2 | 63 | /** |
Rhyme | 0:37c8ecde13c2 | 64 | * afLib |
Rhyme | 0:37c8ecde13c2 | 65 | * |
Rhyme | 0:37c8ecde13c2 | 66 | * The private constructor for the afLib. This one actually initializes the afLib and prepares it for use. |
Rhyme | 0:37c8ecde13c2 | 67 | */ |
Rhyme | 0:37c8ecde13c2 | 68 | afLib::afLib(PinName mcuInterrupt, isr isrWrapper, |
Rhyme | 0:37c8ecde13c2 | 69 | onAttributeSet attrSet, onAttributeSetComplete attrSetComplete, afSPI *theSPI) : fco(mcuInterrupt) |
Rhyme | 0:37c8ecde13c2 | 70 | { |
Rhyme | 0:37c8ecde13c2 | 71 | checkLastSync = new Timer(); |
Rhyme | 0:37c8ecde13c2 | 72 | checkLastSync->start(); |
Rhyme | 0:37c8ecde13c2 | 73 | queueInit(); |
Rhyme | 0:37c8ecde13c2 | 74 | _theSPI= theSPI; |
Rhyme | 0:37c8ecde13c2 | 75 | _request.p_value = NULL; |
Rhyme | 0:37c8ecde13c2 | 76 | |
Rhyme | 0:37c8ecde13c2 | 77 | //_spiSettings = SPISettings(1000000, LSBFIRST, SPI_MODE0); |
Rhyme | 0:37c8ecde13c2 | 78 | _interrupts_pending = 0; |
Rhyme | 0:37c8ecde13c2 | 79 | _state = STATE_IDLE; |
Rhyme | 0:37c8ecde13c2 | 80 | |
Rhyme | 0:37c8ecde13c2 | 81 | _writeCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 82 | _writeCmdOffset = 0; |
Rhyme | 0:37c8ecde13c2 | 83 | |
Rhyme | 0:37c8ecde13c2 | 84 | _outstandingSetGetAttrId = 0; |
Rhyme | 0:37c8ecde13c2 | 85 | |
Rhyme | 0:37c8ecde13c2 | 86 | _readCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 87 | _readCmdOffset = 0; |
Rhyme | 0:37c8ecde13c2 | 88 | _readBufferLen = 0; |
Rhyme | 0:37c8ecde13c2 | 89 | |
Rhyme | 0:37c8ecde13c2 | 90 | _txStatus = new StatusCommand(); |
Rhyme | 0:37c8ecde13c2 | 91 | _rxStatus = new StatusCommand(); |
Rhyme | 0:37c8ecde13c2 | 92 | |
Rhyme | 0:37c8ecde13c2 | 93 | _onAttrSet = attrSet; |
Rhyme | 0:37c8ecde13c2 | 94 | _onAttrSetComplete = attrSetComplete; |
Rhyme | 0:37c8ecde13c2 | 95 | _theSPI->begin(); |
Rhyme | 0:37c8ecde13c2 | 96 | |
Rhyme | 0:37c8ecde13c2 | 97 | // AJS where does this get moved to?? |
Rhyme | 0:37c8ecde13c2 | 98 | #ifdef ARDUINO |
Rhyme | 0:37c8ecde13c2 | 99 | pinMode(mcuInterrupt, INPUT); |
Rhyme | 0:37c8ecde13c2 | 100 | attachInterrupt(mcuInterrupt, isrWrapper, FALLING); |
Rhyme | 0:37c8ecde13c2 | 101 | #endif |
Rhyme | 0:37c8ecde13c2 | 102 | fco.fall(isrWrapper); |
Rhyme | 0:37c8ecde13c2 | 103 | SERIAL_PRINT_DBG_ASR("afLib init done!!\n"); |
Rhyme | 0:37c8ecde13c2 | 104 | } |
Rhyme | 0:37c8ecde13c2 | 105 | //wsugi 20161128 |
Rhyme | 0:37c8ecde13c2 | 106 | afLib::~afLib() |
Rhyme | 0:37c8ecde13c2 | 107 | { |
Rhyme | 0:37c8ecde13c2 | 108 | SERIAL_PRINT_DBG_ASR("deleted\n"); |
Rhyme | 0:37c8ecde13c2 | 109 | if(_readBuffer != NULL) |
Rhyme | 0:37c8ecde13c2 | 110 | { |
Rhyme | 0:37c8ecde13c2 | 111 | delete[] (_readBuffer); |
Rhyme | 0:37c8ecde13c2 | 112 | _readBuffer = NULL; |
Rhyme | 0:37c8ecde13c2 | 113 | } |
Rhyme | 0:37c8ecde13c2 | 114 | |
Rhyme | 0:37c8ecde13c2 | 115 | if(_writeBuffer != NULL) |
Rhyme | 0:37c8ecde13c2 | 116 | { |
Rhyme | 0:37c8ecde13c2 | 117 | delete[] (_writeBuffer); |
Rhyme | 0:37c8ecde13c2 | 118 | _writeBuffer = NULL; |
Rhyme | 0:37c8ecde13c2 | 119 | } |
Rhyme | 0:37c8ecde13c2 | 120 | |
Rhyme | 0:37c8ecde13c2 | 121 | if(_readCmd != NULL) |
Rhyme | 0:37c8ecde13c2 | 122 | { |
Rhyme | 0:37c8ecde13c2 | 123 | delete (_readCmd); |
Rhyme | 0:37c8ecde13c2 | 124 | _readCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 125 | } |
Rhyme | 0:37c8ecde13c2 | 126 | |
Rhyme | 0:37c8ecde13c2 | 127 | if(_writeCmd != NULL) |
Rhyme | 0:37c8ecde13c2 | 128 | { |
Rhyme | 0:37c8ecde13c2 | 129 | delete (_writeCmd); |
Rhyme | 0:37c8ecde13c2 | 130 | _writeCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 131 | } |
Rhyme | 0:37c8ecde13c2 | 132 | |
Rhyme | 0:37c8ecde13c2 | 133 | if(_txStatus != NULL) |
Rhyme | 0:37c8ecde13c2 | 134 | { |
Rhyme | 0:37c8ecde13c2 | 135 | delete (_txStatus); |
Rhyme | 0:37c8ecde13c2 | 136 | _txStatus = NULL; |
Rhyme | 0:37c8ecde13c2 | 137 | } |
Rhyme | 0:37c8ecde13c2 | 138 | |
Rhyme | 0:37c8ecde13c2 | 139 | if(_rxStatus != NULL) |
Rhyme | 0:37c8ecde13c2 | 140 | { |
Rhyme | 0:37c8ecde13c2 | 141 | delete (_rxStatus); |
Rhyme | 0:37c8ecde13c2 | 142 | _rxStatus = NULL; |
Rhyme | 0:37c8ecde13c2 | 143 | } |
Rhyme | 0:37c8ecde13c2 | 144 | |
Rhyme | 0:37c8ecde13c2 | 145 | for (int i = 0; i < REQUEST_QUEUE_SIZE; i++) |
Rhyme | 0:37c8ecde13c2 | 146 | { |
Rhyme | 0:37c8ecde13c2 | 147 | if (_requestQueue[i].p_value != NULL) |
Rhyme | 0:37c8ecde13c2 | 148 | { |
Rhyme | 0:37c8ecde13c2 | 149 | delete[] (_requestQueue[i].p_value); |
Rhyme | 0:37c8ecde13c2 | 150 | _requestQueue[i].p_value = NULL; |
Rhyme | 0:37c8ecde13c2 | 151 | } |
Rhyme | 0:37c8ecde13c2 | 152 | } |
Rhyme | 0:37c8ecde13c2 | 153 | |
Rhyme | 0:37c8ecde13c2 | 154 | if(checkLastSync != NULL) |
Rhyme | 0:37c8ecde13c2 | 155 | { |
Rhyme | 0:37c8ecde13c2 | 156 | delete (checkLastSync); |
Rhyme | 0:37c8ecde13c2 | 157 | checkLastSync = NULL; |
Rhyme | 0:37c8ecde13c2 | 158 | } |
Rhyme | 0:37c8ecde13c2 | 159 | |
Rhyme | 0:37c8ecde13c2 | 160 | _iaflib = NULL; |
Rhyme | 0:37c8ecde13c2 | 161 | } |
Rhyme | 0:37c8ecde13c2 | 162 | //wsugi 20161128 |
Rhyme | 0:37c8ecde13c2 | 163 | /** |
Rhyme | 0:37c8ecde13c2 | 164 | * loop |
Rhyme | 0:37c8ecde13c2 | 165 | * |
Rhyme | 0:37c8ecde13c2 | 166 | * This is how the afLib gets time to run its state machine. This method should be called periodically from the |
Rhyme | 0:37c8ecde13c2 | 167 | * loop() function of the Arduino sketch. |
Rhyme | 0:37c8ecde13c2 | 168 | * This function pulls pending attribute operations from the queue. It takes approximately 4 calls to loop() to |
Rhyme | 0:37c8ecde13c2 | 169 | * complete one attribute operation. |
Rhyme | 0:37c8ecde13c2 | 170 | */ |
Rhyme | 0:37c8ecde13c2 | 171 | void afLib::loop(void) { |
Rhyme | 0:37c8ecde13c2 | 172 | reset_watch_dog() ; /* 26-Dec-2017 by Motoo Tanaka */ |
Rhyme | 0:37c8ecde13c2 | 173 | if (isIdle() && (queueGet(&_request.messageType, &_request.requestId, &_request.attrId, &_request.valueLen, |
Rhyme | 0:37c8ecde13c2 | 174 | &_request.p_value) == afSUCCESS)) { |
Rhyme | 0:37c8ecde13c2 | 175 | switch (_request.messageType) { |
Rhyme | 0:37c8ecde13c2 | 176 | case MSG_TYPE_GET: |
Rhyme | 0:37c8ecde13c2 | 177 | doGetAttribute(_request.requestId, _request.attrId); |
Rhyme | 0:37c8ecde13c2 | 178 | break; |
Rhyme | 0:37c8ecde13c2 | 179 | |
Rhyme | 0:37c8ecde13c2 | 180 | case MSG_TYPE_SET: |
Rhyme | 0:37c8ecde13c2 | 181 | doSetAttribute(_request.requestId, _request.attrId, _request.valueLen, _request.p_value); |
Rhyme | 0:37c8ecde13c2 | 182 | break; |
Rhyme | 0:37c8ecde13c2 | 183 | |
Rhyme | 0:37c8ecde13c2 | 184 | case MSG_TYPE_UPDATE: |
Rhyme | 0:37c8ecde13c2 | 185 | doUpdateAttribute(_request.requestId, _request.attrId, 0, _request.valueLen, _request.p_value); |
Rhyme | 0:37c8ecde13c2 | 186 | break; |
Rhyme | 0:37c8ecde13c2 | 187 | |
Rhyme | 0:37c8ecde13c2 | 188 | default: |
Rhyme | 0:37c8ecde13c2 | 189 | SERIAL_PRINT_DBG_ASR("%s\n","loop: request type!"); |
Rhyme | 0:37c8ecde13c2 | 190 | } |
Rhyme | 0:37c8ecde13c2 | 191 | } |
Rhyme | 0:37c8ecde13c2 | 192 | |
Rhyme | 0:37c8ecde13c2 | 193 | if (_request.p_value != NULL) { |
Rhyme | 0:37c8ecde13c2 | 194 | delete[] (_request.p_value); //wsugi delete (_request.p_value); |
Rhyme | 0:37c8ecde13c2 | 195 | _request.p_value = NULL; |
Rhyme | 0:37c8ecde13c2 | 196 | } |
Rhyme | 0:37c8ecde13c2 | 197 | runStateMachine(); |
Rhyme | 0:37c8ecde13c2 | 198 | } |
Rhyme | 0:37c8ecde13c2 | 199 | |
Rhyme | 0:37c8ecde13c2 | 200 | /** |
Rhyme | 0:37c8ecde13c2 | 201 | * updateIntsPending |
Rhyme | 0:37c8ecde13c2 | 202 | * |
Rhyme | 0:37c8ecde13c2 | 203 | * Interrupt-safe method for updating the interrupt count. This is called to increment and decrement the interrupt count |
Rhyme | 0:37c8ecde13c2 | 204 | * as interrupts are received and handled. |
Rhyme | 0:37c8ecde13c2 | 205 | */ |
Rhyme | 0:37c8ecde13c2 | 206 | void afLib::updateIntsPending(int amount) { |
Rhyme | 0:37c8ecde13c2 | 207 | // fco.disable_irq(); |
Rhyme | 0:37c8ecde13c2 | 208 | __disable_irq() ; // Disable Interrupts |
Rhyme | 0:37c8ecde13c2 | 209 | _interrupts_pending += amount; |
Rhyme | 0:37c8ecde13c2 | 210 | __enable_irq() ; // Enable Interrupts |
Rhyme | 0:37c8ecde13c2 | 211 | // fco.enable_irq(); |
Rhyme | 0:37c8ecde13c2 | 212 | } |
Rhyme | 0:37c8ecde13c2 | 213 | |
Rhyme | 0:37c8ecde13c2 | 214 | /** |
Rhyme | 0:37c8ecde13c2 | 215 | * sendCommand |
Rhyme | 0:37c8ecde13c2 | 216 | * |
Rhyme | 0:37c8ecde13c2 | 217 | * This increments the interrupt count to kick off the state machine in the next call to loop(). |
Rhyme | 0:37c8ecde13c2 | 218 | */ |
Rhyme | 0:37c8ecde13c2 | 219 | /** |
Rhyme | 0:37c8ecde13c2 | 220 | * In this fucntion, only disable/enable fco irq is enough |
Rhyme | 0:37c8ecde13c2 | 221 | */ |
Rhyme | 0:37c8ecde13c2 | 222 | void afLib::sendCommand(void) { |
Rhyme | 0:37c8ecde13c2 | 223 | fco.disable_irq(); |
Rhyme | 0:37c8ecde13c2 | 224 | if (_interrupts_pending == 0 && _state == STATE_IDLE) { |
Rhyme | 0:37c8ecde13c2 | 225 | updateIntsPending(1); |
Rhyme | 0:37c8ecde13c2 | 226 | } |
Rhyme | 0:37c8ecde13c2 | 227 | fco.enable_irq(); |
Rhyme | 0:37c8ecde13c2 | 228 | } |
Rhyme | 0:37c8ecde13c2 | 229 | |
Rhyme | 0:37c8ecde13c2 | 230 | /** |
Rhyme | 0:37c8ecde13c2 | 231 | * getAttribute |
Rhyme | 0:37c8ecde13c2 | 232 | * |
Rhyme | 0:37c8ecde13c2 | 233 | * The public getAttribute method. This method queues the operation and returns immediately. Applications must call |
Rhyme | 0:37c8ecde13c2 | 234 | * loop() for the operation to complete. |
Rhyme | 0:37c8ecde13c2 | 235 | */ |
Rhyme | 0:37c8ecde13c2 | 236 | int afLib::getAttribute(const uint16_t attrId) { |
Rhyme | 0:37c8ecde13c2 | 237 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 238 | uint8_t dummy; // This value isn't actually used. |
Rhyme | 0:37c8ecde13c2 | 239 | // return queuePut(MSG_TYPE_GET, _requestId++, attrId, 0, &dummy); |
Rhyme | 0:37c8ecde13c2 | 240 | return queuePut(MSG_TYPE_GET, _requestId, attrId, 0, &dummy); /* by moto on 17-Nov-2017 */ |
Rhyme | 0:37c8ecde13c2 | 241 | } |
Rhyme | 0:37c8ecde13c2 | 242 | |
Rhyme | 0:37c8ecde13c2 | 243 | /** |
Rhyme | 0:37c8ecde13c2 | 244 | * The many moods of setAttribute |
Rhyme | 0:37c8ecde13c2 | 245 | * |
Rhyme | 0:37c8ecde13c2 | 246 | * These are the public versions of the setAttribute method. |
Rhyme | 0:37c8ecde13c2 | 247 | * These methods queue the operation and return immediately. Applications must call loop() for the operation to complete. |
Rhyme | 0:37c8ecde13c2 | 248 | */ |
Rhyme | 0:37c8ecde13c2 | 249 | int afLib::setAttributeBool(const uint16_t attrId, const bool value) { |
Rhyme | 0:37c8ecde13c2 | 250 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 251 | uint8_t val = value ? 1 : 0; |
Rhyme | 0:37c8ecde13c2 | 252 | return queuePut(IS_MCU_ATTR(attrId) ? MSG_TYPE_UPDATE : MSG_TYPE_SET, _requestId, attrId, sizeof(val), |
Rhyme | 0:37c8ecde13c2 | 253 | (uint8_t *)&val); |
Rhyme | 0:37c8ecde13c2 | 254 | } |
Rhyme | 0:37c8ecde13c2 | 255 | |
Rhyme | 0:37c8ecde13c2 | 256 | int afLib::setAttribute8(const uint16_t attrId, const int8_t value) { |
Rhyme | 0:37c8ecde13c2 | 257 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 258 | return queuePut(IS_MCU_ATTR(attrId) ? MSG_TYPE_UPDATE : MSG_TYPE_SET, _requestId, attrId, sizeof(value), |
Rhyme | 0:37c8ecde13c2 | 259 | (uint8_t *)&value); |
Rhyme | 0:37c8ecde13c2 | 260 | } |
Rhyme | 0:37c8ecde13c2 | 261 | |
Rhyme | 0:37c8ecde13c2 | 262 | int afLib::setAttribute16(const uint16_t attrId, const int16_t value) { |
Rhyme | 0:37c8ecde13c2 | 263 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 264 | return queuePut(IS_MCU_ATTR(attrId) ? MSG_TYPE_UPDATE : MSG_TYPE_SET, _requestId, attrId, sizeof(value), |
Rhyme | 0:37c8ecde13c2 | 265 | (uint8_t *) &value); |
Rhyme | 0:37c8ecde13c2 | 266 | } |
Rhyme | 0:37c8ecde13c2 | 267 | |
Rhyme | 0:37c8ecde13c2 | 268 | int afLib::setAttribute32(const uint16_t attrId, const int32_t value) { |
Rhyme | 0:37c8ecde13c2 | 269 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 270 | return queuePut(IS_MCU_ATTR(attrId) ? MSG_TYPE_UPDATE : MSG_TYPE_SET, _requestId, attrId, sizeof(value), |
Rhyme | 0:37c8ecde13c2 | 271 | (uint8_t *) &value); |
Rhyme | 0:37c8ecde13c2 | 272 | } |
Rhyme | 0:37c8ecde13c2 | 273 | |
Rhyme | 0:37c8ecde13c2 | 274 | int afLib::setAttribute64(const uint16_t attrId, const int64_t value) { |
Rhyme | 0:37c8ecde13c2 | 275 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 276 | return queuePut(IS_MCU_ATTR(attrId) ? MSG_TYPE_UPDATE : MSG_TYPE_SET, _requestId, attrId, sizeof(value), |
Rhyme | 0:37c8ecde13c2 | 277 | (uint8_t *) &value); |
Rhyme | 0:37c8ecde13c2 | 278 | } |
Rhyme | 0:37c8ecde13c2 | 279 | |
Rhyme | 0:37c8ecde13c2 | 280 | int afLib::setAttribute(const uint16_t attrId, const string &value) { |
Rhyme | 0:37c8ecde13c2 | 281 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 282 | return queuePut(IS_MCU_ATTR(attrId) ? MSG_TYPE_UPDATE : MSG_TYPE_SET, _requestId, attrId, value.length(), |
Rhyme | 0:37c8ecde13c2 | 283 | (uint8_t *) value.c_str()); |
Rhyme | 0:37c8ecde13c2 | 284 | } |
Rhyme | 0:37c8ecde13c2 | 285 | |
Rhyme | 0:37c8ecde13c2 | 286 | int afLib::setAttribute(const uint16_t attrId, const uint16_t valueLen, const char *value) { |
Rhyme | 0:37c8ecde13c2 | 287 | if (valueLen > MAX_ATTRIBUTE_SIZE) { |
Rhyme | 0:37c8ecde13c2 | 288 | return afERROR_INVALID_PARAM; |
Rhyme | 0:37c8ecde13c2 | 289 | } |
Rhyme | 0:37c8ecde13c2 | 290 | |
Rhyme | 0:37c8ecde13c2 | 291 | if (value == NULL) { |
Rhyme | 0:37c8ecde13c2 | 292 | return afERROR_INVALID_PARAM; |
Rhyme | 0:37c8ecde13c2 | 293 | } |
Rhyme | 0:37c8ecde13c2 | 294 | |
Rhyme | 0:37c8ecde13c2 | 295 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 296 | return queuePut(IS_MCU_ATTR(attrId) ? MSG_TYPE_UPDATE : MSG_TYPE_SET, _requestId, attrId, valueLen, |
Rhyme | 0:37c8ecde13c2 | 297 | (const uint8_t *) value); |
Rhyme | 0:37c8ecde13c2 | 298 | } |
Rhyme | 0:37c8ecde13c2 | 299 | |
Rhyme | 0:37c8ecde13c2 | 300 | int afLib::setAttribute(const uint16_t attrId, const uint16_t valueLen, const uint8_t *value) { |
Rhyme | 0:37c8ecde13c2 | 301 | if (valueLen > MAX_ATTRIBUTE_SIZE) { |
Rhyme | 0:37c8ecde13c2 | 302 | return afERROR_INVALID_PARAM; |
Rhyme | 0:37c8ecde13c2 | 303 | } |
Rhyme | 0:37c8ecde13c2 | 304 | |
Rhyme | 0:37c8ecde13c2 | 305 | if (value == NULL) { |
Rhyme | 0:37c8ecde13c2 | 306 | return afERROR_INVALID_PARAM; |
Rhyme | 0:37c8ecde13c2 | 307 | } |
Rhyme | 0:37c8ecde13c2 | 308 | |
Rhyme | 0:37c8ecde13c2 | 309 | _requestId++; |
Rhyme | 0:37c8ecde13c2 | 310 | return queuePut(IS_MCU_ATTR(attrId) ? MSG_TYPE_UPDATE : MSG_TYPE_SET, _requestId, attrId, valueLen, value); |
Rhyme | 0:37c8ecde13c2 | 311 | } |
Rhyme | 0:37c8ecde13c2 | 312 | |
Rhyme | 0:37c8ecde13c2 | 313 | int afLib::setAttributeComplete(uint8_t requestId, const uint16_t attrId, const uint16_t valueLen, const uint8_t *value) { |
Rhyme | 0:37c8ecde13c2 | 314 | if (valueLen > MAX_ATTRIBUTE_SIZE) { |
Rhyme | 0:37c8ecde13c2 | 315 | return afERROR_INVALID_PARAM; |
Rhyme | 0:37c8ecde13c2 | 316 | } |
Rhyme | 0:37c8ecde13c2 | 317 | |
Rhyme | 0:37c8ecde13c2 | 318 | if (value == NULL) { |
Rhyme | 0:37c8ecde13c2 | 319 | return afERROR_INVALID_PARAM; |
Rhyme | 0:37c8ecde13c2 | 320 | } |
Rhyme | 0:37c8ecde13c2 | 321 | |
Rhyme | 0:37c8ecde13c2 | 322 | return queuePut(MSG_TYPE_UPDATE, requestId, attrId, valueLen, value); |
Rhyme | 0:37c8ecde13c2 | 323 | } |
Rhyme | 0:37c8ecde13c2 | 324 | |
Rhyme | 0:37c8ecde13c2 | 325 | /** |
Rhyme | 0:37c8ecde13c2 | 326 | * doGetAttribute |
Rhyme | 0:37c8ecde13c2 | 327 | * |
Rhyme | 0:37c8ecde13c2 | 328 | * The private version of getAttribute. This version actually calls sendCommand() to kick off the state machine and |
Rhyme | 0:37c8ecde13c2 | 329 | * execute the operation. |
Rhyme | 0:37c8ecde13c2 | 330 | */ |
Rhyme | 0:37c8ecde13c2 | 331 | int afLib::doGetAttribute(uint8_t requestId, uint16_t attrId) { |
Rhyme | 0:37c8ecde13c2 | 332 | if (_interrupts_pending > 0 || _writeCmd != NULL) { |
Rhyme | 0:37c8ecde13c2 | 333 | return afERROR_BUSY; |
Rhyme | 0:37c8ecde13c2 | 334 | } |
Rhyme | 0:37c8ecde13c2 | 335 | |
Rhyme | 0:37c8ecde13c2 | 336 | _writeCmd = new Command(requestId, MSG_TYPE_GET, attrId); |
Rhyme | 0:37c8ecde13c2 | 337 | if (!_writeCmd->isValid()) { |
Rhyme | 0:37c8ecde13c2 | 338 | SERIAL_PRINT_DBG_ASR("getAttribute invalid command:"); |
Rhyme | 0:37c8ecde13c2 | 339 | _writeCmd->dumpBytes(); |
Rhyme | 0:37c8ecde13c2 | 340 | _writeCmd->dump(); |
Rhyme | 0:37c8ecde13c2 | 341 | delete (_writeCmd); |
Rhyme | 0:37c8ecde13c2 | 342 | _writeCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 343 | return afERROR_INVALID_COMMAND; |
Rhyme | 0:37c8ecde13c2 | 344 | } |
Rhyme | 0:37c8ecde13c2 | 345 | |
Rhyme | 0:37c8ecde13c2 | 346 | _outstandingSetGetAttrId = attrId; |
Rhyme | 0:37c8ecde13c2 | 347 | |
Rhyme | 0:37c8ecde13c2 | 348 | // Start the transmission. |
Rhyme | 0:37c8ecde13c2 | 349 | sendCommand(); |
Rhyme | 0:37c8ecde13c2 | 350 | |
Rhyme | 0:37c8ecde13c2 | 351 | return afSUCCESS; |
Rhyme | 0:37c8ecde13c2 | 352 | } |
Rhyme | 0:37c8ecde13c2 | 353 | |
Rhyme | 0:37c8ecde13c2 | 354 | /** |
Rhyme | 0:37c8ecde13c2 | 355 | * doSetAttribute |
Rhyme | 0:37c8ecde13c2 | 356 | * |
Rhyme | 0:37c8ecde13c2 | 357 | * The private version of setAttribute. This version actually calls sendCommand() to kick off the state machine and |
Rhyme | 0:37c8ecde13c2 | 358 | * execute the operation. |
Rhyme | 0:37c8ecde13c2 | 359 | */ |
Rhyme | 0:37c8ecde13c2 | 360 | int afLib::doSetAttribute(uint8_t requestId, uint16_t attrId, uint16_t valueLen, uint8_t *value) { |
Rhyme | 0:37c8ecde13c2 | 361 | if (_interrupts_pending > 0 || _writeCmd != NULL) { |
Rhyme | 0:37c8ecde13c2 | 362 | return afERROR_BUSY; |
Rhyme | 0:37c8ecde13c2 | 363 | } |
Rhyme | 0:37c8ecde13c2 | 364 | _writeCmd = new Command(requestId, MSG_TYPE_SET, attrId, valueLen, value); |
Rhyme | 0:37c8ecde13c2 | 365 | if (!_writeCmd->isValid()) { |
Rhyme | 0:37c8ecde13c2 | 366 | SERIAL_PRINT_DBG_ASR("setAttributeComplete invalid command:"); |
Rhyme | 0:37c8ecde13c2 | 367 | _writeCmd->dumpBytes(); |
Rhyme | 0:37c8ecde13c2 | 368 | _writeCmd->dump(); |
Rhyme | 0:37c8ecde13c2 | 369 | delete (_writeCmd); |
Rhyme | 0:37c8ecde13c2 | 370 | _writeCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 371 | return afERROR_INVALID_COMMAND; |
Rhyme | 0:37c8ecde13c2 | 372 | } |
Rhyme | 0:37c8ecde13c2 | 373 | |
Rhyme | 0:37c8ecde13c2 | 374 | _outstandingSetGetAttrId = attrId; |
Rhyme | 0:37c8ecde13c2 | 375 | |
Rhyme | 0:37c8ecde13c2 | 376 | // Start the transmission. |
Rhyme | 0:37c8ecde13c2 | 377 | sendCommand(); |
Rhyme | 0:37c8ecde13c2 | 378 | |
Rhyme | 0:37c8ecde13c2 | 379 | return afSUCCESS; |
Rhyme | 0:37c8ecde13c2 | 380 | } |
Rhyme | 0:37c8ecde13c2 | 381 | |
Rhyme | 0:37c8ecde13c2 | 382 | /** |
Rhyme | 0:37c8ecde13c2 | 383 | * doUpdateAttribute |
Rhyme | 0:37c8ecde13c2 | 384 | * |
Rhyme | 0:37c8ecde13c2 | 385 | * setAttribute calls on MCU attributes turn into updateAttribute calls. See documentation on the SPI protocol for |
Rhyme | 0:37c8ecde13c2 | 386 | * more information. This method calls sendCommand() to kick off the state machine and execute the operation. |
Rhyme | 0:37c8ecde13c2 | 387 | */ |
Rhyme | 0:37c8ecde13c2 | 388 | int afLib::doUpdateAttribute(uint8_t requestId, uint16_t attrId, uint8_t status, uint16_t valueLen, uint8_t *value) { |
Rhyme | 0:37c8ecde13c2 | 389 | if (_interrupts_pending > 0 || _writeCmd != NULL) { |
Rhyme | 0:37c8ecde13c2 | 390 | return afERROR_BUSY; |
Rhyme | 0:37c8ecde13c2 | 391 | } |
Rhyme | 0:37c8ecde13c2 | 392 | |
Rhyme | 0:37c8ecde13c2 | 393 | _writeCmd = new Command(requestId, MSG_TYPE_UPDATE, attrId, status, 3 /* MCU Set it */, valueLen, value); |
Rhyme | 0:37c8ecde13c2 | 394 | if (!_writeCmd->isValid()) { |
Rhyme | 0:37c8ecde13c2 | 395 | SERIAL_PRINT_DBG_ASR("updateAttribute invalid command:"); |
Rhyme | 0:37c8ecde13c2 | 396 | _writeCmd->dumpBytes(); |
Rhyme | 0:37c8ecde13c2 | 397 | _writeCmd->dump(); |
Rhyme | 0:37c8ecde13c2 | 398 | delete (_writeCmd); |
Rhyme | 0:37c8ecde13c2 | 399 | return afERROR_INVALID_COMMAND; |
Rhyme | 0:37c8ecde13c2 | 400 | } |
Rhyme | 0:37c8ecde13c2 | 401 | |
Rhyme | 0:37c8ecde13c2 | 402 | // Start the transmission. |
Rhyme | 0:37c8ecde13c2 | 403 | sendCommand(); |
Rhyme | 0:37c8ecde13c2 | 404 | |
Rhyme | 0:37c8ecde13c2 | 405 | return afSUCCESS; |
Rhyme | 0:37c8ecde13c2 | 406 | } |
Rhyme | 0:37c8ecde13c2 | 407 | |
Rhyme | 0:37c8ecde13c2 | 408 | /** |
Rhyme | 0:37c8ecde13c2 | 409 | * parseCommand |
Rhyme | 0:37c8ecde13c2 | 410 | * |
Rhyme | 0:37c8ecde13c2 | 411 | * A debug method for parsing a string into a command. This is not required for library operation and is only supplied |
Rhyme | 0:37c8ecde13c2 | 412 | * as an example of how to execute attribute operations from a command line interface. |
Rhyme | 0:37c8ecde13c2 | 413 | */ |
Rhyme | 0:37c8ecde13c2 | 414 | #ifdef ATTRIBUTE_CLI |
Rhyme | 0:37c8ecde13c2 | 415 | int afLib::parseCommand(const char *cmd) { |
Rhyme | 0:37c8ecde13c2 | 416 | if (_interrupts_pending > 0 || _writeCmd != NULL) { |
Rhyme | 0:37c8ecde13c2 | 417 | _theLog->print("Busy: "); |
Rhyme | 0:37c8ecde13c2 | 418 | _theLog->print(_interrupts_pending); |
Rhyme | 0:37c8ecde13c2 | 419 | _theLog->print(", "); |
Rhyme | 0:37c8ecde13c2 | 420 | _theLog->println(_writeCmd != NULL); |
Rhyme | 0:37c8ecde13c2 | 421 | return afERROR_BUSY; |
Rhyme | 0:37c8ecde13c2 | 422 | } |
Rhyme | 0:37c8ecde13c2 | 423 | |
Rhyme | 0:37c8ecde13c2 | 424 | int reqId = _requestId++; |
Rhyme | 0:37c8ecde13c2 | 425 | _writeCmd = new Command(_theLog,reqId, cmd); |
Rhyme | 0:37c8ecde13c2 | 426 | if (!_writeCmd->isValid()) { |
Rhyme | 0:37c8ecde13c2 | 427 | _theLog->print("BAD: "); |
Rhyme | 0:37c8ecde13c2 | 428 | _theLog->println(cmd); |
Rhyme | 0:37c8ecde13c2 | 429 | _writeCmd->dumpBytes(); |
Rhyme | 0:37c8ecde13c2 | 430 | _writeCmd->dump(); |
Rhyme | 0:37c8ecde13c2 | 431 | delete (_writeCmd); |
Rhyme | 0:37c8ecde13c2 | 432 | _writeCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 433 | return afERROR_INVALID_COMMAND; |
Rhyme | 0:37c8ecde13c2 | 434 | } |
Rhyme | 0:37c8ecde13c2 | 435 | |
Rhyme | 0:37c8ecde13c2 | 436 | // Start the transmission. |
Rhyme | 0:37c8ecde13c2 | 437 | sendCommand(); |
Rhyme | 0:37c8ecde13c2 | 438 | |
Rhyme | 0:37c8ecde13c2 | 439 | return afSUCCESS; |
Rhyme | 0:37c8ecde13c2 | 440 | } |
Rhyme | 0:37c8ecde13c2 | 441 | #endif |
Rhyme | 0:37c8ecde13c2 | 442 | |
Rhyme | 0:37c8ecde13c2 | 443 | /** |
Rhyme | 0:37c8ecde13c2 | 444 | * runStateMachine |
Rhyme | 0:37c8ecde13c2 | 445 | * |
Rhyme | 0:37c8ecde13c2 | 446 | * The state machine for afLib. This state machine is responsible for implementing the KSP SPI protocol and executing |
Rhyme | 0:37c8ecde13c2 | 447 | * attribute operations. |
Rhyme | 0:37c8ecde13c2 | 448 | * This method is run: |
Rhyme | 0:37c8ecde13c2 | 449 | * 1. In response to receiving an interrupt from the ASR-1. |
Rhyme | 0:37c8ecde13c2 | 450 | * 2. When an attribute operation is pulled out of the queue and executed. |
Rhyme | 0:37c8ecde13c2 | 451 | */ |
Rhyme | 0:37c8ecde13c2 | 452 | void afLib::runStateMachine(void) { |
Rhyme | 0:37c8ecde13c2 | 453 | if (_interrupts_pending > 0) { |
Rhyme | 0:37c8ecde13c2 | 454 | switch (_state) { |
Rhyme | 0:37c8ecde13c2 | 455 | case STATE_IDLE: |
Rhyme | 0:37c8ecde13c2 | 456 | //deathWish.attach(&afLib::kick_the_bucket,10); |
Rhyme | 0:37c8ecde13c2 | 457 | onStateIdle(); |
Rhyme | 0:37c8ecde13c2 | 458 | return; |
Rhyme | 0:37c8ecde13c2 | 459 | |
Rhyme | 0:37c8ecde13c2 | 460 | case STATE_STATUS_SYNC: |
Rhyme | 0:37c8ecde13c2 | 461 | onStateSync(); |
Rhyme | 0:37c8ecde13c2 | 462 | break; |
Rhyme | 0:37c8ecde13c2 | 463 | |
Rhyme | 0:37c8ecde13c2 | 464 | case STATE_STATUS_ACK: |
Rhyme | 0:37c8ecde13c2 | 465 | onStateAck(); |
Rhyme | 0:37c8ecde13c2 | 466 | break; |
Rhyme | 0:37c8ecde13c2 | 467 | |
Rhyme | 0:37c8ecde13c2 | 468 | case STATE_SEND_BYTES: |
Rhyme | 0:37c8ecde13c2 | 469 | onStateSendBytes(); |
Rhyme | 0:37c8ecde13c2 | 470 | break; |
Rhyme | 0:37c8ecde13c2 | 471 | |
Rhyme | 0:37c8ecde13c2 | 472 | case STATE_RECV_BYTES: |
Rhyme | 0:37c8ecde13c2 | 473 | onStateRecvBytes(); |
Rhyme | 0:37c8ecde13c2 | 474 | break; |
Rhyme | 0:37c8ecde13c2 | 475 | |
Rhyme | 0:37c8ecde13c2 | 476 | case STATE_CMD_COMPLETE: |
Rhyme | 0:37c8ecde13c2 | 477 | onStateCmdComplete(); |
Rhyme | 0:37c8ecde13c2 | 478 | break; |
Rhyme | 0:37c8ecde13c2 | 479 | } |
Rhyme | 0:37c8ecde13c2 | 480 | |
Rhyme | 0:37c8ecde13c2 | 481 | updateIntsPending(-1); |
Rhyme | 0:37c8ecde13c2 | 482 | } else { |
Rhyme | 0:37c8ecde13c2 | 483 | if (syncRetries > 0 && syncRetries < MAX_SYNC_RETRIES && checkLastSync->read_ms() - lastSync > 1000) { |
Rhyme | 0:37c8ecde13c2 | 484 | updateIntsPending(1); |
Rhyme | 0:37c8ecde13c2 | 485 | } else if (syncRetries >= MAX_SYNC_RETRIES) { |
Rhyme | 0:37c8ecde13c2 | 486 | SERIAL_PRINT_DBG_ASR("No response from ASR-1 - does profile have MCU enabled?\n"); |
Rhyme | 0:37c8ecde13c2 | 487 | #if defined(TARGET_KL25Z) |
Rhyme | 0:37c8ecde13c2 | 488 | // WatchDogWrapper::getSelf()->kick_the_bucket(); |
Rhyme | 0:37c8ecde13c2 | 489 | #endif //TARGET_KL25Z |
Rhyme | 0:37c8ecde13c2 | 490 | syncRetries = 0; |
Rhyme | 0:37c8ecde13c2 | 491 | _state = STATE_IDLE; |
Rhyme | 0:37c8ecde13c2 | 492 | } |
Rhyme | 0:37c8ecde13c2 | 493 | } |
Rhyme | 0:37c8ecde13c2 | 494 | } |
Rhyme | 0:37c8ecde13c2 | 495 | |
Rhyme | 0:37c8ecde13c2 | 496 | /** |
Rhyme | 0:37c8ecde13c2 | 497 | * onStateIdle |
Rhyme | 0:37c8ecde13c2 | 498 | * |
Rhyme | 0:37c8ecde13c2 | 499 | * If there is a command to be written, update the bytes to send. Otherwise we're sending a zero-sync message. |
Rhyme | 0:37c8ecde13c2 | 500 | * Either way advance the state to send a sync message. |
Rhyme | 0:37c8ecde13c2 | 501 | */ |
Rhyme | 0:37c8ecde13c2 | 502 | void afLib::onStateIdle(void) { |
Rhyme | 0:37c8ecde13c2 | 503 | if (_writeCmd != NULL) { |
Rhyme | 0:37c8ecde13c2 | 504 | // Include 2 bytes for length |
Rhyme | 0:37c8ecde13c2 | 505 | _bytesToSend = _writeCmd->getSize() + 2; |
Rhyme | 0:37c8ecde13c2 | 506 | } else { |
Rhyme | 0:37c8ecde13c2 | 507 | _bytesToSend = 0; |
Rhyme | 0:37c8ecde13c2 | 508 | } |
Rhyme | 0:37c8ecde13c2 | 509 | _state = STATE_STATUS_SYNC; |
Rhyme | 0:37c8ecde13c2 | 510 | printState(_state); |
Rhyme | 0:37c8ecde13c2 | 511 | } |
Rhyme | 0:37c8ecde13c2 | 512 | |
Rhyme | 0:37c8ecde13c2 | 513 | /** |
Rhyme | 0:37c8ecde13c2 | 514 | * onStateSync |
Rhyme | 0:37c8ecde13c2 | 515 | * |
Rhyme | 0:37c8ecde13c2 | 516 | * Write a sync message over SPI to let the ASR-1 know that we want to send some data. |
Rhyme | 0:37c8ecde13c2 | 517 | * Check for a "collision" which occurs if the ASR-1 is trying to send us data at the same time. |
Rhyme | 0:37c8ecde13c2 | 518 | */ |
Rhyme | 0:37c8ecde13c2 | 519 | void afLib::onStateSync(void) { |
Rhyme | 0:37c8ecde13c2 | 520 | int result; |
Rhyme | 0:37c8ecde13c2 | 521 | |
Rhyme | 0:37c8ecde13c2 | 522 | _txStatus->setAck(false); |
Rhyme | 0:37c8ecde13c2 | 523 | _txStatus->setBytesToSend(_bytesToSend); |
Rhyme | 0:37c8ecde13c2 | 524 | _txStatus->setBytesToRecv(0); |
Rhyme | 0:37c8ecde13c2 | 525 | |
Rhyme | 0:37c8ecde13c2 | 526 | result = exchangeStatus(_txStatus, _rxStatus); |
Rhyme | 0:37c8ecde13c2 | 527 | |
Rhyme | 0:37c8ecde13c2 | 528 | if (result == afSUCCESS && _rxStatus->isValid() && inSync(_txStatus, _rxStatus)) { |
Rhyme | 0:37c8ecde13c2 | 529 | syncRetries = 0; // Flag that sync completed. |
Rhyme | 0:37c8ecde13c2 | 530 | _state = STATE_STATUS_ACK; |
Rhyme | 0:37c8ecde13c2 | 531 | if (_txStatus->getBytesToSend() == 0 && _rxStatus->getBytesToRecv() > 0) { |
Rhyme | 0:37c8ecde13c2 | 532 | _bytesToRecv = _rxStatus->getBytesToRecv(); |
Rhyme | 0:37c8ecde13c2 | 533 | } |
Rhyme | 0:37c8ecde13c2 | 534 | } else { |
Rhyme | 0:37c8ecde13c2 | 535 | // Try resending the preamble |
Rhyme | 0:37c8ecde13c2 | 536 | _state = STATE_STATUS_SYNC; |
Rhyme | 0:37c8ecde13c2 | 537 | lastSync = checkLastSync->read_ms(); |
Rhyme | 0:37c8ecde13c2 | 538 | syncRetries++; |
Rhyme | 0:37c8ecde13c2 | 539 | // _txStatus->dumpBytes(); |
Rhyme | 0:37c8ecde13c2 | 540 | // _rxStatus->dumpBytes(); |
Rhyme | 0:37c8ecde13c2 | 541 | } |
Rhyme | 0:37c8ecde13c2 | 542 | printState(_state); |
Rhyme | 0:37c8ecde13c2 | 543 | } |
Rhyme | 0:37c8ecde13c2 | 544 | |
Rhyme | 0:37c8ecde13c2 | 545 | /** |
Rhyme | 0:37c8ecde13c2 | 546 | * onStateAck |
Rhyme | 0:37c8ecde13c2 | 547 | * |
Rhyme | 0:37c8ecde13c2 | 548 | * Acknowledge the previous sync message and advance the state. |
Rhyme | 0:37c8ecde13c2 | 549 | * If there are bytes to send, advance to send bytes state. |
Rhyme | 0:37c8ecde13c2 | 550 | * If there are bytes to receive, advance to receive bytes state. |
Rhyme | 0:37c8ecde13c2 | 551 | * Otherwise it was a zero-sync so advance to command complete. |
Rhyme | 0:37c8ecde13c2 | 552 | */ |
Rhyme | 0:37c8ecde13c2 | 553 | void afLib::onStateAck(void) { |
Rhyme | 0:37c8ecde13c2 | 554 | int result; |
Rhyme | 0:37c8ecde13c2 | 555 | |
Rhyme | 0:37c8ecde13c2 | 556 | _txStatus->setAck(true); |
Rhyme | 0:37c8ecde13c2 | 557 | _txStatus->setBytesToRecv(_rxStatus->getBytesToRecv()); |
Rhyme | 0:37c8ecde13c2 | 558 | _bytesToRecv = _rxStatus->getBytesToRecv(); |
Rhyme | 0:37c8ecde13c2 | 559 | result = writeStatus(_txStatus); |
Rhyme | 0:37c8ecde13c2 | 560 | if (result != afSUCCESS) { |
Rhyme | 0:37c8ecde13c2 | 561 | _state = STATE_STATUS_SYNC; |
Rhyme | 0:37c8ecde13c2 | 562 | printState(_state); |
Rhyme | 0:37c8ecde13c2 | 563 | return; |
Rhyme | 0:37c8ecde13c2 | 564 | } |
Rhyme | 0:37c8ecde13c2 | 565 | if (_bytesToSend > 0) { |
Rhyme | 0:37c8ecde13c2 | 566 | _writeBufferLen = (uint16_t) _writeCmd->getSize(); |
Rhyme | 0:37c8ecde13c2 | 567 | _writeBuffer = new uint8_t[_bytesToSend]; |
Rhyme | 0:37c8ecde13c2 | 568 | memcpy(_writeBuffer, (uint8_t * ) & _writeBufferLen, 2); |
Rhyme | 0:37c8ecde13c2 | 569 | _writeCmd->getBytes(&_writeBuffer[2]); |
Rhyme | 0:37c8ecde13c2 | 570 | _state = STATE_SEND_BYTES; |
Rhyme | 0:37c8ecde13c2 | 571 | } else if (_bytesToRecv > 0) { |
Rhyme | 0:37c8ecde13c2 | 572 | _state = STATE_RECV_BYTES; |
Rhyme | 0:37c8ecde13c2 | 573 | } else { |
Rhyme | 0:37c8ecde13c2 | 574 | _state = STATE_CMD_COMPLETE; |
Rhyme | 0:37c8ecde13c2 | 575 | } |
Rhyme | 0:37c8ecde13c2 | 576 | printState(_state); |
Rhyme | 0:37c8ecde13c2 | 577 | } |
Rhyme | 0:37c8ecde13c2 | 578 | |
Rhyme | 0:37c8ecde13c2 | 579 | /** |
Rhyme | 0:37c8ecde13c2 | 580 | * onStateSendBytes |
Rhyme | 0:37c8ecde13c2 | 581 | * |
Rhyme | 0:37c8ecde13c2 | 582 | * Send the required number of bytes to the ASR-1 and then advance to command complete. |
Rhyme | 0:37c8ecde13c2 | 583 | */ |
Rhyme | 0:37c8ecde13c2 | 584 | void afLib::onStateSendBytes(void) { |
Rhyme | 0:37c8ecde13c2 | 585 | // _theLog->print("send bytes: "); _theLog->println(_bytesToSend); |
Rhyme | 0:37c8ecde13c2 | 586 | sendBytes(); |
Rhyme | 0:37c8ecde13c2 | 587 | |
Rhyme | 0:37c8ecde13c2 | 588 | if (_bytesToSend == 0) { |
Rhyme | 0:37c8ecde13c2 | 589 | _writeBufferLen = 0; |
Rhyme | 0:37c8ecde13c2 | 590 | delete[] (_writeBuffer); //wsugi delete (_writeBuffer); |
Rhyme | 0:37c8ecde13c2 | 591 | _writeBuffer = NULL; |
Rhyme | 0:37c8ecde13c2 | 592 | _state = STATE_CMD_COMPLETE; |
Rhyme | 0:37c8ecde13c2 | 593 | printState(_state); |
Rhyme | 0:37c8ecde13c2 | 594 | } |
Rhyme | 0:37c8ecde13c2 | 595 | } |
Rhyme | 0:37c8ecde13c2 | 596 | |
Rhyme | 0:37c8ecde13c2 | 597 | /** |
Rhyme | 0:37c8ecde13c2 | 598 | * onStateRecvBytes |
Rhyme | 0:37c8ecde13c2 | 599 | * |
Rhyme | 0:37c8ecde13c2 | 600 | * Receive the required number of bytes from the ASR-1 and then advance to command complete. |
Rhyme | 0:37c8ecde13c2 | 601 | */ |
Rhyme | 0:37c8ecde13c2 | 602 | void afLib::onStateRecvBytes(void) { |
Rhyme | 0:37c8ecde13c2 | 603 | // _theLog->print("receive bytes: "); _theLog->println(_bytesToRecv); |
Rhyme | 0:37c8ecde13c2 | 604 | recvBytes(); |
Rhyme | 0:37c8ecde13c2 | 605 | if (_bytesToRecv == 0) { |
Rhyme | 0:37c8ecde13c2 | 606 | _state = STATE_CMD_COMPLETE; |
Rhyme | 0:37c8ecde13c2 | 607 | printState(_state); |
Rhyme | 0:37c8ecde13c2 | 608 | _readCmd = new Command(_readBufferLen, &_readBuffer[2]); |
Rhyme | 0:37c8ecde13c2 | 609 | delete[] (_readBuffer); //wsugi delete (_readBuffer); |
Rhyme | 0:37c8ecde13c2 | 610 | _readBuffer = NULL; |
Rhyme | 0:37c8ecde13c2 | 611 | } |
Rhyme | 0:37c8ecde13c2 | 612 | } |
Rhyme | 0:37c8ecde13c2 | 613 | |
Rhyme | 0:37c8ecde13c2 | 614 | /** |
Rhyme | 0:37c8ecde13c2 | 615 | * onStateCmdComplete |
Rhyme | 0:37c8ecde13c2 | 616 | * |
Rhyme | 0:37c8ecde13c2 | 617 | * Call the appropriate sketch callback to report the result of the command. |
Rhyme | 0:37c8ecde13c2 | 618 | * Clear the command object and go back to waiting for the next interrupt or command. |
Rhyme | 0:37c8ecde13c2 | 619 | */ |
Rhyme | 0:37c8ecde13c2 | 620 | void afLib::onStateCmdComplete(void) { |
Rhyme | 0:37c8ecde13c2 | 621 | _state = STATE_IDLE; |
Rhyme | 0:37c8ecde13c2 | 622 | printState(_state); |
Rhyme | 0:37c8ecde13c2 | 623 | if (_readCmd != NULL) { |
Rhyme | 0:37c8ecde13c2 | 624 | uint8_t *val = new uint8_t[_readCmd->getValueLen()]; |
Rhyme | 0:37c8ecde13c2 | 625 | _readCmd->getValue(val); |
Rhyme | 0:37c8ecde13c2 | 626 | |
Rhyme | 0:37c8ecde13c2 | 627 | switch (_readCmd->getCommand()) { |
Rhyme | 0:37c8ecde13c2 | 628 | case MSG_TYPE_SET: |
Rhyme | 0:37c8ecde13c2 | 629 | _onAttrSet(_readCmd->getReqId(), _readCmd->getAttrId(), _readCmd->getValueLen(), val); |
Rhyme | 0:37c8ecde13c2 | 630 | break; |
Rhyme | 0:37c8ecde13c2 | 631 | |
Rhyme | 0:37c8ecde13c2 | 632 | case MSG_TYPE_UPDATE: |
Rhyme | 0:37c8ecde13c2 | 633 | if (_readCmd->getAttrId() == _outstandingSetGetAttrId) { |
Rhyme | 0:37c8ecde13c2 | 634 | _outstandingSetGetAttrId = 0; |
Rhyme | 0:37c8ecde13c2 | 635 | } |
Rhyme | 0:37c8ecde13c2 | 636 | _onAttrSetComplete(_readCmd->getReqId(), _readCmd->getAttrId(), _readCmd->getValueLen(), val); |
Rhyme | 0:37c8ecde13c2 | 637 | break; |
Rhyme | 0:37c8ecde13c2 | 638 | |
Rhyme | 0:37c8ecde13c2 | 639 | default: |
Rhyme | 0:37c8ecde13c2 | 640 | break; |
Rhyme | 0:37c8ecde13c2 | 641 | } |
Rhyme | 0:37c8ecde13c2 | 642 | delete[] (val); //wsugi delete (val); |
Rhyme | 0:37c8ecde13c2 | 643 | delete (_readCmd); |
Rhyme | 0:37c8ecde13c2 | 644 | _readCmdOffset = 0; |
Rhyme | 0:37c8ecde13c2 | 645 | _readCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 646 | } |
Rhyme | 0:37c8ecde13c2 | 647 | |
Rhyme | 0:37c8ecde13c2 | 648 | if (_writeCmd != NULL) { |
Rhyme | 0:37c8ecde13c2 | 649 | // Fake a callback here for MCU attributes as we don't get one from the module. |
Rhyme | 0:37c8ecde13c2 | 650 | if (_writeCmd->getCommand() == MSG_TYPE_UPDATE && IS_MCU_ATTR(_writeCmd->getAttrId())) { |
Rhyme | 0:37c8ecde13c2 | 651 | _onAttrSetComplete(_writeCmd->getReqId(), _writeCmd->getAttrId(), _writeCmd->getValueLen(), _writeCmd->getValueP()); |
Rhyme | 0:37c8ecde13c2 | 652 | } |
Rhyme | 0:37c8ecde13c2 | 653 | delete (_writeCmd); |
Rhyme | 0:37c8ecde13c2 | 654 | _writeCmdOffset = 0; |
Rhyme | 0:37c8ecde13c2 | 655 | _writeCmd = NULL; |
Rhyme | 0:37c8ecde13c2 | 656 | } |
Rhyme | 0:37c8ecde13c2 | 657 | } |
Rhyme | 0:37c8ecde13c2 | 658 | |
Rhyme | 0:37c8ecde13c2 | 659 | /** |
Rhyme | 0:37c8ecde13c2 | 660 | * exchangeStatus |
Rhyme | 0:37c8ecde13c2 | 661 | * |
Rhyme | 0:37c8ecde13c2 | 662 | * Write a status command object to the ASR-1 and clock in a status object from the ASR-1 at the same time. |
Rhyme | 0:37c8ecde13c2 | 663 | */ |
Rhyme | 0:37c8ecde13c2 | 664 | int afLib::exchangeStatus(StatusCommand *tx, StatusCommand *rx) { |
Rhyme | 0:37c8ecde13c2 | 665 | int result = afSUCCESS; |
Rhyme | 0:37c8ecde13c2 | 666 | uint16_t len = tx->getSize(); |
Rhyme | 0:37c8ecde13c2 | 667 | int bytes[len]; |
Rhyme | 0:37c8ecde13c2 | 668 | char rbytes[len+1]; |
Rhyme | 0:37c8ecde13c2 | 669 | int index = 0; |
Rhyme | 0:37c8ecde13c2 | 670 | tx->getBytes(bytes); |
Rhyme | 0:37c8ecde13c2 | 671 | |
Rhyme | 0:37c8ecde13c2 | 672 | // _theSPI->beginSPI(); |
Rhyme | 0:37c8ecde13c2 | 673 | |
Rhyme | 0:37c8ecde13c2 | 674 | for (int i=0;i<len;i++) |
Rhyme | 0:37c8ecde13c2 | 675 | { |
Rhyme | 0:37c8ecde13c2 | 676 | rbytes[i]=bytes[i]; |
Rhyme | 0:37c8ecde13c2 | 677 | } |
Rhyme | 0:37c8ecde13c2 | 678 | rbytes[len]=tx->getChecksum(); |
Rhyme | 0:37c8ecde13c2 | 679 | |
Rhyme | 0:37c8ecde13c2 | 680 | printTransaction((uint8_t*)rbytes,len+1); |
Rhyme | 0:37c8ecde13c2 | 681 | |
Rhyme | 0:37c8ecde13c2 | 682 | _theSPI->beginSPI(); |
Rhyme | 0:37c8ecde13c2 | 683 | _theSPI->transfer(rbytes,len+1); |
Rhyme | 0:37c8ecde13c2 | 684 | _theSPI->endSPI(); |
Rhyme | 0:37c8ecde13c2 | 685 | |
Rhyme | 0:37c8ecde13c2 | 686 | printTransaction((uint8_t*)rbytes,len+1); |
Rhyme | 0:37c8ecde13c2 | 687 | |
Rhyme | 0:37c8ecde13c2 | 688 | uint8_t cmd = bytes[index++]; |
Rhyme | 0:37c8ecde13c2 | 689 | if (cmd != 0x30 && cmd != 0x31) { |
Rhyme | 0:37c8ecde13c2 | 690 | SERIAL_PRINT_DBG_ASR("exchangeStatus bad cmd: 0x%02x\n",cmd); |
Rhyme | 0:37c8ecde13c2 | 691 | result = afERROR_INVALID_COMMAND; |
Rhyme | 0:37c8ecde13c2 | 692 | } |
Rhyme | 0:37c8ecde13c2 | 693 | |
Rhyme | 0:37c8ecde13c2 | 694 | rx->setBytesToSend(rbytes[index + 0] | (rbytes[index + 1] << 8)); |
Rhyme | 0:37c8ecde13c2 | 695 | rx->setBytesToRecv(rbytes[index + 2] | (rbytes[index + 3] << 8)); |
Rhyme | 0:37c8ecde13c2 | 696 | rx->setChecksum(rbytes[index+4]); |
Rhyme | 0:37c8ecde13c2 | 697 | //_theSPI->endSPI(); |
Rhyme | 0:37c8ecde13c2 | 698 | return result; |
Rhyme | 0:37c8ecde13c2 | 699 | } |
Rhyme | 0:37c8ecde13c2 | 700 | |
Rhyme | 0:37c8ecde13c2 | 701 | /** |
Rhyme | 0:37c8ecde13c2 | 702 | * inSync |
Rhyme | 0:37c8ecde13c2 | 703 | * |
Rhyme | 0:37c8ecde13c2 | 704 | * Check to make sure the Arduino and the ASR-1 aren't trying to send data at the same time. |
Rhyme | 0:37c8ecde13c2 | 705 | * Return true only if there is no collision. |
Rhyme | 0:37c8ecde13c2 | 706 | */ |
Rhyme | 0:37c8ecde13c2 | 707 | bool afLib::inSync(StatusCommand *tx, StatusCommand *rx) { |
Rhyme | 0:37c8ecde13c2 | 708 | return (tx->getBytesToSend() == 0 && rx->getBytesToRecv() == 0) || |
Rhyme | 0:37c8ecde13c2 | 709 | (tx->getBytesToSend() > 0 && rx->getBytesToRecv() == 0) || |
Rhyme | 0:37c8ecde13c2 | 710 | (tx->getBytesToSend() == 0 && rx->getBytesToRecv() > 0); |
Rhyme | 0:37c8ecde13c2 | 711 | } |
Rhyme | 0:37c8ecde13c2 | 712 | |
Rhyme | 0:37c8ecde13c2 | 713 | /** |
Rhyme | 0:37c8ecde13c2 | 714 | * writeStatus |
Rhyme | 0:37c8ecde13c2 | 715 | * |
Rhyme | 0:37c8ecde13c2 | 716 | * Write a status command to the ASR-1 and ignore the result. If you want to read bytes at the same time, use |
Rhyme | 0:37c8ecde13c2 | 717 | * exchangeStatus instead. |
Rhyme | 0:37c8ecde13c2 | 718 | */ |
Rhyme | 0:37c8ecde13c2 | 719 | int afLib::writeStatus(StatusCommand *c) { |
Rhyme | 0:37c8ecde13c2 | 720 | int result = afSUCCESS; |
Rhyme | 0:37c8ecde13c2 | 721 | uint16_t len = c->getSize(); |
Rhyme | 0:37c8ecde13c2 | 722 | int bytes[len]; |
Rhyme | 0:37c8ecde13c2 | 723 | char rbytes[len+1]; |
Rhyme | 0:37c8ecde13c2 | 724 | int index = 0; |
Rhyme | 0:37c8ecde13c2 | 725 | c->getBytes(bytes); |
Rhyme | 0:37c8ecde13c2 | 726 | |
Rhyme | 0:37c8ecde13c2 | 727 | _theSPI->beginSPI(); |
Rhyme | 0:37c8ecde13c2 | 728 | |
Rhyme | 0:37c8ecde13c2 | 729 | for (int i=0;i<len;i++) |
Rhyme | 0:37c8ecde13c2 | 730 | { |
Rhyme | 0:37c8ecde13c2 | 731 | rbytes[i]=bytes[i]; |
Rhyme | 0:37c8ecde13c2 | 732 | } |
Rhyme | 0:37c8ecde13c2 | 733 | rbytes[len]=c->getChecksum(); |
Rhyme | 0:37c8ecde13c2 | 734 | printTransaction((uint8_t*)rbytes,len+1); |
Rhyme | 0:37c8ecde13c2 | 735 | _theSPI->transfer(rbytes,len+1); |
Rhyme | 0:37c8ecde13c2 | 736 | printTransaction((uint8_t*)rbytes,len+1); |
Rhyme | 0:37c8ecde13c2 | 737 | uint8_t cmd = rbytes[index++]; |
Rhyme | 0:37c8ecde13c2 | 738 | if (cmd != 0x30 && cmd != 0x31) { |
Rhyme | 0:37c8ecde13c2 | 739 | SERIAL_PRINT_DBG_ASR("writeStatus bad cmd: 0x%02x\n",cmd); |
Rhyme | 0:37c8ecde13c2 | 740 | result = afERROR_INVALID_COMMAND; |
Rhyme | 0:37c8ecde13c2 | 741 | } |
Rhyme | 0:37c8ecde13c2 | 742 | |
Rhyme | 0:37c8ecde13c2 | 743 | |
Rhyme | 0:37c8ecde13c2 | 744 | _theSPI->endSPI(); |
Rhyme | 0:37c8ecde13c2 | 745 | |
Rhyme | 0:37c8ecde13c2 | 746 | // c->dump(); |
Rhyme | 0:37c8ecde13c2 | 747 | // c->dumpBytes(); |
Rhyme | 0:37c8ecde13c2 | 748 | |
Rhyme | 0:37c8ecde13c2 | 749 | return result; |
Rhyme | 0:37c8ecde13c2 | 750 | } |
Rhyme | 0:37c8ecde13c2 | 751 | |
Rhyme | 0:37c8ecde13c2 | 752 | /** |
Rhyme | 0:37c8ecde13c2 | 753 | * sendBytes |
Rhyme | 0:37c8ecde13c2 | 754 | * |
Rhyme | 0:37c8ecde13c2 | 755 | * Send the specified number of data bytes to the ASR-1. Do this in chunks of SPI_FRAME_LEN bytes. |
Rhyme | 0:37c8ecde13c2 | 756 | */ |
Rhyme | 0:37c8ecde13c2 | 757 | void afLib::sendBytes() { |
Rhyme | 0:37c8ecde13c2 | 758 | uint16_t len = _bytesToSend > SPI_FRAME_LEN ? SPI_FRAME_LEN : _bytesToSend; |
Rhyme | 0:37c8ecde13c2 | 759 | uint8_t bytes[SPI_FRAME_LEN]; |
Rhyme | 0:37c8ecde13c2 | 760 | memset(bytes, 0xff, sizeof(bytes)); |
Rhyme | 0:37c8ecde13c2 | 761 | |
Rhyme | 0:37c8ecde13c2 | 762 | memcpy(bytes, &_writeBuffer[_writeCmdOffset], len); |
Rhyme | 0:37c8ecde13c2 | 763 | |
Rhyme | 0:37c8ecde13c2 | 764 | _theSPI->beginSPI(); |
Rhyme | 0:37c8ecde13c2 | 765 | printTransaction(bytes,len+1); |
Rhyme | 0:37c8ecde13c2 | 766 | _theSPI->transfer((char *)bytes,len); |
Rhyme | 0:37c8ecde13c2 | 767 | printTransaction(bytes,len+1); |
Rhyme | 0:37c8ecde13c2 | 768 | _theSPI->endSPI(); |
Rhyme | 0:37c8ecde13c2 | 769 | |
Rhyme | 0:37c8ecde13c2 | 770 | // dumpBytes("Sending:", len, bytes); |
Rhyme | 0:37c8ecde13c2 | 771 | |
Rhyme | 0:37c8ecde13c2 | 772 | _writeCmdOffset += len; |
Rhyme | 0:37c8ecde13c2 | 773 | _bytesToSend -= len; |
Rhyme | 0:37c8ecde13c2 | 774 | } |
Rhyme | 0:37c8ecde13c2 | 775 | |
Rhyme | 0:37c8ecde13c2 | 776 | /** |
Rhyme | 0:37c8ecde13c2 | 777 | * recvBytes |
Rhyme | 0:37c8ecde13c2 | 778 | * |
Rhyme | 0:37c8ecde13c2 | 779 | * Receive the specified number of data bytes from the ASR-1. Do this in chunks of SPI_FRAME_LEN bytes. |
Rhyme | 0:37c8ecde13c2 | 780 | */ |
Rhyme | 0:37c8ecde13c2 | 781 | void afLib::recvBytes() { |
Rhyme | 0:37c8ecde13c2 | 782 | uint16_t len = _bytesToRecv > SPI_FRAME_LEN ? SPI_FRAME_LEN : _bytesToRecv; |
Rhyme | 0:37c8ecde13c2 | 783 | |
Rhyme | 0:37c8ecde13c2 | 784 | if (_readCmdOffset == 0) { |
Rhyme | 0:37c8ecde13c2 | 785 | _readBufferLen = _bytesToRecv; |
Rhyme | 0:37c8ecde13c2 | 786 | _readBuffer = new uint8_t[_readBufferLen]; |
Rhyme | 0:37c8ecde13c2 | 787 | } |
Rhyme | 0:37c8ecde13c2 | 788 | |
Rhyme | 0:37c8ecde13c2 | 789 | _theSPI->beginSPI(); |
Rhyme | 0:37c8ecde13c2 | 790 | |
Rhyme | 0:37c8ecde13c2 | 791 | |
Rhyme | 0:37c8ecde13c2 | 792 | char * start =(char*)_readBuffer + _readCmdOffset; |
Rhyme | 0:37c8ecde13c2 | 793 | printTransaction((uint8_t*)start,len+1); |
Rhyme | 0:37c8ecde13c2 | 794 | _theSPI->transfer(start,len); |
Rhyme | 0:37c8ecde13c2 | 795 | printTransaction((uint8_t*)start,len+1); |
Rhyme | 0:37c8ecde13c2 | 796 | |
Rhyme | 0:37c8ecde13c2 | 797 | _theSPI->endSPI(); |
Rhyme | 0:37c8ecde13c2 | 798 | |
Rhyme | 0:37c8ecde13c2 | 799 | // dumpBytes("Receiving:", len, _readBuffer); |
Rhyme | 0:37c8ecde13c2 | 800 | |
Rhyme | 0:37c8ecde13c2 | 801 | _readCmdOffset += len; |
Rhyme | 0:37c8ecde13c2 | 802 | _bytesToRecv -= len; |
Rhyme | 0:37c8ecde13c2 | 803 | } |
Rhyme | 0:37c8ecde13c2 | 804 | |
Rhyme | 0:37c8ecde13c2 | 805 | /** |
Rhyme | 0:37c8ecde13c2 | 806 | * isIdle |
Rhyme | 0:37c8ecde13c2 | 807 | * |
Rhyme | 0:37c8ecde13c2 | 808 | * Provide a way for the sketch to know if we're idle. Returns true if there are no attribute operations in progress. |
Rhyme | 0:37c8ecde13c2 | 809 | */ |
Rhyme | 0:37c8ecde13c2 | 810 | bool afLib::isIdle() { |
Rhyme | 0:37c8ecde13c2 | 811 | return _interrupts_pending == 0 && _state == STATE_IDLE && _outstandingSetGetAttrId == 0; |
Rhyme | 0:37c8ecde13c2 | 812 | } |
Rhyme | 0:37c8ecde13c2 | 813 | |
Rhyme | 0:37c8ecde13c2 | 814 | /** |
Rhyme | 0:37c8ecde13c2 | 815 | * These methods are required to disable/enable interrupts for the Linux version of afLib. |
Rhyme | 0:37c8ecde13c2 | 816 | * They are no-ops on Arduino. |
Rhyme | 0:37c8ecde13c2 | 817 | */ |
Rhyme | 0:37c8ecde13c2 | 818 | #ifndef ARDUINO |
Rhyme | 0:37c8ecde13c2 | 819 | void noInterrupts(){} |
Rhyme | 0:37c8ecde13c2 | 820 | void interrupts(){} |
Rhyme | 0:37c8ecde13c2 | 821 | #endif |
Rhyme | 0:37c8ecde13c2 | 822 | |
Rhyme | 0:37c8ecde13c2 | 823 | void afLib::mcuISR() { |
Rhyme | 0:37c8ecde13c2 | 824 | // _theLog->println("mcu"); |
Rhyme | 0:37c8ecde13c2 | 825 | updateIntsPending(1); |
Rhyme | 0:37c8ecde13c2 | 826 | } |
Rhyme | 0:37c8ecde13c2 | 827 | |
Rhyme | 0:37c8ecde13c2 | 828 | /**************************************************************************** |
Rhyme | 0:37c8ecde13c2 | 829 | * Queue Methods * |
Rhyme | 0:37c8ecde13c2 | 830 | ****************************************************************************/ |
Rhyme | 0:37c8ecde13c2 | 831 | /** |
Rhyme | 0:37c8ecde13c2 | 832 | * queueInit |
Rhyme | 0:37c8ecde13c2 | 833 | * |
Rhyme | 0:37c8ecde13c2 | 834 | * Create a small queue to prevent flooding the ASR-1 with attribute operations. |
Rhyme | 0:37c8ecde13c2 | 835 | * The initial size is small to allow running on small boards like UNO. |
Rhyme | 0:37c8ecde13c2 | 836 | * Size can be increased on larger boards. |
Rhyme | 0:37c8ecde13c2 | 837 | */ |
Rhyme | 0:37c8ecde13c2 | 838 | void afLib::queueInit() { |
Rhyme | 0:37c8ecde13c2 | 839 | for (int i = 0; i < REQUEST_QUEUE_SIZE; i++) { |
Rhyme | 0:37c8ecde13c2 | 840 | _requestQueue[i].p_value = NULL; |
Rhyme | 0:37c8ecde13c2 | 841 | } |
Rhyme | 0:37c8ecde13c2 | 842 | } |
Rhyme | 0:37c8ecde13c2 | 843 | |
Rhyme | 0:37c8ecde13c2 | 844 | /** |
Rhyme | 0:37c8ecde13c2 | 845 | * queuePut |
Rhyme | 0:37c8ecde13c2 | 846 | * |
Rhyme | 0:37c8ecde13c2 | 847 | * Add an item to the end of the queue. Return an error if we're out of space in the queue. |
Rhyme | 0:37c8ecde13c2 | 848 | */ |
Rhyme | 0:37c8ecde13c2 | 849 | int afLib::queuePut(uint8_t messageType, uint8_t requestId, const uint16_t attributeId, uint16_t valueLen, |
Rhyme | 0:37c8ecde13c2 | 850 | const uint8_t *value) { |
Rhyme | 0:37c8ecde13c2 | 851 | for (int i = 0; i < REQUEST_QUEUE_SIZE; i++) { |
Rhyme | 0:37c8ecde13c2 | 852 | if (_requestQueue[i].p_value == NULL) { |
Rhyme | 0:37c8ecde13c2 | 853 | _requestQueue[i].messageType = messageType; |
Rhyme | 0:37c8ecde13c2 | 854 | _requestQueue[i].attrId = attributeId; |
Rhyme | 0:37c8ecde13c2 | 855 | _requestQueue[i].requestId = requestId; |
Rhyme | 0:37c8ecde13c2 | 856 | _requestQueue[i].valueLen = valueLen; |
Rhyme | 0:37c8ecde13c2 | 857 | _requestQueue[i].p_value = new uint8_t[valueLen]; |
Rhyme | 0:37c8ecde13c2 | 858 | memcpy(_requestQueue[i].p_value, value, valueLen); |
Rhyme | 0:37c8ecde13c2 | 859 | return afSUCCESS; |
Rhyme | 0:37c8ecde13c2 | 860 | } |
Rhyme | 0:37c8ecde13c2 | 861 | } |
Rhyme | 0:37c8ecde13c2 | 862 | |
Rhyme | 0:37c8ecde13c2 | 863 | return afERROR_QUEUE_OVERFLOW; |
Rhyme | 0:37c8ecde13c2 | 864 | } |
Rhyme | 0:37c8ecde13c2 | 865 | |
Rhyme | 0:37c8ecde13c2 | 866 | /** |
Rhyme | 0:37c8ecde13c2 | 867 | * queueGet |
Rhyme | 0:37c8ecde13c2 | 868 | * |
Rhyme | 0:37c8ecde13c2 | 869 | * Pull and return the oldest item from the queue. Return an error if the queue is empty. |
Rhyme | 0:37c8ecde13c2 | 870 | */ |
Rhyme | 0:37c8ecde13c2 | 871 | int afLib::queueGet(uint8_t *messageType, uint8_t *requestId, uint16_t *attributeId, uint16_t *valueLen, |
Rhyme | 0:37c8ecde13c2 | 872 | uint8_t **value) { |
Rhyme | 0:37c8ecde13c2 | 873 | for (int i = 0; i < REQUEST_QUEUE_SIZE; i++) { |
Rhyme | 0:37c8ecde13c2 | 874 | if (_requestQueue[i].p_value != NULL) { |
Rhyme | 0:37c8ecde13c2 | 875 | *messageType = _requestQueue[i].messageType; |
Rhyme | 0:37c8ecde13c2 | 876 | *attributeId = _requestQueue[i].attrId; |
Rhyme | 0:37c8ecde13c2 | 877 | *requestId = _requestQueue[i].requestId; |
Rhyme | 0:37c8ecde13c2 | 878 | *valueLen = _requestQueue[i].valueLen; |
Rhyme | 0:37c8ecde13c2 | 879 | *value = new uint8_t[*valueLen]; |
Rhyme | 0:37c8ecde13c2 | 880 | memcpy(*value, _requestQueue[i].p_value, *valueLen); |
Rhyme | 0:37c8ecde13c2 | 881 | delete[] (_requestQueue[i].p_value); //wsugi delete (_requestQueue[i].p_value); |
Rhyme | 0:37c8ecde13c2 | 882 | _requestQueue[i].p_value = NULL; |
Rhyme | 0:37c8ecde13c2 | 883 | return afSUCCESS; |
Rhyme | 0:37c8ecde13c2 | 884 | } |
Rhyme | 0:37c8ecde13c2 | 885 | } |
Rhyme | 0:37c8ecde13c2 | 886 | |
Rhyme | 0:37c8ecde13c2 | 887 | return afERROR_QUEUE_UNDERFLOW; |
Rhyme | 0:37c8ecde13c2 | 888 | } |
Rhyme | 0:37c8ecde13c2 | 889 | |
Rhyme | 0:37c8ecde13c2 | 890 | /**************************************************************************** |
Rhyme | 0:37c8ecde13c2 | 891 | * Debug Methods * |
Rhyme | 0:37c8ecde13c2 | 892 | ****************************************************************************/ |
Rhyme | 0:37c8ecde13c2 | 893 | /** |
Rhyme | 0:37c8ecde13c2 | 894 | * dumpBytes |
Rhyme | 0:37c8ecde13c2 | 895 | * |
Rhyme | 0:37c8ecde13c2 | 896 | * Dump a byte buffer to the debug log. |
Rhyme | 0:37c8ecde13c2 | 897 | */ |
Rhyme | 0:37c8ecde13c2 | 898 | void afLib::dumpBytes(char *label, int len, uint8_t *bytes) { |
Rhyme | 0:37c8ecde13c2 | 899 | SERIAL_PRINT_DBG_ASR("%s\n",label); |
Rhyme | 0:37c8ecde13c2 | 900 | for (int i = 0; i < len; i++) { |
Rhyme | 0:37c8ecde13c2 | 901 | if (i > 0) { |
Rhyme | 0:37c8ecde13c2 | 902 | SERIAL_PRINT_DBG_ASR(", "); |
Rhyme | 0:37c8ecde13c2 | 903 | } |
Rhyme | 0:37c8ecde13c2 | 904 | uint8_t b = bytes[i] & 0xff; |
Rhyme | 0:37c8ecde13c2 | 905 | |
Rhyme | 0:37c8ecde13c2 | 906 | if (b < 0x10) { |
Rhyme | 0:37c8ecde13c2 | 907 | SERIAL_PRINT_DBG_ASR("0x02x", b); |
Rhyme | 0:37c8ecde13c2 | 908 | } else { |
Rhyme | 0:37c8ecde13c2 | 909 | //_theLog->print("0x"); |
Rhyme | 0:37c8ecde13c2 | 910 | SERIAL_PRINT_DBG_ASR("0x02x",b);//, HEX); |
Rhyme | 0:37c8ecde13c2 | 911 | } |
Rhyme | 0:37c8ecde13c2 | 912 | } |
Rhyme | 0:37c8ecde13c2 | 913 | SERIAL_PRINT_DBG_ASR("\n"); |
Rhyme | 0:37c8ecde13c2 | 914 | } |
Rhyme | 0:37c8ecde13c2 | 915 | |
Rhyme | 0:37c8ecde13c2 | 916 | /** |
Rhyme | 0:37c8ecde13c2 | 917 | * printState |
Rhyme | 0:37c8ecde13c2 | 918 | * |
Rhyme | 0:37c8ecde13c2 | 919 | * Print the current state of the afLib state machine. For debugging, just remove the return statement. |
Rhyme | 0:37c8ecde13c2 | 920 | */ |
Rhyme | 0:37c8ecde13c2 | 921 | void afLib::printState(int state) { |
Rhyme | 0:37c8ecde13c2 | 922 | // return; |
Rhyme | 0:37c8ecde13c2 | 923 | switch (state) { |
Rhyme | 0:37c8ecde13c2 | 924 | case STATE_IDLE: |
Rhyme | 0:37c8ecde13c2 | 925 | SERIAL_PRINT_DBG_ASR("STATE_IDLE\n"); |
Rhyme | 0:37c8ecde13c2 | 926 | break; |
Rhyme | 0:37c8ecde13c2 | 927 | case STATE_STATUS_SYNC: |
Rhyme | 0:37c8ecde13c2 | 928 | SERIAL_PRINT_DBG_ASR("STATE_STATUS_SYNC\n"); |
Rhyme | 0:37c8ecde13c2 | 929 | break; |
Rhyme | 0:37c8ecde13c2 | 930 | case STATE_STATUS_ACK: |
Rhyme | 0:37c8ecde13c2 | 931 | SERIAL_PRINT_DBG_ASR("STATE_STATUS_ACK\n"); |
Rhyme | 0:37c8ecde13c2 | 932 | break; |
Rhyme | 0:37c8ecde13c2 | 933 | case STATE_SEND_BYTES: |
Rhyme | 0:37c8ecde13c2 | 934 | SERIAL_PRINT_DBG_ASR("STATE_SEND_BYTES\n"); |
Rhyme | 0:37c8ecde13c2 | 935 | break; |
Rhyme | 0:37c8ecde13c2 | 936 | case STATE_RECV_BYTES: |
Rhyme | 0:37c8ecde13c2 | 937 | SERIAL_PRINT_DBG_ASR("STATE_RECV_BYTES\n"); |
Rhyme | 0:37c8ecde13c2 | 938 | break; |
Rhyme | 0:37c8ecde13c2 | 939 | case STATE_CMD_COMPLETE: |
Rhyme | 0:37c8ecde13c2 | 940 | SERIAL_PRINT_DBG_ASR("STATE_CMD_COMPLETE\n"); |
Rhyme | 0:37c8ecde13c2 | 941 | break; |
Rhyme | 0:37c8ecde13c2 | 942 | default: |
Rhyme | 0:37c8ecde13c2 | 943 | SERIAL_PRINT_DBG_ASR("Unknown State!\n"); |
Rhyme | 0:37c8ecde13c2 | 944 | break; |
Rhyme | 0:37c8ecde13c2 | 945 | } |
Rhyme | 0:37c8ecde13c2 | 946 | } |
Rhyme | 0:37c8ecde13c2 | 947 | |
Rhyme | 0:37c8ecde13c2 | 948 | void afLib::printTransaction(uint8_t *rbytes, int len) |
Rhyme | 0:37c8ecde13c2 | 949 | { |
Rhyme | 0:37c8ecde13c2 | 950 | //return; |
Rhyme | 0:37c8ecde13c2 | 951 | int i = 0; |
Rhyme | 0:37c8ecde13c2 | 952 | for(;i<=len;++i) |
Rhyme | 0:37c8ecde13c2 | 953 | { |
Rhyme | 0:37c8ecde13c2 | 954 | SERIAL_PRINT_DBG_ASR("0x%02x:",rbytes[i]); |
Rhyme | 0:37c8ecde13c2 | 955 | } |
Rhyme | 0:37c8ecde13c2 | 956 | SERIAL_PRINT_DBG_ASR("\n"); |
Rhyme | 0:37c8ecde13c2 | 957 | } |