STM32F051 debug and program problem
Hello,
I am currently migrating from AVR to STM32 with STM32F051C8T6 on small project (PWM regulator with feedback from back EMF). First there was problem to force system workbench connect to MCU. The problem was in configuration file for debbuger/programer as I found on some forum (problem with NRST pin). Now I am able to program and “debug” via SW.
BUT, I have strange problem with jumping to functions/procedures. The code I programmed is included.
The problem is when I try to “step in to” via F5 on the row where InitializeTimer(1000) is, nothing happen. Nothing. It will not jump into InitializeTimer.
This also sometimes happen with LED() and I have no fucking idea what is wrong.
Also when I program flash memory with this program, it looks like it freezes on InitializeTimer(1000) - or sometimes on LED(). So it is probably not debug problem. Perhaps startup file is broken?
Please, can anybody help me? I am kind of out with ideas what to do.
- include “stm32f0xx.h”
- include “stm32f0xx_rcc.h”
- include “stm32f0xx_tim.h”
- include “stm32f0xx_gpio.h”
- include
- include “delay.h”
- define LED_PORT GPIOA
- define LED_STATUS GPIO_Pin_11
- define PWM_PIN GPIO_Pin_8
- define TXD_PIN GPIO_Pin_9
- define BTN_PORT GPIOB
- define BTN_START GPIO_Pin_7
volatile uint8_t motor_on = 0;
void Init(void);
void Setup_Init_Clocks();
void LED(uint8_t on);
void InitializeTimer(int period);
void InitializePWMChannel();
void LED(uint8_t on)
{
if (on > 0)
{
GPIO_SetBits(LED_PORT, LED_STATUS);
}
else {
GPIO_ResetBits(LED_PORT, LED_STATUS);
}
}
int main(void)
{
Setup_Init_Clocks();
Init();
LED(1);
InitializeTimer(1000);
InitializePWMChannel();
while(1)
{
}
return 1;
}
void Init(void)
{
RCC_ClocksTypeDef RCC_Clocks;
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA-B Periph clock enable */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
/* Configure
* PB7 USER Button input
* */
GPIO_InitStructure.GPIO_Pin = BTN_START;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(BTN_PORT, &GPIO_InitStructure);
/* Configure PA8, PA9 and PA11 in output pushpull mode
* PA11 = Blue LED
* */
GPIO_InitStructure.GPIO_Pin = LED_STATUS | PWM_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(LED_PORT, &GPIO_InitStructure);
/* SysTick end of count event each 1us */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000000);
}
void Setup_Init_Clocks()
{
// Set up 48 MHz Core Clock using HSI (8Mhz) with PLL x 6
RCC_PLLConfig(RCC_PLLSource_HSI, RCC_PLLMul_6);
RCC_PLLCmd(ENABLE);
// Wait for PLLRDY after enabling PLL.
while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) != SET)
{ }
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); // Select the PLL as clock source.
SystemCoreClockUpdate();
}
void InitializeTimer(int period)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
TIM_TimeBaseInitTypeDef timerInitStructure;
timerInitStructure.TIM_Prescaler = 16;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = period;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM1, &timerInitStructure);
TIM_Cmd(TIM1, ENABLE);
}
void InitializePWMChannel()
{
TIM_OCInitTypeDef outputChannelInit = {0};
outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
outputChannelInit.TIM_Pulse = 500;
outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(TIM1, &outputChannelInit);
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_2);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
void SetPWM(uint16_t pwm)
{
TIM_SetCompare1(TIM1, (uint32_t) pwm);
}
I also attached file after hitting F5 (step into InitializeTimer(1000)).