Last active
November 26, 2017 21:15
-
-
Save arpruss/5be978f59669b3942242db7abf954c68 to your computer and use it in GitHub Desktop.
stm32f1 bitbanding gpio
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BITBAND_SRAM_BASE 0x22000000u | |
#define SRAM_START 0x20000000u | |
#define BITBAND_PERIPHERAL_BASE 0x42000000u | |
#define PERIPHERAL_START 0x40000000u | |
#define BITBAND_SRAM(address, bit) ( *(volatile uint32*)( BITBAND_SRAM_BASE + ((uint32)(address)-SRAM_START) * 32u + (uint32)(bit)*4u) ) | |
#define BITBAND_PERIPHERAL(address, bit) *( (volatile uint32*)( BITBAND_PERIPHERAL_BASE + ((uint32)(address)-PERIPHERAL_START) * 32u + (uint32)(bit)*4u) ) | |
#define GPIO_OFFSET(gpioLetter) (0x400u * (uint32)((gpioLetter)-'A')) | |
#define BITBAND_GPIO_INPUT(gpioLetter, bit) BITBAND_PERIPHERAL((uint32)&(GPIOA_BASE->IDR)+GPIO_OFFSET(gpioLetter), (bit)) | |
#define BITBAND_GPIO_OUTPUT(gpioLetter, bit) BITBAND_PERIPHERAL((uint32)&(GPIOA_BASE->ODR)+GPIO_OFFSET(gpioLetter), (bit)) | |
#define ATOI2(s) ((s)[1] ? 10*(uint32)((s)[0]-'0') + (s)[1]-'0' : (uint32)((s)[0]-'0')) | |
#define XPIN_TO_GPIO(pin) (#pin[1]) | |
#define XPARSE_PIN_BIT(pin) (ATOI2(#pin+2)) | |
#define XVALIDATE_PIN(pin) (#pin[0] == 'P' && #pin[1] >= 'A' && #pin[1] <= 'I' && #pin[2] >= '0' && #pin[2] <= '9' && \ | |
(#pin[3] == 0 || (#pin[3] >= '0' && #pin[3] <= '9' && #pin[4] == 0)) && ATOI2(#pin+2) < 16) | |
#define DIGITAL_READ(pin) XVALIDATE_PIN(pin) ? (BITBAND_GPIO_INPUT(XPIN_TO_GPIO(pin), XPARSE_PIN_BIT(pin))) : digitalRead((pin)) | |
#define DIGITAL_WRITE_BITBAND(pin,value) \ | |
do { \ | |
if (XVALIDATE_PIN(pin)) \ | |
(BITBAND_GPIO_OUTPUT(XPIN_TO_GPIO(pin), XPARSE_PIN_BIT(pin)))=(value); \ | |
else \ | |
digitalWrite((pin),(value)); \ | |
} while(0) | |
#define DIGITAL_WRITE(pin,value) \ | |
do { \ | |
if (XVALIDATE_PIN(pin)) \ | |
*(volatile uint32*)((uint32)&(GPIOA_BASE->BSRR)+GPIO_OFFSET(XPIN_TO_GPIO(pin))) = (0x10000u<<(XPARSE_PIN_BIT(pin)))>>(((uint8)(value))<<4); \ | |
else \ | |
digitalWrite((pin),(value)); \ | |
} while(0) | |
void setup() { | |
Serial.begin(9600); | |
pinMode(PB12, OUTPUT); | |
} | |
void loop() { | |
DIGITAL_WRITE(PB12,1); | |
Serial.println(String(DIGITAL_READ(PB12),HEX) ); | |
delay(1000); | |
DIGITAL_WRITE(PB12,0); | |
Serial.println(String(DIGITAL_READ(PB12),HEX) ); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment