我用的是STM32F4CubeF4的库文件,用SPI和一个芯片通讯。
通过SPI读回来的数据全是0x00,然后用Jlink仿真,发现待发送的数据根本就没有写到SPI_DR寄存器里去,SPI_DR寄存器里始终是0x00。
大家用过这个库文件里的SPI吗,能否给个例程?
初始化SPI的代码如下:
void SPIx_Init(void)
{
if(HAL_SPI_GetState(&hnucleo_Spi) == HAL_SPI_STATE_RESET)
{
/* SPI Config */
hnucleo_Spi.Instance = NUCLEO_SPIx; // SPI2
/* SPI baudrate is set to 12,5 MHz maximum (PCLK2/SPI_BaudRatePrescaler = 100/8 = 12,5 MHz)
to verify these constraints:
- ST7735 LCD SPI interface max baudrate is 15MHz for write and 6.66MHz for read
Since the provided driver doesn't use read capability from LCD, only constraint
on write baudrate is considered.
- SD card SPI interface max baudrate is 25MHz for write/read
- PCLK2 max frequency is 100 MHz
*/
hnucleo_Spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
hnucleo_Spi.Init.Direction = SPI_DIRECTION_2LINES;
hnucleo_Spi.Init.CLKPhase = SPI_PHASE_1EDGE;
hnucleo_Spi.Init.CLKPolarity = SPI_POLARITY_LOW;
hnucleo_Spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
hnucleo_Spi.Init.CRCPolynomial = 7;
hnucleo_Spi.Init.DataSize = SPI_DATASIZE_8BIT;
hnucleo_Spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
hnucleo_Spi.Init.NSS = SPI_NSS_SOFT;
hnucleo_Spi.Init.TIMode = SPI_TIMODE_DISABLED;
hnucleo_Spi.Init.Mode = SPI_MODE_MASTER;
SPIx_MspInit(&hnucleo_Spi);
HAL_SPI_Init(&hnucleo_Spi);
}
}
static void SPIx_MspInit(SPI_HandleTypeDef *hspi)
{
GPIO_InitTypeDef GPIO_InitStruct;
/*** Configure the GPIOs ***/
/* Enable GPIO clock */
NUCLEO_SPIx_SCK_GPIO_CLK_ENABLE(); // Enable GPIOB RCC
NUCLEO_SPIx_MISO_MOSI_GPIO_CLK_ENABLE(); // // Enable GPIOB RCC
/* Configure SPI SCK */
GPIO_InitStruct.Pin = NUCLEO_SPIx_SCK_PIN; // GPIOB_PIN_13
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = NUCLEO_SPIx_SCK_AF; // 5
HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
/* Configure SPI MISO and MOSI */
GPIO_InitStruct.Pin = NUCLEO_SPIx_MOSI_PIN; // GPIOB_PIN_15
GPIO_InitStruct.Alternate = NUCLEO_SPIx_MISO_MOSI_AF; // 5
HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
GPIO_InitStruct.Pin = NUCLEO_SPIx_MISO_PIN; // GPIOB_PIN_14
HAL_GPIO_Init(NUCLEO_SPIx_MISO_MOSI_GPIO_PORT, &GPIO_InitStruct);
/* Configure SPI NSS */
GPIO_InitStruct.Pin = GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(NUCLEO_SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);
HAL_GPIO_WritePin(NUCLEO_SPIx_SCK_GPIO_PORT, GPIO_PIN_12, GPIO_PIN_RESET);
/*** Configure the SPI peripheral ***/
/* Enable SPI clock */
NUCLEO_SPIx_CLK_ENABLE();
}
另外,SPI2的时钟已经在库文件里的HAL层使能了。
请各位帮忙看看,多谢了!
|