This repository has been archived by the owner on Nov 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.h
210 lines (179 loc) · 6.64 KB
/
uart.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
Copyright (c)
(c) 2015-2016 Chintalagiri Shashank, Quazar Technologies Pvt. Ltd.
This file is part of
Embedded bootstraps : hal-uC
This library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file hal_uc_uart.h
* @brief HAL for UART peripherals
*
* This file is the hardware abstraction layer for uC UART interfaces
*
* @see uart_impl.h
* @see uart_impl.c
*/
#ifndef HAL_UC_UART_H
#define HAL_UC_UART_H
#include <stdarg.h>
#include <platform/transport.h>
#include <platform/types.h>
#include "map.h"
#ifdef uC_INCLUDE_UART_IFACE
/**
* @name UART API Setup Functions
*/
/**@{*/
/**
* Initialize UART. Config parameters for the UART are (currently) defined in UC_MAP.
* @param intfnum Identifier of the UART interface
*/
void uart_init(uint8_t intfnum);
/**@}*/
/**
* @name Main UART API Functions
*/
/**@{*/
/**
* @brief Trigger UART Send over the specified interface.
* @param intfnum Identifier of the UART interface
*
* Trigger the transmission of data within the UART
* transmit buffer. This buffer would typically be a ::bytebuf,
* though the implentation could use whatever it wants.
*
* This function should only be called when there is atleast
* one valid byte in the transmit buffer. It is the callers
* responsibility to ensure this is so.
*
* This function should be as optimized as possible. It
* should be small enough to be inlined, and should actually
* be inlined.
*
* Buffer preparation should be done by other functions.
* @see uart_vprintf_buf()
* @see uart_putc_buf()
*/
static inline void uart_send_trigger(uint8_t intfnum);
static inline void uart_send_flush(uint8_t intfnum);
static inline uint8_t uart_reqlock(uint8_t intfnum, uint8_t len, uint8_t token);
/**
* @brief TX buffer prep function - putchar
* @param intfnum Identifier of the UART interface.
* @param byte byte to be sent.
* @param token token against which buffer lock should be obtained
* and/or used.
* @param handlelock If 1, the function will deal with interacting
* with the underlying locking mechanism. If not, it
* assumes you already hold the necessary locks and use
* the provided token.
* @warning If the locking is left to this function, it will give up
* immediately if the lock is held elsewhere. You should
* check the return value of the function if the send is
* critical.
* @return 0 for error, 1 for success.
*
* Send a single character via the specified UART interface using the bytebuf.
*
* @see uart_send_trigger()
*/
static inline uint8_t uart_putc(uint8_t intfnum, uint8_t byte, uint8_t token, uint8_t handlelock);
static inline uint8_t uart_write(uint8_t intfnum, uint8_t *buffer, uint8_t len, uint8_t token);
/**
* @brief TX buffer prep function - printf
* @param intfnum Identifier of the UART interface.
* @param format subset of the standard printf format.
* @param args pointer to stdarg.h var_list instance.
* @return 0 for error, number of characters written for success.
*
* Print to the specified UART interface via a printf-like
* function.
*
* This function should not be used in bandwidth-limited transmissions.
* Both the implementation of printf and the locking mechanism
* are likely to be bottlenecks to data generation and transmission.
* The uart_putc_buf() function with caller managed buffer locking
* will be considerably more efficient.
*
* This function should be callable identically to printf.
* If only a subset of the format strings are allowed, this
* should be documented by the implementation. The current
* implemetation of printf.h can be taken as the
* least-common-denominator implementation for the moment.
*
* @note float should perhaps be added to the implementation.
*
* @see uart_send_trigger()
*/
uint8_t uart_vprintf(uint8_t intfnum, const char *format, ...);
/**
* @brief RX buffer status function - unread bytes
* @param intfnum Identifier of the UART interface.
* @return number of unread bytes in the recieve buffer.
*
* Get number of unread bytes in the specified UART interface's rxbuffer.
*
*/
static inline uint8_t uart_population_rxb(uint8_t intfnum);
static inline void uart_discard_rxb(uint8_t intfnum);
static inline uint8_t uart_getc(uint8_t intfnum);
static inline uint8_t uart_read(uint8_t intfnum, uint8_t *buffer, uint8_t len);
void _uart0_irqhandler(void);
void _uart1_irqhandler(void);
extern uint16_t * uart0_overrun_counter;
extern uint16_t * uart1_overrun_counter;
/**@}*/
/**
* @name Hardware Debug-Only UART API Functions
*/
/**@{*/
/**
* @brief Send a single character over the specified UART interface
* @param byte The character to be sent
* @param intfnum Identifier of the UART interface
*
* This function uses the simplest possible method
* to send a single byte of data. It should not use
* buffers, interrupts, etc.
*
* @warning In a general application, this function would only be used
* in special circumstances and with extreme care. Using this alongside
* bytebufs will cause data loss. If you intend to use this interface,
* the peripheral initialization may need to be changed to not enable
* interrupts.
*/
static inline void uart_putc_bare(uint8_t intfnum, uint8_t byte);
/**
* @brief Recieve a single character from the specified UART interface
* @param intfnum Identifier of the UART interface
*
* This function uses the simplest possible method
* to recieve a single byte of data. It should not use
* buffers, interrupts, etc.
*
* @warning In a general application, this function would only be used
* in special circumstances and with extreme care. Using this alongside
* bytebufs will cause data loss. If you intend to use this interface,
* the peripheral initialization may need to be changed to not enable
* interrupts.
*
* @return Character recieved
*/
static inline uint8_t uart_getc_bare(uint8_t intfnum);
/**@}*/
extern const pluggable_transport_t ptransport_uart;
#endif
// Set up the implementation
#include <hal_platform/uart_impl.h>
#include <hal_platform/uart_handlers.h>
#endif