MTS_SPI_Slave unfinished
Fork of MTS-Serial by
MTS_SPI_Slave.cpp@13:010d349bc731, 2017-11-16 (annotated)
- Committer:
- ScottHoppeMultitech
- Date:
- Thu Nov 16 16:13:57 2017 +0000
- Revision:
- 13:010d349bc731
- Child:
- 14:d5a86071845e
unfinished
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ScottHoppeMultitech | 13:010d349bc731 | 1 | #include "mbed.h" |
ScottHoppeMultitech | 13:010d349bc731 | 2 | #include "MTS_SPI_Slave.h" |
ScottHoppeMultitech | 13:010d349bc731 | 3 | #include "MTSLog.h" |
ScottHoppeMultitech | 13:010d349bc731 | 4 | //#include <Thread.h> |
ScottHoppeMultitech | 13:010d349bc731 | 5 | |
ScottHoppeMultitech | 13:010d349bc731 | 6 | using namespace mts; |
ScottHoppeMultitech | 13:010d349bc731 | 7 | Serial debug(USBTX, USBRX); //DELETE |
ScottHoppeMultitech | 13:010d349bc731 | 8 | |
ScottHoppeMultitech | 13:010d349bc731 | 9 | MTS_SPI_Slave::MTS_SPI_Slave(PinName mosi, PinName miso, PinName sclk, PinName ssel, int txBufferSize, int rxBufferSize) |
ScottHoppeMultitech | 13:010d349bc731 | 10 | : MTSBufferedIO(txBufferSize, rxBufferSize) |
ScottHoppeMultitech | 13:010d349bc731 | 11 | , spi(mosi,miso,sclk,ssel) |
ScottHoppeMultitech | 13:010d349bc731 | 12 | |
ScottHoppeMultitech | 13:010d349bc731 | 13 | { |
ScottHoppeMultitech | 13:010d349bc731 | 14 | //this is where you are going to have a thread that is constantly polling to see if data has been sent, having the thread go to sleep in the time inbetween the polling |
ScottHoppeMultitech | 13:010d349bc731 | 15 | Thread thread; |
ScottHoppeMultitech | 13:010d349bc731 | 16 | debug.baud(115200); //DELETE |
ScottHoppeMultitech | 13:010d349bc731 | 17 | //thread.start(this, &MTS_SPI_Slave::recieved_Read); |
ScottHoppeMultitech | 13:010d349bc731 | 18 | osStatus status = thread.start(this, &MTS_SPI_Slave::recieved_Read); |
ScottHoppeMultitech | 13:010d349bc731 | 19 | if (status != osOK) { |
ScottHoppeMultitech | 13:010d349bc731 | 20 | debug.printf("Thread is not OK"); |
ScottHoppeMultitech | 13:010d349bc731 | 21 | } |
ScottHoppeMultitech | 13:010d349bc731 | 22 | debug.printf("Thread state: %d\n\r",thread.get_state()); |
ScottHoppeMultitech | 13:010d349bc731 | 23 | } |
ScottHoppeMultitech | 13:010d349bc731 | 24 | |
ScottHoppeMultitech | 13:010d349bc731 | 25 | MTS_SPI_Slave::~MTS_SPI_Slave() |
ScottHoppeMultitech | 13:010d349bc731 | 26 | { |
ScottHoppeMultitech | 13:010d349bc731 | 27 | } |
ScottHoppeMultitech | 13:010d349bc731 | 28 | |
ScottHoppeMultitech | 13:010d349bc731 | 29 | void MTS_SPI_Slave::recieved_Read(){ |
ScottHoppeMultitech | 13:010d349bc731 | 30 | debug.printf("Outside While\n\r"); |
ScottHoppeMultitech | 13:010d349bc731 | 31 | while(true){ |
ScottHoppeMultitech | 13:010d349bc731 | 32 | // debug.printf("Within While\n\r"); |
ScottHoppeMultitech | 13:010d349bc731 | 33 | if(this->receive()){ |
ScottHoppeMultitech | 13:010d349bc731 | 34 | debug.printf("Recieved\n\r"); |
ScottHoppeMultitech | 13:010d349bc731 | 35 | this->handleRead(); |
ScottHoppeMultitech | 13:010d349bc731 | 36 | } |
ScottHoppeMultitech | 13:010d349bc731 | 37 | } |
ScottHoppeMultitech | 13:010d349bc731 | 38 | } |
ScottHoppeMultitech | 13:010d349bc731 | 39 | |
ScottHoppeMultitech | 13:010d349bc731 | 40 | void MTS_SPI_Slave::frequency(int frequency) |
ScottHoppeMultitech | 13:010d349bc731 | 41 | { |
ScottHoppeMultitech | 13:010d349bc731 | 42 | spi.frequency(frequency); |
ScottHoppeMultitech | 13:010d349bc731 | 43 | } |
ScottHoppeMultitech | 13:010d349bc731 | 44 | |
ScottHoppeMultitech | 13:010d349bc731 | 45 | void MTS_SPI_Slave::format(int format_bits,int format_mode) |
ScottHoppeMultitech | 13:010d349bc731 | 46 | { |
ScottHoppeMultitech | 13:010d349bc731 | 47 | spi.format(format_bits,format_mode); |
ScottHoppeMultitech | 13:010d349bc731 | 48 | } |
ScottHoppeMultitech | 13:010d349bc731 | 49 | |
ScottHoppeMultitech | 13:010d349bc731 | 50 | int MTS_SPI_Slave::receive(){ |
ScottHoppeMultitech | 13:010d349bc731 | 51 | return spi.receive(); |
ScottHoppeMultitech | 13:010d349bc731 | 52 | } |
ScottHoppeMultitech | 13:010d349bc731 | 53 | |
ScottHoppeMultitech | 13:010d349bc731 | 54 | void MTS_SPI_Slave::handleRead(){ |
ScottHoppeMultitech | 13:010d349bc731 | 55 | |
ScottHoppeMultitech | 13:010d349bc731 | 56 | int byte = spi.read(); |
ScottHoppeMultitech | 13:010d349bc731 | 57 | debug.printf("handleRead() executed with byte: %c \n\r",byte); |
ScottHoppeMultitech | 13:010d349bc731 | 58 | if(rxBuffer.write((char)byte) != 1) { |
ScottHoppeMultitech | 13:010d349bc731 | 59 | logError("SPI Rx Byte Dropped [%c][0x%02X]", byte, byte); |
ScottHoppeMultitech | 13:010d349bc731 | 60 | } |
ScottHoppeMultitech | 13:010d349bc731 | 61 | } |
ScottHoppeMultitech | 13:010d349bc731 | 62 | |
ScottHoppeMultitech | 13:010d349bc731 | 63 | void MTS_SPI_Slave::handleWrite(){ |
ScottHoppeMultitech | 13:010d349bc731 | 64 | while(txBuffer.size() != 0) { |
ScottHoppeMultitech | 13:010d349bc731 | 65 | char byte; |
ScottHoppeMultitech | 13:010d349bc731 | 66 | if(txBuffer.read(byte)==1){ |
ScottHoppeMultitech | 13:010d349bc731 | 67 | spi.reply(byte); |
ScottHoppeMultitech | 13:010d349bc731 | 68 | } |
ScottHoppeMultitech | 13:010d349bc731 | 69 | else{ |
ScottHoppeMultitech | 13:010d349bc731 | 70 | return; |
ScottHoppeMultitech | 13:010d349bc731 | 71 | } |
ScottHoppeMultitech | 13:010d349bc731 | 72 | } |
ScottHoppeMultitech | 13:010d349bc731 | 73 | } |
ScottHoppeMultitech | 13:010d349bc731 | 74 |