cancel
Showing results for 
Search instead for 
Did you mean: 

I would like to do an absolute minimum bare bones project. Could someone direct me to a C project that just shows how to make a port input or output, and how to toggle the port high and low?

MHolm
Associate II

I've got a MCBSTM32F400 development board. Every "getting started" thing I've found (blinky, for example) has like 900 files in the project, and 150,000 lines of code. This is so disheartening. I got into microcontrollers because of their simplicity.

28 REPLIES 28

The idea is that you get the compiler to read the thousands of pages of code, and you don't translate it line by line manually.... It is why you have a PC running at 3 GHz on your desk and you've downloaded and installed 300 GB of tools.

You need a vector table with a minimum of two entries.

You can #define register addresses you're using

Enable the clocks on for the GPIO bank

Write the GPIO mode, speed and pull-up setting.

Toggle the bit in the GPIO ODR register repetitively.

Pretty sure this could be done with 100 lines or less.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
MHolm
Associate II

I came from the ATMega, ATTiny, dsPIC30F families. I don't doubt that it can be done in less than 100 lines, it's just that I haven't been able to find any example projects in C like that. There was no need to have a vector table with at least 2 entries with any microcontroller I've used before, so is there a sample project anywhere that says what the 2 entries are? I would have just stuck with the dspic family, but I would like to upgrade to floating point.

MNapi
Senior III

you sound more like me a year ago when I got STM32 board and I did not have clue what to do with it.

You need first use Cube MX it will generate bunch of initialization files, you setup there chosen pin as output for example PIN5 (PA5) it means PIN 5 on port A

then in project manager setup your IDE (dowbload Keil, I found it as best tool)

then click on generate code it will open Keil with all ready to go.

then find main. c, this where you put your code in

while();

{

//put your code here

}

put

 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);

it wll turn the PIN low

 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_SET);

it will make the pin go HIGH

you can put between HAL_Delay(1000);

MHolm
Associate II

OK, I'll try that out! Yes, I did a year subscription to Keil, and it does feel very natural. It sounds like i need to change my thinking about how this chip works. I started out with DOS C programmiing back in the day, because dos didn't get in the way, and I could just program. Then windows came along and you had to auto-generate projects that were hopelessly complicated, but you had your spot where you could type your code, and as long as you didn't break all the stuff, you were good to go. The atmel and dspic families were not too bad to just read the datasheet (maybe 700 pages at worst), initialize the bits as they needed to be, and then you just go to town (they were "DOS"). The STM32 datasheet I found the other day was like 3000 pages, and all the sample project I've found were basically telling me "there is a ton of crap that has to happen for you to do anything on this chip, so don't even try to do it from scratch". I guess I was coming at it from the wrong direction.

It is a good board but unlike Arduino it was designed for scientists working on space station. You cannot figure it out on your own you need somebody to guide you step by step

see this guy on youtube, he is is really amazing I wish STM32 would do something like this to show people how to program this boards.

https://www.youtube.com/channel/UC-CuJ6qKst9-8Z-EXjoYK3Q/videos?sort=dd&view=0&shelf_id=1

Piranha
Chief II

All of this "You need a HAL library and CubeMX generated bloatware to do anything" is absolute bulls*it. And in a long term that is even harmful, because You get used to very inflexible, badly designed, interrupt unsafe and full of other bugs code library.

This is the cleanest and best explained bare metal example I've found:

https://www.embeddedrelated.com/showarticle/460.php

And here is a very easy way to start with a clean almost empty project:

https://blog.bastelhalde.de/post/setting-up-embitz-1-11-for-an-stm32f7-hal-project

I don't use Keil but any half-decent C environment should provide a way to start a simple project which allows to write main() and compile that into working binary, for a given mcu; without any further litter, just the minimal startup code (which ought to contain the vector table according to CMSIS); and provide a path to #include the CMSIS-mandated device header.

Once you find out how to achieve this in Keil (which tests the assumption of Keil being at least a half-decent C environment), then blinky on PG6 is

#include <stdint.h>
#include <stm32f4xx.h>
 
// constants missing from the CMSIS-mandated device headers:
  // GPIOx_MODER - 2 bits per pin
#define GPIO_Mode_In                         0x00  // GPIO Input Mode
#define GPIO_Mode_Out                        0x01  // GPIO Output Mode
#define GPIO_Mode_AlternateFunction          0x02  // GPIO Alternate function Mode
#define GPIO_Mode_Analog                     0x03  // GPIO Analog Mode
 
 
void LoopDelay(volatile uint32_t n) {
	while(n > 0) n--;
}
 
#define DELAY_CONSTANT 400000
 
int main(void) {
  RCC->AHB1ENR |= 0
    | RCC_AHB1ENR_GPIOGEN
  ; 
  GPIOG->MODER = (GPIOG->MODER 
    & (~GPIO_MODER_MODER6)      // LED PG6
  ) | (0 
    | (GPIO_Mode_Out * GPIO_MODER_MODER6_0)   // LED PG6
  );
 
  while(1) {
    GPIOG->BSRR = 0
      | GPIO_BSRR_BS_6         // LED - set
    ;
    LoopDelay(DELAY_CONSTANT);
    GPIOG->BSRR = 0
      | GPIO_BSRR_BR_6         // LED - reset
    ;
    LoopDelay(DELAY_CONSTANT);
 
 
  }
}

 DELAY_CONSTANT might need to be tweaked if the LED blinks too fast or too slow.

JW

Assembler is not for people who are new to it. But why don't you setup for example your own youtube channel and show some simple projects. I would be glad to learn a little from you.

You cannot learn form those 1000+ pages manulas made by STM32. It is for people who do programming for living and studied computer science.

I totally agree. I'm designing an audio system using six H750s and I need to know exactly what they are all doing all the time. Whilst this does seem to be possible with XMOS devices, with STM the only way is to get back to bare metal programming ... which suits me fine :)