/*## Configure the SPI4 peripheral ###*/ Spi4Handle.Instance = SPI4; //use STM32F429 SPI4 Spi4Handle.Init.Direction = SPI_DIRECTION_2LINES_RXONLY; Spi4Handle.Init.CLKPhase = SPI_PHASE_1EDGE; //read at DCLK falling edge Spi4Handle.Init.CLKPolarity = SPI_POLARITY_HIGH; //read at DCLK falling edge Spi4Handle.Init.DataSize = SPI_DATASIZE_8BIT; //or 16BIT Spi4Handle.Init.NSS = SPI_NSS_HARD_INPUT; //make /CS low active Spi4Handle.Init.Mode = SPI_MODE_SLAVE; //MCU SPI4 as SPI Slave /*## Enable EXTI0 and SPI4 to Receive AD7768 Data bits ###*/ // clear EXTI0 IT flag prior to enable external interrupt 0 !!! __HAL_GPIO_EXTI_CLEAR_IT(KEY_BUTTON_PIN); HAL_NVIC_EnableIRQ(EXTI0_IRQn); // wait for EXTI0 interrupt (/DRDY rising edge) to prepare for reading last conversion data if (EXTI0_Flag == SET) { EXTI0_Flag = RESET;//clear /DRDY rising edge flag variable // throw out the last byte/word captured in the previous ODR cycle !!! Rx_temp = *(__IO uint8_t *)&Spi4Handle.Instance->DR; __HAL_SPI_ENABLE(&Spi4Handle); // SPI4_CNVByteNum is the total data byte number to read in one conversion cycle while (SPI4_ByteCount < SPI4_CNVByteNum) { // Check the RXNE flag if (__HAL_SPI_GET_FLAG(&Spi4Handle, SPI_FLAG_RXNE))// { // transfer the received data from DR register to memory SPI_RxBuffer[RxBuf_Idn] = *(__IO uint8_t *)&Spi4Handle.Instance->DR; RxBuf_Idn++; SPI4_ByteCount++; } } // disable SPI4 to prevent read in extra data after all channel codes finished due to /DRDY is low active and DCLK continuously pulses __HAL_SPI_DISABLE(&Spi4Handle); SPI4_CNVCount++; RxBuf_Idn = SPI4_CNVCount * SPI4_CNVByteNum; SPI4_ByteCount = 0; }//end of if (EXTI0_Flag == SET) else {//*** other software jobs ***//} /*## handles External 0 interrupt request ###*/ // EXTI0 rising edge triggered to leave more response time for going into EXTI0_IRQHandler !!! void EXTI0_IRQHandler(void) { if(__HAL_GPIO_EXTI_GET_IT(EXTI0) != RESET) { // enable SPI4 as soon as possible, and make sure before the first DCLK falling edge after /DRDY falling !!! __HAL_SPI_ENABLE(&Spi4Handle); __HAL_GPIO_EXTI_CLEAR_IT(EXTI0); EXTI0_Flag = SET; } }