Project

General

Profile

Statistics
| Revision:

root / proj / libs / uart / include / uart.h @ 338

History | View | Annotate | Download (6.95 KB)

1
#ifndef UART_H_INCLUDED
2
#define UART_H_INCLUDED
3

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

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

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

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

    
92
/**
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
int uart_set_bits_per_character(int base_addr, uint8_t     bits_per_char);
101
/**
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
int uart_set_stop_bits         (int base_addr, uint8_t     stop         );
110
/**
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
int uart_set_parity            (int base_addr, uart_parity par          );
117
/**
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
int uart_set_bit_rate          (int base_addr, uint32_t    bit_rate     );
126

    
127
/**
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
int uart_enable_int_rx (int base_addr);
133
/**
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
int uart_disable_int_rx(int base_addr);
139
/**
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
int uart_enable_int_tx (int base_addr);
145
/**
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
int uart_disable_int_tx(int base_addr);
151

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

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

    
206
/**
207
 * @}
208
 */
209

    
210
#endif //UART_H_INCLUDED