Update platform drivers

Committer:
EndaKilgarriff
Date:
Wed Jun 17 14:54:14 2020 +0000
Revision:
10:b5115cd6b916
Parent:
9:9e247b9c9abf
Roll back delay.cpp to previous version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
EndaKilgarriff 9:9e247b9c9abf 1 /***************************************************************************//**
EndaKilgarriff 9:9e247b9c9abf 2 * @file uart.cpp
EndaKilgarriff 9:9e247b9c9abf 3 * @brief Implementation of UART No-OS platform driver interfaces
EndaKilgarriff 9:9e247b9c9abf 4 ********************************************************************************
EndaKilgarriff 9:9e247b9c9abf 5 * Copyright (c) 2020 Analog Devices, Inc.
EndaKilgarriff 9:9e247b9c9abf 6 *
EndaKilgarriff 9:9e247b9c9abf 7 * All rights reserved.
EndaKilgarriff 9:9e247b9c9abf 8 *
EndaKilgarriff 9:9e247b9c9abf 9 * This software is proprietary to Analog Devices, Inc. and its licensors.
EndaKilgarriff 9:9e247b9c9abf 10 * By using this software you agree to the terms of the associated
EndaKilgarriff 9:9e247b9c9abf 11 * Analog Devices Software License Agreement.
EndaKilgarriff 9:9e247b9c9abf 12 *******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 13
EndaKilgarriff 9:9e247b9c9abf 14 /******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 15 /***************************** Include Files **********************************/
EndaKilgarriff 9:9e247b9c9abf 16 /******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 17 #include <stdio.h>
EndaKilgarriff 9:9e247b9c9abf 18 #include <mbed.h>
EndaKilgarriff 9:9e247b9c9abf 19
EndaKilgarriff 9:9e247b9c9abf 20 #include "platform_drivers.h"
EndaKilgarriff 9:9e247b9c9abf 21 #include "uart_extra.h"
EndaKilgarriff 9:9e247b9c9abf 22
EndaKilgarriff 9:9e247b9c9abf 23 /******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 24 /************************ Functions Definitions *******************************/
EndaKilgarriff 9:9e247b9c9abf 25 /******************************************************************************/
EndaKilgarriff 9:9e247b9c9abf 26
EndaKilgarriff 9:9e247b9c9abf 27 /**
EndaKilgarriff 9:9e247b9c9abf 28 * @brief Read data from UART device.
EndaKilgarriff 9:9e247b9c9abf 29 * @param desc - Instance of UART.
EndaKilgarriff 9:9e247b9c9abf 30 * @param data - Pointer to buffer containing data.
EndaKilgarriff 9:9e247b9c9abf 31 * @param bytes_number - Number of bytes to read.
EndaKilgarriff 9:9e247b9c9abf 32 * @return SUCCESS in case of success, FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 33 */
EndaKilgarriff 9:9e247b9c9abf 34 int32_t uart_read(struct uart_desc *desc, uint8_t *data, uint32_t bytes_number)
EndaKilgarriff 9:9e247b9c9abf 35 {
EndaKilgarriff 9:9e247b9c9abf 36 mbed::Serial *uart; // pointer to Serial/UART instance
EndaKilgarriff 9:9e247b9c9abf 37
EndaKilgarriff 9:9e247b9c9abf 38 if (desc) {
EndaKilgarriff 9:9e247b9c9abf 39 if (((mbed_uart_desc *)(desc->extra))->uart_port) {
EndaKilgarriff 9:9e247b9c9abf 40 uart = (Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port);
EndaKilgarriff 9:9e247b9c9abf 41
EndaKilgarriff 9:9e247b9c9abf 42 if (data) {
EndaKilgarriff 9:9e247b9c9abf 43 for (size_t i = 0; i < bytes_number; i++) {
EndaKilgarriff 9:9e247b9c9abf 44 data[i] = uart->getc();
EndaKilgarriff 9:9e247b9c9abf 45 }
EndaKilgarriff 9:9e247b9c9abf 46
EndaKilgarriff 9:9e247b9c9abf 47 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 48 }
EndaKilgarriff 9:9e247b9c9abf 49 }
EndaKilgarriff 9:9e247b9c9abf 50 }
EndaKilgarriff 9:9e247b9c9abf 51
EndaKilgarriff 9:9e247b9c9abf 52 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 53 }
EndaKilgarriff 9:9e247b9c9abf 54
EndaKilgarriff 9:9e247b9c9abf 55
EndaKilgarriff 9:9e247b9c9abf 56 /**
EndaKilgarriff 9:9e247b9c9abf 57 * @brief Write data to UART device.
EndaKilgarriff 9:9e247b9c9abf 58 * @param desc - Instance of UART.
EndaKilgarriff 9:9e247b9c9abf 59 * @param data - Pointer to buffer containing data.
EndaKilgarriff 9:9e247b9c9abf 60 * @param bytes_number - Number of bytes to read.
EndaKilgarriff 9:9e247b9c9abf 61 * @return SUCCESS in case of success, FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 62 */
EndaKilgarriff 9:9e247b9c9abf 63 int32_t uart_write(struct uart_desc *desc, const uint8_t *data,
EndaKilgarriff 9:9e247b9c9abf 64 uint32_t bytes_number)
EndaKilgarriff 9:9e247b9c9abf 65 {
EndaKilgarriff 9:9e247b9c9abf 66 mbed::Serial *uart; // pointer to Serial/UART instance
EndaKilgarriff 9:9e247b9c9abf 67
EndaKilgarriff 9:9e247b9c9abf 68 if (desc) {
EndaKilgarriff 9:9e247b9c9abf 69 if (((mbed_uart_desc *)(desc->extra))->uart_port) {
EndaKilgarriff 9:9e247b9c9abf 70 uart = (Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port);
EndaKilgarriff 9:9e247b9c9abf 71
EndaKilgarriff 9:9e247b9c9abf 72 if (data) {
EndaKilgarriff 9:9e247b9c9abf 73 for (size_t i = 0; i < bytes_number; i++) {
EndaKilgarriff 9:9e247b9c9abf 74 uart->putc(data[i]);
EndaKilgarriff 9:9e247b9c9abf 75 }
EndaKilgarriff 9:9e247b9c9abf 76
EndaKilgarriff 9:9e247b9c9abf 77 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 78 }
EndaKilgarriff 9:9e247b9c9abf 79 }
EndaKilgarriff 9:9e247b9c9abf 80 }
EndaKilgarriff 9:9e247b9c9abf 81
EndaKilgarriff 9:9e247b9c9abf 82 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 83 }
EndaKilgarriff 9:9e247b9c9abf 84
EndaKilgarriff 9:9e247b9c9abf 85
EndaKilgarriff 9:9e247b9c9abf 86 /**
EndaKilgarriff 9:9e247b9c9abf 87 * @brief Submit reading buffer to the UART driver.
EndaKilgarriff 9:9e247b9c9abf 88 *
EndaKilgarriff 9:9e247b9c9abf 89 * Buffer is used until bytes_number bytes are read.
EndaKilgarriff 9:9e247b9c9abf 90 * @param desc: Descriptor of the UART device
EndaKilgarriff 9:9e247b9c9abf 91 * @param data: Buffer where data will be read
EndaKilgarriff 9:9e247b9c9abf 92 * @param bytes_number: Number of bytes to be read.
EndaKilgarriff 9:9e247b9c9abf 93 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 94 */
EndaKilgarriff 9:9e247b9c9abf 95 int32_t uart_read_nonblocking(struct uart_desc *desc,
EndaKilgarriff 9:9e247b9c9abf 96 uint8_t *data,
EndaKilgarriff 9:9e247b9c9abf 97 uint32_t bytes_number)
EndaKilgarriff 9:9e247b9c9abf 98 {
EndaKilgarriff 9:9e247b9c9abf 99 mbed::Serial *uart; // pointer to Serial/UART instance
EndaKilgarriff 9:9e247b9c9abf 100
EndaKilgarriff 9:9e247b9c9abf 101 if (desc) {
EndaKilgarriff 9:9e247b9c9abf 102 if (((mbed_uart_desc *)(desc->extra))->uart_port) {
EndaKilgarriff 9:9e247b9c9abf 103 uart = (Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port);
EndaKilgarriff 9:9e247b9c9abf 104
EndaKilgarriff 9:9e247b9c9abf 105 if (data) {
EndaKilgarriff 9:9e247b9c9abf 106 for (size_t i = 0; i < bytes_number; i++) {
EndaKilgarriff 9:9e247b9c9abf 107 if (uart->readable() > 0) {
EndaKilgarriff 9:9e247b9c9abf 108 data[i] = uart->getc();
EndaKilgarriff 9:9e247b9c9abf 109 }
EndaKilgarriff 9:9e247b9c9abf 110 }
EndaKilgarriff 9:9e247b9c9abf 111
EndaKilgarriff 9:9e247b9c9abf 112 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 113 }
EndaKilgarriff 9:9e247b9c9abf 114 }
EndaKilgarriff 9:9e247b9c9abf 115 }
EndaKilgarriff 9:9e247b9c9abf 116
EndaKilgarriff 9:9e247b9c9abf 117 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 118 }
EndaKilgarriff 9:9e247b9c9abf 119
EndaKilgarriff 9:9e247b9c9abf 120
EndaKilgarriff 9:9e247b9c9abf 121 /**
EndaKilgarriff 9:9e247b9c9abf 122 * @brief Submit writting buffer to the UART driver.
EndaKilgarriff 9:9e247b9c9abf 123 *
EndaKilgarriff 9:9e247b9c9abf 124 * Data from the buffer is sent over the UART, the function returns imediatly.
EndaKilgarriff 9:9e247b9c9abf 125 * @param desc: Descriptor of the UART device
EndaKilgarriff 9:9e247b9c9abf 126 * @param data: Buffer where data will be written
EndaKilgarriff 9:9e247b9c9abf 127 * @param bytes_number: Number of bytes to be written.
EndaKilgarriff 9:9e247b9c9abf 128 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 129 */
EndaKilgarriff 9:9e247b9c9abf 130 int32_t uart_write_nonblocking(struct uart_desc *desc,
EndaKilgarriff 9:9e247b9c9abf 131 const uint8_t *data,
EndaKilgarriff 9:9e247b9c9abf 132 uint32_t bytes_number)
EndaKilgarriff 9:9e247b9c9abf 133 {
EndaKilgarriff 9:9e247b9c9abf 134 mbed::Serial *uart; // pointer to Serial/UART instance
EndaKilgarriff 9:9e247b9c9abf 135
EndaKilgarriff 9:9e247b9c9abf 136 if (desc) {
EndaKilgarriff 9:9e247b9c9abf 137 if (((mbed_uart_desc *)(desc->extra))->uart_port) {
EndaKilgarriff 9:9e247b9c9abf 138 uart = (Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port);
EndaKilgarriff 9:9e247b9c9abf 139
EndaKilgarriff 9:9e247b9c9abf 140 if (data) {
EndaKilgarriff 9:9e247b9c9abf 141 for (size_t i = 0; i < bytes_number; i++) {
EndaKilgarriff 9:9e247b9c9abf 142 if (uart->writable() > 0) {
EndaKilgarriff 9:9e247b9c9abf 143 uart->putc(data[i]);
EndaKilgarriff 9:9e247b9c9abf 144 }
EndaKilgarriff 9:9e247b9c9abf 145 }
EndaKilgarriff 9:9e247b9c9abf 146
EndaKilgarriff 9:9e247b9c9abf 147 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 148 }
EndaKilgarriff 9:9e247b9c9abf 149 }
EndaKilgarriff 9:9e247b9c9abf 150 }
EndaKilgarriff 9:9e247b9c9abf 151
EndaKilgarriff 9:9e247b9c9abf 152 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 153 }
EndaKilgarriff 9:9e247b9c9abf 154
EndaKilgarriff 9:9e247b9c9abf 155
EndaKilgarriff 9:9e247b9c9abf 156 /**
EndaKilgarriff 9:9e247b9c9abf 157 * @brief Initialize the UART communication peripheral.
EndaKilgarriff 9:9e247b9c9abf 158 * @param desc - The UART descriptor.
EndaKilgarriff 9:9e247b9c9abf 159 * @param param - The structure that contains the UART parameters.
EndaKilgarriff 9:9e247b9c9abf 160 * @return SUCCESS in case of success, FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 161 */
EndaKilgarriff 9:9e247b9c9abf 162 int32_t uart_init(struct uart_desc **desc, struct uart_init_param *param)
EndaKilgarriff 9:9e247b9c9abf 163 {
EndaKilgarriff 9:9e247b9c9abf 164 mbed::Serial *uart; // pointer to new Serial/UART instance
EndaKilgarriff 9:9e247b9c9abf 165 mbed_uart_desc *mbed_desc; // Pointer to mbed uart descriptor
EndaKilgarriff 9:9e247b9c9abf 166
EndaKilgarriff 9:9e247b9c9abf 167 if (desc && param) {
EndaKilgarriff 9:9e247b9c9abf 168 // Create the UART description object for the device
EndaKilgarriff 9:9e247b9c9abf 169 uart_desc *new_desc = (uart_desc *)malloc(sizeof(uart_desc));
EndaKilgarriff 9:9e247b9c9abf 170 if (new_desc == NULL) {
EndaKilgarriff 9:9e247b9c9abf 171 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 172 }
EndaKilgarriff 9:9e247b9c9abf 173
EndaKilgarriff 9:9e247b9c9abf 174 new_desc->baud_rate = param->baud_rate;
EndaKilgarriff 9:9e247b9c9abf 175
EndaKilgarriff 9:9e247b9c9abf 176 // Create and configure a new instance of Serial/UART port
EndaKilgarriff 9:9e247b9c9abf 177 uart = new Serial(
EndaKilgarriff 9:9e247b9c9abf 178 (PinName)(((mbed_uart_init_param *)param->extra)->uart_tx_pin),
EndaKilgarriff 9:9e247b9c9abf 179 (PinName)(((mbed_uart_init_param *)param->extra)->uart_rx_pin),
EndaKilgarriff 9:9e247b9c9abf 180 (int)param->baud_rate);
EndaKilgarriff 9:9e247b9c9abf 181
EndaKilgarriff 9:9e247b9c9abf 182 if (uart == NULL) {
EndaKilgarriff 9:9e247b9c9abf 183 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 184 }
EndaKilgarriff 9:9e247b9c9abf 185
EndaKilgarriff 9:9e247b9c9abf 186 // Create a new mbed descriptor to store new UART instances
EndaKilgarriff 9:9e247b9c9abf 187 mbed_desc = (mbed_uart_desc *)malloc(sizeof(mbed_uart_desc));
EndaKilgarriff 9:9e247b9c9abf 188 if (mbed_desc == NULL) {
EndaKilgarriff 9:9e247b9c9abf 189 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 190 }
EndaKilgarriff 9:9e247b9c9abf 191
EndaKilgarriff 9:9e247b9c9abf 192 mbed_desc->uart_port = (Serial *)uart;
EndaKilgarriff 9:9e247b9c9abf 193 new_desc->extra = (mbed_uart_desc *)mbed_desc;
EndaKilgarriff 9:9e247b9c9abf 194
EndaKilgarriff 9:9e247b9c9abf 195 *desc = new_desc;
EndaKilgarriff 9:9e247b9c9abf 196
EndaKilgarriff 9:9e247b9c9abf 197 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 198 }
EndaKilgarriff 9:9e247b9c9abf 199
EndaKilgarriff 9:9e247b9c9abf 200 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 201 }
EndaKilgarriff 9:9e247b9c9abf 202
EndaKilgarriff 9:9e247b9c9abf 203
EndaKilgarriff 9:9e247b9c9abf 204 /**
EndaKilgarriff 9:9e247b9c9abf 205 * @brief Free the resources allocated by uart_init().
EndaKilgarriff 9:9e247b9c9abf 206 * @param desc - The UART descriptor.
EndaKilgarriff 9:9e247b9c9abf 207 * @return SUCCESS in case of success, FAILURE otherwise.
EndaKilgarriff 9:9e247b9c9abf 208 */
EndaKilgarriff 9:9e247b9c9abf 209 int32_t uart_remove(struct uart_desc *desc)
EndaKilgarriff 9:9e247b9c9abf 210 {
EndaKilgarriff 9:9e247b9c9abf 211 if (desc) {
EndaKilgarriff 9:9e247b9c9abf 212 // Free the UART port object
EndaKilgarriff 9:9e247b9c9abf 213 if ((Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port)) {
EndaKilgarriff 9:9e247b9c9abf 214 delete((Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port));
EndaKilgarriff 9:9e247b9c9abf 215 }
EndaKilgarriff 9:9e247b9c9abf 216
EndaKilgarriff 9:9e247b9c9abf 217 // Free the UART extra descriptor object
EndaKilgarriff 9:9e247b9c9abf 218 if ((mbed_uart_desc *)(desc->extra)) {
EndaKilgarriff 9:9e247b9c9abf 219 free((mbed_uart_desc *)(desc->extra));
EndaKilgarriff 9:9e247b9c9abf 220 }
EndaKilgarriff 9:9e247b9c9abf 221
EndaKilgarriff 9:9e247b9c9abf 222 // Free the UART descriptor object
EndaKilgarriff 9:9e247b9c9abf 223 free(desc);
EndaKilgarriff 9:9e247b9c9abf 224
EndaKilgarriff 9:9e247b9c9abf 225 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 226 }
EndaKilgarriff 9:9e247b9c9abf 227
EndaKilgarriff 9:9e247b9c9abf 228 return FAILURE;
EndaKilgarriff 9:9e247b9c9abf 229 }
EndaKilgarriff 9:9e247b9c9abf 230
EndaKilgarriff 9:9e247b9c9abf 231
EndaKilgarriff 9:9e247b9c9abf 232 /**
EndaKilgarriff 9:9e247b9c9abf 233 * @brief Get number of UART errors.
EndaKilgarriff 9:9e247b9c9abf 234 * @param desc - The UART descriptor.
EndaKilgarriff 9:9e247b9c9abf 235 * @return number of errors.
EndaKilgarriff 9:9e247b9c9abf 236 */
EndaKilgarriff 9:9e247b9c9abf 237 uint32_t uart_get_errors(struct uart_desc *desc)
EndaKilgarriff 9:9e247b9c9abf 238 {
EndaKilgarriff 9:9e247b9c9abf 239 if (desc) {
EndaKilgarriff 9:9e247b9c9abf 240 // Unused variable - fix compiler warning
EndaKilgarriff 9:9e247b9c9abf 241 }
EndaKilgarriff 9:9e247b9c9abf 242
EndaKilgarriff 9:9e247b9c9abf 243 return SUCCESS;
EndaKilgarriff 9:9e247b9c9abf 244 }