root / Version 1.2 / RS232_MUX.X / hardware_config.h @ master
Historique | Voir | Annoter | Télécharger (5,434 ko)
1 | 95a3914f | Enzo Niro | /*
|
---|---|---|---|
2 | * File: hardware_config.h
|
||
3 | * Author: Enzo Niro
|
||
4 | * Comments: Board configuration library for MUX232
|
||
5 | * Revision history: 1.0
|
||
6 | */
|
||
7 | |||
8 | // This is a guard condition so that contents of this file are not included
|
||
9 | // more than once.
|
||
10 | #ifndef HARDWARE_CFG_H
|
||
11 | #define HARDWARE_CFG_H
|
||
12 | |||
13 | #include <xc.h> // include processor files - each processor file is guarded. |
||
14 | #include "parameters.h" |
||
15 | |||
16 | |||
17 | |||
18 | |||
19 | typedef struct eepromPort eepromPort; |
||
20 | struct eepromPort
|
||
21 | { |
||
22 | uint8_t regA; // register A for speed and parity configuration
|
||
23 | uint8_t regB; // register B for Data structure and stop bits
|
||
24 | }; |
||
25 | |||
26 | typedef struct cfgPort cfgPort; |
||
27 | struct cfgPort
|
||
28 | { |
||
29 | uint16_t baud; // Port speed
|
||
30 | uint8_t parity; // Parity for frame structure
|
||
31 | uint8_t dataByte; // Data byte structure
|
||
32 | uint8_t stopBit; // Stop bits for frame structure
|
||
33 | uint8_t debugLed; |
||
34 | }; |
||
35 | |||
36 | //EEPROM data structure (for reading registers)
|
||
37 | eepromPort eepromVCOM[4], eepromDebugLeds;
|
||
38 | |||
39 | |||
40 | |||
41 | |||
42 | |||
43 | //Values registers
|
||
44 | |||
45 | #define F5BIT_MODE 0x00 |
||
46 | #define F6BIT_MODE 0x01 |
||
47 | #define F7BIT_MODE 0x02 |
||
48 | #define F8BIT_MODE 0x03 |
||
49 | |||
50 | #define AVR32_NO_PARITY 0x00 |
||
51 | #define AVR32_ODD_PARITY 0x01 //0x03 |
||
52 | #define AVR32_EVEN_PARITY 0x03 //0x02 |
||
53 | #define TL16C_NO_PARITY 0x00 |
||
54 | #define TL16C_ODD_PARITY 0x01 //0x03 |
||
55 | #define TL16C_EVEN_PARITY 0x03 //0x02 |
||
56 | |||
57 | |||
58 | #define ONE_STOPBIT 0x00 |
||
59 | #define TWO_STOPBITS 0x01 |
||
60 | |||
61 | |||
62 | |||
63 | |||
64 | |||
65 | |||
66 | ///////////////////////////
|
||
67 | //Divisor Latch values
|
||
68 | |||
69 | //According to 8MHz
|
||
70 | #ifdef TL16C_8MHZ_CLK
|
||
71 | |||
72 | #define DL_9600_BAUDS 52 |
||
73 | #define DL_19200_BAUDS 26 |
||
74 | #define DL_38400_BAUDS 13 |
||
75 | |||
76 | #else
|
||
77 | #ifdef TL16C_16MHZ_CLK
|
||
78 | |||
79 | #define DL_9600_BAUDS 104 |
||
80 | #define DL_19200_BAUDS 52 |
||
81 | #define DL_38400_BAUDS 26 |
||
82 | |||
83 | #else
|
||
84 | #ifdef TL16C_24MHZ_CLK
|
||
85 | |||
86 | #define DL_9600_BAUDS 156 |
||
87 | #define DL_19200_BAUDS 78 |
||
88 | #define DL_38400_BAUDS 39 |
||
89 | |||
90 | #else
|
||
91 | #error "Please choose a configuration for baud speed" |
||
92 | #define DL_9600_BAUDS 0x0 |
||
93 | #define DL_19200_BAUDS 0x0 |
||
94 | #define DL_38400_BAUDS 0x0 |
||
95 | #define CPU_FREQ_SET 0x0 |
||
96 | |||
97 | #endif //24MHZ |
||
98 | #endif //16MHZ |
||
99 | #endif //8MHZ |
||
100 | |||
101 | |||
102 | |||
103 | //EEPROM addresses
|
||
104 | |||
105 | enum ADDR_BAUDS
|
||
106 | { |
||
107 | ADDR_BAUD_9600 = 0,
|
||
108 | ADDR_BAUD_19200, |
||
109 | ADDR_BAUD_38400, |
||
110 | }; |
||
111 | |||
112 | enum ADDR_PARITY
|
||
113 | { |
||
114 | ADDR_PARITY_NONE = 0,
|
||
115 | ADDR_PARITY_EVEN, |
||
116 | ADDR_PARITY_ODD, |
||
117 | }; |
||
118 | |||
119 | enum ADDR_DATA
|
||
120 | { |
||
121 | ADDR_DATA_F5 = 0,
|
||
122 | ADDR_DATA_F6, |
||
123 | ADDR_DATA_F7, |
||
124 | ADDR_DATA_F8, |
||
125 | }; |
||
126 | |||
127 | enum ADDR_STOPBIT
|
||
128 | { |
||
129 | ADDR_STOP_ONE = 0,
|
||
130 | ADDR_STOP_TWO, |
||
131 | }; |
||
132 | |||
133 | #define DEFAULT_ADDR_BAUD ADDR_BAUD_9600
|
||
134 | #define DEFAULT_ADDR_PARITY ADDR_PARITY_NONE
|
||
135 | #define DEFAULT_ADDR_DATA ADDR_DATA_F8
|
||
136 | #define DEFAULT_ADDR_STOP ADDR_STOP_ONE
|
||
137 | #define DEFAULT_VALUE_DEBUG_LED 0x1F |
||
138 | |||
139 | #define EEPROM_DEBUG_LED_ADDR 0x8 |
||
140 | |||
141 | |||
142 | |||
143 | |||
144 | |||
145 | |||
146 | |||
147 | |||
148 | //////////////////////////////////////////////////
|
||
149 | //TODO Move theses functions out of this lib...
|
||
150 | void getEEPROMCfg(void); |
||
151 | uint8_t getDebugLedsConfiguration(void);
|
||
152 | void readConfiguration(cfgPort *cfg, uint8_t n);
|
||
153 | //////////////////////////////////////////////////
|
||
154 | |||
155 | |||
156 | |||
157 | |||
158 | |||
159 | |||
160 | |||
161 | |||
162 | |||
163 | |||
164 | /**
|
||
165 | <p><b>void getEEPROMCfg(void)</b></p>
|
||
166 | <p><b>Read eeprom values stored for all serial ports</b></p>
|
||
167 | */
|
||
168 | void getEEPROMCfg(void) |
||
169 | { |
||
170 | uint8_t i = 0, j = 0; |
||
171 | while(j < 8) |
||
172 | { |
||
173 | //Read at first bytes
|
||
174 | eepromVCOM[i].regA = eeprom_read_byte(j); |
||
175 | eepromVCOM[i].regB = eeprom_read_byte(j+1);
|
||
176 | //Then check if writing default values is necessary
|
||
177 | if(eepromVCOM[i].regA == 0xFF) |
||
178 | { |
||
179 | eepromVCOM[i].regA = (DEFAULT_ADDR_BAUD << 4) | (DEFAULT_ADDR_PARITY);
|
||
180 | eeprom_write_byte(j, (DEFAULT_ADDR_BAUD << 4) | (DEFAULT_ADDR_PARITY));
|
||
181 | } |
||
182 | if(eepromVCOM[i].regB == 0xFF) |
||
183 | { |
||
184 | eepromVCOM[i].regB = (DEFAULT_ADDR_DATA << 4) | (DEFAULT_ADDR_STOP);
|
||
185 | eeprom_write_byte(j+1, (DEFAULT_ADDR_DATA << 4) | (DEFAULT_ADDR_STOP)); |
||
186 | } |
||
187 | i++; |
||
188 | j+=2;
|
||
189 | } |
||
190 | |||
191 | //And finally, check debug leds value
|
||
192 | eepromDebugLeds.regA = eeprom_read_byte(EEPROM_DEBUG_LED_ADDR); |
||
193 | if(eepromDebugLeds.regA == 0xFF) |
||
194 | { |
||
195 | eepromDebugLeds.regA = DEFAULT_VALUE_DEBUG_LED; |
||
196 | eeprom_write_byte(EEPROM_DEBUG_LED_ADDR, DEFAULT_VALUE_DEBUG_LED); |
||
197 | } |
||
198 | |||
199 | |||
200 | } |
||
201 | |||
202 | /**
|
||
203 | <p><b>uint8_t getDebugLedsConfiguration(void)</b></p>
|
||
204 | <p><b>Get leds configuration for debug (TX/RX) of each serial port</b></p>
|
||
205 | <p><b> Return : uint8_t -> state of each leds [VCOM4 VCOM3 VCOM2 VCOM1 MCOM]</b></p>
|
||
206 | */
|
||
207 | uint8_t getDebugLedsConfiguration(void)
|
||
208 | { |
||
209 | return eepromDebugLeds.regA;
|
||
210 | } |
||
211 | |||
212 | |||
213 | |||
214 | |||
215 | /**
|
||
216 | <p><b>void readConfiguration(cfgPort *cfg, uint8_t n)</b></p>
|
||
217 | <p><b>Get configuration with eeprom read values</b></p>
|
||
218 | <p><b> cfgPort -> Serial port configuration</b></p>
|
||
219 | <p><b> n -> Serial port to configure</b></p>
|
||
220 | */
|
||
221 | void readConfiguration(cfgPort *cfg, uint8_t n)
|
||
222 | { |
||
223 | //BAUD value register for these frequencies in this order :
|
||
224 | //2400, 4800, 9600, 19200, 38400, 57600, 115200
|
||
225 | uint16_t speedCfg[] = { DL_9600_BAUDS, DL_19200_BAUDS, DL_38400_BAUDS }; |
||
226 | uint8_t parityCfg[] = { TL16C_NO_PARITY, TL16C_EVEN_PARITY, TL16C_ODD_PARITY }; |
||
227 | uint8_t dataCfg[] = { F5BIT_MODE, F6BIT_MODE, F7BIT_MODE, F8BIT_MODE }; |
||
228 | uint8_t stopCfg[] = { ONE_STOPBIT, TWO_STOPBITS }; |
||
229 | |||
230 | cfg->baud = speedCfg[(eepromVCOM[n].regA >> 4)];
|
||
231 | cfg->parity = parityCfg[(eepromVCOM[n].regA & 0xF)];
|
||
232 | cfg->dataByte = dataCfg[(eepromVCOM[n].regB >> 4)];
|
||
233 | cfg->stopBit = stopCfg[(eepromVCOM[n].regB & 0xF)];
|
||
234 | } |
||
235 | |||
236 | |||
237 | #endif /* XC_HEADER_TEMPLATE_H */ |