This commit is contained in:
2022-02-04 10:14:45 -06:00
parent 2e0a643432
commit a7303fe5f9
7 changed files with 221 additions and 101 deletions

1
.gitignore vendored
View File

@@ -58,3 +58,4 @@ dkms.conf
# Other
*/.settings
/.vscode

View File

@@ -1,24 +0,0 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"cStandard": "c11",
"cppStandard": "c++17",
"browse": {
"path": [
"${workspaceFolder}"
]
},
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}

View File

@@ -1,5 +0,0 @@
{
"files.associations": {
"math.h": "c"
}
}

View File

@@ -1,7 +1,7 @@
/**
* @file piezoSpeaker.c
* @author Trevor Barnes
* @brief Provides funtionality for initializing, playing notes, and playing songs on the piezo speaker.
* @brief Provides functionality for initializing, playing notes, and playing songs on the piezo speaker.
* @version 0.1
* @date 2022-01-12
*

View File

@@ -15,37 +15,109 @@
#define PIEZOSPEAKER_H_
// RCC
#define RCC_AHB1ENR (volatile uint32_t*) 0x40023830
#define RCC_APB1ENR (volatile uint32_t*) 0x40023840
typedef struct {
uint32_t CR;
uint32_t PLL_CFGR;
uint32_t CFGR;
uint32_t CIR;
uint32_t AHB1RSTR;
uint32_t AHB2RSTR;
uint32_t AHB3RSTR;
uint32_t _RESERVED_1_;
uint32_t APB1RSTR;
uint32_t APB2RSTR;
uint32_t _RESERVED_2_;
uint32_t _RESERVED_3_;
uint32_t AHB1ENR;
uint32_t AHB2ENR;
uint32_t AHB3ENR;
uint32_t _RESERVED_4_;
uint32_t APB1ENR;
uint32_t APB2ENR;
uint32_t _RESERVED_5_;
uint32_t _RESERVED_6_;
} RCC;
// GPIOB
#define GPIOB_MODER (volatile uint32_t*) 0x40020400
#define GPIOBEN 1
#define GPIOB_AFRL (volatile uint32_t*) 0x40020420
#define AFRL_TIM3_CH1_EN 17
typedef struct {
uint32_t MODER;
uint32_t OTYPER;
uint32_t OSPEEDER;
uint32_t PUPDR;
uint32_t IDR;
uint32_t ODR;
uint32_t BSRR;
uint32_t LCKR;
uint32_t AFRL;
uint32_t AFRH;
} GPIO;
// Timer 3
#define TIM3_EN 1
#define TIM3_CCMR1 (volatile uint32_t*) 0x40000418
#define OC1PE 3
#define OC1M_PWM2 0b1110000
#define TIM3_CCER (volatile uint32_t*) 0x40000420
#define CCER_CC1E 1
#define TIM3_EGR (volatile uint32_t*) 0x40000414
#define EGR_UG 1
#define TIM3_PSC (volatile uint32_t*) 0x40000428
#define TIM3_ARR (volatile uint32_t*) 0x4000042C
#define TIM3_CCR1 (volatile uint32_t*) 0x40000434
#define TIM3_CR1 (volatile uint32_t*) 0x40000400
#define CR_ARPE_EN 7
#define CR_CEN 1
typedef struct {
uint32_t CR1;
uint32_t CR2;
uint32_t SMCR;
uint32_t DIER;
uint32_t SR;
uint32_t EGR;
uint32_t CCMR1;
uint32_t CCMR2;
uint32_t CCER;
uint32_t CNT;
uint32_t PSC;
uint32_t ARR;
uint32_t _RESERVED_1_;
uint32_t CCR1;
uint32_t CCR2;
uint32_t CCR3;
uint32_t CCR4;
uint32_t _RESERVED_2_;
uint32_t DCR;
uint32_t DMAR;
uint32_t TIM2_OR;
uint32_t TIM5_OR;
} TIM;
#define PB4_AF_V 0b10
#define PB4_AF_P 9
#define pitchDivisor 1000000
// SYSCFG
typedef struct {
uint32_t MEMRMP;
uint32_t PMC;
uint32_t EXTICR1;
uint32_t EXTICR2;
uint32_t EXTICR3;
uint32_t EXTICR4;
uint32_t CMPCR;
uint32_t CFGR;
} SYSCFG;
typedef struct{
// Interrupt
#define SYSCFG_EXTICR4 (volatile uint32_t*) 0x40013814
#define NVIC_ISER1 (volatile uint32_t*) 0xE000E104
typedef struct {
uint32_t IMR;
uint32_t EMR;
uint32_t RTSR;
uint32_t FTSR;
uint32_t SWIER;
uint32_t PR;
} EXTI;
typedef struct {
unsigned int noteFrequency;
unsigned int noteDuration;
} Note;
@@ -59,6 +131,10 @@ void play_note(Note noteToPlay);
// Iterates through an array of note structs and ends at the termination value "END"
void play_song(Note *songToPlay);
void play_note_br(Note noteToplay);
void play_song_br(Note *songToPlay);
// Frequency Value Signifying a rest
#define r 1

View File

@@ -79,7 +79,7 @@ void songInfo() {
printf("Type 1 or 2 to play a song!\n\r");
printf("Song 1: Imperial March from Star Wars\n\r");
printf("Song 2: Metropolis Theme from Ratchet & Clank\n\r");
printf("Song 3: Flower Garden from Yoshi's Island");
printf("Song 3: Flower Garden from Yoshi's Island\n\r");
}
int main(void) {
@@ -92,26 +92,54 @@ int main(void) {
int data;
int length;
int songSelection;
char background;
for(;;) {
// Get command from user
fgets(line, 100, stdin);
// Parse only the command for strcmp
sscanf(line, "%s", command);
if (!strcmp(command, "help")) {
// Print Help
printHelp();
} else if (!strcmp(command, "songs")) {
// Print Song Info
songInfo();
} else if (!strcmp(command, "rmw")) {
// Read Memory
sscanf(line, "%s %X", command, &address);
readMem(address);
} else if (!strcmp(command, "wmw")) {
// Write Memory
sscanf(line, "%s %X %u", command, &address, &data);
writeMem(address, data);
} else if (!strcmp(command, "dm")) {
// Dump Memory
sscanf(line, "%s %X %u", command, &address, &length);
dumpMem(address, length);
} else if (!strcmp(command, "ps")) {
sscanf(line, "%s %u", command, &songSelection);
// Song Selection Command Format:
// "ps {songSelection} {background}"
sscanf(line, "%s %u %c", command, &songSelection, background);
if (background == 'b') {
switch(songSelection) {
case 1:
printf("Playing Imperial March in the background\n\r");
play_song_br(songIM);
break;
case 2:
printf("Playing Metropolis Theme in the background\n\r");
play_song_br(songMT);
break;
case 3:
printf("Playing Flower Garden in the background\n\r");
play_song_br(songFG);
break;
default:
break;
printf("Invalid song selection\n\r");
}
} else {
// Without valid flag, program plays songs in foreground
switch(songSelection) {
case 1:
printf("Playing Imperial March\n\r");
@@ -127,7 +155,10 @@ int main(void) {
break;
default:
break;
printf("Invalid song selection\n\r");
}
}
} else {
printf("Invalid input, type 'help' for instructions\n\r");
}

View File

@@ -1,7 +1,7 @@
/**
* @file piezoSpeaker.c
* @author Trevor Barnes
* @brief Provides funtionality for initializing, playing notes, and playing songs on the piezo speaker.
* @brief Provides functionality for initializing, playing notes, and playing songs on the piezo speaker.
* @version 0.1
* @date 2022-01-12
*
@@ -14,48 +14,54 @@
#include "piezoSpeaker.h"
#include "delay.h"
volatile RCC* const rcc = 0x40023800;
volatile GPIO* const gpiob = 0x40020400;
volatile TIM* const tim3 = 0x40000400;
volatile SYSCFG* const syscfg = 0x40013800;
volatile EXTI* const exti4 = 0x40013C00;
void piezo_init(){
// GPIOB/Timer3 enable in RCC
*RCC_AHB1ENR |= (1<<GPIOBEN);
*RCC_APB1ENR |= (1<<TIM3_EN);
(*rcc).AHB1ENR |= (1<<GPIOBEN);
(*rcc).APB1ENR |= (1<<TIM3_EN);
// Set to "alternate function" mode
*GPIOB_MODER = (*GPIOB_MODER&~(0b11<<8)) | (PB4_AF_V<<8);
(*gpiob).MODER = ((*gpiob).MODER&~(0b11<<8)) | (PB4_AF_V<<8);
// Set AF to low
*GPIOB_AFRL |= (1<<AFRL_TIM3_CH1_EN);
(*gpiob).AFRL |= (1<<AFRL_TIM3_CH1_EN);
// Set to output capture
*TIM3_CCMR1 |= OC1M_PWM2;
*TIM3_CCMR1 |= (1<<OC1PE);
*TIM3_CCER |= CCER_CC1E;
(*tim3).CCMR1 |= OC1M_PWM2;
(*tim3).CCMR1 |= (1<<OC1PE);
(*tim3).CCER |= CCER_CC1E;
// Enable Preload
*TIM3_CR1 |= (1<<CR_ARPE_EN);
(*tim3).CR1 |= (1<<CR_ARPE_EN);
}
void play_note(Note noteToPlay) {
*TIM3_PSC = 15;
(*tim3).PSC = 15;
// Pitch divisor to scale with timer
*TIM3_ARR = (pitchDivisor)/(noteToPlay.noteFrequency);
(*tim3).ARR = (pitchDivisor)/(noteToPlay.noteFrequency);
// Volume (Smaller dividend = louder sound)
unsigned int freq = (noteToPlay.noteFrequency/10);
// Clear CCR
*TIM3_CCR1 = (*TIM3_CCR1&~(0xFFFF));
*TIM3_CCR1 = freq;
(*tim3).CCR1 = ((*tim3).CCR1&~(0xFFFF));
(*tim3).CCR1 = freq;
// Set EGR
*TIM3_EGR |= EGR_UG;
(*tim3).EGR |= EGR_UG;
// Playing note
// Enables timer
*TIM3_CR1 |= 1;
(*tim3).CR1 |= 1;
// Delay for duration of note
delay_1ms(noteToPlay.noteDuration);
// Disables timer
*TIM3_CR1 &= ~1;
(*tim3).CR1 &= ~1;
}
void play_song(Note *songToPlay){
@@ -66,3 +72,38 @@ void play_song(Note *songToPlay){
i++;
}
}
void play_note_br(Note noteToplay) {
}
void TIM3_IRQHandler(void) {
(*tim3).PSC = 15;
// Pitch divisor to scale with timer
(*tim3).ARR = (pitchDivisor)/(noteToPlay.noteFrequency);
// Volume (Smaller dividend = louder sound)
unsigned int freq = (noteToPlay.noteFrequency/10);
// Clear CCR
(*tim3).CCR1 = ((*tim3).CCR1&~(0xFFFF));
(*tim3).CCR1 = freq;
// Set EGR
(*tim3).EGR |= EGR_UG;
// Playing note
// Enables timer
(*tim3).CR1 |= 1;
// Delay for duration of note
// This is gonna have to change
delay_1ms(noteToPlay.noteDuration);
// Disables timer
(*tim3).CR1 &= ~1;
}
void play_song_br(Note *songToPlay) {
// PB4 Connected to EXTI4
(*syscfg).EXTICR2 &= ~(0xF);
(*syscfg).EXTICR2 &= ~(0x1);
}