0

我尝试使用 SPI 将数据从主机发送到从机,

我对此真的很陌生,我不知道我做错了什么

我不知道它是否与我的初始化配置或我对 spi 进程的理解有关。如果有人知道或可以指导我,因为我不知道出了什么问题。


void SPI_init_master(void){
    RCC->AHBENR |= RCC_AHBENR_GPIOBEN; // enable port b clock
    RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; // enable spi clock
    GPIOB->MODER |= 0x00000A80;     // Configure GPIOB pins 3 and 4 and 5 as alternate function 5
    // AFR[0] is the same as AFRL in the reference manual (p. 241)
    GPIOB->AFR[0] |= 0x00555000;    // configure PB3-PB5
//  SPI1->CR1 |= 0x0044; // enable SPI and Master configuration
     SPI1->CR1 |=  (SPI_CR1_SPE | SPI_CR1_MSTR);
}


void SPI_write(uint8_t ch){
    *(__IO uint8_t *)&SPI1->DR = ch;
    while(!(SPI1->SR & SPI_SR_TXE)); // wait until Transmit buffer empty will be 1 - finish to transmit data and the transmit buffer empty
    print("spi_write - data in DR is: ");
    USART2_printCharacter(ch);
    print("\n");
}


char SPI_read(void)
{
    char ch;
    while(!(SPI1->SR & SPI_SR_RXNE));// wait until Receive buffer not empty will be 1 - finish to transmit data and the transmit buffer not empty
    ch = SPI1->DR;
    print("spi_read - data in DR is: ");
    USART2_printCharacter(ch);
    print("\n");
    return ch;
}
4

0 回答 0