summaryrefslogtreecommitdiffstats
path: root/_log/arduino-due.md
blob: 188154781ebaf2c321f653667570e4be7bc3c389 (plain)
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
---
title: ATSAM3X8E bare-metal notes
date: 2024-09-16
layout: post
---

Bought an Arduino Due to play with ARM Cortex chips. Locked out of the chip for
days—no dev tools (Arduino IDE, Microchip Studio) for OpenBSD. Need to find a
way to bring up the bare-metal ATSAM3X8E.

Three on-chip memories: ROM, flash0, flash1. ARM Cortex-M chips boot into
0x00000. GPNVM bits map one of them to 0x00000: 

 - GPNVM1=0 → ROM (default).
 - GPNVM1=1 and GPNVM2=0 → flash0.
 - GPNVM1=1 and GPNVM2=1 → flash1.

On power-up, control jumps to ROM. Without a user program, Atmel's SAM-BA
bootloader traps execution in ROM. Must remap memory via SWD to force chip to
boot from flash, bypassing factory bootloader.

Toolchain: ST-LINK/V2 programmer, OpenOCD, ARM GNU Compiler Toolchain.

Connect ST-LINK/v2 to Arduino Due's DEBUG port:

<table style="border: none; width: 100%;">
  <tr style="border: none;">
    <td style="border: none; width: 50%; vertical-align: top; background-color: transparent;">
      <img src="schematic.png" alt="Pinout" style="width: 100%">
      <p style="text-align: center;">Wiring</p>
    </td>
    <td style="border: none; width: 50%; vertical-align: top; background-color: transparent;">
      <img src="connections.jpeg" alt="Circuit" style="width: 100%">
      <p style="text-align: center;">Arduino Due</p>
    </td>
  </tr>
</table>

Remap memory using OpenOCD:

```
$ openocd -f openocd-due.cfg
$ telnet localhost 4444
  > halt
  > at91sam3 gpnvm show
  > at91sam3 gpnvm set 1
  > at91sam3 gpnvm show
```

Full command list in OpenOCD manual AT91SAM3 (flash driver section).

At a minimum, vector table must be initialized with stack pointer and reset
vector:

```
__attribute__((noreturn)) void _reset(void) 
{
    unsigned long *dst, *src;
    extern unsigned long _sbss, _ebss;
    extern unsigned long _sdata, _edata, _sidata;

    for (dst = &_sbss; dst < &_ebss; dst++)
        *dst = 0;
    for (dst = &_sdata, src = &_sidata; dst < &_edata;)
        *dst++ = *src++;

    main();
}

extern const unsigned int _sp;

__attribute__ ((section(".vtor"))) const void* _tab[] = { &_sp, _reset };
```  

Linker script places vector table at start of flash0 and initializes stack
pointer to top of RAM (descending stack):

```
MEMORY
{
    rom (rx)  : ORIGIN = 0x00080000, LENGTH = 512K
    ram (rwx) : ORIGIN = 0x20000000, LENGTH = 96K
}

SECTIONS
{
    .text :
    {
        KEEP(*(.vtor))
        *(.text*)
        *(.rodata*)
    } > rom
}

_sp = ORIGIN(ram) + LENGTH(ram);
```

Getting linker script right wasn't easy—long hours with the datasheet, OpenOCD
manual. David Barrass from OpenBSD and folks at OpenOCD mailing lists (bless
them!) helped generously.

Build and upload program:

```
$ arm-none-eabi-gcc -mcpu=cortex-m3 -mthumb -T script.ld \
    -nostartfiles \
    -nostdlib \
    -o a.elf main.c
$ openocd -f openocd-due.cfg -c "program a.elf verify reset exit"
```

Chip unlocked. I'm in.

Commit: <a
href="https://git.asciimx.com/bare-metal-arduino-due/commit/?id=318496925ca76668dd9d63c3d060376f489276f8"
class="external" target="_blank" rel="noopener noreferrer">3184969</a>.