François Gautrais 9 年 前
コミット
d0c625ea3f
9 ファイル変更179 行追加33 行削除
  1. 3 1
      MakefileRpi
  2. 6 2
      bus.c
  3. 1 1
      bus.h
  4. 34 0
      io.h
  5. 45 9
      recv.c
  6. 25 6
      send.c
  7. 49 8
      stub.c
  8. 14 4
      timer.c
  9. 2 2
      timer.h

+ 3 - 1
MakefileRpi

@@ -4,7 +4,7 @@ CROSS_COMPILE=/home/ptitcois/Programmation/rpi/tools/arm-bcm2708/arm-bcm2708-lin
 CC=$(CROSS_COMPILE)gcc
 OBJCOPY=$(CROSS_COMPILE)objcopy
 
-CFLAGS=-I/home/ptitcois/src/wiringPi/wiringPi/
+CFLAGS=-I/home/ptitcois/src/wiringPi/wiringPi/ -g
 
 LDFLAGS=-lwiringPi -L/home/ptitcois/src/wiringPi/wiringPi/ -lpthread -Llib
 
@@ -18,6 +18,8 @@ $(TARGET): clean $(OBJ)
 %.o: %.c
 	$(CC) -o $@ -c $<  $(CFLAGS)
 
+upload: $(TARGET)
+	scp $(TARGET) pi@10.42.0.247:/home/pi/compteur
 
 clean:
 	@rm *.o   $(TARGET) || true

+ 6 - 2
bus.c

@@ -35,6 +35,11 @@ void bus_init_recv(bus_t* bus, int d, int  clk, int ack)
 	bus->ack=ack;
 	bus->ismaster=0;
 	file_init(&bus->buffer);
+	if (wiringPiSetup() == -1)
+	{
+		perror("Unable to setup wiring pi\n");
+    exit (1);
+  }else printf("WiringPi successfully setup\n");
 }
 
 
@@ -51,6 +56,7 @@ void bus_init_send(bus_t* bus, int d, int  clk, int ack)
 void _bus_write(bus_t* bus, char x)
 {
 	int i;
+	unsigned long ref= time_micros();
 	if(!bus->ismaster) return;
 	for(i=0; i<8; i++)
 	{
@@ -66,7 +72,6 @@ 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);
@@ -77,7 +82,6 @@ char _bus_read(bus_t* bus)
 		micro_sleep(50);
 		write_pin(bus->ack, 0);
 	}
-	
 	return x;
 }
 

+ 1 - 1
bus.h

@@ -4,7 +4,7 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdint.h>
-
+#define BUS_TIMEOUT 1000000
 
 
 

+ 34 - 0
io.h

@@ -12,6 +12,40 @@
 	#include <fcntl.h>
 	#include <pthread.h>
 	#define ARM 1
+	#define BCM2708_PERI_BASE        0x20000000
+	#define GPIO_BASE                (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
+ 
+ 
+	#include <stdio.h>
+	#include <stdlib.h>
+	#include <fcntl.h>
+	#include <sys/mman.h>
+	#include <unistd.h>
+	 
+	#define PAGE_SIZE (4*1024)
+	#define BLOCK_SIZE (4*1024)
+	 
+
+	extern int  mem_fd;
+	extern void *gpio_map;
+	 
+	// I/O access
+	extern volatile unsigned *gpio;
+	 
+	// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
+	#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
+	#define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))
+	#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
+	 
+	#define GPIO_SET *(gpio+7)  // sets   bits which are 1 ignores bits which are 0
+	#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
+	#define GPIO_WRITE(gpio, value) ((value)?(GPIO_SET = 1 << gpio):(GPIO_CLR = 1 << gpio)) 
+	 
+	#define GET_GPIO(g) ((*(gpio+13)&(1<<g))?1:0) // 0 if LOW, (1<<g) if HIGH
+	 
+	#define GPIO_PULL *(gpio+37) // Pull up/pull down
+	#define GPIO_PULLCLK0 *(gpio+38) // Pull up/pull down clock
+	void setup_io();
 #else
 	#include <Arduino.h>
 	#include <avr/wdt.h>

+ 45 - 9
recv.c

@@ -5,28 +5,64 @@
 
 #include "bus.h"
 #ifdef __linux__
-	
-#define PIN_DATA 0
-#define PIN_CLK  1
-#define PIN_ACK  2
+
+#define PIN_DATA 2
+#define PIN_CLK  3
+#define PIN_ACK  4
+int data[3]={0};
+#include "io.h"
+
+
+
 
 int main()
 {
 	bus_t bus;
 	pthread_t thread;
 	FILE* file = NULL;
-	init_read_pin(PIN_DATA, PIN_CLK, PIN_ACK);
+	setup_io();
 	bus_init_recv(&bus, PIN_DATA, PIN_CLK, PIN_ACK);
+	init_read_pin(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);
+		char buffer[16];
+		bus_read(&bus, buffer, 5);
+		buffer[5]=0;
+		//file=fopen("data", "r+");
+		printf("'%s'\n", buffer);
+		//fprintf(file,"%u\n", c);
+		//fclose(file);
 	}
+	/*printf("Strat\n");
+	while(1)
+	{
+		int i;
+		for(i=2; i<5; i++)
+		{
+			int a = read_pin(i);
+			if(a!=data[i])
+			{
+				printf("pin[%d] : %d -> %d\n", i, data[i], a);
+				data[i]=a;
+			}else printf("pin[%d] : %d \n", i, a);
+			
+		}
+		sleep(1);
+	}*/
 	free_fifo(PIN_DATA, PIN_CLK, PIN_ACK);
+	/*int i;
+	setup_io();
+	INP_GPIO(2);
+	INP_GPIO(3);
+	while(1)
+	{
+		printf("%d ", GET_GPIO(2));
+		printf("%d ", GET_GPIO(3));
+		printf("\n");
+		sleep(1);
+	}*/
 }
 
 

+ 25 - 6
send.c

@@ -3,13 +3,14 @@
 #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
+#define PIN_DATA 1
+#define PIN_CLK  2
+#define PIN_ACK  0
+#define PIN_IN   4
+#define PIN_LED  3
 
 #include "bus.h"
+#include "timer.h"
 
 void init_interrupt()
 {
@@ -33,7 +34,7 @@ int main()
 	pinMode(PIN_IN, INPUT);
 	pinMode(PIN_LED, OUTPUT);
 	bus_init_send(&bus, PIN_DATA, PIN_CLK, PIN_ACK);
-	while(1)
+	/*while(1)
 	{
 		if(!wait && read_pin(PIN_IN) )
 		{
@@ -48,6 +49,24 @@ int main()
 		}
 		
 		if(!read_pin(PIN_IN)) wait=0;
+	}*/
+	
+	pinMode(PIN_IN, INPUT);
+	int i;
+	/*for(i=0; i<3; i++)
+	{
+		digitalWrite(PIN_CLK, 0);
+		micro_sleep(500000);
+		
+		digitalWrite(PIN_CLK, 1);
+		micro_sleep(500000);
+		digitalWrite(PIN_CLK, 0);
+	}*/
+	while(1)
+	{
+		bus_write_sync(&bus, "Salut", 5);
+		delay(250);
+		//digitalWrite(PIN_DATA, digitalRead(PIN_ACK));
 	}
 }
 

+ 49 - 8
stub.c

@@ -11,6 +11,47 @@
 #endif
 
 
+#ifdef ARM
+
+	int  mem_fd;
+	void *gpio_map;
+	 
+	// I/O access
+	volatile unsigned *gpio;
+	
+	void setup_io()
+	{
+		 /* open /dev/mem */
+		 if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
+				printf("can't open /dev/mem \n");
+				exit(-1);
+		 }
+	 
+		 /* mmap GPIO */
+		 gpio_map = mmap(
+				NULL,             //Any adddress in our space will do
+				BLOCK_SIZE,       //Map length
+				PROT_READ|PROT_WRITE,// Enable reading & writting to mapped memory
+				MAP_SHARED,       //Shared with other processes
+				mem_fd,           //File to map
+				GPIO_BASE         //Offset to GPIO peripheral
+		 );
+	 
+		 close(mem_fd); //No need to keep mem_fd open after mmap
+	 
+		 if (gpio_map == MAP_FAILED) {
+				printf("mmap error %d\n", (int)gpio_map);//errno also set!
+				exit(-1);
+		 }
+	 
+		 // Always use volatile pointer!
+		 gpio = (volatile unsigned *)gpio_map;
+	 
+	 
+	} // setup_io
+	
+#endif
+
 void write_pin(int pin, int value)
 {
 	#ifdef __linux__
@@ -18,7 +59,7 @@ void write_pin(int pin, int value)
 			char c = (value)?'1':'0';
 			write(fifos[pin], &c, 1);	
 		#else
-			digitalWrite(pin, value);
+			GPIO_WRITE(pin, value);
 		#endif
 	#else
 		digitalWrite(pin, value);
@@ -34,7 +75,7 @@ int read_pin(int pin)
 			while(read(fifos[pin], &c,1)<=0)usleep(10);
 			return (c=='0')?0:1;
 		#else
-			return digitalRead(pin);
+			return GET_GPIO(pin);
 		#endif
 	#else
 		return digitalRead(pin);
@@ -49,9 +90,9 @@ void init_read_pin(int d, int clk, int ack)
 			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); 
+			INP_GPIO(d);
+			INP_GPIO(clk);
+			OUT_GPIO(ack); 
 		#endif
 	#else
 		pinMode(d, INPUT);
@@ -67,9 +108,9 @@ void init_write_pin(int d, int clk, int ack)
 			fifos[clk]=open("/tmp/fifodata", O_WRONLY);
 			fifos[ack]=open("/tmp/fifoclk", 	O_WRONLY);
 		#else
-			pinMode(d, OUTPUT);
-			pinMode(clk, OUTPUT);
-			pinMode(ack, INPUT);
+			OUT_GPIO(d);
+			OUT_GPIO(clk);
+			INP_GPIO(ack);
 		#endif
 	#else
 		pinMode(d, OUTPUT);

+ 14 - 4
timer.c

@@ -4,13 +4,23 @@
 static uint64_t __time_us;
 static uint64_t __delta_time_us;
 
-void micro_sleep(int x)
+
+unsigned long time_micros()
+{
+	#ifdef __linux__
+		return 0;
+	#else
+		return millis()*1000;
+	#endif
+}
+
+
+void micro_sleep(unsigned long x)
 {
 	#ifdef __linux__
 		usleep(x);
 	#else
-		unsigned long xx=micros();
-		while(micros()<=xx+x);
+		delay(x/1000);
 	#endif
 }
 
@@ -21,7 +31,7 @@ void 			timer_init()
 
 uint64_t 	get_time_us()
 {
-	return micros();
+	return millis()*1000;
 }
 
 

+ 2 - 2
timer.h

@@ -7,6 +7,6 @@ void 			timer_init();
 uint64_t 	get_time_us();
 void 			set_delta();
 uint64_t 	get_delta();
-void 			micro_sleep(int x);
-
+void 			micro_sleep(unsigned long x);
+unsigned long time_micros();
 #endif