ARM:0016 STM32CubeMX – problems with RTC

This is a very short message about proper initialization of RTC (real time clock). There is one trick designed in HAL and in several internet resources it is wrongly described.
Proper RTC init:
Find “MX_RTC_Init(void)“, scroll to “USER CODE BEGIN Check_RTC_BKUP“. Now read one of the available user variables from “backup” RAM and open new “IF“:

/* USER CODE BEGIN Check_RTC_BKUP */
if(HAL_RTCEx_BKUPRead(&hrtc,RTC_BKP_DR1)!= 0x5051)
{
/* USER CODE END Check_RTC_BKUP */

Number “0×5051″ is random value, it can be any. Maybe only zero or max value is not good option, as it may be get from uninitialized ram.
Now scroll lots of code and add…

....
/* USER CODE BEGIN RTC_Init 2 */
    } // Kitame user code virsuje yra IF komanda. Čia ji užsidaro. (closing IF from beginning)
    else
    {
// LAIKAS BUVO ISSAUGOTAS, NES USER REGISTRAS TURI MAGIC skaiciu 0x5051 (battery backup is valid- we received "magic" number)
// Čia galima ką nors padaryti iš tos laimės arba pagalvoti apie kalendoriaus atstatymą. Sako kad jis nelabai
    }

//Čia įrašom tą MAGIC skaičių. Jis po pilno reseto ir RTC mirties turi neišlikti. (Write "magic" number to RAM)
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, 0x5051);//Write data to the specified backing area register
/* USER CODE END RTC_Init 2 */

All this stuff is made to force users to validate RTC status. In some internet examples there was even a recommendation to exit init code before full initialization. Meanwhile reading “magic” value ensures that the programmer checks RTC status and initializes it if it fails.

P.S. To start RTC second interrupt just execute this macro:

__HAL_RTC_SECOND_ENABLE_IT(&hrtc,RTC_IT_SEC);

Posted in Anything, MCU | Tagged , , | Leave a comment

ARM:0015 STM32CubeMX screen libraries, part 2

Continued from Part 1.
There are lots of different LCD or OLED screens in the market. Typically, it is possible to define 3 types of screens according to interface type: I2C, SPI and parallel data bus. There are also analog and LVDS (HDMI) types too, but they are typically not used with weak MCU. Parallel types use many wires for the interface, but are much faster. I2C type is slow, but very useful for a small screen as it can be on the same bus as some sensors. Many I2C type screens do not have a RESET pin, so the init sequence may be very slow. SPI interface is fast (not very fast when refreshing the whole screen) and uses CS pin for device select and D/C pin for data or command selection. Some screens from China do not have CS pin.
The logic of graphic chips is the same, but init sequence and commands may be different. Also, there are some options about the “glass” type, so there is no universal screen driver. Here is some screen collection connected to the same STM32F103 chip and using all the screens simultaneously.
LCD SPI I2C color screens gcc library for STM32F103 ARM bluepill
From left to right: ILI9341 (320 x 240), ST7789W (240 x 240), oldest ST7735R (160 x 128, other configurations optional), SSD1306 (128 x 64), SSD1306 (128 x 32, I2C).

Small screens do not have a full graphics library implemented (who needs to draw a circle on a 32 lines screen?). The font in the library has an error (lost small “A”).
Typically library can do:

  • Draw arbitrary pixel
  • Draw color data to rectangle: fill with color or image data (sprite)
  • Draw horizontal or vertical lines
  • Draw rectangle
  • Print text using fixed size font
  • Print text using proportional font with lame sizing (vertical or horizontal)
  • Print large proportional text (limited font data)
  • Do some hardware tricks: scroll, inverse, change intensity, flip image…

Many libraries use source code from the internet, but some errors are fixed and I added optimization for speed and size. Also, I introduced my errors too.

For example there is some simplification in source code. First line is from popular “programming language”, second is mine:

//was:
 for (pixels = 0; pixels < x1 - x0 ; pixels++) { ili9341_send_word(color); }
//now:
 for(x0=x0;x0<x1;x0++){ILI9341_send_word(color);}

 


We save one variable and useless calculations.

Other example. It is more important in speed optimization (for the screens it is very important):

//was, original:
 for(i=0;i < (w * h);i++)
 {
 c1 = *buffer++;
 c2 = *buffer++;
 ili9341_send_byte(c1);
 ili9341_send_byte(c2);
 }

//new:
 ILI9341_write_buffer(buffer, w*h*2);

 


Instead of sending thousand separated bytes, why not to use HAL library to send whole buffer using DMA or interrupt driven routine. Even sending buffer in blocking mode is much faster, than sending thousand of separate calls to procedure for just one byte of data.

There was problem in demo program- the function names for different screens used same variables and macros. In the real word program it is not the problem, because nobody use several different screens in the same project.

All source code for libraries, demo program for STM32F103 (bluepill), compiled binaries are included in archive.
Not all tricks are implemented in libraries- especially hardware scrolling and specific controller chip features.

Posted in Anything, MCU | Tagged , , | Leave a comment

ARM:0014 STM32CubeMX libraries for sensors and screens

In fact, any new program written for MCU is only copy-paste from older projects. Not real copy paste, but using the same libraries and subroutines. For some upcoming bigger project, I decided to use STM32 microcontroller, this means, I need to port several AVR libraries from older projects to the new ARM MCU. Due to historical reasons, older libraries were a bit chaotic. New versions are a bit improved, unified and much easier to use.

STM32CubeMX bluepill gcc libraries for sensors and various LCD screens
Porting somehow was very fast. Most of the problems were with the I2C bus- breadboard I used had some issues and I added additional termination resistors to keep everything stable. Maybe bigger capacitance of the breadboard or bad connections inside.

All sensors and screens are from China, only FM75 is from an old plasma TV set and MAX44007 donated by a friend. I had no idea what to create next and also I was a bit bored. And one day, I received a letter from them, with an offer to create some any project I wish and they will give some of the needed components. After this, I made a small poll about projects people need for now. I add some engineering touch to poll results and in a few posts I will start step by step articles about one smart thing.

Sensors and screens used in this test are:

  • LM75 temperature sensor, I2C (FM75 is advanced version, compatible with LM75)
  • MAX44007, I2C, digital calibrated light sensor.
  • PCF8574, I2C, 8bit I/O extender.
  • Old school txt LCD ekraniukas on PCF8574 extender. Free 5V – 3V level shifter.
  • SSD1306 OLED screen on I2C bus.
  • Analog input- not external module, but part of STM MCU chip.
  • RTC (real time clock)- also not external, but build in STM MCU.
  • BMP180 Atmospheric pressure sensor with temperature detection option.

Somehow I forgot to add DHT11 sensor code in this post. Sorry. I think in some future post it will be.
The OLED screen is using Commodore 64 font and there is error in font data. Will fix in future.

All libraries are compatible with GNU gcc and STM32CubeMX programs. All files compile without single warning.

Do download source code: OLED screen, sensors libraries compatible with STM32CubeMX and gcc. Compiled versions for STM32F103C8 (Bluepill PCB module).

Some data from sensors are shown on OLED and LCD screens and all data is sent using the USB virtual COM port for debug purposes.

Continued on Part 2.

Posted in Advert, Anything, MCU | Tagged , , | Leave a comment

ARM:0013 STM32CubeMX – USB HID keyboard

Now we will build a USB keyboard. This project is based on STM32CubeMX canned example, HID mouse (see previous article). But we will change mouse to keyboard. Both devices are from the same HID class. First we need to change the device descriptor. Descriptors are quite complex bunch of numbers with strict structure. For this example we will use another program and generate generic keyboard descriptor.

HID descriptor creator
This is a screenshot of a HID descriptor tool from www.usb.org.
Continue reading

Posted in Anything, MCU | Tagged , , , | Leave a comment

ARM:0012 STM32CubeMX – USB HID mouse

Next canned project for STM32F103 MCU using the cube- USB HID mouse. HID (human interface device) means it is the standard device compatible with all computers with USB ports. STM32CubeMX has all device descriptors and init procedures already built in.
Select USB device tab and set mode to “Human Interface device class”:

STM32F103 USB mouse hid
Current version of cube software is not flexible- there are no options to change more settings and select HID devices. Also it is confusing- there is no mention that it is a mouse project.
Set all other options like in previous examples, generate code and copy files from previous PS2 project. Why? We need some input and we are preparing for the next project. If you think it is complicated, just download current project files at the bottom of this post.

Continue reading

Posted in Anything, MCU | Tagged , , , | Leave a comment

ARM:0011 STM32CubeMX – Pin interrupts

Next tutorial- interrupts, keyboard (PS2 input) and USB COM port.

I have a small keyboard in my stash. It was from some flop device made by Amstrad. This keyboard has a 4 pin connector and I had hoped that it would be the USB. But after examining, I found it is PS2 only. Good stuff for the tutorial.

Amstrad PS2 keyboard
First we will do the PS2 interface and simply connect this keyboard to a virtual COM port via USB. It is possible to build a direct PS2-USB HID keyboard connection, but this is only a tutorial and we shall do it step by step.
Continue reading

Posted in Anything, MCU | Tagged , , , | Leave a comment

ARM:0009a – bug in CubeMX makefile

First you generate code, then you regenerate code, do make and…

> "make.exe" all
make.exe all -C ..
make.exe[1]: Entering directory 'F:/MyDoks/elektronika/ARM/daiktas-cube/SINGLE_UART'
Makefile:118: *** missing separator.  Stop.
make.exe[1]: Leaving directory 'F:/MyDoks/elektronika/ARM/daiktas-cube/SINGLE_UART'
make.exe: *** [makefile:3: all] Error 2

> Process Exit Code: 2
> Time Taken: 00:00

The bug is in generated main makefile:

# C includes
C_INCLUDES =  \
-IInc \
-IDrivers/STM32F1xx_HAL_Driver/Inc \
-IDrivers/STM32F1xx_HAL_Driver/Inc/Legacy \
-IDrivers/CMSIS/Device/ST/STM32F1xx/Include \
-IDrivers/CMSIS/Include
-IDrivers/CMSIS/Include

 


There is a repeated line with “Include”. Just delete that line. Very interesting, but after next code generation this bug will not appear!

Posted in Anything, MCU | Tagged , , | 1 Comment

ARM:0010 STM32CubeMX- USB 2 COM dongle

This article is also an introduction, but this time a bit more practical- USB to serial port (COM) dongle. This tutorial will show how to create USB devices from “canned” software using only STM32CubeMX. For this example I used some scrap board with an STM32F102 MCU. All written in this article can be implemented in bluepill or any other STM32 chip with USB support.
I used this board because it has RS485/422 drivers on board and I was testing some concepts for my next bigger project.
STM32F102/103 USB device CDC com dongle
I have three of these boards. For testing, I closed the RS422 loop to itself. Now I have hardware echo and I can test software on heavy loads- transmission and receiving at same time.

Lets begin…
Continue reading

Posted in Anything, MCU | Tagged , , , | Leave a comment

ARM:0009 STM32CubeMX tool from STM MCUs

There will be several posts about using the cheapest MCU board from China. So called Bluepill- PCB containing STM32F103C8T6 microcontroller, USB connector and some small elements. For a few bucks/euros we have upto 72MHz CPU, 64Kb of ROM and up to 20kb of RAM. Up to 100 millions instructions on one chip.
As all ARM chips it needs quite complicated initialization code and peripheral configuration. And there are tons of peripherals on the same pin. Manufacturer of the chip, STMicroelectronics, decided to make a graphical tool for chip configuration and init libraries. Also, they included some libraries for peripherals from simple GPIO to complex USB or fat file system. And all the stuff is compatible with generic GNU C (gcc) available free from ARM holdings and other sources. Same compiler is used in the well known learning system arduino, but without the use of quasi language extension. All software can be portable to any other platform and system. ’cause it is ISO/ANSI standard!
This tool is called STM32CubeMX (stand alone tool). First versions were extremely bad and full of bugs. After several years it became usable, but still there are errors, bugs and undocumented features.

This is an intro article, tutorial about how to build your first project with lots of pictures. Sorry, this blog version is not updated to the new width of the pictures. Will be fixed soon.

STM32cubemx tutorial starting first project
Continue reading

Posted in Anything, MCU | Tagged , , | Leave a comment

Collecting horses in Fallout 4

Due to a virus outbreak I was burning my time:

horses in fallout 4
It is some type of fetish.

Posted in Anything, Personal | Tagged , | Leave a comment