From 9931aaf299e3d8c1263cc7f1148bd1e6fe7314e8 Mon Sep 17 00:00:00 2001 From: Jimmy Date: Thu, 10 Dec 2020 14:58:46 +1300 Subject: [PATCH] Intial commit --- .gitignore | 2 ++ makefile | 28 ++++++++++++++++++++++ src/main.cpp | 21 +++++++++++++++++ src/usart.cpp | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/usart.h | 14 +++++++++++ term.py | 13 +++++++++++ 6 files changed, 143 insertions(+) create mode 100644 .gitignore create mode 100644 makefile create mode 100644 src/main.cpp create mode 100644 src/usart.cpp create mode 100644 src/usart.h create mode 100644 term.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..662f6d5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/ +bin/ diff --git a/makefile b/makefile new file mode 100644 index 0000000..661d854 --- /dev/null +++ b/makefile @@ -0,0 +1,28 @@ +MCU=atmega32 +F_CPU=8000000 +PROG=dragon_jtag +PORT?=/dev/ttyUSB0 +CC=avr-g++ +OBJCOPY=avr-objcopy +CFLAGS=-Wall -g -mmcu=${MCU} -DF_CPU=${F_CPU} -I. +TARGET=main +SRCS= src/*.cpp + +all: build flash + +build: + ${CC} ${CFLAGS} -o bin/${TARGET}.bin ${SRCS} + ${CC} ${CFLAGS} -o bin/${TARGET}.elf ${SRCS} + ${OBJCOPY} -j .text -j .data -O ihex bin/${TARGET}.bin bin/${TARGET}.hex + +flash: + avrdude -p ${MCU} -c ${PROG} -U flash:w:bin/main.hex + +clean: + rm -f bin/* + +term: + python3 term.py + +avarice: + avarice --program --file bin/main.elf --part atmega32 --dragon :4242 diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..770521e --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,21 @@ +#include +#include +#include "usart.h" +#include + + + + + +int main (void) { + DDRD |= (1< +#include +#include "usart.h" + +namespace usart +{ + namespace { + static FILE mystdout; + } + + void init( unsigned long baud) { + fdev_setup_stream(&mystdout, put_printf, NULL, _FDEV_SETUP_WRITE); + stdout = &mystdout; + const unsigned int ubrr = F_CPU/8/baud-1; + + /*Set baud rate */ + + + #if defined __AVR_ATmega328P__ + UBRR0H = (unsigned char)(ubrr>>8); + UBRR0L = (unsigned char)ubrr; + UCSR0A |= (1<>8); + UBRRL = (unsigned char)ubrr; + UCSRA |= (1< + +namespace usart +{ + void init(unsigned long baud); + void put(char data ); + char get(); + int put_printf(char var, FILE *stream); +} // namespace usart + + + diff --git a/term.py b/term.py new file mode 100644 index 0000000..a6c0b3d --- /dev/null +++ b/term.py @@ -0,0 +1,13 @@ +import serial +from time import sleep + +with serial.Serial('/dev/ttyUSB0', 9600, timeout=1) as ser: + ser.write(b'a') + while True: + x = ser.read() # read one byte + if x != b'': + pass + print(x.decode('utf')) + #ser.write(b'a') + #print(ser.read()) + sleep(1) \ No newline at end of file