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