ChibiOS/HAL  6.1.0
hal_uart.c
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_uart.c
19  * @brief UART Driver code.
20  *
21  * @addtogroup UART
22  * @{
23  */
24 
25 #include "hal.h"
26 
27 #if (HAL_USE_UART == TRUE) || defined(__DOXYGEN__)
28 
29 /*===========================================================================*/
30 /* Driver local definitions. */
31 /*===========================================================================*/
32 
33 /*===========================================================================*/
34 /* Driver exported variables. */
35 /*===========================================================================*/
36 
37 /*===========================================================================*/
38 /* Driver local variables and types. */
39 /*===========================================================================*/
40 
41 /*===========================================================================*/
42 /* Driver local functions. */
43 /*===========================================================================*/
44 
45 /*===========================================================================*/
46 /* Driver exported functions. */
47 /*===========================================================================*/
48 
49 /**
50  * @brief UART Driver initialization.
51  * @note This function is implicitly invoked by @p halInit(), there is
52  * no need to explicitly initialize the driver.
53  *
54  * @init
55  */
56 void uartInit(void) {
57 
58  uart_lld_init();
59 }
60 
61 /**
62  * @brief Initializes the standard part of a @p UARTDriver structure.
63  *
64  * @param[out] uartp pointer to the @p UARTDriver object
65  *
66  * @init
67  */
68 void uartObjectInit(UARTDriver *uartp) {
69 
70  uartp->state = UART_STOP;
71  uartp->txstate = UART_TX_IDLE;
72  uartp->rxstate = UART_RX_IDLE;
73  uartp->config = NULL;
74 #if UART_USE_WAIT == TRUE
75  uartp->early = false;
76  uartp->threadrx = NULL;
77  uartp->threadtx = NULL;
78 #endif /* UART_USE_WAIT */
79 #if UART_USE_MUTUAL_EXCLUSION == TRUE
80  osalMutexObjectInit(&uartp->mutex);
81 #endif /* UART_USE_MUTUAL_EXCLUSION */
82 
83  /* Optional, user-defined initializer.*/
84 #if defined(UART_DRIVER_EXT_INIT_HOOK)
85  UART_DRIVER_EXT_INIT_HOOK(uartp);
86 #endif
87 }
88 
89 /**
90  * @brief Configures and activates the UART peripheral.
91  *
92  * @param[in] uartp pointer to the @p UARTDriver object
93  * @param[in] config pointer to the @p UARTConfig object
94  *
95  * @api
96  */
97 void uartStart(UARTDriver *uartp, const UARTConfig *config) {
98 
99  osalDbgCheck((uartp != NULL) && (config != NULL));
100 
101  osalSysLock();
102  osalDbgAssert((uartp->state == UART_STOP) || (uartp->state == UART_READY),
103  "invalid state");
104 
105  uartp->config = config;
106  uart_lld_start(uartp);
107  uartp->state = UART_READY;
108  osalSysUnlock();
109 }
110 
111 /**
112  * @brief Deactivates the UART peripheral.
113  *
114  * @param[in] uartp pointer to the @p UARTDriver object
115  *
116  * @api
117  */
118 void uartStop(UARTDriver *uartp) {
119 
120  osalDbgCheck(uartp != NULL);
121 
122  osalSysLock();
123 
124  osalDbgAssert((uartp->state == UART_STOP) || (uartp->state == UART_READY),
125  "invalid state");
126 
127  uart_lld_stop(uartp);
128  uartp->config = NULL;
129  uartp->state = UART_STOP;
130  uartp->txstate = UART_TX_IDLE;
131  uartp->rxstate = UART_RX_IDLE;
132 
133  osalSysUnlock();
134 }
135 
136 /**
137  * @brief Starts a transmission on the UART peripheral.
138  * @note The buffers are organized as uint8_t arrays for data sizes below
139  * or equal to 8 bits else it is organized as uint16_t arrays.
140  *
141  * @param[in] uartp pointer to the @p UARTDriver object
142  * @param[in] n number of data frames to send
143  * @param[in] txbuf the pointer to the transmit buffer
144  *
145  * @api
146  */
147 void uartStartSend(UARTDriver *uartp, size_t n, const void *txbuf) {
148 
149  osalDbgCheck((uartp != NULL) && (n > 0U) && (txbuf != NULL));
150 
151  osalSysLock();
152  osalDbgAssert(uartp->state == UART_READY, "is active");
153  osalDbgAssert(uartp->txstate != UART_TX_ACTIVE, "tx active");
154 
155  uart_lld_start_send(uartp, n, txbuf);
156  uartp->txstate = UART_TX_ACTIVE;
157  osalSysUnlock();
158 }
159 
160 /**
161  * @brief Starts a transmission on the UART peripheral.
162  * @note The buffers are organized as uint8_t arrays for data sizes below
163  * or equal to 8 bits else it is organized as uint16_t arrays.
164  * @note This function has to be invoked from a lock zone.
165  *
166  * @param[in] uartp pointer to the @p UARTDriver object
167  * @param[in] n number of data frames to send
168  * @param[in] txbuf the pointer to the transmit buffer
169  *
170  * @iclass
171  */
172 void uartStartSendI(UARTDriver *uartp, size_t n, const void *txbuf) {
173 
175  osalDbgCheck((uartp != NULL) && (n > 0U) && (txbuf != NULL));
176  osalDbgAssert(uartp->state == UART_READY, "is active");
177  osalDbgAssert(uartp->txstate != UART_TX_ACTIVE, "tx active");
178 
179  uart_lld_start_send(uartp, n, txbuf);
180  uartp->txstate = UART_TX_ACTIVE;
181 }
182 
183 /**
184  * @brief Stops any ongoing transmission.
185  * @note Stopping a transmission also suppresses the transmission callbacks.
186  *
187  * @param[in] uartp pointer to the @p UARTDriver object
188  *
189  * @return The number of data frames not transmitted by the
190  * stopped transmit operation.
191  * @retval UART_ERR_NOT_ACTIVE if there was no transmit operation in progress.
192  *
193  * @api
194  */
195 size_t uartStopSend(UARTDriver *uartp) {
196  size_t n;
197 
198  osalDbgCheck(uartp != NULL);
199 
200  osalSysLock();
201  osalDbgAssert(uartp->state == UART_READY, "not active");
202 
203  if (uartp->txstate == UART_TX_ACTIVE) {
204  n = uart_lld_stop_send(uartp);
205  uartp->txstate = UART_TX_IDLE;
206  }
207  else {
208  n = UART_ERR_NOT_ACTIVE;
209  }
210  osalSysUnlock();
211 
212  return n;
213 }
214 
215 /**
216  * @brief Stops any ongoing transmission.
217  * @note Stopping a transmission also suppresses the transmission callbacks.
218  * @note This function has to be invoked from a lock zone.
219  *
220  * @param[in] uartp pointer to the @p UARTDriver object
221  *
222  * @return The number of data frames not transmitted by the
223  * stopped transmit operation.
224  * @retval UART_ERR_NOT_ACTIVE if there was no transmit operation in progress.
225  *
226  * @iclass
227  */
228 size_t uartStopSendI(UARTDriver *uartp) {
229 
231  osalDbgCheck(uartp != NULL);
232  osalDbgAssert(uartp->state == UART_READY, "not active");
233 
234  if (uartp->txstate == UART_TX_ACTIVE) {
235  size_t n = uart_lld_stop_send(uartp);
236  uartp->txstate = UART_TX_IDLE;
237  return n;
238  }
239  return UART_ERR_NOT_ACTIVE;
240 }
241 
242 /**
243  * @brief Starts a receive operation on the UART peripheral.
244  * @note The buffers are organized as uint8_t arrays for data sizes below
245  * or equal to 8 bits else it is organized as uint16_t arrays.
246  *
247  * @param[in] uartp pointer to the @p UARTDriver object
248  * @param[in] n number of data frames to receive
249  * @param[in] rxbuf the pointer to the receive buffer
250  *
251  * @api
252  */
253 void uartStartReceive(UARTDriver *uartp, size_t n, void *rxbuf) {
254 
255  osalDbgCheck((uartp != NULL) && (n > 0U) && (rxbuf != NULL));
256 
257  osalSysLock();
258  osalDbgAssert(uartp->state == UART_READY, "is active");
259  osalDbgAssert(uartp->rxstate != UART_RX_ACTIVE, "rx active");
260 
261  uart_lld_start_receive(uartp, n, rxbuf);
262  uartp->rxstate = UART_RX_ACTIVE;
263  osalSysUnlock();
264 }
265 
266 /**
267  * @brief Starts a receive operation on the UART peripheral.
268  * @note The buffers are organized as uint8_t arrays for data sizes below
269  * or equal to 8 bits else it is organized as uint16_t arrays.
270  * @note This function has to be invoked from a lock zone.
271  *
272  * @param[in] uartp pointer to the @p UARTDriver object
273  * @param[in] n number of data frames to receive
274  * @param[out] rxbuf the pointer to the receive buffer
275  *
276  * @iclass
277  */
278 void uartStartReceiveI(UARTDriver *uartp, size_t n, void *rxbuf) {
279 
281  osalDbgCheck((uartp != NULL) && (n > 0U) && (rxbuf != NULL));
282  osalDbgAssert(uartp->state == UART_READY, "is active");
283  osalDbgAssert(uartp->rxstate != UART_RX_ACTIVE, "rx active");
284 
285  uart_lld_start_receive(uartp, n, rxbuf);
286  uartp->rxstate = UART_RX_ACTIVE;
287 }
288 
289 /**
290  * @brief Stops any ongoing receive operation.
291  * @note Stopping a receive operation also suppresses the receive callbacks.
292  *
293  * @param[in] uartp pointer to the @p UARTDriver object
294  *
295  * @return The number of data frames not received by the
296  * stopped receive operation.
297  * @retval UART_ERR_NOT_ACTIVE if there was no receive operation in progress.
298  *
299  * @api
300  */
301 size_t uartStopReceive(UARTDriver *uartp) {
302  size_t n;
303 
304  osalDbgCheck(uartp != NULL);
305 
306  osalSysLock();
307  osalDbgAssert(uartp->state == UART_READY, "not active");
308 
309  if (uartp->rxstate == UART_RX_ACTIVE) {
310  n = uart_lld_stop_receive(uartp);
311  uartp->rxstate = UART_RX_IDLE;
312  }
313  else {
314  n = UART_ERR_NOT_ACTIVE;
315  }
316  osalSysUnlock();
317 
318  return n;
319 }
320 
321 /**
322  * @brief Stops any ongoing receive operation.
323  * @note Stopping a receive operation also suppresses the receive callbacks.
324  * @note This function has to be invoked from a lock zone.
325  *
326  * @param[in] uartp pointer to the @p UARTDriver object
327  *
328  * @return The number of data frames not received by the
329  * stopped receive operation.
330  * @retval UART_ERR_NOT_ACTIVE if there was no receive operation in progress.
331  *
332  * @iclass
333  */
334 size_t uartStopReceiveI(UARTDriver *uartp) {
335 
337  osalDbgCheck(uartp != NULL);
338  osalDbgAssert(uartp->state == UART_READY, "not active");
339 
340  if (uartp->rxstate == UART_RX_ACTIVE) {
341  size_t n = uart_lld_stop_receive(uartp);
342  uartp->rxstate = UART_RX_IDLE;
343  return n;
344  }
345  return UART_ERR_NOT_ACTIVE;
346 }
347 
348 #if (UART_USE_WAIT == TRUE) || defined(__DOXYGEN__)
349 /**
350  * @brief Performs a transmission on the UART peripheral.
351  * @note The function returns when the specified number of frames have been
352  * sent to the UART or on timeout.
353  * @note The buffers are organized as uint8_t arrays for data sizes below
354  * or equal to 8 bits else it is organized as uint16_t arrays.
355  * @note This function implements a software timeout, it does not use
356  * any underlying HW timeout mechanism.
357  *
358  * @param[in] uartp pointer to the @p UARTDriver object
359  * @param[in,out] np number of data frames to transmit, on exit the number
360  * of frames actually transmitted
361  * @param[in] txbuf the pointer to the transmit buffer
362  * @param[in] timeout operation timeout
363  * @return The operation status.
364  * @retval MSG_OK if the operation completed successfully.
365  * @retval MSG_TIMEOUT if the operation timed out.
366  *
367  * @api
368  */
369 msg_t uartSendTimeout(UARTDriver *uartp, size_t *np,
370  const void *txbuf, sysinterval_t timeout) {
371  msg_t msg;
372 
373  osalDbgCheck((uartp != NULL) && (*np > 0U) && (txbuf != NULL));
374 
375  osalSysLock();
376  osalDbgAssert(uartp->state == UART_READY, "is active");
377  osalDbgAssert(uartp->txstate != UART_TX_ACTIVE, "tx active");
378 
379  /* Transmission start.*/
380  uartp->early = true;
381  uart_lld_start_send(uartp, *np, txbuf);
382  uartp->txstate = UART_TX_ACTIVE;
383 
384  /* Waiting for result.*/
385  msg = osalThreadSuspendTimeoutS(&uartp->threadtx, timeout);
386  if (msg != MSG_OK) {
387  *np -= uartStopSendI(uartp);
388  }
389  osalSysUnlock();
390 
391  return msg;
392 }
393 
394 /**
395  * @brief Performs a transmission on the UART peripheral.
396  * @note The function returns when the specified number of frames have been
397  * physically transmitted or on timeout.
398  * @note The buffers are organized as uint8_t arrays for data sizes below
399  * or equal to 8 bits else it is organized as uint16_t arrays.
400  * @note This function implements a software timeout, it does not use
401  * any underlying HW timeout mechanism.
402  *
403  * @param[in] uartp pointer to the @p UARTDriver object
404  * @param[in,out] np number of data frames to transmit, on exit the number
405  * of frames actually transmitted
406  * @param[in] txbuf the pointer to the transmit buffer
407  * @param[in] timeout operation timeout
408  * @return The operation status.
409  * @retval MSG_OK if the operation completed successfully.
410  * @retval MSG_TIMEOUT if the operation timed out.
411  *
412  * @api
413  */
415  const void *txbuf, sysinterval_t timeout) {
416  msg_t msg;
417 
418  osalDbgCheck((uartp != NULL) && (*np > 0U) && (txbuf != NULL));
419 
420  osalSysLock();
421  osalDbgAssert(uartp->state == UART_READY, "is active");
422  osalDbgAssert(uartp->txstate != UART_TX_ACTIVE, "tx active");
423 
424  /* Transmission start.*/
425  uartp->early = false;
426  uart_lld_start_send(uartp, *np, txbuf);
427  uartp->txstate = UART_TX_ACTIVE;
428 
429  /* Waiting for result.*/
430  msg = osalThreadSuspendTimeoutS(&uartp->threadtx, timeout);
431  if (msg != MSG_OK) {
432  *np = uartStopSendI(uartp);
433  }
434  osalSysUnlock();
435 
436  return msg;
437 }
438 
439 /**
440  * @brief Performs a receive operation on the UART peripheral.
441  * @note The function returns when the specified number of frames have been
442  * received or on error/timeout.
443  * @note The buffers are organized as uint8_t arrays for data sizes below
444  * or equal to 8 bits else it is organized as uint16_t arrays.
445  * @note This function implements a software timeout, it does not use
446  * any underlying HW timeout mechanism.
447  *
448  * @param[in] uartp pointer to the @p UARTDriver object
449  * @param[in,out] np number of data frames to receive, on exit the number
450  * of frames actually received
451  * @param[in] rxbuf the pointer to the receive buffer
452  * @param[in] timeout operation timeout
453  *
454  * @return The operation status.
455  * @retval MSG_OK if the operation completed successfully.
456  * @retval MSG_TIMEOUT if the operation timed out.
457  * @retval MSG_RESET in case of a receive error.
458  *
459  * @api
460  */
462  void *rxbuf, sysinterval_t timeout) {
463  msg_t msg;
464 
465  osalDbgCheck((uartp != NULL) && (*np > 0U) && (rxbuf != NULL));
466 
467  osalSysLock();
468  osalDbgAssert(uartp->state == UART_READY, "is active");
469  osalDbgAssert(uartp->rxstate != UART_RX_ACTIVE, "rx active");
470 
471  /* Receive start.*/
472  uart_lld_start_receive(uartp, *np, rxbuf);
473  uartp->rxstate = UART_RX_ACTIVE;
474 
475  /* Waiting for result.*/
476  msg = osalThreadSuspendTimeoutS(&uartp->threadrx, timeout);
477  if (msg != MSG_OK) {
478  *np -= uartStopReceiveI(uartp);
479  }
480  osalSysUnlock();
481 
482  return msg;
483 }
484 #endif
485 
486 #if (UART_USE_MUTUAL_EXCLUSION == TRUE) || defined(__DOXYGEN__)
487 /**
488  * @brief Gains exclusive access to the UART bus.
489  * @details This function tries to gain ownership to the UART bus, if the bus
490  * is already being used then the invoking thread is queued.
491  * @pre In order to use this function the option @p UART_USE_MUTUAL_EXCLUSION
492  * must be enabled.
493  *
494  * @param[in] uartp pointer to the @p UARTDriver object
495  *
496  * @api
497  */
499 
500  osalDbgCheck(uartp != NULL);
501 
502  osalMutexLock(&uartp->mutex);
503 }
504 
505 /**
506  * @brief Releases exclusive access to the UART bus.
507  * @pre In order to use this function the option @p UART_USE_MUTUAL_EXCLUSION
508  * must be enabled.
509  *
510  * @param[in] uartp pointer to the @p UARTDriver object
511  *
512  * @api
513  */
515 
516  osalDbgCheck(uartp != NULL);
517 
518  osalMutexUnlock(&uartp->mutex);
519 }
520 #endif
521 
522 #endif /* HAL_USE_UART == TRUE */
523 
524 /** @} */
static void osalMutexObjectInit(mutex_t *mp)
Initializes s mutex_t object.
Definition: osal.h:681
const UARTConfig * config
Current configuration data.
Definition: hal_uart_lld.h:145
void uartStartSend(UARTDriver *uartp, size_t n, const void *txbuf)
Starts a transmission on the UART peripheral.
Definition: hal_uart.c:147
size_t uartStopReceive(UARTDriver *uartp)
Stops any ongoing receive operation.
Definition: hal_uart.c:301
void uartInit(void)
UART Driver initialization.
Definition: hal_uart.c:56
void uart_lld_start(UARTDriver *uartp)
Configures and activates the UART peripheral.
Definition: hal_uart_lld.c:80
bool early
Synchronization flag for transmit operations.
Definition: hal_uart_lld.h:150
size_t uartStopSendI(UARTDriver *uartp)
Stops any ongoing transmission.
Definition: hal_uart.c:228
size_t uart_lld_stop_receive(UARTDriver *uartp)
Stops any ongoing receive operation.
Definition: hal_uart_lld.c:182
HAL subsystem header.
#define osalDbgCheckClassI()
I-Class state check.
Definition: osal.h:292
thread_reference_t threadrx
Waiting thread on RX.
Definition: hal_uart_lld.h:154
void uartStop(UARTDriver *uartp)
Deactivates the UART peripheral.
Definition: hal_uart.c:118
void osalMutexLock(mutex_t *mp)
Locks the specified mutex.
Definition: osal.c:384
static void osalSysUnlock(void)
Leaves a critical zone from thread context.
Definition: osal.h:540
msg_t uartSendTimeout(UARTDriver *uartp, size_t *np, const void *txbuf, sysinterval_t timeout)
Performs a transmission on the UART peripheral.
Definition: hal_uart.c:369
size_t uartStopReceiveI(UARTDriver *uartp)
Stops any ongoing receive operation.
Definition: hal_uart.c:334
size_t uart_lld_stop_send(UARTDriver *uartp)
Stops any ongoing transmission.
Definition: hal_uart_lld.c:145
size_t uartStopSend(UARTDriver *uartp)
Stops any ongoing transmission.
Definition: hal_uart.c:195
uarttxstate_t txstate
Transmitter state.
Definition: hal_uart_lld.h:137
void uart_lld_stop(UARTDriver *uartp)
Deactivates the UART peripheral.
Definition: hal_uart_lld.c:101
msg_t uartReceiveTimeout(UARTDriver *uartp, size_t *np, void *rxbuf, sysinterval_t timeout)
Performs a receive operation on the UART peripheral.
Definition: hal_uart.c:461
void uart_lld_start_receive(UARTDriver *uartp, size_t n, void *rxbuf)
Starts a receive operation on the UART peripheral.
Definition: hal_uart_lld.c:163
int32_t msg_t
Type of a message.
Definition: osal.h:160
void uartStartSendI(UARTDriver *uartp, size_t n, const void *txbuf)
Starts a transmission on the UART peripheral.
Definition: hal_uart.c:172
void uart_lld_start_send(UARTDriver *uartp, size_t n, const void *txbuf)
Starts a transmission on the UART peripheral.
Definition: hal_uart_lld.c:126
void uartStartReceiveI(UARTDriver *uartp, size_t n, void *rxbuf)
Starts a receive operation on the UART peripheral.
Definition: hal_uart.c:278
void uartReleaseBus(UARTDriver *uartp)
Releases exclusive access to the UART bus.
Definition: hal_uart.c:514
void uartAcquireBus(UARTDriver *uartp)
Gains exclusive access to the UART bus.
Definition: hal_uart.c:498
void uartStartReceive(UARTDriver *uartp, size_t n, void *rxbuf)
Starts a receive operation on the UART peripheral.
Definition: hal_uart.c:253
#define osalDbgCheck(c)
Function parameters check.
Definition: osal.h:278
void uart_lld_init(void)
Low level UART driver initialization.
Definition: hal_uart_lld.c:65
uint32_t sysinterval_t
Type of system time interval.
Definition: osal.h:170
uartrxstate_t rxstate
Receiver state.
Definition: hal_uart_lld.h:141
msg_t osalThreadSuspendTimeoutS(thread_reference_t *trp, sysinterval_t timeout)
Sends the current thread sleeping and sets a reference variable.
Definition: osal.c:211
void uartStart(UARTDriver *uartp, const UARTConfig *config)
Configures and activates the UART peripheral.
Definition: hal_uart.c:97
uartstate_t state
Driver state.
Definition: hal_uart_lld.h:133
static void osalSysLock(void)
Enters a critical zone from thread context.
Definition: osal.h:530
Driver configuration structure.
Definition: hal_uart_lld.h:100
thread_reference_t threadtx
Waiting thread on TX.
Definition: hal_uart_lld.h:158
void uartObjectInit(UARTDriver *uartp)
Initializes the standard part of a UARTDriver structure.
Definition: hal_uart.c:68
#define osalDbgAssert(c, remark)
Condition assertion.
Definition: osal.h:258
void osalMutexUnlock(mutex_t *mp)
Unlocks the specified mutex.
Definition: osal.c:404
Structure representing an UART driver.
Definition: hal_uart_lld.h:129
mutex_t mutex
Mutex protecting the peripheral.
Definition: hal_uart_lld.h:164
msg_t uartSendFullTimeout(UARTDriver *uartp, size_t *np, const void *txbuf, sysinterval_t timeout)
Performs a transmission on the UART peripheral.
Definition: hal_uart.c:414