root / Version 1.5 / RS232_MUX.X / hardware_config.h @ f0dbe1d9
Historique | Voir | Annoter | Télécharger (6,665 ko)
1 | f0dbe1d9 | 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 | }; |
||
34 | |||
35 | //EEPROM data structure (for reading registers)
|
||
36 | eepromPort eepromVCOM[4];
|
||
37 | |||
38 | |||
39 | |||
40 | |||
41 | |||
42 | //Values registers
|
||
43 | |||
44 | #define F5BIT_MODE 0x00 |
||
45 | #define F6BIT_MODE 0x01 |
||
46 | #define F7BIT_MODE 0x02 |
||
47 | #define F8BIT_MODE 0x03 |
||
48 | |||
49 | #define AVR32_NO_PARITY 0x00 |
||
50 | #define AVR32_ODD_PARITY 0x01 //0x03 |
||
51 | #define AVR32_EVEN_PARITY 0x03 //0x02 |
||
52 | #define TL16C_NO_PARITY 0x00 |
||
53 | #define TL16C_ODD_PARITY 0x01 //0x03 |
||
54 | #define TL16C_EVEN_PARITY 0x03 //0x02 |
||
55 | |||
56 | |||
57 | #define ONE_STOPBIT 0x00 |
||
58 | #define TWO_STOPBITS 0x01 |
||
59 | |||
60 | |||
61 | |||
62 | |||
63 | |||
64 | |||
65 | ///////////////////////////
|
||
66 | //Divisor Latch values
|
||
67 | |||
68 | //According to 8MHz
|
||
69 | #ifdef TL16C_8MHZ_CLK
|
||
70 | |||
71 | #define DL_9600_BAUDS 52 |
||
72 | #define DL_19200_BAUDS 26 |
||
73 | #define DL_38400_BAUDS 13 |
||
74 | |||
75 | #else
|
||
76 | #ifdef TL16C_16MHZ_CLK
|
||
77 | |||
78 | #define DL_9600_BAUDS 104 |
||
79 | #define DL_19200_BAUDS 52 |
||
80 | #define DL_38400_BAUDS 26 |
||
81 | |||
82 | #else
|
||
83 | #ifdef TL16C_24MHZ_CLK
|
||
84 | |||
85 | #define DL_9600_BAUDS 156 |
||
86 | #define DL_19200_BAUDS 78 |
||
87 | #define DL_38400_BAUDS 39 |
||
88 | |||
89 | #else
|
||
90 | #error "Please choose a configuration for baud speed" |
||
91 | #define DL_9600_BAUDS 0x0 |
||
92 | #define DL_19200_BAUDS 0x0 |
||
93 | #define DL_38400_BAUDS 0x0 |
||
94 | #define CPU_FREQ_SET 0x0 |
||
95 | |||
96 | #endif //24MHZ |
||
97 | #endif //16MHZ |
||
98 | #endif //8MHZ |
||
99 | |||
100 | |||
101 | |||
102 | //EEPROM addresses
|
||
103 | |||
104 | enum ADDR_BAUDS
|
||
105 | { |
||
106 | ADDR_BAUD_9600 = 0,
|
||
107 | ADDR_BAUD_19200, |
||
108 | ADDR_BAUD_38400, |
||
109 | }; |
||
110 | |||
111 | enum ADDR_PARITY
|
||
112 | { |
||
113 | ADDR_PARITY_NONE = 0,
|
||
114 | ADDR_PARITY_EVEN, |
||
115 | ADDR_PARITY_ODD, |
||
116 | }; |
||
117 | |||
118 | enum ADDR_DATA
|
||
119 | { |
||
120 | ADDR_DATA_F5 = 0,
|
||
121 | ADDR_DATA_F6, |
||
122 | ADDR_DATA_F7, |
||
123 | ADDR_DATA_F8, |
||
124 | }; |
||
125 | |||
126 | enum ADDR_STOPBIT
|
||
127 | { |
||
128 | ADDR_STOP_ONE = 0,
|
||
129 | ADDR_STOP_TWO, |
||
130 | }; |
||
131 | |||
132 | #define DEFAULT_ADDR_BAUD ADDR_BAUD_9600
|
||
133 | #define DEFAULT_ADDR_PARITY ADDR_PARITY_NONE
|
||
134 | #define DEFAULT_ADDR_DATA ADDR_DATA_F8
|
||
135 | #define DEFAULT_ADDR_STOP ADDR_STOP_ONE
|
||
136 | #define DEFAULT_VALUE_DEBUG_LED 0x1F |
||
137 | |||
138 | #define EEPROM_DEBUG_LED_ADDR 0x8 |
||
139 | |||
140 | |||
141 | |||
142 | |||
143 | |||
144 | |||
145 | |||
146 | |||
147 | //////////////////////////////////////////////////
|
||
148 | //EEPROM/CFG function
|
||
149 | void getEEPROMCfg(void); |
||
150 | void setEEPROMCfg(void); |
||
151 | void writeConfiguration(cfgPort *cfg, uint8_t bauds, uint8_t parity, uint8_t dataMode, uint8_t stopBit, uint8_t n);
|
||
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 | |||
192 | |||
193 | /**
|
||
194 | <p><b>void getEEPROMCfg(void)</b></p>
|
||
195 | <p><b>Write eeprom values stored for all serial ports</b></p>
|
||
196 | */
|
||
197 | void setEEPROMCfg(void) |
||
198 | { |
||
199 | uint8_t i = 0, j = 0; |
||
200 | //Write EEPROM cfg
|
||
201 | while(j < 8) |
||
202 | { |
||
203 | //Read at first bytes
|
||
204 | eeprom_write_byte(j, eepromVCOM[i].regA); |
||
205 | eeprom_write_byte(j+1, eepromVCOM[i].regB);
|
||
206 | i++; //next cfg
|
||
207 | j+=2; //next address |
||
208 | } |
||
209 | |||
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 | |||
238 | |||
239 | |||
240 | /**
|
||
241 | <p><b>void readConfiguration(cfgPort *cfg, uint8_t n)</b></p>
|
||
242 | <p><b>Get configuration with eeprom read values</b></p>
|
||
243 | <p><b> cfgPort -> Serial port configuration</b></p>
|
||
244 | <p><b> bauds -> Set bauds speed (9600 / 19200 / 38400)</b></p>
|
||
245 | <p><b> parity -> Set parity Mode (NO_PARITY / EVEN_PARITY / ODD_PARITY)</b></p>
|
||
246 | <p><b> dataMode -> Set number of data bits into frame (5/6/7/8)</b></p>
|
||
247 | <p><b> stopBit -> Set number of stop bits (1/2)</b></p>
|
||
248 | */
|
||
249 | void writeConfiguration(cfgPort *cfg, uint8_t bauds, uint8_t parity, uint8_t dataMode, uint8_t stopBit, uint8_t n)
|
||
250 | { |
||
251 | //BAUD value register for these frequencies in this order :
|
||
252 | //2400, 4800, 9600, 19200, 38400, 57600, 115200
|
||
253 | uint16_t speedCfg[] = { DL_9600_BAUDS, DL_19200_BAUDS, DL_38400_BAUDS }; |
||
254 | uint8_t parityCfg[] = { TL16C_NO_PARITY, TL16C_EVEN_PARITY, TL16C_ODD_PARITY }; |
||
255 | uint8_t dataCfg[] = { F5BIT_MODE, F6BIT_MODE, F7BIT_MODE, F8BIT_MODE }; |
||
256 | uint8_t stopCfg[] = { ONE_STOPBIT, TWO_STOPBITS }; |
||
257 | |||
258 | //Set CFG
|
||
259 | cfg->baud = speedCfg[bauds > 2 ? 2 : bauds]; |
||
260 | cfg->parity = parityCfg[parity > 2 ? 2 : parity]; |
||
261 | cfg->dataByte = dataCfg[dataMode > 3 ? 3 : dataMode]; |
||
262 | cfg->stopBit = stopCfg[stopBit > 1 ? 1 : stopBit]; |
||
263 | |||
264 | //Prepare buffer into RAM
|
||
265 | eepromVCOM[n].regA = (cfg->baud << 4) | cfg->parity;
|
||
266 | eepromVCOM[n].regB = (cfg->dataByte << 4) | cfg->stopBit;
|
||
267 | |||
268 | } |
||
269 | |||
270 | |||
271 | #endif /* XC_HEADER_TEMPLATE_H */ |