Skip to content

FA2022 USB-D11C-serial (Pi3 + OpenOCD)

Raspberry Pi3 + OpenOCD

Set up Raspberry Pi

Download Raspberry Pi Imager

Ctrl-Shift-X to open Advanced menu



Write Operating System RASPBERRY PI OS (32-BIT) to Storage microSD card

Ref. Programming Microcontrollers using OpenOCD on a Raspberry Pi

Install OpenOCD

In mac’s terminal

 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
% ssh pi@raspberrypi.local
@       WARNING: POSSIBLE DNS SPOOFING DETECTED!          @
% ssh-keygen -R raspberrypi.local

The authenticity of host 'raspberrypi.local (fe80::faab:348c:edcd:446f%en0)' can't be established.
ECDSA key fingerprint is SHA256:Dxs24lNBk9lAubin3bRb9H4FMjG5NFNVr6mNUVaCcbw.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

$ sudo apt-get update

$ sudo apt-get install git autoconf libtool make pkg-config libusb-1.0-0 libusb-1.0-0-dev

$ git clone http://openocd.zylin.com/openocd

$ ls
openocd
$ cd openocd
$ ls
bootstrap
$ ./bootstrap

$ ./configure --enable-sysfsgpio --enable-bcm2835gpio

$ ls
makefile
$ make
$ sudo make install

$ cd /usr/local/share/openocd/scripts/interface
$ ls
raspberrypi2-native.cfg

Set up config file (.cfg)

 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
$ cat /usr/local/share/openocd/scripts/interface/raspberrypi2-native.cfg

#
# Config for using Raspberry Pi's expansion header
#
# This is best used with a fast enough buffer but also
# is suitable for direct connection if the target voltage
# matches RPi's 3.3V and the cable is short enough.
#
# Do not forget the GND connection, pin 6 of the expansion header.
#

adapter driver bcm2835gpio

bcm2835gpio peripheral_base 0x3F000000

# Transition delay calculation: SPEED_COEFF/khz - SPEED_OFFSET
# These depend on system clock, calibrated for stock 700MHz
# bcm2835gpio speed SPEED_COEFF SPEED_OFFSET
bcm2835gpio speed_coeffs 146203 36

# Each of the JTAG lines need a gpio number set: tck tms tdi tdo
# Header pin numbers: 23 22 19 21
bcm2835gpio jtag_nums 11 25 10 9

# Each of the SWD lines need a gpio number set: swclk swdio
# Header pin numbers: 23 22
bcm2835gpio swd_nums 11 25

# If you define trst or srst, use appropriate reset_config
# Header pin numbers: TRST - 26, SRST - 18

# bcm2835gpio trst_num 7
# reset_config trst_only

# bcm2835gpio srst_num 24
# reset_config srst_only srst_push_pull

# or if you have both connected,
# reset_config trst_and_srst srst_push_pull

Note

As default in raspberrypi2-native.cfg ,
SWD CLK is GPIO_11
SWD DIO is GPIO_25
SWD RST does not set

Attention

raspi Zero
bcm2835gpio peripheral_base 0x20000000

raspi 3B/3B+
bcm2835gpio peripheral_base 0x3F000000

raspi 4B
bcm2835gpio peripheral_base 0x7E000000

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
$ cd ~
$ git clone https://gitlab.cba.mit.edu/pub/hello-world/atsamd11.git
$ ls
atsamd11
$ cd atsamd11/bootloader/
$ ls
openocd.cfg
sam_ba_Generic_D11C14A_SAMD11C14A.bin
unprotect_bootloader.cfg

$ nano openocd.cfg

Copy below in openocd.cfg

 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
source [find interface/raspberrypi2-native.cfg]
transport select swd

set CHIPNAME at91samd11c14a
source [find target/at91samdXX.cfg]


# did not yet manage to make a working setup using srst
# reset_config srst_only
# reset_config  srst_nogate

adapter speed 200

adapter srst delay 100
adapter srst pulse_width 100

bcm2835gpio swd_nums 25 24
bcm2835gpio trst_num 7
bcm2835gpio srst_num 18

init
targets
#reset halt
reset init

at91samd bootloader 0
program sam_ba_Generic_D11C14A_SAMD11C14A.bin verify
at91samd bootloader 4096
reset
shutdown

Note

You can set SWD pins like bcm2835gpio_swd_nums 25 24
SWD CLK is GPIO_25
SWD DIO is GPIO_24

Reset pin can be set like bcm2835gpio_srst_num 18
SWD RST is GPIO_18

Ref. adapter speed 200
Ref. reset init

Replace sam_ba_Generic_D11C14A_SAMD11C14A.bin

 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
$ cd ~/atsamd11/bootloader
$ ls
unprotect_bootloader.cfg
openocd.cfg
sam_ba_Generic_D11C14A_SAMD11C14A.bin

$ mv sam_ba_Generic_D11C14A_SAMD11C14A.bin sam_ba_Generic_D11C14A_SAMD11C14A.bin.org
$ ls
unprotect_bootloader.cfg
openocd.cfg
sam_ba_Generic_D11C14A_SAMD11C14A.bin.org

$ cd ~
$ git clone https://github.com/mattairtech/ArduinoCore-samd.git
$ ls
ArduinoCore-samd
atsamd11

$ cd ArduinoCore-samd/bootloaders/zero/binaries/
$ ls | grep SAMD11C
sam_ba_Generic_D11C14A_SAMD11C14A.bin

$ cp sam_ba_Generic_D11C14A_SAMD11C14A.bin ~/atsamd11/bootloader/sam_ba_Generic_D11C14A_SAMD11C14A.bin

$ ls ~/atsamd11/bootloader
unprotect_bootloader.cfg
openocd.cfg
sam_ba_Generic_D11C14A_SAMD11C14A.bin.org
sam_ba_Generic_D11C14A_SAMD11C14A.bin

Connection

SAMD11C board Raspberry Pi3
RST GPIO18
GND GND
DIO GPIO24
CLK GPIO25
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ pinout

   3V3  (1) (2)  5V    
 GPIO2  (3) (4)  5V    
 GPIO3  (5) (6)  GND   
 GPIO4  (7) (8)  GPIO14
   GND  (9) (10) GPIO15
GPIO17 (11) (12) GPIO18
GPIO27 (13) (14) GND   
GPIO22 (15) (16) GPIO23
   3V3 (17) (18) GPIO24
GPIO10 (19) (20) GND   
 GPIO9 (21) (22) GPIO25
GPIO11 (23) (24) GPIO8
   GND (25) (26) GPIO7
 GPIO0 (27) (28) GPIO1
 GPIO5 (29) (30) GND   
 GPIO6 (31) (32) GPIO12
GPIO13 (33) (34) GND   
GPIO19 (35) (36) GPIO16
GPIO26 (37) (38) GPIO20
   GND (39) (40) GPIO21


Update: Made simple_cables

Burn bootloader

 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
$ cd ~
$ cd atsamd11/bootloader/
$ ls
openocd.cfg
sam_ba_Generic_D11C14A_SAMD11C14A.bin
unprotect_bootloader.cfg

$ sudo openocd

Open On-Chip Debugger 0.11.0+dev-00502-gf66a16c4a (2021-11-28-15:06)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
Info : BCM2835 GPIO JTAG/SWD bitbang driver
Info : clock speed 200 kHz
Info : SWD DPIDR 0x0bc11477
Info : at91samd11c14a.cpu: Cortex-M0+ r0p1 processor detected
Info : at91samd11c14a.cpu: target has 4 breakpoints, 2 watchpoints
Info : starting gdb server for at91samd11c14a.cpu on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread
xPSR: 0x81000000 pc: 0xfffffffe msp: 0xfffffffc
target halted due to debug-request, current mode: Thread
xPSR: 0x81000000 pc: 0xfffffffe msp: 0xfffffffc
** Programming Started **
Info : SAMD MCU: SAMD11C14A (16KB Flash, 4KB RAM)
** Programming Finished **
** Verify Started **
** Verified OK **
shutdown command invoked
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
Ctl+C

1
2
$ lsusb
Bus 001 Device 006: ID 16d0:0557 MCS Generic SAMD11C14A

In case of erase bootloader

1
2
3
4
5
6
7
8
$ cd ~
$ cd atsamd11/bootloader/
$ ls
openocd.cfg
sam_ba_Generic_D11C14A_SAMD11C14A.bin
unprotect_bootloader.cfg

$ nano unprotect_bootloader.cfg

unprotect_bootloader.cfg

 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
source [find interface/raspberrypi2-native.cfg]
transport select swd

set CHIPNAME at91samd11c14a
source [find target/at91samdXX.cfg]


# did not yet manage to make a working setup using srst
# reset_config srst_only
# reset_config  srst_nogate

adapter speed 200

adapter srst delay 100
adapter srst pulse_width 100

bcm2835gpio swd_nums 25 24
bcm2835gpio trst_num 7
bcm2835gpio srst_num 18

init
targets
#reset halt
reset init

at91samd bootloader 0
#program sam_ba_Generic_D11C14A_SAMD11C14A.bin verify
#at91samd bootloader 4096
reset
shutdown
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
$ sudo openocd -f unprotect_bootloader.cfg
Open On-Chip Debugger 0.11.0+dev-00502-gf66a16c4a (2021-11-28-15:06)
Licensed under GNU GPL v2
For bug reports, read
    http://openocd.org/doc/doxygen/bugs.html
Info : BCM2835 GPIO JTAG/SWD bitbang driver
Info : clock speed 200 kHz
Info : SWD DPIDR 0x0bc11477
Info : at91samd11c14a.cpu: Cortex-M0+ r0p1 processor detected
Info : at91samd11c14a.cpu: target has 4 breakpoints, 2 watchpoints
Info : starting gdb server for at91samd11c14a.cpu on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread
xPSR: 0x21000000 pc: 0xfffffffe msp: 0xfffffffc
Error: at91samd11c14a.cpu -- clearing lockup after double fault
target halted due to debug-request, current mode: Handler HardFault
xPSR: 0x21000003 pc: 0xfffffffe msp: 0xffffffd8
shutdown command invoked

On mac terminal

1
2
3
4
5
6
% ls /dev | grep usb
cu.usbmodem14601
tty.usbmodem14601

% lsusb
Bus 020 Device 005: ID 16d0:0557 16d0 Generic SAMD11C14A

Download SAMD11Cserial
Code -> Download ZIP

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
% cd Downloads
% unzip SAMD11C_serial-main.zip
% ls
SAMD11C_serial-main.zip
SAMD11C_serial-main
% cd SAMD11C_serial-main
% ls
SAMD11C_serial
SAMD21E_serial
% cd SAMD11C_serial
% ls
SAMD11C_serial.ino
% open .

Double click to open it in Arduino

 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
/*
SAMD USB to serial
Author  : Quentin Bolsee
License : CC BY-NC-SA 4.0 https://creativecommons.org/licenses/by-nc-sa/4.0/
Date    : 2020-07-27
Version :
    - 0.0 : first draft
*/

//#define DEBUG
#define BAUD_DEFAULT 9600
#define PIN_TX 4


const byte config_lookup[3][2][4] = {
  {{SERIAL_5N1, SERIAL_6N1, SERIAL_7N1, SERIAL_8N1},
   {SERIAL_5N2, SERIAL_6N2, SERIAL_7N2, SERIAL_8N2}},
  {{SERIAL_5O1, SERIAL_6O1, SERIAL_7O1, SERIAL_8O1},
   {SERIAL_5O2, SERIAL_6O2, SERIAL_7O2, SERIAL_8O2}},
  {{SERIAL_5E1, SERIAL_6E1, SERIAL_7E1, SERIAL_8E1},
   {SERIAL_5E2, SERIAL_6E2, SERIAL_7E2, SERIAL_8E2}},
};


typedef struct {
  long unsigned  baud;
  uint8_t   paritytype; // parity: 0=none, 1=odd, 2=even, 3=mark, 4=space
  uint8_t   stopbits; // stopbits: 0=1, 1=1.5, 2=2
  uint8_t   numbits; // databits: 5,6,7,8,16
} SerialConfig;


SerialConfig current_config = {BAUD_DEFAULT, 0, 1, 8};


void update_serial(SerialConfig new_config) {
  if (new_config.paritytype > 2 || !(new_config.stopbits == 0 || new_config.stopbits == 2) || new_config.numbits < 5 || new_config.numbits > 8) {
    return;
  }

  // lookup indexes
  int i = new_config.paritytype;
  int j = new_config.stopbits == 2 ? 1 : 0;
  int k = (int)new_config.numbits - 5;

  Serial2.flush();
  Serial2.end();
  Serial2.begin(new_config.baud, config_lookup[i][j][k]);
  current_config.baud = new_config.baud;
  current_config.paritytype = new_config.paritytype;
  current_config.stopbits = new_config.stopbits;
  current_config.numbits = new_config.numbits;
}


void setup() {
  // prevents small voltage drops when Serial is closed then opened
  pinMode(PIN_TX, OUTPUT);
  digitalWrite(PIN_TX, HIGH);
  SerialUSB.begin(BAUD_DEFAULT);
  Serial2.begin(BAUD_DEFAULT);
}


void loop() {
  // Detect config change
  SerialConfig new_config = {SerialUSB.baud(), SerialUSB.paritytype(), SerialUSB.stopbits(), SerialUSB.numbits()};
  if (new_config.baud != current_config.baud || new_config.paritytype != current_config.paritytype ||
      new_config.stopbits != current_config.stopbits || new_config.numbits != current_config.numbits) {
    update_serial(new_config);
  }

  #ifdef DEBUG
    SerialUSB.println("Settings: ");
    SerialUSB.print(current_config.baud);
    SerialUSB.print(", ");
    SerialUSB.print(current_config.paritytype);
    SerialUSB.print(", ");
    SerialUSB.print(current_config.stopbits);
    SerialUSB.print(", ");
    SerialUSB.println(current_config.numbits);
    delay(1000);
  #else
    char c;
    if (SerialUSB.available()) {
      c = (char)SerialUSB.read();
      Serial2.write(c);
    }
    if (Serial2.available()) {
      c = (char)Serial2.read();
      SerialUSB.write(c);
    }
  #endif
}

Arduino IDE

File -> Preferences
Additional Board Manager URLs: https://www.mattairtech.com/software/arduino/package_MattairTech_index.json

Tools -> Board -> Board Manager
Type mattairtech or samd to find mattairtech SAM D|L|C core for Arduino , then Install

Upload the sketch into the SAMD11C_serial board

Program hello.t3216.echo.into from UPDI with SAMD11C_serial boards

Download hello.t3216.echo.ino from class page

Additional Board Manager URLs: http://drazzy.com/package_drazzy.com_index.json

Board Manager


Last update: December 28, 2021