Project

General

Profile

Statistics
| Revision:

root / proj / src / libs / uart / include / uart.h @ 383

History | View | Annotate | Download (6.97 KB)

1
#ifndef UART_H_INCLUDED
2
#define UART_H_INCLUDED
3

    
4
/**
5
 * @defgroup uart uart
6
 * @ingroup libs
7
 * @brief UART module.
8
 *
9
 * @{
10
 */
11

    
12
/// @brief COM1 base address
13
#define COM1_ADDR           0x3F8
14
/// @brief COM2 base address
15
#define COM2_ADDR           0x2F8
16
/// @brief COM1 IRQ line
17
#define COM1_IRQ            4
18
/// @brief COM2 IRQ line
19
#define COM2_IRQ            3
20
/// @brief COM1 Vector (???)
21
#define COM1_VECTOR         0x0C
22
/// @brief COM2 Vector (???)
23
#define COM2_VECTOR         0x0B
24

    
25
/**
26
 * @brief Possible parity schemes for UART.
27
 */
28
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
/**
37
 * @brief UART configuration
38
 */
39
typedef struct {
40
    /// @brief  Base address of the serial port this configuration was read from
41
    int     base_addr               ;
42
    /// @brief  LCR (Line Control Register)
43
    uint8_t lcr                     ;
44
    /// @brief DLL (Divisor Latch Least significant byte)
45
    uint8_t dll                     ;
46
    /// @brief DLL (Divisor Latch Most significant byte)
47
    uint8_t dlm                     ;
48
    /// @brief Number of bits per char
49
    uint8_t bits_per_char           ;
50
    /// @brief Number of stop bits
51
    uint8_t stop_bits               ;
52
    /// @brief Parity scheme
53
    uart_parity parity              ;
54
    /// @brief Break control (???)
55
    uint8_t break_control         :1;
56
    /// @brief Divisor Latch Access Bit (1 if DLL, DLM can be accessed)
57
    uint8_t dlab                  :1;
58
    ///  @brief Divisor latch
59
    uint16_t divisor_latch          ;
60
    /// @brief IER (Interrupt Enable Register)
61
    uint8_t ier                     ;
62
    /// @brief  Receiver data interrupt enabled if 1
63
    uint8_t received_data_int     :1;
64
    /// @brief  Transmitter empty interrupt enabled if 1
65
    uint8_t transmitter_empty_int :1;
66
    /// @brief  Receiver line status register (LSR) change interrupt enabled if 1
67
    uint8_t receiver_line_stat_int:1;
68
    /// @brief  Modem status interrupt enabled if 1
69
    uint8_t modem_stat_int        :1;
70
} uart_config;
71

    
72
/**
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
int (subscribe_uart_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
80
/**
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
int uart_get_config(int base_addr, uart_config *config);
87
/**
88
 * @brief Print to stdout a UART configuration in a human-friendly way.
89
 * @param   config  UART configuration to be printed
90
 */
91
void uart_print_config(uart_config config);
92

    
93
/**
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
int uart_set_bits_per_character(int base_addr, uint8_t     bits_per_char);
102
/**
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
int uart_set_stop_bits         (int base_addr, uint8_t     stop         );
111
/**
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
int uart_set_parity            (int base_addr, uart_parity par          );
118
/**
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
int uart_set_bit_rate          (int base_addr, uint32_t    bit_rate     );
127

    
128
/**
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
int uart_enable_int_rx (int base_addr);
134
/**
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
int uart_disable_int_rx(int base_addr);
140
/**
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
int uart_enable_int_tx (int base_addr);
146
/**
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
int uart_disable_int_tx(int base_addr);
152

    
153
/**
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
int nctp_init(void);
169
/**
170
 * @brief Dump all the content of the NCTP in/out queues.
171
 * @return SUCCESS if operation is successful, other value otherwise
172
 */
173
int nctp_dump(void);
174
/**
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
int nctp_set_processor(void (*proc_func)(const uint8_t*, const size_t));
179
/**
180
 * @brief Free resources taken on NCTP initialization.
181
 * @return SUCCESS if operation is successful, other value otherwise
182
 */
183
int nctp_free(void);
184

    
185
/**
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
int nctp_send(size_t num, const uint8_t *const *ptr, const size_t *const sz);
193
/**
194
 * @brief Get NCTP interrupt handler error code.
195
 * @return  SUCCESS if previous interrupt handler call was successful, other value otherwise
196
 */
197
int (nctp_get_ih_error)(void);
198
/**
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
void nctp_ih(void);
206

    
207
/**
208
 * @}
209
 */
210

    
211
#endif //UART_H_INCLUDED