Author Topic: Want to get started with ARM stm32  (Read 29165 times)

0 Members and 1 Guest are viewing this topic.

Offline rwgast_lowlevellogicdesinTopic starter

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: us
    • LowLevel-LogicDesign
Want to get started with ARM stm32
« on: January 23, 2015, 02:03:54 am »
Ive been using the propeller microcontoller along with arduino and AVRs for about two years now. I would like to start learning arm, the chips are cheaper than AVRs and have beefy peripherals.

I found this book, http://www.cs.indiana.edu/~geobrown/book.pdf it seems to be a very good introduction to using stm32 micro controllers. The issue is the author uses an stm32 LV discovery board which has an stm32f1 on board. I currently have stm32f0, stm32L1, stmf3 discovery boards. Can I use any of these with this book? I.E is the f3 code compatible with the f1? Im hoping the only thing I would need to adapt to is the boards pinouts?

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Want to get started with ARM stm32
« Reply #1 on: January 23, 2015, 12:46:57 pm »
First of all, it is a very good book to get started.

Quote
Can I use any of these with this book?

It depends on your level of skills.

You definitely cannot directly use the examples - they rely on (slightly) different libraries.

However, the basic principles between the series of chips and libraries are highly similar so with minimum changes, you can indeed port most / all of the examples from one series to another series.

Whether you can do that or not is a total unknown to anyone but yourself.
================================
https://dannyelectronics.wordpress.com/
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4064
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Want to get started with ARM stm32
« Reply #2 on: January 23, 2015, 02:27:36 pm »
ST put effort in keeping their products very similar. Reducing the effort required to change code and hardware to a better chip.

For books on ARM I can recommend "The Definitive Guide to ARM Cortex...".
 

Offline neslekkim

  • Super Contributor
  • ***
  • Posts: 1305
  • Country: no
Re: Want to get started with ARM stm32
« Reply #3 on: January 23, 2015, 02:48:04 pm »
For books on ARM I can recommend "The Definitive Guide to ARM Cortex...".

I think there are two books in this series, are both equally good?
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Want to get started with ARM stm32
« Reply #4 on: January 23, 2015, 03:04:10 pm »
There are a few, from M0 to M4, and some with multiple revisions.

But they are not useful (at all?) for a practitioner's point of view. When you learn a chip, the biggest distinguisher is the peripherals, which differ vastly from chip to chip, and vendor to vendor. The books by Yue cover none of that.

What the books do teach is largely transparent for a high level language programmer, save for debugging, etc.

End of the day, the datasheets, and if you use libraries, the library documents / source code, are the best learning materials for any chip.
================================
https://dannyelectronics.wordpress.com/
 

Offline neslekkim

  • Super Contributor
  • ***
  • Posts: 1305
  • Country: no
Re: Want to get started with ARM stm32
« Reply #5 on: January 24, 2015, 12:37:01 am »
So, learning from M0-M3-M4 these books are not useful?

For peripherals I understand that the books don't cover it, since it differs from implementors, but I was more curious about the Mx cores.
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3891
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Want to get started with ARM stm32
« Reply #6 on: January 24, 2015, 12:49:12 am »
These books sure are useful.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Want to get started with ARM stm32
« Reply #7 on: January 24, 2015, 01:08:56 am »
Quote
So, learning from M0-M3-M4 these books are not useful?

It is definitely useful, just not sufficiently useful to justify the investment to learn it, to me.

There are far more things that we do NOT learn than things we do learn. That does not mean those things we don't learn are not useful.

Quote
I was more curious about the Mx cores.

Then get those books - they are great for that purpose.

For learning to use a particular chip, not so much.
================================
https://dannyelectronics.wordpress.com/
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4192
  • Country: us
Re: Want to get started with ARM stm32
« Reply #8 on: January 24, 2015, 06:54:38 am »
Quote
they rely on (slightly) different libraries.
Libraries that have been replaced by ST with a NEW set of libraries :-(
(The book uses the ST Standard Peripheral Library.   The new thing us the "ST Cube HAL libraries.")
In theory, I believe the new HAL libraries are supposed to have fewer differences from one family to another.  It's hard to tell; the documentation is pretty sucky.

I'm not convinced that the libraries are easier to use than writing low-level C yourself, based on the datasheets.  Especially if the libraries keep changing, aren't anywhere near the same on chips from different vendors (they all have "easy to use libraries"; all different!), and have a substantial level of complexity and verbosity.  :-(

If you want a set of libraries to do basic embedded things like pin-twiddling, and want it to work across multiple chip families from multiple vendors, it may be that your only choice is Arduino.  (which is depressing.)  Possible contender: MBed (I haven't looked at it much.)
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3891
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Want to get started with ARM stm32
« Reply #9 on: January 24, 2015, 11:20:14 am »
dannyf: I can't tell, because I don't know you, but if I want to program a chip, it is a good start to learn about the architecture first.

Judging by rwgast_lowlevellogicdesin's nickname, the book is right for him.  :D

If he needs only continuing arduino style slapping libraries together, then the book is useless.

Two ways:
a) learn ARM properly. From the architecture, through assembly to the top level of RT systems.
With STM32 I'd suggest sticking with StdPeriph libraries. They are supported so far, but new STM32 series will not have them, only Cube/HAL. Shame, these HAL libraries was meant good, made some things easier to implement for beginners, but are useless for anything little more advanced, than using example codes. The HAL libraries are slow, sometimes badly designed and have almost zero support for low-power applications, even on the STM32L series. User needs to modify the libraries and spends a lot of time with that, I think sometimes more than it would take writing the thing from scratch yourself.

b) Just toying style, slapping libraries together. Then the HAL has some potential, so does that MBED nonsense.

Just a related note:
StdPeriph exists for these: F0, F1, F2, F4, partly F3 (not for the new ones like 334), L1. Doesn't exist fort L0 and L4. Probably also will not exist for F7.
HAL exists for all of them, for some, STM32Cube support is nowhere near.
 

Offline russian2

  • Contributor
  • Posts: 18
Re: Want to get started with ARM stm32
« Reply #10 on: January 24, 2015, 01:36:00 pm »
is the f3 code compatible with the f1? Im hoping the only thing I would need to adapt to is the boards pinouts?
f1 is defenitely not the same thing as f3. With all this weird peripheral register magic, I suggest you invest in the same exact chip/board the book is describing.

I also want to suggest ChibiOS - the magic of ChibiOS is HAL, hardware abstract layer - which hides all the register insanity behind a nice driver API
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Want to get started with ARM stm32
« Reply #11 on: January 24, 2015, 01:53:31 pm »
Quote
which hides all the register insanity behind a nice driver API

You can simply use the libraries to do that.
================================
https://dannyelectronics.wordpress.com/
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7673
  • Country: de
  • A qualified hobbyist ;)
Re: Want to get started with ARM stm32
« Reply #12 on: January 24, 2015, 01:56:05 pm »
I've also just started with Cortex-M and settting up a tool chain under linux at the moment. From what I've already learned there are two abstraction layers available. The basic one is CMSIS to set up the MCU (clock etc.) and it seems to be some standard, available from other vendors too. The other one is the HAL to hide the hardware, somewhat similar to arduino. I'm sure I won't use the HAL, but I'm thinking about using CMSIS. I would like to know from someone already done several ARM based projects about CMSIS. Is it useful?
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3891
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Want to get started with ARM stm32
« Reply #13 on: January 24, 2015, 02:10:28 pm »
Crap or crap, thats the choice.  :D If you need to hide hardware and are afraid of learning the basics, then it is doesn't matter, what will you use.

Good designer does know about the architecture, from the low level to the top and can combine or make a right choice depending on the requirements of things he is doing.

There is no "what is better, HAL or CMSIS", each are good for something.  I personally prefer (on STM32) using StdPeriph combined with direct access to registers, sometimes with inline assembly.

For example: I don't know, why would I use some crap hundred-line code HAL_GPIO_TogglePin() whoknowswhat-it-is-doing function, if it is right enough, to just XOR an GPIOx->ODR register?
 

Offline madires

  • Super Contributor
  • ***
  • Posts: 7673
  • Country: de
  • A qualified hobbyist ;)
Re: Want to get started with ARM stm32
« Reply #14 on: January 24, 2015, 04:23:35 pm »
I'm with you, I don't like a HAL_GPIO_TogglePin() either. What I meant was CMSIS CORE, which offers a standardized way to initialize the MCU. It also supports a nice way to access the registers (by a C structure). That's quite convenient if you want to support different Cortex-M types. Of course you could go for lovely #ifdef orgies ;) I've just started to look into the STM32 and trying to figure out the best way for me.
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3891
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Want to get started with ARM stm32
« Reply #15 on: January 24, 2015, 05:10:41 pm »
Initializing the MCU clock and memory system, is about 20-30 lines of assembly, or 5 to 10 lines in C, directly accessing the registers or about 5 commands by a StdPeriph library. Means for the simplier devices, F439 like-ones are a bit complicated, but nothing impossible to do "manually".

Why the hell would somene like to use standardized CMSIS? It only adds ballast and unnecessary overhead for the intended functionality.

What is so complicated on setting up an ARM Cortex MCU?  Only set the desired FLASH wait states (number found in a documentation) - that means single write to a register, start needed clock sources (eg external crystal oscillator, which is called HSE //high speed external// on STM32), wait for stabilization of the clock source - flipping and polling a single bit. Setup the PLL (if needed) clock source, divide and multiply ratios, start PLL and wait for its lock. Then switch the system clock source t the PLL output and check the success.

Sounded difficult? If yes, programming ARM MCU's wasn't a good choice then. There is hell of a lot much more complicated stuff, then setting a clock source and distribution system on the chip. And its cost is only to open the manual (which you must do if you want or not) and learn, how the clock system works. Otherwise it is better to choose simpler MCU, than ARM.

Here a small minimalistic example how to setup F051 via registers, also with a SysTick timer. Using registers.

Code: [Select]
void SystemInit() {

RCC->CFGR = 0x00280000;  //0b0000 0000 0010 1000  0000 0000 0000 0000
RCC->CR   |= RCC_CR_PLLON;                //set PLL ON
while (! RCC->CR2 & RCC_CR_PLLRDY);       //wait for PLL to lock

FLASH->ACR = FLASH_ACR_PRFTBE | 0x01;     //FLASH prefetch enabled, 1 WaitState

RCC->CFGR |=  RCC_CFGR_SW_1;              //switching clock source to PLL output 

//SysTick configuration
SysTick->LOAD = 48000-1; //Reload value (48000-1) -> 1ms
SysTick->VAL  = 0;
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;
NVIC_EnableIRQ(SysTick_IRQn);
}


Yes, it is not as transparent and not done absolutely properly, but that was my absolute first try on ARM. And after a one or two afternoons, it worked.
If someone tells you, that for a beginner, this is way too hard...  He lies.  And note that the MCU also works with the clock system in its default state. Will be slow, won't be ideal (arduino is not ideal at all), so where's the problem to start that way?

And that's an proper init of F103, using StdPeriph (again, no timeouts or error handling, but for simple educational purposes)

Code: [Select]
void SystemInit()
{
  /* Set 2 wait states for FLASH */
FLASH_SetLatency(FLASH_Latency_2);
  /* enable prefetch buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

/* start HSI (internal high speed oscillator), it should be running default, but to be sure */
RCC_HSICmd(ENABLE);
/* wait for the oscillator to start */
while (!RCC_GetFlagStatus(RCC_FLAG_HSIRDY));

/* Set the clock source as HSI/2 for the PLL, multiply by 16 */
RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_16);
/* Enable the PLL */
RCC_PLLCmd(ENABLE);
/* Wait for PLL to lock */
while (!RCC_GetFlagStatus(RCC_FLAG_PLLRDY));

              /* Set the prescalers for the AHB and APB buses */
              RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB = 64MHz
RCC_PCLK1Config(RCC_HCLK_Div2);  //APB1 = 32MHz
RCC_PCLK2Config(RCC_HCLK_Div1);  //APB2 = 64MHz

/* now we switch system clock to the PLL's output */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* check it is switched to the requested source */
while (RCC_GetSYSCLKSource() != 0x08);
}

Now you've seen some code examples, how simple the basic init is. I don't see there any need to rely on 3rd party libraries.

Now I expect someone starts bitching me out, that I am too much vintage style or I am not using libraries, or I hate multiplatform-stuff, etc... I get it, but he will not change my opinion. If he wants to use libraries, especially HAL, good luck using them (respectively hacking them to be usable) for advanced applications or to even fit devices without megabytes of flash.

And to be exact, the init in the HAL is also made quite wrongly. Sometimes, HAL just tries to start LSE, if there is not even connected any crystal, or starts it, then shuts it down, so sometimes it can take a few seconds to start the MCU.

So you can make a decision yourself, which way will you go. I don't want to obtrude anybody, to learn anything... So shouldn't you tell me that this knowledge is useless. (It is useful as you wouldn't beleive maybe)

As a standard "it depends"... on many things. Hope my little insight into this topic was helpful for you.

//So what kind of application will you do? STM32 only or must be easily portable to other vendors' chips? - These and like so are the questions you must ask yourself. Based on that you can make a decision, what is suitable for you.

//By the way - if you are brave enough to throw away HAL, I woul'd suggest doing the same with CMSIS. :D
« Last Edit: January 24, 2015, 05:38:51 pm by Yansi »
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4064
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Want to get started with ARM stm32
« Reply #16 on: January 24, 2015, 09:24:32 pm »
The above two code fragments are usually called even before main from within the startup assembly. (for when you cannod find them in the examples )
This is done to speed up the scatter loading process the compiler inserts before main() and after the startup assembly. In this scatter loading code the default values are copied from flash to sram for global scope variables that use them.

The realview compiler does it this way, I'm not sure about gcc but it wouldn't be strange if it worked similar.
Running the above two fragments inside main will also work, but you'll have slower boot.

CMSIS is ideal for accesring peripherals that are standardised on all ARM cores, such as the systick, nvic, debug and the core itself.
They also provide math and dsp code optimised for ARM for free.  ;)
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3891
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Want to get started with ARM stm32
« Reply #17 on: January 24, 2015, 11:05:48 pm »
Where did I say to run them in main?   >:(

Call them from the startup. A little knowledge required, so what I told ya about the Yiu's book?  >:D

Still I get that, any knowledge additional to so called "libraries" is useless (for most people).  ;D

Code: [Select]
;Reset handler
Reset_Handler    PROC
        EXPORT  Reset_Handler             [WEAK]
        IMPORT  __main
        IMPORT  SystemInit
        LDR     R0, =SystemInit
        BLX     R0
        LDR     R0, =__main
        BX      R0
        ENDP

You can modify the startup code as your wishes go. Also you can simply combine assembly and C modules, because of the clever IMPORT/EXPORT functionality.

Yea, the math and dsp code is the only thing what the CMSIS is really good for.
« Last Edit: January 24, 2015, 11:10:06 pm by Yansi »
 

Offline rwgast_lowlevellogicdesinTopic starter

  • Frequent Contributor
  • **
  • Posts: 591
  • Country: us
    • LowLevel-LogicDesign
Re: Want to get started with ARM stm32
« Reply #18 on: January 25, 2015, 01:59:36 am »
Well thanks for this info, I guess I will definitely buy the VL discovery board. What I want is to learn how to use an ARM chip at the lower level, I.E using C and bit twiddling registers, Im not opposed to using something like a HAL but then thats not really learning arm chips, its implementing a high level library and programming in C.

What I guess im asking is if stm32 is like the Atmega line where you have standard librarys and methods of doing things, once you learn those switching between chips in the line is as a quick look at the data sheet and adjusting your code for the register names and pin numbers. It seems to me switching between arm chip vendors is not an easy thing and you have to learn a whole new chip landscape to use a different vendors peripherals. Almost as complicated as switching from AVR to 8 bit PIC.

I like the STM chips but the more I read the more convoluted im starting to think the line is... maybe it is as simple as an Atmega and people are just whiners. What im looking for is for a vendor of arm chips that makes it easy to just choose a chip with the peripherals you need and start programming it, i.e. i guess where the standard libraries are the same across most or all of the same chips, not necessarily register names and pin out though.

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3891
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Want to get started with ARM stm32
« Reply #19 on: January 25, 2015, 03:14:32 am »
"I want is to learn how to use an ARM chip at the lower level"

Nicely done, I appreciate that.


If you'd like to compare ARMs ant AVRs, I can partly tell ya, that an Cortex M0 (STM32F0) series is "like atmega", but 32bits adult only edition.  ;D With Cortex M3 or M4 or now the new M7, things start getting complicated more and more.

If you really want to take ARMs from the basic, I'd suggest staaring with an M0 and go for bigger then. A good choice is STM32F0-DISCOVERY. There is an Cortex M0, STM32F051RB.  Or the F0 VL (there is an F030 chip, simillar to F051, but a little bit stripped). Then I'd suggest visiting F1 series, with an F103 NUCLEO. And then whatever you want (again my suggestion is 32F429I-DISCOVERY )

Quote
What I guess im asking is if stm32 is like the Atmega line where you have standard librarys and methods of doing things, once you learn those switching between chips in the line is as a quick look at the data sheet and adjusting your code for the register names and pin numbers. It seems to me switching between arm chip vendors is not an easy thing and you have to learn a whole new chip landscape to use a different vendors peripherals. Almost as complicated as switching from AVR to 8 bit PIC.

If you mean old AVRs (yes, now almost obsolete) atmegas and attinys, there are big differences from chip to chip. Especially microchip is an expert for having each 8bit chip different.
The advantage of ST's stuff is that we keep the series of STM32 compatible as much as possible, so it is easy to switch between chips or sometimes between series. For example, if you have TIMER1, you can be almost sure, it  is an advanced control timer and will have same functionality in a serie and almost same between series. I admit, that the documentations isn't always as good, but you will quickly learn how to orient in the documentation (datasheet, reference and programming manuals)
Going from vendor to vendor is a big leap every time... But once started, fear dissapears.

Quote
maybe it is as simple as an Atmega and people are just whiners.
Sometimes I think the same, when seeing different sort of arduino style slap-together masterpieces.

Yes, StdPeriph library is almost the same on every series. Sometimes only modified a little or with added functionality to fit the exact specifics of that chip or series. Unlike HAL, which tries to be standard as a law, and then must be bent with much effort to meet the specifics. And some functionality is not even implemented in HAL, which is getting designers angry, bending the HAL libraries yourself is not so easy, HAL is sometimes just overcomplicated.

Most of the peripheral types are shared across the MCU families. So are the register names. If  you learn them on F0, most of them will be similar on any STM32.

Good you have mentioned the pinouts. They are compatible on most chips with the same package size. So you can just take F407 discovery boad and swap to chip for f205 and make a custom discovery board that way (because F2-discovery does not exist). An the F2 and F4 series are similar in many things.

I will add som more info "tomorrow", now I'll tak a nap, 4:14AM the clock told me. Crazy. ;D
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4192
  • Country: us
Re: Want to get started with ARM stm32
« Reply #20 on: January 25, 2015, 05:08:22 am »
Quote
Cortex M0 (STM32F0) series is "like atmega", but 32bits adult only edition.  With Cortex M3 or M4 or now the new M7, things start getting complicated more and more.
How do you figure that?  Complexity (especially with C) goes up with the peripheral set, rather than with the core.  It's true that the "faster" cores tend to come on the same chips with more and more complex peripherals, but that's just a coincidence.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Want to get started with ARM stm32
« Reply #21 on: January 25, 2015, 01:50:56 pm »
Quote
maybe it is as simple as an Atmega

Wait to be shocked, :)

Pick a datasheet and read it yourself to see what's involved. No need to guess.
================================
https://dannyelectronics.wordpress.com/
 

Offline Yansi

  • Super Contributor
  • ***
  • Posts: 3891
  • Country: 00
  • STM32, STM8, AVR, 8051
Re: Want to get started with ARM stm32
« Reply #22 on: January 25, 2015, 03:09:44 pm »
Here's the shocker, try urself  ;D  (for a STM32F051 from the F0-DISCOVERY.. it is a basic, entry level ARM MCU)

Datasheet: http://www.st.com/st-web-ui/static/active/en/resource/technical/document/datasheet/DM00039193.pdf
Reference manual: http://www.st.com/web/en/resource/technical/document/reference_manual/DM00031936.pdf
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4064
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Want to get started with ARM stm32
« Reply #23 on: January 25, 2015, 03:57:35 pm »
With most vendors, the peripherals are just bought drop-in blocks of HDL. As is the Core itself.
They work the same registers and they have the same address offsets. However, some vendors tend to make a mess of the header files translating these address offsets into usable C names across the product families.
 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Want to get started with ARM stm32
« Reply #24 on: January 25, 2015, 04:18:08 pm »
Quote
They work the same registers and they have the same address offsets.

You probably want to check a few datasheets to be sure.

Those peripherals are vastly different from vendor to vendor, and sometimes from family to family and chip to chip.
================================
https://dannyelectronics.wordpress.com/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf