afLib 1.3 which is supporting both SPI and UART

Dependencies:   vt100 mbed afLib_1_3

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mbedSPI.cpp Source File

mbedSPI.cpp

00001 #include "mbed.h"
00002 #include "afSPI.h"
00003 #include "mbedSPI.h"
00004 #include "afErrors.h"
00005 #include "msg_types.h"
00006 #include "vt100.h"
00007 extern vt100 *tty ;
00008 
00009 #if defined (TARGET_KL25Z) || defined (TARGET_TEENSY3_1)
00010  #ifndef SPI0_C1
00011   #define SPI0_C1 (*(uint8_t *)0x40076000)
00012  #endif
00013 #endif
00014 
00015 mbedSPI::mbedSPI(PinName mosi, PinName miso, PinName sckl, PinName cs) :
00016     _spi(mosi, miso, sckl), _cs(cs, 1)
00017 {
00018     _spi.format(8, 0) ;
00019     _spi.frequency(1000000) ; /* 1MHz */
00020 
00021 #if defined (TARGET_KL25Z) || defined (TARGET_TEENSY3_1)
00022     #ifndef SPI0_C1
00023         #define SPI0_C1 (*(uint8_t *)0x40076000)
00024     #endif
00025 //    SPI0_C1 |= 0x01 ; /* LSB First */
00026 //    SPI0_C1 &= 0xFE ; /* MSB First */
00027 #endif
00028 
00029 }
00030 
00031 void mbedSPI::begin(void)
00032 {
00033 }
00034 
00035 void mbedSPI::beginSPI(void)
00036 {
00037     _cs = 0 ;
00038     SPI0_C1 |= 0x01 ; /* LSB First */
00039     wait_us(8) ;
00040 }
00041 
00042 void mbedSPI::endSPI(void)
00043 {
00044     _cs = 1 ;
00045     SPI0_C1 &= 0xFE ; /* MSB First */
00046     wait_us(1) ;
00047     // SPI.endTransaction() // in the original code
00048 }
00049 
00050 /**
00051  * on 17-Jan-2018 disable/enable irq added
00052  * before and after of each _spi.writes
00053  */
00054 void mbedSPI::transfer(char *bytes, int len)
00055 {
00056     int i ;
00057     for (i = 0 ; i < len ; i++ ) {
00058         __disable_irq() ; // Disable Interrupts
00059         bytes[i] = _spi.write(bytes[i]) ;
00060         __enable_irq() ; // Enable Interrupts
00061     }
00062 }
00063 
00064 void mbedSPI::checkForInterrupt(volatile int *interrupts_pending, bool idle)
00065 {
00066     // Nothing required here.
00067 }
00068 
00069 int  mbedSPI::exchangeStatus(StatusCommand *tx, StatusCommand *rx)
00070 {
00071     int         result = afSUCCESS ;
00072     uint16_t    len = tx->getSize() ;
00073     int         *bytes ;
00074     char        *rbytes ;
00075     int         index = 0 ;
00076     bytes = new int[len] ;
00077     rbytes = new char[len + 1] ;
00078     tx->getBytes(bytes) ;
00079     
00080     beginSPI() ;
00081     for (int i = 0 ; i < len ; i++ ) {
00082         rbytes[i] = bytes[i] ;
00083     }
00084     rbytes[len] = tx->getChecksum() ;
00085     transfer(rbytes, len + 1) ;
00086     
00087     uint8_t cmd = bytes[index++] ;
00088     if (cmd != SYNC_REQUEST && cmd != SYNC_ACK) {
00089         tty->printf("exchangeStatus bad cmd: %02X\n", cmd) ;
00090         result = afERROR_INVALID_COMMAND ;
00091     }
00092     
00093     rx->setBytesToSend(rbytes[index + 0] | (rbytes[index + 1] << 8)) ;
00094     rx->setBytesToRecv(rbytes[index + 2] | (rbytes[index + 3] << 8)) ;
00095     rx->setChecksum(rbytes[index | 4]);
00096     
00097     endSPI() ;
00098 
00099     delete [] bytes ;
00100     delete [] rbytes ;    
00101     return result ;
00102 }
00103 
00104 int  mbedSPI::writeStatus(StatusCommand *c)
00105 {
00106     int         result = afSUCCESS ;
00107     uint16_t    len = c->getSize() ;
00108     int         *bytes ;
00109     char         *rbytes ;
00110     int         index = 0 ;
00111     bytes = new int[len] ;
00112     rbytes = new char[len+1] ;
00113     c->getBytes(bytes) ;
00114     
00115     beginSPI() ;
00116     
00117     for (int i = 0 ; i < len ; i++ ) {
00118         rbytes[i] = bytes[i] ;
00119     }
00120     rbytes[len] = c->getChecksum() ;
00121     transfer(rbytes, len + 1) ;
00122     
00123     uint8_t cmd = rbytes[index++] ;
00124     if (cmd != SYNC_REQUEST && cmd != SYNC_ACK) {
00125         tty->printf("writeStatus bad cmd: %02X\n", cmd) ;
00126         result = afERROR_INVALID_COMMAND ;
00127     }
00128     
00129     endSPI() ;
00130     
00131     delete [] rbytes ;
00132     delete [] bytes ;
00133     
00134     return result ;
00135 }
00136 
00137 void mbedSPI::sendBytes(char *bytes, int len)
00138 {
00139     beginSPI() ;
00140     
00141     transfer(bytes, len) ;
00142 
00143     endSPI() ;
00144 }
00145 
00146 void mbedSPI::recvBytes(char *bytes, int len)
00147 {
00148     beginSPI() ;
00149     
00150     transfer(bytes, len) ;
00151 
00152     endSPI() ;
00153 }
00154 
00155 void mbedSPI::sendBytesOffset(char *bytes, uint16_t *bytesToSend, uint16_t *offset)
00156 {
00157     uint16_t len = 0;
00158 
00159     len = *bytesToSend > SPI_FRAME_LEN ? SPI_FRAME_LEN : *bytesToSend;
00160 
00161     char *buffer ;
00162     buffer = new char[len] ;
00163     memset(buffer, 0xff, sizeof(buffer));
00164 
00165     memcpy(buffer, &bytes[*offset], len);
00166 
00167     sendBytes(buffer, len);
00168 
00169     *offset += len;
00170     *bytesToSend -= len;
00171     
00172     delete [] buffer ;
00173 }
00174 
00175 #if 1
00176 void mbedSPI::recvBytesOffset(char **bytes, uint16_t *bytesLen, uint16_t *bytesToRecv, uint16_t *offset)
00177 {
00178     uint16_t len = 0;
00179 
00180     len = *bytesToRecv > SPI_FRAME_LEN ? SPI_FRAME_LEN : *bytesToRecv;
00181 
00182     if (*offset == 0) {
00183         *bytesLen = *bytesToRecv;
00184         *bytes = new char[*bytesLen];
00185     }
00186 
00187     char *start = *bytes + *offset;
00188 
00189     recvBytes(start, len);
00190 
00191     *offset += len;
00192     *bytesToRecv -= len;
00193 }
00194 #endif
00195 
00196 #if 0
00197 void mbedSPI::recvBytesOffset(char *bytes, uint16_t *bytesLen, uint16_t *bytesToRecv, uint16_t *offset)
00198 {
00199     uint16_t len = 0;
00200 
00201     len = *bytesToRecv > SPI_FRAME_LEN ? SPI_FRAME_LEN : *bytesToRecv;
00202 
00203     if (*offset == 0) {
00204         *bytesLen = *bytesToRecv;
00205 //        *bytes = new char[*bytesLen];
00206     }
00207 
00208     char *start = bytes + *offset;
00209 
00210     recvBytes(start, len);
00211 
00212     *offset += len;
00213     *bytesToRecv -= len;
00214 }
00215 #endif