Sunday, March 31, 2024

Simplicity Studio and Keil 8051 compiler issues and tutorial

Working with Simplicity Studio and Keil compiler can be frustrating sometimes. Most of the issues encountered were difficult to solve because the error messages given by the Keil compiler were very confusing. Thus, I will post here the most common issues that I've came across in the hope it will help others and perhaps me in the future.



Keil Error Messages

A list of Keil error codes and their description can be found here: However, below are scenarios in which the error message and the cause are not related. For example when actually missing a ';' the error message is: syntax error near 'token', expected ';' but when something is not defined because an include file is missing, the error message is: missing ';' before.

Friday, March 15, 2024

Delay functions for Silicon Labs microcontrollers

Sometimes you need to delay the program execution, for example when waiting for another device to startup or respond and the software cannot continue. An interrupt driven delay is better in most cases but not in all cases. For this purpose I made two functions one for milliseconds and one for microseconds, that uses NOPs to delay the program execution for a certain amount of time. The milliseconds delay is quite accurate, the microseconds one, not so much.

For this to work, the SYSCLK must be defined first. It is recommended to be defined in the project settings. You can find a short tutorial here on how to add a global define in Simplicity Studio:

Tested on:

- C8051F330

For other microcontrollers the following SFR declarations include file should be replaced accordingly:

#include <SI_C8051F330_Register_Enums.h>

Library usage


Milliseconds delay

_delay_ms(uint32_t ms)

Microseconds delay

_delay_us(uint32_t us)

I couldn't figure a way to make millis delay more accurate since the code used to create the delay, adds delay and cannot be easily subtracted because it depends of the delay time. I think it should be implemented using assembly but i don't know the language. If you need just a certain delay time, you could tweak the function for that particular delay, using a logic analyzer.


v1.0 delay Contains delay.h and delay.c

UART library for Silicon Labs microcontrollers using interrupts

This UART library is made for Silicon Labs microcontrollers that can be used for serial communications.

UART is a type of serial interface, as opposed to a parallel interface. A parallel interface can work at higher speeds but the disadvantage is that it needs multiple input/output lines. Other examples of serial interfaces are SPI and I2C. 

UART library for Silicon Labs microcontrollers using interrupts

Supported Devices

At the moment only following devices are supported. I might add more in the feature. It might work for other similar devices not listed below.

  • C8051F330

Defining SYSCLK or F_CPU in Simplicity Studio

The macro SYSCLK or F_CPU specifies the CPU frequency and is used by libraries such as <delay.h> to calculate the delay based on the frequency of the processor or by UART libraries to calculate baud rate.

Up until now I used to define F_CPU like this:

#define F_CPU    24500000UL

Defining this way will work but it could lead to issues and confusion when you have multiple files that define this macro. The ideal way is to define it in a single place. This could be a Makefile if you are using custom Makefiles or in the IDE project configuration.

Defining SYSCLK / F_CPU in Simplicity Studio

Open the project properties window in Project -> Properties (Alt+Enter) then in the left panel select the C/C++ Build -> Settings menu. Then in Tool Settings - > Keil 8051 Compiler -> Symbols use the green + button to define a new symbol SYSCLK=2450000UL. Of course the SYSCLK value depends on your CPU frequency that in this example is 24500000 Hertz or 24.5MHz. The UL at the end stands for unsigned integer.

Defining SYSCLK symbol in Simplicity Studio 01

Defining SYSCLK symbol in Simplicity Studio 02

That's it. Now all included files in your project will use a single SYSCLK value without giving errors such as "SYSCLK is not defined", "SYSCLK redefined", etc.

Since this method of defining is not as obvious as the first one, it is easy to forged to do it when starting a new project but there is a simple solution for that - using preprocessor conditionals.

#ifndef SYSCLK
	#warning	"SYSCLK not defined. Define it in project properties."
#elif SYSCLK != 24500000
	#warning	"Wrong SYSCLK frequency!"

This macro can be placed at the beginning of the main.c file before any type of code. If the SYSCLK is not defined it will output a warning. If SYSCLK is defined but is not the value specified in the macro, it will also output a warning. This is also helpful to see what the CPU frequency is and to ensure that the defined value in project properties is correct.

Tuesday, March 5, 2024

Open source software for programming Silicon Labs microcontrollers using C2 interface

The C2 Programmer is designed for programming Silicon Labs microcontrollers over the C2 interface by the use of an AVR microcontroller with the necessary firmware and an USB to serial adapter. The software is written in JavaScript and is based on ScriptCommunicator by Stefan Zieker.

Custom open source C2 programmer

What does it do

  • Save flash memory to .bin (binary), .txt (hex format) or .hex (Intel hex) file.
  • Write an Intel hex file to flash memory.
  • Erase device option.



Why use a custom programmer when you can buy one? The reason I have started developing this software is not necessarily cost related. I could have just bought a programmer and be done with it instead of coding hours and hours, but I thought it will be a good addition for the community since being custom made it can be... customized. There are other reasons such as, maybe the commercial one is not available in your area or not in stock, or you might want to program only one or two microcontrollers and consider is not worth investing in a programmer.

Actually this is how the project started. I have an RDM-6300 RFID card reader that I thought is a writer too. Then I saw it's using the C8051F330 microcontroller from Silabs and decided to give it a try and make it also write RFID cards or at least add support for other chips. By not buying a programmer I made one that I could use in my project and also make it available for others.

Note that debugging is not supported so if you need this feature, consider buying a commercial programmer.

Theory of operation

Application on the computer instructs the firmware on an AVR microcontroller on what commands to send to the C2 interface of the programmed device. The communication between computer and programming device (the AVR) is done using an USB to serial converter. 

C2 programmer diagram

The application is build on top of ScriptCommunicator which is a scriptable cross-platform data terminal that supports serial port (RS232, USB to serial), UDP, TCP client/server, SPI, I2C, and CAN. Script Communicator it's an awesome application that unfortunately is not that well known. The best part is that the interface can be made to look how you want by using the Qt Designer included with the app.