From e31d24120e976a67a7c8d5a1387c27cbd58e9584 Mon Sep 17 00:00:00 2001 From: jakegoodwin Date: Sat, 14 Jan 2023 06:51:49 +0000 Subject: [PATCH 1/9] Initial commit --- LICENSE | 11 +++++++++++ README.md | 2 ++ 2 files changed, 13 insertions(+) create mode 100644 LICENSE create mode 100644 README.md diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..ea890af --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright (c) . + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1521d74 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# AVR_ATMEGA_USART + From 527ca1e9e7e983bf52b788cbb1f6f574a4df30a8 Mon Sep 17 00:00:00 2001 From: jakegoodwin Date: Fri, 13 Jan 2023 21:13:45 -0800 Subject: [PATCH 2/9] INIT COMMIT --- Makefile | 210 ++++++++++++++++++++++++++++++++++++++++++++++++++++ avr_usart.c | 121 ++++++++++++++++++++++++++++++ avr_usart.h | 47 ++++++++++++ 3 files changed, 378 insertions(+) create mode 100644 Makefile create mode 100644 avr_usart.c create mode 100644 avr_usart.h diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d9a9bec --- /dev/null +++ b/Makefile @@ -0,0 +1,210 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +#MCU = atmega168p +#MCU = atmega2560 +#MCU = attiny85 +MCU = atmega328p +F_CPU = 1000000UL +BAUD = 9600UL +## Also try BAUD = 19200 or 38400 if you're feeling lucky. + +## A directory for common include files and the simple USART library. +## If you move either the current folder or the Library folder, you'll +## need to change this path to match. +#LIBDIR = ../../AVR-Programming-Library +#LIBDIR = /usr/lib/gcc/avr/5.4.0/ +LIBDIR = /lib/avr/ + +##########------------------------------------------------------########## +########## Programmer Defaults ########## +########## Set up once, then forget about it ########## +########## (Can override. See bottom of file.) ########## +##########------------------------------------------------------########## + +PROGRAMMER_TYPE = usbasp-clone +# extra arguments to avrdude: baud rate, chip type, -F flag, etc. +PROGRAMMER_ARGS = -b 9600 -B 4 -v + +##########------------------------------------------------------########## +########## Program Locations ########## +########## Won't need to change if they're in your PATH ########## +##########------------------------------------------------------########## + +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude + +##########------------------------------------------------------########## +########## Makefile Magic! ########## +########## Summary: ########## +########## We want a .hex file ########## +########## Compile source files into .elf ########## +########## Convert .elf file into .hex ########## +########## You shouldn't need to edit below. ########## +##########------------------------------------------------------########## + +## The name of your project (without the .c) +# TARGET = blinkLED +## Or name it automatically after the enclosing directory +TARGET = $(lastword $(subst /, ,$(CURDIR))) + +# Object files: will find all .c/.h files in current directory +# and in LIBDIR. If you have any other (sub-)directories with code, +# you can add them in to SOURCES below in the wildcard statement. +SOURCES=$(wildcard *.c $(LIBDIR)/*.c) +OBJECTS=$(SOURCES:.c=.o) +HEADERS=$(SOURCES:.c=.h) + +## Compilation options, type man avr-gcc if you're curious. +CPPFLAGS = -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR) +CFLAGS = -Os -g -std=gnu99 -Wall +## Use short (8-bit) data types +CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +## Splits up object files per function +CFLAGS += -ffunction-sections -fdata-sections +LDFLAGS = -Wl,-Map,$(TARGET).map +## Optional, but often ends up with smaller code +LDFLAGS += -Wl,--gc-sections +## Relax shrinks code even more, but makes disassembly messy +## LDFLAGS += -Wl,--relax +## LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf +## LDFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf +TARGET_ARCH = -mmcu=$(MCU) + +## Explicit pattern rules: +## To make .o files from .c files +%.o: %.c $(HEADERS) Makefile + $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<; + +$(TARGET).elf: $(OBJECTS) + $(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@ + +%.hex: %.elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +## These targets don't have files named after them +.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses + +all: $(TARGET).hex + +debug: + @echo + @echo "Source files:" $(SOURCES) + @echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD) + @echo + +# Optionally create listing file from .elf +# This creates approximate assembly-language equivalent of your code. +# Useful for debugging time-sensitive bits, +# or making sure the compiler does what you want. +disassemble: $(TARGET).lst + +disasm: disassemble + +# Optionally show how big the resulting program is +size: $(TARGET).elf + $(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf + +clean: + rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \ + $(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \ + $(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \ + $(TARGET).eeprom + +squeaky_clean: + rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ *.eeprom + +##########------------------------------------------------------########## +########## Programmer-specific details ########## +########## Flashing code to AVR using avrdude ########## +##########------------------------------------------------------########## + +flash: $(TARGET).hex + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$< + +## An alias +program: flash + +flash_eeprom: $(TARGET).eeprom + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$< + +avrdude_terminal: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt + +## If you've got multiple programmers that you use, +## you can define them here so that it's easy to switch. +## To invoke, use something like `make flash_arduinoISP` +flash_usbtiny: PROGRAMMER_TYPE = usbtiny +flash_usbtiny: PROGRAMMER_ARGS = # USBTiny works with no further arguments +flash_usbtiny: flash + +flash_usbasp: PROGRAMMER_TYPE = usbasp +flash_usbasp: PROGRAMMER_ARGS = # USBasp works with no further arguments +flash_usbasp: flash + +flash_arduinoISP: PROGRAMMER_TYPE = avrisp +flash_arduinoISP: PROGRAMMER_ARGS = -b 19200 -P /dev/ttyACM0 +## (for windows) flash_arduinoISP: PROGRAMMER_ARGS = -b 19200 -P com5 +flash_arduinoISP: flash + +flash_109: PROGRAMMER_TYPE = avr109 +flash_109: PROGRAMMER_ARGS = -b 9600 -P /dev/ttyUSB0 +flash_109: flash + +##########------------------------------------------------------########## +########## Fuse settings and suitable defaults ########## +##########------------------------------------------------------########## + +## Mega 48, 88, 168, 328 default values +LFUSE = 0x62 # +HFUSE = 0xdf #High fuses +EFUSE = 0x00 # + +## ATtiny85 Settings +#LFUSE = 0x62 +#HFUSE = 0xdf +#EFUSE = 0xff + + + + + +## Generic +FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m + +fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \ + $(PROGRAMMER_ARGS) $(FUSE_STRING) +show_fuses: + $(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv + +## Called with no extra definitions, sets to defaults +set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m +set_default_fuses: fuses + +## Set the fuse byte for full-speed mode +## Note: can also be set in firmware for modern chips +set_fast_fuse: LFUSE = 0xE2 +set_fast_fuse: FUSE_STRING = -U lfuse:w:$(LFUSE):m +set_fast_fuse: fuses + +## Set the EESAVE fuse byte to preserve EEPROM across flashes +set_eeprom_save_fuse: HFUSE = 0xD7 +set_eeprom_save_fuse: FUSE_STRING = -U hfuse:w:$(HFUSE):m +set_eeprom_save_fuse: fuses + +## Clear the EESAVE fuse byte +clear_eeprom_save_fuse: FUSE_STRING = -U hfuse:w:$(HFUSE):m +clear_eeprom_save_fuse: fuses diff --git a/avr_usart.c b/avr_usart.c new file mode 100644 index 0000000..bdae0f9 --- /dev/null +++ b/avr_usart.c @@ -0,0 +1,121 @@ +/* + * Author: Jake Goodwin + * Date: 2023 + * Description: Small library to communicate with the AT-09 bluetooth module. + */ + +#include +#include +#include +#include +#include "avr_at-09.h" + + +//############################# +//Globals +//############################# + +int main() { + unsigned char* tx_buffer[16]; + unsigned char* rx_buffer[16]; + + led_blink(); + led_blink(); + led_blink(); + _delay_ms(1000); + + init_usart0(); + + //enable interrupts + sei(); + + while(1) { + led_blink(); + } + + return 0; +} + + + +//############################# +//FUNCTIONS +//############################# + +/* + * Input: None + * Output: None + * Description: init usart0 hardware in async mode + */ +void init_usart0(void) { + DDRB |= (1<>8); + UBRR0L |= (uint8_t) BT_UBRR; + + //Enable recv and Transmit pins, overrides other uses. + //IN the usart control and status register 0B + UCSR0B = (1< AT-09 TX + * AVR D1 --> AT-09 RX + * AVR D2 --> AT-09 State + */ +#define FOSC 1000000 +#define BLE_BAUD 9600 +#define BT_UBRR FOSC/16/BLE_BAUD - 1 + +#define TX_PIN PIND0 +#define TX_DDR DDD0 + +#define RX_PIN PIND1 +#define RX_DDR DDD1 + +#define AT_PORT PORTD + +//LED PIN for indicator and debug. +#define LED_PIN PINC0 +#define LED_DDR DDC0 + + +//############################# +//TYPES +//############################# + +//############################# +//FUNCTION PROTOTYPES +//############################# +static void led_blink(void); + +void init_usart0(void); +void tx_usart0(unsigned char data); +unsigned char rx_usart0(void); + From 13ef0d793da1912f63107e1b87467b5ef134539b Mon Sep 17 00:00:00 2001 From: jakegoodwin Date: Fri, 13 Jan 2023 21:14:14 -0800 Subject: [PATCH 3/9] added a ignore file --- .gitignore | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e69de29 From 290629670d253f4184741919d7640f22b14d1c55 Mon Sep 17 00:00:00 2001 From: jakegoodwin Date: Fri, 13 Jan 2023 21:14:53 -0800 Subject: [PATCH 4/9] updated to ignore binary stuff. --- .gitignore | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.gitignore b/.gitignore index e69de29..a8c0ee5 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.map +*.hex +*.elf From ae1a5d59475c10d2cd7e1c5561b1798e21682ddc Mon Sep 17 00:00:00 2001 From: jakegoodwin Date: Fri, 13 Jan 2023 22:47:43 -0800 Subject: [PATCH 5/9] set 9600 baud rate, tested and working --- avr_usart.c | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/avr_usart.c b/avr_usart.c index bdae0f9..fc833ad 100644 --- a/avr_usart.c +++ b/avr_usart.c @@ -8,7 +8,7 @@ #include #include #include -#include "avr_at-09.h" +#include "avr_usart.h" //############################# @@ -23,14 +23,22 @@ int main() { led_blink(); led_blink(); _delay_ms(1000); + _delay_ms(1000); init_usart0(); //enable interrupts sei(); + unsigned char data[] = "AT+WAKE"; + while(1) { - led_blink(); + _delay_ms(1000); + //led_blink(); + for(uint8_t i = 0; i < 7; i++) { + tx_usart0(data[i]); + } + } return 0; @@ -48,16 +56,19 @@ int main() { * Description: init usart0 hardware in async mode */ void init_usart0(void) { - DDRB |= (1<>8); - UBRR0L |= (uint8_t) BT_UBRR; - + //set baud rate, this is 9600Baud with a 8Mhz clock + //UBRR0H |= (uint8_t) (BT_UBRR>>8); + //UBRR0L |= (uint8_t) BT_UBRR; + UBRR0H |= (uint8_t) (0x33>>8); + UBRR0L |= (uint8_t) 0x33; + + + //Enable recv and Transmit pins, overrides other uses. //IN the usart control and status register 0B UCSR0B = (1< Date: Fri, 13 Jan 2023 22:48:19 -0800 Subject: [PATCH 6/9] set for 8Mhz 9600 BAUD, reduces error rate too 0.2% --- avr_usart.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/avr_usart.h b/avr_usart.h index 6bab4ec..821781d 100644 --- a/avr_usart.h +++ b/avr_usart.h @@ -15,8 +15,8 @@ * AVR D1 --> AT-09 RX * AVR D2 --> AT-09 State */ -#define FOSC 1000000 -#define BLE_BAUD 9600 +#define FOSC 8000000 +#define BLE_BAUD 9600 #define BT_UBRR FOSC/16/BLE_BAUD - 1 #define TX_PIN PIND0 From e583b5beb310720e41d95800fc5c5bcec67d01ed Mon Sep 17 00:00:00 2001 From: jakegoodwin Date: Fri, 13 Jan 2023 22:59:09 -0800 Subject: [PATCH 7/9] merged with remote repo --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 1521d74..add5ed7 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ +<<<<<<< HEAD # AVR_ATMEGA_USART +======= +# AVR ATMEGA328P USART + +## Description: + +This is a asynchronous serial program for the ATMEGA seris of mcu. The sotfware +is tested and working. + + +## ISSUES: + +It's required to set the fuses in the chip for 8Mhz, otherwise a baud rate +of 9600 will result in errors at a 7% rate. + +>>>>>>> 6b73f1e (Added a readme file) From a71b57048efcaf6b530342336ab88024112e3880 Mon Sep 17 00:00:00 2001 From: jakegoodwin Date: Fri, 13 Jan 2023 22:59:59 -0800 Subject: [PATCH 8/9] merged readme files --- README.md | 4 ---- 1 file changed, 4 deletions(-) diff --git a/README.md b/README.md index add5ed7..1124644 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,4 @@ -<<<<<<< HEAD # AVR_ATMEGA_USART -======= -# AVR ATMEGA328P USART ## Description: @@ -14,5 +11,4 @@ is tested and working. It's required to set the fuses in the chip for 8Mhz, otherwise a baud rate of 9600 will result in errors at a 7% rate. ->>>>>>> 6b73f1e (Added a readme file) From 6871b2ebee994b32d3b09c6bdaaa1197dbe91c34 Mon Sep 17 00:00:00 2001 From: jakegoodwin Date: Sat, 14 Jan 2023 11:19:21 -0800 Subject: [PATCH 9/9] Updated README with roadmap of library --- README.md | 42 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1124644..4b90a09 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,52 @@ # AVR_ATMEGA_USART - +--- ## Description: -This is a asynchronous serial program for the ATMEGA seris of mcu. The sotfware +This is a asynchronous serial program for the ATMEGA seiries of mcu. The sotfware is tested and working. +Over time, I'm planning to add support for multiple AVR MCUs, however that +may end up requiring more memory, so extensive of use of define macros will +likely end up being the end result. + +## Features: + +- Multiple Baud Rates: 300, 600, 1200, 2400, 9600 ... +- Asynchronous Receive and Transmit. +- Optional Parity checking. +- 7-8 data bits. +- Hardware abstraction API +- No Interrupt service routines required. +- BSD-3 Licensing, aka you can use this for whatevery you want pretty much. + + + +## Instructions + + +### Example 1 + +```C +#include "avr_usart.h" + +void send_bt_cmd(char* cmd) { + init_usart(); + unsigned char data[] = "AT+WAKE\0"; + uint8_t result = usart_send_str(data); + + if(result) { + raise_error(); + } +} + +``` + + ## ISSUES: It's required to set the fuses in the chip for 8Mhz, otherwise a baud rate of 9600 will result in errors at a 7% rate. +Another is that I need to design a standard naming scheme.