Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions src/fw/board/boards/board_obelix.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ static const I2CSlavePort s_i2c_aw86225 = {
.bus = &s_i2c_bus_1,
.address = 0x58,
};

I2CSlavePort *const I2C_AW86225 = &s_i2c_aw86225;

static const AW86225Config s_aw86225_config = {
Expand Down Expand Up @@ -488,6 +488,24 @@ static I2CBusHalState s_i2c_bus_hal_state_4 = {
.Mode = HAL_I2C_MODE_MASTER,
.core = CORE_ID_HCPU,
},
.dma_tx = {
.Instance = DMA1_Channel6,
.dma_irq = DMAC1_CH6_IRQn,
.request = DMA_REQUEST_3,
.dma_irq_prio = 5,
},
.dma_rx = {
.Instance = DMA1_Channel7,
.dma_irq = DMAC1_CH7_IRQn,
.request = DMA_REQUEST_3,
.dma_irq_prio = 5,
},
.hdma_tx = {
.Instance = DMA1_Channel6,
},
.hdma_rx = {
.Instance = DMA1_Channel7,
},
};

static I2CBusHal s_i2c_bus_hal_4 = {
Expand All @@ -507,6 +525,9 @@ static I2CBusHal s_i2c_bus_hal_4 = {
.module = RCC_MOD_I2C4,
.irqn = I2C4_IRQn,
.irq_priority = 5,
.dma_tx_irqn = DMAC1_CH6_IRQn,
.dma_rx_irqn = DMAC1_CH7_IRQn,
.dma_irq_priority = 5,
};

static I2CBusState s_i2c_bus_state_4;
Expand All @@ -521,6 +542,8 @@ static I2CBus s_i2c_bus_4 = {
I2CBus *const I2C4_BUS = &s_i2c_bus_4;

IRQ_MAP(I2C4, i2c_irq_handler, I2C4_BUS);
IRQ_MAP(DMAC1_CH6, i2c_dma_tx_irq_handler, I2C4_BUS);
IRQ_MAP(DMAC1_CH7, i2c_dma_rx_irq_handler, I2C4_BUS);

static const I2CSlavePort s_i2c_gh3x2x = {
.bus = &s_i2c_bus_4,
Expand Down Expand Up @@ -563,7 +586,7 @@ static const I2CSlavePort s_i2c_w1160 = {
.bus = &s_i2c_bus_1,
.address = 0x48,
};

I2CSlavePort *const I2C_W1160 = &s_i2c_w1160;

const BoardConfigPower BOARD_CONFIG_POWER = {
Expand Down Expand Up @@ -610,7 +633,7 @@ static const MicDevice mic_device = {
.clk_gpio = {
.pad = PAD_PA22,
.func = PDM1_CLK,
.flags = PIN_NOPULL,
.flags = PIN_NOPULL,
},
.data_gpio = {
.pad = PAD_PA23,
Expand All @@ -619,7 +642,7 @@ static const MicDevice mic_device = {
},
.pdm_dma_irq = DMAC1_CH5_IRQn,
.pdm_irq = PDM1_IRQn,
.pdm_irq_priority = 5,
.pdm_irq_priority = 5,
.channels = 1,
.sample_rate = 16000,
.channel_depth = 16,
Expand Down
52 changes: 48 additions & 4 deletions src/fw/drivers/i2c/sf32lb.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,28 @@ void i2c_irq_handler(I2CBus *bus) {
portEND_SWITCHING_ISR(woken);
}

void i2c_dma_tx_irq_handler(I2CBus *bus) {
I2CBusHal *hal = bus->hal;
I2C_HandleTypeDef *hdl = &hal->state->hdl;

if (hdl->State == HAL_I2C_STATE_BUSY_TX) {
HAL_DMA_IRQHandler(&hal->state->hdma_tx);
} else if (hal->state->hdma_tx.State == HAL_DMA_STATE_BUSY) {
HAL_DMA_IRQHandler(&hal->state->hdma_tx);
}
}

void i2c_dma_rx_irq_handler(I2CBus *bus) {
I2CBusHal *hal = bus->hal;
I2C_HandleTypeDef *hdl = &hal->state->hdl;

if (hdl->State == HAL_I2C_STATE_BUSY_RX) {
HAL_DMA_IRQHandler(&hal->state->hdma_rx);
} else if (hal->state->hdma_rx.State == HAL_DMA_STATE_BUSY) {
HAL_DMA_IRQHandler(&hal->state->hdma_rx);
}
}

void i2c_hal_init_transfer(I2CBus *bus) {}

void i2c_hal_abort_transfer(I2CBus *bus) {
Expand All @@ -59,11 +81,20 @@ void i2c_hal_start_transfer(I2CBus *bus) {
}
} else {
if (transfer->direction == I2CTransferDirection_Read) {
ret =
HAL_I2C_Master_Receive_IT(hdl, transfer->device_address, transfer->data, transfer->size);
if (hal->state->hdma_rx.Instance != NULL) {
HAL_DMA_Init(&hal->state->hdma_rx);
mpu_dcache_invalidate(transfer->data, transfer->size);
ret = HAL_I2C_Master_Receive_DMA(hdl, transfer->device_address, transfer->data, transfer->size);
} else {
ret = HAL_I2C_Master_Receive_IT(hdl, transfer->device_address, transfer->data, transfer->size);
}
} else {
ret =
HAL_I2C_Master_Transmit_IT(hdl, transfer->device_address, transfer->data, transfer->size);
if (hal->state->hdma_tx.Instance != NULL) {
HAL_DMA_Init(&hal->state->hdma_tx);
ret = HAL_I2C_Master_Transmit_DMA(hdl, transfer->device_address, transfer->data, transfer->size);
} else {
ret = HAL_I2C_Master_Transmit_IT(hdl, transfer->device_address, transfer->data, transfer->size);
}
}
}

Expand Down Expand Up @@ -109,6 +140,19 @@ void i2c_hal_init(I2CBus *bus) {
ret = HAL_I2C_Init(hdl);
PBL_ASSERTN(ret == HAL_OK);

if (hal->state->hdma_tx.Instance != NULL && hal->state->hdma_rx.Instance != NULL ) {
__HAL_LINKDMA(hdl, hdmatx, hal->state->hdma_tx);
__HAL_LINKDMA(hdl, hdmarx, hal->state->hdma_rx);

HAL_I2C_DMA_Init(hdl, &hal->state->dma_rx, &hal->state->dma_tx);

HAL_NVIC_SetPriority(hal->dma_tx_irqn, hal->dma_irq_priority, 0);
NVIC_EnableIRQ(hal->dma_tx_irqn);

HAL_NVIC_SetPriority(hal->dma_rx_irqn, hal->dma_irq_priority, 0);
NVIC_EnableIRQ(hal->dma_rx_irqn);
}

HAL_NVIC_SetPriority(hal->irqn, hal->irq_priority, 0);
NVIC_EnableIRQ(hal->irqn);
}
9 changes: 9 additions & 0 deletions src/fw/drivers/i2c/sf32lb.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@

typedef struct I2CBusHalState {
I2C_HandleTypeDef hdl;
DMA_HandleTypeDef hdma_tx;
DMA_HandleTypeDef hdma_rx;
struct dma_config dma_tx;
struct dma_config dma_rx;
} I2CBusHalState;

typedef const struct I2CBusHal {
Expand All @@ -20,6 +24,11 @@ typedef const struct I2CBusHal {
RCC_MODULE_TYPE module;
IRQn_Type irqn;
uint8_t irq_priority;
IRQn_Type dma_tx_irqn;
IRQn_Type dma_rx_irqn;
uint8_t dma_irq_priority;
} I2CBusHal;

void i2c_irq_handler(I2CBus *bus);
void i2c_dma_tx_irq_handler(I2CBus *bus);
void i2c_dma_rx_irq_handler(I2CBus *bus);
Loading