afLib 1.3 which is supporting both SPI and UART
Dependencies: vt100 mbed afLib_1_3
af_utils/mbedSPI.cpp@0:87662653a3c6, 2018-04-23 (annotated)
- Committer:
- Rhyme
- Date:
- Mon Apr 23 06:15:26 2018 +0000
- Revision:
- 0:87662653a3c6
First UART working version
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Rhyme | 0:87662653a3c6 | 1 | #include "mbed.h" |
| Rhyme | 0:87662653a3c6 | 2 | #include "afSPI.h" |
| Rhyme | 0:87662653a3c6 | 3 | #include "mbedSPI.h" |
| Rhyme | 0:87662653a3c6 | 4 | #include "afErrors.h" |
| Rhyme | 0:87662653a3c6 | 5 | #include "msg_types.h" |
| Rhyme | 0:87662653a3c6 | 6 | #include "vt100.h" |
| Rhyme | 0:87662653a3c6 | 7 | extern vt100 *tty ; |
| Rhyme | 0:87662653a3c6 | 8 | |
| Rhyme | 0:87662653a3c6 | 9 | #if defined (TARGET_KL25Z) || defined (TARGET_TEENSY3_1) |
| Rhyme | 0:87662653a3c6 | 10 | #ifndef SPI0_C1 |
| Rhyme | 0:87662653a3c6 | 11 | #define SPI0_C1 (*(uint8_t *)0x40076000) |
| Rhyme | 0:87662653a3c6 | 12 | #endif |
| Rhyme | 0:87662653a3c6 | 13 | #endif |
| Rhyme | 0:87662653a3c6 | 14 | |
| Rhyme | 0:87662653a3c6 | 15 | mbedSPI::mbedSPI(PinName mosi, PinName miso, PinName sckl, PinName cs) : |
| Rhyme | 0:87662653a3c6 | 16 | _spi(mosi, miso, sckl), _cs(cs, 1) |
| Rhyme | 0:87662653a3c6 | 17 | { |
| Rhyme | 0:87662653a3c6 | 18 | _spi.format(8, 0) ; |
| Rhyme | 0:87662653a3c6 | 19 | _spi.frequency(1000000) ; /* 1MHz */ |
| Rhyme | 0:87662653a3c6 | 20 | |
| Rhyme | 0:87662653a3c6 | 21 | #if defined (TARGET_KL25Z) || defined (TARGET_TEENSY3_1) |
| Rhyme | 0:87662653a3c6 | 22 | #ifndef SPI0_C1 |
| Rhyme | 0:87662653a3c6 | 23 | #define SPI0_C1 (*(uint8_t *)0x40076000) |
| Rhyme | 0:87662653a3c6 | 24 | #endif |
| Rhyme | 0:87662653a3c6 | 25 | // SPI0_C1 |= 0x01 ; /* LSB First */ |
| Rhyme | 0:87662653a3c6 | 26 | // SPI0_C1 &= 0xFE ; /* MSB First */ |
| Rhyme | 0:87662653a3c6 | 27 | #endif |
| Rhyme | 0:87662653a3c6 | 28 | |
| Rhyme | 0:87662653a3c6 | 29 | } |
| Rhyme | 0:87662653a3c6 | 30 | |
| Rhyme | 0:87662653a3c6 | 31 | void mbedSPI::begin(void) |
| Rhyme | 0:87662653a3c6 | 32 | { |
| Rhyme | 0:87662653a3c6 | 33 | } |
| Rhyme | 0:87662653a3c6 | 34 | |
| Rhyme | 0:87662653a3c6 | 35 | void mbedSPI::beginSPI(void) |
| Rhyme | 0:87662653a3c6 | 36 | { |
| Rhyme | 0:87662653a3c6 | 37 | _cs = 0 ; |
| Rhyme | 0:87662653a3c6 | 38 | SPI0_C1 |= 0x01 ; /* LSB First */ |
| Rhyme | 0:87662653a3c6 | 39 | wait_us(8) ; |
| Rhyme | 0:87662653a3c6 | 40 | } |
| Rhyme | 0:87662653a3c6 | 41 | |
| Rhyme | 0:87662653a3c6 | 42 | void mbedSPI::endSPI(void) |
| Rhyme | 0:87662653a3c6 | 43 | { |
| Rhyme | 0:87662653a3c6 | 44 | _cs = 1 ; |
| Rhyme | 0:87662653a3c6 | 45 | SPI0_C1 &= 0xFE ; /* MSB First */ |
| Rhyme | 0:87662653a3c6 | 46 | wait_us(1) ; |
| Rhyme | 0:87662653a3c6 | 47 | // SPI.endTransaction() // in the original code |
| Rhyme | 0:87662653a3c6 | 48 | } |
| Rhyme | 0:87662653a3c6 | 49 | |
| Rhyme | 0:87662653a3c6 | 50 | /** |
| Rhyme | 0:87662653a3c6 | 51 | * on 17-Jan-2018 disable/enable irq added |
| Rhyme | 0:87662653a3c6 | 52 | * before and after of each _spi.writes |
| Rhyme | 0:87662653a3c6 | 53 | */ |
| Rhyme | 0:87662653a3c6 | 54 | void mbedSPI::transfer(char *bytes, int len) |
| Rhyme | 0:87662653a3c6 | 55 | { |
| Rhyme | 0:87662653a3c6 | 56 | int i ; |
| Rhyme | 0:87662653a3c6 | 57 | for (i = 0 ; i < len ; i++ ) { |
| Rhyme | 0:87662653a3c6 | 58 | __disable_irq() ; // Disable Interrupts |
| Rhyme | 0:87662653a3c6 | 59 | bytes[i] = _spi.write(bytes[i]) ; |
| Rhyme | 0:87662653a3c6 | 60 | __enable_irq() ; // Enable Interrupts |
| Rhyme | 0:87662653a3c6 | 61 | } |
| Rhyme | 0:87662653a3c6 | 62 | } |
| Rhyme | 0:87662653a3c6 | 63 | |
| Rhyme | 0:87662653a3c6 | 64 | void mbedSPI::checkForInterrupt(volatile int *interrupts_pending, bool idle) |
| Rhyme | 0:87662653a3c6 | 65 | { |
| Rhyme | 0:87662653a3c6 | 66 | // Nothing required here. |
| Rhyme | 0:87662653a3c6 | 67 | } |
| Rhyme | 0:87662653a3c6 | 68 | |
| Rhyme | 0:87662653a3c6 | 69 | int mbedSPI::exchangeStatus(StatusCommand *tx, StatusCommand *rx) |
| Rhyme | 0:87662653a3c6 | 70 | { |
| Rhyme | 0:87662653a3c6 | 71 | int result = afSUCCESS ; |
| Rhyme | 0:87662653a3c6 | 72 | uint16_t len = tx->getSize() ; |
| Rhyme | 0:87662653a3c6 | 73 | int *bytes ; |
| Rhyme | 0:87662653a3c6 | 74 | char *rbytes ; |
| Rhyme | 0:87662653a3c6 | 75 | int index = 0 ; |
| Rhyme | 0:87662653a3c6 | 76 | bytes = new int[len] ; |
| Rhyme | 0:87662653a3c6 | 77 | rbytes = new char[len + 1] ; |
| Rhyme | 0:87662653a3c6 | 78 | tx->getBytes(bytes) ; |
| Rhyme | 0:87662653a3c6 | 79 | |
| Rhyme | 0:87662653a3c6 | 80 | beginSPI() ; |
| Rhyme | 0:87662653a3c6 | 81 | for (int i = 0 ; i < len ; i++ ) { |
| Rhyme | 0:87662653a3c6 | 82 | rbytes[i] = bytes[i] ; |
| Rhyme | 0:87662653a3c6 | 83 | } |
| Rhyme | 0:87662653a3c6 | 84 | rbytes[len] = tx->getChecksum() ; |
| Rhyme | 0:87662653a3c6 | 85 | transfer(rbytes, len + 1) ; |
| Rhyme | 0:87662653a3c6 | 86 | |
| Rhyme | 0:87662653a3c6 | 87 | uint8_t cmd = bytes[index++] ; |
| Rhyme | 0:87662653a3c6 | 88 | if (cmd != SYNC_REQUEST && cmd != SYNC_ACK) { |
| Rhyme | 0:87662653a3c6 | 89 | tty->printf("exchangeStatus bad cmd: %02X\n", cmd) ; |
| Rhyme | 0:87662653a3c6 | 90 | result = afERROR_INVALID_COMMAND ; |
| Rhyme | 0:87662653a3c6 | 91 | } |
| Rhyme | 0:87662653a3c6 | 92 | |
| Rhyme | 0:87662653a3c6 | 93 | rx->setBytesToSend(rbytes[index + 0] | (rbytes[index + 1] << 8)) ; |
| Rhyme | 0:87662653a3c6 | 94 | rx->setBytesToRecv(rbytes[index + 2] | (rbytes[index + 3] << 8)) ; |
| Rhyme | 0:87662653a3c6 | 95 | rx->setChecksum(rbytes[index | 4]); |
| Rhyme | 0:87662653a3c6 | 96 | |
| Rhyme | 0:87662653a3c6 | 97 | endSPI() ; |
| Rhyme | 0:87662653a3c6 | 98 | |
| Rhyme | 0:87662653a3c6 | 99 | delete [] bytes ; |
| Rhyme | 0:87662653a3c6 | 100 | delete [] rbytes ; |
| Rhyme | 0:87662653a3c6 | 101 | return result ; |
| Rhyme | 0:87662653a3c6 | 102 | } |
| Rhyme | 0:87662653a3c6 | 103 | |
| Rhyme | 0:87662653a3c6 | 104 | int mbedSPI::writeStatus(StatusCommand *c) |
| Rhyme | 0:87662653a3c6 | 105 | { |
| Rhyme | 0:87662653a3c6 | 106 | int result = afSUCCESS ; |
| Rhyme | 0:87662653a3c6 | 107 | uint16_t len = c->getSize() ; |
| Rhyme | 0:87662653a3c6 | 108 | int *bytes ; |
| Rhyme | 0:87662653a3c6 | 109 | char *rbytes ; |
| Rhyme | 0:87662653a3c6 | 110 | int index = 0 ; |
| Rhyme | 0:87662653a3c6 | 111 | bytes = new int[len] ; |
| Rhyme | 0:87662653a3c6 | 112 | rbytes = new char[len+1] ; |
| Rhyme | 0:87662653a3c6 | 113 | c->getBytes(bytes) ; |
| Rhyme | 0:87662653a3c6 | 114 | |
| Rhyme | 0:87662653a3c6 | 115 | beginSPI() ; |
| Rhyme | 0:87662653a3c6 | 116 | |
| Rhyme | 0:87662653a3c6 | 117 | for (int i = 0 ; i < len ; i++ ) { |
| Rhyme | 0:87662653a3c6 | 118 | rbytes[i] = bytes[i] ; |
| Rhyme | 0:87662653a3c6 | 119 | } |
| Rhyme | 0:87662653a3c6 | 120 | rbytes[len] = c->getChecksum() ; |
| Rhyme | 0:87662653a3c6 | 121 | transfer(rbytes, len + 1) ; |
| Rhyme | 0:87662653a3c6 | 122 | |
| Rhyme | 0:87662653a3c6 | 123 | uint8_t cmd = rbytes[index++] ; |
| Rhyme | 0:87662653a3c6 | 124 | if (cmd != SYNC_REQUEST && cmd != SYNC_ACK) { |
| Rhyme | 0:87662653a3c6 | 125 | tty->printf("writeStatus bad cmd: %02X\n", cmd) ; |
| Rhyme | 0:87662653a3c6 | 126 | result = afERROR_INVALID_COMMAND ; |
| Rhyme | 0:87662653a3c6 | 127 | } |
| Rhyme | 0:87662653a3c6 | 128 | |
| Rhyme | 0:87662653a3c6 | 129 | endSPI() ; |
| Rhyme | 0:87662653a3c6 | 130 | |
| Rhyme | 0:87662653a3c6 | 131 | delete [] rbytes ; |
| Rhyme | 0:87662653a3c6 | 132 | delete [] bytes ; |
| Rhyme | 0:87662653a3c6 | 133 | |
| Rhyme | 0:87662653a3c6 | 134 | return result ; |
| Rhyme | 0:87662653a3c6 | 135 | } |
| Rhyme | 0:87662653a3c6 | 136 | |
| Rhyme | 0:87662653a3c6 | 137 | void mbedSPI::sendBytes(char *bytes, int len) |
| Rhyme | 0:87662653a3c6 | 138 | { |
| Rhyme | 0:87662653a3c6 | 139 | beginSPI() ; |
| Rhyme | 0:87662653a3c6 | 140 | |
| Rhyme | 0:87662653a3c6 | 141 | transfer(bytes, len) ; |
| Rhyme | 0:87662653a3c6 | 142 | |
| Rhyme | 0:87662653a3c6 | 143 | endSPI() ; |
| Rhyme | 0:87662653a3c6 | 144 | } |
| Rhyme | 0:87662653a3c6 | 145 | |
| Rhyme | 0:87662653a3c6 | 146 | void mbedSPI::recvBytes(char *bytes, int len) |
| Rhyme | 0:87662653a3c6 | 147 | { |
| Rhyme | 0:87662653a3c6 | 148 | beginSPI() ; |
| Rhyme | 0:87662653a3c6 | 149 | |
| Rhyme | 0:87662653a3c6 | 150 | transfer(bytes, len) ; |
| Rhyme | 0:87662653a3c6 | 151 | |
| Rhyme | 0:87662653a3c6 | 152 | endSPI() ; |
| Rhyme | 0:87662653a3c6 | 153 | } |
| Rhyme | 0:87662653a3c6 | 154 | |
| Rhyme | 0:87662653a3c6 | 155 | void mbedSPI::sendBytesOffset(char *bytes, uint16_t *bytesToSend, uint16_t *offset) |
| Rhyme | 0:87662653a3c6 | 156 | { |
| Rhyme | 0:87662653a3c6 | 157 | uint16_t len = 0; |
| Rhyme | 0:87662653a3c6 | 158 | |
| Rhyme | 0:87662653a3c6 | 159 | len = *bytesToSend > SPI_FRAME_LEN ? SPI_FRAME_LEN : *bytesToSend; |
| Rhyme | 0:87662653a3c6 | 160 | |
| Rhyme | 0:87662653a3c6 | 161 | char *buffer ; |
| Rhyme | 0:87662653a3c6 | 162 | buffer = new char[len] ; |
| Rhyme | 0:87662653a3c6 | 163 | memset(buffer, 0xff, sizeof(buffer)); |
| Rhyme | 0:87662653a3c6 | 164 | |
| Rhyme | 0:87662653a3c6 | 165 | memcpy(buffer, &bytes[*offset], len); |
| Rhyme | 0:87662653a3c6 | 166 | |
| Rhyme | 0:87662653a3c6 | 167 | sendBytes(buffer, len); |
| Rhyme | 0:87662653a3c6 | 168 | |
| Rhyme | 0:87662653a3c6 | 169 | *offset += len; |
| Rhyme | 0:87662653a3c6 | 170 | *bytesToSend -= len; |
| Rhyme | 0:87662653a3c6 | 171 | |
| Rhyme | 0:87662653a3c6 | 172 | delete [] buffer ; |
| Rhyme | 0:87662653a3c6 | 173 | } |
| Rhyme | 0:87662653a3c6 | 174 | |
| Rhyme | 0:87662653a3c6 | 175 | #if 1 |
| Rhyme | 0:87662653a3c6 | 176 | void mbedSPI::recvBytesOffset(char **bytes, uint16_t *bytesLen, uint16_t *bytesToRecv, uint16_t *offset) |
| Rhyme | 0:87662653a3c6 | 177 | { |
| Rhyme | 0:87662653a3c6 | 178 | uint16_t len = 0; |
| Rhyme | 0:87662653a3c6 | 179 | |
| Rhyme | 0:87662653a3c6 | 180 | len = *bytesToRecv > SPI_FRAME_LEN ? SPI_FRAME_LEN : *bytesToRecv; |
| Rhyme | 0:87662653a3c6 | 181 | |
| Rhyme | 0:87662653a3c6 | 182 | if (*offset == 0) { |
| Rhyme | 0:87662653a3c6 | 183 | *bytesLen = *bytesToRecv; |
| Rhyme | 0:87662653a3c6 | 184 | *bytes = new char[*bytesLen]; |
| Rhyme | 0:87662653a3c6 | 185 | } |
| Rhyme | 0:87662653a3c6 | 186 | |
| Rhyme | 0:87662653a3c6 | 187 | char *start = *bytes + *offset; |
| Rhyme | 0:87662653a3c6 | 188 | |
| Rhyme | 0:87662653a3c6 | 189 | recvBytes(start, len); |
| Rhyme | 0:87662653a3c6 | 190 | |
| Rhyme | 0:87662653a3c6 | 191 | *offset += len; |
| Rhyme | 0:87662653a3c6 | 192 | *bytesToRecv -= len; |
| Rhyme | 0:87662653a3c6 | 193 | } |
| Rhyme | 0:87662653a3c6 | 194 | #endif |
| Rhyme | 0:87662653a3c6 | 195 | |
| Rhyme | 0:87662653a3c6 | 196 | #if 0 |
| Rhyme | 0:87662653a3c6 | 197 | void mbedSPI::recvBytesOffset(char *bytes, uint16_t *bytesLen, uint16_t *bytesToRecv, uint16_t *offset) |
| Rhyme | 0:87662653a3c6 | 198 | { |
| Rhyme | 0:87662653a3c6 | 199 | uint16_t len = 0; |
| Rhyme | 0:87662653a3c6 | 200 | |
| Rhyme | 0:87662653a3c6 | 201 | len = *bytesToRecv > SPI_FRAME_LEN ? SPI_FRAME_LEN : *bytesToRecv; |
| Rhyme | 0:87662653a3c6 | 202 | |
| Rhyme | 0:87662653a3c6 | 203 | if (*offset == 0) { |
| Rhyme | 0:87662653a3c6 | 204 | *bytesLen = *bytesToRecv; |
| Rhyme | 0:87662653a3c6 | 205 | // *bytes = new char[*bytesLen]; |
| Rhyme | 0:87662653a3c6 | 206 | } |
| Rhyme | 0:87662653a3c6 | 207 | |
| Rhyme | 0:87662653a3c6 | 208 | char *start = bytes + *offset; |
| Rhyme | 0:87662653a3c6 | 209 | |
| Rhyme | 0:87662653a3c6 | 210 | recvBytes(start, len); |
| Rhyme | 0:87662653a3c6 | 211 | |
| Rhyme | 0:87662653a3c6 | 212 | *offset += len; |
| Rhyme | 0:87662653a3c6 | 213 | *bytesToRecv -= len; |
| Rhyme | 0:87662653a3c6 | 214 | } |
| Rhyme | 0:87662653a3c6 | 215 | #endif |