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@13:269ae13f9857, 2019-11-25 (annotated)
- Committer:
- mark.chen@ebn.com.tw
- Date:
- Mon Nov 25 16:02:36 2019 +0800
- Revision:
- 13:269ae13f9857
- Parent:
- 11:2679ddcf8c96
Update code 2019/11/25
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 | 10:89f383af0885 | 54 | event=serial_irq_handler_asynch(&_serial); | 
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 55 | } | 
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 56 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 57 | rx_event = event & SERIAL_EVENT_RX_MASK; | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 58 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 59 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 60 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 61 | if (_rx_asynch_set && rx_event) { | 
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 62 | |
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 63 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 64 | event_callback_t cb = _rx_callback; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 65 | _rx_asynch_set = false; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 66 | _rx_callback = NULL; | 
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 67 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 68 | if (cb) { | 
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 69 | sprintf(uart9debug,"Uart9 run cb \r\n"); | 
| mark.chen@ebn.com.tw | 10:89f383af0885 | 70 | uartdebug=true; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 71 | cb.call(rx_event); | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 72 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 73 | sleep_manager_unlock_deep_sleep(); | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 74 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 75 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 76 | int tx_event = event & SERIAL_EVENT_TX_MASK; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 77 | if (_tx_asynch_set && tx_event) { | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 78 | event_callback_t cb = _tx_callback; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 79 | _tx_asynch_set = false; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 80 | _tx_callback = NULL; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 81 | if (cb) { | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 82 | cb.call(tx_event); | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 83 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 84 | sleep_manager_unlock_deep_sleep(); | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 85 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 86 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 87 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 88 | int UART9BIT::write(const uint16_t *buffer, int length, const event_callback_t &callback, int event) | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 89 | { | 
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 90 | int result = 0; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 91 | lock(); | 
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 92 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 93 | if (!serial_tx_active(&_serial) && !_tx_asynch_set) { | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 94 | start_write((void *)buffer, length, 16, callback, event); | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 95 | } else { | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 96 | result = -1; // transaction ongoing | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 97 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 98 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 99 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 100 | unlock(); | 
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 101 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 102 | return result; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 103 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 104 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 105 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 106 | 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 | 107 | { | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 108 | _tx_asynch_set = true; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 109 | _tx_callback = callback; | 
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 110 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 111 | _thunk9_irq.callback(&UART9BIT::interrupt_handler_asynch); | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 112 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 113 | sleep_manager_lock_deep_sleep(); | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 114 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 115 | if(Enable9Bit) | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 116 | { | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 117 | serial_tx_asynch9bit(&_serial, buffer, buffer_size, buffer_width, _thunk9_irq.entry(), event, _tx_usage); | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 118 | } | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 119 | else | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 120 | { | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 121 | serial_tx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk9_irq.entry(), event, _tx_usage); | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 122 | } | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 123 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 124 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 125 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 126 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 127 | 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 | 128 | { | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 129 | _rx_asynch_set = true; | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 130 | _rx_callback = callback; | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 131 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 132 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 133 | _thunk9_irq.callback(&UART9BIT::interrupt_handler_asynch); | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 134 | sleep_manager_lock_deep_sleep(); | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 135 | |
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 136 | if(Enable9Bit) | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 137 | { | 
| mark.chen@ebn.com.tw | 9:bd6af3d4c651 | 138 | serial_rx_asynch9bit(&_serial, buffer, buffer_size, buffer_width, _thunk9_irq.entry(), event, char_match, _rx_usage); | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 139 | } | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 140 | else | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 141 | { | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 142 | serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk9_irq.entry(), event, char_match, _rx_usage); | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 143 | } | 
| mark.chen@ebn.com.tw | 7:af4ceb878eee | 144 | |
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 145 | } | 
| mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 146 | |
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 147 | uint16_t UART9BIT::getu16() | 
| mark.chen@ebn.com.tw | 8:716d4274f7b7 | 148 | { | 
| mark.chen@ebn.com.tw | 11:2679ddcf8c96 | 149 | return ::serial_getc_9bit(&_serial); | 
| mark.chen@ebn.com.tw | 11:2679ddcf8c96 | 150 | } | 
| mark.chen@ebn.com.tw | 11:2679ddcf8c96 | 151 | |
| mark.chen@ebn.com.tw | 11:2679ddcf8c96 | 152 | int UART9BIT::serial_readable(void) | 
| mark.chen@ebn.com.tw | 11:2679ddcf8c96 | 153 | { | 
| mark.chen@ebn.com.tw | 11:2679ddcf8c96 | 154 | return ::serial_readable_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 | 11:2679ddcf8c96 | 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 |