drivers/can_peak_win32/can_peak_win32.c
changeset 267 96c688ebcde7
parent 145 e747d2e26af0
child 301 f4b64aa848e1
equal deleted inserted replaced
266:8678a3cf7fe7 267:96c688ebcde7
    29 #include <fcntl.h>
    29 #include <fcntl.h>
    30 #endif
    30 #endif
    31 
    31 
    32 #include "cancfg.h"
    32 #include "cancfg.h"
    33 #include "can_driver.h"
    33 #include "can_driver.h"
    34 
    34 #include "def.h"
    35 #ifndef extra_PCAN_init_params
    35 #ifndef extra_PCAN_init_params
    36 	#define extra_PCAN_init_params /**/
    36 	#define extra_PCAN_init_params /**/
    37 #else
    37 #else
    38 	#define extra_PCAN_init_params\
    38 	#define extra_PCAN_init_params\
    39 		,getenv("PCANHwType") ? strtol(getenv("PCANHwType"),NULL,0):0\
    39 		,getenv("PCANHwType") ? strtol(getenv("PCANHwType"),NULL,0):0\
    64 	if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
    64 	if(!strcmp( optarg, "5K")) return CAN_BAUD_5K;
    65 	if(!strcmp( optarg, "none")) return 0;
    65 	if(!strcmp( optarg, "none")) return 0;
    66 	return 0x0000;
    66 	return 0x0000;
    67 }
    67 }
    68 
    68 
       
    69 #define MyCase(fc) case fc: printf(#fc);break;
       
    70 void print_message(Message *m)
       
    71 {
       
    72 	int i;
       
    73 	switch(m->cob_id.w >> 7)
       
    74 	{
       
    75 		MyCase(SYNC)
       
    76 		MyCase(TIME_STAMP)
       
    77 		MyCase(PDO1tx)
       
    78 		MyCase(PDO1rx)
       
    79 		MyCase(PDO2tx)
       
    80 		MyCase(PDO2rx)
       
    81 		MyCase(PDO3tx)
       
    82 		MyCase(PDO3rx)
       
    83 		MyCase(PDO4tx)
       
    84 		MyCase(PDO4rx)
       
    85 		MyCase(SDOtx)
       
    86 		MyCase(SDOrx)
       
    87 		MyCase(NODE_GUARD)
       
    88 		MyCase(NMT)
       
    89 	}
       
    90 	printf(" rtr:%d", m->rtr);
       
    91 	printf(" len:%d", m->len);
       
    92 	for (i = 0 ; i < m->len ; i++)
       
    93 		printf(" %02x", m->data[i]);
       
    94 	printf("\n");
       
    95 }
       
    96 
    69 void
    97 void
    70 canInit (s_BOARD *board)
    98 canInit (s_BOARD *board)
    71 {
    99 {
    72 	int baudrate;
   100 	int baudrate;
    73 	
   101 	
    74 #ifdef PCAN2_HEADER_
   102 //#ifdef PCAN2_HEADER_
    75 	// if not the first handler
   103 	// if not the first handler
    76 	if(second_board == (s_BOARD *)board)
   104 	if(second_board == (s_BOARD *)board)
    77 		if(baudrate = TranslateBaudeRate(board->baudrate))
   105 		if(baudrate = TranslateBaudeRate(board->baudrate))
    78 			CAN2_Init (baudrate,
   106 			CAN2_Init (baudrate,
    79 			  CAN_INIT_TYPE_ST extra_PCAN_init_params);
   107 			  CAN_INIT_TYPE_ST extra_PCAN_init_params);
    80 #endif
   108 //#endif
    81 	if(first_board == (s_BOARD *)board)
   109 	if(first_board == (s_BOARD *)board)
    82 		if(baudrate = TranslateBaudeRate(board->baudrate))
   110 		if(baudrate = TranslateBaudeRate(board->baudrate))
    83 			CAN_Init (baudrate,
   111 			CAN_Init (baudrate,
    84 			  CAN_INIT_TYPE_ST extra_PCAN_init_params);
   112 			  CAN_INIT_TYPE_ST extra_PCAN_init_params);
    85 }
   113 }
    88 UNS8
   116 UNS8
    89 canReceive_driver (CAN_HANDLE fd0, Message * m)
   117 canReceive_driver (CAN_HANDLE fd0, Message * m)
    90 {
   118 {
    91 	UNS8 data;
   119 	UNS8 data;
    92 	TPCANMsg peakMsg;
   120 	TPCANMsg peakMsg;
    93 
       
    94 	DWORD Res;
   121 	DWORD Res;
    95 
       
    96 	do{
   122 	do{
    97 		// We read the queue looking for messages.
   123 		// We read the queue looking for messages.
    98 		// 
   124 		// 
    99 		//pthread_mutex_lock (&PeakCan_mutex);
   125 		//pthread_mutex_lock (&PeakCan_mutex);
   100 #ifdef PCAN2_HEADER_
   126 #ifdef PCAN2_HEADER_
   105 #endif
   131 #endif
   106 		if(first_board == (s_BOARD *)fd0)
   132 		if(first_board == (s_BOARD *)fd0)
   107 			Res = CAN_Read (&peakMsg);
   133 			Res = CAN_Read (&peakMsg);
   108 		else
   134 		else
   109 			Res = CAN_ERR_BUSOFF;
   135 			Res = CAN_ERR_BUSOFF;
   110 	
       
   111 		// A message was received
   136 		// A message was received
   112 		// We process the message(s)
   137 		// We process the message(s)
   113 		// 
   138 		// 
   114 		if (Res == CAN_ERR_OK)
   139 		if (Res == CAN_ERR_OK)
   115 		{
   140 		{
   127 				//pthread_mutex_unlock (&PeakCan_mutex);
   152 				//pthread_mutex_unlock (&PeakCan_mutex);
   128 				return peakMsg.MSGTYPE ==
   153 				return peakMsg.MSGTYPE ==
   129 					MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN;
   154 					MSGTYPE_STATUS ? peakMsg.DATA[2] : CAN_ERR_OVERRUN;
   130 			}
   155 			}
   131 			m->cob_id.w = peakMsg.ID;
   156 			m->cob_id.w = peakMsg.ID;
       
   157 			
   132 			if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)	/* bits of MSGTYPE_ */
   158 			if (peakMsg.MSGTYPE == CAN_INIT_TYPE_ST)	/* bits of MSGTYPE_ */
   133 				m->rtr = 0;
   159 				m->rtr = 0;
   134 			else
   160 			else
   135 				m->rtr = 1;
   161 				m->rtr = 1;
   136 			m->len = peakMsg.LEN;	/* count of data bytes (0..8) */
   162 			m->len = peakMsg.LEN;	/* count of data bytes (0..8) */
   137 			for (data = 0; data < peakMsg.LEN; data++)
   163 			for (data = 0; data < peakMsg.LEN; data++)
   138 				m->data[data] = peakMsg.DATA[data];	/* data bytes, up to 8 */
   164 				m->data[data] = peakMsg.DATA[data];	/* data bytes, up to 8 */
   139 	
   165 		
   140 		}else{
   166 		}else{
   141 		//pthread_mutex_unlock (&PeakCan_mutex);
   167 		//pthread_mutex_unlock (&PeakCan_mutex);
   142 		//if (Res != CAN_ERR_OK)
   168 		//if (Res != CAN_ERR_OK)
   143 		//{
   169 		//{
   144 			if (!
   170 			if (!
   169 	}
   195 	}
   170 	peakMsg.LEN = m->len;
   196 	peakMsg.LEN = m->len;
   171 	/* count of data bytes (0..8) */
   197 	/* count of data bytes (0..8) */
   172 	for (data = 0; data < m->len; data++)
   198 	for (data = 0; data < m->len; data++)
   173 		peakMsg.DATA[data] = m->data[data];	/* data bytes, up to 8 */
   199 		peakMsg.DATA[data] = m->data[data];	/* data bytes, up to 8 */
       
   200 	
   174 	do
   201 	do
   175 	{
   202 	{
   176 #ifdef PCAN2_HEADER_
   203 #ifdef PCAN2_HEADER_
   177 		// if not the first handler
   204 		// if not the first handler
   178 		if(second_board == (s_BOARD *)fd0)
   205 		if(second_board == (s_BOARD *)fd0)
       
   206 		{
   179 			errno = CAN2_Write (&peakMsg);
   207 			errno = CAN2_Write (&peakMsg);
       
   208 		}
   180 		else 
   209 		else 
   181 #endif
   210 #endif
   182 		if(first_board == (s_BOARD *)fd0)
   211 		if(first_board == (s_BOARD *)fd0)
   183 			errno = CAN_Write (&peakMsg);
   212 			{
       
   213 				errno = CAN_Write (&peakMsg);
       
   214 			}
   184 		else 
   215 		else 
   185 			goto fail;
   216 			goto fail;
   186 		if (errno)
   217 		if (errno)
   187 		{
   218 		{
   188 			if (errno == CAN_ERR_BUSOFF)
   219 			if (errno == CAN_ERR_BUSOFF)
   202 
   233 
   203 /***************************************************************************/
   234 /***************************************************************************/
   204 CAN_HANDLE
   235 CAN_HANDLE
   205 canOpen_driver (s_BOARD * board)
   236 canOpen_driver (s_BOARD * board)
   206 {
   237 {
   207 #ifdef PCAN2_HEADER_
   238   char busname[64];
   208 	if(first_board != NULL && second_board != NULL)
   239   char* pEnd;
   209 #else
   240 
   210 	if(first_board != NULL)
   241   //printf ("Board Busname=%d.\n",strtol(board->busname, &pEnd,0));
   211 #endif
   242   if (strtol(board->busname, &pEnd,0) == 0)
   212 	{
   243   {
   213 		fprintf (stderr, "Open failed.\n");
   244       first_board = board;
   214 		fprintf (stderr,
   245       printf ("First Board selected\n");
   215 				 "can_peak_win32.c: no more can port available with this pcan library\n");
   246   }
   216 		fprintf (stderr,
   247   if (strtol(board->busname, &pEnd,0) == 1)
   217 				 "can_peak_win32.c: please link another executable with another pcan lib\n");
   248   {
   218 		return NULL;
   249      second_board = board;
   219 	}
   250      printf ("Second Board selected\n");
   220 
   251   }
   221 #ifdef PCAN2_HEADER_
       
   222 	if(first_board == NULL)
       
   223 		first_board = board;
       
   224 	else
       
   225 		second_board = board; 
       
   226 #else
       
   227 	first_board = board;
       
   228 #endif
       
   229 
       
   230 	canInit(board);
   252 	canInit(board);
   231 	
   253 	
   232 	return (CAN_HANDLE)board;
   254 	return (CAN_HANDLE)board;
   233 }
   255 }
   234 
   256