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