Patrick McNamara / Mbed 2 deprecated ADC_SDcard_serial

Dependencies:   BufferedSerial SDFileSystem mbed

Fork of SDFileSystem_HelloWorld by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers serial_transfer.h Source File

serial_transfer.h

00001 #include "mbed.h"
00002 
00003 
00004 
00005 
00006 
00007 
00008 /************************************************************************
00009 *  Copy tx line buffer to large tx buffer for tx interrupt routine      *
00010 ************************************************************************/
00011 void send_line() 
00012 {
00013     int i = 0;
00014     char temp_char;
00015     bool empty;
00016 
00017     __disable_irq();                                            //NVIC_DisableIRQ(UART1_IRQn);// Start Critical Section - don't interrupt while changing global buffer variables
00018     
00019     empty = (tx_in == tx_out);
00020     
00021     while ((i==0) || (tx_line[i-1] != '\n')) 
00022     {
00023         
00024         if (((tx_in + 1) % buffer_size) == tx_out)          // Wait if buffer full
00025         {        
00026             __enable_irq();                                  //NVIC_EnableIRQ(UART1_IRQn);// End Critical Section - need to let interrupt routine empty buffer by sending
00027             while (((tx_in + 1) % buffer_size) == tx_out) 
00028             {
00029             }
00030           
00031             __disable_irq();                                  //NVIC_DisableIRQ(UART1_IRQn);  // Start Critical Section - don't interrupt while changing global buffer variables
00032         }
00033 
00034         tx_buffer[tx_in] = tx_line[i];
00035 //        if (tx_buffer[tx_in] == '\r'){pc.printf("returnline");}
00036         i++;
00037         tx_in = (tx_in + 1) % buffer_size;
00038     } 
00039 
00040     if (uart1.writeable() && (empty)) 
00041     {
00042         temp_char = tx_buffer[tx_out];
00043         tx_out = (tx_out + 1) % buffer_size;
00044         
00045         
00046         uart1.putc(temp_char);                                // Send first character to start tx interrupts, if stopped
00047 
00048 //        pc.printf("send command. Value: %c\r\n", temp_char);
00049     }
00050     
00051     __enable_irq();                                              //NVIC_EnableIRQ(UART1_IRQn);  // End Critical Section
00052     return;
00053 }
00054  
00055  
00056  
00057 /************************************************************************
00058 *  Read a line from the large rx buffer from rx interrupt routine       *
00059 ************************************************************************/
00060 void read_line() 
00061 {
00062     int i = 0;
00063     
00064     __disable_irq();                                        //NVIC_DisableIRQ(UART1_IRQn); // Start Critical Section - don't interrupt while changing global buffer variables
00065     while ((i==0) || (rx_line[i-1] != '\r'))                // Loop reading rx buffer characters until end of line character
00066     {
00067         if (rx_in == rx_out)                                // Wait if buffer empty
00068         {
00069             __enable_irq();                                 //NVIC_EnableIRQ(UART1_IRQn); // End Critical Section - need to allow rx interrupt to get new characters for buffer
00070             
00071             while (rx_in == rx_out) 
00072             {
00073             }
00074             __disable_irq();                                //NVIC_DisableIRQ(UART1_IRQn); // Start Critical Section - don't interrupt while changing global buffer variables
00075         }
00076 
00077         rx_line[i] = rx_buffer[rx_out];
00078 
00079         i++;
00080         rx_out = (rx_out + 1) % buffer_size;
00081     }
00082 
00083     __enable_irq();                                         //NVIC_EnableIRQ(UART1_IRQn); // End Critical Section
00084 
00085     strcat(squibCLT_array3,rx_line);
00086     
00087     rx_line[i-1] = 0;
00088      
00089     return;
00090 }
00091  
00092  
00093 
00094 /************************************************************************
00095 *  Interupt Routine to read in data from serial port                    *
00096 ************************************************************************/
00097 void Rx_interrupt() 
00098 {
00099             
00100     while ((uart2.readable()) && (((rx_in + 1) % buffer_size) != rx_out))    // Loop just in case more than one character is in UART's receive FIFO buffer
00101                                                                              // Stop if buffer full
00102     {
00103             rx_buffer[rx_in] = uart2.getc();
00104 //          pc.putc(rx_buffer[rx_in]);                                              // Uncomment to Echo to USB serial to watch data flow
00105             rx_in = (rx_in + 1) % buffer_size;
00106             
00107     }
00108     
00109     rxInterruptCount++;
00110     return;
00111 }
00112  
00113  
00114 
00115 /************************************************************************
00116 *  Interupt Routine to write out data to serial port                    *
00117 ************************************************************************/
00118 void Tx_interrupt() 
00119 {
00120     while ((uart1.writeable()) && (tx_in != tx_out))                        // Loop to fill more than one character in UART's transmit FIFO buffer      // Stop if buffer empty
00121     {
00122 
00123         uart1.putc(tx_buffer[tx_out]);
00124         tx_out = (tx_out + 1) % buffer_size;
00125     }
00126 
00127     return;
00128 }
00129 
00130 
00131 
00132 
00133 /************************************************************************
00134 * copies sd card array to serial buffer and sends it over buffer
00135 *
00136 ************************************************************************/
00137 void sendSdCardDataOverSerial()
00138 {
00139     Timer timer;
00140     int begin, end;
00141     
00142     pc.printf("Sending over serial...");
00143     
00144     timer.start();
00145     begin = timer.read_us();
00146     
00147     for (int i = 0; i < arrayCount; i++)
00148     {
00149         sprintf(tx_line, "%.6f\r\n]", squibCLT_array2[i]);
00150         send_line(); 
00151     }
00152 
00153     end = timer.read_us();
00154     
00155     pc.printf("Done.\r\n");
00156     
00157     pc.printf("Transmit took %d microseconds to complete.\r\n", end - begin);
00158 }
00159 
00160 
00161 
00162 /************************************************************************
00163 * handles receiving data and storing it back into local memory
00164 *
00165 ************************************************************************/    
00166 void receiveSdCardDataOverSerial()
00167 {
00168     Timer timer;
00169     int begin, end;
00170 
00171     pc.printf("Receiving over serial...");
00172         
00173     timer.start();
00174     begin = timer.read_us();
00175     
00176     for (int i = 0; i < arrayCount; i++)
00177     {
00178 // for some reason you have to use this function to read in everything being sent instead of relying on the interrupt service exclusively
00179         read_line();
00180         //sprintf(squib_test, "%s", rx_line);
00181     }
00182     end = timer.read_us();
00183     
00184     pc.printf("Done.\r\n");
00185     
00186     pc.printf("Receive took %d microseconds to complete.\r\n", end - begin);
00187 }
00188