#include "myRadio_gpio.h" #include "stm32f10x.h" #include "stm32f10x_exti.h" #include "stm32f10x_it.h" RADIO_GPIO_CALLBACK gpioCallback; static irqCallback_ts myIrqCallback_extiLine1; //---------------------------射频SPI驱动部分--------------------- void BOARD_SPI_NSS_H(void) { GPIO_WriteBit(BOARD_GPIO_SPI_CSN, BOARD_PIN_H); } void BOARD_SPI_NSS_L(void) { GPIO_WriteBit(BOARD_GPIO_SPI_CSN, BOARD_PIN_L); } void BOARD_SPI_SCK_H(void) { GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_H); } void BOARD_SPI_SCK_L(void) { GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_L); } void BOARD_SPI_MISO_H(void) { GPIO_WriteBit(BOARD_GPIO_SPI_MISO, BOARD_PIN_H); } void BOARD_SPI_MISO_L(void) { GPIO_WriteBit(BOARD_GPIO_SPI_MISO, BOARD_PIN_L); } void BOARD_SPI_MOSI_H(void) { GPIO_WriteBit(BOARD_GPIO_SPI_MOSI, BOARD_PIN_H); } void BOARD_SPI_MOSI_L(void) { GPIO_WriteBit(BOARD_GPIO_SPI_MOSI, BOARD_PIN_L); } uint8_t READ_BOARD_SPI_MISO(void) { return GPIO_ReadInputDataBit(BOARD_GPIO_SPI_MISO); } //---------------------------射频驱动IO部分--------------------- void RF_SX1278_RESET_H(void) { GPIO_WriteBit(RF_SX1278_RESET, BOARD_PIN_H); } void RF_SX1278_RESET_L(void) { GPIO_WriteBit(RF_SX1278_RESET, BOARD_PIN_L); } void RF_SX1278_DIO0_H(void) { GPIO_WriteBit(RF_SX1278_DIO0, BOARD_PIN_H); } void RF_SX1278_DIO0_L(void) { GPIO_WriteBit(RF_SX1278_DIO0, BOARD_PIN_L); } void RF_SX1278_DIO1_H(void) { GPIO_WriteBit(RF_SX1278_DIO1, BOARD_PIN_H); } void RF_SX1278_DIO1_L(void) { GPIO_WriteBit(RF_SX1278_DIO1, BOARD_PIN_L); } void RF_SX1278_DIO2_H(void) { GPIO_WriteBit(RF_SX1278_DIO2, BOARD_PIN_H); } void RF_SX1278_DIO2_L(void) { GPIO_WriteBit(RF_SX1278_DIO2, BOARD_PIN_L); } void RF_SX1278_DIO3_H(void) { GPIO_WriteBit(RF_SX1278_DIO3, BOARD_PIN_H); } void RF_SX1278_DIO3_L(void) { GPIO_WriteBit(RF_SX1278_DIO3, BOARD_PIN_L); } void RF_SX1278_DIO4_L(void) { GPIO_WriteBit(RF_SX1278_DIO4, BOARD_PIN_L); } void RF_SX1278_DIO5_L(void) { GPIO_WriteBit(RF_SX1278_DIO5, BOARD_PIN_L); } void RF_EXT_PA_RE_H(void) { GPIO_WriteBit(RF_EXTPA_RE, BOARD_PIN_H); } void RF_EXT_PA_RE_L(void) { GPIO_WriteBit(RF_EXTPA_RE, BOARD_PIN_L); } void RF_EXT_PA_TE_H(void) { GPIO_WriteBit(RF_EXTPA_TE, BOARD_PIN_H); } void RF_EXT_PA_TE_L(void) { GPIO_WriteBit(RF_EXTPA_TE, BOARD_PIN_L); } uint8_t READ_RF_SX1278_DIO0(void) { return GPIO_ReadInputDataBit(RF_SX1278_DIO0); } uint8_t READ_RF_SX1278_DIO1(void) { return GPIO_ReadInputDataBit(RF_SX1278_DIO1); } uint8_t READ_RF_SX1278_DIO2(void) { return GPIO_ReadInputDataBit(RF_SX1278_DIO2); } uint8_t READ_RF_SX1278_DIO3(void) { return GPIO_ReadInputDataBit(RF_SX1278_DIO3); } uint8_t READ_RF_SX1278_DIO4(void) { return GPIO_ReadInputDataBit(RF_SX1278_DIO4); } uint8_t READ_RF_SX1278_DIO5(void) { return GPIO_ReadInputDataBit(RF_SX1278_DIO5); } /** * @brief 引脚控制 * * @param sta =true,设置进入低功耗的引脚状态 * =false,设置退出低功耗的引脚状态 */ void myRadio_gpio_intoLwPwr(bool sta) { if (sta == false) { } else { } } void extiLine1_callback(uint8_t status, uint32_t param) { gpioCallback(1); } void myRadio_gpio_irq_init() { // NVIC_InitTypeDef NVIC_InitStructure; // EXTI_InitTypeDef EXTI_InitStructure; // GPIO_InitTypeDef GPIO_InitStructure; // myIrqCallback_extiLine1.thisCb = extiLine1_callback; // EXTILINE1_callbackRegiste(&myIrqCallback_extiLine1); // GPIO_InitStructure.GPIO_Pin = RF_SX1278_DIO1_PIN; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // GPIO_Init(RF_SX1278_DIO1_PORT, &GPIO_InitStructure); // EXTI_ClearITPendingBit(EXTI_Line1); // EXTI_InitStructure.EXTI_Line = EXTI_Line1; // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; // EXTI_InitStructure.EXTI_LineCmd = ENABLE; // EXTI_Init(&EXTI_InitStructure); // GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1); // /* Enable and set EXTI1 Interrupt */ // NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01; // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00; // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // NVIC_Init(&NVIC_InitStructure); } void myRadio_gpio_init(RADIO_GPIO_CALLBACK cb) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; #if defined(SPI_HARD) //----------SPI1时钟使能 RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE ); GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CLK | BOARD_PIN_SPI_MISO | BOARD_PIN_SPI_MOSI; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BOARD_PORT_SPI_CLK, &GPIO_InitStructure); GPIO_SetBits(BOARD_GPIO_SPI_CLK | BOARD_PIN_SPI_MISO | BOARD_PIN_SPI_MOSI); /*!< SPI configuration */ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; 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_16; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); /*!< Enable the SPI1 */ SPI_Cmd(SPI1, ENABLE); #else GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MOSI; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BOARD_PORT_SPI_MOSI, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MISO; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BOARD_PORT_SPI_MISO, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CLK; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BOARD_PORT_SPI_CLK, &GPIO_InitStructure); #endif GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CSN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(BOARD_PORT_SPI_CSN, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_SX1278_RESET_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_SX1278_RESET_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_SX1278_DIO0_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_SX1278_DIO0_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_SX1278_DIO1_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_SX1278_DIO1_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_SX1278_DIO2_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_SX1278_DIO2_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_SX1278_DIO3_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_SX1278_DIO3_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_SX1278_DIO4_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_SX1278_DIO4_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_SX1278_DIO5_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_SX1278_DIO5_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_EXTPA_RE_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_EXTPA_RE_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = RF_EXTPA_TE_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(RF_EXTPA_TE_PORT, &GPIO_InitStructure); BOARD_SPI_NSS_H(); // myRadio_gpio_irq_init(); if (cb) { gpioCallback = cb; } } uint8_t myRadioSpi_rwByte(uint8_t byteToWrite) { uint8_t i, temp; temp = 0; #if defined(SPI_HARD) while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位 { i++; if(i > 200)return 0; } SPI_I2S_SendData(SPI1, byteToWrite); //通过外设SPIx发送一个数据 i=0; while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)//检查指定的SPI标志位设置与否:接受缓存非空标志位 { i++; if(i > 200)return 0; } /*!< Return the byte read from the SPI bus */ temp = SPI_I2S_ReceiveData(SPI1); #else BOARD_SPI_SCK_L(); for(i = 0; i < 8; i ++) { if(byteToWrite & 0x80) { //SpiSetMosiHigh(); BOARD_SPI_MOSI_H(); } else { //SpiSetMosiLow(); BOARD_SPI_MOSI_L(); } byteToWrite <<= 1; //SpiSetSckHigh(); BOARD_SPI_SCK_H(); temp <<= 1; if(READ_BOARD_SPI_MISO()) { temp ++; } //SpiSetSckLow(); BOARD_SPI_SCK_L(); } #endif return temp; } void myRadioSpi_wBuffer(uint8_t* pData, uint8_t len) { uint8_t i; for(i = 0; i < len; i++) { myRadioSpi_rwByte(*pData); pData ++; } } void myRadioSpi_rBuffer(uint8_t* pData, uint8_t len) { uint8_t i; for(i = 0; i < len; i++) { *pData = myRadioSpi_rwByte(0xFF); pData ++; } } void myRadioSpi_rwBuffer(uint8_t* pDataR, uint8_t* pDataW, uint8_t len) { uint8_t i; for(i = 0; i < len; i++) { *pDataR = myRadioSpi_rwByte(*pDataW); pDataR ++; pDataW ++; } }