Chris BAYLEY / Mbed 2 deprecated SPI-2-USB

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Relay bytes received as SPI slave to PC-USB-Serial
00002 
00003 #include "mbed.h"
00004 #include "MODSERIAL.h"
00005 
00006 //SPISlave    slave(p5, NC, p7, p8);
00007 SPISlave    slave(p11, NC, p13, p14);
00008 MODSERIAL   pc(USBTX,NC,1024,0);
00009 //Serial    pc(USBTX,NC);
00010 DigitalOut  ledTX(LED1);
00011 DigitalOut  ledRX(LED2);
00012 DigitalOut  ledOV(LED4);
00013 Timeout     toRXLed;
00014 
00015 
00016 #define fSPI (5000000)
00017 #define BAUD (115200)
00018 
00019 
00020 void offRXLed(void)
00021 {
00022     ledRX = 0;
00023 }
00024 
00025 void onRX(void)
00026 {
00027     ledRX = 1;
00028     toRXLed.attach(offRXLed,0.1);
00029 }
00030 
00031 // This function is called when a character goes from the TX buffer
00032 // to the Uart THR FIFO register.
00033 void txCallback(MODSERIAL_IRQ_INFO *q) {
00034     ledTX = 1;
00035 }
00036 
00037 // This function is called when TX buffer goes empty
00038 void txEmpty(MODSERIAL_IRQ_INFO *q) {
00039     ledTX = 0;
00040 }   
00041 
00042 // This function is called when TX buffer is Overrun
00043 void txOVR(MODSERIAL_IRQ_INFO *q) {
00044     ledOV = 1;
00045 }
00046 
00047   
00048 int main()
00049 {
00050     pc.baud(BAUD);
00051     slave.frequency(fSPI);
00052     pc.attach(&txEmpty,    MODSERIAL::TxEmpty);
00053     pc.attach(&txCallback,    MODSERIAL::TxIrq);
00054     pc.attach(&txOVR,    MODSERIAL::TxOvIrq);
00055     
00056     pc.printf("\n==== SPI 2 USB ready... ===\n");
00057 
00058     while(1)
00059     {
00060         while ( !slave.receive()  );
00061         {
00062             pc.putc( slave.read() );   // Read byte from master
00063             onRX();
00064         }
00065     }
00066 }