Example #1
0
u8 	Icar_SendRead(u8 type,u8* Strp)
{
	u8 buffer[16],CANflag=1;
	u16 len;			 
	u8 *pFrm=buffer;
 	u32 iClick=OSTime;
	u8 FRAMES[]="\x30\x00\x00\x00\x00\x00\x00\x00";

	Icar_TranToSendBuf(type,Strp,buffer);
	len=buffer[0];
	InitFrameBuffer();
	SendFrame(type,buffer);
	pFrm+=1+len;

	while(1){
		len=(u16)RecvFrame(type,buffer);
		if(len==0)break;
		if(((type==OBD_TYPE_CAN1) ||(type==OBD_TYPE_CAN2))&&len>13){AddFrameToBuffer(buffer,13);AddFrameToBuffer(buffer+13,13);}
		else AddFrameToBuffer(buffer,len);
		if(*pFrm!=(u8)F_MUT){  //
			if(OSTime-iClick>g_box_out_time)break;
		}
		if(((type==OBD_TYPE_CAN1) ||(type==OBD_TYPE_CAN2))&&((buffer[7]==0x10)||(buffer[7+13]==0x10))&&(CANflag)){ can_send( can_snd_id[obd_read_canid_idx], DAT_FRAME, 8, FRAMES);CANflag=0;}
		else break;
	}
	return obd_fbuff.frame_count;
}
/**
*
* The entry point for showing the XCanPs driver in polled mode. The example
* configures the device for internal loop back mode, then sends a Can
* frame, receives the same Can frame, and verifies the frame contents.
*
* @param	DeviceId is the XPAR_<CANPS_instance>_DEVICE_ID value from
*		xparameters.h
*
* @return	XST_SUCCESS if successful, otherwise driver-specific error code.
*
* @note
*
* If the device is not working correctly, this function may enter an infinite
* loop and will never return to the caller.
*
******************************************************************************/
int CanPsPolledExample(u16 DeviceId)
{
	int Status;
	XCanPs *CanInstPtr = &Can;
	XCanPs_Config *ConfigPtr;

	/*
	 * Initialize the Can device.
	 */
	ConfigPtr = XCanPs_LookupConfig(DeviceId);
	if (CanInstPtr == NULL) {
		return XST_FAILURE;
	}
	Status = XCanPs_CfgInitialize(CanInstPtr,
					ConfigPtr,
					ConfigPtr->BaseAddr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Run self-test on the device, which verifies basic sanity of the
	 * device and the driver.
	 */
	Status = XCanPs_SelfTest(CanInstPtr);
	if (Status != XST_SUCCESS) {
		return XST_FAILURE;
	}

	/*
	 * Enter Configuration Mode so we can setup Baud Rate Prescaler
	 * Register (BRPR) and Bit Timing Register (BTR).
	 */
	XCanPs_EnterMode(CanInstPtr, XCANPS_MODE_CONFIG);
	while(XCanPs_GetMode(CanInstPtr) != XCANPS_MODE_CONFIG);

	/*
	 * Setup Baud Rate Prescaler Register (BRPR) and
	 * Bit Timing Register (BTR).
	 */
	XCanPs_SetBaudRatePrescaler(CanInstPtr, TEST_BRPR_BAUD_PRESCALAR);
	XCanPs_SetBitTiming(CanInstPtr, TEST_BTR_SYNCJUMPWIDTH,
				TEST_BTR_SECOND_TIMESEGMENT,

				TEST_BTR_FIRST_TIMESEGMENT);

	/*
	 * Enter Loop Back Mode.
	 */
	XCanPs_EnterMode(CanInstPtr, XCANPS_MODE_LOOPBACK);
	while(XCanPs_GetMode(CanInstPtr) != XCANPS_MODE_LOOPBACK);

	/*
	 * Send a frame, receive the frame via the loop back and verify its
	 * contents.
	 */
	Status = SendFrame(CanInstPtr);
	if (Status != XST_SUCCESS) {
		return Status;
	}

	Status = RecvFrame(CanInstPtr);

	return Status;
}
/* main function is a recv-thread, it is used to receive messages   *
*  sending from the senrver.                                        * 
*  if it receives a control-message, it will set needsendACK = 1    *
*  (to tell send thread that he should send an ACK to the server    *
*   saying "got it").                                               *
*  if receive a ACK message, it will set haverecvACK = 1, to let    *
*  the send thread knows that the server had received a temperature *
*  message.                                                         */
int main()
{
  pthread_t sendthread, sendACKthread;
  char recvframe[MAX_FRAME_SIZE] = "ABCDEFG";
//  char *device = "/dev/ttyAMA0";
//  char *device = "/dev/ttyUSB0";
  char *device = "/dev/pts/4";
  fre  = 1;
  stop = 0;
  haverecvACK = 0;
  needsendACK = 0;
  pthread_mutex_init(&stop_mutex, NULL);
  pthread_mutex_init(&fre_mutex, NULL);
  pthread_mutex_init(&have_recv_ack_mutex, NULL);
  pthread_mutex_init(&need_send_ack_mutex, NULL);
  fd = OpenPort(device);
  if(fd < 0) return -1;
  pthread_create(&sendthread,    NULL, (void *)&SendThreadFun, NULL);
  pthread_create(&sendACKthread, NULL, (void *)&SendACKFun,    NULL);

  while(stop != 1)
  {

    /*receive a frame and analyse it*/
    RecvFrame(fd, recvframe);
    int recvmode = AnalyseFrame(recvframe);

    if(recvmode == 0)
    {
       printf("recv: ACK\n");
       pthread_mutex_lock(&have_recv_ack_mutex);
       haverecvACK = 1;
       show_state();
       pthread_mutex_unlock(&have_recv_ack_mutex);
    }

    /* if it receives a frequency-message, it will set needsendACK = 1 *
     *  (to tell send thread that he should send an ACK to the server  *
     *   saying "got it").                                             */
    if(recvmode == 2 ) 
    {
      printf("recv: change frequency %d\n", fre);
      char delayAscii[7];
      /*extract the number from the recv frame */
      memcpy(delayAscii, recvframe + 3, 6);
      delayAscii[6] = '\0';
      pthread_mutex_lock(&fre_mutex);
      fre = atol((const char *) delayAscii);
      pthread_mutex_unlock(&fre_mutex);

      pthread_mutex_lock(&need_send_ack_mutex);
      needsendACK = 1;
      show_state();
      pthread_mutex_unlock(&need_send_ack_mutex);
    }

    /* if it receives a stop-message, it will set needsendACK = 1      *
     *  (to tell send thread that he should send an ACK to the server  *
     *   saying "got it").                                             */
    if(recvmode == 3)
    {

      printf("recv: STOP!\n");
      pthread_mutex_lock(&stop_mutex);
      stop = 1;
      pthread_mutex_unlock(&stop_mutex);

      pthread_mutex_lock(&need_send_ack_mutex);
      needsendACK = 1;
      show_state();
      pthread_mutex_unlock(&need_send_ack_mutex);
    }

    memset(recvframe, '\0', MAX_FRAME_SIZE);
    //usleep(500000);
  }
  pthread_join(sendthread, NULL);
  pthread_join(sendACKthread, NULL);
  return 0;
}