an old afLib which supports both SPI and UART

Committer:
Rhyme
Date:
Tue Mar 20 06:47:25 2018 +0000
Revision:
0:6f371c791202
SPI mode started working

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:6f371c791202 1 /**
Rhyme 0:6f371c791202 2 * Copyright 2015 Afero, Inc.
Rhyme 0:6f371c791202 3 *
Rhyme 0:6f371c791202 4 * Licensed under the Apache License, Version 2.0 (the "License");
Rhyme 0:6f371c791202 5 * you may not use this file except in compliance with the License.
Rhyme 0:6f371c791202 6 * You may obtain a copy of the License at
Rhyme 0:6f371c791202 7 *
Rhyme 0:6f371c791202 8 * http://www.apache.org/licenses/LICENSE-2.0
Rhyme 0:6f371c791202 9 *
Rhyme 0:6f371c791202 10 * Unless required by applicable law or agreed to in writing, software
Rhyme 0:6f371c791202 11 * distributed under the License is distributed on an "AS IS" BASIS,
Rhyme 0:6f371c791202 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Rhyme 0:6f371c791202 13 * See the License for the specific language governing permissions and
Rhyme 0:6f371c791202 14 * limitations under the License.
Rhyme 0:6f371c791202 15 */
Rhyme 0:6f371c791202 16
Rhyme 0:6f371c791202 17 #ifndef AFLIB_H__
Rhyme 0:6f371c791202 18 #define AFLIB_H__
Rhyme 0:6f371c791202 19
Rhyme 0:6f371c791202 20 #include "iafLib.h"
Rhyme 0:6f371c791202 21 #include "SPI.h"
Rhyme 0:6f371c791202 22 #include "Command.h"
Rhyme 0:6f371c791202 23 #include "StatusCommand.h"
Rhyme 0:6f371c791202 24 #include "afSPI.h"
Rhyme 0:6f371c791202 25 #define Stream Serial
Rhyme 0:6f371c791202 26
Rhyme 0:6f371c791202 27 #define STATE_IDLE 0
Rhyme 0:6f371c791202 28 #define STATE_STATUS_SYNC 1
Rhyme 0:6f371c791202 29 #define STATE_STATUS_ACK 3
Rhyme 0:6f371c791202 30 #define STATE_SEND_BYTES 4
Rhyme 0:6f371c791202 31 #define STATE_RECV_BYTES 5
Rhyme 0:6f371c791202 32 #define STATE_CMD_COMPLETE 6
Rhyme 0:6f371c791202 33
Rhyme 0:6f371c791202 34 #define REQUEST_QUEUE_SIZE 10
Rhyme 0:6f371c791202 35
Rhyme 0:6f371c791202 36 typedef struct {
Rhyme 0:6f371c791202 37 uint8_t messageType;
Rhyme 0:6f371c791202 38 uint16_t attrId;
Rhyme 0:6f371c791202 39 uint8_t requestId;
Rhyme 0:6f371c791202 40 uint16_t valueLen;
Rhyme 0:6f371c791202 41 uint8_t *p_value;
Rhyme 0:6f371c791202 42 uint8_t status;
Rhyme 0:6f371c791202 43 uint8_t reason;
Rhyme 0:6f371c791202 44 } request_t;
Rhyme 0:6f371c791202 45
Rhyme 0:6f371c791202 46 class afLib : public iafLib {
Rhyme 0:6f371c791202 47 public:
Rhyme 0:6f371c791202 48 afLib(AttrSetHandler attrSet, AttrNotifyHandler attrNotify, Stream *serial, afTransport *theTransport);
Rhyme 0:6f371c791202 49 afLib(PinName mcuInterrupt, isr isrWrapper, AttrSetHandler attrSet, AttrNotifyHandler attrNotify, Stream *serial, afTransport *theTransport);
Rhyme 0:6f371c791202 50
Rhyme 0:6f371c791202 51 virtual int getRequestId(void) ; /* 20-Mar-2018 by Motoo Tanaka */
Rhyme 0:6f371c791202 52
Rhyme 0:6f371c791202 53 virtual void loop(void);
Rhyme 0:6f371c791202 54
Rhyme 0:6f371c791202 55 virtual int getAttribute(const uint16_t attrId);
Rhyme 0:6f371c791202 56
Rhyme 0:6f371c791202 57 virtual int setAttributeBool(const uint16_t attrId, const bool value);
Rhyme 0:6f371c791202 58
Rhyme 0:6f371c791202 59 virtual int setAttribute8(const uint16_t attrId, const int8_t value);
Rhyme 0:6f371c791202 60
Rhyme 0:6f371c791202 61 virtual int setAttribute16(const uint16_t attrId, const int16_t value);
Rhyme 0:6f371c791202 62
Rhyme 0:6f371c791202 63 virtual int setAttribute32(const uint16_t attrId, const int32_t value);
Rhyme 0:6f371c791202 64
Rhyme 0:6f371c791202 65 virtual int setAttribute64(const uint16_t attrId, const int64_t value);
Rhyme 0:6f371c791202 66
Rhyme 0:6f371c791202 67 virtual int setAttributeStr(const uint16_t attrId, const char *value);
Rhyme 0:6f371c791202 68
Rhyme 0:6f371c791202 69 virtual int setAttributeCStr(const uint16_t attrId, const uint16_t valueLen, const char *value);
Rhyme 0:6f371c791202 70
Rhyme 0:6f371c791202 71 virtual int setAttributeBytes(const uint16_t attrId, const uint16_t valueLen, const uint8_t *value);
Rhyme 0:6f371c791202 72
Rhyme 0:6f371c791202 73 virtual bool isIdle();
Rhyme 0:6f371c791202 74
Rhyme 0:6f371c791202 75 virtual void mcuISR();
Rhyme 0:6f371c791202 76
Rhyme 0:6f371c791202 77 private:
Rhyme 0:6f371c791202 78 Stream *_theLog;
Rhyme 0:6f371c791202 79 afTransport *_theTransport;
Rhyme 0:6f371c791202 80 InterruptIn *fco ; /* 20-Mar-2018 Motoo Tanaka */
Rhyme 0:6f371c791202 81
Rhyme 0:6f371c791202 82 //SPISettings _spiSettings;
Rhyme 0:6f371c791202 83 volatile int _interrupts_pending;
Rhyme 0:6f371c791202 84 int _state;
Rhyme 0:6f371c791202 85 uint16_t _bytesToSend;
Rhyme 0:6f371c791202 86 uint16_t _bytesToRecv;
Rhyme 0:6f371c791202 87 uint8_t _requestId;
Rhyme 0:6f371c791202 88 uint16_t _outstandingSetGetAttrId;
Rhyme 0:6f371c791202 89
Rhyme 0:6f371c791202 90 // Application Callbacks.
Rhyme 0:6f371c791202 91 AttrSetHandler _attrSetHandler;
Rhyme 0:6f371c791202 92 AttrNotifyHandler _attrNotifyHandler;
Rhyme 0:6f371c791202 93
Rhyme 0:6f371c791202 94 Command *_writeCmd;
Rhyme 0:6f371c791202 95 uint16_t _writeBufferLen;
Rhyme 0:6f371c791202 96 uint8_t *_writeBuffer;
Rhyme 0:6f371c791202 97
Rhyme 0:6f371c791202 98 Command *_readCmd;
Rhyme 0:6f371c791202 99 uint16_t _readBufferLen;
Rhyme 0:6f371c791202 100 uint8_t *_readBuffer;
Rhyme 0:6f371c791202 101
Rhyme 0:6f371c791202 102 uint16_t _writeCmdOffset;
Rhyme 0:6f371c791202 103 uint16_t _readCmdOffset;
Rhyme 0:6f371c791202 104
Rhyme 0:6f371c791202 105 StatusCommand *_txStatus;
Rhyme 0:6f371c791202 106 StatusCommand *_rxStatus;
Rhyme 0:6f371c791202 107
Rhyme 0:6f371c791202 108 request_t _request;
Rhyme 0:6f371c791202 109
Rhyme 0:6f371c791202 110 #ifdef ATTRIBUTE_CLI
Rhyme 0:6f371c791202 111 int parseCommand(const char *cmd);
Rhyme 0:6f371c791202 112 #endif
Rhyme 0:6f371c791202 113
Rhyme 0:6f371c791202 114 void sendCommand(void);
Rhyme 0:6f371c791202 115
Rhyme 0:6f371c791202 116 void runStateMachine(void);
Rhyme 0:6f371c791202 117
Rhyme 0:6f371c791202 118 void printState(int state);
Rhyme 0:6f371c791202 119
Rhyme 0:6f371c791202 120 bool inSync(StatusCommand *tx, StatusCommand *rx);
Rhyme 0:6f371c791202 121
Rhyme 0:6f371c791202 122 void sendBytesSPI(char *bytes, int len);
Rhyme 0:6f371c791202 123 void sendBytesUART(char *bytes, int len);
Rhyme 0:6f371c791202 124 void sendBytes();
Rhyme 0:6f371c791202 125
Rhyme 0:6f371c791202 126 void recvBytesSPI(char *bytes, int len);
Rhyme 0:6f371c791202 127 void recvBytesUART(char *bytes, int len);
Rhyme 0:6f371c791202 128 void recvBytes();
Rhyme 0:6f371c791202 129
Rhyme 0:6f371c791202 130 void dumpBytes(char *label, int len, uint8_t *bytes);
Rhyme 0:6f371c791202 131
Rhyme 0:6f371c791202 132 void updateIntsPending(int amount);
Rhyme 0:6f371c791202 133
Rhyme 0:6f371c791202 134 void queueInit(void);
Rhyme 0:6f371c791202 135
Rhyme 0:6f371c791202 136 int queuePut(uint8_t messageType, uint8_t requestId, const uint16_t attributeId, uint16_t valueLen, const uint8_t *value, uint8_t status, uint8_t reason);
Rhyme 0:6f371c791202 137
Rhyme 0:6f371c791202 138 int queueGet(uint8_t *messageType, uint8_t *requestId, uint16_t *attributeId, uint16_t *valueLen, uint8_t **value, uint8_t *status, uint8_t *reason);
Rhyme 0:6f371c791202 139
Rhyme 0:6f371c791202 140 int doGetAttribute(uint8_t requestId, uint16_t attrId);
Rhyme 0:6f371c791202 141
Rhyme 0:6f371c791202 142 int doSetAttribute(uint8_t requestId, uint16_t attrId, uint16_t valueLen, uint8_t *value);
Rhyme 0:6f371c791202 143
Rhyme 0:6f371c791202 144 int doUpdateAttribute(uint8_t requestId, uint16_t attrId, uint16_t valueLen, uint8_t *value, uint8_t status, uint8_t reason);
Rhyme 0:6f371c791202 145
Rhyme 0:6f371c791202 146 int setAttributeComplete(uint8_t requestId, const uint16_t attrId, const uint16_t valueLen, const uint8_t *value, uint8_t status, uint8_t reason);
Rhyme 0:6f371c791202 147
Rhyme 0:6f371c791202 148 void onStateIdle(void);
Rhyme 0:6f371c791202 149 void onStateSync(void);
Rhyme 0:6f371c791202 150 void onStateAck(void);
Rhyme 0:6f371c791202 151 void onStateSendBytes(void);
Rhyme 0:6f371c791202 152 void onStateRecvBytes(void);
Rhyme 0:6f371c791202 153 void onStateCmdComplete(void);
Rhyme 0:6f371c791202 154 };
Rhyme 0:6f371c791202 155
Rhyme 0:6f371c791202 156 #endif // AFLIB_H__