Subversion Repositories HomeAutomation

Rev

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

Rev 240 Rev 248
Line 7... Line 7...
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 but before any ISR()
10
#include "vectors.h" // Must be included after sfr_defs.h but before any ISR()
11
#include "can.h"
11
#include "can.h"
-
 
12
 
-
 
13
#define FUNCTmask       0x1E000000
-
 
14
#define FUNCTshift      25
-
 
15
#define FUNCCmask       0x01FF8000
-
 
16
#define FUNCCshift      15
-
 
17
#define NIDmask         0x00007E00
-
 
18
#define NIDshift        9
-
 
19
#define SIDmask         0x000001FF
-
 
20
#define SIDshift        0
-
 
21
 
-
 
22
#define FUNCTbios       0x00
-
 
23
 
-
 
24
#define FUNCCstartprg   0x81
-
 
25
#define FUNCCprgdata    0x82
-
 
26
#define FUNCCstopprg    0x83
-
 
27
 
-
 
28
#define PRG_STATE_IDLE          0
-
 
29
#define PRG_STATE_PROGRAMMING   1
-
 
30
 
12
 
31
 
13
//---------------------------------------------------------------------------
32
//---------------------------------------------------------------------------
14
// Globals
33
// Globals
15
 
34
 
16
volatile long gMilliSecTick;
35
volatile long gMilliSecTick;
17
 
36
 
18
//---------------------------------------------------------------------------
37
//---------------------------------------------------------------------------
19
// Function definitions for the exported interface
38
// Function definitions for the exported interface
20
 
39
 
21
void reset(void) {
40
void reset(void) {
22
    //Just loop and let the watchdog reset
41
    //Just loop and let the watchdog reset
Line 24... Line 43...
24
    while(1);
43
    while(1);
25
};
44
};
26
 
45
 
27
void bios_putchar(char c) {
46
void bios_putchar(char c) {
28
    UART_PUTCHAR(c);
47
    UART_PUTCHAR(c);
29
};
48
};
30
 
49
 
31
char bios_getchar(void) {
50
char bios_getchar(void) {
32
    char c;
51
    char c;
33
    UART_GETCHAR(c)
52
    UART_GETCHAR(c)
34
    return c;
53
    return c;
Line 57... Line 76...
57
}
76
}
58
 
77
 
59
static int console_putchar(char c, FILE *stream) {
78
static int console_putchar(char c, FILE *stream) {
60
    bios_putchar(c);
79
    bios_putchar(c);
61
    return 0;
80
    return 0;
62
}
81
}
63
 
82
 
64
static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
83
static FILE bios_stdio = FDEV_SETUP_STREAM(console_putchar, console_getchar,
65
                                         _FDEV_SETUP_RW);
84
                                         _FDEV_SETUP_RW);
66
   
85
   
67
 
86
 
Line 95... Line 114...
95
    IVSEL_REG = _BV(IVSEL);
114
    IVSEL_REG = _BV(IVSEL);
96
    sei();
115
    sei();
97
   
116
   
98
    printf("AVR BIOS\n");
117
    printf("AVR BIOS\n");
99
   
118
   
100
    if (pgm_read_word(0) != 0xffff) {
-
 
101
        printf("Starting application.\n");
-
 
102
        app_reset();
-
 
103
    }
-
 
104
   
-
 
105
    printf("No application found.\n");
-
 
106
   
-
 
107
    printf("\n------------------------------------------------------------\n");
-
 
108
    printf(  "   CAN Test: Periodic Transmission\n");
-
 
109
    printf(  "------------------------------------------------------------\n");
-
 
110
   
119
   
111
    printf("CanInit...");
120
    printf("CanInit...");
112
    if (Can_Init() != CAN_OK) {
121
    if (Can_Init() != CAN_OK) {
113
        printf("FAILED!\n");
122
        printf("FAILED!\n");
114
    }
123
    }
115
    else {
124
    else {
116
        printf("OK!\n");
125
        printf("OK!\n");
117
    }
126
    }
118
   
127
   
119
    uint32_t timeStamp = 0;
128
    uint32_t timeStamp = 0;
120
   
129
   
121
    Can_Message_t txMsg;
130
    Can_Message_t txMsg;
122
    Can_Message_t rxMsg;
131
    Can_Message_t rxMsg;
123
    //The databytes are just what happens to be in the memory. They are never set.
132
    //The databytes are just what happens to be in the memory. They are never set.
124
    txMsg.RemoteFlag = 0;
133
    txMsg.RemoteFlag = 0;
125
    txMsg.ExtendedFlag = 1;
134
    txMsg.ExtendedFlag = 1;
126
#define NODENUMBER 2
-
 
127
#if NODENUMBER == 1
-
 
-
 
135
 
128
    txMsg.Id = 16;
136
    txMsg.Id = 15;
129
    txMsg.DataLength = 2;
137
    txMsg.DataLength = 2;
130
#elif NODENUMBER == 2
-
 
-
 
138
   
131
    txMsg.Id = 32;
139
    uint16_t address;
132
    txMsg.DataLength = 5;
140
    uint16_t lastoffset=0x0;
133
#else
-
 
134
# error NODENUMBER not set!
141
    uint8_t prg_state=PRG_STATE_IDLE;
135
#endif
-
 
136
   
142
   
137
    uint8_t i;
143
    uint8_t i;
138
    /* main loop */
144
    /* main loop */
139
    while (1) {
145
    while (1) {
140
        /* service the CAN routines */
146
        /* service the CAN routines */
141
        Can_Service();
147
        Can_Service();
142
       
148
       
143
        /* send CAN message and check for CAN errors once every second */
149
        /* send CAN message and check for CAN errors once every second */
144
        if ((timebase_get() - timeStamp) >= 1000) {
150
        if ((timebase_get() - timeStamp) >= 10000) {
145
            timeStamp = timebase_get();
151
            timeStamp = timebase_get();
-
 
152
 
146
            /* send txMsg */
153
            /* send txMsg */
147
            txMsg.Id++;
154
            //txMsg.Id++;
148
            Can_Send(&txMsg);
155
            Can_Send(&txMsg);
-
 
156
           
-
 
157
            if (prg_state==PRG_STATE_IDLE) {
-
 
158
                if (pgm_read_word(0) != 0xffff) {
-
 
159
                    printf("Starting application.\n");
-
 
160
                    app_reset();
-
 
161
                }
-
 
162
                printf("No application found.\n");
-
 
163
            }
149
        }
164
        }
150
       
165
       
151
        /* check if any messages have been received */
166
        /* check if any messages have been received */
152
        while (Can_Receive(&rxMsg) == CAN_OK) {
167
        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));
168
            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={ ");
169
            printf("data={ ");
155
            for (i=0; i<rxMsg.DataLength; i++) {
170
            for (i=0; i<rxMsg.DataLength; i++) {
156
                printf("%x ", rxMsg.Data.bytes[i]);
171
                printf("%x ", rxMsg.Data.bytes[i]);
157
            }
172
            }
158
            printf("}\n");
173
            printf("}\n");
159
           
174
           
-
 
175
            if ((rxMsg.Id & FUNCTmask) == ((uint32_t)FUNCTbios<<FUNCTshift)) {
-
 
176
                //printf("Got 'bios package'\n");
-
 
177
                //om mottagen data är bois-data (detta borde redan filtrerats i can_receive?)
-
 
178
                if ((prg_state==PRG_STATE_IDLE) && ((rxMsg.Id & FUNCCmask) == ((uint32_t)FUNCCstartprg<<FUNCCshift)) && (rxMsg.DataLength == 6)) {
-
 
179
                    //om mottagen data har funktionskoden starta programmering
-
 
180
 
-
 
181
                    //ska kolla om nod-id i IDENT stämmer med nodens id
-
 
182
                    //om ok:
-
 
183
                    prg_state=PRG_STATE_PROGRAMMING;
-
 
184
                    address = rxMsg.Data.bytes[2];
-
 
185
                    address |= rxMsg.Data.bytes[3];
-
 
186
                    printf("Got 'start programming' at address %x\n", address);
-
 
187
                } else if (prg_state==PRG_STATE_PROGRAMMING) {
-
 
188
                    if ((rxMsg.Id & FUNCCmask) == ((uint32_t)FUNCCprgdata<<FUNCCshift)) {
-
 
189
                        //om mottagen data har funktionskoden programdata
-
 
190
                        uint16_t tmpaddress = rxMsg.Data.bytes[0] + (rxMsg.Data.bytes[1] << 8);
-
 
191
                        if (tmpaddress >= lastoffset) {
-
 
192
                            tmpaddress += address;
-
 
193
                            printf("Got 'programdatapacket' at offset %x\n", tmpaddress);
-
 
194
                            for (i=2; i<rxMsg.DataLength; i+=2) {
-
 
195
                                flash_write_word(tmpaddress, rxMsg.Data.bytes[i] + (rxMsg.Data.bytes[i+1] << 8));
-
 
196
                                tmpaddress+=2;
-
 
197
                            }
-
 
198
                            printf("Setting lastoffset to %x\n", tmpaddress);
-
 
199
                            lastoffset = tmpaddress;
-
 
200
                        }
-
 
201
                    } else if ((rxMsg.Id & FUNCCmask) == ((uint32_t)FUNCCstopprg<<FUNCCshift)) {
-
 
202
                        flash_flush_buffer();
-
 
203
                        prg_state=PRG_STATE_IDLE;
-
 
204
                        lastoffset=0x0;
-
 
205
                        //beräkna crc på all programdata mellan address och address+lastoffset
-
 
206
                        printf("Got 'end programming'\n");
-
 
207
                    }
-
 
208
                }
-
 
209
            }
-
 
210
           
160
            if (rxMsg.Id % 20 == 19) reset();
211
            //if (rxMsg.Id % 20 == 19) reset();
161
        }
212
        }
162
    }
213
    }
163
 
214
 
164
    return 0;
215
    return 0;
165
}
216
}