示例#1
0
/**
 *  \brief SAMD21 SAM-BA Main loop.
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES 
	P_USB_CDC pCdc;
#endif
	DEBUG_PIN_HIGH;

	/* Jump in application if condition is satisfied */
	check_start_application();

	/* We have determined we should stay in the monitor. */
	/* System initialization */
	system_init();
	cpu_irq_enable();

	#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
	/* UART is enabled in all cases */
	usart_open();
#endif

#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
	pCdc = (P_USB_CDC)usb_init();
#endif
	DEBUG_PIN_LOW;

	// output on D13 (PA.17)
	LED_PORT.PINCFG[LED_PIN].reg &= ~ (uint8_t)(PORT_PINCFG_INEN);
	LED_PORT.DIRSET.reg = (uint32_t)(1 << LED_PIN);

	/* Wait for a complete enum on usb or a '#' char on serial line */
	while (1) {
   	        pulse_led(1); // while we're waiting, blink the D13 

#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
		if (pCdc->IsConfigured(pCdc) != 0) {
			main_b_cdc_enable = true;
		}

		//Check if a USB enumeration has succeeded
		//And com port was opened
		if (main_b_cdc_enable) {
			sam_ba_monitor_init(SAM_BA_INTERFACE_USBCDC);
			//SAM-BA on USB loop
			while(1) {
				sam_ba_monitor_run();
			}
		}
#endif
#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
		/* Check if a '#' has been received */
		if (!main_b_cdc_enable && usart_sharp_received()) {
			sam_ba_monitor_init(SAM_BA_INTERFACE_USART);
			/* SAM-BA on UART loop */
			while(1) {
				sam_ba_monitor_run();
			}
		}
#endif
	}
}
示例#2
0
/**
 *  \brief SAM-BA Main loop.
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{

	DEBUG_PIN_HIGH;

	/* Jump in application if condition is satisfied */
	check_start_application();

	/* We have determined we should stay in the monitor. */
	/* System initialization */
	system_init();

	/* UART is enabled in all cases */
	usart_open();

	DEBUG_PIN_LOW;
	/* Wait for a complete enum on usb or a '#' char on serial line */
	while (1) {
		/* Check if a '#' has been received */
		if (usart_sharp_received()) {
			sam_ba_monitor_init(SAM_BA_INTERFACE_USART);
			/* SAM-BA on UART loop */
			while(1) {
				sam_ba_monitor_run();
			}
		}
	}
}
/**
 *  \brief SAMD20 SAM-BA Main loop.
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
  
#if DEBUG_ENABLE
        /* Set BOOT_LED pin as output */
        PORT->Group[0].DIRSET.reg=(1u<<BOOT_LED);
#endif
	DEBUG_PIN_HIGH;
        
	/* Jump in application if condition is satisfied */
	check_start_application();
        
        /* Make OSC8M prescalar to zero rather divide by 8 */
        SYSCTRL->OSC8M.bit.PRESC = 0;

	/* UART is enabled in all cases */
	usart_open();

	/* Wait for a complete enum on usb or a '#' char on serial line */
	while (1) {
		/* Check if a '#' has been received */
		if (usart_sharp_received()) {
                        DEBUG_PIN_LOW;
			sam_ba_monitor_init(SAM_BA_INTERFACE_USART);
			/* SAM-BA on UART loop */
			while(1) {
				sam_ba_monitor_run();
			}
		}
	}
}
示例#4
0
/**
 *  \brief SAMD21 SAM-BA Main loop.
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
  P_USB_CDC pCdc;
#endif
  DEBUG_PIN_HIGH;

  /* Jump in application if condition is satisfied */
  check_start_application();

  /* We have determined we should stay in the monitor. */

  /* System initialization */
  board_init();
  __enable_irq();

  /* Initialize LEDs */
  LED_init();
  #if !defined(BOARD_LED_FADE_ENABLED)
  LED_on();
  #endif
  LEDRX_init();
  LEDRX_off();
  LEDTX_init();
  LEDTX_off();

  /* Start the sys tick (20 us) */
  SysTick_Config(VARIANT_MCK / 50000);


/* If SDCARD_ENABLED defined, read optional external pins and run SD Card bootloader (if enabled).
 */
  #if defined(SDCARD_ENABLED)
    uint8_t bootloaderMode = SD_BOOTLOADER_MODE_NO_UPDATE;
    #if defined(SAM_BA_INTERFACE_USE_PIN)
      uint8_t sdcardStatus = SD_BOOTLOADER_NOT_CALLED;
    #endif

    #if defined(SDCARD_USE_PIN1)
      pinConfig(SDCARD_PIN1_PORT, SDCARD_PIN1_PIN, SDCARD_PIN1_CONFIG);
    #endif
    #if defined(SDCARD_USE_PIN2)
      #if !defined(SDCARD_USE_PIN1)
        #error "main.c: When SDCARD_USE_PIN2 is defined, SDCARD_USE_PIN1 must also be defined"
      #endif
      pinConfig(SDCARD_PIN2_PORT, SDCARD_PIN2_PIN, SDCARD_PIN2_CONFIG);
    #endif

    #if (defined(SDCARD_USE_PIN1) && defined(SDCARD_USE_PIN2))
      bool pin1 = isPinActive(SDCARD_PIN1_PORT, SDCARD_PIN1_PIN, SDCARD_PIN1_POLARITY);
      bool pin2 = isPinActive(SDCARD_PIN2_PORT, SDCARD_PIN2_PIN, SDCARD_PIN2_POLARITY);
      if (pin1) {
        bootloaderMode = SD_BOOTLOADER_MODE_UPDATE;
      } else if (pin2) {
        bootloaderMode = SD_BOOTLOADER_MODE_UPDATE2;
      }
    #elif defined(SDCARD_USE_PIN1)
      if (isPinActive(SDCARD_PIN1_PORT, SDCARD_PIN1_PIN, SDCARD_PIN1_POLARITY)) {
        bootloaderMode = SD_BOOTLOADER_MODE_UPDATE;
      }
    #else
      bootloaderMode = SD_BOOTLOADER_MODE_UPDATE;
    #endif

    if (bootloaderMode != SD_BOOTLOADER_MODE_NO_UPDATE) {
    #if defined(SAM_BA_INTERFACE_USE_PIN)
      sdcardStatus = sdBootloader(bootloaderMode);
    #else
      sdBootloader(bootloaderMode);
    #endif
    }
  #endif

/* When using SAM_BA_INTERFACE_USE_PIN, check the pin to determine which SAM-BA interface to use.
 */
#if defined(SAM_BA_INTERFACE_USE_PIN)
  uint8_t sambaInterface = SAM_BA_NONE;
  pinConfig(SAM_BA_INTERFACE_PIN_PORT, SAM_BA_INTERFACE_PIN_PIN, SAM_BA_INTERFACE_PIN_CONFIG);

  if (isPinActive(SAM_BA_INTERFACE_PIN_PORT, SAM_BA_INTERFACE_PIN_PIN, PIN_POLARITY_ACTIVE_LOW)) {      // pin is low
  #if ((SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES) && (SAM_BA_INTERFACE_PIN_POLARITY == PIN_POLARITY_USBCDC_LOW))
    sambaInterface = SAM_BA_USBCDC_ONLY;
  #elif ((SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY) && (SAM_BA_INTERFACE_PIN_POLARITY == PIN_POLARITY_ACTIVE_LOW))
    sambaInterface = SAM_BA_USBCDC_ONLY;
  #elif ((SAM_BA_INTERFACE == SAM_BA_UART_ONLY) && (SAM_BA_INTERFACE_PIN_POLARITY == PIN_POLARITY_ACTIVE_LOW))
    sambaInterface = SAM_BA_UART_ONLY;
  #endif
  } else {      // pin is high
  #if ((SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES) && (SAM_BA_INTERFACE_PIN_POLARITY == PIN_POLARITY_USBCDC_HIGH))
    sambaInterface = SAM_BA_USBCDC_ONLY;
  #elif ((SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY) && (SAM_BA_INTERFACE_PIN_POLARITY == PIN_POLARITY_ACTIVE_HIGH))
    sambaInterface = SAM_BA_USBCDC_ONLY;
  #elif ((SAM_BA_INTERFACE == SAM_BA_UART_ONLY) && (SAM_BA_INTERFACE_PIN_POLARITY == PIN_POLARITY_ACTIVE_HIGH))
    sambaInterface = SAM_BA_UART_ONLY;
  #endif
  }
#endif

/* Check if we should continue with a SAM-BA interface (if compiled and enabled).
 * Only show LED_STATUS_FILE_NOT_FOUND if a SAM-BA interface is not available.
 * LED_STATUS_FILE_NOT_FOUND occurs if there is no SD Card, no FAT16/FAT32
 * filesystem, or no file (UPDATE.BIN or UPDATE2.BIN) in the root directory.
 */
#if defined(SAM_BA_INTERFACE_USE_PIN)
  #if defined(SDCARD_ENABLED)
  if (sambaInterface == SAM_BA_NONE) {
    if (sdcardStatus == SD_BOOTLOADER_FILE_NOT_FOUND) {
      LED_status(LED_STATUS_FILE_NOT_FOUND);
    } else if (sdcardStatus == SD_BOOTLOADER_NOT_CALLED) {
      LED_status(LED_STATUS_NO_SAM_BA_INTERFACE);
    }
  }
  #else
    #if (SAM_BA_INTERFACE == SAM_BA_NONE)
      LED_status(LED_STATUS_NO_SAM_BA_INTERFACE);
    #else
    if (sambaInterface == SAM_BA_NONE) {
      LED_status(LED_STATUS_NO_SAM_BA_INTERFACE);
    }
    #endif
  #endif
#else
  #if (SAM_BA_INTERFACE == SAM_BA_NONE)
    LED_status(LED_STATUS_NO_SAM_BA_INTERFACE);
  #endif
#endif

/* Enable the appropriate SAM-BA interfaces. When using SAM_BA_INTERFACE_USE_PIN,
 * the peripheral and pins are only setup for the selected/enabled interface.
 */
#if defined(SAM_BA_INTERFACE_USE_PIN)
  if (sambaInterface == SAM_BA_UART_ONLY) {
#endif

#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
  /* UART is enabled in all cases */
    serial_open();
#endif

#if defined(SAM_BA_INTERFACE_USE_PIN)
  } else if (sambaInterface == SAM_BA_USBCDC_ONLY) {
#endif

#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
    pCdc = usb_init();
#endif

#if defined(SAM_BA_INTERFACE_USE_PIN)
  }
#endif


  ledDirection = 4;
  DEBUG_PIN_LOW;

  /* Wait for a complete enum on usb or a '#' char on serial line */
  while (1)
  {
#if defined(SAM_BA_INTERFACE_USE_PIN)
    if (sambaInterface == SAM_BA_USBCDC_ONLY) {
#endif

#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
    if (pCdc->IsConfigured(pCdc) != 0)
    {
      main_b_cdc_enable = true;
    }

    /* Check if a USB enumeration has succeeded and if comm port has been opened */
    if (main_b_cdc_enable)
    {
      sam_ba_monitor_init(SAM_BA_INTERFACE_USBCDC);
      /* SAM-BA on USB loop */
      while( 1 )
      {
        sam_ba_monitor_run();
      }
    }
#endif

#if defined(SAM_BA_INTERFACE_USE_PIN)
    } else if (sambaInterface == SAM_BA_UART_ONLY) {
#endif

#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
    /* Check if a '#' has been received */
    if (!main_b_cdc_enable && serial_sharp_received())
    {
      sam_ba_monitor_init(SAM_BA_INTERFACE_USART);
      /* SAM-BA on Serial loop */
      while(1)
      {
        sam_ba_monitor_run();
      }
    }
#endif

#if defined(SAM_BA_INTERFACE_USE_PIN)
    }
#endif
  }
}
示例#5
0
/**
 *  \brief SAMD21 SAM-BA Main loop.
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
  P_USB_CDC pCdc;
#endif
  DEBUG_PIN_HIGH;

  /* Jump in application if condition is satisfied */
  check_start_application();

  /* We have determined we should stay in the monitor. */
  /* System initialization */
  board_init();
  __enable_irq();

#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
  /* UART is enabled in all cases */
  serial_open();
#endif

#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
  pCdc = usb_init();
#endif

  DEBUG_PIN_LOW;

  LED_on();

  /* Wait for a complete enum on usb or a '#' char on serial line */
  while (1)
  {
    pulse_led(3); // while we're waiting, blink the D13 

#if SAM_BA_INTERFACE == SAM_BA_USBCDC_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
    if (pCdc->IsConfigured(pCdc) != 0)
    {
      main_b_cdc_enable = true;
    }

    /* Check if a USB enumeration has succeeded and if comm port has been opened */
    if (main_b_cdc_enable)
    {
      sam_ba_monitor_init(SAM_BA_INTERFACE_USBCDC);
      /* SAM-BA on USB loop */
      while( 1 )
      {
        sam_ba_monitor_run();
      }
    }
#endif

#if SAM_BA_INTERFACE == SAM_BA_UART_ONLY  ||  SAM_BA_INTERFACE == SAM_BA_BOTH_INTERFACES
    /* Check if a '#' has been received */
    if (!main_b_cdc_enable && serial_sharp_received())
    {
      sam_ba_monitor_init(SAM_BA_INTERFACE_USART);
      /* SAM-BA on Serial loop */
      while(1)
      {
        sam_ba_monitor_run();
      }
    }
#endif
  }
}
示例#6
0
/**
 *  \brief SAMD21 WINO SAM-BA Main loop.
 *  \return Unused (ANSI-C compatibility).
 */
int main(void)
{
	/* Save the value of the boot key memory before it is overwritten */
	uint32_t bootKeyPtrVal = *bootKeyPtr;
	*bootKeyPtr = 0;
  
	P_USB_CDC pCdc;

	DEBUG_PIN_HIGH;

	volatile PortGroup *esp8266_port = (volatile PortGroup *)(&(PORT->Group[ESP8266_CH_PD_PIN / 32]));

	/* Enable the ouput on the ESP8266 power down Pin and drive it to low */
	esp8266_port->DIRSET.reg = ESP8266_CH_PD_PIN_MASK;
	esp8266_port->PINCFG[ESP8266_CH_PD_PIN & 0x1F].reg = PORT_PINCFG_INEN;
	esp8266_port->OUTCLR.reg = ESP8266_CH_PD_PIN_MASK;

	/* Enable the ouput on the LED Pin and drive it to high */
	PORT->Group[LED_PORT].DIRSET.reg = LED_PIN_MASK;
	LED_ON;

	/* System initialization */
	system_init();
	cpu_irq_enable();
	usart_open();
	pCdc = (P_USB_CDC)usb_init();
  
	DEBUG_USART_OPEN;
  
	/* Pointer to store application start address */
	uint32_t *data_ptr = (uint32_t *)0x20000000;
	
	/* Store the application start address @0x20000000 */
	*data_ptr = APP_START_ADDRESS;

	/* Jump in application if condition is satisfied */
	static uint32_t app_start_address;

	/* Load the Reset Handler address of the application */
	app_start_address = *(uint32_t *)(APP_START_ADDRESS + 4);
	
	int waiting = WAIT_SHORT;
	int elapsed;

	/* Test reset vector of application @APP_START_ADDRESS+4
	 * If *(APP_START+0x4) == 0xFFFFFFFF then set waiting
	 * Application erased condition
	 */
	if (app_start_address == 0xFFFFFFFF)
	{
		waiting = -1;
	}
	else if (bootKeyPtrVal == bootKey)
	{
		waiting = WAIT_LONG;
	}
  
	DEBUG_PIN_LOW;
  
	*bootKeyPtr = bootKey;
  
	/* Wait for a complete enum on usb or a '#' char on serial line.
	 * If there is no user application in the device's memory,
	 * this loop will not end, in the other case, it will wait until timeout.
	 */
	while (waiting)
	{
		if (pCdc->IsConfigured(pCdc))
		{
			sam_ba_monitor_init(SAM_BA_INTERFACE_USBCDC);
			main_b_cdc_enable = true;
			break;
		}
		
		if (usart_sharp_received())
		{
			sam_ba_monitor_init(SAM_BA_INTERFACE_USART);
			break;
		}
		
		if (PORT->Group[LED_PORT].OUT.reg == LED_PIN_MASK)
		{
			if (elapsed > 3000)
			{
				LED_OFF;
				elapsed = 0;
			}
		}
		else
		{
			if (elapsed > 30000)
			{
				LED_ON;
				elapsed = 0;
			}
		}
		
		if (waiting > 0)
			waiting--;
			
		elapsed++;
	}

	*bootKeyPtr = 0;
	while (waiting)
	{
		uint32_t tmp = do_sam_ba_monitor();

		if (PORT->Group[LED_PORT].OUT.reg == LED_PIN_MASK)
		{
			if (elapsed > (1 ? 5000 : 1))
			{
				LED_OFF;
				elapsed = 0;
			}
		}
		else
		{
			if (elapsed > (1 ? 50000 : 2))
			{
				LED_ON;
				elapsed = 0;
			}
		}
   
		if (waiting > 0)
		{
			/* If there is no activity, decrement timeout */
			/* Else set WAIT_SHORT timeout */
			if (tmp == 0)
				waiting--;
			else
				waiting = WAIT_SHORT;
		}
		
		elapsed++;
	}
	
	/* Reset LED pin */
	LED_OFF;
	PORT->Group[LED_PORT].DIRCLR.reg = LED_PIN_MASK;
  
	usart_close();
	usb_deinit();
	
	// reset clocks
	
	//SYSCTRL->OSC8M.reg = SYSCTRL_OSC8M_RESETVALUE;
	//SYSCTRL->OSC32K.reg = SYSCTRL_OSC32K_RESETVALUE;
	//SYSCTRL->XOSC32K.reg = SYSCTRL_XOSC32K_RESETVALUE;
	//SYSCTRL->DFLLCTRL.reg = SYSCTRL_DFLLCTRL_RESETVALUE;
	//SYSCTRL->DFLLMUL.reg = SYSCTRL_DFLLMUL_RESETVALUE;
	//SYSCTRL->DFLLVAL.reg = SYSCTRL_DFLLVAL_RESETVALUE;
	
	/* Jump to the application */
  
	/* Rebase the Stack Pointer */
	__set_MSP(*(uint32_t *) APP_START_ADDRESS);

	/* Rebase the vector table base address */
	SCB->VTOR = ((uint32_t) APP_START_ADDRESS & SCB_VTOR_TBLOFF_Msk);

	/* Jump to application Reset Handler in the application */
	asm("bx %0"::"r"(app_start_address));
}