root / proj / src / libs / uart / include / uart.h @ 377
History | View | Annotate | Download (6.97 KB)
1 | 235 | up20180642 | #ifndef UART_H_INCLUDED
|
---|---|---|---|
2 | #define UART_H_INCLUDED
|
||
3 | |||
4 | 334 | up20180642 | /**
|
5 | * @defgroup uart uart
|
||
6 | 351 | up20180642 | * @ingroup libs
|
7 | 334 | up20180642 | * @brief UART module.
|
8 | *
|
||
9 | * @{
|
||
10 | */
|
||
11 | |||
12 | 335 | up20180642 | /// @brief COM1 base address
|
13 | 249 | up20180642 | #define COM1_ADDR 0x3F8 |
14 | 335 | up20180642 | /// @brief COM2 base address
|
15 | 249 | up20180642 | #define COM2_ADDR 0x2F8 |
16 | 335 | up20180642 | /// @brief COM1 IRQ line
|
17 | 249 | up20180642 | #define COM1_IRQ 4 |
18 | 335 | up20180642 | /// @brief COM2 IRQ line
|
19 | 249 | up20180642 | #define COM2_IRQ 3 |
20 | 335 | up20180642 | /// @brief COM1 Vector (???)
|
21 | 249 | up20180642 | #define COM1_VECTOR 0x0C |
22 | 335 | up20180642 | /// @brief COM2 Vector (???)
|
23 | 249 | up20180642 | #define COM2_VECTOR 0x0B |
24 | 235 | up20180642 | |
25 | 335 | up20180642 | /**
|
26 | * @brief Possible parity schemes for UART.
|
||
27 | */
|
||
28 | 249 | up20180642 | typedef enum { |
29 | uart_parity_none = 0x0,
|
||
30 | uart_parity_odd = 0x1,
|
||
31 | uart_parity_even = 0x3,
|
||
32 | uart_parity_par1 = 0x5,
|
||
33 | uart_parity_par0 = 0x7
|
||
34 | } uart_parity; |
||
35 | |||
36 | 335 | up20180642 | /**
|
37 | * @brief UART configuration
|
||
38 | */
|
||
39 | 235 | up20180642 | typedef struct { |
40 | 335 | up20180642 | /// @brief Base address of the serial port this configuration was read from
|
41 | 242 | up20180642 | int base_addr ;
|
42 | 335 | up20180642 | /// @brief LCR (Line Control Register)
|
43 | 242 | up20180642 | uint8_t lcr ; |
44 | 335 | up20180642 | /// @brief DLL (Divisor Latch Least significant byte)
|
45 | 242 | up20180642 | uint8_t dll ; |
46 | 335 | up20180642 | /// @brief DLL (Divisor Latch Most significant byte)
|
47 | 242 | up20180642 | uint8_t dlm ; |
48 | 335 | up20180642 | /// @brief Number of bits per char
|
49 | 242 | up20180642 | uint8_t bits_per_char ; |
50 | 335 | up20180642 | /// @brief Number of stop bits
|
51 | 242 | up20180642 | uint8_t stop_bits ; |
52 | 335 | up20180642 | /// @brief Parity scheme
|
53 | 249 | up20180642 | uart_parity parity ; |
54 | 335 | up20180642 | /// @brief Break control (???)
|
55 | 242 | up20180642 | uint8_t break_control :1;
|
56 | 335 | up20180642 | /// @brief Divisor Latch Access Bit (1 if DLL, DLM can be accessed)
|
57 | 242 | up20180642 | uint8_t dlab :1;
|
58 | 335 | up20180642 | /// @brief Divisor latch
|
59 | 242 | up20180642 | uint16_t divisor_latch ; |
60 | 335 | up20180642 | /// @brief IER (Interrupt Enable Register)
|
61 | 242 | up20180642 | uint8_t ier ; |
62 | 335 | up20180642 | /// @brief Receiver data interrupt enabled if 1
|
63 | 242 | up20180642 | uint8_t received_data_int :1;
|
64 | 335 | up20180642 | /// @brief Transmitter empty interrupt enabled if 1
|
65 | 242 | up20180642 | uint8_t transmitter_empty_int :1;
|
66 | 335 | up20180642 | /// @brief Receiver line status register (LSR) change interrupt enabled if 1
|
67 | 242 | up20180642 | uint8_t receiver_line_stat_int:1;
|
68 | 335 | up20180642 | /// @brief Modem status interrupt enabled if 1
|
69 | 242 | up20180642 | uint8_t modem_stat_int :1;
|
70 | 235 | up20180642 | } uart_config; |
71 | |||
72 | 334 | up20180642 | /**
|
73 | * @brief Subscribes UART interrupts and disables Minix Default IH.
|
||
74 | * @param interrupt_bit Bit of interrupt vector that will be set when UART interrupt is pending
|
||
75 | * @param interrupt_id UART interrupt ID to specify the UART interrupt in other calls
|
||
76 | * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
|
||
77 | * @see {_ERRORS_H_::errors}
|
||
78 | */
|
||
79 | 263 | up20180642 | int (subscribe_uart_interrupt)(uint8_t interrupt_bit, int *interrupt_id); |
80 | 334 | up20180642 | /**
|
81 | * @brief Get UART configuration.
|
||
82 | * @param base_addr Base address of the serial port whose configuration we want
|
||
83 | * @param config Pointer to uart_config where the read configuration will be stored
|
||
84 | * @return SUCCESS if operation is successful, other value otherwise
|
||
85 | */
|
||
86 | 235 | up20180642 | int uart_get_config(int base_addr, uart_config *config); |
87 | 334 | up20180642 | /**
|
88 | * @brief Print to stdout a UART configuration in a human-friendly way.
|
||
89 | * @param config UART configuration to be printed
|
||
90 | */
|
||
91 | 235 | up20180642 | void uart_print_config(uart_config config);
|
92 | |||
93 | 334 | up20180642 | /**
|
94 | * @brief Set number of bits per character.
|
||
95 | *
|
||
96 | * Valid numbers of bits per char go from 5 to 8.
|
||
97 | * @param base_addr Base address of serial port
|
||
98 | * @param bits_per_char Number of bits per char
|
||
99 | * @return SUCCESS if operation is successful, other value otherwise
|
||
100 | */
|
||
101 | 235 | up20180642 | int uart_set_bits_per_character(int base_addr, uint8_t bits_per_char); |
102 | 334 | up20180642 | /**
|
103 | * @brief Set number of stop bits.
|
||
104 | *
|
||
105 | * Valid numbers of stop bits are 1 and 2.
|
||
106 | * @param base_addr Base address of serial port
|
||
107 | * @param stop Number of stop bits
|
||
108 | * @return SUCCESS if operation is successful, other value otherwise
|
||
109 | */
|
||
110 | 235 | up20180642 | int uart_set_stop_bits (int base_addr, uint8_t stop ); |
111 | 334 | up20180642 | /**
|
112 | * @brief Set parity scheme to be used.
|
||
113 | * @param base_addr Base address of serial port
|
||
114 | * @param par Parity scheme
|
||
115 | * @return SUCCESS if operation is successful, other value otherwise
|
||
116 | */
|
||
117 | 235 | up20180642 | int uart_set_parity (int base_addr, uart_parity par ); |
118 | 334 | up20180642 | /**
|
119 | * @brief Set bit rate.
|
||
120 | *
|
||
121 | * Valid numbers of bit rates go up to 115200 bps.
|
||
122 | * @param base_addr Base address of serial port
|
||
123 | * @param bit_rate Number of bits per second
|
||
124 | * @return SUCCESS if operation is successful, other value otherwise
|
||
125 | */
|
||
126 | 328 | up20180642 | int uart_set_bit_rate (int base_addr, uint32_t bit_rate ); |
127 | 235 | up20180642 | |
128 | 334 | up20180642 | /**
|
129 | * @brief Enable Receiver Register Ready interrupts
|
||
130 | * @param base_addr Base address of serial port
|
||
131 | * @return SUCCESS if operation is successful, other value otherwise
|
||
132 | */
|
||
133 | 252 | up20180642 | int uart_enable_int_rx (int base_addr); |
134 | 334 | up20180642 | /**
|
135 | * @brief Disable Receiver Register Ready interrupts
|
||
136 | * @param base_addr Base address of serial port
|
||
137 | * @return SUCCESS if operation is successful, other value otherwise
|
||
138 | */
|
||
139 | 252 | up20180642 | int uart_disable_int_rx(int base_addr); |
140 | 334 | up20180642 | /**
|
141 | * @brief Enable Transmitter Register Empty interrupts
|
||
142 | * @param base_addr Base address of serial port
|
||
143 | * @return SUCCESS if operation is successful, other value otherwise
|
||
144 | */
|
||
145 | 252 | up20180642 | int uart_enable_int_tx (int base_addr); |
146 | 334 | up20180642 | /**
|
147 | * @brief Disable Transmitter Register Empty interrupts
|
||
148 | * @param base_addr Base address of serial port
|
||
149 | * @return SUCCESS if operation is successful, other value otherwise
|
||
150 | */
|
||
151 | 252 | up20180642 | int uart_disable_int_tx(int base_addr); |
152 | |||
153 | 334 | up20180642 | /**
|
154 | * @}
|
||
155 | */
|
||
156 | |||
157 | /**
|
||
158 | * @defgroup nctp nctp
|
||
159 | * @ingroup uart
|
||
160 | * @brief NCTP (Non-Critical Transfer Protocol) module.
|
||
161 | * @{
|
||
162 | */
|
||
163 | |||
164 | /**
|
||
165 | * @brief Initialize NCTP structures.
|
||
166 | * @return SUCCESS if operation is successful, other value otherwise
|
||
167 | */
|
||
168 | 297 | up20180642 | int nctp_init(void); |
169 | 334 | up20180642 | /**
|
170 | * @brief Dump all the content of the NCTP in/out queues.
|
||
171 | * @return SUCCESS if operation is successful, other value otherwise
|
||
172 | */
|
||
173 | 298 | up20180642 | int nctp_dump(void); |
174 | 334 | up20180642 | /**
|
175 | * @brief Set function to be called when a full message is received.
|
||
176 | * @return SUCCESS if operation is successful, other value otherwise
|
||
177 | */
|
||
178 | 297 | up20180642 | int nctp_set_processor(void (*proc_func)(const uint8_t*, const size_t)); |
179 | 334 | up20180642 | /**
|
180 | * @brief Free resources taken on NCTP initialization.
|
||
181 | * @return SUCCESS if operation is successful, other value otherwise
|
||
182 | */
|
||
183 | 282 | up20180642 | int nctp_free(void); |
184 | |||
185 | 334 | up20180642 | /**
|
186 | * @brief Send a message throught serial port COM1
|
||
187 | * @param num Number of different blocks to send in the message
|
||
188 | * @param ptr Array of length num, with pointers to blocks that should be sent
|
||
189 | * @param sz Size of each block that should be sent, in bytes
|
||
190 | * @return SUCCESS if operation is successful, other value otherwise
|
||
191 | */
|
||
192 | 325 | up20180642 | int nctp_send(size_t num, const uint8_t *const *ptr, const size_t *const sz); |
193 | 334 | up20180642 | /**
|
194 | * @brief Get NCTP interrupt handler error code.
|
||
195 | * @return SUCCESS if previous interrupt handler call was successful, other value otherwise
|
||
196 | */
|
||
197 | 323 | up20180642 | int (nctp_get_ih_error)(void); |
198 | 334 | up20180642 | /**
|
199 | * @brief NCTP interrupt handler.
|
||
200 | *
|
||
201 | * @note Must be called on all interrupt cycles. Even if not necessary, it must be
|
||
202 | * called so the NCTP is allowed to dump any messages it might receive, even
|
||
203 | * if those messages are unexpected.
|
||
204 | */
|
||
205 | 282 | up20180642 | void nctp_ih(void); |
206 | |||
207 | 334 | up20180642 | /**
|
208 | * @}
|
||
209 | */
|
||
210 | |||
211 | 235 | up20180642 | #endif //UART_H_INCLUDED |