#include <stdio.h>
#include <stdint.h>
#define HIGH 1
#define LOW 0
#define GPIO_PORT_B 0x40010C00
#define GPIO_PORT_C 0x40011000
#define RCC_CLOCK 0x40021000
#define RCC_APB2 (RCC_CLOCK + 0x18)
#define GPIO_PORT_B_MODE (GPIO_PORT_B + 0x04)
#define GPIO_PORT_B_OUT (GPIO_PORT_B + 0x0C)
#define GPIO_PORT_C_MODE (GPIO_PORT_C + 0x04)
#define GPIO_PORT_C_OUT (GPIO_PORT_C + 0x0C)
#define GPIO_PORT_B_INPUT (GPIO_PORT_B + 0x08)
#define READ_INPUT ((*portBIn >> 12) & HIGH)
#define SET_BIT(reg, bit) (reg |= (HIGH << bit))
#define CLEAR_BIT(reg, bit) (reg &= ~(HIGH << bit))
#define __IO volatile
__IO void delay(uint32_t count) {
for (__IO uint32_t i = 0; i < count; i++) {
// printf("Count: %d \n", i);
}
}
void setUpGPIO(uint32_t *RCC_APB2_Clock, uint32_t *portBMode, uint32_t *portBOut, uint32_t *portCMode, uint32_t *portCOut, uint32_t *portBIn ) {
*RCC_APB2_Clock |= (3 << 3);
*portBMode |= (3 << 4);
*portBMode &= ~(1 << 6);
*portBMode &= ~(3 << 16);
*portBMode &= ~(3 << 18);
*portBMode |= (0x10 << 18);
*portCMode |= (3 << 20);
*portCMode &= ~(1 << 22);
}
int main()
{
__IO uint32_t *RCC_APB2_Clock = (uint32_t *)RCC_APB2;
__IO uint32_t *portBMode = (uint32_t *)GPIO_PORT_B_MODE;
__IO uint32_t *portBOut = (uint32_t *)GPIO_PORT_B_OUT;
__IO uint32_t *portCMode = (uint32_t *)GPIO_PORT_C_MODE;
__IO uint32_t *portCOut = (uint32_t *)GPIO_PORT_C_OUT;
__IO uint32_t *portBIn = (uint32_t*)GPIO_PORT_B_INPUT;
setUpGPIO(RCC_APB2_Clock, portBMode, portBOut, portCMode, portCOut, portBIn);
SET_BIT(*portBOut, 9);
SET_BIT(*portCOut, 13);
while (1) {
// *portBOut ^= (1 << 9);
// *portCOut ^= (1 << 13);
if ((READ_INPUT)) {
SET_BIT(*portBOut, 9);
SET_BIT(*portCOut, 13);
} else {
CLEAR_BIT(*portBOut, 9);
CLEAR_BIT(*portCOut, 13);
}
}
}