src/ layout
authorTero Marttila <terom@paivola.fi>
Thu, 03 Apr 2014 18:54:50 +0300
changeset 50 e4ac5a591dcd
parent 49 f01fb659e54d
child 51 ec6271f0637b
src/ layout
.hgignore
Makefile
hello.c
src/hello.c
src/stdlib.h
stdlib.h
--- a/.hgignore	Thu Apr 03 18:49:18 2014 +0300
+++ b/.hgignore	Thu Apr 03 18:54:50 2014 +0300
@@ -3,9 +3,9 @@
 .*.swo
 .*.swp
 
+build/
+
 *.hex
-*.elf
-
 *.s.*
 
 docs/
--- a/Makefile	Thu Apr 03 18:49:18 2014 +0300
+++ b/Makefile	Thu Apr 03 18:54:50 2014 +0300
@@ -2,10 +2,12 @@
 # console
 # dmx
 
-all: $(PROG).hex
+HEX = build/src/$(PROG).hex
+
+all: $(HEX)
 
 build:
-	mkdir -p build/
+	mkdir -p build/src
 
 ## Compiler
 MCU = atmega328p
@@ -17,17 +19,17 @@
 CPPFLAGS = -DF_CPU=$(CPU)UL
 CFLAGS = -g -mmcu=$(MCU) -Os
 
-%.elf: %.c
+build/%.elf: %.c
 	$(CC) $(CPPFLAGS) $(CFLAGS) -o $@ $*.c
 	$(CC) -MM $(CPPFLAGS) -MT $@ -MF build/$*.d $*.c
 
 # existing .d files for rebuilding existing .o's
 -include $(wildcard build/*.d)
 
-%.hex: %.elf
+build/%.hex: build/%.elf
 	avr-objcopy -O ihex -R .eeprom $< $@
 
-.PRECIOUS: %.elf
+.PRECIOUS: build/%.elf
 
 ## Assembler
 AS = avra
@@ -58,7 +60,7 @@
 AD = avrdude
 ADFLAGS = -p $(AD_PART) -c $(AD_PROG) -b $(AD_BAUD) -P $(AD_PORT)
 
-upload: $(PROG).hex
+upload: $(HEX)
 	$(AD) $(ADFLAGS) -U flash:w:$<
 
 ## Console
--- a/hello.c	Thu Apr 03 18:49:18 2014 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,116 +0,0 @@
-#include <avr/io.h>
-#include "stdlib.h"
-
-#include <avr/interrupt.h>
-
-#define TIMER_FLAGS     GPIOR0
-#define TIMER1_BUSY     1
-
-/*
- * Setup timers.
- */
-void timer_init (void)
-{
-    TCCR1A = (
-            // OC1A output pin disconnected
-            0b00 << COM1A0
-            // OC1B output pin disconnected
-        |   0b00 << COM1B0
-            // no PWM
-        |   0b00 << WGM10
-    );
-    TCCR1B = (
-            // CTC mode
-            0b01 << WGM12 
-    );
-    TCCR1C = 0;
-}
-
-void timer1_start (short cycles)
-{
-    // count up from zero...
-    TCNT1 = 0;
-
-    // ...to the given number of timer cycles
-    OCR1A = cycles;
-
-    // start!
-    sbi(&TIMER_FLAGS, TIMER1_BUSY);
-    TIMSK1 = (
-            // OCA interrupt
-            0b1 << OCIE1A       // enable
-    );
-    TCCR1B = (
-            // WGM
-            0b01  << WGM12      // CTC
-
-            // clocksource
-        |   0b101 << CS10       // 1024'th
-    );
-}
-
-static void timer1_stop ()
-{
-    // WGM: normal
-    // clocksource: stop
-    TCCR1B = 0;
-
-    cbi(&TIMER_FLAGS, TIMER1_BUSY);
-}
-
-ISR(TIMER1_COMPA_vect)
-{
-    timer1_stop();
-
-    // XXX: cpu will automatically wake up from sleep()
-}
-
-/*
- * Sleep on timer1 interrupt.
- */
-void timer_sleep (int cycles)
-{
-    // set timer
-    timer1_start(cycles);
-
-    // sleep
-    // TODO: PRR
-    SMCR = (
-            // idle sleep
-            (0b00 << SM0)
-
-            // enable sleep
-        |   (0b1 << SE)
-    );
-    
-    // sleep
-    while (tbi(&TIMER_FLAGS, TIMER1_BUSY)) {
-        __asm__ ( "sleep" :: );
-    }
-
-    // cleanup
-    SMCR = 0;
-}
-
-int main (void)
-{
-    timer_init();
-
-    // LED
-    sbi(&DDRB, DDB5);
-
-    sei();
-
-    // blink
-    short timeout = 1000;
-    short delta = 10;
-
-    while (true) {
-        // bitflip
-        xbi(&PORTB, PORTB5);
-        
-        timer_sleep(timeout);
-        
-        timeout += delta;
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/hello.c	Thu Apr 03 18:54:50 2014 +0300
@@ -0,0 +1,116 @@
+#include <avr/io.h>
+#include "stdlib.h"
+
+#include <avr/interrupt.h>
+
+#define TIMER_FLAGS     GPIOR0
+#define TIMER1_BUSY     1
+
+/*
+ * Setup timers.
+ */
+void timer_init (void)
+{
+    TCCR1A = (
+            // OC1A output pin disconnected
+            0b00 << COM1A0
+            // OC1B output pin disconnected
+        |   0b00 << COM1B0
+            // no PWM
+        |   0b00 << WGM10
+    );
+    TCCR1B = (
+            // CTC mode
+            0b01 << WGM12 
+    );
+    TCCR1C = 0;
+}
+
+void timer1_start (short cycles)
+{
+    // count up from zero...
+    TCNT1 = 0;
+
+    // ...to the given number of timer cycles
+    OCR1A = cycles;
+
+    // start!
+    sbi(&TIMER_FLAGS, TIMER1_BUSY);
+    TIMSK1 = (
+            // OCA interrupt
+            0b1 << OCIE1A       // enable
+    );
+    TCCR1B = (
+            // WGM
+            0b01  << WGM12      // CTC
+
+            // clocksource
+        |   0b101 << CS10       // 1024'th
+    );
+}
+
+static void timer1_stop ()
+{
+    // WGM: normal
+    // clocksource: stop
+    TCCR1B = 0;
+
+    cbi(&TIMER_FLAGS, TIMER1_BUSY);
+}
+
+ISR(TIMER1_COMPA_vect)
+{
+    timer1_stop();
+
+    // XXX: cpu will automatically wake up from sleep()
+}
+
+/*
+ * Sleep on timer1 interrupt.
+ */
+void timer_sleep (int cycles)
+{
+    // set timer
+    timer1_start(cycles);
+
+    // sleep
+    // TODO: PRR
+    SMCR = (
+            // idle sleep
+            (0b00 << SM0)
+
+            // enable sleep
+        |   (0b1 << SE)
+    );
+    
+    // sleep
+    while (tbi(&TIMER_FLAGS, TIMER1_BUSY)) {
+        __asm__ ( "sleep" :: );
+    }
+
+    // cleanup
+    SMCR = 0;
+}
+
+int main (void)
+{
+    timer_init();
+
+    // LED
+    sbi(&DDRB, DDB5);
+
+    sei();
+
+    // blink
+    short timeout = 1000;
+    short delta = 10;
+
+    while (true) {
+        // bitflip
+        xbi(&PORTB, PORTB5);
+        
+        timer_sleep(timeout);
+        
+        timeout += delta;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/stdlib.h	Thu Apr 03 18:54:50 2014 +0300
@@ -0,0 +1,32 @@
+#include <util/delay.h>
+
+#define false       0
+#define true        1
+
+typedef volatile uint8_t ioport_t;
+
+static inline ioport_t tbi(ioport_t *port, int bit)
+{
+    return *port & (1 << bit);
+}
+
+static inline void sbi(ioport_t *port, int bit)
+{
+    *port |= (1 << bit);
+}
+
+static inline void cbi(ioport_t *port, int bit)
+{
+    *port &= ~(1 << bit);
+}
+
+static inline void xbi(ioport_t *port, int bit)
+{
+    *port ^= (1 << bit);
+}
+
+void delay_1s ()
+{
+    // busyloop
+    _delay_ms(1000);
+}
--- a/stdlib.h	Thu Apr 03 18:49:18 2014 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,32 +0,0 @@
-#include <util/delay.h>
-
-#define false       0
-#define true        1
-
-typedef volatile uint8_t ioport_t;
-
-static inline ioport_t tbi(ioport_t *port, int bit)
-{
-    return *port & (1 << bit);
-}
-
-static inline void sbi(ioport_t *port, int bit)
-{
-    *port |= (1 << bit);
-}
-
-static inline void cbi(ioport_t *port, int bit)
-{
-    *port &= ~(1 << bit);
-}
-
-static inline void xbi(ioport_t *port, int bit)
-{
-    *port ^= (1 << bit);
-}
-
-void delay_1s ()
-{
-    // busyloop
-    _delay_ms(1000);
-}