blink example

This commit is contained in:
MegiDervishi 2020-12-14 20:22:04 +01:00
parent 93e5335332
commit b965402461
10 changed files with 167 additions and 23 deletions

View file

@ -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

61
arduinolib.c Normal file
View file

@ -0,0 +1,61 @@
//#include <math.h>
#include <avr/io.h>
#include <util/delay.h>
#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<<PORTB5);
//PORTB &= ~_BV(PORTB5); //put it to low
return;
}
PORTB |= (1<<PORTB5);
//PORTB |= _BV(PORTB5);
return;
}
DDRB |= _BV(DDB4);
if (v == 0)
{
PORTB &= ~(1<<PORTB4);
//PORTB &= ~_BV(PORTB4); //put it to low
return;
}
PORTB |= (1<<PORTB4);
//PORTB |= _BV(PORTB4);
return;
}
/*void Arduinolib__dwrite_step(int p, int v, Arduinolib__dwrite_out *out)
{
pinMode(p,OUTPUT);
if(v==0)
{
digitalWrite(p,LOW);
//Serial.write("low");
return;
}
digitalWrite(p,HIGH);
//Serial.write("high");
}
*/
/*void Mathext__st_cos_reset(Mathext__st_cos_mem *self)
{
int j;
self->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);
}*/

2
arduinolib.epi Normal file
View file

@ -0,0 +1,2 @@
fun dwrite(p:int; v:bool) returns ()

1
arduinolib.h Normal file
View file

@ -0,0 +1 @@
#include "arduinolib_types.h"

11
arduinolib_types.h Normal file
View file

@ -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

32
led.c
View file

@ -1,20 +1,20 @@
#include <avr/io.h>
#include <util/delay.h>
#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);
}
}

17
main.c Normal file
View file

@ -0,0 +1,17 @@
#include <avr/io.h>
#include <util/delay.h>
#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;
}

4
main.h Normal file
View file

@ -0,0 +1,4 @@
#ifndef MAIN_H
#define MAIN_H
#include "prog_c/prog.h"
#endif

24
prog.ept Normal file
View file

@ -0,0 +1,24 @@
open Arduinolib
node led<<period, ledMax : int; opin : int>>() 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

8
script.sh Normal file
View file

@ -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