![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Renjian Hao
Dependencies: L3G4200D L3GD20 LSM303DLHC LSM303DLM PwmIn Servo mbed
Fork of Fish_2014Fall by
iSerial/iSerial.cpp@0:8f37781c0054, 2015-04-17 (annotated)
- Committer:
- tzxl10000
- Date:
- Fri Apr 17 21:13:44 2015 +0000
- Revision:
- 0:8f37781c0054
Fish_2014 with BBBCamshift
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tzxl10000 | 0:8f37781c0054 | 1 | // |
tzxl10000 | 0:8f37781c0054 | 2 | // iSerial.cpp ... Serial Driver with Interrupt Rec/Send |
tzxl10000 | 0:8f37781c0054 | 3 | // |
tzxl10000 | 0:8f37781c0054 | 4 | // 2009.11.13 ... Originally written by Y.Kuroda for Renesas H83664 |
tzxl10000 | 0:8f37781c0054 | 5 | // 2012.08.31 ... Code convert for mbed in C++ |
tzxl10000 | 0:8f37781c0054 | 6 | // |
tzxl10000 | 0:8f37781c0054 | 7 | #include <stdarg.h> |
tzxl10000 | 0:8f37781c0054 | 8 | #include "mbed.h" |
tzxl10000 | 0:8f37781c0054 | 9 | #include "RingBuffer.h" |
tzxl10000 | 0:8f37781c0054 | 10 | #include "iSerial.h" |
tzxl10000 | 0:8f37781c0054 | 11 | |
tzxl10000 | 0:8f37781c0054 | 12 | //DigitalOut led3(LED3); |
tzxl10000 | 0:8f37781c0054 | 13 | //DigitalOut led4(LED4); |
tzxl10000 | 0:8f37781c0054 | 14 | |
tzxl10000 | 0:8f37781c0054 | 15 | |
tzxl10000 | 0:8f37781c0054 | 16 | void |
tzxl10000 | 0:8f37781c0054 | 17 | iSerial::enable_uart_irq(void) |
tzxl10000 | 0:8f37781c0054 | 18 | { |
tzxl10000 | 0:8f37781c0054 | 19 | switch(tx){ |
tzxl10000 | 0:8f37781c0054 | 20 | case USBTX: |
tzxl10000 | 0:8f37781c0054 | 21 | #if defined(TARGET_LPC1768) |
tzxl10000 | 0:8f37781c0054 | 22 | NVIC_EnableIRQ(UART2_IRQn); |
tzxl10000 | 0:8f37781c0054 | 23 | #elif defined(TARGET_LPC11U24) |
tzxl10000 | 0:8f37781c0054 | 24 | NVIC_EnableIRQ(UART_IRQn); |
tzxl10000 | 0:8f37781c0054 | 25 | #endif |
tzxl10000 | 0:8f37781c0054 | 26 | // led1 = !led1; |
tzxl10000 | 0:8f37781c0054 | 27 | break; |
tzxl10000 | 0:8f37781c0054 | 28 | |
tzxl10000 | 0:8f37781c0054 | 29 | case p9: |
tzxl10000 | 0:8f37781c0054 | 30 | #if defined(TARGET_LPC1768) |
tzxl10000 | 0:8f37781c0054 | 31 | NVIC_EnableIRQ(UART1_IRQn); |
tzxl10000 | 0:8f37781c0054 | 32 | #elif defined(TARGET_LPC11U24) |
tzxl10000 | 0:8f37781c0054 | 33 | NVIC_EnableIRQ(UART_IRQn); |
tzxl10000 | 0:8f37781c0054 | 34 | #endif |
tzxl10000 | 0:8f37781c0054 | 35 | break; |
tzxl10000 | 0:8f37781c0054 | 36 | |
tzxl10000 | 0:8f37781c0054 | 37 | #if defined(TARGET_LPC1768) |
tzxl10000 | 0:8f37781c0054 | 38 | case p13: |
tzxl10000 | 0:8f37781c0054 | 39 | NVIC_EnableIRQ(UART3_IRQn); |
tzxl10000 | 0:8f37781c0054 | 40 | break; |
tzxl10000 | 0:8f37781c0054 | 41 | case p28: |
tzxl10000 | 0:8f37781c0054 | 42 | NVIC_EnableIRQ(UART0_IRQn); |
tzxl10000 | 0:8f37781c0054 | 43 | break; |
tzxl10000 | 0:8f37781c0054 | 44 | #endif |
tzxl10000 | 0:8f37781c0054 | 45 | } |
tzxl10000 | 0:8f37781c0054 | 46 | } |
tzxl10000 | 0:8f37781c0054 | 47 | |
tzxl10000 | 0:8f37781c0054 | 48 | void |
tzxl10000 | 0:8f37781c0054 | 49 | iSerial::disable_uart_irq(void) |
tzxl10000 | 0:8f37781c0054 | 50 | { |
tzxl10000 | 0:8f37781c0054 | 51 | switch(tx){ |
tzxl10000 | 0:8f37781c0054 | 52 | case USBTX: |
tzxl10000 | 0:8f37781c0054 | 53 | #if defined(TARGET_LPC1768) |
tzxl10000 | 0:8f37781c0054 | 54 | NVIC_DisableIRQ(UART2_IRQn); |
tzxl10000 | 0:8f37781c0054 | 55 | #elif defined(TARGET_LPC11U24) |
tzxl10000 | 0:8f37781c0054 | 56 | NVIC_DisableIRQ(UART_IRQn); |
tzxl10000 | 0:8f37781c0054 | 57 | #endif |
tzxl10000 | 0:8f37781c0054 | 58 | // led1 = !led1; |
tzxl10000 | 0:8f37781c0054 | 59 | break; |
tzxl10000 | 0:8f37781c0054 | 60 | |
tzxl10000 | 0:8f37781c0054 | 61 | case p9: |
tzxl10000 | 0:8f37781c0054 | 62 | #if defined(TARGET_LPC1768) |
tzxl10000 | 0:8f37781c0054 | 63 | NVIC_DisableIRQ(UART1_IRQn); |
tzxl10000 | 0:8f37781c0054 | 64 | #elif defined(TARGET_LPC11U24) |
tzxl10000 | 0:8f37781c0054 | 65 | NVIC_DisableIRQ(UART_IRQn); |
tzxl10000 | 0:8f37781c0054 | 66 | #endif |
tzxl10000 | 0:8f37781c0054 | 67 | break; |
tzxl10000 | 0:8f37781c0054 | 68 | |
tzxl10000 | 0:8f37781c0054 | 69 | #if defined(TARGET_LPC1768) |
tzxl10000 | 0:8f37781c0054 | 70 | case p13: |
tzxl10000 | 0:8f37781c0054 | 71 | NVIC_DisableIRQ(UART3_IRQn); |
tzxl10000 | 0:8f37781c0054 | 72 | break; |
tzxl10000 | 0:8f37781c0054 | 73 | case p28: |
tzxl10000 | 0:8f37781c0054 | 74 | NVIC_DisableIRQ(UART0_IRQn); |
tzxl10000 | 0:8f37781c0054 | 75 | break; |
tzxl10000 | 0:8f37781c0054 | 76 | #endif |
tzxl10000 | 0:8f37781c0054 | 77 | } |
tzxl10000 | 0:8f37781c0054 | 78 | } |
tzxl10000 | 0:8f37781c0054 | 79 | |
tzxl10000 | 0:8f37781c0054 | 80 | /* |
tzxl10000 | 0:8f37781c0054 | 81 | * Interrupt Function |
tzxl10000 | 0:8f37781c0054 | 82 | */ |
tzxl10000 | 0:8f37781c0054 | 83 | void |
tzxl10000 | 0:8f37781c0054 | 84 | iSerial::rx_handler(void) |
tzxl10000 | 0:8f37781c0054 | 85 | { |
tzxl10000 | 0:8f37781c0054 | 86 | // led3 = 1; |
tzxl10000 | 0:8f37781c0054 | 87 | while(Serial::readable()) |
tzxl10000 | 0:8f37781c0054 | 88 | rxbuf.save(Serial::getc()); |
tzxl10000 | 0:8f37781c0054 | 89 | // led3 = 0; |
tzxl10000 | 0:8f37781c0054 | 90 | } |
tzxl10000 | 0:8f37781c0054 | 91 | |
tzxl10000 | 0:8f37781c0054 | 92 | void |
tzxl10000 | 0:8f37781c0054 | 93 | iSerial::tx_handler(void) |
tzxl10000 | 0:8f37781c0054 | 94 | { |
tzxl10000 | 0:8f37781c0054 | 95 | // led4 = 1; |
tzxl10000 | 0:8f37781c0054 | 96 | while(Serial::writeable() && txbuf.check()) |
tzxl10000 | 0:8f37781c0054 | 97 | Serial::putc( txbuf.read() ); |
tzxl10000 | 0:8f37781c0054 | 98 | // led4 = 0; |
tzxl10000 | 0:8f37781c0054 | 99 | } |
tzxl10000 | 0:8f37781c0054 | 100 | |
tzxl10000 | 0:8f37781c0054 | 101 | iSerial::iSerial(PinName _tx, PinName _rx, int _txbufsize, int _rxbufsize) |
tzxl10000 | 0:8f37781c0054 | 102 | : Serial(_tx, _rx), |
tzxl10000 | 0:8f37781c0054 | 103 | tx(_tx), |
tzxl10000 | 0:8f37781c0054 | 104 | rx(_rx), |
tzxl10000 | 0:8f37781c0054 | 105 | txbufsize(_txbufsize), |
tzxl10000 | 0:8f37781c0054 | 106 | rxbufsize(_rxbufsize), |
tzxl10000 | 0:8f37781c0054 | 107 | txbuf(RingBuffer(txbufsize)), |
tzxl10000 | 0:8f37781c0054 | 108 | rxbuf(RingBuffer(rxbufsize)), |
tzxl10000 | 0:8f37781c0054 | 109 | str(new char [txbufsize]) |
tzxl10000 | 0:8f37781c0054 | 110 | { |
tzxl10000 | 0:8f37781c0054 | 111 | __disable_irq(); |
tzxl10000 | 0:8f37781c0054 | 112 | |
tzxl10000 | 0:8f37781c0054 | 113 | attach(this, &iSerial::tx_handler, Serial::TxIrq); |
tzxl10000 | 0:8f37781c0054 | 114 | attach(this, &iSerial::rx_handler, Serial::RxIrq); |
tzxl10000 | 0:8f37781c0054 | 115 | |
tzxl10000 | 0:8f37781c0054 | 116 | // format(8,Serial::None,1); // default |
tzxl10000 | 0:8f37781c0054 | 117 | // baud(baudrate); |
tzxl10000 | 0:8f37781c0054 | 118 | |
tzxl10000 | 0:8f37781c0054 | 119 | __enable_irq(); |
tzxl10000 | 0:8f37781c0054 | 120 | enable_uart_irq(); |
tzxl10000 | 0:8f37781c0054 | 121 | } |
tzxl10000 | 0:8f37781c0054 | 122 | |
tzxl10000 | 0:8f37781c0054 | 123 | iSerial::~iSerial() |
tzxl10000 | 0:8f37781c0054 | 124 | { |
tzxl10000 | 0:8f37781c0054 | 125 | delete [] str; |
tzxl10000 | 0:8f37781c0054 | 126 | } |
tzxl10000 | 0:8f37781c0054 | 127 | |
tzxl10000 | 0:8f37781c0054 | 128 | int |
tzxl10000 | 0:8f37781c0054 | 129 | iSerial::readable(void) |
tzxl10000 | 0:8f37781c0054 | 130 | { |
tzxl10000 | 0:8f37781c0054 | 131 | return rxbuf.check(); |
tzxl10000 | 0:8f37781c0054 | 132 | } |
tzxl10000 | 0:8f37781c0054 | 133 | |
tzxl10000 | 0:8f37781c0054 | 134 | int |
tzxl10000 | 0:8f37781c0054 | 135 | iSerial::getc(void) |
tzxl10000 | 0:8f37781c0054 | 136 | { |
tzxl10000 | 0:8f37781c0054 | 137 | unsigned short int c; |
tzxl10000 | 0:8f37781c0054 | 138 | |
tzxl10000 | 0:8f37781c0054 | 139 | while(!rxbuf.check()); // wait receiving a character |
tzxl10000 | 0:8f37781c0054 | 140 | disable_uart_irq(); |
tzxl10000 | 0:8f37781c0054 | 141 | c = rxbuf.read(); |
tzxl10000 | 0:8f37781c0054 | 142 | enable_uart_irq(); |
tzxl10000 | 0:8f37781c0054 | 143 | |
tzxl10000 | 0:8f37781c0054 | 144 | return c; |
tzxl10000 | 0:8f37781c0054 | 145 | } |
tzxl10000 | 0:8f37781c0054 | 146 | |
tzxl10000 | 0:8f37781c0054 | 147 | void |
tzxl10000 | 0:8f37781c0054 | 148 | iSerial::putc(short ch) |
tzxl10000 | 0:8f37781c0054 | 149 | { |
tzxl10000 | 0:8f37781c0054 | 150 | if(txbuf.check()==0 && Serial::writeable()){ |
tzxl10000 | 0:8f37781c0054 | 151 | Serial::putc(ch); |
tzxl10000 | 0:8f37781c0054 | 152 | |
tzxl10000 | 0:8f37781c0054 | 153 | } else { |
tzxl10000 | 0:8f37781c0054 | 154 | while(txbuf.full()){ |
tzxl10000 | 0:8f37781c0054 | 155 | disable_uart_irq(); |
tzxl10000 | 0:8f37781c0054 | 156 | tx_handler(); |
tzxl10000 | 0:8f37781c0054 | 157 | enable_uart_irq(); |
tzxl10000 | 0:8f37781c0054 | 158 | } |
tzxl10000 | 0:8f37781c0054 | 159 | |
tzxl10000 | 0:8f37781c0054 | 160 | disable_uart_irq(); |
tzxl10000 | 0:8f37781c0054 | 161 | txbuf.save(ch); |
tzxl10000 | 0:8f37781c0054 | 162 | enable_uart_irq(); |
tzxl10000 | 0:8f37781c0054 | 163 | } |
tzxl10000 | 0:8f37781c0054 | 164 | } |
tzxl10000 | 0:8f37781c0054 | 165 | |
tzxl10000 | 0:8f37781c0054 | 166 | short int |
tzxl10000 | 0:8f37781c0054 | 167 | iSerial::putstr(const char* s) |
tzxl10000 | 0:8f37781c0054 | 168 | { |
tzxl10000 | 0:8f37781c0054 | 169 | int i=0; |
tzxl10000 | 0:8f37781c0054 | 170 | for(; ; i++){ |
tzxl10000 | 0:8f37781c0054 | 171 | if(*s==0) break; |
tzxl10000 | 0:8f37781c0054 | 172 | putc(*s++); |
tzxl10000 | 0:8f37781c0054 | 173 | } |
tzxl10000 | 0:8f37781c0054 | 174 | return i; |
tzxl10000 | 0:8f37781c0054 | 175 | } |
tzxl10000 | 0:8f37781c0054 | 176 | |
tzxl10000 | 0:8f37781c0054 | 177 | short int |
tzxl10000 | 0:8f37781c0054 | 178 | iSerial::puts(const char* s) |
tzxl10000 | 0:8f37781c0054 | 179 | { |
tzxl10000 | 0:8f37781c0054 | 180 | short int n = putstr(s); |
tzxl10000 | 0:8f37781c0054 | 181 | putc(CR); |
tzxl10000 | 0:8f37781c0054 | 182 | putc(LF); |
tzxl10000 | 0:8f37781c0054 | 183 | return n; |
tzxl10000 | 0:8f37781c0054 | 184 | } |
tzxl10000 | 0:8f37781c0054 | 185 | |
tzxl10000 | 0:8f37781c0054 | 186 | char* |
tzxl10000 | 0:8f37781c0054 | 187 | iSerial::printf(const char* format, ...) |
tzxl10000 | 0:8f37781c0054 | 188 | { |
tzxl10000 | 0:8f37781c0054 | 189 | va_list arg; |
tzxl10000 | 0:8f37781c0054 | 190 | va_start(arg,format); |
tzxl10000 | 0:8f37781c0054 | 191 | vsprintf(str, format, arg); |
tzxl10000 | 0:8f37781c0054 | 192 | va_end(arg); |
tzxl10000 | 0:8f37781c0054 | 193 | putstr(str); |
tzxl10000 | 0:8f37781c0054 | 194 | return str; |
tzxl10000 | 0:8f37781c0054 | 195 | } |
tzxl10000 | 0:8f37781c0054 | 196 | |
tzxl10000 | 0:8f37781c0054 | 197 | |
tzxl10000 | 0:8f37781c0054 | 198 | void |
tzxl10000 | 0:8f37781c0054 | 199 | iSerial::flush(void) |
tzxl10000 | 0:8f37781c0054 | 200 | { |
tzxl10000 | 0:8f37781c0054 | 201 | while(txbuf.check()) |
tzxl10000 | 0:8f37781c0054 | 202 | enable_uart_irq(); |
tzxl10000 | 0:8f37781c0054 | 203 | } |
tzxl10000 | 0:8f37781c0054 | 204 | |
tzxl10000 | 0:8f37781c0054 | 205 |