Compare commits

...

13 Commits

4 changed files with 47 additions and 73 deletions

View File

@ -8,7 +8,7 @@
#MCU = atmega2560
#MCU = attiny85
MCU = atmega328p
F_CPU = 1000000UL
F_CPU = 14745600UL
BAUD = 9600UL
## Also try BAUD = 19200 or 38400 if you're feeling lucky.
@ -19,6 +19,10 @@ BAUD = 9600UL
#LIBDIR = /usr/lib/gcc/avr/5.4.0/
LIBDIR = /lib/avr/
#OUTPUT DIRs
#This is to tell the makefile where we want to put the object and hex files etc
DEBUG_DIR = ./out
##########------------------------------------------------------##########
########## Programmer Defaults ##########
########## Set up once, then forget about it ##########
@ -57,7 +61,7 @@ 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)
SOURCES=$(wildcard src/*.c modules/*/*.c $(LIBDIR)/*.c)
OBJECTS=$(SOURCES:.c=.o)
HEADERS=$(SOURCES:.c=.h)
@ -83,16 +87,16 @@ TARGET_ARCH = -mmcu=$(MCU)
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<;
$(TARGET).elf: $(OBJECTS)
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $(DEBUG_DIR)/$@
%.hex: %.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
$(OBJCOPY) -j .text -j .data -O ihex $(DEBUG_DIR)/$< $(DEBUG_DIR)/$@
%.eeprom: %.elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $(DEBUG_DIR)/$< $(DEBUG_DIR)/$@
%.lst: %.elf
$(OBJDUMP) -S $< > $@
$(OBJDUMP) -S $(DEBUG_DIR)/$< > $(DEBUG_DIR)/$@
## These targets don't have files named after them
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses
@ -131,7 +135,7 @@ squeaky_clean:
########## Flashing code to AVR using avrdude ##########
##########------------------------------------------------------##########
flash: $(TARGET).hex
flash: $(DEBUG_DIR)/$(TARGET).hex
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$<
## An alias
@ -173,7 +177,7 @@ flash_109: flash
#EFUSE=0x00 ##
## Mega 328p fuses
LFUSE = 0x62
LFUSE = 0x62
HFUSE = 0xd9
EFUSE = 0xff
@ -191,6 +195,14 @@ show_fuses:
set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
set_default_fuses: fuses
## For external clock low-power crystal.
# The low order fuse is set for a low-power crystal. 14.7456Mhz
# cksel[3:1] 8.0-16.0 = 111 //Sets it for the range of 8Mhz - 16Mhz
# cksel[0] 1 SUT[1:0] 11 //gives 16K ck startup time aprox 65ms
set_crystal_fuses: LFUSE = 0xFF
set_crystal_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m
set_crystal_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

View File

@ -9,6 +9,15 @@ 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.
Parity calculations are done in hardware automatically, so there are no
functions written to interface with the serial error flags as the end user
will know more about exception and error handling for their application.
**2023-02-18:**
Support for using external crystal oscillators was added to the makefile.
Using a 14.7456Mhz crystal allows for reduced error rates when using
any standard baud rates.
## Features:

View File

@ -1,7 +1,7 @@
/*
* Author: Jake Goodwin
* Date: 2023
* Description: Small library to communicate with the AT-09 bluetooth module.
* Description: Small library to handle Serial communication on AVR micros
*/
#include <avr/io.h>
@ -25,9 +25,8 @@ int main() {
init_usart0();
//enable parity, because the internal
//crystal is garbage
serial0_enable_parity_bit(2);
//enable even parity
serial0_enable_parity_bit(1);
//enable interrupts
sei();
@ -64,8 +63,8 @@ int main() {
void init_usart0(void) {
//setup stuff for usart communications.
UBRR0H |= (uint8_t) (BT_UBRR>>8);
UBRR0L |= (uint8_t) BT_UBRR;
UBRR0H |= (uint8_t) (F_UBRR>>8);
UBRR0L |= (uint8_t) F_UBRR;
//Enable recv and Transmit pins, overrides other uses.
//IN the usart control and status register 0B
@ -158,24 +157,6 @@ void serial0_write(unsigned char* buffer, uint8_t write_length) {
}
/*
* Input: none
* Output: none
* Description:
*/
void serial0_enable_line_feeds(void) {
;
}
/*
* Input: none
* Output: none
* Description:
*/
void serial0_enable_carriage_returns(void) {
;
}
/*
* Input: A parity setting as a 8bit uint
* Output: none
@ -233,40 +214,6 @@ void serial0_read(unsigned char* buffer, uint8_t buf_length) {
}
/*
* Inupt: A serialbuffer and length
* Output: None
* Description: Reads the serial data into a buffer of x length,
* it does error checking from the register first.
*/
void serial0_read_with_err_checking(unsigned char* buffer, uint8_t buf_length) {
//first wait for the data to be received.
while( !(UCSR0A & (1<<RXC0)) ) {
;//Equiv of continue
}
//check for errors in the register.
//Check for frame error.
if(UCSR0A & (1<<FE0)) {
led_blink();
}
//check for data over-run
else if(UCSR0A & (1<<DOR0)) {
led_blink();
led_blink();
}
//check for parity error
else if(UCSR0A & (1<<UPE0)) {
led_blink();
led_blink();
led_blink();
}
//now return the data
buffer[0] = UDR0;
}
/*
* Inupt: BUAD RATE as uint8_t
* Output: None

View File

@ -4,7 +4,7 @@
* Description: AT-09 Bluetooth module library for AVR micro crontrollers.
* Filename: avr_at-09.h
*/
#define __AVR_ATmega328P__
//#define __AVR_ATmega328P__
#include <inttypes.h>
#include <stdint.h>
@ -17,9 +17,18 @@
* AVR D1 --> AT-09 RX
* AVR D2 --> AT-09 State
*/
#define FOSC 1000000UL
#define BLE_BAUD 4800
#define BT_UBRR ((FOSC)/(BLE_BAUD*16UL)-1)
//F_CPU and BAUD should be defined with -D when compiling the code.
#ifndef F_CPU
#define F_CPU 14745600UL
#endif
#ifndef BUAD
#define BAUD 4800
#endif
#define F_UBRR ((F_CPU)/(BAUD*16UL)-1)
#define TX_PIN PIND1
#define TX_DDR DDD1
@ -55,12 +64,9 @@ unsigned char rx_usart0(void);
void serial0_write(unsigned char* buffer, uint8_t write_length);
void serial0_read(unsigned char* buffer, uint8_t buf_length);
void serial0_read_with_err_checking(unsigned char* buffer, uint8_t buf_length);
void serial0_set_baud(uint8_t baud);
void seiral0_enable_timeouts(uint8_t ms);
void serial0_flush_rxbuf(void);
void serial0_enable_line_feeds(void);
void serial0_enable_carriage_returns(void);
void serial0_enable_parity_bit(uint8_t pos);
void serial0_set_stop_bits(uint8_t number);