element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Design For A Cause 2021
  • Challenges & Projects
  • Design Challenges
  • Design For A Cause 2021
  • More
  • Cancel
Design For A Cause 2021
Blog ACE - Stuff happens
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: amgalbu
  • Date Created: 25 Apr 2021 4:35 PM Date Created
  • Views 1129 views
  • Likes 9 likes
  • Comments 8 comments
  • ace
  • design_for_a_cause_2021
Related
Recommended

ACE - Stuff happens

amgalbu
amgalbu
25 Apr 2021

This was not a planned blog post, but I bricked the Arduino Nano and may be my experience could be useful to anyone else who may have the same kind of problem in the future

 

What happened

While testing low-power modes, I created an application that put to sleep the Arduino Nano's Cortex M0 CPU every second... which means I had no enough time to download a different application from the Arduino IDE...

 

 

What I did

The only option you have when nothing else works is a low-level interface like JTAG or the even simpler SWD. The latter is the default in-circuit programming interface for ARM devices so it is supported by all the ARM processors.

I had in my lab an STLink/V2 programmer, which is supposed to support both SWD and JTAG interfaces but for some reason I was unable to connect to the board

Luckily I found one amazing open source project called OpenOCD which can work on different debuggers and microcontrollers, having configuration files of almost all available ARM Cortex SoC and the best part is that it has support for Raspberry PI GPIO bit-bang programmer profile. This means we can use Raspberry Pi GPIOs to behave as a programmer pins and connect it to our microcontroller to flash bootloaders.

 

Prepare the hardware

You need to connect only two wires: SWD (pin 2) and SWCLK (pin3)

image

 

I connected SWCLK to GPIO 25 of the Raspberry Pi and SWD to GPIO 24 (see picture below). Blu wire is connected to SWD, the yellow wire to SWCLK. The red wire is the reset, but looks like it is not strictly required

 

image

 

I powered up the Arduino board using a USB cable connected to Raspberry Pi so that extra ground wiring is not required

 

 

Install openOCD

  • sudo apt-get install autoconf libtool libusb-dev
  • git clone --recursive git://git.code.sf.net/p/openocd/code openocd-code
  • cd openocd-code

 

Build openOCD

  • ./bootstrap
  • ./configure --enable-bcm2835gpio
  • make
  • sudo make install

 

Prepare a config file

  • cd ~
  • mkdir openocd-config
  • cd openocd-config
  • nano openocd.cfg
  • Paste the following content

 

interface bcm2835gpio

# Raspi1 peripheral_base address
# bcm2835gpio_peripheral_base 0x20000000
# Raspi2 and Raspi3 peripheral_base address
bcm2835gpio_peripheral_base 0x3F000000

# Raspi1 BCM2835: (700Mhz)
# bcm2835gpio_speed_coeffs 113714 28
# Raspi2 BCM2836 (900Mhz):
# bcm2835gpio_speed_coeffs 146203 36
# Raspi3 BCM2837 (1200Mhz): 
bcm2835gpio_speed_coeffs 194938 48

# SWD GPIO set: swclk swdio
bcm2835gpio_swd_nums 25 24

transport select swd

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

# Uncomment & lower speed to address errors
# adapter_khz 1000

init
targets
reset halt

  • save and close

 

Download bootloader file

The bootloader file for Arduino Nano 33 IoT can be download from https://github.com/arduino/ArduinoCore-samd/blob/master/bootloaders/nano_33_iot/samd21_sam_ba_arduino_nano_33_iot.bin

Copy the file in the folder you created (~/openocd-config) and name it bl.bin

 

Launch openOCD

In a shell window, run the command

     cd ~/openocd-config

     sudo openocd

 

image

 

Install and launch Telnet

  • install Telnet (this tool is not available by default on Raspberry Pi)

          apt-get install telnet

  • launch telnet

     telnet localhost 4444

 

Erase flash and install bootloader

  • from the telnet prompt, enter the following commands

     reset halt

     flash erase 0 262144

     at91samd bootloader 0

     program bl.bin verify

     reset

     run

 

image

 

Hope this help!

  • Sign in to reply

Top Comments

  • colporteur
    colporteur over 4 years ago in reply to amgalbu +1
    Thanks for the response. I will repeat in my own words and some that I copied from research. Your sketch impacted the serial interface. Without the serial interface there is no means to load another sketch…
  • amgalbu
    amgalbu over 4 years ago in reply to colporteur

    That's a perfect explanation!!

     

    Ambrogio

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • colporteur
    colporteur over 4 years ago in reply to amgalbu

    Thanks for the response. I will repeat in my own words and some that I copied from research.

     

    Your sketch impacted the serial interface. Without the serial interface there is no means to load another sketch. This renders the Nano useless.

     

    OpenOCD (Open On-Chip Debugger) is open-source software that interfaces with a hardware debugger's JTAG port. The process you describe uses the Serial Wire Debug (SWD) a 2-pin interface (SWDIO/SWCLK) to establish a communication link between the Nano and a Raspberry Pi. The SWD is an alternative JTAG interface that uses the JTAG protocol.

     

    OpenOCD provides debugging and in-system programming for embedded target devices. OpenOCD provides the ability to flash NAND and NOR FLASH memory devices that are attached to the processor on the target system.

     

    Using OpenOCD running on a the Pi, to communicate with the Nano, a new bootloader is being uploaded that doesn't have the links to the sketch, thereby releasing the hold on the serial interface.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • amgalbu
    amgalbu over 4 years ago in reply to colporteur

    Hello Sean

    I found the world "bricked" in several forums and it refers to the condition where you are unable to download a new sketch (which I called improperly "application"). As you may know, an Arduino sketch needs at least two functions: a setup and loop. But, if you programmed other bare-metal microcontrollers, you many other software components are required to build a complete program: for example an interrupt vector and the entry point for the reset interrupt (the assembly code to be executed when microcontrollers is powered up or resets). All these missing components are included in the Arduino bootloader. Arduino IDE builds your source code and links setup() and loop() functions to the bootloader binary to create the image to be flashed into Arduimo Flash memory

    Bootloader also includes serial communication procedures and all the functions to process the commands sent by the IDE over the serial line

    If, for any reason, these serial procedures can not be executed IDE can not communicate with the board and the board becomes unusable

    The only way to solve this problem is to flash a pristine bootloader into the Arduino board through another "door": the SWD interface

     

    In my case, I wrote an application that put the micro to sleep every second. Putting the micro to sleep means switching off the USB interface (i.e. the serial port). So I had not enough time to download a new application before the micro was put to sleep again

     

    Hope this makes the post more clear

     

    Best regards

    Ambrogio

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • javagoza
    javagoza over 4 years ago in reply to colporteur

    I am Spanish, so I also have trouble understanding some terms. I also did not know the term bricked, but I understand the problem Ambrogio had.

     

    Programs can be uploaded to the Arduino just like any other microcontroller using a programmer, but the Arduino supplies its boards with a small loaded program called a bootloader, which allows new programs to be loaded on top without the need for an external programmer.

     

    If you want to use the entire program space on the chip or avoid the bootloader delay, you can upload your programs using an external programmer.

     

    The problem that Ambrogio had is that he had loaded a program that put the microcontroller in a state that did not allow the bootloader to run correctly and therefore could not load new programs. This is why he tried reloading the bootloader program with a specific ST programmer, it has not worked for him, and he found an open source solution that works on a raspberry pi that works fine as a programmer for the Arduino Nano 33 IoT.

     

    A sketch is the name that Arduino uses for a program. It is the unit of code that is loaded and executed on an Arduino board.

    Arduino sketches are "coding" patterns for microcontrollers.

    The programs are divided into 4 sections:

    • The comment section
    • The variables section
    • The configuration section: where the sketch begins
    • The loop section: this section will repeat over and over again

    It is not limited to just those sections, but makes coding easier for beginners.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • colporteur
    colporteur over 4 years ago

    Can you dummy down the information for me. I'm new to the world of Arduinos and don't have all the nomenclature and/or experience to fully comprehend. I do recognize the word "bricked" and would like to avoid making them as much as possible. I'm thinking your post was a discovery of how to "unbrick" the device.

     

    You created an application that impacted the clock. Is there a difference between an application and a sketch? If a sketch fails or causes issues don't you just reload another sketch? Is it possible to create a sketch that alters the device to prevent if from getting another sketch?

     

    I would like to add the "unbricking" knowledge in my toolbox. If you could share a few more details it would be appreciated.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube