123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490 |
- #include "myRadio_gpio.h"
- #include "misc.h"
- #include "stm32f10x.h"
- #include "stm32f10x_exti.h"
- #include "stm32f10x_it.h"
- RADIO_GPIO_CALLBACK gpioCallback;
- static irqCallback_ts myIrqCallback_extiLine2;
- uint8_t sdioMode = 255;
- void gpio_delayUs(uint32_t time)
- {
- delay_us(time);
- }
- void gpio_delayMs(uint32_t time)
- {
- delay_ms(time);
- }
- //---------------------------射频SPI驱动部分---------------------
- void BOARD_A5133_SCS_H(void)
- {
- GPIO_WriteBit(RF_A5133_SCS, BOARD_PIN_H);
- }
- void BOARD_A5133_SCS_L(void)
- {
- GPIO_WriteBit(RF_A5133_SCS, BOARD_PIN_L);
- }
- void BOARD_A5133_SCK_H(void)
- {
- GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_H);
- }
- void BOARD_A5133_SCK_L(void)
- {
- GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_L);
- }
- void SET_A5133_SDIO_IN(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- if (sdioMode == 0)
- {
- return;
- }
- sdioMode = 0;
- GPIO_InitStructure.GPIO_Pin = RF_A5133_SDIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_SDIO_PORT, &GPIO_InitStructure);
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- }
- void SET_A5133_SDIO_OUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- if (sdioMode == 1)
- {
- return;
- }
- sdioMode = 1;
- GPIO_InitStructure.GPIO_Pin = RF_A5133_SDIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_SDIO_PORT, &GPIO_InitStructure);
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- }
- void BOARD_A5133_SDIO_H(void)
- {
- SET_A5133_SDIO_OUT();
- GPIO_WriteBit(RF_A5133_SDIO, BOARD_PIN_H);
- }
- void BOARD_A5133_SDIO_L(void)
- {
- SET_A5133_SDIO_OUT();
- GPIO_WriteBit(RF_A5133_SDIO, BOARD_PIN_L);
- }
- uint8_t READ_RF_A5133_SDIO(void)
- {
- SET_A5133_SDIO_IN();
- return GPIO_ReadInputDataBit(RF_A5133_SDIO);
- }
- //---------------------------射频驱动IO部分---------------------
- void SET_A5133_GPIO1_IN(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO1_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_GPIO1_PORT, &GPIO_InitStructure);
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- }
- void SET_A5133_GPIO1_OUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO1_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_GPIO1_PORT, &GPIO_InitStructure);
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- }
- void RF_A5133_GPIO1_H(void)
- {
- GPIO_WriteBit(RF_A5133_GPIO1, BOARD_PIN_H);
- }
- void RF_A5133_GPIO1_L(void)
- {
- GPIO_WriteBit(RF_A5133_GPIO1, BOARD_PIN_L);
- }
- void SET_A5133_GPIO2_IN(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO2_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_GPIO2_PORT, &GPIO_InitStructure);
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- }
- void SET_A5133_GPIO2_OUT(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO2_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_GPIO2_PORT, &GPIO_InitStructure);
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- __NOP();
- }
- void RF_A5133_GPIO2_H(void)
- {
- GPIO_WriteBit(RF_A5133_GPIO2, BOARD_PIN_H);
- }
- void RF_A5133_GPIO2_L(void)
- {
- GPIO_WriteBit(RF_A5133_GPIO2, BOARD_PIN_L);
- }
- void RF_A5133_CKO_H(void)
- {
- GPIO_WriteBit(RF_A5133_CKO, BOARD_PIN_H);
- }
- void RF_A5133_CKO_L(void)
- {
- GPIO_WriteBit(RF_A5133_CKO, 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);
- }
- void RF_EXT_PA_CTR_H(void)
- {
- GPIO_WriteBit(RF_EXTPA_CTR, BOARD_PIN_H);
- }
- void RF_EXT_PA_CTR_L(void)
- {
- GPIO_WriteBit(RF_EXTPA_CTR, BOARD_PIN_L);
- }
- uint8_t READ_RF_A5133_GPIO1(void)
- {
- return GPIO_ReadInputDataBit(RF_A5133_GPIO1);
- }
- /**
- * @brief 引脚控制
- *
- * @param sta =true,设置进入低功耗的引脚状态
- * =false,设置退出低功耗的引脚状态
- */
- void myRadio_gpio_intoLwPwr(bool sta)
- {
- if (sta == false)
- {
- }
- else
- {
- }
- }
- void extiLine2_callback(uint8_t status, uint32_t param)
- {
- if (gpioCallback)
- {
- gpioCallback(1);
- }
- }
- void myRadio_gpio_irq_init()
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- myIrqCallback_extiLine2.thisCb = extiLine2_callback;
- EXTILINE2_callbackRegiste(&myIrqCallback_extiLine2);
- GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO1_PIN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(RF_A5133_GPIO1_PORT, &GPIO_InitStructure);
- EXTI_ClearITPendingBit(EXTI_Line2);
- EXTI_InitStructure.EXTI_Line = EXTI_Line2;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
- /* Enable and set EXTI1 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = EXTI2_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) && defined(SPI_4LINE)
- //----------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_8;
- 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 = RF_A5133_SDIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_SDIO_PORT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = RF_A5133_SCK_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_SCK_PORT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO2_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_GPIO2_PORT, &GPIO_InitStructure);
- BOARD_A5133_SDIO_H();
- #endif
- GPIO_InitStructure.GPIO_Pin = RF_A5133_SCS_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_SCS_PORT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO1_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_GPIO1_PORT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = RF_A5133_CKO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_A5133_CKO_PORT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = RF_EXTPA_CTR_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(RF_EXTPA_CTR_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_A5133_SCS_H();
- BOARD_A5133_SCK_L();
- RF_A5133_GPIO1_H();
- myRadio_gpio_irq_init();
- if (cb)
- gpioCallback = cb;
- }
- uint8_t myRadioSpi_rwByte(uint8_t byteToWrite)
- {
- #if defined(SPI_HARD)
- uint8_t i, temp;
- temp = 0;
- 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)
- // {
- // BOARD_SPI_MOSI_H();
- // }
- // else
- // {
- // BOARD_SPI_MOSI_L();
- // }
- // byteToWrite <<= 1;
- // BOARD_SPI_SCK_H();
- // temp <<= 1;
- // if(READ_BOARD_SPI_MISO())
- // {
- // temp ++;
- // }
- // BOARD_SPI_SCK_L();
- // }
- uint8_t i, temp;
- temp = 0;
- // BOARD_A5133_SCK_L();
- for (i = 0; i < 8; i++)
- {
- if (byteToWrite & 0x80)
- {
- BOARD_A5133_SDIO_H();
- }
- else
- {
- BOARD_A5133_SDIO_L();
- }
- byteToWrite <<= 1;
- gpio_delayUs(10);
- BOARD_A5133_SCK_H();
- temp <<= 1;
- if (READ_RF_A5133_SDIO())
- {
- temp++;
- }
- BOARD_A5133_SCK_L();
- }
- #endif
- return temp;
- }
- /**
- * @brief
- * 下降沿后,RF芯片会改变SDIO的状态
- * 上升沿后才能读取SDIO状态
- * @return uint8_t
- */
- uint8_t myRadioSpi_rByte(void)
- {
- uint8_t i, temp;
- // temp = 0;
- // // BOARD_A5133_SCK_L();
- // for (i = 0; i < 8; i++)
- // {
- // gpio_delayUs(10);
- // temp <<= 1;
- // if (READ_RF_A5133_SDIO())
- // {
- // temp++;
- // }
- // BOARD_A5133_SCK_H();
- // gpio_delayUs(10);
- // BOARD_A5133_SCK_L();
- // }
- temp = myRadioSpi_rwByte(0x00);
- return temp;
- }
- /**
- * @brief
- * 在上升沿之前需设置好SDIO的状态
- * @param byteToWrite
- */
- void myRadioSpi_wByte(uint8_t byteToWrite)
- {
- // uint8_t i;
- // BOARD_A5133_SCK_L();
- // for (i = 0; i < 8; i++)
- // {
- // if (byteToWrite & 0x80)
- // {
- // BOARD_A5133_SDIO_H();
- // }
- // else
- // {
- // BOARD_A5133_SDIO_L();
- // }
- // byteToWrite <<= 1;
- // gpio_delayUs(10);
- // BOARD_A5133_SCK_H();
- // gpio_delayUs(10);
- // BOARD_A5133_SCK_L();
- // }
- myRadioSpi_rwByte(byteToWrite);
- }
- 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++;
- }
- }
|