PES 4 - Smart Medication Dispenser / PES4_ProgrammeforDesignReview2

Dependencies:   SDFileSystem mbed

Fork of PES4_Programme by PES 4 - Smart Medication Dispenser

Committer:
itslinear
Date:
Mon Apr 09 15:19:36 2018 +0000
Revision:
89:f63e4736d875
Child:
93:32cd0af29c2b
added not working serialConnection

Who changed what in which revision?

UserRevisionLine numberNew contents of line
itslinear 89:f63e4736d875 1 #include "serialConnection.h"
itslinear 89:f63e4736d875 2 /*
itslinear 89:f63e4736d875 3 // Setup a serial interrupt function to receive data
itslinear 89:f63e4736d875 4 pc.attach(&Rx_interrupt, Serial::RxIrq);
itslinear 89:f63e4736d875 5 // Setup a serial interrupt function to transmit data
itslinear 89:f63e4736d875 6 pc.attach(&Tx_interrupt, Serial::TxIrq);
itslinear 89:f63e4736d875 7
itslinear 89:f63e4736d875 8
itslinear 89:f63e4736d875 9 // Copy tx line buffer to large tx buffer for tx interrupt routine
itslinear 89:f63e4736d875 10 void send_line()
itslinear 89:f63e4736d875 11 {
itslinear 89:f63e4736d875 12 int i;
itslinear 89:f63e4736d875 13 char temp_char;
itslinear 89:f63e4736d875 14 bool empty;
itslinear 89:f63e4736d875 15 i = 0;
itslinear 89:f63e4736d875 16
itslinear 89:f63e4736d875 17 // Start Critical Section - don't interrupt while changing global buffer variables
itslinear 89:f63e4736d875 18 NVIC_DisableIRQ(UART1_IRQn);
itslinear 89:f63e4736d875 19 empty = (tx_in == tx_out);
itslinear 89:f63e4736d875 20 while ((i==0) || (tx_line[i-1] != '\n')) {
itslinear 89:f63e4736d875 21 // Wait if buffer full
itslinear 89:f63e4736d875 22 if (((tx_in + 1) % buffer_size) == tx_out) {
itslinear 89:f63e4736d875 23 // End Critical Section - need to let interrupt routine empty buffer by sending
itslinear 89:f63e4736d875 24 NVIC_EnableIRQ(UART1_IRQn);
itslinear 89:f63e4736d875 25 while (((tx_in + 1) % buffer_size) == tx_out) {
itslinear 89:f63e4736d875 26 }
itslinear 89:f63e4736d875 27 // Start Critical Section - don't interrupt while changing global buffer variables
itslinear 89:f63e4736d875 28 NVIC_DisableIRQ(UART1_IRQn);
itslinear 89:f63e4736d875 29 }
itslinear 89:f63e4736d875 30 tx_buffer[tx_in] = tx_line[i];
itslinear 89:f63e4736d875 31 i++;
itslinear 89:f63e4736d875 32 tx_in = (tx_in + 1) % buffer_size;
itslinear 89:f63e4736d875 33 }
itslinear 89:f63e4736d875 34 if (pc.writeable() && (empty)) {
itslinear 89:f63e4736d875 35 temp_char = tx_buffer[tx_out];
itslinear 89:f63e4736d875 36 tx_out = (tx_out + 1) % buffer_size;
itslinear 89:f63e4736d875 37 // Send first character to start tx interrupts, if stopped
itslinear 89:f63e4736d875 38 device.putc(temp_char);
itslinear 89:f63e4736d875 39 }
itslinear 89:f63e4736d875 40 // End Critical Section
itslinear 89:f63e4736d875 41 NVIC_EnableIRQ(UART1_IRQn);
itslinear 89:f63e4736d875 42 return;
itslinear 89:f63e4736d875 43 }
itslinear 89:f63e4736d875 44
itslinear 89:f63e4736d875 45
itslinear 89:f63e4736d875 46 // Read a line from the large rx buffer from rx interrupt routine
itslinear 89:f63e4736d875 47 void read_line()
itslinear 89:f63e4736d875 48 {
itslinear 89:f63e4736d875 49 int i;
itslinear 89:f63e4736d875 50 i = 0;
itslinear 89:f63e4736d875 51 // Start Critical Section - don't interrupt while changing global buffer variables
itslinear 89:f63e4736d875 52 NVIC_DisableIRQ(UART1_IRQn);
itslinear 89:f63e4736d875 53 // Loop reading rx buffer characters until end of line character
itslinear 89:f63e4736d875 54 while ((i==0) || (rx_line[i-1] != '\r')) {
itslinear 89:f63e4736d875 55 // Wait if buffer empty
itslinear 89:f63e4736d875 56 if (rx_in == rx_out) {
itslinear 89:f63e4736d875 57 // End Critical Section - need to allow rx interrupt to get new characters for buffer
itslinear 89:f63e4736d875 58 NVIC_EnableIRQ(UART1_IRQn);
itslinear 89:f63e4736d875 59 while (rx_in == rx_out) {
itslinear 89:f63e4736d875 60 }
itslinear 89:f63e4736d875 61 // Start Critical Section - don't interrupt while changing global buffer variables
itslinear 89:f63e4736d875 62 NVIC_DisableIRQ(UART1_IRQn);
itslinear 89:f63e4736d875 63 }
itslinear 89:f63e4736d875 64 rx_line[i] = rx_buffer[rx_out];
itslinear 89:f63e4736d875 65 i++;
itslinear 89:f63e4736d875 66 rx_out = (rx_out + 1) % buffer_size;
itslinear 89:f63e4736d875 67 }
itslinear 89:f63e4736d875 68 // End Critical Section
itslinear 89:f63e4736d875 69 NVIC_EnableIRQ(UART1_IRQn);
itslinear 89:f63e4736d875 70 rx_line[i-1] = 0;
itslinear 89:f63e4736d875 71 return;
itslinear 89:f63e4736d875 72 }
itslinear 89:f63e4736d875 73
itslinear 89:f63e4736d875 74
itslinear 89:f63e4736d875 75 // Interupt Routine to read in data from serial port
itslinear 89:f63e4736d875 76 void Rx_interrupt()
itslinear 89:f63e4736d875 77 {
itslinear 89:f63e4736d875 78 // Loop just in case more than one character is in UART's receive FIFO buffer
itslinear 89:f63e4736d875 79 // Stop if buffer full
itslinear 89:f63e4736d875 80 while ((device.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
itslinear 89:f63e4736d875 81 rx_buffer[rx_in] = device.getc();
itslinear 89:f63e4736d875 82 rx_in = (rx_in + 1) % buffer_size;
itslinear 89:f63e4736d875 83 }
itslinear 89:f63e4736d875 84 return;
itslinear 89:f63e4736d875 85 }
itslinear 89:f63e4736d875 86
itslinear 89:f63e4736d875 87
itslinear 89:f63e4736d875 88 // Interupt Routine to write out data to serial port
itslinear 89:f63e4736d875 89 void Tx_interrupt()
itslinear 89:f63e4736d875 90 {
itslinear 89:f63e4736d875 91 // Loop to fill more than one character in UART's transmit FIFO buffer
itslinear 89:f63e4736d875 92 // Stop if buffer empty
itslinear 89:f63e4736d875 93 while ((device.writeable()) && (tx_in != tx_out)) {
itslinear 89:f63e4736d875 94 device.putc(tx_buffer[tx_out]);
itslinear 89:f63e4736d875 95 tx_out = (tx_out + 1) % buffer_size;
itslinear 89:f63e4736d875 96 }
itslinear 89:f63e4736d875 97 return;
itslinear 89:f63e4736d875 98 }*/