/* Allocate a DMA with configurations. 
This function will allocate a proper channel for a DMA transfer request. */
enum status_code dma_allocate(struct dma_resource *resource, struct dma_resource_config *config)
/* Allocate a DMA with configurations. 
This function will allocate a proper channel for a DMA transfer request. */
enum status_code dma_allocate(struct dma_resource *resource, struct dma_resource_config *config)
{
	uint8_t new_channel;
	if (!_dma_inst._dma_init) {
		system_peripheral_reset(PERIPHERAL_DMA);
		LPMCU_MISC_REGS0->IRQ_MUX_IO_SEL_3.bit.MUX_15 = LPMCU_MISC_REGS_IRQ_MUX_IO_SEL_3_MUX_15_16_Val;
		system_register_isr(31, (uint32_t)dma_isr_handler);
		_dma_inst._dma_init = true;
	}
	new_channel = _dma_find_first_free_channel_and_allocate();
	if (new_channel == DMA_INVALID_CHANNEL) {
		return STATUS_ERR_NOT_FOUND;
	}
	resource->channel_id = new_channel;
	_dma_set_config(resource, config);
	resource->descriptor = NULL;
	_dma_active_resource[resource->channel_id] = resource;
	return STATUS_OK;
}