From b965402461be6c969b0bae241443228604a547cd Mon Sep 17 00:00:00 2001 From: MegiDervishi Date: Mon, 14 Dec 2020 20:22:04 +0100 Subject: [PATCH] blink example --- Makefile | 30 +++++++++++++++++------ arduinolib.c | 61 ++++++++++++++++++++++++++++++++++++++++++++++ arduinolib.epi | 2 ++ arduinolib.h | 1 + arduinolib_types.h | 11 +++++++++ led.c | 32 ++++++++++++------------ main.c | 17 +++++++++++++ main.h | 4 +++ prog.ept | 24 ++++++++++++++++++ script.sh | 8 ++++++ 10 files changed, 167 insertions(+), 23 deletions(-) create mode 100644 arduinolib.c create mode 100644 arduinolib.epi create mode 100644 arduinolib.h create mode 100644 arduinolib_types.h create mode 100644 main.c create mode 100644 main.h create mode 100644 prog.ept create mode 100644 script.sh diff --git a/Makefile b/Makefile index ddb2760..bcad47a 100644 --- a/Makefile +++ b/Makefile @@ -2,8 +2,12 @@ CC = avr-gcc OBJCOPY = avr-objcopy AVRDUDE = avrdude CFLAGS = -Os -DF_CPU=16000000UL -mmcu=atmega328p -SERIAL = /dev/ttyACM0 -TARGET = led +SERIAL = /dev/cu.usbmodem143401 +ADDR = /Users/megidervishi/.opam/4.10.0/lib/heptagon/c +TARGET = prog +LIB = arduinolib +MAIN = main + .PHONY: all clean flash @@ -13,14 +17,26 @@ flash: $(TARGET).hex $(AVRDUDE) -F -V -c arduino -p ATMEGA328P -P $(SERIAL) -b 115200 \ -U flash:w:$< + $(TARGET).hex: $(TARGET) $(OBJCOPY) -O ihex -R .eeprom $< $@ -$(TARGET): $(TARGET).o - $(CC) $(CFLAGS) -o $@ $< -$(TARGET).o: $(TARGET).c - $(CC) $(CFLAGS) -c -o $@ $< +$(TARGET): $(TARGET).o + $(CC) $(CFLAGS) -o $@ *.o + +$(TARGET).o : $(TARGET).c + $(CC) -g $(CFLAGS) -c -I $(ADDR) -I . $(LIB).c $(TARGET)_c/*.c $(MAIN).c + + +$(TARGET).c: + heptc $(LIB).epi + heptc -target c $(TARGET).ept + clean: - rm -f $(TARGET) $(TARGET).hex $(TARGET).o + rm -f $(TARGET) $(TARGET).hex $(TARGET).o $(TARGET).log $(TARGET).mls + rm -f *.o *.epci + rm -r $(TARGET)_c + + diff --git a/arduinolib.c b/arduinolib.c new file mode 100644 index 0000000..1b2e743 --- /dev/null +++ b/arduinolib.c @@ -0,0 +1,61 @@ +//#include +#include +#include +#include "arduinolib.h" +//#include "HardwareSerial.h" + +void Arduinolib__dwrite_step(int p, int v, Arduinolib__dwrite_out *out){ + /* set pin 5 of PORTB for output. i.e port 13 */ + if (p == 13){ + DDRB |= _BV(DDB5); + if (v == 0) + { + PORTB &= ~(1<i = 0; + for(j = 0; j < 100; ++j) + self->mem[j] = 0.0; +} + +void Mathext__st_cos_step(float a, Mathext__st_cos_out *out, Mathext__st_cos_mem *self) +{ + out->o = self->mem[self->i]; + self->i = (self->i+1) % 100; + self->mem[self->i] = cosf(a); +}*/ diff --git a/arduinolib.epi b/arduinolib.epi new file mode 100644 index 0000000..fc4a16f --- /dev/null +++ b/arduinolib.epi @@ -0,0 +1,2 @@ +fun dwrite(p:int; v:bool) returns () + diff --git a/arduinolib.h b/arduinolib.h new file mode 100644 index 0000000..74ec448 --- /dev/null +++ b/arduinolib.h @@ -0,0 +1 @@ +#include "arduinolib_types.h" diff --git a/arduinolib_types.h b/arduinolib_types.h new file mode 100644 index 0000000..0570697 --- /dev/null +++ b/arduinolib_types.h @@ -0,0 +1,11 @@ +#ifndef ARDUINOLIB_H +#define ARDUINOLIB_H +//#include "Arduino.h" +//#include "WConstants.h" +/* Example of a combinatorial function */ +typedef struct Arduinolib__dwrite { +} Arduinolib__dwrite_out; + +void Arduinolib__dwrite_step(int p, int v,Arduinolib__dwrite_out *o); + +#endif diff --git a/led.c b/led.c index 9d2f67f..9826340 100644 --- a/led.c +++ b/led.c @@ -1,20 +1,20 @@ #include #include +#define BLINK_DELAY_MS 4000 - -int main(void) { - DDRC = 0x01; // initialize port C - - while (1) { - // LED on - PORTC = 0b00000001; // PC0 = High = Vcc - _delay_ms(500); // wait 500 milliseconds - - // LED off - PORTC = 0b00000000; // PC0 = Low = 0v - _delay_ms(500); // wait 500 milliseconds - } - - return 0; -} +int main (void) +{ + /* set pin 5 of PORTB for output*/ + DDRB |= _BV(DDB4); + + while(1) { + /* set pin 5 high to turn led on */ + PORTB |= _BV(PORTB4); + _delay_ms(BLINK_DELAY_MS); + + /* set pin 5 low to turn led off */ + PORTB &= ~_BV(PORTB4); + _delay_ms(BLINK_DELAY_MS); + } +} \ No newline at end of file diff --git a/main.c b/main.c new file mode 100644 index 0000000..6b0d724 --- /dev/null +++ b/main.c @@ -0,0 +1,17 @@ +#include +#include +#include "main.h" + +//#define BLINK_DELAY_MS 4000 + +int main (void) +{ + Prog__main_mem mem; + Prog__main_reset(&mem); + while(1){ + Prog__main_out _res; + Prog__main_step(&_res, &mem); + _delay_ms(1000); +} +return 0; +} \ No newline at end of file diff --git a/main.h b/main.h new file mode 100644 index 0000000..7269ffa --- /dev/null +++ b/main.h @@ -0,0 +1,4 @@ +#ifndef MAIN_H +#define MAIN_H +#include "prog_c/prog.h" +#endif diff --git a/prog.ept b/prog.ept new file mode 100644 index 0000000..e349a16 --- /dev/null +++ b/prog.ept @@ -0,0 +1,24 @@ +open Arduinolib + +node led<>() returns () +var i : int; + ledTime : int; + upt : bool; + downt : bool; + +let + i = 0 fby (i+1); + ledTime = i % period; + upt = ledTime < ledMax; + downt = not upt; + + () = dwrite((opin, true) when upt); + () = dwrite((opin, false) when downt); +tel + +node main() returns () +let + () = led<<4, 2, 13>>(); + () = led<<3, 1, 9>>(); +tel + diff --git a/script.sh b/script.sh new file mode 100644 index 0000000..a2d3e42 --- /dev/null +++ b/script.sh @@ -0,0 +1,8 @@ +heptc arduinolib.epi +heptc -target c prog.ept +gcc -c -I /Users/megidervishi/.opam/4.10.0/lib/heptagon/c -I . arduinolib.c prog_c/*.c + +$(TARGET).c: + heptc $(CFILE).epi + heptc -target c $(HEPT).ept + $(CC) $(CFLAGS) -c -I $(ADDR) -I . $(CFILE).c $(TARGET).c