cancel
Showing results for 
Search instead for 
Did you mean: 

SPI interrupt problem (repetition)

parisa
Senior
Posted on January 11, 2017 at 21:22

Hi,

Here is my program

#include <stdio.h>

#include <string.h>

#include 'stm32f10x_gpio.h'

#include 'stm32f10x_rcc.h'

#include 'stm32f10x_usart.h'

#include 'stm32f10x_spi.h'

#define SPI_FLASH           SPI3

#define SPI_CLK             RCC_APB1Periph_SPI3

#define SPI_GPIO                GPIOC

#define SPI_GPIO_CLK        RCC_APB2Periph_GPIOC

#define SPI_PIN_SCK         GPIO_Pin_10

#define SPI_PIN_MISO        GPIO_Pin_11

#define SPI_PIN_MOSI        GPIO_Pin_12

void SPI3_IRQHandler(void)

{    

                USART_SendData(USART1,SPI_I2S_GetITStatus(SPI3, SPI_I2S_IT_TXE)+48)    ;    

                SPI_I2S_ClearFlag(SPI3,SPI_I2S_IT_TXE);            

}

void SPIConfig(void){

SPI_InitTypeDef  SPI_InitStructure;

  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable SPI and GPIO clocks */

  RCC_APB2PeriphClockCmd( SPI_GPIO_CLK , ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3,ENABLE);

  GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE);

  /* Configure SPI pins: SCK, MISO and MOSI */

  GPIO_InitStructure.GPIO_Pin = SPI_PIN_SCK | SPI_PIN_MISO | SPI_PIN_MOSI;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(SPI_GPIO, &GPIO_InitStructure);

    

    SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

  SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;

 SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;

  SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;

  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;

  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

  SPI_InitStructure.SPI_CRCPolynomial = 7;

  SPI_Init(SPI3, &SPI_InitStructure);

SPI_I2S_ITConfig(SPI3,SPI_I2S_IT_TXE,ENABLE);

SPI_I2S_ITConfig(SPI3,SPI_I2S_IT_RXNE,ENABLE);

  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  NVIC_InitStructure.NVIC_IRQChannel = SPI3_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

  /* Enable the SPI  */

  SPI_Cmd(SPI3, ENABLE);

}

    

void delay(){

    int i,j;

    for(i=0;i<10000;i++)

    for(j=0;j<250;j++);

}

int main(){    

unsgined char p[3]    

SPIConfig();            

    

    while(1){            

    delay();delay();

    USART_SendData(USART1,'A')    ;        

    }

    

    }

    

}

But it prints '1' char continuously and  it doesn't arrive at     USART_SendData(USART1,'A')    ;   line to send 'A' char to terminal while I didn't send & receive anything.what is my mistake?

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on January 11, 2017 at 22:35

>>

what is my mistake?

Unless you actually send some data TXE is going to continue to assert until you disable it. Clearing the IT flag isn't sufficient.

You'd also do well to qualify the source of the interrupt in the IRQ Handler

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

View solution in original post

1 REPLY 1
Posted on January 11, 2017 at 22:35

>>

what is my mistake?

Unless you actually send some data TXE is going to continue to assert until you disable it. Clearing the IT flag isn't sufficient.

You'd also do well to qualify the source of the interrupt in the IRQ Handler

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..