SparkFun Forums 

Where electronics enthusiasts find answers.

Everything ARM and LPC
By naveed_33
#146737
Hello,
i am new to STM32 controller and i am using a STM32f0 discovery kit. i have been trying to use the interrupt but whenever i generate the external interrupt the controller stops working and freezes while actually nothing should happen because i have defined nothing . this is the code that i wrote for the controller.

#include "stm32f0xx_gpio.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_spi.h"
#include "stm32f0xx_exti.h"
#include "stm32f0xx_syscfg.h"
#include "stm32f0xx_tim.h"
int k =1;

void delay(int a)
{
int b = 500;
a= a*100;
while (a>0)
{
while(b >0)
{b--;}
a--;
}

int sysinit()
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef demo;
SPI_InitTypeDef spicon;
EXTI_InitTypeDef EXTI_InitStructure;
// this function initializes clocks to the ports and defines which ports are inputs and outputs
RCC_DeInit();
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB |RCC_AHBPeriph_GPIOC , ENABLE);
//sending clock to a b c ports
//i think i should initialize the spi1 clock too
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);//clock to spi1 module
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);// clock to gp timer2
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);// dont know but clock has to be sent here
// interrupt settings
EXTI_ClearITPendingBit(EXTI_Line0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0 ;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// connecting GPIOA) with EXTI1
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
// Prioritising interrupt below
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// GPIO settings
demo.GPIO_Mode = GPIO_Mode_AF;
demo.GPIO_Speed = GPIO_Speed_Level_3;
demo.GPIO_OType = GPIO_OType_PP;
demo.GPIO_PuPd = GPIO_PuPd_DOWN;
demo.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; //SPI 1 ports enabled yet spi not initialized
GPIO_Init(GPIOA, &demo);
demo.GPIO_Mode = GPIO_Mode_OUT;
demo.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_Init(GPIOA, &demo);// outputs at PA1,2,3
demo.GPIO_Mode = GPIO_Mode_IN;
demo.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_Init(GPIOB, &demo);
demo.GPIO_Pin = GPIO_Pin_0;
// defining user button 2 for personal use
GPIO_Init(GPIOA, &demo);
demo.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
demo.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(GPIOC, &demo);
//configuring gpio for spi...
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5 |GPIO_PinSource6 |GPIO_PinSource7|GPIO_PinSource4 , GPIO_AF_0);
/// my SPI 1 initialization code for spi master///
spicon.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
spicon.SPI_Mode = SPI_Mode_Master;
spicon.SPI_DataSize = SPI_DataSize_8b;
spicon.SPI_CPOL = SPI_CPOL_High ;
spicon.SPI_CPHA = SPI_CPHA_2Edge;
spicon.SPI_NSS = SPI_NSS_Hard ;
spicon.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128;
spicon.SPI_FirstBit = SPI_FirstBit_MSB;
spicon.SPI_CRCPolynomial = SPI_CRCLength_8b;
SPI_Init(SPI1, &spicon);
SPI_Cmd(SPI1, ENABLE);


return 0;
}
void EXTI0_1_IRQn_IRQHandler(void)
{
//Check if EXTI_Line1 is asserted
if(EXTI_GetITStatus(EXTI_Line0) != RESET)
{
// do nothing
}
//we need to clear line pending bit manually
EXTI_ClearITPendingBit(EXTI_Line0);
}
int main()
{

sysinit();


while (k)
{
SPI_SendData8(SPI1, 0xAA);
GPIO_SetBits(GPIOC,GPIO_Pin_9);
GPIO_ResetBits(GPIOC,GPIO_Pin_8 );
delay (800);
GPIO_ResetBits(GPIOC,GPIO_Pin_9 );
GPIO_SetBits(GPIOC,GPIO_Pin_8);
SPI_SendData8(SPI1, 0xBA);
delay (800);
}
return 0;
}
By hsutherl
#146752
I can't see the problem. The freeze is severe enough that your debugger no longer works?

If, say, you were using the wrong startup file, then you might be jumping to the wrong interrupt vector, and anything could happen. You might have to post the entire project somewhere.

(I assume the lack of a bracket at the end of delay() is a copy-paste artifact.)