Rtos API example

Committer:
marcozecchini
Date:
Sat Feb 23 12:13:36 2019 +0000
Revision:
0:9fca2b23d0ba
final commit

Who changed what in which revision?

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