在STM32F4发现上配置SPI

时间:2018-06-29 07:48:59

标签: c arm spi

用示波器上的引脚进行测量时,无法从MOSI引脚或CLK引脚中获取任何数据。

这是我的代码:

包括

包括“ stm32f407xx.h”

void delay(){

                  int i;

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

}

int main(){

           // Enable RCC APB2 clock for SYSCFG (for externalinterrupt)
                  RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
                  // Enable RCC APB1 clock for SPI2
                  RCC->APB1ENR |= (1 << 14);
                  // GPIOA for pushbutton
                  RCC->AHB1ENR |= (1 << 0);
                  // GPIOB for SPI
                  RCC->AHB1ENR |= (1 << 1);
                  // GPIOD for LED
                  RCC->AHB1ENR |= (1 << 3);

////////用于MOSI,MISO och CLK引脚的GPIO B初始化:

                  GPIOB->MODER |= (2 << (2*13)); //Sets pin 13 as Alternate function
                  GPIOB->MODER |= (2 << (2*14)); //Sets pin 14 as Alternate function
                  GPIOB->MODER |= (2 << (2*15)); //Sets pin 15 as Alternate function
                  GPIOB->OTYPER |= PUSH_PULL_PIN_13;
                  GPIOB->OTYPER |= PUSH_PULL_PIN_14;
                  GPIOB->OTYPER |= PUSH_PULL_PIN_15;

                  GPIOB->OSPEEDR |= OSPEEDR_MEDIUM_PIN_13;
                  GPIOB->OSPEEDR |= OSPEEDR_MEDIUM_PIN_14;
                  GPIOB->OSPEEDR |= OSPEEDR_MEDIUM_PIN_15;

                  GPIOB->PUPDR |= PUPDR_PULLDOWN_PIN_13; // Pull down for CLK
                  GPIOB->PUPDR |= PUPDR_PULLUP_PIN_14; // Pull up for MISO
                  GPIOB->PUPDR |= PUPDR_PULLUP_PIN_15; // Pull up for MOSI

                  GPIOB->AFR[1] |= ALT_FUNC_AF5_PIN_13;  // 5 för AF5 till pin 13
                  GPIOB->AFR[1] |= ALT_FUNC_AF5_PIN_14;  // 5 för AF5 till pin 14
                  GPIOB->AFR[1] |= ALT_FUNC_AF5_PIN_15;  // 5 för AF5 till pin 15

//////////按钮的GPIO A初始化

                  GPIOA->MODER |= MODER_INPUT_PIN_0;

用于LED的GPIO D初始化

                  GPIOD->MODER |= MODER_OUTPUT_PIN_12; //Sets pin 12 as output
                  GPIOD->MODER |= MODER_OUTPUT_PIN_13; //Sets pin 13 as output
                  GPIOD->MODER |= MODER_OUTPUT_PIN_14; //Sets pin 14 as output
                  GPIOD->MODER |= MODER_OUTPUT_PIN_15; //Sets pin 15 as output

                  GPIOD->OTYPER |= PUSH_PULL_PIN_12;
                  GPIOD->OTYPER |= PUSH_PULL_PIN_13;
                  GPIOD->OTYPER |= PUSH_PULL_PIN_14;
                  GPIOD->OTYPER |= PUSH_PULL_PIN_15;

                  GPIOD->OSPEEDR |= OSPEEDR_MEDIUM_PIN_12;
                  GPIOD->OSPEEDR |= OSPEEDR_MEDIUM_PIN_13;
                  GPIOD->OSPEEDR |= OSPEEDR_MEDIUM_PIN_14;
                  GPIOD->OSPEEDR |= OSPEEDR_MEDIUM_PIN_15;

                  GPIOD->PUPDR |= PUPDR_PULLDOWN_PIN_12; //Frågetecken på denna, ska det vara 0 1 eller 2??
                  GPIOD->PUPDR |= PUPDR_PULLDOWN_PIN_13; //Frågetecken på denna, ska det vara 0 1 eller 2??
                  GPIOD->PUPDR |= PUPDR_PULLDOWN_PIN_14; //Frågetecken på denna, ska det vara 0 1 eller 2??
                  GPIOD->PUPDR |= PUPDR_PULLDOWN_PIN_15; //Frågetecken på denna, ska det vara 0 1 eller 2??

                  GPIOD->AFR[1] |= ALT_FUNC_AF0_PIN_12;  // 0 för AF0 till pin 12
                  GPIOD->AFR[1] |= ALT_FUNC_AF0_PIN_13;  // 0 för AF0 till pin 13
                  GPIOD->AFR[1] |= ALT_FUNC_AF0_PIN_14;  // 0 för AF0 till pin 14
                  GPIOD->AFR[1] |= ALT_FUNC_AF0_PIN_15;  // 0 för AF0 till pin 15

//////// SPI初始化

                  SPI2->CR1 |= (0 << CR1_CLOCK_PHASE);       //Set clockphase in CR1 register
                  SPI2->CR1 |= (1 << CR1_CLOCK_POLARITY);    //Set polarity in CR1 register
                  SPI2->CR1 |= (4 << CR1_BAUD_RATE);         //Set baudrate
                  SPI2->CR1 |= (1 << CR1_DATA_FRAME_FORMAT); //16-bit transmission
                  SPI2->CR1 |= (1 << CR1_ENABLE_SPI);        //Enable SPI
                  SPI2->CR1 |= (1 << CR1_MASTER_SELECT);     //MASTER SELECTION
                  SPI2->CR1 |= (1 << CR1_LSB_FIRST);         //LSB FIRST
                  SPI2->CR1 |= (1 << CR1_SLAVE_SELECT);      //SSI set
                  SPI2->CR1 |= (1 << CR1_SW_SLAVE_MANAGE);   //SSM, software slave management selected, which means SSI will be used insted of I/O
                  SPI2->CR1 |= (0 << CR1_RECEIVE_ONLY);      //Transmit and Receive
                  SPI2->CR1 |= (0 << CR1_CRC_TRANSFER);      //NOT USED, CRC
                  //SPI2->CR1 |= (0 << CR1_CRC_CALCULATION); //NOT USED, CRC calculation disabled
                  //SPI2->CR1 |= (0 << CR1_BIDIOE);          //enable transmit-only if set
                  SPI2->CR1 |= (0 << CR1_BIDIMODE);          //1-line bidirectional mode if set

/////////启用外部中断

                  //SYSCFG->EXTICR[0] |= (0 << 0); //Enable
                  EXTI->IMR |= (1 << 0);      //Enable interrupt
                  EXTI->RTSR |= (1 << 0);    //set Risnig edge interrupt
                  EXTI->FTSR |= (0 << 0);    //falling edge interrupt (disabled)
                  NVIC_EnableIRQ(EXTI0_IRQn);
                  SPI2->CR2 |= (1 << 7);

                  while(1){
                         GPIOD->ODR |= (1 << 12); // led ON
                         delay();
                         GPIOD->ODR &= ~(1 << 12); // led OFF
                         delay();
                  }

                  return 0;

}

void EXTI0_IRQHandler(){

         EXTI->PR |= (1 << 0); //Clears interrupt pending bit

         while(1){
                GPIOD->ODR |= (1 << 13); //Blå led ON
                delay();
                GPIOD->ODR &= ~(1 << 13); //Blå led OFF
                delay();
                // Here I want to Send Data
                SPI2->DR |= 0xFF;
         }

}

1 个答案:

答案 0 :(得分:0)

请使用stm32f407xx.h文件中的适当宏定义。最好阅读:

TIM2->DIER |= TIM_DIER_UIE; /* interrupt enable on 1st channel */

大于:

TIM2->DIER |= (1 << 1); /* interrupt enable on 1st channel */

这将避免混乱,并可能指出您的问题出在哪里。在当前代码格式下,如果正确设置寄存器,则需要花费很多时间。

编辑:好的,闻起来有些香:

GPIOD->AFR[1] |= (0 << (30));  // 5 för AF0 till pin 15

AFR寄存器的每个引脚为4位。您为什么要转移价值30?不应该是28吗?

edit_2:您的按钮是否触发EXTI irq?如果是的话-那不好... 我不记得板上是否有提供按钮反跳功能的硬件,尽管您应该将其放入代码中。现在,中断可能被触发很多次,而不是按1 irq = 1次按钮。

edit_3:

SPI2->CR2 |= (1 << 7);

当TX缓冲区为空时启用irq。缓冲区为空,直到您按下按钮。这会导致不停地调用SPI2中断,并且您尚未为其定义处理程序。尝试阐明您要实现的目标以及应该如何实现。