Roll Your Own Raspberry Pi Kernel

Getting Started.

There are many distirbutions of operating systems avaialble for the Raspberry Pi. But what if you wanted to write your own? Where would you start if you wanted a realtime system without all the overheads of a bulky operating system. This is my work through on getting started with writing my own kernel from scratch for the Raspberry Pi. I will endeavour to write this in C, mainly because it’s a language that I am familiar with, but also as it has a track record of being the language used to write most of the operating systems today, and also because it is the goto language for interfacing directly with hardware which is ultimately what we are trying to do.

There are probably many tutorials out there already that show you how to do something similar. This is not meant to compete with them, and there are certainly no guarantees that it will match them. This blog isn’t really for you. It’s for me. Though you may find it useful.

Of the other tutorials that I have found two really stand out.

Baking Pi - A Tutorial for writing an OS for the Raspberry Pi, mainly in Assembly.
Valvers.com - A tutorial for compiling a system for the Raspberry Pi, written mainly in C.

Cross Compiling

I will be doing the majority of the work on my mac. The thing with doing that is that my mac has an Intel processor, where the Raspberry Pi has a Broadcom ARM chipset. Therefore what is known as a cross compiler is needed.

The GCC ARM Embedded Project on Launchpad provides a GCC toolchain to use on mac. Addtionally you could head to https://developer.arm.com/open-source/gnu-toolchain/gnu-rm which now holds the most recent and up to date compilers. However the easiest way to install the cross compiler on a mac is to use homebrew. To install the gcc cross compiler, first of all install homebrew and then install the compiler with the command brew install gcc-arm-none-eabi-49 You should also install at this time the gnu debugger. brew install gdb-arm-none-eabi
You can now type on the command line arm-none-eabi-gcc and if all is functioning you should see a response similar to the below.

1
2
3
>arm-none-eabi-gcc
arm-none-eabi-gcc: fatal error: no input files
compilation terminated.

Again you can test the debugger by typing arm-none-eabi-gdb If all is well this should open up the debugger on the command line. You can exit by typing quit and return.

The GCC settings for compiling code for the orginal Raspberry Pi can be found on the elinux page.

1
-Ofast -mfpu=vfp -mfloat-abi=hard -march=armv6zk -mtune=arm1176jzf-s

As mentioned on that page, -Ofast may cause issues so it is recommended to use -O2 instead. Also -mcpu=arm1176jzf-s can be used in place of -march=armv6zk -mtune=arm1176jzf-s

For the Raspberry Pi 2 as it has a different architecture. The porcessor has been replaced by a quad core Cortex A7. To compile effectively for this processor the compiler options are:

1
-O2 -mfpu=neon-vfpv4 -mfloat-abi=hard -march=armv7-a -mtune=cortex-a7

Connect to MySQL server in C

Connecting to a mysql database from C is a fairly straightforward process. The following instructions should work on any Linux distro or UNIX computer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <mysql.h>
#include <stdio.h>

main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;

char *server = "localhost";
char *user = "USER"; /* Enter your mysql username */
char *password = "PASSWORD"; /* Enter your mysql password */
char *database = "mysql";

conn = mysql_init(NULL);

/* Connect to the mysql database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}

res = mysql_use_result(conn);

/* output all table names */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);

/* close our connection */
mysql_free_result(res);
mysql_close(conn);
}

MySQL comes with a script called mysql_config. It provides useful information for compiling your MySQL client and connecting it to a MySQL database server. You need to use following two options.

Pass the libs option i.e. ‘Libraries’ to show required Libraries to link with the MySQL client library.

1
$ mysql_config --libs

Output:

1
-L/usr/local/Cellar/mysql/8.0.13/lib -lmysqlclient -lssl -lcrypto

Pass cflags option ‘Compiler flags’ to find include files and critical compiler flags and defines used when compiling the libmysqlclient library.

1
$ mysql_config --cflags

Output:

1
-I/usr/local/Cellar/mysql/8.0.13/include/mysql

You need to pass above two options to your compiler. So to compile above program, enter:

1
$ gcc $(mysql_config --cflags) mysql.c $(mysql_config --libs)

Now execute program:

1
$ ./a.out

Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
MySQL Tables in mysql database:
columns_priv
component
db
default_roles
engine_cost
func
general_log
global_grants
gtid_executed
help_category
help_keyword
help_relation
help_topic
innodb_index_stats
innodb_table_stats
password_history
plugin
procs_priv
proxies_priv
role_edges
server_cost
servers
slave_master_info
slave_relay_log_info
slave_worker_info
slow_log
tables_priv
time_zone
time_zone_leap_second
time_zone_name
time_zone_transition
time_zone_transition_type
user

You have successfully connected to and retrieved information from your MySQL database from within a C environment.

Connecting and using the DS18B20 temperature sensor with a microcontroller

Introduction

The DS18B20 is a 1-Wire digital temperature sensor from Maxim IC. It reports the temperature in degrees Celsius with 9 to 12-bit precision, with a range from -55 to 125 (+/-0.5). Each temperature sensor has a unique 64-Bit serial number etched into the silicon which allows for a huge number of sensors to be used on one data bus with the probability of 9,223,372,036,854,775,807 to 1 that any two will clash!

The DS18B20 Temperature sensor

Features

  • Unique 1-Wire interface that requires only one port pin for communication.
  • Each device has a unique 64-bit serial code stored in an onboard ROM, as mentioned above.
  • Requires no external components.
  • Can be powered from it’s data line.
  • Power supply range is from 3.0V to 5.5V.
  • Measures temperatures from –55°C to +125°C (–67°F to +257°F) with ±0.5°C accuracy from –10°C to +85°C.
  • Thermometer resolution is user-selectable from 9 to 12 bits.
  • Converts temperature to 12-bit digital word in a maximum time of 750ms.
  • Alarm search command which allows it to identify and addresses devices whose temperature is outside of programmed limits (temperature alarm condition).
  • Applications include thermostatic controls, industrial systems, consumer products, thermometers, or any thermally sensitive system.

What you need to make a working thermometer

To construct an electronic thermometer you will need the following:

  • A microcontroller. (In this instance an Arduino or Atmega328p)
  • A DS18B20 sensor and a 4.7k resistor.
  • Breadboard.
  • Jumper leads.

Libraries

You will need to download and install 2 libraries for this exercise. If you are using the arduino then this should be fairly straight forward and can be acheived by following the menus within the IDE. The two libraries that you will need are:

  • 1-wire bus
  • Dallas Temperature

Build the simple circuit

The DS18B20 circuit diagram

To read the data from DS18B20 in the serial monitor of the Arduino IDE, build the circuit in the above schematic.

Connect the sensor pins to the Arduino in the following order: pin 1 to GND; pin 2 to any digital pin (pin 2 in this case); pin 3 to +5V or +3.3V. Connect the pull-up resistor between the Vcc supply and the data wire.

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// First we include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>

// Data wire is plugged into pin 2 on the Arduino
#define ONE_WIRE_BUS 2

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);

void setup(void)
{
// start serial port
Serial.begin(9600);
Serial.println("Dallas Temperature IC Control Library Demo");

// Start up the library
sensors.begin();
}

void loop(void)
{
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
Serial.print(" Requesting temperatures...");
sensors.requestTemperatures(); // Send the command to get temperature readings
Serial.println("DONE");
Serial.print("Temperature is: ");
Serial.print(sensors.getTempCByIndex(0)); // Why "byIndex"?
// You can have more than one DS18B20 on the same bus.
// 0 refers to the first IC on the wire
delay(1000);
}

Place the above code into the IDE and upload via the arduino uploader. If everything is okay you should see the temperature being measured and showed in the serial monitor of the IDE.

The difference between TCP and UDP

What is the difference between TCP and UDP?

Both TCP and UDP are protocols used for sending bits of data — known as packets — over the Internet. They both build on top of the Internet protocol. In other words, whether you are sending a packet via TCP or UDP, that packet is sent to an IP address. These packets are treated similarly, as they are forwarded from your computer to intermediary routers and on to the destination.

TCP and UDP are not the only protocols that work on top of IP. However, they are the most widely used. The widely used term “TCP/IP” refers to TCP over IP. UDP over IP could just as well be referred to as “UDP/IP”, although this is not a common term.

TCP

TCP stands for Transmission Control Protocol. It is the most commonly used protocol on the Internet.

When you load a web page, your computer sends TCP packets to the web server’s address, asking it to send the web page to you. The web server responds by sending a stream of TCP packets, which your web browser stitches together to form the web page and display it to you. When you click a link, sign in, post a comment, or do anything else, your web browser sends TCP packets to the server and the server sends TCP packets back. TCP is not just one way communication — the remote system sends packets back to acknowledge it is received your packets.

TCP guarantees the recipient will receive the packets in order by numbering them. The recipient sends messages back to the sender saying it received the messages. If the sender does not get a correct response, it will resend the packets to ensure the recipient received them. Packets are also checked for errors. TCP is all about this reliability — packets sent with TCP are tracked so no data is lost or corrupted in transit. This is why file downloads do not become corrupted even if there are network hiccups. Of course, if the recipient is completely offline, your computer will give up and you will see an error message saying it can not communicate with the remote host.

UDP

UDP stands for User Datagram Protocol — a datagram is the same thing as a packet of information. The UDP protocol works similarly to TCP, but it throws all the error-checking stuff out. All the back-and-forth communication and deliverability guarantees slow things down.

When using UDP, packets are just sent to the recipient. The sender will not wait to make sure the recipient received the packet — it will just continue sending the next packets. If you are the recipient and you miss some UDP packets, too bad — you can not ask for those packets again. There is no guarantee you are getting all the packets and there is no way to ask for a packet again if you miss it, but losing all this overhead means the computers can communicate more quickly.

UDP is used when speed is desirable and error correction is not necessary. For example, UDP is frequently used for live broadcasts and online games.

avrdude

Avrdude is a library that allows you to perform functions and upload files to the AVR series of microcontrollers. It is open source software and is freely available to download. With an interface it makes it easy to upload files and interrogate microcontrollers from the command line. It is the software that the Arduino IDE uses in the background.

Example of erasing a chip completely: (Warning this will erase the bootloader too!)

avrdude -p m328p - c usbtiny -e

Will result in output similar to the below, confirming that your chip indeed has been erased.

1
2
3
4
5
6
7
8
9
10
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.01s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: erasing chip

avrdude: safemode: Fuses OK (E:FD, H:DE, L:FF)

avrdude done. Thank you.

Programming fuses with avrdude and USBTiny

Fuse programming on the ATMega328 is very straightforward with the USBTiny. Connect the USBTiny to the AVR with the ICSP (In Circuit Serial Programming) connections. You need to ensure that you have avrdude installed on your computer. Once connected the following commands apply.

avrdude -c usbtiny -p ATmega328p

the above line reads the fuse settings on an ATmega328p

avrdude -p m328p -c usbtiny -U efuse:w:0xff:m -U hfuse:w:0xd9:m -U lfuse:w:0xe2:m

the above line writes the fuse bits on an ATmega328p

The fuse settings for standard 16MHz ext clock are E:FD H:DE L:FF

The fuse settings for standard 8MHz int clock are E:FF H:D9 L:E2

The fuse settings for standard 1MHz int clock are E:FF H:D9 L:62

^