Merge branch 'main' of github.com:tbarnes98/CE2812-Workspace

This commit is contained in:
2022-02-03 18:11:43 -06:00
8 changed files with 231 additions and 237 deletions

View File

@@ -1,7 +1,7 @@
/**
* @file memory.h
* @author Trevor Barnes
* @brief
* @brief Provides headers for reading, writing, and dumping memory
* @version 0.1
* @date 2022-01-19
*
@@ -16,8 +16,6 @@
#include <inttypes.h>
#include "uart_driver.h"
void initMemConsole();
/**
* Reads and prints the memory value at address provided: "addr"
*/

View File

@@ -1,7 +1,7 @@
/**
* @file memory.c
* @author Trevor Barnes
* @brief
* @brief Contains functionality for memory manipulation through the console
* @version 0.1
* @date 2022-01-19
*
@@ -13,13 +13,6 @@
#include <inttypes.h>
#include "memory.h"
#define F_CPU 16000000UL
void initMemConsole() {
init_usart2(57600, F_CPU);
printf("Memory Console Initialized! Type 'help' for info.\n\r");
}
void readMem(uint32_t addr) {
// Assign and casts a new int pointer the value of addr
uint32_t * memPtr = (uint32_t *)addr;

View File

@@ -1,7 +1,7 @@
/**
* @file memory.h
* @author Trevor Barnes
* @brief
* @brief Provides headers for reading, writing, and dumping memory
* @version 0.1
* @date 2022-01-19
*
@@ -16,8 +16,6 @@
#include <inttypes.h>
#include "uart_driver.h"
void initMemConsole();
/**
* Reads and prints the memory value at address provided: "addr"
*/

View File

@@ -1,9 +1,11 @@
/**
* @file piezoSpeaker.h
* @author Trevor Barnes
* @brief
* @brief Header file for the piezo speaker's C file that provides all of the necessary timer, RCC,
* and GPIOB values for working with the respective components. Note frequency values and durations
* are also defined.
* @version 0.1
* @date 2022-01-19
* @date 2022-01-12
*
* @copyright Copyright (c) 2022
*
@@ -41,131 +43,142 @@
#define PB4_AF_V 0b10
#define PB4_AF_P 9
#define mil 1000000
#define pitchDivisor 1000000
typedef struct{
double noteFrequency;
double noteDuration;
unsigned int noteFrequency;
unsigned int noteDuration;
} Note;
// Initializes the piezo speaker to be used with timer 3
void piezo_init();
int play_note(Note noteToPlay);
// void play_note(double playFrequency, double playDuration);
// Plays a given note at a certain frequency for a certain duration
void play_note(Note noteToPlay);
void play_song(Note *song);
// Iterates through an array of note structs and ends at the termination value "END"
void play_song(Note *songToPlay);
// Frequency Value Signifying a rest
#define r 1
// Note Frequency Symbols (Rounded to nearest integer value)
#define C0 16
#define Db0 17
#define D0 18
#define Eb0 19
#define E0 21
#define F0 22
#define Gb0 23
#define G0 25
#define Ab0 26
#define A0 28
#define Bb0 29
#define B0 31
#define C1 33
#define Db1 35
#define D1 37
#define Eb1 39
#define E1 41
#define F1 44
#define Gb1 46
#define G1 49
#define Ab1 52
#define A1 55
#define Bb1 58
#define B1 62
#define C2 65
#define Db2 69
#define D2 73
#define Eb2 78
#define E2 82
#define F2 87
#define Gb2 93
#define G2 98
#define Ab2 104
#define A2 110
#define Bb2 117
#define B2 123
#define C3 131
#define Db3 139
#define D3 147
#define Eb3 156
#define E3 165
#define F3 175
#define Gb3 185
#define G3 196
#define Ab3 208
#define A3 220
#define Bb3 233
#define B3 247
#define C4 262
#define Db4 277
#define D4 294
#define Eb4 311
#define E4 330
#define F4 349
#define Gb4 370
#define G4 392
#define Ab4 415
#define A4 440
#define Bb4 466
#define B4 494
#define C5 523
#define Db5 554
#define D5 587
#define Eb5 622
#define E5 659
#define F5 698
#define Gb5 740
#define G5 784
#define Ab5 831
#define A5 880
#define Bb5 932
#define B5 988
#define C6 1047
#define Db6 1109
#define D6 1175
#define Eb6 1245
#define E6 1319
#define F6 1397
#define Gb6 1480
#define G6 1568
#define Ab6 1661
#define A6 1760
#define Bb6 1865
#define B6 1976
#define C7 2093
#define Db7 2217
#define D7 2349
#define Eb7 2489
#define E7 2637
#define F7 2794
#define Gb7 2960
#define G7 3136
#define Ab7 3322
#define A7 3520
#define Bb7 3730
#define B7 3951
#define C8 4186
#define Db8 4435
#define D8 4699
#define Eb8 4978
// Value that indicates the end of a song
#define END 0
// Note Frequency Symbols
// Rest technically = 0
#define r 0
#define C0 16.35
#define Db0 17.32
#define D0 18.35
#define Eb0 19.45
#define E0 20.60
#define F0 21.83
#define Gb0 23.12
#define G0 24.50
#define Ab0 25.96
#define A0 27.50
#define Bb0 29.14
#define B0 30.87
#define C1 32.70
#define Db1 34.65
#define D1 36.71
#define Eb1 38.89
#define E1 41.20
#define F1 43.65
#define Gb1 46.25
#define G1 49.00
#define Ab1 51.91
#define A1 55.00
#define Bb1 58.27
#define B1 61.74
#define C2 65.41
#define Db2 69.30
#define D2 73.42
#define Eb2 77.78
#define E2 82.41
#define F2 87.31
#define Gb2 92.50
#define G2 98.00
#define Ab2 103.83
#define A2 110.00
#define Bb2 116.54
#define B2 123.47
#define C3 130.81
#define Db3 138.59
#define D3 146.83
#define Eb3 155.56
#define E3 164.81
#define F3 174.61
#define Gb3 185.00
#define G3 196.00
#define Ab3 207.65
#define A3 220.00
#define Bb3 233.08
#define B3 246.94
#define C4 261.63
#define Db4 277.18
#define D4 293.66
#define Eb4 311.13
#define E4 329.63
#define F4 349.23
#define Gb4 369.99
#define G4 392.00
#define Ab4 415.30
#define A4 440.00
#define Bb4 466.16
#define B4 493.88
#define C5 523.25
#define Db5 554.37
#define D5 587.33
#define Eb5 622.25
#define E5 659.26
#define F5 698.46
#define Gb5 739.99
#define G5 783.99
#define Ab5 830.61
#define A5 880.00
#define Bb5 932.33
#define B5 987.77
#define C6 1046.50
#define Db6 1108.73
#define D6 1174.66
#define Eb6 1244.51
#define E6 1318.51
#define F6 1396.91
#define Gb6 1479.98
#define G6 1567.98
#define Ab6 1661.22
#define A6 1760.00
#define Bb6 1864.66
#define B6 1975.53
#define C7 2093.00
#define Db7 2217.46
#define D7 2349.32
#define Eb7 2489.02
#define E7 2637.02
#define F7 2793.83
#define Gb7 2959.96
#define G7 3135.96
#define Ab7 3322.44
#define A7 3520.01
#define Bb7 3729.31
#define B7 3951.07
#define C8 4186.01
#define Db8 4434.92
#define D8 4698.64
#define Eb8 4978.03
// Ending "termination" value indicating end of a song
#define T 5000.00
// Tempo value set at 120 beats per min
#define BPM 120
#define H 2*Q
// Quarter Note
#define Q 60000/BPM
#define E Q/2
#define S Q/4
// Half Note
#define H 2*Q
// Whole Note
#define W 4*Q
// Eighth Note
#define E Q/2
// Sixteenth Note
#define S Q/4
#endif

View File

@@ -1,8 +1,18 @@
/*
* main.c
*
* Created on: January 12, 2022
* Author: Trevor Barnes
/**
* @file main.c
* @author Trevor Barnes
* @brief Main Driver for the Week 5 "Play a Tune" Lab. This program uses a similar menu style to the previous
* lab. The user is given a new option to play 3 different hard coded songs in arrays of "Note" structs,
* provided by the piezoSpeaker.h file.
* Experience: As a former music student I enjoyed working on this lab very much, to the point that I became
* sort of a perfectionist with its implementation. Getting the timer configured and working with the piezo speaker
* was certainly the most difficult and time consuming part. Once I had that figured out though figuring
* out the best note implementation and songs to play was very enjoyable.
* @version 0.1
* @date 2022-01-19
*
* @copyright Copyright (c) 2022
*
*/
#include <stdio.h>
@@ -17,72 +27,71 @@
#define F_CPU 16000000UL
// Super Mario "Flagpole Fanfare"
Note songSMFF[66]= { {G3, S/3}, {Ab3,S/3}, {A3, S/3}, {Bb3,S/3}, {B3,S/3},
{C4, S/3}, {Db4,S/3}, {D4, S/3}, {Eb4,S/3}, {E4,S/3},
{F4, S/3}, {Gb4,S/3}, {G4, S/3}, {Ab4,S/3}, {A4,S/3},
{Bb4,S/3}, {B4, S/3}, {C5, S/3}, {Db5,S/3}, {D5,S/3},
{Eb5,S/3}, {E5, S/3}, {F5, S/3}, {Gb5,S/3}, {G5,S/3},
{Ab5,S/3}, {A5, S/3}, {Bb5,S/3}, {B5, S/3}, {C6,S/3},
{Db6,S/3}, {D6, S/3}, {Eb6,S/3}, {E6, S/3}, {F6,S/3},
{Eb6,S/3}, {G6, S/3}, {r, 7*S}, {G3, Q}, {C4, Q},
{E4, Q}, {G4, Q}, {C5, Q}, {E5, Q}, {G5, H},
{E5, H}, {Ab3, Q}, {C4, Q}, {Eb4, Q}, {Ab4, Q},
{C5, Q}, {Eb5, Q}, {Ab6, H}, {Eb5, H}, {Bb3, Q},
{D4, Q}, {F4, Q}, {Bb4, Q}, {D5, Q}, {F5, Q},
{Bb5, W}, {B5, Q}, {B5, Q}, {B5, Q}, {C6, W}, {T, Q} };
// 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},
{END}
};
// Imperial March
Note songIM[138] = { {A3, Q}, {r, Q}, {A3, Q}, {r, Q}, {A3, Q}, {r, Q},
{F3,E+S}, {r, E+S}, {C4, S}, {r, S}, {A3, Q}, {r, Q},
{F3,E+S}, {r, E+S}, {C4, S}, {r, S}, {A3, H}, {r, H},
{E4, Q}, {r, Q}, {E4, Q}, {r, Q}, {E4, Q}, {r, Q},
{F4,E+S}, {r, E+S}, {C4, S}, {r, S}, {Ab3, Q}, {r, Q},
{F3,E+S}, {r, E+S}, {C4, S}, {r, S}, {A3, H}, {r, H},
{A4, Q}, {r, Q}, {A3, E+S}, {r, E+S}, {A3, S}, {r, S},
{A4, Q}, {r, Q}, {Ab4,E+S}, {r, E+S}, {G4, S}, {r, S},
{Gb4, Q}, {r, S}, {E4, S}, {r, S}, {F4, E}, {r, E},
{r, E}, {Bb3, E}, {r, E}, {Eb4, Q}, {r, Q}, {D4,E+S},
{r, E+S}, {Db4, S}, {r, H}, {C4, S}, {r, S}, {B3, S},
{r, S}, {C4, E}, {r, E}, {r, E}, {F3, E}, {r, E},
{Ab3, Q}, {r, Q}, {F3, E+S}, {r, E+S}, {A3, S}, {r, S},
{C4, Q}, {r, Q}, {A3, E+S}, {r, E+S}, {C4, S}, {r, S},
{E4, H}, {r, H}, {A4, Q}, {r, Q}, {A3, E+S}, {r, E+S},
{A3, S}, {r, S}, {A4, Q}, {r, S}, {Ab4,E+S}, {r, E+S},
{G4, S}, {r, S}, {Gb4, S}, {r, S}, {E4, S}, {r, S},
{F4, E}, {r, E}, {r, E}, {Bb3, E}, {r, E}, {Eb4, Q},
{r, Q}, {D4,E+S}, {r, E+S}, {Db4, S}, {r, S}, {C4, S},
{r, S}, {B3, S}, {r, S}, {C4, E}, {r, E}, {r, E},
{F3, E}, {r, E}, {Ab3, Q}, {r, Q}, {F3, E+S}, {r, E+S},
{C4, S}, {r, S}, {A3, Q}, {r, Q}, {F3, E+S}, {r, E+S},
{C4, S}, {r, S}, {A3, H}, {r, H}, {T, Q} };
// Metropolis Theme - Ratchet & Clank
Note songMT[33] = {
{B5, E}, {G5, E}, {E5, E}, {G5, E}, {B5, E}, {G5, E}, {E5, E}, {B5, E},
{Bb5, E}, {F5, E}, {D5, E}, {F5, E}, {Bb5, E}, {F5, E}, {D5, E}, {Bb5, E},
{B5, E}, {G5, E}, {E5, E}, {G5, E}, {B5, E}, {G5, E}, {E5, E}, {B5, E},
{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}
};
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\n\r");
printf("Song 2: Super Mario Bros Flagpole Fanfare\n\r");
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");
}
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;
// play_note(261.63, 10000.00);
for(;;) {
// Get command from user
fgets(line, 100, stdin);
@@ -106,11 +115,15 @@ int main(void) {
switch(songSelection) {
case 1:
printf("Playing Imperial March\n\r");
play_song(&songIM[0]);
break;
play_song(songIM);
break;
case 2:
printf("Playing Super Mario Bros Flagpole Fanfare\n\r");
play_song(&songSMFF[0]);
printf("Playing Metropolis Theme\n\r");
play_song(songMT);
break;
case 3:
printf("Playing Flower Garden\n\r");
play_song(songFG);
break;
default:
break;

View File

@@ -1,7 +1,7 @@
/**
* @file memory.c
* @author Trevor Barnes
* @brief
* @brief Contains functionality for memory manipulation through the console
* @version 0.1
* @date 2022-01-19
*
@@ -13,20 +13,13 @@
#include <inttypes.h>
#include "memory.h"
#define F_CPU 16000000UL
void initMemConsole() {
init_usart2(57600, F_CPU);
printf("Memory Console Initialized! Type 'help' for info.\n\r");
}
void readMem(uint32_t addr) {
// Assign and casts a new int pointer the value of addr
uint32_t * memPtr = (uint32_t *)addr;
// Formatted print with both hex and decimal values
printf("Memory Value at %#08x\n\r"
"Hex: %#08x\n\r"
"Decimal: %d\n\r", addr, *memPtr, *memPtr);
"Decimal: %u\n\r", (int)addr, (int)*memPtr, (int)*memPtr);
return;
}
@@ -36,7 +29,7 @@ void writeMem(uint32_t addr, uint32_t data) {
// Write data
*memPtr = data;
// Confirmation printout showing the new value and address
printf("Value written at %#08x: %u \n\r", addr, data);
printf("Value written at %#08x: %u \n\r", (int)addr, (int)data);
return;
}

View File

@@ -1,9 +1,9 @@
/**
* @file piezoSpeaker.c
* @author Trevor Barnes
* @brief
* @brief Provides funtionality for initializing, playing notes, and playing songs on the piezo speaker.
* @version 0.1
* @date 2022-01-19
* @date 2022-01-12
*
* @copyright Copyright (c) 2022
*
@@ -16,66 +16,52 @@
void piezo_init(){
//enable GPIOB and Timer 3 RCC
// GPIOB/Timer3 enable in RCC
*RCC_AHB1ENR |= (1<<GPIOBEN);
*RCC_APB1ENR |= (1<<TIM3_EN);
//set GPIO B to alternate function (0b10<<9)
//clears the two bits and then set it
// Set to "alternate function" mode
*GPIOB_MODER = (*GPIOB_MODER&~(0b11<<8)) | (PB4_AF_V<<8);
//set alternate function low register to TIM3
// Set AF to low
*GPIOB_AFRL |= (1<<AFRL_TIM3_CH1_EN);
//Configure capture/compare mode register configuration
//to enable preload and set to pwm
// Set to output capture
*TIM3_CCMR1 |= OC1M_PWM2;
*TIM3_CCMR1 |= (1<<OC1PE);
//Configure CCER to enable timer 3 as output capture
*TIM3_CCER |= CCER_CC1E;
//Configure control register to enable preload
// Enable Preload
*TIM3_CR1 |= (1<<CR_ARPE_EN);
}
play_note(Note noteToPlay) {
//void play_note(double playFrequency, double playDuration) {
double freq = noteToPlay.noteFrequency;
double dur = noteToPlay.noteDuration;
void play_note(Note noteToPlay) {
*TIM3_PSC = 15;
//Divisor controls pitch
*TIM3_ARR = mil/freq;
// Pitch divisor to scale with timer
*TIM3_ARR = (pitchDivisor)/(noteToPlay.noteFrequency);
//Loudness (Smaller dividend = louder sound)
freq = freq/10;
// Volume (Smaller dividend = louder sound)
unsigned int freq = (noteToPlay.noteFrequency/10);
//clear ccr1
// Clear CCR
*TIM3_CCR1 = (*TIM3_CCR1&~(0xFFFF));
*TIM3_CCR1 = freq;
//set EGR (accept only a byte of info so steps)
// Set EGR
*TIM3_EGR |= EGR_UG;
//~~~Plays the notes
//Enables enable bit control register
// Playing note
// Enables timer
*TIM3_CR1 |= 1;
//delay that leaves the speaker on for desired amount of time
delay_1ms(dur);
//Disables enable bit
// Delay for duration of note
delay_1ms(noteToPlay.noteDuration);
// Disables timer
*TIM3_CR1 &= ~1;
}
void play_song(Note *songToPlay){
int i = 0;
double freq = songToPlay[i].noteFrequency;
while(freq != T) {
// Iterate through song and play each note till "END" value
while(songToPlay[i].noteFrequency != END) {
play_note(songToPlay[i]);
i++;
}