François Gautrais 9 年之前
父节点
当前提交
49d23ede95
共有 14 个文件被更改,包括 550 次插入0 次删除
  1. 38 0
      Makefile
  2. 23 0
      MakefileRpi
  3. 二进制
      attiny85.a
  4. 136 0
      bus.c
  5. 41 0
      bus.h
  6. 23 0
      io.h
  7. 二进制
      lib/attiny85.a
  8. 二进制
      lib/libwiringPi.a
  9. 二进制
      libwiringPi.a
  10. 33 0
      recv.c
  11. 118 0
      send.c
  12. 90 0
      stub.c
  13. 36 0
      timer.c
  14. 12 0
      timer.h

+ 38 - 0
Makefile

@@ -0,0 +1,38 @@
+OBJ=bus.o stub.o timer.o send.o
+TARGET=send
+CC=avr-gcc
+OBJCOPY=$(CROSS_COMPILE)avr-objcopy
+DEVICE=/dev/ttyUSB0 
+
+CFLAGS_ATTINY=-Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=attiny85 -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/home/ptitcois/sketchbook/hardware/tiny/cores/tiny
+CFLAGS=$(CFLAGS_ATTINY)
+
+LDFLAGS_ATINY= -Os -Wl,--gc-sections -mmcu=attiny85 
+LDFLAGS=$(LDFLAGS_ATINY) attiny85.a -Llib
+
+all: $(TARGET).hex $(TARGET).eep
+
+
+$(TARGET).elf: clean $(OBJ) 
+	$(CC) $(OBJ) $(LDFLAGS)  -lm  -o $@
+	make -f MakefileRpi  
+	
+
+$(TARGET).eep: $(TARGET).elf
+	$(OBJCOPY) -O ihex -j .eeprom --set-section-flags=.eeprom=alloc,load --no-change-warnings --change-section-lma .eeprom=0 $< $@ 
+
+
+$(TARGET).hex: $(TARGET).elf
+	$(OBJCOPY) -O ihex -R .eeprom  $< $@ 
+
+upload: $(TARGET).hex
+	avrdude -v -v -v -v -pattiny85 -cstk500v1 -P$(DEVICE) -b19200 -Uflash:w:$<:i 
+	
+%.o: %.c
+	echo compile
+	$(CC) -o $@ -c $<  $(CFLAGS)
+
+
+clean:
+	rm *.o *.d *.elf *.eep *.hex  $(TARGET) || true
+	make -f MakefileRpi clean  

+ 23 - 0
MakefileRpi

@@ -0,0 +1,23 @@
+OBJ=bus.o stub.o timer.o recv.o
+TARGET=recv
+CROSS_COMPILE=/home/ptitcois/Programmation/rpi/tools/arm-bcm2708/arm-bcm2708-linux-gnueabi/bin/arm-bcm2708-linux-gnueabi-
+CC=$(CROSS_COMPILE)gcc
+OBJCOPY=$(CROSS_COMPILE)objcopy
+
+CFLAGS=-I/home/ptitcois/src/wiringPi/wiringPi/
+
+LDFLAGS=-lwiringPi -L/home/ptitcois/src/wiringPi/wiringPi/ -lpthread -Llib
+
+all: $(TARGET)
+
+
+$(TARGET): clean $(OBJ) 
+	$(CC) $(OBJ) $(LDFLAGS)  -lm  -o $@
+	
+
+%.o: %.c
+	$(CC) -o $@ -c $<  $(CFLAGS)
+
+
+clean:
+	@rm *.o   $(TARGET) || true

二进制
attiny85.a


+ 136 - 0
bus.c

@@ -0,0 +1,136 @@
+#include "bus.h"
+#include "timer.h"
+
+
+void file_produce(file_t* f, char x)
+{
+	f->buffer[f->t]=x;
+	f->t=(f->t+1)%1024;
+}
+
+char file_consume(file_t* f)
+{
+	char x;
+	while(f->t==f->q) micro_sleep(10);
+	x=f->buffer[f->q];
+	f->q=(f->q+1)%1024;
+	return x;
+}
+
+int file_size(file_t* f)
+{
+	return (f->t>=f->q)?f->t-f->q:1024-f->q+f->t;
+}
+
+void file_init(file_t* file)
+{
+	file->t=file->q=0;
+}
+
+
+void bus_init_recv(bus_t* bus, int d, int  clk, int ack)
+{
+	bus->data=d;
+	bus->clk=clk;
+	bus->ack=ack;
+	bus->ismaster=0;
+	file_init(&bus->buffer);
+}
+
+
+void bus_init_send(bus_t* bus, int d, int  clk, int ack)
+{
+	bus->data=d;
+	bus->clk=clk;
+	bus->ack=ack;
+	bus->ismaster=1;
+	file_init(&bus->buffer);
+}
+
+
+void _bus_write(bus_t* bus, char x)
+{
+	int i;
+	if(!bus->ismaster) return;
+	for(i=0; i<8; i++)
+	{
+		while(read_pin(bus->ack)) micro_sleep(10);
+		write_pin(bus->data, (x&(1<<i))?1:0);
+		write_pin(bus->clk, 1);
+		while(!read_pin(bus->ack)) micro_sleep(10);
+		write_pin(bus->clk, 0);
+	}
+}
+
+char _bus_read(bus_t* bus)
+{
+	int x=0, i, j=0;
+	if(bus->ismaster) return -1;
+	
+	for(i=0; i<8; i++)
+	{
+		write_pin(bus->ack, 0);
+		while(!read_pin(bus->clk)) micro_sleep(10);
+		j=read_pin(bus->data);
+		x+=j<<i;
+		write_pin(bus->ack, 1);
+		micro_sleep(50);
+		write_pin(bus->ack, 0);
+	}
+	
+	return x;
+}
+
+void* bus_send_thread(void* _bus)
+{
+	bus_t* bus = (bus_t*)_bus;
+	if(!bus->ismaster) return NULL;
+	while(1)
+	{
+		while(!file_size(&bus->buffer)) micro_sleep(10); 
+		_bus_write(bus, file_consume(&bus->buffer));
+	}
+	return NULL;
+}
+
+void* bus_recv_thread(void* _bus)
+{
+	bus_t* bus = (bus_t*)_bus;
+	if(bus->ismaster) return NULL;
+	while(1)
+	{
+		file_produce(&bus->buffer, _bus_read(bus));
+	}
+	return NULL;
+}
+
+void bus_write(bus_t* bus, const void* x, int length)
+{
+	const uint8_t *iter=(const uint8_t*)x;
+	int i;
+	if(!bus->ismaster) return;
+	
+	for(i=0; i<length; i++)
+		file_produce(&bus->buffer, iter[i]);
+}
+
+void bus_read(bus_t* bus, void* x, int length)
+{
+	uint8_t *iter=(uint8_t*)x;
+	int i;
+	if(bus->ismaster) return;
+	for(i=0; i<length; i++)
+	  iter[i]=file_consume(&bus->buffer);
+}
+
+
+void bus_write_sync(bus_t* bus, const void* x, int length)
+{
+	const uint8_t *iter=(const uint8_t*)x;
+	int i;
+	if(!bus->ismaster) return;
+	
+	for(i=0; i<length; i++)
+		_bus_write(bus, iter[i]);
+	
+}

+ 41 - 0
bus.h

@@ -0,0 +1,41 @@
+#ifndef BUS_H
+#define BUS_H
+
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+
+
+
+void write_pin(int pin, int value);
+int read_pin(int pin);
+void init_read_pin(int d, int clk, int ack);
+void init_write_pin(int d, int clk, int ack);
+void free_fifo(int d, int clk, int ack);
+
+typedef struct
+{
+	int t;
+	int q;
+	char buffer[1024];
+} file_t;
+
+typedef struct 
+{
+	int data;
+	int clk;
+	int ack;
+	int ismaster;
+	file_t buffer;
+} bus_t;
+
+void  bus_init_recv		(bus_t* bus, int d, int  clk, int ack);
+void  bus_init_send		(bus_t* bus, int d, int  clk, int ack);
+void* bus_send_thread	(void* _bus);
+void* bus_recv_thread	(void* _bus);
+void  bus_write				(bus_t* bus, const void* x, int length);
+void  bus_write_sync	(bus_t* bus, const void* x, int length);
+void  bus_read				(bus_t* bus, void* x, int length);
+
+#endif

+ 23 - 0
io.h

@@ -0,0 +1,23 @@
+#ifndef IO_H
+#define IO_H
+
+#ifdef __linux__
+	#include <unistd.h>
+	#include <wiringPi.h>
+	#include <unistd.h>
+	#include <sys/types.h>
+	#include <sys/stat.h>
+	#include <sys/types.h>
+	#include <sys/stat.h>
+	#include <fcntl.h>
+	#include <pthread.h>
+	#define ARM 1
+#else
+	#include <Arduino.h>
+	#include <avr/wdt.h>
+	#include <avr/sleep.h>
+	#include <avr/io.h> 
+	#include <avr/interrupt.h> 
+#endif
+
+#endif

二进制
lib/attiny85.a


二进制
lib/libwiringPi.a


二进制
libwiringPi.a


+ 33 - 0
recv.c

@@ -0,0 +1,33 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "bus.h"
+#ifdef __linux__
+	
+#define PIN_DATA 0
+#define PIN_CLK  1
+#define PIN_ACK  2
+
+int main()
+{
+	bus_t bus;
+	pthread_t thread;
+	FILE* file = NULL;
+	init_read_pin(PIN_DATA, PIN_CLK, PIN_ACK);
+	bus_init_recv(&bus, PIN_DATA, PIN_CLK, PIN_ACK);
+	pthread_create(&thread, NULL, bus_recv_thread, &bus);
+	while(1)
+	{
+		uint32_t c;
+		bus_read(&bus, &c, 4);
+		file=fopen("data", "r+");
+		fprintf(file,"%u\n", c);
+		fclose(file);
+	}
+	free_fifo(PIN_DATA, PIN_CLK, PIN_ACK);
+}
+
+
+#endif

+ 118 - 0
send.c

@@ -0,0 +1,118 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "io.h"
+
+#define PIN_DATA 0
+#define PIN_CLK  1
+#define PIN_ACK  2
+#define PIN_IN   3
+#define PIN_LED  4
+
+#include "bus.h"
+
+void init_interrupt()
+{
+	MCUSR = 0;
+	wdt_disable();
+	DDRB &= ~(1<<PB3); /* Set PB0 as input */
+	PORTB |= (1<<PB3); /* Activate PULL UP resistor */ 
+
+	PCMSK |= (1 << PCINT3); /* Enable PCINT0 */
+	GIMSK |= (1 << PCIE); /* Activate interrupt on enabled PCINT7-0 */
+	sei ();
+}
+int main()
+{
+	int  wait=0, state=0;
+	unsigned long last=0;
+	bus_t bus;
+	init();
+	init_write_pin(PIN_DATA,PIN_CLK,PIN_ACK);
+	//init_interrupt();
+	pinMode(PIN_IN, INPUT);
+	pinMode(PIN_LED, OUTPUT);
+	bus_init_send(&bus, PIN_DATA, PIN_CLK, PIN_ACK);
+	while(1)
+	{
+		if(!wait && read_pin(PIN_IN) )
+		{
+			unsigned long val;
+			val=micros()-last;
+			if(val<5000) continue;
+			wait=1;
+			last=micros();
+			//bus_write_sync(&bus, &val, 4);
+			state=(!state)?1:0;
+			digitalWrite(PIN_LED, state);
+		}
+		
+		if(!read_pin(PIN_IN)) wait=0;
+	}
+}
+
+
+
+
+
+
+/*
+ * #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "io.h"
+
+#define PIN_DATA 0
+#define PIN_CLK  1
+#define PIN_ACK  2
+#define PIN_IN   3
+#define PIN_LED  4
+
+#include "bus.h"
+
+void init_interrupt()
+{
+	MCUSR = 0;
+	wdt_disable();
+	DDRB &= ~(1<<PB3); 
+	PORTB |= (1<<PB3); 
+
+	PCMSK |= (1 << PCINT3); 
+	GIMSK |= (1 << PCIE); 
+	sei ();
+}
+
+bus_t bus;
+
+int main()
+{
+	init();
+	init_write_pin(PIN_DATA,PIN_CLK,PIN_ACK);
+	init_interrupt();
+	pinMode(PIN_IN, INPUT);
+	pinMode(PIN_LED, OUTPUT);
+	bus_init_send(&bus, PIN_DATA, PIN_CLK, PIN_ACK);
+	while(1)
+	{
+		//bus_send_thread(&bus);
+	}
+}
+
+unsigned long last=0;
+uint8_t ledstate=0;
+
+ISR (PCINT0_vect){
+   uint8_t x = (PORTB&(1<<PB3))>>PB3;
+   if(x)
+   {
+		 uint32_t val=micros()-last;
+		 last=micros();
+		 bus_write(&bus, &val, 4);
+		 ledstate=(ledstate)?0:1;
+		 digitalWrite(PIN_LED, ledstate);
+	 }
+}
+
+*/
+
+

+ 90 - 0
stub.c

@@ -0,0 +1,90 @@
+#include "bus.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "io.h"
+
+#ifdef __linux__
+	#ifndef ARM
+		int fifos[128];
+	#endif
+#endif
+
+
+void write_pin(int pin, int value)
+{
+	#ifdef __linux__
+		#ifndef ARM
+			char c = (value)?'1':'0';
+			write(fifos[pin], &c, 1);	
+		#else
+			digitalWrite(pin, value);
+		#endif
+	#else
+		digitalWrite(pin, value);
+	#endif
+}
+
+int read_pin(int pin)
+{
+	#ifdef __linux__
+		#ifndef ARM
+			int f = fifos[pin];
+			int c=0;
+			while(read(fifos[pin], &c,1)<=0)usleep(10);
+			return (c=='0')?0:1;
+		#else
+			return digitalRead(pin);
+		#endif
+	#else
+		return digitalRead(pin);
+	#endif
+}
+
+void init_read_pin(int d, int clk, int ack)
+{
+	#ifdef __linux__
+		#ifndef ARM
+			fifos[d]=open("/tmp/fifodata", O_RDONLY | O_NONBLOCK);
+			fifos[clk]=open("/tmp/fifoclk", 	O_RDONLY | O_NONBLOCK);
+			fifos[ack]=open("/tmp/fifoack", 	O_WRONLY );
+		#else
+			pinMode(d, INPUT);
+			pinMode(clk, INPUT);
+			pinMode(ack, OUTPUT); 
+		#endif
+	#else
+		pinMode(d, INPUT);
+		pinMode(clk, INPUT);
+		pinMode(ack, OUTPUT); 
+	#endif
+}
+void init_write_pin(int d, int clk, int ack)
+{
+	#ifdef __linux__
+		#ifndef ARM
+			fifos[d]=open("/tmp/fifoack", 	O_RDONLY  | O_NONBLOCK);
+			fifos[clk]=open("/tmp/fifodata", O_WRONLY);
+			fifos[ack]=open("/tmp/fifoclk", 	O_WRONLY);
+		#else
+			pinMode(d, OUTPUT);
+			pinMode(clk, OUTPUT);
+			pinMode(ack, INPUT);
+		#endif
+	#else
+		pinMode(d, OUTPUT);
+		pinMode(clk, OUTPUT);
+		pinMode(ack, INPUT);
+	#endif
+}
+
+void free_fifo(int d, int clk, int ack)
+{
+	#ifdef __linux__
+		#ifndef ARM
+			close(fifos[d]);
+			close(fifos[clk]);
+			close(fifos[ack]);
+		#endif
+	#endif
+}

+ 36 - 0
timer.c

@@ -0,0 +1,36 @@
+#include "timer.h"
+#include "io.h"
+
+static uint64_t __time_us;
+static uint64_t __delta_time_us;
+
+void micro_sleep(int x)
+{
+	#ifdef __linux__
+		usleep(x);
+	#else
+		unsigned long xx=micros();
+		while(micros()<=xx+x);
+	#endif
+}
+
+void 			timer_init()
+{
+	__time_us=0;
+}
+
+uint64_t 	get_time_us()
+{
+	return micros();
+}
+
+
+void 			set_delta()
+{
+	__delta_time_us=__time_us;
+}
+
+uint64_t 	get_delta()
+{
+	return __time_us-__delta_time_us;
+}

+ 12 - 0
timer.h

@@ -0,0 +1,12 @@
+#ifndef TIMER_H
+#define TIMER_H
+
+#include <stdint.h>
+
+void 			timer_init();
+uint64_t 	get_time_us();
+void 			set_delta();
+uint64_t 	get_delta();
+void 			micro_sleep(int x);
+
+#endif