Compare commits
No commits in common. "6feec2b706fa5ebf82643c95bfd171b050417233" and "a7f2348ddcdecfaba7fdaaa88a514eb0ff36f1c8" have entirely different histories.
6feec2b706
...
a7f2348ddc
28
Makefile
28
Makefile
|
@ -8,7 +8,7 @@
|
||||||
#MCU = atmega2560
|
#MCU = atmega2560
|
||||||
#MCU = attiny85
|
#MCU = attiny85
|
||||||
MCU = atmega328p
|
MCU = atmega328p
|
||||||
F_CPU = 14745600UL
|
F_CPU = 1000000UL
|
||||||
BAUD = 9600UL
|
BAUD = 9600UL
|
||||||
## Also try BAUD = 19200 or 38400 if you're feeling lucky.
|
## Also try BAUD = 19200 or 38400 if you're feeling lucky.
|
||||||
|
|
||||||
|
@ -19,10 +19,6 @@ BAUD = 9600UL
|
||||||
#LIBDIR = /usr/lib/gcc/avr/5.4.0/
|
#LIBDIR = /usr/lib/gcc/avr/5.4.0/
|
||||||
LIBDIR = /lib/avr/
|
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 ##########
|
########## Programmer Defaults ##########
|
||||||
########## Set up once, then forget about it ##########
|
########## Set up once, then forget about it ##########
|
||||||
|
@ -61,7 +57,7 @@ TARGET = $(lastword $(subst /, ,$(CURDIR)))
|
||||||
# Object files: will find all .c/.h files in current directory
|
# Object files: will find all .c/.h files in current directory
|
||||||
# and in LIBDIR. If you have any other (sub-)directories with code,
|
# and in LIBDIR. If you have any other (sub-)directories with code,
|
||||||
# you can add them in to SOURCES below in the wildcard statement.
|
# you can add them in to SOURCES below in the wildcard statement.
|
||||||
SOURCES=$(wildcard src/*.c modules/*/*.c $(LIBDIR)/*.c)
|
SOURCES=$(wildcard *.c $(LIBDIR)/*.c)
|
||||||
OBJECTS=$(SOURCES:.c=.o)
|
OBJECTS=$(SOURCES:.c=.o)
|
||||||
HEADERS=$(SOURCES:.c=.h)
|
HEADERS=$(SOURCES:.c=.h)
|
||||||
|
|
||||||
|
@ -87,16 +83,16 @@ TARGET_ARCH = -mmcu=$(MCU)
|
||||||
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<;
|
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<;
|
||||||
|
|
||||||
$(TARGET).elf: $(OBJECTS)
|
$(TARGET).elf: $(OBJECTS)
|
||||||
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $(DEBUG_DIR)/$@
|
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
|
||||||
|
|
||||||
%.hex: %.elf
|
%.hex: %.elf
|
||||||
$(OBJCOPY) -j .text -j .data -O ihex $(DEBUG_DIR)/$< $(DEBUG_DIR)/$@
|
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||||
|
|
||||||
%.eeprom: %.elf
|
%.eeprom: %.elf
|
||||||
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $(DEBUG_DIR)/$< $(DEBUG_DIR)/$@
|
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
|
||||||
|
|
||||||
%.lst: %.elf
|
%.lst: %.elf
|
||||||
$(OBJDUMP) -S $(DEBUG_DIR)/$< > $(DEBUG_DIR)/$@
|
$(OBJDUMP) -S $< > $@
|
||||||
|
|
||||||
## These targets don't have files named after them
|
## These targets don't have files named after them
|
||||||
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses
|
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses
|
||||||
|
@ -135,7 +131,7 @@ squeaky_clean:
|
||||||
########## Flashing code to AVR using avrdude ##########
|
########## Flashing code to AVR using avrdude ##########
|
||||||
##########------------------------------------------------------##########
|
##########------------------------------------------------------##########
|
||||||
|
|
||||||
flash: $(DEBUG_DIR)/$(TARGET).hex
|
flash: $(TARGET).hex
|
||||||
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$<
|
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$<
|
||||||
|
|
||||||
## An alias
|
## An alias
|
||||||
|
@ -177,7 +173,7 @@ flash_109: flash
|
||||||
#EFUSE=0x00 ##
|
#EFUSE=0x00 ##
|
||||||
|
|
||||||
## Mega 328p fuses
|
## Mega 328p fuses
|
||||||
LFUSE = 0x62
|
LFUSE = 0x62
|
||||||
HFUSE = 0xd9
|
HFUSE = 0xd9
|
||||||
EFUSE = 0xff
|
EFUSE = 0xff
|
||||||
|
|
||||||
|
@ -195,14 +191,6 @@ 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: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
|
||||||
set_default_fuses: fuses
|
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
|
## Set the fuse byte for full-speed mode
|
||||||
## Note: can also be set in firmware for modern chips
|
## Note: can also be set in firmware for modern chips
|
||||||
set_fast_fuse: LFUSE = 0xE2
|
set_fast_fuse: LFUSE = 0xE2
|
||||||
|
|
|
@ -9,15 +9,6 @@ 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
|
may end up requiring more memory, so extensive of use of define macros will
|
||||||
likely end up being the end result.
|
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:
|
## Features:
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* Author: Jake Goodwin
|
* Author: Jake Goodwin
|
||||||
* Date: 2023
|
* Date: 2023
|
||||||
* Description: Small library to handle Serial communication on AVR micros
|
* Description: Small library to communicate with the AT-09 bluetooth module.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <avr/io.h>
|
#include <avr/io.h>
|
||||||
|
@ -25,8 +25,9 @@ int main() {
|
||||||
|
|
||||||
init_usart0();
|
init_usart0();
|
||||||
|
|
||||||
//enable even parity
|
//enable parity, because the internal
|
||||||
serial0_enable_parity_bit(1);
|
//crystal is garbage
|
||||||
|
serial0_enable_parity_bit(2);
|
||||||
|
|
||||||
//enable interrupts
|
//enable interrupts
|
||||||
sei();
|
sei();
|
||||||
|
@ -63,8 +64,8 @@ int main() {
|
||||||
void init_usart0(void) {
|
void init_usart0(void) {
|
||||||
//setup stuff for usart communications.
|
//setup stuff for usart communications.
|
||||||
|
|
||||||
UBRR0H |= (uint8_t) (F_UBRR>>8);
|
UBRR0H |= (uint8_t) (BT_UBRR>>8);
|
||||||
UBRR0L |= (uint8_t) F_UBRR;
|
UBRR0L |= (uint8_t) BT_UBRR;
|
||||||
|
|
||||||
//Enable recv and Transmit pins, overrides other uses.
|
//Enable recv and Transmit pins, overrides other uses.
|
||||||
//IN the usart control and status register 0B
|
//IN the usart control and status register 0B
|
||||||
|
@ -157,6 +158,24 @@ 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
|
* Input: A parity setting as a 8bit uint
|
||||||
* Output: none
|
* Output: none
|
||||||
|
@ -214,6 +233,40 @@ 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
|
* Inupt: BUAD RATE as uint8_t
|
||||||
* Output: None
|
* Output: None
|
|
@ -4,7 +4,7 @@
|
||||||
* Description: AT-09 Bluetooth module library for AVR micro crontrollers.
|
* Description: AT-09 Bluetooth module library for AVR micro crontrollers.
|
||||||
* Filename: avr_at-09.h
|
* Filename: avr_at-09.h
|
||||||
*/
|
*/
|
||||||
//#define __AVR_ATmega328P__
|
#define __AVR_ATmega328P__
|
||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
@ -17,18 +17,9 @@
|
||||||
* AVR D1 --> AT-09 RX
|
* AVR D1 --> AT-09 RX
|
||||||
* AVR D2 --> AT-09 State
|
* AVR D2 --> AT-09 State
|
||||||
*/
|
*/
|
||||||
|
#define FOSC 1000000UL
|
||||||
|
#define BLE_BAUD 4800
|
||||||
//F_CPU and BAUD should be defined with -D when compiling the code.
|
#define BT_UBRR ((FOSC)/(BLE_BAUD*16UL)-1)
|
||||||
#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_PIN PIND1
|
||||||
#define TX_DDR DDD1
|
#define TX_DDR DDD1
|
||||||
|
@ -64,9 +55,12 @@ unsigned char rx_usart0(void);
|
||||||
|
|
||||||
void serial0_write(unsigned char* buffer, uint8_t write_length);
|
void serial0_write(unsigned char* buffer, uint8_t write_length);
|
||||||
void serial0_read(unsigned char* buffer, uint8_t buf_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 serial0_set_baud(uint8_t baud);
|
||||||
void seiral0_enable_timeouts(uint8_t ms);
|
void seiral0_enable_timeouts(uint8_t ms);
|
||||||
void serial0_flush_rxbuf(void);
|
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_enable_parity_bit(uint8_t pos);
|
||||||
void serial0_set_stop_bits(uint8_t number);
|
void serial0_set_stop_bits(uint8_t number);
|
||||||
|
|
Loading…
Reference in New Issue