PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

Revision:
94:b24d2b432b27
Parent:
93:32cd0af29c2b
Child:
95:b02e1b2a6cbd
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/source/serialConnection.cpp	Tue Apr 10 07:47:18 2018 +0000
@@ -0,0 +1,107 @@
+#include "serialConnection.h"
+
+Serial pc(USBTX, USBRX);
+
+void setBaud()
+{
+    pc.baud(460800);
+}
+
+void attachSerialInterrupts()
+{
+// Setup a serial interrupt function to receive data
+    pc.attach(&Rx_interrupt, Serial::RxIrq);
+// Setup a serial interrupt function to transmit data
+    pc.attach(&Tx_interrupt, Serial::TxIrq);
+}
+
+// Interupt Routine to read in data from serial port
+void Rx_interrupt()
+{
+    // Loop just in case more than one character is in UART's receive FIFO buffer
+    // Stop if buffer full
+    while ((pc.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
+        rx_buffer[rx_in] = pc.getc();
+        rx_in = (rx_in + 1) % buffer_size;
+    }
+    return;
+}
+
+
+// Interupt Routine to write out data to serial port
+void Tx_interrupt()
+{
+    // Loop to fill more than one character in UART's transmit FIFO buffer
+    // Stop if buffer empty
+    while ((pc.writeable()) && (tx_in != tx_out)) {
+        pc.putc(tx_buffer[tx_out]);
+        tx_out = (tx_out + 1) % buffer_size;
+    }
+    return;
+}
+
+
+// Copy tx line buffer to large tx buffer for tx interrupt routine
+void send_line()
+{
+    int i;
+    char temp_char;
+    bool empty;
+    i = 0;
+
+    // Start Critical Section - don't interrupt while changing global buffer variables
+    NVIC_DisableIRQ(UART0_IRQn);
+    empty = (tx_in == tx_out);
+    while ((i==0) || (tx_line[i-1] != '\n')) {
+        // Wait if buffer full
+        if (((tx_in + 1) % buffer_size) == tx_out) {
+            // End Critical Section - need to let interrupt routine empty buffer by sending
+            NVIC_EnableIRQ(UART1_IRQn);
+            while (((tx_in + 1) % buffer_size) == tx_out) {
+            }
+            // Start Critical Section - don't interrupt while changing global buffer variables
+            NVIC_DisableIRQ(UART1_IRQn);
+        }
+        tx_buffer[tx_in] = tx_line[i];
+        i++;
+        tx_in = (tx_in + 1) % buffer_size;
+    }
+    if (pc.writeable() && (empty)) {
+        temp_char = tx_buffer[tx_out];
+        tx_out = (tx_out + 1) % buffer_size;
+        // Send first character to start tx interrupts, if stopped
+        pc.putc(temp_char);
+    }
+    // End Critical Section
+    NVIC_EnableIRQ(UART1_IRQn);
+    return;
+}
+
+
+// Read a line from the large rx buffer from rx interrupt routine
+void read_line()
+{
+    int i;
+    i = 0;
+    // Start Critical Section - don't interrupt while changing global buffer variables
+    NVIC_DisableIRQ(UART1_IRQn);
+    // Loop reading rx buffer characters until end of line character
+    while ((i==0) || (rx_line[i-1] != '\r')) {
+        // Wait if buffer empty
+        if (rx_in == rx_out) {
+            // End Critical Section - need to allow rx interrupt to get new characters for buffer
+            NVIC_EnableIRQ(UART1_IRQn);
+            while (rx_in == rx_out) {
+            }
+            // Start Critical Section - don't interrupt while changing global buffer variables
+            NVIC_DisableIRQ(UART1_IRQn);
+        }
+        rx_line[i] = rx_buffer[rx_out];
+        i++;
+        rx_out = (rx_out + 1) % buffer_size;
+    }
+    // End Critical Section
+    NVIC_EnableIRQ(UART1_IRQn);
+    rx_line[i-1] = 0;
+    return;
+}