From ab6977f4685d0b88fc593273725a0db489c49a1e Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sun, 6 Feb 2022 10:12:17 +0100 Subject: working leds --- .gitignore | 2 ++ src/main.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 67 insertions(+), 19 deletions(-) diff --git a/.gitignore b/.gitignore index a11e827..8b8427e 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ STM32-base STM32-base-STM32Cube bin obj +.cache/ +compile_commands.json diff --git a/src/main.c b/src/main.c index 0365c80..dea5b7a 100644 --- a/src/main.c +++ b/src/main.c @@ -1,30 +1,76 @@ #include "stm32f0xx.h" -// Quick and dirty delay -static void delay (unsigned int time) { - for (unsigned int i = 0; i < time; i++) - for (volatile unsigned int j = 0; j < 2000; j++); +#define PINOUT_LED_1 3 +#define PINOUT_LED_2 5 +#define PINOUT_LED_3 4 +#define PINOUT_LED_4 6 + +const unsigned short leds[] = {PINOUT_LED_1, PINOUT_LED_2, PINOUT_LED_3, PINOUT_LED_4}; + +/* + * This function configures the I/O-ports that are used by the I/O-shield. It + * uses register RCC_AHBENR to enable the clocks for the ports that are used, + * register GPIOx_MODER to configure pins as input, analog or output and + * GPIOx_PUPDR to configure pull-up and pull-down resistors. + */ +void shieldConfig() { + // enable clock for I/O port D + RCC->AHBENR |= RCC_AHBENR_GPIOBEN; + + GPIOB->MODER &= ~((0b11 << (PINOUT_LED_1 * 2)) |\ + (0b11 << (PINOUT_LED_2 * 2)) |\ + (0b11 << (PINOUT_LED_3 * 2)) |\ + (0b11 << (PINOUT_LED_4 * 2))); + + GPIOB->MODER |= (0b01 << (PINOUT_LED_1 * 2)) |\ + (0b01 << (PINOUT_LED_2 * 2)) |\ + (0b01 << (PINOUT_LED_3 * 2)) |\ + (0b01 << (PINOUT_LED_4 * 2)); } -int main (void) { - // Turn on the GPIOC peripheral - RCC->AHBENR |= RCC_AHBENR_GPIOCEN; +/* + * This function drives led of the I/O-shield. should be the number + * of the LED to drive (0, 1, 2 or 3) and should be an integer with value + * 0 or 1 which indicates if the LED should be turned on (1) or off (0). + */ +void ledWrite(int num, int on) { + GPIOB->ODR &= ~(1 << leds[num]); + GPIOB->ODR |= (on << leds[num]); +} - // Put pin 13 in general purpose output mode - GPIOC->MODER |= GPIO_MODER_MODER13_0; +/* + * This function returns the status of the pushbutton of the I/O-shield. + * Return value 1 indicates that the button is currently pressed and return + * value 0 indicates that the button is currently not pressed. + */ +int buttonRead() { + return 0; +} - while (1) { - // Reset the state of pin 13 to output low - GPIOC->BSRR = GPIO_BSRR_BR_13; +void delay() +{ + int i; + for (i = 0; i < 50000; i++) + { + asm("nop"); + } +} +int main() +{ + shieldConfig(); - delay(500); + // RCC->AHBENR |= RCC_AHBENR_GPIOAEN; + // GPIOA->MODER &= ~(1 << (PINOUT_LED_1 * 2 + 1)); + // GPIOA->MODER |= (1 << (PINOUT_LED_1 * 2)); - // Set the state of pin 13 to output high - GPIOC->BSRR = GPIO_BSRR_BS_13; + // GPIOA_MODER &= ~(0 << (PINOUT_LED_1 * 2 + 1)); - delay(500); - } + while (1) + { + for(int i = 0; i < 2 * 4; i++) { + ledWrite(i % 4, i >= 4); - // Return 0 to satisfy compiler - return 0; + delay(); + } + } } -- cgit v1.2.3