コード例 #1
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);
  }
}
コード例 #2
0
ファイル: usb_dev_msc.c プロジェクト: ev3rt-git/ev3rt-hrp3
//*****************************************************************************
//
// This is the main loop that runs the application.
//
//*****************************************************************************
static int
main(void)
{
//	tRectangle sRect;
    unsigned int index, i;
	unsigned char *src, *dest;

	 /* Sets up 'Level 1" page table entries.
     * The page table entry consists of the base address of the page
     * and the attributes for the page. The following operation is to
     * setup one-to-one mapping page table for DDR memeory range and set
     * the atributes for the same. The DDR memory range is from 0xC0000000
     * to 0xDFFFFFFF. Thus the base of the page table ranges from 0xC00 to
     * 0xDFF. Cache(C bit) and Write Buffer(B bit) are enabled  only for
     * those page table entries which maps to DDR RAM and internal RAM.
     * All the pages in the DDR range are provided with R/W permissions
     */

#ifdef DMA_MODE
	for(index = 0; index < (4*1024); index++)
    {
         if((index >= 0xC00 && index < 0xE00)|| (index == 0x800))
         {
              pageTable[index] = (index << 20) | 0x00000C1E;
         }
         else
         {
              pageTable[index] = (index << 20) | 0x00000C12;
         }
    }

	/* Configures translation table base register
	* with pagetable base address.
	*/
    CP15TtbSet((unsigned int )pageTable);

    /* Enables MMU */
   	CP15MMUEnable();

	/* Enable Data Cache */
  	CP15DCacheEnable();

	/* Disable Instruction Cache*/
	CP15ICacheDisable();
#endif

#if 0 // No LCD -- ertl-liyixiao
    //
    //configures arm interrupt controller to generate raster interrupt
    //
    SetupIntc();

	//
	//Configures raster to display image
	//
	SetUpLCD();

	/* configuring the base ceiling */
	RasterDMAFBConfig(SOC_LCDC_0_REGS,
					  (unsigned int)(g_pucBuffer+PALETTE_OFFSET),
					  (unsigned int)(g_pucBuffer+PALETTE_OFFSET) + sizeof(g_pucBuffer) - 2 - PALETTE_OFFSET,
					  0);

	RasterDMAFBConfig(SOC_LCDC_0_REGS,
					  (unsigned int)(g_pucBuffer+PALETTE_OFFSET),
					  (unsigned int)(g_pucBuffer+PALETTE_OFFSET) + sizeof(g_pucBuffer) - 2 - PALETTE_OFFSET,
					  1);

	// Copy palette info into buffer
	src = (unsigned char *) palette_32b;
	dest = (unsigned char *) (g_pucBuffer+PALETTE_OFFSET);
	for( i = 4; i < (PALETTE_SIZE+4); i++)
	{
		*dest++ = *src++;
	}

	GrOffScreen16BPPInit(&g_sSHARP480x272x16Display, g_pucBuffer, LCD_WIDTH, LCD_HEIGHT);

	// Initialize a drawing context.
	GrContextInit(&g_sContext, &g_sSHARP480x272x16Display);

	/* enable End of frame interrupt */
	RasterEndOfFrameIntEnable(SOC_LCDC_0_REGS);

	/* enable raster */
	RasterEnable(SOC_LCDC_0_REGS);
	ConfigRasterDisplayEnable();


    //
    // Fill the top 15 rows of the screen with blue to create the banner.
    //
    sRect.sXMin = 0;
    sRect.sYMin = 0;
    sRect.sXMax = GrContextDpyWidthGet(&g_sContext) - 1;
    sRect.sYMax = DISPLAY_BANNER_HEIGHT;
    GrContextForegroundSet(&g_sContext, ClrDarkBlue);
    GrRectFill(&g_sContext, &sRect);

    //
    // Put a white box around the banner.
    //
    GrContextForegroundSet(&g_sContext, ClrWhite);
    GrRectDraw(&g_sContext, &sRect);

    //
    // Put the application name in the middle of the banner.
    //
    GrContextFontSet(&g_sContext, &g_sFontCm20);
    GrStringDrawCentered(&g_sContext, "usb-dev-msc", -1,
                         GrContextDpyWidthGet(&g_sContext) / 2, 10, 0);

    //
    // Initialize the idle timeout and reset all flags.
    //
    g_ulIdleTimeout = 0;
    g_ulFlags = 0;

    //
    // Initialize the state to idle.
    //
    g_eMSCState = MSC_DEV_IDLE;

    //
    // Draw the status bar and set it to idle.
    //
    UpdateStatus("Idle", 1);
#endif

	//SetupAINTCInt();
	//ConfigureAINTCIntUSB();
	//DelayTimerSetup();

#if 0 // No LCD
	SystickConfigure(SysTickHandler);
	SystickPeriodSet(10);
	SystickEnable();
#endif

    USBDMSCInit(0, (tUSBDMSCDevice *)&g_sMSCDevice);



#ifdef DMA_MODE
	Cppi41DmaInit(USB_INSTANCE, epInfo, NUMBER_OF_ENDPOINTS);

	for(;g_bufferIndex < NUM_OF_RX_BDs; g_bufferIndex++)
	{
		dataBuffer = (unsigned char *)cppiDmaAllocBuffer();
		doDmaRxTransfer(USB_INSTANCE, USB_MSC_BUFER_SIZE, dataBuffer,
							g_sMSCDevice.psPrivateData->ucOUTEndpoint);
	}
#endif

#if 0 // No LCD -- ertl-liyixiao
	//
    // Drop into the main loop.
    //
     //
    // Drop into the main loop.
	//

    while(1)
    {
        switch(g_eMSCState)
        {
            case MSC_DEV_READ:
            {
                //
                // Update the screen if necessary.
                //
                if(g_ulFlags & FLAG_UPDATE_STATUS)
                {
                    UpdateStatus("Reading", 0);
                    g_ulFlags &= ~FLAG_UPDATE_STATUS;
                }

                //
                // If there is no activity then return to the idle state.
                //
                if(g_ulIdleTimeout == 0)
                {
                    UpdateStatus("Idle     ", 0);
                    g_eMSCState = MSC_DEV_IDLE;
                }

                break;
            }
            case MSC_DEV_WRITE:
            {
                //
                // Update the screen if necessary.
                //
                if(g_ulFlags & FLAG_UPDATE_STATUS)
                {
                    UpdateStatus("Writing ", 0);
                    g_ulFlags &= ~FLAG_UPDATE_STATUS;
                }

                //
                // If there is no activity then return to the idle state.
                //
                if(g_ulIdleTimeout == 0)
                {
                    UpdateStatus("Idle     ", 0);
                    g_eMSCState = MSC_DEV_IDLE;
                }
                break;
            }
            case MSC_DEV_IDLE:
            default:
            {
                break;
            }
        }
    }
#endif

}