-
Notifications
You must be signed in to change notification settings - Fork 2
/
sub_sound.c
274 lines (182 loc) · 6.5 KB
/
sub_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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
////////////////////////////////////////////////////////////////////////////////////////
// The 303 Defcon Badge Project for 2012
//
// This is a hardware and software platform that can be used to make a cool badge
// It includes lightshow, sound DAC, and chiptunes subsystem
// Alcohol sensor, and an RF communciation system.
// The software includes the ability to access functions locally, and remotely given
// sufficient badge privilege, it also has an STD simulator that probabalistically
// causes badges to infect other badges through the beacon subsystem.
////////////////////////////////////////////////////////////////////////////////////////
//
// Please see general credits and information in the main.c file
//
// (c) 2012, Don Kark (Th3D0n)
//
////////////////////////////////////////////////////////////////////////////////////////
#include <system.h>
#include "Badge.h"
#define DAC_I2C_ADDRESS 0x60 // Default I2C address for 7406 Chip
#define DAC_CONFIG 10000000b // Set to write DAC config bits; no powerr down, VDD ref
#define DAC_CMD_VALOUT 0x00 // Command to set DAC output value (volatile)
static void sound_wait_poll();
static void sound_wait_pollint();
void sound_i2c_reset()
{
//
// Generate start condition and wait
//
pir3.SSP2IF = 0; // Clear Interrupt and Collision Indicators
pir3.BCL2IF = 0;
sound_wait_poll(); // Wait till OK to send command
ssp2con2.SEN = 1; // Send Start Condition
sound_wait_poll(); // Wait until start condition finished
pir3.SSP2IF = 0; // Clear Interrupt and Collision Indicators
pir3.BCL2IF = 0;
ssp2buf = 0xff ; // Send "reset" device address
sound_wait_poll(); // Wait Until Complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision Indicators
pir3.BCL2IF = 0;
ssp2con2.RSEN = 1; // Restart Condition
sound_wait_poll(); // Wait Until Complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision Indicators
pir3.BCL2IF = 0;
ssp2con2.PEN = 1; // Send Stop Condition
sound_wait_poll();
// Turn Amp On
//delay_ms(10);
//sound_amp_on();
//delay_ms(10);
}
void sound_config_polled()
{
//
// Generate start condition and wait
//
pir3.SSP2IF = 0; // Clear Interrupt and Collision Indicators
pir3.BCL2IF = 0;
sound_wait_poll(); // Wait till OK to send command
ssp2con2.SEN = 1; // Send Start Condition
sound_wait_poll(); // Wait until start condition finished
pir3.SSP2IF = 0; // Clear Interrupt and Collision Indicators
pir3.BCL2IF = 0;
ssp2buf = DAC_I2C_ADDRESS<<1 ; // Send DAC device address
sound_wait_poll(); // Wait Until Complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision Indicators
pir3.BCL2IF = 0;
ssp2buf = DAC_CONFIG; // Send Configuration Command
sound_wait_poll(); // Wait Until Complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision Indicators
pir3.BCL2IF = 0;
ssp2con2.PEN = 1; // Send Stop Condition
sound_wait_poll();
// Turn Amp On
//delay_ms(10);
//sound_amp_on();
//delay_ms(10);
}
static void sound_wait_poll()
{
while ((ssp2con2 & 0x17) || ssp2stat.READ_WRITE ); // Wait Until Complete
}
static void sound_wait_pollint()
{
while ((ssp2con2 & 0x17) || ssp2stat.READ_WRITE ); // Wait Until Complete
}
void sound_val_polled(unsigned char dacval)
{
//
// Generate start condition and wait
//
sound_wait_poll(); // Wait for all-clear to send
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2con2.SEN = 1; // Send Start Condition
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2buf = DAC_I2C_ADDRESS<<1; // Send device address
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2buf = DAC_CMD_VALOUT; // Set to write DAC; no powerr down
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2buf = dacval; // Set the DAC value
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2con2.PEN = 1; // Send Stop Condition Stop
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
}
void sound_hsstart()
{
//
// Generate start condition and wait
//
sound_wait_poll(); // Wait for all-clear to send
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2con2.SEN = 1; // Send Start Condition
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2buf = 0x08; // Set HS enable
sound_wait_poll(); // Wait to complete (does not ack they HS command)
ssp2stat.SMP = 1; // Disable slew rate control
ssp2add = 5; // Set to HS mode (set BRG to higher speed) (8MHz/(val) + 1) (was 3)
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2con2.RSEN = 1; // Send Repeated Start Condition in HS mode
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2buf = DAC_I2C_ADDRESS<<1; // Send device address
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
}
void sound_val_hs(unsigned char dacval)
{
//
// Generate start condition and wait
//
sound_wait_pollint(); // Wait for all-clear to send
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2buf = DAC_CMD_VALOUT; // Set to write DAC; no powerr down
sound_wait_pollint(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2buf = dacval; // Set the DAC value
}
void sound_hsstop()
{
//
// Generate start condition and wait
//
sound_wait_poll(); // Wait for all-clear to send
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2con2.PEN = 1; // Send Stop Condition Stop
sound_wait_poll(); // Wait to complete
pir3.SSP2IF = 0; // Clear Interrupt and Collision FLags
pir3.BCL2IF = 0;
ssp2stat.SMP = 0; //Enable slew rate control
ssp2add = 19; // Reset to Normal FS I2C rate
}
// // Non interrupt driven beeper. Does not play well if interrupt-based sounds being played
void sound_beep_polled()
{
unsigned char i;
sound_amp_on();
delay_ms(100);
for(i=0; i < 255; i++) {
sound_val_polled( i & 0x01 ? 178 : 78);
delay_10us(10);
}
sound_amp_off();
}