aboutsummaryrefslogtreecommitdiffstats
path: root/atxswitch
diff options
context:
space:
mode:
authorVG <vg@devys.org>2015-08-26 18:45:01 +0200
committerVG <vg@devys.org>2015-08-26 18:45:01 +0200
commit7346cfaad4d969db060f3c7ae242ea93f4fff5c0 (patch)
tree3d3a09a29f783270b8da931e3c879234553ec43f /atxswitch
downloadavr-7346cfaad4d969db060f3c7ae242ea93f4fff5c0.tar.gz
avr-7346cfaad4d969db060f3c7ae242ea93f4fff5c0.tar.bz2
avr-7346cfaad4d969db060f3c7ae242ea93f4fff5c0.zip
Diffstat (limited to 'atxswitch')
-rw-r--r--atxswitch/Makefile35
-rw-r--r--atxswitch/main.c25
-rw-r--r--atxswitch/uart.h76
3 files changed, 136 insertions, 0 deletions
diff --git a/atxswitch/Makefile b/atxswitch/Makefile
new file mode 100644
index 0000000..db8a005
--- /dev/null
+++ b/atxswitch/Makefile
@@ -0,0 +1,35 @@
+# settings for arduino nano v3
+DEVICE = atmega328p
+CLOCK = 16000000
+PORT=/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A9SBZLHD-if00-port0
+#AVRDUDE=avrdude -V -F -p m328p -c arduino -b 115200 -P$(PORT)
+#AVRDUDE=avrdude -p $(DEVICE) -c arduino -b 115200 -P $(PORT)
+AVRDUDE=avrdude -p $(DEVICE) -c arduino -b 57600 -P $(PORT)
+
+DEFS = -DF_CPU=$(CLOCK)UL
+LIBS = -Wl,--relax,--gc-sections,--print-gc-sections,--entry=main
+LIBS = -Wl,--entry=main,--gc-sections,--rela
+CFLAGS = -Wall -Werror -pedantic -std=c99
+CFLAGS += -Os -funsigned-bitfields -fpack-struct -fshort-enums
+CFLAGS += -mmcu=$(DEVICE) $(DEFS)
+#LDFLAGS = $(LIBS)
+
+all: main.hex size
+
+main.hex: main.c
+ avr-gcc -o main.elf $? $(CFLAGS) $(LDFLAGS)
+ avr-objcopy -j .text -j .data -O ihex main.elf main.hex
+
+f: flash
+flash: all
+ $(AVRDUDE) -U flash:w:main.hex:i
+
+clean:
+ rm -f *.o *.elf *.hex
+
+size:
+ avr-size --mcu=$(DEVICE) -t -A main.elf
+ avr-size --mcu=$(DEVICE) -C main.elf
+ avr-nm --size-sort main.elf
+
+
diff --git a/atxswitch/main.c b/atxswitch/main.c
new file mode 100644
index 0000000..2e9ff5c
--- /dev/null
+++ b/atxswitch/main.c
@@ -0,0 +1,25 @@
+#include <avr/io.h>
+#include "uart.h"
+
+int main(void)
+{
+ /* serial */
+ uart_init();
+
+ /* D4 = output and high (=> atx down) */
+ PORTD |= 0x10;
+ DDRD |= 0x10;
+
+ for (;;)
+ {
+ switch(uart_getchar()) {
+ case '?': break; /* useful for auto documentation */
+ case '0': PORTD |= 0x10; break;
+ case '1': PORTD &= ~0x10; break;
+ case 'T': PORTD ^= 0x10; break;
+ }
+ uart_putchar((PORTD & 0x10) ? '0' : '1');
+ }
+
+ return 0;
+}
diff --git a/atxswitch/uart.h b/atxswitch/uart.h
new file mode 100644
index 0000000..22e0655
--- /dev/null
+++ b/atxswitch/uart.h
@@ -0,0 +1,76 @@
+/* #define F_CPU 16000000UL <= defined in Makefile */
+#define BAUD 9600
+
+/* This file is *highly* inspired from:
+ * http://www.appelsiini.net/2011/simple-usart-with-avr-libc
+ */
+
+#include <util/setbaud.h>
+
+#if 0
+static inline void uart_init()
+{
+ UCSRB |= (1 << RXEN) | (1 << TXEN);
+ UCSRC |= (1 << UCSZ0) | (1 << UCSZ1);
+#define BAUD 115200
+#include <util/setbaud.h>
+ UBRRH = UBRRH_VALUE;
+ UBRRL = UBRRL_VALUE;
+#if USE_2X
+ UCSRA |= (1 << U2X);
+#else
+ UCSRA &= ~(1 << U2X);
+#endif
+}
+#endif
+
+void uart_init(void)
+{
+ UBRR0H = UBRRH_VALUE;
+ UBRR0L = UBRRL_VALUE;
+
+#if USE_2X
+ UCSR0A |= _BV(U2X0);
+#else
+ UCSR0A &= ~(_BV(U2X0));
+#endif
+
+ UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */
+ UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */
+}
+
+void uart_putchar(char c)
+{
+ UDR0 = c;
+ loop_until_bit_is_set(UCSR0A, TXC0); /* Wait until transmission ready. */
+}
+
+char uart_getchar(void)
+{
+ loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
+ return UDR0;
+}
+
+#if 0
+/* alternatives for stream uses */
+void uart_putchar(char c, FILE *stream)
+{
+ if (c == '\n') {
+ uart_putchar('\r', stream);
+ }
+ loop_until_bit_is_set(UCSR0A, UDRE0);
+ UDR0 = c;
+}
+
+char uart_getchar(FILE *stream)
+{
+ loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */
+ return UDR0;
+}
+#endif
+
+#if 0
+FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);
+FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ);
+FILE uart_io FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
+#endif