work
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -29,18 +29,18 @@
|
||||
|
||||
// Imperial March - Star Wars
|
||||
Note songIM[138] = {
|
||||
{A4, Q}, {A4, Q}, {A4, Q}, {F4, S*3}, {C5, S}, {A4, Q}, {F4, S*3}, {C5, S}, {A4, H},
|
||||
{E5, Q}, {E5, Q}, {E5, Q}, {F5, S*3}, {C5, S}, {Ab4, Q}, {F4, S*3}, {C5, S}, {A4, H},
|
||||
{A5, Q}, {A4, S*3}, {A4, S}, {A5, Q}, {Ab5, S*3}, {G5, S},
|
||||
{Gb5, S}, {F5, S}, {Gb5, E}, {r, E}, {Bb4, E}, {Eb5, Q}, {D5, S*3}, {Db5, S},
|
||||
{C5, S}, {B4, S}, {C5, E}, {r, E}, {F4, E}, {Ab4, Q}, {F4, S*3}, {A4, S},
|
||||
{C5, Q}, {A4, S*3}, {C5, S}, {E5, H},
|
||||
{A5, Q}, {A4, S*3}, {A4, S}, {A5, Q}, {Ab5, S*3}, {G5, S},
|
||||
{Gb5, S}, {F5, S}, {Gb5, E}, {r, E}, {Bb4, E}, {Eb5, Q}, {D5, S*3}, {Db5, S},
|
||||
{C5, S}, {B4, S}, {C5, E}, {r, E}, {F4, E}, {Ab4, Q}, {F4, S*3}, {C5, S},
|
||||
{A4, Q}, {F4, S*3}, {C5, S}, {A4, H},
|
||||
{A4, Q}, {A4, Q}, {A4, Q}, {F4, S*3}, {C5, S}, {A4, Q}, {F4, S*3}, {C5, S}, {A4, H},
|
||||
{E5, Q}, {E5, Q}, {E5, Q}, {F5, S*3}, {C5, S}, {Ab4, Q}, {F4, S*3}, {C5, S}, {A4, H},
|
||||
{A5, Q}, {A4, S*3}, {A4, S}, {A5, Q}, {Ab5, S*3}, {G5, S},
|
||||
{Gb5, S}, {F5, S}, {Gb5, E}, {r, E}, {Bb4, E}, {Eb5, Q}, {D5, S*3}, {Db5, S},
|
||||
{C5, S}, {B4, S}, {C5, E}, {r, E}, {F4, E}, {Ab4, Q}, {F4, S*3}, {A4, S},
|
||||
{C5, Q}, {A4, S*3}, {C5, S}, {E5, H},
|
||||
{A5, Q}, {A4, S*3}, {A4, S}, {A5, Q}, {Ab5, S*3}, {G5, S},
|
||||
{Gb5, S}, {F5, S}, {Gb5, E}, {r, E}, {Bb4, E}, {Eb5, Q}, {D5, S*3}, {Db5, S},
|
||||
{C5, S}, {B4, S}, {C5, E}, {r, E}, {F4, E}, {Ab4, Q}, {F4, S*3}, {C5, S},
|
||||
{A4, Q}, {F4, S*3}, {C5, S}, {A4, H},
|
||||
{END}
|
||||
};
|
||||
};
|
||||
|
||||
// Metropolis Theme - Ratchet & Clank
|
||||
Note songMT[33] = {
|
||||
@@ -50,84 +50,115 @@ Note songMT[33] = {
|
||||
{Bb5, E}, {F5, E}, {D5, E}, {F5, E}, {Bb5, E}, {F5, E}, {D5, E}, {Bb5, E},
|
||||
{END}
|
||||
};
|
||||
|
||||
|
||||
// Flower Garden - Yoshi's Island
|
||||
Note songFG[77] = {
|
||||
{E4, E}, {r, E}, {G4, E}, {r, S}, {G4, S}, {E4, E}, {C4, E}, {r, Q},
|
||||
{A3, E}, {r, E}, {C4, E}, {r, S}, {A3, S}, {D4, E}, {E4, E}, {r, Q},
|
||||
{E4, E}, {r, E}, {G4, E}, {r, S}, {G4, S}, {E4, E}, {C4, E}, {r, Q},
|
||||
{A3, E}, {r, E}, {C4, E}, {r, S}, {A3, S}, {E4, E}, {D4, E}, {r, Q},
|
||||
{G5, S}, {Gb5, S}, {G5, E+(Q*3)},
|
||||
{r , E}, {F5, E}, {E5, E}, {F5, E}, {E5, E}, {C5, E}, {A4, E}, {G4, E+(Q*5)}, {r, E},
|
||||
{C5, E}, {B4, E}, {D5, E}, {A5, E}, {G5, E+H+Q}, {r, E},
|
||||
{A5, E}, {B5, E}, {A5, E}, {G5, E}, {F5, E}, {E5, E}, {D5, E}, {E5, Q}, {C5, E}, {G4, E+(Q*3)}, {r, E},
|
||||
{C5, E}, {B4, E}, {C5, E}, {D5, E}, {E5, E+Q}, {G5, Q}, {C5, Q}, {E5, Q},
|
||||
{F5, E}, {E5, E}, {F5, E}, {D5, E*2}, {C5, E}, {B4, E}, {C5, E+W},
|
||||
{END}
|
||||
};
|
||||
{E4, E}, {r, E}, {G4, E}, {r, S}, {G4, S}, {E4, E}, {C4, E}, {r, Q},
|
||||
{A3, E}, {r, E}, {C4, E}, {r, S}, {A3, S}, {D4, E}, {E4, E}, {r, Q},
|
||||
{E4, E}, {r, E}, {G4, E}, {r, S}, {G4, S}, {E4, E}, {C4, E}, {r, Q},
|
||||
{A3, E}, {r, E}, {C4, E}, {r, S}, {A3, S}, {E4, E}, {D4, E}, {r, Q},
|
||||
{G5, S}, {Gb5, S}, {G5, E+(Q*3)},
|
||||
{r , E}, {F5, E}, {E5, E}, {F5, E}, {E5, E}, {C5, E}, {A4, E}, {G4, E+(Q*5)}, {r, E},
|
||||
{C5, E}, {B4, E}, {D5, E}, {A5, E}, {G5, E+H+Q}, {r, E},
|
||||
{A5, E}, {B5, E}, {A5, E}, {G5, E}, {F5, E}, {E5, E}, {D5, E}, {E5, Q}, {C5, E}, {G4, E+(Q*3)}, {r, E},
|
||||
{C5, E}, {B4, E}, {C5, E}, {D5, E}, {E5, E+Q}, {G5, Q}, {C5, Q}, {E5, Q},
|
||||
{F5, E}, {E5, E}, {F5, E}, {D5, E*2}, {C5, E}, {B4, E}, {C5, E+W},
|
||||
{END}
|
||||
};
|
||||
|
||||
void printHelp() {
|
||||
printf("*Commands*\n\r");
|
||||
printf("'rmw {hex address}' - Reads mem at a given address\n\r");
|
||||
printf("'wmw {hex address} {value}' - Writes the given value as a word to the given address\n\r");
|
||||
printf("'dm {hex address} {length}' - Dumps the memory at a given address. Defaults to 16 B if no "
|
||||
"length is given\n\r");
|
||||
printf("'ps {song choice}' - Plays a song with the given selection\n\r");
|
||||
printf("'songs' - Prints info about each song selection\n\r");
|
||||
printf("*Commands*\n\r");
|
||||
printf("'rmw {hex address}' - Reads mem at a given address\n\r");
|
||||
printf("'wmw {hex address} {value}' - Writes the given value as a word to the given address\n\r");
|
||||
printf("'dm {hex address} {length}' - Dumps the memory at a given address. Defaults to 16 B if no "
|
||||
"length is given\n\r");
|
||||
printf("'ps {song choice}' - Plays a song with the given selection\n\r");
|
||||
printf("'songs' - Prints info about each song selection\n\r");
|
||||
}
|
||||
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("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\n\r");
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
init_usart2(57600, F_CPU);
|
||||
piezo_init();
|
||||
led_init();
|
||||
init_usart2(57600, F_CPU);
|
||||
piezo_init();
|
||||
led_init();
|
||||
char line[50];
|
||||
char command[10];
|
||||
int address;
|
||||
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);
|
||||
switch(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");
|
||||
play_song(songIM);
|
||||
play_song(songIM);
|
||||
break;
|
||||
case 2:
|
||||
printf("Playing Metropolis Theme\n\r");
|
||||
play_song(songMT);
|
||||
break;
|
||||
case 3:
|
||||
case 3:
|
||||
printf("Playing Flower Garden\n\r");
|
||||
play_song(songFG);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
printf("Invalid song selection\n\r");
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
printf("Invalid input, type 'help' for instructions\n\r");
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user