Statistiques
| Branche: | Révision:

root / Version 1.2 / RS232_MUX.X / trash_code_unused.txt @ master

Historique | Voir | Annoter | Télécharger (9,401 ko)

1
Section to put temporary code that we use in a close future....
2

    
3

    
4
//////////////////////////////////////////////////////////////////////////
5
//Start of code
6

    
7

    
8

    
9

    
10
    ////////////////////////////////////////////////////////////
11
    //Local variables
12
    
13
    const char *boot_msg = BOOT_MSG;
14
    
15
    cfgPort cfgVCOM1, cfgVCOM2, cfgVCOM3, cfgVCOM4;
16
    uint8_t ledsCfg;
17
    
18
    
19
    
20
    //Output buffer
21
    uint8_t MCOMBuf[256], VCOM1Buf[256], VCOM2Buf[256], VCOM3Buf[256], VCOM4Buf[256];
22
    //Output buffer index 
23
    uint8_t MCOMIndex = 0, VCOM1Index = 0, VCOM2Index = 0, VCOM3Index = 0, VCOM4Index = 0;
24
    //local timer counter for local process (blink led, ect...)
25
    uint32_t timerCnt = 0;
26
    //local timer for counting how many cycles passed since we've got activity on serial port ?
27
    uint32_t serialTimer = 0;
28
    
29
    //Counter to make a timeout if all data has passed
30
    uint16_t MCOM_counter = 0;
31
    uint16_t VCOM1_counter = 0;
32
    uint16_t VCOM2_counter = 0;
33
    uint16_t VCOM3_counter = 0;
34
    uint16_t VCOM4_counter = 0;
35
    
36
    
37
    for(int i = 0; i < 256; i++)
38
    {
39
        MCOMBuf[i] = 0;
40
        VCOM1Buf[i] = 0;
41
        VCOM2Buf[i] = 0;
42
        VCOM3Buf[i] = 0;
43
        VCOM4Buf[i] = 0;
44
    }
45
    
46
   
47
    PORTD.DIR = 0x01; // DEBUG LED
48
    //PORTMUX.USARTROUTEA = _USART0_REG_WR; //set TX and RX on PD4 and PD5 pins
49
    _PROTECTED_WRITE(CLKCTRL.OSCHFCTRLA, _FREQSEL_REG_WR); //switch to 24 MHz
50
    
51
    ///////////////////////////////////////////
52
    //Prepare VCOM configuration
53
    
54
    //At first read EEPROM !
55
    getEEPROMCfg(); //Get serial port configuration
56
    ledsCfg = getDebugLedsConfiguration(); //get debug leds configuration
57
    
58
    //And then, read configuration for each port...
59
    
60
    readConfiguration(&cfgVCOM1, 0);
61
    readConfiguration(&cfgVCOM2, 1);
62
    readConfiguration(&cfgVCOM3, 2);
63
    readConfiguration(&cfgVCOM4, 3);
64
    
65
    ///////////////////////////////////////////
66
    //Interrupt & serial port setup
67
    
68
    //Set port with debug leds
69
    
70
    initPort1(UART_BAUD_VALUE, (ledsCfg & 0x1), F8BIT_MODE, ONE_STOPBIT, EVEN_PARITY);
71
    
72
    //initPort0(UART_BAUD_VALUE, (ledsCfg & 0x1), F8BIT_MODE, ONE_STOPBIT, EVEN_PARITY); //init master port with debug leds
73
    //initPort1(cfgVCOM1.baud, (ledsCfg & 0x2) >> 1, cfgVCOM1.dataByte, cfgVCOM1.stopBit, cfgVCOM1.parity); //init vcom1 port with debug leds
74
    //initPort2(cfgVCOM2.baud, (ledsCfg & 0x4) >> 2, cfgVCOM2.dataByte, cfgVCOM2.stopBit, cfgVCOM2.parity); //init vcom2 port with debug leds
75
    //initPort3(cfgVCOM3.baud, (ledsCfg & 0x8) >> 3, cfgVCOM3.dataByte, cfgVCOM3.stopBit, cfgVCOM3.parity); //init vcom3 port with debug leds
76
    //initPort4(cfgVCOM4.baud, (ledsCfg & 0x16) >> 4, cfgVCOM4.dataByte, cfgVCOM4.stopBit, cfgVCOM4.parity); //init vcom4 port  with debug leds     
77
    //Timer init
78
    //Setup for interrupt every 25ms
79
    timerInterruptInit(0x2EE0);
80
    
81
    sei(); //never forget to enable global interrupt mask !
82
    
83
    ///////////////////////////////////////////
84
    
85
    //Debug led on boot sequence
86
    WATCHDOG_RESET; //reset WD counter
87
    bootSequence(); //Launch leds sequence
88
    
89
    
90
    for(uint8_t i = 0; i < 11; i++)
91
    {
92
        txWrite0(boot_msg[i]);
93
    }
94
    
95
    while(1)
96
    {
97
        WATCHDOG_RESET; //Always call this to prevent undesired boot...
98
        
99
        
100
        ///////////////////////////////////////////////////////////////
101
        //Master Port buffer read
102
        
103
        if(portAvailable0() > 0)
104
        {
105
            MCOMBuf[MCOMIndex] = rxRead0(); //store byte into buffer
106
            MCOMIndex++; //increment to next byte
107
            global_n = MCOMIndex;
108
            MCOM_counter = 0; //refresh timeout counter
109
        }
110

    
111
            
112
        
113
        
114
        ///////////////////////////////////////////////////////////////
115
        //Virtual Port 1 buffer read
116
        
117
        if(portAvailable1() > 0) //check if we have bytes to read
118
        {
119
            VCOM1Buf[VCOM1Index] = rxRead1(); //store byte into buffer
120
            VCOM1Index++; //increment to next byte
121
            VCOM1_counter = 0; //refresh timeout counter
122
        }
123

    
124
        
125
        
126
        ///////////////////////////////////////////////////////////////
127
        //Virtual Port 2 buffer read
128
       
129
        if(portAvailable2() > 0) //check if we have bytes to read
130
        {
131
            VCOM2Buf[VCOM2Index] = rxRead2(); //store byte into buffer
132
            VCOM2Index++; //increment to next byte
133
            VCOM2_counter = 0; //refresh timeout counter
134
        }
135

    
136
        
137
        
138
        ///////////////////////////////////////////////////////////////
139
        //Virtual Port 3 buffer read
140

    
141
        if(portAvailable3() > 0) //check if we have bytes to read
142
        {
143
            VCOM3Buf[VCOM3Index] = rxRead3(); //store byte into buffer
144
            VCOM3Index++; //increment to next byte
145
            VCOM3_counter = 0; //refresh timeout counter
146
        }
147

    
148
        
149
        
150
        
151
        ///////////////////////////////////////////////////////////////
152
        //Virtual Port 4 buffer read
153

    
154
        if(portAvailable4() > 0) //check if we have bytes to read
155
        {
156
            VCOM4Buf[VCOM4Index] = rxRead4(); //store byte into buffer
157
            VCOM4Index++; //increment to next byte
158
            VCOM4_counter = 0; //refresh timeout counter
159
        }
160
        
161
        ////////////////////////////////////////////////////////////////////////
162
        
163
        
164
        ///////////////////////////////////////
165
        //When buf is ready
166
        if(MCOM_counter > SERIAL_PORT_TIMEOUT_COUNT)
167
        {         
168
            //Send buffer to VCOM
169
            MCOM_sendframe(MCOMBuf, MCOMIndex);
170
            
171
            //Clear the buffer
172
            for(uint8_t i = 0; i < MCOMIndex; i++)
173
            {
174
                MCOMBuf[i] = 0;
175
            }
176
            MCOMIndex = 0; //reset index buffer
177
            MCOM_counter = 0; //reset counter
178
        }
179
        
180
        
181
        ///////////////////////////////////////
182
        //When buf is ready
183
        if(VCOM1_counter > SERIAL_PORT_TIMEOUT_COUNT)
184
        {
185
            //Send buffer to VCOM
186
            VCOM_sendframe(VCOM1Buf, VCOM1Index, VCOM1_INDEX);
187
            
188
            //Clear the buffer
189
            for(uint8_t i = 0; i < VCOM1Index; i++)
190
            {
191
                VCOM1Buf[i] = 0;
192
            }
193
            VCOM1Index = 0; //reset index buffer
194
            VCOM1_counter = 0; //reset counter
195
        }
196
        
197
        
198
        ///////////////////////////////////////
199
        //When buf is ready
200
        if(VCOM2_counter > SERIAL_PORT_TIMEOUT_COUNT)
201
        {
202
            //Send buffer to VCOM
203
            VCOM_sendframe(VCOM2Buf, VCOM2Index, VCOM2_INDEX);
204
            
205
            //Clear the buffer
206
            for(uint8_t i = 0; i < VCOM2Index; i++)
207
            {
208
                VCOM2Buf[i] = 0;
209
            }
210
            VCOM2Index = 0; //reset index buffer
211
            VCOM2_counter = 0; //reset counter
212
        }
213
        
214
        
215
        ///////////////////////////////////////
216
        //When buf is ready
217
        if(VCOM3_counter > SERIAL_PORT_TIMEOUT_COUNT)
218
        {
219
            //Send buffer to VCOM
220
            VCOM_sendframe(VCOM3Buf, VCOM3Index, VCOM3_INDEX);
221
            
222
            //Clear the buffer
223
            for(uint8_t i = 0; i < VCOM3Index; i++)
224
            {
225
                VCOM3Buf[i] = 0;
226
            }
227
            VCOM3Index = 0; //reset index buffer
228
            VCOM3_counter = 0; //reset counter
229
        }
230
        
231
        
232
        ///////////////////////////////////////
233
        //When buf is ready
234
        if(VCOM4_counter > SERIAL_PORT_TIMEOUT_COUNT)
235
        {
236
            //Send buffer to VCOM
237
            VCOM_sendframe(VCOM4Buf, VCOM4Index, VCOM4_INDEX);
238
            
239
            //Clear the buffer
240
            for(uint8_t i = 0; i < VCOM4Index; i++)
241
            {
242
                VCOM4Buf[i] = 0;
243
            }
244
            VCOM4Index = 0; //reset index buffer
245
            VCOM4_counter = 0; //reset counter
246
        }
247
        ////////////////////////////////////////////////////////////////////////
248
        
249
        //NOTE : Not sure that stuff will be useful...
250
        //Just in case if bytes does not match, clear counter
251

    
252
        
253
        
254
        //Refresh led status
255
        if(abs(getTimerCounts() - timerCnt) >= 25)
256
        {
257
            senseDebugLeds(); //display leds status
258
            timerCnt = getTimerCounts(); //refresh counter
259
        }
260
        
261
        //count
262
        if(abs(getTimerCounts() - serialTimer) >= TIMEBASE_COUNT)
263
        {
264
            //prevent incrementing counter if no bytes available...
265
            
266
            //For master port
267
            if(MCOMIndex > 0)
268
                MCOM_counter++;
269
            
270
            //For virtual port 1
271
            if(VCOM1Index > 0)
272
                VCOM1_counter++;
273
            
274
            //For virtual port 2
275
            if(VCOM2Index > 0)
276
                VCOM2_counter++;
277
            
278
            //For virtual port 3
279
            if(VCOM3Index > 0)
280
                VCOM3_counter++;
281
            
282
            //For virtual port 4
283
            if(VCOM4Index > 0)
284
                VCOM4_counter++;
285
            
286
            
287
            if(MCOMIndex >= 255)
288
            MCOMIndex = 0;
289
        
290
            if(VCOM1Index >= 255)
291
                VCOM1Index = 0;
292
        
293
            if(VCOM2Index >= 255)
294
                VCOM2Index = 0;
295
        
296
            if(VCOM3Index >= 255)
297
                VCOM3Index = 0;
298
        
299
            if(VCOM4Index >= 255)
300
                VCOM4Index = 0;
301
            
302
            
303
            serialTimer = getTimerCounts(); //refresh counter
304
        }
305
    }