STM32 V5 source code
guowenxue
2018-02-04 785deec23b4cb1e7c4c4d81eb808f195adb1d98a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2005, Swedish Institute of Computer Science
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * This file is part of the Contiki operating system.
 *
 */
 
#include <stdio.h>
#include <avr/boot.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include "elfloader-arch.h"
#include "lib/mmem.h"
#include <string.h> //memset
 
#define R_AVR_NONE             0
#define R_AVR_32               1
#define R_AVR_7_PCREL          2
#define R_AVR_13_PCREL         3
#define R_AVR_16               4
#define R_AVR_16_PM            5
#define R_AVR_LO8_LDI          6
#define R_AVR_HI8_LDI          7
#define R_AVR_HH8_LDI          8
#define R_AVR_LO8_LDI_NEG      9
#define R_AVR_HI8_LDI_NEG     10
#define R_AVR_HH8_LDI_NEG     11
#define R_AVR_LO8_LDI_PM      12
#define R_AVR_HI8_LDI_PM      13
#define R_AVR_HH8_LDI_PM      14
#define R_AVR_LO8_LDI_PM_NEG  15
#define R_AVR_HI8_LDI_PM_NEG  16
#define R_AVR_HH8_LDI_PM_NEG  17
#define R_AVR_CALL            18
 
#define ELF32_R_TYPE(info)      ((unsigned char)(info))
 
#define DEBUG 0
#if DEBUG
#include <avr/pgmspace.h>
#define PRINTF(FORMAT,args...) printf_P(PSTR(FORMAT),##args)
#else
#define PRINTF(...)
#endif
 
static struct mmem module_heap;
/*---------------------------------------------------------------------------*/
void*
elfloader_arch_allocate_ram(int size)
{
  /* Free previously allocated memory */
  /* TODO Assumes memory address 0 can't be allocated, use flag instead? */
  if (MMEM_PTR(&module_heap) != 0) {
    mmem_free(&module_heap);
  }
  
  /* Allocate RAM for module */
  if (mmem_alloc (&module_heap, size) ==  0) {
    return NULL;
  }
  
  return (char*)MMEM_PTR(&module_heap);
}
 
/*---------------------------------------------------------------------------*/
/* TODO: Currently, modules are written to the fixed address 0x10000. Since
 *        flash rom uses word addresses on the AVR, we return 0x8000 here
 */
void*
elfloader_arch_allocate_rom(int size)
{
  return (void *)0x8000;
}
 
/*---------------------------------------------------------------------------*/
/* Eliminate compiler warnings for (non-functional) code when flash requires 32 bit addresses and pointers are 16 bit */
#define INCLUDE_APPLICATE_SOURCE 1
#ifdef __GNUC__
#if (FLASHEND > USHRT_MAX) && (__SIZEOF_POINTER__ <= 2)
#undef INCLUDE_APPLICATE_SOURCE
#define INCLUDE_APPLICATE_SOURCE 0
#endif
#if (__SIZEOF_POINTER__ > 2)
#define INCLUDE_32BIT_CODE 1
#endif
#endif
#if INCLUDE_APPLICATE_SOURCE
 
BOOTLOADER_SECTION void
elfloader_arch_write_rom(int fd, unsigned short textoff, unsigned int size, char *mem)
{
    unsigned char   buf[SPM_PAGESIZE];
    unsigned short* flashptr = (unsigned short *) mem;
 
 
    // Sanity-check size of loadable module
    if (size <= 0)
    return;
 
  
    // Seek to patched module and burn it to flash (in chunks of
    // size SPM_PAGESIZE, i.e. 256 bytes on the ATmega128)
    cfs_seek(fd, textoff, CFS_SEEK_SET);
    for (flashptr=(unsigned short *)mem; flashptr < (unsigned short *) mem + size; flashptr += SPM_PAGESIZE) {
    memset (buf, 0, SPM_PAGESIZE);
    cfs_read(fd, buf, SPM_PAGESIZE);
 
    // Disable interrupts
    uint8_t sreg;
    sreg = SREG;
    cli ();
  
    // Erase flash page
    boot_page_erase (flashptr);
    boot_spm_busy_wait ();
    
    unsigned short *origptr =  flashptr;
 
    int i;    
    // Store data into page buffer
    for(i = 0; i < SPM_PAGESIZE; i+=2) {
        boot_page_fill (flashptr, (uint16_t)((buf[i+1] << 8) | buf[i]));
        PORTB = 0xff - 7;
        ++flashptr;
    }
    
    // Burn page
    boot_page_write (origptr);
    boot_spm_busy_wait();
    
    // Reenable RWW sectin
    boot_rww_enable ();
    boot_spm_busy_wait ();    
 
    // Restore original interrupt settings
    SREG = sreg;
    }
}
#endif /* INCLUDE_APPLICATE_SOURCE */
 
/*---------------------------------------------------------------------------*/
static void
write_ldi(int fd, unsigned char *instr, unsigned char byte)
{
  instr[0] = (instr[0] & 0xf0) | (byte & 0x0f);
  instr[1] = (instr[1] & 0xf0) | (byte >> 4);
  cfs_write (fd, instr, 2);
}
/*---------------------------------------------------------------------------*/
void
elfloader_arch_relocate(int fd, unsigned int sectionoffset,
    //            struct elf32_rela *rela, elf32_addr addr)
            char *sectionaddr,
            struct elf32_rela *rela, char *addr)
{
  unsigned int type;
  unsigned char instr[4];
 
  cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
  cfs_read(fd, instr, 4);
  cfs_seek(fd, sectionoffset + rela->r_offset, CFS_SEEK_SET);
  
  type = ELF32_R_TYPE(rela->r_info);
 
  addr += rela->r_addend;
 
  switch(type) {
  case R_AVR_NONE:
  case R_AVR_32:
    PRINTF(PSTR ("elfloader-avr.c: unsupported relocation type: "));
    PRINTF("%d\n", type);
    break;
 
  case R_AVR_7_PCREL: { /* 4 */
    /*
     * Relocation is relative to PC. -2: branch instructions add 2 to PC.
     * Do not use >> 1 for division because branch instructions use
     * signed offsets.
     */
    int16_t a = (((int)addr - rela->r_offset -2) / 2);
    instr[0] |= (a << 3) & 0xf8;
    instr[1] |= (a >> 5) & 0x03;
    cfs_write(fd, instr, 2);
  }
    break;
  case R_AVR_13_PCREL: { /* 3 */
    /*
     * Relocation is relative to PC. -2: RJMP adds 2 to PC.
     * Do not use >> 1 for division because RJMP uses signed offsets.
     */
    int16_t a = (int)addr / 2;
    a -= rela->r_offset / 2;
    a--;
    instr[0] |= a & 0xff;
    instr[1] |= (a >> 8) & 0x0f;
    cfs_write(fd, instr, 2);
  }
    break;
 
  case R_AVR_16:    /* 4 */
    instr[0] = (int)addr  & 0xff;
    instr[1] = ((int)addr >> 8) & 0xff;
 
    cfs_write(fd, instr, 2);
    break;
 
  case R_AVR_16_PM: /* 5 */
    addr = (char *)((int)addr >> 1);
    instr[0] = (int)addr  & 0xff;
    instr[1] = ((int)addr >> 8) & 0xff;
 
    cfs_write(fd, instr, 2);
    break;
 
  case R_AVR_LO8_LDI: /* 6 */
    write_ldi(fd, instr, (int)addr);
    break;
  case R_AVR_HI8_LDI: /* 7 */
    write_ldi(fd, instr, (int)addr >> 8);
    break;
 
#if INCLUDE_32BIT_CODE       /* 32 bit AVRs */
  case R_AVR_HH8_LDI: /* 8 */
    write_ldi(fd, instr, (int)addr >> 16);
    break;
#endif
 
  case R_AVR_LO8_LDI_NEG: /* 9 */
    addr = (char *) (0 - (int)addr);
    write_ldi(fd, instr, (int)addr);
    break;
  case R_AVR_HI8_LDI_NEG: /* 10 */
    addr = (char *) (0 - (int)addr);
    write_ldi(fd, instr, (int)addr >> 8);
    break;
    
#if INCLUDE_32BIT_CODE         /* 32 bit AVRs */
  case R_AVR_HH8_LDI_NEG: /* 11 */
    addr = (char *)(0 - (int)addr);
    write_ldi(fd, instr, (int)addr >> 16);
    break;
#endif
 
  case R_AVR_LO8_LDI_PM: /* 12 */
    write_ldi(fd, instr, (int)addr >> 1);
    break;
  case R_AVR_HI8_LDI_PM: /* 13 */
    write_ldi(fd, instr, (int)addr >> 9);
    break;
 
#if INCLUDE_32BIT_CODE         /* 32 bit AVRs */
  case R_AVR_HH8_LDI_PM: /* 14 */
    write_ldi(fd, instr, (int)addr >> 17);
    break;
#endif
 
  case R_AVR_LO8_LDI_PM_NEG: /* 15 */
    addr = (char *) (0 - (int)addr);
    write_ldi(fd, instr, (int)addr >> 1);
    break;
  case R_AVR_HI8_LDI_PM_NEG: /* 16 */
    addr = (char *) (0 - (int)addr);
    write_ldi(fd, instr, (int)addr >> 9);
    break;
    
#if INCLUDE_32BIT_CODE         /* 32 bit AVRs */
  case R_AVR_HH8_LDI_PM_NEG: /* 17 */
    addr = (char *) (0 - (int)addr);
    write_ldi(fd, instr, (int)addr >> 17);
    break;
#endif
 
  case R_AVR_CALL: /* 18 */
      /* old solution: 
     addr = ((int16_t)addr >> 1);
     instr[2] = (int16_t)addr & 0xff;
     instr[3] = (int16_t)addr >> 8;
    */
 
    /* new solution */
    instr[2] = (uint8_t) ((int)addr) & 0xff;
    instr[3] = ((int)addr) >> 8;
    cfs_write(fd, instr, 4);
    break;
 
  default:
    PRINTF(PSTR ("Unknown relocation type!\n"));
    break;
  }
}
/*---------------------------------------------------------------------------*/
void
elfloader_unload(void) {
}