Subversion Repositories HomeAutomation

Rev

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

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