Test

Dependencies:   mbed

Fork of Test_Serial_Api_Interrupt by Jens Altenburg

Committer:
prof_al
Date:
Tue Nov 28 16:16:28 2017 +0000
Revision:
0:e5e3838890fd
Child:
1:a4bb6f6eacf0
Program code for testing serial Interrupts

Who changed what in which revision?

UserRevisionLine numberNew contents of line
prof_al 0:e5e3838890fd 1 #include "mbed.h"
prof_al 0:e5e3838890fd 2
prof_al 0:e5e3838890fd 3
prof_al 0:e5e3838890fd 4 #define nSerialPatch /* compiler switch for workaround */
prof_al 0:e5e3838890fd 5
prof_al 0:e5e3838890fd 6 DigitalOut myled(LED1);
prof_al 0:e5e3838890fd 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 */
prof_al 0:e5e3838890fd 11
prof_al 0:e5e3838890fd 12 unsigned char bSizeData; /*
prof_al 0:e5e3838890fd 13 * Description : number of transmission data
prof_al 0:e5e3838890fd 14 */
prof_al 0:e5e3838890fd 15
prof_al 0:e5e3838890fd 16 unsigned char *pUartData; /*
prof_al 0:e5e3838890fd 17 * Description : pointer to data
prof_al 0:e5e3838890fd 18 */
prof_al 0:e5e3838890fd 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
prof_al 0:e5e3838890fd 24 /* new data to UART */
prof_al 0:e5e3838890fd 25 unsigned char SYS_vStartComX( unsigned char *pData, unsigned char bSize ){ /* new data to UART */
prof_al 0:e5e3838890fd 26 unsigned char i;
prof_al 0:e5e3838890fd 27 if(bSizeData) i = 0;
prof_al 0:e5e3838890fd 28 else{
prof_al 0:e5e3838890fd 29 bSizeData = bSize; /* number of transmission data */
prof_al 0:e5e3838890fd 30 pUartData = pData;
prof_al 0:e5e3838890fd 31 stComX.putc(*pUartData); /* first data */
prof_al 0:e5e3838890fd 32 pUartData++;
prof_al 0:e5e3838890fd 33 bSizeData--;
prof_al 0:e5e3838890fd 34 #ifdef nSerialPatch
prof_al 0:e5e3838890fd 35 USART2->CR1 |= USART_CR1_TXEIE; /* enable TXE interrupt */
prof_al 0:e5e3838890fd 36 #endif
prof_al 0:e5e3838890fd 37 i = 1;
prof_al 0:e5e3838890fd 38 }
prof_al 0:e5e3838890fd 39 return i;
prof_al 0:e5e3838890fd 40 }
prof_al 0:e5e3838890fd 41
prof_al 0:e5e3838890fd 42 /* TX ISR */
prof_al 0:e5e3838890fd 43 void vIsrTxComX( void ){
prof_al 0:e5e3838890fd 44 if(stComX.writeable()){ /* check stComX */
prof_al 0:e5e3838890fd 45 if(bSizeData){ /* further data available */
prof_al 0:e5e3838890fd 46 stComX.putc(*pUartData); /* next data */
prof_al 0:e5e3838890fd 47 pUartData++;
prof_al 0:e5e3838890fd 48 bSizeData--;
prof_al 0:e5e3838890fd 49 }
prof_al 0:e5e3838890fd 50 #ifdef nSerialPatch
prof_al 0:e5e3838890fd 51 else{
prof_al 0:e5e3838890fd 52 USART2->CR1 &= ~USART_CR1_TXEIE; /* disable TXE interrupt */
prof_al 0:e5e3838890fd 53 }
prof_al 0:e5e3838890fd 54 #else
prof_al 0:e5e3838890fd 55 stComX.putc('e');
prof_al 0:e5e3838890fd 56 #endif
prof_al 0:e5e3838890fd 57 }
prof_al 0:e5e3838890fd 58 }
prof_al 0:e5e3838890fd 59
prof_al 0:e5e3838890fd 60 /* RX ISR */
prof_al 0:e5e3838890fd 61 void vIsrRxComX( void ){
prof_al 0:e5e3838890fd 62 volatile unsigned char i;
prof_al 0:e5e3838890fd 63 if(stComX.readable()){ /* check stComX */
prof_al 0:e5e3838890fd 64 i = (unsigned char)stComX.getc();
prof_al 0:e5e3838890fd 65 if(i == 'n'){
prof_al 0:e5e3838890fd 66 SYS_vStartComX( abString, sizeof(abString)-1);
prof_al 0:e5e3838890fd 67 }
prof_al 0:e5e3838890fd 68 }
prof_al 0:e5e3838890fd 69 }
prof_al 0:e5e3838890fd 70
prof_al 0:e5e3838890fd 71 int main() {
prof_al 0:e5e3838890fd 72 /* establish serial interrupts */
prof_al 0:e5e3838890fd 73 stComX.attach(&vIsrRxComX, Serial::RxIrq);
prof_al 0:e5e3838890fd 74 stComX.attach(&vIsrTxComX, Serial::TxIrq);
prof_al 0:e5e3838890fd 75
prof_al 0:e5e3838890fd 76 /* endless loop */
prof_al 0:e5e3838890fd 77 while(1) {
prof_al 0:e5e3838890fd 78 myled = 1;
prof_al 0:e5e3838890fd 79 wait(0.2);
prof_al 0:e5e3838890fd 80 myled = 0;
prof_al 0:e5e3838890fd 81 wait(0.2);
prof_al 0:e5e3838890fd 82 }
prof_al 0:e5e3838890fd 83 }