diff options
author | Nao Pross <naopross@thearcway.org> | 2018-10-30 12:07:58 +0100 |
---|---|---|
committer | Nao Pross <naopross@thearcway.org> | 2018-10-30 12:34:37 +0100 |
commit | cda16639cbf19d6eb5c11f05f1045e54b3fdaba9 (patch) | |
tree | 25227b76ae6dd50d5eddc30e4806af2003e70f54 /sw-avr/rom-loader | |
parent | Update gitignore (diff) | |
download | z80uPC-cda16639cbf19d6eb5c11f05f1045e54b3fdaba9.tar.gz z80uPC-cda16639cbf19d6eb5c11f05f1045e54b3fdaba9.zip |
[avr] Add test program for IO and basic usart
Diffstat (limited to 'sw-avr/rom-loader')
-rw-r--r-- | sw-avr/rom-loader/main.c | 34 | ||||
-rw-r--r-- | sw-avr/rom-loader/makefile | 25 | ||||
-rw-r--r-- | sw-avr/rom-loader/pinmap.h | 19 | ||||
-rwxr-xr-x | sw-avr/rom-loader/rom-loader.bin | bin | 6352 -> 0 bytes | |||
-rw-r--r-- | sw-avr/rom-loader/usart.c | 37 | ||||
-rw-r--r-- | sw-avr/rom-loader/usart.h | 10 |
6 files changed, 115 insertions, 10 deletions
diff --git a/sw-avr/rom-loader/main.c b/sw-avr/rom-loader/main.c index b178c06..784afb6 100644 --- a/sw-avr/rom-loader/main.c +++ b/sw-avr/rom-loader/main.c @@ -1,6 +1,38 @@ #include <avr/io.h> +#include "usart.h" +#include "pinmap.h" + +// TODO: remove +#include <util/delay.h> + + +inline void io_init(void) +{ + ADDRL_DDR |= (_BV(ADDRL_bitE) | _BV(ADDRL_bitD)); + ADDRL_DDR |= (0x7<<ADDRL_bitA0); // 3 bits + + ADDRH_DDR |= (_BV(ADDRH_bitE) | _BV(ADDRH_bitD)); + ADDRH_DDR |= (0x7<<ADDRH_bitA0); + + // all output + DATA_DDR = 0xFF; +} + int main(void) { - return; + io_init(); + usart_init(); + + // TODO: remove + DATA_PORT |= _BV(1); + + while (1) { + DATA_PORT ^= _BV(1); + _delay_ms(500); + + usart_send('c'); + } + + return 0; } diff --git a/sw-avr/rom-loader/makefile b/sw-avr/rom-loader/makefile index e68174f..85222c8 100644 --- a/sw-avr/rom-loader/makefile +++ b/sw-avr/rom-loader/makefile @@ -1,17 +1,24 @@ -CC=avr-gcc -CARGS=-Wall -mmcu=atmega328p -D__AVR_ATmega328p__ -Os -I. +SOURCES := main.c usart.c +OBJECTS := $(patsubst %.c,%.o,$(SOURCES)) -SOURCES=main.c +CFLAGS := -Wall -mmcu=atmega328p -Os -I. \ + -D__AVR_ATmega328p__ -DF_CPU=1000000UL -DBAUD=9600 +LDFLAGS := .PHONY: all flash clean -all: rom-loader.bin +all: rom-loader.hex -flash: rom-loader.bin - avrdude -c usbasp -p atmega328p +flash: rom-loader.hex + avrdude -c usbasp -p atmega328p -U flash:w:$< clean: - rm rom-loader.bin + rm -f *.bin *.hex *.o -rom-loader.bin: $(SOURCES) - $(CC) $(CARGS) $< -o $@ +rom-loader.hex: rom-loader.bin + avr-objcopy -R .eeprom -O ihex $< $@ +rom-loader.bin: $(OBJECTS) + avr-gcc $(CFLAGS) $(OBJECTS) -o $@ $(LDFLAGS) + +%.o: %.c + avr-gcc $(CFLAGS) $< -c -o $@ $(LDFLAGS) diff --git a/sw-avr/rom-loader/pinmap.h b/sw-avr/rom-loader/pinmap.h new file mode 100644 index 0000000..1149feb --- /dev/null +++ b/sw-avr/rom-loader/pinmap.h @@ -0,0 +1,19 @@ +#ifndef PINMAP_H +#define PINMAP_H + +#define ADDRL_DDR DDRC +#define ADDRL_PORT PORTC +#define ADDRL_bitE 3 +#define ADDRL_bitD 4 +#define ADDRL_bitA0 0 + +#define ADDRH_DDR DDRD +#define ADDRH_PORT PORTD +#define ADDRH_bitE 7 +#define ADDRH_bitD 3 +#define ADDRH_bitA0 4 + +#define DATA_DDR DDRB +#define DATA_PORT PORTB + +#endif diff --git a/sw-avr/rom-loader/rom-loader.bin b/sw-avr/rom-loader/rom-loader.bin Binary files differdeleted file mode 100755 index 1feb241..0000000 --- a/sw-avr/rom-loader/rom-loader.bin +++ /dev/null diff --git a/sw-avr/rom-loader/usart.c b/sw-avr/rom-loader/usart.c new file mode 100644 index 0000000..5637d4f --- /dev/null +++ b/sw-avr/rom-loader/usart.c @@ -0,0 +1,37 @@ +#include "usart.h" + +#include <avr/io.h> +#include <util/setbaud.h> + +void usart_init(void) +{ + // automated by util/setbaud.h + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; + +#if USE_2X + UCSR0A |= _BV(U2X0); +#else + UCSR0A &= ~(_BV(U2X0)); +#endif + + // 8 bit data + UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); + + // enable TX and RX pins + UCSR0B = _BV(RXEN0) | _BV(TXEN0); +} + +void usart_send(uint8_t c) +{ + UDR0 = c; + // wait until transmission ready + loop_until_bit_is_set(UCSR0A, TXC0); +} + +uint8_t usart_recv(void) +{ + // wait until data exists + loop_until_bit_is_set(UCSR0A, RXC0); + return UDR0; +} diff --git a/sw-avr/rom-loader/usart.h b/sw-avr/rom-loader/usart.h new file mode 100644 index 0000000..f7d8806 --- /dev/null +++ b/sw-avr/rom-loader/usart.h @@ -0,0 +1,10 @@ +#ifndef USART_H +#define USART_H + +#include <stdint.h> + +void usart_init(void); +void usart_send(uint8_t c); +uint8_t usart_recv(void); + +#endif |