drivers/can_virtual/can_virtual.c
changeset 145 e747d2e26af0
parent 0 4472ee7c6c3e
child 157 8b45ff4202c3
equal deleted inserted replaced
144:3ebf16150b2e 145:e747d2e26af0
    22 
    22 
    23 /*
    23 /*
    24 	Virtual CAN driver.
    24 	Virtual CAN driver.
    25 */
    25 */
    26 
    26 
    27 #include <stdlib.h>
    27 #include <stdio.h>
       
    28 #include <unistd.h>
    28 
    29 
    29 #include <sys/types.h>
    30 #include "can_driver.h"
    30 #include <unistd.h>
       
    31 #include <stdio.h>
       
    32 
    31 
    33 #include <applicfg.h>
    32 #define MAX_NB_CAN_PIPES 16
    34 #include "timer.h"
       
    35 #include "can_driver.h"
       
    36 #include "timers_driver.h"
       
    37 
       
    38 #define MAX_NB_CAN_PIPES 10
       
    39 
    33 
    40 typedef struct {
    34 typedef struct {
    41   char used;
    35   char used;
    42   int pipe[2];
    36   int pipe[2];
    43   TASK_HANDLE receiveTask;
       
    44   CO_Data* d;
       
    45 } CANPipe;
    37 } CANPipe;
    46 
    38 
    47 CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,{0,0},},};
    39 CANPipe canpipes[MAX_NB_CAN_PIPES] = {{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},{0,},};
    48 
    40 
    49 /*********functions which permit to communicate with the board****************/
    41 /*********functions which permit to communicate with the board****************/
    50 UNS8 canReceive(CAN_HANDLE fd0, Message *m)
    42 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m)
    51 {
    43 {
    52 	if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) < sizeof(Message))
    44 	if(read(((CANPipe*)fd0)->pipe[0], m, sizeof(Message)) < sizeof(Message))
    53 	{
    45 	{
    54 		return 1;
    46 		return 1;
    55 	}
    47 	}
    56 	return 0;
    48 	return 0;
    57 }
    49 }
    58 
    50 
    59 void canReceiveLoop(CAN_HANDLE fd0)
       
    60 {
       
    61 	CO_Data* d = ((CANPipe*)fd0)->d;
       
    62 	Message m;
       
    63 	while (1) {
       
    64 		if(!canReceive(fd0, &m))
       
    65 		{  
       
    66 			EnterMutex();
       
    67 			canDispatch(d, &m);
       
    68 			LeaveMutex();
       
    69 		}else{
       
    70 			break;
       
    71 		}
       
    72 	}
       
    73 }
       
    74 
       
    75 /***************************************************************************/
    51 /***************************************************************************/
    76 UNS8 canSend(CAN_HANDLE fd0, Message *m)
    52 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m)
    77 {
    53 {
    78   int i;
    54   int i;
    79   // Send to all readers, except myself
    55   // Send to all readers, except myself
    80   for(i=0; i < MAX_NB_CAN_PIPES; i++)
    56   for(i=0; i < MAX_NB_CAN_PIPES; i++)
    81   {
    57   {
    84 		write(canpipes[i].pipe[1], m, sizeof(Message));
    60 		write(canpipes[i].pipe[1], m, sizeof(Message));
    85   	}
    61   	}
    86   }
    62   }
    87   return 0;
    63   return 0;
    88 }
    64 }
    89 
    65 /*
       
    66 int TranslateBaudeRate(char* optarg){
       
    67 	if(!strcmp( optarg, "1M")) return 1000;
       
    68 	if(!strcmp( optarg, "500K")) return 500;
       
    69 	if(!strcmp( optarg, "250K")) return 250;
       
    70 	if(!strcmp( optarg, "125K")) return 125;
       
    71 	if(!strcmp( optarg, "100K")) return 100;
       
    72 	if(!strcmp( optarg, "50K")) return 50;
       
    73 	if(!strcmp( optarg, "20K")) return 20;
       
    74 	if(!strcmp( optarg, "10K")) return 10;
       
    75 	if(!strcmp( optarg, "5K")) return 5;
       
    76 	if(!strcmp( optarg, "none")) return 0;
       
    77 	return 0;
       
    78 }*/
    90 /***************************************************************************/
    79 /***************************************************************************/
    91 CAN_HANDLE canOpen(s_BOARD *board)
    80 CAN_HANDLE canOpen_driver(s_BOARD *board)
    92 {
    81 {
    93   int i;  
    82   int i;  
    94   for(i=0; i < MAX_NB_CAN_PIPES; i++)
    83   for(i=0; i < MAX_NB_CAN_PIPES; i++)
    95   {
    84   {
    96   	if(!canpipes[i].used)
    85   	if(!canpipes[i].used)
   103       fprintf (stderr, "Open failed.\n");
    92       fprintf (stderr, "Open failed.\n");
   104       return (CAN_HANDLE)NULL;
    93       return (CAN_HANDLE)NULL;
   105     }
    94     }
   106 
    95 
   107    canpipes[i].used = 1;
    96    canpipes[i].used = 1;
   108 
       
   109    canpipes[i].d = board->d;
       
   110    CreateReceiveTask((CAN_HANDLE) &canpipes[i], &canpipes[i].receiveTask);
       
   111 
       
   112    return (CAN_HANDLE) &canpipes[i];
    97    return (CAN_HANDLE) &canpipes[i];
   113 }
    98 }
   114 
    99 
   115 /***************************************************************************/
   100 /***************************************************************************/
   116 int canClose(CAN_HANDLE fd0)
   101 int canClose_driver(CAN_HANDLE fd0)
   117 {
   102 {
   118   close(((CANPipe*)fd0)->pipe[0]);
   103   close(((CANPipe*)fd0)->pipe[0]);
   119   close(((CANPipe*)fd0)->pipe[1]);
   104   close(((CANPipe*)fd0)->pipe[1]);
   120   ((CANPipe*)fd0)->used = 0;
   105   ((CANPipe*)fd0)->used = 0;
   121   WaitReceiveTaskEnd(&((CANPipe*)fd0)->receiveTask);
   106   return 0;
   122 }
   107 }
   123 
   108 
   124 
   109