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