123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388 |
- // ========================================================
- /// @file SPI.c
- /// @brief Software simulation SPI performance function
- /// @version V1.0
- /// @date 2014/05/04
- /// @company HR_WT.
- /// @website http://www.HR_WT.com
- /// @author ISA
- /// @mobile
- /// @tel
- // ========================================================
- #include "sx1276_7_8_spi.h"
- #include "stm32f0xx.h"
- #include "mapp_gpio.h"
- #include "main.h"
- /**********************************************
- 函数功能;CS
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- void SX1278_SPI_nSEL_H(void)
- {
- GPIO_SetBits(GPIOA , GPIO_Pin_6);
- }
- /**********************************************
- 函数功能;SX1278 IO驱动
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- void SX1278_SPI_nSEL_L(void)
- {
- GPIO_ResetBits(GPIOA , GPIO_Pin_6);
- }
- /**********************************************
- 函数功能;SX1278 IO驱动
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- void SX1278_SPI_SCK_H(void)
- {
- GPIO_SetBits(GPIOA , GPIO_Pin_5);
- }
- /**********************************************
- 函数功能;SX1278 IO驱动
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- void SX1278_SPI_SCK_L(void)
- {
- GPIO_ResetBits(GPIOA , GPIO_Pin_5);
- }
- /**********************************************
- 函数功能;SX1278 IO驱动
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- void SX1278_SPI_MOSI_H(void)
- {
- #ifdef MAIN_433
- GPIO_SetBits(GPIOA , GPIO_Pin_0);
- #else
- GPIO_SetBits(GPIOA , GPIO_Pin_2);
- #endif
- }
- /**********************************************
- 函数功能;SX1278 IO驱动
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- void SX1278_SPI_MOSI_L(void)
- {
- #ifdef MAIN_433
- GPIO_ResetBits(GPIOA , GPIO_Pin_0);
- #else
- GPIO_ResetBits(GPIOA , GPIO_Pin_2);
- #endif
- }
- /**********************************************
- 函数功能;SX1278 IO驱动
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- unsigned char GetSX1278_SPI_MISO(void)
- {
- #ifdef MAIN_433
- return GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_1);
- #else
- return GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_3);
- #endif
- }
- /**********************************************
- 函数功能;SX1278 IO驱动
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- unsigned char Get_SX1278_NIRQ(void)
- {
- return GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_4);
- }
- /**********************************************
- 函数功能;SX1278 IO初始化
- 入口参数:
- 出口参数:
- 作者 :凡达电子,www.finedar.com
- QQ :75531552
- **********************************************/
- #ifdef MAIN_433
- void SX1278_Main_IO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable GPIO clocks */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
- /************PA******************
- PA0------------------1276_M0SI
- PA1----------------- 1276_MIS0
- PA2------------------
- PA3------------------
- PA4------------------1276_IRQ
- PA5------------------1276_CLK
- PA6------------------1276_CSN
- PA7------------------
- PA9------------------
- PA10----------------
- PA13----------------
- PA14----------------
- ****************************************/
-
-
- // 复用推挽输出
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_5| GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //GpioSpiSclCfg(SET);
- //GpioMOSICfg(SET);
-
- // PA3->MISO;PA4 PENINQ
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- // PB1->CE
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- #else
- void SX1278_IO_Init(void)
- {
- uint32_t u32prm = 0;
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Enable GPIO clocks */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
- /************PA******************
- PA0------------------RTP_SDO
- PA1----------------- CTP_RST
- PA2------------------1276_M0SI
- PA3------------------ 1276_MIS0
- PA4------------------1276_IRQ
- PA5------------------1276_CLK
- PA6------------------1276_CSN
- PA7------------------MCU_SCL
- PA9------------------MCU_SDA
- PA10---------------- TCP_INT 检测触摸按键按下
- PA13----------------
- PA14----------------
- ****************************************/
-
-
- // 复用推挽输出
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2| GPIO_Pin_5| GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GpioSpiSclCfg(SET);
- GpioMOSICfg(SET);
-
- // PA3->MISO;PA4 PENINQ
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- // PB1->CE
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB, GPIO_Pin_1);
-
- u32prm = ( (YKQADDER[0] <<24) | (YKQADDER[1] <<16) | (YKQADDER[2] <<8) | YKQADDER[4] ) ;
- gtArm.ykq_add = HexToInt(u32prm);
- }
- #endif
- /**********************************************************
- **Name: SPICmd8bit
- **Function: SPI Write one byte
- **Input: WrPara
- **Output: none
- **note: use for burst mode
- **********************************************************/
- void SPICmd8bit(unsigned char WrPara)
- {
- unsigned char bitcnt;
- SX1278_SPI_nSEL_L();
- SX1278_SPI_SCK_L();
-
- for(bitcnt=8; bitcnt!=0; bitcnt--)
- {
- SX1278_SPI_SCK_L();
- if(WrPara&0x80)
- SX1278_SPI_MOSI_H();
- else
- SX1278_SPI_MOSI_L();
- SX1278_SPI_SCK_H();
- WrPara <<= 1;
- }
- SX1278_SPI_SCK_L();
- SX1278_SPI_MOSI_H();
- }
- /**********************************************************
- **Name: SPIRead8bit
- **Function: SPI Read one byte
- **Input: None
- **Output: result byte
- **Note: use for burst mode
- **********************************************************/
- unsigned char SPIRead8bit(void)
- {
- unsigned char RdPara = 0;
- unsigned char bitcnt;
-
- SX1278_SPI_nSEL_L();
- SX1278_SPI_MOSI_H(); //Read one byte data from FIFO, MOSI hold to High
- for(bitcnt=8; bitcnt!=0; bitcnt--)
- {
- SX1278_SPI_SCK_L();
- RdPara <<= 1;
- SX1278_SPI_SCK_H();
- if(GetSX1278_SPI_MISO())
- RdPara |= 0x01;
- else
- RdPara |= 0x00;
- }
- SX1278_SPI_SCK_L();
- return(RdPara);
- }
- /**********************************************************
- **Name: SPIRead
- **Function: SPI Read CMD
- **Input: adr -> address for read
- **Output: None
- **********************************************************/
- unsigned char SPIRead(unsigned char adr)
- {
- unsigned char tmp;
- SPICmd8bit(adr); //Send address first
- tmp = SPIRead8bit();
- SX1278_SPI_nSEL_H();
- return(tmp);
- }
- /**********************************************************
- **Name: SPIWrite
- **Function: SPI Write CMD
- **Input: unsigned char address & unsigned char data
- **Output: None
- **********************************************************/
- void SPIWrite(unsigned char adr, unsigned char WrPara)
- {
-
- SX1278_SPI_nSEL_L();
-
- SPICmd8bit(adr|0x80); //写入地址
- SPICmd8bit(WrPara);//写入数据
-
- SX1278_SPI_SCK_L();
- SX1278_SPI_MOSI_H();
- SX1278_SPI_nSEL_H();
- }
- /**********************************************************
- **Name: SPIBurstRead
- **Function: SPI burst read mode
- **Input: adr-----address for read
- ** ptr-----data buffer point for read
- ** length--how many bytes for read
- **Output: None
- **********************************************************/
- void SPIBurstRead(unsigned char adr, unsigned char *ptr, unsigned char length)
- {
- unsigned char i;
- if(length<=1) //length must more than one
- return;
- else
- {
- SX1278_SPI_SCK_L();
- SX1278_SPI_nSEL_L();
- SPICmd8bit(adr);
- for(i=0;i<length;i++)
- ptr[i] = SPIRead8bit();
- SX1278_SPI_nSEL_H();
- }
- }
- /**********************************************************
- **Name: SPIBurstWrite
- **Function: SPI burst write mode
- **Input: adr-----address for write
- ** ptr-----data buffer point for write
- ** length--how many bytes for write
- **Output: none
- **********************************************************/
- void BurstWrite(unsigned char adr, unsigned char *ptr, unsigned char length)
- {
- unsigned char i;
- if(length<=1) //length must more than one
- return;
- else
- {
- SX1278_SPI_SCK_L();
- SX1278_SPI_nSEL_L();
- SPICmd8bit(adr|0x80);
- for(i=0;i<length;i++)
- SPICmd8bit(ptr[i]);
- SX1278_SPI_nSEL_H();
- }
- }
|