diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-02-06 10:12:17 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-02-06 10:12:17 +0100 |
commit | ab6977f4685d0b88fc593273725a0db489c49a1e (patch) | |
tree | bfba129dcacd6b6d58e0af1ef2f2344ca3b6b8fe | |
parent | d4e7c2c1d53fd4aaa47fd370e8b4cc3c981cb12e (diff) |
working leds
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | src/main.c | 84 |
2 files changed, 67 insertions, 19 deletions
@@ -2,3 +2,5 @@ STM32-base STM32-base-STM32Cube bin obj +.cache/ +compile_commands.json @@ -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 <num> of the I/O-shield. <num> should be the number + * of the LED to drive (0, 1, 2 or 3) and <on> 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(); + } + } } |