Commits

Andrew Cooper committed 7cbc410

move at90usb128 and common files to arch/ subdirectory

  • Participants
  • Parent commits 8aca957

Comments (0)

Files changed (64)

File arch/at90usb128/lib_board/stk_525/stk525.gif

Added
New image

File arch/at90usb128/lib_board/stk_525/stk_525.c

+/**
+ * @file
+ *
+ * @brief This file contains the low level functions for the ADC
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+//_____  I N C L U D E S ___________________________________________________
+
+#include "config.h"
+#include "stk_525.h"
+
+#if (TARGET_BOARD==STK525)
+
+/// this define is set on config.h file
+#ifdef USE_ADC
+#include "lib_mcu/adc/adc_drv.h"
+
+//_____ M A C R O S ________________________________________________________
+
+
+//_____ P R I V A T E    D E C L A R A T I O N _____________________________
+
+
+//_____ D E F I N I T I O N ________________________________________________
+
+/**
+ * The following table give the correspondance between the ADC code and the temperature
+ * Each elements of the table corresponds to an ADC code value.
+ * The index in the table give the corresponding temperature (in°C) with
+ * the following formula : Tempeature=index-20.
+ * table[O] corresponds to -20°C temperature code
+ * The following table gives the ADC code for VCC=3.3V and Aref=AVcc
+ */
+code U16 temperature_code[]=
+	{0x3B4,0x3B0,0x3AB,0x3A6,0x3A0,0x39A,0x394,0x38E,0x388,0x381,0x37A,0x373,
+	0x36B,0x363,0x35B,0x353,0x34A,0x341,0x338,0x32F,0x325,0x31B,0x311,0x307,
+	0x2FC,0x2F1,0x2E6,0x2DB,0x2D0,0x2C4,0x2B8,0x2AC,0x2A0,0x294,0x288,0x27C,
+	0x26F,0x263,0x256,0x24A,0x23D,0x231,0x225,0x218,0x20C,0x200,0x1F3,0x1E7,
+	0x1DB,0x1CF,0x1C4,0x1B8,0x1AC,0x1A1,0x196,0x18B,0x180,0x176,0x16B,0x161,
+	0x157,0x14D,0x144,0x13A,0x131,0x128,0x11F,0x117,0x10F,0x106,0xFE,0xF7,
+	0xEF,0xE8,0xE1,0xDA,0xD3,0xCD,0xC7,0xC0,0xBA,0xB5,0xAF,0xAA,0xA4,0x9F,
+	0x9A,0x96,0x91,0x8C,0x88,0x84,0x80,0x7C,0x78,0x74,0x71,0x6D,0x6A,0x67,
+	0x64,0x61,0x5E,0x5B,0x58,0x55,0x53,0x50,0x4E,0x4C,0x49,0x47,0x45,0x43,
+	0x41,0x3F,0x3D,0x3C,0x3A,0x38};
+
+//_____ D E C L A R A T I O N ______________________________________________
+
+#ifdef __ICCAVR__
+#pragma diag_suppress=Pa082
+#endif
+
+U16 Get_adc_mic_val(void)
+	{
+	Start_conv_channel(ADC_MIC_CH);
+	while (!Is_adc_conv_finished());
+	return Adc_get_10_bits_result();
+	}
+
+U16 Get_adc_pot_val(void)
+	{
+	Start_conv_channel(ADC_POT_CH);
+	while (!Is_adc_conv_finished());
+	return Adc_get_10_bits_result();
+	}
+
+U16 Get_adc_temp_val(void)
+	{
+	Start_conv_channel(ADC_TEMP_CH);
+	while (!Is_adc_conv_finished());
+	return Adc_get_10_bits_result();
+	}
+
+S16 Read_temperature(void)
+	{
+#ifndef __GNUC__
+	U16 adc_code;
+	S8 index=0;
+	adc_code=Get_adc_temp_val();
+	if(adc_code>temperature_code[0])
+		{
+		return (S16)(-20);
+		}
+	else
+		{
+		while(temperature_code[index++]>adc_code);
+		return (S16)(index-1-20);
+		}
+#else
+	U16 adc_code;
+	S8 index=0;
+	adc_code=Get_adc_temp_val();
+	if(adc_code>pgm_read_word_near(&temperature_code))
+		{
+		return (S16)(-20);
+		}
+	else
+		{
+		while(pgm_read_word_near(&temperature_code[index++])>adc_code);
+		return (S16)(index-1-20);
+		}
+
+#endif
+	}
+
+#endif   // USE_ADC
+#endif   // (TARGET_BOARD==STK525)

File arch/at90usb128/lib_board/stk_525/stk_525.h

+/**
+ * @file
+ *
+ * @brief This file contains the low level macros and definition for stk525 board
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef STK_525_H
+#define STK_525_H
+
+//_____ I N C L U D E S ____________________________________________________
+#include "config.h"
+
+#if (TARGET_BOARD==STK525)
+
+//_____ M A C R O S ________________________________________________________
+
+/**
+ * @defgroup STK525_module STK525
+ * This module contains low level hardware abstraction layer for STK525 board
+ * @image html stk525.gif
+ * @{
+ */
+
+/// @defgroup STK_525_leds Leds Management
+/// Macros to manage Leds on STK525
+/// @{
+#define  LED_PORT             PORTD
+#define  LED_DDR              DDRD
+#define  LED_PIN              PIND
+#define  LED0_BIT             PIND4
+#define  LED1_BIT             PIND5
+#define  LED2_BIT             PIND6
+#define  LED3_BIT             PIND7
+
+#define  Leds_init()          (LED_DDR  |=  (1<<LED0_BIT) | (1<<LED1_BIT) | (1<<LED2_BIT) | (1<<LED3_BIT))
+#define  Leds_on()            (LED_PORT |=  (1<<LED0_BIT) | (1<<LED1_BIT) | (1<<LED2_BIT) | (1<<LED3_BIT))
+#define  Leds_off()           (LED_PORT &= ~((1<<LED0_BIT) | (1<<LED1_BIT) | (1<<LED2_BIT) | (1<<LED3_BIT)))
+#define  Leds_set_val(c)      (Leds_off(),LED_PORT |= (c<<4)&((1<<LED0_BIT) | (1<<LED1_BIT) | (1<<LED2_BIT) | (1<<LED3_BIT)))
+#define  Leds_get_val()       (LED_PORT>>4)
+
+#define  Led0_on()            (LED_PORT |=  (1<<LED0_BIT))
+#define  Led1_on()            (LED_PORT |=  (1<<LED1_BIT))
+#define  Led2_on()            (LED_PORT |=  (1<<LED2_BIT))
+#define  Led3_on()            (LED_PORT |=  (1<<LED3_BIT))
+#define  Led0_off()           (LED_PORT &= ~(1<<LED0_BIT))
+#define  Led1_off()           (LED_PORT &= ~(1<<LED1_BIT))
+#define  Led2_off()           (LED_PORT &= ~(1<<LED2_BIT))
+#define  Led3_off()           (LED_PORT &= ~(1<<LED3_BIT))
+#define  Led0_toggle()        (LED_PIN  |=  (1<<LED0_BIT))
+#define  Led1_toggle()        (LED_PIN  |=  (1<<LED1_BIT))
+#define  Led2_toggle()        (LED_PIN  |=  (1<<LED2_BIT))
+#define  Led3_toggle()        (LED_PIN  |=  (1<<LED3_BIT))
+#define  Is_led0_on()         (LED_PIN  &   (1<<LED0_BIT) ? TRUE : FALSE)
+#define  Is_led1_on()         (LED_PIN  &   (1<<LED1_BIT) ? TRUE : FALSE)
+#define  Is_led2_on()         (LED_PIN  &   (1<<LED2_BIT) ? TRUE : FALSE)
+#define  Is_led3_on()         (LED_PIN  &   (1<<LED3_BIT) ? TRUE : FALSE)
+/// @}
+
+/// @defgroup STK_525_joy Joystick Management
+/// Macros to manage Joystick on STK525
+/// @{
+#define  Joy_init()           (DDRB &= ~((1<<PINB7)|(1<<PINB6)|(1<<PINB5)), PORTB |= ((1<<PINB7)|(1<<PINB6)|(1<<PINB5)), DDRE &= ~((1<<PINE5)|(1<<PINE4)), PORTE |= ((1<<PINE5)|(1<<PINE4)))
+
+#define  Is_btn_left()        Is_hwb()
+#define  Is_btn_middle()      ((PINB & (1<<PINB5)) ?  FALSE : TRUE)
+#define  Is_btn_right()       FALSE
+
+#define  Is_joy_left()        ((PINB & (1<<PINB6)) ?  FALSE : TRUE)
+#define  Is_joy_up()          ((PINB & (1<<PINB7)) ?  FALSE : TRUE)
+#define  Is_joy_down()        ((PINE & (1<<PINE5)) ?  FALSE : TRUE)
+#define  Is_joy_right()       ((PINE & (1<<PINE4)) ?  FALSE : TRUE)
+
+#define  Is_btn_not_left()    Is_not_hwb()
+#define  Is_btn_not_middle()  ((PINB & (1<<PINB5)) ?  TRUE : FALSE)
+#define  Is_btn_not_right()   TRUE
+
+#define  Is_joy_not_up()      ((PINB & (1<<PINB7)) ?  TRUE : FALSE)
+#define  Is_joy_not_left()    ((PINB & (1<<PINB6)) ?  TRUE : FALSE)
+#define  Is_joy_not_down()    ((PINE & (1<<PINE5)) ?  TRUE : FALSE)
+#define  Is_joy_not_right()   ((PINE & (1<<PINE4)) ?  TRUE : FALSE)
+
+* Enable interrupts for switches (PINB5 to PINB7)
+* - Set up interrupts for switches
+* - clear flag
+* - enable interrupt
+#define  Switches_enable_it() {  \
+      PCMSK0 |= (1<<PCINT5)|(1<<PCINT6)|(1<<PCINT7);\
+      PCIFR |= (1<<PCIF0);       \
+      PCICR |= (1<<PCIE0);}
+
+* Disable interrupts for switches (PINB5 to PINB7)
+* - disable interrupt
+* - clear flag
+* - Clear mask
+#define  Switches_disable_it() { \
+      PCICR  &= ~(1<<PCIE0);     \
+      PCIFR  |= (1<<PCIF0);      \
+      PCMSK0 &= ~((1<<PCINT5)|(1<<PCINT6)|(1<<PCINT7));}
+/// @}
+
+
+/// @defgroup USB_key_HWB HWB button management
+/// HWB button is connected to PE2 and can also
+/// be used as generic push button
+/// @{
+#define  Hwb_button_init()    (DDRE  &= ~(1<<PINE2), PORTE |= (1<<PINE2))
+#define  Is_hwb()             ((PINE &   (1<<PINE2)) ?  FALSE : TRUE)
+#define  Is_not_hwb()         ((PINE &   (1<<PINE2)) ?  TRUE : FALSE)
+/// @}
+
+
+/// @defgroup STK_525_df_STK525 ATMEL Hardware data flash configuration
+/// Macros to init the environnement for DF on STK525
+/// @{
+
+// One AT45DB321C
+#define DF_4_MB
+#define DF_NB_MEM          1
+
+// PINB0 : SPI Slave Select pin, this pin must be disable (disable input mode) because it is used by joystick
+//         Note: the joystick can work fine in Output mode, because the pull-up resistor is activated and the level pin can be read.
+// PINB1 : the SCK signal direction is OUTPUT
+// PINB2 : the SI signal direction is OUTPUT
+// PINB3 : the SO signal direction is INPUT
+// PINB4 : the CS signal direction is OUTPUT
+#define  Df_init_spi()     (DDRB  &= ~((1<<PINB3)), \
+                            DDRB  |=  ((1<<PINB4)|(1<<PINB2)|(1<<PINB1)|(1<<PINB0)), \
+                            PORTB |=  ((1<<PINB4)|(1<<PINB3)|(1<<PINB2)|(1<<PINB1)|(1<<PINB0)) )
+#define  Df_select_0()     (PORTB &= ~(1<<PINB4))
+#define  Df_desel_all()    (PORTB |=  (1<<PINB4))
+/// @}
+
+/// @}
+
+
+///STK 525 ADC Potentiometer Channel Definition
+#define ADC_POT_CH   0x01
+#define ADC_MIC_CH   0x02
+#define ADC_TEMP_CH  0x00
+
+#ifdef USE_ADC       ///< this define is set in config.h file
+/**
+ * @brief Get_adc_mic_val.
+ *
+ * This function performs an ADC conversion from the stk525 MIC channel
+ * an returns the 10 bits result in an U16 value.
+ *
+ * @warning USE_ADC should be defined in config.h
+ *
+ * @param none
+ *
+ * @return U16 microphone sample value.
+ */
+U16 Get_adc_mic_val(void);
+
+/**
+ * @brief Get_adc_temp_val.
+ *
+ * This function performs an ADC conversion from the stk525 TEMP channel
+ * an returns the 10 bits result in an U16 value.
+ *
+ * @warning USE_ADC should be defined in config.h
+ *
+ * @param none
+ *
+ * @return U16 analog sensor temperature value.
+ */
+U16 Get_adc_temp_val(void);
+
+/**
+ * @brief Get_adc_pot_val.
+ *
+ * This function performs an ADC conversion from the stk525 POT channel
+ * an returns the 10 bits result in an U16 value.
+ *
+ * @warning USE_ADC should be defined in config.h
+ *
+ * @param none
+ *
+ * @return U16 analog potentiometer value.
+ */
+U16 Get_adc_pot_val(void);
+
+/**
+ * @brief Read_temperature.
+ *
+ * This function performs an ADC conversion from the stk525 POT channel
+ * an returns the 10 bits result of the temperature (in °C) in an S16 value.
+ *
+ * @warning USE_ADC should be defined in config.h
+ *
+ * @param none
+ *
+ * @return S16 temperature value in °C.
+ */
+S16 Read_temperature(void);
+
+#endif   // USE_ADC
+#endif   // TARGET_BOARD==STK525
+#endif   // STK_525_H

File arch/at90usb128/lib_board/usb_key/avrusbkey.gif

Added
New image

File arch/at90usb128/lib_board/usb_key/usb_key.c

+/**
+ * @file
+ *
+ * @brief This file contains the low level functions for USB KEY hardware
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+//_____  I N C L U D E S ___________________________________________________
+
+#include "config.h"
+#include "usb_key.h"
+
+#if (TARGET_BOARD==USBKEY)
+
+/// this define is set on config.h file
+#ifdef USE_ADC
+#include "lib_mcu/adc/adc_drv.h"
+
+//_____ M A C R O S ________________________________________________________
+
+
+//_____ P R I V A T E    D E C L A R A T I O N _____________________________
+
+
+//_____ D E F I N I T I O N ________________________________________________
+
+/**
+ * The following table give the correspondance between the ADC code and the temperature
+ * Each elements of the table corresponds to an ADC code value.
+ * The index in the table give the corresponding temperature (in °C) with
+ * the following formula : Tempeature=index-20.
+ * table[O] corresponds to -20°C temperature code
+ * The following table gives the ADC code for VCC=3.3V and Aref=AVcc
+ */
+code U16 temperature_code[]=
+	{0x3B4,0x3B0,0x3AB,0x3A6,0x3A0,0x39A,0x394,0x38E,0x388,0x381,0x37A,0x373,
+	0x36B,0x363,0x35B,0x353,0x34A,0x341,0x338,0x32F,0x325,0x31B,0x311,0x307,
+	0x2FC,0x2F1,0x2E6,0x2DB,0x2D0,0x2C4,0x2B8,0x2AC,0x2A0,0x294,0x288,0x27C,
+	0x26F,0x263,0x256,0x24A,0x23D,0x231,0x225,0x218,0x20C,0x200,0x1F3,0x1E7,
+	0x1DB,0x1CF,0x1C4,0x1B8,0x1AC,0x1A1,0x196,0x18B,0x180,0x176,0x16B,0x161,
+	0x157,0x14D,0x144,0x13A,0x131,0x128,0x11F,0x117,0x10F,0x106,0xFE,0xF7,
+	0xEF,0xE8,0xE1,0xDA,0xD3,0xCD,0xC7,0xC0,0xBA,0xB5,0xAF,0xAA,0xA4,0x9F,
+	0x9A,0x96,0x91,0x8C,0x88,0x84,0x80,0x7C,0x78,0x74,0x71,0x6D,0x6A,0x67,
+	0x64,0x61,0x5E,0x5B,0x58,0x55,0x53,0x50,0x4E,0x4C,0x49,0x47,0x45,0x43,
+	0x41,0x3F,0x3D,0x3C,0x3A,0x38};
+
+//_____ D E C L A R A T I O N ______________________________________________
+
+#ifdef __ICCAVR__
+#pragma diag_suppress=Pa082
+#endif
+
+U16 Get_adc_temp_val(void)
+	{
+	Start_conv_channel(ADC_TEMP_CH);
+	while (!Is_adc_conv_finished());
+	return Adc_get_10_bits_result();
+	}
+
+S16 Read_temperature(void)
+	{
+#ifndef __GNUC__
+	U16 adc_code;
+	S8 index=0;
+	adc_code=Get_adc_temp_val();
+	if(adc_code>temperature_code[0])
+		{
+		return (S16)(-20);
+		}
+	else
+		{
+		while(temperature_code[index++]>adc_code);
+		return (S16)(index-1-20);
+		}
+#else
+	U16 adc_code;
+	S8 index=0;
+	adc_code=Get_adc_temp_val();
+	if(adc_code>pgm_read_word_near(&temperature_code))
+		{
+		return (S16)(-20);
+		}
+	else
+		{
+		while(pgm_read_word_near(&temperature_code[index++])>adc_code);
+		return (S16)(index-1-20);
+		}
+
+#endif
+	}
+
+#endif   // USE_ADC
+#endif   // (TARGET_BOARD==USBKEY)

File arch/at90usb128/lib_board/usb_key/usb_key.h

+/**
+ * @file
+ *
+ * @brief This file contains the low level macros and definition for USB KEY board
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef USB_KEY_H
+#define USB_KEY_H
+
+//_____ I N C L U D E S ____________________________________________________
+
+#include "config.h"
+#if (TARGET_BOARD==USBKEY)
+
+//_____ M A C R O S ________________________________________________________
+
+
+/**
+ * @defgroup USB_KEY_module USBKEY
+ * This module contains low level hardware abstraction layer for AT90USBkey board
+ * @image html avrusbkey.gif
+ * @{
+ */
+
+/**
+ * @defgroup USB_key_leds Leds Management
+ * Macros to manage Leds on USB KEY
+ * The led 0 correspond at led D2 RED
+ * The led 1 correspond at led D2 GREEN
+ * The led 2 correspond at led D5 GREEN
+ * The led 3 correspond at led D5 RED
+ * @{
+ */
+#define  LED_PORT             PORTD
+#define  LED_DDR              DDRD
+#define  LED_PIN              PIND
+#define  LED0_BIT             PIND4
+#define  LED1_BIT             PIND5
+#define  LED2_BIT             PIND6
+#define  LED3_BIT             PIND7
+
+#define  Leds_init()          (LED_DDR |= (1<<LED0_BIT) | (1<<LED1_BIT) | (1<<LED2_BIT) | (1<<LED3_BIT))
+#define  Leds_on()            (LED_PORT |= (1<<LED0_BIT) | (1<<LED1_BIT) | (1<<LED2_BIT) | (1<<LED3_BIT))
+#define  Leds_off()           (LED_PORT &= ~((1<<LED0_BIT) | (1<<LED1_BIT) | (1<<LED2_BIT) | (1<<LED3_BIT)))
+#define  Leds_set_val(c)      (Leds_off(),LED_PORT |= (c<<4)&((1<<LED0_BIT) | (1<<LED1_BIT) | (1<<LED2_BIT) | (1<<LED3_BIT)))
+#define  Leds_get_val()       (LED_PORT>>4)
+
+#define  Led0_on()            (LED_PORT |=  (1<<LED0_BIT))
+#define  Led1_on()            (LED_PORT |=  (1<<LED1_BIT))
+#define  Led2_on()            (LED_PORT |=  (1<<LED2_BIT))
+#define  Led3_on()            (LED_PORT |=  (1<<LED3_BIT))
+#define  Led0_off()           (LED_PORT &= ~(1<<LED0_BIT))
+#define  Led1_off()           (LED_PORT &= ~(1<<LED1_BIT))
+#define  Led2_off()           (LED_PORT &= ~(1<<LED2_BIT))
+#define  Led3_off()           (LED_PORT &= ~(1<<LED3_BIT))
+#define  Led0_toggle()        (LED_PIN  |=  (1<<LED0_BIT))
+#define  Led1_toggle()        (LED_PIN  |=  (1<<LED1_BIT))
+#define  Led2_toggle()        (LED_PIN  |=  (1<<LED2_BIT))
+#define  Led3_toggle()        (LED_PIN  |=  (1<<LED3_BIT))
+#define  Is_led0_on()         (LED_PIN  &   (1<<LED0_BIT) ? TRUE : FALSE)
+#define  Is_led1_on()         (LED_PIN  &   (1<<LED1_BIT) ? TRUE : FALSE)
+#define  Is_led2_on()         (LED_PIN  &   (1<<LED2_BIT) ? TRUE : FALSE)
+#define  Is_led3_on()         (LED_PIN  &   (1<<LED3_BIT) ? TRUE : FALSE)
+/// @}
+
+/**
+ * @defgroup USB_key_joy Joystick Management
+ * Macros to manage Joystick on USB KEY
+ * @{
+ */
+#define  Joy_init()           (DDRB &= ~((1<<PINB7)|(1<<PINB6)|(1<<PINB5)), PORTB |= ((1<<PINB7)|(1<<PINB6)|(1<<PINB5)), DDRE &= ~((1<<PINE5)|(1<<PINE4)), PORTE |= ((1<<PINE5)|(1<<PINE4)))
+
+#define  Is_btn_left()        Is_hwb()
+#define  Is_btn_middle()      ((PINB & (1<<PINB5)) ?  FALSE : TRUE)
+#define  Is_btn_right()       FALSE
+
+#define  Is_joy_up()          ((PINB & (1<<PINB7)) ?  FALSE : TRUE)
+#define  Is_joy_left()        ((PINB & (1<<PINB6)) ?  FALSE : TRUE)
+#define  Is_joy_down()        ((PINE & (1<<PINE5)) ?  FALSE : TRUE)
+#define  Is_joy_right()       ((PINE & (1<<PINE4)) ?  FALSE : TRUE)
+
+#define  Is_btn_not_left()    Is_not_hwb()
+#define  Is_btn_not_middle()  ((PINB & (1<<PINB5)) ?  TRUE : FALSE)
+#define  Is_btn_not_right()   TRUE
+
+#define  Is_joy_not_up()      ((PINB & (1<<PINB7)) ?  TRUE  : FALSE)
+#define  Is_joy_not_left()    ((PINB & (1<<PINB6)) ?  TRUE : FALSE)
+#define  Is_joy_not_down()    ((PINE & (1<<PINE5)) ?  TRUE : FALSE)
+#define  Is_joy_not_right()   ((PINE & (1<<PINE4)) ?  TRUE : FALSE)
+
+/**
+ * Enable interrupts for switches (PINB5 to PINB7)
+ * - Set up interrupts for switches
+ * - clear flag
+ * - enable interrupt
+ */
+#define  Switches_enable_it() {  \
+      PCMSK0 |= (1<<PCINT5)|(1<<PCINT6)|(1<<PCINT7);\
+      PCIFR |= (1<<PCIF0);       \
+      PCICR |= (1<<PCIE0);}
+
+/**
+ * Disable interrupts for switches (PINB5 to PINB7)
+ * - disable interrupt
+ * - clear flag
+ * - Clear mask
+ */
+#define  Switches_disable_it() { \
+      PCICR  &= ~(1<<PCIE0);     \
+      PCIFR  |= (1<<PCIF0);      \
+      PCMSK0 &= ~((1<<PCINT5)|(1<<PCINT6)|(1<<PCINT7));}
+/// @}
+
+
+/**
+ * @defgroup USB_key_HWB HWB button management
+ * HWB button is connected to PE2 and can also
+ * be used as generic push button
+ * @{
+ */
+#define Hwb_button_init()      (DDRE  &= ~(1<<PINE2), PORTE |= (1<<PINE2))
+#define Is_hwb()               ((PINE &   (1<<PINE2)) ?  FALSE : TRUE)
+#define Is_not_hwb()           ((PINE &   (1<<PINE2)) ?  TRUE : FALSE)
+/// @}
+
+
+/**
+ * @defgroup USB_key_df USB KEY Hardware data flash configuration
+ * Macros to init the environnement for DF on USB KEY
+ * @{
+ */
+
+/// One AT45DB642D
+#define DF_8_MB
+#define DF_NB_MEM          2
+
+// PINB0 : SPI Slave Select pin, this pin must be disable (disable input mode) because it is used by joystick
+//         Note: the joystick can work fine in Output mode, because the pull-up resistor is activated and the level pin can be read.
+// PINB1 : the SCK signal direction is OUTPUT
+// PINB2 : the SI signal direction is OUTPUT
+// PINB3 : the SO signal direction is INPUT
+// PINE0 : the CS0 signal direction is OUTPUT
+// PINE1 : the CS1 signal direction is OUTPUT
+#define  Df_init_spi()     (DDRB  &= ~((1<<PINB3)), \
+                            DDRB  |=  ((1<<PINB2)|(1<<PINB1)|(1<<PINB0)), \
+                            PORTB |=  ((1<<PINB3)|(1<<PINB2)|(1<<PINB1)|(1<<PINB0)),\
+                            DDRE  |=  ((1<<PINE1)|(1<<PINE0)), \
+                            PORTE |=  ((1<<PINE1)|(1<<PINE0)) )
+#define  Df_select_0()     (PORTE &= ~(1<<PINE0))
+#define  Df_select_1()     (PORTE &= ~(1<<PINE1))
+#define  Df_desel_all()    (PORTE |=  ((1<<PINE1)|(1<<PINE0)))
+
+/// @}
+
+///@}
+
+
+/// USB KEY ADC Channel Definition
+#define ADC_TEMP_CH  0x00
+
+#ifdef USE_ADC       ///< this define is set in config.h file
+/**
+ * @brief This function performs an ADC conversion from the USB KEY TEMP channel
+ * an returns the 10 bits result in an U16 value.
+ *
+ * @warning USE_ADC should be defined in config.h
+ *
+ * @param none
+ *
+ * @return U16 analog sensor temperature value.
+ */
+U16 Get_adc_temp_val(void);
+
+/**
+ * @brief This function performs an ADC conversion from the USB KEY TEMP channel
+ * an returns the 10 bits result of the temperature (in °C) in an S16 value.
+ *
+ * @warning USE_ADC should be defined in config.h
+ *
+ * @param none
+ *
+ * @return S16 temperature value in °C.
+ */
+S16 Read_temperature(void);
+
+#endif   // USE_ADC
+#endif   // TARGET_BOARD==USBKEY
+#endif   // USB_KEY_H

File arch/at90usb128/lib_mcu/flash/flash_drv.h

+/**
+ * @file
+ *
+ * @brief Driver routines to read (no write) datas stored in AVRMega flash
+ *
+ * These routines can be stored and executed in all flash space.
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  All AVRMega devices
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef FLASH_DRV_H
+#define FLASH_DRV_H
+
+//_____ I N C L U D E S ______________________________________________________
+
+#include "config.h"
+
+//_____ M A C R O S ________________________________________________________
+
+/**
+ * @name These macros allow to read a specific device ID of the product.
+ * @{
+ */
+/// read device ID1 of the product.
+#define Flash_read_id1()            ( flash_read_sig(0x0000))
+/// read device ID2 of the product.
+#define Flash_read_id2()            ( flash_read_sig(0x0002))
+/// read device ID3 of the product.
+#define Flash_read_id3()            ( flash_read_sig(0x0004))
+/// read the OSCAL byte of the product.
+#define Flash_read_osccal()         ( flash_read_sig(0x0001))
+/// read the serial number of the product.
+#define Flash_read_sn(pos)          ( flash_read_sig((0x07*2)+pos))
+/// Size of the serial number containt in product.
+#define SN_LENGTH                   10
+/// @}
+
+/**
+ * @name These macros allow to read a specific fuse of the product.
+ * @{
+ */
+/// read the low fuse byte of the product.
+#define Flash_read_fuse_low()       ( flash_read_fuse(0x0000))
+/// read device high fuse byte of the product.
+#define Flash_read_fuse_high()      ( flash_read_fuse(0x0003))
+/// read extended fuse byte of the product.
+#define Flash_read_fuse_extended()  ( flash_read_fuse(0x0002))
+/// read lock byte of the product.
+#define Flash_read_lock()           ( flash_read_fuse(0x0001))
+/// @}
+
+/**
+ * @name These macros allow to check bits from extended fuse of the product.
+ * @{
+ */
+/// Check if the OCD is running
+#define Is_OCD_enable()             (0==(Flash_read_fuse_high()&(1<<FUSE_OCDEN_bp)))
+/// Check if the JTAG interface is enabled
+#define Is_JTAG_enable()            (0==(Flash_read_fuse_high()&(1<<FUSE_JTAGEN_bp)))
+/// @}
+
+
+/**
+ * @name High Fuse Byte
+ * @{
+ */
+/// Select Reset Vector
+#define FUSE_BOOTRST_bp    0
+/// Select Boot Size
+#define FUSE_BOOTSZ0_bp    1
+/// Select Boot Size
+#define FUSE_BOOTSZ1_bp    2
+/// EEPROM memory is preserved through chip erase
+#define FUSE_EESAVE_bp     3
+/// Watchdog timer always on
+#define FUSE_WDTON_bp      4
+/// Enable Serial programming and Data Downloading
+#define FUSE_SPIEN_bp      5
+/// Enable JTAG
+#define FUSE_JTAGEN_bp     6
+/// Enable OCD
+#define FUSE_OCDEN_bp      7
+/// @}
+
+
+//_____ D E C L A R A T I O N S ____________________________________________
+
+/**
+ * @brief read device IDs of the product.
+ *
+ * @param  adr   Address of device ID to read.
+ *
+ * @return byte  Read value
+ */
+U8 flash_read_sig( unsigned long adr );
+
+/**
+ * @brief read a fuse byte of the product.
+ *
+ * @param  adr   Address of fuse to read.
+ *
+ * @return byte  Read value
+ */
+U8 flash_read_fuse( unsigned long adr );
+
+#endif  // FLASH_DRV_H

File arch/at90usb128/lib_mcu/flash/flash_drv.s

+/**
+ * @file
+ *
+ * @brief Driver routines to read (no write) datas stored in AVRMega flash
+ * These routines can be stored and executed in all flash space.
+ *
+ * - Compiler:           GNU GCC for AVR
+ * - Supported devices:  All AVRMega devices
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __GNUC__
+#error Assembler file supported only by GNU GCC
+#endif
+
+//_____ I N C L U D E S ______________________________________________________
+
+
+#include <avr/io.h>     // Registers declarations
+
+//_____ M A C R O S ________________________________________________________
+
+/**
+ * Macro for I/O Location used in following instructions :
+ * "IN - Load an I/O Location to Register", "OUT - Store Register to I/O Location", ...
+ */
+#define _ASM_SFR_IO_( sfr ) (sfr-0x20)
+
+
+//_____ D E C L A R A T I O N S ______________________________________________
+
+   .global  flash_read_sig
+   .global  flash_read_fuse
+
+
+/**
+ * @{
+ * \verbatim
+ */
+
+/**
+ * @brief This macro function allows to read device IDs of the product.
+ *
+ * @param  add   Address of device ID to read.
+ *
+ * @return byte  Read value (R24)
+ */
+flash_read_sig:
+   RCALL    WAIT_SPMEN                       // Wait for SPMEN flag cleared
+   MOV      R31,R23
+   MOV      R30,R22                          // move adress to z pointer (R31=ZH R30=ZL)
+   LDI      R20, ((1<<SPMEN) | (1<<SIGRD))
+   OUT      _ASM_SFR_IO_(SPMCSR), R20        // argument 2 decides function (r18)
+   LPM                                       // Store program memory
+   MOV      R24, R0                          // Store return value
+   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared
+
+
+/**
+ * @brief This macro function allows to read a fuse byte of the product.
+ *
+ * @param  add   Address of fuse to read.
+ *
+ * @return byte  Read value (R24)
+ */
+flash_read_fuse:
+   RCALL    WAIT_SPMEN                       // Wait for SPMEN flag cleared
+   MOV      R31,R23
+   MOV      R30,R22                          // move adress to z pointer (R31=ZH R30=ZL)
+   LDI      R20,((1<<SPMEN) | (1<<BLBSET))
+   OUT      _ASM_SFR_IO_(SPMCSR), R20        // argument 2 decides function (r18)
+   LPM                                       // Store program memory
+   MOV      R24, R0                          // Store return value
+   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared
+
+/**
+ * @brief Performs an active wait on SPME flag
+ */
+WAIT_SPMEN:
+   MOV      R0, R18
+   IN       R18,_ASM_SFR_IO_(SPMCSR)         // get SPMCR into r18
+   SBRC     R18,SPMEN
+   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared
+   MOV      R18, R0
+   RET
+
+/**
+ * \endverbatim
+ * @}
+ */
+

File arch/at90usb128/lib_mcu/flash/flash_drv.s90

+/*This file has been prepared for Doxygen automatic documentation generation.*/
+/*! @file 
+ *
+ * @brief Driver routines to read (no write) datas stored in AVRMega flash
+ * These routines can be stored and executed in all flash space.
+ *
+ * - Compiler:           IAR EWAVR for AVR
+ * - Supported devices:  All AVRMega devices
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ **/
+
+/* Copyright (c) 2007, Atmel Corporation All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of ATMEL may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __IAR_SYSTEMS_ASM__
+#error Assembler file supported only by IAR EWAVR for AVR
+#endif
+
+
+//_____ I N C L U D E S ______________________________________________________
+
+#define  ENABLE_BIT_DEFINITIONS
+#include <ioavr.h>
+ 
+   NAME  flash_drv(16)
+
+//_____ D E C L A R A T I O N S ______________________________________________
+
+   PUBLIC   flash_read_sig
+   PUBLIC   flash_read_fuse
+   
+
+ * @{
+ * \verbatim
+
+   RSEG     CODE
+
+ * @brief This macro function allows to read device IDs of the product.
+ *
+ * @param  add   Address of device ID to read.
+ *
+ * @return byte  Read value (R16)
+ *
+flash_read_sig:
+   RCALL    WAIT_SPMEN                       // Wait for SPMEN flag cleared
+   MOV      R31,R17                         
+   MOV      R30,R16                          // move adress to z pointer (R31=ZH R30=ZL)
+   LDI      R20, ((1<<SPMEN) | (1<<SIGRD))  
+   OUT      SPMCSR, R20                      // argument 2 decides function (r18)
+   LPM                                       // Store program memory
+   MOV      R16, R0                          // Store return value
+   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared
+
+
+ * @brief This macro function allows to read a fuse byte of the product.
+ *
+ * @param  add   Address of fuse to read.
+ * 
+ * @return byte  Read value (R16)
+ *
+flash_read_fuse:
+   RCALL    WAIT_SPMEN                       // Wait for SPMEN flag cleared
+   MOV      R31,R17                         
+   MOV      R30,R16                          // move adress to z pointer (R31=ZH R30=ZL)
+   LDI      R20,((1<<SPMEN) | (1<<BLBSET))  
+   OUT      SPMCSR, R20                      // argument 2 decides function (r18)
+   LPM                                       // Store program memory
+   MOV      R16, R0                          // Store return value
+   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared
+
+
+ * @brief Performs an active wait on SPME flag
+ *
+WAIT_SPMEN:
+   MOV      R0, R18
+   IN       R18,SPMCSR                       // get SPMCR into r18
+   SBRC     R18,SPMEN
+   RJMP     WAIT_SPMEN                       // Wait for SPMEN flag cleared
+   MOV      R18, R0
+   RET
+
+   END
+
+ * \endverbatim
+ * @}

File arch/at90usb128/lib_mcu/pll/pll_drv.h

+/**
+ * @file
+ *
+ * @brief This file contains the low level macros and definition for the USB PLL
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef PLL_DRV_H
+#define PLL_DRV_H
+
+//_____ I N C L U D E S ____________________________________________________
+
+/**
+ * @defgroup PLL PLL driver
+ * PLL Module
+ * @{
+ */
+//_____ M A C R O S ________________________________________________________
+
+/**
+ * @defgroup PLL_macros PLL Macros
+ * These functions allow to control the PLL
+ * @{
+ */
+#define PLLx06          ( (0<<PLLP2) | (1<<PLLP1) | (1<<PLLP0) )
+
+#ifdef __ICCAVR__
+#if (defined(__AT90USB1287__) || defined(__AT90USB1286__))
+#define PLLx03          ( (1<<PLLP2) | (0<<PLLP1) | (1<<PLLP0) )
+#elif (defined(__AT90USB647__) || defined(__AT90USB646__) || defined(__ATmega32U6__))
+#define PLLx03          ( (1<<PLLP2) | (1<<PLLP1) | (0<<PLLP0) )
+#else
+#error TARGET should be defined
+#endif
+#elif defined __GNUC__
+#if (defined(__AVR_AT90USB1287__) || defined(__AVR_AT90USB1286__))
+#define PLLx03          ( (1<<PLLP2) | (0<<PLLP1) | (1<<PLLP0) )
+#elif (defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB646__) || defined(__AVR_ATmega32U6__))
+#define PLLx03          ( (1<<PLLP2) | (1<<PLLP1) | (0<<PLLP0) )
+#endif
+#else // Other compiler
+#error Compiler unknow
+#endif
+
+/// Start the USB PLL with correct clockfactor
+#define Start_pll(clockfactor)   (PLLCSR = ( clockfactor  | (1<<PLLE)  ))
+/// return 1 when PLL locked
+#define Is_pll_ready()           (PLLCSR & (1<<PLOCK) )
+/// Test PLL lock bit and wait until lock is set
+#define Wait_pll_ready()         while (!(PLLCSR & (1<<PLOCK)))
+/// Stop the PLL
+#define Stop_pll()               (PLLCSR  &= (~(1<<PLLE)),PLLCSR=0 )
+#ifdef CUSTOM_PLL_START_AUTO
+#define    Pll_start_auto()   CUSTOM_PLL_START_AUTO
+#else
+
+/// Start the PLL in autofactor mode regarding FOSC define
+#if   (FOSC==8000)
+#define Pll_start_auto()   Start_pll(PLLx06)
+#elif (FOSC==16000)
+#define Pll_start_auto()   Start_pll(PLLx03)
+#else
+#error   "FOSC should be defined with 8000KHz or 16000KHz in config.h"
+#endif
+#endif
+
+/// @}
+
+/// @}
+#endif  // PLL_DRV_H

File arch/at90usb128/lib_mcu/power/power_drv.c

+/**
+ * @file
+ *
+ * @brief This file contains the Power and clock management driver routines.
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+//_____ I N C L U D E S ____________________________________________________
+
+#include "config.h"
+#include "power_drv.h"
+
+//_____ D E C L A R A T I O N ______________________________________________
+
+
+/**
+ * @brief This function write the CPU prescaler register to a define value
+ *
+ * @param x the precaler value to be written
+ */
+#ifndef __GNUC__
+// Ignored if IAR compiler optimization option "Always do cross call optimization" is enabled
+#pragma optimize=none
+void Set_cpu_prescaler(U8 x)
+	{
+	U8 save_int=Get_interrupt_state();
+	Disable_interrupt();
+	CLKPR=(1<<CLKPCE);
+	CLKPR=x;
+	if(save_int)
+		{Enable_interrupt();}
+	}
+#endif
+
+/**
+ * @brief This function makes the AVR core enter power down mode.
+ */
+void set_power_down_mode( void )
+	{
+	Setup_power_down_mode();
+	Sleep_instruction();
+	}
+
+/**
+ * @brief This function makes the AVR core enter idle mode.
+ */
+void set_idle_mode( void )
+	{
+	Setup_idle_mode();
+	Sleep_instruction();
+	}
+
+/**
+ * This function makes the AVR core enter adc noise reduction mode.
+ */
+void set_adc_noise_reduction_mode( void )
+	{
+	Setup_adc_noise_reduction_mode();
+	Sleep_instruction();
+	}
+
+/**
+ * This function makes the AVR core enter power save mode.
+ */
+void set_power_save_mode( void )
+	{
+	Setup_power_save_mode();
+	Sleep_instruction();
+	}
+
+/**
+ * This function makes the AVR core enter standby mode.
+ */
+void set_standby_mode( void )
+	{
+	Setup_standby_mode();
+	Sleep_instruction();
+	}
+
+/**
+ * This function makes the AVR core enter extended standby mode.
+ */
+void set_ext_standby_mode( void )
+	{
+	Setup_ext_standby_mode();
+	Sleep_instruction();
+	}

File arch/at90usb128/lib_mcu/power/power_drv.h

+/**
+ * @file
+ *
+ * @brief This file contains the Power Management low level driver definition
+ *
+ * This module allows to configure the different power mode of the AVR core and
+ * also to setup the the internal clock prescaler
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef _POWER_DRV_H_
+#define _POWER_DRV_H_
+
+#ifdef  __GNUC__
+#include <avr/power.h>
+#endif
+/**
+ * @defgroup powermode Power management drivers
+ *
+ * @{
+ */
+
+//_____ M A C R O S ________________________________________________________
+
+#define Setup_idle_mode()                       (SMCR=0,SMCR |= (1<<SE))
+#define Setup_power_down_mode()                 (SMCR=0,SMCR |= (1<<SE)+(1<<SM1))
+#define Setup_adc_noise_reduction_mode()        (SMCR=0,SMCR |= (1<<SE)+(1<<SM0))
+#define Setup_power_save_mode()                 (SMCR=0,SMCR |= (1<<SE)+(1<<SM1)+(1<<SM0))
+#define Setup_standby_mode()                    (SMCR=0,SMCR |= (1<<SE)+(1<<SM2)+(1<<SM1))
+#define Setup_ext_standby_mode()                (SMCR=0,SMCR |= (1<<SE)+(1<<SM2)+(1<<SM1)+(1<<SM0))
+
+/**
+ * @brief This function reset the internal CPU core clock prescaler
+ */
+#ifdef  __GNUC__
+#define Clear_prescaler()                       clock_prescale_set(0)
+#else
+#define Clear_prescaler()                       Set_cpu_prescaler(0)
+#endif
+
+/**
+ * @brief This function configure the internal CPU core clock prescaler value
+ *
+ * @param x: prescaler new value
+ */
+#ifdef  __GNUC__
+#define Set_cpu_prescaler(x)                        clock_prescale_set(x)
+#else
+extern void Set_cpu_prescaler(U8 x);
+#endif
+
+#define Sleep_instruction()      {asm("SLEEP");}
+
+//Backward compatibility
+#define Set_power_down_mode()      set_power_down_mode()
+#define Set_idle_mode()            set_idle_mode()
+
+//_____ D E C L A R A T I O N ______________________________________________
+
+void set_idle_mode( void );
+void set_power_down_mode( void );
+void set_adc_noise_reduction_mode( void );
+void set_power_save_mode( void );
+void set_standby_mode( void );
+void set_ext_standby_mode( void );
+
+/**
+ * @brief This function makes the AVR core enter idle mode.
+ */
+#define Enter_idle_mode()                 (set_idle_mode())
+
+/**
+ * @brief This function makes the AVR core enter power down mode.
+ */
+#define Enter_power_down_mode()           (set_power_down_mode())
+
+/**
+ * @brief This function makes the AVR core enter adc noise reduction mode.
+ */
+#define Enter_adc_noise_reduction_mode()  (set_adc_noise_reduction_mode())
+
+/**
+ * @brief This function makes the AVR core enter power save mode.
+ */
+#define Enter_power_save_mode()           (set_power_save_mode())
+
+/**
+ * @brief This function makes the AVR core enter standby mode.
+ */
+
+#define Enter_standby_mode()              (set_standby_mode())
+
+/**
+ * @brief This function makes the AVR core enter extended standby mode.
+ */
+#define Enter_ext_standby_mode()          (set_ext_standby_mode())
+
+/// @}
+
+#endif  // _POWER_DRV_H_

File arch/at90usb128/lib_mcu/usart/usart.c

+// AVR306: Using the AVR UART in C
+// Routines for interrupt controlled USART
+// Last modified: 02-06-21
+// Modified by: AR
+
+/* Includes */
+#include <avr/io.h>
+#include <avr/interrupt.h>
+#include "config.h"
+#include "usart.h"
+
+/* UART Buffer Defines */
+#define USART_RX_BUFFER_MASK ( USART_RX_BUFFER_SIZE - 1 )
+#define USART_TX_BUFFER_MASK ( USART_TX_BUFFER_SIZE - 1 )
+#if ( USART_RX_BUFFER_SIZE & USART_RX_BUFFER_MASK )
+#error RX buffer size is not a power of 2
+#endif
+#if ( USART_TX_BUFFER_SIZE & USART_TX_BUFFER_MASK )
+#error TX buffer size is not a power of 2
+#endif
+
+/* Static Variables */
+static unsigned char USART_RxBuf[USART_RX_BUFFER_SIZE];
+static volatile unsigned char USART_RxHead;
+static volatile unsigned char USART_RxTail;
+static unsigned char USART_TxBuf[USART_TX_BUFFER_SIZE];
+static volatile unsigned char USART_TxHead;
+static volatile unsigned char USART_TxTail;
+
+/* Initialize USART */
+void USART0_Init( unsigned int baudrate )
+	{
+	unsigned char x;
+
+	/* Set the baud rate */
+	UBRR1H = ( unsigned char )( baudrate >> 8 );
+	UBRR1L = ( unsigned char )baudrate;
+
+	/* Enable UART receiver and transmitter */
+	UCSR1B = ( ( 1 << RXCIE1 ) | ( 1 << RXEN1 ) | ( 1 << TXEN1 ) );
+
+	/* Set frame format: 8 data 2stop */
+	UCSR1C = ( 1 << USBS1 ) | ( 1 << UCSZ11 ) | ( 1 << UCSZ10 ); //For devices with Extended IO
+	//UCSR0C = (1<<URSEL)|(1<<USBS0)|(1<<UCSZ01)|(1<<UCSZ00);   //For devices without Extended IO
+
+	/* Flush receive buffer */
+	x = 0;
+
+	USART_RxTail = x;
+	USART_RxHead = x;
+	USART_TxTail = x;
+	USART_TxHead = x;
+	}
+
+/* Interrupt handlers */
+ISR(USART1_RX_vect)
+	{
+	unsigned char rxdata;
+	unsigned char tmphead;
+
+	/* Read the received data */
+	rxdata = UDR1;
+	/* Calculate buffer index */
+	tmphead = ( USART_RxHead + 1 ) & USART_RX_BUFFER_MASK;
+	USART_RxHead = tmphead; /* Store new index */
+
+	if( tmphead == USART_RxTail )
+		{
+		/* ERROR! Receive buffer overflow */
+		}
+
+	USART_RxBuf[tmphead] = rxdata; /* Store received data in buffer */
+	}
+
+ISR(USART1_TX_vect)
+	{
+	unsigned char tmptail;
+
+	/* Check if all data is transmitted */
+	if( USART_TxHead != USART_TxTail )
+		{
+		/* Calculate buffer index */
+		tmptail = ( USART_TxTail + 1 ) & USART_TX_BUFFER_MASK;
+		USART_TxTail = tmptail; /* Store new index */
+
+		UDR1 = USART_TxBuf[tmptail]; /* Start transmition */
+		}
+	else
+		{
+		UCSR1B &= ~ ( 1 << UDRIE1 ); /* Disable UDRE interrupt */
+		}
+	}
+
+/* Read and write functions */
+unsigned char USART0_Receive( void )
+	{
+	unsigned char tmptail;
+
+	while( USART_RxHead == USART_RxTail )
+		/* Wait for incomming data */
+		;
+	tmptail = ( USART_RxTail + 1 ) & USART_RX_BUFFER_MASK;/* Calculate buffer index */
+
+	USART_RxTail = tmptail; /* Store new index */
+
+	return USART_RxBuf[tmptail]; /* Return data */
+	}
+
+void USART0_Transmit( unsigned char txdata )
+	{
+	unsigned char tmphead;
+	/* Calculate buffer index */
+	tmphead = ( USART_TxHead + 1 ) & USART_TX_BUFFER_MASK; /* Wait for free space in buffer */
+	while( tmphead == USART_TxTail )
+		;
+
+	USART_TxBuf[tmphead] = txdata; /* Store data in buffer */
+	USART_TxHead = tmphead; /* Store new index */
+
+	UCSR1B |= ( 1 << UDRIE1 ); /* Enable UDRE interrupt */
+	}
+
+unsigned char DataInReceiveBuffer( void )
+	{
+	return ( USART_RxHead != USART_RxTail ); /* Return 0 (FALSE) if the receive buffer is empty */
+	}

File arch/at90usb128/lib_mcu/usart/usart.h

+/**
+ * @file
+ *
+ * @brief API for AT90USB128x USART module
+ *
+ * @author               Andrew Cooper
+ *
+ */
+
+/* Copyright (c) 2009 Andrew Cooper. All rights reserved.
+ *
+ */
+
+//_____ I N C L U D E S ____________________________________________________
+
+//_____ M A C R O S ________________________________________________________
+
+//_____ D E C L A R A T I O N ______________________________________________
+
+void USART0_Init( unsigned int baudrate );
+
+unsigned char USART0_Receive( void );
+
+void USART0_Transmit( unsigned char data );

File arch/at90usb128/lib_mcu/usb/usb_drv.c

+/**
+ * @file
+ *
+ * @brief This file contains the USB driver routines.
+ *
+ * - Compiler:           IAR EWAVR and GNU GCC for AVR
+ * - Supported devices:  AT90USB1287, AT90USB1286, AT90USB647, AT90USB646
+ *
+ * @author               Atmel Corporation: http://www.atmel.com \n
+ *                       Support and FAQ: http://support.atmel.no/
+ *
+ */
+
+/* Copyright (c) 2009 Atmel Corporation. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ *
+ * 3. The name of Atmel may not be used to endorse or promote products derived
+ * from this software without specific prior written permission.
+ *
+ * 4. This software may only be redistributed and used in connection with an Atmel
+ * AVR product.
+ *
+ * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
+ * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
+ * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
+ * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+//_____ I N C L U D E S ____________________________________________________
+
+#include "config.h"
+#include "conf_usb.h"
+#include "usb_drv.h"
+
+//_____ M A C R O S ________________________________________________________
+
+//_____ D E C L A R A T I O N ______________________________________________
+
+#if (USB_DEVICE_FEATURE==DISABLED && USB_HOST_FEATURE==DISABLED)
+#error at least one of  USB_DEVICE_FEATURE or USB_HOST_FEATURE should be unabled
+#endif
+
+#if (USB_DEVICE_FEATURE == ENABLED)
+
+/**
+ * @brief usb_configure_endpoint.
+ *
+ * This function configures an endpoint with the selected type.
+ *
+ *
+ * @param config0
+ * @param config1
+ *
+ * @return Is_endpoint_configured.
+ */
+U8 usb_config_ep( U8 config0, U8 config1 )
+	{
+	Usb_enable_endpoint();
+	UECFG0X = config0;
+	UECFG1X = ( UECFG1X & ( 1 << ALLOC ) ) | config1;
+	Usb_allocate_memory();
+	return ( Is_endpoint_configured() );
+	}
+
+/**
+ * @brief usb_select_endpoint_interrupt.
+ *
+ * This function select the endpoint where an event occurs and returns the
+ * number of this endpoint. If no event occurs on the endpoints, this
+ * function returns 0.
+ *
+ * @return endpoint number.
+ */
+U8 usb_select_enpoint_interrupt( void )
+	{
+	U8 interrupt_flags;
+	U8 ep_num;
+
+	ep_num = 0;
+	interrupt_flags = Usb_interrupt_flags();
+
+	while( ep_num < MAX_EP_NB )
+		{
+		if( interrupt_flags & 1 )
+			{
+			return ( ep_num );
+			}
+		else
+			{
+			ep_num++ ;
+			interrupt_flags = interrupt_flags >> 1;
+			}
+		}
+	return 0;
+	}
+
+/**
+ * @brief usb_send_packet.
+ *
+ * This function moves the data pointed by tbuf to the selected endpoint fifo
+ * and sends it through the USB.
+ *
+ *
+ * @param ep_num       number of the addressed endpoint
+ * @param *tbuf        address of the first data to send
+ * @param data_length  number of bytes to send
+ *
+ * @return address of the next U8 to send.
+ *
+ * Example:
+ * usb_send_packet(3,&first_data,0x20);    // send packet on the endpoint #3
+ * while(!(Usb_tx_complete));              // wait packet ACK'ed by the Host
+ * Usb_clear_tx_complete();                     // acknowledge the transmit
+ *
+ * Note:
+ * tbuf is incremented of 'data_length'.
+ */
+U8 usb_send_packet( U8 ep_num, U8* tbuf, U8 data_length )
+	{
+	U8 remaining_length;
+
+	remaining_length = data_length;
+	Usb_select_endpoint(ep_num);
+	while( Is_usb_write_enabled() && ( 0 != remaining_length ) )
+		{
+		Usb_write_byte(*tbuf);
+		remaining_length-- ;
+		tbuf++ ;
+		}
+	return remaining_length;
+	}
+
+/**
+ * @brief usb_read_packet.
+ *
+ * This function moves the data stored in the selected endpoint fifo to
+ * the address specified by *rbuf.
+ *
+ *
+ * @param ep_num       number of the addressed endpoint
+ * @param *rbuf        aaddress of the first data to write with the USB data
+ * @param data_length  number of bytes to read
+ *
+ * @return address of the next U8 to send.
+ *
+ * Example:
+ * while(!(Usb_rx_complete));                      // wait new packet received
+ * usb_read_packet(4,&first_data,usb_get_nb_byte); // read packet from ep 4
+ * Usb_clear_rx();                                 // acknowledge the transmit
+ *
+ * Note:
+ * rbuf is incremented of 'data_length'.
+ */
+U8 usb_read_packet( U8 ep_num, U8* rbuf, U8 data_length )
+	{
+	U8 remaining_length;
+
+	remaining_length = data_length;
+	Usb_select_endpoint(ep_num);
+
+	while( Is_usb_read_enabled() && ( 0 != remaining_length ) )
+		{
+		* rbuf = Usb_read_byte();
+		remaining_length-- ;
+		rbuf++ ;
+		}
+	return remaining_length;
+	}
+
+/**
+ * @brief usb_halt_endpoint.
+ *
+ * This function sends a STALL handshake for the next Host request. A STALL
+ * handshake will be send for each next request untill a SETUP or a Clear Halt
+ * Feature occurs for this endpoint.
+ *
+ * @param ep_num number of the addressed endpoint
+ *
+ * @return none
+ */
+void usb_halt_endpoint( U8 ep_num )
+	{
+	Usb_select_endpoint(ep_num);
+	Usb_enable_stall_handshake();
+	}
+
+/**
+ * @brief usb_init_device.
+ *
+ * This function initializes the USB device controller and
+ * configures the Default Control Endpoint.
+ *
+ *
+ * @return status
+ */
+U8 usb_init_device( void )
+	{
+	Usb_select_device();
+	if( Is_usb_id_device() )
+		{
+		Usb_select_endpoint(EP_CONTROL);
+		if( ! Is_usb_endpoint_enabled() )
+			{
+#if (USB_LOW_SPEED_DEVICE==DISABLE)
+			return usb_configure_endpoint(EP_CONTROL,
+				TYPE_CONTROL,
+				DIRECTION_OUT,
+				SIZE_64,
+				ONE_BANK,
+				NYET_DISABLED);
+#else
+			return usb_configure_endpoint(EP_CONTROL,
+				TYPE_CONTROL,
+				DIRECTION_OUT,
+				SIZE_8,