From 7b4848824dcec8c5564bd2b11371f9c5f4a04376 Mon Sep 17 00:00:00 2001 From: root Date: Tue, 23 Aug 2016 01:56:47 +0100 Subject: working --- app/ringer.c | 169 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 app/ringer.c (limited to 'app/ringer.c') diff --git a/app/ringer.c b/app/ringer.c new file mode 100644 index 0000000..edaad24 --- /dev/null +++ b/app/ringer.c @@ -0,0 +1,169 @@ +#include "project.h" + +static int period; +static int place; +static int ringer_enabled=1; +int ringing; + + +#define C6 1047 +#define E6 1319 + +static void +buzzer_on (void) +{ + gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM1_CH1); + gpio_set_mode (GPIOB, GPIO_MODE_OUTPUT_2_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_TIM1_CH1N); + + timer_reset (TIM1); + + timer_set_mode (TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_CENTER_1, + TIM_CR1_DIR_UP); + timer_set_oc_mode (TIM1, TIM_OC1, TIM_OCM_PWM2); + timer_enable_oc_output (TIM1, TIM_OC1); + timer_enable_oc_output (TIM1, TIM_OC1N); + timer_enable_break_main_output (TIM1); + + timer_set_oc_value (TIM1, TIM_OC1, period / 2); + timer_set_period (TIM1, period); + + timer_enable_counter (TIM1); +} + +static void +buzzer_off (void) +{ + gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO8); + gpio_clear (GPIOA, GPIO8); + gpio_set_mode (GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO13); + gpio_clear (GPIOB, GPIO13); + timer_disable_counter (TIM1); +} + +static void +buzzer_set_freq (int hz) +{ + period = (48000000 / 4) / hz; + timer_set_oc_value (TIM1, TIM_OC1, period / 2); + timer_set_period (TIM1, period); +} + +static void +buzzer_init (void) +{ + + /*buzzer */ + buzzer_off (); + buzzer_set_freq (440); + +} + + +void +ringer_tick (void) +{ + static int t; + + if (!ringing) + return; + ringing--; + + if (!ringing) + { + place = 0; + buzzer_off (); + return; + } + + + + t++; + if (t < 50) + return; + t = 0; + + + switch (place) + { + case 1: + case 3: + case 5: + case 7: + + case 13: + case 15: + case 17: + case 19: + place++; + buzzer_set_freq (C6); + break; + + case 0: + case 12: + place++; + buzzer_set_freq (E6); + buzzer_on (); + break; + case 2: + case 4: + case 6: + case 14: + case 16: + case 18: + place++; + buzzer_set_freq (E6); + break; + + case 8: + case 20: + place++; + buzzer_off (); + break; + + default: + place++; + break; + case 59: + place = 0; + } + + + + +} + +void +ringer_off (void) +{ + place = 0; + ringing = 0; + buzzer_off (); +} + +void +ringer_ring (int l) +{ +//mb(); +if (!ringer_enabled) return; + ringing = l; +} + +void ringer_disable(void) +{ +ringer_enabled=0; +ringer_off(); +} + +void ringer_enable(void) +{ +ringer_enabled=1; +} + + + +void ringer_init(void) +{ +buzzer_init(); +} -- cgit v1.2.3