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