新手入门
积分 15
金钱 15
注册时间 2023-3-13
在线时间 6 小时
1 金钱
本帖最后由 小黄菜 于 2023-12-3 22:39 编辑
/**
PB13 SCK / PB14 MISO / PB15 MOSI
PD11 CS UNO / PD13 CS DUE
*/
#define SPIT_FLAG_TIMEOUT ((uint32_t)0x1000)
#define SCHA_ERROR(fmt,arg...) printf("<<-SCHA-ERROR->> "fmt"\n",##arg)
/*SPI接口定义-开头****************************/
#define SCHA_SPI SPI2
#define SCHA_SPI_CLK RCC_APB1Periph_SPI2
#define SCHA_SPI_CLK_INIT RCC_APB1PeriphClockCmd //22.5M bits/s
#define SCHA_SPI_SCK_PIN GPIO_Pin_13
#define SCHA_SPI_SCK_GPIO_PORT GPIOB
#define SCHA_SPI_SCK_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SCHA_SPI_SCK_PINSOURCE GPIO_PinSource13
#define SCHA_SPI_SCK_AF GPIO_AF_SPI2
#define SCHA_SPI_MISO_PIN GPIO_Pin_14
#define SCHA_SPI_MISO_GPIO_PORT GPIOB
#define SCHA_SPI_MISO_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SCHA_SPI_MISO_PINSOURCE GPIO_PinSource14
#define SCHA_SPI_MISO_AF GPIO_AF_SPI2
#define SCHA_SPI_MOSI_PIN GPIO_Pin_15
#define SCHA_SPI_MOSI_GPIO_PORT GPIOB
#define SCHA_SPI_MOSI_GPIO_CLK RCC_AHB1Periph_GPIOB
#define SCHA_SPI_MOSI_PINSOURCE GPIO_PinSource15
#define SCHA_SPI_MOSI_AF GPIO_AF_SPI2
#define SCHA_UNO_CS_PIN GPIO_Pin_11
#define SCHA_UNO_CS_GPIO_PORT GPIOD
#define SCHA_UNO_CS_GPIO_CLK RCC_AHB1Periph_GPIOD
#define SCHA_DUE_CS_PIN GPIO_Pin_13
#define SCHA_DUE_CS_GPIO_PORT GPIOD
#define SCHA_DUE_CS_GPIO_CLK RCC_AHB1Periph_GPIOD
#define SPI_SCHA_UNO_CS_LOW() GPIO_ResetBits(GPIOD, GPIO_Pin_11)
#define SPI_SCHA_UNO_CS_HIGH() GPIO_SetBits(GPIOD, GPIO_Pin_11)
#define SPI_SCHA_DUE_CS_LOW() GPIO_ResetBits(GPIOD, GPIO_Pin_13)
#define SPI_SCHA_DUE_CS_HIGH() GPIO_SetBits(GPIOD, GPIO_Pin_13)
/*SPI接口定义-结尾****************************/
/**
* @brief SPI_SCHA初始化
* @param 无
* @retval 无
*/
void SPI_SCHA_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能 SCHA_SPI 及GPIO 时钟 */
/*!< SPI_SCHA_SPI_CS_GPIO, SPI_SCHA_SPI_MOSI_GPIO,
SPI_SCHA_SPI_MISO_GPIO,SPI_SCHA_SPI_SCK_GPIO 时钟使能 */
RCC_AHB1PeriphClockCmd (SCHA_SPI_SCK_GPIO_CLK | SCHA_SPI_MISO_GPIO_CLK|SCHA_SPI_MOSI_GPIO_CLK|SCHA_UNO_CS_GPIO_CLK|SCHA_DUE_CS_GPIO_CLK, ENABLE);
/*!< SPI_SCHA_SPI 时钟使能 */
SCHA_SPI_CLK_INIT(SCHA_SPI_CLK, ENABLE);
//设置引脚复用
GPIO_PinAFConfig(SCHA_SPI_SCK_GPIO_PORT,SCHA_SPI_SCK_PINSOURCE,SCHA_SPI_SCK_AF);
GPIO_PinAFConfig(SCHA_SPI_MISO_GPIO_PORT,SCHA_SPI_MISO_PINSOURCE,SCHA_SPI_MISO_AF);
GPIO_PinAFConfig(SCHA_SPI_MOSI_GPIO_PORT,SCHA_SPI_MOSI_PINSOURCE,SCHA_SPI_MOSI_AF);
/*!< 配置 SPI_SCHA_SPI 引脚: SCK */
GPIO_InitStructure.GPIO_Pin = SCHA_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(SCHA_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
/*!< 配置 SPI_SCHA_SPI 引脚: MISO */
GPIO_InitStructure.GPIO_Pin = SCHA_SPI_MISO_PIN;
GPIO_Init(SCHA_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/*!< 配置 SPI_SCHA_SPI 引脚: MOSI */
GPIO_InitStructure.GPIO_Pin = SCHA_SPI_MOSI_PIN;
GPIO_Init(SCHA_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure);
/*!< 配置 SPI_SCHA_SPI 引脚:UNO CS */
GPIO_InitStructure.GPIO_Pin = SCHA_UNO_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(SCHA_UNO_CS_GPIO_PORT, &GPIO_InitStructure);
/*!< 配置 SPI_SCHA_SPI 引脚 UE CS */
GPIO_InitStructure.GPIO_Pin = SCHA_DUE_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Init(SCHA_DUE_CS_GPIO_PORT, &GPIO_InitStructure);
/* 停止信号 SCHA: CS引脚高电平*/
SPI_SCHA_UNO_CS_HIGH();
SPI_SCHA_DUE_CS_HIGH();
/* SCHA_SPI 模式配置 */
// SCHA芯片 支持SPI模式0 据此设置CPOL0 CPHA0 SCK空闲时低电平 奇数边沿采样
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SCHA_SPI, &SPI_InitStructure);
/* 使能 SCHA_SPI */
SPI_Cmd(SCHA_SPI, ENABLE);
}
unsigned int SCHA_SPI_WR(unsigned int wrData,u8 DUE_UNO)
{
uint32_t num1,num2;
uint32_t AngelData;
if(DUE_UNO==0) SPI_SCHA_DUE_CS_LOW();
else SPI_SCHA_UNO_CS_LOW();
/****** 高16位******/
SPITimeout = SPIT_FLAG_TIMEOUT;
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SCHA_SPI, SPI_I2S_FLAG_TXE) == RESET)
{
if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(0);
}
SPI_I2S_SendData(SCHA_SPI, (wrData & (0xFFFF0000)));
printf("%x \r\n",wrData & (0xFFFF0000) );
SPITimeout = SPIT_FLAG_TIMEOUT;
/* Wait to receive a Half Word */
while (SPI_I2S_GetFlagStatus(SCHA_SPI, SPI_I2S_FLAG_RXNE) == RESET)
{
if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(1);
}
num1 = SPI_I2S_ReceiveData(SCHA_SPI); //读SPI
printf("num1:%x\r\n",num1);
printf("num1: %x\r\n",(num1&0x0000FFFF));
/********** 低16位 ****************/
SPITimeout = SPIT_FLAG_TIMEOUT;
/* Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SCHA_SPI, SPI_I2S_FLAG_TXE) == RESET)
{
if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(2);
}
SPI_I2S_SendData(SCHA_SPI, (wrData & (0x0000FFFF)));
printf("%x \r\n",wrData & (0x0000FFFF) );
SPITimeout = SPIT_FLAG_TIMEOUT;
/* Wait to receive a Half Word */
while (SPI_I2S_GetFlagStatus(SCHA_SPI, SPI_I2S_FLAG_RXNE) == RESET)
{
if((SPITimeout--) == 0) return SPI_TIMEOUT_UserCallback(3);
}
num2 = SPI_I2S_ReceiveData(SCHA_SPI); //读SPI
printf("num2:%x\r\n",num2);
printf("num2: %x\r\n",(num2&0x0000FFFF)<<16);
// SPI_I2S_SendData(SCHA_SPI, (wrData & (0x0000FFFF)));
// printf("%x \r\n",wrData & (0x0000FFFF) );
// num2 = SPI2->DR;//读SPI
// printf("num2:%x\r\n",num2);
// printf("num2: %x\r\n",(num2&0x0000FFFF)<<16);
if(DUE_UNO==0) SPI_SCHA_DUE_CS_HIGH();
else SPI_SCHA_UNO_CS_HIGH();
AngelData = ((num2&0x0000FFFF)<<16 | (num1&0x0000FFFF));
printf("SPI返回值: %x \r\n",AngelData);
return AngelData ;
}
我来回答
协议
协议