Subversion Repositories HomeAutomation

Rev

Rev 589 | Details | Compare with Previous | Last modification | View Log | SVN | RSS feed

Rev Author Line No. Line
587 pengi 1
#include <inttypes.h>
2
#include <avr/interrupt.h>
3
#include <stdio.h>
4
#include <string.h>
5
#include <config.h> // All configuration parameters
6
#include <bios.h>   // BIOS interface declarations, including CAN structure and ID defines.
589 pengi 7
#include <drivers/channel/channel.h>
587 pengi 8
 
9
#define APP_TYPE    0xf001
10
#define APP_VERSION 0x0002
11
 
12
// A simple message "queue", with space for one message only.
13
// These are declared volatile to tell the compiler not to optimize away accesses.
14
volatile Can_Message_t rxMsg; // Message storage
15
volatile uint8_t rxMsgFull;   // Synchronization flag
16
 
17
uint16_t dim_val = 0x7070;
18
 
19
// CAN message reception callback.
20
// This function runs with interrupts disabled, keep it as short as possible.
21
void can_receive(Can_Message_t *msg) {
22
    if (!rxMsgFull) {
23
        memcpy((void*)&rxMsg, msg, sizeof(rxMsg));
24
        rxMsgFull = 1;
25
    }
26
}
27
 
28
void Channel_Callback_Value( uint8_t id, uint16_t value ) {
29
    dim_val = value;
30
}
31
 
32
int main(void)
33
{
34
    uint16_t val = 0;
35
 
36
    Can_Message_t txMsg;
37
 
38
    DDRD |= (1<<PORTD4);
39
    // Enable interrupts as early as possible
40
    sei();
41
 
42
    txMsg.Id = (CAN_NMT_APP_START << CAN_SHIFT_NMT_TYPE) | (NODE_ID << CAN_SHIFT_NMT_SID);
43
    txMsg.DataLength = 4;
44
    txMsg.RemoteFlag = 0;
45
    txMsg.ExtendedFlag = 1;
46
    txMsg.Data.words[0] = APP_TYPE;
47
    txMsg.Data.words[1] = APP_VERSION;
48
 
49
    // Set up callback for CAN reception, this is optional if only sending is required.
50
    BIOS_CanCallback = &can_receive;
51
    // Send CAN_NMT_APP_START
52
    BIOS_CanSend(&txMsg);
53
 
54
    Channel_Init();
55
 
588 pengi 56
    Channel_Bind( 0, 3 );
57
    Channel_SetValue( 0, 0x3333 );
58
 
59
    for(;;) {
587 pengi 60
        val+=27;
61
        if( val<dim_val ) {
62
            PORTD |= (1<<PORTD4);
63
        } else {
64
            PORTD &= ~(1<<PORTD4);
65
        }
66
        if (rxMsgFull) {
588 pengi 67
            Channel_HandleMsg( (Can_Message_t *)&rxMsg );
587 pengi 68
            rxMsgFull = 0;
69
        }
70
    }
71
 
72
    return 0;
73
}