From a7303fe5f92b9fb7476b702ab4482daca74cbe31 Mon Sep 17 00:00:00 2001 From: tbarnes98 Date: Fri, 4 Feb 2022 10:14:45 -0600 Subject: [PATCH] work --- .gitignore | 3 +- .vscode/c_cpp_properties.json | 24 ------- .vscode/settings.json | 5 -- labW5barnestr/Src/piezoSpeaker.c | 2 +- labW6barnestr/Inc/piezoSpeaker.h | 100 +++++++++++++++++++++++---- labW6barnestr/Src/main.c | 115 ++++++++++++++++++++----------- labW6barnestr/Src/piezoSpeaker.c | 73 +++++++++++++++----- 7 files changed, 221 insertions(+), 101 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index eeedd05..fac2adc 100644 --- a/.gitignore +++ b/.gitignore @@ -57,4 +57,5 @@ dkms.conf /.metadata/ # Other -*/.settings \ No newline at end of file +*/.settings +/.vscode \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 252f167..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -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 -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 83a7064..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "math.h": "c" - } -} \ No newline at end of file diff --git a/labW5barnestr/Src/piezoSpeaker.c b/labW5barnestr/Src/piezoSpeaker.c index 166dd64..cdbb0ae 100644 --- a/labW5barnestr/Src/piezoSpeaker.c +++ b/labW5barnestr/Src/piezoSpeaker.c @@ -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 * diff --git a/labW6barnestr/Inc/piezoSpeaker.h b/labW6barnestr/Inc/piezoSpeaker.h index 25b335b..6a436f5 100644 --- a/labW6barnestr/Inc/piezoSpeaker.h +++ b/labW6barnestr/Inc/piezoSpeaker.h @@ -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 diff --git a/labW6barnestr/Src/main.c b/labW6barnestr/Src/main.c index 45711cc..906d55a 100644 --- a/labW6barnestr/Src/main.c +++ b/labW6barnestr/Src/main.c @@ -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"); } diff --git a/labW6barnestr/Src/piezoSpeaker.c b/labW6barnestr/Src/piezoSpeaker.c index 166dd64..165adbb 100644 --- a/labW6barnestr/Src/piezoSpeaker.c +++ b/labW6barnestr/Src/piezoSpeaker.c @@ -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<