version 0.1 written by Akiba, taken from here:
[chibi.git] / freakusb / hw / at90usbxx6_7 / hw.c
blobe4167440073f7da3a018c50835dca8ca479fb749
1 /*******************************************************************
2 Copyright (C) 2008 FreakLabs
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 Please post support questions to the FreakLabs forum.
19 *******************************************************************/
20 /*!
21 \defgroup hw_at90usb Hardware - AT90USB
22 \file hw.c
23 \ingroup hw_at90usb
25 /*******************************************************************/
26 #include <avr/io.h>
27 #include <avr/interrupt.h>
28 #include <avr/wdt.h>
29 #include <avr/pgmspace.h>
30 #include "at90usb.h"
31 #include "types.h"
33 /**************************************************************************/
34 /*!
35 Initialize the hardware.
36 1) Turn off the watchdog timer (in AVR MCUs)
37 2) Configure the PLL
38 3) Enable the USB and relevant peripherals
39 4) Enable the global interrupt.
41 /**************************************************************************/
42 void hw_init()
44 MCUSR &= ~(1 << WDRF);
45 wdt_disable();
47 #ifndef AT90USB16
48 SET_DEVICE_MODE();
50 // config PLL and turn it on
51 PLLCSR = (1 << PLLP1) | (1 << PLLP0);
52 PLLCSR |= (1 << PLLE);
53 while (!PLL_LOCKED);
55 // enable the USB block
56 USBCON |= ((1 << USBE) | (1 << OTGPADE));
58 // unfreeze the clock
59 USBCON &= ~(1 << FRZCLK);
61 // set the speed
62 UDCON &= ~(1 << LSM);
64 // set the interrupts: vbus, suspend, and end of reset
65 USBCON |= (1 << VBUSTE);
66 UDIEN |= (1 << SUSPE) | (1 << EORSTE);
67 #else
68 // set the interrupts: vbus, suspend, and end of reset
69 UDIEN |= (_BV(SUSPE) | _BV(EORSTE));
71 // enable vbus sense pin
72 VBUS_SENSE_DDR &= ~_BV(VBUS_SENSE_IO);
73 VBUS_SENSE_PORT &= ~_BV(VBUS_SENSE_IO);
75 // enable the vbus sense interrupt
76 PCICR |= _BV(PCIE0);
78 // disable the usb interface
79 USBCON &= ~_BV(USBE);
81 // enable the usb interface
82 USBCON |= _BV(USBE);
83 #endif
85 // turn on the global interrupts
86 sei();
89 /**************************************************************************/
90 /*!
91 Returns a byte stored in the flash. Some MCUs can't access the flash
92 memory from a standard pointer so they need a special function to handle it.
94 /**************************************************************************/
95 U8 hw_flash_get_byte(U8 *addr)
97 return pgm_read_byte(addr);
100 /**************************************************************************/
102 Disable global interrupts
104 /**************************************************************************/
105 void hw_intp_disable()
107 cli();
110 /**************************************************************************/
112 Enable global interrupts
114 /**************************************************************************/
115 void hw_intp_enable()
117 sei();