avr_mdcart_sram_lite

changeset 0:c47d05df0667

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 07 Mar 2017 23:50:45 +0200
parents
children 318a758ede82
files .hgignore Makefile serial.c serial.h test.c
diffstat 5 files changed, 202 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.hgignore	Tue Mar 07 23:50:45 2017 +0200
     1.3 @@ -0,0 +1,6 @@
     1.4 +\.o$
     1.5 +\.swp$
     1.6 +\.eep$
     1.7 +\.hex$
     1.8 +\.map$
     1.9 +^test_ftdi$
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/Makefile	Tue Mar 07 23:50:45 2017 +0200
     2.3 @@ -0,0 +1,38 @@
     2.4 +src = $(wildcard *.c)
     2.5 +obj = $(src:.c=.o)
     2.6 +bin = test_ftdi
     2.7 +hex = $(bin).hex
     2.8 +eep = $(bin).eep
     2.9 +
    2.10 +mcu_gcc = atmega644p
    2.11 +mcu_dude = m644p
    2.12 +
    2.13 +CC = avr-gcc
    2.14 +OBJCOPY = avr-objcopy
    2.15 +
    2.16 +CFLAGS = -Os -pedantic -Wall -mmcu=$(mcu_gcc) -DXTAL=14745600
    2.17 +LDFLAGS = -Wl,-Map,$(bin).map -mmcu=$(mcu_gcc) -lprintf_min
    2.18 +
    2.19 +.PHONY: all
    2.20 +all: $(hex) $(eep)
    2.21 +
    2.22 +$(bin): $(obj)
    2.23 +	$(CC) -o $@ $(obj) $(LDFLAGS)
    2.24 +
    2.25 +$(hex): $(bin)
    2.26 +	$(OBJCOPY) -j .text -j .data -O ihex -R .eeprom $< $@
    2.27 +
    2.28 +$(eep): $(bin)
    2.29 +	$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
    2.30 +
    2.31 +.PHONY: fuses
    2.32 +fuses:
    2.33 +	avrdude -c usbtiny -p $(mcu_dude) -U lfuse:w:0xf7:m -U hfuse:w:0x99:m -U efuse:w:0xff:m
    2.34 +
    2.35 +.PHONY: program
    2.36 +program: $(hex)
    2.37 +	avrdude -c usbtiny -p $(mcu_dude) -e -U flash:w:$(hex)
    2.38 +
    2.39 +.PHONY: clean
    2.40 +clean:
    2.41 +	rm -f $(bin) $(obj) $(hex) $(eep) $(bin).map
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/serial.c	Tue Mar 07 23:50:45 2017 +0200
     3.3 @@ -0,0 +1,106 @@
     3.4 +#ifdef XTAL
     3.5 +#define F_CLK	XTAL
     3.6 +#define F_CPU	XTAL
     3.7 +#else
     3.8 +#warning "compiled for 1mhz internal rc osc. serial comms won't work"
     3.9 +#define F_CLK	1000000
    3.10 +#define F_CPU	1000000
    3.11 +#endif
    3.12 +
    3.13 +#include <stdio.h>
    3.14 +#include <avr/io.h>
    3.15 +#include <avr/interrupt.h>
    3.16 +#include <util/delay.h>
    3.17 +#include <avr/power.h>
    3.18 +
    3.19 +static int uart_send_char(char c, FILE *fp);
    3.20 +static int uart_get_char(FILE *fp);
    3.21 +
    3.22 +#define BUF_SZ	64
    3.23 +#define BUF_IDX_MASK	0x3f
    3.24 +#define NEXT_IDX(x)	(((x) + 1) & BUF_IDX_MASK)
    3.25 +static char outbuf[BUF_SZ];
    3.26 +static volatile unsigned char out_rd, out_wr;
    3.27 +static char inbuf[BUF_SZ];
    3.28 +static volatile unsigned char in_rd, in_wr;
    3.29 +
    3.30 +static FILE std_stream = FDEV_SETUP_STREAM(uart_send_char, uart_get_char, _FDEV_SETUP_RW);
    3.31 +
    3.32 +
    3.33 +
    3.34 +void init_serial(long baud)
    3.35 +{
    3.36 +	unsigned int ubrr_val = F_CLK / 16 / baud - 1;
    3.37 +
    3.38 +	power_usart0_enable();
    3.39 +
    3.40 +	/* set baud generator timer reset value */
    3.41 +	UBRR0H = (unsigned char)(ubrr_val >> 8);
    3.42 +	UBRR0L = (unsigned char)ubrr_val;
    3.43 +
    3.44 +	/* enable rx/tx and recv interrupt */
    3.45 +	UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
    3.46 +	/* set frame format: 8n1 */
    3.47 +	UCSR0C = 3 << UCSZ00;
    3.48 +
    3.49 +	stdin = stdout = stderr = &std_stream;
    3.50 +}
    3.51 +
    3.52 +int have_input(void)
    3.53 +{
    3.54 +	return in_wr != in_rd;
    3.55 +}
    3.56 +
    3.57 +static int uart_send_char(char c, FILE *fp)
    3.58 +{
    3.59 +	int next;
    3.60 +
    3.61 +	if(c == '\n') {
    3.62 +		uart_send_char('\r', fp);
    3.63 +	}
    3.64 +
    3.65 +	next = NEXT_IDX(out_wr);
    3.66 +	while(next == out_rd);
    3.67 +
    3.68 +	outbuf[out_wr] = c;
    3.69 +	out_wr = next;
    3.70 +
    3.71 +	/* enable the Tx data register empty interrupt */
    3.72 +	UCSR0B |= 1 << UDRIE0;
    3.73 +	return 0;
    3.74 +}
    3.75 +
    3.76 +static int uart_get_char(FILE *fp)
    3.77 +{
    3.78 +	char c;
    3.79 +
    3.80 +	while(in_rd == in_wr);
    3.81 +
    3.82 +	c = inbuf[in_rd];
    3.83 +	in_rd = NEXT_IDX(in_rd);
    3.84 +	return c;
    3.85 +}
    3.86 +
    3.87 +ISR(USART0_RX_vect)
    3.88 +{
    3.89 +	char c = UDR0;
    3.90 +
    3.91 +	if(c == '\r') {
    3.92 +		c = '\n';
    3.93 +	}
    3.94 +
    3.95 +	inbuf[in_wr] = c;
    3.96 +	in_wr = NEXT_IDX(in_wr);
    3.97 +}
    3.98 +
    3.99 +/* USART Tx data register empty (can send more data) */
   3.100 +ISR(USART0_UDRE_vect)
   3.101 +{
   3.102 +	if(out_rd != out_wr) {
   3.103 +		UDR0 = outbuf[out_rd];
   3.104 +		out_rd = NEXT_IDX(out_rd);
   3.105 +	} else {
   3.106 +		/* no more data to send for now, disable the interrupt */
   3.107 +		UCSR0B &= ~(1 << UDRIE0);
   3.108 +	}
   3.109 +}
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/serial.h	Tue Mar 07 23:50:45 2017 +0200
     4.3 @@ -0,0 +1,7 @@
     4.4 +#ifndef SERIAL_H_
     4.5 +#define SERIAL_H_
     4.6 +
     4.7 +void init_serial(long baud);
     4.8 +int have_input(void);
     4.9 +
    4.10 +#endif	/* SERIAL_H_ */
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/test.c	Tue Mar 07 23:50:45 2017 +0200
     5.3 @@ -0,0 +1,45 @@
     5.4 +#include <stdio.h>
     5.5 +#include <string.h>
     5.6 +#include <ctype.h>
     5.7 +#include <avr/io.h>
     5.8 +#include <avr/interrupt.h>
     5.9 +#include "serial.h"
    5.10 +
    5.11 +void procmd(const char *cmd);
    5.12 +
    5.13 +#define MAX_INPUT_SIZE	128
    5.14 +static char input[MAX_INPUT_SIZE];
    5.15 +static unsigned char inp_cidx;
    5.16 +
    5.17 +int main(void)
    5.18 +{
    5.19 +	init_serial(38400);
    5.20 +	sei();
    5.21 +
    5.22 +	printf("welcome!\n");
    5.23 +
    5.24 +	for(;;) {
    5.25 +		while(have_input()) {
    5.26 +			int c = getchar();
    5.27 +			putchar(c);
    5.28 +
    5.29 +			if(c == '\r' || c == '\n') {
    5.30 +				input[inp_cidx] = 0;
    5.31 +				procmd(input);
    5.32 +				inp_cidx = 0;
    5.33 +			} else if(inp_cidx < sizeof input - 1) {
    5.34 +				input[inp_cidx++] = c;
    5.35 +			}
    5.36 +		}
    5.37 +	}
    5.38 +	return 0;
    5.39 +}
    5.40 +
    5.41 +void procmd(const char *cmd)
    5.42 +{
    5.43 +	if(strcmp(cmd, "info") == 0) {
    5.44 +		printf("test firmware v0\n");
    5.45 +	} else {
    5.46 +		printf("unknown command: %s\n", cmd);
    5.47 +	}
    5.48 +}