Subversion Repositories HomeAutomation

Rev

Details | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
51 johboh 1
/*********************************************************************
2
 *
3
 *                  Main application
4
 *
5
 *********************************************************************
97 johboh 6
 * FileName:        $HeadURL: https://svn.auml.se/svn/HomeAutomation/trunk/EmbeddedSoftware/PIC/canToSerial/Source/Main.c $
7
 * Last changed:    $LastChangedDate: 2006-11-14 21:22:17 +0100 (Tue, 14 Nov 2006) $
8
 * By:              $LastChangedBy: johboh $
9
 * Revision:        $Revision: 97 $
51 johboh 10
 ********************************************************************/
11
 
53 johboh 12
#include <compiler.h>
13
#include <stackTasks.h>
14
#include <Tick.h>
51 johboh 15
 
16
#ifdef USE_CAN
53 johboh 17
    #include <CAN.h>
51 johboh 18
#endif
19
 
20
#ifdef USE_UART
53 johboh 21
    #include <uart.h>
51 johboh 22
#endif
23
 
24
 
25
 
26
static void mainInit(void);
27
 
28
void main()
29
{
30
    static TICK t = 0;
31
 
32
    // Inits
33
 
34
    mainInit();
35
 
36
    #ifdef USE_CAN
37
        canInit();
38
    #endif
39
 
40
    #ifdef USE_UART
41
        uartInit();
42
    #endif
43
 
44
    tickInit();
45
 
64 johboh 46
 
47
 
51 johboh 48
    while(1)
64 johboh 49
    {  
51 johboh 50
    }
51
 
52
}
53
 
54
 
55
#pragma interruptlow HighISR
56
void HighISR(void)
57
{
58
 
59
 
60
    #ifdef USE_UART
61
        uartISR();
62
    #endif
63
 
64
    #ifdef USE_CAN
65
        canISR();
66
    #endif
67
 
68
 
69
    tickUpdate();
70
}
71
#pragma code highVector=0x08
72
void HighVector (void)
73
{
74
    _asm goto HighISR _endasm
75
}
76
#pragma code // Return to default code section
77
 
78
 
79
 
80
#pragma interruptlow LowISR
81
void LowISR(void)
82
{
83
 
84
}
85
#pragma code lowVector=0x18
86
void LowVector (void)
87
{
88
    _asm goto LowISR _endasm
89
}
90
#pragma code // Return to default code section
91
 
92
 
93
/*
94
*   Function: mainInit
95
*
96
*   Input:  none
97
*   Output: none
98
*   Pre-conditions: none
99
*   Affects: Se code
100
*   Depends: none.
101
*/
102
void mainInit()
103
{
104
    LED0_TRIS=0;   
105
 
106
    // Enable internal PORTB pull-ups
107
    INTCON2bits.RBPU = 0;
108
 
109
    // Enable Interrupts
110
    INTCONbits.GIEH = 1;
111
    INTCONbits.GIEL = 1;
112
 
113
 
114
    // Enable interrupt prirority
115
    RCONbits.IPEN=1;
116
 
117
 
118
}
119
 
120
/*
121
*   Function: canParse
122
*
123
*   Input:  Received can message
124
*   Output: none
125
*   Pre-conditions: canInit and received packet.
126
*   Affects: Sensors/actuators/etc. See code.
127
*   Depends: none.
128
*/
129
#ifdef USE_CAN
130
void canParse(CAN_MESSAGE cm)
131
{
132
    int i=0;
133
 
134
    uartPutc(UART_START_BYTE);
135
    uartPutc((BYTE)(cm.ident));
136
    uartPutc((BYTE)(cm.ident>>8));
137
    uartPutc((BYTE)(cm.ident>>16));
138
    uartPutc((BYTE)(cm.ident>>24));
139
    uartPutc(cm.extended);
56 johboh 140
    uartPutc(cm.remote_request);
51 johboh 141
    uartPutc(cm.data_length);
142
    for(i=0;i<8;i++) uartPutc(cm.data[i]);
143
    uartPutc(UART_END_BYTE);
144
 
145
}
146
#endif
147
 
148
 
149
/*
150
*   Function: uartParse
151
*
152
*   Input:  Received uart message
153
*   Output: none
154
*   Pre-conditions: uartInit and received byte.
155
*   Affects: Sensors/actuators/etc. See code.
156
*   Depends: none.
157
*/
158
#ifdef USE_UART
159
void uartParse(BYTE c)
160
{
161
    BYTE i;
162
    unsigned int wait = 0;
163
    CAN_MESSAGE cm;
164
    static BOOL waitingMessage = FALSE;
165
    static TICK timeout=0;
166
    static BYTE count=0;
167
 
168
    if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20)
169
    {
170
        waitingMessage=FALSE;
171
    }
172
 
173
 
174
    if (waitingMessage==TRUE)
175
    {
176
 
177
        timeout=tickGet();
178
 
56 johboh 179
 
51 johboh 180
        // UART END
56 johboh 181
        if(count>=15)
51 johboh 182
        {
183
            if (c==UART_END_BYTE)
184
            {
56 johboh 185
                LED0_IO=~LED0_IO;
64 johboh 186
                //canParse(cm);
187
                while(!canSendMessage(cm,PRIO_HIGEST));
51 johboh 188
            }
189
            waitingMessage=FALSE;
190
            return;
191
        }
192
 
193
        // data
56 johboh 194
        if(count>=7)
51 johboh 195
        {
56 johboh 196
            cm.data[count-7]=c;
51 johboh 197
            count++;
198
            return;
199
        }
200
 
201
        // data length
56 johboh 202
        if(count>=6)
51 johboh 203
        {
204
            cm.data_length=c;
205
            count++;
206
            return;
207
        }
208
 
56 johboh 209
        // remote request
210
        if(count>=5)
211
        {
212
            cm.remote_request=c;
213
            count++;
214
            return;
215
        }
216
 
51 johboh 217
        // extended
218
        if(count>=4)
219
        {
220
            cm.extended=c;
221
            count++;
222
            return;
223
        }
224
 
225
        // ident
226
        if(count>=0)
227
        {
228
            cm.ident+=((DWORD)c<<(count*8));
229
            count++;
230
            return;
231
        }
232
 
233
    }
234
 
235
 
236
    if (c==UART_START_BYTE && waitingMessage==FALSE)
237
    {
238
            waitingMessage=TRUE;
239
            timeout=tickGet();
240
            count=0;
241
            cm.ident=0;
242
            return;
243
    }
244
 
245
 
246
}
247
#endif