Exemplo n.º 1
0
 void can_init(can_t *obj, PinName rd, PinName td)
 {
    uint32_t can_td = (CANName)pinmap_peripheral(td, PinMap_CAN_TD);
    uint32_t can_rd = (CANName)pinmap_peripheral(rd, PinMap_CAN_RD);
    obj->can = (CANName)pinmap_merge(can_td, can_rd);
    MBED_ASSERT((int)obj->can != NC);
    
    const struct nu_modinit_s *modinit = get_modinit(obj->can, can_modinit_tab);
    MBED_ASSERT(modinit != NULL);
    MBED_ASSERT(modinit->modname == obj->can);
    
    // Reset this module
    SYS_ResetModule(modinit->rsetidx);
    
    // Enable IP clock
    CLK_EnableModuleClock(modinit->clkidx);
     
    obj->index = 0;
    
    pinmap_pinout(td, PinMap_CAN_TD);
    pinmap_pinout(rd, PinMap_CAN_RD);
    
    /* For M453 mbed Board Transmitter Setting (RS Pin) */
    GPIO_SetMode(PA, BIT0| BIT1, GPIO_MODE_OUTPUT);    
    PA0 = 0x00;
    PA1 = 0x00;   

    CAN_Open((CAN_T *)NU_MODBASE(obj->can), 500000, CAN_NORMAL_MODE);
    
    can_filter(obj, 0, 0, CANStandard, 0);
 }
Exemplo n.º 2
0
void SelectCANSpeed()
{
    uint32_t unItem;
    int32_t i32Err =0;

    printf("Please select CAN speed you desired\n");
    printf("[0] 1000Kbps\n");
    printf("[1]  500Kbps\n");
    printf("[2]  250Kbps\n");
    printf("[3]  125Kbps\n");
    printf("[4]  100Kbps\n");
    printf("[5]   50Kbps\n");

    unItem = GetChar();
    printf("%c\n",unItem) ;
    if(unItem=='1')
        i32Err = CAN_Open(500);
    else if(unItem=='2')
        i32Err = CAN_Open(250);
    else if(unItem=='3')
        i32Err = CAN_Open(125);
    else if(unItem=='4')
        i32Err = CAN_Open(100);
    else if(unItem=='5')
        i32Err = CAN_Open(50);
    else
        i32Err = CAN_Open(1000);

    if(i32Err<0)
        printf("Set CAN bit rate is fail\n");
}
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
	int i, err;

	signal(SIGTERM, signal_handler);
	signal(SIGINT, signal_handler);

	mlockall(MCL_CURRENT | MCL_FUTURE);

	can_handle = CAN_Open(HW_PCI, 1);
	CAN_Init(can_handle, CAN_BAUD_1M, CAN_INIT_TYPE_EX);
	printf("Status = %i\n", CAN_Status(can_handle));

	for (i = 0; i < 5; i++) {
		can_msg[i].MSGTYPE = MSGTYPE_EXTENDED;
		can_msg[i].LEN = 8;
		printf("Frame = %08lx %02x %02x %02x %02x %02x %02x %02x %02x, "
			"Time diff = %llu ns\n",
			(unsigned long) (can_msg[i].ID = i*13),
			can_msg[i].DATA[0] = i*6,
			can_msg[i].DATA[1] = i*7,
			can_msg[i].DATA[2] = i*8,
			can_msg[i].DATA[3] = i*9,
			can_msg[i].DATA[4] = i*10,
			can_msg[i].DATA[5] = i*11,
			can_msg[i].DATA[6] = i*12,
			can_msg[i].DATA[7] = i*13,
			i == 0 ? 0 : (unsigned long long) TIME_DIFF
		);
	}

	err = rt_task_create(&test_task, "test", 0, 99, T_JOINABLE);
	if (err) {
		printf("receivetest: Failed to create rt task, code %d\n",
			errno);
		return err;
	}
 	rt_task_start(&test_task, test, NULL);
	if (err) {
		printf("receivetest: Failed to start rt task, code %d\n",
			errno);
		return errno;
    	}
	rt_task_join(&test_task);

	CAN_Close(can_handle);
	return 0;
}
int main()
{
	int i=0;
	int err=0;
	signal(SIGTERM, signal_handler);
	signal(SIGINT, signal_handler);

	mlockall(MCL_CURRENT | MCL_FUTURE);

	can_handle = CAN_Open(HW_PCI, 1);
	CAN_Init(can_handle, CAN_BAUD_1M, CAN_INIT_TYPE_EX);
	printf("Status = %i\n", CAN_Status(can_handle));

	err=rt_task_create(&test_task, "test", 0, 99, T_JOINABLE);
	if (err) {
      		printf("receivetest: Failed to create rt task, code %d\n",errno);
      	return err;
	}
 	rt_task_start(&test_task, test, NULL);
	if (err) {
      		printf("receivetest: Failed to start rt task, code %d\n",errno);
      	return errno;
    	}
	rt_task_join(&test_task);

	for (i = 0; i < 5; i++)
	{
		printf(
			"Frame = %08lx %02x %02x %02x %02x %02x %02x %02x %02x, Time diff = %llu ns\n",
			(unsigned long) can_msg[i].Msg.ID,
			can_msg[i].Msg.DATA[0],
			can_msg[i].Msg.DATA[1],
			can_msg[i].Msg.DATA[2],
			can_msg[i].Msg.DATA[3],
			can_msg[i].Msg.DATA[4],
			can_msg[i].Msg.DATA[5],
			can_msg[i].Msg.DATA[6],
			can_msg[i].Msg.DATA[7],
			i == 0 ? 0 :
				(can_msg[i].dwTime*(unsigned long long)1000+can_msg[i].wUsec)*1000-
				(can_msg[i-1].dwTime*(unsigned long long)1000+can_msg[i-1].wUsec)*1000
		);
	}

	CAN_Close(can_handle);
	return 0;
}
Exemplo n.º 5
0
PcanPci::PcanPci(unsigned char port, unsigned short speed)
{
    std::cout << "Connecting to PCAN PCI... ";
    pcanHandle = CAN_Open(HW_PCI, port);

    if (pcanHandle == NULL)
        std::cout << "error!" << std::endl;
    else if (!CAN_Init(pcanHandle, speed, CAN_INIT_TYPE_ST) == 0)
        std::cout << "error!" << std::endl;
    else if (!resetMsgFilter())
        std::cout << "error!" << std::endl;
    else
    {
        emptyReadQueue();
        std::cout << "done!" << std::endl;
    }
}
Exemplo n.º 6
0
// open the CAN port
int open_can(bool bDevNodeGiven,bool bTypeGiven,const char *szDevNode,int nType,__u32 dwPort,__u16 wIrq)
{
  int err = 0;
  if ((bDevNodeGiven) || (!bDevNodeGiven && !bTypeGiven)) 
    h = LINUX_CAN_Open(szDevNode, O_RDWR);
  else 
  {
		// please use what is appropriate
		// HW_DONGLE_SJA
		// HW_DONGLE_SJA_EPP
		// HW_ISA_SJA
		// HW_PCI
		// HW_USB
    h = CAN_Open(nType, dwPort, wIrq);
  }
  if (!h) 
    return 1;
  
  return err;
}
Exemplo n.º 7
0
void pcan_transmit::init(int argc, char **argv)
{
    /*!
				 *\brief The init() function is based on the middle part of the main() function of the transmitest program.
				 *
				 * Compared to the transmitest program a lot of cases have been removed.
				 * Therefore, only the PCAN-USB adapter is, and standard messages are, supported.
				 *
				 * */




	int   nExtended = CAN_INIT_TYPE_ST;
	int nType;
	__u32 dwPort;
	__u16 wIrq;
	char *ptr;
	__u16 wBTR0BTR1 = 0x011C;
	// parameter wBTR0BTR1
	// bitrate codes of BTR0/BTR1 registers
	//#define CAN_BAUD_1M     0x0014  //   1 MBit/s
	//#define CAN_BAUD_500K   0x001C  // 500 kBit/s
    //#define CAN_BAUD_250K   0x011C  // 250 kBit/s
	//#define CAN_BAUD_125K   0x031C  // 125 kBit/s
	//#define CAN_BAUD_100K   0x432F  // 100 kBit/s
	//#define CAN_BAUD_50K    0x472F  //  50 kBit/s
	//#define CAN_BAUD_20K    0x532F  //  20 kBit/s
	//#define CAN_BAUD_10K    0x672F  //  10 kBit/s
	//#define CAN_BAUD_5K     0x7F7F  //   5 kBit/s
	const char  *szDevNode = DEFAULT_NODE;
	bool bDevNodeGiven = false;
	bool bTypeGiven = false;
	char txt[VERSIONSTRING_LEN];




   /* QApplication a(argc, argv);
    QWidget *mainWindow=new QWidget;
    mainWindow->setWindowTitle("Change the Data Value");
    QSlider *slider= new QSlider(Qt::Horizontal);
    QSpinBox *spinner= new QSpinBox;

    slider->setRange(0,100);
    spinner->setRange(0,100);

    connect(ui->slider, SIGNAL(valueChanged(double)), SLOT(onSliderValueChanged(double)));


double slidValue= ui->Slider->value();



    //string msg[]= "ID, LEN, ss";*/


    
    
    
    // decode command line arguments
		for (int i = 1; i < argc; i++)
		{
		    char c;

		    ptr = argv[i];

		    while (*ptr == '-')
		      ptr++;

		    c = *ptr;
		    ptr++;

		    if (*ptr == '=')
		      ptr++;

		    switch(tolower(c))
		    {
		      case '?':
		      case 'h':
		    	hlpMsg();
		    	do_exit(errno, h);
		        break;
		      case 'f':
		        szDevNode = ptr;
		        bDevNodeGiven = true;
		        break;
		      case 'b':
		        wBTR0BTR1 = (__u16)strtoul(ptr, NULL, 16);
		        break;
		      default:
		         errno = EINVAL;
		         do_exit(errno, h);;
		        break;
		    }
		}




		  /* open CAN port */
		   if ((bDevNodeGiven) || (!bDevNodeGiven && !bTypeGiven))
		   {
		     h = LINUX_CAN_Open(szDevNode, O_RDWR);
		     if (!h)
		     {
		       printf("pcan_transmit: can't open %s\n", szDevNode);
		       do_exit(errno, h);;
		     }
		   }
		   else {
		     // please use what is appropriate
		     // HW_DONGLE_SJA
		     // HW_DONGLE_SJA_EPP
		     // HW_ISA_SJA
		     // HW_PCI
		     h = CAN_Open(nType, dwPort, wIrq);
		     if (!h)
		     {
		       printf("pcan_transmit: can't open %s device.\n", getNameOfInterface(nType));
		       do_exit(errno, h);;
		     }
		   }
		   /* clear status */
		     CAN_Status(h);
		     // get version info
		     errno = CAN_VersionInfo(h, txt);
		     if (!errno)
		       printf("pcan_transmit: driver version = %s\n", txt);
		     else {
		       perror("pcan_transmit: CAN_VersionInfo()");
		       do_exit(errno, h);;
		     }
		     // init to a user defined bit rate
		     if (wBTR0BTR1)
		     {
		       errno = CAN_Init(h, wBTR0BTR1, nExtended);
		       if (errno)
		       {
		         perror("pcan_transmit: CAN_Init()");
		         do_exit(errno, h);;
		       }
		     }

}
Exemplo n.º 8
0
// here all is done
int main(int argc, char *argv[])
{
	char *ptr;
	int i;
	int nType = HW_PCI;
	__u32 dwPort = 0;
	__u16 wIrq = 0;
	__u16 wBTR0BTR1 = 0;
	int nExtended = CAN_INIT_TYPE_ST;
	const char  *szDevNode = DEFAULT_NODE;
	bool bDevNodeGiven = false;
	bool bTypeGiven = false;
	bool bDisplayOn = true;
	char txt[VERSIONSTRING_LEN];

	errno = 0;

	current_release = CURRENT_RELEASE;
	disclaimer("receivetest");

	init();

	// decode command line arguments
	for (i = 1; i < argc; i++) {
		char c;

		ptr = argv[i];

		while (*ptr == '-')
			ptr++;

		c = *ptr;
		ptr++;

		if (*ptr == '=')
			ptr++;

		switch(tolower(c)) {
		case 'f':
			szDevNode = ptr;
			bDevNodeGiven = true;
			break;
		case 'd':
			if (strcmp(ptr, "no") == 0)
				bDisplayOn = false;
			break;
		case 't':
			nType = getTypeOfInterface(ptr);
			if (!nType) {
				errno = EINVAL;
				printf("receivetest: unknown type of interface!\n");
				goto error;
			}
			bTypeGiven = true;
			break;
		case 'p':
			dwPort = strtoul(ptr, NULL, 16);
			break;
		case 'i':
			wIrq = (__u16)strtoul(ptr, NULL, 10);
			break;
		case 'e':
			nExtended = CAN_INIT_TYPE_EX;
			break;
		case '?':
		case 'h':
			hlpMsg();
			goto error;
			break;
		case 'b':
			wBTR0BTR1 = (__u16)strtoul(ptr, NULL, 16);
			break;
		default:
			errno = EINVAL;
			perror("receivetest: unknown command line argument!\n");
			goto error;
			break;
		}
	}

	// simple command input check
	if (bDevNodeGiven && bTypeGiven) {
		errno = EINVAL;
		perror("receivetest: device node and type together is useless");
		goto error;
	}

	// give some information back
	if (!bTypeGiven) {
		printf("receivetest: device node=\"%s\"\n", szDevNode);
	} else {
		printf("receivetest: type=%s", getNameOfInterface(nType));
		switch (nType) {
		case HW_USB:
		case HW_USB_PRO:
		case HW_USB_FD:
		case HW_USB_PRO_FD:
			if (dwPort)
				printf(", %d. device\n", dwPort);
			else
				printf(", standard device\n");
			break;

		default:
			if (dwPort) {
				if (nType == HW_PCI)
					printf(", %d. PCI device", dwPort);
				else
					printf(", port=0x%08x", dwPort);
			} else
				printf(", port=default");

			if ((wIrq) && !(nType == HW_PCI))
				printf(" irq=0x%04x\n", wIrq);
			else
				printf(", irq=default\n");
			break;
		}
	}

	if (nExtended == CAN_INIT_TYPE_EX)
		printf("             Extended frames are accepted");
	else
		printf("             Only standard frames are accepted");
	if (wBTR0BTR1)
		printf(", init with BTR0BTR1=0x%04x\n", wBTR0BTR1);
	else
		printf(", init with 500 kbit/sec.\n");

	/* open CAN port */
	if ((bDevNodeGiven) || (!bDevNodeGiven && !bTypeGiven)) {
		h = LINUX_CAN_Open(szDevNode, O_RDWR);
		if (!h) {
			printf("receivetest: can't open %s\n", szDevNode);
			goto error;
		}
	} else {
		// please use what is appropriate  
		// HW_DONGLE_SJA 
		// HW_DONGLE_SJA_EPP 
		// HW_ISA_SJA 
		// HW_PCI 
		h = CAN_Open(nType, dwPort, wIrq);
		if (!h) {
			printf("receivetest: can't open %s device.\n",
				getNameOfInterface(nType));
			goto error;
		}
	}

	/* clear status */
	//  CAN_Status(h);

	// get version info
	errno = CAN_VersionInfo(h, txt);
	if (!errno)
		printf("receivetest: driver version = %s\n", txt);
	else {
		perror("receivetest: CAN_VersionInfo()");
		goto error;
	}

	// init to a user defined bit rate
	if (wBTR0BTR1) {
		errno = CAN_Init(h, wBTR0BTR1, nExtended);
		if (errno) {
			perror("receivetest: CAN_Init()");
			goto error;
		}
	}
	errno = read_loop(bDisplayOn);
	if (!errno)
		return 0;

error:
	do_exit(errno);
	return errno;
}
int main(int argc, char *argv[])
{
  char *ptr;
  int i;
  int nType = HW_PCI;
  __u32 dwPort = 0;
  __u16 wIrq = 0;
  __u16 wBTR0BTR1 = 0;
  __u32 dwMaxTimeInterval = 0, dwMaxLoop = 0;
  char *filename = NULL;
  const char *szDevNode = DEFAULT_NODE;
  bool bDevNodeGiven = false;
  bool bTypeGiven = false;
  parser MyParser;
  char txt[VERSIONSTRING_LEN];

  errno = 0;

  current_release = CURRENT_RELEASE;
  disclaimer("transmitest");

  init();

  // decode command line arguments
  for (i = 1; i < argc; i++) 
  {
    char c;

    ptr = argv[i];

    if (*ptr == '-') 
    {
      while (*ptr == '-')
        ptr++;

      c = *ptr;
      ptr++;

      if (*ptr == '=')
      ptr++;

      switch(tolower(c)) 
      {
        case 'f':
          szDevNode = ptr;
          bDevNodeGiven = true;
          break;
        case 't':
          nType = getTypeOfInterface(ptr);
          if (!nType) 
          {
            errno = EINVAL;
            printf("transmitest: unknown type of interface\n");
            goto error;
          }
          bTypeGiven = true;
          break;
        case 'p':
          dwPort = strtoul(ptr, NULL, 16);
          break;
        case 'i':
          wIrq   = (__u16)strtoul(ptr, NULL, 10);
          break;
        case 'e':
          nExtended = CAN_INIT_TYPE_EX;
          break;
        case '?': 
        case 'h':
          hlpMsg();
          goto error;
          break;
        case 'b':
          wBTR0BTR1 = (__u16)strtoul(ptr, NULL, 16);
          break;
        case 'r':
          dwMaxTimeInterval = strtoul(ptr, NULL, 10);
          break;
        case 'n':
          dwMaxLoop = strtoul(ptr, NULL, 10);
          break;
        default:
          errno = EINVAL;
          printf("transmitest: unknown command line argument\n");
          goto error;
          break;
      }
    }
    else
      filename = ptr;
  }

  // test for filename
  if (filename == NULL) 
  {
    errno = EINVAL;
    perror("transmitest: no filename given");
    goto error;
  }

  // test device node and type
  if (bDevNodeGiven && bTypeGiven) 
  {
    errno = EINVAL;
    perror("transmitest: device node and type together is useless");
    goto error;
  }

  // give the filename to my parser
  MyParser.setFileName(filename);

  // tell some information to the user
  if (!bTypeGiven) 
  {
    printf("transmitest: device node=\"%s\"\n", szDevNode);
  }
  else {
    printf("transmitest: type=%s", getNameOfInterface(nType));
    if (nType == HW_USB) 
      printf(", Serial Number=default, Device Number=%d\n", dwPort); 
    else {
      if (dwPort) 
      {
        if (nType == HW_PCI)
          printf(", %d. PCI device", dwPort);
        else
          printf(", port=0x%08x", dwPort);
      }
      else
        printf(", port=default");
      
      if ((wIrq) && !(nType == HW_PCI))
        printf(" irq=0x%04x\n", wIrq);
      else
        printf(", irq=default\n");
    }
  }

  if (nExtended == CAN_INIT_TYPE_EX)
    printf("             Extended frames are sent");
  else
    printf("             Only standard frames are sent");
  
  if (wBTR0BTR1)
    printf(", init with BTR0BTR1=0x%04x\n", wBTR0BTR1);
  else
    printf(", init with 500 kbit/sec.\n");
    printf("             Data will be read from \"%s\".\n", filename);

  if (dwMaxTimeInterval)
    printf("             Messages are send in random time intervalls with a max. gap time of %d msec.\n", dwMaxTimeInterval);
  
  /* get the list of data from parser */
  List = MyParser.Messages();
  if (!List)
  {
    errno = MyParser.nGetLastError();
    perror("transmitest: error at file read");
    goto error;
  }
  
  /* open CAN port */
  if ((bDevNodeGiven) || (!bDevNodeGiven && !bTypeGiven)) 
  {
    h = LINUX_CAN_Open(szDevNode, O_RDWR);
    if (!h)
    {
      printf("transmitest: can't open %s\n", szDevNode);
      goto error;
    }
  }
  else {
    // please use what is appropriate  
    // HW_DONGLE_SJA 
    // HW_DONGLE_SJA_EPP 
    // HW_ISA_SJA 
    // HW_PCI 
    h = CAN_Open(nType, dwPort, wIrq);
    if (!h)
    {
      printf("transmitest: can't open %s device.\n", getNameOfInterface(nType));
      goto error;
    }
  }

  /* clear status */
  CAN_Status(h);
  
  // get version info
  errno = CAN_VersionInfo(h, txt);
  if (!errno)
    printf("transmitest: driver version = %s\n", txt);
  else {
    perror("transmitest: CAN_VersionInfo()");
    goto error;
  }
  
  // init to a user defined bit rate
  if (wBTR0BTR1) 
  {
    errno = CAN_Init(h, wBTR0BTR1, nExtended);
    if (errno) 
    {
      perror("transmitest: CAN_Init()");
      goto error;
    }
  }
  // enter in the write loop
  errno = write_loop(dwMaxTimeInterval, dwMaxLoop);
  if (!errno)
    return 0;

  error:
    do_exit(errno);
    return errno;
}
Exemplo n.º 10
0
int main(void)
{
  FIL log_file;
  int i, j, result;
  int file_number = 0;
  u8 configNumber = 1;
  u8 maxConfig;
  u8 *p;
  time_t rawtime;
  u8 debugState = 0; //debug off

#ifdef DEBUG
  debug();
#endif

  RCC_Configuration();
  GPIO_Configuration();
  NVIC_Configuration();
  SystickConfigure();
  Timer3Init();
#ifdef UART1_DEBUG
  (void)USART1_Init(115200);
#endif
  LedConfigureGPIO();
  ButtonConfigureGPIO();
  BuzzerConfigureGPIO();

#ifdef CAN_FEATURE_ENABLED
  GPIO_InitTypeDef GPIO_InitStructure;

  // Configure CAN pin: RX
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  // Configure CAN pin: TX
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
#endif

  MMC_PowerOff();
  timer2=1000;
  while(timer2);
  MMC_PowerOn();

#ifdef UART1_DEBUG
  printf("\n\n%s %s\n", HW_VERSION, SW_VERSION);
#endif

  ConfigureClock();
  timer2=1000;
  while(timer2);

  buttonState = 0;

  CardInserted();
  
  if (FR_OK != f_mount(0, &Fatfs[0]))
  {
    LedSetColor(amber, fast);
    BuzzerSetMode(0x83);
#ifdef UART1_DEBUG
    printf("error mounting fat device\n");
#endif
    while(buttonState != 2);
    NVIC_GenerateSystemReset();
  }

  if (ReadTimeDateFile())
  {
#ifdef UART1_DEBUG
    rawtime = RTC_GetCounter();
    printf("Time set: %s\n", ctime (&rawtime) );
#endif
  }

#ifdef DEBUG_LOG
  if (ReadDebugFile())
  {
    debugState = 1;
  }
#endif

#ifdef CAN_FEATURE_ENABLED
  if (ReadCanFile())
  {
    canDiag = 1;
    CAN_Open();
  }
#endif

  i = ReadSpeedFile();
  if (i > 0)
  {
    KW1281_SPEED = i;
    autobaud = 0;
  }

  if (ReadDelayFile())
  {
    user_defined_timings = 1;
  }
  
  if (!ReadConfig())
  {
    LedSetColor(amber, fast); 
    BuzzerSetMode(0x83);
#ifdef UART1_DEBUG
    printf("error reading config\n");
#endif
    MMC_PowerOff();
    while(buttonState != 2);
    NVIC_GenerateSystemReset(); 
  }
  
  if (config[0][0] > 0)
  {
    maxConfig = 1;
    if (config[1][0] > 0)
    {
      maxConfig = 2;
      if (config[2][0] > 0)
      {
        maxConfig = 3;
      }
    }
  }
  else
  {
    LedSetColor(amber, fast); 
    BuzzerSetMode(0x83);
#ifdef UART1_DEBUG
    printf("error reading config\n");
#endif
    MMC_PowerOff();
    while(buttonState != 2);
    NVIC_GenerateSystemReset(); 
  }

  file_number = GetNextFileNumber();
#ifdef UART1_DEBUG
  printf("file number %d\n", file_number);
#endif
  if (!file_number)
  {
    LedSetColor(amber, fast); 
    BuzzerSetMode(0x83);
#ifdef UART1_DEBUG
    printf("can't get number of next file\n");
#endif
    MMC_PowerOff();
    while(buttonState != 2);
    NVIC_GenerateSystemReset(); 
  }
  
  while(1)
  {
    i = USART2_GetData();
    buttonState = 0;
    LedSetColor(configNumber, continous);
    BuzzerSetMode(configNumber);
    do
    {
      if (2 == buttonState)
      {
        configNumber = (configNumber < maxConfig)?configNumber+1:1;
        LedSetColor(configNumber, continous);
        BuzzerSetMode(configNumber);
        buttonState = 0;
      }
    } while (1 != buttonState);
    
    buttonState = 0;
    LedSetColor(green, fast);
    BuzzerSetMode(0x81);

    i = CreateLogFile(file_number, &log_file);

    i = f_printf(&log_file, "%s, sw_ver: %s\n", HW_VERSION, SW_VERSION);

    rawtime = RTC_GetCounter();
    i = f_printf(&log_file, "Current time: %s\n", ctime (&rawtime));
    
    timeSec = 0;
    time10MSec = 0;
    
    if (canDiag)
    {
      i = f_printf(&log_file, "CAN 500kbit\n\n");
      LedSetColor(configNumber, slow);
      BuzzerSetMode(configNumber);
      result = vwtp(&config[configNumber-1], &log_file, debugState);
      
      switch (result)
      {
        case 0:
          timer2 = 500;
          i = f_printf(&log_file, "\n\nLogging terminated by user\n");
          break;
        
        case 1:
          LedSetColor(red, fast);
          BuzzerSetMode(0x82); //communication error
          timer2 = 2000;
          i = f_printf(&log_file, "\n\nConnection lost\n");
          break;
          
        case 2:
          i = CloseLogFile(&log_file);
          LedSetColor(amber, fast);
          BuzzerSetMode(0x83); //filesystem error
          MMC_PowerOff();
          while(buttonState != 2);
          NVIC_GenerateSystemReset();  
          break;
        
        case 11:
          LedSetColor(red, fast);
          BuzzerSetMode(0x82); //communication error
          timer2 = 2000;
          i = f_printf(&log_file, "\n\nCannot connect with ECU\n", result);
          break;

        default:
          LedSetColor(red, fast);
          BuzzerSetMode(0x82); //communication error
          timer2 = 2000;
          i = f_printf(&log_file, "\n\nCommunication error, error code = %d\n", result);
          break;
      }
    } 
    else //KW1281 diag
    {
      i = kw1281_max_init_attempts;
      do
      {
        result = ISO9141Init(&KW1281_SPEED);

        --i;
        if ((result>0) && (i>0))
        {
          timer2=1000;
          while(timer2);
        }
      } while (result && i);

      if (0 == result) //connected with ECU
      {
        i = f_printf(&log_file, "Connected @ %d baud %s%s\n\n", KW1281_SPEED, autobaud?"(autodetected)":"(set by user)", user_defined_timings?", user defined timings":"");
        LedSetColor(configNumber, slow);
        BuzzerSetMode(configNumber);
    
        buttonState = 0;
        i = kw1281_diag(&config[configNumber-1], &log_file, debugState);
      
        switch (i)
        {
          case 0:
            timer2 = 500;
            i = f_printf(&log_file, "\n\nLogging terminated by user\n");
            break;
        
          case 1:
            LedSetColor(red, fast);
            BuzzerSetMode(0x82); //communication error
            timer2 = 2000;
            i = f_printf(&log_file, "\n\nConnection lost\n");
            break;
          
          case 2:
          default:
            i = CloseLogFile(&log_file);
            LedSetColor(amber, fast);
            BuzzerSetMode(0x83); //filesystem error
            MMC_PowerOff();
            while(buttonState != 2);
            NVIC_GenerateSystemReset();  
        }
      }
      if (0 != result)
      {
        i = f_printf(&log_file, "Cannot connect with ECU: ");
        if (0xff == result)
        {
          i = f_printf(&log_file, "no response\n");
        }
        else if (0x7f == result)
        {
          i = f_printf(&log_file, "sync error - wrong speed (set to %d baud)?!\n", KW1281_SPEED);
        }
        else if (0x10 == result)
        {
          i = f_printf(&log_file, "not a KW1281 protocol\n");
        }
        else
        {
          i = f_printf(&log_file, "error code %d\n", result);
        }
        LedSetColor(red, fast); 
        BuzzerSetMode(0x82);
        timer2 = 2000;
      }
    } // end of KW1281 diag

    i = CloseLogFile(&log_file);
    file_number++;
    while (timer2>0);
  }
}