root / Version 2.0 / RS232_MUX.X / hardware_config.h @ 8d73eb8a
Historique | Voir | Annoter | Télécharger (6,928 ko)
1 |
/*
|
---|---|
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 |
#include <avr/io.h> |
16 |
|
17 |
FUSES = { |
18 |
.WDTCFG = 0x0A, // WDTCFG {PERIOD=4KCLK, WINDOW=OFF} |
19 |
.BODCFG = 0x00, // BODCFG {SLEEP=DISABLE, ACTIVE=DISABLE, SAMPFREQ=128Hz, LVL=BODLEVEL0} |
20 |
.OSCCFG = 0x00, // OSCCFG {CLKSEL=OSCHF} |
21 |
.SYSCFG0 = 0xC0, // SYSCFG0 {EESAVE=CLEAR, RSTPINCFG=GPIO, CRCSEL=CRC16, CRCSRC=NOCRC} |
22 |
.SYSCFG1 = 0x00, // SYSCFG1 {SUT=0MS} |
23 |
.CODESIZE = 0x00, // CODESIZE {CODESIZE=User range: 0x0 - 0xFF} |
24 |
.BOOTSIZE = 0x00, // BOOTSIZE {BOOTSIZE=User range: 0x0 - 0xFF} |
25 |
}; |
26 |
|
27 |
LOCKBITS = 0x5CC5C55C; // {KEY=NOLOCK} |
28 |
|
29 |
|
30 |
|
31 |
typedef struct eepromPort eepromPort; |
32 |
struct eepromPort
|
33 |
{ |
34 |
uint8_t regA; // register A for speed and parity configuration
|
35 |
uint8_t regB; // register B for Data structure and stop bits
|
36 |
}; |
37 |
|
38 |
typedef struct cfgPort cfgPort; |
39 |
struct cfgPort
|
40 |
{ |
41 |
uint16_t baud; // Port speed
|
42 |
uint8_t parity; // Parity for frame structure
|
43 |
uint8_t dataByte; // Data byte structure
|
44 |
uint8_t stopBit; // Stop bits for frame structure
|
45 |
}; |
46 |
|
47 |
//EEPROM data structure (for reading registers)
|
48 |
eepromPort eepromVCOM[4];
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
//Values registers
|
55 |
|
56 |
#define F5BIT_MODE 0x00 |
57 |
#define F6BIT_MODE 0x01 |
58 |
#define F7BIT_MODE 0x02 |
59 |
#define F8BIT_MODE 0x03 |
60 |
|
61 |
#define AVR32_NO_PARITY 0x00 |
62 |
#define AVR32_ODD_PARITY 0x01 //0x03 |
63 |
#define AVR32_EVEN_PARITY 0x03 //0x02 |
64 |
#define TL16C_NO_PARITY 0x00 |
65 |
#define TL16C_ODD_PARITY 0x01 //0x03 |
66 |
#define TL16C_EVEN_PARITY 0x03 //0x02 |
67 |
|
68 |
|
69 |
#define ONE_STOPBIT 0x00 |
70 |
#define TWO_STOPBITS 0x01 |
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
///////////////////////////
|
78 |
//Divisor Latch values
|
79 |
|
80 |
//According to 8MHz
|
81 |
#ifdef TL16C_8MHZ_CLK
|
82 |
|
83 |
#define DL_9600_BAUDS 52 |
84 |
#define DL_19200_BAUDS 26 |
85 |
#define DL_38400_BAUDS 13 |
86 |
|
87 |
#else
|
88 |
#ifdef TL16C_16MHZ_CLK
|
89 |
|
90 |
#define DL_9600_BAUDS 104 |
91 |
#define DL_19200_BAUDS 52 |
92 |
#define DL_38400_BAUDS 26 |
93 |
|
94 |
#else
|
95 |
#ifdef TL16C_24MHZ_CLK
|
96 |
|
97 |
#define DL_9600_BAUDS 156 |
98 |
#define DL_19200_BAUDS 78 |
99 |
#define DL_38400_BAUDS 39 |
100 |
|
101 |
#else
|
102 |
#error "Please choose a configuration for baud speed" |
103 |
#define DL_9600_BAUDS 0x0 |
104 |
#define DL_19200_BAUDS 0x0 |
105 |
#define DL_38400_BAUDS 0x0 |
106 |
#define CPU_FREQ_SET 0x0 |
107 |
|
108 |
#endif //24MHZ |
109 |
#endif //16MHZ |
110 |
#endif //8MHZ |
111 |
|
112 |
|
113 |
|
114 |
//EEPROM addresses
|
115 |
|
116 |
enum ADDR_BAUDS
|
117 |
{ |
118 |
ADDR_BAUD_9600 = 0,
|
119 |
ADDR_BAUD_19200, |
120 |
ADDR_BAUD_38400, |
121 |
}; |
122 |
|
123 |
enum ADDR_PARITY
|
124 |
{ |
125 |
ADDR_PARITY_NONE = 0,
|
126 |
ADDR_PARITY_EVEN, |
127 |
ADDR_PARITY_ODD, |
128 |
}; |
129 |
|
130 |
enum ADDR_DATA
|
131 |
{ |
132 |
ADDR_DATA_F5 = 0,
|
133 |
ADDR_DATA_F6, |
134 |
ADDR_DATA_F7, |
135 |
ADDR_DATA_F8, |
136 |
}; |
137 |
|
138 |
enum ADDR_STOPBIT
|
139 |
{ |
140 |
ADDR_STOP_ONE = 0,
|
141 |
ADDR_STOP_TWO, |
142 |
}; |
143 |
|
144 |
#define DEFAULT_ADDR_BAUD ADDR_BAUD_9600
|
145 |
#define DEFAULT_ADDR_PARITY ADDR_PARITY_NONE
|
146 |
#define DEFAULT_ADDR_DATA ADDR_DATA_F8
|
147 |
#define DEFAULT_ADDR_STOP ADDR_STOP_ONE
|
148 |
#define DEFAULT_VALUE_DEBUG_LED 0x1F |
149 |
|
150 |
#define EEPROM_DEBUG_LED_ADDR 0x8 |
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
//////////////////////////////////////////////////
|
160 |
//EEPROM/CFG function
|
161 |
void getEEPROMCfg(void); |
162 |
void setEEPROMCfg(void); |
163 |
void writeConfiguration(cfgPort *cfg, uint8_t bauds, uint8_t parity, uint8_t dataMode, uint8_t stopBit, uint8_t n);
|
164 |
void readConfiguration(cfgPort *cfg, uint8_t n);
|
165 |
//////////////////////////////////////////////////
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
/**
|
177 |
<p><b>void getEEPROMCfg(void)</b></p>
|
178 |
<p><b>Read eeprom values stored for all serial ports</b></p>
|
179 |
*/
|
180 |
void getEEPROMCfg(void) |
181 |
{ |
182 |
uint8_t i = 0, j = 0; |
183 |
while(j < 8) |
184 |
{ |
185 |
//Read at first bytes
|
186 |
eepromVCOM[i].regA = eeprom_read_byte(j); |
187 |
eeprom_busy_wait(); |
188 |
eepromVCOM[i].regB = eeprom_read_byte(j+1);
|
189 |
eeprom_busy_wait(); |
190 |
//Then check if writing default values is necessary
|
191 |
if(eepromVCOM[i].regA == 0xFF) |
192 |
{ |
193 |
eepromVCOM[i].regA = (DEFAULT_ADDR_BAUD << 4) | (DEFAULT_ADDR_PARITY);
|
194 |
eeprom_write_byte(j, (DEFAULT_ADDR_BAUD << 4) | (DEFAULT_ADDR_PARITY));
|
195 |
} |
196 |
if(eepromVCOM[i].regB == 0xFF) |
197 |
{ |
198 |
eepromVCOM[i].regB = (DEFAULT_ADDR_DATA << 4) | (DEFAULT_ADDR_STOP);
|
199 |
eeprom_write_byte(j+1, (DEFAULT_ADDR_DATA << 4) | (DEFAULT_ADDR_STOP)); |
200 |
} |
201 |
i++; |
202 |
j+=2;
|
203 |
} |
204 |
} |
205 |
|
206 |
|
207 |
/**
|
208 |
<p><b>void getEEPROMCfg(void)</b></p>
|
209 |
<p><b>Write eeprom values stored for all serial ports</b></p>
|
210 |
*/
|
211 |
void setEEPROMCfg(void) |
212 |
{ |
213 |
uint8_t i = 0, j = 0; |
214 |
//Write EEPROM cfg
|
215 |
while(j < 8) |
216 |
{ |
217 |
//Read at first bytes
|
218 |
eeprom_write_byte(j, eepromVCOM[i].regA); |
219 |
eeprom_busy_wait(); |
220 |
eeprom_write_byte(j+1, eepromVCOM[i].regB);
|
221 |
eeprom_busy_wait(); |
222 |
i++; //next cfg
|
223 |
j+=2; //next address |
224 |
} |
225 |
|
226 |
} |
227 |
|
228 |
|
229 |
|
230 |
|
231 |
/**
|
232 |
<p><b>void readConfiguration(cfgPort *cfg, uint8_t n)</b></p>
|
233 |
<p><b>Get configuration with eeprom read values</b></p>
|
234 |
<p><b> cfgPort -> Serial port configuration</b></p>
|
235 |
<p><b> n -> Serial port to configure</b></p>
|
236 |
*/
|
237 |
void readConfiguration(cfgPort *cfg, uint8_t n)
|
238 |
{ |
239 |
//BAUD value register for these frequencies in this order :
|
240 |
//2400, 4800, 9600, 19200, 38400, 57600, 115200
|
241 |
/*uint16_t speedCfg[] = { DL_9600_BAUDS, DL_19200_BAUDS, DL_38400_BAUDS };
|
242 |
uint8_t parityCfg[] = { TL16C_NO_PARITY, TL16C_EVEN_PARITY, TL16C_ODD_PARITY };
|
243 |
uint8_t dataCfg[] = { F5BIT_MODE, F6BIT_MODE, F7BIT_MODE, F8BIT_MODE };
|
244 |
uint8_t stopCfg[] = { ONE_STOPBIT, TWO_STOPBITS };*/
|
245 |
|
246 |
cfg->baud = (eepromVCOM[n].regA >> 4);
|
247 |
cfg->parity = (eepromVCOM[n].regA & 0xF);
|
248 |
cfg->dataByte = (eepromVCOM[n].regB >> 4);
|
249 |
cfg->stopBit = (eepromVCOM[n].regB & 0xF);
|
250 |
} |
251 |
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
/**
|
257 |
<p><b>void readConfiguration(cfgPort *cfg, uint8_t n)</b></p>
|
258 |
<p><b>Get configuration with eeprom read values</b></p>
|
259 |
<p><b> cfgPort -> Serial port configuration</b></p>
|
260 |
<p><b> bauds -> Set bauds speed (9600 / 19200 / 38400)</b></p>
|
261 |
<p><b> parity -> Set parity Mode (NO_PARITY / EVEN_PARITY / ODD_PARITY)</b></p>
|
262 |
<p><b> dataMode -> Set number of data bits into frame (5/6/7/8)</b></p>
|
263 |
<p><b> stopBit -> Set number of stop bits (1/2)</b></p>
|
264 |
*/
|
265 |
void writeConfiguration(cfgPort *cfg, uint8_t bauds, uint8_t parity, uint8_t dataMode, uint8_t stopBit, uint8_t n)
|
266 |
{ |
267 |
//BAUD value register for these frequencies in this order :
|
268 |
//2400, 4800, 9600, 19200, 38400, 57600, 115200
|
269 |
|
270 |
|
271 |
//Set CFG
|
272 |
cfg->baud = bauds > 2 ? 2 : bauds; |
273 |
cfg->parity = parity > 2 ? 2 : parity; |
274 |
cfg->dataByte = dataMode > 3 ? 3 : dataMode; |
275 |
cfg->stopBit = stopBit > 1 ? 1 : stopBit; |
276 |
|
277 |
//Prepare buffer into RAM
|
278 |
eepromVCOM[n].regA = (cfg->baud << 4) | cfg->parity;
|
279 |
eepromVCOM[n].regB = (cfg->dataByte << 4) | cfg->stopBit;
|
280 |
|
281 |
} |
282 |
|
283 |
|
284 |
#endif /* XC_HEADER_TEMPLATE_H */ |
285 |
|