-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
77 lines (67 loc) · 2.06 KB
/
sound.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
* Copyright Jacques Deschênes 2018, 2019
* This file is part of BP_CHIPCON.
*
* BP_CHIPCON is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BP_CHIPCON is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with BP_CHIPCON. If not, see <http://www.gnu.org/licenses/>.
*/
#include "sound.h"
#include "tvout.h"
#include "chipcon_vm.h"
void sound_init(){
AFIO->MAPR&=AFIO_MAPR_TIM2_REMAP_MASK;
AFIO->MAPR|=3<<AFIO_MAPR_TIM2_REMAP_POS;
config_pin(PORTB,10,OUTPUT_ALT_PP_SLOW);
RCC->APB1ENR|=RCC_APB1ENR_TIM2EN;
TMR2->CCMR2=(7<<TMR_CCMR2_OC3M_POS)|TMR_CCMR2_OC3PE;
TMR2->CCER=TMR_CCER_CC3E;
TMR2->PSC=9; //FCLK/10
set_int_priority(IRQ_TIM2,0);
enable_interrupt(IRQ_TIM2);
}
void tone(uint16_t freq, uint16_t duration){
TMR2->ARR=FCLK/10/freq;
TMR2->CCR3=TMR2->ARR/2;
TMR2->CR1|=TMR_CR1_CEN;
sound_timer=duration;
}
void beep(uint16_t duration){
tone(1000,duration);
}
void sound_stop(){
TMR2->CR1&=~TMR_CR1_CEN;
TMR2->DIER&=~TMR_DIER_UIE;
}
static const uint16_t tempered_scale[16]={
440,466,494,523,554,587,622,659,698,740,784,831,880,932,988,1046
};
// joue une note de la gamme tempérée
void key_tone(int note, int length,int wait_end){
tone(tempered_scale[note],length);
if (wait_end) while (sound_timer);
}
// produit un bruit
void noise(int length){
srand(ntsc_ticks);
tone(6000,length);
TMR2->DIER|=TMR_DIER_UIE;
}
void __attribute__((__interrupt__)) sound_handler(){
uint8_t byte,mask;
if (rand()&1){
TMR2->CCER|=TMR_CCER_CC3P;
}else{
TMR2->CCER&=~TMR_CCER_CC3P;
}
TMR2->SR&=~TMR_SR_UIF;
}