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.
uart.cpp
00001 /***************************************************************************//** 00002 * @file uart.cpp 00003 * @brief Implementation of UART No-OS platform driver interfaces 00004 ******************************************************************************** 00005 * Copyright (c) 2020 Analog Devices, Inc. 00006 * 00007 * All rights reserved. 00008 * 00009 * This software is proprietary to Analog Devices, Inc. and its licensors. 00010 * By using this software you agree to the terms of the associated 00011 * Analog Devices Software License Agreement. 00012 *******************************************************************************/ 00013 00014 /******************************************************************************/ 00015 /***************************** Include Files **********************************/ 00016 /******************************************************************************/ 00017 #include <stdio.h> 00018 #include <mbed.h> 00019 00020 #include "platform_drivers.h" 00021 #include "uart_extra.h" 00022 00023 /******************************************************************************/ 00024 /************************ Functions Definitions *******************************/ 00025 /******************************************************************************/ 00026 00027 /** 00028 * @brief Read data from UART device. 00029 * @param desc - Instance of UART. 00030 * @param data - Pointer to buffer containing data. 00031 * @param bytes_number - Number of bytes to read. 00032 * @return SUCCESS in case of success, FAILURE otherwise. 00033 */ 00034 int32_t uart_read(struct uart_desc *desc, uint8_t *data, uint32_t bytes_number) 00035 { 00036 mbed::Serial *uart; // pointer to Serial/UART instance 00037 00038 if (desc) { 00039 if (((mbed_uart_desc *)(desc->extra))->uart_port) { 00040 uart = (Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port); 00041 00042 if (data) { 00043 for (size_t i = 0; i < bytes_number; i++) { 00044 data[i] = uart->getc(); 00045 } 00046 00047 return SUCCESS; 00048 } 00049 } 00050 } 00051 00052 return FAILURE; 00053 } 00054 00055 00056 /** 00057 * @brief Write data to UART device. 00058 * @param desc - Instance of UART. 00059 * @param data - Pointer to buffer containing data. 00060 * @param bytes_number - Number of bytes to read. 00061 * @return SUCCESS in case of success, FAILURE otherwise. 00062 */ 00063 int32_t uart_write(struct uart_desc *desc, const uint8_t *data, 00064 uint32_t bytes_number) 00065 { 00066 mbed::Serial *uart; // pointer to Serial/UART instance 00067 00068 if (desc) { 00069 if (((mbed_uart_desc *)(desc->extra))->uart_port) { 00070 uart = (Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port); 00071 00072 if (data) { 00073 for (size_t i = 0; i < bytes_number; i++) { 00074 uart->putc(data[i]); 00075 } 00076 00077 return SUCCESS; 00078 } 00079 } 00080 } 00081 00082 return FAILURE; 00083 } 00084 00085 00086 /** 00087 * @brief Submit reading buffer to the UART driver. 00088 * 00089 * Buffer is used until bytes_number bytes are read. 00090 * @param desc: Descriptor of the UART device 00091 * @param data: Buffer where data will be read 00092 * @param bytes_number: Number of bytes to be read. 00093 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. 00094 */ 00095 int32_t uart_read_nonblocking(struct uart_desc *desc, 00096 uint8_t *data, 00097 uint32_t bytes_number) 00098 { 00099 mbed::Serial *uart; // pointer to Serial/UART instance 00100 00101 if (desc) { 00102 if (((mbed_uart_desc *)(desc->extra))->uart_port) { 00103 uart = (Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port); 00104 00105 if (data) { 00106 for (size_t i = 0; i < bytes_number; i++) { 00107 if (uart->readable() > 0) { 00108 data[i] = uart->getc(); 00109 } 00110 } 00111 00112 return SUCCESS; 00113 } 00114 } 00115 } 00116 00117 return FAILURE; 00118 } 00119 00120 00121 /** 00122 * @brief Submit writting buffer to the UART driver. 00123 * 00124 * Data from the buffer is sent over the UART, the function returns imediatly. 00125 * @param desc: Descriptor of the UART device 00126 * @param data: Buffer where data will be written 00127 * @param bytes_number: Number of bytes to be written. 00128 * @return \ref SUCCESS in case of success, \ref FAILURE otherwise. 00129 */ 00130 int32_t uart_write_nonblocking(struct uart_desc *desc, 00131 const uint8_t *data, 00132 uint32_t bytes_number) 00133 { 00134 mbed::Serial *uart; // pointer to Serial/UART instance 00135 00136 if (desc) { 00137 if (((mbed_uart_desc *)(desc->extra))->uart_port) { 00138 uart = (Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port); 00139 00140 if (data) { 00141 for (size_t i = 0; i < bytes_number; i++) { 00142 if (uart->writable() > 0) { 00143 uart->putc(data[i]); 00144 } 00145 } 00146 00147 return SUCCESS; 00148 } 00149 } 00150 } 00151 00152 return FAILURE; 00153 } 00154 00155 00156 /** 00157 * @brief Initialize the UART communication peripheral. 00158 * @param desc - The UART descriptor. 00159 * @param param - The structure that contains the UART parameters. 00160 * @return SUCCESS in case of success, FAILURE otherwise. 00161 */ 00162 int32_t uart_init(struct uart_desc **desc, struct uart_init_param *param) 00163 { 00164 mbed::Serial *uart; // pointer to new Serial/UART instance 00165 mbed_uart_desc *mbed_desc; // Pointer to mbed uart descriptor 00166 00167 if (desc && param) { 00168 // Create the UART description object for the device 00169 uart_desc *new_desc = (uart_desc *)malloc(sizeof(uart_desc)); 00170 if (new_desc == NULL) { 00171 return FAILURE; 00172 } 00173 00174 new_desc->baud_rate = param->baud_rate; 00175 00176 // Create and configure a new instance of Serial/UART port 00177 uart = new Serial( 00178 (PinName)(((mbed_uart_init_param *)param->extra)->uart_tx_pin), 00179 (PinName)(((mbed_uart_init_param *)param->extra)->uart_rx_pin), 00180 (int)param->baud_rate); 00181 00182 if (uart == NULL) { 00183 return FAILURE; 00184 } 00185 00186 // Create a new mbed descriptor to store new UART instances 00187 mbed_desc = (mbed_uart_desc *)malloc(sizeof(mbed_uart_desc)); 00188 if (mbed_desc == NULL) { 00189 return FAILURE; 00190 } 00191 00192 mbed_desc->uart_port = (Serial *)uart; 00193 new_desc->extra = (mbed_uart_desc *)mbed_desc; 00194 00195 *desc = new_desc; 00196 00197 return SUCCESS; 00198 } 00199 00200 return FAILURE; 00201 } 00202 00203 00204 /** 00205 * @brief Free the resources allocated by uart_init(). 00206 * @param desc - The UART descriptor. 00207 * @return SUCCESS in case of success, FAILURE otherwise. 00208 */ 00209 int32_t uart_remove(struct uart_desc *desc) 00210 { 00211 if (desc) { 00212 // Free the UART port object 00213 if ((Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port)) { 00214 delete((Serial *)(((mbed_uart_desc *)(desc->extra))->uart_port)); 00215 } 00216 00217 // Free the UART extra descriptor object 00218 if ((mbed_uart_desc *)(desc->extra)) { 00219 free((mbed_uart_desc *)(desc->extra)); 00220 } 00221 00222 // Free the UART descriptor object 00223 free(desc); 00224 00225 return SUCCESS; 00226 } 00227 00228 return FAILURE; 00229 } 00230 00231 00232 /** 00233 * @brief Get number of UART errors. 00234 * @param desc - The UART descriptor. 00235 * @return number of errors. 00236 */ 00237 uint32_t uart_get_errors(struct uart_desc *desc) 00238 { 00239 if (desc) { 00240 // Unused variable - fix compiler warning 00241 } 00242 00243 return SUCCESS; 00244 }
Generated on Tue Jul 12 2022 17:15:46 by
