From 51dd44c00ca05c2bdb4b3894ff2a28c7450c51ec Mon Sep 17 00:00:00 2001 From: David Lenfesty Date: Fri, 29 Nov 2019 17:29:55 -0700 Subject: [PATCH] Added testing app --- final_project/312_final.code-workspace | 9 +- .../testing/.vscode/c_cpp_properties.json | 19 ++++ final_project/testing/.vscode/settings.json | 3 + final_project/testing/.vscode/tasks.json | 19 ++++ final_project/testing/Makefile | 52 ++++++++++ final_project/testing/comms.h | 97 +++++++++++++++++++ final_project/testing/main.c | 42 ++++++++ 7 files changed, 240 insertions(+), 1 deletion(-) create mode 100644 final_project/testing/.vscode/c_cpp_properties.json create mode 100644 final_project/testing/.vscode/settings.json create mode 100644 final_project/testing/.vscode/tasks.json create mode 100644 final_project/testing/Makefile create mode 100644 final_project/testing/comms.h create mode 100644 final_project/testing/main.c diff --git a/final_project/312_final.code-workspace b/final_project/312_final.code-workspace index 2bf40c0..c079050 100644 --- a/final_project/312_final.code-workspace +++ b/final_project/312_final.code-workspace @@ -5,12 +5,19 @@ }, { "path": "lcd_disp" + }, + { + "path": "testing" } ], "settings": { "files.associations": { "io.h": "c", - "interrupt.h": "c" + "interrupt.h": "c", + "periph.h": "c", + "comms.h": "c", + "string.h": "c", + "main.h": "c" } } } \ No newline at end of file diff --git a/final_project/testing/.vscode/c_cpp_properties.json b/final_project/testing/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..6a05b2e --- /dev/null +++ b/final_project/testing/.vscode/c_cpp_properties.json @@ -0,0 +1,19 @@ +{ + "configurations": [ + { + "name": "Linux", + "intelliSenseMode": "gcc-x64", + "compilerPath": "/usr/lib/ccache/avr-gcc", + "cStandard": "c99", + "cppStandard": "c++17", + "compilerArgs": [ + "-Wall", "-pedantic", "-mmcu=atmega328" + ], + "defines": [ + "F_CPU=16000000", + "__" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/final_project/testing/.vscode/settings.json b/final_project/testing/.vscode/settings.json new file mode 100644 index 0000000..02b62d2 --- /dev/null +++ b/final_project/testing/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "C_Cpp.intelliSenseEngine": "Tag Parser" +} \ No newline at end of file diff --git a/final_project/testing/.vscode/tasks.json b/final_project/testing/.vscode/tasks.json new file mode 100644 index 0000000..794c3b1 --- /dev/null +++ b/final_project/testing/.vscode/tasks.json @@ -0,0 +1,19 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "make", + "type": "shell", + "command": "make", + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [ + "$gcc" + ] + } + ] +} \ No newline at end of file diff --git a/final_project/testing/Makefile b/final_project/testing/Makefile new file mode 100644 index 0000000..49da8da --- /dev/null +++ b/final_project/testing/Makefile @@ -0,0 +1,52 @@ + +NAME := sd-reader +HEX := $(NAME).hex +OUT := $(NAME).out +MAP := $(NAME).map +SOURCES := $(wildcard *.c) +HEADERS := $(wildcard *.h) +OBJECTS := $(patsubst %.c,%.o,$(SOURCES)) + +MCU := atmega328 +MCU_AVRDUDE := m328 +MCU_FREQ := 8000000UL + +CC := avr-gcc +OBJCOPY := avr-objcopy +SIZE := avr-size -A +DOXYGEN := doxygen + +CFLAGS := -Werror -Wall -pedantic -mmcu=$(MCU) -std=c99 -g -Os -DF_CPU=$(MCU_FREQ) + +all: $(HEX) + +clean: + rm -f $(HEX) $(OUT) $(MAP) $(OBJECTS) + rm -rf doc/html + +flash: $(HEX) + avrdude -y -c avr910 -p $(MCU_AVRDUDE) -U flash:w:$(HEX) + +$(HEX): $(OUT) + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +$(OUT): $(OBJECTS) + $(CC) $(CFLAGS) -o $@ -Wl,-Map,$(MAP) $^ + @echo + @$(SIZE) $@ + @echo + +%.o: %.c $(HEADERS) + $(CC) $(CFLAGS) -c -o $@ $< + +%.pp: %.c + $(CC) $(CFLAGS) -E -o $@ $< + +%.ppo: %.c + $(CC) $(CFLAGS) -E $< + +doc: $(HEADERS) $(SOURCES) Doxyfile + $(DOXYGEN) Doxyfile + +.PHONY: all clean flash doc + diff --git a/final_project/testing/comms.h b/final_project/testing/comms.h new file mode 100644 index 0000000..5e36a6a --- /dev/null +++ b/final_project/testing/comms.h @@ -0,0 +1,97 @@ +#ifndef COMMS_H_ +#define COMMS_H_ + +#include +#include + +#ifdef __AVR_ATmega328P__ + #define MEGA328 +#endif +#ifdef __AVR_ATtiny2313__ + #define TINY2313 +#endif + +/** + * Our format goes like this: + * + * 1 byte command, with data after + */ + + +typedef enum { + COMMS_CMD_CLR = 0U, //! Clears LCD and all file names + COMMS_CMD_NUM, //! sends the total number of songs to choose from + COMMS_CMD_QUERY_NAME, //! Queries for the name of a given song ID + COMMS_CMD_REPLY_NAME, //! Responds with the name of the given song ID + COMMS_CMD_SELECT_FILE, //! selects a file from the given list to play, data is 1 byte for id + COMMS_CMD_PLAY, //! Starts playing file + COMMS_CMD_PAUSE //! pauses playing file +} comms_cmd_t; + +/** @brief Inline function to send commands and values. + */ +inline void comms_send(comms_cmd_t cmd) { + #ifdef MEGA328 + while (! (UCSR0A & (1 << TXC0))); + UDR0 = cmd; + #elif defined(TINY2313) + while (! (UCSRA) & (1 << TXC)) + UDR = cmd; + #endif +} + +/** @brief Sends the number of songs to play + */ +inline void comms_send_num(uint8_t num) { + comms_send(COMMS_CMD_NUM); + comms_send(num); +} + +/** @brief Selects a file from the list on the DAC side. + * + * If another song is playing, this will stop + * that song from playing and select the new one. + */ +inline void comms_select_file(uint8_t id) { + comms_send(COMMS_CMD_SELECT_FILE); + comms_send((comms_cmd_t) id); +} + +/** @brief Queries for the name of the given song ID. + */ +inline void comms_query_name(uint8_t id) { + comms_send(COMMS_CMD_QUERY_NAME); + comms_send(id); +} + +/** @brief Replies with name of song + */ +inline void comms_reply_name(char* name) { + comms_send(COMMS_CMD_REPLY_NAME); + + uint8_t len = strlen(name); + comms_send(len); + for (uint8_t i = 0; i < len; i++) { + comms_send(name[i]); + } +} + +/** @brief Clears the LCD list of files. + */ +inline void comms_clear() { + comms_send(COMMS_CMD_CLR); +} + +/** @brief Starts playing song. + */ +inline void comms_play() { + comms_send(COMMS_CMD_PLAY); +} + +/** @brief Pauses playing song. + */ +inline void comms_pause() { + comms_send(COMMS_CMD_PLAY); +} + +#endif \ No newline at end of file diff --git a/final_project/testing/main.c b/final_project/testing/main.c new file mode 100644 index 0000000..f1ea714 --- /dev/null +++ b/final_project/testing/main.c @@ -0,0 +1,42 @@ +#include "comms.h" +#include + + +#define USART_BAUDRATE 9600 //! USART baudrate, change this to set it. +#define UBRR_VALUE (((F_CPU/(USART_BAUDRATE*16UL)))-1) + +char* song_names[3] = { + "baila.wav", + "giorno.wav", + "nevergonnagiveyo" +}; + +int main() { + // initialize USART + UBRR0L = UBRR_VALUE & 255; + UBRR0H = UBRR_VALUE >> 8; + UCSR0B = (1 << TXEN0) | (1 << RXEN0); // fire-up USART + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); // fire-up USART + + while(1) { + // Send a clear command + comms_clear(); + _delay_ms(1000); + + // send a number of songs + comms_send_num(3); + + // Wait for request, get song num, then send song info 3 times + for (uint8_t i = 0; i < 3; i++) { + while(! (UCSR0A & (1 << TXC0))); + uint8_t in_song = UDR0; + while(! (UCSR0A & (1 << TXC0))); + in_song = UDR0; + comms_reply_name(song_names[in_song]); + } + + // wait a while + _delay_ms(20000); + + } +} \ No newline at end of file