From 1854aafa26f63febc19ceed252b6b295fb2d02dc Mon Sep 17 00:00:00 2001 From: GitHub Actions Date: Fri, 6 Sep 2024 02:19:19 +0000 Subject: [PATCH] Update Zephyr MSDK Hal based on MSDK PR: https://github.com/analogdevicesinc/msdk/pull/1109 --- .../PeriphDrivers/Include/MAX32665/dma.h | 22 ++++++++ .../PeriphDrivers/Source/DMA/dma_me14.c | 20 +++++++ .../PeriphDrivers/Source/I2C/i2c_reva.c | 53 +++++++++++++++---- MAX/msdk_sha | 2 +- 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/MAX/Libraries/PeriphDrivers/Include/MAX32665/dma.h b/MAX/Libraries/PeriphDrivers/Include/MAX32665/dma.h index 8578a42f..1aa29ff7 100644 --- a/MAX/Libraries/PeriphDrivers/Include/MAX32665/dma.h +++ b/MAX/Libraries/PeriphDrivers/Include/MAX32665/dma.h @@ -445,6 +445,28 @@ int MXC_DMA_Stop(int ch); */ mxc_dma_ch_regs_t *MXC_DMA_GetCHRegs(int ch); +typedef void (*dma_handler_t)(void); +/** + * @brief Get the appropriate "hard" DMA handler for the specified DMA instance. + * @ref MXC_DMA_DMA0_Handler or @ref MXC_DMA_DMA1_Handler. + * Mostly used by internal drivers. + */ +dma_handler_t MXC_DMA_Get_DMA_Handler(mxc_dma_regs_t *dma); + +/** + * @brief Interrupt handler function for DMA0 + * @details Used by some internal drivers that automatically assign DMA handlers. + * This is equivalent to "MXC_DMA_Handler(MXC_DMA0);" + */ +void MXC_DMA_DMA0_Handler(void); + +/** + * @brief Interrupt handler function for DMA0 + * @details Used by some internal drivers that automatically assign DMA handlers. + * This is equivalent to "MXC_DMA_Handler(MXC_DMA1);" + */ +void MXC_DMA_DMA1_Handler(void); + /** * @brief Interrupt handler function * @param dma Pointer to DMA registers. diff --git a/MAX/Libraries/PeriphDrivers/Source/DMA/dma_me14.c b/MAX/Libraries/PeriphDrivers/Source/DMA/dma_me14.c index 4b38fa55..ac368cf1 100644 --- a/MAX/Libraries/PeriphDrivers/Source/DMA/dma_me14.c +++ b/MAX/Libraries/PeriphDrivers/Source/DMA/dma_me14.c @@ -177,6 +177,26 @@ mxc_dma_ch_regs_t *MXC_DMA_GetCHRegs(int ch) return MXC_DMA_RevA_GetCHRegs(ch); } +dma_handler_t MXC_DMA_Get_DMA_Handler(mxc_dma_regs_t *dma) +{ + if (dma == MXC_DMA0) + return MXC_DMA_DMA0_Handler; + else if (dma == MXC_DMA1) + return MXC_DMA_DMA1_Handler; + else + return NULL; +} + +void MXC_DMA_DMA0_Handler(void) +{ + MXC_DMA_RevA_Handler((mxc_dma_reva_regs_t *)MXC_DMA0); +} + +void MXC_DMA_DMA1_Handler(void) +{ + MXC_DMA_RevA_Handler((mxc_dma_reva_regs_t *)MXC_DMA1); +} + void MXC_DMA_Handler(mxc_dma_regs_t *dma) { if (MXC_DMA_GET_IDX(dma) != -1) { diff --git a/MAX/Libraries/PeriphDrivers/Source/I2C/i2c_reva.c b/MAX/Libraries/PeriphDrivers/Source/I2C/i2c_reva.c index d809f88a..d6fa4193 100644 --- a/MAX/Libraries/PeriphDrivers/Source/I2C/i2c_reva.c +++ b/MAX/Libraries/PeriphDrivers/Source/I2C/i2c_reva.c @@ -32,6 +32,7 @@ #include "i2c_reva.h" #include "dma.h" #include "dma_reva.h" +#include "nvic_table.h" /* **** Variable Declaration **** */ typedef struct { @@ -297,8 +298,8 @@ int MXC_I2C_RevA_DMA_Init(mxc_i2c_reva_regs_t *i2c, mxc_dma_reva_regs_t *dma, bo bool use_dma_rx) { int8_t i2cNum; - int8_t rxChannel; - int8_t txChannel; + int8_t rxChannel = -1; + int8_t txChannel = -1; if (i2c == NULL || dma == NULL) { return E_NULL_PTR; @@ -354,6 +355,15 @@ int MXC_I2C_RevA_DMA_Init(mxc_i2c_reva_regs_t *i2c, mxc_dma_reva_regs_t *dma, bo MXC_DMA_SetChannelInterruptEn(txChannel, 0, 1); states[i2cNum].channelTx = txChannel; +#ifdef __arm__ + NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(txChannel)); +#if TARGET_NUM == 32665 + MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(txChannel), + MXC_DMA_Get_DMA_Handler((mxc_dma_regs_t *)dma)); +#else + MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(txChannel), MXC_DMA_Handler); +#endif +#endif } // Set up I2C DMA RX. @@ -384,6 +394,15 @@ int MXC_I2C_RevA_DMA_Init(mxc_i2c_reva_regs_t *i2c, mxc_dma_reva_regs_t *dma, bo MXC_DMA_EnableInt(rxChannel); MXC_DMA_SetChannelInterruptEn(rxChannel, 0, 1); +#ifdef __arm__ + NVIC_EnableIRQ(MXC_DMA_CH_GET_IRQ(rxChannel)); +#if TARGET_NUM == 32665 + MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(txChannel), + MXC_DMA_Get_DMA_Handler((mxc_dma_regs_t *)dma)); +#else + MXC_NVIC_SetVector(MXC_DMA_CH_GET_IRQ(txChannel), MXC_DMA_Handler); +#endif +#endif states[i2cNum].channelRx = rxChannel; } @@ -1082,8 +1101,8 @@ int MXC_I2C_RevA_MasterTransactionDMA(mxc_i2c_reva_req_t *req, mxc_dma_regs_t *d MXC_I2C_SetRXThreshold((mxc_i2c_regs_t *)i2c, 1); states[i2cNum].req = req; - states[i2cNum].writeDone = 0; - states[i2cNum].readDone = 0; + states[i2cNum].writeDone = (req->tx_len == 0); + states[i2cNum].readDone = (req->rx_len == 0); // If MXC_I2C_DMA_Init(...) was not already called, then configure both DMA TX/RXchannels by default. if (states[i2cNum].dma_initialized == false) { @@ -1122,17 +1141,31 @@ int MXC_I2C_RevA_MasterTransactionDMA(mxc_i2c_reva_req_t *req, mxc_dma_regs_t *d i2c->rxctrl1 = req->rx_len; // 0 for 256, otherwise number of bytes to read } - MXC_I2C_Start((mxc_i2c_regs_t *)i2c); // Start or Restart as needed - - while (i2c->mstctrl & MXC_F_I2C_REVA_MSTCTRL_RESTART) {} - - i2c->fifo = ((req->addr) << 1) | 0x1; // Load the slave address with write bit set - #if TARGET_NUM == 32665 MXC_I2C_ReadRXFIFODMA((mxc_i2c_regs_t *)i2c, req->rx_buf, req->rx_len, NULL, dma); #else MXC_I2C_ReadRXFIFODMA((mxc_i2c_regs_t *)i2c, req->rx_buf, req->rx_len, NULL); #endif + + MXC_I2C_Start((mxc_i2c_regs_t *)i2c); // Start or Restart as needed + + while (i2c->mstctrl & MXC_F_I2C_REVA_MSTCTRL_RESTART) {} + + i2c->fifo = ((req->addr) << 1) | 0x1; // Load the slave address with write bit set + while (!((i2c->intfl0 & MXC_F_I2C_REVA_INTFL0_ADDR_ACK) || + (i2c->intfl0 & MXC_F_I2C_REVA_INTFL0_ADDR_NACK_ERR))) { + // Wait for an ACK or NACK from the slave + } + if (!(i2c->intfl0 & MXC_F_I2C_REVA_INTFL0_ADDR_ACK)) { + // If we did not get an ACK, then something went wrong. + // Abort the transaction and signal the user's callback + MXC_I2C_RevA_Stop(i2c); + MXC_DMA_Stop(states[i2cNum].channelRx); + if (states[i2cNum].req->callback != NULL) { + states[i2cNum].req->callback(states[i2cNum].req, E_COMM_ERR); + } + return E_COMM_ERR; + } } } else { states[i2cNum].readDone = 1; diff --git a/MAX/msdk_sha b/MAX/msdk_sha index 380244a1..c1ba05dc 100644 --- a/MAX/msdk_sha +++ b/MAX/msdk_sha @@ -1 +1 @@ -6b6f2c3e372b48a197727c62b68df2e9fb9955c9 +4e410385fd91984636606afd801858fbe1a480b6