简体   繁体   English

STM32 SPI发送

[英]STM32 SPI Transmit

I've been using an STM32F103C8, trying for a while to get the SPI interface. 我一直在使用STM32F103C8,试图获取SPI接口已有一段时间。 Been using Atollic TrueStudio. 一直在使用Atollic TrueStudio。 I'm still new at this so if this is a dumb question I hope you'll forgive me. 我在这方面还很陌生,所以如果这是一个愚蠢的问题,希望您能原谅我。

I can't seem to transmit anything on the SPI interface no matter what I try. 无论尝试如何,我似乎都无法在SPI接口上传输任何内容。 I looked through the manual and I thought I did everything correctly but obviously not. 我翻阅了手册,以为自己做的一切正确,但显然做不到。 From what I read, it seems to be: 根据我的阅读,似乎是:

  • Enable SPI Clock in RCC APB2ENR 在RCC APB2ENR中使能SPI时钟
  • Configure the SPI CR1 Register 配置SPI CR1寄存器
  • Enable the SPI using the SPE bit 使用SPE位使能SPI
  • Transmit by loading data into the SPI-> DR Register 通过将数据加载到SPI-> DR寄存器进行发送
  • Receive by reading data from the SPI-> DR register since there is a transmit and a receive buffer linked to SPI->DR 通过读取来自SPI-> DR寄存器的数据进行接收,因为存在与SPI-> DR链接的发送和接收缓冲区

I tried looping back the MOSI pin to the MISO pin and doing a write, and got nothing. 我尝试将MOSI引脚环回至MISO引脚并进行写操作,但一无所获。 I then connected a logic analyser and the SPI clock pin isn't even doing anything. 然后,我连接了逻辑分析仪,而SPI时钟引脚甚至什么也没做。 Relevant code below, if anyone can help that'd be great: 下面的相关代码,如果有人可以帮助的话,那会很棒:

void PrintStrToUART(char str[])
{
char *pointertostr;
for (pointertostr = &str[0]; *pointertostr != '\0'; pointertostr++){
    USART1 -> DR = (*pointertostr & USART_DR_DR);
    while ((USART1 -> SR & USART_SR_TXE) == 0){
        ; 
        }
    }
}

void PrintCharArrayToUART(unsigned char str[], int arraysize){
int j;
unsigned char buffer[((5*arraysize)-2)];
unsigned char *positionpointer = &buffer[0];

for(j=0; j <= (arraysize-1); j++){
    if(j){
        positionpointer += sprintf(positionpointer, ", ");
    }
    positionpointer += sprintf(positionpointer, "%d", str[j]);
}

int newarrayend;
newarrayend = (positionpointer - &buffer[0]);

PrintStrToUART("[");
for(j = 0; j <= newarrayend; j++){ 
    USART1 -> DR = (buffer[j] & USART_DR_DR);
    while ((USART1 -> SR & USART_SR_TXE) == 0){
        ;
    }
}
PrintStrToUART("]\n\r");
}

void SelfSPIInit(void){ //because the Mx one is not working
RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; //Enable SPI clock

//Set baud prescaler
SPI1->CR1 = SPI_CR1_BR; //Slowest SPI I can have

//CPHA CPOL
SPI1-> CR1 &= ~(SPI_CR1_CPOL | SPI_CR1_CPHA);

//8 bit data format
SPI1->CR1 &= ~(SPI_CR1_DFF);

//Full duplex mode
SPI1->CR1 &= ~(SPI_CR1_BIDIMODE);
SPI1->CR1 &= ~(SPI_CR1_RXONLY);

//MSB first
SPI1->CR1 &= ~(SPI_CR1_LSBFIRST);

//Master mode
SPI1->CR1 |= SPI_CR1_MSTR;

//Software NSS mode off
SPI1->CR1 |= SPI_CR1_SSM | SPI_CR1_SSI;

//Enable
SPI1->CR1 |= SPI_CR1_SPE;

//Enable the SPI GPIO pins

}

int main(void)
{
int i;
HAL_Init();

/* Configure the system clock */
SystemClock_Config(); //config to run at the correct speed

/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_DMA_Init();
MX_I2C1_Init();
MX_TIM1_Init();
MX_USART1_UART_Init();
MX_TIM2_Init();
MX_CRC_Init();
MX_USART2_UART_Init();
MX_ADC1_Init();
MX_TIM3_Init();
//MX_SPI1_Init();
SelfSPIInit();

TIM1 -> CR1 |= TIM_CR1_CEN;
TIM2 -> CR1 |= TIM_CR1_CEN;
I2C1 -> CR1 |= I2C_CR1_PE; //peripheral enable
SPI1 -> CR1 |= SPI_CR1_SPE;

unsigned char newchararray[21] = {0x41};
unsigned char *pnewchararray = &newchararray[0];

unsigned char recarray[7] = {127};
unsigned char *precarray = &recarray[0];

while (1)
{

  GPIOC -> BSRR |= GPIO_BSRR_BR13;
  GPIOA -> BSRR |= GPIO_BSRR_BR6;
  MilliSecondDelay(500);
  GPIOC -> BSRR |= GPIO_BSRR_BS13;
  GPIOA -> BSRR |= GPIO_BSRR_BS6;
  MilliSecondDelay(500);
  SPI1->DR = *pnewchararray;
  while((SPI1->SR & SPI_SR_TXE) == 0){
      //while transmit buffer not empty, wait
      ;
  }
  while((SPI1->SR & SPI_SR_RXNE) == 0){
      ;//while receive buffer not empty
  }
  *precarray = SPI1->DR;
  PrintCharArrayToUART(precarray, 7);
 }
}

Sorry about the indenting, as I said I'm still new that this, but that's the code that compiles and runs. 很抱歉缩进,正如我所说的那样,我仍然很陌生,但这就是编译和运行的代码。 Any help appreciated, thanks! 任何帮助表示赞赏,谢谢!

Edit: Wasn't sure if I should include the USART functions, that is just to print to my laptop so I can see what the SPI data register has. 编辑:不确定我是否应该包括USART功能,那只是为了打印到我的笔记本电脑上,以便我可以看到SPI数据寄存器中的内容。 But I thought if I included it, the program would make more sense. 但是我认为,如果包含它,该程序将更有意义。

Please have a look at http://www.handsonembedded.com/stm32f103-spl-tutorial-5/ . 请访问http://www.handsonembedded.com/stm32f103-spl-tutorial-5/ There is a full spi example for the stm32f103. stm32f103有完整的spi示例。

还有一个是:您好像忘记了引脚多路复用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM