void logic_init(void) { SPI_InitTypeDef SPI_InitStructure; logic_reset(); SPI_I2S_DeInit(SPI3); 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_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_Init(SPI3, &SPI_InitStructure); SPI_Cmd(SPI3, ENABLE); GPIO_SetBits(LOGIC_CSN_PIN); logic_read_flags(); }
/*spi4 initialize */ void enable_spi4() { GPIO_InitTypeDef GPIO_InitStruct; SPI_InitTypeDef SPI_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI4, ENABLE); GPIO_PinAFConfig(GPIOE, GPIO_PinSource2, GPIO_AF_SPI4); GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_SPI4); GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_SPI4); /* CSN PB12 */ GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_5 | GPIO_Pin_6; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOE, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_Init(GPIOE, &GPIO_InitStruct); SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // 雙線全雙工 SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // 主模式 SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // 數據大小8位 SPI_InitStruct.SPI_CPOL = SPI_CPOL_High; // 時鐘極性,空閒時為低 SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge; // 第1個邊沿有效,上升沿為采樣時刻 SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // NSS信號由軟件產生 SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 8分頻,9MHz SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; // 高位在前 SPI_InitStruct.SPI_CRCPolynomial = 7; SPI_Init(SPI4, &SPI_InitStruct); SPI_Cmd(SPI4, ENABLE); }
void SPI_ini(void) { SPI_InitTypeDef SPI_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); SPI_InitStruct.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_32; SPI_InitStruct.SPI_Direction=SPI_Direction_2Lines_FullDuplex; SPI_InitStruct.SPI_Mode=SPI_Mode_Master; SPI_InitStruct.SPI_DataSize=SPI_DataSize_8b; SPI_InitStruct.SPI_FirstBit=SPI_FirstBit_MSB; SPI_InitStruct.SPI_CPOL=SPI_CPOL_High; SPI_InitStruct.SPI_CPHA=SPI_CPHA_1Edge; SPI_InitStruct.SPI_NSS=SPI_NSS_Soft|SPI_NSSInternalSoft_Set; SPI_Init(SPI1,&SPI_InitStruct); GPIO_SetBits(GPIOE,GPIO_Pin_8); SPI_Cmd(SPI1,ENABLE); for(int i=0;i<100000;i++); }
void hal_spi_c::init_spi(spi_hw_conf_t *hw) { #if 0 SPI_InitTypeDef SPI; if (hw->SPIx_RCC == RCC_APB2Periph_SPI1) RCC_APB2PeriphClockCmd(hw->SPIx_RCC, ENABLE); else RCC_APB1PeriphClockCmd(hw->SPIx_RCC, ENABLE); SPI_I2S_DeInit(SPIx); SPI.SPI_Direction = SPIx_Direction; // set to full duplex mode, separate MOSI and MISO lines SPI.SPI_Mode = SPIx_Mode_SPI; // transmit in master mode, NSS pin has to be always high SPI.SPI_DataSize = SPIx_DataSize; // one packet of data is 8 bits wide SPI.SPI_CPOL = SPIx_CPOL; // clock is low when idle SPI.SPI_CPHA = SPIx_CPHA; // data sampled at first edge SPI.SPI_NSS = SPIx_NSS; // set the NSS management to internal and pull internal NSS high SPI.SPI_BaudRatePrescaler = SPIx_BaudRatePrescaler; // SPI frequency is APB2 frequency / 32 SPI.SPI_FirstBit = SPIx_FirstBit; // data is transmitted MSB firs SPI.SPI_CRCPolynomial = SPIx_CRCPolynomial; SPI_Init(hw->SPIx, &SPI); SPI_RxFIFOThresholdConfig(hw->SPIx, SPI_RxFIFOThreshold_QF); SPI_Cmd(hw->SPIx, ENABLE); // enable SPI #endif }
void rt_hw_spi2_init(void) { GPIO_InitTypeDef GPIO_InitStruct; SPI_InitTypeDef SPI_InitStruct; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); CS_Configuration(); GPIO_PinAFConfig(GPIOC,GPIO_PinSource2,GPIO_AF_SPI2); GPIO_PinAFConfig(GPIOC,GPIO_PinSource3,GPIO_AF_SPI2); GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_SPI2); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13; GPIO_Init(GPIOB, &GPIO_InitStruct); SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // ????? SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // ??? SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // ????8? SPI_InitStruct.SPI_CPOL = SPI_CPOL_High; // ????,????? SPI_InitStruct.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // NSS??????? SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 8??,9MHz SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; // ???? SPI_InitStruct.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStruct); SPI_Cmd(SPI2, ENABLE); rt_spi_bus_register(&spi2_bus,"spi2",(struct rt_spi_ops *)&spi2_ops); }
/** * @brief 初始化SPI0 * @param * @retval None */ void SPI0_Init(void) { CKCU_PeripClockConfig_TypeDef CKCUClock = {{0}}; /* Enable PA & SPI0 & AFIO */ CKCUClock.Bit.PA = 1; CKCUClock.Bit.SPI0 = 1; CKCUClock.Bit.AFIO = 1; CKCU_PeripClockConfig(CKCUClock, ENABLE); /* SPI0_SEL idle state is HIGH */ GPIO_PullResistorConfig(HT_GPIOA, GPIO_PIN_8, GPIO_PR_UP); /* Configure related IO to SPI0 */ AFIO_GPAConfig(AFIO_PIN_4 | AFIO_PIN_5 | AFIO_PIN_6 | AFIO_PIN_7, AFIO_MODE_5); /* SPI0 configuration */ SPI_InitStructure.SPI_Mode = SPI_MASTER; SPI_InitStructure.SPI_FIFO = SPI_FIFO_DISABLE; SPI_InitStructure.SPI_DataLength = SPI_DATALENGTH_8; SPI_InitStructure.SPI_SELMode = SPI_SEL_SOFTWARE; SPI_InitStructure.SPI_SELPolarity = SPI_SELPOLARITY_LOW; SPI_InitStructure.SPI_CPOL = SPI_CPOL_LOW; SPI_InitStructure.SPI_CPHA = SPI_CPHA_FIRST; SPI_InitStructure.SPI_FirstBit = SPI_FIRSTBIT_MSB; SPI_InitStructure.SPI_RxFIFOTriggerLevel = 0; SPI_InitStructure.SPI_TxFIFOTriggerLevel = 0; SPI_InitStructure.SPI_ClockPrescaler = 4; SPI_Init(HT_SPI0, &SPI_InitStructure); /* Set SEL as output mode for slave select */ SPI_SELOutputCmd(HT_SPI0, ENABLE); /* Enable SPI0 */ SPI_Cmd(HT_SPI0, ENABLE); }
void SPI1_Init(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /*使能SPI1和GPIOA的时钟*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1|RCC_APB2Periph_GPIOA, ENABLE); /*SPI1 接口信号配置*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /*分配给ENC28J60芯片的SPI1_NSS信号配置*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_SetBits(GPIOA, GPIO_Pin_4); //置高分配给ENC28J60的 SPI1_NSS信号 /* SPI1接口模式参数配置 */ 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); /*使能SPI1接口*/ SPI_Cmd(SPI1, ENABLE); }
/** * @brief Initializes the SPI * @param None * @retval None */ void HEX_SPI_Init() { // Enable GPIOB clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; // Configure SPI2-NSS, SPI2-SCK, SPI2-MOSI as output GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); // Configure SPI2-MISO as input floating GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); // Enable SPI2 Peripheral clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); SPI_InitTypeDef SPI_InitStructure; 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_2Edge; // ??? SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; // Hard or soft for chip select??? SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; // 24MHz/8 = 3 MHz, Test faster later SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; // ??? SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); HEX_SPI_WriteRead(0xAA); }
void ssp3_init(void) { #if 0 SPI_InitTypeDef SPI_InitStructure; int_dis(INT_SRC_NBR_SPI3); sFLASH_LowLevel_Init(); /*!< Deselect the FLASH: Chip Select high */ mx25l64_cs_set(); /*!< 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_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI3, &SPI_InitStructure); /*!< Enable the sFLASH_SPI */ SPI_Cmd(SPI3, ENABLE); #endif }
/** * @brief SET SPI protocol * @param None * @retval None */ static void drv95HF_StructureConfigSPI ( void ) { SPI_InitTypeDef SPI_InitStructure; /* Initialize the SPI with default values */ SPI_StructInit(&SPI_InitStructure); /* SPI Config master with NSS manages by software using the SSI bit*/ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; /* Init the SPI BRIDGE */ SPI_Init(RFTRANS_95HF_SPI, &SPI_InitStructure); #ifdef USE_DMA SPI_I2S_DMACmd(RFTRANS_95HF_SPI, SPI_I2S_DMAReq_Rx, ENABLE); SPI_I2S_DMACmd(RFTRANS_95HF_SPI, SPI_I2S_DMAReq_Tx, ENABLE); #endif /* Enable SPI */ SPI_Cmd(RFTRANS_95HF_SPI, ENABLE); }
/** * 音频设备初始化 * */ void AUDIO_Init() { SPI_InitTypeDef SPI_InitStructure; // AUDIO_Lowlevel_Init(); /*!< Deselect the FLASH: Chip Select high */ AUDIO_CS_HIGH(); /*!< 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_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(AUDIO_SPI, &SPI_InitStructure); /*使能SPII*/ SPI_Cmd(AUDIO_SPI, ENABLE); }
void GPIO_Configuration(void) { uint32_t i; SPI_InitTypeDef SPI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; for(i=0;i<sizeof(pins_spi)/sizeof(PIN_SPI);i++) { RCC_APB2PeriphClockCmd(pins_spi[i].GPIO_Bus,ENABLE); GPIO_Init(pins_spi[i].GPIOx,&pins_spi[i].GPIO_InitStructure); } GPIO_SetBits(CS_Port,CS_Pin); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); 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_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); SPI_I2S_ITConfig(SPI1, SPI_I2S_IT_RXNE, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = SPI1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 4; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }
void ILI9341_InitPins() { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_2| GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); 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 SPI1 */ SPI_Cmd(SPI1, ENABLE); }
/* spi flash 设备初始化 */ void w25x64_init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1,ENABLE); GPIO_InitStructure.GPIO_Pin = PC_CS_FLASH; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIO_CS_FLASH, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA,&GPIO_InitStructure); SPI_FLASH_CS_HIGH(); 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_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; //CLK上升沿采样,因为上升沿是第二个边沿动作,所以也可以理解为第二个边沿采样 SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; ////SPI频率 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; //crc7,stm32spi带硬件ecc SPI_Init(SPI1, &SPI_InitStructure); SPI_Cmd(SPI1, ENABLE); SPI_FLASH_CS_HIGH(); }
void SPI_Configuration(void) { //SPI_StructInit(&SPI_InitStructure); SPI_InitTypeDef SPI_InitStructure; GPIO_SetBits(PORT_SIG_GYRO_CS,PIN_SIG_GYRO_CS); GPIO_SetBits(PORT_SIG_ACC_CS,PIN_SIG_ACC_CS); 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_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; //SPI_Init(SPI1, &SPI_InitStructure); SPI_Init(SPI2, &SPI_InitStructure); /* Enable SPI2 RXNE interrupt */ //SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_RXNE, DISABLE); //SPI_I2S_ITConfig(SPI2, SPI_I2S_IT_TXE, DISABLE); /* Enable SPI1 */ //SPI_Cmd(SPI1, ENABLE); /* Enable SPI2 */ SPI_Cmd(SPI2, ENABLE); }
static void spi_config(void) { #if RT_USING_SERIAL_FLASH || EM_ALL_TYPE_BASE SPI_InitTypeDef SPI_InitStructure; #endif #if RT_USING_SERIAL_FLASH extern void spiflash_spi_cfg(void); spiflash_spi_cfg(); #endif #if EM_ALL_TYPE_BASE /* SPI2 configuration */ /* * SPI2 clk -- APB1, fCLK1(max = 36MHz) * enc28j60's clk(max) = 10MHz * DS80349C.errata #1:When the SPI clock from the host microcontroller is run at frequencies of less than 8 MHz, * reading or writing to the MAC registers may be unreliable. */ 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_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); #endif //ade7880_spi_cfg(); return; }
void CC_t::Init(void) { // ******** Hardware init section ******* // SPI clock init RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); // ==== GPIO init ==== klGpio::SetupByMsk(GPIOB, CC_CS, GPIO_Mode_Out_PP); // Configure CC_CS as Push-Pull output klGpio::SetupByMsk(GPIOB, CC_SCLK | CC_MOSI, GPIO_Mode_AF_PP); // Configure MOSI & SCK as Alternate Function Push Pull klGpio::SetupByMsk(GPIOB, CC_MISO, GPIO_Mode_IN_FLOATING); // Configure MISO as Input Floating //klGpio::SetupByMsk(GPIOB, CC_GDO0, GPIO_Mode_IPU); // Configure CC_GDO as Input Pull-up this->CS_Hi(); // ==== IRQ ==== IrqPin.Init(GPIOB, 4, GPIO_Mode_IPU); IrqPin.IrqSetup(EXTI_Trigger_Rising); IrqPin.IrqDisable(); // ==== SPI init ==== MSB first, master, SCK idle low SPI_InitTypeDef SPI_InitStructure; 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_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); // ******* Firmware init section ******* Reset(); FlushRxFIFO(); RfConfig(); IrqPin.IrqEnable(); }
void spi_init(SPI_TypeDef *spi) { /* System clocks configuration ---------------------------------------------*/ rcc_configuration(); /* GPIO configuration ------------------------------------------------------*/ spi_gpio_configuration(); /* SPI1 Config -------------------------------------------------------------*/ SPI_I2S_DeInit(spi); SPI_InitTypeDef SPI_InitStructure; 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_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(spi, &SPI_InitStructure); /* Enable SPI1 */ SPI_Cmd(spi, ENABLE); }
/************************************************* 名称:spi_init(void) 功能:spi外设1初始化 输入参数:无 输出参数:无 返回值: 无 **************************************************/ void spi_init(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* 配置SPI1管脚 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 |GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* SPI1配置选项 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE); 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); /* 使能SPI1 */ SPI_Cmd(SPI1, ENABLE); }
void SPI1_Configuration(void) { SPI_InitTypeDef SPI_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd (RCC_APB2Periph_SPI1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; // SS: output GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); SPI1_Deselect(); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; // CLK, MOSI: output GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // MISO: input GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); 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; // 60MHz/8 = 7.5MHz SPI bitrate SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); // SPI_RxFIFOThresholdConfig(SPI1, SPI_RxFIFOThreshold_QF); SPI_CalculateCRC(SPI1, DISABLE); SPI_Cmd(SPI1, ENABLE); }
void spiInit(SPI_TypeDef *SPI) { volatile uint8_t dummyread __attribute__((unused)); GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /////////////////////////////////// if (SPI == SPI1) { RCC_AHB1PeriphClockCmd(SPI1_GPIO_CLOCK, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); GPIO_PinAFConfig(SPI1_GPIO, SPI1_SCK_PIN_SOURCE, GPIO_AF_SPI1); GPIO_PinAFConfig(SPI1_GPIO, SPI1_MISO_PIN_SOURCE, GPIO_AF_SPI1); GPIO_PinAFConfig(SPI1_GPIO, SPI1_MOSI_PIN_SOURCE, GPIO_AF_SPI1); GPIO_StructInit(&GPIO_InitStructure); // Init pins GPIO_InitStructure.GPIO_Pin = SPI1_SCK_PIN | SPI1_MISO_PIN | SPI1_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(SPI1_GPIO, &GPIO_InitStructure); /////////////////////////////// SPI_StructInit(&SPI_InitStructure); 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_128; // 42/128 = 328.125 kHz SPI Clock SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); SPI_CalculateCRC(SPI1, DISABLE); SPI_Cmd(SPI1, ENABLE); while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); dummyread = SPI_I2S_ReceiveData(SPI1); } /////////////////////////////////// if (SPI == SPI2) { RCC_AHB1PeriphClockCmd(SPI2_GPIO_CLOCK, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); GPIO_StructInit(&GPIO_InitStructure); // Init pins GPIO_InitStructure.GPIO_Pin = SPI2_SCK_PIN | SPI2_MISO_PIN | SPI2_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(SPI2_GPIO, &GPIO_InitStructure); GPIO_PinAFConfig(SPI2_GPIO, SPI2_SCK_PIN_SOURCE, GPIO_AF_SPI2); GPIO_PinAFConfig(SPI2_GPIO, SPI2_MISO_PIN_SOURCE, GPIO_AF_SPI2); GPIO_PinAFConfig(SPI2_GPIO, SPI2_MOSI_PIN_SOURCE, GPIO_AF_SPI2); /////////////////////////////// RCC_AHB1PeriphClockCmd(MAX7456_CS_GPIO_CLOCK, ENABLE); GPIO_InitStructure.GPIO_Pin = MAX7456_CS_PIN; 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_NOPULL; GPIO_Init(MAX7456_CS_GPIO, &GPIO_InitStructure); /////////////////////////////// SPI_StructInit(&SPI_InitStructure); 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_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; // 42/4 = 10.5 MHz SPI Clock SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); SPI_Cmd(SPI2, ENABLE); SPI_CalculateCRC(SPI2, DISABLE); SPI_Cmd(SPI2, ENABLE); DISABLE_MAX7456; while (SPI_I2S_GetFlagStatus(MAX7456_SPI, SPI_I2S_FLAG_TXE) == RESET); dummyread = SPI_I2S_ReceiveData(MAX7456_SPI); } /////////////////////////////////// if (SPI == SPI3) { RCC_AHB1PeriphClockCmd(SPI3_GPIO_CLOCK, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE); GPIO_StructInit(&GPIO_InitStructure); // Init pins GPIO_InitStructure.GPIO_Pin = SPI3_SCK_PIN | SPI3_MISO_PIN | SPI3_MOSI_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(SPI3_GPIO, &GPIO_InitStructure); GPIO_PinAFConfig(SPI3_GPIO, SPI3_SCK_PIN_SOURCE, GPIO_AF_SPI3); GPIO_PinAFConfig(SPI3_GPIO, SPI3_MISO_PIN_SOURCE, GPIO_AF_SPI3); GPIO_PinAFConfig(SPI3_GPIO, SPI3_MOSI_PIN_SOURCE, GPIO_AF_SPI3); GPIO_StructInit(&GPIO_InitStructure); /////////////////////////////// RCC_AHB1PeriphClockCmd(MPU6000_CS_GPIO_CLOCK, ENABLE); GPIO_InitStructure.GPIO_Pin = MPU6000_CS_PIN; 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_NOPULL; GPIO_Init(MPU6000_CS_GPIO, &GPIO_InitStructure); /////////////////////////////// SPI_StructInit(&SPI_InitStructure); 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_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // 42/64 = 0.65625 MHz SPI Clock SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI3, &SPI_InitStructure); SPI_Cmd(SPI3, ENABLE); SPI_CalculateCRC(SPI3, DISABLE); SPI_Cmd(SPI3, ENABLE); DISABLE_MPU6000; while (SPI_I2S_GetFlagStatus(MPU6000_SPI, SPI_I2S_FLAG_TXE) == RESET); dummyread = SPI_I2S_ReceiveData(MPU6000_SPI); } /////////////////////////////////// }
/** * @brief Initializes the low level interface used to drive the L3GD20 * @param None * @retval None */ static void L3GD20_LowLevel_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /* Enable the SPI periph */ RCC_APB2PeriphClockCmd(L3GD20_SPI_CLK, ENABLE); /* Enable SCK, MOSI and MISO GPIO clocks */ RCC_AHB1PeriphClockCmd(L3GD20_SPI_SCK_GPIO_CLK | L3GD20_SPI_MISO_GPIO_CLK | L3GD20_SPI_MOSI_GPIO_CLK, ENABLE); /* Enable CS GPIO clock */ RCC_AHB1PeriphClockCmd(L3GD20_SPI_CS_GPIO_CLK, ENABLE); /* Enable INT1 GPIO clock */ RCC_AHB1PeriphClockCmd(L3GD20_SPI_INT1_GPIO_CLK, ENABLE); /* Enable INT2 GPIO clock */ RCC_AHB1PeriphClockCmd(L3GD20_SPI_INT2_GPIO_CLK, ENABLE); GPIO_PinAFConfig(L3GD20_SPI_SCK_GPIO_PORT, L3GD20_SPI_SCK_SOURCE, L3GD20_SPI_SCK_AF); GPIO_PinAFConfig(L3GD20_SPI_MISO_GPIO_PORT, L3GD20_SPI_MISO_SOURCE, L3GD20_SPI_MISO_AF); GPIO_PinAFConfig(L3GD20_SPI_MOSI_GPIO_PORT, L3GD20_SPI_MOSI_SOURCE, L3GD20_SPI_MOSI_AF); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //GPIO_PuPd_NOPULL;// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_SCK_PIN; GPIO_Init(L3GD20_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_MOSI_PIN; GPIO_Init(L3GD20_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_MISO_PIN; GPIO_Init(L3GD20_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); /* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(L3GD20_SPI); SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; 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_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(L3GD20_SPI, &SPI_InitStructure); /* Enable SPI1 */ SPI_Cmd(L3GD20_SPI, ENABLE); /* Configure GPIO PIN for Lis Chip select */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(L3GD20_SPI_CS_GPIO_PORT, &GPIO_InitStructure); /* Deselect : Chip Select high */ GPIO_SetBits(L3GD20_SPI_CS_GPIO_PORT, L3GD20_SPI_CS_PIN); /* Configure GPIO PINs to detect Interrupts */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_INT1_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(L3GD20_SPI_INT1_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_INT2_PIN; GPIO_Init(L3GD20_SPI_INT2_GPIO_PORT, &GPIO_InitStructure); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); /* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration(); /* SPI_SLAVE_Rx_DMA_Channel configuration ---------------------------------------------*/ DMA_DeInit(SPI_SLAVE_Rx_DMA_Channel); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPI_SLAVE_DR_Base; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)SPI_SLAVE_Buffer_Rx; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = BufferSize; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(SPI_SLAVE_Rx_DMA_Channel, &DMA_InitStructure); /* SPI_MASTER configuration ------------------------------------------------------*/ SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Hard; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI_MASTER, &SPI_InitStructure); /* SPI_SLAVE configuration ------------------------------------------------------*/ SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Rx; SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPI_SLAVE, &SPI_InitStructure); /* Enable SPI_MASTER NSS output for master mode */ SPI_SSOutputCmd(SPI_MASTER, ENABLE); /* Enable SPI_SLAVE Rx request */ SPI_I2S_DMACmd(SPI_SLAVE, SPI_I2S_DMAReq_Rx, ENABLE); /* Enable SPI_SLAVE */ SPI_Cmd(SPI_SLAVE, ENABLE); /* Enable SPI_MASTER */ SPI_Cmd(SPI_MASTER, ENABLE); /* Enable DMA1 Channel4 */ DMA_Cmd(SPI_SLAVE_Rx_DMA_Channel, ENABLE); /* Transfer procedure */ while (TxIdx < BufferSize) { /* Wait for SPI_MASTER Tx buffer empty */ while (SPI_I2S_GetFlagStatus(SPI_MASTER, SPI_I2S_FLAG_TXE) == RESET); /* Send SPI_MASTER data */ SPI_I2S_SendData(SPI_MASTER, SPI_MASTER_Buffer_Tx[TxIdx++]); } /* Wait for DMA1 channel4 transfer complete */ while (!DMA_GetFlagStatus(SPI_SLAVE_Rx_DMA_FLAG)); /* Check the corectness of written data */ TransferStatus = Buffercmp(SPI_SLAVE_Buffer_Rx, SPI_MASTER_Buffer_Tx, BufferSize); /* TransferStatus = PASSED, if the transmitted and received data are equal */ /* TransferStatus = FAILED, if the transmitted and received data are different */ while (1) {} }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* SPI configuration ------------------------------------------------------*/ SPI_Config(); /* SysTick configuration ---------------------------------------------------*/ SysTickConfig(); /* Initialize LEDs mounted on STM320518-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Master board configuration ------------------------------------------------*/ #ifdef SPI_MASTER /* Initialize push-buttons mounted on STM320518-EVAL board */ STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_UP, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_DOWN, BUTTON_MODE_GPIO); STM_EVAL_PBInit(BUTTON_SEL, BUTTON_MODE_GPIO); /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPIx, &SPI_InitStructure); /* Initialize the FIFO threshold */ SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF); /* TIM configuration ------------------------------------------------------*/ TIM_Config(); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); /* Enable NSS output for master mode */ SPI_SSOutputCmd(SPIx, ENABLE); /* TIM Capture Compare DMA Request enable */ TIM_DMACmd(TIMx, TIMx_DMA_CHANNEL, ENABLE); while (1) { /* DMA channel Rx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = (uint16_t)1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &CommandReceived; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure); /* DMA TIM trigger channel Configuration */ DMA_InitStructure.DMA_BufferSize = (uint16_t)1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &CommandTransmitted; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(TIMx_CHANNEL_DMA_CHANNEL, &DMA_InitStructure); /* Enable the SPI Rx DMA request */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); CommandTransmitted = 0x00; CommandReceived = 0x00; /* Clear the RxBuffer */ Fill_Buffer(RxBuffer, TXBUFFERSIZE); PressedButton = Read_Joystick(); while (PressedButton == JOY_NONE) { PressedButton = Read_Joystick(); } switch (PressedButton) { /* JOY_RIGHT button pressed */ case JOY_RIGHT: CommandTransmitted = CMD_RIGHT; NumberOfByte = CMD_RIGHT_SIZE; break; /* JOY_LEFT button pressed */ case JOY_LEFT: CommandTransmitted = CMD_LEFT; NumberOfByte = CMD_LEFT_SIZE; break; /* JOY_UP button pressed */ case JOY_UP: CommandTransmitted = CMD_UP; NumberOfByte = CMD_UP_SIZE; break; /* JOY_DOWN button pressed */ case JOY_DOWN: CommandTransmitted = CMD_DOWN; NumberOfByte = CMD_DOWN_SIZE; break; /* JOY_SEL button pressed */ case JOY_SEL: CommandTransmitted = CMD_SEL; NumberOfByte = CMD_SEL_SIZE; break; default: break; } /* Enable the DMA channel */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE); /* Enable DMA1 TIM Trigger Channel */ DMA_Cmd(TIMx_CHANNEL_DMA_CHANNEL, ENABLE); /* TIM enable counter */ TIM_Cmd(TIMx, ENABLE); /* Wait the SPI DMA Rx transfer complete or time out*/ TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.*/ TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear DMA1 global flags*/ DMA_ClearFlag(TIMx_CHANNEL_DMA_FLAG_GL); DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL); /* disable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE); DMA_Cmd(TIMx_CHANNEL_DMA_CHANNEL, DISABLE); /* disable the SPI Rx DMA request */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); /* TIM disable counter */ TIM_Cmd(TIMx, DISABLE); if (CommandReceived == CMD_ACK) { /* DMA channel Rx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = (uint16_t)NumberOfByte; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure); /* DMA channel Tx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = (uint16_t)NumberOfByte; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(TIMx_CHANNEL_DMA_CHANNEL, &DMA_InitStructure); /* Enable the SPI Rx DMA request */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); /* Enable the DMA channel */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE); /* Enable DMA1 TIM Trigger Channel */ DMA_Cmd(TIMx_CHANNEL_DMA_CHANNEL, ENABLE); /* TIM enable counter */ TIM_Cmd(TIMx, ENABLE); /* Wait the SPI Rx DMA transfer complete or time out */ TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.*/ TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear DMA1 global flags */ DMA_ClearFlag(TIMx_CHANNEL_DMA_FLAG_GL); DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL); /* Disable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE); DMA_Cmd(TIMx_CHANNEL_DMA_CHANNEL, DISABLE); /* Disable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); /* TIM disable counter */ TIM_Cmd(TIMx, DISABLE); switch (NumberOfByte) { /* CMD_RIGHT command received */ case CMD_RIGHT_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn OFF LED4 */ STM_EVAL_LEDOff(LED4); } break; /* CMD_LEFT command received */ case CMD_LEFT_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn OFF LED2 and LED3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* CMD_UP command received */ case CMD_UP_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn OFF LED3 and LED4 */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* CMD_DOWN command received */ case CMD_DOWN_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* CMD_SEL command received */ case CMD_SEL_SIZE: if ((Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE, SPI_DATAMASK) != FAILED) && (CommandReceived == CMD_ACK)) { /* Turn ON LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } } } #endif /* SPI_MASTER */ /* Slave board configuration -----------------------------------------------*/ #ifdef SPI_SLAVE /* Initializes the SPI communication */ SPI_I2S_DeInit(SPIx); SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPIx, &SPI_InitStructure); /* Initialize the FIFO threshold */ SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF); CommandTransmitted = CMD_ACK; /* Infinite Loop */ while (1) { /* DMA channel Rx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &CommandReceived; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure); /* DMA channel Tx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) &CommandTransmitted; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(SPIx_TX_DMA_CHANNEL, &DMA_InitStructure); /* Enable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); CommandReceived = 0x00; /* Enable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE); DMA_Cmd(SPIx_TX_DMA_CHANNEL, ENABLE); /* Wait the SPI DMA transfers complete or time out */ while (DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET) {} TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(SPIx_TX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.*/ TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* Clear DMA1 global flags */ DMA_ClearFlag(SPIx_TX_DMA_FLAG_GL); DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL); /* Disable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE); DMA_Cmd(SPIx_TX_DMA_CHANNEL, DISABLE); /* Disable the SPI peripheral */ SPI_Cmd(SPIx, DISABLE); /* Disable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, DISABLE); switch (CommandReceived) { /* CMD_RIGHT command received */ case CMD_RIGHT: NumberOfByte = CMD_RIGHT_SIZE; break; /* CMD_LEFT command received */ case CMD_LEFT: NumberOfByte = CMD_LEFT_SIZE; break; /* CMD_UP command received */ case CMD_UP: NumberOfByte = CMD_UP_SIZE; break; /* CMD_DOWN command received */ case CMD_DOWN: NumberOfByte = CMD_DOWN_SIZE; break; /* CMD_SEL command received */ case CMD_SEL: NumberOfByte = CMD_SEL_SIZE; break; default: break; } /* DMA channel Rx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = NumberOfByte; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_Init(SPIx_RX_DMA_CHANNEL, &DMA_InitStructure); /* DMA channel Tx of SPI Configuration */ DMA_InitStructure.DMA_BufferSize = NumberOfByte; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SPIx_DR_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)TxBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_Init(SPIx_TX_DMA_CHANNEL, &DMA_InitStructure); /* Enable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, ENABLE); SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); /* Enable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, ENABLE); DMA_Cmd(SPIx_TX_DMA_CHANNEL, ENABLE); /* Wait the SPI DMA transfers complete or time out */ while (DMA_GetFlagStatus(SPIx_RX_DMA_FLAG_TC) == RESET) {} TimeOut = USER_TIMEOUT; while ((DMA_GetFlagStatus(SPIx_TX_DMA_FLAG_TC) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } /* The BSY flag can be monitored to ensure that the SPI communication is complete. This is required to avoid corrupting the last transmission before disabling the SPI or entering the Stop mode. The software must first wait until TXE=1 and then until BSY=0.*/ TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } TimeOut = USER_TIMEOUT; while ((SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_BSY) == SET)&&(TimeOut != 0x00)) {} if(TimeOut == 0) { TimeOut_UserCallback(); } switch (NumberOfByte) { /* CMD_RIGHT command received */ case CMD_RIGHT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_RIGHT_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED2 and LED3 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); /* Turn OFF LED4 */ STM_EVAL_LEDOff(LED4); } break; /* CMD_LEFT command received */ case CMD_LEFT_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_LEFT_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED4 */ STM_EVAL_LEDOn(LED4); /* Turn OFF LED2 and LED3 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED3); } break; /* CMD_UP command received */ case CMD_UP_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_UP_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED2 */ STM_EVAL_LEDOn(LED2); /* Turn OFF LED3 and LED4 */ STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED4); } break; /* CMD_DOWN command received */ case CMD_DOWN_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_DOWN_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED3 */ STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } break; /* CMD_SEL command received */ case CMD_SEL_SIZE: if (Buffercmp(TxBuffer, RxBuffer, CMD_SEL_SIZE, SPI_DATAMASK) != FAILED) { /* Turn ON LED2, LED3 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED4); } break; default: break; } /* Clear DMA1 global flags */ DMA_ClearFlag(SPIx_TX_DMA_FLAG_GL); DMA_ClearFlag(SPIx_RX_DMA_FLAG_GL); /* Disable the DMA channels */ DMA_Cmd(SPIx_RX_DMA_CHANNEL, DISABLE); DMA_Cmd(SPIx_TX_DMA_CHANNEL, DISABLE); /* Disable the SPI peripheral */ SPI_Cmd(SPIx, DISABLE); /* Disable the SPI Rx and Tx DMA requests */ SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Rx, DISABLE); SPI_I2S_DMACmd(SPIx, SPI_I2S_DMAReq_Tx, DISABLE); } #endif /* SPI_SLAVE */ }
/*====================================================================================================*/ void NRF24L01_Config( void ) { GPIO_InitTypeDef GPIO_InitStruct; SPI_InitTypeDef SPI_InitStruct; /* Clk Init *************************************************************/ RCC_AHB1PeriphClockCmd(NRF_CE_GPIO_CLK | NRF_IRQ_GPIO_CLK | NRF_CSN_GPIO_CLK | NRF_SCK_GPIO_CLK | NRF_SDO_GPIO_CLK | NRF_SDI_GPIO_CLK, ENABLE); RCC_APB1PeriphClockCmd(NRF_SPIx_CLK, ENABLE); GPIO_PinAFConfig(NRF_SCK_GPIO_PORT, NRF_SCK_SOURCE, NRF_SCK_AF); GPIO_PinAFConfig(NRF_SDO_GPIO_PORT, NRF_SDO_SOURCE, NRF_SDO_AF); GPIO_PinAFConfig(NRF_SDI_GPIO_PORT, NRF_SDI_SOURCE, NRF_SDI_AF); /* CE */ GPIO_InitStruct.GPIO_Pin = NRF_CE_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(NRF_CE_GPIO_PORT, &GPIO_InitStruct); NRF_CE = 0; /* IRQ */ GPIO_InitStruct.GPIO_Pin = NRF_IRQ_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(NRF_IRQ_GPIO_PORT, &GPIO_InitStruct); /* CSN */ GPIO_InitStruct.GPIO_Pin = NRF_CSN_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(NRF_CSN_GPIO_PORT, &GPIO_InitStruct); NRF_CSN = 1; /* SCK */ GPIO_InitStruct.GPIO_Pin = NRF_SCK_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(NRF_SCK_GPIO_PORT, &GPIO_InitStruct); /* SDO */ GPIO_InitStruct.GPIO_Pin = NRF_SDO_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(NRF_SDO_GPIO_PORT, &GPIO_InitStruct); /* SDI */ GPIO_InitStruct.GPIO_Pin = NRF_SDI_PIN; GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(NRF_SDI_GPIO_PORT, &GPIO_InitStruct); /* SPI Init ****************************************************************/ SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // Full Duplex SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // Master Mode SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // Data Size 8 bit SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // Transitioned On The Rising Edge SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // Latched On the Rising Edge SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // Software NSS Signal SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; // fsck = APB1 36MHz / 2 = 18MHz SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB; // MSB First SPI_InitStruct.SPI_CRCPolynomial = 7; SPI_Init(NRF_SPIx, &SPI_InitStruct); SPI_Cmd(NRF_SPIx, ENABLE); }
OSStatus MicoSpiInitialize( const mico_spi_device_t* spi ) { GPIO_InitTypeDef gpio_init_structure; OSStatus result; SPI_InitTypeDef spi_init; check_string( spi != NULL, "Bad args"); MicoMcuPowerSaveConfig(false); /* Init SPI GPIOs */ gpio_init_structure.GPIO_Mode = GPIO_Mode_AF; gpio_init_structure.GPIO_OType = GPIO_OType_PP; gpio_init_structure.GPIO_Speed = GPIO_Speed_100MHz; gpio_init_structure.GPIO_Pin = ((uint32_t) (1 << spi_mapping[spi->port].pin_clock->number)) | ((uint32_t) (1 << spi_mapping[spi->port].pin_miso->number )) | ((uint32_t) (1 << spi_mapping[spi->port].pin_mosi->number )); GPIO_Init( spi_mapping[spi->port].pin_clock->bank, &gpio_init_structure ); /* Init the chip select GPIO */ MicoGpioInitialize(spi->chip_select, OUTPUT_PUSH_PULL); MicoGpioOutputHigh(spi->chip_select); GPIO_PinAFConfig( spi_mapping[spi->port].pin_clock->bank, spi_mapping[spi->port].pin_clock->number, spi_mapping[spi->port].gpio_af ); GPIO_PinAFConfig( spi_mapping[spi->port].pin_miso->bank, spi_mapping[spi->port].pin_miso->number, spi_mapping[spi->port].gpio_af ); GPIO_PinAFConfig( spi_mapping[spi->port].pin_mosi->bank, spi_mapping[spi->port].pin_mosi->number, spi_mapping[spi->port].gpio_af ); /* Configure baudrate */ result = wiced_spi_configure_baudrate( spi->speed, &spi_init.SPI_BaudRatePrescaler ); if ( result != kNoErr ) { return result; } /* Configure data-width */ if ( spi->bits == 8 ) { spi_init.SPI_DataSize = SPI_DataSize_8b; } else if ( spi->bits == 16 ) { if ( spi->mode & SPI_USE_DMA ) { /* 16 bit mode is not supported for a DMA */ return kGeneralErr; } spi_init.SPI_DataSize = SPI_DataSize_16b; } else { /* Requested mode is not supported */ return kOptionErr; } /* Configure MSB or LSB */ if ( spi->mode & SPI_MSB_FIRST ) { spi_init.SPI_FirstBit = SPI_FirstBit_MSB; } else { spi_init.SPI_FirstBit = SPI_FirstBit_LSB; } /* Configure mode CPHA and CPOL */ if ( spi->mode & SPI_CLOCK_IDLE_HIGH ) { spi_init.SPI_CPOL = SPI_CPOL_High; } else { spi_init.SPI_CPOL = SPI_CPOL_Low; } if ( spi->mode & SPI_CLOCK_RISING_EDGE ) { spi_init.SPI_CPHA = ( spi->mode & SPI_CLOCK_IDLE_HIGH )? SPI_CPHA_2Edge : SPI_CPHA_1Edge; } else { spi_init.SPI_CPHA = ( spi->mode & SPI_CLOCK_IDLE_HIGH )? SPI_CPHA_1Edge : SPI_CPHA_2Edge; } spi_init.SPI_Direction = SPI_Direction_2Lines_FullDuplex; spi_init.SPI_Mode = SPI_Mode_Master; spi_init.SPI_NSS = SPI_NSS_Soft; SPI_CalculateCRC( spi_mapping[spi->port].spi_regs, DISABLE ); /* Enable SPI peripheral clock */ spi_mapping[spi->port].peripheral_clock_func( spi_mapping[spi->port].peripheral_clock_reg, ENABLE ); /* Init and enable SPI */ SPI_Init( spi_mapping[spi->port].spi_regs, &spi_init ); SPI_Cmd ( spi_mapping[spi->port].spi_regs, ENABLE ); MicoMcuPowerSaveConfig(true); current_spi_device = (mico_spi_device_t*)spi; return kNoErr; }
/** * @brief Initializes the low level interface used to drive the L3GD20 * @param None * @retval None */ void L3GD20_LowLevel_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; SPI_InitTypeDef SPI_InitStructure; /* Enable the SPI periph */ RCC_APB2PeriphClockCmd(L3GD20_SPI_CLK, ENABLE); /* Enable SCK, MOSI and MISO GPIO clocks */ RCC_AHB1PeriphClockCmd(L3GD20_SPI_SCK_GPIO_CLK | L3GD20_SPI_MISO_GPIO_CLK | L3GD20_SPI_MOSI_GPIO_CLK, ENABLE); /* Enable CS GPIO clock */ RCC_AHB1PeriphClockCmd(L3GD20_SPI_CS_GPIO_CLK, ENABLE); /* Enable INT1 GPIO clock */ RCC_AHB1PeriphClockCmd(L3GD20_SPI_INT1_GPIO_CLK, ENABLE); /* Enable INT2 GPIO clock */ RCC_AHB1PeriphClockCmd(L3GD20_SPI_INT2_GPIO_CLK, ENABLE); GPIO_PinAFConfig(L3GD20_SPI_SCK_GPIO_PORT, L3GD20_SPI_SCK_SOURCE, L3GD20_SPI_SCK_AF); GPIO_PinAFConfig(L3GD20_SPI_MISO_GPIO_PORT, L3GD20_SPI_MISO_SOURCE, L3GD20_SPI_MISO_AF); GPIO_PinAFConfig(L3GD20_SPI_MOSI_GPIO_PORT, L3GD20_SPI_MOSI_SOURCE, L3GD20_SPI_MOSI_AF); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; /* SPI SCK pin configuration */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_SCK_PIN; GPIO_Init(L3GD20_SPI_SCK_GPIO_PORT, &GPIO_InitStructure); /* SPI MOSI pin configuration */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_MOSI_PIN; GPIO_Init(L3GD20_SPI_MOSI_GPIO_PORT, &GPIO_InitStructure); /* SPI MISO pin configuration */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_MISO_PIN; GPIO_Init(L3GD20_SPI_MISO_GPIO_PORT, &GPIO_InitStructure); /* SPI configuration -------------------------------------------------------*/ SPI_I2S_DeInit(L3GD20_SPI); 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 baudrate is set to 5.6 MHz (PCLK2/SPI_BaudRatePrescaler = 90/16 = 5.625 MHz) to verify these constraints: - ILI9341 LCD SPI interface max baudrate is 10MHz for write and 6.66MHz for read - l3gd20 SPI interface max baudrate is 10MHz for write/read - PCLK2 frequency is set to 90 MHz */ SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(L3GD20_SPI, &SPI_InitStructure); /* Enable L3GD20_SPI */ SPI_Cmd(L3GD20_SPI, ENABLE); /* Configure GPIO PIN for Lis Chip select */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_CS_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(L3GD20_SPI_CS_GPIO_PORT, &GPIO_InitStructure); /* Deselect : Chip Select high */ GPIO_SetBits(L3GD20_SPI_CS_GPIO_PORT, L3GD20_SPI_CS_PIN); /* Configure GPIO PINs to detect Interrupts */ GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_INT1_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(L3GD20_SPI_INT1_GPIO_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = L3GD20_SPI_INT2_PIN; GPIO_Init(L3GD20_SPI_INT2_GPIO_PORT, &GPIO_InitStructure); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup files (startup_stm32f40xx.s/startup_stm32f427x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f4xx.c file */ /* SPI configuration */ SPI_Config(); /* SysTick configuration */ SysTickConfig(); /* LEDs configuration */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); #ifdef SPI_MASTER /* Master board configuration */ /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_Init(SPIx, &SPI_InitStructure); /* The Data transfer is performed in the SPI interrupt routine */ /* Configure the Tamper Button */ STM_EVAL_PBInit(BUTTON_TAMPER,BUTTON_MODE_GPIO); /* Wait until Tamper Button is pressed */ while (STM_EVAL_PBGetState(BUTTON_TAMPER)); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); /* Initialize Buffer counters */ ubTxIndex = 0; ubRxIndex = 0; /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE); /* Enable the Tx buffer empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); #endif /* SPI_MASTER */ #ifdef SPI_SLAVE /* Slave board configuration */ /* Initializes the SPI communication */ SPI_InitStructure.SPI_Mode = SPI_Mode_Slave; SPI_Init(SPIx, &SPI_InitStructure); /* The Data transfer is performed in the SPI interrupt routine */ /* Initialize Buffer counters */ ubTxIndex = 0; ubRxIndex = 0; /* Enable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, ENABLE); /* Enable the Tx empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, ENABLE); /* Enable the SPI peripheral */ SPI_Cmd(SPIx, ENABLE); #endif /* SPI_SLAVE */ /* Waiting the end of Data transfer */ while ((ubTxIndex < BUFFERSIZE) && (ubRxIndex < BUFFERSIZE)) { } /* Disable the Rx buffer not empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_RXNE, DISABLE); /* Disable the Tx empty interrupt */ SPI_I2S_ITConfig(SPIx, SPI_I2S_IT_TXE, DISABLE); /* Disable the SPI peripheral */ SPI_Cmd(SPIx, DISABLE); if (Buffercmp(aTxBuffer, aRxBuffer, BUFFERSIZE) != FAILED) { /* Turn ON LED1 and LED3 */ STM_EVAL_LEDOn(LED1); STM_EVAL_LEDOn(LED3); /* Turn OFF LED2 and LED4 */ STM_EVAL_LEDOff(LED2); STM_EVAL_LEDOff(LED4); } else { /* Turn OFF LED1 and LED3 */ STM_EVAL_LEDOff(LED1); STM_EVAL_LEDOff(LED3); /* Turn ON LED2 and LED4 */ STM_EVAL_LEDOn(LED2); STM_EVAL_LEDOn(LED4); } /* Infinite Loop */ while (1) { } }
void spi_init() { // clear variables memset(&spi_vars,0,sizeof(spi_vars_t)); SPI_InitTypeDef SPI_InitStructure; //enable SPI2, GPIOA, GPIOB and GPIOC, Clock RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); //Configure SPI-related pins: PB.13 as SCLK pin ,PB.14 as MISO pin, PB.15 as MOSI pin, PA.4 as /SEL pin GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); // force reset RESET_CLR(); CSn_SET(); SLEEP_CLR(); for (uint16_t j=0;j<0xFFFF;j++); //small wait RESET_SET(); //Configure SPI2 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //Full-duplex synchronous transfers on two lines SPI_InitStructure.SPI_Mode = SPI_Mode_Master;//Master Mode SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8-bit transfer frame format SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; //the SCK pin has a low-level idle state SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //the first rising edge on the SCK pin is the MSBit capture strobe, SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;//Software NSS mode SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;//BaudRate Prescaler = 8 SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;//data order with MSB-first SPI_InitStructure.SPI_CRCPolynomial = 7;//CRC Polynomial = 7 SPI_Init(SPI2, &SPI_InitStructure); //enable SPI2 SPI_Cmd(SPI2, ENABLE); #ifdef SPI_IN_INTERRUPT_MODE //Configure NVIC: Preemption Priority = 1 and Sub Priority = 1 NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = SPI2_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); #endif }
int spi_master_setup(u08 div,u08 mode) { SPI_InitTypeDef SPI_InitStructure; // SPI configuration switch(div) { case 2: SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; break; case 4: SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; break; case 8: SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8; break; case 16: SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16; break; case 32: SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32; break; case 64: SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; break; case 128: SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; break; default: SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; break; } switch(mode) { case 0: SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; break; case 1: SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; break; case 2: SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; break; case 3: SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; break; case 4: SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; break; case 5: SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; break; case 6: SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; break; case 7: SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_LSB; break; default: return -2; } 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_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI_SPI, &SPI_InitStructure); // Enable SPI SPI_Cmd(SPI_SPI, ENABLE); // check TXE, RXNE, ERR bit in SPI->SR if(SPI_I2S_GetFlagStatus(SPI_SPI, SPI_I2S_FLAG_TXE) == RESET) logd(__FUNCTION__,"SPI Transmit bufffer not empty!\r\n",Error); if(SPI_I2S_GetFlagStatus(SPI_SPI, SPI_I2S_FLAG_RXNE)== SET) logd(__FUNCTION__,"SPI Recieve buffer has vailed data!\r\n",Warning); return 0; }