root / Version 1.8 / RS232_MUX.X / main.c @ c82771d8
Historique | Voir | Annoter | Télécharger (31,187 ko)
1 | c82771d8 | Enzo Niro | /*
|
---|---|---|---|
2 | * File: main.c
|
||
3 | * Author: eniro
|
||
4 | * Version : 1.8
|
||
5 | *
|
||
6 | * Created on March 25, 2024, 3:51 PM
|
||
7 | */
|
||
8 | |||
9 | |||
10 | |||
11 | /*
|
||
12 |
|
||
13 | TODO LIST :
|
||
14 | *
|
||
15 | */
|
||
16 | |||
17 | #define F_CPU 24000000UL |
||
18 | |||
19 | #include <xc.h> |
||
20 | #include <stdio.h> |
||
21 | #include <string.h> |
||
22 | #include <math.h> |
||
23 | #include <stdlib.h> |
||
24 | #include <util/delay.h> |
||
25 | #include <avr/interrupt.h> |
||
26 | #include <avr/eeprom.h> |
||
27 | |||
28 | #include "hardware_uart.h" |
||
29 | #include "hardware_timer.h" |
||
30 | #include "hardware_TL16C754C.h" |
||
31 | #include "hardware_config.h" |
||
32 | #include "frame_definitions.h" |
||
33 | #include "parameters.h" |
||
34 | |||
35 | //USART setup
|
||
36 | |||
37 | //To calculate BAUD value register -> BAUD = 64*FCLK/(16*FBAUDS)
|
||
38 | #define UART_BAUD_VALUE 833 // 10000 is equivalent as 9600 bauds (on 24MHz clock) according to datasheet |
||
39 | #define USART0_REG 0x3 |
||
40 | #define BOOT_MSG "RS232-MUX\r\n" |
||
41 | |||
42 | |||
43 | |||
44 | //OSC. setup
|
||
45 | #define FREQSEL 0x9 |
||
46 | #define _FREQSEL_REG_WR ((FREQSEL) << 2) |
||
47 | #define _USART0_REG_WR (USART0_REG & 0x7) |
||
48 | |||
49 | |||
50 | |||
51 | |||
52 | |||
53 | |||
54 | enum portIndex {
|
||
55 | PORT_VCOMA_INDEX = 0,
|
||
56 | PORT_VCOMB_INDEX, |
||
57 | PORT_VCOMC_INDEX, |
||
58 | PORT_VCOMD_INDEX, |
||
59 | PORT_MCOM_INDEX, |
||
60 | }; |
||
61 | |||
62 | |||
63 | //////////////////////////////////////////////////////////////
|
||
64 | //Prototypes
|
||
65 | |||
66 | void bootSequence(void); |
||
67 | void setRAMCfg(cfgPort *cfg, uint8_t *localBuffer);
|
||
68 | void MCOM_scanframe(uint8_t *data, uint8_t n, cfgPort *localPort);
|
||
69 | void MCOM_sendframe(uint8_t *data, uint8_t n, uint8_t port);
|
||
70 | void VCOM_sendframe(uint8_t *data, uint8_t n);
|
||
71 | void senseDebugLeds(bool *hasWritten, bool *hasRead); |
||
72 | void readCFG(bool EEPROM_read, cfgPort *localPort); |
||
73 | //String comparator (better version...)
|
||
74 | bool stringComp(uint8_t *str1, uint8_t *str2, uint8_t n, uint8_t m);
|
||
75 | //reset watchdog counter
|
||
76 | #define WATCHDOG_RESET asm("WDR") |
||
77 | |||
78 | |||
79 | |||
80 | //////////////////////////////////////////////////////////////////////////////
|
||
81 | //Little Keep save old configuration accessible everywhere (Not good as it is)
|
||
82 | cfgPort OLD_CFG[4];
|
||
83 | ///////////////////////////
|
||
84 | |||
85 | |||
86 | |||
87 | |||
88 | #ifdef USE_PORT_1 //USART3 |
||
89 | #define INIT_PORT(w,x,y,z) initPort1(w,x,y,z)
|
||
90 | #define TX_WRITE(x) txWrite1(x)
|
||
91 | #define RX_READ rxRead1()
|
||
92 | #define PORT_AVAILABLE portAvailable1()
|
||
93 | #else
|
||
94 | #ifdef USE_PORT_2 // USART1 |
||
95 | #define INIT_PORT(w,x,y,z) initPort2(w,x,y,z)
|
||
96 | #define TX_WRITE(x) txWrite2(x)
|
||
97 | #define RX_READ rxRead2()
|
||
98 | #define PORT_AVAILABLE portAvailable2()
|
||
99 | #else
|
||
100 | #ifdef USE_PORT_3 //USART4 |
||
101 | #define INIT_PORT(w,x,y,z) initPort3(w,x,y,z)
|
||
102 | #define TX_WRITE(x) txWrite3(x)
|
||
103 | #define RX_READ rxRead3()
|
||
104 | #define PORT_AVAILABLE portAvailable3()
|
||
105 | #else
|
||
106 | #ifdef USE_PORT_4 //USART2 |
||
107 | #define INIT_PORT(w,x,y,z) initPort4(w,x,y,z)
|
||
108 | #define TX_WRITE(x) txWrite4(x)
|
||
109 | #define RX_READ rxRead4()
|
||
110 | #define PORT_AVAILABLE portAvailable4()
|
||
111 | #else
|
||
112 | #error "Please choose an uart port..." |
||
113 | #endif
|
||
114 | #endif
|
||
115 | #endif
|
||
116 | #endif
|
||
117 | |||
118 | |||
119 | int main(void) { |
||
120 | |||
121 | cfgPort VCOM_cfg[4];
|
||
122 | |||
123 | uint8_t masterBuffer[64], bufferA[64], bufferB[64], bufferC[64], bufferD[64]; |
||
124 | uint8_t VCOMAIndex = 0, VCOMBIndex = 0, VCOMCIndex = 0, VCOMDIndex = 0, MCOMIndex; |
||
125 | uint16_t MCOM_counter = 0;
|
||
126 | uint16_t VCOMA_counter = 0;
|
||
127 | uint16_t VCOMB_counter = 0;
|
||
128 | uint16_t VCOMC_counter = 0;
|
||
129 | uint16_t VCOMD_counter = 0;
|
||
130 | |||
131 | bool VCOM_can_read[] = {false, false, false, false}; |
||
132 | |||
133 | uint8_t debug; |
||
134 | uint8_t pipoDebugger; |
||
135 | |||
136 | bool writtenStatus = false; //put write status on master port |
||
137 | bool readStatus[] = {false, false, false, false, false}; // put read status on all ports |
||
138 | |||
139 | setWait(5000);
|
||
140 | |||
141 | |||
142 | for(int i = 0; i < 64; i++) |
||
143 | { |
||
144 | bufferA[i] = 0;
|
||
145 | } |
||
146 | for(int i = 0; i < 64; i++) |
||
147 | { |
||
148 | bufferB[i] = 0;
|
||
149 | } |
||
150 | for(int i = 0; i < 64; i++) |
||
151 | { |
||
152 | bufferC[i] = 0;
|
||
153 | } |
||
154 | for(int i = 0; i < 64; i++) |
||
155 | { |
||
156 | bufferD[i] = 0;
|
||
157 | } |
||
158 | |||
159 | _PROTECTED_WRITE(CLKCTRL.OSCHFCTRLA, _FREQSEL_REG_WR); //switch to 24 MHz
|
||
160 | |||
161 | //_delay_ms(3000);
|
||
162 | |||
163 | //Enable pins (Port direction)
|
||
164 | ADDR_ENABLE; //Address pins
|
||
165 | ENABLE_WR_RD_PINS; //Read and Write pins
|
||
166 | ENABLE_CS_PINS; //Chip select pins
|
||
167 | ENABLE_RESET_PIN; //Reset pin
|
||
168 | |||
169 | //////////////////////////////////////////////////////////////
|
||
170 | //Remember to put high level on cs pins to disable devices...
|
||
171 | SETCSA; |
||
172 | SETCSB; |
||
173 | SETCSC; |
||
174 | SETCSD; |
||
175 | //Also don't forget IOR and IOW
|
||
176 | SETWR; |
||
177 | SETRD; |
||
178 | //////////////////////////////////////////////////////////////
|
||
179 | |||
180 | |||
181 | ////////////////////////////////////////
|
||
182 | //init TL16C IC
|
||
183 | SETRST; |
||
184 | //_delay_ms(10);
|
||
185 | setWait(100);
|
||
186 | CLRRST; |
||
187 | setWait(5);
|
||
188 | ////////////////////////////////////////
|
||
189 | |||
190 | //Init virtual COM ports
|
||
191 | setWait(20000);
|
||
192 | |||
193 | //Debug
|
||
194 | //char chrDebug[50];
|
||
195 | //sprintf(chrDebug, "%x %x %x %x %x %x %x %x\r\n", eeprom_read_byte(0),eeprom_read_byte(1),eeprom_read_byte(2),eeprom_read_byte(3),eeprom_read_byte(4),eeprom_read_byte(5),eeprom_read_byte(6),eeprom_read_byte(7));
|
||
196 | |||
197 | |||
198 | |||
199 | getEEPROMCfg(); //at first read EEPROM configuration
|
||
200 | readConfiguration(&VCOM_cfg[PORT_VCOMA_INDEX], PORT_VCOMA_INDEX); //Then read configuration for VCOMA
|
||
201 | readConfiguration(&VCOM_cfg[PORT_VCOMB_INDEX], PORT_VCOMB_INDEX); //Then read configuration for VCOMB
|
||
202 | readConfiguration(&VCOM_cfg[PORT_VCOMC_INDEX], PORT_VCOMC_INDEX); //Then read configuration for VCOMC
|
||
203 | readConfiguration(&VCOM_cfg[PORT_VCOMD_INDEX], PORT_VCOMD_INDEX); //Then read configuration for VCOMD
|
||
204 | |||
205 | //Read for old cfg (in case we want to read EEPROM...)
|
||
206 | readConfiguration(&OLD_CFG[PORT_VCOMA_INDEX], PORT_VCOMA_INDEX); //Then read configuration for VCOMA
|
||
207 | readConfiguration(&OLD_CFG[PORT_VCOMB_INDEX], PORT_VCOMB_INDEX); //Then read configuration for VCOMB
|
||
208 | readConfiguration(&OLD_CFG[PORT_VCOMC_INDEX], PORT_VCOMC_INDEX); //Then read configuration for VCOMC
|
||
209 | readConfiguration(&OLD_CFG[PORT_VCOMD_INDEX], PORT_VCOMD_INDEX); //Then read configuration for VCOMD
|
||
210 | |||
211 | //Init Virtual Ports
|
||
212 | initPortA(VCOM_cfg[PORT_VCOMA_INDEX].baud, VCOM_cfg[PORT_VCOMA_INDEX].dataByte, VCOM_cfg[PORT_VCOMA_INDEX].parity, VCOM_cfg[PORT_VCOMA_INDEX].stopBit); |
||
213 | initPortB(VCOM_cfg[PORT_VCOMB_INDEX].baud, VCOM_cfg[PORT_VCOMB_INDEX].dataByte, VCOM_cfg[PORT_VCOMB_INDEX].parity, VCOM_cfg[PORT_VCOMB_INDEX].stopBit); |
||
214 | initPortC(VCOM_cfg[PORT_VCOMC_INDEX].baud, VCOM_cfg[PORT_VCOMC_INDEX].dataByte, VCOM_cfg[PORT_VCOMC_INDEX].parity, VCOM_cfg[PORT_VCOMC_INDEX].stopBit); |
||
215 | initPortD(VCOM_cfg[PORT_VCOMD_INDEX].baud, VCOM_cfg[PORT_VCOMD_INDEX].dataByte, VCOM_cfg[PORT_VCOMD_INDEX].parity, VCOM_cfg[PORT_VCOMD_INDEX].stopBit); |
||
216 | |||
217 | |||
218 | |||
219 | //Master Port
|
||
220 | INIT_PORT(UART_BAUD_VALUE, F8BIT_MODE, ONE_STOPBIT, AVR32_NO_PARITY); |
||
221 | |||
222 | PORTC.DIR |= 0x04;
|
||
223 | PORTC.OUT |= 0x04;
|
||
224 | setWait(500);
|
||
225 | PORTC.OUT &= ~(0x04);
|
||
226 | /*for(int i = 0; i < 25; i++)
|
||
227 | {
|
||
228 | TX_WRITE(chrDebug[i]);
|
||
229 | }*/
|
||
230 | |||
231 | timerInit(60000); //10 ms interrupt |
||
232 | sei(); //never forget to enable global interrupt mask !
|
||
233 | |||
234 | PORTA.DIR = 0x3F;
|
||
235 | |||
236 | |||
237 | TX_WRITE('G');TX_WRITE('G');TX_WRITE(' ');TX_WRITE('B');TX_WRITE('O');TX_WRITE('Y'); |
||
238 | bootSequence(); |
||
239 | //TODO : Enable WDT
|
||
240 | |||
241 | while(1) |
||
242 | { |
||
243 | //WDT refresh (avoid program freeze when on crash...)
|
||
244 | WATCHDOG_RESET; |
||
245 | |||
246 | ////////////////////////////////////////////////////////
|
||
247 | //Read UART A
|
||
248 | |||
249 | |||
250 | if(portA_available()) //Check if one byte available |
||
251 | { |
||
252 | /////////////////////////////////////////
|
||
253 | //Frame check
|
||
254 | |||
255 | VCOM_can_read[PORT_VCOMA_INDEX] = true;
|
||
256 | if(getParitySetA()) //if parity set |
||
257 | { |
||
258 | if(getErrorStatusA()) //if parity failed (or stop bit also) |
||
259 | { |
||
260 | VCOM_can_read[PORT_VCOMA_INDEX] = false;
|
||
261 | } |
||
262 | } |
||
263 | |||
264 | /////////////////////////////////////////
|
||
265 | |||
266 | if(VCOM_can_read[PORT_VCOMA_INDEX]) //if we can read according to parity |
||
267 | { |
||
268 | readStatus[PORT_VCOMA_INDEX] = true;
|
||
269 | bufferA[VCOMAIndex] = rxReadA(); |
||
270 | VCOMAIndex++; |
||
271 | VCOMA_counter = 0; //for frame timeout |
||
272 | } |
||
273 | else //if read failed |
||
274 | { |
||
275 | rxReadA(); //flush serial
|
||
276 | } |
||
277 | } |
||
278 | |||
279 | ////////////////////////////////////////////////////////
|
||
280 | //Read UART B
|
||
281 | |||
282 | |||
283 | if(portB_available()) //Check if one byte available |
||
284 | { |
||
285 | /////////////////////////////////////////
|
||
286 | //Frame check
|
||
287 | |||
288 | VCOM_can_read[PORT_VCOMB_INDEX] = true;
|
||
289 | if(getParitySetB()) //if parity set |
||
290 | { |
||
291 | if(getErrorStatusB()) //if parity failed (or stop bit also) |
||
292 | { |
||
293 | VCOM_can_read[PORT_VCOMB_INDEX] = false;
|
||
294 | } |
||
295 | } |
||
296 | |||
297 | /////////////////////////////////////////
|
||
298 | |||
299 | |||
300 | |||
301 | if(VCOM_can_read[PORT_VCOMB_INDEX]) //if we can read according to parity |
||
302 | { |
||
303 | readStatus[PORT_VCOMB_INDEX] = true;
|
||
304 | bufferB[VCOMBIndex] = rxReadB(); |
||
305 | VCOMBIndex++; |
||
306 | //if(VCOMBIndex > 64)
|
||
307 | // VCOMBIndex = 0;
|
||
308 | VCOMB_counter = 0; //for frame timeout |
||
309 | } |
||
310 | else //if read failed |
||
311 | { |
||
312 | rxReadB(); //flush serial
|
||
313 | } |
||
314 | } |
||
315 | |||
316 | |||
317 | ////////////////////////////////////////////////////////
|
||
318 | //Read UART C
|
||
319 | |||
320 | |||
321 | if(portC_available()) //Check if one byte available |
||
322 | { |
||
323 | /////////////////////////////////////////
|
||
324 | //Frame check
|
||
325 | |||
326 | VCOM_can_read[PORT_VCOMC_INDEX] = true;
|
||
327 | if(getParitySetC()) //if parity set |
||
328 | { |
||
329 | if(getErrorStatusC()) //if parity failed (or stop bit also) |
||
330 | { |
||
331 | VCOM_can_read[PORT_VCOMC_INDEX] = false;
|
||
332 | } |
||
333 | } |
||
334 | |||
335 | /////////////////////////////////////////
|
||
336 | |||
337 | |||
338 | |||
339 | if(VCOM_can_read[PORT_VCOMC_INDEX]) //if we can read according to parity |
||
340 | { |
||
341 | readStatus[PORT_VCOMC_INDEX] = true;
|
||
342 | bufferC[VCOMCIndex] = rxReadC(); |
||
343 | VCOMCIndex++; |
||
344 | //if(VCOMBIndex > 64)
|
||
345 | // VCOMBIndex = 0;
|
||
346 | VCOMC_counter = 0; //for frame timeout |
||
347 | } |
||
348 | else //if read failed |
||
349 | { |
||
350 | rxReadC(); //flush serial
|
||
351 | } |
||
352 | } |
||
353 | |||
354 | |||
355 | |||
356 | ////////////////////////////////////////////////////////
|
||
357 | //Read UART D
|
||
358 | |||
359 | |||
360 | if(portD_available()) //Check if one byte available |
||
361 | { |
||
362 | /////////////////////////////////////////
|
||
363 | //Frame check
|
||
364 | |||
365 | VCOM_can_read[PORT_VCOMD_INDEX] = true;
|
||
366 | if(getParitySetD()) //if parity set |
||
367 | { |
||
368 | if(getErrorStatusD()) //if parity failed (or stop bit also) |
||
369 | { |
||
370 | VCOM_can_read[PORT_VCOMD_INDEX] = false;
|
||
371 | } |
||
372 | } |
||
373 | |||
374 | /////////////////////////////////////////
|
||
375 | |||
376 | |||
377 | |||
378 | if(VCOM_can_read[PORT_VCOMD_INDEX]) //if we can read according to parity |
||
379 | { |
||
380 | readStatus[PORT_VCOMD_INDEX] = true;
|
||
381 | bufferD[VCOMDIndex] = rxReadD(); |
||
382 | VCOMDIndex++; |
||
383 | //if(VCOMBIndex > 64)
|
||
384 | // VCOMBIndex = 0;
|
||
385 | VCOMD_counter = 0; //for frame timeout |
||
386 | } |
||
387 | else //if read failed |
||
388 | { |
||
389 | rxReadD(); //flush serial
|
||
390 | } |
||
391 | } |
||
392 | |||
393 | |||
394 | ////////////////////////////////////////////////////////
|
||
395 | //Read UART Master
|
||
396 | |||
397 | |||
398 | if(PORT_AVAILABLE > 0) //check if we have bytes to read |
||
399 | { |
||
400 | readStatus[PORT_MCOM_INDEX] = true;
|
||
401 | masterBuffer[MCOMIndex] = RX_READ; //store byte into buffer
|
||
402 | MCOMIndex++; //increment to next byte
|
||
403 | //if(MCOMIndex > 64)
|
||
404 | // MCOMIndex = 0;
|
||
405 | MCOM_counter = 0; //refresh timeout counter |
||
406 | } |
||
407 | /*else
|
||
408 | {
|
||
409 | readStatus[PORT_MCOM_INDEX] = false; //To prevent the bug on uC (Silicon cause ?)
|
||
410 | }*/
|
||
411 | |||
412 | |||
413 | |||
414 | |||
415 | //Timed process (for leds, etc...)
|
||
416 | if(getTimerFlag())
|
||
417 | { |
||
418 | /////////////////////////////////////////////////////////
|
||
419 | //Timeout process
|
||
420 | |||
421 | //For master port
|
||
422 | if(MCOMIndex > 0) |
||
423 | MCOM_counter++; |
||
424 | |||
425 | //For virtual port 1
|
||
426 | if(VCOMAIndex > 0) |
||
427 | VCOMA_counter++; |
||
428 | |||
429 | //For virtual port 2
|
||
430 | if(VCOMBIndex > 0) |
||
431 | VCOMB_counter++; |
||
432 | |||
433 | //For virtual port 3
|
||
434 | if(VCOMCIndex > 0) |
||
435 | VCOMC_counter++; |
||
436 | |||
437 | //For virtual port 4
|
||
438 | if(VCOMDIndex > 0) |
||
439 | VCOMD_counter++; |
||
440 | |||
441 | |||
442 | senseDebugLeds(&writtenStatus, readStatus); |
||
443 | clearTimerFlag(); |
||
444 | } |
||
445 | |||
446 | |||
447 | |||
448 | |||
449 | |||
450 | ///////////////////////////////////////
|
||
451 | //When buf is ready from master
|
||
452 | if(MCOM_counter > SERIAL_PORT_TIMEOUT_COUNT || MCOMIndex >= 64) |
||
453 | { |
||
454 | //Send buffer to VCOM
|
||
455 | if(masterBuffer[1] == MASTER_ID) //check if frame is destinated to the MUX232 |
||
456 | { |
||
457 | MCOM_scanframe(masterBuffer, MCOMIndex, VCOM_cfg); |
||
458 | } |
||
459 | else //otherwise send it through a VCOM |
||
460 | { |
||
461 | VCOM_sendframe(masterBuffer, MCOMIndex); |
||
462 | } |
||
463 | |||
464 | MCOMIndex = 0; //reset index buffer |
||
465 | MCOM_counter = 0; //reset counter |
||
466 | } |
||
467 | |||
468 | |||
469 | ///////////////////////////////////////
|
||
470 | //When buf is ready from VCOMA
|
||
471 | |||
472 | if(VCOMA_counter > SERIAL_PORT_TIMEOUT_COUNT || VCOMAIndex >= 64) |
||
473 | { |
||
474 | writtenStatus = true;//Say a wrote has been made |
||
475 | //Send buffer to VCOM
|
||
476 | MCOM_sendframe(bufferA, VCOMAIndex, PORT_VCOMA_INDEX); |
||
477 | VCOMAIndex = 0; //reset index buffer |
||
478 | VCOMA_counter = 0; //reset counter |
||
479 | } |
||
480 | |||
481 | |||
482 | |||
483 | ///////////////////////////////////////
|
||
484 | //When buf is ready from VCOMB
|
||
485 | |||
486 | if(VCOMB_counter > SERIAL_PORT_TIMEOUT_COUNT || VCOMBIndex >= 64) |
||
487 | { |
||
488 | writtenStatus = true;//Say a wrote has been made |
||
489 | //Send buffer to VCOM
|
||
490 | MCOM_sendframe(bufferB, VCOMBIndex, PORT_VCOMB_INDEX); |
||
491 | VCOMBIndex = 0; //reset index buffer |
||
492 | VCOMB_counter = 0; //reset counter |
||
493 | } |
||
494 | |||
495 | |||
496 | ///////////////////////////////////////
|
||
497 | //When buf is ready from VCOMC
|
||
498 | if(VCOMC_counter > SERIAL_PORT_TIMEOUT_COUNT || VCOMCIndex >= 64) |
||
499 | { |
||
500 | writtenStatus = true;//Say a wrote has been made |
||
501 | //Send buffer to VCOM
|
||
502 | MCOM_sendframe(bufferC, VCOMCIndex, PORT_VCOMC_INDEX); |
||
503 | VCOMCIndex = 0; //reset index buffer |
||
504 | VCOMC_counter = 0; //reset counter |
||
505 | } |
||
506 | |||
507 | |||
508 | |||
509 | |||
510 | if(VCOMD_counter > SERIAL_PORT_TIMEOUT_COUNT || VCOMDIndex >= 64) |
||
511 | { |
||
512 | |||
513 | writtenStatus = true; //Say a wrote has been made |
||
514 | //Send buffer to VCOM
|
||
515 | MCOM_sendframe(bufferD, VCOMDIndex, PORT_VCOMD_INDEX); |
||
516 | VCOMDIndex = 0; //reset index buffer |
||
517 | VCOMD_counter = 0; //reset counter |
||
518 | } |
||
519 | |||
520 | |||
521 | } |
||
522 | |||
523 | |||
524 | return 0; |
||
525 | } |
||
526 | |||
527 | |||
528 | |||
529 | |||
530 | ///////////////////////////////////////////////////////////
|
||
531 | //Functions / macros
|
||
532 | |||
533 | void readCFG(bool EEPROM_read, cfgPort *localPort) |
||
534 | { |
||
535 | cfgPort *debugPort; |
||
536 | |||
537 | const uint8_t *dispBaud[] = {"9600", "19200", "38400"}; |
||
538 | const uint8_t dispParity[] = {'N', 'E', 'O'}; //The Matrix has you... |
||
539 | const uint8_t dispData[] = {'5', '6', '7', '8'}; |
||
540 | const uint8_t dispBit[] = {'1', '2'}; |
||
541 | char msg[30]; //23 characters max |
||
542 | |||
543 | |||
544 | //Read EEPROM configuration or RAM ?
|
||
545 | //if(EEPROM_read)
|
||
546 | // getEEPROMCfg();
|
||
547 | |||
548 | |||
549 | |||
550 | if(!EEPROM_read) //Read RAM CFG ? |
||
551 | debugPort = &localPort[PORT_VCOMA_INDEX]; |
||
552 | else
|
||
553 | debugPort = &OLD_CFG[PORT_VCOMA_INDEX]; |
||
554 | |||
555 | //readConfiguration(debugPort, PORT_VCOMA_INDEX); //or just get CFG read from EEPROM
|
||
556 | //Disp CFGRead bytes and display directly CFG
|
||
557 | for(int i = 0; i < 30; i++) |
||
558 | { |
||
559 | msg[i] = 0;
|
||
560 | } |
||
561 | sprintf(msg, "%c%c%c%cVCOMA B%s F%c%c%c\r\n",
|
||
562 | STX, |
||
563 | MASTER_ID, |
||
564 | FRAME_ADDR_1, |
||
565 | FRAME_ADDR_2, |
||
566 | dispBaud[debugPort->baud], |
||
567 | dispData[debugPort->dataByte], |
||
568 | dispParity[debugPort->parity], |
||
569 | dispBit[debugPort->stopBit] |
||
570 | ); |
||
571 | for(uint8_t i = 0; i < 22; i++) |
||
572 | TX_WRITE(msg[i]); //Display string
|
||
573 | |||
574 | if(!EEPROM_read) //Read RAM CFG ? |
||
575 | debugPort = &localPort[PORT_VCOMB_INDEX]; |
||
576 | else
|
||
577 | debugPort = &OLD_CFG[PORT_VCOMB_INDEX]; |
||
578 | //readConfiguration(debugPort, PORT_VCOMB_INDEX); //or just get CFG read from EEPROM
|
||
579 | //Disp CFGRead bytes and display directly CFG
|
||
580 | for(int i = 0; i < 30; i++) |
||
581 | { |
||
582 | msg[i] = 0;
|
||
583 | } |
||
584 | |||
585 | sprintf(msg, "%c%c%c%cVCOMB B%s F%c%c%c\r\n",
|
||
586 | STX, |
||
587 | MASTER_ID, |
||
588 | FRAME_ADDR_1, |
||
589 | FRAME_ADDR_2, |
||
590 | dispBaud[debugPort->baud], |
||
591 | dispData[debugPort->dataByte], |
||
592 | dispParity[debugPort->parity], |
||
593 | dispBit[debugPort->stopBit] |
||
594 | ); |
||
595 | for(uint8_t i = 0; i < 22; i++) |
||
596 | TX_WRITE(msg[i]); //Display string
|
||
597 | |||
598 | if(!EEPROM_read) //Read RAM CFG ? |
||
599 | debugPort = &localPort[PORT_VCOMC_INDEX]; |
||
600 | else
|
||
601 | debugPort = &OLD_CFG[PORT_VCOMC_INDEX]; |
||
602 | //readConfiguration(debugPort, PORT_VCOMC_INDEX); //or just get CFG read from EEPROM
|
||
603 | //Disp CFGRead bytes and display directly CFG
|
||
604 | for(int i = 0; i < 30; i++) |
||
605 | { |
||
606 | msg[i] = 0;
|
||
607 | } |
||
608 | sprintf(msg, "%c%c%c%cVCOMC B%s F%c%c%c\r\n",
|
||
609 | STX, |
||
610 | MASTER_ID, |
||
611 | FRAME_ADDR_1, |
||
612 | FRAME_ADDR_2, |
||
613 | dispBaud[debugPort->baud], |
||
614 | dispData[debugPort->dataByte], |
||
615 | dispParity[debugPort->parity], |
||
616 | dispBit[debugPort->stopBit] |
||
617 | ); |
||
618 | for(uint8_t i = 0; i < 22; i++) |
||
619 | TX_WRITE(msg[i]); //Display string
|
||
620 | |||
621 | if(!EEPROM_read) //Read RAM CFG ? |
||
622 | debugPort = &localPort[PORT_VCOMD_INDEX]; |
||
623 | else
|
||
624 | debugPort = &OLD_CFG[PORT_VCOMD_INDEX]; |
||
625 | //readConfiguration(debugPort, PORT_VCOMD_INDEX); //or just get CFG read from EEPROM
|
||
626 | //Disp CFGRead bytes and display directly CFG
|
||
627 | for(int i = 0; i < 30; i++) |
||
628 | { |
||
629 | msg[i] = 0;
|
||
630 | } |
||
631 | sprintf(msg, "%c%c%c%cVCOMD B%s F%c%c%c\r\n",
|
||
632 | STX, |
||
633 | MASTER_ID, |
||
634 | FRAME_ADDR_1, |
||
635 | FRAME_ADDR_2, |
||
636 | dispBaud[debugPort->baud], |
||
637 | dispData[debugPort->dataByte], |
||
638 | dispParity[debugPort->parity], |
||
639 | dispBit[debugPort->stopBit] |
||
640 | ); |
||
641 | for(uint8_t i = 0; i < 22; i++) |
||
642 | TX_WRITE(msg[i]); //Display string
|
||
643 | } |
||
644 | |||
645 | |||
646 | //Set RAM Cfg that we must apply it to the EEPROM !
|
||
647 | void setRAMCfg(cfgPort *cfg, uint8_t *localBuffer)
|
||
648 | { |
||
649 | //Set default CFG
|
||
650 | uint8_t localBaudsCfg = ADDR_BAUD_9600, localDataCfg = ADDR_DATA_F8; |
||
651 | uint8_t localParityCfg = ADDR_PARITY_NONE, localSBCfg = ADDR_STOP_ONE; |
||
652 | uint8_t localVCOMIndex; |
||
653 | uint8_t localIndex; |
||
654 | uint8_t i; |
||
655 | |||
656 | ////////////////////////////////////////////////////////
|
||
657 | //VCOM CONFIGURATION
|
||
658 | |||
659 | //Be sure our first character is B (so we can estimate that frame is good enough)
|
||
660 | if (localBuffer[MAX232_B_STR_INDEX] == MAX232_B_STR_CHAR)
|
||
661 | { |
||
662 | localIndex = MAX232_B_STR_INDEX; |
||
663 | i = 0;
|
||
664 | //Calculate number of bytes between B and F char into frame (9600 bauds or 19200/38400 bauds for example)
|
||
665 | while (localBuffer[localIndex] != MAX232_F_STR_CHAR)
|
||
666 | { |
||
667 | i++; |
||
668 | localIndex++; |
||
669 | } |
||
670 | if (i < 6) //number of char = 4 ? ("9600") |
||
671 | { |
||
672 | |||
673 | //////////////////////////////////////////
|
||
674 | //At first set bauds speed
|
||
675 | if (localBuffer[MAX232_B_STR_INDEX + 1] == '9' && localBuffer[MAX232_B_STR_INDEX + 2] == '6' && |
||
676 | localBuffer[MAX232_B_STR_INDEX + 3] == '0' && localBuffer[MAX232_B_STR_INDEX + 4] == '0') |
||
677 | { |
||
678 | //Put CFG into 9600 bauds
|
||
679 | localBaudsCfg = ADDR_BAUD_9600; |
||
680 | } |
||
681 | //////////////////////////////////////////
|
||
682 | //Set Data bits
|
||
683 | localDataCfg = localBuffer[MAX232_B_STR_INDEX + 6] - '5'; // Shift to 0 because ADDR_DATA_F5 = 0, ADDR_DATA_F6 = 1, ect... |
||
684 | //We have already localDataCfg as ADDR_PARITY_NONE (keep aat this value if it equals to 'N')
|
||
685 | //localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 7] == 'N') ? ADDR_PARITY_NONE : localDataCfg;
|
||
686 | //Does it equals to ODD ?
|
||
687 | localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 7] == 'O') ? ADDR_PARITY_ODD : localParityCfg; |
||
688 | //or EVEN ?
|
||
689 | localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 7] == 'E') ? ADDR_PARITY_EVEN : localParityCfg; |
||
690 | //Read (localBuffer[MAX232_B_STR_INDEX + 8] for stop bits -> 1/2
|
||
691 | localSBCfg = (localBuffer[MAX232_B_STR_INDEX + 8] == '2'); //Return condition state only... |
||
692 | //Read (localBuffer[MAX232_B_STR_INDEX + 10] VCOM -> A/B/C/D
|
||
693 | localVCOMIndex = localBuffer[MAX232_B_STR_INDEX + 10] - CESAR_SHIFT;
|
||
694 | } |
||
695 | else //number of char = 5 ? ("19200"/"38400") |
||
696 | { |
||
697 | //////////////////////////////////////////
|
||
698 | //At first set bauds speed
|
||
699 | |||
700 | if (localBuffer[MAX232_B_STR_INDEX + 1] == '1' && localBuffer[MAX232_B_STR_INDEX + 2] == '9' && |
||
701 | localBuffer[MAX232_B_STR_INDEX + 3] == '2' && localBuffer[MAX232_B_STR_INDEX + 4] == '0' && |
||
702 | localBuffer[MAX232_B_STR_INDEX + 5] == '0') |
||
703 | { |
||
704 | //Put CFG into 19200 bauds
|
||
705 | localBaudsCfg = ADDR_BAUD_19200; |
||
706 | } |
||
707 | |||
708 | if (localBuffer[MAX232_B_STR_INDEX + 1] == '3' && localBuffer[MAX232_B_STR_INDEX + 2] == '8' && |
||
709 | localBuffer[MAX232_B_STR_INDEX + 3] == '4' && localBuffer[MAX232_B_STR_INDEX + 4] == '0' && |
||
710 | localBuffer[MAX232_B_STR_INDEX + 5] == '0') |
||
711 | { |
||
712 | //Put CFG into 38400 bauds
|
||
713 | localBaudsCfg = ADDR_BAUD_38400; |
||
714 | } |
||
715 | |||
716 | //////////////////////////////////////////
|
||
717 | |||
718 | //Set Data bits
|
||
719 | localDataCfg = localBuffer[MAX232_B_STR_INDEX + 7] - '5'; // Shift to 0 because ADDR_DATA_F5 = 0, ADDR_DATA_F6 = 1, ect... |
||
720 | //We have already localDataCfg as ADDR_PARITY_NONE (keep aat this value if it equals to 'N')
|
||
721 | //localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 7] == 'N') ? ADDR_PARITY_NONE : localDataCfg;
|
||
722 | //Does it equals to ODD ?
|
||
723 | localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 8] == 'O') ? ADDR_PARITY_ODD : localParityCfg; |
||
724 | //or EVEN ?
|
||
725 | localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 8] == 'E') ? ADDR_PARITY_EVEN : localParityCfg; |
||
726 | //Read (localBuffer[MAX232_B_STR_INDEX + 8] for stop bits -> 1/2
|
||
727 | localSBCfg = (localBuffer[MAX232_B_STR_INDEX + 9] == '2'); //Return condition state only for stop bits... |
||
728 | //Read (localBuffer[MAX232_B_STR_INDEX + 10] VCOM -> A/B/C/D
|
||
729 | localVCOMIndex = localBuffer[MAX232_B_STR_INDEX + 11] - CESAR_SHIFT;
|
||
730 | //TX_WRITE('F'); TX_WRITE('D'); TX_WRITE('P'); TX_WRITE(localBuffer[MAX232_B_STR_INDEX + 9]);
|
||
731 | //TX_WRITE('F'); TX_WRITE('D'); TX_WRITE('P'); TX_WRITE(localBuffer[MAX232_B_STR_INDEX + 11]);
|
||
732 | //TX_WRITE('F'); TX_WRITE('D'); TX_WRITE('P'); TX_WRITE(localParityCfg);
|
||
733 | } |
||
734 | //Write into RAM buffer (and prepare for EEPROM writing)
|
||
735 | writeConfiguration(&cfg[localVCOMIndex], localBaudsCfg, localParityCfg, localDataCfg, localSBCfg, localVCOMIndex); |
||
736 | } |
||
737 | ////////////////////////////////////////////////////////
|
||
738 | |||
739 | } |
||
740 | |||
741 | |||
742 | //Read a frame destinated to the MUX232 itself
|
||
743 | |||
744 | void MCOM_scanframe(uint8_t *data, uint8_t n, cfgPort *localPort)
|
||
745 | { |
||
746 | |||
747 | uint8_t *localBuffer; //Calculate byte quantity of effective data
|
||
748 | uint8_t i; |
||
749 | |||
750 | |||
751 | while(localBuffer == NULL) |
||
752 | { |
||
753 | localBuffer = malloc(n-6);
|
||
754 | setWait(5);
|
||
755 | } |
||
756 | |||
757 | for(i = 0; i < n-6; i++) |
||
758 | localBuffer[i] = 0;
|
||
759 | |||
760 | //At first, check if first byte is the start of text
|
||
761 | if(data[0] == STX) |
||
762 | { |
||
763 | if(data[2] == FRAME_ADDR_1 && data[3] == FRAME_ADDR_2) //check if we have the MUX address identifier |
||
764 | { |
||
765 | if(data[n-1] == CR_CHAR) //check the end of text (second byte) |
||
766 | { |
||
767 | if(data[n-2] == LF_CHAR) //check the end of text (first byte) |
||
768 | { |
||
769 | for(i = 0; i < n-6; i++) //regarder la valeur de n |
||
770 | { |
||
771 | localBuffer[i] = data[i + 4]; //Get effective data |
||
772 | } |
||
773 | //Once data was collected, analysis it
|
||
774 | if(!stringComp(localBuffer, MAX232_WRITE_COMMAND, n-6, MAX232_WRITE_SIZE)) //if the command is Write configuration |
||
775 | { |
||
776 | //Save CFG into EEPROM
|
||
777 | setEEPROMCfg(); |
||
778 | TX_WRITE(0x2);
|
||
779 | TX_WRITE(FRAME_ADDR_1); |
||
780 | TX_WRITE(FRAME_ADDR_2); |
||
781 | TX_WRITE(MASTER_ID); |
||
782 | TX_WRITE('W');TX_WRITE('R'); |
||
783 | TX_WRITE('C');TX_WRITE('F');TX_WRITE('G'); |
||
784 | TX_WRITE(LF_CHAR);TX_WRITE(CR_CHAR); |
||
785 | |||
786 | TX_WRITE(0x2);
|
||
787 | TX_WRITE(FRAME_ADDR_1); |
||
788 | TX_WRITE(FRAME_ADDR_2); |
||
789 | TX_WRITE(MASTER_ID); |
||
790 | TX_WRITE('R');TX_WRITE('E');TX_WRITE('B');TX_WRITE('O');TX_WRITE('O');TX_WRITE('T'); |
||
791 | TX_WRITE(LF_CHAR);TX_WRITE(CR_CHAR); |
||
792 | |||
793 | while(1); //Force WDT reboot |
||
794 | } |
||
795 | else if(!stringComp(localBuffer, MAX232_READ_RAM_COMMAND, n-6, MAX232_READ_RAM_SIZE)) //is this Read configuration ? |
||
796 | { |
||
797 | //Read CFG from RAM
|
||
798 | readCFG(false, localPort);
|
||
799 | } |
||
800 | else if(!stringComp(localBuffer, MAX232_READ_ROM_COMMAND, n-6, MAX232_READ_ROM_SIZE)) //is this Read configuration ? |
||
801 | { |
||
802 | //Read CFG from EEPROM
|
||
803 | readCFG(true, localPort);
|
||
804 | } |
||
805 | else //otherwise that might be set configuration |
||
806 | { |
||
807 | setRAMCfg(localPort, localBuffer); |
||
808 | TX_WRITE(0x2);
|
||
809 | TX_WRITE(FRAME_ADDR_1); |
||
810 | TX_WRITE(FRAME_ADDR_2); |
||
811 | TX_WRITE(MASTER_ID); |
||
812 | TX_WRITE('S');TX_WRITE('E');TX_WRITE('T'); |
||
813 | TX_WRITE('C');TX_WRITE('F');TX_WRITE('G'); |
||
814 | TX_WRITE(LF_CHAR);TX_WRITE(CR_CHAR); |
||
815 | } |
||
816 | } |
||
817 | } |
||
818 | } |
||
819 | } |
||
820 | } |
||
821 | |||
822 | |||
823 | |||
824 | //Send a frame from virtual port to master port
|
||
825 | |||
826 | void MCOM_sendframe(uint8_t *data, uint8_t n, uint8_t port)
|
||
827 | { |
||
828 | //////////////////////////
|
||
829 | //Send frame
|
||
830 | |||
831 | TX_WRITE(0x02); //STX |
||
832 | TX_WRITE(port + CESAR_SHIFT); //Which virtual port called master port ?
|
||
833 | TX_WRITE(FRAME_ADDR_1); //ADDR
|
||
834 | TX_WRITE(FRAME_ADDR_2); //ADDR
|
||
835 | |||
836 | for(uint8_t i = 0; i < n; i++) //DATA |
||
837 | TX_WRITE(data[i]); |
||
838 | |||
839 | TX_WRITE(LF_CHAR); //End of frame
|
||
840 | TX_WRITE(CR_CHAR); //End of frame
|
||
841 | //////////////////////////
|
||
842 | } |
||
843 | |||
844 | //////////////////////////////////////////////////////////////////////////////////////////
|
||
845 | //Send a frame from master port to virtual port (that calculate whose port to send ?)
|
||
846 | |||
847 | void VCOM_sendframe(uint8_t *data, uint8_t n)
|
||
848 | { |
||
849 | uint16_t addrFunctions[] = {&txWriteA, &txWriteB, &txWriteC, &txWriteD }; //list of available functions addresses
|
||
850 | void (*localTxWrite)(); //function pointer |
||
851 | |||
852 | |||
853 | ////////////////////////////////
|
||
854 | |||
855 | //At first, check if first byte is the start of text
|
||
856 | if(data[0] == STX) |
||
857 | { |
||
858 | if(data[2] == FRAME_ADDR_1 && data[3] == FRAME_ADDR_2) //check if we have the MUX address identifier |
||
859 | { |
||
860 | if(data[n-1] == CR_CHAR) //check the end of text (second byte) |
||
861 | { |
||
862 | if(data[n-2] == LF_CHAR) //check the end of text (first byte) |
||
863 | { |
||
864 | data[1]-=CESAR_SHIFT; //convert port index to buffer equivalent |
||
865 | localTxWrite = addrFunctions[(data[1] < 3) ? data[1] : 3 ]; //data[3] |
||
866 | for(int i = 4; i < n-2; i++) //send frame without (STX + FRAME_ADDR_B1 + FRAME_ADDR_B2 + LF_CHAR + CR_CHAR) |
||
867 | { |
||
868 | localTxWrite(data[i]); |
||
869 | } |
||
870 | |||
871 | } |
||
872 | } |
||
873 | } |
||
874 | } |
||
875 | } |
||
876 | |||
877 | |||
878 | |||
879 | |||
880 | |||
881 | |||
882 | |||
883 | void bootSequence(void) |
||
884 | { |
||
885 | debugLedsTest(); |
||
886 | } |
||
887 | |||
888 | |||
889 | |||
890 | |||
891 | /**
|
||
892 | <p><b>void debugLedsTest(void)</b></p>
|
||
893 | <p><b>Debug leds test sequence</b></p>
|
||
894 | */
|
||
895 | void debugLedsTest(void) |
||
896 | { |
||
897 | |||
898 | PORTA.OUT = 1;
|
||
899 | for(int i = 0; i < 6; i++) |
||
900 | { |
||
901 | PORTA.OUT <<= 1;
|
||
902 | _delay_ms(250);
|
||
903 | } |
||
904 | PORTA.OUT = 0;
|
||
905 | } |
||
906 | |||
907 | /**
|
||
908 | <p><b>void senseDebugLeds(void)</b></p>
|
||
909 | <p><b>Call this to update leds status</b></p>
|
||
910 | */
|
||
911 | void senseDebugLeds(bool *hasWritten, bool *hasRead) |
||
912 | { |
||
913 | |||
914 | |||
915 | |||
916 | ///////////////////////////
|
||
917 | //Check Master Port
|
||
918 | if(*hasWritten)
|
||
919 | { |
||
920 | PORTA.OUT |= 0x20;
|
||
921 | *hasWritten = false;
|
||
922 | } |
||
923 | else
|
||
924 | PORTA.OUT &= ~(0x20);
|
||
925 | |||
926 | if(hasRead[PORT_MCOM_INDEX])
|
||
927 | { |
||
928 | PORTA.OUT |= 0x10;
|
||
929 | hasRead[PORT_MCOM_INDEX] = false;
|
||
930 | } |
||
931 | else
|
||
932 | PORTA.OUT &= ~(0x10);
|
||
933 | |||
934 | ///////////////////////////
|
||
935 | //Check VCOMA
|
||
936 | if(hasRead[PORT_VCOMA_INDEX])
|
||
937 | { |
||
938 | PORTA.OUT |= 0x01;
|
||
939 | hasRead[PORT_VCOMA_INDEX] = false;
|
||
940 | } |
||
941 | else
|
||
942 | PORTA.OUT &= ~(0x01);
|
||
943 | |||
944 | ///////////////////////////
|
||
945 | //Check VCOMB
|
||
946 | if(hasRead[PORT_VCOMB_INDEX])
|
||
947 | { |
||
948 | PORTA.OUT |= 0x02;
|
||
949 | hasRead[PORT_VCOMB_INDEX] = false;
|
||
950 | } |
||
951 | else
|
||
952 | PORTA.OUT &= ~(0x02);
|
||
953 | |||
954 | ///////////////////////////
|
||
955 | //Check VCOMC
|
||
956 | if(hasRead[PORT_VCOMC_INDEX])
|
||
957 | { |
||
958 | PORTA.OUT |= 0x04;
|
||
959 | hasRead[PORT_VCOMC_INDEX] = false;
|
||
960 | } |
||
961 | else
|
||
962 | PORTA.OUT &= ~(0x04);
|
||
963 | |||
964 | |||
965 | ///////////////////////////
|
||
966 | //Check VCOMD
|
||
967 | if(hasRead[PORT_VCOMD_INDEX])
|
||
968 | { |
||
969 | PORTA.OUT |= 0x08;
|
||
970 | hasRead[PORT_VCOMD_INDEX] = false;
|
||
971 | } |
||
972 | else
|
||
973 | PORTA.OUT &= ~(0x08);
|
||
974 | } |
||
975 | |||
976 | |||
977 | //String comparator (better version...)
|
||
978 | bool stringComp(uint8_t *str1, uint8_t *str2, uint8_t n, uint8_t m)
|
||
979 | { |
||
980 | bool result = false; |
||
981 | |||
982 | if(n == m)
|
||
983 | { |
||
984 | for(int i = 0; i < n; i++) |
||
985 | { |
||
986 | //TX_WRITE('A');TX_WRITE(':');TX_WRITE(str1[i]);TX_WRITE(" ");
|
||
987 | //TX_WRITE('B');TX_WRITE(':');TX_WRITE(str2[i]);TX_WRITE("\n");TX_WRITE("\r");
|
||
988 | if(str1[i] != str2[i]) //different string... |
||
989 | result = true; //haaaaaaaaaaaaaaaaaaaaaaaaaaaax |
||
990 | } |
||
991 | } |
||
992 | else
|
||
993 | { |
||
994 | result = true;
|
||
995 | } |
||
996 | |||
997 | |||
998 | |||
999 | return result;
|
||
1000 | } |
||
1001 | |||
1002 | |||
1003 | |||
1004 | |||
1005 |