stm32l476主 stm8l152从
一、
stm32l476 上 rt-thread 注册spi1设备spi10
__HAL_RCC_GPIOB_CLK_ENABLE();
rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_10);//注册并设置片选引脚
/* 查 找 spi 设 备 获 取 设 备 句 柄 */
spi_asr6505_dev0 = (struct rt_spi_device *)rt_device_find(ASR6505_DEVICE0);
if (!spi_asr6505_dev0) {
rt_kprintf("asr6505 sample run failed! can't find %s device!\n", ASR6505_DEVICE0);
return -RT_ENOSYS;
}
{
struct rt_spi_configuration cfg;
cfg.data_width = 8;
cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB; /* SPI Compatible Modes 0 */
cfg.max_hz = 500 * 1000; /* SPI Interface with Clock Speeds Up to 20 MHz */
rt_spi_configure(spi_asr6505_dev0, &cfg);
}
线程调用 rt_spi_transfer? 发送接收数据
二、
stm8l152上初始化spi1 开接收中断
CLK_PeripheralClockConfig (CLK_Peripheral_SPI1,ENABLE);//开启SPI时钟
GPIO_Init(GPIOB, GPIO_Pin_4, GPIO_Mode_In_PU_No_IT);
GPIO_Init(GPIOB, GPIO_Pin_5, GPIO_Mode_In_PU_No_IT);
GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_In_PU_No_IT);
GPIO_Init(GPIOB, GPIO_Pin_7, GPIO_Mode_Out_PP_High_Fast);
SPI_DeInit(SPI1);
SYSCFG_REMAPPinConfig(REMAP_Pin_SPI1PortF, ENABLE);
SPI_Init(SPI1,SPI_FirstBit_MSB, SPI_BaudRatePrescaler_16, SPI_Mode_Slave,\
SPI_CPOL_Low, SPI_CPHA_1Edge, \
SPI_Direction_2Lines_FullDuplex, SPI_NSS_Hard, 0x07);
SPI_ITConfig(SPI1, SPI_IT_RXNE, ENABLE);
SPI_Cmd(SPI1,ENABLE); /* 使能SPI */
中断中接收并发送数据?
if(SPI_GetITStatus(SPI1, SPI_IT_RXNE)){
spi_slave_rx[spi_rx_count++] = SPI_ReceiveData(SPI1);
SPI_SendData(SPI1, spi_slave_tx[spi_tx_count++]);
}
?三、
遇到的问题:主、从之间的时钟要设置相近;主机接收从机数据有误差;多从机分时跟主机通信
参考:
嵌入式】STM32实现SPI双机通信的一些细节(2)片选总结
STM32 HAL库SPI+DMA接收数据的配置和使用方法
|