Created
March 5, 2015 00:40
-
-
Save ChuckM/927f63e7aed180290503 to your computer and use it in GitHub Desktop.
mutex example
This file contains hidden or 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
#include <libopencm3/stm32/rcc.h> | |
#include <libopencm3/stm32/gpio.h> | |
#include <libopencm3/cm3/nvic.h> | |
#include <libopencm3/cm3/systick.h> | |
#include <libopencm3/cm3/sync.h> | |
#include <local_libs/util_timer.h> | |
#include <local_conf/board_conf.h> | |
static mutex_t mtx; | |
static uint32_t systick_busy_cnt = 0; | |
static uint32_t systick_busy_total = 0; | |
static void clock_setup(void) | |
{ | |
/* Run at 72MHz */ | |
rcc_clock_setup_in_board_out_72mhz(); | |
} | |
static void gpio_setup(void) | |
{ | |
/* Enable LED GPIO clock. */ | |
rcc_periph_clock_enable(RCC_GPIOB); | |
rcc_periph_clock_enable(RCC_GPIOA); | |
/* Set LED GPIO to 'output push-pull'. */ | |
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, | |
GPIO_CNF_OUTPUT_PUSHPULL, GPIO11); | |
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, | |
GPIO_CNF_OUTPUT_PUSHPULL, GPIO2); | |
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, | |
GPIO_CNF_OUTPUT_PUSHPULL, GPIO3); | |
} | |
/* | |
* Set up the systick system periodic interrupt | |
*/ | |
static void systick_setup(void) | |
{ | |
/* 72MHz / 8 => 9000000 counts per second. */ | |
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8); | |
/* 9000000/9000 = 1000 overflows per second - every 1ms one interrupt */ | |
/* SysTick interrupt every N clock pulses: set reload to N-1 */ | |
systick_set_reload(8999); | |
systick_interrupt_enable(); | |
/* Start counting. */ | |
systick_counter_enable(); | |
} | |
/* | |
* Called at 1kHz | |
*/ | |
void sys_tick_handler(void) | |
{ | |
int i; | |
if (mutex_trylock(&mtx)) | |
{ | |
gpio_toggle(GPIOA,GPIO3); | |
mutex_unlock(&mtx); | |
} | |
} | |
int main(void) | |
{ | |
clock_setup(); | |
gpio_setup(); | |
util_timer_t tim_ms; | |
tim_ms.periph = DELAY_TIM; | |
tim_ms.res = RES_MS; | |
timer_init(&tim_ms); | |
systick_setup(); | |
while (1) | |
{ | |
mutex_lock(&mtx); | |
gpio_set(GPIOB,GPIO11); | |
gpio_set(GPIOA,GPIO2); | |
timer_delay(&tim_ms,20); // wait 20 ms with timer | |
gpio_clear(GPIOB,GPIO11); | |
gpio_clear(GPIOA,GPIO2); | |
mutex_unlock(&mtx); | |
timer_delay(&tim_ms,80); // wait 80 ms with timer | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment