root / Version 1.6 / RS232_MUX.X / main.c @ 0e6cfafc
Historique | Voir | Annoter | Télécharger (30,799 ko)
1 |
/*
|
---|---|
2 |
* File: main.c
|
3 |
* Author: eniro
|
4 |
* Version : 1.6
|
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 |
char chrDebug[50]; |
200 |
|
201 |
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)); |
202 |
|
203 |
|
204 |
|
205 |
getEEPROMCfg(); //at first read EEPROM configuration
|
206 |
readConfiguration(&VCOM_cfg[PORT_VCOMA_INDEX], PORT_VCOMA_INDEX); //Then read configuration for VCOMA
|
207 |
readConfiguration(&VCOM_cfg[PORT_VCOMB_INDEX], PORT_VCOMB_INDEX); //Then read configuration for VCOMB
|
208 |
readConfiguration(&VCOM_cfg[PORT_VCOMC_INDEX], PORT_VCOMC_INDEX); //Then read configuration for VCOMC
|
209 |
readConfiguration(&VCOM_cfg[PORT_VCOMD_INDEX], PORT_VCOMD_INDEX); //Then read configuration for VCOMD
|
210 |
|
211 |
//Read for old cfg (in case we want to read EEPROM...)
|
212 |
readConfiguration(&OLD_CFG[PORT_VCOMA_INDEX], PORT_VCOMA_INDEX); //Then read configuration for VCOMA
|
213 |
readConfiguration(&OLD_CFG[PORT_VCOMB_INDEX], PORT_VCOMB_INDEX); //Then read configuration for VCOMB
|
214 |
readConfiguration(&OLD_CFG[PORT_VCOMC_INDEX], PORT_VCOMC_INDEX); //Then read configuration for VCOMC
|
215 |
readConfiguration(&OLD_CFG[PORT_VCOMD_INDEX], PORT_VCOMD_INDEX); //Then read configuration for VCOMD
|
216 |
|
217 |
/*initPortA(DL_9600_BAUDS, F8BIT_MODE, TL16C_ODD_PARITY, ONE_STOPBIT);
|
218 |
initPortB(DL_9600_BAUDS, F8BIT_MODE, TL16C_NO_PARITY, ONE_STOPBIT);
|
219 |
initPortC(DL_9600_BAUDS, F8BIT_MODE, TL16C_NO_PARITY, ONE_STOPBIT);
|
220 |
initPortD(DL_9600_BAUDS, F8BIT_MODE, TL16C_NO_PARITY, ONE_STOPBIT);*/
|
221 |
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); |
222 |
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); |
223 |
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); |
224 |
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); |
225 |
|
226 |
|
227 |
|
228 |
//Master Port
|
229 |
INIT_PORT(UART_BAUD_VALUE, F8BIT_MODE, ONE_STOPBIT, AVR32_NO_PARITY); |
230 |
|
231 |
setWait(500);
|
232 |
|
233 |
for(int i = 0; i < 25; i++) |
234 |
{ |
235 |
TX_WRITE(chrDebug[i]); |
236 |
} |
237 |
|
238 |
timerInit(60000); //10 ms interrupt |
239 |
sei(); //never forget to enable global interrupt mask !
|
240 |
|
241 |
PORTA.DIR = 0x3F;
|
242 |
|
243 |
//TODO : Enable WDT
|
244 |
|
245 |
while(1) |
246 |
{ |
247 |
|
248 |
//TODO : ADD WDT refresh
|
249 |
|
250 |
////////////////////////////////////////////////////////
|
251 |
//Read UART A
|
252 |
|
253 |
|
254 |
if(portA_available()) //Check if one byte available |
255 |
{ |
256 |
|
257 |
/////////////////////////////////////////
|
258 |
//Frame check
|
259 |
|
260 |
VCOM_can_read[PORT_VCOMA_INDEX] = true;
|
261 |
if(getParitySetA()) //if parity set |
262 |
{ |
263 |
if(getErrorStatusA()) //if parity failed (or stop bit also) |
264 |
{ |
265 |
VCOM_can_read[PORT_VCOMA_INDEX] = false;
|
266 |
} |
267 |
} |
268 |
|
269 |
/////////////////////////////////////////
|
270 |
|
271 |
if(VCOM_can_read[PORT_VCOMA_INDEX]) //if we can read according to parity |
272 |
{ |
273 |
readStatus[PORT_VCOMA_INDEX] = true;
|
274 |
bufferA[VCOMAIndex] = rxReadA(); |
275 |
VCOMAIndex++; |
276 |
VCOMA_counter = 0; //for frame timeout |
277 |
} |
278 |
else //if read failed |
279 |
{ |
280 |
rxReadA(); //flush serial
|
281 |
} |
282 |
} |
283 |
|
284 |
////////////////////////////////////////////////////////
|
285 |
//Read UART B
|
286 |
|
287 |
|
288 |
if(portB_available()) //Check if one byte available |
289 |
{ |
290 |
/////////////////////////////////////////
|
291 |
//Frame check
|
292 |
|
293 |
VCOM_can_read[PORT_VCOMB_INDEX] = true;
|
294 |
if(getParitySetB()) //if parity set |
295 |
{ |
296 |
if(getErrorStatusB()) //if parity failed (or stop bit also) |
297 |
{ |
298 |
VCOM_can_read[PORT_VCOMB_INDEX] = false;
|
299 |
} |
300 |
} |
301 |
|
302 |
/////////////////////////////////////////
|
303 |
|
304 |
|
305 |
|
306 |
if(VCOM_can_read[PORT_VCOMB_INDEX]) //if we can read according to parity |
307 |
{ |
308 |
readStatus[PORT_VCOMB_INDEX] = true;
|
309 |
bufferB[VCOMBIndex] = rxReadB(); |
310 |
VCOMBIndex++; |
311 |
//if(VCOMBIndex > 64)
|
312 |
// VCOMBIndex = 0;
|
313 |
VCOMB_counter = 0; //for frame timeout |
314 |
} |
315 |
else //if read failed |
316 |
{ |
317 |
rxReadB(); //flush serial
|
318 |
} |
319 |
} |
320 |
|
321 |
|
322 |
////////////////////////////////////////////////////////
|
323 |
//Read UART C
|
324 |
|
325 |
|
326 |
if(portC_available()) //Check if one byte available |
327 |
{ |
328 |
/////////////////////////////////////////
|
329 |
//Frame check
|
330 |
|
331 |
VCOM_can_read[PORT_VCOMC_INDEX] = true;
|
332 |
if(getParitySetC()) //if parity set |
333 |
{ |
334 |
if(getErrorStatusC()) //if parity failed (or stop bit also) |
335 |
{ |
336 |
VCOM_can_read[PORT_VCOMC_INDEX] = false;
|
337 |
} |
338 |
} |
339 |
|
340 |
/////////////////////////////////////////
|
341 |
|
342 |
|
343 |
|
344 |
if(VCOM_can_read[PORT_VCOMC_INDEX]) //if we can read according to parity |
345 |
{ |
346 |
readStatus[PORT_VCOMC_INDEX] = true;
|
347 |
bufferC[VCOMCIndex] = rxReadC(); |
348 |
VCOMCIndex++; |
349 |
//if(VCOMBIndex > 64)
|
350 |
// VCOMBIndex = 0;
|
351 |
VCOMC_counter = 0; //for frame timeout |
352 |
} |
353 |
else //if read failed |
354 |
{ |
355 |
rxReadC(); //flush serial
|
356 |
} |
357 |
} |
358 |
|
359 |
|
360 |
|
361 |
////////////////////////////////////////////////////////
|
362 |
//Read UART D
|
363 |
|
364 |
|
365 |
if(portD_available()) //Check if one byte available |
366 |
{ |
367 |
/////////////////////////////////////////
|
368 |
//Frame check
|
369 |
|
370 |
VCOM_can_read[PORT_VCOMD_INDEX] = true;
|
371 |
if(getParitySetD()) //if parity set |
372 |
{ |
373 |
if(getErrorStatusD()) //if parity failed (or stop bit also) |
374 |
{ |
375 |
VCOM_can_read[PORT_VCOMD_INDEX] = false;
|
376 |
} |
377 |
} |
378 |
|
379 |
/////////////////////////////////////////
|
380 |
|
381 |
|
382 |
|
383 |
if(VCOM_can_read[PORT_VCOMD_INDEX]) //if we can read according to parity |
384 |
{ |
385 |
readStatus[PORT_VCOMD_INDEX] = true;
|
386 |
bufferD[VCOMDIndex] = rxReadD(); |
387 |
VCOMDIndex++; |
388 |
//if(VCOMBIndex > 64)
|
389 |
// VCOMBIndex = 0;
|
390 |
VCOMD_counter = 0; //for frame timeout |
391 |
} |
392 |
else //if read failed |
393 |
{ |
394 |
rxReadD(); //flush serial
|
395 |
} |
396 |
} |
397 |
|
398 |
|
399 |
////////////////////////////////////////////////////////
|
400 |
//Read UART Master
|
401 |
|
402 |
|
403 |
if(PORT_AVAILABLE > 0) //check if we have bytes to read |
404 |
{ |
405 |
readStatus[PORT_MCOM_INDEX] = true;
|
406 |
masterBuffer[MCOMIndex] = RX_READ; //store byte into buffer
|
407 |
MCOMIndex++; //increment to next byte
|
408 |
//if(MCOMIndex > 64)
|
409 |
// MCOMIndex = 0;
|
410 |
MCOM_counter = 0; //refresh timeout counter |
411 |
} |
412 |
else
|
413 |
{ |
414 |
readStatus[PORT_MCOM_INDEX] = false;
|
415 |
} |
416 |
|
417 |
|
418 |
|
419 |
|
420 |
//Timed process (for leds, etc...)
|
421 |
if(getTimerFlag())
|
422 |
{ |
423 |
/////////////////////////////////////////////////////////
|
424 |
//Timeout process
|
425 |
|
426 |
//For master port
|
427 |
if(MCOMIndex > 0) |
428 |
MCOM_counter++; |
429 |
|
430 |
//For virtual port 1
|
431 |
if(VCOMAIndex > 0) |
432 |
VCOMA_counter++; |
433 |
|
434 |
//For virtual port 2
|
435 |
if(VCOMBIndex > 0) |
436 |
VCOMB_counter++; |
437 |
|
438 |
//For virtual port 3
|
439 |
if(VCOMCIndex > 0) |
440 |
VCOMC_counter++; |
441 |
|
442 |
//For virtual port 4
|
443 |
if(VCOMDIndex > 0) |
444 |
VCOMD_counter++; |
445 |
|
446 |
|
447 |
senseDebugLeds(&writtenStatus, readStatus); |
448 |
clearTimerFlag(); |
449 |
} |
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
///////////////////////////////////////
|
456 |
//When buf is ready from master
|
457 |
if(MCOM_counter > SERIAL_PORT_TIMEOUT_COUNT || MCOMIndex >= 64) |
458 |
{ |
459 |
//Send buffer to VCOM
|
460 |
if(masterBuffer[1] == MASTER_ID) //check if frame is destinated to the MUX232 |
461 |
{ |
462 |
MCOM_scanframe(masterBuffer, MCOMIndex, VCOM_cfg); |
463 |
} |
464 |
else //otherwise send it through a VCOM |
465 |
{ |
466 |
VCOM_sendframe(masterBuffer, MCOMIndex); |
467 |
} |
468 |
|
469 |
MCOMIndex = 0; //reset index buffer |
470 |
MCOM_counter = 0; //reset counter |
471 |
} |
472 |
|
473 |
|
474 |
///////////////////////////////////////
|
475 |
//When buf is ready from VCOMA
|
476 |
|
477 |
if(VCOMA_counter > SERIAL_PORT_TIMEOUT_COUNT || VCOMAIndex >= 64) |
478 |
{ |
479 |
writtenStatus = true;//Say a wrote has been made |
480 |
//Send buffer to VCOM
|
481 |
MCOM_sendframe(bufferA, VCOMAIndex, PORT_VCOMA_INDEX); |
482 |
VCOMAIndex = 0; //reset index buffer |
483 |
VCOMA_counter = 0; //reset counter |
484 |
} |
485 |
|
486 |
|
487 |
|
488 |
///////////////////////////////////////
|
489 |
//When buf is ready from VCOMB
|
490 |
|
491 |
if(VCOMB_counter > SERIAL_PORT_TIMEOUT_COUNT || VCOMBIndex >= 64) |
492 |
{ |
493 |
writtenStatus = true;//Say a wrote has been made |
494 |
//Send buffer to VCOM
|
495 |
MCOM_sendframe(bufferB, VCOMBIndex, PORT_VCOMB_INDEX); |
496 |
VCOMBIndex = 0; //reset index buffer |
497 |
VCOMB_counter = 0; //reset counter |
498 |
} |
499 |
|
500 |
|
501 |
///////////////////////////////////////
|
502 |
//When buf is ready from VCOMC
|
503 |
if(VCOMC_counter > SERIAL_PORT_TIMEOUT_COUNT || VCOMCIndex >= 64) |
504 |
{ |
505 |
writtenStatus = true;//Say a wrote has been made |
506 |
//Send buffer to VCOM
|
507 |
MCOM_sendframe(bufferC, VCOMCIndex, PORT_VCOMC_INDEX); |
508 |
VCOMCIndex = 0; //reset index buffer |
509 |
VCOMC_counter = 0; //reset counter |
510 |
} |
511 |
|
512 |
|
513 |
|
514 |
|
515 |
if(VCOMD_counter > SERIAL_PORT_TIMEOUT_COUNT || VCOMDIndex >= 64) |
516 |
{ |
517 |
|
518 |
writtenStatus = true; //Say a wrote has been made |
519 |
//Send buffer to VCOM
|
520 |
MCOM_sendframe(bufferD, VCOMDIndex, PORT_VCOMD_INDEX); |
521 |
VCOMDIndex = 0; //reset index buffer |
522 |
VCOMD_counter = 0; //reset counter |
523 |
} |
524 |
|
525 |
|
526 |
} |
527 |
|
528 |
|
529 |
|
530 |
return 0; |
531 |
} |
532 |
|
533 |
|
534 |
|
535 |
|
536 |
///////////////////////////////////////////////////////////
|
537 |
//Functions / macros
|
538 |
|
539 |
void readCFG(bool EEPROM_read, cfgPort *localPort) |
540 |
{ |
541 |
cfgPort *debugPort; |
542 |
|
543 |
const uint8_t *dispBaud[] = {"9600", "19200", "38400"}; |
544 |
const uint8_t dispParity[] = {'N', 'E', 'O'}; //The Matrix has you... |
545 |
const uint8_t dispData[] = {'5', '6', '7', '8'}; |
546 |
const uint8_t dispBit[] = {'1', '2'}; |
547 |
char msg[30]; //23 characters max |
548 |
|
549 |
|
550 |
//Read EEPROM configuration or RAM ?
|
551 |
//if(EEPROM_read)
|
552 |
// getEEPROMCfg();
|
553 |
|
554 |
|
555 |
|
556 |
if(!EEPROM_read) //Read RAM CFG ? |
557 |
debugPort = &localPort[PORT_VCOMA_INDEX]; |
558 |
else
|
559 |
debugPort = &OLD_CFG[PORT_VCOMA_INDEX]; |
560 |
|
561 |
//readConfiguration(debugPort, PORT_VCOMA_INDEX); //or just get CFG read from EEPROM
|
562 |
//Disp CFGRead bytes and display directly CFG
|
563 |
for(int i = 0; i < 30; i++) |
564 |
{ |
565 |
msg[i] = 0;
|
566 |
} |
567 |
sprintf(msg, "%c%c%c%cVCOMA B%s F%c%c%c\r\n",
|
568 |
STX, |
569 |
MASTER_ID, |
570 |
FRAME_ADDR_1, |
571 |
FRAME_ADDR_2, |
572 |
dispBaud[debugPort->baud], |
573 |
dispData[debugPort->dataByte], |
574 |
dispParity[debugPort->parity], |
575 |
dispBit[debugPort->stopBit] |
576 |
); |
577 |
for(uint8_t i = 0; i < 22; i++) |
578 |
TX_WRITE(msg[i]); //Display string
|
579 |
|
580 |
if(!EEPROM_read) //Read RAM CFG ? |
581 |
debugPort = &localPort[PORT_VCOMB_INDEX]; |
582 |
else
|
583 |
debugPort = &OLD_CFG[PORT_VCOMB_INDEX]; |
584 |
//readConfiguration(debugPort, PORT_VCOMB_INDEX); //or just get CFG read from EEPROM
|
585 |
//Disp CFGRead bytes and display directly CFG
|
586 |
for(int i = 0; i < 30; i++) |
587 |
{ |
588 |
msg[i] = 0;
|
589 |
} |
590 |
|
591 |
sprintf(msg, "%c%c%c%cVCOMB B%s F%c%c%c\r\n",
|
592 |
STX, |
593 |
MASTER_ID, |
594 |
FRAME_ADDR_1, |
595 |
FRAME_ADDR_2, |
596 |
dispBaud[debugPort->baud], |
597 |
dispData[debugPort->dataByte], |
598 |
dispParity[debugPort->parity], |
599 |
dispBit[debugPort->stopBit] |
600 |
); |
601 |
for(uint8_t i = 0; i < 22; i++) |
602 |
TX_WRITE(msg[i]); //Display string
|
603 |
|
604 |
if(!EEPROM_read) //Read RAM CFG ? |
605 |
debugPort = &localPort[PORT_VCOMC_INDEX]; |
606 |
else
|
607 |
debugPort = &OLD_CFG[PORT_VCOMC_INDEX]; |
608 |
//readConfiguration(debugPort, PORT_VCOMC_INDEX); //or just get CFG read from EEPROM
|
609 |
//Disp CFGRead bytes and display directly CFG
|
610 |
for(int i = 0; i < 30; i++) |
611 |
{ |
612 |
msg[i] = 0;
|
613 |
} |
614 |
sprintf(msg, "%c%c%c%cVCOMC B%s F%c%c%c\r\n",
|
615 |
STX, |
616 |
MASTER_ID, |
617 |
FRAME_ADDR_1, |
618 |
FRAME_ADDR_2, |
619 |
dispBaud[debugPort->baud], |
620 |
dispData[debugPort->dataByte], |
621 |
dispParity[debugPort->parity], |
622 |
dispBit[debugPort->stopBit] |
623 |
); |
624 |
for(uint8_t i = 0; i < 22; i++) |
625 |
TX_WRITE(msg[i]); //Display string
|
626 |
|
627 |
if(!EEPROM_read) //Read RAM CFG ? |
628 |
debugPort = &localPort[PORT_VCOMD_INDEX]; |
629 |
else
|
630 |
debugPort = &OLD_CFG[PORT_VCOMD_INDEX]; |
631 |
//readConfiguration(debugPort, PORT_VCOMD_INDEX); //or just get CFG read from EEPROM
|
632 |
//Disp CFGRead bytes and display directly CFG
|
633 |
for(int i = 0; i < 30; i++) |
634 |
{ |
635 |
msg[i] = 0;
|
636 |
} |
637 |
sprintf(msg, "%c%c%c%cVCOMD B%s F%c%c%c\r\n",
|
638 |
STX, |
639 |
MASTER_ID, |
640 |
FRAME_ADDR_1, |
641 |
FRAME_ADDR_2, |
642 |
dispBaud[debugPort->baud], |
643 |
dispData[debugPort->dataByte], |
644 |
dispParity[debugPort->parity], |
645 |
dispBit[debugPort->stopBit] |
646 |
); |
647 |
for(uint8_t i = 0; i < 22; i++) |
648 |
TX_WRITE(msg[i]); //Display string
|
649 |
} |
650 |
|
651 |
|
652 |
//Set RAM Cfg that we must apply it to the EEPROM !
|
653 |
void setRAMCfg(cfgPort *cfg, uint8_t *localBuffer)
|
654 |
{ |
655 |
//Set default CFG
|
656 |
uint8_t localBaudsCfg = ADDR_BAUD_9600, localDataCfg = ADDR_DATA_F8; |
657 |
uint8_t localParityCfg = ADDR_PARITY_NONE, localSBCfg = ADDR_STOP_ONE; |
658 |
uint8_t localVCOMIndex; |
659 |
uint8_t localIndex; |
660 |
uint8_t i; |
661 |
|
662 |
////////////////////////////////////////////////////////
|
663 |
//VCOM CONFIGURATION
|
664 |
|
665 |
//Be sure our first character is B (so we can estimate that frame is good enough)
|
666 |
if (localBuffer[MAX232_B_STR_INDEX] == MAX232_B_STR_CHAR)
|
667 |
{ |
668 |
localIndex = MAX232_B_STR_INDEX; |
669 |
i = 0;
|
670 |
//Calculate number of bytes between B and F char into frame (9600 bauds or 19200/38400 bauds for example)
|
671 |
while (localBuffer[localIndex] != MAX232_F_STR_CHAR)
|
672 |
{ |
673 |
i++; |
674 |
localIndex++; |
675 |
} |
676 |
if (i < 6) //number of char = 4 ? ("9600") |
677 |
{ |
678 |
|
679 |
//////////////////////////////////////////
|
680 |
//At first set bauds speed
|
681 |
if (localBuffer[MAX232_B_STR_INDEX + 1] == '9' && localBuffer[MAX232_B_STR_INDEX + 2] == '6' && |
682 |
localBuffer[MAX232_B_STR_INDEX + 3] == '0' && localBuffer[MAX232_B_STR_INDEX + 4] == '0') |
683 |
{ |
684 |
//Put CFG into 9600 bauds
|
685 |
localBaudsCfg = ADDR_BAUD_9600; |
686 |
} |
687 |
//////////////////////////////////////////
|
688 |
//Set Data bits
|
689 |
localDataCfg = localBuffer[MAX232_B_STR_INDEX + 6] - '5'; // Shift to 0 because ADDR_DATA_F5 = 0, ADDR_DATA_F6 = 1, ect... |
690 |
//We have already localDataCfg as ADDR_PARITY_NONE (keep aat this value if it equals to 'N')
|
691 |
//localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 7] == 'N') ? ADDR_PARITY_NONE : localDataCfg;
|
692 |
//Does it equals to ODD ?
|
693 |
localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 7] == 'O') ? ADDR_PARITY_ODD : localParityCfg; |
694 |
//or EVEN ?
|
695 |
localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 7] == 'E') ? ADDR_PARITY_EVEN : localParityCfg; |
696 |
//Read (localBuffer[MAX232_B_STR_INDEX + 8] for stop bits -> 1/2
|
697 |
localSBCfg = (localBuffer[MAX232_B_STR_INDEX + 8] == '2'); //Return condition state only... |
698 |
//Read (localBuffer[MAX232_B_STR_INDEX + 10] VCOM -> A/B/C/D
|
699 |
localVCOMIndex = localBuffer[MAX232_B_STR_INDEX + 10] - CESAR_SHIFT;
|
700 |
} |
701 |
else //number of char = 5 ? ("19200"/"38400") |
702 |
{ |
703 |
//////////////////////////////////////////
|
704 |
//At first set bauds speed
|
705 |
|
706 |
if (localBuffer[MAX232_B_STR_INDEX + 1] == '1' && localBuffer[MAX232_B_STR_INDEX + 2] == '9' && |
707 |
localBuffer[MAX232_B_STR_INDEX + 3] == '2' && localBuffer[MAX232_B_STR_INDEX + 4] == '0' && |
708 |
localBuffer[MAX232_B_STR_INDEX + 5] == '0') |
709 |
{ |
710 |
//Put CFG into 19200 bauds
|
711 |
localBaudsCfg = ADDR_BAUD_19200; |
712 |
} |
713 |
|
714 |
if (localBuffer[MAX232_B_STR_INDEX + 1] == '3' && localBuffer[MAX232_B_STR_INDEX + 2] == '8' && |
715 |
localBuffer[MAX232_B_STR_INDEX + 3] == '4' && localBuffer[MAX232_B_STR_INDEX + 4] == '0' && |
716 |
localBuffer[MAX232_B_STR_INDEX + 5] == '0') |
717 |
{ |
718 |
//Put CFG into 38400 bauds
|
719 |
localBaudsCfg = ADDR_BAUD_38400; |
720 |
} |
721 |
|
722 |
//////////////////////////////////////////
|
723 |
|
724 |
//Set Data bits
|
725 |
localDataCfg = localBuffer[MAX232_B_STR_INDEX + 7] - '5'; // Shift to 0 because ADDR_DATA_F5 = 0, ADDR_DATA_F6 = 1, ect... |
726 |
//We have already localDataCfg as ADDR_PARITY_NONE (keep aat this value if it equals to 'N')
|
727 |
//localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 7] == 'N') ? ADDR_PARITY_NONE : localDataCfg;
|
728 |
//Does it equals to ODD ?
|
729 |
localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 8] == 'O') ? ADDR_PARITY_ODD : localParityCfg; |
730 |
//or EVEN ?
|
731 |
localParityCfg = (localBuffer[MAX232_B_STR_INDEX + 8] == 'E') ? ADDR_PARITY_EVEN : localParityCfg; |
732 |
//Read (localBuffer[MAX232_B_STR_INDEX + 8] for stop bits -> 1/2
|
733 |
localSBCfg = (localBuffer[MAX232_B_STR_INDEX + 9] == '2'); //Return condition state only for stop bits... |
734 |
//Read (localBuffer[MAX232_B_STR_INDEX + 10] VCOM -> A/B/C/D
|
735 |
localVCOMIndex = localBuffer[MAX232_B_STR_INDEX + 11] - CESAR_SHIFT;
|
736 |
//TX_WRITE('F'); TX_WRITE('D'); TX_WRITE('P'); TX_WRITE(localBuffer[MAX232_B_STR_INDEX + 9]);
|
737 |
//TX_WRITE('F'); TX_WRITE('D'); TX_WRITE('P'); TX_WRITE(localBuffer[MAX232_B_STR_INDEX + 11]);
|
738 |
//TX_WRITE('F'); TX_WRITE('D'); TX_WRITE('P'); TX_WRITE(localParityCfg);
|
739 |
} |
740 |
//Write into RAM buffer (and prepare for EEPROM writing)
|
741 |
writeConfiguration(&cfg[localVCOMIndex], localBaudsCfg, localParityCfg, localDataCfg, localSBCfg, localVCOMIndex); |
742 |
} |
743 |
////////////////////////////////////////////////////////
|
744 |
|
745 |
} |
746 |
|
747 |
|
748 |
//Read a frame destinated to the MUX232 itself
|
749 |
|
750 |
void MCOM_scanframe(uint8_t *data, uint8_t n, cfgPort *localPort)
|
751 |
{ |
752 |
|
753 |
uint8_t *localBuffer; //Calculate byte quantity of effective data
|
754 |
uint8_t i; |
755 |
|
756 |
|
757 |
while(localBuffer == NULL) |
758 |
{ |
759 |
localBuffer = malloc(n-6);
|
760 |
setWait(5);
|
761 |
} |
762 |
|
763 |
for(i = 0; i < n-6; i++) |
764 |
localBuffer[i] = 0;
|
765 |
|
766 |
//At first, check if first byte is the start of text
|
767 |
if(data[0] == STX) |
768 |
{ |
769 |
if(data[2] == FRAME_ADDR_1 && data[3] == FRAME_ADDR_2) //check if we have the MUX address identifier |
770 |
{ |
771 |
if(data[n-1] == CR_CHAR) //check the end of text (second byte) |
772 |
{ |
773 |
if(data[n-2] == LF_CHAR) //check the end of text (first byte) |
774 |
{ |
775 |
for(i = 0; i < n-6; i++) //regarder la valeur de n |
776 |
{ |
777 |
localBuffer[i] = data[i + 4]; //Get effective data |
778 |
} |
779 |
//Once data was collected, analysis it
|
780 |
if(!stringComp(localBuffer, MAX232_WRITE_COMMAND, n-6, MAX232_WRITE_SIZE)) //if the command is Write configuration |
781 |
{ |
782 |
//Save CFG into EEPROM
|
783 |
setEEPROMCfg(); |
784 |
TX_WRITE(0x2);
|
785 |
TX_WRITE(FRAME_ADDR_1); |
786 |
TX_WRITE(FRAME_ADDR_2); |
787 |
TX_WRITE(MASTER_ID); |
788 |
TX_WRITE('W');TX_WRITE('R'); |
789 |
TX_WRITE('C');TX_WRITE('F');TX_WRITE('G'); |
790 |
TX_WRITE(LF_CHAR);TX_WRITE(CR_CHAR); |
791 |
} |
792 |
else if(!stringComp(localBuffer, MAX232_READ_RAM_COMMAND, n-6, MAX232_READ_RAM_SIZE)) //is this Read configuration ? |
793 |
{ |
794 |
//Read CFG from RAM
|
795 |
readCFG(false, localPort);
|
796 |
} |
797 |
else if(!stringComp(localBuffer, MAX232_READ_ROM_COMMAND, n-6, MAX232_READ_ROM_SIZE)) //is this Read configuration ? |
798 |
{ |
799 |
//Read CFG from EEPROM
|
800 |
readCFG(true, localPort);
|
801 |
} |
802 |
else //otherwise that might be set configuration |
803 |
{ |
804 |
setRAMCfg(localPort, localBuffer); |
805 |
TX_WRITE(0x2);
|
806 |
TX_WRITE(FRAME_ADDR_1); |
807 |
TX_WRITE(FRAME_ADDR_2); |
808 |
TX_WRITE(MASTER_ID); |
809 |
TX_WRITE('S');TX_WRITE('E');TX_WRITE('T'); |
810 |
TX_WRITE('C');TX_WRITE('F');TX_WRITE('G'); |
811 |
TX_WRITE(LF_CHAR);TX_WRITE(CR_CHAR); |
812 |
} |
813 |
} |
814 |
} |
815 |
} |
816 |
} |
817 |
} |
818 |
|
819 |
|
820 |
|
821 |
//Send a frame from virtual port to master port
|
822 |
|
823 |
void MCOM_sendframe(uint8_t *data, uint8_t n, uint8_t port)
|
824 |
{ |
825 |
//////////////////////////
|
826 |
//Send frame
|
827 |
|
828 |
TX_WRITE(0x02); //STX |
829 |
TX_WRITE(port + CESAR_SHIFT); //Which virtual port called master port ?
|
830 |
TX_WRITE(FRAME_ADDR_1); //ADDR
|
831 |
TX_WRITE(FRAME_ADDR_2); //ADDR
|
832 |
|
833 |
for(uint8_t i = 0; i < n; i++) //DATA |
834 |
TX_WRITE(data[i]); |
835 |
|
836 |
TX_WRITE(LF_CHAR); //End of frame
|
837 |
TX_WRITE(CR_CHAR); //End of frame
|
838 |
//////////////////////////
|
839 |
} |
840 |
|
841 |
//////////////////////////////////////////////////////////////////////////////////////////
|
842 |
//Send a frame from master port to virtual port (that calculate whose port to send ?)
|
843 |
|
844 |
void VCOM_sendframe(uint8_t *data, uint8_t n)
|
845 |
{ |
846 |
uint16_t addrFunctions[] = {&txWriteA, &txWriteB, &txWriteC, &txWriteD }; //list of available functions addresses
|
847 |
void (*localTxWrite)(); //function pointer |
848 |
|
849 |
|
850 |
////////////////////////////////
|
851 |
|
852 |
//At first, check if first byte is the start of text
|
853 |
if(data[0] == STX) |
854 |
{ |
855 |
if(data[2] == FRAME_ADDR_1 && data[3] == FRAME_ADDR_2) //check if we have the MUX address identifier |
856 |
{ |
857 |
if(data[n-1] == CR_CHAR) //check the end of text (second byte) |
858 |
{ |
859 |
if(data[n-2] == LF_CHAR) //check the end of text (first byte) |
860 |
{ |
861 |
data[1]-=CESAR_SHIFT; //convert port index to buffer equivalent |
862 |
localTxWrite = addrFunctions[(data[1] < 3) ? data[1] : 3 ]; //data[3] |
863 |
for(int i = 4; i < n-2; i++) //send frame without (STX + FRAME_ADDR_B1 + FRAME_ADDR_B2 + LF_CHAR + CR_CHAR) |
864 |
{ |
865 |
localTxWrite(data[i]); |
866 |
} |
867 |
|
868 |
} |
869 |
} |
870 |
} |
871 |
} |
872 |
} |
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
void bootSequence(void) |
881 |
{ |
882 |
debugLedsTest(); |
883 |
} |
884 |
|
885 |
|
886 |
|
887 |
|
888 |
/**
|
889 |
<p><b>void debugLedsTest(void)</b></p>
|
890 |
<p><b>Debug leds test sequence</b></p>
|
891 |
*/
|
892 |
void debugLedsTest(void) |
893 |
{ |
894 |
|
895 |
PORTA.OUT = 1;
|
896 |
for(int i = 0; i < 6; i++) |
897 |
{ |
898 |
PORTA.OUT <<= 1;
|
899 |
_delay_ms(125);
|
900 |
} |
901 |
PORTA.OUT = 0;
|
902 |
} |
903 |
|
904 |
/**
|
905 |
<p><b>void senseDebugLeds(void)</b></p>
|
906 |
<p><b>Call this to update leds status</b></p>
|
907 |
*/
|
908 |
void senseDebugLeds(bool *hasWritten, bool *hasRead) |
909 |
{ |
910 |
|
911 |
|
912 |
|
913 |
///////////////////////////
|
914 |
//Check Master Port
|
915 |
if(*hasWritten)
|
916 |
{ |
917 |
PORTA.OUT |= 0x20;
|
918 |
*hasWritten = false;
|
919 |
} |
920 |
else
|
921 |
PORTA.OUT &= ~(0x20);
|
922 |
|
923 |
if(hasRead[PORT_MCOM_INDEX])
|
924 |
{ |
925 |
PORTA.OUT |= 0x10;
|
926 |
hasRead[PORT_MCOM_INDEX] = false;
|
927 |
} |
928 |
else
|
929 |
PORTA.OUT &= ~(0x10);
|
930 |
|
931 |
///////////////////////////
|
932 |
//Check VCOMA
|
933 |
if(hasRead[PORT_VCOMA_INDEX])
|
934 |
{ |
935 |
PORTA.OUT |= 0x01;
|
936 |
hasRead[PORT_VCOMA_INDEX] = false;
|
937 |
} |
938 |
else
|
939 |
PORTA.OUT &= ~(0x01);
|
940 |
|
941 |
///////////////////////////
|
942 |
//Check VCOMB
|
943 |
if(hasRead[PORT_VCOMB_INDEX])
|
944 |
{ |
945 |
PORTA.OUT |= 0x02;
|
946 |
hasRead[PORT_VCOMB_INDEX] = false;
|
947 |
} |
948 |
else
|
949 |
PORTA.OUT &= ~(0x02);
|
950 |
|
951 |
///////////////////////////
|
952 |
//Check VCOMC
|
953 |
if(hasRead[PORT_VCOMC_INDEX])
|
954 |
{ |
955 |
PORTA.OUT |= 0x04;
|
956 |
hasRead[PORT_VCOMC_INDEX] = false;
|
957 |
} |
958 |
else
|
959 |
PORTA.OUT &= ~(0x04);
|
960 |
|
961 |
|
962 |
///////////////////////////
|
963 |
//Check VCOMD
|
964 |
if(hasRead[PORT_VCOMD_INDEX])
|
965 |
{ |
966 |
PORTA.OUT |= 0x08;
|
967 |
hasRead[PORT_VCOMD_INDEX] = false;
|
968 |
} |
969 |
else
|
970 |
PORTA.OUT &= ~(0x08);
|
971 |
} |
972 |
|
973 |
|
974 |
//String comparator (better version...)
|
975 |
bool stringComp(uint8_t *str1, uint8_t *str2, uint8_t n, uint8_t m)
|
976 |
{ |
977 |
bool result = false; |
978 |
|
979 |
if(n == m)
|
980 |
{ |
981 |
for(int i = 0; i < n; i++) |
982 |
{ |
983 |
//TX_WRITE('A');TX_WRITE(':');TX_WRITE(str1[i]);TX_WRITE(" ");
|
984 |
//TX_WRITE('B');TX_WRITE(':');TX_WRITE(str2[i]);TX_WRITE("\n");TX_WRITE("\r");
|
985 |
if(str1[i] != str2[i]) //different string... |
986 |
result = true; //haaaaaaaaaaaaaaaaaaaaaaaaaaaax |
987 |
} |
988 |
} |
989 |
else
|
990 |
{ |
991 |
result = true;
|
992 |
} |
993 |
|
994 |
|
995 |
|
996 |
return result;
|
997 |
} |
998 |
|
999 |
|
1000 |
|
1001 |
|
1002 |
|
1003 |
|