Subversion Repositories HomeAutomation

Rev

Rev 142 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 142 Rev 149
1
/**
1
/**
2
 * CAN test program.
2
 * CAN test program.
3
 *
3
 *
4
 * @target  AVR
4
 * @target  AVR
5
 * @date    2006-10-29
5
 * @date    2006-10-29
6
 * @author  Jimmy Myhrman
6
 * @author  Jimmy Myhrman
7
 *  
7
 *  
8
 */
8
 */
9
 
9
 
10
/*-----------------------------------------------------------------------------
10
/*-----------------------------------------------------------------------------
11
 * Includes
11
 * Includes
12
 *---------------------------------------------------------------------------*/
12
 *---------------------------------------------------------------------------*/
13
/* system files */
13
/* system files */
14
#include <avr/io.h>
14
#include <avr/io.h>
15
#include <avr/interrupt.h>
15
#include <avr/interrupt.h>
16
#include <avr/wdt.h>
16
#include <avr/wdt.h>
17
#include <stdio.h>
17
#include <stdio.h>
18
/* lib files */
18
/* lib files */
19
#include <can.h>
19
#include <can.h>
-
 
20
#include <serial.h>
20
#include <uart.h>
21
#include <uart.h>
21
#include <timebase.h>
22
#include <timebase.h>
22
 
23
 
23
 
24
 
24
/*-----------------------------------------------------------------------------
25
/*-----------------------------------------------------------------------------
25
 * Defines
26
 * Defines
26
 *---------------------------------------------------------------------------*/
27
 *---------------------------------------------------------------------------*/
27
#define UART_START_BYTE 253
28
#define UART_START_BYTE 253
28
#define UART_END_BYTE 250
29
#define UART_END_BYTE 250
29
 
30
 
30
 
31
 
31
/*-----------------------------------------------------------------------------
32
/*-----------------------------------------------------------------------------
32
 * Functions
33
 * Functions
33
 *---------------------------------------------------------------------------*/
34
 *---------------------------------------------------------------------------*/
34
/**
35
/**
35
 * Parses an incoming UART byte by maintaining a state machine. The UART
36
 * Parses an incoming UART byte by maintaining a state machine. The UART
36
 * bytes will build up a CAN message according to the protocol described here:
37
 * bytes will build up a CAN message according to the protocol described here:
37
 *
38
 *
38
 * http://www.arune.se/projekt/doku.php?id=homeautomation:pc-mjukvara
39
 * http://www.arune.se/projekt/doku.php?id=homeautomation:pc-mjukvara
39
 *
40
 *
40
 * @param c
41
 * @param c
41
 *      The received UART byte.
42
 *      The received UART byte.
42
 */
43
 */
43
void UartParseByte(uint8_t c) {
44
void UartParseByte(uint8_t c) {
44
    static Can_Message_t cm;
45
    static Can_Message_t cm;
45
    static uint8_t waitingMessage = 0;
46
    static uint8_t waitingMessage = 0;
46
    static uint32_t startTime = 0;
47
    static uint32_t startTime = 0;
47
    static int8_t count = 0;
48
    static int8_t count = 0;
48
   
49
   
49
    /* 50ms timeout */
50
    /* 50ms timeout */
50
    if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) {
51
    if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) {
51
        waitingMessage = 0;
52
        waitingMessage = 0;
52
    }
53
    }
53
   
54
   
54
    if (waitingMessage) {
55
    if (waitingMessage) {
55
        /* save start time */
56
        /* save start time */
56
        startTime = Timebase_CurrentTime();
57
        startTime = Timebase_CurrentTime();
57
        /* UART END */
58
        /* UART END */
58
        if (count >= 15) {
59
        if (count >= 15) {
59
            if (c == UART_END_BYTE) {
60
            if (c == UART_END_BYTE) {
60
                PORTC ^= (1<<PC0);
61
                PORTC ^= (1<<PC0);
61
                Can_Send(&cm);
62
                Can_Send(&cm);
62
            }
63
            }
63
            waitingMessage = 0;
64
            waitingMessage = 0;
64
            return;
65
            return;
65
        }
66
        }
66
        /* data */
67
        /* data */
67
        else if (count >= 7) {
68
        else if (count >= 7) {
68
            cm.Data.bytes[count-7] = c;
69
            cm.Data.bytes[count-7] = c;
69
            count++;
70
            count++;
70
            return;
71
            return;
71
        }
72
        }
72
        /* data length */
73
        /* data length */
73
        else if (count >= 6) {
74
        else if (count >= 6) {
74
            cm.DataLength = c;
75
            cm.DataLength = c;
75
            count++;
76
            count++;
76
            return;
77
            return;
77
        }
78
        }
78
        /* remote request flag */
79
        /* remote request flag */
79
        else if (count >= 5) {
80
        else if (count >= 5) {
80
            cm.RemoteFlag = c;
81
            cm.RemoteFlag = c;
81
            count++;
82
            count++;
82
            return;
83
            return;
83
        }
84
        }
84
        /* extended */
85
        /* extended */
85
        else if (count >= 4) {
86
        else if (count >= 4) {
86
            cm.ExtendedFlag = c;
87
            cm.ExtendedFlag = c;
87
            count++;
88
            count++;
88
            return;
89
            return;
89
        }
90
        }
90
        /* ident */
91
        /* ident */
91
        else if (count >= 0) {
92
        else if (count >= 0) {
92
            cm.Id += ((uint32_t)c << (count*8));
93
            cm.Id += ((uint32_t)c << (count*8));
93
            count++;
94
            count++;
94
            return;
95
            return;
95
        }
96
        }
96
    }
97
    }
97
   
98
   
98
    if (c == UART_START_BYTE && !waitingMessage) {
99
    if (c == UART_START_BYTE && !waitingMessage) {
99
        waitingMessage = 1;
100
        waitingMessage = 1;
100
        startTime = Timebase_CurrentTime();
101
        startTime = Timebase_CurrentTime();
101
        count = 0;
102
        count = 0;
102
        cm.Id = 0;
103
        cm.Id = 0;
103
        return;
104
        return;
104
    }
105
    }
105
}
106
}
106
 
107
 
107
 
108
 
108
/*-----------------------------------------------------------------------------
109
/*-----------------------------------------------------------------------------
109
 * Main Program
110
 * Main Program
110
 *---------------------------------------------------------------------------*/
111
 *---------------------------------------------------------------------------*/
111
int main(void) {
112
int main(void) {
112
    Timebase_Init();
113
    Timebase_Init();
113
    Uart_Init();
114
    Serial_Init();
114
    Can_Init();
115
    Can_Init();
115
   
116
   
116
    DDRC = 1<<PC1 | 1<<PC0;
117
    DDRC = 1<<PC1 | 1<<PC0;
117
    PORTC = (1<<PC1) | (1<<PC0);
118
    PORTC = (1<<PC1) | (1<<PC0);
118
   
119
   
119
    sei();
120
    sei();
120
   
121
   
121
    Can_Message_t rxMsg;
122
    Can_Message_t rxMsg;
122
    uint16_t rxByte;
123
    uint16_t rxByte;
123
    uint8_t i = 0;
124
    uint8_t i = 0;
124
   
125
   
125
    /* main loop */
126
    /* main loop */
126
    while (1) {
127
    while (1) {
127
        /* service the CAN routines */
128
        /* service the CAN routines */
128
        Can_Service();
129
        Can_Service();
129
       
130
       
130
        /* any new CAN messages received? */
131
        /* any new CAN messages received? */
131
        if (Can_Receive(&rxMsg) == CAN_OK) {
132
        if (Can_Receive(&rxMsg) == CAN_OK) {
132
            PORTC ^= (1<<PC1);
133
            PORTC ^= (1<<PC1);
133
            /* send message to CanWatcher */
134
            /* send message to CanWatcher */
134
            uart_putc(UART_START_BYTE);
135
            uart_putc(UART_START_BYTE);
135
            uart_putc((uint8_t)rxMsg.Id);
136
            uart_putc((uint8_t)rxMsg.Id);
136
            uart_putc((uint8_t)(rxMsg.Id>>8));
137
            uart_putc((uint8_t)(rxMsg.Id>>8));
137
            uart_putc((uint8_t)(rxMsg.Id>>16));
138
            uart_putc((uint8_t)(rxMsg.Id>>16));
138
            uart_putc((uint8_t)(rxMsg.Id>>24));
139
            uart_putc((uint8_t)(rxMsg.Id>>24));
139
            uart_putc(rxMsg.ExtendedFlag);
140
            uart_putc(rxMsg.ExtendedFlag);
140
            uart_putc(rxMsg.RemoteFlag);
141
            uart_putc(rxMsg.RemoteFlag);
141
            uart_putc(rxMsg.DataLength);
142
            uart_putc(rxMsg.DataLength);
142
            for (i=0; i<8; i++) {
143
            for (i=0; i<8; i++) {
143
                uart_putc(rxMsg.Data.bytes[i]);
144
                uart_putc(rxMsg.Data.bytes[i]);
144
            }
145
            }
145
            uart_putc(UART_END_BYTE);
146
            uart_putc(UART_END_BYTE);
146
        }
147
        }
147
       
148
       
148
        /* any UART bytes received? */
149
        /* any UART bytes received? */
149
        rxByte = uart_getc();
150
        rxByte = uart_getc();
150
        while (rxByte != UART_NO_DATA) {
151
        while (rxByte != UART_NO_DATA) {
151
            /* parse byte! */
152
            /* parse byte! */
152
            UartParseByte((uint8_t)(rxByte & 0x00FF));
153
            UartParseByte((uint8_t)(rxByte & 0x00FF));
153
            /* receive next */
154
            /* receive next */
154
            rxByte = uart_getc();
155
            rxByte = uart_getc();
155
        }
156
        }
156
    }
157
    }
157
   
158
   
158
    return 0;
159
    return 0;
159
}
160
}
160
 
161