test
targets/TARGET_STM/serial_api.c@2:4364577b5ad8, 2020-11-09 (annotated)
- Committer:
- elijahsj
- Date:
- Mon Nov 09 00:33:19 2020 -0500
- Revision:
- 2:4364577b5ad8
- Parent:
- 1:8a094db1347f
copied mbed library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elijahsj | 1:8a094db1347f | 1 | /* mbed Microcontroller Library |
elijahsj | 1:8a094db1347f | 2 | ******************************************************************************* |
elijahsj | 1:8a094db1347f | 3 | * Copyright (c) 2015, STMicroelectronics |
elijahsj | 1:8a094db1347f | 4 | * All rights reserved. |
elijahsj | 1:8a094db1347f | 5 | * |
elijahsj | 1:8a094db1347f | 6 | * Redistribution and use in source and binary forms, with or without |
elijahsj | 1:8a094db1347f | 7 | * modification, are permitted provided that the following conditions are met: |
elijahsj | 1:8a094db1347f | 8 | * |
elijahsj | 1:8a094db1347f | 9 | * 1. Redistributions of source code must retain the above copyright notice, |
elijahsj | 1:8a094db1347f | 10 | * this list of conditions and the following disclaimer. |
elijahsj | 1:8a094db1347f | 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
elijahsj | 1:8a094db1347f | 12 | * this list of conditions and the following disclaimer in the documentation |
elijahsj | 1:8a094db1347f | 13 | * and/or other materials provided with the distribution. |
elijahsj | 1:8a094db1347f | 14 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
elijahsj | 1:8a094db1347f | 15 | * may be used to endorse or promote products derived from this software |
elijahsj | 1:8a094db1347f | 16 | * without specific prior written permission. |
elijahsj | 1:8a094db1347f | 17 | * |
elijahsj | 1:8a094db1347f | 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
elijahsj | 1:8a094db1347f | 19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
elijahsj | 1:8a094db1347f | 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
elijahsj | 1:8a094db1347f | 21 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
elijahsj | 1:8a094db1347f | 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
elijahsj | 1:8a094db1347f | 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
elijahsj | 1:8a094db1347f | 24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
elijahsj | 1:8a094db1347f | 25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
elijahsj | 1:8a094db1347f | 26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
elijahsj | 1:8a094db1347f | 27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
elijahsj | 1:8a094db1347f | 28 | ******************************************************************************* |
elijahsj | 1:8a094db1347f | 29 | */ |
elijahsj | 1:8a094db1347f | 30 | #include "mbed_assert.h" |
elijahsj | 1:8a094db1347f | 31 | #include "mbed_error.h" |
elijahsj | 1:8a094db1347f | 32 | #include "serial_api.h" |
elijahsj | 1:8a094db1347f | 33 | #include "serial_api_hal.h" |
elijahsj | 1:8a094db1347f | 34 | #include "PeripheralPins.h" |
elijahsj | 1:8a094db1347f | 35 | |
elijahsj | 1:8a094db1347f | 36 | #if DEVICE_SERIAL |
elijahsj | 1:8a094db1347f | 37 | |
elijahsj | 1:8a094db1347f | 38 | void init_uart(serial_t *obj) |
elijahsj | 1:8a094db1347f | 39 | { |
elijahsj | 1:8a094db1347f | 40 | struct serial_s *obj_s = SERIAL_S(obj); |
elijahsj | 1:8a094db1347f | 41 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
elijahsj | 1:8a094db1347f | 42 | huart->Instance = (USART_TypeDef *)(obj_s->uart); |
elijahsj | 1:8a094db1347f | 43 | |
elijahsj | 1:8a094db1347f | 44 | huart->Init.BaudRate = obj_s->baudrate; |
elijahsj | 1:8a094db1347f | 45 | huart->Init.WordLength = obj_s->databits; |
elijahsj | 1:8a094db1347f | 46 | huart->Init.StopBits = obj_s->stopbits; |
elijahsj | 1:8a094db1347f | 47 | huart->Init.Parity = obj_s->parity; |
elijahsj | 1:8a094db1347f | 48 | #if DEVICE_SERIAL_FC |
elijahsj | 1:8a094db1347f | 49 | huart->Init.HwFlowCtl = obj_s->hw_flow_ctl; |
elijahsj | 1:8a094db1347f | 50 | #else |
elijahsj | 1:8a094db1347f | 51 | huart->Init.HwFlowCtl = UART_HWCONTROL_NONE; |
elijahsj | 1:8a094db1347f | 52 | #endif |
elijahsj | 1:8a094db1347f | 53 | huart->Init.OverSampling = UART_OVERSAMPLING_16; |
elijahsj | 1:8a094db1347f | 54 | huart->TxXferCount = 0; |
elijahsj | 1:8a094db1347f | 55 | huart->TxXferSize = 0; |
elijahsj | 1:8a094db1347f | 56 | huart->RxXferCount = 0; |
elijahsj | 1:8a094db1347f | 57 | huart->RxXferSize = 0; |
elijahsj | 1:8a094db1347f | 58 | |
elijahsj | 1:8a094db1347f | 59 | if (obj_s->pin_rx == NC) { |
elijahsj | 1:8a094db1347f | 60 | huart->Init.Mode = UART_MODE_TX; |
elijahsj | 1:8a094db1347f | 61 | } else if (obj_s->pin_tx == NC) { |
elijahsj | 1:8a094db1347f | 62 | huart->Init.Mode = UART_MODE_RX; |
elijahsj | 1:8a094db1347f | 63 | } else { |
elijahsj | 1:8a094db1347f | 64 | huart->Init.Mode = UART_MODE_TX_RX; |
elijahsj | 1:8a094db1347f | 65 | } |
elijahsj | 1:8a094db1347f | 66 | |
elijahsj | 1:8a094db1347f | 67 | if (HAL_UART_Init(huart) != HAL_OK) { |
elijahsj | 1:8a094db1347f | 68 | error("Cannot initialize UART\n"); |
elijahsj | 1:8a094db1347f | 69 | } |
elijahsj | 1:8a094db1347f | 70 | } |
elijahsj | 1:8a094db1347f | 71 | |
elijahsj | 1:8a094db1347f | 72 | void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits) |
elijahsj | 1:8a094db1347f | 73 | { |
elijahsj | 1:8a094db1347f | 74 | struct serial_s *obj_s = SERIAL_S(obj); |
elijahsj | 1:8a094db1347f | 75 | |
elijahsj | 1:8a094db1347f | 76 | switch (parity) { |
elijahsj | 1:8a094db1347f | 77 | case ParityOdd: |
elijahsj | 1:8a094db1347f | 78 | obj_s->parity = UART_PARITY_ODD; |
elijahsj | 1:8a094db1347f | 79 | break; |
elijahsj | 1:8a094db1347f | 80 | case ParityEven: |
elijahsj | 1:8a094db1347f | 81 | obj_s->parity = UART_PARITY_EVEN; |
elijahsj | 1:8a094db1347f | 82 | break; |
elijahsj | 1:8a094db1347f | 83 | default: // ParityNone |
elijahsj | 1:8a094db1347f | 84 | case ParityForced0: // unsupported! |
elijahsj | 1:8a094db1347f | 85 | case ParityForced1: // unsupported! |
elijahsj | 1:8a094db1347f | 86 | obj_s->parity = UART_PARITY_NONE; |
elijahsj | 1:8a094db1347f | 87 | break; |
elijahsj | 1:8a094db1347f | 88 | } |
elijahsj | 1:8a094db1347f | 89 | |
elijahsj | 1:8a094db1347f | 90 | switch (data_bits) { |
elijahsj | 1:8a094db1347f | 91 | case 9: |
elijahsj | 1:8a094db1347f | 92 | MBED_ASSERT(parity == UART_PARITY_NONE); |
elijahsj | 1:8a094db1347f | 93 | obj_s->databits = UART_WORDLENGTH_9B; |
elijahsj | 1:8a094db1347f | 94 | break; |
elijahsj | 1:8a094db1347f | 95 | default: |
elijahsj | 1:8a094db1347f | 96 | case 8: |
elijahsj | 1:8a094db1347f | 97 | if (parity != UART_PARITY_NONE) { |
elijahsj | 1:8a094db1347f | 98 | obj_s->databits = UART_WORDLENGTH_9B; |
elijahsj | 1:8a094db1347f | 99 | } else { |
elijahsj | 1:8a094db1347f | 100 | obj_s->databits = UART_WORDLENGTH_8B; |
elijahsj | 1:8a094db1347f | 101 | } |
elijahsj | 1:8a094db1347f | 102 | break; |
elijahsj | 1:8a094db1347f | 103 | #if defined UART_WORDLENGTH_7B |
elijahsj | 1:8a094db1347f | 104 | case 7: |
elijahsj | 1:8a094db1347f | 105 | if (parity != UART_PARITY_NONE) { |
elijahsj | 1:8a094db1347f | 106 | obj_s->databits = UART_WORDLENGTH_8B; |
elijahsj | 1:8a094db1347f | 107 | } else { |
elijahsj | 1:8a094db1347f | 108 | obj_s->databits = UART_WORDLENGTH_7B; |
elijahsj | 1:8a094db1347f | 109 | } |
elijahsj | 1:8a094db1347f | 110 | break; |
elijahsj | 1:8a094db1347f | 111 | #endif |
elijahsj | 1:8a094db1347f | 112 | } |
elijahsj | 1:8a094db1347f | 113 | |
elijahsj | 1:8a094db1347f | 114 | if (stop_bits == 2) { |
elijahsj | 1:8a094db1347f | 115 | obj_s->stopbits = UART_STOPBITS_2; |
elijahsj | 1:8a094db1347f | 116 | } else { |
elijahsj | 1:8a094db1347f | 117 | obj_s->stopbits = UART_STOPBITS_1; |
elijahsj | 1:8a094db1347f | 118 | } |
elijahsj | 1:8a094db1347f | 119 | |
elijahsj | 1:8a094db1347f | 120 | init_uart(obj); |
elijahsj | 1:8a094db1347f | 121 | } |
elijahsj | 1:8a094db1347f | 122 | |
elijahsj | 1:8a094db1347f | 123 | /****************************************************************************** |
elijahsj | 1:8a094db1347f | 124 | * READ/WRITE |
elijahsj | 1:8a094db1347f | 125 | ******************************************************************************/ |
elijahsj | 1:8a094db1347f | 126 | |
elijahsj | 1:8a094db1347f | 127 | int serial_readable(serial_t *obj) |
elijahsj | 1:8a094db1347f | 128 | { |
elijahsj | 1:8a094db1347f | 129 | struct serial_s *obj_s = SERIAL_S(obj); |
elijahsj | 1:8a094db1347f | 130 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
elijahsj | 1:8a094db1347f | 131 | /* To avoid a target blocking case, let's check for |
elijahsj | 1:8a094db1347f | 132 | * possible OVERRUN error and discard it |
elijahsj | 1:8a094db1347f | 133 | */ |
elijahsj | 1:8a094db1347f | 134 | if(__HAL_UART_GET_FLAG(huart, UART_FLAG_ORE)) { |
elijahsj | 1:8a094db1347f | 135 | __HAL_UART_CLEAR_OREFLAG(huart); |
elijahsj | 1:8a094db1347f | 136 | } |
elijahsj | 1:8a094db1347f | 137 | // Check if data is received |
elijahsj | 1:8a094db1347f | 138 | return (__HAL_UART_GET_FLAG(huart, UART_FLAG_RXNE) != RESET) ? 1 : 0; |
elijahsj | 1:8a094db1347f | 139 | } |
elijahsj | 1:8a094db1347f | 140 | |
elijahsj | 1:8a094db1347f | 141 | int serial_writable(serial_t *obj) |
elijahsj | 1:8a094db1347f | 142 | { |
elijahsj | 1:8a094db1347f | 143 | struct serial_s *obj_s = SERIAL_S(obj); |
elijahsj | 1:8a094db1347f | 144 | UART_HandleTypeDef *huart = &uart_handlers[obj_s->index]; |
elijahsj | 1:8a094db1347f | 145 | |
elijahsj | 1:8a094db1347f | 146 | // Check if data is transmitted |
elijahsj | 1:8a094db1347f | 147 | return (__HAL_UART_GET_FLAG(huart, UART_FLAG_TXE) != RESET) ? 1 : 0; |
elijahsj | 1:8a094db1347f | 148 | } |
elijahsj | 1:8a094db1347f | 149 | |
elijahsj | 1:8a094db1347f | 150 | void serial_pinout_tx(PinName tx) |
elijahsj | 1:8a094db1347f | 151 | { |
elijahsj | 1:8a094db1347f | 152 | pinmap_pinout(tx, PinMap_UART_TX); |
elijahsj | 1:8a094db1347f | 153 | } |
elijahsj | 1:8a094db1347f | 154 | |
elijahsj | 1:8a094db1347f | 155 | void serial_break_clear(serial_t *obj) |
elijahsj | 1:8a094db1347f | 156 | { |
elijahsj | 1:8a094db1347f | 157 | (void)obj; |
elijahsj | 1:8a094db1347f | 158 | } |
elijahsj | 1:8a094db1347f | 159 | |
elijahsj | 1:8a094db1347f | 160 | #endif /* DEVICE_SERIAL */ |