Blame view

RIOT/cpu/x86/include/x86_uart.h 6.05 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  /*
   * Copyright (C) 2014  René Kijewski  <rene.kijewski@fu-berlin.de>
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
   *
   * This library is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this library; if not, write to the Free Software
   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
   */
  
  /**
   * Using Communations Port 0 (COM0) to interface with a human being on x86 boards.
   *
   * @ingroup x86-irq
   * @{
   * @file
   * @author  René Kijewski <rene.kijewski@fu-berlin.de>
   */
  
  #ifndef CPU__X86__UART__H__
  #define CPU__X86__UART__H__
  
  #include "x86_pic.h"
  
  #include <stdio.h>
  #include <stdint.h>
  #include <stdlib.h>
  #include <unistd.h>
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  /**
   * @brief   Early initialization of the UART system, before there are interrupts.
   *
   * This function is called during initialization by x86_startup().
   * You must not call this function on your own accord.
   */
  void x86_early_init_uart(void);
  
  /**
   * @brief   Full initialization of the Universal Asynchronous Receiver Transmitter.
   *
   * This function is called during initialization by x86_startup().
   * You must not call this function on your own accord.
   */
  void x86_init_uart(void);
  
  /**
   * @brief       Write out characters to the UART.
   * @param[in]   buf   Buffer to write.
   * @param       len   Length of the buffer.
   * @returns     Actual amount of bytes written, always the same as len.
   *
   * This function blocks with interrupts disabled once the output buffer is full.
   */
  ssize_t x86_uart_write(const char *buf, size_t len);
  
  /**
   * @brief        Read in characters to the UART.
   * @param[out]   buf   Buffer to set.
   * @param        len   Length of the buffer.
   * @returns      Actual amount of bytes read, always the same as len.
   *
   * This function blocks with interrupts disabled once the input buffer is full.
   */
  ssize_t x86_uart_read(char *buf, size_t len);
  
  #define COM1_PORT (0x03F8)
  #define COM2_PORT (0x02F8)
  #define COM3_PORT (0x03E8)
  #define COM4_PORT (0x02E8)
  
  #define COM1_IRQ (PIC_NUM_RS232_1_3)
  #define COM2_IRQ (PIC_NUM_RS232_2_4)
  #define COM3_IRQ (PIC_NUM_RS232_1_3)
  #define COM4_IRQ (PIC_NUM_RS232_2_4)
  
  /* 115200 baud */
  #define BAUD_LL (0x01)
  #define BAUD_HL (0x00)
  
  enum uart_port_offs_t {
      /*          DLAB RW */
      THR = 0, /*   0   W  Transmitter Holding Buffer */
      RBR = 0, /*   0  R   Receiver Buffer */
      DLL = 0, /*   1  RW  Divisor Latch Low Byte */
      IER = 1, /*   0  RW  Interrupt Enable Register */
      DLH = 1, /*   1  RW  Divisor Latch High Byte */
      IIR = 2, /*   -  R   Interrupt Identification Register */
      FCR = 2, /*   -  RW  FIFO Control Register */
      LCR = 3, /*   -  RW  Line Control Register */
      MCR = 4, /*   -  RW  Modem Control Register */
      LSR = 5, /*   -  R   Line Status Register */
      MSR = 6, /*   -  R   Modem Status Register */
      SR  = 7, /*   -  RW  Scratch Register */
  };
  
  enum ier_t {
      IER_RECV  = 1 << 0, /* Enable Received Data Available Interrupt */
      IER_SEND  = 1 << 1, /* Enable Transmitter Holding Register Empty Interrupt */
      IER_LS    = 1 << 2, /* Enable Receiver Line Status Interrupt */
      IER_MS    = 1 << 3, /* Enable Modem Status Interrupt */
      IER_SLEEP = 1 << 4, /* Enables Sleep Mode (16750) */
      IER_LPM   = 1 << 5, /* Enables Low Power Mode (16750) */
  };
  
  enum fcr_t {
      FCR_ENABLE     = 1 << 0, /* Enable FIFOs */
      FCR_CLR_RECV   = 1 << 1, /* Clear Receive FIFO */
      FCR_CLR_SEND   = 1 << 2, /* Clear Transmit FIFO */
      FCR_MDA        = 1 << 3, /* DMA Mode Select */
      FCR_64BYTE     = 1 << 5, /* Enable 64 Byte FIFO (16750) */
  
      FCR_TRIGGER_1  = 0 << 6, /* 1 byte */
      FCR_TRIGGER_16 = 1 << 6, /* 16 bytes */
      FCR_TRIGGER_32 = 2 << 6, /* 32 bytes */
      FCR_TRIGGER_56 = 3 << 6, /* 56 bytes */
  };
  
  enum lcr_t {
      LCR_WORD_BITS_5 = (0) << 0, /* Word length: 5 bits */
      LCR_WORD_BITS_6 = (1) << 0, /* Word length: 6 bits */
      LCR_WORD_BITS_7 = (2) << 0, /* Word length: 7 bits */
      LCR_WORD_BITS_8 = (3) << 0, /* Word length: 8 bits */
  
      LCR_STOP_BITS_1 = (0) << 2, /* Stop bits: 1 */
      LCR_STOP_BITS_2 = (1) << 2, /* Stop bits: 1.5 or 2 */
  
      LCR_PAR_NONE    = (0) << 3, /* no parity */
      LCR_PAR_ODD     = (1) << 3, /* odd parity */
      LCR_PAR_EVEN    = (3) << 3, /* even parity */
      LCR_PAR_MARK    = (5) << 3, /* mark (?) */
      LCR_PAR_SPACE   = (7) << 3, /* space (?) */
  
      LCR_SET_BREAK   = (1) << 6, /* set break enabled */
  
      LCR_DLAB        = (1) << 7, /* divisor latch access bit */
  };
  
  enum mcr_t {
      MCR_DSR      = 1 << 0, /* Data Terminal Ready */
      MCR_RTS      = 1 << 1, /* Request To Send */
      MCR_AUX1     = 1 << 2, /* Auxiliary Output 1 */
      MCR_AUX2     = 1 << 3, /* Auxiliary Output 2 */
      MCR_LOOPBACK = 1 << 4, /* Loopback Mode */
      MCR_AUTOFLOW = 1 << 5, /* Autoflow Control Enabled (16750) */
  };
  
  enum iir_t {
      IIR_IPF          = 1 << 0, /* interrupt pending flag */
  
      IIR_INT_MASK     = 3 << 1,
      IIR_INT_MS       = 0 << 1, /* modem status interrupt (reset: read MSR) */
      IIR_INT_TH       = 1 << 1, /* THR empty interrupt (reset: read IIR or write to THR) */
      IIR_INT_BR       = 2 << 1, /* recv data available interrupt (reset: read RBR) */
      IIR_INT_LS       = 3 << 1, /* line status changed (reset: read LSR) */
      IIR_INT_TO       = 6 << 1, /* timeout interrupt pending (reset: read RBR) */
  
      IIR_FIFO64       = 1 << 5, /* 64 byte FIFO enabled */
  
      IIR_FIFO_MASK    = 3 << 6,
      IIR_FIFO_NONE    = 0 << 6, /* no FIFO available */
      IIR_FIFO_RES     = 1 << 6, /* reserved condition */
      IIR_FIFO_MAL     = 2 << 6, /* FIFO enabled, not working */
      IIR_FIFO_ENABLED = 3 << 6, /* FIFO enabled */
  };
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif
  
  /** @} */