Subversion Repositories HomeAutomation

Rev

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

Rev 228 Rev 240
1
#include "bios_funs.h"
1
#include "bios_funs.h"
2
#include "avr_cfg.h"
2
#include "avr_cfg.h"
3
#include <inttypes.h>
3
#include <inttypes.h>
4
#include <avr/io.h>
4
#include <avr/io.h>
5
#include <avr/interrupt.h>
5
#include <avr/interrupt.h>
6
#include <avr/wdt.h>
6
#include <avr/wdt.h>
7
#include <avr/pgmspace.h>
7
#include <avr/pgmspace.h>
8
#include <stdio.h>
8
#include <stdio.h>
9
#include "flash.h"
9
#include "flash.h"
10
#include "vectors.h" // Must be included after sfr_defs.h
10
#include "vectors.h" // Must be included after sfr_defs.h but before any ISR()
-
 
11
#include "can.h"
11
 
12
 
12
//---------------------------------------------------------------------------
13
//---------------------------------------------------------------------------
13
// Globals
14
// Globals
14
 
15
 
15
volatile long gMilliSecTick;
16
volatile long gMilliSecTick;
16
 
17
 
17
//---------------------------------------------------------------------------
18
//---------------------------------------------------------------------------
18
// Function definitions for the exported interface
19
// Function definitions for the exported interface
19
 
20
 
20
void reset(void) {
21
void reset(void) {
21
    //Just loop and let the watchdog reset
22
    //Just loop and let the watchdog reset
22
    cli();
23
    cli();
23
    while(1);
24
    while(1);
24
};
25
};
25
 
26
 
26
void bios_putchar(char c) {
27
void bios_putchar(char c) {
27
    UART_PUTCHAR(c);
28
    UART_PUTCHAR(c);
28
};
29
};
29
 
30
 
30
char bios_getchar(void) {
31
char bios_getchar(void) {
31
    char c;
32
    char c;
32
    UART_GETCHAR(c)
33
    UART_GETCHAR(c)
33
    return c;
34
    return c;
34
};
35
};
35
 
36
 
36
long timebase_get(void) {
37
long timebase_get(void) {
37
    uint8_t sreg;
38
    uint8_t sreg;
38
    long res;
39
    long res;
39
   
40
   
40
    //Disable interrupts while reading tick count
41
    //Disable interrupts while reading tick count
41
    sreg=SREG;
42
    sreg=SREG;
42
    cli();
43
    cli();
43
   
44
   
44
    res = gMilliSecTick;
45
    res = gMilliSecTick;
45
   
46
   
46
    SREG=sreg;
47
    SREG=sreg;
47
   
48
   
48
    return res;
49
    return res;
49
}
50
}
50
 
51
 
51
//---------------------------------------------------------------------------
52
//---------------------------------------------------------------------------
52
// Function definitions for the private functions
53
// Function definitions for the private functions
53
 
54
 
54
static int console_getchar(FILE *stream) {
55
static int console_getchar(FILE *stream) {
55
    return bios_getchar();
56
    return bios_getchar();
56
}
57
}
57
 
58
 
58
static int console_putchar(char c, FILE *stream) {
59
static int console_putchar(char c, FILE *stream) {
59
    bios_putchar(c);
60
    bios_putchar(c);
60
    return 0;
61
    return 0;
61
}
62
}
62
 
63
 
63
static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
64
static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
64
                                         _FDEV_SETUP_RW);
65
                                         _FDEV_SETUP_RW);
65
   
66
   
66
 
67
 
67
//---------------------------------------------------------------------------
68
//---------------------------------------------------------------------------
68
// Interrupt Service Routines
69
// Interrupt Service Routines
69
// Read app_vectors.h!
70
// Read app_vectors.h!
70
 
71
 
71
ISR(TIMEBASE_VECTOR) {
72
ISR(TIMEBASE_VECTOR) {
72
    TIMEBASE_COUNTER -= TIMEBASE_RELOAD;
73
    TIMEBASE_COUNTER -= TIMEBASE_RELOAD;
73
    gMilliSecTick++;
74
    gMilliSecTick++;
74
    wdt_reset();
75
    wdt_reset();
75
}
76
}
76
 
77
 
77
int main() {
78
int main() {
78
    void (*app_reset)(void) = 0;
79
    void (*app_reset)(void) = 0;
79
   
80
   
80
    // Setup USART for debug channel
81
    // Setup USART for debug channel
81
    UART_INIT();
82
    UART_INIT();
82
    stdout = &bios_stdio;
83
    stdout = &bios_stdio;
83
    stdin = &bios_stdio;
84
    stdin = &bios_stdio;
84
   
85
   
85
    // Initialize a suitable hardware timer to create a 1KHz interrupt.
86
    // Initialize a suitable hardware timer to create a 1KHz interrupt.
86
    TIMEBASE_INIT();
87
    TIMEBASE_INIT();
87
   
88
   
88
    // Enable watchdog timer to protect against an application locking the
89
    // Enable watchdog timer to protect against an application locking the
89
    // node by leaving interrupts disabled. 
90
    // node by leaving interrupts disabled. 
90
    wdt_enable(WDTO_500MS);
91
    wdt_enable(WDTO_500MS);
91
   
92
   
92
    // Move interrupt vectors to start of bootloader section and enable interrupts
93
    // Move interrupt vectors to start of bootloader section and enable interrupts
93
    IVSEL_REG = _BV(IVCE);
94
    IVSEL_REG = _BV(IVCE);
94
    IVSEL_REG = _BV(IVSEL);
95
    IVSEL_REG = _BV(IVSEL);
95
    sei();
96
    sei();
96
   
97
   
97
    printf("AVR BIOS\n");
98
    printf("AVR BIOS\n");
98
   
99
   
99
    if (pgm_read_word(0) != 0xffff) {
100
    if (pgm_read_word(0) != 0xffff) {
100
        printf("Starting application.\n");
101
        printf("Starting application.\n");
101
        app_reset();
102
        app_reset();
102
    }
103
    }
103
   
104
   
104
    printf("No application found.\n");
105
    printf("No application found.\n");
-
 
106
   
-
 
107
    printf("\n------------------------------------------------------------\n");
-
 
108
    printf(  "   CAN Test: Periodic Transmission\n");
-
 
109
    printf(  "------------------------------------------------------------\n");
-
 
110
   
-
 
111
    printf("CanInit...");
-
 
112
    if (Can_Init() != CAN_OK) {
-
 
113
        printf("FAILED!\n");
-
 
114
    }
-
 
115
    else {
-
 
116
        printf("OK!\n");
-
 
117
    }
-
 
118
   
-
 
119
    uint32_t timeStamp = 0;
-
 
120
   
-
 
121
    Can_Message_t txMsg;
-
 
122
    Can_Message_t rxMsg;
-
 
123
    //The databytes are just what happens to be in the memory. They are never set.
-
 
124
    txMsg.RemoteFlag = 0;
-
 
125
    txMsg.ExtendedFlag = 1;
-
 
126
#define NODENUMBER 2
-
 
127
#if NODENUMBER == 1
-
 
128
    txMsg.Id = 16;
-
 
129
    txMsg.DataLength = 2;
-
 
130
#elif NODENUMBER == 2
-
 
131
    txMsg.Id = 32;
-
 
132
    txMsg.DataLength = 5;
-
 
133
#else
-
 
134
# error NODENUMBER not set!
-
 
135
#endif
-
 
136
   
-
 
137
    uint8_t i;
-
 
138
    /* main loop */
-
 
139
    while (1) {
-
 
140
        /* service the CAN routines */
-
 
141
        Can_Service();
-
 
142
       
-
 
143
        /* send CAN message and check for CAN errors once every second */
-
 
144
        if ((timebase_get() - timeStamp) >= 1000) {
-
 
145
            timeStamp = timebase_get();
-
 
146
            /* send txMsg */
-
 
147
            txMsg.Id++;
-
 
148
            Can_Send(&txMsg);
-
 
149
        }
-
 
150
       
-
 
151
        /* check if any messages have been received */
-
 
152
        while (Can_Receive(&rxMsg) == CAN_OK) {
-
 
153
            printf("BIOS Received: ID=%lx, DLC=%u, EXT=%u, RTR=%u, ", rxMsg.Id, (uint16_t)(rxMsg.DataLength), (uint16_t)(rxMsg.ExtendedFlag), (uint16_t)(rxMsg.RemoteFlag));
-
 
154
            printf("data={ ");
-
 
155
            for (i=0; i<rxMsg.DataLength; i++) {
-
 
156
                printf("%x ", rxMsg.Data.bytes[i]);
-
 
157
            }
-
 
158
            printf("}\n");
-
 
159
           
-
 
160
            if (rxMsg.Id % 20 == 19) reset();
-
 
161
        }
-
 
162
    }
-
 
163
 
105
    return 0;
164
    return 0;
106
}
165
}
107
 
166