project changes

This commit is contained in:
2022-02-20 19:39:36 -06:00
parent debbd37501
commit 76f65fd1e9
5 changed files with 69 additions and 39 deletions

11
.project Normal file
View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>CE2812-Workspace</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
</buildSpec>
<natures>
</natures>
</projectDescription>

View File

@@ -41,7 +41,8 @@ typedef struct {
// GPIOB // GPIOB
#define GPIOBEN 1 #define GPIOBEN 1
#define AFRL_TIM3_CH1_EN 17 #define PB4_AF_V 0b10
#define PB4_AF_P 9
typedef struct { typedef struct {
uint32_t MODER; uint32_t MODER;
@@ -56,41 +57,44 @@ typedef struct {
uint32_t AFRH; uint32_t AFRH;
} GPIO; } GPIO;
// Timer 3
typedef struct {
uint32_t CR1; // Control Register 1
uint32_t CR2; // Control Register 2
uint32_t SMCR; // Slave Mode Control Register
uint32_t DIER; // DMA/Interrupt Enable Register
uint32_t SR; // Status Register
uint32_t EGR; // Event Generation Register
uint32_t CCMR1; // Capture/Compare Mode Register 1
uint32_t CCMR2; // Capture/Compare Mode Register 2
uint32_t CCER; // Capture/Compare Enable Register
uint32_t CNT; // Counter
uint32_t PSC; // Prescaler
uint32_t ARR; // Auto-Reload Register
uint32_t _RESERVED_1_;
uint32_t CCR1; // Capture/Compare Register 1
uint32_t CCR2; // Capture/Compare Register 2
uint32_t CCR3; // Capture/Compare Register 3
uint32_t CCR4; // Capture/Compare Register 4
uint32_t _RESERVED_2_;
uint32_t DCR; // DMA Control Register
uint32_t DMAR; // DMA Address for Full Transfer
uint32_t TIM2_OR; // TIM2 Option Register
uint32_t TIM5_OR; // TIM5 Option Register
} TIM;
// Timers 3 & 4
#define TIM3_EN 1 #define TIM3_EN 1
#define TIM4_EN 2
#define AFRL_TIM3_CH1_EN 17
#define OC1PE 3 #define OC1PE 3
#define OC1M_PWM2 0b1110000 #define OC1M_PWM2 0b1110000
#define CCER_CC1E 1 #define CCER_CC1E 1
#define EGR_UG 1 #define EGR_UG 1
#define CR_ARPE_EN 7 #define CR_ARPE_EN 7
#define CR_CEN 1 #define CR_CEN 1
typedef struct { #define DIER_TIE 6
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 #define pitchDivisor 1000000
// SYSCFG // SYSCFG
@@ -125,7 +129,7 @@ extern Note songIM[];
extern Note songMT[]; extern Note songMT[];
extern Note songFG[]; extern Note songFG[];
// Initializes the piezo speaker to be used with timer 3 // Initializes the piezo speaker to be used with timer 3. Also initializes timer 4 for note duration interrupt
void piezo_init(); void piezo_init();
// Plays a given note at a certain frequency for a certain duration // Plays a given note at a certain frequency for a certain duration

View File

@@ -16,7 +16,10 @@
volatile RCC* const rcc = (RCC*) 0x40023800; volatile RCC* const rcc = (RCC*) 0x40023800;
volatile GPIO* const gpiob = (GPIO*) 0x40020400; volatile GPIO* const gpiob = (GPIO*) 0x40020400;
// PWM Timer
volatile TIM* const tim3 = (TIM*) 0x40000400; volatile TIM* const tim3 = (TIM*) 0x40000400;
// Interrupt Timer for Duration
volatile TIM* const tim4 = (TIM*) 0x40000800;
volatile SYSCFG* const syscfg = (SYSCFG*) 0x40013800; volatile SYSCFG* const syscfg = (SYSCFG*) 0x40013800;
volatile EXTI* const exti4 = (EXTI*) 0x40013C00; volatile EXTI* const exti4 = (EXTI*) 0x40013C00;
@@ -64,11 +67,13 @@ Note songFG[77] = {
{END} {END}
}; };
int volumeDivisor = 10;
void piezo_init(){ void piezo_init(){
// GPIOB/Timer3 enable in RCC // GPIOB/Timer3/Timer4 enable in RCC
(*rcc).AHB1ENR |= (1<<GPIOBEN); (*rcc).AHB1ENR |= (1<<GPIOBEN);
(*rcc).APB1ENR |= (1<<TIM3_EN); (*rcc).APB1ENR |= (1<<TIM3_EN);
(*rcc).APB1ENR |= (1<<TIM4_EN);
// Set to "alternate function" mode // 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);
@@ -82,6 +87,9 @@ void piezo_init(){
// Enable Preload // Enable Preload
(*tim3).CR1 |= (1<<CR_ARPE_EN); (*tim3).CR1 |= (1<<CR_ARPE_EN);
// Configure Timer 4 for Interrupt
(*tim4).DIER |= (1<<DIER_TIE);
} }
void play_note(Note noteToPlay) { void play_note(Note noteToPlay) {
@@ -90,7 +98,7 @@ void play_note(Note noteToPlay) {
(*tim3).ARR = (pitchDivisor)/(noteToPlay.noteFrequency); (*tim3).ARR = (pitchDivisor)/(noteToPlay.noteFrequency);
// Volume (Smaller dividend = louder sound) // Volume (Smaller dividend = louder sound)
unsigned int freq = (noteToPlay.noteFrequency/10); unsigned int freq = (noteToPlay.noteFrequency/volumeDivisor);
// Clear CCR // Clear CCR
(*tim3).CCR1 = ((*tim3).CCR1&~(0xFFFF)); (*tim3).CCR1 = ((*tim3).CCR1&~(0xFFFF));
@@ -117,31 +125,38 @@ void play_song(Note *songToPlay){
} }
} }
void TIM3_IRQHandler(void) { void TIM4_IRQHandler(void) {
//temp //temp
tim4->SR = 0;
if(PLAY) { if(PLAY) {
(*tim3).PSC = 15; (*tim3).PSC = 15;
// Pitch divisor to scale with timer // Pitch divisor to scale with timer
//current note frequency global variable //current note frequency global variable
//(*tim3).ARR = (pitchDivisor)/(noteToPlay.noteFrequency); (*tim3).ARR = (pitchDivisor)/(currentSong[currentNoteIndex].noteFrequency);
// Volume (Smaller dividend = louder sound) // Volume (Smaller dividend = louder sound)
//current note frequency global variable //current note frequency global variable
//unsigned int freq = (noteToPlay.noteFrequency/10); unsigned int freq = (currentSong[currentNoteIndex].noteFrequency/volumeDivisor);
// Clear CCR // Clear CCR
(*tim3).CCR1 = ((*tim3).CCR1&~(0xFFFF)); (*tim3).CCR1 = ((*tim3).CCR1&~(0xFFFF));
//(*tim3).CCR1 = freq; (*tim3).CCR1 = freq;
// Set EGR // Set EGR
(*tim3).EGR |= EGR_UG; (*tim3).EGR |= EGR_UG;
// Set timer 4 count to duration of note
(*tim4).CNT = (currentSong[currentNoteIndex].noteDuration);
(*tim4).EGR |= EGR_UG;
// Start timer 4
(*tim4).CR1 |= 1;
// Playing note // Playing note
// Enables timer // Enables timer
(*tim3).CR1 |= 1; (*tim3).CR1 |= 1;
// Delay for duration of note // Delay for duration of note
// This is gonna have to change
//delay_1ms(noteToPlay.noteDuration); //delay_1ms(noteToPlay.noteDuration);
// Load timer with value of note duration // Load timer with value of note duration
//currentNote = currentSong[currentNoteIndex]; //currentNote = currentSong[currentNoteIndex];
} else { } else {
@@ -150,6 +165,7 @@ void TIM3_IRQHandler(void) {
} }
//currentNote = currentSong //currentNote = currentSong
} }
currentNoteIndex++;
} }
void play_song_br(Note *songToPlay) { void play_song_br(Note *songToPlay) {

View File

@@ -1 +0,0 @@
/Debug/

View File

@@ -73,4 +73,4 @@ void PendSV_Handler(void) {
stack_pointer = tasks[current_task].stack_pointer; stack_pointer = tasks[current_task].stack_pointer;
asm volatile("pop {r4-r11,lr}\n\t" asm volatile("pop {r4-r11,lr}\n\t"
"bx lr"); "bx lr");
} }