UART INTERRUPT FOR READING SERIAL DATA HERE I USED RawSerial class to read data from Slave device on UART PORT. MODULE IS TESTED WITH TERMINAL. JAYDEEP SHAH --radhey04ec@gmail.com

Committer:
radhey04ec
Date:
Mon Jul 20 04:34:34 2020 +0000
Revision:
0:c831d14afee7
USE OF RAWSERIAL CLASS; UART INTERRUPT USAGE; READ DATA FROM SLAVE DEVICE; FINAL COMMIT -- TEST OK

Who changed what in which revision?

UserRevisionLine numberNew contents of line
radhey04ec 0:c831d14afee7 1 //UART - SERIAL INTERRUPT ADVANCE PART
radhey04ec 0:c831d14afee7 2
radhey04ec 0:c831d14afee7 3 //READ DATA FROM SLAVE DEVICE USING UART PORT
radhey04ec 0:c831d14afee7 4
radhey04ec 0:c831d14afee7 5 /* NOTE *****************************************************************
radhey04ec 0:c831d14afee7 6
radhey04ec 0:c831d14afee7 7 If you use normal Serial object than you can face problems of Mutex lock when using Interrupt Handller
radhey04ec 0:c831d14afee7 8
radhey04ec 0:c831d14afee7 9 That leads to crash on board OS, and you can see the error message on Terminal
radhey04ec 0:c831d14afee7 10
radhey04ec 0:c831d14afee7 11 Instead of Serial use --> Rawserial object , so you will not face any problems when using IRQ handler
radhey04ec 0:c831d14afee7 12
radhey04ec 0:c831d14afee7 13 ******************************************************************* */
radhey04ec 0:c831d14afee7 14
radhey04ec 0:c831d14afee7 15 /* About Program : --------------------------------------------------------
radhey04ec 0:c831d14afee7 16 Slave board providing data with different Baud rate -57600
radhey04ec 0:c831d14afee7 17 PC connected with STM Nucleo with 9600 Baud rate
radhey04ec 0:c831d14afee7 18 STM CONNECTED WITH PC AND SLAVE With UART Port
radhey04ec 0:c831d14afee7 19 ----------------------------------------------------------------------------- */
radhey04ec 0:c831d14afee7 20
radhey04ec 0:c831d14afee7 21 /* CREATOR ---
radhey04ec 0:c831d14afee7 22 CREATED BY : JAYDEEP SHAH
radhey04ec 0:c831d14afee7 23 SUBJECT : RECEIVE DATA USING UART IRQ
radhey04ec 0:c831d14afee7 24 radhey04ec@gmail.com
radhey04ec 0:c831d14afee7 25 ------ */
radhey04ec 0:c831d14afee7 26
radhey04ec 0:c831d14afee7 27
radhey04ec 0:c831d14afee7 28
radhey04ec 0:c831d14afee7 29 #include "mbed.h"
radhey04ec 0:c831d14afee7 30 #define BUFFER_SIZE 90 //SIZE OF BUFFER ---DEPENDS ON YOUR DATA SIZE
radhey04ec 0:c831d14afee7 31
radhey04ec 0:c831d14afee7 32 char rxBuffer[BUFFER_SIZE]; //CREATE CIRCULAR BUFFER -- TO STORE RECEIVE DATA
radhey04ec 0:c831d14afee7 33
radhey04ec 0:c831d14afee7 34 unsigned int bufferReadingIndex=0; //CREATE POINTER TO READ DATA FROM UART AND STORE INTO ARRAY
radhey04ec 0:c831d14afee7 35
radhey04ec 0:c831d14afee7 36 unsigned int i=0; //counter to read data from buffer Array
radhey04ec 0:c831d14afee7 37
radhey04ec 0:c831d14afee7 38 // 0 - Rx - PA_0 : Board Tx ___ Arduino Connector socket >>> First_pin _Orange -- If FTDI CABLE USE
radhey04ec 0:c831d14afee7 39 // 1 - Tx - PA_1 : Board Rx ____ Arduino connector socket >>> Second_pin _red -- If FTDI CABLE USE
radhey04ec 0:c831d14afee7 40
radhey04ec 0:c831d14afee7 41 //If you use only Serial calss , there is chance of damage OS because of MUTEX GUARD
radhey04ec 0:c831d14afee7 42
radhey04ec 0:c831d14afee7 43 //use RawSerial class
radhey04ec 0:c831d14afee7 44 RawSerial UT(PA_0,PA_1); //UART PIN DECLARATION
radhey04ec 0:c831d14afee7 45 RawSerial pc(USBTX,USBRX); //HOST PC TERMINAL - 9600 BAUD WITH 8-N-1 STTING
radhey04ec 0:c831d14afee7 46
radhey04ec 0:c831d14afee7 47 //NOTE : UT OBJ FOR SLAVE BOARD & pc OBJ FOR TERMINAL
radhey04ec 0:c831d14afee7 48
radhey04ec 0:c831d14afee7 49 //DEFINE Rx Interrupt function --DECLARATION
radhey04ec 0:c831d14afee7 50 void RxInterrupt(void);
radhey04ec 0:c831d14afee7 51
radhey04ec 0:c831d14afee7 52 //Function that read response data
radhey04ec 0:c831d14afee7 53 void response(void);
radhey04ec 0:c831d14afee7 54
radhey04ec 0:c831d14afee7 55
radhey04ec 0:c831d14afee7 56 int main()
radhey04ec 0:c831d14afee7 57 {
radhey04ec 0:c831d14afee7 58 UT.baud(57600); //BAUD RATE SETTING
radhey04ec 0:c831d14afee7 59 UT.format(8,Serial::None,1); //FORMAT OF UART COMMUNICATION
radhey04ec 0:c831d14afee7 60
radhey04ec 0:c831d14afee7 61 //INTERRUPT ATTACHMENT WHEN RECEIVE DATA
radhey04ec 0:c831d14afee7 62 UT.attach(&RxInterrupt,Serial::RxIrq);
radhey04ec 0:c831d14afee7 63
radhey04ec 0:c831d14afee7 64 pc.printf("\n TESTING TURN ON : \n");
radhey04ec 0:c831d14afee7 65 wait(1);
radhey04ec 0:c831d14afee7 66
radhey04ec 0:c831d14afee7 67 UT.putc('T'); //ENTER IN TEST MODE -- SLAVE BOARD ENTER INTO TEST MODE
radhey04ec 0:c831d14afee7 68 wait(0.5);
radhey04ec 0:c831d14afee7 69 response(); //Read Response from Slave
radhey04ec 0:c831d14afee7 70
radhey04ec 0:c831d14afee7 71
radhey04ec 0:c831d14afee7 72 ThisThread::sleep_for(1000);
radhey04ec 0:c831d14afee7 73
radhey04ec 0:c831d14afee7 74 UT.putc('d'); // RED LED OF SLAVE BOARD ON
radhey04ec 0:c831d14afee7 75 wait(1);
radhey04ec 0:c831d14afee7 76 response(); //Read Response from Slave
radhey04ec 0:c831d14afee7 77
radhey04ec 0:c831d14afee7 78 ThisThread::sleep_for(5000);
radhey04ec 0:c831d14afee7 79
radhey04ec 0:c831d14afee7 80
radhey04ec 0:c831d14afee7 81 UT.putc('b'); // RED LED OF SLAVE BOARD OFF
radhey04ec 0:c831d14afee7 82 wait(1);
radhey04ec 0:c831d14afee7 83 response(); //Read Response from slave
radhey04ec 0:c831d14afee7 84
radhey04ec 0:c831d14afee7 85 ThisThread::sleep_for(1000);
radhey04ec 0:c831d14afee7 86
radhey04ec 0:c831d14afee7 87 UT.putc('Q'); //QUIT FROM TEST MODE -- NORMAL MODE SELECT
radhey04ec 0:c831d14afee7 88 wait(1);
radhey04ec 0:c831d14afee7 89 response();//Read response from Slave
radhey04ec 0:c831d14afee7 90
radhey04ec 0:c831d14afee7 91 ThisThread::sleep_for(3000);
radhey04ec 0:c831d14afee7 92
radhey04ec 0:c831d14afee7 93 while(1)
radhey04ec 0:c831d14afee7 94 {
radhey04ec 0:c831d14afee7 95 }
radhey04ec 0:c831d14afee7 96
radhey04ec 0:c831d14afee7 97 }
radhey04ec 0:c831d14afee7 98
radhey04ec 0:c831d14afee7 99
radhey04ec 0:c831d14afee7 100 void RxInterrupt() //if Rx buffer have data --- Interrupt call
radhey04ec 0:c831d14afee7 101 {
radhey04ec 0:c831d14afee7 102 if(UT.readable()) //IF data available
radhey04ec 0:c831d14afee7 103 {
radhey04ec 0:c831d14afee7 104 rxBuffer[bufferReadingIndex++] = UT.getc(); // read and store into Buffer
radhey04ec 0:c831d14afee7 105 if(bufferReadingIndex >= BUFFER_SIZE) // If limit cross
radhey04ec 0:c831d14afee7 106 {
radhey04ec 0:c831d14afee7 107 bufferReadingIndex =0; // RESET CONTER
radhey04ec 0:c831d14afee7 108 }
radhey04ec 0:c831d14afee7 109 }
radhey04ec 0:c831d14afee7 110 }
radhey04ec 0:c831d14afee7 111
radhey04ec 0:c831d14afee7 112 void response() //FUNCTION TO READ DATA
radhey04ec 0:c831d14afee7 113 {
radhey04ec 0:c831d14afee7 114 char rxByte;
radhey04ec 0:c831d14afee7 115
radhey04ec 0:c831d14afee7 116 while(i != bufferReadingIndex) //READ WHILE COMPLETE DATA NOT RECEIVED
radhey04ec 0:c831d14afee7 117 {
radhey04ec 0:c831d14afee7 118 rxByte = rxBuffer[i]; //READ DATA ONE BY ONE CHARACTER
radhey04ec 0:c831d14afee7 119 pc.putc(rxByte); // SEND ON TERMINAL
radhey04ec 0:c831d14afee7 120 i++; //COUNTER INCREMENT
radhey04ec 0:c831d14afee7 121
radhey04ec 0:c831d14afee7 122 if(i >= BUFFER_SIZE) //IF LIMIT CROSS
radhey04ec 0:c831d14afee7 123 {
radhey04ec 0:c831d14afee7 124 i = 0; //RESET COUNTER
radhey04ec 0:c831d14afee7 125 }
radhey04ec 0:c831d14afee7 126 }
radhey04ec 0:c831d14afee7 127 //NO NEED TO USE BELOW DATA LOGIC
radhey04ec 0:c831d14afee7 128 //i = 0;
radhey04ec 0:c831d14afee7 129 //bufferReadingIndex = 0;
radhey04ec 0:c831d14afee7 130 }
radhey04ec 0:c831d14afee7 131