Writing a serial out for STM nucleo

Hi, I’ve been trying to get started with STM32 for a while and I always reach the same sticking point, which is that I can’t bridge between my current knowledge (8 bit MCUs written with AVR GCC) and the different way of coding for STMs (HAL or CMSIS or whatever, I’m not even exactly sure which I should be using)

I recently bought a nucleo f031K6 board for a project im making. My project is some switches, leds, a serial DAC and a serial ADC. One thing that im going to have to use a fair amount is a serial data out so I thought id start with how to do that. The way I would usually do this is to use a number (let’s say 8 bit) to hold a ‘walking bit’ to count through each bit in a byte, on each count ANDs with the data (also 8 bit) and the output is then sent to the output pin. I am used to programming in C in AVR GCC so I would write something like this -

for(i=0;i<8;i++){ // loop 8 times

COUNT <<= 1;  // left shift walking bit

	if (COUNT & DATA) {  // compare data and walking bit
		PORTA |= 1<<0;  //set output if data is high
		} else {
		PPORTA &= ~(1<<0); // clear output if data is low
	}
	}

How do I achieve the same thing with my nucleo? I am most used to using bare C, but I don’t know how to code this for STM. I am happy to use CMSIS or HAL, whatever is closest to what I know already, I just need to figure out how to write code for these things then I think I can make progress :slight_smile:

any help much appreciated

Assuming you have initialized the GPIO correctly, low-level pin access usually looks like this:

// For F1, F3, G4
GPIOA->BSRR = GPIO_Pin_0; // Set Pin 0 of GPIOA high
GPIOA->BRR = GPIO_Pin_0; // Set Pin 0 of GPIOA low

// For F4
GPIOA->BSRRL = GPIO_Pin_0; // Set Pin 0 of GPIOA high
GPIOA->BSRRH = GPIO_Pin_0; // Set Pin 0 of GPIOA low

// For H7
GPIOA->BSRR = GPIO_Pin_0; // Set Pin 0 of GPIOA high
GPIOA->BSRR = GPIO_Pin_0 << 16U; // Set Pin 0 of GPIOA low
1 Like

That’s great, I was hoping I could use syntax like that. So, as you are not using the HAL commands, does that mean you are using CMSIS? Im trying to understand where the difference is.

Are there resources that will tell me exactly which registers are on my chip and what they do? I might be being slow, but I can’t seem to find them

These are neither HAL commands nor CMSIS commands. You’re not mentioning if you’re using stmlib or are trying to download the files from ST.

Search for GPIO_TypeDef in the source files of whatever you’re using.

1 Like

Im not exactly sure what you mean, could you elaborate? Im using STMcubeIDE and I opened it, generated the code to set up the processor and put a blink sketch on the nucleo board. I don’t know anything that isn’t required to do that

I thought you were trying to do this using the Mutable Instruments development environment; or if you were trying your own environment with a makefile.

This is not the case here as you’re using the IDE. I’m not familiar with it.

You may want to search out Mastering STM32

It’s an excellent guide to get into programming those Nucleo’s for beginners.

HAL is a library like Arduino. HAL utilizes definitions used in CMSIS.
So you are really using both when you use the HAL library.
That’s how I understand it…

So you would use your cubeMX to config your Nucleo, then open your choice of IDE.
I use System Workbench for STM32
https://www.openstm32.org/HomePage

It is a customized Eclipse IDE for STM32’s.
I started using it because it’s FREE and my project currently uses 400KB.
So using something like the free ver. of Keil just won’t do for me…

Don’t feel bad if you struggle at first. If coding uC’s was easy, everybody would be doing it!

You might also check into this guy’s tutorials on STM32’s

He has a strong Indian accent, but you get the hang of it and he’s VERY GOOD at showing you the deep workings of the STM32’s and other ARM uC’s.

Both the book and the class are worth every penny spent if you truly want to gain knowledge how to program these powerful uC’s.

GOOD LUCK!

4 Likes

thanks very much! that’s a great help