Author: acassis

A huddle of electronic modules

I decided to list all the modules on my pile of modules:

3 – Relay module
2 – Analog Microphone module with ampliflier
2 – ADXL345 Accelerometer module (GY-291)
2 – Zero cross detector module
3 – GP2Y0AH01K0F distance sensor
4 – LDR sensor module
1 – MPU9250 module
3 – 4×4 telephone keypad module
2 – PS2 joystick module
1 – VEML6070 UV sensor module
1 – W25Q32FVZPIG SPI NOR Flash module
1 – VS1053 Audio MP3 module
8 – nRF24 2.4GHz module
5 – MicroSD Card module
1 – WS2812 Breakout module
1 – OV7660 (CF7670C-V2) camera module
2 – HMC5883L 3-axis compass (GY-273)
2 – HMC5983 3-axis compass module (GY-282)
2 – Si1145 UV IR light sensor module (GY1145)
1 – BMP280 pressure module
3 – MAX485 RS485 module from LC Technology
1 – ML8511 Analog UV sensor module (GYML8511)
2 – ACS712 Current sensor module
2 – Hall sensor analog module with amplifier
1 – Tilt module (mercury bulb)
1 – Tilt module with 2 brass capsule/tube
2 – GY-86 10DOF MS5611 HMC5883L MPU6050 module (GY-86)
2 – LM75 temperature sensor module (CJMCU-75)
2 – APDS-9960 Gesture sensor module
1 – RDA5807M FM module (RRD-102)
1 – SX1509 keypad controller module
3 – Laser pointer module
3 – Reed-switch module with amplifier
2 – MQ2 butane sensor module (FC-22)
1 – Hall sensor module without amplifier (Keyes without number)
2 – MCP4725 DAC module
2 – ADCL335 Analog Accelerometer module (GY-61)
2 – Ra-01 LoRa module 433MHz
1 – TCS34725FN Color Light sensor module
1 – BH1750FVI light sensor module
1 – MAX30100 Pulse Oximeter and Heart-Rate Sensor module
2 – Buzzer module (RobotDyn)
1 – LSM6DS3 3-axis accelometer gyroscope
1 – MLX90614 Infrared Thermometer module (GY-906)
2 – BME-280 pressure module
1 – VL53L0X laser ranging sensor module (GYVL53L0X)
5 – TXB0108 level-shifter module (HW-0108)
1 – HTU21 humidity sensor module (GY-21)
2 – TEA5767 FM radio module (PL102BC-N)
1 – DHT11 humidity and temperature sensor module
2 – Thyristor AC switch 5V logic AC 220V/5A module
1 – DS18B20 1-wire temperature probe
1 – USB Audio IN/OUT module
1 – AT24C02B EEPROM module
5 – HX711 24-bit ADC Load Cell amplifier module
1 – BMP180 barometer sensor module (GY-68)
2 – ??? module with a small QFN chip with this code: 2222 C3H 9L8GS

I already created drivers for some of these sensors in the NuttX (i.e.: ADXL345, APDS-9960, BH1750FVI, BMP180, VEML6070, ZeroCross, etc). But there are many more to be added.

Advertisements

How to build NuttX on Windows 10

First let me to make things clear: I stopped using Windows operating system about 18 years ago, when in 2000 Chernobyl virus erased the BIOS Flash and the Hard Disk of my computer that was running Windows 98 at that time.

But since many people are asking me to show how to compile NuttX on Windows, I will show how you can do it using Windows 10 (sorry Windows 7, Windows Vista, Windows XP, Windows 2000, Windows Millennium, Windows 98, Windows 95, Windows 3.1, … and DOS users, but it only works on Windows 10).

You need to enable to “Developer Mode” of Windows: click on Window icon at bottom left corner of your screen and then click on “Windows Settings” (gear icon).

Settings:

In the “Windows Settings” click on “Update & Security”.

Update & Security:

Inside it click in “For Developers”.

For developers:

And enable the option “Developer Mode”.

Developer mode:

After that you need to enable the Windows Subsystem for Linux. Open the “Control Panel”.

Control Panel:

Enter inside “Programs”.

Programs:

Click on “Turn Windows features on or off”.

Turn Windows features on or off:

Then it will open the “Windows Features” window, scroll down until you find “Windows Subsystem for Linux”.

Enable the Windows Subsystem for Linux:

Then click to enable it:

At this moment the Windows will ask to restart, click in the Restart button.

When the system restarted you can run the “bash” command:

It will show the message explaining the a Linux distribution is not installed:

Open the browser and go to: https://aka.ms/wslstore

The the Microsoft Store will open automatically:

Click in the Ubuntu distribution, and the click on Get button:

When it finishes the download click on Launch button:

It will run the installation script, when it finishes you can close it and run the Ubuntu command:

Then the Linux terminal will open:

Now you can follow these steps to compile NuttX:

$ sudo apt-get update

$ sudo apt-get install automake bison build-essential flex gcc-arm-none-eabi gperf git libncurses5-dev libtool libusb-dev libusb-1.0.0-dev pkg-config

Create the nuttx workspace:

mkdir ~/nuttxspace

cd ~/nuttxspace

The Windows Subsystem for Linux doesn’t support USB devices (except USB Pendrive), then we don’t need to install OpenOCD. We will use the JLink too on Windows itself.

Clone the NuttX repositories:

$ cd ~/nuttxspace

$ git clone https://bitbucket.org/nuttx/nuttx

$ git clone https://bitbucket.org/nuttx/apps

$ git clone https://bitbucket.org/nuttx/tools

Configure, compile and install the Kconfig-Frontends (needed by NuttX’s menuconfig)

$ cd ~/nuttxspace

$ cd tools/kconfig-frontends/

$ ./configure

$ make

$ sudo make install

$ sudo ldconfig

Now we can compile NuttX for XMC4-Relax board!

Run the configure script:

$ cd ~/nuttxspace

$ cd nuttx

$ ./tools/configure.sh xmc4500-relax/nsh

Run menuconfig and change the build to Linux and UART3 as serial console:

$ make menuconfig

Build Setup  --->
    Build Host Platform (Linux)

System Type  --->
    XMC4xxx Peripheral Support  --->
        [ ] USIC0
        [*] USIC1  (press SPACE to select)
        [ ] USIC2

    XMC4xxx USIC Configuration  --->
        USIC1 Channel 0 Configuration (Not used)  --->
        USIC1 Channel 1 Configuration (UART3)  --->

Device Drivers  --->
    [*] Serial Driver Support  --->
        Serial console (UART3)  --->

Time to compile it:

$ make

If everything compiled correctly you get a nuttx.bin:

$ ls -l nuttx.bin
-rwxrwxr-x 1 alan alan 79804 Jan  8 12:25 nuttx.bin

Just copy this nuttx.bin binary to Windows side:

$ cp nuttx.bin /mnt/c/ProgramData/

You will need to configure Windows Explorer to show Hidden files, this way you could see the nuttx.bin at C:\ProgramData

Running NuttX on Infineon XMC4500-Relax

This is a step-by-step tutorial explaining how to compile and install NuttX on Infineon XMC4500-Relax board.

These steps where tested on Ubuntu 16.04 LTS (Long Term Support).

Open the Linux terminal and install the dependence packages using this command:

$ sudo apt-get install automake bison build-essential flex gcc-arm-none-eabi gperf git libncurses5-dev libtool libusb-dev libusb-1.0.0-dev pkg-config

Create the nuttx workspace:

mkdir ~/nuttxspace

cd ~/nuttxspace

Clone and compile the OpenOCD (it is used to flash the firmware in the board).

$ git clone http://repo.or.cz/r/openocd.git

$ cd ~/nuttxspace/openocd

$ ./bootstrap

$ ./configure --enable-internal-jimtcl --enable-maintainer-mode --disable-werror --disable-shared --enable-stlink --enable-jlink --enable-rlink --enable-vslink --enable-ti-icdi --enable-remote-bitbang

$ make

$ sudo make install

Copy the OpenOCD udev rules and update it:

$ cd contrib

$ sudo cp 60-openocd.rules /etc/udev/rules.d/

$ sudo udevadm trigger

Clone the NuttX repositories:

$ cd ~/nuttxspace

$ git clone https://bitbucket.org/nuttx/nuttx

$ git clone https://bitbucket.org/nuttx/apps

$ git clone https://bitbucket.org/nuttx/tools

Configure, compile and install the Kconfig-Frontends (needed by NuttX’s menuconfig)

$ cd ~/nuttxspace

$ cd tools/kconfig-frontends/

$ ./configure

$ make

$ sudo make install

$ sudo ldconfig

Now we can compile NuttX for XMC4-Relax board!

Run the configure script:

$ cd ~/nuttxspace

$ cd nuttx

$ ./tools/configure.sh xmc4500-relax/nsh

Run menuconfig and change the build to Linux and UART3 as serial console:

$ make menuconfig

Build Setup  --->
    Build Host Platform (Linux)

System Type  --->
    XMC4xxx Peripheral Support  --->
        [ ] USIC0
        [*] USIC1  (press SPACE to select)
        [ ] USIC2

    XMC4xxx USIC Configuration  --->
        USIC1 Channel 0 Configuration (Not used)  --->
        USIC1 Channel 1 Configuration (UART3)  --->

Device Drivers  --->
    [*] Serial Driver Support  --->
        Serial console (UART3)  --->

Time to compile it:

$ make

If everything compiled correctly you get a nuttx.bin:

$ ls -l nuttx.bin
-rwxrwxr-x 1 alan alan 79804 Jan  8 12:25 nuttx.bin

Plug the USB cable on you computer and on XMC4500-Relax board and use OpenOCD to flash it:

$ sudo openocd -f board/xmc4500-relax.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x0c000000"

You should see these messages:

$ sudo openocd -f board/xmc4500-relax.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x0c000000"
Open On-Chip Debugger 0.10.0+dev-00172-g7719e96 (2017-11-12-09:13)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
adapter speed: 4000 kHz
cortex_m reset_config sysresetreq
adapter speed: 1000 kHz
Info : No device selected, using first device.
Info : J-Link Lite-XMC4000 Rev.1 compiled Apr  2 2015 18:25:20
Info : Hardware version: 1.00
Info : VTarget = 3.300 V
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : xmc4500.cpu: hardware has 6 breakpoints, 4 watchpoints
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x08000200 msp: 0x20001e84
auto erase enabled
Info : Clearing flash status
Info : Clearing flash status
wrote 81920 bytes from file nuttx.bin in 19.998228s (4.000 KiB/s)

Configure your Linux machine to let your user to have access to serial port:

$ sudo usermod -a -G dialout alan

You just replace ‘alan’ with your username.

You need a USB/Serial adapter that works to 3.3V, like FDTI dongle or Silabs CP2102.

Connect the USB/Serial RXD to XMC4500-Relax board P0.1 (Header pin X1 pin 34), connect TXD to P0.0 (Header pin X1 pin 36) and connect the GND to board GND (Header pin X1 pin 1, 2, 3 or 4).

Configure minicom to use your USB/Serial adapter (i.e. at /dev/ttyUSB0) to 115200 8n1:

$ sudo minicom -s

Serial port setup
    A -    Serial Device      : /dev/ttyUSB0
    E -    Bps/Par/Bits       : 115200 8N1
    F - Hardware Flow Control : No
    G - Software Flow Control : No

Save setup as dfl

Exit

Press the XMC4500 Reset button and you should see:

NuttShell (NSH)                                                                 
nsh>

Type ‘help’ or ‘?’ to the the listing of enabled commands:

nsh> help
help usage:  help [-v] []

  [           dirname     help        mh          set         unset       
  ?           dd          hexdump     mount       sh          usleep      
  basename    df          kill        mv          sleep       xd          
  break       echo        ls          mw          test        
  cat         exec        mb          ps          time        
  cd          exit        mkdir       pwd         true        
  cp          false       mkfatfs     rm          uname       
  cmp         free        mkrd        rmdir       umount      

Builtin Apps:
  i2c
  ramtest
nsh> 

Running NuttX on ESP32 board

NuttX supports the Expressif ESP32 inclusive with SMP (Symmetric MultiProcessing) support, then I decided to post here the steps I followed to get it working:

Install esptool:

$ sudo pip install esptool

Install to toolchain:

You can download the pre-build toolchain:

https://esp-idf.readthedocs.io/en/latest/get-started/index.html

Or: you can compile it from source code:

$ git clone -b xtensa-1.22.x https://github.com/espressif/crosstool-NG.git
$ cd crosstool-NG
$ ./bootstrap && ./configure --prefix=$PWD && make install
$ ./ct-ng xtensa-esp32-elf
$ unset LD_LIBRARY_PATH
$ ./ct-ng build
$ chmod -R u+w builds/xtensa-esp32-elf
$ sudo cp -a builds/xtensa-esp32-elf/* /usr/local/

Now you can compile the NuttX:

$ mkdir ~/nuttxspace
$ cd ~/nuttxspace
$ git clone https://bitbucket.org/nuttx/nuttx
$ git clone https://bitbucket.org/nuttx/apps
$ cd nuttx
$ make distclean
$ ./tools/configure.sh esp32-core/nsh
$ make

The nuttx.bin generated is too big, you need to generate it from elf file (nuttx) :

$ esptool.py --chip esp32 elf2image --flash_mode dio --flash_size 4MB -o ./nuttx.bin nuttx

You will need the bootloader.bin and the partitions, you can get it from hello word example. You need to clone and update the submodules of esp-idf (read the Get Started from above link), then:

$ cp -a esp-idf/examples/get-started/hello_world .
$ cd hello_world
$ make
$ cp build/bootloader/bootloader.bin ~/nuttxspace/nuttx/
$ cp build/partitions_singleapp.bin ~/nuttxspace/nuttx/partitions.bin

The files nuttx.bin, bootloader.bin and partitions.bin need to be flashed in the board. To do that first put the board in ROM Boot mode:

Press and hold the BOOT button of ESP32 board (IO0 pin will be put in low level) and press and release the RESET button. Now you can release the BOOT button.

This is the command to flash:

$ sudo esptool.py --chip esp32 --port /dev/ttyUSB0 --baud 921600 write_flash 0x1000 bootloader.bin 0x8000 partitions.bin 0x10000 nuttx.bin
esptool.py v2.2
Connecting....
Chip is ESP32D0WDQ6 (revision 0)
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 921600
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0220
Compressed 19392 bytes to 11445...
Wrote 19392 bytes (11445 compressed) at 0x00001000 in 0.2 seconds (effective 901.6 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 82...
Wrote 3072 bytes (82 compressed) at 0x00008000 in 0.0 seconds (effective 10507.6 kbit/s)...
Hash of data verified.
Compressed 185024 bytes to 45527...
Wrote 185024 bytes (45527 compressed) at 0x00010000 in 2.1 seconds (effective 710.2 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting...
$

Finally open minicom and just reset the board, you should get it:

$ minicom


Welcome to minicom 2.7

OPTIONS: I18n 
Compiled on Feb  7 2016, 13:37:27.
Port /dev/ttyUSB0, 15:40:48

Press CTRL-A Z for help on special keys

ets Jun  8 2016 00:22:57

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
ets Jun  8 2016 00:22:57

rst:0x10 (RTCWDT_RTC_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0018,len:4
load:0x3fff001c,len:5568
ho 0 tail 12 room 4
load:0x40078000,len:0
load:0x40078000,len:13716
entry 0x40078fb4
I (31) boot: ESP-IDF v3.1-dev-162-geb659e4 2nd stage bootloader
I (31) boot: compile time 15:35:11
I (42) boot: Enabling RNG early entropy source...
I (42) boot: SPI Speed      : 40MHz
I (42) boot: SPI Mode       : DIO
I (45) boot: SPI Flash Size : 4MB
I (49) boot: Partition Table:
I (52) boot: ## Label            Usage          Type ST Offset   Length
I (60) boot:  0 nvs              WiFi data        01 02 00009000 00006000
I (67) boot:  1 phy_init         RF data          01 01 0000f000 00001000
I (75) boot:  2 factory          factory app      00 00 00010000 00100000
I (82) boot: End of partition table
I (86) esp_image: segment 0: paddr=0x00010020 vaddr=0x3ffb0df0 size=0x00054 (    84) load
I (95) esp_image: segment 1: paddr=0x0001007c vaddr=0x40080000 size=0x00400 (  1024) load
I (104) esp_image: segment 2: paddr=0x00010484 vaddr=0x40080400 size=0x00300 (   768) load
I (113) esp_image: segment 3: paddr=0x0001078c vaddr=0x400c0000 size=0x00000 (     0) load
I (122) esp_image: segment 4: paddr=0x00010794 vaddr=0x00000000 size=0x0f874 ( 63604) 
I (153) esp_image: segment 5: paddr=0x00020010 vaddr=0x3f400010 size=0x018e4 (  6372) map
I (155) esp_image: segment 6: paddr=0x000218fc vaddr=0x00000000 size=0x0e714 ( 59156) 
I (179) esp_image: segment 7: paddr=0x00030018 vaddr=0x400d0018 size=0x0d278 ( 53880) map
I (199) boot: Loaded app from partition at offset 0x10000
I (199) boot: Disabling RNG early entropy source...

NuttShell (NSH)
nsh> 
nsh> ?
help usage:  help [-v] []

  [           cmp         false       mkdir       rm          true        
  ?           dirname     free        mh          rmdir       uname       
  basename    dd          help        mount       set         umount      
  break       df          hexdump     mv          sh          unset       
  cat         echo        kill        mw          sleep       usleep      
  cd          exec        ls          ps          test        xd          
  cp          exit        mb          pwd         time        

Builtin Apps:
nsh>

How to get IRC notification on pidgin

I use pidgin as my centralized message application (Google Hangout, Jabber, ICQ, …) and also to get messages from freenode IRC.

But there is an issue with #IRC support on pidgin: I don’t know when someone posted a new message in the channel (#nuttx channel of course).

Then search about it a found a solution here:

https://ubuntuforums.org/showthread.php?t=1415173

There is a program called pidgin-irc-notifications that helps:

https://bitbucket.org/boralyl/pidgin-irc-notifications/overview

Note: I need to install pynotify first:

$ sudo pip install py-notify

Using FAT32 long file names on NuttX

NuttX supports FAT32 with long file names, but there is some details that you need to know to get it working. I will share here the issues that I was facing and how I fixed it.

The original FAT uses 8.3 (8 chars for name and 3 chars for extension), but it can supports files with up to 255 characters (including the extension).

To use it we need to enable long file name support in the menuconfig:

 File Systems  --->
     [*] FAT file system
     [*]   FAT upper/lower names
     [*]   FAT long file names             -------->  (CONFIG_FAT_LFN=y)
     (255)   FAT maximum file name size    -------->  (CONFIG_FAT_MAXFNAME=255)

Initially I will mount an empty SDCard to do my tests:

NuttShell (NSH)

nsh> mount -t vfat /dev/mmcsd0 /mnt

nsh> ls /mnt
/mnt:

nsh>

Let’s to create a small 8.3 file that will be renamed:

nsh> echo "Small file" > /mnt/tinyfile.txt
nsh> ls -l /mnt/tinyfile.txt
 -rw-rw-rw-      12 /mnt/tinyfile.txt
nsh>

Now I will try to rename it to a file with a long name:

nsh> mv /mnt/tinyfile.txt /mnt/this_is_a_file_with_a_long_name_jnsh>

Strange, while I was typing “this_is_a_file_with_a_long_name_just_for_test.txt” the typing was interrupted and the nsh> appeared.

Let see if the file was renamed:

nsh> ls /mnt
/mnt:
 this_is_a_file_with_a_long_name_

Then I remembered that the max line size of NSH was just 60 (CONFIG_NSH_LINELEN=60), I will increase it to 300 characters:

Application Configuration  --->
    NSH Library  --->
          Command Line Configuration  --->
              (300) Max command line length    -------->  (CONFIG_NSH_LINELEN=300)

I will create a tinyfile.txt again and try to rename it:

nsh> mv /mnt/tinyfile.txt /mnt/this_is_a_file_with_a_long_name_just_for_test.txt
nsh> 

Right, now the nsh prompt accept the enter the name, but let see if the file was created correctly:

nsh> ls /mnt
/mnt:
 this_is_a_file_with_a_long_name_
 this_is_a_file_with_a_long_name_

Whoa, it created two files with same name? How it is possible? Don’t worry, these files are different. Opening the SDCard in the Linux I can see:

this_is_a_file_with_a_long_name_j
this_is_a_file_with_a_long_name_just_for_test.txt

So, the LS command is showing only 32 characters. After some investigation I discovered that CONFIG_NAME_MAX=32 was the issue. Let to increase it to 255:

RTOS Features  --->
    Files and I/O  --->
        (255) Maximum size of a file name    -------->  (CONFIG_NAME_MAX=255)

Now I can see the files:

nsh> ls /mnt
/mnt:
 this_is_a_file_with_a_long_name_j
 this_is_a_file_with_a_long_name_just_for_test.txt

My next step is to create a file with 255 characters, like this:

this_file_will_have_a_big_filename_to_verify_how_to_work_with_files_with_long_names_our_test_need_to_have_the_max_filename_size_supported_by_fat32_that_means_it_could_have_up_to_255_characters_including_the_extension_in_the_filename_so_here_we_arrived.txt

nsh> echo "Test" > /mnt/this_file_will_have_a_big_filename_to_verify_how_to_work_with_files_with_long_names_our_test_need_to_have_the_max_filename_size_supported_by_fat32_that_means_it_could_have_up_to_255_characters_including_the_extension_in_the_filename_so_here_we_arrived.txt
nsh> ls /mnt
/mnt:
 this_file_will_have_a_big_filename_to_verify_how_to_work_with_files_with_long_names_our_test_need_to_have_the_max_filename_size_supported_by_fat32_that_means_it_could_have_up_to_255_characters_including_the_extension_in_the_filename_so_here_we_arrived.txt
nsh> 

Very nice! It worked correctly.

As you can see NuttX is similar to Linux kernel, many features depends on other features’ configuration. They are inter-dependent.

Fashing a bad firmware can drive your crazy

I put by XMC4500 in a state where OpenOCD was unable the communicate with it:

$ sudo openocd -f board/xmc4500-relax.cfg -c init -c "reset halt" -c "flash write_image erase nuttx.bin 0x0c000000"
Open On-Chip Debugger 0.10.0+dev-00172-g7719e96 (2017-11-12-09:13)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
adapter speed: 4000 kHz
cortex_m reset_config sysresetreq
adapter speed: 1000 kHz
Info : No device selected, using first device.
Info : J-Link Lite-XMC4000 Rev.1 compiled Apr  2 2015 18:25:20
Info : Hardware version: 1.00
Info : VTarget = 3.300 V
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Error: Could not initialize the debug port
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Info : SWD DPIDR 0x2ba01477
Error: Could not initialize the debug port
Error: Target not examined, reset NOT asserted!
in procedure 'reset' 
in procedure 'ocd_bouncer'

What happened?

I just changed BOARD_SYSDIV :

#  define BOARD_SYSDIV              0

But in clockconfig.c it does:

  /* Setup fSYS clock */

  regval  = (BOARD_ENABLE_PLL << SCU_SYSCLKCR_SYSSEL);
  regval |= SCU_SYSCLKCR_SYSDIV(BOARD_SYSDIV);
  putreg32(regval, XMC4_SCU_SYSCLKCR);

SCU_SYSCLKCR_SYSDIV is defined this way:

#  define SCU_SYSCLKCR_SYSDIV(n)    ((uint32_t)((n)-1) << SCU_SYSCLKCR_SYSDIV_SHIFT)

So, it wrote 0xffffffff to SCU_SYSCLKCR register. :-/

Then I tried to flash using JLinkExe from Segger, but it also didn’t work:

$ JLinkExe -if SWD
SEGGER J-Link Commander V6.14h (Compiled May 10 2017 18:39:45)
DLL version V6.14h, compiled May 10 2017 18:39:37

Connecting to J-Link via USB...O.K.
Firmware: J-Link Lite-XMC4000 Rev.1 compiled Apr  2 2015 18:25:20
Hardware version: V1.00
S/N: 551014400
VTref = 3.300V


Type "connect" to establish a target connection, '?' for help
J-Link>connect
Please specify device / core. : XMC4500-1024
Type '?' for selection dialog
Device>
Specify target interface speed [kHz]. : 4000 kHz
Speed>
Device "XMC4500-1024" selected.


Performing XMC4500 connection sequence.
No AP preselected. Assuming that AP[0] is the AHB-AP
AP-IDR: 0x24770011, Type: AHB-AP
AHB-AP ROM: 0xE00FF000 (Base addr. of first ROM table)
Found Cortex-M4 r0p1, Little endian.
FPUnit: 6 code (BP) slots and 2 literal slots
CoreSight components:
ROMTbl[0] @ E00FF000
ROMTbl[0][0]: E000E000, CID: B105E00D, PID: 000BB00C SCS
ROMTbl[0][1]: E0001000, CID: B105E00D, PID: 003BB002 DWT
ROMTbl[0][2]: E0002000, CID: B105E00D, PID: 002BB003 FPB
ROMTbl[0][3]: E0000000, CID: B105E00D, PID: 003BB001 ITM
ROMTbl[0][4]: E0040000, CID: B105900D, PID: 000BB9A1 TPIU
ROMTbl[0][5]: E0041000, CID: B105900D, PID: 000BB925 ETM
Cortex-M4 identified.
J-Link>loadbin ./nuttx.bin 0
Downloading file [./nuttx.bin]...
Writing target memory failed.

Fortunately the “erase” command worked:

J-Link>erase
Erasing device (XMC4500-1024)...
Comparing flash   [100%] Done.
Erasing flash     [100%] Done.
Verifying flash   [100%] Done.
J-Link: Flash download: Total time needed: 22.650s (Prepare: 0.041s, Compare: 0.000s, Erase: 22.604s, Program: 0.000s, Verify: 0.000s, Restore: 0.004s)
Erasing done.

But even after the flash erase the programming is not working:

J-Link>loadbin ./nuttx.bin 0
Downloading file [./nuttx.bin]...
Writing target memory failed.
J-Link>

Then I decided to try OpenOCD again:

$ sudo openocd -f board/xmc4500-relax.cfg
Open On-Chip Debugger 0.10.0+dev-00172-g7719e96 (2017-11-12-09:13)
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
adapter speed: 4000 kHz
cortex_m reset_config sysresetreq
adapter speed: 1000 kHz
Info : No device selected, using first device.
Info : J-Link Lite-XMC4000 Rev.1 compiled Apr  2 2015 18:25:20
Info : Hardware version: 1.00
Info : VTarget = 3.300 V
Info : clock speed 1000 kHz
Info : SWD DPIDR 0x2ba01477
Info : xmc4500.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : accepting 'telnet' connection on tcp/4444

And using telnet I tried to send the reset command:

$ telnet 127.0.0.1 4444
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Open On-Chip Debugger
> reset halt
timed out while waiting for target halted
TARGET: xmc4500.cpu - Not halted
in procedure 'reset' 
in procedure 'ocd_bouncer'


Halt timed out, wake up GDB.
> 

At this moment I thought my board could be damaged, but I decided to try to flash it anyway:

> flash write_image erase nuttx.bin 0x0c000000
auto erase enabled
Cannot communicate... target not halted.
auto_probe failed

Hmm, let to test only “halt” instead of “reset halt” :

> halt
target halted due to debug-request, current mode: Thread 
xPSR: 0x41000000 pc: 0x000000e6 msp: 0x2000ff3c

Good, now let to try again:

> flash write_image erase nuttx.bin 0x0c000000
auto erase enabled
Clearing flash status
Clearing flash status
wrote 81920 bytes from file nuttx.bin in 17.523651s (4.565 KiB/s)

Very good, my board is live again!