[Memo] Burn firmware to uC / ラズパイで、マイコンのファームを焼く

ラズパイで、マイコンのファームを焼く.. <font color='red'>**ための準備**</font>
下書きがいっぱいになってしまった(ぉぃ!)ので、とりあえず、一般公開.. 続きはそのうち... 

# Overview // 概要
- Burn uC's firmware via RPi-GPIO


# Environment
- Raspberry Pi 4B
- OS: 2020-08-20-raspios-buster-armhf-lite.zip

- Microcontroller
    - ATmega8 x2
    - Nordic nRF52832
    - SAMD21G18 (Arduino MKRZero series)


<!--
ラズパイ以前使用前
- ATmega8 : USBasp w/ BLHeli (2 times)
- nRF52832: J-Link 
- SAMD21G18 
-->

# Connection

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/75594/b186a9a6-114f-3dd3-bb88-9ae7977bbaa1.png)

- 接続は、GPIOになってるとこなら、設定ファイル変更でいけるハズ。

# Setup Raspberry Pi

1. Burn RPi OS image to microSD by using [Raspberry Pi Imager](https://www.raspberrypi.org/downloads/)
2. Update and install tools

    ```bash:Copy_And_Paste
    # When you are using apt-cacher-ng
    # export http_proxy="http://apt-cache-server.local:3142"
    sudo apt-get update && \
    sudo apt upgrade -y && \
    sudo apt install -y git avrdude telnet srecord
    ```
3. Install openocd from source code 

    ```bash:Copy_And_Paste
    ### OpenOCD
    # export http_proxy="http://apt-cache-server.local:3142"
    sudo apt update && \
    sudo apt-get install -y git autoconf libtool make pkg-config libusb-1.0-0 libusb-1.0-0-dev && \
    git clone git://repo.or.cz/openocd.git && \
    cd openocd/ && \
    time ./bootstrap && \
    time ./configure --enable-sysfsgpio --enable-bcm2835gpio && \
    time make -j4 && \
    sudo make install
    ```

4. Configuration files for OpenOCD and avrdude
    - Atmel SAMD21 

        ```txt:openocd-at91samd.cfg
        # ATMEL-SAMD21
        #interface sysfsgpio
        adapter driver sysfsgpio

        # SWD GPIO set: swclk swdio
        sysfsgpio_swd_nums 6 5

        transport select swd
        set CHIPNAME at91samd
        source [find target/at91samdXX.cfg]

        ### Command(s)
        init
        targets
        reset halt
        ```
    - Nordic nRF52832

        ```txt:openocd-nrf52.cfg
        # Nordic nRf52832
        #interface sysfsgpio
        adapter driver sysfsgpio

        # SWD GPIO set: swclk swdio
        sysfsgpio_swd_nums 26 19

        transport select swd
        source [find target/nrf52.cfg]

        ### Command(s)
        init
        targets
        reset halt
        ```
    - ATmega8 

        ```txt:/etc/avrdude.confに追記する.id=は適当に。
        programmer
          id    = "j15";
          desc  = "Use the Linux sysfs interface to bitbang GPIO lines";
          type  = "linuxgpio";
          mosi  = 18;
          miso  = 25;
          sck   = 24;
          reset = 23;
        ;
        programmer
          id    = "j17";
          desc  = "Use the Linux sysfs interface to bitbang GPIO lines";
          type  = "linuxgpio";
          mosi  = 12;
          miso  = 21;
          sck   = 20;
          reset = 16;
        ;
        ```


# How to use // 使い方
## OpenOCD - Atmel SAMD21

## OpenOCD - Nordic nRF52832

```txt:実行例-端末その１_サーバ起動
pi@raspberrypi:~ $ openocd -f openocd-nrf52.cfg 
Open On-Chip Debugger 0.10.0+dev-01411-g051e80812-dirty (2020-09-29-01:39)
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : SysfsGPIO JTAG/SWD bitbang driver
Info : This adapter doesn't support configurable speed
Info : SWD DPIDR 0x2ba01477
Info : nrf52.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : starting gdb server for nrf52.cpu on 3333
Info : Listening on port 3333 for gdb connections
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000a80 msp: 0x20000400
Info : Listening on port 6666 for tcl connections
Info : Listening on port 4444 for telnet connections
```

```txt:実行例-端末その２_サーバに接続
pi@raspberrypi:~ $ telnet localhost 4444
Trying ::1...
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Open On-Chip Debugger
> reset halt
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000a80 msp: 0x20000400
> flash erase_address 0x0 0x80000
erased address 0x00000000 (length 524288) in 0.762454s (671.516 KiB/s)

> flash write_image s132_nrf52_7.0.1_softdevice.hex
Padding image section 0 at 0x00000b00 with 1280 bytes
wrote 153792 bytes from file s132_nrf52_7.0.1_softdevice.hex in 8.331557s (18.026 KiB/s)

> verify_image s132_nrf52_7.0.1_softdevice.hex
verified 152512 bytes in 0.572362s (260.216 KiB/s)

> flash write_image secure_bootloader_ble_s132_pca10040.hex
Padding image section 0 at 0x0007d7f6 with 2 bytes
wrote 23920 bytes from file secure_bootloader_ble_s132_pca10040.hex in 1.519181s (15.376 KiB/s)

> verify_image secure_bootloader_ble_s132_pca10040.hex
verified 23918 bytes in 0.286536s (81.517 KiB/s)

> reset run
> exit
Connection closed by foreign host.
pi@raspberrypi:~ $ 
```



## avrdude - ATMega8