Not able to create a minimal main.c file to turn on the leds

Post here to get support
Post Reply
franciscofigari
Beginner
Posts: 3
Joined: Mon Feb 05, 2018 7:24 pm

Not able to create a minimal main.c file to turn on the leds

Post by franciscofigari »

Hi,
At my lab we have the goal of replacing the crazyflie firmware. Briefly, we want to get rid of FreeRTOS and bitcraze controllers, and only use the provided drivers.
Right now I'm trying to create a basic program which should just start some leds. This is my main.c file:

Code: Select all

#include <led.h>

int main() {
	ledInit();
	ledTest();
	
	return 0;
}
After generting the .bin file, I flash it on the crazyflie using the antenna and the client from the VM. I know the flashing process is working because I'm being able to flash the original firmware back. However once I reset the crazyflie the following happens:
- M2 blue led gets lit on
- After a little moment, M4 and M1 red leds get lit on (the blue one still on).
However that's not what the ledTest routine should do.

I checked the original main.c file, and platforimInit is called. The problem is that we want to start from zero, hence I only want to call the essential functions that will allow us to work.
Well, my question is: Do I need to call some other function before being able to play with the leds? Is there another one for the motors?
Even if this functions call the FreeRTOS, I'ld appreciate knowing which ones are mandatory for the code in my example to work.

Thank you.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Not able to create a minimal main.c file to turn on the leds

Post by arnaud »

Hi,

That is an interesting project, please keep us updated of your progress :-).

First of all, I am not sure of why you are observing this behavior on the RED leds. The M2 LED is handled by the nRF51 radio and power management MCU, do you do not have direct access of it from the STM32.

The function you are calling is using freeRTOS (https://github.com/bitcraze/crazyflie-f ... f405.c#L98) which is a problem if you are not in a FreeRTOS task.

More generally I do not think you will be able to use any unmodified drivers since all our driver are using FreeRTOS in one way or another.

You can look at the Crazyflie driver and extract the relevant parts, mostly the initialization. But what you most likely will need to do is to start from the STM32F405 reference manual and ST lib documentation. For example, ST has a cube-MX software that can generate some startup code for you, it would be a much easier start than with our drivers (I have started the LPS node firrmware like that).

One comment about debug: while it is possible to develop everything only from the bootloader, it would be much easier to have a debugger attached to the Crazyflie in order to be able to debug your code when bugs happens (like now, to understand exactly what is happening).
franciscofigari
Beginner
Posts: 3
Joined: Mon Feb 05, 2018 7:24 pm

Re: Not able to create a minimal main.c file to turn on the leds

Post by franciscofigari »

Hi,
I forgot to mention that for the moment we won't touch the nRF51. The idea is to develop what we need step by step.

I would have thought that the crazyflie drivers used acted directly on hardware. But instead they use FreeRTOS and the STM32 drivers, and FreeRTOS uses the STM32 drivers, right?
Should calling ledInit() be enough to be able to use the leds?

About the lights I'm receiving, it happens sometimes even when flashing the original firmware and kind of randomly. What is the meaning of both M1 and M4 red leds and M2 blue led being constantly lit?
To flash the STM firmware I use the radio antenna. I'm following theses steps on the VM:
- `make` to build the project
- put the crazyflie in bootloader mode
- `make cload`
The output seems to indicate that the flashing is being done correctly, and do not report any error. And since it worked many times I suppose this method is correct. I made some little modifications to the Makefile to be able to work with C++, but the core is still the same.

Yes, being able to debug would be great. I'll see if I can get the adapter. In the meantime, is it possible someway to make some logs somewhere?

Thank for the answers btw.
arnaud
Bitcraze
Posts: 2538
Joined: Tue Feb 06, 2007 12:36 pm

Re: Not able to create a minimal main.c file to turn on the leds

Post by arnaud »

Not touching the nRF51 is a good idea. The nRF51 firmware has been designed to work as a power management chip and radio bridge so is you implement the syslink protocol (between STM and nRF) you will be able to read battery voltage, control the system power and communicate with the radio.

FreeRTOS does not use the STM32 driver, thanks to the Cortex-M architecture providing everything you need for an OS (ie. systick timer and interrupt controller) the freeRTOS implementation is the same for all Cortex-M4 MCUs. The drivers in the Crazyflie makes hardware functionality available to FreeRTOS tasks, this means that the drivers use the STM32 lib to access the hardware (poking register directly is not super fun in a STM32) and use FreeRTOS to make this accessible safely to taks (typically synchronizing the code with mutex semaphore and queues).

You cannot call ledInit unmodified without running in a FreeRTOS taks, you need to remove the FreeRTOS parts in the code. When you have done that, initializing the GPIOs in output, the way ledInit is doing it, should be enough to take control of the LEDs. In order to re-write a firmware for Crazyflie you will have to work with the schematic, the STM32 reference manual and the STM32 lib (if you want to use it) so I think a good start would be to write the led driver yourself.

Are you flashing from the virtual machine? if so, it might be that you have errors while flashing. You can try to install the winusb driver for the crazyradio dongle, I have seen it help. If you are not using the VM on windows, I am very interested to know your setup. What you are observing looks like flash error and I have not observed that in any other case than with the VM.

As for C++, I would advise to first compile a basic C program with C compiler and then when it works switch to the C++ compiler, I have no big knowledge of C++ but the few time I have tried to compile a C firmware with a C++ compiler things have not worked out of the box.

To debug you will need the debug adapter and a SWD debugger. We normally use the stLink V2 but anything that supports the STM32 should work (including j-link). In the mean time, you can try to enable the UART that is routed to the deck expansion port, you could make an FTDI-cable adapter with a prototyping deck and get a serial console that way.
franciscofigari
Beginner
Posts: 3
Joined: Mon Feb 05, 2018 7:24 pm

Re: Not able to create a minimal main.c file to turn on the leds

Post by franciscofigari »

Indeed I'm flashing from the VM (the host being a W10 machine). I'll try again next week directly from the host.
Thanks for all the info.
Post Reply