/* Find a free channel for a DMA resource. 
Find a channel for the requested DMA resource. */
static uint8_t _dma_find_first_free_channel_and_allocate(void)
/* Find a free channel for a DMA resource. 
Find a channel for the requested DMA resource. */
static uint8_t _dma_find_first_free_channel_and_allocate(void)
{
	uint8_t count;
	uint32_t tmp;
	bool allocated = false;
	system_interrupt_enter_critical_section();
	tmp = _dma_inst.allocated_channels;
	for (count = 0; count < CONF_MAX_USED_CHANNEL_NUM; ++count) {
		if (!(tmp & 0x00000001)) {
			_dma_inst.allocated_channels |= 1 << count;
			_dma_inst.free_channels--;
			allocated = true;
			break;
		}
		tmp = tmp >> 1;
	}
	system_interrupt_leave_critical_section();
	if (!allocated) {
		return DMA_INVALID_CHANNEL;
	} else {
		return count;
	}
}