root / Version 1.8 / RS232_MUX.X / hardware_config.h @ c82771d8
Historique | Voir | Annoter | Télécharger (6,422 ko)
1 | c82771d8 | Enzo Niro | /*
|
---|---|---|---|
2 | * File: hardware_config.h
|
||
3 | * Author: Enzo Niro
|
||
4 | * Comments: Board configuration library for MUX232
|
||
5 | * Revision history: 1.6
|
||
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 | eeprom_busy_wait(); |
||
176 | eepromVCOM[i].regB = eeprom_read_byte(j+1);
|
||
177 | eeprom_busy_wait(); |
||
178 | //Then check if writing default values is necessary
|
||
179 | if(eepromVCOM[i].regA == 0xFF) |
||
180 | { |
||
181 | eepromVCOM[i].regA = (DEFAULT_ADDR_BAUD << 4) | (DEFAULT_ADDR_PARITY);
|
||
182 | eeprom_write_byte(j, (DEFAULT_ADDR_BAUD << 4) | (DEFAULT_ADDR_PARITY));
|
||
183 | } |
||
184 | if(eepromVCOM[i].regB == 0xFF) |
||
185 | { |
||
186 | eepromVCOM[i].regB = (DEFAULT_ADDR_DATA << 4) | (DEFAULT_ADDR_STOP);
|
||
187 | eeprom_write_byte(j+1, (DEFAULT_ADDR_DATA << 4) | (DEFAULT_ADDR_STOP)); |
||
188 | } |
||
189 | i++; |
||
190 | j+=2;
|
||
191 | } |
||
192 | } |
||
193 | |||
194 | |||
195 | /**
|
||
196 | <p><b>void getEEPROMCfg(void)</b></p>
|
||
197 | <p><b>Write eeprom values stored for all serial ports</b></p>
|
||
198 | */
|
||
199 | void setEEPROMCfg(void) |
||
200 | { |
||
201 | uint8_t i = 0, j = 0; |
||
202 | //Write EEPROM cfg
|
||
203 | while(j < 8) |
||
204 | { |
||
205 | //Read at first bytes
|
||
206 | eeprom_write_byte(j, eepromVCOM[i].regA); |
||
207 | eeprom_busy_wait(); |
||
208 | eeprom_write_byte(j+1, eepromVCOM[i].regB);
|
||
209 | eeprom_busy_wait(); |
||
210 | i++; //next cfg
|
||
211 | j+=2; //next address |
||
212 | } |
||
213 | |||
214 | } |
||
215 | |||
216 | |||
217 | |||
218 | |||
219 | /**
|
||
220 | <p><b>void readConfiguration(cfgPort *cfg, uint8_t n)</b></p>
|
||
221 | <p><b>Get configuration with eeprom read values</b></p>
|
||
222 | <p><b> cfgPort -> Serial port configuration</b></p>
|
||
223 | <p><b> n -> Serial port to configure</b></p>
|
||
224 | */
|
||
225 | void readConfiguration(cfgPort *cfg, uint8_t n)
|
||
226 | { |
||
227 | //BAUD value register for these frequencies in this order :
|
||
228 | //2400, 4800, 9600, 19200, 38400, 57600, 115200
|
||
229 | /*uint16_t speedCfg[] = { DL_9600_BAUDS, DL_19200_BAUDS, DL_38400_BAUDS };
|
||
230 | uint8_t parityCfg[] = { TL16C_NO_PARITY, TL16C_EVEN_PARITY, TL16C_ODD_PARITY };
|
||
231 | uint8_t dataCfg[] = { F5BIT_MODE, F6BIT_MODE, F7BIT_MODE, F8BIT_MODE };
|
||
232 | uint8_t stopCfg[] = { ONE_STOPBIT, TWO_STOPBITS };*/
|
||
233 | |||
234 | cfg->baud = (eepromVCOM[n].regA >> 4);
|
||
235 | cfg->parity = (eepromVCOM[n].regA & 0xF);
|
||
236 | cfg->dataByte = (eepromVCOM[n].regB >> 4);
|
||
237 | cfg->stopBit = (eepromVCOM[n].regB & 0xF);
|
||
238 | } |
||
239 | |||
240 | |||
241 | |||
242 | |||
243 | |||
244 | /**
|
||
245 | <p><b>void readConfiguration(cfgPort *cfg, uint8_t n)</b></p>
|
||
246 | <p><b>Get configuration with eeprom read values</b></p>
|
||
247 | <p><b> cfgPort -> Serial port configuration</b></p>
|
||
248 | <p><b> bauds -> Set bauds speed (9600 / 19200 / 38400)</b></p>
|
||
249 | <p><b> parity -> Set parity Mode (NO_PARITY / EVEN_PARITY / ODD_PARITY)</b></p>
|
||
250 | <p><b> dataMode -> Set number of data bits into frame (5/6/7/8)</b></p>
|
||
251 | <p><b> stopBit -> Set number of stop bits (1/2)</b></p>
|
||
252 | */
|
||
253 | void writeConfiguration(cfgPort *cfg, uint8_t bauds, uint8_t parity, uint8_t dataMode, uint8_t stopBit, uint8_t n)
|
||
254 | { |
||
255 | //BAUD value register for these frequencies in this order :
|
||
256 | //2400, 4800, 9600, 19200, 38400, 57600, 115200
|
||
257 | |||
258 | |||
259 | //Set CFG
|
||
260 | cfg->baud = bauds > 2 ? 2 : bauds; |
||
261 | cfg->parity = parity > 2 ? 2 : parity; |
||
262 | cfg->dataByte = dataMode > 3 ? 3 : dataMode; |
||
263 | cfg->stopBit = stopBit > 1 ? 1 : stopBit; |
||
264 | |||
265 | //Prepare buffer into RAM
|
||
266 | eepromVCOM[n].regA = (cfg->baud << 4) | cfg->parity;
|
||
267 | eepromVCOM[n].regB = (cfg->dataByte << 4) | cfg->stopBit;
|
||
268 | |||
269 | } |
||
270 | |||
271 | |||
272 | #endif /* XC_HEADER_TEMPLATE_H */ |