This repository has been archived by the owner on Feb 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound.c
92 lines (78 loc) · 2.13 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
* pwm.c
*
* Created on: Mar 8, 2020
* Author: Troi-Ryan Stoeffler
*/
#include "sound.h"
#include <stdbool.h>
#include "hw_types.h"
#include "hw_ints.h"
#include "hw_memmap.h"
#include "hw_apps_rcm.h"
#include "hw_common_reg.h"
#include "interrupt.h"
#include "rom.h"
#include "rom_map.h"
#include "timer.h"
#include "utils.h"
#include "prcm.h"
#include "timer_if.h"
#include "gpio.h"
static bool freqFlag = false, isGenerating = false;
static char *empty = "";
char *song = "";
void playSound(char *newSong) {
song = newSong;
}
void stopSound(void) {
song = empty;
}
int isSoundPlaying(void) {
return song[0] == '\0';
}
void generateFrequency(unsigned long frequency) {
if (!isGenerating) {
isGenerating = true;
Tick_Timer_IF_Start(TIMERA0_BASE, TIMER_A, 80000000 / (frequency / 2));
} else {
MAP_TimerLoadSet(TIMERA0_BASE, TIMER_A, 80000000 / (frequency / 2));
}
}
void stopFrequencyGenerator(void) {
isGenerating = false;
freqFlag = false;
GPIOPinWrite(GPIOA0_BASE, 0x1, 0x0);
Timer_IF_Stop(TIMERA0_BASE, TIMER_A); //stop the timer
}
void InitSoundModules() {
GPIOPinWrite(GPIOA0_BASE, 0x1, 0x0);
isGenerating = false;
Timer_IF_Init(PRCM_TIMERA0, TIMERA0_BASE, TIMER_CFG_PERIODIC, TIMER_A, 0);
Timer_IF_IntSetup(TIMERA0_BASE, TIMER_A, frequencyGenerator);
}
void DeInitSoundModules() {
GPIOPinWrite(GPIOA0_BASE, 0x1, 0x0);
MAP_TimerDisable(TIMERA0_BASE, TIMER_A);
MAP_PRCMPeripheralClkDisable(PRCM_TIMERA0, PRCM_RUN_MODE_CLK);
}
void updateSoundModules(void) {
if (song[0] == '\0') {
stopFrequencyGenerator();
} else {
generateFrequency(2000 + (((song[0] - ZERO_CHAR) - 1) * 375));
song++;
}
}
void frequencyGenerator(void) {
Timer_IF_InterruptClear(TIMERA0_BASE); // clear timer interrupt
if (freqFlag = !freqFlag) {
GPIOPinWrite(GPIOA0_BASE, 0x1, 0x1);
} else {
GPIOPinWrite(GPIOA0_BASE, 0x1, 0x0);
}
}
static void Tick_Timer_IF_Start(unsigned long ulBase, unsigned long ulTimer, unsigned long ulValue) {
MAP_TimerLoadSet(ulBase,ulTimer,ulValue);
MAP_TimerEnable(ulBase,ulTimer);
}