1 #include "core/rom_drivers.h"
2 #include "core/gpio/gpio.h"
3 #include "dataflash/at45db041d.h"
9 USB_DEV_INFO DeviceInfo
;
10 MSC_DEVICE_INFO MscDevInfo
;
11 ROM
** rom
= (ROM
**)0x1fff1ff8;
14 void usbMSCWrite(uint32_t offset
, uint8_t src
[], uint32_t length
) {
15 dataflash_random_write(src
, offset
, length
);
18 void usbMSCRead(uint32_t offset
, uint8_t dst
[], uint32_t length
) {
19 dataflash_random_read(dst
, offset
, length
);
22 void usbMSCInit(void) {
23 dataflash_initialize();
26 SCB_PDRUNCFG
&= ~(SCB_PDSLEEPCFG_USBPAD_PD
); // Power-up USB PHY
27 SCB_PDRUNCFG
&= ~(SCB_PDSLEEPCFG_USBPLL_PD
); // Power-up USB PLL
29 SCB_USBPLLCLKSEL
= SCB_USBPLLCLKSEL_SOURCE_MAINOSC
; // Select PLL Input
30 SCB_USBPLLCLKUEN
= SCB_USBPLLCLKUEN_UPDATE
; // Update Clock Source
31 SCB_USBPLLCLKUEN
= SCB_USBPLLCLKUEN_DISABLE
; // Toggle Update Register
32 SCB_USBPLLCLKUEN
= SCB_USBPLLCLKUEN_UPDATE
;
34 // Wait until the USB clock is updated
35 while (!(SCB_USBPLLCLKUEN
& SCB_USBPLLCLKUEN_UPDATE
));
37 // Set USB clock to 48MHz (12MHz x 4)
38 SCB_USBPLLCTRL
= (SCB_USBPLLCTRL_MULT_4
);
39 while (!(SCB_USBPLLSTAT
& SCB_USBPLLSTAT_LOCK
)); // Wait Until PLL Locked
40 SCB_USBCLKSEL
= SCB_USBCLKSEL_SOURCE_USBPLLOUT
;
42 // Set USB pin functions
43 // bsx says, its only needed for usb-hid. And it conflicts with btn_0
44 // IOCON_PIO0_1 &= ~IOCON_PIO0_1_FUNC_MASK;
45 // IOCON_PIO0_1 |= IOCON_PIO0_1_FUNC_CLKOUT; // CLK OUT
46 IOCON_PIO0_3
&= ~IOCON_PIO0_3_FUNC_MASK
;
47 IOCON_PIO0_3
|= IOCON_PIO0_3_FUNC_USB_VBUS
; // VBus
48 IOCON_PIO0_6
&= ~IOCON_PIO0_6_FUNC_MASK
;
49 IOCON_PIO0_6
|= IOCON_PIO0_6_FUNC_USB_CONNECT
; // Soft Connect
51 // Disable internal resistor on VBUS (0.3)
52 gpioSetPullup(&IOCON_PIO0_3
, gpioPullupMode_Inactive
);
54 // workaround for long connect delay
55 *((uint32_t *)(0x10000054)) = 0x0;
59 MscDevInfo
.idVendor
= USB_VENDOR_ID
;
60 MscDevInfo
.idProduct
= USB_PROD_ID
;
61 MscDevInfo
.bcdDevice
= USB_DEVICE
;
62 MscDevInfo
.StrDescPtr
= (uint32_t)&USB_MSCStringDescriptor
[0];
63 MscDevInfo
.MSCInquiryStr
= (uint32_t)&"r0ket DataFlash "; // 28 char response to SCSI INQUIRY
64 MscDevInfo
.BlockCount
= 1024;
65 MscDevInfo
.BlockSize
= 512;
66 MscDevInfo
.MemorySize
= 1024*512;
67 MscDevInfo
.MSC_Write
= &usbMSCWrite
;
68 MscDevInfo
.MSC_Read
= &usbMSCRead
;
70 DeviceInfo
.DevType
= USB_DEVICE_CLASS_STORAGE
;
71 DeviceInfo
.DevDetailPtr
= (uint32_t)&MscDevInfo
;
73 /* Enable Timer32_1, IOCON, and USB blocks (for USB ROM driver) */
74 SCB_SYSAHBCLKCTRL
|= (SCB_SYSAHBCLKCTRL_CT32B1
| SCB_SYSAHBCLKCTRL_IOCON
| SCB_SYSAHBCLKCTRL_USB_REG
);
76 /* Use pll and pin init function in rom */
77 /* Warning: This will also set the system clock to 48MHz! */
78 // (*rom)->pUSBD->init_clk_pins();
80 /* insert a delay between clk init and usb init */
81 for (n
= 0; n
< 75; n
++) {__asm("nop");}
83 (*rom
)->pUSBD
->init(&DeviceInfo
); /* USB Initialization */
84 (*rom
)->pUSBD
->connect(true); /* USB Connect */
85 usbMSCenabled
|=USB_MSC_ENABLEFLAG
;
89 void USB_IRQHandler() {
94 void usbMSCOff(void) {
95 (*rom
)->pUSBD
->connect(false); /* USB Disconnect */
96 usbMSCenabled
&=~USB_MSC_ENABLEFLAG
;