Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
UART9BIT.CPP@9:bd6af3d4c651, 2019-08-27 (annotated)
- Committer:
- mark.chen@ebn.com.tw
- Date:
- Tue Aug 27 10:48:12 2019 +0800
- Revision:
- 9:bd6af3d4c651
- Parent:
- 8:716d4274f7b7
- Child:
- 10:89f383af0885
Update code 20190827
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| MarkChen | 0:f0f0961f1727 | 1 | /* mbed Microcontroller Library |
| MarkChen | 0:f0f0961f1727 | 2 | * Copyright (c) 2015-2016 Nuvoton |
| MarkChen | 0:f0f0961f1727 | 3 | * |
| MarkChen | 0:f0f0961f1727 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| MarkChen | 0:f0f0961f1727 | 5 | * you may not use this file except in compliance with the License. |
| MarkChen | 0:f0f0961f1727 | 6 | * You may obtain a copy of the License at |
| MarkChen | 0:f0f0961f1727 | 7 | * |
| MarkChen | 0:f0f0961f1727 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| MarkChen | 0:f0f0961f1727 | 9 | * |
| MarkChen | 0:f0f0961f1727 | 10 | * Unless required by applicable law or agreed to in writing, software |
| MarkChen | 0:f0f0961f1727 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| MarkChen | 0:f0f0961f1727 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| MarkChen | 0:f0f0961f1727 | 13 | * See the License for the specific language governing permissions and |
| MarkChen | 0:f0f0961f1727 | 14 | * limitations under the License. |
| MarkChen | 0:f0f0961f1727 | 15 | */ |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 16 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 17 | |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 18 | #include "UART9BIT.h" |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 19 | #include "serial_api9bit.h" |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 20 | |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 21 | |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 22 | UART9BIT::UART9BIT(PinName tx, PinName rx, int baud,bool en9bit) :_thunk9_irq(this), Serial( tx, rx, baud) |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 23 | { |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 24 | Enable9Bit=en9bit; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 25 | if(en9bit) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 26 | format(9,UART9BIT::None,1); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 27 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 28 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 29 | void UART9BIT::format(int bits, Parity parity, int stop_bits) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 30 | { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 31 | lock(); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 32 | serial_format_9bit(&_serial, bits, (SerialParity)parity, stop_bits); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 33 | unlock(); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 34 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 35 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 36 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 37 | void UART9BIT::interrupt_handler_asynch(void) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 38 | { |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 39 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 40 | int event; |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 41 | int rx_event; |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 42 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 43 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 44 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 45 | if(Enable9Bit) |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 46 | { |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 47 | event=serial_irq_handler_asynch9bit(&_serial); |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 48 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 49 | sprintf(uart9debug,"Uart9 serial_irq_handler_asynch9bit event=%04x\r\n",event); uartdebug=true; |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 50 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 51 | } |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 52 | else |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 53 | { |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 54 | event=serial_irq_handler_asynch(&_serial); |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 55 | //sprintf(uart9debug,"Uart9 serial_irq_handler_asynch \r\n"); |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 56 | // uartdebug=true; |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 57 | } |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 58 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 59 | rx_event = event & SERIAL_EVENT_RX_MASK; |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 60 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 61 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 62 | // sprintf(uart9debug,"Uart9 serial_irq _rx_asynch_set=%d rx_event=%04x event=%04x SERIAL_EVENT_RX_MASK=%04x\r\n",_rx_asynch_set,rx_event,event,SERIAL_EVENT_RX_MASK); |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 63 | // uartdebug=true; |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 64 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 65 | if (_rx_asynch_set && rx_event) { |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 66 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 67 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 68 | event_callback_t cb = _rx_callback; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 69 | _rx_asynch_set = false; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 70 | _rx_callback = NULL; |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 71 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 72 | if (cb) { |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 73 | sprintf(uart9debug,"Uart9 run cb \r\n"); |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 74 | uartdebug=true; |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 75 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 76 | cb.call(rx_event); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 77 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 78 | sleep_manager_unlock_deep_sleep(); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 79 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 80 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 81 | int tx_event = event & SERIAL_EVENT_TX_MASK; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 82 | if (_tx_asynch_set && tx_event) { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 83 | event_callback_t cb = _tx_callback; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 84 | _tx_asynch_set = false; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 85 | _tx_callback = NULL; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 86 | if (cb) { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 87 | cb.call(tx_event); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 88 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 89 | sleep_manager_unlock_deep_sleep(); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 90 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 91 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 92 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 93 | int UART9BIT::write(const uint16_t *buffer, int length, const event_callback_t &callback, int event) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 94 | { |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 95 | int result = 0; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 96 | lock(); |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 97 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 98 | if (!serial_tx_active(&_serial) && !_tx_asynch_set) { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 99 | start_write((void *)buffer, length, 16, callback, event); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 100 | } else { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 101 | result = -1; // transaction ongoing |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 102 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 103 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 104 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 105 | unlock(); |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 106 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 107 | return result; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 108 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 109 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 110 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 111 | void UART9BIT::start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 112 | { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 113 | _tx_asynch_set = true; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 114 | _tx_callback = callback; |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 115 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 116 | _thunk9_irq.callback(&UART9BIT::interrupt_handler_asynch); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 117 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 118 | sleep_manager_lock_deep_sleep(); |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 119 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 120 | if(Enable9Bit) |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 121 | { |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 122 | serial_tx_asynch9bit(&_serial, buffer, buffer_size, buffer_width, _thunk9_irq.entry(), event, _tx_usage); |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 123 | } |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 124 | else |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 125 | { |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 126 | serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk9_irq.entry(), event, _tx_usage); |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 127 | } |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 128 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 129 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 130 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 131 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 132 | void UART9BIT::start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 133 | { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 134 | _rx_asynch_set = true; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 135 | _rx_callback = callback; |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 136 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 137 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 138 | _thunk9_irq.callback(&UART9BIT::interrupt_handler_asynch); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 139 | sleep_manager_lock_deep_sleep(); |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 140 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 141 | if(Enable9Bit) |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 142 | { |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 143 | serial_rx_asynch9bit(&_serial, buffer, buffer_size, buffer_width, _thunk9_irq.entry(), event, char_match, _rx_usage); |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 144 | } |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 145 | else |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 146 | { |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 147 | serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk9_irq.entry(), event, char_match, _rx_usage); |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 148 | } |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 149 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 150 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 151 | |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 152 | uint16_t UART9BIT::getu16() |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 153 | { |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 154 | return serial_getc_9bit(&_serial); |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 155 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 156 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 157 | void UART9BIT::putu16(uint16_t data) |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 158 | { |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 159 | return serial_putc_9bit(&_serial,data); |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 160 | } |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 161 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 162 | int UART9BIT::read(uint16_t *buffer, int length, const event_callback_t &callback, int event, unsigned char char_match) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 163 | { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 164 | int result = 0; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 165 | lock(); |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 166 | if (!serial_rx_active(&_serial) && !_rx_asynch_set) { |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 167 | start_read((void *)buffer, length, 8, callback, event, char_match); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 168 | } else { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 169 | result = -1; // transaction ongoing |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 170 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 171 | unlock(); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 172 | return result; |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 173 | } |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 174 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 175 | int UART9BIT::GitRxDataLen(void) |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 176 | { |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 177 | int retcnt=0; |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 178 | retcnt=_serial.rx_buff.pos; |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 179 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 180 | return retcnt; |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 181 | } |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 182 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 183 | void UART9BIT::SetRxDataLen(size_t len) |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 184 | { |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 185 | int retcnt=0; |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 186 | _serial.rx_buff.pos=len; |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 187 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 188 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 189 | } |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 190 | |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 191 | |
| mark.chen@ebn.com.tw | 1:9d4632d0b16f | 192 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 193 | void UART9BIT::GetUartName(void) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 194 | { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 195 | switch(_serial.serial.uart) |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 196 | { |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 197 | case UART_0: |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 198 | printf("Uart 0 init \r\n"); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 199 | break; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 200 | case UART_1: |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 201 | printf("Uart 1 init \r\n"); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 202 | break; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 203 | case UART_2: |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 204 | printf("Uart 2 init \r\n"); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 205 | break; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 206 | case UART_3: |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 207 | printf("Uart 3 init \r\n"); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 208 | break; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 209 | case UART_4: |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 210 | printf("Uart 4 init \r\n"); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 211 | break; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 212 | case UART_5: |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 213 | printf("Uart 5 init \r\n"); |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 214 | break; |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 215 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 216 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 217 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 218 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 219 | } |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 220 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 221 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 222 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 223 |