bit circ_buffer_put_nbytes(circ_buffer_t *b, uint8_t *bytes, uint16_t *n){
  uint16_t n_actual = 0;
  bit ret = PASS;
  uint8_t int_state = Get_interrupt_state();
  Disable_interrupt();

  while(b->num != b->size &&
          n_actual < *n){
    b->buf[b->end] = bytes[n_actual];
    b->end = b->end + 1;
    if(b->end == b->size)
      b->end = 0;
    b->num = b->num + 1;

    n_actual++;
  }

  if(n_actual < *n){
    ret = FAIL;
  }

  *n = n_actual;
  if(int_state != 0)
    Enable_interrupt();
  return ret;
}
Exemplo n.º 2
0
void Set_cpu_prescaler(U8 x)
{
   U8 save_int=Get_interrupt_state();
   Disable_interrupt();
   CLKPR=(1<<CLKPCE);
   CLKPR=x;
   if(save_int) { Enable_interrupt(); }
}
Exemplo n.º 3
0
//!
//! @brief This function initializes the USB device controller
//!
//! This function enables the USB controller and init the USB interrupts.
//! The aim is to allow the USB connection detection in order to send
//! the appropriate USB event to the operating mode manager.
//! Start device function is executed once VBUS connection has been detected
//! either by the VBUS change interrupt either by the VBUS high level
//!
//! @param none
//!
//! @return none
//!
void usb_start_device (void)
{
   Pll_start_auto();
   Wait_pll_ready();

   Disable_interrupt();
   Usb_unfreeze_clock();
   usb_init_device();         // configure the USB controller EP0
   Usb_attach();
   Enable_interrupt();
   
   Usb_enable_suspend_interrupt();
   Usb_enable_reset_interrupt();
#if (USB_OTG_FEATURE == ENABLED)
   Usb_enable_id_interrupt();
#endif
}
Exemplo n.º 4
0
//------------------------------------------------------------------------------
//  @fn main
//!
//! Core of "main_can_boot_loader.c".
//!
//! @brief: This application performs a CAN boot loader for AT90CAN128/64/32
//!         with aut-bitrate, IAP (In Application Programming) and API
//!         (Application Programming Interface).
//!
//! @warning: Don't forget to full locate the boot loader in "Boot Loader
//!           Flash Section".
//!           xxx_HWCB defined in "board.h" file
//!
//! @param none.
//!
//! @return Integer 0
//!
//------------------------------------------------------------------------------
int main (void)
{
    U8      u8_temp;
    U8      hwcb = FALSE;
    Bool    auto_b = 1;

//! --- First of all, disabling the Global Interrupt
    Disable_interrupt();


//turn off LED
	DDRG |= _BV(PING0);
	DDRG |= _BV(PING1);
	PORTG |= _BV(PING0);
	PORTG |= _BV(PING1);
//! --- If comming from RESET then test of the HardWare Condition Bit
    if (MCUSR != 0)
    {
        //- Clear all reset flags
        MCUSR = 0;


        //- Get HWCB (HardWare Condition Bit)
        DDRG &= ~_BV(PING0);      // HWCB pin in input
        PORTG |= _BV(PING0);        // Pull-up on HWCB
        hwcb = PING & _BV(PING0);
		DDRG |= _BV(PING0);

		//DEG check EEPROM for bootloader flag, and read bootloader configuration from there.
        //- Start application else start boot loader
			//check if alias is in EEPROM
		U16 alias;
		alias = (eeprom_rd_byte(0x0FF6)) << 8;
		alias |= eeprom_rd_byte(0x0FF7);
		if(alias != 0x0000)
		{
			if(alias != 0xFFFF)
			{
				put_conf_byte(NNB, (U8)(alias & 0x0F));
				put_conf_byte(CRIS, (U8)((alias >> 4) & 0xFF));
			}
			//else, just use the default values, that's fine.
			
			hwcb = 0; //force reboot into bootloader
		}
bit circ_buffer_put_byte(circ_buffer_t *b, uint8_t byte){
  bit ret = PASS;
  uint8_t int_state = Get_interrupt_state();
  Disable_interrupt();

  if(b->num == b->size){
    ret=FAIL;
    goto end;
  }
  b->buf[b->end] = byte;
  b->end = b->end + 1;
  if(b->end == b->size)
    b->end = 0;
  b->num = b->num + 1;

end:
  if(int_state != 0)
    Enable_interrupt();
  return ret;
}
bit circ_buffer_get_byte(circ_buffer_t *b, uint8_t *byte){
  bit ret = PASS;
  uint8_t int_state = Get_interrupt_state();
  Disable_interrupt();

  if(b->num == 0){
    ret = FAIL;
    goto end;
  }

  *byte = b->buf[b->start];
  b->start = b->start + 1;
  if(b->start == b->size)
    b->start = 0;
  b->num = b->num - 1;

end:
  if(int_state != 0)
    Enable_interrupt();
  return ret;
}
Exemplo n.º 7
0
int main (
  void
  )
{
  static UNSIGNED16 checksum;
  UNSIGNED8 bootCrcValid;
  UNSIGNED8 appCrcValid;

  // Disable all interrupts (just in case we jumped here)
  Disable_interrupt();
  
  // Initialize low-level hardware
  HW_Init();

  // Initialize Flash/EEPROM module and routines
  FLASH_Init();

  // Read node configuration and initialize status/error variables
  MAIN_Init();

  // Initialize Timer
  TIMER_Init();

  // Initialize CAN contoller
  CANHW_Init();

  // Initialize SDO handler
  SDO_Init();

  // Initialize Controls
  bootCrcValid = 0;
  appCrcValid = 0;

#if 0
  // If the bootloader is executed from power-up reset, initiate
  // waiting period of 60 seconds before attempting to start the
  // application.
  // Otherwise, we assume that the bootloader was called from the
  // application. In this case, send the bootup message (and don't
  // wait the 60 seconds).
  if ( gNodeStatus.run_mode == EXECMODE_POWERUP )
  {
    // Set the timer for the main waiting period in number of ticks
    TIMER_Set(TIMER_MAIN, WAIT_TIMEOUT*(1000/TIMERTICK_MS));
    gNodeStatus.run_mode = EXECMODE_TIMER;
  }
  else
  {
    gNodeStatus.run_mode = EXECMODE_NORMAL;
  }
#endif

  // Calculat bootloader checksum
  bootCrcValid = FLASH_Checksum_OK(BL_CHECKSUM_START, BL_CHECKSUM_END, BL_CHECKSUM_ADR, &checksum);

  // Check the bootloader (provides reference for embedded CRC)
  if ((0 == bootCrcValid) || (boot_crc == checksum))
  { // no error - do nothing
    ;
  }
  else
  { // Signal error. Extra info is the calculated checksum.
    MAIN_Signal_Error(ERRCODE_INTERNAL, checksum);
  }

  appCrcValid = FLASH_Checksum_OK(AP_CHECKSUM_START, AP_CHECKSUM_END, AP_CHECKSUM_ADR,   &checksum);

  // Check the main application checksum and set error status if wrong
  if (0 == appCrcValid)
  { // no error - do nothing
    ;
  }
  else
  { // Signal error. Extra info is the calculated checksum.
    MAIN_Signal_Error(ERRCODE_USER, checksum);
    // Don't auto-start application after timeout
    gNodeStatus.run_mode = EXECMODE_NORMAL;
  }

   // start_code: 0=power-up, 1=external, 2=brown-out, 3=watchdog, 4=J-Tag, 5=jump
   if ( (0 == bootCrcValid) ||
        (0 == appCrcValid) ||
        (2 == gNodeStatus.start_code) ||
        (3 == gNodeStatus.start_code) )
   {
      CANHW_Send_Boot();
      
      if ( (0 == bootCrcValid) )
      {
         CANHW_Send_Emergency(0x8000, 1, 0, 0, 0, 0);
      }
      else  if ( (2 == gNodeStatus.start_code) )
      {
         CANHW_Send_Emergency(0x8000, 2, 0, 0, 0, 0);
      }
      else if ( (3 == gNodeStatus.start_code) )
      {
         CANHW_Send_Emergency(0x8000, 3, 0, 0, 0, 0);
      }
      else if ( (0 == appCrcValid) )
      {
         CANHW_Send_Emergency(0x8000, 4, 0, 0, 0, 0);
      }

      gNodeStatus.run_mode = EXECMODE_NORMAL;
   }
   else if ( (0 == gNodeStatus.start_code) ||
             (1 == gNodeStatus.start_code) ||
             (4 == gNodeStatus.start_code) )
   {
      TIMER_Set(TIMER_MAIN, WAIT_TIMEOUT*(1000/TIMERTICK_MS));
      gNodeStatus.run_mode = EXECMODE_TIMER;
   }
   else if ( (5 == gNodeStatus.start_code) )
   {
      gNodeStatus.run_mode = EXECMODE_NORMAL;
   }
   else
   {
      // Set the location of the interrupt vectors to 0
      MCUCR = BIT(IVCE);
      MCUCR = 0x00;
      // Call the absolute address RESVEC_ADR
      ((void(*)(void))RESVEC_ADR)();
   }

  // Endless loop to execute the bootloader. The only exit point is
  // a jump to the application if the condition for execution is
  // fulfilled.
  for ( ;; )
  {
    BOOLEAN message_processed;

    // Update timers
    TIMER_Poll();

    // Serve the watchdog
    COP_Serve();

    // Check for a waiting CAN message and process them (SDO, NMT).
    // This can change the execution mode!
    message_processed = CANHW_Process_Messages();

    // If requested, check the application and jump to it, if successful
    if ( gNodeStatus.run_mode == EXECMODE_RUN )
    {
      // Jump to main application, if the checksum is ok
      if ( FLASH_Checksum_OK(AP_CHECKSUM_START, AP_CHECKSUM_END,
                             AP_CHECKSUM_ADR,   &checksum)     )
      {
        // Set the location of the interrupt vectors to 0
        MCUCR = BIT(IVCE);
        MCUCR = 0x00;
        // Call the absolute address RESVEC_ADR
        ((void(*)(void))RESVEC_ADR)();
      }
      else
      { // Checksum not valid: Don't start application and report error, stay here
        CANHW_Send_Emergency(0x8000, 4, 0, 0, 0, 0);
        MAIN_Signal_Error(ERRCODE_USER, checksum); // Signal error. Extra info is the calculated checksum.
        gNodeStatus.run_mode = EXECMODE_NORMAL;
      }
    }
    else if ( gNodeStatus.run_mode == EXECMODE_TIMER )
    { // check for other conditions when running in timed mode

      if ( message_processed )
      { // After a successfully received message leave the timed mode
        gNodeStatus.run_mode = EXECMODE_NORMAL;
      }
      else if ( TIMER_Expired(TIMER_MAIN) )
      { // Check for initial 60 second waiting period, if expired prepare
        // for running the application.
        gNodeStatus.run_mode = EXECMODE_RUN;
      }
      else
      { // do nothing
        ;
      }
    }
    else
    { // do nothing
      ;
    }
  } // for ever loop

  // don't return from here
  return(0);
}
//******************************************************************************
//  @fn main
//!
//! Core of "main_can_boot_loader.c".
//!
//! @brief: This application performs a CAN boot loader for AT90CAN128/64/32
//!         with aut-bitrate, IAP (In Application Programming) and API
//!         (Application Programming Interface).
//!
//! @warning: Don't forget to full locate the boot loader in "Boot Loader
//!           Flash Section".
//!           xxx_HWCB defined in "board.h" file
//!
//! @param none.
//!
//! @return Integer 0
//!
//******************************************************************************
int main (void)
{
    //U8      hwcb = FALSE;
    Bool    auto_b = 1;

    if(eeprom_rd_byte( (U16) ENNB  )  != 254)
      eeprom_wr_byte((U16) ENNB, 254);
       
    //DDRA = 0xFF;
	//PORTA = 0x00;

        //boot_conf[6] = 0x05;
		
    //uint8_t NNB;
    //NNB = eeprom_read_byte( (uint8_t*) 0 );
    //eeprom_update_byte((uint8_t*)0, NNB);
    
    
    //U8 mup = get_conf_byte(NNB);
    //U8 mupp[64];
    //------
    //put_conf_byte(NNB, 0x02);	//set node id
    //put_conf_byte(BSB, 0xFF);	//start programming
    //eeprom_wr_byte(NNB, 0x03);
    
    //------
    
    
//! --- First of all, disabling the Global Interrupt
    Disable_interrupt();

    
//! --- If comming from RESET then test of the HardWare Condition Bit
    if (MCUSR != 0)
    {
      //- Clear all reset flags
      MCUSR = 0;
    }
      if(eeprom_rd_byte( (U16) EBSB ) != 0xFF){
        // Start application
        //PORTA = 0xFF;
        isp_jump_to( (((U16)SA_H)<<8) | ((U16)SA_L));
      }
    

//! --- Open CAN communication

    can_communication_opened = 0;   // Not opened

    //- Pull-up on TxCAN & RxCAN one by one to use bit-addressing
    CAN_PORT_DIR &= ~(1<<CAN_INPUT_PIN );
    CAN_PORT_DIR &= ~(1<<CAN_OUTPUT_PIN);
    CAN_PORT_OUT |=  (1<<CAN_INPUT_PIN );
    CAN_PORT_OUT |=  (1<<CAN_OUTPUT_PIN);

    while (1)
    {
        auto_b = ((~auto_b)&0x01);      // Flip of "auto_b"

        //- Wait until activity on RxCAN
        while ((CAN_PORT_IN & (1<<CAN_INPUT_PIN)) != 0);

        //- Reset CAN peripheral
	    Can_reset();

	    //- Set CAN Bit-timming
        if (EB != 0xFF)
        {
//            CANBT1 = get_conf_byte(BTC1);
//            CANBT2 = get_conf_byte(BTC2);
//            CANBT3 = get_conf_byte(BTC3);
            
            CANBT1 = BTC1;
            CANBT2 = BTC2;
            CANBT3 = BTC3;

        }
        else
        //- Loop on auto-bitrate until it is performed
        {
            while ((Can_bit_timing(auto_b))==0);        // c.f. macro in "can_drv.h"
        }

	    //- Enable CAN peripheral
        can_clear_all_mob();                        // c.f. function in "can_drv.c"
        Can_enable();                               // c.f. macro in "can_drv.h"

//! --- CAN ISP protocol execution
        //- Initialization of CAN ISP PROTOCOL only when CAN communication NOT opened
        if(!can_communication_opened) can_isp_protocol_init();

        //- Exit with "0" only if CAN hardware error occurs
        while(can_isp_protocol_task());
    }
}   // End of "main"