MTS_SPI_Slave unfinished

Fork of MTS-Serial by MultiTech

Committer:
ScottHoppeMultitech
Date:
Thu Nov 16 17:02:41 2017 +0000
Revision:
14:d5a86071845e
Parent:
13:010d349bc731
added Thread member

Who changed what in which revision?

UserRevisionLine numberNew 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 14:d5a86071845e 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 14:d5a86071845e 12 , _thread(new Thread)
ScottHoppeMultitech 13:010d349bc731 13
ScottHoppeMultitech 13:010d349bc731 14 {
ScottHoppeMultitech 13:010d349bc731 15 //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 14:d5a86071845e 16
ScottHoppeMultitech 14:d5a86071845e 17
ScottHoppeMultitech 13:010d349bc731 18 debug.baud(115200); //DELETE
ScottHoppeMultitech 14:d5a86071845e 19 // _thread->start(this, &MTS_SPI_Slave::recieved_Read);
ScottHoppeMultitech 14:d5a86071845e 20 osStatus status = _thread->start(this, &MTS_SPI_Slave::recieved_Read);
ScottHoppeMultitech 13:010d349bc731 21 if (status != osOK) {
ScottHoppeMultitech 13:010d349bc731 22 debug.printf("Thread is not OK");
ScottHoppeMultitech 13:010d349bc731 23 }
ScottHoppeMultitech 14:d5a86071845e 24 debug.printf("Thread state: %d\n\r",_thread->get_state());
ScottHoppeMultitech 13:010d349bc731 25 }
ScottHoppeMultitech 13:010d349bc731 26
ScottHoppeMultitech 13:010d349bc731 27 MTS_SPI_Slave::~MTS_SPI_Slave()
ScottHoppeMultitech 13:010d349bc731 28 {
ScottHoppeMultitech 13:010d349bc731 29 }
ScottHoppeMultitech 13:010d349bc731 30
ScottHoppeMultitech 13:010d349bc731 31 void MTS_SPI_Slave::recieved_Read(){
ScottHoppeMultitech 13:010d349bc731 32 debug.printf("Outside While\n\r");
ScottHoppeMultitech 13:010d349bc731 33 while(true){
ScottHoppeMultitech 13:010d349bc731 34 // debug.printf("Within While\n\r");
ScottHoppeMultitech 13:010d349bc731 35 if(this->receive()){
ScottHoppeMultitech 13:010d349bc731 36 debug.printf("Recieved\n\r");
ScottHoppeMultitech 13:010d349bc731 37 this->handleRead();
ScottHoppeMultitech 13:010d349bc731 38 }
ScottHoppeMultitech 13:010d349bc731 39 }
ScottHoppeMultitech 13:010d349bc731 40 }
ScottHoppeMultitech 13:010d349bc731 41
ScottHoppeMultitech 13:010d349bc731 42 void MTS_SPI_Slave::frequency(int frequency)
ScottHoppeMultitech 13:010d349bc731 43 {
ScottHoppeMultitech 13:010d349bc731 44 spi.frequency(frequency);
ScottHoppeMultitech 13:010d349bc731 45 }
ScottHoppeMultitech 13:010d349bc731 46
ScottHoppeMultitech 13:010d349bc731 47 void MTS_SPI_Slave::format(int format_bits,int format_mode)
ScottHoppeMultitech 13:010d349bc731 48 {
ScottHoppeMultitech 13:010d349bc731 49 spi.format(format_bits,format_mode);
ScottHoppeMultitech 13:010d349bc731 50 }
ScottHoppeMultitech 13:010d349bc731 51
ScottHoppeMultitech 13:010d349bc731 52 int MTS_SPI_Slave::receive(){
ScottHoppeMultitech 13:010d349bc731 53 return spi.receive();
ScottHoppeMultitech 13:010d349bc731 54 }
ScottHoppeMultitech 13:010d349bc731 55
ScottHoppeMultitech 13:010d349bc731 56 void MTS_SPI_Slave::handleRead(){
ScottHoppeMultitech 13:010d349bc731 57
ScottHoppeMultitech 13:010d349bc731 58 int byte = spi.read();
ScottHoppeMultitech 13:010d349bc731 59 debug.printf("handleRead() executed with byte: %c \n\r",byte);
ScottHoppeMultitech 13:010d349bc731 60 if(rxBuffer.write((char)byte) != 1) {
ScottHoppeMultitech 13:010d349bc731 61 logError("SPI Rx Byte Dropped [%c][0x%02X]", byte, byte);
ScottHoppeMultitech 13:010d349bc731 62 }
ScottHoppeMultitech 13:010d349bc731 63 }
ScottHoppeMultitech 13:010d349bc731 64
ScottHoppeMultitech 13:010d349bc731 65 void MTS_SPI_Slave::handleWrite(){
ScottHoppeMultitech 13:010d349bc731 66 while(txBuffer.size() != 0) {
ScottHoppeMultitech 13:010d349bc731 67 char byte;
ScottHoppeMultitech 13:010d349bc731 68 if(txBuffer.read(byte)==1){
ScottHoppeMultitech 13:010d349bc731 69 spi.reply(byte);
ScottHoppeMultitech 13:010d349bc731 70 }
ScottHoppeMultitech 13:010d349bc731 71 else{
ScottHoppeMultitech 13:010d349bc731 72 return;
ScottHoppeMultitech 13:010d349bc731 73 }
ScottHoppeMultitech 13:010d349bc731 74 }
ScottHoppeMultitech 13:010d349bc731 75 }
ScottHoppeMultitech 13:010d349bc731 76