MSP430 LaunchPad - Introduction to Low-powered Microcontrollers Development Board

Two months ago, I was working on an assignment which involved writing code for a simulator where the targeted machine was a microcontroller (also known as MCU or SoC). There were numerous issues with the project. One of such issue is using Interrupt Service Routine (ISR) to display the LED light at a specific interval and duration. There are numerious ways to solve this (I only found this after the submission), but we didn't managed to get it done right during that time. Furthermore, the main goal of the assignment was to let everyone experience a software engineering process through different team roles. Hence, correctness is not the priority here. Nevertheless, these unresolved issues piqued my interest on embedded programming and C/C++ programming languages. Also, my own self-exploration exposed me to different microcontroller kits available in the Maker community.

When come to microcontroller kits, Arduino Uno is the preferred choice right now due to its popularity, which again, is due to its beginner friendliness. But these comments by david-given (a), IgorPartola, wyager, Declanomous, david-given (b), and sotojuan at HN convinced me that TI MSP430 LaunchPad would be a better choice. Cheap (I've made a mistake purchasing the wrong LaunchPad kit, more on this later) enough and low-powered microcontroller for anyone to learn embedded programming and study hardware.

The next step is to purchase the microcontroller kits. Now, which MSP43X LaunchPad development kits? I bought the original LaunchPad kit, MSP-EXP430G2 for roughly MYR 50+ inclusive of tax from Element14. Make sure you add comment to the order form that the purchase is for personal use. Otherwise the sales person will have to reconfirm with you again. If you still remember I said I made a mistake picking the wrong kit. If I were to choose again, I will pick MSP-430FR4133 instead although it's more expensive. Two good reasons. First, the development board have an embedded Liquid Crystal Display (LCD). You don't have to buy an additional LCD display and going through the hassle of connecting it through the breadboard. Second, the MSP-EXP430G2 is not supported by GNU/Linux version of Code Composer Studio, the default Integrated Development Environment. These are not critical issues but just merely minor inconveniences. Videos below will show more details on the differences between two development boards.





The item arrived within 3 working days in a black box. The list of items inside the box is clearly explained by video below.


The full list of items are:
  • 1pc Quick Start Guide (slac432a)
  • 1pc MSP-EXP430G2 LaunchPad development board
  • 1pc MSP430G2553 microcontroller (with temperature measure demo app)
  • 1pc MSP430G2452 microcontroller
  • 1pc Mini-USB cable
  • 1pc 32kHz external crystal (I've no idea what this for even I've read slaa322c)
  • 2pc 10-pin PCB Connector (2 male/2 female)
  • 2pc LaunchPad sticker
One mildly interesting thing is the MSP430 part numbers. For example, the breakdown of the part number of microcontroller MSP430G2553 are:
  • MSP430 = CPU
  • MSP = Mixed Signal Processor
  • 430 = 430 MCU platform
  • G = Flash (value line) memory type
  • 2 = Generation 2 series where speed is up to 16 Mhz
  • 5 = Model within the generation
  • 5 = Memory configuration (RAM: 0.5K, ROM: 8K)
  • 3 = Variant of the devices
In the coming post, we will go through on setting the development environment in Ubuntu 16.10. Stay tuned.

This Week I Learned - 2017 Week 04

Last week post or the whole series.

What is like to reach the end of middle age and heading towards old age? Both good and bad. The good thing is after a perennial quest of what you want out of life, you finally have some ideas on what to pursuit. The bad? Eventually reality will catch up on you. Your body is getting weaker, both physically and mentally. Distractions or rather addiction to distractions to fend off. Resistances to stay focused due to our short attention spans (made worse by increase mobile phone usage). And never ending growing lists of to-do items to check off. Hence, before this coming Chinese New Year, I've decided to do some virtual "house cleaning", remove and clear off all those pending draft blog posts, my digital drives, and others. One of the post is Using ansible-lint with Git's pre-commit. However, nothing much can be written down this week as I've limited Net access.
#1 Why you shouldn't use delay function in embedded project (Arduino). You should use non-blocking function (also known as asynchronous function) or work along the hardware timer. Working with embedded project made me realized that I'm quite ignorant about programming with constraints and limited resources. Well, if you spent most of your web development time with PHP, I doubt anyone needs to worry much about optimization, just delegates to the frameworks.

#2 "Sometimes people fit snugly into a crack in the world that fits their shape, and sometimes no such crack exists." via HN. Very well written comment.

Using ansible-lint with Git's pre-commit

While browsing randomly, I stumbled upon the linter for Ansible called, you guess it, ansible-lint. However, another interesting utility came up called pre-commit, which is a framework to manage your Git's pre-commit hooks.

Installation is simple. Use `pip` to install it.
$ sudo -H pip install pre-commit

Create the config file, `.pre-commit-config.yaml` and add the content below.
- repo: https://github.com/willthames/ansible-lint.git
  sha: v3.3.3
  hooks:
    - id: ansible-lint
      files: \.(yaml|yml)$


Install the necessary tool.
$ pre-commit autoupdate
Updating https://github.com/willthames/ansible-lint.git...already up to date.

Install to the repository's `pre-commit` hook.
$ pre-commit install
pre-commit installed at /home/ang/src/dotfiles-ansible/.git/hooks/pre-commit
$ git commit
[INFO] Installing environment for https://github.com/willthames/ansible-lint.git.
[INFO] Once installed this environment will be reused.
[INFO] This may take a few minutes...

This Week I Learned - 2017 Week 03

Last week post or the whole series.

#1 Having trouble finishing any personal side projects as a developer? Rethink the purpose on why you want to start a side projects? Don't be so hard on yourself, nobody is keeping scores and gives a crap. Do it because you want to learn, experiment, and have some fun along the way. However, keep the coding streak going. Don't overwhelmed yourself, it's not a full time job. Also, do not fall into the trap of researching more than doing ala analysis paralysis, a common behaviour when you're starting a new venture or hobbies and get overly excited.

#2 ShewhartJuran, and Deming. The giants of Total Quality Management (TQM) and theory. Humphrey applied these theories to software quality and thus, leads to the development of Software Engineering Institute (SEI), Capability Maturity Model (CMM), and Capability Maturity Model Integration (CMMI). Can quality theory used for manufacturing translates well to software development? Depends. Highly for embedded system development but doubt so for normal web development.

On a related note, can we apply these quality models and theories to our personal productivity and optimize our habits? If we consistently miss our commitments to our self, isn't that a symptoms of process failure? Hence, if we fix the process and thus fix our habits, can we increase the probability of the success of our commitments? Also can we apply CMMI's capability level to personal financial planning? That's food for thought. Again, this is a possible interesting idea, daily SCRUM family meeting. I've tried something similar with limited success. Feedback from SO is that it's too formal and too rigid.

#3 Keep up with fast changes of development field? Don't do front-end stuff. Don't follow the flash-in-the-pan (hot) technologies. Learn durable skills. Study and take online classes. Be a generalist? Don't think so. While we're still thinking on what hottest technologies or practices to use, ask yourself "when is practice X useful?" instead of "is practice X a good idea?" Good example is the numerous Agile practices, there are so many and not every one is applicable for your organization. One little gem I learned, Agile is used primary to solve communication or requirements issues. And also, another example of those practices is the use of monorepo, even through you're using a distributed version control system.

#4 T(Type) Driven Design over T(Test) Driven Design. Interesting indeed. Instead of embracing T(Test)DD, the problem can be partially solved by switching to a statically type programming languages. For example, switching from Python/Perl/PHP to Golang?

#5 While watching the short documentary series Mind Field on the subject on isolation, little I realized that boredom is the result of your brain lacking stimulation and worse, and epidemic psychological behaviour will little scientific studies.  Moreover, researches have identified that there are different types of boredom. Interestingly, Plutchik's wheel of emotions grouped the emotion of boredom with disgust and loathing. No wonder philosopher, Søren Kierkegaard said "boredom is the root of all evil" (or is it creativity?). How to prevent our self from succumb to boredom? Thinking about boredom itself, why something bores you. Or because we've become habituated? Maybe practice gratitude is an alternative way to resolve that?

#6 The Old School Object Oriented Perl (OSOOP). Yes, the bless-way. As I mentioned many times, specifically, Perl is a text processing programming language with multiple ways of doing things. Hence, OO supports is to the bare essentials, there is not syntactic sugar what so ever. If you're creating a CPAN module, best to use lightweight Moo package, which was designed for that purpose.

This Week I Learned - 2017 Week 02

Last week post or the whole series.

#1 Continue with CMake again. Not sure why, I'm having lots of fun picking up C++ and enjoying the learning process, although it's quite frustrating when you're stuck with a problem. The weird thing is, is totally different experience with doing web development compare to system programming. Nevertheless, some knowledge gained from using CMake.

As I mentioned before, if you're picking up C++, start with Modern C++, or at least C11 standard. To enable this explicitly in CMake, put this line in your CMakeLists.txt file.
set(CMAKE_CXX_STANDARD 11)

To verify that the setting have been set to compiler.
$ make VERBOSE=1 | grep std
/usr/bin/c++     -std=gnu++11 -o CMakeFiles/stree.dir/stree.cc.o -c /home/ang/src/stree/stree.cc

If you move all your source files into a `src` folder, how do you configure CMake to include these files during compilation? Using GLOB directive.
file(GLOB SOURCES src/*.cpp)
add_executable(progname ${SOURCES})

#2 The C++ 'const' Declaration: Why & How. Definitely a good guide and explanation on understanding and using `const` keyword in C++. The gist of it is `const` prevents alternation but it sometimes caused confusion depends on the context of usages in variables, methods, return values, or function arguments.

#3 It just dawned on me that the sole reason Design Patterns (the book) was written is to collect all the workarounds to the limitation of C++ language itself. For example C++ does not support introspection or dynamic instantiation, hence, a list of creation patterns exists to handle the object creation. What this means is that C++ can't create objects during run-time (like from a string name), everything needs to be defined during compile-time. Or should I use Registry Pattern instead? While we're on the Design Patterns book, much have changed since the book was published. Most of the code example is still using the old C++ way, Modern C++ should make these code cleaner. Furthermore, Iterator was added to Standard Template Library (STL) and there is no need for anyone to implement it manually. I'm slowly beginning to understand why so many programming languages (Java, Rust, Go, and others) were created. Coding in C++ is not, what I would describe, a convenient experiences. I'm still in the early phase of learning, struggling is inevitable, especially you'll need to unlearn quite a number of things from dynamic languages.

#4 ISO standards for programming languages. Stumbled upon this while looking into different standards for software development. Was reading these intensively for the past two months. Seriously, after working for quite some times in development field, didn't realize that these standards exists in the first place. Notable standards are ISO/IEC 14882 (C++), ISO/IEC 9899 (C), ISO/IEC 23270 (C#), and ISO/IEC 30170 (Ruby). Surprise that one particular programming language is notable missing from the list, Java. Did I miss out anything? Nevertheless, standard compliance ensures portability in different operating systems.

Meanwhile, MIL-STD-498, a military standard for system development life-cycle. Superseded by IEEE STD 12207 or ISO/IEC 12007. Yet I'm still enduring the 498 while newer standards already long established. Why? Simply because the documentation is freely available and most academic world still following these syllabus. Standard feel rigid, especially when there are lots of paper works and the development style still following water fall model, very sequentially.

#5 The same thing every single damn year. Generic advice on solving, yet another first word problem, which is just sugarcoating for seeking fortune or fame. Working with younger people have some profound effect on me. How do I regain and reignite my interests on those old hobbies of mine again? You do learn a lot from them but at the same you also see a lot of repeated mistakes. Unfortunately, life moves forward. There are things you must give up as the time or stamina does not permits it anymore. Or it it?

#6 What do you want to learn in 2017 and this is how/now I plan to do it. Yes, another same damn question asked every year, especially the end of the previous year. So many things to try out, so little time. Develop a system and incorporated it into your daily life. Just start? But if you're not finishing, then you're just busy but not productive. Hence, to finish!

This Week I Learned - 2017 Week 01

Last week post or the whole series.

First week of the new year 2017. A busy and slow week. Busy because wrapping up most of the pending to-do list. Slow due to the progress of clearing up the list. When you're overwhelmed by numerous tasks at hand, the best way is still sleep over it. You can't do much with a tired mind, especially at my age now. You can accomplish lots of thing with a good sleep and fresh mind in the wee hours in the morning, roughly around 4:30 a.m.


Did something happened in July? Darker green means more steps and lighter green, the opposite. To the best of my recollection, I've misplaced my pedometer and resolved to use Google's Fit step counter, which is not entirely accurate. Typically offset by half against my pedometer.

#1 Remember about last week post on adding extra options so we compile the sample D source code? The extra options can be added to the `dmd.conf` configuration file. Following these steps to add them to `dmd.conf` file so that you won't need to type them again and again.

Copy the config file to your home directory
$ cp /etc/dmd.conf $HOME

Append the options, `-fPIC -defaultlib=libphobos2.so` to `DFLAGS` variable.
[Environment32]
DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import
-L-L/usr/lib/i386-linux-gnu -L--export-dynamic -fPIC -defaultlib=libphobos2.so

[Environment64]
DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import-L-L/usr/lib/x86_64-linux-gnu
-L--export-dynamic -fPIC -defaultlib=libphobos2.so

#2 Some new concepts in Modern C++. What is this Resource acquisition is initialization (RAII) I kept reading about? It's a programming idiom is about safe resource management and in C++, through an object's lifetime. Resources are initialized and acquired in object's constructor and released through object's destructor. In other words, resources are cleaned up when the scope of code have exit. See the diagram below for further understanding. Other programming languages implemented this idiom through a try/finally block instead through an object's destructor.


Second, is the concept of template. As C++ is a statically typed programming language (type checking is done at compile-time rather than run-time, see this good example), when you declare or initialize any variables, you need to specific a data type. What if you need to pass in a variable to a function which can accept different kinds of numeric data, an integer or and a floating-point number? This is when you will use template to specific a generic type.

Example of the function templates is shown below. You can pass in different kind of data types like integer, floating-number, or character.
#include <iostream>
using namespace std ;
//max returns the maximum of the two elements
template 
T max(T a, T b) {
    return a > b ? a : b ;
}

While we're at Modern C++, there is a list of resources which can get you started as well as the C++11/14/17 feature list. For learning C++ today, the minimum standard you should follow is C++11.

#3 Instead of listening to music while coding, why not listen to development related podcasts like Software Engineering Radio, The Changelog, The Bike Shed, or FLOSS weekly? All this better than some annoying pig squeal and snort noises.

#4 Accidentally messed up my commit dates while testing Git. Need to updates some old commits. However, you have to force push to override the history.
$ git filter-branch --env-filter \
'if [ $GIT_COMMIT = f1d2d2f924e986ac86fdf7b36c94bcdf32beec15 ]
then
    export GIT_AUTHOR_DATE="Sun Jan 7 01:02:03 2017 +0800"
    export GIT_COMMITTER_DATE="Sun Jan 7 01:02:03 2017 +0800"
fi'

$ git push -f origin master

And to remove all the uncommitted files from your project, you can use this Git command. However, be careful as it also remove my Vim's swap files (.swp). Did I forget to set a global path for all Vim's swap files?
$ git clean -d -f -x

#5 As I'm starting to build something in C++, creating and maintaining a Makefile is quite a hassle. Luckily we have CMake which helps let us bootstrap the Makefile fast. One thing I've learned when using CMake is compile and build your program in a specific folder. This will prevent all the temporary files generated by CMake from cluttering your root folder.
$ mkdir build && cd build
$ cmake ../
$ tree -L 1 .
.
├── CMakeCache.txt
├── CMakeFiles
├── cmake_install.cmake
└── Makefile


This Week I Learned - 2017 Week 00

Happy new year!

2017, the year of the fire rooster. It will be interesting to see what this year will unfold itself. The plan will still remain the same every year. The usual stay alive and healthy, more reading, learning, writing, coding, and producing as well as build new habits. In other words, do, try, make more mistakes. As they said, "One who makes no mistakes make nothing at all". Be constant aware of you thoughts and actions. Live in the moments. There is a Zen saying, "When hungry, eat. When tired, sleep". Nevertheless, do plan ahead and learn from your past. In short, continue what you planned last year and adjust accordingly.

Learning reflection for 2016. I wrote 58 posts last year. Still a firm believer of quantity over quality. Writing is like exercising, you need to practice persistently to get better. However, blindly deliberate practice without any targets may be wasteful and leads to no where. Still something to ponder about. What I learned last year? Mostly Perl and Git as well as others stuff in between. When there are always rooms for improvement, all these exposure to new old stuff (Perl is damn old anyway) did satisfy my intellectual curiosity. Exposure to C++ for the last two months was interesting. It really piqued my interesting on static type programming languages.

As usual, here we go, something new I've learned this week.

#1 According to ISO 8601, the definition of week 01 if 1st Jan falls on Monday till Thursday. If it falls on Friday till Sunday, it's still the last week of previous year. However, there is no week 00. Nevertheless, I still prefer to call it week 00, as 1st Jan signifies a fresh start.

#2 Information overload? Thinking of applying digital minimalism? (via HN). FOMO is probably is main cause to our digital clutter. Unless these digital tools can bring values to your offline life, ditch them. Likewise, I still have a long way to go with my minimalist lifestyle, not everything fit into one bag yet. There are literally thousands of things I wish to ditch away. Still, one thing at a time.

#3 The D programming language. If you're doing doing development for quite some time and follow the programming language trend, you probably heard about this programming language. I've stumbled upon this again while doing some C++ coding. Developed as an alternative or replacement to C++, it still failed, after all these years, to gain any traction (based on my reading and impression of Thoughtworks' Technology Radar, HN, and Dlang subreddit). It was one year ago since Andrei Alexandrescu quit his secure job at Facebook (get a big pay cut but financially still ok) to push D forward on full time basis, does it really helps?

I played and read through the documentation, on the surface, it's looks quite nice, Python-like syntax with C/C++ speed but isn't Golang or Nim existed for the same reason? Nevertheless, the documentation was fun to read. Love the Contract Programming, especially the Invariants.  The wiki post on Components Programming Using D (ala functional programming) was one of the most interesting read on programming language this new year.

While we at it, some adjustments are needed to get Dlang to work in Ubuntu 16.10.

First, setup the APT repository for D.
$ sudo wget http://master.dl.sourceforge.net/project/d-apt/files/d-apt.list \
-O /etc/apt/sources.list.d/d-apt.list

$ sudo apt-get update && sudo apt-get -y --allow-unauthenticated install \
--reinstall d-apt-keyring && $ sudo apt-get update

$ sudo apt install dmd

Next, generate the sample hello word project.
$ dub init hello
$ cd hello/source

However, you will encounter error below during compilation.
$ dmd app.d
/usr/bin/ld: app.o: relocation R_X86_64_32 against symbol `__dmd_personality_v0' 
can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libphobos2.a(exception_249_55a.o): 
relocation R_X86_64_32 against symbol `__dmd_personality_v0' can not 
be used when making a shared object; recompile with -fPIC

Add these additional options to get it to run.
$ dmd app.d -fPIC -defaultlib=libphobos2.so
./app
Edit source/app.d to start your project.

Compile and run on-the-fly.
$ rdmd -defaultlib=libphobos2.so -fPIC app.d
Edit source/app.d to start your project.