Booting the i.MX7 Sabre board from the mikroBUS SPI flash

dsc_0199
I’ve been working with the Freescale i.MX7 Sabre board and I wanted to free up the SD card so I could do some SDIO testing. I decided to boot the board from SPI flash ( I needed to test that out anyway ) and then load a kernel and rootfs across the network.

On the i.MX7 board there is a mikroBUS connector which basically just breaks out SPI/I2C/serial and a couple of GPIOs. There is a 8Mb SPI flash that you can get for this bus called the flash click. Just what I needed so I ordered one.

The documentation the board comes with claims the flash chip is a M25P80, no problem u-boot supports that. I added the config below into “include/configs/mx7dsabresd.h” to enable the u-boot SPI flash tools.

#define CONFIG_CMD_SF
#define CONFIG_MXC_SPI

Also enable SPI flash in the defconfig “configs/mx7dsabresd_secure_defconfig”

CONFIG_SPI_FLASH=y

You also need to add some code to your machine initialisation “board/freescale/mx7dsabresd/mx7dsabresd.c” so that the pads get configured properly.

define SPI_PAD_CTRL \
  (PAD_CTL_HYS | PAD_CTL_DSE_3P3V_49OHM | PAD_CTL_SRE_FAST)

static iomux_v3_cfg_t const ecspi3_pads[] = {
  MX7D_PAD_SAI2_RX_DATA__ECSPI3_SCLK | MUX_PAD_CTRL(SPI_PAD_CTRL),
  MX7D_PAD_SAI2_TX_SYNC__ECSPI3_MISO | MUX_PAD_CTRL(SPI_PAD_CTRL),
  MX7D_PAD_SAI2_TX_BCLK__ECSPI3_MOSI | MUX_PAD_CTRL(SPI_PAD_CTRL),
  MX7D_PAD_SAI2_TX_DATA__GPIO6_IO22 | MUX_PAD_CTRL(NO_PAD_CTRL),
};

int board_spi_cs_gpio(unsigned bus, unsigned cs)
{
       return (bus == 2 && cs == 0) ? (IMX_GPIO_NR(6, 22)) : -1;
}

static void setup_spi(void)
{
       imx_iomux_v3_setup_multiple_pads(ecspi3_pads, ARRAY_SIZE(ecspi3_pads));
}

One more edit to “board/freescale/mx7dsabresd/mx7dsabresd.c” to get the code above called. Into the function board_init add this

#ifdef CONFIG_MXC_SPI
       setup_spi();
#endif

So then I built myself a new u-boot

make mx7dsabresd_secure_defconfig         
CROSS_COMPILE=arm-linux-gnueabihf- make

Now take your newly minted u-boot, burn it to an SD card.

sudo dd if=u-boot.imx of=/dev/mmcblk0 seek=1 bs=1024

and it boots the board

U-Boot 2016.11-rc3-00044-g38cacda-dirty (Nov 09 2016 - 15:59:35 -0700)

CPU:   Freescale i.MX7D rev1.1 996 MHz (running at 792 MHz)
CPU:   Commercial temperature grade (0C to 95C) at 35C
Reset cause: POR
Board: i.MX7D SABRESD in secure mode
I2C:   ready
DRAM:  1 GiB
PMIC: PFUZE3000 DEV_ID=0x30 REV_ID=0x11
MMC:   FSL_SDHC: 0, FSL_SDHC: 1
Video: 480x272x24
In:    serial
Out:   serial
Err:   serial
switch to partitions #0, OK
mmc0 is current device
Net:   FEC0
Hit any key to stop autoboot:  0 
=>

Excellent now I want to scan for the flash chip so that I can burn my new u-boot to it.

=> sf probe 2:0
SF: Unsupported flash IDs: manuf 1c, jedec 3014, ext_jedec 1c30
Failed to initialize SPI flash at 2:0

WTF that’s not the jedec id for a N25P80. So if you go back to the link near the top of the page the mikroBUS flash click actually uses the EN25Q80B which is not in mainline u-boot. Back to the code we go.

Edit “drivers/mtd/spi/sf_params.c” and right after “#ifdef CONFIG_SPI_FLASH_EON /* EON */” add the line below.

  {"EN25Q80B",        0x1c3014, 0x0, 64 * 1024,  16, 0},

You’ll also nee to edit your config again “include/configs/mx7dsabresd.h” and add EON SPI to the configuration.

#define CONFIG_SPI_FLASH_EON

Rebuild it and burn it back to the SD card and try the probe again.

=> sf probe 2:0
SF: Detected EN25Q80B with page size 256 Bytes, erase size 64 KiB, total 1 MiB

So now u-boot can understand the flash, lets erase enough space for u-boot it and flash it.

=> sf erase 0 80000 
SF: 524288 bytes @ 0x0 Erased: OK
=> mmc read 0x80000000 2 400

MMC read: dev # 0, block # 2, count 1024 ... 1024 blocks read: OK
=> sf write 0x80000000 400 80000
device 0 offset 0x400, size 0x80000
SF: 524288 bytes @ 0x400 Written: OK

Now set the jumpers to boot from the SPI flash, remove the SD card and reset the board.dsc_0201

Here’s the mikroBUS flash click patch that should apply to mainline u-boot.

Scaling an STL in FreeCAD

I found a new way to scale STLs in FreeCAD where you don’t need to use the Python console.

Import the STL.
Go to the Part Workbench.
Select the STL in the Model list
Menu: Part->Create shape from mesh…
Select the new part
Menu: Part->convert to solid
Select the new part
Menu: Part->Refine shape
Change to the Draft Workbench
Menu: Draft->Clone

Now you can adjust the scale in the properties window of the clone.

Scaling Upverter STL Output in FreeCAD

Upverter has a nice STL export ( when it works ) of your board so you can do mechanical modelling for your design. Unfortunately when it gets imported into FreeCAD the scale is 10 times too small.

FreeCAD as of 0.15 does not have menu item that allows you to scale mesh imports. Luckily the python console can do the scaling of imported meshes.

First import the mesh ( STL file ) that you want to add to the current design. Select it in the “Combo view->Model” tree on the left of the FreeCAD screen. Then use the python console ( View->Views->Python console ) with the code below to scale it up 10 times.

import Mesh,BuildRegularGeoms
mat=FreeCAD.Matrix()
mat.scale(10.0,10.0,10.0)
mesh=App.ActiveDocument.ActiveObject.Mesh.copy()
mesh.transform(mat)
Mesh.show(mesh)

Now you should have a correctly sized copy of the STL board that you imported.

Here is the original post on importing and scaling ( I wanted to scale a mesh I had already imported ).

Aerogarden Bulb Mod

DSC_0220I’ve had this Aerogarden for a while but most of it’s life has been spent in a box in the garage because the lights quit working shortly after the 1 year warranty ran out. I decided to take it out this weekend and figure out what the issue was.

DSC_0217I disassembled the the light fixture and pulled out the power board and there are 2 250V 3A fuses on there. Both of then were blown. I de-soldered those thinking I would just replace them and the board would work. Then I got to thinking why do I want to fix the power supply for the proprietary ( and not very long lasting ) bulbs so instead I hacked it to take A19 ( “standard” ) light bulbs.

I didn’t take pictures along the way but the hack is pretty simple.

  1. Disassemble the light fixture – 2 of the bolts are Torx. You don’t need to remove the 2 screws next to the power plug
  2. Remove the old power supply and CFL mounting brackets
  3. Mark the size of the holes for the new light fixtures – medium socket
  4. Cut out the holes with a Dremel. I left the original CFL mounting holes in case I wanted to go back to the old bulbs.
  5. Cut a piece of aluminium L bracket to length and height. Notice there is a notch part way though the light fixture that you need to make room for. I also added 4 holes for alignment to the old screw mounts. I would check to make sure that the fixture has room to close properly at this point.
  6. Attach some 14 gauge wire to the bases and mount them to the L bracket.
  7. Attach the new wire to the power plug on the fixture.
  8. Slide the bases through the holes from step 3.
  9. Mount the L bracket to the fixture with 2 Tek self taping bolts.
  10. Re-assemble the fixture and you’re all done.

DSC_0214

DSC_0215
DSC_0213

I’ve currently got 3 60W incandescent grow bulbs in there but I’m on the lookout for some LED or CFL grow lights.

SWD with the MDBT40 BLE module

IMG_20150301_173929

I was recently introduced to the MDBT40 module that is based on the Nordic nRF51822. The processor nicely solves a number problems I’ve been looking at and the fact that it’s Bluetooth Smart also solves how I’ll get the data from it.

I designed some custom hardware ( the picture at left ) around the module and I needed some way to program it. My earlier post shows my investigations with SWD and OpenOCD.I put a SWD header on the board to re-program it.

OpenOCD Configuration for the nRF51822

bishop on the Nordic Developer Zone figured out how to get the nRF51822 part working with S-Link and OpenOCD. I took his configuration and adjusted it to work with the bus blaster v3.

source [find  interface/ftdi/dp_busblaster_kt-link.cfg]
transport select swd
set WORKAREASIZE 0
source [find target/nrf51.cfg]

Save it as nrf51822.cfg so then you can start openocd with it.

$ openocd -f target/nrf51822.cfg

OpenOCD defaults to a telnet interace on 4444.

$ telnet 127.0.0.1 4444
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Open On-Chip Debugger
> halt                                    
target state: halted
target halted due to debug-request, current mode: Thread
xPSR: 0x21000000 pc: 0x000163da msp: 0x20003fd8
> nrf51 mass_erase                        
> flash write_image _build/akkea1_tfw.hex
Padding image section 0 with 2112 bytes
Padding image section 1 with 3572 bytes
Padding image section 2 with 1 bytes
not enough working area available(requested 32)
no working area available, falling back to slow memory writes
wrote 95556 bytes from file _build/akkea1_tfw.hex in 14.267832s (6.540 KiB/s)
> reset

The board is now prgrammed with your custom firmware. This should work with any MDBT40 based board the has the 12K programming resistor per the Raytac application note. It should also work with other nRF51822 based board with a SWD interface

Recovering a bricked CC3D board using a bus blaster

IMG_20150214_155435A few months ago I went to upgrade my OpenPilot CC3D board an something went amiss and I ended up trashing the bootloader. I didn’t have a SWD dongle to reprogram it with so its been collecting dust. I just purchased a v3 bus blaster for a different project I’m working on and it has a SWD firmware so I thought I’d try and recover the CC3D. Below are the steps I used.

Download and install the latest OpenOCD

git clone git://git.code.sf.net/p/openocd/code openocd
cd openocd/
./bootstrap
./configure --prefix=/usr
make
sudo make install

Re-flash the Bus Blaster v3 for KT-link buffer

The instructions below came from here

git clone https://github.com/bharrisau/busblaster.git
cd busblaster/synthesis
openocd -f board/dp_busblaster_v3.cfg -c "adapter_khz 1000; init; svf system.svf; shutdown"

Getting/Building the bootloader

Now you need to get a bootloader to flash into the CC3D. You might be able to download bl_coptercontrol.hex but I’ve already got the OpenPilot development environment installed so I just built it using

make all_bl all_bu

If you don’t have the dev environment setup there are build instructions.

Here is the file I used bl_coptercontrol.hex but the regular disclaimers apply, if this file damages or bricks your CC3D I am not responsible.

OpenOCD config file

You need create a flashing script “cc3d-swd.cfg” to pass to OpenOCD.

source /usr/share/openocd/scripts/interface/ftdi/dp_busblaster_kt-link.cfg
transport select swd
source [find target/stm32f1x.cfg]

proc program_device () {
    reset init
        sleep 50
        flash probe 0
        sleep 50
        flash protect 0 0 last off
        sleep 50
        flash write_image erase unlock "path_to_hex_file/bl_coptercontrol.hex"
        sleep 50
        reset run
}

init
program_device ()
exit

Obiously replace “path_to_hex_file” to where you built or downloaded bl_coptercontrol.hex

Connect the bus blaster to the CC3d

I got the cable connections from here.

     adapter         ┊            target
                     ┊
     GND ──────────────────────── GND
                     ┊
     TCK ──────────────────────── SWCK
                     ┊
     TMS ──────────────────────── SWDAT
                     ┊

Finally reflashing the CC3D bootloader

openocd -f cc3d-swd.cfg

Mini Quadcopter Parts

I created some CAD drawings for quadcopter parts I kept breaking. I got tired of making and cutting out the fibreglass myself so I created some Oshpark boards.

You can go here to order your very own Quadcopter parts

Here’s what the first batch looked like. I didn’t like the look of the power board so I did a new layout for my second batch.

I’ve tried the screw positions with the KK multirotor board and the CC3D flight control board.

IMG_20150116_164459

AP510 APRS

I recently got my amateur radio certificate and was interested doing some APRS messaging so I purchased a Sainsonic AP510. It’s a nice little TNC with GPS, bluetooth and a thermometer.

It comes with minimal documentation in 4 languages and no software to configure it.

There’s a Yahoo Group that has firmware, configuration software and upgrade software. It’s annoying that you need to sign up for a Yahoo account to download the files there.

There’s also a Chinese website that has more firmware and some forums ( the Yahoo site has mined most needed data from there )

There’s also a good French website that explains how to flash the firmware and has some files needed for windows 7.

I found a German site that explains the serial cable pinout.

Upgrading the firmware

Standard disclaimer: this has worked for me but if it fails and bricks your AP510 I’m not responsible.

So far I have only gotten this to work on Windows, it will run under Wine but won’t connect. On windows XP these files AVRTTupdata.exe.zip and the VB6 runtime will be enough. On any newer windows MSCOMM32.OCX and MSSTDFMT.DLL will be required as well.

Make sure the ini file is in the same directory as the AVRTTupdata when you run it as it has the correct setup for the AVR in the AP510. The first time you run AVRTTUpdata it will be in Chinese, select language and put it into English.

  1. Use the option->comport menu item to chose the correct comport. Don’t change any of the other settings.
  2. Use the folder icon to load the hex file into the Upgrade tool.
  3. Turn off the AP510.
  4. Push and hold the power button on the AP510.
  5. While still holding the power button push the chip icon and the download will begin.
  6. Keep holding the power button until the download completes.

Voila your AP510 is now upgraded.

Linux Configuration tool

Update: This is deprecated use chirp instead as it now supports the AP510

The windows configuration tool is in the archive above. I didn’t want to start up a VM each time to change the config so I wrote a simple python tool AP510-setup to configure it. You’ll need to edit the script to change the comport, callsign and other settings. I’ve licensed it GPL V2 and please send me any improvements you make to the script.

To use the script:

  1. Edit the configuration
  2. Turn the AP510 off
  3. Start the script
  4. Turn the AP510 back on
  5. The script should dump the current settings, update the new settings and then dump the new settings

i’m Watch code to read the accelerometer

I figured out how to read and write the i2c bus on the imWatch so I can now sample at whatever rate I’d like. This tarball includes code that should work upto 200Hz without overloading the processor.

Faster rates should be possible but it would mean getting the FIFO in the accelerometer working and then backdating the samples.

Building the JNI

I’m not going to detail how to use a JNI within an Android project. there are plenty of other tutorials out there.

From the tarball put “akkea-jni.c” and “Android.mk” into the jni directory of your android >project. Then run

ndk-build

You should get output similar to below.

Compile thumb : akkea-jni <= akkea-jni.c
SharedLibrary : libakkea-jni.so
Install : libakkea-jni.so => libs/armeabi/libakkea-jni.so

Using the Accelerometer JNI code

Add these native prototypes and constants to your android code.

public native int readAccel( SeismoVector vector, int fd, long startTime );
public native int openI2c( String path );
public native void closeI2c( int fileHandle );
public native int i2cReadReg( int file, int addr, int reg);
public native int i2cWriteReg( int file, int addr, int reg, int val);

int i2cHandle;
final byte ACCEL_I2C_ADDR = 25;
final int ACCEL_RATE_OFF = 0;
final int ACCEL_RATE_1HZ = 1 << 4;
final int ACCEL_RATE_10HZ = 2 << 4;
final int ACCEL_RATE_25HZ = 3 << 4;
final int ACCEL_RATE_50HZ = 4 << 4;
final int ACCEL_RATE_100HZ = 5 << 4;
final int ACCEL_RATE_200HZ = 6 << 4;
final int ACCEL_RATE_400HZ = 7 << 4;
final int ACCEL_RATE_1_6KHZ = 8 << 4;
final int ACCEL_RATE_5KHZ = 9 << 4;

The I2C bus and accelerometer need to be setup

public boolean openI2C() {

  try
  {
    Runtime.getRuntime().exec("chmod 777 /dev/i2c-0");
  } catch (IOException e) {
    e.printStackTrace();
  }

  i2cHandle = openI2c( "/dev/i2c-0" );

  if( i2cHandle <= 0 )
    return false;

  int[] arr = new int[4];
  for( int i=0; i<128; i++ ) {
    arr[0] = i2cReadReg( i2cHandle, ACCEL_I2C_ADDR, i );
    if( arr[0] != 0 )
      Log.d(TAG, "Register " + String.valueOf( i ) + " = " + String.valueOf( arr[0] ));
  }

  int val;
  val = i2cReadReg( i2cHandle, ACCEL_I2C_ADDR, 0x20 );
  val = ( val & 0x0F );

  switch( HZ ) {
    case 1:
      val |= ACCEL_RATE_1HZ;
      break;
    case 10:
      val |= ACCEL_RATE_10HZ;
      break;
    case 25:
      val |= ACCEL_RATE_25HZ;
      break;
    case 100:
      val |= ACCEL_RATE_100HZ;
      break;
    case 200:
      val |= ACCEL_RATE_200HZ;
      break;
    case 400:
      val |= ACCEL_RATE_400HZ;
      break;
    case 1600:
      val |= ACCEL_RATE_1_6KHZ;
      break;
  }

  i2cWriteReg( i2cHandle, ACCEL_I2C_ADDR, 0x20, val );

  val = i2cReadReg( i2cHandle, ACCEL_I2C_ADDR, 0x20 );
  Log.d(TAG, "Register Rate " + String.valueOf( val ));

  return true;
}

I then use a TimerTask to read the accelerometer at whatever rate I want.

class sensorTask extends TimerTask {
  @Override
    public void run() {
      long time;

      if( index >= totalSamples ) {
        Log.d(TAG, "index past end of array");
        return;
      }

      SeismoVector vector = new SeismoVector();

      time = readAccel( vector, i2cHandle, startTime );

      if( startTime == 0 )
        startTime = time;

      vectors[index++] = vector;

    }
};

Hobby King X230 replacement parts

I purchased the X230 Quad copter kit from hobby king in December. I’ve been learning to fly it and in the process have been breaking parts.

Hobby king doesn’t sell replacement parts so I’ve been patching it up as I go along. Some of the pieces are getting too broken to be patched. I’ve created a DXF of the parts and I’m going to have them built out of carbon fibre for increased crash resistance 🙂

Quad_designAt left is a 1:2 jpeg of the parts. If you’d like the DXF it’s here

If you’d like the SVG it’s here

Edit: I don’t recommend using carbon fibre to prototype this unless you have a laser cutter.