STM32 Adventures – output clock to pin

When working with STM32 using registries only it is important that you have REFERENCE MANUAL and DATASHEET available. We will use them a lot πŸ™‚


In today’s post we will be outputting SYSCLK to external pin of our STM32F407 discoveryΒ board. As mentioned we dive into our RM and find part responsible for RCC configurations. From there it is easy to see which bits we need to output SYSCLK to our pin.



As you can see on the above reset value shows that desired SYSCLK and default no prescaler will be selected. Then in this instance let’s find out on which pin we have to enable our alternate function.

To do this we need to find it in DS.



Great – it is default system function! So the only thing we should do is just set this PIN as output

int main(void)


	 *  Enable SYSCLK output to PC9


let’s see it if works by looking into our logic level analyzer



We will defenitely be able to use this functionality in interacting with external hardware – but thats for future posts


Gitlab – custom pre-receive hook

As many of you I’m also using Gitlab to manage some of my projects. What I have recently been doing – was discovering how great it is to enable pipeline within your projects.

That have enabled me to install several runners and configure different stages of deployments for my repositories. While this all sounds cool it relies on single file called .gitlab-ci.yml

This would not be a big problem if not the fact that some of repositories have other developers working on it and potentially changing that file could present a security risk for my services/servers. So to overcome this I have come up with pre-receive hook that is now sort of ACL for my file unless secret commit message is included.


In repository create folder called *custom_hooks* i.e.


Then create file called *pre-receive* and apply permissions to it

chmod +x pre-receive
chown git.git pre-receive

AfterwardsΒ you can just select the language you are interested in programming your custom git hook – below is my Ruby attempt.

What it does it check if thr push is not by any chance unathorised change to our gitlab-ci.yml file.

You would be able to change this file if your commit message will be done with specific secret. But I leave this forΒ ppl to adapt for their needs.


#!/usr/bin/env ruby

our_secret = "aaaa"

params = gets
oldref = params.split()[0]
newref = params.split()[1]
refname = params.split()[2]

changed_files = `git diff --no-commit-id --name-only #{oldref}..#{newref}`

            if changed_files.include? '.gitlab-ci.yml' then
            commit_messages = `git log --pretty=%s #{newref} | head -3`.split("\n")

             unless commit_messages.include? our_secret
              puts "================================================================= "
              puts "β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—     β–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•— β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—  "
              puts "β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•β•β•     β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β•β•β•β•šβ•β•β–ˆβ–ˆβ•”β•β•β•β–ˆβ–ˆβ•”β•β•β•β•β•β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•— "
              puts "β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—       β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—  β–ˆβ–ˆβ•‘        β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—  β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘ "
              puts "β–ˆβ–ˆβ•”β•β•β–ˆβ–ˆβ•—β–ˆβ–ˆβ•”β•β•β•  β–ˆβ–ˆ   β–ˆβ–ˆβ•‘β–ˆβ–ˆβ•”β•β•β•  β–ˆβ–ˆβ•‘        β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ•”β•β•β•  β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘ "
              puts "β–ˆβ–ˆβ•‘  β–ˆβ–ˆβ•‘β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β•β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β•šβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—   β–ˆβ–ˆβ•‘   β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•—β–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ–ˆβ•”β• "
              puts "β•šβ•β•  β•šβ•β•β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β• β•šβ•β•β•β•β•β•β• β•šβ•β•β•β•β•β•   β•šβ•β•   β•šβ•β•β•β•β•β•β•β•šβ•β•β•β•β•β•  "
              puts "================================================================= "
              puts ""
              puts " oldref is #{oldref}"
              puts " newref is #{newref}"
              puts " refname is #{refname}"
              puts " "
              puts " We are very very sorry but your change seems to violate our policies! "
              puts " Please check with your nearest Guru! :) ERROR_ID: 001        "
              puts "================================================================= "

              exit 1

            end # if changed_files.include?
exit 0


I hope this will get you going and leave comments if you make some interesting changes to it πŸ™‚


Flashing ESP8266 ESP-03 with NodeMCU

Recently I have been working withΒ ESP8266 ESP-03 and decided to flash it with NodeMCU. Looking at how to do this ( here ) it seemed to be pretty straight. forward task.

However when I started to try and flash the module I have not received any results.Β Most of the cases module became unresponsive. After many hours ( really ) of looking for some guideline where the problem can be I have came across post which referenced to write the default data into 2 memory addresses of the module.

So I have done so with the following command and that has helped – now got my nodemcu running as it should be!


esptool.py --port /dev/tty.usbserial-A7HFE6HK --baud 115200 write_flash -fm qio -fs 4m 0x00000 bin/nodemcu_integer_master_20161025-0533.bin 0x7C000 ~/Downloads/esp8266_nonos_sdk_v2.0.0_16_08_10/ESP8266_NONOS_SDK/bin/esp_init_data_default.bin 0x3fc000 ~/Downloads/esp8266_nonos_sdk_v2.0.0_16_08_10/ESP8266_NONOS_SDK/bin/esp_init_data_default.bin

Hope this will help you if you have been looking for some tips!



STM32 – minimal libraries project

Hey! So you most likely here because you would like to challenge yourself and try to program 32bit microcontrollers using minimal amount of libraries ( HAL/CMSIS ). On the internet you will find multiple discussions which approach to use – I will not be touching that – I guess everyone has his own way πŸ˜‰

We will starting by shortly discussing environment used and then we will move on to configuring our project. I will be using for this STM324F407 discovery board – but with the right changes you will be able to adjust it to your own needs.

IΒ will cover in this post:

  • Creating new project in Eclipse
  • Setting all libraries
  • Setting project & compilator settings

Before you start you should have


This project will use several files i.e. startup file / linker script and several header files.


Setting upΒ project

Go ahead and create new ARM project. Here we will not be using any ‘Hello world’ or prepared examples – we start blank!



Here just go through the wizard and finish.Β When done we will move on getting extra files we need for our project.


Create folders inside the project folder so the structure will look as follow


Download firmware using STM32Cube

Now we need to start our STM32Cube to get all files we will need to move on with our project.Β Go to help -> install new libraries and then select the firmware for your cortex. For me it will be for STM32F4



Get following files into the project directories:

β”œβ”€β”€ cmsis
β”‚Β Β  β”œβ”€β”€ include
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ cmsis_gcc.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ core_cm0plus.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ core_cm4.h ** THIS IS BASED ON YOUR CORTEX!! If you have different take different lib file !! **
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ core_cmFunc.h
β”‚Β Β  β”‚Β Β  β”œβ”€β”€ core_cmInstr.h
β”‚Β Β  β”‚Β Β  └── core_cmSimd.h
β”‚Β Β  └── src
β”œβ”€β”€ include
β”œβ”€β”€ src
β”‚Β Β  └── main.c ( this is file created by you )
└── system
    β”œβ”€β”€ include
    β”‚Β Β  β”œβ”€β”€ stm32f407xx.h
    β”‚Β Β  β”œβ”€β”€ stm32f4xx.h
    β”‚Β Β  └── system_stm32f4xx.h
    └── src
        β”œβ”€β”€ startup_stm32f407xx.s
        └── system_stm32f4xx.c


Modify project settings

Now since we have all the files we need your project should look similar to the following



We will now go and modify several project settings. Open project preferences and makeΒ required modifications

  • Change extension used by assembler source file to be *.sΒ ( it’s project specific setting )stm32_minimallibs_06
  • Change to appropriate cortex family modelstm32_minimallibs_07
  • Modify preprocessor definitions to match your processor ( this settings in image below are for STM32F407VGx )stm32_minimallibs_08
  • Modify includes in Assembler and Compiler so it contains “${ProjDirPath}/cmsis/include” and “${ProjDirPath}/system/include
  • Under Linker -> General have the followingΒ ${ProjDirPath}/STM32F407VGTx_FLASH.ldΒ ( if you are using different MCU then you will need to change this )
  • Modify paths which include source codestm32_minimallibs_09


Blink the diode

So now you are in position to just blink the diode using the following code in main.c

 * main.c
 *  Created on: 12 Nov 2016
 *      Author: rafpe
#include "stm32f4xx.h"
#include "stm32f407xx.h"

int main(void)
	volatile uint32_t delay;

	RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; // enable the clock to GPIOD


	GPIOD->MODER = (1 << 26); // set pin 13 to be general purpose output

	while (1)
		for (delay = 1000000; delay; delay--)
			GPIOD->ODR ^= (1 << 13);


Compile and you are ready to flash your device.




GivenΒ all setup here we are HSI is used as system clock source. We will definitely come back to this as we will need more than i.e. 16 Mhz! Happy coding!






Using STM32CubeMX on MacOS

When it comes toΒ using multiple tools to program/develop it can be sometimes challenging to have them running in single OS. Might be that you would end up having virtual machine running some of your tools – which of course – does not make it easier at all!

One of tools that I really have need of using ( when it comes to development of 32bit microcontrollers ) is STM32CubeMX. If you have not clue what that tool does – in short – it is GUI which allows for quick configuration of microcontroller and its peripheral. It is nice at the end to generate you the code in selected IDE project format ( we will come back to alternate use of this in one of next posts )

Download the software

We will start off with downloading the software. Go STM32CubeMx download pageΒ Β and scroll down to the bottom. You will find the download option for the software there



Page requires you to create account – but that is part which I leave completely up to you πŸ˜‰


Install software

Once downloaded on our Mac it would be great to just go ahead and install. If you open folder youΒ will see the following files.



For obvious reasons you will start the ‘application’ file which in return will open the installer



The installation is like ‘camera’ – just next => next=> done! The only thing you need to do now is to open it and use it features!


Use the app



I hope this will get you and your toolkit on the right track! Enjoy coding!





Setup for AVR development on MacOS


So today we look into something which I really was looking for recently. In nutshell it is setup of required components which are necessary to program AVRs on our MacOS.

Here I assume you for sure already have HomeBrew installed as it will be our main point of software installation.


Add new tap

brew tap osx-cross/avr

Install avr-gcc

brew install avr-libc

This one will installΒ avr-binutils and avr-gcc. TheΒ avr-gccΒ installation takes time as it is compiling … so make ur self your favourite drink here πŸ˜‰


Install avrdude

brew install avrdude --with-usb


Avrdude error when loading MCU in Eclipse

If you have error when using Eclipse and avrdude it seems that the AVR plugin has not been updated for a while. Although this does not affect programming can be easily solved. My friend sanderv32Β has created a really nice AVRdude wrapper which solves this problem once and for all!

You can find his repo here =>Β https://github.com/sanderv32/avrdude-av


Happy coding!


GPG secured passwords in git using pass

It might happen that for your working environment you need to store passwords securely. Nowadays many people is using ‘cloud’ solutions – but as you do well know cloud is nothing else than ‘someone’s else computer’ πŸ˜‰ . Having that said that limits options you have available. As this is point of preference I will try not to get into discussion of ‘the best solution’ but will just show you what I have been using and what I really liked a lot.

Solution is called pass and is available on the websiteΒ https://www.passwordstore.org/

So let’s go ahead and install this on our machine – installation steps are nicely outlined on the product page so here I will just focus on CentOs

sudo yum install pass

As you might have seen from documentation you will need your GPG key(s) – for this demo I have created dummy one

[[email protected] ~]# gpg --list-keys
pub   2048R/5CBDFF98 2016-10-30
uid                  RafPe <[email protected]>
sub   2048R/B3B34661 2016-10-30

[[email protected] ~]#


Let’s go ahead and initialise our pass with GPG key I have created.

[[email protected] ~]# pass init 5CBDFF98
mkdir: created directory β€˜/root/.password-store/’
Password store initialized for 5CBDFF98


Once the above is completed we can start adding passwords to our safe – simply by issuing

[[email protected] ~]# pass insert Business/serviceA/systemA
mkdir: created directory β€˜/root/.password-store/Business’
mkdir: created directory β€˜/root/.password-store/Business/serviceA’


Listing password then becomes really intuitive

[[email protected] ~]# pass ls
Password Store
└── Business
    └── serviceA
        β”œβ”€β”€ systemA
        └── systemB


To recover password we will just call the tree value

[[email protected] ~]# pass Business/serviceA/systemA

NowΒ we will be asked for our GPG passphrase key in order to retrieve it.



Here we would now would like to make our password safe more reliable by using GIT to store our secrets. I’m using Gogs (GoGitAsService) which is a lightweight version available.

By issuing the following commmands we get our pass to store secrets in git :


# Initialize 
[[email protected] ~]# pass git init

Add remote repository ( here you would need to adjust your remote repository to match – I’m using local docker instance )

[[email protected] ~]# pass git remote add origin

Commit all changes

[[email protected] ~]# pass git push -u --all
Username for '': rafpe
Password for 'http://[email protected]:10080':
Counting objects: 7, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 1.05 KiB | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
[[email protected] ~]#


Once thats done we can take a peak on our repo which now has encrypted passwords for our specified items.




From now on whenever I would be making changes I can just push them nicely to GIT and I have everything under control! Documentation has a lot to offer so be sure to check it – more detailedΒ https://git.zx2c4.com/password-store/about/


I personally think the product is good – especially in environments where you should not store passwords in ‘clouds’ due to security constraints which may apply.


Scaffolding application templates using Yeoman.io

When you quickly need to create a new application starting every time from scratch can be a pain in the back side πŸ™‚ But have no fear – there is a really nice service calledΒ YeomanΒ available here ( Β http://yeoman.io/ ).

Based on community best practices it acts as “generator” of all what you need to start your new app. Since I’m using MacBook nowadays – we will go through installation on that platform.

brew install node


Once done we can install yeoman and for demo purposes generator for hubot

npm install -g yo generator-hubot


And there it is πŸ˜‰ from this point onwards we just just create our apps – easily and whats most import on demand! Let’s start with something simple like Hubot

mkdir myhubot
cd myhubot
yo hubot





And off it goes πŸ˜‰ Now the possibilities are broader and for example starting with c# app or angular is as easy as discovering them here ( generators ) . Try it πŸ™‚


Installing KVM on CentOS 7 with Chef

Some time ago I have made decision that I will be running my own hardware to get learning curve on different level. This has already happened and in one of my previous posts you could have read that I got the server assembled and it is running. Of course if you are interested in more details look below.

Previously I have been using extensively Ansible but since some time I’m learning Chef. So taking that into account this is one of my first cookbooks. For this reason it is extremely simple. It is just installing base packages and couple of extra ones which you may find useful.

If you run the cookbook the output would be looking similar to this one :



This is really simple run-book. I will be trying to extend it along side of server development. If you will have some comments for improvements feel free to make a pull requests . Complete repository is availableΒ in Github repo



Vagrant – create multiple VMs with multiple network interfaces

Today I will just share with you piece of code that I quite often used when I was working with quick spin of machines using Vagrant. It will create 2 VMs for you based on your requirements ( can be more if you modify the code πŸ™‚ )


# based on http://stackoverflow.com/a/33789603/2476347
    :hostname => "uno",
    :ip => "",
    :box => "lamudi/centos-7.0",
    :ram => 1024,
    :cpu => 2
    :hostname => "duo",
    :ip => "",
    :box => "lamudi/centos-7.0",
    :ram => 1024,
    :cpu => 2

Vagrant.configure(2) do |config|
    servers.each do |machine|
        config.vm.define machine[:hostname] do |node|
            node.vm.box = machine[:box]
            node.vm.hostname = machine[:hostname]
            node.vm.network "private_network", ip: machine[:ip]
            node.vm.network "public_network", type: "dhcp", bridge: "en0: Wi-Fi (AirPort)"
            node.vm.provider "virtualbox" do |vb|
                vb.customize ["modifyvm", :id, "--memory", machine[:ram]]


The code as usual has a lot of potential for upgrade πŸ™‚ if you would have any suggestions please leave comments!