Hi,
I'm using the STM32F103ZE uC on the MCBSTM32E development board. I tried the example programs, they all compile and download and run fine, but they are NOT SIMPLE. They use a lot of Library files etc, etc, etc. I just try to make some ports go high or low.
The example GPIO program states: - Enable the clock signal for the GPIO. - Configure the Alternate Function to use a GPIO (usually standard after reset). - Configure the GPIO pins as input or output. - Set remaining parameters like speed, pull-up/down. - Enable the GPIO. - Read from or write to the GPIO.
Can someone give me a simple (and I mean REALLY SIMPLE) program to control just one single GPIO port?
My questions to activate some port: - do I need something to know about internal clock signals? - do I need some startup file?
Thanks,
Henk
Don't forget to get a copy of UM0427.PDF User Manual "ARM®-based 32-bit MCU STM32F101xx and STM32F103xx firmware library" from STMicro for the Library function definitions.
--Cpt. Vince Foster 2nd Cannon Place Fort Marcy Park, VA
Oh, yeah... you'll need this too..
#define PA_IDR ( GPIO_MAP_PA + GPIOx_OFFSET_IDR ) // #define PB_IDR ( GPIO_MAP_PB + GPIOx_OFFSET_IDR ) // #define PC_IDR ( GPIO_MAP_PC + GPIOx_OFFSET_IDR ) // #define PD_IDR ( GPIO_MAP_PD + GPIOx_OFFSET_IDR ) // #define PE_IDR ( GPIO_MAP_PE + GPIOx_OFFSET_IDR ) // #define PF_IDR ( GPIO_MAP_PF + GPIOx_OFFSET_IDR ) // #define PG_IDR ( GPIO_MAP_PG + GPIOx_OFFSET_IDR ) //
Thanks for all the info, I studied for 4 hours on all header files and now it's clear to me and I made a REALLY SIMPLE program and: IT WORKS!!!!!!
The real problem was the GPIO clock that should be switched on. I think I'm an expert now. ;-)
//------------------------------------------------------------ // // Very simpe program to control the 8 PB08-PB15 Leds on the // Keil MCBSTM32E board using a STM32F103ZE micro controller. // // This program will show a toggling 0xAAAA / 0x5555 pattern // on the onboard Leds appr. each 300ms. // // Copyright! Non of this shall be reproduced in any way! ( ;-) ) // // 2010.08.06 // Henk van Winkoop // The Netherlands // // Note: only lowercase names are used to avoid YELLING. // //------------------------------------------------------------ //============================================================ // MAIN //============================================================ int main(void){ //------------------------------ // declare variables //------------------------------ //CRN register for input output settings int *crh; //ODR register for output data int *odr; //APB2ENR register for clock selection int *apb2enr; //delay counter variable int i; //------------------------------ // assign addresses to pointers //------------------------------ //define the address of crh register crh=(int*)(0x40010C04); //define the address of odr register odr=(int*)(0x40010C0C); //define the the address of the apb2enr register apb2enr=(int*)0x40021018; //------------------------------ // init the micro controller //------------------------------ //enable the GPIO clock for port GPIOB *apb2enr|=0x0008; //set all PB08-PB15 ports as output *crh=0x33333333; //------------------------------ // main loop //------------------------------ //forever do... for(;;){ //roughly wait 300ms for(i=0;i<0x40000;i++){ }//for //switch only all even-numbered Leds on *odr=0x0000aaaa; //roughly wait 300ms for(i=0;i<0x40000;i++){ }//for //switch only all odd-numbered Leds on *odr=0x00005555; }//for }//main
Thanks for listening folks!
Me again,
I forgot to tell:
the only additional file needed is: STM32F10x.s
Well, now that I'm on 'steam':
I've modified my program so that is runs using the official added STMicroelectronics header files. Then this program gets even smaller.
I hope other newbies will appreciate this.
All you need (except love) is: - the GPIO header file - the startup file - init the GPIO clock - set the GPIO port to Output - write data to the port
So this is the real answer I was looking for. . .
//------------------------------------------------------------ // // Very simpe program to control the 8 PB08-PB15 Leds on the // Keil MCBSTM32E board using a STM32F103ZE micro controller. // // This program will show a toggling 0xAAAA / 0x5555 pattern // on the onboard Leds appr. each 300ms. // // Copyright! Non of this shall be reproduced in any way! ( ;-) ) // // 2010.08.06 // Henk van Winkoop // The Netherlands // // The only additional files needed is: STM32F10x.s // // This version uses the STMicroelectronics manufacturer // setup header files. // //------------------------------------------------------------ //include the file that defines all kind of GPIO stuff #include <stm32f10x_gpio.h> //============================================================ // MAIN //============================================================ int main(void){ //------------------------------ // declare variables //------------------------------ //delay counter variable int i; //------------------------------ // init the micro controller //------------------------------ //enable the GPIO clock for port GPIOB RCC->APB2ENR|=0x0008; //set all PB08-PB15 ports as output GPIOB->CRH=0x33333333; //------------------------------ // main loop //------------------------------ //forever do... for(;;){ //roughly wait 300ms for(i=0;i<0x40000;i++){ }//for //switch only all even-numbered Leds on GPIOB->ODR=0x0000aaaa; //roughly wait 300ms for(i=0;i<0x40000;i++){ }//for //switch only all odd-numbered Leds on GPIOB->ODR=0x00005555; }//for }//main
Thanks for listening...
How long will your delay take if you try to change the optimization level?
Might there be a problem?
Is there any good way to overcome this problem?
as far as I know you should add some assembler nop() instruction inside a 'for' loop to be sure this code is not optimized into the recycle bin. This is the way to do it with 8051 or Atmel AVR uC's so I assume Cortex will have some identical code.
www.8052.com/.../162556
A nop() only adds a delay if the compiler knows that the instruction time of the nop() is an important side effect. If the compiler doesn't, then the nop is "nothing" and can be removed.
Besides - even if you do have a nop instruction in the loop, the optimization level will change the number of machine cycles neede for the loop primitives which means that the total time will change depending on what code that is generated for the loop.
A nop() can be good when you need sub-microsecond delays. Maybe get 20ns extra delay between activating two GPIO signals (under the assumption that the compiler knows and honors the "side effect" of the nop).
Whenever you want longer delays, you should look into the timer system. Either busy-looping while waiting for a timer register to reach a certain value, or do something else while waiting for the timer to generate an interrupt.
The only time you may consider software delay loops, is if you write the delay loop in assembler. And then you better avoid trying to inline that assembler inside a C function - write the delay() function fully in assembler.
The PDF does not exist any more!
:-(
See: my.st.com/.../Flat.aspx
no UM0427.PDF ?? Oh no! >:| --Cpt. Vince Foster 2nd Cannon Place Fort Marcy Park, VA
Well, then gleen the data from:
www.st.com/.../familiesdocs-110.html
P.S. Is there an easy site to post the .PDF file that I have? Just curious.
It's all in the helpfile (.chm) included in the download - the point was that it's no longer available as a .pdf
Andy,
Thanks for the info. Now I've got to incorporate the *new* libraries. Oh, I'm so looking forward to that.
CMSIS (Cortex Microcontroller Software Interface Standard) info: http://www.onarm.com/
P.S. I got to develop the prototype of this puppy "SDB" (Small Diameter Bomb)... media.militaryphotos.net/.../adr.mpeg (Yes, I'm bored and it is a cool video: Andy, I emailed you the photo of me & the SDB a while ago)
I can't seem to get "stm32f10x_stdperiph_lib_um.chm" working. I'm not familiar with the help 'books' within uVision.
I tried adding it to the 'books' but that didn't work. Any suggestions?
Yes, I'm trying to utilize STMicro's "new" StdPeriph_Lib_V3.30 libraries, and the help files -- I'm just guessing -- will come in handy.
I can't seem to get "stm32f10x_stdperiph_lib_um.chm" working.
It turns out that on Windows7, the .chm file needs to be in a directory without spaces... I put it in the root directory":
C:\stm32f10x_stdperiph_lib_um.chm
And now it works. (whereas it wasn't working in other locations on my C drive. Of course, I made a short-cut to the help file(s) from ST-Microelectronics).
I thought that others using the STM32 series who might have had an 'issue' with this might benefit from this tid-bit of information.