4

I'm a little confused about the language/lingo/terminology of the ST line of MCUs in general, and I think this is stopping me from progressing.

A little background: I'm an EE who learned all I know about FW through compulsory college courses that used the AVR platform. Loved it, very simple and easy to use. A quick look through the one and only datasheet, and bang you're abstracting away! Macro's, pound defines, etc...It was so simple! You write a main.c and a Makefile, compile and fire away with avr-dude...life is good.

Then, I walked into the thresher of the STM32 ARM core, and holy smokes...STDPerifieral Libraries, HAL layer, CubeMX, assembly start up files, CMSIS, IDE specific project files, and a dozen different types of just the STM32F4...I have been quickly overwhelmed!

My real problem has been that I've got this STM32F411 Discovery Board, and I need to get an 20x4 Character LCD running. I found a couple of decent looking projects on github and the like:

https://stm32f4-discovery.net/2014/06/library-16-interfacing-hd44780-lcd-controller-with-stm32f4/

https://github.com/EarToEarOak/STM32F4-HD44780

https://github.com/6thimage/hd44780

https://github.com/mirkoggl/STM32F4_LiquidCrystal

But, I cannot figure out how to get any of these compiled correctly. I've been using the CubeMx to generate a Makefile as a starting point, mostly because it makes a linker script and the .s file that I apparently need, but have absolutely no experience or idea on how to do this on my own. That's my addiction to the CubeMX.

I think most of my issues are that I am getting a lot of conflicts by trying to glue the MCU specific generated code by CubeMX and the various online project code together...Either the online projects are calling some .h file or referencing something that the Makefile can't find, and that's where things are going off the rails.... For example, some of these projects are calling the stm32f4xx.h file, but CubeMX doesn't include that in it's generated code.

I had to go elsewhere in the system and copy that file to the ./Inc folder, but that seems really backwards...And, it still didn't build.

If I'm going to keep my head above water here, I need a AVR to STM32 12-step rehab program or something...

So, the big question is:

How would you guys take one of these projects from the first git clone xxxx and build it to run on an STM32F411 Discovery Board using arm-none-eabi and Makefiles only...No IDE's allowed.

And, what are the most important differences to understand between developing for the 8-bit AVR's and the STM32F4? With what limited experience and the amount of novice blunders I'm making, I think the safest path forward is to make a project with CubeMX so that everything is at least set up write, and then carefully try to add the source files in by hand? But that still doesn't resolve what I'm fundementally misunderstanding about what a sane workflow with these devices should be if I'm using Unix as an IDE.

Thanks!

testname123
  • 1,061
  • 3
  • 20
  • 43
  • The problem with most embedded projects is that they start as some port of some helloworld.c from some maker project found in an electronics journal or something like that. They are never built around the idea of hardware portability because that requires an upfront effort which is tedious and never pays off for a single project. Also the silicium vendors don't build their libraries with such an idea (if they supply one at all), and instead program for the specific part. – Vroomfondel Aug 24 '17 at 10:20

1 Answers1

5

So a very broad question. first and formost what exactly was your avr experience, clearly if you started at main.c then someone else built your sandbox/tool environment for you including the bootstrap. The avr is more harvard-ish than arm so actually it is more of a PITA to truly build for without someone doing the work for you.

There is no reason why the experience cannot be exactly the same. You can take an avr document read about the registers for some peripheral poke at the registers for that peripheral in your program and make it work. You can take the st document, read about the registers fro some peripheral, poke at the registers for that peripheral in your program and make it work.

You can take some library like arduino for your avr, read about the api calls, write a program that makes some calls, program the device. Can take some library for your st chip and do the same thing, api calls wont be the same. arduino libraries are not the same api calls as other avr libraries made by atmel or some other party. You could jump on mbed.org and start writing api calls for your or some st chips and poof a working binary.

All mcu chip vendors need to provide libraries as not everyone is willing or (so they think) able to bare metal their way through (not that the libraries are not bare metal but are just api calls so system like). they wouldnt survive in this day and age. Likewise you have to have newer better with some new name so the libraries change. ARM is wonderful but at the same time a royal PITA. Because they make cores not chips, and their cores are surrounded by different stuff. there are a ton of cortex-m4 solutions that use the same cortex-m4 core, but you cant write one cortex-m4 program that works on all of them as the chip vendor specific stuff is all different (sure a massive if-then-else program would work if you could get it to fit). ARM is trying to make a magic layer that looks the same ish and the vendors are being dragged along so this CMSIS and MBED are ARM driven notions to solve this problem. AVR doesnt have this problem, the core and the chip vendor are one in the same. Now there are a number of different AVR cores and even if you had the same peripheral you might not be able to write one binary that works across all of them and or there might be binaries for one (xmega) that dont work on another (tiny) because of differences in the avr core implementation even if the peripherals were the same. the avr problem is much smaller than the arm problem though, but all contained within one company. so the peripherals if they vary are not going to vary nearly as much as atmel vs nxp vs st vs ti vs others using the same arm cores (or at least same named, there are items in the arm source that are easy to modify, with or without floating point with 16 bit fetches or 32 bit fetches, etc, documented in the trm for that core, creating more pain).

within a company like ST across just the cortex-ms they have created different peripherals a number of timers, gpio, uart, pll/clock, etc. If you take the bare metal, talk to the registers no other libraries approach, you will see that the transition from the cortex-m3 to the cortex-m0 they started using a different gpio peripheral, but maybe some of the others were the same. Fast forward to today we have cortex-m3, cortex-m0, cortex-m0+, cortex-m4 and cortex-m7 based devices just from ST, and there are peripherals that mix and match one product line may have a timer similar to an early cortex-m3 product but the gpio that is more like the first cortex-m0 product. And they seem to mix and match each new family they create from a pool of peripherals. So I might have code for a specific chip that works great on some other specific chip, same family or maybe even a little different. But move that to another st stm32 family and maybe the uart code works but the gpio doesnt, take that first program and move it to yet another family and maybe the gpio works and the uart doesnt. Once you have a library of your own for each of the different peripherals you can mix and match that and their libraries attempt to do that, and use a common-ish ideally call such that the code ports a little better but you have to build for the different chip to get the different stuff linked in. Not perfect.

Also look at how old say the very popular thanks to arduino and perhaps avr-freaks before that atmega328p that thing is relatively ancient. In the time since that came out all the stm32s were created, and for various reasons size/speed/internal politics/etc different peripheral choices were created. All the above mentioned cortex-m variations were created with different target use cases within the mcu world in the time that the atmega328p didnt change.

So if you take one avr document and one avr, and have a somewhat working toolchain, you can write programs straight from the avr docs. If you take one stm32 document for one stm32 chip, take just about any of the gcc cross compilers for arm (arm-none-eabi, arm-linux-gnueabi, etc), do your own boostrap and linker script which are pretty trivial for the cortex-m, you can write programs straight from the stm32 docs and the arm docs, no problems both are somewhat well written. repeat for a different stm32 chip, repeat for a cortex-m based nxp chip, repeat for a cortex-m based ti chip.

As a programmer though you want to write one program for two chips you need to look at the two chips and see what is common and different and design your program to either avoid the differences or if-then-else or use a link time if-then-else solution.

I find the libraries from the chip vendors harder to use than just reading the docs and programming registers. YMMV. I recommend you continue to attempt to use their old and their new libraries and try going directly to the registers and find what fits you best. the arms will run circles around the avrs at similar prices, power, etc. so there is value in attempting to use these other parts. avr, msp430, etc have become also-rans to cortex-m based products so professionally you should dig into one or more.

so for example a simple led blinker for the stm32f411 with an led on pa5

flash.s

.thumb


.thumb_func
.global _start
_start:
stacktop: .word 0x20001000
.word reset
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang
.word hang

.thumb_func
reset:
    bl notmain
    b hang
.thumb_func
hang:   b .

.align

.thumb_func
.globl PUT16
PUT16:
    strh r1,[r0]
    bx lr

.thumb_func
.globl PUT32
PUT32:
    str r1,[r0]
    bx lr

.thumb_func
.globl GET32
GET32:
    ldr r0,[r0]
    bx lr

.thumb_func
.globl dummy
dummy:
    bx lr

.end

notmain.c

void PUT32 ( unsigned int, unsigned int );
unsigned int GET32 ( unsigned int );
void dummy ( unsigned int );

#define RCCBASE 0x40023800
#define RCC_AHB1ENR (RCCBASE+0x30)

#define GPIOABASE 0x40020000
#define GPIOA_MODER     (GPIOABASE+0x00)
#define GPIOA_OTYPER    (GPIOABASE+0x04)
#define GPIOA_OSPEEDR   (GPIOABASE+0x08)
#define GPIOA_PUPDR     (GPIOABASE+0x0C)
#define GPIOA_BSRR      (GPIOABASE+0x18)

#define STK_CSR 0xE000E010
#define STK_RVR 0xE000E014
#define STK_CVR 0xE000E018


static void led_init ( void )
{
    unsigned int ra;

    ra=GET32(RCC_AHB1ENR);
    ra|=1<<0; //enable GPIOA
    PUT32(RCC_AHB1ENR,ra);

    ra=GET32(GPIOA_MODER);
    ra&=~(3<<10); //PA5
    ra|=1<<10; //PA5
    PUT32(GPIOA_MODER,ra);

    ra=GET32(GPIOA_OTYPER);
    ra&=~(1<<5); //PA5
    PUT32(GPIOA_OTYPER,ra);

    ra=GET32(GPIOA_OSPEEDR);
    ra|=3<<10; //PA5
    PUT32(GPIOA_OSPEEDR,ra);
    //pupdr
    ra=GET32(GPIOA_PUPDR);
    ra&=~(3<<10); //PA5
    PUT32(GPIOA_PUPDR,ra);
}

static void led_on ( void )
{
    PUT32(GPIOA_BSRR,((1<<5)<<0));
}

static void led_off ( void )
{
    PUT32(GPIOA_BSRR,((1<<5)<<16));
}

void do_delay ( unsigned int sec )
{
    unsigned int ra,rb,rc,rd;

    rb=GET32(STK_CVR);
    for(rd=0;rd<sec;)
    {
        ra=GET32(STK_CVR);
        rc=(rb-ra)&0x00FFFFFF;
        if(rc>=16000000)
        {
            rb=ra;
            rd++;
        }
    }
}

int notmain ( void )
{
    unsigned int rx;

    led_init();

    PUT32(STK_CSR,0x00000004);
    PUT32(STK_RVR,0xFFFFFFFF);
    PUT32(STK_CSR,0x00000005);

    for(rx=0;rx<5;rx++)
    {
        led_on();
        while(1) if(GET32(STK_CVR)&0x200000) break;
        led_off();
        while(1) if((GET32(STK_CVR)&0x200000)==0) break;
    }

    while(1)
    {
        led_on();
        do_delay(10);
        led_off();
        do_delay(1);
    }
    return(0);
}

flash.ld

MEMORY
{
    rom : ORIGIN = 0x08000000, LENGTH = 0x1000
    ram : ORIGIN = 0x20000000, LENGTH = 0x1000
}

SECTIONS
{
    .text : { *(.text*) } > rom
    .rodata : { *(.rodata*) } > rom
    .bss : { *(.bss*) } > ram
}

then compile

arm-none-eabi-as --warn --fatal-warnings -mcpu=cortex-m4 flash.s -o flash.o
arm-none-eabi-gcc -Wall -Werror -O2 -nostdlib -nostartfiles -ffreestanding  -mthumb -mcpu=cortex-m4 -c notmain.c -o notmain.o
arm-none-eabi-ld -o notmain.elf -T flash.ld flash.o notmain.o
arm-none-eabi-objdump -D notmain.elf > notmain.list
arm-none-eabi-objcopy notmain.elf notmain.bin -O binary

can replace with arm-whatever-linux-gnueabi and will still build and run.

there are some toolchains that add extra stuff when they see main() so historically I personally avoid that. I dont use .data on mcus/etc that boot off of flash so dont have to copy that over (burns .text yes), and I dont assume .bss is zero I initialize things before I use them. So I can cheat on my bootstrap, but there are many examples of slightly more complicated linker scripts that give you bss offset and size and .data offset and size and destination to take this to the next level if you wish to have your C more pure. I also prefer to control the instruction used for loads and stores, some/many hope the compiler chooses correctly, have seen that fail. YMMV. so there is a lot of personal style, but take your stm32f11 document and look at those addresses and registers and even if you completely hate my code or style you should still see how easy it was to use that peripheral. Likewise the timers are in the arm docs. These days st being one of them some vendors have their own versions of the arm docs in a modified form so much of the arm info is covered but still some gaps.

As a general rule for arm figure out from the chip vendors documentation what arm core you have in your chip. Then go to arms site and find the technical reference manual (TRM) for that core (cortex-m4 in this case). then in that document arm mentions that is architecture armv7-m get the architectural reference manual for armv7-m. These three documents are your primary sources of information, after your first cortex-m4 you probably only need the chip vendor docs 99% of the time, certainly within a chip vendor. Also find the cpuid registers or chip id or whatever that doc calls it and compare it to what you read out of the chip/arm core. Sometimes there are different versions of the arm core (r1p3 meaning revision 1.3) and rare but happens a change between revisions means using the newest doc against an older core can result in subtle differences. again arm and arm based chips are improving/changing wayy faster than atmel avr based ones. After the first or second you get the hang of it...

If you were going to use a PIC32 for example it would be a similar story microchip for the pic32 docs, then off to mips for the core docs (and then wishing that microchip documented their peripherals even as half as good as atmel (which they now own), ti, st, nxp, etc). Another mixing of buy a processor core and wrap my own stuff around it. the pic32s are painful to program this way, really need libraries buried in the microchip toolchain, and use a ton more power, etc. no reason why mips based shouldnt be able to compete with arm based, but they dont...mips or chip vendors or a combination are at fault there.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Sorry Olaf did another teach them to fish rather than give them a fish. (although I did give a fish too). – old_timer Aug 24 '17 at 04:17
  • to add to this "how would you take one of these...". I would cut and paste my own spi controller or i2c code for that chip, and then from the chip docs for the HD44780 see what the protocol is and modify accordingly, then take the code from some repo where someone got it working as these lcd drivers tend to have some range of control and use/examine both the init order and the values used by this third party repo. May find that theirs is for a different pixel size display and wont work, might find that my contrast setting needs to be higher/lower, etc. but it is a starting point as – old_timer Aug 24 '17 at 04:22
  • ...the lcd driver docs tend to leave out good/working examples of init order and values and you can spend a long time guessing going straight from the docs. Normally you will need to tune and the third party libraries wont be able to contain all the panels made with that thing, just the ones the author has and tested against. – old_timer Aug 24 '17 at 04:24
  • with anything spi or i2c on any microcontroller I start with bit banging the gpio and NOT using the on chip peripheral, I figure out the external device/target (the lcd panel in this case) then I decide what to do from there. bit banging is extremely portable, the spi controllers in various mcus are not, they vary widely across chip vendors and sometimes within a vendor across families. because spi slaves and i2c slaves dont normally completely conform to a/the standards but sometimes use standards terms, hacking is involved, would be worse using a library esp if it tried to conform... – old_timer Aug 24 '17 at 04:27
  • This answer is...spectacular. Thank you very much! It's been two 4am nights in a row, and after reading this, I feel reinvigorated. Thank you, and I'll start at the bottom and head up with the reference documents instead of trying to cheat with the abstracted libraries. Thanks again! – testname123 Aug 24 '17 at 14:53
  • wow, okay thanks, though I was going to get flamed to death and have to delete it. that could still happen but if it helped you at all then that is all that matters, good luck...have fun. – old_timer Aug 24 '17 at 17:27
  • I've been reading the reference manual all morning...this is an overwhelming document, but seems like the right way to go...if it was easy, i'd already be good at it! :) – testname123 Aug 24 '17 at 17:46
  • new to you processor compared to the avr, the m4 is a bit beefy hard to call it a microcontroller with maybe floating point, maybe a cache, and other features. Can pretty much look past those things and just focus on enough to bootstrap then let the compiler worry about the rest just write C code. – old_timer Aug 24 '17 at 19:31