Friday, 24 November 2017

Create your Own I2C module

If you have a daughter system, for example sensor(s), and you need to talk to it from the master MCU, you can of course use CAN bus. But you can also turn it into an I2C module.

ref:
http://www.instructables.com/id/I2C-between-Arduinos/
http://dsscircuits.com/articles/arduino-i2c-slave-guide

Create a New Branch from a History Commit

When we want to checkout a new branch we do:

git checkout -b name-of-new-branch 

This is actually shorten for:

git checkout -b name-of-new-branch current-branch 

That is to say, if we don't specify the starting point of this new branch, it starts by default from the current active branch. Since every commit has an SHA1 (Hash value) as its ID, we can use these IDs as the start pointer when we are using checkout command. For example:

git checkout -b name-of-new-branch 169d2dc 

In this way, the active branch is now switched to this new branch and things are the same with branch 169d2dc.

Note that we might need to use the long full SHA1 ID in case the short one conflicts with others.


ref:
https://liam0205.me/2015/04/29/git-checkout-history-version/

Git Workflow for Embedded Systems

The special thing about embedded system is that it sits in between software and hardware. Therefore, you might hit walls if adopting the traditional git workflow. The following blog specified the obstacles really well:

https://medium.com/jumperiot/how-to-use-git-flow-in-embedded-software-development-dbb2a78da413

I met the problem of different hardware configurations and I have to go back to history versions to branch out and do some redundant work. But most importantly, keep the following three things in mind:

(1) Split the code base into unrelated libraries/modules that support different configurations, manage them separately and then do a configuration management. Note that you’ll need to invest in proper software architecture and abstraction layers.
(2) Control different configuration with features flags on the same branches.
(3) Create isolated and long lived branches for each version/hardware configuration.

ref:
https://medium.com/jumperiot/how-to-use-git-flow-in-embedded-software-development-dbb2a78da413
https://liam0205.me/2015/04/29/git-checkout-history-version/

Thursday, 23 November 2017

Producer Consumer Model in Python

All credits go to Akshar Raaj

Note that if Queue is used, queue itself is threading safe to use because Queue encapsulates the behaviour of Condition, wait(), notify(), acquire() etc.

ref:
http://agiliq.com/blog/2013/10/producer-consumer-problem-in-python/

Wednesday, 22 November 2017

Install Python 3 Packets

By default if you install something by
      pip install some_packet
I got the packet in Python 2.7 installed.

But if a packet, say pyusb, is a dependent to another packet that has be on Python 3, say pystlink, I will get:

Traceback (most recent call last):
  File "pystlink.py", line 4, in <module>
    import lib.stlinkusb
  File "/home/boris/Softwares/EmbeddedSystem/pystlink/pystlink-master/lib/stlinkusb.py", line 1, in <module>
    import usb.core
ImportError: No module named 'usb'

If I try to go to Python 3 and import usb, I would get the same output. Therefore, I need to install pyusb in Python 3 and here is how.

sudo apt-get install python3-pip
sudo pip3 install MODULE_NAME

ref:
https://stackoverflow.com/questions/10763440/how-to-install-python3-version-of-package-via-pip-on-ubuntu

Monday, 20 November 2017

UART Communication Between NodeMCU and Arduino

First, I'm using MicroPython on the NodeMCU side.

Test 1:
Connections:
VIN     -    VIN
GND   -    GND
3          -    D3
2          -    D2

Arduino side code:
#include <SoftwareSerial.h>
SoftwareSerial ArduinoSerial(3, 2); // RX, TX
void setup() 
{
  Serial.begin(115200);
  ArduinoSerial.begin(4800);
}

void loop() 
{
  ArduinoSerial.write('abc');
  delay(100);
}

NudeMCU side:
from machine import 
UART uart = UART(1, 4800) 
uart.init(4800, bits=8, parity=None, stop=1) 
uart.read()

And...it didn't work.





There is UART 0 that is connected to the usb-serial converter and runs the repl.
There is UART 1 that only has a TX pin so I cannot receive data.
OSError: UART(1) can't read
There is UART 2 that doesn't seem to exist in micropython.
ValueError: UART(2) does not exist

So be it...Then we have two choices:
(1) use UART 1 but only sending data from NodeMCU to Arduino -- Test 2
(2) use UART 0 but not use the USB. -- Test 3

Both of the above tests are done using Arduino after flashing program to NodeMCU.

Test 2:




Test 3:





ref:
https://www.arduinoall.com/article/59/nodemcu-esp8266-esp8285-arduino-30-esp8266-nodemcu-%E0%B8%95%E0%B8%B4%E0%B8%94%E0%B8%95%E0%B9%88%E0%B8%AD-arduino-%E0%B9%81%E0%B8%9A%E0%B8%9A-serial
https://www.arduino.cc/en/Reference/SoftwareSerial
https://www.arduino.cc/en/Tutorial/SoftwareSerialExample
https://docs.micropython.org/en/latest/esp8266/library/machine.UART.html?highlight=uart
https://github.com/micropython/micropython/issues/2391
https://github.com/esp8266/Arduino/issues/482

Thursday, 16 November 2017

MicroPython + NodeMCU Getting Started


(0) sudo pip install esptool
(1) esptool.py --port /dev/ttyUSB0 erase_flash

esptool.py v2.1
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 7.8s
Hard resetting...

(2) esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20171101-v1.9.3.bin 

esptool.py v2.1
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0040
Compressed 600888 bytes to 392073...
Wrote 600888 bytes (392073 compressed) at 0x00000000 in 8.9 seconds (effective 542.3 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting...

(3) picocom /dev/ttyUSB0 -b 115200

picocom v1.7

port is        : /dev/ttyUSB0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,

Terminal ready

////////////////////////////////////////////////
I have to unplug and plug the USB back in. Resetting the device won't work. I got the following error message:

picocom v1.7

port is        : /dev/ttyUSB0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,


FATAL: cannot open /dev/ttyUSB0: Device or resource busy

This could also be a bad usb cable. Use a good one with a data line on it.


///////////////////////////////////////////////////
///////////////////////////////////////////////////
///////////////////////////////////////////////////
Also you might need to press enter a few times to see the Python prompt:

.......
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,

Terminal ready

>>> 
>>> 
>>> 

(4) Start playing around
Hookup an LED on D7 which is mapped to GPIO13.

>>> import machine
>>> pin = machine.Pin(13, machine.Pin.OUT)
>>> pin.on()
>>> pin.off()

You can see the LED goes on and off now. Happy tinkering!


ref:
https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html
https://dev.to/kenwalger/micropython-and-the-nodemcu-esp8266
http://www.instructables.com/id/MicroPython-Basics-Using-NodeMCU-ESP8266/
https://hackaday.com/2016/07/21/micropython-on-the-esp8266-kicking-the-tires/

Wednesday, 15 November 2017

Getting Started with NodeMCU

Many suggest to install different kind of drivers. But I thought would at least see that list in dmesg.
But the first reference woke me up with some similar problems I met.

USE A BETTER/DIFFERENT USB CABLE!!!!

ref:
http://www.esp8266.com/viewtopic.php?f=13&t=4366
https://www.silabs.com/products/development-tools/software/usb-to-uart-bridge-vcp-drivers
https://www.marginallyclever.com/2017/02/setup-nodemcu-drivers-arduino-ide/
https://github.com/nodemcu/nodemcu-devkit/blob/master/Drivers/CH341SER_LINUX.ZIP
http://mohanp.com/nodemcu-esp8266-with-adruino-ide/

Monday, 6 November 2017

System Panic in Particle System (STM32)



particle System Panic reset reason 130

ref:
https://community.particle.io/t/photon-system-panic-hard-fault-task-stack-size/30758
https://community.particle.io/t/core-firmware-sos-panic-codes/4337/5

Monday, 30 October 2017

Manage Startup Applications in Ubuntu

Ubuntu has an GUI called Startup Applications.

ref:
https://linux.cn/article-5943-1.html

Wednesday, 18 October 2017

You really count on using things like GitKraken in the Server? Git itself is powerful enough:

git log --graph --full-history --all --color \
        --pretty=format:"%x1b[31m%h%x09%x1b[32m%d%x1b[0m%x20%s"
ref:
https://stackoverflow.com/questions/1838873/visualizing-branch-topology-in-git


Tuesday, 19 September 2017

Particle Installation Troubleshooting

If you see such things like:

node-pre-gyp ERR! Pre-built binaries not found for serialport@4.0.7 and node@4.2.1 (node-v46 ABI) (falling back to source compile with node-gyp)
gyp WARN EACCES user "root" does not have permission to access the dev dir "/root/.node-gyp/4.2.1"
gyp WARN EACCES attempting to reinstall using temporary dev dir "/usr/local/lib/node_modules/particle-cli/node_modules/serialport/.node-gyp"
make: Entering directory '/usr/local/lib/node_modules/particle-cli/node_modules/serialport/build'
make: *** No rule to make target '../.node-gyp/4.2.1/include/node/common.gypi', needed by 'Makefile'. Stop.
make: Leaving directory '/usr/local/lib/node_modules/particle-cli/node_modules/serialport/build'
gyp ERR! build error
gyp ERR! stack Error: make failed with exit code: 2
gyp ERR! stack at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:270:23)
gyp ERR! stack at emitTwo (events.js:87:13)
gyp ERR! stack at ChildProcess.emit (events.js:172:7)
gyp ERR! stack at Process.ChildProcess.handle.onexit (internal/childprocess.js:200:12)
gyp ERR! System Linux 4.9.24+
gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/usr/local/lib/node_modules/particle-cli/node_modules/serialport/build/Release/serialport.node" "--module_name=serialport" "--module_path=/usr/local/lib/node_modules/particle-cli/node_modules/serialport/build/Release"
gyp ERR! cwd /usr/local/lib/node_modules/particle-cli/node_modules/serialport
gyp ERR! node -v v4.2.1
gyp ERR! node-gyp -v v3.0.3
gyp ERR! not ok
node-pre-gyp ERR! build error
node-pre-gyp ERR! stack Error: Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/usr/local/lib/node_modules/particle-cli/node_modules/serialport/build/Release/serialport.node --module_name=serialport --module_path=/usr/local/lib/node_modules/particle-cli/node_modules/serialport/build/Release' (1)
node-pre-gyp ERR! stack at ChildProcess. (/usr/local/lib/node_modules/particle-cli/node_modules/serialport/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack at emitTwo (events.js:87:13)
node-pre-gyp ERR! stack at ChildProcess.emit (events.js:172:7)
node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:818:16)
node-pre-gyp ERR! stack at Process.ChildProcess.handle.onexit (internal/childprocess.js:211:5)
node-pre-gyp ERR! System Linux 4.9.24+
node-pre-gyp ERR! command "/usr/local/bin/node" "/usr/local/lib/node_modules/particle-cli/node_modules/serialport/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /usr/local/lib/node_modules/particle-cli/node_modules/serialport
node-pre-gyp ERR! node -v v4.2.1
node-pre-gyp ERR! node-pre-gyp -v v0.6.32
node-pre-gyp ERR! not ok
Failed to execute '/usr/local/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/usr/local/lib/node_modules/particle-cli/node_modules/serialport/build/Release/serialport.node --module_name=serialport --module_path=/usr/local/lib/node_modules/particle-cli/node_modules/serialport/build/Release' (1)
npm ERR! Linux 4.9.24+
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "-g" "particle-cli"
npm ERR! node v4.2.1
npm ERR! npm v2.14.7
npm ERR! code ELIFECYCLE
npm ERR! serialport@4.0.7 install: node-pre-gyp install --fallback-to-build
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the serialport@4.0.7 install script 'node-pre-gyp install --fallback-to-build'.
npm ERR! This is most likely a problem with the serialport package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node-pre-gyp install --fallback-to-build
npm ERR! You can get their info via:
npm ERR! npm owner ls serialport
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! /home/pi/npm-debug.log


This is a known but relatively undocumented bug with the particle-cli installation process. The following should work:

           $ sudo npm install -g --unsafe-perm node-pre-gyp npm serialport particle-cli

ref:
https://community.particle.io/t/connecting-particle-photon-to-local-server-on-raspberry-pi-zero-w/32227
https://community.particle.io/t/new-cli-on-os-x-10-11-el-capitan-solved/28802/8



Sunday, 17 September 2017

"The volume "boot" has only 0 bytes disk space remaining" error

Very simple

sudo apt-get autoremove

This will delete those unuseful kernels in /boot.

ref:
https://ubuntuforums.org/showthread.php?t=2239126
https://askubuntu.com/questions/345588/what-is-the-safest-way-to-clean-up-boot-partition

Thursday, 7 September 2017

Setting up GCC ARM Coding Environment for STMicrocontroller


1. Installing compiler and stlink

To compile C and/or C++ source code of your firmware you will need gcc-arm-none-eabi compiler and stlink.

Installing gcc-arm-none-eabi

What is extremely useful, there are complete and easy to install packages for all major platforms (https://launchpad.net/~team-gcc-arm-embedded/+archive/ubuntu/ppa)
sudo add-apt-repository ppa:team-gcc-arm-embedded/ppa
sudo apt-get update
sudo apt-get install gcc-arm-none-eabi

Installing stlink

At first, we need install dependencies then build it from sources (https://github.com/texane/stlink/blob/master/doc/compiling.md#build-from-sources).
sudo apt-get install git build-essential libusb-1.0.0-dev cmake
cd $HOME
git clone git@github.com:texane/stlink.git
cd stlink
make release
cd build/Release && make install DESTDIR=_install
echo "export PATH=\$PATH:$HOME/stlink/build/Release/_install/usr/local/bin" >> $HOME/.bashrc

2. Compiling and burning the code

Now that you have the toolchain installed, a next step is to compile the source code into a .ELF, then generate .BIN file and finally burn this this binary file to STM32 chip using ST-Link v2 programmer.

Example code

Here is an example content of main.c file. The code does nothing except getting stuck in an endless loop but it’s always something!
int
main(void)
{

        while (1);
}

Compiling

The command below will compile your code. It’s GCC so I assume it looks familiar to you and no additional explanations are needed. If you want perform compilation for some other MCU then you need specify at least appropriate -mcpu, .LD and .S files (not provided in this tutorial)
$ arm-none-eabi-gcc -std=gnu99 -g -O2 -Wall -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0 -fsingle-precision-constant -Wdouble-promotion main.c -o main.elf
After performing successful compilation, you can check program and data memory size with this command.
$ arm-none-eabi-size -tA main.elf 
main.elf  :
section              size        addr
.isr_vector           192   134217728
.text                6404   134217920
.rodata                60   134224324
.ARM                    8   134224384
.init_array             8   134224392
.fini_array             4   134224400
.data                1092   536870912
.jcr                    4   536872004
.bss                   32   536872008
._user_heap_stack    1536   536872040
.ARM.attributes        40           0
.comment               31           0
.debug_line          7416           0
.debug_info         22917           0
.debug_abbrev        6837           0
.debug_aranges        744           0
.debug_loc           6584           0
.debug_ranges         472           0
.debug_str           5717           0
.debug_frame         2004           0
Total               62102

Generating .BIN

Most programmers will not accept a GNU executable as an input file, so we need to do a little more processing. So, the next step is about converting the information form .ELF into .BIN file. The GNU utility that does this is called arm-none-eabi-objcopy.
$ arm-none-eabi-objcopy -O binary main.elf main.bin

Burning

The utility called st-flash can program processors using the content of the .BIN files specified on the command line. With the command below, the file main.bin will be burned into the flash memory.
$ st-flash write main.bin 0x8000000
Voila! Chip is programmed.

3. Make and Makefiles

Now, we can automate this process by creating a Makefile and putting our commands there. The structure of a Makefile is very simple, and more information about it can be found here. Utility make reads automatically a Makefile file in the folder where you launch it. Take a look at simple Makefile presented bellow.
TARGET=main

CC=arm-none-eabi-gcc
LD=arm-none-eabi-gcc
AR=arm-none-eabi-ar
AS=arm-none-eabi-as
CP=arm-none-eabi-objcopy
OD=arm-none-eabi-objdump
SE=arm-none-eabi-size
SF=st-flash

CFLAGS  = -std=gnu99 -g -O2 -Wall
CFLAGS += -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0
CFLAGS += -fsingle-precision-constant -Wdouble-promotion

SRCS =  main.c

.PHONY: $(TARGET)

$(TARGET): $(TARGET).elf

$(TARGET).elf: $(SRCS)
        $(CC) $(INCLUDE) $(CFLAGS) $^ -o $@
        $(CP) -O binary $(TARGET).elf $(TARGET).bin

clean:
        rm -f *.o $(TARGET).elf $(TARGET).bin

flash:
        $(SF) write $(TARGET).bin 0x8000000
If you launch a simple make in the terminal, only label “all” will be executed. When you launch make flash label “flash” will be executed, and so on.

4. Summary

Essentially, assuming that our program is in main.c, only those three things are needed to compile and burn the code to STM32 chip.
$ arm-none-eabi-gcc -std=gnu99 -g -O2 -Wall -mlittle-endian -mthumb -mthumb-interwork -mcpu=cortex-m0 -fsingle-precision-constant -Wdouble-promotion main.c -o main.elf
$ arm-none-eabi-objcopy -O binary main.elf main.bin
$ st-flash write main.bin 0x8000000


It’s important to highlight that we can easily automate whole process with Makefiles. Sooner or later you will need it!



ref:
http://blog.podkalicki.com/how-to-compile-and-burn-the-code-to-stm32-chip-on-linux-ubuntu/
https://startingelectronics.org/tutorials/STM32-microcontrollers/programming-STM32-flash-in-Linux/
http://fishpepper.de/2016/09/16/installing-using-st-link-v2-to-flash-stm32-on-linux/

Tuesday, 5 September 2017

The Django Book Study -- Installing Django

I stucked a bit at this part:

To use this new Python virtual environment, we have to activate it, so let’s go back to the command prompt and type the following:
env_mysite\scripts\activate
This will run the activate script inside your virtual environment’s \scripts folder. You will notice your command prompt has now changed:
(env_mysite) C:\Users\Nigel\OneDrive\Documents\mysite_project>
The (env_mysite) at the beginning of the command prompt lets you know that you are running in the virtual environment. Our next step is to install Django.
Apparently, this is how it goes in Windows. In Linux, we would have to do the following:
source bin/activate
And then we can see:
boris@boris-D630:~/workspace/mysite_project/env_mysite$ source bin/activate
(env_mysite)boris@boris-D630:~/workspace/mysite_project/env_mysite$


ref:
https://pypi.python.org/pypi/virtualenv/1.8.2

Monday, 7 August 2017

dpkg: error processing package linux-image-generic (--configure): dependency problems - leaving unconfigured

I was installing ffmpeg the other day and something seems failed to be installed. I thought ffmpeg was not successfully installed but it actually DID!! I did it again and again. Everytime I'm getting error messages:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
ffmpeg is already the newest version (7:3.3.3-1ubuntu1~16.04.york0).
The following packages were automatically installed and are no longer required:
  linux-headers-4.4.0-72 linux-headers-4.4.0-72-generic linux-image-4.4.0-72-generic linux-image-extra-4.4.0-72-generic
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 277 not upgraded.
3 not fully installed or removed.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Setting up linux-image-extra-4.4.0-89-generic (4.4.0-89.112) ...
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.4.0-89-generic /boot/vmlinuz-4.4.0-89-generic
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.4.0-89-generic /boot/vmlinuz-4.4.0-89-generic
update-initramfs: Generating /boot/initrd.img-4.4.0-89-generic

gzip: stdout: No space left on device
E: mkinitramfs failure cpio 141 gzip 1
update-initramfs: failed for /boot/initrd.img-4.4.0-89-generic with 1.
run-parts: /etc/kernel/postinst.d/initramfs-tools exited with return code 1
dpkg: error processing package linux-image-extra-4.4.0-89-generic (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of linux-image-generic:
 linux-image-generic depends on linux-image-extra-4.4.0-89-generic; however:
  Package linux-image-extra-4.4.0-89-generic is not configured yet.

dpkg: error processing package linux-image-generic (--configure):
 dependency problems - leaving unconfigured
dpkg: dependency problems prevent configuration of linux-generic:
 linux-generic depends on linux-image-generic (= 4.4.0.89.95); however:
  Package linux-image-generic is not configured yet.

dpkg: error processing package linux-generic (--configure):
 dependency problems - leaving unconfigured
No apport report written because the error message indicates its a followup error from a previous failure.
                                                                                                          No apport report written because the error message indicates its a followup error from a previous failure.
                                                                             Errors were encountered while processing:
 linux-image-extra-4.4.0-89-generic
 linux-image-generic
 linux-generic
E: Sub-process /usr/bin/dpkg returned an error code (1)

Actually, it turned out the same errors appear when I am trying to install any softwares.

This is what I get and did:

Your problem is:
update-initramfs: Generating /boot/initrd.img-3.16.0-43-generic

gzip: stdout: No space left on device
E: mkinitramfs failure cpio 141 gzip 1
Solution:
apt-get autoremove

So I did sudo apt-get autoremove

and here are the outputs:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-4.4.0-72 linux-headers-4.4.0-72-generic linux-image-4.4.0-72-generic linux-image-extra-4.4.0-72-generic
0 upgraded, 0 newly installed, 4 to remove and 277 not upgraded.
3 not fully installed or removed.
After this operation, 297 MB disk space will be freed.
Do you want to continue? [Y/n] y
(Reading database ... 259942 files and directories currently installed.)
Removing linux-headers-4.4.0-72-generic (4.4.0-72.93) ...
Removing linux-headers-4.4.0-72 (4.4.0-72.93) ...
Removing linux-image-extra-4.4.0-72-generic (4.4.0-72.93) ...
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
update-initramfs: Generating /boot/initrd.img-4.4.0-72-generic
run-parts: executing /etc/kernel/postinst.d/pm-utils 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
run-parts: executing /etc/kernel/postinst.d/update-notifier 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
run-parts: executing /etc/kernel/postinst.d/zz-update-grub 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.0-89-generic
Found initrd image: /boot/initrd.img-4.4.0-89-generic
Found linux image: /boot/vmlinuz-4.4.0-75-generic
Found initrd image: /boot/initrd.img-4.4.0-75-generic
Found linux image: /boot/vmlinuz-4.4.0-72-generic
Found initrd image: /boot/initrd.img-4.4.0-72-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
Found Windows 7 (loader) on /dev/sda1
Found Windows 7 (loader) on /dev/sda2
done
Removing linux-image-4.4.0-72-generic (4.4.0-72.93) ...
Examining /etc/kernel/postrm.d .
run-parts: executing /etc/kernel/postrm.d/initramfs-tools 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
update-initramfs: Deleting /boot/initrd.img-4.4.0-72-generic
run-parts: executing /etc/kernel/postrm.d/zz-update-grub 4.4.0-72-generic /boot/vmlinuz-4.4.0-72-generic
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.0-89-generic
Found initrd image: /boot/initrd.img-4.4.0-89-generic
Found linux image: /boot/vmlinuz-4.4.0-75-generic
Found initrd image: /boot/initrd.img-4.4.0-75-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
Found Windows 7 (loader) on /dev/sda1
Found Windows 7 (loader) on /dev/sda2
done
Setting up linux-image-extra-4.4.0-89-generic (4.4.0-89.112) ...
run-parts: executing /etc/kernel/postinst.d/apt-auto-removal 4.4.0-89-generic /boot/vmlinuz-4.4.0-89-generic
run-parts: executing /etc/kernel/postinst.d/initramfs-tools 4.4.0-89-generic /boot/vmlinuz-4.4.0-89-generic
update-initramfs: Generating /boot/initrd.img-4.4.0-89-generic
run-parts: executing /etc/kernel/postinst.d/pm-utils 4.4.0-89-generic /boot/vmlinuz-4.4.0-89-generic
run-parts: executing /etc/kernel/postinst.d/unattended-upgrades 4.4.0-89-generic /boot/vmlinuz-4.4.0-89-generic
run-parts: executing /etc/kernel/postinst.d/update-notifier 4.4.0-89-generic /boot/vmlinuz-4.4.0-89-generic
run-parts: executing /etc/kernel/postinst.d/zz-update-grub 4.4.0-89-generic /boot/vmlinuz-4.4.0-89-generic
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-4.4.0-89-generic
Found initrd image: /boot/initrd.img-4.4.0-89-generic
Found linux image: /boot/vmlinuz-4.4.0-75-generic
Found initrd image: /boot/initrd.img-4.4.0-75-generic
Found memtest86+ image: /memtest86+.elf
Found memtest86+ image: /memtest86+.bin
Found Windows 7 (loader) on /dev/sda1
Found Windows 7 (loader) on /dev/sda2
done
Setting up linux-image-generic (4.4.0.89.95) ...
Setting up linux-generic (4.4.0.89.95) ...



########################################################################
// Later on my computer at work, I was trying to install screen and I got:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages were automatically installed and are no longer required:
  linux-headers-4.4.0-70 linux-headers-4.4.0-70-generic linux-headers-4.4.0-72 linux-headers-4.4.0-72-generic linux-headers-4.4.0-75 linux-headers-4.4.0-75-generic
  linux-headers-4.4.0-78 linux-headers-4.4.0-78-generic linux-headers-4.4.0-79 linux-headers-4.4.0-79-generic linux-headers-4.4.0-81 linux-headers-4.4.0-81-generic
  linux-image-4.4.0-70-generic linux-image-4.4.0-72-generic linux-image-4.4.0-75-generic linux-image-4.4.0-78-generic linux-image-4.4.0-79-generic
  linux-image-4.4.0-81-generic linux-image-extra-4.4.0-70-generic linux-image-extra-4.4.0-72-generic linux-image-extra-4.4.0-75-generic linux-image-extra-4.4.0-78-generic
  linux-image-extra-4.4.0-79-generic linux-image-extra-4.4.0-81-generic
Use 'sudo apt autoremove' to remove them.
Suggested packages:
  iselect | screenie | byobu ncurses-term
The following NEW packages will be installed:
  screen
0 upgraded, 1 newly installed, 0 to remove and 264 not upgraded.
Need to get 560 kB of archives.
After this operation, 972 kB of additional disk space will be used.
Get:1 http://ca.archive.ubuntu.com/ubuntu xenial/main amd64 screen amd64 4.3.1-2build1 [560 kB]
Fetched 560 kB in 25s (22.3 kB/s)                                                                                                                                           
Selecting previously unselected package screen.
(Reading database ... 415154 files and directories currently installed.)
Preparing to unpack .../screen_4.3.1-2build1_amd64.deb ...
Unpacking screen (4.3.1-2build1) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...
ureadahead will be reprofiled on next reboot
Processing triggers for install-info (6.1.0.dfsg.1-5) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up screen (4.3.1-2build1) ...
Processing triggers for systemd (229-4ubuntu10) ...
Processing triggers for ureadahead (0.100.0-19) ...

So I guess Ubuntu pushed some updates...

ref:
https://askubuntu.com/questions/517857/dpkg-error-processing-package-linux-image-generic-configure-dependency-pro

Saturday, 1 July 2017

Troubleshoot "failed to find target android-xx" When Including Android Studio Project

It's very common that the example project was built under other Android SDK. I got:

    failed to find target android-23

But I have android-25 installed.

(1) Go to the path where the Android SDK installed, we can see:
/SDK/sources/android-25
/SDK/build-tools/25.0.3

(2) Open build.gradle(Module:app)
We can see:
android {
    compileSdkVersion 23    buildToolsVersion "23.0.3"

Change it to:
android {
    compileSdkVersion 25    buildToolsVersion "25.0.3"

(3) Now press "Try Again"

Tuesday, 27 June 2017

Change Particle Photon SSID

This is for building a IoT product with Particle Photon and you want the users to see the SSID of your company's name rather than Photon-WXYZ.

System.set(SYSTEM_CONFIG_SOFTAP_PREFIX, "YourCompanyName");
System.set(SYSTEM_CONFIG_SOFTAP_SUFFIX, "xxxx");

Note that it won't go back to default after being updated!!!

ref:
https://community.particle.io/t/change-photon-ssid/13737/26
https://community.particle.io/t/particle-app-cannot-setup-photon/14201

Thursday, 1 June 2017

stm32duino Getting Started with Blue Pill Board


The first consideration is that there are several ways to program an STM32F103C8T6 MCU:
  1. Just like all modern MCUs today, there is a way to program (and most of the time, debug) the chip at the lowest level using a JTAG probe. For the particular STM32 case, you can use an STLink, a JLink or a Black Magic Probe that all use the standard ARM SWD programming / debugging interface, readily available on  a separate 4-pin header on the Blue/Red Pill board short edge facing the micro USB connector
  2. Just like for the ATMega328, we can use an Arduino bootloader stored into Flash memory, here the equivalent is the STM32duino bootloader
  3. Serial (UART): unlike the ATMega328, the STM32 features an UART bootloader in ROM, i.e. available in an empty chip straight out of the production line
As we try to lower the prerequisite to program these Blue / Red Pill boards, the third method has the main advantage to not require any additional hardware, except for a standard serial UART to USB cable (as I suppose that your host PC does not provide a native RS232 UART DB9 or DB25 connector for a while…). This is the method that we will use here.
Here is the step-by-step procedure:

Requirements:

  • an STM32F103C8T6 “Blue Pill” or “Red Pill”module (ARM32 Cortex-M3, 72 Mhz, 64K flash, 20K SRAM, 33 I/O pins, 3.3V), available here for $1.52 (expect a 1 month delay to EEC with standard shipment method)
  • a soldering iron (in order to solder the large 2.54 mm pitch male headers provided with the board)
  • a Serial-to-USB cable, I used my old faithful FTDI TTL-232R-3V3 cable (datasheet here), but any know working cable or adapter will do
  • 4x Male / Female short “Dupont” wires

Step #1: Wiring

Wire the STM32 module and the Serial-to-USB cable as shown below:

STM32F103C8T6 FTDI TTL3.3V Cable
If you only have +3.3V power supply available, connect it to the “3.3” pin instead of “5V”.
Connect the cable to one of your free USB port on the PC. In order to find out which Linux device is associated with the cable, run the “dmesg” command in a terminal and search for the assigned device name near the last lines. You should see something like this:
$ dmesg
...
[19086.232386] ftdi_sio 3-4:1.0: FTDI USB Serial Device converter detected
[19086.232488] usb 3-4: Detected FT232RL
[19086.232858] usb 3-4: FTDI USB Serial Device converter now attached to ttyUSB0

Step #2: Setup the Arduino IDE

As the Arduino IDE packaged in the standard Linux distros is generally outdated and/or difficult to match with an official Arduino IDE release, it is better to scratch it if already installed, and download and install the latest Arduino IDE (I did use 1.6.12) directly from the Arduino official download page, either for Linux 32 bits or Linux 64 bits. This should create a folder “arduino-1.6.12” in your home directory.
If you want to add a menu item, icons and mime type for Arduino for the current user, run the “install.sh” script from this directory:
$ cd ~/arduino-1.6.12
$ ./install.sh
While you are at it, I suggest to check that your standard user has access to the Serial-to-USB cable. This can be done by running the command “id” and check for group “dialout”. If not found, you can add your user to the corresponding group:
$ id
uid=1000(your_user_here) gid=1000(your_user_here) groups=1000(your_user_here),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)
$ sudo adduser your_user_here dialout
Restart your session (this is required), and check that you now are part of the “dialout” group:
$ id
uid=1000(your_user_here) gid=1000(your_user_here) groups=1000(your_user_here),4(adm),20(dialout),24(cdrom),27(sudo),30(dip),46(plugdev),113(lpadmin),128(sambashare)

Step #3: Setup the Arduino Due (ARM Cortex M3) package

In order to install the required GNU cross toolchain for the ARM Cortex M3 core (the one used in the STM32F103C8T6), we must install support for the Arduino SAM Boards (Arduino Due) which contains the same core.
In the Arduino IDE “Tools” menu, select the “Board” item, and in the cascaded menu, select the “Boards Manager…” item:
board-manager-menuBoards MAnager Menu
This will display the Boards Manager dialog box, in which we need to select the “Arduino SAM Boards (32-bits ARM Cortex M3) by Arduino” and click on the corresponding “Install” button to install the latest version (mine is 1.6.9):
Boards Manager
You can then close the “Boards Manager” Dialog box.

Step #4: Setup the Arduino STM32 package

Despite an active community, the STM32 is not (yet) an officially-supported Arduino platform, thus it is not available from the standard “Board Manager” in the IDE menus. You will have to manually download it from “https://github.com/rogerclarkmelbourne/Arduino_STM32“, extract it and copy the folder ‘Arduino_STM32-master’ to your Arduino/hardware folder (“~/Arduino/hardware”), then rename the folder by removing the “-master” suffix from the folder name in order to obtain a new folder named “~/Arduino/hardware/Arduino_STM32” (this suffix is the repository branch added automatically by Github, we don’t need it).

Step #5: Select the Board parameters

Launch the Arduino IDE and in the “Tools” menu, select the correct values for “Board”, “Variant”, “Upload method” and “Port”:
  • Board: “Generic STM32F103C series”
  • Variant: “STM32F103C8 (20k RAM 64k Flash)” (we will speak about it later, in the “Bonus” section below)
  • Upload method: “Serial”
  • Port: “/dev/ttyUSB0” or the one found at end of step #1 above
Board Selection
Variant Selection
Upload method Selection
Port Selection

Step #6: Compile a Sketch

Copy & Paste the following code to replace the current sketch code:
#define pinLED PC13

void setup() {
 Serial.begin(9600);
 pinMode(pinLED, OUTPUT);
 Serial.println("START"); 
}

void loop() {
 digitalWrite(pinLED, HIGH);
 delay(1000);
 digitalWrite(pinLED, LOW);
 delay(1000);
 Serial.println("Hello World"); 
}
Nothing fancy, if you already know the Arduino language, we just set up the UART @ 9600 bps, set the PC13 GPIO as an output (this one has a onboard LED attached to it) and send the “START” welcome message on the UART. Then in a loop, we toggle the LED ON/OFF with a 50% duty cycle with a 2 s period, while at the same time sending “Hello World” greetings on the UART.
By clicking on the “Check” icon in the Arduino IDE, you can compile (“Verify”) the sketch, you should not get any error.

Step #7: Upload the Sketch to the Board

On the board, set the yellow jumper for BOOT0 to the “1” position (please refer to the board picture above) and press the RESET button.
In the Arduino IDE, click on the Arrow button to upload the sketch to the board.
If everything goes as expected, you should see the red LED blink @ 2 Hz and get our messages sent on the UART by opening the “Serial Monitor” @ 9600 bps in the Arduino IDE “Tools” menu.
Not working? If you get a message like “Failed to open port: /dev/ttyUSB0”, please check that the cable is indeed associated to the correct Linux device by running the “dmesg” command like above, and make sure that your current user has access to the UART by performing the step #2 above.
For my part, I had a rather unusual message while trying to upload the sketch, I obtained a:
Got NACK from device on command 0x43
 Can't initiate chip erase!
Googling around, I found this thread that helped me to identify the root cause of this problem: the chip is actually locked, which is rather unusual for a development board! However, the provided solution involved using a tool from ST that only works under Windows 🙁
I found a purely Linux solution by running one of the tools installed by the Arduino STM32 package above: using the “stm32flash” utility, you can read and write unprotect the chip, using the following commands:
$ cd ~/Arduino/hardware/Arduino_STM32/tools/linux/stm32flash
$ ./stm32flash -k /dev/ttyUSB0
stm32flash Arduino_STM32_0.9

http://github.com/rogerclarkmelbourne/arduino_stm32

Interface serial_posix: 57600 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0410 (Medium-density)
- RAM : 20KiB (512b reserved by bootloader)
- Flash : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
Read-UnProtecting flash
Done.

$ ./stm32flash -u /dev/ttyUSB0
stm32flash Arduino_STM32_0.9

http://github.com/rogerclarkmelbourne/arduino_stm32

Interface serial_posix: 57600 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0410 (Medium-density)
- RAM : 20KiB (512b reserved by bootloader)
- Flash : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
Write-unprotecting flash
Done.
Going back to the Arduino IDE, you should now be able to upload your sketch successfully to the board!

Bonus

If you were careful and checked either the “stm32flash” utility output above, or check into the Arduino IDE messages during the sketch upload, you may have notice this:
Using Parser : Raw BINARY
Interface serial_posix: 230400 8E1
Version : 0x22
Option 1 : 0x00
Option 2 : 0x00
Device ID : 0x0410 (Medium-density)
- RAM : 20KiB (512b reserved by bootloader)
- Flash : 128KiB (sector size: 4x1024)
- Option RAM : 16b
- System RAM : 2KiB
The “128 KiB” line above means that we are not on an STM32F103C8 MCU with only 64 kB of Flash, but more likely on an STM32F103CB MCU with 128 kB of Flash! This confirms the last point in my previous message that both variants share the same chip production mask and only differ by memory tests.



ref:
http://www.wifi4things.com/stm32f103c8t6-blue-pill-board-with-arduino-ide-on-linux/