initial firmware working

Fri, 12 May 2017 15:33:22 +0200

author
Malte Di Donato <mdd@neo-soft.org>
date
Fri, 12 May 2017 15:33:22 +0200
changeset 0
bdcf2c76d86e
child 1
d224ff5c155b

initial firmware working

firmware/Makefile file | annotate | diff | comparison | revisions
firmware/main.c file | annotate | diff | comparison | revisions
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/firmware/Makefile	Fri May 12 15:33:22 2017 +0200
@@ -0,0 +1,80 @@
+# Name: Makefile
+# Project: hid-data example
+# Author: Christian Starkjohann
+# Creation Date: 2008-04-07
+# Tabsize: 4
+# Copyright: (c) 2008 by OBJECTIVE DEVELOPMENT Software GmbH
+# License: GNU GPL v2 (see License.txt), GNU GPL v3 or proprietary (CommercialLicense.txt)
+
+DEVICE  = attiny2313
+F_CPU   = 8000000	# in Hz
+AVRDUDE = avrdude -c usbasp -p $(DEVICE) -B .5 -P USB -u
+
+CFLAGS  = -I. -DDEBUG_LEVEL=0
+OBJECTS = main.o
+
+COMPILE = avr-gcc -Wall -Os -DF_CPU=$(F_CPU) $(CFLAGS) -mmcu=$(DEVICE)
+
+FUSE_H = 0x9f
+FUSE_L = 0xe4
+
+# symbolic targets:
+help:
+	@echo "This Makefile has no default rule. Use one of the following:"
+	@echo "make hex ....... to build main.hex"
+	@echo "make program ... to flash fuses and firmware"
+	@echo "make fuse ...... to flash the fuses"
+	@echo "make flash ..... to flash the firmware (use this on metaboard)"
+	@echo "make clean ..... to delete objects and hex file"
+
+hex: main.hex
+
+program: flash fuse
+
+# rule for programming fuse bits:
+fuse:
+	@[ "$(FUSE_H)" != "" -a "$(FUSE_L)" != "" ] || \
+		{ echo "*** Edit Makefile and choose values for FUSE_L and FUSE_H!"; exit 1; }
+	$(AVRDUDE) -U hfuse:w:$(FUSE_H):m -U lfuse:w:$(FUSE_L):m
+
+# rule for uploading firmware:
+flash: main.hex
+	$(AVRDUDE) -U flash:w:main.hex:i
+
+# rule for deleting dependent files (those which can be built by Make):
+clean:
+	rm -f main.hex main.lst main.obj main.cof main.list main.map main.eep.hex main.elf *.o main.s 
+
+# Generic rule for compiling C files:
+.c.o:
+	$(COMPILE) -c $< -o $@
+
+# Generic rule for assembling Assembler source files:
+.S.o:
+	$(COMPILE) -x assembler-with-cpp -c $< -o $@
+# "-x assembler-with-cpp" should not be necessary since this is the default
+# file type for the .S (with capital S) extension. However, upper case
+# characters are not always preserved on Windows. To ensure WinAVR
+# compatibility define the file type manually.
+
+# Generic rule for compiling C to assembler, used for debugging only.
+.c.s:
+	$(COMPILE) -S $< -o $@
+
+# file targets:
+
+main.elf: $(OBJECTS)	# usbdrv dependency only needed because we copy it
+	$(COMPILE) -o main.elf $(OBJECTS)
+
+main.hex: main.elf
+	rm -f main.hex main.eep.hex
+	avr-objcopy -j .text -j .data -O ihex main.elf main.hex
+	avr-size main.hex
+
+# debugging targets:
+
+disasm:	main.elf
+	avr-objdump -d main.elf
+
+cpp:
+	$(COMPILE) -E main.c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/firmware/main.c	Fri May 12 15:33:22 2017 +0200
@@ -0,0 +1,125 @@
+#include <avr/io.h>
+#include <util/delay.h>
+#include <stdlib.h>
+
+uint8_t mode;
+
+void entprellung( volatile uint8_t *port, uint8_t maske ) {
+  uint8_t   port_puffer;
+  uint8_t   entprellungs_puffer;
+
+  for( entprellungs_puffer=0 ; entprellungs_puffer!=0xff ; ) {
+    entprellungs_puffer<<=1;
+    port_puffer = *port;
+    _delay_us(150);
+    if( (*port & maske) == (port_puffer & maske) )
+      entprellungs_puffer |= 0x01;
+  }
+}
+
+void led(uint8_t n) {
+    // first clear all leds 
+    PORTB &= 0b00011101;
+
+    // enable selected led
+    switch (n) {
+        case 1:
+            PORTB |= _BV(1);
+            break;
+        case 2:
+            PORTB |= _BV(5);
+            break;
+        case 3:
+            PORTB |= _BV(6);
+            break;
+        case 4:
+            PORTB |= _BV(7);
+            break;
+    }
+
+}
+
+
+void init(void) {
+    // initialize LED and FET pins
+    DDRB = 0b11101011;
+
+    // DEVELOP: ENABLE PULL-UP ON AVR
+    //PORTD |= ( 1 << PD2 );
+
+
+    TCCR1A = (1<<WGM10)|(1<<COM1A1)   // Set up the two Control registers of Timer1.
+            |(1<<COM1B1);             // Wave Form Generation is Fast PWM 8 Bit,
+    TCCR1B = (1<<WGM12)     // OC1A and OC1B are cleared on compare match
+            |(1<<CS10);               // and set at BOTTOM. Clock Prescaler is 1024.
+
+
+    uint8_t i;
+    for(i = 0; i<5; i++) {
+        led(i);
+        _delay_ms(50);
+    }
+
+    for(i = 4; i>mode; i--) {
+        led(i);
+        _delay_ms(50);
+    }
+
+    led(mode);
+    pwm_update();
+}
+
+void pwm_update(void) {
+    PORTB |= _BV(0); // switch on led driver
+
+    switch (mode) {
+        case 1: 
+            OCR1A = 63;                       // Dutycycle of OC1A = 25%
+            break;
+        case 2: 
+            OCR1A = 127;                       // Dutycycle of OC1A = 50%
+            break;
+        case 3: 
+            OCR1A = 189;                       // Dutycycle of OC1A = 75%
+            break;
+        case 4: 
+            OCR1A = 255;                       // Dutycycle of OC1A = 100%
+            break;
+        default:
+            // switch off pwm and driver
+            OCR1A = 0;
+            PORTB &= _BV(0);
+    }
+}
+
+
+void main(void) {
+
+    mode = 1;
+    uint8_t key_last = _BV(2);
+    uint8_t tmp;
+
+    init();
+
+    while (1) {                /* main event loop */
+
+        entprellung( &PIND, _BV(2) );
+        tmp = PIND & _BV(2);
+        if (tmp != key_last) {
+            key_last = tmp;
+            if (tmp) {
+                if (mode < 4) {
+                    mode++;
+                } else {
+                    mode = 1;
+                }
+                led(mode);
+                pwm_update();
+                _delay_ms(50);
+                _delay_ms(50);
+            }
+        }
+
+    }
+
+}
\ No newline at end of file

mercurial