SOCKET-CAN support added. Many thanks to Jan Kiszka !
authoretisserant
Thu, 31 Aug 2006 10:28:53 +0200
changeset 47 8a1047ab51f4
parent 46 a3b169324368
child 48 adc6572caf5d
SOCKET-CAN support added. Many thanks to Jan Kiszka !
configure
drivers/can_socket/Makefile.in
drivers/can_socket/can_socket.c
drivers/timers_xeno/timers_xeno.c
include/can_socket/cancfg.h
--- a/configure	Thu Aug 10 17:57:33 2006 +0200
+++ b/configure	Thu Aug 31 10:28:53 2006 +0200
@@ -479,9 +479,10 @@
 fi
 
 if [ "$SUB_TIMERS_DRIVER" = "xeno" ]; then
-	SUB_EXE_CFLAGS=$SUB_EXE_CFLAGS\ -lnative\
+	SUB_EXE_CFLAGS=$SUB_EXE_CFLAGS\ -lnative\ -lrtdm\
 \	-L`$XENO_CONFIG --library-dir`
 	SUB_PROG_CFLAGS=$SUB_PROG_CFLAGS\ `$XENO_CONFIG --xeno-cflags`
+	RTCAN_SOCKET=1
 fi
 
 if [ "$SUB_TIMERS_DRIVER" = "none" ]; then
@@ -624,7 +625,8 @@
  CANOPEN_LITTLE_ENDIAN\
  US_TO_TIMEVAL_FACTOR\
  TIMEVAL\
- TIMEVAL_MAX; do
+ TIMEVAL_MAX\
+ RTCAN_SOCKET; do
 if [ "${!i}" = "" ]; then
 echo "// $i is not defined" >> include/config.h
 else
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/can_socket/Makefile.in	Thu Aug 31 10:28:53 2006 +0200
@@ -0,0 +1,58 @@
+#! gmake
+
+#
+# Copyright (C) 2006 Laurent Bessard
+# 
+# This file is part of canfestival, a library implementing the canopen
+# stack
+# 
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License as published by the Free Software Foundation; either
+# version 2.1 of the License, or (at your option) any later version.
+# 
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+# 
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+# 
+
+CC = SUB_CC
+OPT_CFLAGS = -O2
+CFLAGS = SUB_OPT_CFLAGS
+PROG_CFLAGS = SUB_PROG_CFLAGS
+PREFIX = SUB_PREFIX
+TARGET = SUB_TARGET
+CAN_DRIVER = SUB_CAN_DRIVER
+TIMERS_DRIVER = SUB_TIMERS_DRIVER
+
+INCLUDES = -I../../include -I../../include/$(TARGET) -I../../include/$(CAN_DRIVER) -I../../include/$(TIMERS_DRIVER)
+
+OBJS = $(CAN_DRIVER).o
+
+SRC_HFILES = ../../include/$(CAN_DRIVER)/cancfg.h
+
+TARGET_HFILES = $(PREFIX)/include/canfestival/cancfg.h
+
+all: driver
+
+driver: $(OBJS)
+
+%o: %c
+       $(CC) $(CFLAGS) $(PROG_CFLAGS) ${PROGDEFINES} $(INCLUDES) -o $@ -c $<
+
+install:
+       mkdir -p $(PREFIX)/include/canfestival
+       cp $(SRC_HFILES) $(PREFIX)/include/canfestival
+
+uninstall:
+       rm -f $(TARGET_HFILES)
+
+clean:
+       -\rm $(OBJS)
+
+mrproper: clean
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/drivers/can_socket/can_socket.c	Thu Aug 31 10:28:53 2006 +0200
@@ -0,0 +1,178 @@
+/*
+This file is part of CanFestival, a library implementing CanOpen Stack.
+
+Copyright (C): Edouard TISSERANT and Francis DUPIN
+
+See COPYING file for copyrights details.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stddef.h> /* for NULL */
+
+#include "config.h"
+
+#ifdef RTCAN_SOCKET
+#include "rtdm/rtcan.h"
+#define CAN_IFNAME     "rtcan%s"
+#define CAN_SOCKET     rt_dev_socket
+#define CAN_CLOSE      rt_dev_close
+#define CAN_RECV       rt_dev_recv
+#define CAN_SEND       rt_dev_send
+#define CAN_BIND       rt_dev_bind
+#define CAN_IOCTL      rt_dev_ioctl
+#else
+#include "af_can.h"
+#define CAN_IFNAME     "can%s"
+#define CAN_SOCKET     socket
+#define CAN_CLOSE      close
+#define CAN_RECV       recv
+#define CAN_SEND       send
+#define CAN_BIND       bind
+#define CAN_IOCTL      ioctl
+#endif
+
+struct CANPort;
+#define CAN_HANDLE struct CANPort *
+
+#include <applicfg.h>
+
+#include "timer.h"
+#include "can_driver.h"
+#include "timers_driver.h"
+
+typedef struct CANPort {
+       int fd;
+       TASK_HANDLE receiveTask;
+       CO_Data* d;
+} CANPort;
+
+/*********functions which permit to communicate with the board****************/
+UNS8 canReceive(CAN_HANDLE fd0, Message *m)
+{
+       int res;
+       struct can_frame frame;
+
+       res = CAN_RECV(fd0->fd, &frame, sizeof(frame), 0);
+       if (res < 0)
+               return 1;
+
+       m->cob_id.w = frame.can_id & CAN_EFF_MASK;
+       m->len      = frame.can_dlc;
+       if (frame.can_id & CAN_RTR_FLAG)
+               m->rtr = 1;
+       else
+               m->rtr = 0;
+       memcpy(m->data, frame.data, 8);
+
+       return 0;
+}
+
+void canReceiveLoop(CAN_HANDLE fd0)
+{
+       CO_Data* d = fd0->d;
+       Message m;
+
+       while (1) {
+               if (canReceive(fd0, &m) != 0)
+                       break;
+
+               EnterMutex();
+               canDispatch(d, &m);
+               LeaveMutex();
+       }
+}
+
+/***************************************************************************/
+UNS8 canSend(CAN_HANDLE fd0, Message *m)
+{
+       int res;
+       struct can_frame frame;
+
+       frame.can_id = m->cob_id.w;
+       if (frame.can_id >= 0x800)
+               frame.can_id |= CAN_EFF_FLAG;
+       frame.can_dlc = m->len;
+       if (m->rtr)
+               frame.can_id |= CAN_RTR_FLAG;
+       else
+               memcpy(frame.data, m->data, 8);
+
+       res = CAN_SEND(fd0->fd, &frame, sizeof(frame), 0);
+       if (res < 0)
+               return 1;
+
+       return 0;
+}
+
+/***************************************************************************/
+CAN_HANDLE canOpen(s_BOARD *board)
+{
+       CAN_HANDLE fd0;
+       struct ifreq ifr;
+       struct sockaddr_can addr;
+       int err;
+
+       fd0 = malloc(sizeof(*fd0));
+       if (!fd0)
+               return NULL;
+
+       fd0->fd = CAN_SOCKET(PF_CAN, SOCK_RAW, 0);
+       if(fd0->fd < 0){
+               fprintf(stderr,"Socket creation failed.\n");
+               goto error_ret;
+       }
+
+       snprintf(ifr.ifr_name, IFNAMSIZ, CAN_IFNAME, board->busname);
+       err = CAN_IOCTL(fd0->fd, SIOCGIFINDEX, &ifr);
+       if (err) {
+               fprintf(stderr, "Unknown device: %s\n", ifr.ifr_name);
+               goto error_close;
+       }
+
+       addr.can_family  = AF_CAN;
+       addr.can_ifindex = ifr.ifr_ifindex;
+       err = CAN_BIND(fd0->fd, (struct sockaddr *)&addr,
+                             sizeof(addr));
+       if (err) {
+               fprintf(stderr, "Binding failed.\n");
+               goto error_close;
+       }
+
+       fd0->d = board->d;
+       CreateReceiveTask(fd0, &fd0->receiveTask);
+       return fd0;
+
+ error_close:
+       CAN_CLOSE(fd0->fd);
+
+ error_ret:
+       free(fd0);
+       return NULL;
+}
+
+/***************************************************************************/
+int canClose(CAN_HANDLE fd0)
+{
+       if (fd0) {
+               WaitReceiveTaskEnd(&fd0->receiveTask);
+               CAN_CLOSE(fd0->fd);
+               free(fd0);
+       }
+       return 0;
+}
--- a/drivers/timers_xeno/timers_xeno.c	Thu Aug 10 17:57:33 2006 +0200
+++ b/drivers/timers_xeno/timers_xeno.c	Thu Aug 31 10:28:53 2006 +0200
@@ -1,5 +1,6 @@
 #include <stdlib.h>
-#include <unistd.h> 
+#include <unistd.h>
+#include <sys/mman.h>
 
 #include <native/task.h>
 #include <native/timer.h>
@@ -14,7 +15,6 @@
 
 RT_MUTEX CanFestival_mutex;
 RT_TASK timerloop_task;
-RT_ALARM timerloop_alarm;
 RTIME last_time_read;
 RTIME last_occured_alarm;
 RTIME last_alarm_set;
@@ -24,7 +24,6 @@
 void cleanup_all(void)
 {
 	rt_task_delete(&timerloop_task);
-	rt_alarm_delete(&timerloop_alarm);
 }
 void StopTimerLoop(void)
 {
@@ -62,15 +61,13 @@
 {
 	int ret;
 	stop_timer = 0;
+	char taskname[32];
+	snprintf(taskname, sizeof(taskname), "timerloop-%d", getpid());
 
-	ret = rt_alarm_create( &timerloop_alarm, NULL);
-	if (ret) {
-		printf("Failed to create timerloop_alarm, code %d\n",errno);
-		goto error;
-	}
-	
+	mlockall(MCL_CURRENT | MCL_FUTURE);
+
 	//create timerloop_task
-	ret = rt_task_create(&timerloop_task,"timerloop",0,50,0);
+	ret = rt_task_create(&timerloop_task, taskname, 0, 50, 0);
 	if (ret) {
 		printf("Failed to create timerloop_task, code %d\n",errno);
 		return;
@@ -102,10 +99,12 @@
 {
 	int ret;
 	static int id = 0;
-	char taskname[64];
-	sprintf(taskname,"canloop%d",id);
+	char taskname[32];
+	snprintf(taskname, sizeof(taskname), "canloop%d-%d", id, getpid());
 	id++;
 
+	mlockall(MCL_CURRENT | MCL_FUTURE);
+
 	//create timerloop_task
 	ret = rt_task_create(ReceiveLoop_task,taskname,0,50,0);
 	if (ret) {
@@ -125,9 +124,8 @@
 	rt_task_delete(Thread);
 }
 
-//#define max(a,b) a>b?a:b
 void setTimer(TIMEVAL value)
-{	
+{
 	last_alarm_set = (value == TIMEVAL_MAX) ? TIMEVAL_MAX : last_time_read + value;
 	rt_task_unblock(&timerloop_task);
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/include/can_socket/cancfg.h	Thu Aug 31 10:28:53 2006 +0200
@@ -0,0 +1,30 @@
+/*
+This file is part of CanFestival, a library implementing CanOpen Stack. 
+
+Copyright (C): Edouard TISSERANT and Francis DUPIN
+
+See COPYING file for copyrights details.
+
+This library is free software; you can redistribute it and/or
+modify it under the terms of the GNU Lesser General Public
+License as published by the Free Software Foundation; either
+version 2.1 of the License, or (at your option) any later version.
+
+This library is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public
+License along with this library; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#ifndef __CANCFG_H__
+#define __CANCFG_H__
+
+#ifndef CAN_HANDLE
+#define CAN_HANDLE     void *
+#endif
+
+#endif