Hello @lukegalea16
In fact, the content of these flags is cleared before calling the callback function in the DMA IRQ handler. To address this issue, I propose an alternative solution that involves setting software flags in the DMA complete and half-transfer complete callbacks. These flags can then be checked in the I2S RX/TX complete and half-transfer complete callbacks.
Here is the structure of the proposed solution:
// Define the I2S and DMA handles
I2S_HandleTypeDef hi2s2;
DMA_HandleTypeDef hdma_i2s2_tx;
DMA_HandleTypeDef hdma_i2s2_rx;
// Define software flags
volatile uint8_t txHalfCompleteFlag = 0;
volatile uint8_t rxHalfCompleteFlag = 0;
volatile uint8_t txCompleteFlag = 0;
volatile uint8_t rxCompleteFlag = 0;
// Main function
int main(void) {
// HAL initialization
HAL_Init();
// System clock configuration
SystemClock_Config();
// Initialize the I2S2ext peripheral
I2S2ext_Init();
// Initialize the DMA for I2S2ext Tx and Rx
DMA_Init();
// Register custom callbacks for Tx
HAL_DMA_RegisterCallback(&hdma_i2s2_tx, HAL_DMA_XFER_CPLT_CB_ID, TxTransferComplete);
HAL_DMA_RegisterCallback(&hdma_i2s2_tx, HAL_DMA_XFER_HALFCPLT_CB_ID, TxHalfTransferComplete);
// Register custom callbacks for Rx
HAL_DMA_RegisterCallback(&hdma_i2s2_rx, HAL_DMA_XFER_CPLT_CB_ID, RxTransferComplete);
HAL_DMA_RegisterCallback(&hdma_i2s2_rx, HAL_DMA_XFER_HALFCPLT_CB_ID, RxHalfTransferComplete);
// Main loop
while (1) {
// Your main loop code here
}
}
// Custom callback for Tx Half-Transfer Complete
void TxHalfTransferComplete(DMA_HandleTypeDef *hdma) {
txHalfCompleteFlag = 1;
}
// Custom callback for Rx Half-Transfer Complete
void RxHalfTransferComplete(DMA_HandleTypeDef *hdma) {
rxHalfCompleteFlag = 1;
}
// Custom callback for Tx Transfer Complete
void TxTransferComplete(DMA_HandleTypeDef *hdma) {
txCompleteFlag = 1;
}
// Custom callback for Rx Transfer Complete
void RxTransferComplete(DMA_HandleTypeDef *hdma) {
rxCompleteFlag = 1;
}
// Callback function for Half-Transfer Complete
void HAL_I2SEx_TxRxHalfCpltCallback(I2S_HandleTypeDef *hi2s) {
if (txHalfCompleteFlag) {
// Handle Tx half-complete
txHalfCompleteFlag = 0; // Clear the flag after handling
}
if (rxHalfCompleteFlag) {
// Handle Rx half-complete
rxHalfCompleteFlag = 0; // Clear the flag after handling
}
}
// Callback function for Transfer Complete
void HAL_I2SEx_TxRxCpltCallback(I2S_HandleTypeDef *hi2s) {
if (txCompleteFlag) {
// Handle Tx complete
txCompleteFlag = 0; // Clear the flag after handling
}
if (rxCompleteFlag) {
// Handle Rx complete
rxCompleteFlag = 0; // Clear the flag after handling
}
}