t
Fork of mbed-dev by
targets/TARGET_WIZNET/TARGET_W7500x/serial_api.c@149:156823d33999, 2016-10-28 (annotated)
- Committer:
- <>
- Date:
- Fri Oct 28 11:17:30 2016 +0100
- Revision:
- 149:156823d33999
- Parent:
- targets/hal/TARGET_WIZNET/TARGET_W7500x/serial_api.c@144:ef7eb2e8f9f7
This updates the lib to the mbed lib v128
NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
<> | 144:ef7eb2e8f9f7 | 1 | /* mbed Microcontroller Library |
<> | 144:ef7eb2e8f9f7 | 2 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 3 | * Copyright (c) 2015 WIZnet Co.,Ltd. All rights reserved. |
<> | 144:ef7eb2e8f9f7 | 4 | * All rights reserved. |
<> | 144:ef7eb2e8f9f7 | 5 | * |
<> | 144:ef7eb2e8f9f7 | 6 | * Redistribution and use in source and binary forms, with or without |
<> | 144:ef7eb2e8f9f7 | 7 | * modification, are permitted provided that the following conditions are met: |
<> | 144:ef7eb2e8f9f7 | 8 | * |
<> | 144:ef7eb2e8f9f7 | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
<> | 144:ef7eb2e8f9f7 | 10 | * this list of conditions and the following disclaimer. |
<> | 144:ef7eb2e8f9f7 | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
<> | 144:ef7eb2e8f9f7 | 12 | * this list of conditions and the following disclaimer in the documentation |
<> | 144:ef7eb2e8f9f7 | 13 | * and/or other materials provided with the distribution. |
<> | 144:ef7eb2e8f9f7 | 14 | * 3. Neither the name of ARM Limited nor the names of its contributors |
<> | 144:ef7eb2e8f9f7 | 15 | * may be used to endorse or promote products derived from this software |
<> | 144:ef7eb2e8f9f7 | 16 | * without specific prior written permission. |
<> | 144:ef7eb2e8f9f7 | 17 | * |
<> | 144:ef7eb2e8f9f7 | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
<> | 144:ef7eb2e8f9f7 | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
<> | 144:ef7eb2e8f9f7 | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
<> | 144:ef7eb2e8f9f7 | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
<> | 144:ef7eb2e8f9f7 | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
<> | 144:ef7eb2e8f9f7 | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
<> | 144:ef7eb2e8f9f7 | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
<> | 144:ef7eb2e8f9f7 | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
<> | 144:ef7eb2e8f9f7 | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
<> | 144:ef7eb2e8f9f7 | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
<> | 144:ef7eb2e8f9f7 | 28 | ******************************************************************************* |
<> | 144:ef7eb2e8f9f7 | 29 | */ |
<> | 144:ef7eb2e8f9f7 | 30 | |
<> | 144:ef7eb2e8f9f7 | 31 | #include "mbed_assert.h" |
<> | 144:ef7eb2e8f9f7 | 32 | #include "serial_api.h" |
<> | 144:ef7eb2e8f9f7 | 33 | |
<> | 144:ef7eb2e8f9f7 | 34 | #if DEVICE_SERIAL |
<> | 144:ef7eb2e8f9f7 | 35 | |
<> | 144:ef7eb2e8f9f7 | 36 | #include "cmsis.h" |
<> | 144:ef7eb2e8f9f7 | 37 | #include "pinmap.h" |
<> | 144:ef7eb2e8f9f7 | 38 | #include <string.h> |
<> | 144:ef7eb2e8f9f7 | 39 | #include "PeripheralPins.h" |
<> | 144:ef7eb2e8f9f7 | 40 | #include "W7500x_uart.h" |
<> | 144:ef7eb2e8f9f7 | 41 | |
<> | 144:ef7eb2e8f9f7 | 42 | #define UART_NUM (3) |
<> | 144:ef7eb2e8f9f7 | 43 | |
<> | 144:ef7eb2e8f9f7 | 44 | static uint32_t serial_irq_ids[UART_NUM] = {0, 0, 0}; |
<> | 144:ef7eb2e8f9f7 | 45 | |
<> | 144:ef7eb2e8f9f7 | 46 | |
<> | 144:ef7eb2e8f9f7 | 47 | static uart_irq_handler irq_handler; |
<> | 144:ef7eb2e8f9f7 | 48 | static UART_TypeDef *UART; |
<> | 144:ef7eb2e8f9f7 | 49 | |
<> | 144:ef7eb2e8f9f7 | 50 | |
<> | 144:ef7eb2e8f9f7 | 51 | UART_InitTypeDef UART_InitStructure; |
<> | 144:ef7eb2e8f9f7 | 52 | |
<> | 144:ef7eb2e8f9f7 | 53 | int stdio_uart_inited = 0; |
<> | 144:ef7eb2e8f9f7 | 54 | serial_t stdio_uart; |
<> | 144:ef7eb2e8f9f7 | 55 | |
<> | 144:ef7eb2e8f9f7 | 56 | static void init_uart(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 57 | { |
<> | 144:ef7eb2e8f9f7 | 58 | if(obj->index == 2) // For UART2, It is simple UART. |
<> | 144:ef7eb2e8f9f7 | 59 | { |
<> | 144:ef7eb2e8f9f7 | 60 | SystemCoreClockUpdate(); |
<> | 144:ef7eb2e8f9f7 | 61 | //S_UART_Init(obj->baudrate); |
<> | 144:ef7eb2e8f9f7 | 62 | S_UART_SetCTRL((S_UART_CTRL_RX_EN|S_UART_CTRL_TX_EN), DISABLE); |
<> | 144:ef7eb2e8f9f7 | 63 | S_UART_SetBaud(obj->baudrate); |
<> | 144:ef7eb2e8f9f7 | 64 | |
<> | 144:ef7eb2e8f9f7 | 65 | if(obj->pin_rx == NC) |
<> | 144:ef7eb2e8f9f7 | 66 | { |
<> | 144:ef7eb2e8f9f7 | 67 | S_UART_SetCTRL(S_UART_CTRL_TX_EN, ENABLE); |
<> | 144:ef7eb2e8f9f7 | 68 | } |
<> | 144:ef7eb2e8f9f7 | 69 | else if(obj->pin_tx == NC) |
<> | 144:ef7eb2e8f9f7 | 70 | { |
<> | 144:ef7eb2e8f9f7 | 71 | S_UART_SetCTRL(S_UART_CTRL_RX_EN, ENABLE); |
<> | 144:ef7eb2e8f9f7 | 72 | } |
<> | 144:ef7eb2e8f9f7 | 73 | else |
<> | 144:ef7eb2e8f9f7 | 74 | { |
<> | 144:ef7eb2e8f9f7 | 75 | S_UART_SetCTRL((S_UART_CTRL_TX_EN|S_UART_CTRL_RX_EN),ENABLE); |
<> | 144:ef7eb2e8f9f7 | 76 | } |
<> | 144:ef7eb2e8f9f7 | 77 | } |
<> | 144:ef7eb2e8f9f7 | 78 | else // For UART0 and UART1. |
<> | 144:ef7eb2e8f9f7 | 79 | { |
<> | 144:ef7eb2e8f9f7 | 80 | UART = (UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 81 | UART_InitStructure.UART_BaudRate = obj->baudrate; |
<> | 144:ef7eb2e8f9f7 | 82 | UART_InitStructure.UART_WordLength = obj->databits; |
<> | 144:ef7eb2e8f9f7 | 83 | UART_InitStructure.UART_StopBits = obj->stopbits; |
<> | 144:ef7eb2e8f9f7 | 84 | UART_InitStructure.UART_Parity = obj->parity; |
<> | 144:ef7eb2e8f9f7 | 85 | UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None; |
<> | 144:ef7eb2e8f9f7 | 86 | |
<> | 144:ef7eb2e8f9f7 | 87 | |
<> | 144:ef7eb2e8f9f7 | 88 | if (obj->pin_rx == NC) { |
<> | 144:ef7eb2e8f9f7 | 89 | UART_InitStructure.UART_Mode = UART_Mode_Tx; |
<> | 144:ef7eb2e8f9f7 | 90 | } else if (obj->pin_tx == NC) { |
<> | 144:ef7eb2e8f9f7 | 91 | UART_InitStructure.UART_Mode = UART_Mode_Rx; |
<> | 144:ef7eb2e8f9f7 | 92 | } else { |
<> | 144:ef7eb2e8f9f7 | 93 | UART_InitStructure.UART_Mode = (UART_Mode_Rx | UART_Mode_Tx); |
<> | 144:ef7eb2e8f9f7 | 94 | } |
<> | 144:ef7eb2e8f9f7 | 95 | |
<> | 144:ef7eb2e8f9f7 | 96 | UART_Init(UART,&UART_InitStructure); |
<> | 144:ef7eb2e8f9f7 | 97 | } |
<> | 144:ef7eb2e8f9f7 | 98 | } |
<> | 144:ef7eb2e8f9f7 | 99 | |
<> | 144:ef7eb2e8f9f7 | 100 | void serial_init(serial_t *obj, PinName tx, PinName rx) |
<> | 144:ef7eb2e8f9f7 | 101 | { |
<> | 144:ef7eb2e8f9f7 | 102 | // Determine the UART to use (UART_1, UART_2, ...) |
<> | 144:ef7eb2e8f9f7 | 103 | UARTName uart_tx = (UARTName)pinmap_peripheral(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 104 | UARTName uart_rx = (UARTName)pinmap_peripheral(rx, PinMap_UART_RX); |
<> | 144:ef7eb2e8f9f7 | 105 | |
<> | 144:ef7eb2e8f9f7 | 106 | // Get the peripheral name (UART_1, UART_2, ...) from the pin and assign it to the object |
<> | 144:ef7eb2e8f9f7 | 107 | obj->uart = (UARTName)pinmap_merge(uart_tx, uart_rx); |
<> | 144:ef7eb2e8f9f7 | 108 | |
<> | 144:ef7eb2e8f9f7 | 109 | MBED_ASSERT(obj->uart != (UARTName)NC); |
<> | 144:ef7eb2e8f9f7 | 110 | |
<> | 144:ef7eb2e8f9f7 | 111 | // Enable USART clock |
<> | 144:ef7eb2e8f9f7 | 112 | if (obj->uart == UART_0) { |
<> | 144:ef7eb2e8f9f7 | 113 | obj->index = 0; |
<> | 144:ef7eb2e8f9f7 | 114 | } |
<> | 144:ef7eb2e8f9f7 | 115 | |
<> | 144:ef7eb2e8f9f7 | 116 | if (obj->uart == UART_1) { |
<> | 144:ef7eb2e8f9f7 | 117 | obj->index = 1; |
<> | 144:ef7eb2e8f9f7 | 118 | } |
<> | 144:ef7eb2e8f9f7 | 119 | |
<> | 144:ef7eb2e8f9f7 | 120 | if (obj->uart == UART_2) { |
<> | 144:ef7eb2e8f9f7 | 121 | obj->index = 2; |
<> | 144:ef7eb2e8f9f7 | 122 | } |
<> | 144:ef7eb2e8f9f7 | 123 | |
<> | 144:ef7eb2e8f9f7 | 124 | // Configure the UART pins |
<> | 144:ef7eb2e8f9f7 | 125 | pinmap_pinout(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 126 | pinmap_pinout(rx, PinMap_UART_RX); |
<> | 144:ef7eb2e8f9f7 | 127 | if (tx != NC) { |
<> | 144:ef7eb2e8f9f7 | 128 | pin_mode(tx, PullUp); |
<> | 144:ef7eb2e8f9f7 | 129 | } |
<> | 144:ef7eb2e8f9f7 | 130 | if (rx != NC) { |
<> | 144:ef7eb2e8f9f7 | 131 | pin_mode(rx, PullUp); |
<> | 144:ef7eb2e8f9f7 | 132 | } |
<> | 144:ef7eb2e8f9f7 | 133 | |
<> | 144:ef7eb2e8f9f7 | 134 | // Configure UART |
<> | 144:ef7eb2e8f9f7 | 135 | obj->baudrate = 9600; |
<> | 144:ef7eb2e8f9f7 | 136 | obj->databits = UART_WordLength_8b; |
<> | 144:ef7eb2e8f9f7 | 137 | obj->stopbits = UART_StopBits_1; |
<> | 144:ef7eb2e8f9f7 | 138 | obj->parity = UART_Parity_No; |
<> | 144:ef7eb2e8f9f7 | 139 | |
<> | 144:ef7eb2e8f9f7 | 140 | obj->pin_tx = tx; |
<> | 144:ef7eb2e8f9f7 | 141 | obj->pin_rx = rx; |
<> | 144:ef7eb2e8f9f7 | 142 | |
<> | 144:ef7eb2e8f9f7 | 143 | |
<> | 144:ef7eb2e8f9f7 | 144 | init_uart(obj); |
<> | 144:ef7eb2e8f9f7 | 145 | |
<> | 144:ef7eb2e8f9f7 | 146 | // For stdio management |
<> | 144:ef7eb2e8f9f7 | 147 | if (obj->uart == STDIO_UART) { |
<> | 144:ef7eb2e8f9f7 | 148 | stdio_uart_inited = 1; |
<> | 144:ef7eb2e8f9f7 | 149 | memcpy(&stdio_uart, obj, sizeof(serial_t)); |
<> | 144:ef7eb2e8f9f7 | 150 | } |
<> | 144:ef7eb2e8f9f7 | 151 | } |
<> | 144:ef7eb2e8f9f7 | 152 | |
<> | 144:ef7eb2e8f9f7 | 153 | void serial_free(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 154 | { |
<> | 144:ef7eb2e8f9f7 | 155 | // Reset UART and disable clock |
<> | 144:ef7eb2e8f9f7 | 156 | if (obj->uart == UART_0) { |
<> | 144:ef7eb2e8f9f7 | 157 | } |
<> | 144:ef7eb2e8f9f7 | 158 | |
<> | 144:ef7eb2e8f9f7 | 159 | if (obj->uart == UART_1) { |
<> | 144:ef7eb2e8f9f7 | 160 | } |
<> | 144:ef7eb2e8f9f7 | 161 | if (obj->uart == UART_2) { |
<> | 144:ef7eb2e8f9f7 | 162 | } |
<> | 144:ef7eb2e8f9f7 | 163 | |
<> | 144:ef7eb2e8f9f7 | 164 | serial_irq_ids[obj->index] = 0; |
<> | 144:ef7eb2e8f9f7 | 165 | } |
<> | 144:ef7eb2e8f9f7 | 166 | |
<> | 144:ef7eb2e8f9f7 | 167 | void serial_baud(serial_t *obj, int baudrate) |
<> | 144:ef7eb2e8f9f7 | 168 | { |
<> | 144:ef7eb2e8f9f7 | 169 | obj->baudrate = baudrate; |
<> | 144:ef7eb2e8f9f7 | 170 | init_uart(obj); |
<> | 144:ef7eb2e8f9f7 | 171 | } |
<> | 144:ef7eb2e8f9f7 | 172 | |
<> | 144:ef7eb2e8f9f7 | 173 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
<> | 144:ef7eb2e8f9f7 | 174 | { |
<> | 144:ef7eb2e8f9f7 | 175 | if (data_bits == 7) { |
<> | 144:ef7eb2e8f9f7 | 176 | obj->databits = UART_WordLength_7b; |
<> | 144:ef7eb2e8f9f7 | 177 | } else { |
<> | 144:ef7eb2e8f9f7 | 178 | obj->databits = UART_WordLength_8b; |
<> | 144:ef7eb2e8f9f7 | 179 | } |
<> | 144:ef7eb2e8f9f7 | 180 | |
<> | 144:ef7eb2e8f9f7 | 181 | switch (parity) { |
<> | 144:ef7eb2e8f9f7 | 182 | case ParityOdd: |
<> | 144:ef7eb2e8f9f7 | 183 | case ParityForced0: |
<> | 144:ef7eb2e8f9f7 | 184 | obj->parity = UART_Parity_Odd; |
<> | 144:ef7eb2e8f9f7 | 185 | break; |
<> | 144:ef7eb2e8f9f7 | 186 | case ParityEven: |
<> | 144:ef7eb2e8f9f7 | 187 | case ParityForced1: |
<> | 144:ef7eb2e8f9f7 | 188 | obj->parity = UART_Parity_Even; |
<> | 144:ef7eb2e8f9f7 | 189 | break; |
<> | 144:ef7eb2e8f9f7 | 190 | default: // ParityNone |
<> | 144:ef7eb2e8f9f7 | 191 | obj->parity = UART_Parity_No; |
<> | 144:ef7eb2e8f9f7 | 192 | break; |
<> | 144:ef7eb2e8f9f7 | 193 | } |
<> | 144:ef7eb2e8f9f7 | 194 | |
<> | 144:ef7eb2e8f9f7 | 195 | if (stop_bits == 2) { |
<> | 144:ef7eb2e8f9f7 | 196 | obj->stopbits = UART_StopBits_2; |
<> | 144:ef7eb2e8f9f7 | 197 | } else { |
<> | 144:ef7eb2e8f9f7 | 198 | obj->stopbits = UART_StopBits_1; |
<> | 144:ef7eb2e8f9f7 | 199 | } |
<> | 144:ef7eb2e8f9f7 | 200 | |
<> | 144:ef7eb2e8f9f7 | 201 | init_uart(obj); |
<> | 144:ef7eb2e8f9f7 | 202 | } |
<> | 144:ef7eb2e8f9f7 | 203 | |
<> | 144:ef7eb2e8f9f7 | 204 | /****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 205 | * INTERRUPTS HANDLING |
<> | 144:ef7eb2e8f9f7 | 206 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 207 | |
<> | 144:ef7eb2e8f9f7 | 208 | static void uart_irq(UARTName name, int id) |
<> | 144:ef7eb2e8f9f7 | 209 | { |
<> | 144:ef7eb2e8f9f7 | 210 | UART = (UART_TypeDef *)name; |
<> | 144:ef7eb2e8f9f7 | 211 | if (serial_irq_ids[id] != 0) { |
<> | 144:ef7eb2e8f9f7 | 212 | if( UART_GetITStatus(UART,UART_IT_FLAG_TXI) != RESET ){ |
<> | 144:ef7eb2e8f9f7 | 213 | irq_handler(serial_irq_ids[id], TxIrq); |
<> | 144:ef7eb2e8f9f7 | 214 | UART_ClearITPendingBit(UART,UART_IT_FLAG_TXI); |
<> | 144:ef7eb2e8f9f7 | 215 | } |
<> | 144:ef7eb2e8f9f7 | 216 | if( UART_GetITStatus(UART,UART_IT_FLAG_RXI) != RESET ){ |
<> | 144:ef7eb2e8f9f7 | 217 | irq_handler(serial_irq_ids[id], RxIrq); |
<> | 144:ef7eb2e8f9f7 | 218 | } |
<> | 144:ef7eb2e8f9f7 | 219 | } |
<> | 144:ef7eb2e8f9f7 | 220 | } |
<> | 144:ef7eb2e8f9f7 | 221 | |
<> | 144:ef7eb2e8f9f7 | 222 | static void uart2_irq() |
<> | 144:ef7eb2e8f9f7 | 223 | { |
<> | 144:ef7eb2e8f9f7 | 224 | if(serial_irq_ids[2] != 0){ |
<> | 144:ef7eb2e8f9f7 | 225 | if( S_UART_GetITStatus(S_UART_INTSTATUS_TXI) != RESET ){ |
<> | 144:ef7eb2e8f9f7 | 226 | S_UART_ClearITPendingBit(S_UART_INTSTATUS_TXI); |
<> | 144:ef7eb2e8f9f7 | 227 | irq_handler(serial_irq_ids[2], TxIrq); |
<> | 144:ef7eb2e8f9f7 | 228 | } |
<> | 144:ef7eb2e8f9f7 | 229 | if( S_UART_GetITStatus(S_UART_INTSTATUS_RXI) != RESET ) { |
<> | 144:ef7eb2e8f9f7 | 230 | S_UART_ClearITPendingBit(S_UART_INTSTATUS_RXI); |
<> | 144:ef7eb2e8f9f7 | 231 | irq_handler(serial_irq_ids[2], RxIrq); |
<> | 144:ef7eb2e8f9f7 | 232 | } |
<> | 144:ef7eb2e8f9f7 | 233 | } |
<> | 144:ef7eb2e8f9f7 | 234 | } |
<> | 144:ef7eb2e8f9f7 | 235 | |
<> | 144:ef7eb2e8f9f7 | 236 | #ifdef __cplusplus |
<> | 144:ef7eb2e8f9f7 | 237 | extern "C"{ |
<> | 144:ef7eb2e8f9f7 | 238 | #endif |
<> | 144:ef7eb2e8f9f7 | 239 | void UART0_Handler() |
<> | 144:ef7eb2e8f9f7 | 240 | { |
<> | 144:ef7eb2e8f9f7 | 241 | uart_irq(UART_0, 0); |
<> | 144:ef7eb2e8f9f7 | 242 | } |
<> | 144:ef7eb2e8f9f7 | 243 | |
<> | 144:ef7eb2e8f9f7 | 244 | void UART1_Handler() |
<> | 144:ef7eb2e8f9f7 | 245 | { |
<> | 144:ef7eb2e8f9f7 | 246 | uart_irq(UART_1, 1); |
<> | 144:ef7eb2e8f9f7 | 247 | } |
<> | 144:ef7eb2e8f9f7 | 248 | |
<> | 144:ef7eb2e8f9f7 | 249 | void UART2_Handler() |
<> | 144:ef7eb2e8f9f7 | 250 | { |
<> | 144:ef7eb2e8f9f7 | 251 | uart2_irq(); |
<> | 144:ef7eb2e8f9f7 | 252 | } |
<> | 144:ef7eb2e8f9f7 | 253 | #ifdef __cplusplus |
<> | 144:ef7eb2e8f9f7 | 254 | } |
<> | 144:ef7eb2e8f9f7 | 255 | #endif |
<> | 144:ef7eb2e8f9f7 | 256 | |
<> | 144:ef7eb2e8f9f7 | 257 | |
<> | 144:ef7eb2e8f9f7 | 258 | void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id) |
<> | 144:ef7eb2e8f9f7 | 259 | { |
<> | 144:ef7eb2e8f9f7 | 260 | irq_handler = handler; |
<> | 144:ef7eb2e8f9f7 | 261 | serial_irq_ids[obj->index] = id; |
<> | 144:ef7eb2e8f9f7 | 262 | } |
<> | 144:ef7eb2e8f9f7 | 263 | |
<> | 144:ef7eb2e8f9f7 | 264 | void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable) |
<> | 144:ef7eb2e8f9f7 | 265 | { |
<> | 144:ef7eb2e8f9f7 | 266 | IRQn_Type irq_n = (IRQn_Type)0; |
<> | 144:ef7eb2e8f9f7 | 267 | |
<> | 144:ef7eb2e8f9f7 | 268 | |
<> | 144:ef7eb2e8f9f7 | 269 | if (obj->uart == UART_2) |
<> | 144:ef7eb2e8f9f7 | 270 | { |
<> | 144:ef7eb2e8f9f7 | 271 | irq_n = UART2_IRQn; |
<> | 144:ef7eb2e8f9f7 | 272 | |
<> | 144:ef7eb2e8f9f7 | 273 | if (enable){ |
<> | 144:ef7eb2e8f9f7 | 274 | if (irq == RxIrq){ |
<> | 144:ef7eb2e8f9f7 | 275 | S_UART_ITConfig(S_UART_CTRL_RXI,ENABLE); |
<> | 144:ef7eb2e8f9f7 | 276 | } else { |
<> | 144:ef7eb2e8f9f7 | 277 | S_UART_ITConfig(S_UART_CTRL_TXI,ENABLE); |
<> | 144:ef7eb2e8f9f7 | 278 | } |
<> | 144:ef7eb2e8f9f7 | 279 | NVIC_ClearPendingIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 280 | NVIC_EnableIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 281 | } else { // disable |
<> | 144:ef7eb2e8f9f7 | 282 | S_UART_ITConfig((S_UART_CTRL_RXI|S_UART_CTRL_TXI),DISABLE); |
<> | 144:ef7eb2e8f9f7 | 283 | NVIC_DisableIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 284 | } |
<> | 144:ef7eb2e8f9f7 | 285 | } |
<> | 144:ef7eb2e8f9f7 | 286 | else |
<> | 144:ef7eb2e8f9f7 | 287 | { |
<> | 144:ef7eb2e8f9f7 | 288 | UART = (UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 289 | |
<> | 144:ef7eb2e8f9f7 | 290 | if (obj->uart == UART_0) { |
<> | 144:ef7eb2e8f9f7 | 291 | irq_n = UART0_IRQn; |
<> | 144:ef7eb2e8f9f7 | 292 | } |
<> | 144:ef7eb2e8f9f7 | 293 | |
<> | 144:ef7eb2e8f9f7 | 294 | if (obj->uart == UART_1) { |
<> | 144:ef7eb2e8f9f7 | 295 | irq_n = UART1_IRQn; |
<> | 144:ef7eb2e8f9f7 | 296 | } |
<> | 144:ef7eb2e8f9f7 | 297 | |
<> | 144:ef7eb2e8f9f7 | 298 | if (enable) { |
<> | 144:ef7eb2e8f9f7 | 299 | if (irq == RxIrq) { |
<> | 144:ef7eb2e8f9f7 | 300 | UART_ITConfig(UART,UART_IT_FLAG_RXI,ENABLE); |
<> | 144:ef7eb2e8f9f7 | 301 | } else { // TxIrq |
<> | 144:ef7eb2e8f9f7 | 302 | UART_ITConfig(UART,UART_IT_FLAG_TXI,ENABLE); |
<> | 144:ef7eb2e8f9f7 | 303 | } |
<> | 144:ef7eb2e8f9f7 | 304 | |
<> | 144:ef7eb2e8f9f7 | 305 | NVIC_ClearPendingIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 306 | NVIC_EnableIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 307 | } else { // disable |
<> | 144:ef7eb2e8f9f7 | 308 | UART_ITConfig(UART,(UART_IT_FLAG_RXI|UART_IT_FLAG_TXI),DISABLE); |
<> | 144:ef7eb2e8f9f7 | 309 | NVIC_DisableIRQ(irq_n); |
<> | 144:ef7eb2e8f9f7 | 310 | } |
<> | 144:ef7eb2e8f9f7 | 311 | } |
<> | 144:ef7eb2e8f9f7 | 312 | } |
<> | 144:ef7eb2e8f9f7 | 313 | |
<> | 144:ef7eb2e8f9f7 | 314 | /****************************************************************************** |
<> | 144:ef7eb2e8f9f7 | 315 | * READ/WRITE |
<> | 144:ef7eb2e8f9f7 | 316 | ******************************************************************************/ |
<> | 144:ef7eb2e8f9f7 | 317 | |
<> | 144:ef7eb2e8f9f7 | 318 | int serial_getc(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 319 | { |
<> | 144:ef7eb2e8f9f7 | 320 | if (obj->uart == UART_2) |
<> | 144:ef7eb2e8f9f7 | 321 | { |
<> | 144:ef7eb2e8f9f7 | 322 | S_UART_TypeDef *uart = (S_UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 323 | |
<> | 144:ef7eb2e8f9f7 | 324 | while( (uart->STATE & S_UART_STATE_RX_BUF_FULL) == 0 ); |
<> | 144:ef7eb2e8f9f7 | 325 | return (uint16_t)(uart->DATA); |
<> | 144:ef7eb2e8f9f7 | 326 | } |
<> | 144:ef7eb2e8f9f7 | 327 | else |
<> | 144:ef7eb2e8f9f7 | 328 | { |
<> | 144:ef7eb2e8f9f7 | 329 | UART_TypeDef *uart = (UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 330 | while(uart->FR & UART_FR_RXFE); |
<> | 144:ef7eb2e8f9f7 | 331 | |
<> | 144:ef7eb2e8f9f7 | 332 | return (uart->DR & 0xFF); |
<> | 144:ef7eb2e8f9f7 | 333 | } |
<> | 144:ef7eb2e8f9f7 | 334 | } |
<> | 144:ef7eb2e8f9f7 | 335 | |
<> | 144:ef7eb2e8f9f7 | 336 | void serial_putc(serial_t *obj, int c) |
<> | 144:ef7eb2e8f9f7 | 337 | { |
<> | 144:ef7eb2e8f9f7 | 338 | if (obj->uart == UART_2) |
<> | 144:ef7eb2e8f9f7 | 339 | { |
<> | 144:ef7eb2e8f9f7 | 340 | S_UART_TypeDef *uart = (S_UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 341 | |
<> | 144:ef7eb2e8f9f7 | 342 | while(uart->STATE & S_UART_STATE_TX_BUF_FULL); |
<> | 144:ef7eb2e8f9f7 | 343 | uart->DATA = (uint32_t)(c & (uint16_t)0xFF); |
<> | 144:ef7eb2e8f9f7 | 344 | } |
<> | 144:ef7eb2e8f9f7 | 345 | else |
<> | 144:ef7eb2e8f9f7 | 346 | { |
<> | 144:ef7eb2e8f9f7 | 347 | UART_TypeDef *uart = (UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 348 | |
<> | 144:ef7eb2e8f9f7 | 349 | uart->DR = (uint32_t)(c & (uint16_t)0xFF); |
<> | 144:ef7eb2e8f9f7 | 350 | while(uart->FR & UART_FR_BUSY); |
<> | 144:ef7eb2e8f9f7 | 351 | } |
<> | 144:ef7eb2e8f9f7 | 352 | } |
<> | 144:ef7eb2e8f9f7 | 353 | |
<> | 144:ef7eb2e8f9f7 | 354 | int serial_readable(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 355 | { |
<> | 144:ef7eb2e8f9f7 | 356 | int status; |
<> | 144:ef7eb2e8f9f7 | 357 | |
<> | 144:ef7eb2e8f9f7 | 358 | if (obj->uart == UART_2) |
<> | 144:ef7eb2e8f9f7 | 359 | { |
<> | 144:ef7eb2e8f9f7 | 360 | S_UART_TypeDef *uart = (S_UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 361 | status = ((uart->STATE & S_UART_STATE_RX_BUF_FULL) ? 1 : 0); |
<> | 144:ef7eb2e8f9f7 | 362 | } |
<> | 144:ef7eb2e8f9f7 | 363 | else |
<> | 144:ef7eb2e8f9f7 | 364 | { |
<> | 144:ef7eb2e8f9f7 | 365 | UART_TypeDef *uart = (UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 366 | // Check if data is received |
<> | 144:ef7eb2e8f9f7 | 367 | status = ((uart->FR & UART_FR_RXFE) ? 0: 1); |
<> | 144:ef7eb2e8f9f7 | 368 | } |
<> | 144:ef7eb2e8f9f7 | 369 | |
<> | 144:ef7eb2e8f9f7 | 370 | return status; |
<> | 144:ef7eb2e8f9f7 | 371 | } |
<> | 144:ef7eb2e8f9f7 | 372 | |
<> | 144:ef7eb2e8f9f7 | 373 | int serial_writable(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 374 | { |
<> | 144:ef7eb2e8f9f7 | 375 | int status; |
<> | 144:ef7eb2e8f9f7 | 376 | |
<> | 144:ef7eb2e8f9f7 | 377 | if (obj->uart == UART_2) |
<> | 144:ef7eb2e8f9f7 | 378 | { |
<> | 144:ef7eb2e8f9f7 | 379 | S_UART_TypeDef *uart = (S_UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 380 | status = ((uart->STATE & S_UART_STATE_TX_BUF_FULL) ? 0 : 1); |
<> | 144:ef7eb2e8f9f7 | 381 | } |
<> | 144:ef7eb2e8f9f7 | 382 | else |
<> | 144:ef7eb2e8f9f7 | 383 | { |
<> | 144:ef7eb2e8f9f7 | 384 | UART_TypeDef *uart = (UART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 385 | // Check if data is transmitted |
<> | 144:ef7eb2e8f9f7 | 386 | status = ((uart->FR & UART_FR_BUSY) ? 0: 1); |
<> | 144:ef7eb2e8f9f7 | 387 | } |
<> | 144:ef7eb2e8f9f7 | 388 | return status; |
<> | 144:ef7eb2e8f9f7 | 389 | } |
<> | 144:ef7eb2e8f9f7 | 390 | |
<> | 144:ef7eb2e8f9f7 | 391 | void serial_clear(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 392 | { |
<> | 144:ef7eb2e8f9f7 | 393 | // UartHandle.Instance = (USART_TypeDef *)(obj->uart); |
<> | 144:ef7eb2e8f9f7 | 394 | // __HAL_UART_CLEAR_IT(&UartHandle, UART_FLAG_TC); |
<> | 144:ef7eb2e8f9f7 | 395 | // __HAL_UART_SEND_REQ(&UartHandle, UART_RXDATA_FLUSH_REQUEST); |
<> | 144:ef7eb2e8f9f7 | 396 | } |
<> | 144:ef7eb2e8f9f7 | 397 | |
<> | 144:ef7eb2e8f9f7 | 398 | void serial_pinout_tx(PinName tx) |
<> | 144:ef7eb2e8f9f7 | 399 | { |
<> | 144:ef7eb2e8f9f7 | 400 | //pinmap_pinout(tx, PinMap_UART_TX); |
<> | 144:ef7eb2e8f9f7 | 401 | } |
<> | 144:ef7eb2e8f9f7 | 402 | |
<> | 144:ef7eb2e8f9f7 | 403 | void serial_break_set(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 404 | { |
<> | 144:ef7eb2e8f9f7 | 405 | // [TODO] |
<> | 144:ef7eb2e8f9f7 | 406 | } |
<> | 144:ef7eb2e8f9f7 | 407 | |
<> | 144:ef7eb2e8f9f7 | 408 | void serial_break_clear(serial_t *obj) |
<> | 144:ef7eb2e8f9f7 | 409 | { |
<> | 144:ef7eb2e8f9f7 | 410 | // [TODO] |
<> | 144:ef7eb2e8f9f7 | 411 | } |
<> | 144:ef7eb2e8f9f7 | 412 | |
<> | 144:ef7eb2e8f9f7 | 413 | #endif |