Rusty STM32

Introduction

So I’ve recently being have a play with building software in Rust for my Nucleo F7 dev board. As I didn’t really fancy writing USB drivers and wanted to have a play with ST’s STM32Cube drivers I thought the best course of action was to mix C and Rust together in one project.

Setup

For this project I decided that I would use the rust build system (Cargo) and link my C code via a static library. The Embedded rust book (https://docs.rust-embedded.org/book/) gives a good walkthrough on what is needed to build and link a project and get setup with something simple that can print ‘hello world’ via semihosting on the debugger and I did this before adding any of my C code.

As I’m using Visual Studio Code for development currently Cortex-Debug VS Code extension (https://marketplace.visualstudio.com/items?itemName=marus25.cortex-debug). This enables debugging using OpenOCD via ST-Link SWD board on the Nucleo development board. For this kind of project having a debugging capability makes life much easier.

Creating the C setup and drivers

The first thing that can make life a lot easier is to use the STMCubeMX program from ST to build an initial setup C code project that will initialise the clocks on the CPU so they are all running at the correct frequency. This utility has some templates for the Nucleo series boards that would also be a good starting point if you are using something else like the STM32F4 Black pill board or your own custom CPU.

I used this to build a initial project with a serial port and several integrated peripherals then renamed the main method to an init method (and made it return) that can be called from the main rust code.

I did find one gotcha in this in that it looks like the rust init routines do not clear the processors ram before running. This gave the symptom that the serial port would work when the ST-Link windows utility was run but not at any other time so it’s important to manually clear the gState vairable (and probably others) before you initialise the HAL drivers. I’m currently initialising each HAL driver from the C code but this could also be done in Rust (and in that case all initialisation has to be done explicitly).

Creating the Rust code

The Embedded Rust book (https://docs.rust-embedded.org/book/start/qemu.html) provides a good overview on how to setup the Rust side of things. I followed this and got a simple program to output ‘Hello World’ via Semihosting on the Debugger. One thing to note is that the debugger will hit a breakpoint when outputting each character if you don’t enable semihosting on OCD. Using hprintln is also recommended over itm as the later won’t work if you don’t have the SWO pin connected.

In order to link your C code to your rust code you will need to modify the build.rs I added the following lines to mine:

    println!("cargo:rustc-link-lib=static=<stm32-c-library>");
    println!("cargo:rustc-link-search=native=<src-dir>");

This will tell Cargo to link your C code with the rust code when building. For the above example I placed the C library .a file in the source root directory, though for less experminental projects it would be better to create a lib directory. You can then use an extern “C” directive to tell the rust compiler about your C functions. Here is a simple one below:

extern "C" {
pub fn c_main();
pub fn HAL_IncTick();
pub fn SystemInit();
}

As the Rust cortex libraries handle the Interrupt table it is very important that you define an interrupt handler for the SysTick interrupt. Not doing this will cause the board to get stuck in Rust’s DefaultHandler which will just loop forever. The SysTick interrupt is also very important for the STM32 Hal for tasks such as the delay function so the SysTick timer should at least have the following:

#[exception]
fn SysTick() {
    unsafe { HAL_IncTick();};
    //hprint!(".").unwrap();
}

My next steps

Now I have something that works I want to have a go at getting Futures working and changing over the STM32F4 black pill for the bulk of my coding. These STM32F4 boards are much cheaper and should have plenty of ram to run most the things I’m interested in.

A short introduction to UK legal gender for non-UK folks

Since this came up in regards to emigrating outside the UK and gender recognition I thought I’d write a small post about how gender works in the UK and is recorded. As the UK isn’t quite as organised as countries such as Germany it is possible and common for gender to be different depending on circumstances. This is probably best seperated into 2 sections. As always this is just a basic summary and it’s probably best to consult a lawyer.

Birth Certificate

Under UK law Legal Gender is defined as what’s on your Birth certificate. However for most practical purposes this is not actually legally significant as documentation such as driving licences and passports can be changed based on self ID.

The most important area where legal gender is important is getting married in the UK (as marriage vows in England/Wales may have to be recited in regards to legal gender), pension before the pension age was adjusted and if a person ended up in prison.

Eqality act and sex discrimination

Under the equality act it is illegal to descriminate due to trans status (perception or intent to transition) or sex. This would probably have the effect of covering a trans person in a sex discrimination case the same as a CIS person and there is also no need to disclose trans status in most cases (pretty much only for background checks when working with vulnerable people or working in a rape crisis centre). This is one area I’m not particuly well read up on so probably best to talk to a lawyer or trans organisation.

Other documentation

Apart from birth certificates all other documentation does not show legal gender but instead shows either the gender person identifies as or whatever they like (in the case of Ms/Mr/Mx as these have no legal standing).

In the case of ID documents such as passports/driving licences all that is required to change gender is a letter from a doctor or psychiatrist. It is also possibly to change your gender under the NHS using this method.

In the UK anyone can change name using either a deed poll (signed by witness’) or statutary declaration (signed by solicitor/judge). Both of these documents can be created using templates which are available free online.

Due to the equlity act and other provisions it is also easy to change documents such as exam certificates which are not normally changable by CIS people. For example changing your degree certificate is as simple as sending a certified copy of your statuatary declaration or deed poll and stating that you have undergone gender transition.

Toilets etc.

Access to single sex areas such as toilets is currently not controlled by law and is via convention. This means that you can use the one that match’s your gender identity. This is also backed up by case law around the Eqaulity act.

And update and maybe some hope for the UK

So it’s been awhile since we made the decision to move countries (Well technically since December). Things have been progressing slowly while we wait for the pandemic to subside and things to open up again.

While plans have been progressing it’s been a difficult month here in the UK and anxiety for not just me but most of the trans community has been through the roof. Earlier in the month we heard leaks from the government that the proposed reforms for our gender recognition laws were going to be shelved and our rights were going to be rolled back. This was rumoured to involve our access to ‘single-sex spaces’ being denied which would meen we potentially could even loose the right to use the toilet without harrasment (or arrest).

There has been a big push back from the community though and demonstrations have happened nationally. The push-back which has hopefully made the government realise that we are the small easily kicked minority that they hoped we would be. Surveys have also shown that there isn’t widespread support for rolling back our rights.

All of this has mean’t that the goverment has now announced that reform will be delayed again (first to this week and now to September). It means that we won’t get the prize of having self-ID for changing our birth-certificates but it also means that things atm won’t get worse either.

For now this feels like small victory. For me personally it gives me time so we can get ready to move. We still don’t trust that things won’t get worse over time and we still need to move somewhere where we can get away from the harm inflicted on us by the media and government. It also means that our friends and others that we care about also have a chance to be safe even though they don’t have the ability to move.

Lou’s future adventures beyond the UK

It’s been too long since I’ve blogged so due to some scary and exciting future life changes I’ve decided that it’s best to get writing again.

It’s been a difficult few years in the UK and things have been slowing getting worse here. The main stream media has become more hostile to trans people and we have a TERF for an equalities minister. The community is fighting back though and hopefully things will get better eventually. So hopefully one day the UK might be a better place for trans people.

We have decided though that this is not a country we wish to live in anymore when we could be happier elsewhere. I suspect one of the things that has really changed is me and my awareness of how England feels rotten to the core and although the younger generations here give me hope it’s not somewhere I feel comfortable anymore.

On a lighter note we are currently exploring a couple of options with help from the people over at https://twitter.com/TransEmigrate, Ireland and Germany.

Germany

Germany was our original plan before the coronavirus hit the world. We had been trying to rush preperations so we could leave before we would need to get visa’s to move there (1st January 2021).

We know a little more about Germany as I’ve been posted there for work and we have been on holiday in the Berlin and Cologne area. There are lots that sound good about Germany and a few things that are a little scary for our situation and we need to research to see what the situation is especially when it comes to things like gender recognition as we have both changed our names and gender on our passports but not birth certificates.

Ireland

Ireland is something we hadn’t thought about till recently. They have self-ID so that is a massive plus for us as we could both change our legal gender easily. This is a massive plus for me as changing birth certificates is costly and bauracratic here. In the UK it may also not be possible for me to change without starting the process again (probably privately) as I believe the people who signed off my transition are no longer practicing. The only downside with Ireland will be living in a smaller country and we already find it difficult to live in rural Cambridgeshire.

Further information

I’m creating a page which will be updated as I discover more here

Adding a custom USB socket to the STM32 NUCLEO-F746

I’m currently building a design on the STM32F7 which I want to later port to a F4-NUCLEO board and eventually my own design so I want to test the USB host without the supplied electronics.

In order to do this I built a small USB breakout adapter and tried directly connecting this to 5V (for power) and PA11/PA12 (for USB).  The breakout was made by simply soldering some header pins to a USB socket.

I then attached the USB socket to CN12 as shown below.

The complete pinout for CN12 can be found here https://developer.mbed.org/platforms/ST-Nucleo-F746ZG/.

You will need to connect the pins on the usb connector as shown below:

  1. VBUS (+5v)
  2. PA11
  3. PA12
  4. GND

STM32 Nucleo boards and blackmagic probe.

I recently purchased a STM32F7 Nucleo boards to play around with Chibios.  These boards feature a integrated STlink debugger, I however do most of my development using a custom built debugger based on the blackmagic probe.  This has the advantage of being easily connectable to GDB via emulating gdbserver on it’s virtual serial port.

Unfortunately there doesn’t look like there is a documented way of connecting a 3rd party debugger to the Nucleo board but reading the schematics showed an answer in the form of the CN4 jumper block.

In order to connect a the black magic probe to the Nucleo board simply remove the jumpers from CN4 on the ST-Link .  You can then access the SWDIO pins on the main MCU as shown in the list below.

  1. SWCLK/TCK
  2. NO-CONNECT
  3. SWDO/TMS
  4. NO-CONNECT

You will also need to connect the ground pin of the blackmagic probe to a ground pin on the Nucleo board (I used one of the ground pins which have jumpers on them which are used for feet on the board.

And here is a picture of the board.

Passive POE Raspberry PI

I’be been using a Raspberry PI with a DVB-T2 card so we can stream TV to our PCs and tablets around the house.  As the PI sometimes crashes I’ve wanted to make it work with passive POE, I’ve also wanted to put it in a box.

So I’ve built a box which has a barrel connector for my POE adaptor cables.  The box also has a DC/DC converter to convert the 12V-24V I’m using in my server cabinet to the 5V required for the Raspberry PI.

As there wasn’t room in the box to have a lead going from the USB power socket on the PI i decided to feed power in via the expansion connector (i also didn’t have many spare USB cables).  Unfortunately this has the disadvantage of bypassing the poly fuse which protects the PI so I will probably add my own at a later date.

A view inside showing the DC/DC converter module.

Duna 1

So I’m currently going through my periodic phase of being addicted to KSP.  This time I’ve been using the USI Lifesupport mod ( https://github.com/BobPalmer/USI-LS/wiki ) to make things a bit challenging.  This mod requires you to keep your Kerbels supplied with food and have enough space to wander around if they are stuck somewhere for a long time.

The current mission which i’m working on is Duna 1, my first mission to Duna (KSP equivalent of mars).

 

Our brave kerbals posing for a photo on Duna in front of a NuLander 1c lander.

Flying the Grob 103a

On Sunday my usual glider the ASK21 wasn’t available so I flew the clubs Grob for the first time.  This was interesting as the Grob is a lot less forgiving of uncoordinated (flying without using the rudder correctly) flight then the ASK-21.  It really highlighted that I’ve started to develop a habit where the first thing I drop in high concentration situations is my rudder control.

Even though my rudder control caused me lot’s of problem I still covered quite a lot flying the Grob.  The aircraft is much less placid then the ASK21 which was really interesting when I practised stalling it.  I found it to have a much more obvious buffeting which was interesting.

A Grob 103 similar to the one we have out our club.

I also made lots of progress with taking of on the winch which was welcome as I haven’t been able to practice this for a few weeks due to the club winch being out of action.  The highlight being my first couple of launches where I correctly flew the whole of the launch on my own!

Outside of real world aviation I’ve also bought some new Saitek rudder pedal’s for my PC.  I think having pedals is even more important when you fly real aircraft as well as simulators and will hopefully get me more into the habit of using the rudder more when I fly in real life.  I’ve also been playing around with trying to stall and spin various aircraft in X-Plane and DCS.  It’s been particularly interesting with DCS’ F86 as this really shows up the affects of high G on stalling speed.   While stalling the F86 I managed to get it to stall where the wing drops and it goest into a dive.  Recovering from this was really difficult and I crashed the aircraft into the ground quite a few times.

IMG_20151026_214019