void spi_txrx(uint8_t* bufTx, uint8_t lenbufTx, spi_return_t returnType, uint8_t* bufRx, uint8_t maxLenBufRx, spi_first_t isFirst, spi_last_t isLast) { #ifdef SPI_IN_INTERRUPT_MODE // disable interrupts NVIC_RESETPRIMASK(); #endif // register spi frame to send spi_vars.pNextTxByte = bufTx; spi_vars.numTxedBytes = 0; spi_vars.txBytesLeft = lenbufTx; spi_vars.returnType = returnType; spi_vars.pNextRxByte = bufRx; spi_vars.maxRxBytes = maxLenBufRx; spi_vars.isFirst = isFirst; spi_vars.isLast = isLast; // SPI is now busy spi_vars.busy = 1; // lower CS signal to have slave listening if (spi_vars.isFirst==SPI_FIRST) { GPIO_ResetBits(GPIOA, GPIO_Pin_4); } #ifdef SPI_IN_INTERRUPT_MODE // implementation 1. use a callback function when transaction finishes // write first byte to TX buffer SPI_I2S_SendData(SPI2,*spi_vars.pNextTxByte); // re-enable interrupts NVIC_SETPRIMASK(); #else // implementation 2. busy wait for each byte to be sent // send all bytes while (spi_vars.txBytesLeft>0) { // write next byte to TX buffer SPI_I2S_SendData(SPI2,*spi_vars.pNextTxByte); // busy wait on the interrupt flag while (SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE) == RESET); // clear the interrupt flag SPI_I2S_ClearFlag(SPI2, SPI_I2S_FLAG_RXNE); // save the byte just received in the RX buffer switch (spi_vars.returnType) { case SPI_FIRSTBYTE: if (spi_vars.numTxedBytes==0) { *spi_vars.pNextRxByte = SPI_I2S_ReceiveData(SPI2); } break; case SPI_BUFFER: *spi_vars.pNextRxByte = SPI_I2S_ReceiveData(SPI2); spi_vars.pNextRxByte++; break; case SPI_LASTBYTE: *spi_vars.pNextRxByte = SPI_I2S_ReceiveData(SPI2); break; } // one byte less to go spi_vars.pNextTxByte++; spi_vars.numTxedBytes++; spi_vars.txBytesLeft--; } // put CS signal high to signal end of transmission to slave if (spi_vars.isLast==SPI_LAST) { GPIO_SetBits(GPIOA, GPIO_Pin_4); } // SPI is not busy anymore spi_vars.busy = 0; #endif }
/******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif /* Configure the system clocks */ RCC_Configuration(); /* Configure GPIOs */ GPIO_Configuration(); /* Configures the EXTI Lines */ EXTI_Configuration(); #ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQChannel; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_Init(&NVIC_InitStructure); while (1) { if(LowPowerMode == 1) { LowPowerMode = 0; /* Toggle GPIO_LED pin 6 */ GPIO_WriteBit(GPIO_LED, GPIO_Pin_6, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_6))); #ifdef WFISLEEPONEXIT NVIC_SystemLPConfig(NVIC_LP_SLEEPONEXIT, ENABLE); #endif #ifdef WFISLEEPNOW NVIC_SystemLPConfig(NVIC_LP_SLEEPONEXIT, DISABLE); __WFI(); #endif #ifdef WFESEVONPEND NVIC_SystemLPConfig(NVIC_LP_SEVONPEND, ENABLE); /* Boost the execution priority to 0: no further exception can be asserted */ NVIC_SETPRIMASK(); #ifdef RTC_Alarm_WFEWakeUp /* Set the RTC Alarm interrupt after 6s */ RTC_SetAlarm(0xFFFFFFFA); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Set the Counter to 0xFFFFFFF5 */ RTC_SetCounter(0xFFFFFFF5); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); #endif __WFE(); #endif #ifdef WFESEVONEVENT NVIC_SystemLPConfig(NVIC_LP_SEVONPEND, DISABLE); #ifdef RTC_Alarm_WFEWakeUp /* Set the RTC Alarm interrupt after 6s */ RTC_SetAlarm(0xFFFFFFFA); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Set the Counter to 0xFFFFFFF5 */ RTC_SetCounter(0xFFFFFFF5); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); #endif __WFE(); #endif } Delay(0xFFFFF); /* Toggle GPIO_LED pin 9 */ GPIO_WriteBit(GPIO_LED, GPIO_Pin_9, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIO_LED, GPIO_Pin_9))); } }