-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpi-pwm.c
94 lines (65 loc) · 1.51 KB
/
rpi-pwm.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
/*
Reference:
Accessing the hardware pwm peripheral on the raspberry pi in c++.
www.hertaville.com/rpipwm.html
Huanle Zhang at UC Davis
www.huanlezhang.com
*/
#include "rpi-gpioclock.h"
#include "rpi-pwm.h"
#include "rpi-gpio.h"
volatile Pwm_registers* sysPwm = (Pwm_registers *) PWM_BASE;
volatile static char* pData;
volatile static int status = -1; // -1 - not start; 0 - start; 1 - stop
volatile static double volScale = 1.0;
volatile static int bWritable = 0; // 0-playing, 1-waiting, 2-ready
void pwmInitBuffer(char *data){
pData = data;
}
void pwmAddBuffer(void){
bWritable = 2;
}
inline int pwmBufferWritable(void){
if (bWritable == 1) return 1;
else return 0;
}
inline int pwmInitialized(void){
return status;
}
inline void pwmStop(void){
status = 1;
}
inline void pwmResume(void){
status = 0;
}
inline void pwmSetVol(double volRatio){
volScale = volRatio;
}
void startPwmAudio(){
char data = 0;
int i = 0;
setGPIOAlt(12, 0);
setGPIOClock(2, 0); // maximum frequency 37.5K, 256 resolution
sysPwm->ctl = 0;
sysPwm->rng1 = 256;
sysPwm->dat1 = 128;
sysPwm->ctl |= PWM_CTL_PWEN1;
status = 1;
bWritable = 1;
while (1){
if(status == 1){ // stop
waitMicroSeconds(10);
continue;
}
if (bWritable == 1) {
waitMicroSeconds(10);
continue;
}
bWritable = 0;
for (i = 0; i < PWM_BUFFER_SIZE; i++){
sysPwm->dat1 = pData[i];
waitMicroSeconds(27);
}
bWritable = 1;
}
}