initial commit
This commit is contained in:
commit
823f030974
8 changed files with 461 additions and 0 deletions
85
src/main.c
Normal file
85
src/main.c
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
#include <libopencm3/cm3/systick.h>
|
||||
#include <libopencm3/stm32/gpio.h>
|
||||
#include <libopencm3/stm32/rcc.h>
|
||||
|
||||
#define LED_OR_PORT GPIOC
|
||||
#define LED_OR_PIN GPIO10
|
||||
|
||||
#define LED_GR_PORT GPIOC
|
||||
#define LED_GR_PIN GPIO11
|
||||
|
||||
#define LED_RD_PORT GPIOC
|
||||
#define LED_RD_PIN GPIO12
|
||||
|
||||
#define COUNT_PERIOD_MS 500
|
||||
|
||||
static volatile uint32_t s_ticks = 0;
|
||||
|
||||
void sys_tick_handler(void) { s_ticks++; }
|
||||
|
||||
static void delay_ms(uint32_t ms) {
|
||||
uint32_t until = s_ticks + ms;
|
||||
while (s_ticks < until)
|
||||
;
|
||||
}
|
||||
|
||||
static void clock_setup(void) {
|
||||
rcc_clock_setup_pll(&rcc_hsi_configs[RCC_CLOCK_3V3_170MHZ]);
|
||||
}
|
||||
|
||||
static void systick_setup(void) {
|
||||
/* 1 ms tick at 170 MHz core clock */
|
||||
systick_set_reload(rcc_ahb_frequency / 1000 - 1);
|
||||
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB);
|
||||
systick_counter_enable();
|
||||
systick_interrupt_enable();
|
||||
}
|
||||
|
||||
static void gpio_setup(void) {
|
||||
rcc_periph_clock_enable(RCC_GPIOC);
|
||||
|
||||
gpio_mode_setup(LED_OR_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_OR_PIN);
|
||||
gpio_mode_setup(LED_GR_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_GR_PIN);
|
||||
gpio_mode_setup(LED_RD_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LED_RD_PIN);
|
||||
|
||||
gpio_set_output_options(LED_OR_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_LOW,
|
||||
LED_OR_PIN);
|
||||
gpio_set_output_options(LED_GR_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_LOW,
|
||||
LED_GR_PIN);
|
||||
gpio_set_output_options(LED_RD_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_LOW,
|
||||
LED_RD_PIN);
|
||||
|
||||
gpio_clear(LED_OR_PORT, LED_OR_PIN);
|
||||
gpio_clear(LED_GR_PORT, LED_GR_PIN);
|
||||
gpio_set(LED_RD_PORT, LED_RD_PIN); // active low
|
||||
}
|
||||
|
||||
static void gpio_write(uint32_t gpioport, uint16_t gpios, uint8_t value) {
|
||||
if (value == 0) {
|
||||
gpio_clear(gpioport, gpios);
|
||||
} else {
|
||||
gpio_set(gpioport, gpios);
|
||||
}
|
||||
}
|
||||
|
||||
static void set_binary_counter(uint8_t value) {
|
||||
gpio_write(LED_OR_PORT, LED_OR_PIN, value & 0x04);
|
||||
gpio_write(LED_GR_PORT, LED_GR_PIN, value & 0x02);
|
||||
gpio_write(LED_RD_PORT, LED_RD_PIN, value & 0x01);
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
clock_setup();
|
||||
systick_setup();
|
||||
gpio_setup();
|
||||
|
||||
uint8_t counter = 0;
|
||||
|
||||
while (1) {
|
||||
set_binary_counter(counter);
|
||||
delay_ms(COUNT_PERIOD_MS);
|
||||
counter++; // overflow is fine
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue