afLib 1.3 which is supporting both SPI and UART
Dependencies: vt100 mbed afLib_1_3
mbedUART.cpp
00001 #include "mbed.h" 00002 #include "afErrors.h" 00003 #include "msg_types.h" 00004 #include "mbedUART.h" 00005 #include "edge_mgr.h" 00006 #include "edge_reset_mgr.h" 00007 00008 #define mbedUART_BUFSIZE 300 00009 00010 mbedUART::mbedUART(PinName rxPin, PinName txPin, Serial *theLog) 00011 { 00012 _uart = new edgeSerial(txPin, rxPin, 9600) ; 00013 _buf = new uint8_t[300] ; 00014 } 00015 00016 mbedUART::~mbedUART(void) 00017 { 00018 if (_buf) { 00019 delete [] _buf ; 00020 } 00021 } 00022 00023 int mbedUART::available(void) 00024 { 00025 return (int)_uart->readable() ; 00026 } 00027 00028 char mbedUART::peek(void) 00029 { 00030 char c ; 00031 c = _uart->edge_peek() ; 00032 // c = _uart->getc() ; 00033 return c ; 00034 00035 // return _uart->peek() ; 00036 } 00037 00038 void mbedUART::read(uint8_t *buffer, int len) 00039 { 00040 _uart->edge_read(buffer, len) ; 00041 } 00042 00043 char mbedUART::read(void) 00044 { 00045 char c ; 00046 c = _uart->edge_read() ; 00047 return( c ) ; 00048 } 00049 00050 void mbedUART::write(uint8_t *buffer, int len) 00051 { 00052 _uart->edge_write(buffer, len) ; 00053 } 00054 00055 void mbedUART::checkForInterrupt(volatile int *interrupts_pending, bool idle) 00056 { 00057 char c ; 00058 if (_uart->readable()) { 00059 c = _uart->edge_peek() ; 00060 //tty->printf("Char %02X received\n", c) ; 00061 if (c == INT_CHAR) { 00062 if (*interrupts_pending == 0) { 00063 c = _uart->edge_read() ; 00064 *interrupts_pending += 1 ; /* (*interrupts_pending)++ ; */ 00065 } else if (idle) { 00066 c = _uart->edge_read() ; 00067 } else { 00068 // _theLog->printf("INT(Pending)\n") ; 00069 } 00070 } else { 00071 if (*interrupts_pending == 0) { 00072 c = _uart->edge_read() ; 00073 } 00074 } 00075 } 00076 } 00077 00078 int mbedUART::exchangeStatus(StatusCommand *tx, StatusCommand *rx) 00079 { 00080 int result = afSUCCESS ; 00081 int i ; 00082 uint16_t len ; 00083 int *bytes ; 00084 char *rbytes ; 00085 int index = 0 ; 00086 len = tx->getSize() ; 00087 bytes = new int[len] ; 00088 rbytes = new char[len + 1] ; 00089 tx->getBytes(bytes) ; 00090 00091 for(i = 0 ; i < len ; i++ ) { 00092 rbytes[i] = bytes[i] ; 00093 } 00094 rbytes[len] = tx->getChecksum() ; 00095 sendBytes(rbytes, len + 1) ; 00096 00097 // Skip any interrupts, that may have come in. 00098 do { 00099 recvBytes(rbytes, 1) ; 00100 } while(rbytes[0] == INT_CHAR) ; 00101 00102 // Okay, we have good first char, now read the rest 00103 recvBytes(&rbytes[1], len) ; 00104 00105 uint8_t cmd = bytes[index++] ; 00106 if (cmd != SYNC_REQUEST && cmd != SYNC_ACK) { 00107 tty->printf("exchangeStatus bad cmd: %02X\n", cmd) ; 00108 result = afERROR_INVALID_COMMAND ; 00109 } 00110 00111 rx->setBytesToSend(rbytes[index + 0] | (rbytes[index + 1] << 8)) ; 00112 rx->setBytesToRecv(rbytes[index + 2] | (rbytes[index + 3] << 8)) ; 00113 rx->setChecksum(rbytes[index + 4]) ; 00114 00115 delete [] rbytes ; 00116 delete [] bytes ; 00117 return result ; 00118 } 00119 00120 00121 int mbedUART::writeStatus(StatusCommand *c) 00122 { 00123 int result = afSUCCESS ; 00124 uint16_t len ; 00125 int *bytes ; 00126 char *rbytes ; 00127 int index = 0 ; 00128 len = c->getSize() ; 00129 bytes = new int[len] ; 00130 rbytes = new char[len + 1] ; 00131 00132 c->getBytes(bytes) ; 00133 for (int i = 0 ; i < len ; i++ ) { 00134 rbytes[i] = bytes[i] ; 00135 } 00136 rbytes[len] = c->getChecksum() ; 00137 00138 sendBytes(rbytes, len + 1) ; 00139 00140 uint8_t cmd = rbytes[index++] ; 00141 if (cmd != SYNC_REQUEST && cmd != SYNC_ACK) { 00142 tty->printf("writeStatus bad cmd: %02X\n", cmd) ; 00143 result = afERROR_INVALID_COMMAND ; 00144 } 00145 // c->dump() ; 00146 // c->dumpBytes() ; 00147 00148 delete [] rbytes ; 00149 delete [] bytes ; 00150 00151 return result ; 00152 } 00153 00154 void mbedUART::sendBytes(char *bytes, int len) 00155 { 00156 #if 0 00157 tty->printf("sendBytes %d: ", len) ; 00158 for (int i = 0 ; i < len ; i++ ) { tty->printf("%02X ", bytes[i]) ; } 00159 tty->printf("\n") ; 00160 #endif 00161 00162 _uart->edge_write((uint8_t *)bytes, len) ; 00163 } 00164 00165 void mbedUART::recvBytes(char *bytes, int len) 00166 { 00167 _uart->edge_read((uint8_t *)bytes, len) ; 00168 00169 #if 0 00170 tty->printf("recvBytes %d: ", len) ; 00171 for (int i = 0 ; i < len ; i++ ) { tty->printf("%02X ", bytes[i]) ; } 00172 tty->printf("\n") ; 00173 #endif 00174 } 00175 00176 void mbedUART::sendBytesOffset(char *bytes, uint16_t *bytesToSend, uint16_t *offset) 00177 { 00178 uint16_t len = 0 ; 00179 00180 len = *bytesToSend ; 00181 00182 // sendBytes(bytes, len) ; 00183 sendBytes(&bytes[*offset], len) ; 00184 00185 // dumpBytes("Sending: ", len, bytes) ; 00186 00187 *offset += len ; 00188 *bytesToSend -= len ; 00189 } 00190 00191 void mbedUART::recvBytesOffset(char **bytes, uint16_t *bytesLen, uint16_t *bytesToRecv, uint16_t *offset) 00192 { 00193 uint16_t len = 0 ; 00194 00195 len = *bytesToRecv ; 00196 00197 if (*offset == 0) { 00198 *bytesLen = *bytesToRecv ; 00199 *bytes = new char[*bytesLen] ; 00200 } 00201 00202 char * start = *bytes + *offset ; 00203 00204 recvBytes(start, len) ; 00205 00206 // dumpBytes("Receiving:", len, _readBuffer) ; 00207 #if 0 00208 tty->printf("Receiving: ") ; 00209 for (int i = 0; i < len ; i++ ) { tty->printf("%02X ", start[i]) ; } 00210 tty->printf("\n") ; 00211 #endif 00212 00213 *offset += len ; 00214 *bytesToRecv -= len ; 00215 }
Generated on Wed Jul 20 2022 21:24:28 by
1.7.2