ATSAMD21 reset Region Lock Bits (cheaply) - Page 1
Forum section: electronics / general

author: MLXXXp
author_rank: Frequent Contributor
timestamp: November 01, 2016, 10:36:20 pm
content: I'm wondering if there is an inexpensive way to reset the Region Lock Bits of an Atmel ATSAMD21E17A.I pledged for 3 Tau boards in the Kickstarter campaign by Rabid Prototypes.https://www.kickstarter.com/projects/rabidprototypes/tau-the-tiny-10-supercharged-arduino-compatible/descriptionUnfortunately, all the boards I received (and quite a few others) were incorrectly programmed with all the Region Lock Bits set to "protected". This means they can't be reprogrammed via the USB port using the installed bootloader. See section 22.6.3 of the datasheet.http://www.atmel.com/Images/Atmel-42181-SAM-D21_Datasheet.pdfRabid Prototypes will rectify their mistake but it requires me to ship the boards to them at my expense. Rather than spend my money on shipping, and waiting for the boards to be fixed and returned, I wonder if there's a way I could do this myself for free, or using hardware that I could obtain for around the cost I would have to spend on shipping (less than $5).I've spent a few hours scanning and researching on the internet and, although there are hints that it may be possible, I haven't found anything concrete.I have a STM32F103RCBT6 based Maple Mini clone. Can it be programmed with Blacksphere Blackmagic probe firmware via USB using the bootloader it came with? Can I even change the lock bits using a Blackmagic probe?Or, can I do something with one of those 3 dollar ST-Link V2 programmers available on ebay?I also have a USB to TTL converter using a FTDI FT321XS, if that's of any use.Assume that I have the ability to come up with any required wiring between the devices at no cost.Thanks in advance for any suggestions.

author: ataradov
author_rank: Super Contributor
timestamp: November 01, 2016, 11:37:36 pm
content: Well, you can create a very simple standalone SWD "unlocker" using any microcontroller you have around by combining thishttps://github.com/ataradov/free-dapand thishttps://github.com/ataradov/edbg. But the effort will hardly be worth it, unless you are doing it out of the interest.

author: amyk
author_rank: Super Contributor
timestamp: November 02, 2016, 12:34:34 am
content: Is your goal to just erase them, or do you want to do something else? Because the former is not hard to do --- read up on how to get the chip into debug mode, and just issue a Chip Erase command.

author: ataradov
author_rank: Super Contributor
timestamp: November 02, 2016, 12:57:31 am
content: Quote from: amyk on November 02, 2016, 12:34:34 amYou can clear the lock bits without erasing the chip (erasing the bootloader on a way). There are no ways to do that for $5 unless you already have tools.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: November 02, 2016, 05:11:14 pm
content: Quote from: ataradov on November 01, 2016, 11:37:36 pmYes. In addition to possibly getting this done faster than sending out the boards and awaiting their return, the learning experience is a big part of me wanting to do this. I enjoy doing this sort of thing and have lots of time available for it.Another reason for doing it myself is to have the tools and knowledge to do again in the future if the need arises.A reason I wondered about the possibility of creating and using a cheap Blackmagic probe or using a ST-Link V2 clone was that, as a bonus, the hardware may be usable as a programmer and/or debugger in the future.Quote from: ataradov on November 02, 2016, 12:57:31 amDid you actually mean to say youcanclear the lock bits without erasing orcan't?Note that this topic is about theRegionlock bits, which I believe still allow reading of the region when locked. I'm not talking about theSecurity Bitwhich prevents reading of program space.

author: ataradov
author_rank: Super Contributor
timestamp: November 02, 2016, 05:19:02 pm
content: Quote from: MLXXXp on November 02, 2016, 05:11:14 pmIt is possible. You will have to write a program that talks SWD protocol.ST-Link can do SWD, for sure. I don't know much about ST-Link support in OpenOCD, but if it is supported then you can use that.In any case, there is no tool (apart from normal Atmel tools, of course) that can readily do this. Anything you do will require a bit of programming.Quote from: MLXXXp on November 02, 2016, 05:11:14 pmYou can clear the lock bits, they are not a security measure, only Security Bit is.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: November 02, 2016, 05:20:31 pm
content: Quote from: amyk on November 02, 2016, 12:34:34 amCan I do this from just the USB port and bootloader that is currently burned into the chip?It's OK to accomplish this using a chip erase as long as whatever tools or I create or buy, or programs I can load, are also able to reprogram the chip afterwards and also set things like the BOOTPROT bits and other things in theNVM User Row.

author: ataradov
author_rank: Super Contributor
timestamp: November 02, 2016, 05:23:07 pm
content: Quote from: MLXXXp on November 02, 2016, 05:20:31 pmThat would depend on what this bootloader can do. There is no standard USB bootloader.But I doubt that bootloader will let you do a chip erase, as it will erase the bootloader itself.Quote from: MLXXXp on November 02, 2016, 05:20:31 pmIf you have tools that let you reprogram the chip, you can just use those tools to remove the lock bits without affecting anything else.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: November 02, 2016, 05:45:01 pm
content: Quote from: ataradov on November 02, 2016, 05:23:07 pmIt's a variation of the standard Arduino Zero bootloader, which I doubt can do much more than load an Arduino sketch.I'm still trying to understand the ATSAMD21 datasheet. Is it possible to permanently set the contents of the NVM User Row using a program running on the chip itself?If this is the case could I?Buy a $3 ST-Link V2 debugger.(If necessary use the ST-link to make my Maple Mini into a Blackmagic probe).Use the ST-link or Blackmagic to do a chip erase and then reburn the bootloader that was installed during manufacture.Write a sketch that sets the BOOTPROT bits and other desired settings in the NVM User Row.

author: ataradov
author_rank: Super Contributor
timestamp: November 02, 2016, 05:51:43 pm
content: Quote from: MLXXXp on November 02, 2016, 05:45:01 pmYes, it is, using WAP command of the NVMCTRL. But is sounds like you can't upload new programs because the lock bits are set and BL does not disable them?You can bit-bang SWD interface from any MCU you may have around (even normal Arduino will do). The problem here is not hardware, it is software. Are you prepared to spend a few days (best case scenario) making it all work?

author: MLXXXp
author_rank: Frequent Contributor
timestamp: November 02, 2016, 06:00:28 pm
content: Quote from: ataradov on November 02, 2016, 05:51:43 pmYes, if necessary. Again, I see this as a learning experience. But should it really take a few days?It looks like an ATSAMD21 can be programmed using an ST-Link with OpenOCD, as hinted in this posting:https://sourceforge.net/p/openocd/mailman/message/34559331/If this is the case, what other work, other than learning to use the tools and wiring up the devices, would have to be done?

author: ataradov
author_rank: Super Contributor
timestamp: November 02, 2016, 06:04:51 pm
content: Quote from: MLXXXp on November 02, 2016, 06:00:28 pmThat alone can take significant time to set up. OpenOCD works well when it works, but getting it to work, especially on non-mainstream targets, can be an issue.I would also check if OpenOCD can clear those bits on its own.Quote from: MLXXXp on November 02, 2016, 06:00:28 pmIf you can erase the device and put a new BL image in, then you are done. Don't worry about setting BOOTPROT back, you can always put a new BL in, if you erase it somehow.

author: MLXXXp
author_rank: Frequent Contributor
timestamp: November 02, 2016, 06:32:21 pm
content: Quote from: ataradov on November 02, 2016, 06:04:51 pmThat's true.I'm still not sure if an ST-link can program an ATSAMD21 or how easy it would be to get it working. That's why I thought it may be easier to use an ST-link to program the Maple Mini as a Blackmagic and then use the Blackmagic to program the ATSAMD21.I was just hoping someone here would have direct experience with doing what I'm trying to accomplish, so I wouldn't be wasting my time, however long it may be.In any case, it looks like your software would be able to accomplish it. I have many AVR based Arduinos that I could use if necessary.