root / Version 0.6 / RS232_MUX.X / main.c @ c9e951f9
Historique | Voir | Annoter | Télécharger (5,438 ko)
1 |
/*
|
---|---|
2 |
* File: main.c
|
3 |
* Author: eniro
|
4 |
*
|
5 |
* Created on March 25, 2024, 3:51 PM
|
6 |
*/
|
7 |
|
8 |
#define F_CPU 24000000UL |
9 |
|
10 |
#include <xc.h> |
11 |
#include <util/delay.h> |
12 |
#include <avr/interrupt.h> |
13 |
|
14 |
#include "hardware_uart.h" |
15 |
|
16 |
|
17 |
|
18 |
//USART setup
|
19 |
#define UART_BAUD_VALUE 10000 // 10000 is equivalent as 9600 bauds according to datasheet |
20 |
#define USART0_REG 0x3 |
21 |
|
22 |
|
23 |
//OSC. setup
|
24 |
#define FREQSEL 0x9 |
25 |
#define _FREQSEL_REG_WR ((FREQSEL) << 2) |
26 |
#define _USART0_REG_WR (USART0_REG & 0x7) |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
//////////////////////////////////////////////////////////////
|
32 |
//Prototypes
|
33 |
/*
|
34 |
//USART0 functions
|
35 |
void txWrite0(uint8_t b); //Write 1 byte to the buffer USART0
|
36 |
uint8_t rxRead0(void); //read 1 byte from buffer USART0
|
37 |
//USART1 functions
|
38 |
void txWrite1(uint8_t b); //Write 1 byte to the buffer USART1
|
39 |
uint8_t rxRead1(void); //read 1 byte from buffer USART1
|
40 |
//USART2 functions
|
41 |
void txWrite2(uint8_t b); //Write 1 byte to the buffer USART2
|
42 |
uint8_t rxRead2(void); //read 1 byte from buffer USART2
|
43 |
//USART3 functions
|
44 |
void txWrite3(uint8_t b); //Write 1 byte to the buffer USART3
|
45 |
uint8_t rxRead3(void); //read 1 byte from buffer USART3
|
46 |
//USART4 functions
|
47 |
void txWrite4(uint8_t b); //Write 1 byte to the buffer USART4
|
48 |
uint8_t rxRead4(void); //read 1 byte from buffer USART4
|
49 |
*/
|
50 |
void bootSequence(void); |
51 |
|
52 |
|
53 |
int main(void) { |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
uint8_t localBuf0[50], localBuf1[50], localBuf2[50], localBuf3[50], localBuf4[50]; |
59 |
uint8_t delay_mux = 0;
|
60 |
int k0 = 0; |
61 |
int k1 = 0; |
62 |
int k2 = 0; |
63 |
int k3 = 0; |
64 |
int k4 = 0; |
65 |
|
66 |
|
67 |
|
68 |
PORTD.DIR = 0x01; // DEBUG LED |
69 |
//PORTMUX.USARTROUTEA = _USART0_REG_WR; //set TX and RX on PD4 and PD5 pins
|
70 |
_PROTECTED_WRITE(CLKCTRL.OSCHFCTRLA, _FREQSEL_REG_WR); //switch to 24 MHz
|
71 |
|
72 |
|
73 |
initPort0(UART_BAUD_VALUE, true); //init master port with debug leds |
74 |
initPort1(UART_BAUD_VALUE, true); //init vcom1 port with debug leds |
75 |
initPort2(UART_BAUD_VALUE, true); //init vcom2 port with debug leds |
76 |
initPort3(UART_BAUD_VALUE, true); //init vcom3 port with debug leds |
77 |
initPort4(UART_BAUD_VALUE, true); //init vcom4 port with debug leds |
78 |
sei(); //never forget to enable global interrupt mask !
|
79 |
|
80 |
|
81 |
bootSequence(); //Launch leds sequence
|
82 |
|
83 |
|
84 |
|
85 |
while(1) |
86 |
{ |
87 |
|
88 |
|
89 |
PORTD.OUT ^= 0x01;
|
90 |
txWrite0(0x45);
|
91 |
txWrite1(0x46);
|
92 |
txWrite2(0x47);
|
93 |
txWrite3(0x48);
|
94 |
txWrite4(0x49);
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
if(portAvailable0() > 0) //check if we have bytes to read |
100 |
{ |
101 |
localBuf0[k0] = rxRead0(); //store byte into buffer
|
102 |
k0++; //increment to next byte
|
103 |
} |
104 |
|
105 |
if(portAvailable1() > 0) //check if we have bytes to read |
106 |
{ |
107 |
localBuf1[k1] = rxRead1(); //store byte into buffer
|
108 |
k1++; //increment to next byte
|
109 |
} |
110 |
|
111 |
if(portAvailable2() > 0) //check if we have bytes to read |
112 |
{ |
113 |
localBuf2[k2] = rxRead2(); //store byte into buffer
|
114 |
k2++; //increment to next byte
|
115 |
} |
116 |
|
117 |
if(portAvailable3() > 0) //check if we have bytes to read |
118 |
{ |
119 |
localBuf3[k3] = rxRead3(); //store byte into buffer
|
120 |
k3++; //increment to next byte
|
121 |
} |
122 |
|
123 |
if(portAvailable4() > 0) //check if we have bytes to read |
124 |
{ |
125 |
localBuf4[k4] = rxRead4(); //store byte into buffer
|
126 |
k4++; //increment to next byte
|
127 |
} |
128 |
|
129 |
|
130 |
if(localBuf0[0] == 0x42 && localBuf0[1] == 0x44) |
131 |
{ |
132 |
//Clear buffer
|
133 |
localBuf0[0] = 0x00; |
134 |
localBuf0[1] = 0x00; |
135 |
k0 = 0;
|
136 |
delay_mux = 0;
|
137 |
} |
138 |
|
139 |
if(localBuf1[0] == 0x42 && localBuf1[1] == 0x44) |
140 |
{ |
141 |
//Clear buffer
|
142 |
localBuf1[0] = 0x00; |
143 |
localBuf1[1] = 0x00; |
144 |
k1 = 0;
|
145 |
delay_mux = 1;
|
146 |
} |
147 |
|
148 |
if(localBuf2[0] == 0x42 && localBuf2[1] == 0x44) |
149 |
{ |
150 |
//Clear buffer
|
151 |
localBuf2[0] = 0x00; |
152 |
localBuf2[1] = 0x00; |
153 |
k2 = 0;
|
154 |
delay_mux = 2;
|
155 |
} |
156 |
|
157 |
if(localBuf3[0] == 0x42 && localBuf3[1] == 0x44) |
158 |
{ |
159 |
//Clear buffer
|
160 |
localBuf3[0] = 0x00; |
161 |
localBuf3[1] = 0x00; |
162 |
k3 = 0;
|
163 |
delay_mux = 3;
|
164 |
} |
165 |
|
166 |
if(localBuf4[0] == 0x42 && localBuf4[1] == 0x44) |
167 |
{ |
168 |
//Clear buffer
|
169 |
localBuf4[0] = 0x00; |
170 |
localBuf4[1] = 0x00; |
171 |
k4 = 0;
|
172 |
delay_mux = 4;
|
173 |
} |
174 |
|
175 |
//Just in case if bytes does not match, clear counter
|
176 |
if(k0 >= 2) |
177 |
k0 = 0;
|
178 |
|
179 |
if(k1 >= 2) |
180 |
k1 = 0;
|
181 |
|
182 |
if(k2 >= 2) |
183 |
k2 = 0;
|
184 |
|
185 |
if(k3 >= 2) |
186 |
k3 = 0;
|
187 |
|
188 |
if(k4 >= 2) |
189 |
k4 = 0;
|
190 |
|
191 |
switch(delay_mux)
|
192 |
{ |
193 |
case 0: |
194 |
_delay_ms(250);
|
195 |
break;
|
196 |
|
197 |
case 1: |
198 |
_delay_ms(500);
|
199 |
break;
|
200 |
|
201 |
case 2: |
202 |
_delay_ms(1000);
|
203 |
break;
|
204 |
|
205 |
case 3: |
206 |
_delay_ms(2000);
|
207 |
break;
|
208 |
|
209 |
default:
|
210 |
_delay_ms(3000);
|
211 |
} |
212 |
|
213 |
} |
214 |
|
215 |
|
216 |
|
217 |
|
218 |
return 0; |
219 |
} |
220 |
|
221 |
|
222 |
|
223 |
|
224 |
///////////////////////////////////////////////////////////
|
225 |
//Functions / macros
|
226 |
|
227 |
|
228 |
void bootSequence(void) |
229 |
{ |
230 |
debugLedsTest(); |
231 |
} |
232 |
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|