Project

General

Profile

Statistics
| Revision:

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

History | View | Annotate | Download (5.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
#define COM1_ADDR           0x3F8
12
#define COM2_ADDR           0x2F8
13
#define COM1_IRQ            4
14
#define COM2_IRQ            3
15
#define COM1_VECTOR         0x0C
16
#define COM2_VECTOR         0x0B
17

    
18
typedef enum {
19
    uart_parity_none = 0x0,
20
    uart_parity_odd  = 0x1,
21
    uart_parity_even = 0x3,
22
    uart_parity_par1 = 0x5,
23
    uart_parity_par0 = 0x7
24
} uart_parity;
25

    
26
typedef struct {
27
    int     base_addr               ;
28
    uint8_t lcr                     ;
29
    uint8_t dll                     ;
30
    uint8_t dlm                     ;
31
    uint8_t bits_per_char           ;
32
    uint8_t stop_bits               ;
33
    uart_parity parity              ;
34
    uint8_t break_control         :1;
35
    uint8_t dlab                  :1;
36
    uint16_t divisor_latch          ;
37
    uint8_t ier                     ;
38
    uint8_t received_data_int     :1;
39
    uint8_t transmitter_empty_int :1;
40
    uint8_t receiver_line_stat_int:1;
41
    uint8_t modem_stat_int        :1;
42
} uart_config;
43

    
44
/**
45
 * @brief Subscribes UART interrupts and disables Minix Default IH.
46
 * @param interrupt_bit Bit of interrupt vector that will be set when UART interrupt is pending
47
 * @param interrupt_id UART interrupt ID to specify the UART interrupt in other calls
48
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
49
 * @see {_ERRORS_H_::errors}
50
 */
51
int (subscribe_uart_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
52
/**
53
 * @brief Get UART configuration.
54
 * @param   base_addr   Base address of the serial port whose configuration we want
55
 * @param   config      Pointer to uart_config where the read configuration will be stored
56
 * @return SUCCESS if operation is successful, other value otherwise
57
 */
58
int uart_get_config(int base_addr, uart_config *config);
59
/**
60
 * @brief Print to stdout a UART configuration in a human-friendly way.
61
 * @param   config  UART configuration to be printed
62
 */
63
void uart_print_config(uart_config config);
64

    
65
/**
66
 * @brief Set number of bits per character.
67
 *
68
 * Valid numbers of bits per char go from 5 to 8.
69
 * @param   base_addr       Base address of serial port
70
 * @param   bits_per_char   Number of bits per char
71
 * @return                  SUCCESS if operation is successful, other value otherwise
72
 */
73
int uart_set_bits_per_character(int base_addr, uint8_t     bits_per_char);
74
/**
75
 * @brief Set number of stop bits.
76
 *
77
 * Valid numbers of stop bits are 1 and 2.
78
 * @param   base_addr   Base address of serial port
79
 * @param   stop        Number of stop bits
80
 * @return              SUCCESS if operation is successful, other value otherwise
81
 */
82
int uart_set_stop_bits         (int base_addr, uint8_t     stop         );
83
/**
84
 * @brief Set parity scheme to be used.
85
 * @param   base_addr   Base address of serial port
86
 * @param   par         Parity scheme
87
 * @return              SUCCESS if operation is successful, other value otherwise
88
 */
89
int uart_set_parity            (int base_addr, uart_parity par          );
90
/**
91
 * @brief Set bit rate.
92
 *
93
 * Valid numbers of bit rates go up to 115200 bps.
94
 * @param   base_addr       Base address of serial port
95
 * @param   bit_rate        Number of bits per second
96
 * @return                  SUCCESS if operation is successful, other value otherwise
97
 */
98
int uart_set_bit_rate          (int base_addr, uint32_t    bit_rate     );
99

    
100
/**
101
 * @brief Enable Receiver Register Ready interrupts
102
 * @param   base_addr   Base address of serial port
103
 * @return              SUCCESS if operation is successful, other value otherwise
104
 */
105
int uart_enable_int_rx (int base_addr);
106
/**
107
 * @brief Disable Receiver Register Ready interrupts
108
 * @param   base_addr   Base address of serial port
109
 * @return              SUCCESS if operation is successful, other value otherwise
110
 */
111
int uart_disable_int_rx(int base_addr);
112
/**
113
 * @brief Enable Transmitter Register Empty interrupts
114
 * @param   base_addr   Base address of serial port
115
 * @return              SUCCESS if operation is successful, other value otherwise
116
 */
117
int uart_enable_int_tx (int base_addr);
118
/**
119
 * @brief Disable Transmitter Register Empty interrupts
120
 * @param   base_addr   Base address of serial port
121
 * @return              SUCCESS if operation is successful, other value otherwise
122
 */
123
int uart_disable_int_tx(int base_addr);
124

    
125
/**
126
 * @}
127
 */
128

    
129
/**
130
* @defgroup nctp    nctp
131
* @ingroup uart
132
* @brief NCTP (Non-Critical Transfer Protocol) module.
133
* @{
134
*/
135

    
136
/**
137
 * @brief Initialize NCTP structures.
138
 * @return SUCCESS if operation is successful, other value otherwise
139
 */
140
int nctp_init(void);
141
/**
142
 * @brief Dump all the content of the NCTP in/out queues.
143
 * @return SUCCESS if operation is successful, other value otherwise
144
 */
145
int nctp_dump(void);
146
/**
147
 * @brief Set function to be called when a full message is received.
148
 * @return SUCCESS if operation is successful, other value otherwise
149
 */
150
int nctp_set_processor(void (*proc_func)(const uint8_t*, const size_t));
151
/**
152
 * @brief Free resources taken on NCTP initialization.
153
 * @return SUCCESS if operation is successful, other value otherwise
154
 */
155
int nctp_free(void);
156

    
157
/**
158
 * @brief Send a message throught serial port COM1
159
 * @param   num     Number of different blocks to send in the message
160
 * @param   ptr     Array of length num, with pointers to blocks that should be sent
161
 * @param   sz      Size of each block that should be sent, in bytes
162
 * @return SUCCESS if operation is successful, other value otherwise
163
 */
164
int nctp_send(size_t num, const uint8_t *const *ptr, const size_t *const sz);
165
/**
166
 * @brief Get NCTP interrupt handler error code.
167
 * @return  SUCCESS if previous interrupt handler call was successful, other value otherwise
168
 */
169
int (nctp_get_ih_error)(void);
170
/**
171
 * @brief NCTP interrupt handler.
172
 *
173
 * @note Must be called on all interrupt cycles. Even if not necessary, it must be
174
 * called so the NCTP is allowed to dump any messages it might receive, even
175
 * if those messages are unexpected.
176
 */
177
void nctp_ih(void);
178

    
179
/**
180
 * @}
181
 */
182

    
183
#endif //UART_H_INCLUDED