Intial commit

This commit is contained in:
2020-12-11 10:17:19 +13:00
parent 9024442baa
commit 1c89ad2ea0
16 changed files with 900 additions and 0 deletions

2
lib/usart/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
.vscode/
bin/

19
lib/usart/LICENSE Normal file
View File

@@ -0,0 +1,19 @@
MIT License Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

2
lib/usart/README.md Normal file
View File

@@ -0,0 +1,2 @@
# usart

28
lib/usart/makefile Normal file
View File

@@ -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 test/main.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

65
lib/usart/src/usart.cpp Normal file
View File

@@ -0,0 +1,65 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#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<<U2X0);
//Enable receiver and transmitter
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
//UCSRB |= (1<< RXCIE)|(1<<TXCIE);
/* Set frame format: 8data, 2stop bit */
UCSR0C = (1<<USBS0)|(3<<UCSZ00);
#elif defined __AVR_ATmega32__ || defined __AVR_ATmega16__
UBRRH = (unsigned char)(ubrr>>8);
UBRRL = (unsigned char)ubrr;
UCSRA |= (1<<U2X);
//Enable receiver and transmitter
UCSRB = (1<<RXEN)|(1<<TXEN);
//UCSRB |= (1<< RXCIE)|(1<<TXCIE);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1 << URSEL)|(3<<UCSZ0);
#endif
}
void put(char data) {
#if defined __AVR_ATmega328P__
while ( !( UCSR0A & (1<<UDRE0)) ); UDR0 = data;
#elif defined __AVR_ATmega32__ || defined __AVR_ATmega16__
while ( !( UCSRA & (1<<UDRE)) ); UDR = data;
#endif
}
char get( void ) {
#if defined __AVR_ATmega328P__
while ( !(UCSR0A & (1<<RXC0)) ); return UDR0;
#elif defined __AVR_ATmega32__ || defined __AVR_ATmega16__
while ( !(UCSRA & (1<<RXC)) ); return UDR;
#endif
}
int put_printf(char var, FILE *stream) {
put(var);
return 0;
}
} // namespace usart

14
lib/usart/src/usart.h Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <stdio.h>
namespace usart
{
void init(unsigned long baud);
void put(char data );
char get();
int put_printf(char var, FILE *stream);
} // namespace usart

13
lib/usart/term.py Normal file
View File

@@ -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)

21
lib/usart/test/main.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include <avr/io.h>
#include <avr/interrupt.h>
#include "../src/usart.h"
#include <util/delay.h>
int main (void) {
DDRD |= (1<<PD3)|(1<<PD4)|(1<<PD5);
usart::init(9600);
for (;;) {// Loop forever
usart::put('A');
//printf("Hello");
PORTD ^= (1<<PD3)|(1<<PD4)|(1<<PD5);
_delay_ms(1000);
}
}