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
 *********************************************************************
6
 * FileName:        Main.c
7
 *
8
 * Author               Date    Comment
9
 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10
 * Johan Böhlin     21-10-06    Original        (Rev 1.0)
11
 ********************************************************************/
12
 
53 johboh 13
#include <compiler.h>
14
#include <stackTasks.h>
15
#include <Tick.h>
51 johboh 16
 
17
#ifdef USE_CAN
53 johboh 18
    #include <CAN.h>
51 johboh 19
#endif
20
 
21
#ifdef USE_UART
53 johboh 22
    #include <uart.h>
51 johboh 23
#endif
24
 
25
 
26
 
27
static void mainInit(void);
28
 
29
 
30
void main()
31
{
32
    static TICK t = 0;
33
 
34
    // Inits
35
 
36
    mainInit();
37
 
38
    #ifdef USE_CAN
39
        canInit();
40
    #endif
41
 
42
    #ifdef USE_UART
43
        uartInit();
44
    #endif
45
 
46
    tickInit();
47
 
48
    while(1)
49
    {
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
 
169
    if (waitingMessage==TRUE && (tickGet()-timeout)>TICK_SECOND/20)
170
    {
171
        waitingMessage=FALSE;
172
    }
173
 
174
 
175
    if (waitingMessage==TRUE)
176
    {
177
 
178
        timeout=tickGet();
179
 
56 johboh 180
 
51 johboh 181
        // UART END
56 johboh 182
        if(count>=15)
51 johboh 183
        {
184
            if (c==UART_END_BYTE)
185
            {
56 johboh 186
                LED0_IO=~LED0_IO;
187
                while(!canSendMessage(cm,PRIO_LOWEST));
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