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@6:64f5dad70ee2, 2019-08-21 (annotated)
- Committer:
- mark.chen@ebn.com.tw
- Date:
- Wed Aug 21 16:27:30 2019 +0800
- Revision:
- 6:64f5dad70ee2
- Parent:
- 5:b97a733f6ed8
- Child:
- 7:af4ceb878eee
Uplaod code 2019/08/21
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 | */ |
MarkChen | 0:f0f0961f1727 | 16 | |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 17 | #include "UART9BIT.h" |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 18 | #include "serial_api9bit.h" |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 19 | |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 20 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 21 | UART9BIT::UART9BIT(PinName tx, PinName rx, int baud,bool en9bit) :_thunk9_irq(this), SerialBase( tx, rx, baud) |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 22 | { |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 23 | Enable9Bit=en9bit; |
mark.chen@ebn.com.tw | 5:b97a733f6ed8 | 24 | printf("Uart9bit init \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 25 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 26 | if(en9bit) |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 27 | format(9,UART9BIT::None,1); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 28 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 29 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 30 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 31 | void UART9BIT::format(int bits, Parity parity, int stop_bits) |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 32 | { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 33 | lock(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 34 | serial_format_9bit(&_serial, bits, (SerialParity)parity, stop_bits); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 35 | unlock(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 36 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 37 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 38 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 39 | void UART9BIT::interrupt_handler_asynch(void) |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 40 | { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 41 | int event = serial_irq_handler_asynch(&_serial); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 42 | int rx_event = event & SERIAL_EVENT_RX_MASK; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 43 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 44 | if (_rx_asynch_set && rx_event) { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 45 | event_callback_t cb = _rx_callback; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 46 | _rx_asynch_set = false; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 47 | _rx_callback = NULL; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 48 | if (cb) { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 49 | cb.call(rx_event); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 50 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 51 | sleep_manager_unlock_deep_sleep(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 52 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 53 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 54 | int tx_event = event & SERIAL_EVENT_TX_MASK; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 55 | if (_tx_asynch_set && tx_event) { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 56 | event_callback_t cb = _tx_callback; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 57 | _tx_asynch_set = false; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 58 | _tx_callback = NULL; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 59 | if (cb) { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 60 | cb.call(tx_event); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 61 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 62 | sleep_manager_unlock_deep_sleep(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 63 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 64 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 65 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 66 | int UART9BIT::write(const uint16_t *buffer, int length, const event_callback_t &callback, int event) |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 67 | { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 68 | int result = 0; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 69 | printf("U9 01 \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 70 | lock(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 71 | // printf("U9 Write 2\r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 72 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 73 | if (!serial_tx_active(&_serial) && !_tx_asynch_set) { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 74 | start_write((void *)buffer, length, 16, callback, event); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 75 | } else { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 76 | result = -1; // transaction ongoing |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 77 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 78 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 79 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 80 | unlock(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 81 | printf("U9 Write ok\r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 82 | return result; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 83 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 84 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 85 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 86 | 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 | 87 | { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 88 | _tx_asynch_set = true; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 89 | _tx_callback = callback; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 90 | printf("U9 10 \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 91 | _thunk9_irq.callback(&UART9BIT::interrupt_handler_asynch); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 92 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 93 | sleep_manager_lock_deep_sleep(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 94 | printf("U9 11 \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 95 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 96 | serial_tx_asynch9bit(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, _tx_usage); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 97 | printf("U9 12 \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 98 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 99 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 100 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 101 | 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 | 102 | { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 103 | _rx_asynch_set = true; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 104 | _rx_callback = callback; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 105 | _thunk9_irq.callback(&UART9BIT::interrupt_handler_asynch); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 106 | sleep_manager_lock_deep_sleep(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 107 | serial_rx_asynch(&_serial, buffer, buffer_size, buffer_width, _thunk_irq.entry(), event, char_match, _rx_usage); |
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 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 112 | 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 | 113 | { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 114 | int result = 0; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 115 | lock(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 116 | if (!serial_rx_active(&_serial) && !_rx_asynch_set) { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 117 | start_read((void *)buffer, length, 16, callback, event, char_match); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 118 | } else { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 119 | result = -1; // transaction ongoing |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 120 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 121 | unlock(); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 122 | return result; |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 123 | } |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 124 | |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 125 | |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 126 | |
mark.chen@ebn.com.tw | 1:9d4632d0b16f | 127 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 128 | void UART9BIT::GetUartName(void) |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 129 | { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 130 | switch(_serial.serial.uart) |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 131 | { |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 132 | case UART_0: |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 133 | printf("Uart 0 init \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 134 | break; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 135 | case UART_1: |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 136 | printf("Uart 1 init \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 137 | break; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 138 | case UART_2: |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 139 | printf("Uart 2 init \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 140 | break; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 141 | case UART_3: |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 142 | printf("Uart 3 init \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 143 | break; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 144 | case UART_4: |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 145 | printf("Uart 4 init \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 146 | break; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 147 | case UART_5: |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 148 | printf("Uart 5 init \r\n"); |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 149 | break; |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 150 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 151 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 152 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 153 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 154 | } |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 155 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 156 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 157 | |
mark.chen@ebn.com.tw | 6:64f5dad70ee2 | 158 |