diff --git a/drivers/afio.c b/drivers/afio.c new file mode 100644 index 0000000..688bc20 --- /dev/null +++ b/drivers/afio.c @@ -0,0 +1,63 @@ +/** @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------------------------------------------------------------- + diff --git a/drivers/afio.h b/drivers/afio.h new file mode 100644 index 0000000..961390c --- /dev/null +++ b/drivers/afio.h @@ -0,0 +1,26 @@ +/** @file afio.h + * Module handling Alternate Functions for Inputs/Outputs + * + * The module provides functions to remap ios to diverse peripherals + */ + +#ifndef _AFIO_H_ +#define _AFIO_H_ + +//--includes-------------------------------------------------------------------- + +#include "stdint.h" +#include "stdbool.h" + +#include "exti.h" + + +//--type definitions------------------------------------------------------------ + +//--functions------------------------------------------------------------------- + +void afio_map_exti(enum ExtiLine line_mask, enum GpioPort port); + + +#endif //_AFIO_H_ +