Subversion Repositories HomeAutomation

Rev

Rev 73 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | SVN | RSS feed

Rev 73 Rev 127
Line 39... Line 39...
39
 *
39
 *
40
 * @param c
40
 * @param c
41
 *      The received UART byte.
41
 *      The received UART byte.
42
 */
42
 */
43
void UartParseByte(uint8_t c) {
43
void UartParseByte(uint8_t c) {
44
    static CanMessage_t cm;
44
    static Can_Message_t cm;
45
    static uint8_t waitingMessage = 0;
45
    static uint8_t waitingMessage = 0;
46
    static uint32_t startTime = 0;
46
    static uint32_t startTime = 0;
47
    static int8_t count = 0;
47
    static int8_t count = 0;
48
   
48
   
49
    /* 50ms timeout */
49
    /* 50ms timeout */
50
    if (waitingMessage && TimebasePassedTimeMS(startTime) > 50) {
50
    if (waitingMessage && Timebase_PassedTimeMillis(startTime) > 50) {
51
        waitingMessage = 0;
51
        waitingMessage = 0;
52
    }
52
    }
53
   
53
   
54
    if (waitingMessage) {
54
    if (waitingMessage) {
55
        /* save start time */
55
        /* save start time */
56
        startTime = TimebaseCurrentTime();
56
        startTime = Timebase_CurrentTime();
57
        /* UART END */
57
        /* UART END */
58
        if (count >= 15) {
58
        if (count >= 15) {
59
            if (c == UART_END_BYTE) {
59
            if (c == UART_END_BYTE) {
60
                PORTC ^= (1<<PC0);
60
                PORTC ^= (1<<PC0);
61
                CanSend(&cm);
61
                Can_Send(&cm);
62
            }
62
            }
63
            waitingMessage = 0;
63
            waitingMessage = 0;
64
            return;
64
            return;
65
        }
65
        }
66
        /* data */
66
        /* data */
Line 95... Line 95...
95
        }
95
        }
96
    }
96
    }
97
   
97
   
98
    if (c == UART_START_BYTE && !waitingMessage) {
98
    if (c == UART_START_BYTE && !waitingMessage) {
99
        waitingMessage = 1;
99
        waitingMessage = 1;
100
        startTime = TimebaseCurrentTime();
100
        startTime = Timebase_CurrentTime();
101
        count = 0;
101
        count = 0;
102
        cm.Id = 0;
102
        cm.Id = 0;
103
        return;
103
        return;
104
    }
104
    }
105
}
105
}
Line 107... Line 107...
107
 
107
 
108
/*-----------------------------------------------------------------------------
108
/*-----------------------------------------------------------------------------
109
 * Main Program
109
 * Main Program
110
 *---------------------------------------------------------------------------*/
110
 *---------------------------------------------------------------------------*/
111
int main(void) {
111
int main(void) {
112
    TimebaseInit();
112
    Timebase_Init();
113
    UartInit();
113
    Uart_Init();
114
    CanInit(CAN_BITRATE_1M);
114
    Can_Init();
115
   
115
   
116
    DDRC = 1<<PC1 | 1<<PC0;
116
    DDRC = 1<<PC1 | 1<<PC0;
117
    PORTC = (1<<PC1) | (1<<PC0);
117
    PORTC = (1<<PC1) | (1<<PC0);
118
   
118
   
119
    sei();
119
    sei();
120
   
120
   
121
    CanMessage_t rxMsg;
121
    Can_Message_t rxMsg;
122
    uint16_t rxByte;
122
    uint16_t rxByte;
123
    uint8_t i = 0;
123
    uint8_t i = 0;
124
   
124
   
125
    /* main loop */
125
    /* main loop */
126
    while (1) {
126
    while (1) {
127
        /* any new CAN messages received? */
127
        /* any new CAN messages received? */
128
        if (CanReceive(&rxMsg) == CAN_OK) {
128
        if (Can_Receive(&rxMsg) == CAN_OK) {
129
            PORTC ^= (1<<PC1);
129
            PORTC ^= (1<<PC1);
130
            /* send message to CanWatcher */
130
            /* send message to CanWatcher */
131
            uart_putc(UART_START_BYTE);
131
            uart_putc(UART_START_BYTE);
132
            uart_putc((uint8_t)rxMsg.Id);
132
            uart_putc((uint8_t)rxMsg.Id);
133
            uart_putc((uint8_t)(rxMsg.Id>>8));
133
            uart_putc((uint8_t)(rxMsg.Id>>8));