/** @file afio.c
 * Module handling Alternate Functions for Inputs/Outputs
 *
 * The module provides functions to remap ios to diverse peripherals
 */

//--includes--------------------------------------------------------------------

#include "afio.h"
#include "afio_regs.h"

#include "rcc.h"


//--local definitions-----------------------------------------------------------

//--local variables-------------------------------------------------------------

static volatile struct AFIO* regs = (struct AFIO*)AFIO_BASE_ADDRESS;


//--public functions------------------------------------------------------------

void afio_map_exti(enum ExtiLine line_mask, enum GpioPort port)
{
	//ensure afio peripheral is enabled
	rcc_enable(RCC_AHB_NONE, RCC_APB1_NONE, RCC_APB2_AFIO);

	for (uint8_t i = 0; i < 4; ++i) {
		if (line_mask & (0x1 << i)) {
			regs->EXTICR1.word &= ~(0xF << (4 * i));
			regs->EXTICR1.word |= (port << (4 * i));
		}
	}

	line_mask = line_mask >> 4;
	for (uint8_t i = 0; i < 4; ++i) {
		if (line_mask & (0x1 << i)) {
			regs->EXTICR2.word &= ~(0xF << (4 * i));
			regs->EXTICR2.word |= (port << (4 * i));
		}
	}

	line_mask = line_mask >> 4;
	for (uint8_t i = 0; i < 4; ++i) {
		if (line_mask & (0x1 << i)) {
			regs->EXTICR3.word &= ~(0xF << (4 * i));
			regs->EXTICR3.word |= (port << (4 * i));
		}
	}

	line_mask = line_mask >> 4;
	for (uint8_t i = 0; i < 4; ++i) {
		if (line_mask & (0x1 << i)) {
			regs->EXTICR4.word &= ~(0xF << (4 * i));
			regs->EXTICR4.word |= (port << (4 * i));
		}
	}
}


//--local functions-------------------------------------------------------------

