add projects

This commit is contained in:
2022-01-21 14:53:11 -06:00
parent 39ab045662
commit ca7c76e436
111 changed files with 37834 additions and 0 deletions

28
labW5barnestr/Src/delay.c Normal file
View File

@@ -0,0 +1,28 @@
/*
* delay.c
*
* Created on: Dec 10, 2021
* Author: Trevor Barnes
*/
#include <inttypes.h>
#include "delay.h" //include declaration header file
void delay_1ms(uint32_t n){
// 1ms = 16,000 ticks
for (int i = n ; i > 0 ; i--) {
// Clear value register
*STK_VAL = 0x0000;
// Store 16,000 in STK_LOAD
*STK_LOAD = 16000;
// Enable clock, no prescaler, no interrupt
*STK_CTRL |= CLKSOURCE;
*STK_CTRL |= EN;
// Loop n times: Wait for countflag high
int flag;
do {
flag = ((*STK_CTRL & (1<<16))>>16);
} while (flag != 1);
}
}

147
labW5barnestr/Src/led.c Normal file
View File

@@ -0,0 +1,147 @@
/*
* led.c
*
* Created on: Dec 10, 2021
* Author: Trevor Barnes
*/
#include "led.h"
#include "delay.h"
#include <inttypes.h>
#include <stdio.h>
int ledSpeed = 5;
void led_init(){
// Initialize corresponding RCC and GPIO registers
*RCC_AHB1ENR |= (1<<GPIOBEN);
*GPIOB_MODER &= ~(0x3FFF<<10);
*GPIOB_MODER |= (0x555<<10);
*GPIOB_MODER &= ~(0xFF<<24);
*GPIOB_MODER |= (0x55<<24);
}
void led_allOn(){
// Set all LED Bits
*GPIOB_ODR |= ALL_LEDS;
}
void led_allOff(){
// Reset all LED bits
*GPIOB_ODR &= ~(ALL_LEDS);
}
void led_on(uint8_t ledIndex){
// Set individual LED based on passed in index
switch (ledIndex) {
case 0:
*GPIOB_BSRR = (1<<5);
break;
case 1:
*GPIOB_BSRR = (1<<6);
break;
case 2:
*GPIOB_BSRR = (1<<7);
break;
case 3:
*GPIOB_BSRR = (1<<8);
break;
case 4:
*GPIOB_BSRR = (1<<9);
break;
case 5:
*GPIOB_BSRR = (1<<10);
break;
case 6:
*GPIOB_BSRR = (1<<12);
break;
case 7:
*GPIOB_BSRR = (1<<13);
break;
case 8:
*GPIOB_BSRR = (1<<14);
break;
case 9:
*GPIOB_BSRR = (1<<15);
break;
default:
printf("LED index out of range\n\r");
break;
}
}
void led_off(uint8_t ledIndex){
// Reset individual LED based on passed in index
if (ledIndex < 6) {
*GPIOB_BSRR = (1<<(21+ledIndex));
} else if (ledIndex >= 6) {
// Add pin offset to index
*GPIOB_BSRR = (1<<(22+ledIndex));
} else {
printf("LED index out of range\n\r");
}
}
void led_scan(){
led_allOff();
// Right to left each LED
for (int i = 0; i <= 9 ; i++) {
// Scaled Delay
delay_1ms(50+(ledSpeed*50));
if(i != 0){
led_off(i-1);
}
led_on(i);
}
// Left to right each LED
for (int i = 9; i >= 0; i--) {
if(i != 9){
led_off(i+1);
}
led_on(i);
// Scaled Delay
delay_1ms(50+(ledSpeed*50));
}
led_off(0);
}
void led_flash(){
// Flash LED on and off 10 times at a speed between 0-1 seconds
for (int i = 0; i < 10; i++) {
led_allOn();
delay_1ms(100+(ledSpeed*100));
led_allOff();
delay_1ms(100+(ledSpeed*100));
}
}
void led_setSpeed(uint8_t speed){
ledSpeed = speed;
}
void led_incSpeed(){
if (ledSpeed == 0){
printf("Speed too fast\n\r");
} else {
ledSpeed--;
}
}
void led_decSpeed(){
if (ledSpeed == 9){
printf("Speed too slow\n\r");
} else {
ledSpeed++;
}
}
uint8_t getCurrentSpeed()
{
return ledSpeed;
}

138
labW5barnestr/Src/main.c Normal file
View File

@@ -0,0 +1,138 @@
/*
* main.c
*
* Created on: January 12, 2022
* Author: Trevor Barnes
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "uart_driver.h"
#include "memory.h"
#include "delay.h"
#include "piezoSpeaker.h"
#define F_CPU 16000000UL
// Imperial March
Note t1n1 ={A3, Q}, t1n2 ={r, Q}, t1n3 ={A3, Q}, t1n4 ={r, Q}, t1n5 ={A3, Q}, t1n6 ={r, Q},
t1n7 ={F3,E+S}, t1n9 ={r, E+S}, t1n10 ={C4, S}, t1n11 ={r, S}, t1n12 ={A3, Q}, t1n13 ={r, Q},
t1n14 ={F3,E+S}, t1n15 ={r, E+S}, t1n16 ={C4, S}, t1n17 ={r, S}, t1n18 ={A3, H}, t1n19 ={r, H},
t1n20 ={E4, Q}, t1n21 ={r, Q}, t1n22 ={E4, Q}, t1n23 ={r, Q}, t1n24 ={E4, Q}, t1n25 ={r, Q},
t1n26 ={F4,E+S}, t1n27 ={r, E+S}, t1n28 ={C4, S}, t1n29 ={r, S}, t1n30 ={Ab3, Q}, t1n31 ={r, Q},
t1n32 ={F3,E+S}, t1n33 ={r, E+S}, t1n34 ={C4, S}, t1n35 ={r, S}, t1n36 ={A3, H}, t1n37 ={r, H},
t1n38 ={A4, Q}, t1n39 ={r, Q}, t1n40 ={A3, E+S}, t1n41 ={r, E+S}, t1n42 ={A3, S}, t1n43 ={r, S},
t1n44 ={A4, Q}, t1n45 ={r, Q}, t1n46 ={Ab4,E+S}, t1n47 ={r, E+S}, t1n48 ={G4, S}, t1n49 ={r, S},
t1n50 ={Gb4, Q}, t1n51 ={r, S}, t1n52 ={E4, S}, t1n53 ={r, S}, t1n54 ={F4, E}, t1n55 ={r, E},
t1n56 ={r, E}, t1n57 ={Bb3, E}, t1n58 ={r, E}, t1n59 ={Eb4, Q}, t1n60 ={r, Q}, t1n61 ={D4,E+S},
t1n62 ={r, E+S}, t1n63 ={Db4, S}, t1n137={r, H}, t1n64 ={C4, S}, t1n65 ={r, S}, t1n66 ={B3, S},
t1n67 ={r, S}, t1n68 ={C4, E}, t1n69 ={r, E}, t1n70 ={r, E}, t1n71 ={F3, E}, t1n72 ={r, E},
t1n73 ={Ab3, Q}, t1n74 ={r, Q}, t1n75 ={F3, E+S}, t1n76 ={r, E+S}, t1n77 ={A3, S}, t1n78 ={r, S},
t1n79 ={C4, Q}, t1n80 ={r, Q}, t1n81 ={A3, E+S}, t1n82 ={r, E+S}, t1n83 ={C4, S}, t1n84 ={r, S},
t1n85 ={E4, H}, t1n86 ={r, H}, t1n87 ={A4, Q}, t1n88 ={r, Q}, t1n89 ={A3, E+S}, t1n90 ={r, E+S},
t1n91 ={A3, S}, t1n92 ={r, S}, t1n93 ={A4, Q}, t1n94 ={r, S}, t1n95 ={Ab4,E+S}, t1n96 ={r, E+S},
t1n97 ={G4, S}, t1n98 ={r, S}, t1n99 ={Gb4, S}, t1n100={r, S}, t1n101={E4, S}, t1n102={r, S},
t1n103={F4, E}, t1n104={r, E}, t1n105={r, E}, t1n106={Bb3, E}, t1n107={r, E}, t1n108={Eb4, Q},
t1n109={r, Q}, t1n110={D4,E+S}, t1n111={r, E+S}, t1n112={Db4, S}, t1n113={r, S}, t1n114={C4, S},
t1n115={r, S}, t1n116={B3, S}, t1n117={r, S}, t1n118={C4, E}, t1n119={r, E}, t1n120={r, E},
t1n121={F3, E}, t1n122={r, E}, t1n123={Ab3, Q}, t1n124={r, Q}, t1n125={F3, E+S}, t1n126={r, E+S},
t1n127={C4, S}, t1n128={r, S}, t1n129={A3, Q}, t1n130={r, Q}, t1n131={F3, E+S}, t1n132={r, E+S},
t1n133={C4, S}, t1n134={r, S}, t1n135={A3, H}, t1n136={r, H};
// Super Mario "Flagpole Fanfare"
Note t2n1 ={G3, S/3}, t2n46={Ab3,S/3}, t2n2 ={A3, S/3}, t2n47={Bb3,S/3}, t2n3 ={B3,S/3},
t2n4 ={C4, S/3}, t2n48={Db4,S/3}, t2n5 ={D4, S/3}, t2n49={Eb4,S/3}, t2n6 ={E4,S/3},
t2n7 ={F4, S/3}, t2n50={Gb4,S/3}, t2n8 ={G4, S/3}, t2n51={Ab4,S/3}, t2n9 ={A4,S/3},
t2n52={Bb4,S/3}, t2n10={B4, S/3}, t2n11={C5, S/3}, t2n53={Db5,S/3}, t2n12={D5,S/3},
t2n54={Eb5,S/3}, t2n13={E5, S/3}, t2n14={F5, S/3}, t2n55={Gb5,S/3}, t2n15={G5,S/3},
t2n56={Ab5,S/3}, t2n16={A5, S/3}, t2n57={Bb5,S/3}, t2n17={B5, S/3}, t2n18={C6,S/3},
t2n58={Db6,S/3}, t2n19={D6, S/3}, t2n59={Eb6,S/3}, t2n20={E6, S/3}, t2n21={F6,S/3},
t2n60={Eb6,S/3}, t2n22={G6, S/3}, t2n23={r, 7*S}, t2n24={G3, Q}, t2n25={C4, Q},
t2n26={E4, Q}, t2n27={G4, Q}, t2n28={C5, Q}, t2n29={E5, Q}, t2n30={G5, H},
t2n31={E5, H}, t2n32={Ab3, Q}, t2n33={C4, Q}, t2n34={Eb4, Q}, t2n35={Ab4, Q},
t2n36={C5, Q}, t2n37={Eb5, Q}, t2n38={Ab6, H}, t2n39={Eb5, H}, t2n40={Bb3, Q},
t2n41={D4, Q}, t2n42={F4, Q}, t2n43={Bb4, Q}, t2n44={D5, Q}, t2n45={F5, Q},
t2n61={Bb5, W}, t2n62={B5, Q}, t2n63={B5, Q}, t2n64={B5, Q}, t2n65={C6, W};
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");
}
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");
}
int main(void) {
init_usart2(57600, F_CPU);
piezo_init();
led_init();
// Inititialize Imperial March Note Array
Note song1[137]={t1n1, t1n2, t1n3, t1n4, t1n5, t1n6, t1n7, t1n9, t1n10, t1n11, t1n12, t1n13, t1n14, t1n15,
t1n16, t1n17, t1n18, t1n19, t1n20, t1n21, t1n22, t1n23, t1n24, t1n25, t1n26, t1n27, t1n28, t1n29,
t1n30, t1n31, t1n32, t1n33, t1n34, t1n35, t1n36, t1n37, t1n38, t1n39, t1n40, t1n41, t1n42, t1n43,
t1n44, t1n45, t1n46, t1n47, t1n48, t1n49, t1n50, t1n51, t1n52, t1n53, t1n54, t1n55, t1n56, t1n57,
t1n58, t1n59, t1n60, t1n61, t1n62, t1n63, t1n137,t1n64, t1n65, t1n66, t1n67, t1n68, t1n69, t1n70,
t1n71, t1n72, t1n73, t1n74, t1n75, t1n76, t1n77, t1n78, t1n79, t1n80, t1n81, t1n82, t1n83, t1n84,
t1n85, t1n86, t1n87, t1n88, t1n89, t1n90, t1n91, t1n92, t1n93, t1n94, t1n95, t1n96, t1n97, t1n98,
t1n99, t1n100,t1n101,t1n102,t1n103,t1n104,t1n105,t1n106,t1n107,t1n108,t1n109,t1n110,t1n111,t1n112,
t1n113,t1n114,t1n115,t1n116,t1n117,t1n118,t1n119,t1n120,t1n121,t1n122,t1n123,t1n124,t1n125,t1n126,
t1n127,t1n128,t1n129,t1n130,t1n131,t1n132,t1n133,t1n134,t1n135,t1n136};
int song1Size = sizeof(song1)/sizeof(song1[0]);
// Initialize Super Mario Note Array
Note song2[65]={t2n1, t2n46,t2n2, t2n47,t2n3, t2n4, t2n48,t2n5, t2n49,t2n6, t2n7, t2n50,t2n8, t2n51,t2n9, t2n52,
t2n10,t2n11,t2n53,t2n12,t2n54,t2n13,t2n14,t2n55,t2n15,t2n56,t2n16,t2n57,t2n17,t2n18,t2n58,t2n19,t2n59,
t2n20,t2n21,t2n60,t2n22,t2n23,t2n24,t2n25,t2n26,t2n27,t2n28,t2n29,t2n30,t2n31,t2n32,t2n33,t2n34,t2n35,
t2n36,t2n37,t2n38,t2n39,t2n40,t2n41,t2n42,t2n43,t2n44,t2n45,t2n61,t2n62,t2n63,t2n64,t2n65};
int song2Size = sizeof(song2)/sizeof(song2[0]);
char line[50];
char command[10];
int address;
int data;
static int length;
static int songSelection;
for(;;) {
// Get command from user
fgets(line, 100, stdin);
// Parse only the command for strcmp
sscanf(line, "%s", command);
if (!strcmp(command, "help")) {
printHelp();
} else if (!strcmp(command, "songs")) {
songInfo();
} else if (!strcmp(command, "rmw")) {
sscanf(line, "%s %X", command, &address);
readMem(address);
} else if (!strcmp(command, "wmw")) {
sscanf(line, "%s %X %u", command, &address, &data);
writeMem(address, data);
} else if (!strcmp(command, "dm")) {
sscanf(line, "%s %X %u", command, &address, &length);
dumpMem(address, length);
} else if (!strcmp(command, "ps")) {
sscanf(line, "%s %u", command, &songSelection);
switch(songSelection) {
case 1:
printf("Playing Imperial March\n\r");
play_song(&song1[0], song1Size);
break;
case 2:
printf("Playing Super Mario Bros Flagpole Fanfare\n\r");
play_song(&song2[0], song2Size);
break;
default:
break;
}
} else {
printf("Invalid input, type 'help' for instructions\n\r");
}
}
}

View File

@@ -0,0 +1,66 @@
/**
* @file memory.c
* @author Trevor Barnes
* @brief
* @version 0.1
* @date 2022-01-19
*
* @copyright Copyright (c) 2022
*
*/
#include <stdio.h>
#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);
return;
}
void writeMem(uint32_t addr, uint32_t data) {
// Assign and casts a new int pointer the value of addr
uint32_t * memPtr = (uint32_t *)addr;
// Write data
*memPtr = data;
// Confirmation printout showing the new value and address
printf("Value written at %#08x: %u \n\r", addr, data);
return;
}
void dumpMem(uint32_t addr, int length) {
// Set length to default value if length is negative
// (No limit or protection for large, overflow values yet)
if(length <= 0) {
length = 16;
printf("Length set to default! (16)\n\r");
}
// Assign and casts a new int pointer the value of addr
uint8_t * memPtr = (uint8_t *)addr;
// Loop that executes each read and print operation
for(int i=0 ; i < length ; i++) {
// Print newline and memory location every 16 bytes
if((i % 16) == 0) {
printf("\n\r%p:", memPtr);
}
// Print each byte
printf(" %02X", *memPtr);
// Iterate pointer to next byte
memPtr++;
}
printf("\n\r");
return;
}

View File

@@ -0,0 +1,76 @@
/**
* @file piezoSpeaker.c
* @author Trevor Barnes
* @brief
* @version 0.1
* @date 2022-01-19
*
* @copyright Copyright (c) 2022
*
*/
#include <stdio.h>
#include <inttypes.h>
#include "piezoSpeaker.h"
#include "delay.h"
void piezo_init(){
//enable GPIOB and Timer 3 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
*GPIOB_MODER = (*GPIOB_MODER&~(0b11<<8)) | (PB4_AF_V<<8);
//set alternate function low register to TIM3
*GPIOB_AFRL |= (1<<AFRL_TIM3_CH1_EN);
//Configure capture/compare mode register configuration
//to enable preload and set to pwm
*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
*TIM3_CR1 |= (1<<CR_ARPE_EN);
}
void play_note(double frequency, double duration){
*TIM3_PSC = 15;
//Divisor controls pitch
*TIM3_ARR = mil/frequency;
//Loudness (Smaller dividend = louder sound)
double freq = frequency/10;
//clear ccr1
*TIM3_CCR1 = (*TIM3_CCR1&~(0xFFFF));
*TIM3_CCR1 = freq;
//set EGR (accept only a byte of info so steps)
*TIM3_EGR |= EGR_UG;
//~~~Plays the notes
//Enables enable bit control register
*TIM3_CR1 |= 1;
//delay that leaves the speaker on for desired amount of time
delay_1ms(duration);
//Disables enable bit
*TIM3_CR1 &= ~1;
}
void play_song(Note *song, int size){
for(int i = 0; i < size; i++){
play_note(song[i].freq, song[i].duration);
}
}

View File

@@ -0,0 +1,92 @@
/*
* uart_driver.c
*
* Created on: Nov 8, 2016
* Author: barnekow
*/
#include "uart_driver.h"
#include <inttypes.h>
#include <stdio.h>
// These will override _read and _write in syscalls.c, which are
// prototyped as weak
int _read(int file, char *ptr, int len)
{
int DataIdx;
// Modified the for loop in order to get the correct behavior for fgets
int byteCnt = 0;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
//*ptr++ = __io_getchar();
byteCnt++;
//*ptr++ = usart2_getch();
*ptr = usart2_getch();
if(*ptr == '\n') break;
ptr++;
}
//return len;
return byteCnt; // Return byte count
}
int _write(int file, char *ptr, int len)
{
int DataIdx;
for (DataIdx = 0; DataIdx < len; DataIdx++)
{
usart2_putch(*ptr++);
}
return len;
}
char usart2_getch(){
char c;
while((*(USART_SR)&(1<<RXNE)) != (1<<RXNE));
c = ((char) *USART_DR); // Read character from usart
usart2_putch(c); // Echo back
if (c == '\r'){ // If character is CR
usart2_putch('\n'); // send it
c = '\n'; // Return LF. fgets is terminated by LF
}
return c;
}
void usart2_putch(char c){
while((*(USART_SR)&(1<<TXE)) != (1<<TXE));
*(USART_DR) = c;
}
void init_usart2(uint32_t baud, uint32_t sysclk){
// Enable clocks for GPIOA and USART2
*(RCC_AHB1ENR) |= (1<<GPIOAEN);
*(RCC_APB1ENR) |= (1<<USART2EN);
// Function 7 of PORTA pins is USART
*(GPIOA_AFRL) &= (0xFFFF00FF); // Clear the bits associated with PA3 and PA2
*(GPIOA_AFRL) |= (0b01110111<<8); // Choose function 7 for both PA3 and PA2
*(GPIOA_MODER) &= (0xFFFFFF0F); // Clear mode bits for PA3 and PA2
*(GPIOA_MODER) |= (0b1010<<4); // Both PA3 and PA2 in alt function mode
// Set up USART2
//USART2_init(); //8n1 no flow control
// over8 = 0..oversample by 16
// M = 0..1 start bit, data size is 8, 1 stop bit
// PCE= 0..Parity check not enabled
// no interrupts... using polling
*(USART_CR1) = (1<<UE)|(1<<TE)|(1<<RE); // Enable UART, Tx and Rx
*(USART_CR2) = 0; // This is the default, but do it anyway
*(USART_CR3) = 0; // This is the default, but do it anyway
*(USART_BRR) = sysclk/baud;
/* I'm not sure if this is needed for standard IO*/
//setvbuf(stderr, NULL, _IONBF, 0);
//setvbuf(stdin, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
}