afLib 1.3 which is supporting both SPI and UART

Dependencies:   vt100 mbed afLib_1_3

Committer:
Rhyme
Date:
Mon Apr 23 06:15:26 2018 +0000
Revision:
0:87662653a3c6
First UART working version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:87662653a3c6 1 #include "mbed.h"
Rhyme 0:87662653a3c6 2 #include "afErrors.h"
Rhyme 0:87662653a3c6 3 #include "msg_types.h"
Rhyme 0:87662653a3c6 4 #include "mbedUART.h"
Rhyme 0:87662653a3c6 5 #include "edge_mgr.h"
Rhyme 0:87662653a3c6 6 #include "edge_reset_mgr.h"
Rhyme 0:87662653a3c6 7
Rhyme 0:87662653a3c6 8 #define mbedUART_BUFSIZE 300
Rhyme 0:87662653a3c6 9
Rhyme 0:87662653a3c6 10 mbedUART::mbedUART(PinName rxPin, PinName txPin, Serial *theLog)
Rhyme 0:87662653a3c6 11 {
Rhyme 0:87662653a3c6 12 _uart = new edgeSerial(txPin, rxPin, 9600) ;
Rhyme 0:87662653a3c6 13 _buf = new uint8_t[300] ;
Rhyme 0:87662653a3c6 14 }
Rhyme 0:87662653a3c6 15
Rhyme 0:87662653a3c6 16 mbedUART::~mbedUART(void)
Rhyme 0:87662653a3c6 17 {
Rhyme 0:87662653a3c6 18 if (_buf) {
Rhyme 0:87662653a3c6 19 delete [] _buf ;
Rhyme 0:87662653a3c6 20 }
Rhyme 0:87662653a3c6 21 }
Rhyme 0:87662653a3c6 22
Rhyme 0:87662653a3c6 23 int mbedUART::available(void)
Rhyme 0:87662653a3c6 24 {
Rhyme 0:87662653a3c6 25 return (int)_uart->readable() ;
Rhyme 0:87662653a3c6 26 }
Rhyme 0:87662653a3c6 27
Rhyme 0:87662653a3c6 28 char mbedUART::peek(void)
Rhyme 0:87662653a3c6 29 {
Rhyme 0:87662653a3c6 30 char c ;
Rhyme 0:87662653a3c6 31 c = _uart->edge_peek() ;
Rhyme 0:87662653a3c6 32 // c = _uart->getc() ;
Rhyme 0:87662653a3c6 33 return c ;
Rhyme 0:87662653a3c6 34
Rhyme 0:87662653a3c6 35 // return _uart->peek() ;
Rhyme 0:87662653a3c6 36 }
Rhyme 0:87662653a3c6 37
Rhyme 0:87662653a3c6 38 void mbedUART::read(uint8_t *buffer, int len)
Rhyme 0:87662653a3c6 39 {
Rhyme 0:87662653a3c6 40 _uart->edge_read(buffer, len) ;
Rhyme 0:87662653a3c6 41 }
Rhyme 0:87662653a3c6 42
Rhyme 0:87662653a3c6 43 char mbedUART::read(void)
Rhyme 0:87662653a3c6 44 {
Rhyme 0:87662653a3c6 45 char c ;
Rhyme 0:87662653a3c6 46 c = _uart->edge_read() ;
Rhyme 0:87662653a3c6 47 return( c ) ;
Rhyme 0:87662653a3c6 48 }
Rhyme 0:87662653a3c6 49
Rhyme 0:87662653a3c6 50 void mbedUART::write(uint8_t *buffer, int len)
Rhyme 0:87662653a3c6 51 {
Rhyme 0:87662653a3c6 52 _uart->edge_write(buffer, len) ;
Rhyme 0:87662653a3c6 53 }
Rhyme 0:87662653a3c6 54
Rhyme 0:87662653a3c6 55 void mbedUART::checkForInterrupt(volatile int *interrupts_pending, bool idle)
Rhyme 0:87662653a3c6 56 {
Rhyme 0:87662653a3c6 57 char c ;
Rhyme 0:87662653a3c6 58 if (_uart->readable()) {
Rhyme 0:87662653a3c6 59 c = _uart->edge_peek() ;
Rhyme 0:87662653a3c6 60 //tty->printf("Char %02X received\n", c) ;
Rhyme 0:87662653a3c6 61 if (c == INT_CHAR) {
Rhyme 0:87662653a3c6 62 if (*interrupts_pending == 0) {
Rhyme 0:87662653a3c6 63 c = _uart->edge_read() ;
Rhyme 0:87662653a3c6 64 *interrupts_pending += 1 ; /* (*interrupts_pending)++ ; */
Rhyme 0:87662653a3c6 65 } else if (idle) {
Rhyme 0:87662653a3c6 66 c = _uart->edge_read() ;
Rhyme 0:87662653a3c6 67 } else {
Rhyme 0:87662653a3c6 68 // _theLog->printf("INT(Pending)\n") ;
Rhyme 0:87662653a3c6 69 }
Rhyme 0:87662653a3c6 70 } else {
Rhyme 0:87662653a3c6 71 if (*interrupts_pending == 0) {
Rhyme 0:87662653a3c6 72 c = _uart->edge_read() ;
Rhyme 0:87662653a3c6 73 }
Rhyme 0:87662653a3c6 74 }
Rhyme 0:87662653a3c6 75 }
Rhyme 0:87662653a3c6 76 }
Rhyme 0:87662653a3c6 77
Rhyme 0:87662653a3c6 78 int mbedUART::exchangeStatus(StatusCommand *tx, StatusCommand *rx)
Rhyme 0:87662653a3c6 79 {
Rhyme 0:87662653a3c6 80 int result = afSUCCESS ;
Rhyme 0:87662653a3c6 81 int i ;
Rhyme 0:87662653a3c6 82 uint16_t len ;
Rhyme 0:87662653a3c6 83 int *bytes ;
Rhyme 0:87662653a3c6 84 char *rbytes ;
Rhyme 0:87662653a3c6 85 int index = 0 ;
Rhyme 0:87662653a3c6 86 len = tx->getSize() ;
Rhyme 0:87662653a3c6 87 bytes = new int[len] ;
Rhyme 0:87662653a3c6 88 rbytes = new char[len + 1] ;
Rhyme 0:87662653a3c6 89 tx->getBytes(bytes) ;
Rhyme 0:87662653a3c6 90
Rhyme 0:87662653a3c6 91 for(i = 0 ; i < len ; i++ ) {
Rhyme 0:87662653a3c6 92 rbytes[i] = bytes[i] ;
Rhyme 0:87662653a3c6 93 }
Rhyme 0:87662653a3c6 94 rbytes[len] = tx->getChecksum() ;
Rhyme 0:87662653a3c6 95 sendBytes(rbytes, len + 1) ;
Rhyme 0:87662653a3c6 96
Rhyme 0:87662653a3c6 97 // Skip any interrupts, that may have come in.
Rhyme 0:87662653a3c6 98 do {
Rhyme 0:87662653a3c6 99 recvBytes(rbytes, 1) ;
Rhyme 0:87662653a3c6 100 } while(rbytes[0] == INT_CHAR) ;
Rhyme 0:87662653a3c6 101
Rhyme 0:87662653a3c6 102 // Okay, we have good first char, now read the rest
Rhyme 0:87662653a3c6 103 recvBytes(&rbytes[1], len) ;
Rhyme 0:87662653a3c6 104
Rhyme 0:87662653a3c6 105 uint8_t cmd = bytes[index++] ;
Rhyme 0:87662653a3c6 106 if (cmd != SYNC_REQUEST && cmd != SYNC_ACK) {
Rhyme 0:87662653a3c6 107 tty->printf("exchangeStatus bad cmd: %02X\n", cmd) ;
Rhyme 0:87662653a3c6 108 result = afERROR_INVALID_COMMAND ;
Rhyme 0:87662653a3c6 109 }
Rhyme 0:87662653a3c6 110
Rhyme 0:87662653a3c6 111 rx->setBytesToSend(rbytes[index + 0] | (rbytes[index + 1] << 8)) ;
Rhyme 0:87662653a3c6 112 rx->setBytesToRecv(rbytes[index + 2] | (rbytes[index + 3] << 8)) ;
Rhyme 0:87662653a3c6 113 rx->setChecksum(rbytes[index + 4]) ;
Rhyme 0:87662653a3c6 114
Rhyme 0:87662653a3c6 115 delete [] rbytes ;
Rhyme 0:87662653a3c6 116 delete [] bytes ;
Rhyme 0:87662653a3c6 117 return result ;
Rhyme 0:87662653a3c6 118 }
Rhyme 0:87662653a3c6 119
Rhyme 0:87662653a3c6 120
Rhyme 0:87662653a3c6 121 int mbedUART::writeStatus(StatusCommand *c)
Rhyme 0:87662653a3c6 122 {
Rhyme 0:87662653a3c6 123 int result = afSUCCESS ;
Rhyme 0:87662653a3c6 124 uint16_t len ;
Rhyme 0:87662653a3c6 125 int *bytes ;
Rhyme 0:87662653a3c6 126 char *rbytes ;
Rhyme 0:87662653a3c6 127 int index = 0 ;
Rhyme 0:87662653a3c6 128 len = c->getSize() ;
Rhyme 0:87662653a3c6 129 bytes = new int[len] ;
Rhyme 0:87662653a3c6 130 rbytes = new char[len + 1] ;
Rhyme 0:87662653a3c6 131
Rhyme 0:87662653a3c6 132 c->getBytes(bytes) ;
Rhyme 0:87662653a3c6 133 for (int i = 0 ; i < len ; i++ ) {
Rhyme 0:87662653a3c6 134 rbytes[i] = bytes[i] ;
Rhyme 0:87662653a3c6 135 }
Rhyme 0:87662653a3c6 136 rbytes[len] = c->getChecksum() ;
Rhyme 0:87662653a3c6 137
Rhyme 0:87662653a3c6 138 sendBytes(rbytes, len + 1) ;
Rhyme 0:87662653a3c6 139
Rhyme 0:87662653a3c6 140 uint8_t cmd = rbytes[index++] ;
Rhyme 0:87662653a3c6 141 if (cmd != SYNC_REQUEST && cmd != SYNC_ACK) {
Rhyme 0:87662653a3c6 142 tty->printf("writeStatus bad cmd: %02X\n", cmd) ;
Rhyme 0:87662653a3c6 143 result = afERROR_INVALID_COMMAND ;
Rhyme 0:87662653a3c6 144 }
Rhyme 0:87662653a3c6 145 // c->dump() ;
Rhyme 0:87662653a3c6 146 // c->dumpBytes() ;
Rhyme 0:87662653a3c6 147
Rhyme 0:87662653a3c6 148 delete [] rbytes ;
Rhyme 0:87662653a3c6 149 delete [] bytes ;
Rhyme 0:87662653a3c6 150
Rhyme 0:87662653a3c6 151 return result ;
Rhyme 0:87662653a3c6 152 }
Rhyme 0:87662653a3c6 153
Rhyme 0:87662653a3c6 154 void mbedUART::sendBytes(char *bytes, int len)
Rhyme 0:87662653a3c6 155 {
Rhyme 0:87662653a3c6 156 #if 0
Rhyme 0:87662653a3c6 157 tty->printf("sendBytes %d: ", len) ;
Rhyme 0:87662653a3c6 158 for (int i = 0 ; i < len ; i++ ) { tty->printf("%02X ", bytes[i]) ; }
Rhyme 0:87662653a3c6 159 tty->printf("\n") ;
Rhyme 0:87662653a3c6 160 #endif
Rhyme 0:87662653a3c6 161
Rhyme 0:87662653a3c6 162 _uart->edge_write((uint8_t *)bytes, len) ;
Rhyme 0:87662653a3c6 163 }
Rhyme 0:87662653a3c6 164
Rhyme 0:87662653a3c6 165 void mbedUART::recvBytes(char *bytes, int len)
Rhyme 0:87662653a3c6 166 {
Rhyme 0:87662653a3c6 167 _uart->edge_read((uint8_t *)bytes, len) ;
Rhyme 0:87662653a3c6 168
Rhyme 0:87662653a3c6 169 #if 0
Rhyme 0:87662653a3c6 170 tty->printf("recvBytes %d: ", len) ;
Rhyme 0:87662653a3c6 171 for (int i = 0 ; i < len ; i++ ) { tty->printf("%02X ", bytes[i]) ; }
Rhyme 0:87662653a3c6 172 tty->printf("\n") ;
Rhyme 0:87662653a3c6 173 #endif
Rhyme 0:87662653a3c6 174 }
Rhyme 0:87662653a3c6 175
Rhyme 0:87662653a3c6 176 void mbedUART::sendBytesOffset(char *bytes, uint16_t *bytesToSend, uint16_t *offset)
Rhyme 0:87662653a3c6 177 {
Rhyme 0:87662653a3c6 178 uint16_t len = 0 ;
Rhyme 0:87662653a3c6 179
Rhyme 0:87662653a3c6 180 len = *bytesToSend ;
Rhyme 0:87662653a3c6 181
Rhyme 0:87662653a3c6 182 // sendBytes(bytes, len) ;
Rhyme 0:87662653a3c6 183 sendBytes(&bytes[*offset], len) ;
Rhyme 0:87662653a3c6 184
Rhyme 0:87662653a3c6 185 // dumpBytes("Sending: ", len, bytes) ;
Rhyme 0:87662653a3c6 186
Rhyme 0:87662653a3c6 187 *offset += len ;
Rhyme 0:87662653a3c6 188 *bytesToSend -= len ;
Rhyme 0:87662653a3c6 189 }
Rhyme 0:87662653a3c6 190
Rhyme 0:87662653a3c6 191 void mbedUART::recvBytesOffset(char **bytes, uint16_t *bytesLen, uint16_t *bytesToRecv, uint16_t *offset)
Rhyme 0:87662653a3c6 192 {
Rhyme 0:87662653a3c6 193 uint16_t len = 0 ;
Rhyme 0:87662653a3c6 194
Rhyme 0:87662653a3c6 195 len = *bytesToRecv ;
Rhyme 0:87662653a3c6 196
Rhyme 0:87662653a3c6 197 if (*offset == 0) {
Rhyme 0:87662653a3c6 198 *bytesLen = *bytesToRecv ;
Rhyme 0:87662653a3c6 199 *bytes = new char[*bytesLen] ;
Rhyme 0:87662653a3c6 200 }
Rhyme 0:87662653a3c6 201
Rhyme 0:87662653a3c6 202 char * start = *bytes + *offset ;
Rhyme 0:87662653a3c6 203
Rhyme 0:87662653a3c6 204 recvBytes(start, len) ;
Rhyme 0:87662653a3c6 205
Rhyme 0:87662653a3c6 206 // dumpBytes("Receiving:", len, _readBuffer) ;
Rhyme 0:87662653a3c6 207 #if 0
Rhyme 0:87662653a3c6 208 tty->printf("Receiving: ") ;
Rhyme 0:87662653a3c6 209 for (int i = 0; i < len ; i++ ) { tty->printf("%02X ", start[i]) ; }
Rhyme 0:87662653a3c6 210 tty->printf("\n") ;
Rhyme 0:87662653a3c6 211 #endif
Rhyme 0:87662653a3c6 212
Rhyme 0:87662653a3c6 213 *offset += len ;
Rhyme 0:87662653a3c6 214 *bytesToRecv -= len ;
Rhyme 0:87662653a3c6 215 }