I am an advanced arduino user and for most functions I write my own code, directly acessing the peripheral registers, for example to configure timers for a specific application.
STM32 is great, but I have no idea how to perform such tasks. Are there any code examples? Say for example I want to create a 1Second delay using one of the timers, where would I start from?
Regards
http://static.leaflabs.com/pub/leaflabs … index.html
EDIT: and this one: http://static.leaflabs.com/pub/leaflabs … /apis.html
I am an advanced arduino user and for most functions I write my own code, directly acessing the peripheral registers, for example to configure timers for a specific application.
STM32 is great, but I have no idea how to perform such tasks. Are there any code examples? Say for example I want to create a 1Second delay using one of the timers, where would I start from?
Regards
STM32 is great, but I have no idea how to perform such tasks. Are there any code examples? Say for example I want to create a 1Second delay using one of the timers, where would I start from?
Regards
https://www.google.com/#q=University+stm32
Ray
You should start with downloading and reading the stm32f103 datasheet, that will give you all the details on how peripherals work, and show all the registers with all the functions.
Most register addresses and bits are defined in libmaple, but there aren’t functions to do everything the peripherals can do.
I.e. for the timers there are functions to use them for PWM, and as simple counters with interrupts, but the timers have several input modes for which there is no function in libmaple.
Is there any example for someting as simple as setting a led to blink using an interupt? I’m sure I could go from there (at the moment I have no idea how to call an interrupt on the stm32).
Thanks.
They look like the use the Standard Peripheral Lib, which unfortunately can’t be used on open source projects due to the restrictive license that STM imposed on it.
It is possible to use the new HAL based SPL,as it has a different license. but the code is a bit different.
But the current core doesnt use the HAL as it was released well after the core had already been written, and it would take a complete rewrite to use the HAL.
@sheepdoll has written a new core that uses the HAL but its purely experimental at the moment.
I’m not sure it really answers the OPs question, as it doesn’t show how to directly control the processor registers for the timers.
IMHO, you really don’t want to start with the timers as an introduction to STM32 hardware programming, the interrupts are a separate subsystem as documented in the NVIC section of the manual, and and quite complex in their own right. The Timers are the easy bit. e.g. setting up the dividers and the counters etc
https://chromium.googlesource.com/chrom … chip/stm32
License:
// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
https://chromium.googlesource.com/chrom … er/LICENSE
-rick
The license seems to prohibit distribution of the SPL source, so unless the core was a recompiled binary I’m not sure how it could be distributed.
I suspect that it probably was not STM’s intention to prevent Open Source use, but it just makes it very hard to have something which requires you to download a key element from a commercial organization who could decide at any time to no longer make that download possible
We presume this way LeafLabs chose not to use the SPL as I think it was available back in 2012 when they were developing libmaple – I presume it was too much of a commercial risk for them to use it.
The HAL on the other hand doesnt have this limitation – however @sheepdoll has found that the linker files produced by the CubeMX have a problematic license. Again I doubt this is on purpose, I think STM don’t realize they have still have some files with this.
But in terms of using the HAL, its probably OK, as I found a replacement for the F103 linker script, that doesn’t have a license attached at all, (but I still need to find one for the F4 etc)
For the F4:
http://visualgdb.com/tutorials/arm/stm32/timers/
This works fine if I was to use the tools for the discovery as the website explains. Unfortunately the include files are not present with the stm32duino/maple port. As Roger mentioned, there are possibly some legal issues behind that.
Going back to the F4 example posted here http://visualgdb.com/tutorials/arm/stm32/timers/, I have previously used similar tools for a discovery board with a stm32 and surelly can find many more using google. No issues here. The issue is achieve the same using the arduino IDE. I havent quite got there, so I am looking at a worked example of some kind?
In steps this would look something like setting up the prescaller, clock selection, master/slave modes for the timer, outputs to toggle, enabling the timer, etc…
The prescaller is located on the TIMxPSC register (DIV/1…65536), timer enable is bit 0 of TIMx_CR1 resgister, etc, etc…
When trying to set, for example TIM1_CNT = 65535 for the prescaller or TIM1_CR1 |= (1<<CEN); to enable the timer the compiler does not recognize the expresions. So I have the idea there is an include file I should perhaps add, but have no idea which one it is… Any advice?
TIM1_CNT
in the headers, then AFIK the answer is no.
The names of registers in the manual, are just internal names in the manual.
Most generic examples use the SPl e.g like posted here
http://electronics.stackexchange.com/qu … -for-timer
But the SPL uses complete different defines.
But you cant use SPL code examples, because libmaple is not written for the SPL ( because of the license on the old SPL).
So you will need to define you own values for the things that are missing, or you will need to look in libmaple core to see what names leaflabs gave to these things.
its generally best to look at the existing code that has been written my leaflabs e.g. to control the hardware timers, and then base you code on that code
Say for example, when programming an atmel I refer to TCNTx for the timer register. Somewhat the compiler links this to a memory location so I don’t think it would be too hard to create a define file with all the variables for the atm32f103. Once its done for one timer the rest is pretty straightforward and would open a few extra possibilities
I’ll try and have a look at the atmel files to see if I have an idea, but any feedback would be welcome
…
I wonder how the maple team designed the pwm functions for example as I find no source files to set the output compare registers?
As I once again mention I’m not too bothered for the time being with direct port manipulation for the I/O’s. I am concerned about timer functions not covered on the leaf lafbs maple such as dead time insertion for half bridges and other custom timer configurations, given I cant just use the same code as provided by ST, so I’m trying to get my head round on how this could be written for the maple.
Respectfully I haven’t found much relevance in the examples provided, as you mentioned yourself these can be easily found on Google and do not solve my problem. For me it is as simple as migrating back to the discovery if it can’t be done, by far is my intention to wind people up…
As I once again mention I’m not too bothered for the time being with direct port manipulation for the I/O’s. I am concerned about timer functions not covered on the leaf lafbs maple such as dead time insertion for half bridges and other custom timer configurations, given I cant just use the same code as provided by ST, so I’m trying to get my head round on how this could be written for the maple.
It may be worth registering on STM’s own community forums, as they may be able to help you with these low level STM32 questions (however they may say you should use the HAL or SPL)
https://my.st.com/public/STe2ecommuniti … Items.aspx
One other place you could try posting to is the old LeafLabs forums . http://forums.leaflabs.com/
It is partially active, and the moderator is quite knowledgeable and may be able to refer you to some old examples of how to access the hardware
I’m looking to do the same thing, but i don’t know how to make it working.
Assuming all registers are not declared (is that right?) i supposed to do like that:
#define TIM2_BASE 0x40000000 //save the initial timer2 register address
#define TIM2_CNT TIM2_BASE + 0x24 //save the count2 register address adding the offsetAfter one day spent on searching, reading and testing
i discovered that the hardware registers are already defined,
i found them declared inside some scattered files.
Hoping to be usefull to someone else in the future,
this is the way i accessed to Timers registers:
(TIMER1->regs).gen->CR1 = 0x00;

![[Pending Enhancement] RTC values resetting](https://sparklogic.ru/wp-content/uploads/2019/11/nucleo-l476rg-zestaw-startowy-z-mikrokontrolerem-z-rodziny-stm32-stm32l476-90x90.jpg)
