JLink SAMD21 Flashing stop to work

I think J-Link is a good debugger, but sometimes is doesn’t work well on some boards. It was working fine to program Arduino M0, but start to present errors recently:

J-Link>con
Specify target interface speed [kHz]. : 4000 kHz
Speed>1000
Device “ATSAML21G18” selected.

Connecting to target via SWD
InitTarget()
Found SW-DP with ID 0x0BC11477
Scanning AP map to find all available APs
AP[1]: Stopped AP scan as end of AP map has been reached
AP[0]: AHB-AP (IDR: 0x04770031)
Iterating through AP map to find AHB-AP to use
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0x41003000
CPUID register: 0x410CC601. Implementer code: 0x41 (ARM)
Found Cortex-M0 r0p1, Little endian.
FPUnit: 4 code (BP) slots and 0 literal slots
CoreSight components:
ROMTbl[0] @ 41003000
ROMTbl[0][0]: E00FF000, CID: B105100D, PID: 000BB4C0 ROM Table
ROMTbl[1] @ E00FF000
ROMTbl[1][0]: E000E000, CID: B105E00D, PID: 000BB008 SCS
ROMTbl[1][1]: E0001000, CID: B105E00D, PID: 000BB00A DWT
ROMTbl[1][2]: E0002000, CID: B105E00D, PID: 000BB00B FPB
ROMTbl[0][1]: 41006000, CID: B105900D, PID: 001BB932 MTB-M0+
Cortex-M0 identified.

J-Link>loadbin nuttx.bin 0
Halting CPU for downloading file.
Downloading file [nuttx.bin]…
Comparing flash [100%] Done.
Erasing flash [100%] Done.
Programming flash [100%] Done.
Verifying flash [100%] Done.
Error while programming flash: Programming failed.

J-Link>erase
Target connection not established yet but required for command.
Please specify device / core. : ATSAML21G18
Type ‘?’ for selection dialog
Device>
Device “ATSAML21G18” selected.

Connecting to target via SWD
InitTarget()
Found SW-DP with ID 0x0BC11477
AP map detection skipped. Manually configured AP map found.
AP[0]: AHB-AP (IDR: Not set)
AP[0]: Core found
AP[0]: AHB-AP ROM base: 0x41003000
CPUID register: 0x410CC601. Implementer code: 0x41 (ARM)
Found Cortex-M0 r0p1, Little endian.
FPUnit: 4 code (BP) slots and 0 literal slots
CoreSight components:
ROMTbl[0] @ 41003000
ROMTbl[0][0]: E00FF000, CID: B105100D, PID: 000BB4C0 ROM Table
ROMTbl[1] @ E00FF000
ROMTbl[1][0]: E000E000, CID: B105E00D, PID: 000BB008 SCS
ROMTbl[1][1]: E0001000, CID: B105E00D, PID: 000BB00A DWT
ROMTbl[1][2]: E0002000, CID: B105E00D, PID: 000BB00B FPB
ROMTbl[0][1]: 41006000, CID: B105900D, PID: 001BB932 MTB-M0+
Cortex-M0 identified.
Erasing device…
Erasing flash [100%] Done.
Verifying flash [100%] Done.
J-Link: Flash download: Total time needed: 0.932s (Prepare: 0.141s, Compare: 0.000s, Erase: 0.698s, Program: 0.000s, Verify: 0.000s, Restore: 0.091s)
Erasing flash [100%] Done.
Verifying flash [100%] Done.
J-Link: Flash download: Total time needed: 0.130s (Prepare: 0.087s, Compare: 0.000s, Erase: 0.002s, Program: 0.000s, Verify: 0.000s, Restore: 0.040s)
Erasing done.
J-Link>exit

J-Link>loadbin nuttx.bin 0
Downloading file [nuttx.bin]…
Erasing flash [100%] Done.
Programming flash [100%] Done.
Verifying flash [100%] Done.
Error while programming flash: Programming failed.
J-Link>

But “hopefully” I’m not the only one to face this same issue:

https://forum.segger.com/index.php/Thread/4645-SOLVED-programming-failed-ATSAMD21G18-with-SEGGER-EDU/

Testing QDBM: Quick Database Manager

The QDBM is a small database manager that could be used for embedded systems (just pay attention on its license: LGPL, so only dynamic linkage if you are using it for commercial applications).

Fortunately the documentation of the project is very good:

https://fallabs.com/qdbm/

The most basic example is demonstrated on project documentation:

#include <depot.h>
#include <stdlib.h>
#include <stdio.h>

#define NAME     "mikio"
#define NUMBER   "000-1234-5678"
#define DBNAME   "book"

int main(int argc, char **argv){
  DEPOT *depot;
  char *val;

  /* open the database */
  if(!(depot = dpopen(DBNAME, DP_OWRITER | DP_OCREAT, -1))){
    fprintf(stderr, "dpopen: %s\n", dperrmsg(dpecode));
    return 1;
  }

  /* store the record */
  if(!dpput(depot, NAME, -1, NUMBER, -1, DP_DOVER)){
    fprintf(stderr, "dpput: %s\n", dperrmsg(dpecode));
  }

  /* retrieve the record */
  if(!(val = dpget(depot, NAME, -1, 0, -1, NULL))){
    fprintf(stderr, "dpget: %s\n", dperrmsg(dpecode));
  } else {
    printf("Name: %s\n", NAME);
    printf("Number: %s\n", val);
    free(val);
  }

  /* close the database */
  if(!dpclose(depot)){
    fprintf(stderr, "dpclose: %s\n", dperrmsg(dpecode));
    return 1;
  }

  return 0;
}

To compile this example you need to install the libqdbm:

$ sudo apt install libqdbm-dev

Save the above code as phone.c and compile this way:

$ gcc -std=c99 -o phone phone.c $(pkg-config --cflags --libs qdbm)

You can also install the package qdbm-util:

$ sudo apt install qdbm-util

Now you can create the database using this command:

$ dpmgr create mydata.qdbm

To insert data:

$ dpmgr put mydata.qdbm “Joe” “Student”

$ dpmgr put mydata.qdbm “Mary” “Doctor”

$ dpmgr put mydata.qdbm “Julia” “Actress”

RTL-SDR as FM radio on Linux

Although you don’t need GNUradio, it is a good idea to install it and the rtl_sdr wil be installed as well:

$ sudo apt install gnuradio
$ sudo apt install gr-osmosdr

You will need the audio “play” command, it is on sox package:

$ sudo apt install sox

And finally (105.1MHz is Antena1 in Belo Horizonte)

$ rtl_fm -M wbfm -f 105.1M | play -r 32k -t raw -e s -b 16 -c 1 -V1 -