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