AnsweredAssumed Answered

Startup file and interrupts

Question asked by chanov.andrey on Sep 14, 2013
Latest reply on Sep 15, 2013 by chanov.andrey
Hi All,

The problem: I've included a startup file in the project from CMSIS library and now enabling interrupts sends MCU directly into hardfault.

More info:
I'm using STM32VL-Discovery with IAR (which hanged when I clicked About->Product info). So, IWARM 6.50.6.4958. I also use Standart Peripherial Library v. 3.5.0 (April 2011). I have a very simple project that fails to do very simple things. First, I have TIM3 running in PWM mode with Update interrupts. I can see PWM on a pin, but if I enable interrupts, on the next step MCU goes to HardFault.
The startup file I use is Libraries\CMSIS\CM3\DeviceSupport\ST\STM32F10x\startup\iar\startup_stm32f10x_md_vl.s
There's another problem, however I'm not sure it has the same origin. But most probably it does. I cannot control pins. In other words, I can output a PWM signal on pins, but I cannot make them HIGH/LOW using neither GPIO_SetBits, GPIO_WiteBits nor GPIOC->BSRR = GPIO_Pin_8; I know the pin is operational because I can remap TIM3 to send CCP3  to that pin and I can see the waveform. And the LED is lit at 50% PWM as I set it to. But if I try to just SetBit - no luck...
I blame the startup file for everything. I used to have that problem in another project and I remember I solved it by just killing the startup file from the project. But this time it doesn't work.

01.#include "stm32f10x_conf.h"
02.#include "stm32f10x.h"
03.//#include "stm32f10x_gpio.h"
04.#include "hw_config.h"
05.//#include "stm32f10x_it.h"
06. 
07.void delay(uint16_t);
08. 
09.uint8_t sine_idx = 0;
10.const uint16_t sine_values[SINE_SAMPLE_SET] = {400, 413, 427, 441, 455, 469, 483, 496, 510, 523, 536, 549, 562, 575, 587, 600, 611, 623, 635, 646, 657, 667, 677, 687, 697, 706, 715, 723, 731, 739, 746, 753, 759, 765, 770, 775, 780, 784, 788, 791, 793, 796, 797, 799, 799, 800, 799, 799, 797, 796, 793, 791, 788, 784, 780, 775, 770, 765, 759, 753, 746, 739, 731, 723, 715, 706, 697, 687, 677, 667, 657, 646, 635, 623, 611, 600, 587, 575, 562, 549, 536, 523, 510, 496, 483, 469, 455, 441, 427, 413, 400, 386, 372, 358, 344, 330, 316, 303, 289, 276, 263, 250, 237, 224, 212, 200, 188, 176, 164, 153, 142, 132, 122, 112, 102, 93, 84, 76, 68, 60, 53, 46, 40, 34, 29, 24, 19, 15, 11, 8, 6, 3, 2, 0, 0, 0, 0, 0, 2, 3, 6, 8, 11, 15, 19, 24, 29, 34, 40, 46, 53, 60, 68, 76, 84, 93, 102, 112, 122, 132, 142, 153, 164, 176, 188, 200, 212, 224, 237, 250, 263, 276, 289, 303, 316, 330, 344, 358, 372, 386};
11. 
12.void delay (uint16_t);
13. 
14.void main (void) { 
15.  /* System Clocks Configuration */
16.  RCC_Configuration();
17.  GPIO_Configuration();
18. 
19.  TIM1_PWM_Configuration();
20.  NVIC_Configuration();
21.  //TIM3_SinePeriod_Configuration();
22.     
23.  /* GPIO Configuration */
24.   
25.   
26.  GPIOC->BSRR = GPIO_Pin_8 | GPIO_Pin_9;
27. 
28.  while (1) {
29.    delay(10000);
30.    TIM1->CCR1 = sine_values[sine_idx++];
31.    if (sine_idx >= SINE_SAMPLE_SET) sine_idx = 1;
32.  }
33.}
34. 
35.void delay(uint16_t delay_counts)
36.{
37.  while (delay_counts--);
38.}

001.#include "hw_config.h"
002. 
003.uint16_t CCR1_Val = 400;
004.uint16_t CCR2_Val = 100;
005.uint16_t CCR3_Val = 100;
006.uint16_t CCR4_Val = 100;
007.uint16_t CCR4_tmp = 100;
008.uint16_t PrescalerValue = 0;
009. 
010.#define NUM_OF_SINE_VALUES 200
011. 
012.ErrorStatus HSEStartUpStatus;
013. 
014. 
015.#define TIM_DMA_Event TIM_DMA_Update
016.#define TIM_DMA_Channel DMA1_Channel3
017.#define DMA_TIM_TRIGGER TIM3
018. 
019. 
020.void TIM3_SinePeriod_Configuration(void)
021.{
022.  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
023.  TIM_OCInitTypeDef  TIM_OCInitStructure;
024. 
025.  TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
026.  TIM_TimeBaseStructure.TIM_Period = 800;         
027.  TIM_TimeBaseStructure.TIM_Prescaler = 0;      
028.  TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;   
029.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 
030.  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
031.   
032.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
033.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
034.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
035.  TIM_OCInitStructure.TIM_Pulse = 400;
036.  TIM_OC1Init(TIM3, &TIM_OCInitStructure);
037.  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
038.    
039.  TIM_Cmd(TIM3, ENABLE);
040.   
041.  TIM_CtrlPWMOutputs(TIM3, ENABLE);
042.   
043.  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
044.}
045. 
046./*void DMA1_SineValuesTransfer_Configuration(void)
047.{
048.  DMA_InitTypeDef DMA_InitStructure;
049.   
050.  DMA_DeInit(TIM_DMA_Channel);
051. 
052.  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&(TIM1->DMAR)); //0x40012C34;
053. // DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)sine_values;
054.  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
055.  DMA_InitStructure.DMA_BufferSize = SINE_SAMPLE_SET;
056.  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
057.  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
058.  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
059.  DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord;
060.  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
061.  DMA_InitStructure.DMA_Priority = DMA_Priority_High;
062.  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
063.  DMA_Init(TIM_DMA_Channel, &DMA_InitStructure);
064.   
065.  TIM_DMAConfig(DMA_TIM_TRIGGER, TIM_DMABase_CCR1, TIM_DMABurstLength_1Transfer);
066.  TIM_DMACmd(DMA_TIM_TRIGGER, TIM_DMA_Event, ENABLE);
067.   
068.#ifndef TEST
069.  DMA_Cmd(TIM_DMA_Channel, ENABLE);
070.#endif //#ifndef TEST
071.}*/
072. 
073.void NVIC_Configuration(void)
074.{
075.  NVIC_InitTypeDef NVIC_InitStructure;
076. 
077.  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
078.   
079.  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
080.  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
081.  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
082.  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
083.  NVIC_Init(&NVIC_InitStructure);
084.}
085.  
086.void TIM1_PWM_Configuration(void)
087.{
088.  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
089.TIM_OCInitTypeDef  TIM_OCInitStructure;
090. 
091.  TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
092.   
093.  /* Compute the prescaler value */
094.  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
095.  /* Time base configuration
096.   * 800 gives 30kHz at 24MHz clock with Presc = 1
097.   */
098.  TIM_TimeBaseStructure.TIM_Period = 800;
099.  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
100.  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
101.  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
102.  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
103. 
104.  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
105. 
106.  /* PWM1 Mode configuration: Channel1 */
107.  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
108.  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
109.  TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
110.  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
111.  TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
112.  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
113.  TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
114.  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
115. 
116.  TIM_OC1Init(TIM1, &TIM_OCInitStructure);
117. 
118.  TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
119. 
120.  /* PWM1 Mode configuration: Channel2 */
121.  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
122. 
123.  TIM_OC2Init(TIM1, &TIM_OCInitStructure);
124. 
125.  TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
126. 
127.  /* PWM1 Mode configuration: Channel3 */
128.  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
129. 
130.  TIM_OC3Init(TIM1, &TIM_OCInitStructure);
131. 
132.  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
133.   
134.  /* Automatic Output enable, Break, dead time and lock configuration*/
135.  TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
136.  TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
137.  TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
138.  TIM_BDTRInitStructure.TIM_DeadTime = 1;
139.  TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
140.  TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
141.  TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
142. 
143.  TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
144.   
145.  TIM_CCPreloadControl(TIM1, ENABLE);
146. 
147.  TIM_ARRPreloadConfig(TIM1, ENABLE);
148.   
149.  //TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
150.  //TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Trigger);
151.  //TIM_SelectInputTrigger(TIM1, TIM_TS_ITR2);
152. 
153.  /* TIM3 enable counter */
154.  TIM_Cmd(TIM1, ENABLE);
155.   
156.  TIM_CtrlPWMOutputs(TIM1, ENABLE);
157.}
158. 
159.void RCC_Configuration(void)
160.{
161.  /* DMA1 clock enable */
162.  //RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
163.   
164.  /* TIM3 clock enable */
165.  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
166. 
167.  /* GPIOA and GPIOB clock enable */
168.  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_TIM1 | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
169.}
170. 
171./**
172.  * @brief  Configure the TIM1 Ouput Channels.
173.  * @param  None
174.  * @retval None
175.  */
176.void GPIO_Configuration(void)
177.{
178.  GPIO_InitTypeDef GPIO_InitStructure;
179. 
180.  /*GPIOB Configuration: TIM1 N_channel1, 1, 2 and 3 */
181.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
182.  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
183.  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
184.  GPIO_Init(GPIOB, &GPIO_InitStructure);
185.   
186.  /*GPIOB Configuration: TIM1 channel1, 1, 2 and 3 */
187.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
188.  GPIO_Init(GPIOA, &GPIO_InitStructure);
189.   
190.  /* PC8 & PC9 LEDs */
191.  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_6;
192.  GPIO_Init(GPIOC, &GPIO_InitStructure);
193. 
194.  //GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);  
195.}
196. 
197./**
198.  * @brief  Selects PLLx3 as System clock source and configure HCLK, PCLK2
199.  *         and PCLK1 prescalers.
200.  * @param  None
201.  * @retval None
202.  */
203.#ifdef TRASH
204.void SetSysClockTo24MHz(void)
205.{
206.  /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration -----------------------------*/  
207.  /* RCC system reset(for debug purpose) */
208.  RCC_DeInit();
209. 
210.  /* Enable HSE */
211.  RCC_HSEConfig(RCC_HSE_ON);
212. 
213.  /* Wait till HSE is ready */
214.  HSEStartUpStatus = RCC_WaitForHSEStartUp();
215. 
216.  if (HSEStartUpStatus == SUCCESS)
217.  {
218.    FLASH_SetLatency(FLASH_Latency_2);
219.  
220.    /* HCLK = SYSCLK */
221.    RCC_HCLKConfig(RCC_SYSCLK_Div1);
222.   
223.    /* PCLK2 = HCLK */
224.    RCC_PCLK2Config(RCC_HCLK_Div1);
225. 
226.    /* PCLK1 = HCLK */
227.    RCC_PCLK1Config(RCC_HCLK_Div1);
228.     
229.    RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_3);
230.     
231.    RCC_PLLCmd(ENABLE);
232.     
233.    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
234.    {
235.    }
236. 
237.    /* Select HSE as system clock source */
238.    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
239. 
240.    /* Wait till PLL is used as system clock source */
241.    while (RCC_GetSYSCLKSource() != 0x08)
242.    {
243.    }
244.  }
245.  else
246.  { /* If HSE fails to start-up, the application will have wrong clock configuration.
247.       User can add here some code to deal with this error */   
248. 
249.    /* Go to infinite loop */
250.    while (1)
251.    {
252.    }
253.  }
254.}
255.#endif //#ifdef TRASH

Outcomes