/* ########################################################################################################################################################## */ /* # Includes #################################################################################################################################### Includes # */ /* ########################################################################################################################################################## */ #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wpadded" #include #pragma GCC diagnostic pop #include "gpio.h" /* ########################################################################################################################################################## */ /* # Global variables #################################################################################################################### Global variables # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # Private define ######################################################################################################################## Private define # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # Private macro ########################################################################################################################## Private macro # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # Private typedef ###################################################################################################################### Private typedef # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # Private functions prototypes ############################################################################################ Private functions prototypes # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # ISR, Hook and Callback functions prototypes ############################################################## ISR, Hook and Callback functions prototypes # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # Private variables ################################################################################################################## Private variables # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # Private functions ################################################################################################################## Private functions # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # Main, ISR, Hook and Callback functions ######################################################################## Main, ISR, Hook and Callback functions # */ /* ########################################################################################################################################################## */ /* ########################################################################################################################################################## */ /* # Public functions #################################################################################################################### Public functions # */ /* ########################################################################################################################################################## */ EXITCODE STM32F1_GPIO_configInput(GPIO_TypeDef *port, const uint8 pin, const STM32F1_IMODE mode) { if (pin >= 16) { return EXIT_FAILURE; } bool isHiReg = pin >= 8; uint8_fast shift = (isHiReg) ? (uint8_fast) ((pin - 8) * 4) : (uint8_fast) (pin * 4); if (isHiReg) { port->CRH = (port->CRH & ~(15UL << shift)) | (uint32) mode << (shift + 2); } else { port->CRL = (port->CRL & ~(15UL << shift)) | (uint32) mode << (shift + 2); } return EXIT_SUCCESS; } EXITCODE STM32F1_GPIO_configOutput(GPIO_TypeDef *port, const uint8 pin, const STM32F1_OMODE mode, const STM32F1_OSPEED ospeed) { if (pin >= 16) { return EXIT_FAILURE; } bool isHiReg = pin >= 8; uint8_fast shift = (isHiReg) ? (uint8_fast) ((pin - 8) * 4) : (uint8_fast) (pin * 4); if (isHiReg) { port->CRH = (port->CRH & ~(15UL << shift)) | (uint32) mode << (shift + 2) | (uint32) ospeed << shift; } else { port->CRL = (port->CRL & ~(15UL << shift)) | (uint32) mode << (shift + 2) | (uint32) ospeed << shift; } return EXIT_SUCCESS; } EXITCODE STM32F1_GPIO_configAlternate(GPIO_TypeDef *port, const uint8 pin, const STM32F1_OMODE mode, const STM32F1_OSPEED ospeed) { if (pin >= 16) { return EXIT_FAILURE; } bool isHiReg = pin >= 8; uint8_fast shift = (isHiReg) ? (uint8_fast) ((pin - 8) * 4) : (uint8_fast) (pin * 4); if (isHiReg) { port->CRH = (port->CRH & ~(15UL << shift)) | 1UL << (shift + 3) | (uint32) mode << (shift + 2) | (uint32) ospeed << shift; } else { port->CRL = (port->CRL & ~(15UL << shift)) | 1UL << (shift + 3) | (uint32) mode << (shift + 2) | (uint32) ospeed << shift; } return EXIT_SUCCESS; } EXITCODE STM32F1_GPIO_setPin(GPIO_TypeDef *port, const uint8 pin) { if (pin >= 16) { return EXIT_FAILURE; } port->BSRR = 1UL << pin; return EXIT_SUCCESS; } EXITCODE STM32F1_GPIO_unsetPin(GPIO_TypeDef *port, const uint8 pin) { if (pin >= 16) { return EXIT_FAILURE; } port->BRR = 1UL << pin; return EXIT_SUCCESS; } EXITCODE STM32F1_GPIO_putPin(GPIO_TypeDef *port, const uint8 pin, const BYTE value) { if (pin >= 16) { return EXIT_FAILURE; } QuadBYTE temp = port->ODR; temp &= ~(1UL << pin); temp |= (value & 1UL) << pin; port->ODR = temp; return EXIT_SUCCESS; } EXITCODE STM32F1_GPIO_getPin(const GPIO_TypeDef *port, const uint8 pin, BYTE *value) { if (pin >= 16) { return EXIT_FAILURE; } *value = (BYTE) ((port->IDR >> pin) & 1UL); return EXIT_SUCCESS; } BYTE STM32F1_GPIO_getPin_Direct(const GPIO_TypeDef *port, const uint8 pin) { BYTE value; STM32F1_GPIO_getPin(port, pin, &value); return value; } /* ########################################################################################################################################################## */ /* EOF */