Sarah Marsh
/
Test_Serial_Api_Interrupt
Test
Fork of Test_Serial_Api_Interrupt by
main.cpp@1:a4bb6f6eacf0, 2017-12-08 (annotated)
- Committer:
- sarahmarshy
- Date:
- Fri Dec 08 17:52:01 2017 +0000
- Revision:
- 1:a4bb6f6eacf0
- Parent:
- 0:e5e3838890fd
Fix attach/detach TX IRQ
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
prof_al | 0:e5e3838890fd | 1 | #include "mbed.h" |
sarahmarshy | 1:a4bb6f6eacf0 | 2 | |
sarahmarshy | 1:a4bb6f6eacf0 | 3 | |
sarahmarshy | 1:a4bb6f6eacf0 | 4 | //#define nSerialPatch /* compiler switch for workaround */ |
sarahmarshy | 1:a4bb6f6eacf0 | 5 | |
prof_al | 0:e5e3838890fd | 6 | DigitalOut myled(LED1); |
sarahmarshy | 1:a4bb6f6eacf0 | 7 | |
prof_al | 0:e5e3838890fd | 8 | Serial stComX(USBTX,USBRX);/* |
prof_al | 0:e5e3838890fd | 9 | * Description : UART communicatin (Txd(D1)/Rxd(D0)) USART2 to serial PC COM-Port |
prof_al | 0:e5e3838890fd | 10 | */ |
sarahmarshy | 1:a4bb6f6eacf0 | 11 | |
prof_al | 0:e5e3838890fd | 12 | unsigned char bSizeData; /* |
prof_al | 0:e5e3838890fd | 13 | * Description : number of transmission data |
prof_al | 0:e5e3838890fd | 14 | */ |
sarahmarshy | 1:a4bb6f6eacf0 | 15 | |
prof_al | 0:e5e3838890fd | 16 | unsigned char *pUartData; /* |
prof_al | 0:e5e3838890fd | 17 | * Description : pointer to data |
prof_al | 0:e5e3838890fd | 18 | */ |
sarahmarshy | 1:a4bb6f6eacf0 | 19 | |
prof_al | 0:e5e3838890fd | 20 | unsigned char abString[] = "Hello World!\n\r"; /* |
prof_al | 0:e5e3838890fd | 21 | * Description : test string |
prof_al | 0:e5e3838890fd | 22 | */ |
prof_al | 0:e5e3838890fd | 23 | |
sarahmarshy | 1:a4bb6f6eacf0 | 24 | void vIsrTxComX(void); |
sarahmarshy | 1:a4bb6f6eacf0 | 25 | |
prof_al | 0:e5e3838890fd | 26 | /* new data to UART */ |
prof_al | 0:e5e3838890fd | 27 | unsigned char SYS_vStartComX( unsigned char *pData, unsigned char bSize ){ /* new data to UART */ |
prof_al | 0:e5e3838890fd | 28 | unsigned char i; |
prof_al | 0:e5e3838890fd | 29 | if(bSizeData) i = 0; |
prof_al | 0:e5e3838890fd | 30 | else{ |
prof_al | 0:e5e3838890fd | 31 | bSizeData = bSize; /* number of transmission data */ |
prof_al | 0:e5e3838890fd | 32 | pUartData = pData; |
sarahmarshy | 1:a4bb6f6eacf0 | 33 | //Attach TX Interrupt |
sarahmarshy | 1:a4bb6f6eacf0 | 34 | stComX.attach(&vIsrTxComX, Serial::TxIrq); |
prof_al | 0:e5e3838890fd | 35 | i = 1; |
prof_al | 0:e5e3838890fd | 36 | } |
prof_al | 0:e5e3838890fd | 37 | return i; |
prof_al | 0:e5e3838890fd | 38 | } |
sarahmarshy | 1:a4bb6f6eacf0 | 39 | |
prof_al | 0:e5e3838890fd | 40 | /* TX ISR */ |
prof_al | 0:e5e3838890fd | 41 | void vIsrTxComX( void ){ |
prof_al | 0:e5e3838890fd | 42 | if(stComX.writeable()){ /* check stComX */ |
prof_al | 0:e5e3838890fd | 43 | if(bSizeData){ /* further data available */ |
prof_al | 0:e5e3838890fd | 44 | stComX.putc(*pUartData); /* next data */ |
prof_al | 0:e5e3838890fd | 45 | pUartData++; |
prof_al | 0:e5e3838890fd | 46 | bSizeData--; |
prof_al | 0:e5e3838890fd | 47 | } |
prof_al | 0:e5e3838890fd | 48 | else{ |
sarahmarshy | 1:a4bb6f6eacf0 | 49 | //Detach TX interrupt, nothing left to send |
sarahmarshy | 1:a4bb6f6eacf0 | 50 | stComX.attach(NULL, Serial::TxIrq); /* disable TXE interrupt */ |
prof_al | 0:e5e3838890fd | 51 | } |
prof_al | 0:e5e3838890fd | 52 | } |
prof_al | 0:e5e3838890fd | 53 | } |
prof_al | 0:e5e3838890fd | 54 | |
prof_al | 0:e5e3838890fd | 55 | /* RX ISR */ |
prof_al | 0:e5e3838890fd | 56 | void vIsrRxComX( void ){ |
prof_al | 0:e5e3838890fd | 57 | volatile unsigned char i; |
prof_al | 0:e5e3838890fd | 58 | if(stComX.readable()){ /* check stComX */ |
prof_al | 0:e5e3838890fd | 59 | i = (unsigned char)stComX.getc(); |
prof_al | 0:e5e3838890fd | 60 | if(i == 'n'){ |
prof_al | 0:e5e3838890fd | 61 | SYS_vStartComX( abString, sizeof(abString)-1); |
prof_al | 0:e5e3838890fd | 62 | } |
prof_al | 0:e5e3838890fd | 63 | } |
prof_al | 0:e5e3838890fd | 64 | } |
sarahmarshy | 1:a4bb6f6eacf0 | 65 | |
prof_al | 0:e5e3838890fd | 66 | int main() { |
prof_al | 0:e5e3838890fd | 67 | /* establish serial interrupts */ |
prof_al | 0:e5e3838890fd | 68 | stComX.attach(&vIsrRxComX, Serial::RxIrq); |
sarahmarshy | 1:a4bb6f6eacf0 | 69 | |
prof_al | 0:e5e3838890fd | 70 | /* endless loop */ |
prof_al | 0:e5e3838890fd | 71 | while(1) { |
prof_al | 0:e5e3838890fd | 72 | myled = 1; |
prof_al | 0:e5e3838890fd | 73 | wait(0.2); |
prof_al | 0:e5e3838890fd | 74 | myled = 0; |
prof_al | 0:e5e3838890fd | 75 | wait(0.2); |
prof_al | 0:e5e3838890fd | 76 | } |
sarahmarshy | 1:a4bb6f6eacf0 | 77 | } |