-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrums.c
More file actions
152 lines (128 loc) · 3.73 KB
/
Copy pathdrums.c
File metadata and controls
152 lines (128 loc) · 3.73 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "stm32f10x.h"
#include "stm32-ints.h"
#include "samples/kick-16bit.h"
#include "samples/crash-16bit.h"
#include "samples/snare-16bit.h"
#include "samples/ride-16bit.h"
#include "samples/tom1-16bit.h"
#include "samples/tom2-16bit.h"
#define bit_set(var,bitno) ((var) |= 1 << (bitno))
#define bit_clr(var,bitno) ((var) &= ~(1 << (bitno)))
#define testbit(var,bitno) (((var)>>(bitno)) & 0x01)
#define TMR_CLK_HZ 24e6
#define TMR_FREQ_HZ 22050
#define TMR_LOAD_VAL ((TMR_CLK_HZ) / (TMR_FREQ_HZ))
#define BTTN_STABLE_TIME_MS 30
/* the button polling code repeats with frequency of audio timer */
#define BTTN_STABLE_CYCLES (((BTTN_STABLE_TIME_MS)*(TMR_FREQ_HZ)) / 1000)
#define dac_write_12bit_left_aligned(val) DAC->DHR12L1 = val
#define TRACK_NR 6
#define NR_SAMPLES_OF(track) ((sizeof(track))/(sizeof(s16)))
#define MIN_SIGNED_16BIT_VAL -32768
#define MAX_SIGNED_16BIT_VAL 32767
void timer2_init(void)
{
bit_set(RCC->APB1ENR, 0); /* enable TMR2 clock */
TIM2->ARR = TMR_LOAD_VAL;
TIM2->CR1 = 0;
TIM2->CR2 = 0;
TIM2->PSC = 0; /* no prescaler */
bit_set(TIM2->CR2, 5); /* timer update event used for TRGO */
bit_set(TIM2->CR1, 7); /* auto reload */
bit_set(TIM2->CR1, 0); /* counter enable */
}
void dac_init(void)
{
bit_set(RCC->APB1ENR, 29); /* enable DAC clock */
DAC->CR = 0;
bit_set(DAC->CR, 5); /* Timer 2 TRGO trigger */
bit_set(DAC->CR, 2); /* trigger enable */
bit_set(DAC->CR, 0); /* DAC channel 1 enable */
}
void set_fast_clk(void)
{
/* setting SYSCLK to 24 MHz internal clock
SYSCLK = (HSI / 2) * PLL_MUL = (8 / 2) * 6 = 24 MHz */
bit_set(RCC->CFGR, 20); /* PLL x 6 (must be set while PLL off) */
bit_set(RCC->CR, 24); /* PLL ON */
bit_set(RCC->CFGR, 1); /* PLL CLK is SYSCLK */
}
void main(void)
{
int i = 0;
u16 dac_val = 0;
char bttn_press = 0;
signed int mix = 0, samp_sum = 0;
unsigned int button_tracker[TRACK_NR];
char tracks_playing = 0; /* each bit tells us track playing */
int samp_play_cnt[TRACK_NR] = {0};
int tracks_samp_nr[TRACK_NR] = {NR_SAMPLES_OF(kick), NR_SAMPLES_OF(crash),
NR_SAMPLES_OF(snare), NR_SAMPLES_OF(ride),
NR_SAMPLES_OF(tom1), NR_SAMPLES_OF(tom2)};
s16 const *tracks[TRACK_NR] = {
kick,
crash,
snare,
ride,
tom1,
tom2
};
set_fast_clk(); /* enable 24 MHz SYSCLK */
RCC->APB2ENR |= (1 << 3) | 1; /* Enable the GPIOB (bit 3) and AFIO (bit 0) */
bit_set(AFIO->MAPR, 25); /* JTAG disabled - SWD enabled to have PB3 available as GPIO */
GPIOB->CRL = 0x88888888; /* Set GPIOB Pin 0 - 7 to input pull-up/down */
GPIOB->ODR = 0xffffffff;
dac_init();
timer2_init();
for (i = 0; i < TRACK_NR; i++)
button_tracker[i] = 0;
while (1)
{
mix = 0;
samp_sum = 0;
for (i = 0; i < TRACK_NR; i++)
{
if (testbit(GPIOB->IDR, i) == 0)
{
if (button_tracker[i] == BTTN_STABLE_CYCLES)
{
/* stable, we call a button press */
bit_set(tracks_playing, i);
samp_play_cnt[i] = tracks_samp_nr[i];
}
button_tracker[i]++;
}
else
button_tracker[i] = 0;
}
for (i = 0; i < TRACK_NR; i++) /* here we mix the samples */
{
if (testbit(tracks_playing, i) == 1)
{
int current_samp_nr = tracks_samp_nr[i] - samp_play_cnt[i];
samp_sum += tracks[i][current_samp_nr];
samp_play_cnt[i]--; /* decrement number of samples left to play */
if (samp_play_cnt[i] == 0)
bit_clr(tracks_playing, i); /* finished playing this track */
}
}
mix = samp_sum;
if (mix < -32768)
mix = -32768;
else if (mix > 32767)
mix = 32767;
mix += 32768; /* add DC offset to convert to unsingned for DAC*/
dac_val = mix;
dac_write_12bit_left_aligned(dac_val);
while (testbit(TIM2->SR, 0) == 0); /* wait for timer reset */
bit_clr(TIM2->SR, 0);
}
}
void nmi_handler(void)
{
return;
}
void hardfault_handler(void)
{
return;
}