Hey Albert, I would like to take a look at this more to try to make a
"usb sd card reader". I'm sure it will be quite slow with 18F but
that's ok. I looked over your blogs which did answer many questions,
but I am still unsure of the steps to take to build USB MSD. Hopefully
this won't be too difficult along with my current data media
libraries.
So far, I did manage to get a message on my PC that says "New Hardware
Found - USB Mass Storage Device". So I seem to be on the right track.
After some time, my pc says the device is not functioning correctly. I
also get the welcome message via serial tx/rx pins (but I get it 4
times, in 5 sec intervals).
Is there a file I should start from? This is not a HID (human
interface device), but it does use one of the generic windows drivers.
What do the USB debug constants do and why might they be useful for
me?
How do I send/receive data? I only see procedures for sending data in
usb_drv_core.jal.
I read that data buffers should be 64 bytes, Here is my file so far.
Did I get my endpoints correct?
-- chip setup
include 18f4550
-- even though the external crystal is 20 MHz, the configuration is
such that
-- the CPU clock is derived from the 96 Mhz PLL clock (div2),
therefore set
-- target frequency to 48 MHz
pragma target clock 48_000_000
-- fuses
pragma target PLLDIV P5 -- divide by 5 - 20MHZ_INPUT
pragma target CPUDIV P2 --
OSC1_OSC2_SRC_1_96MHZ_PLL_SRC_2
pragma target USBPLL F48MHZ -- CLOCK_SRC_FROM_96MHZ_PLL_2
pragma target OSC HS_PLL
pragma target FCMEN DISABLED
pragma target IESO DISABLED
pragma target PWRTE DISABLED -- power up timer
pragma target VREGEN ENABLED -- USB voltage regulator
pragma target VOLTAGE V20 -- brown out voltage
pragma target BROWNOUT DISABLED -- no brownout detection
pragma target WDTPS P32K -- watch dog saler setting
pragma target WDT DISABLED -- no watchdog
pragma target CCP2MUX pin_C1 -- CCP2 pin
pragma target PBADEN DIGITAL -- digital input port<0..4>
pragma target LPT1OSC LOW_POWER -- low power timer 1
pragma target MCLR EXTERNAL -- master reset on RE3
pragma target STVR DISABLED -- reset on stack over/under
flow
pragma target LVP DISABLED -- no low-voltage programming
pragma target XINST ENABLED -- extended instruction set
pragma target DEBUG DISABLED -- background debugging
pragma target CP0 DISABLED -- code block 0 not protected
pragma target CP1 DISABLED -- code block 1 not protected
pragma target CP2 DISABLED -- code block 2 not protected
pragma target CP3 DISABLED -- code block 3 not protected
pragma target CPB DISABLED -- bootblock code not write
protected
pragma target CPD DISABLED -- eeprom code not write
protected
pragma target WRT0 DISABLED -- table writeblock 0 not
protected
pragma target WRT1 DISABLED -- table write block 1 not
protected
pragma target WRT2 DISABLED -- table write block 2 not
protected
pragma target WRT3 DISABLED -- table write block 3 not
protected
pragma target WRTB DISABLED -- bootblock not write
protected
pragma target WRTD DISABLED -- eeprom not write protected
pragma target WRTC DISABLED -- config not write protected
pragma target EBTR0 DISABLED -- table read block 0 not
protected
pragma target EBTR1 DISABLED -- table read block 1 not
protected
pragma target EBTR2 DISABLED -- table read block 2 not
protected
pragma target EBTR3 DISABLED -- table read block 3 not
protected
pragma target EBTRB DISABLED -- boot block not protected
enable_digital_io() -- make all pins digital I/O
-- led def
alias led is pin_A0
pin_A0_direction = output
include delay
include print
-- setup uart for communication
const serial_hw_baudrate = 115200 -- set the baud rate
include serial_hardware
serial_hw_init()
-- consts to enable debugging
const USB_DEBUG = 0
const USB_DEBUG_HIGH = 0
-- usb definitions
include usb_defs
-------------------------------------------------------------------------
-- Setup USB endpoints, 1 enpoint is required for a MSD class
-------------------------------------------------------------------------
const bit USB_EP0 = 1
const byte USB_EP0_OUT_SIZE = 64
const word USB_EP0_OUT_ADDR = (USB_BASE_ADDRESS + 0x0080 )
const byte USB_EP0_IN_SIZE = 64
const word USB_EP0_IN_ADDR = (USB_EP0_OUT_ADDR + USB_EP0_OUT_SIZE )
var volatile byte usb_ep0out_buf[ USB_EP0_OUT_SIZE ] at
USB_EP0_OUT_ADDR
var volatile byte usb_ep0in_buf[ USB_EP0_IN_SIZE ] at USB_EP0_IN_ADDR
const bit USB_EP1 = 1
const byte USB_EP1_OUT_SIZE = 64
const word USB_EP1_OUT_ADDR = (USB_BASE_ADDRESS + USB_EP0_IN_SIZE)
const byte USB_EP1_IN_SIZE = 64
const word USB_EP1_IN_ADDR = (USB_EP1_OUT_ADDR + USB_EP1_OUT_SIZE )
var volatile byte usb_ep1out_buf[ USB_EP1_OUT_SIZE ] at
USB_EP1_OUT_ADDR
var volatile byte usb_ep1in_buf[ USB_EP1_IN_SIZE ] at USB_EP1_IN_ADDR
const bit USB_EP2 = 0
const bit USB_EP3 = 0
--
-----------------------------------------------------------------------
-- USB_MSD_TX_BUFFER_SIZE contains the size of the transmit FIFO
buffer
--
-----------------------------------------------------------------------
const byte USB_MSD_TX_BUFFER_SIZE = 64
--
-----------------------------------------------------------------------
-- USB_MSD_RX_BUFFER_SIZE contains the size of the receive FIFO buffer
--
-----------------------------------------------------------------------
const byte USB_MSD_RX_BUFFER_SIZE = 64
-------------------------------------------------------------------------
-- define the buffer in dual port of the USB SIE, so data is
-- accessible from the JAL code
-------------------------------------------------------------------------
var volatile byte usb_msd_buffer_in[USB_EP1_IN_SIZE] at
USB_EP1_IN_ADDR
var volatile byte usb_msd_buffer_out[USB_EP1_OUT_SIZE] at
USB_EP1_OUT_ADDR
--
-----------------------------------------------------------------------
-- Sets the default USB_DEVICE_DESCRIPTOR record.
--
-----------------------------------------------------------------------
const byte USB_DEVICE_DESCRIPTOR[USB_DEVICE_DESCRIPTOR_SIZE] =
{
0x12, -- Descriptor size in bytes (18)
0x01, --Descriptor type (DEVICE)
0x00,
0x02, -- USB specification release (BCD) (2.00)
0x00, -- Class (specified at interface level)
0x00, -- Subclass (specified at interface level)
0x00, -- Protocol (specified at interface level)
0x40, -- Maximum packet size for endpoint zero (64)
0xD8,
0x04, -- Vendor ID (Microchip Technology; assigned by USB-IF)
0x09,
0x00, -- Product ID (assigned by vendor)
0x00,
0x01, -- Device release number (BCD, assigned by vendor) (1.00)
0x01, -- Manufacturer string index
0x02, -- Product string index
0x03, -- Serial number string index
0x01 -- Number of possible configurations
}
--
-----------------------------------------------------------------------
-- Sets the default USB_CONFIGURATION_DESCRIPTOR record.
--
-----------------------------------------------------------------------
const USB_CONFIGURATION_DESCRIPTOR_SIZE = 0x09 + 0x09 + 0x07 + 0x07
const byte
USB_CONFIGURATION_DESCRIPTOR[ USB_CONFIGURATION_DESCRIPTOR_SIZE ]=
{
--Configuration Descriptor
0x09, -- Descriptor size in bytes (9)
0x02, -- Descriptor type (CONFIGURATION)
0x20,
0x00, -- Total length of this and subordinate descriptors
0x01, -- Number of interfaces in this configuration
0x01, -- Identifier for this configuration
0x00, -- Configuration string index (no string defined)
0xC0, -- Attributes: self powered, no remote wakeup
0x32, -- Maximum power consumption (100 mA)
--Interface Descriptor
0x09, -- Descriptor size in bytes (9)
0x04, -- Descriptor type (INTERFACE)
0x00, -- Interface Number
0x00, -- Alternate Setting Number
0x02, -- Number of endpoints in this interface
0x08, -- Class code (mass storage)
0x06, -- Subclass code (SCSI transparent command set)
0x50, -- Protocol code (bulk-only transport)
0x00, -- Interface string index (no string defined)
--Endpoint Descriptor
0x07, -- Descriptor size in bytes (7)
0x05, -- Descriptor type (ENDPOINT)
0x81, -- Endpoint number and direction (1 IN)
0x02, -- Transfer type (bulk)
0x40,
0x00, -- Maximum packet size (64)
0x00, -- Maximum latency (doesn't apply to full-speed bulk
endpoints)
--Endpoint Descriptor
0x07, -- Descriptor size in bytes (7)
0x05, -- Descriptor type (ENDPOINT)
0x01, -- Endpoint number and direction (1 OUT)
0x02, -- Transfer type (bulk)
0x40,
0x00, -- Maximum packet size (64)
0x00 -- Maximum latency/high-speed OUT NAK rate (doesn't apply to
full-speed bulk endpoints)
}
const byte USB_STRING0[] =
{
0x04, -- bLength
USB_DT_STRING, -- bDescriptorType
0x09, -- wLANGID[0] (low byte)
0x04 -- wLANGID[0] (high byte)
}
const byte USB_STRING1[0x34] =
{
0x34, -- bLength
USB_DT_STRING, -- bDescriptorType
"M", 0x00,
"i", 0x00,
"c", 0x00,
"r", 0x00,
"o", 0x00,
"c", 0x00,
"h", 0x00,
"i", 0x00,
"p", 0x00,
" ", 0x00,
"T", 0x00,
"e", 0x00,
"c", 0x00,
"h", 0x00,
"n", 0x00,
"o", 0x00,
"l", 0x00,
"o", 0x00,
"g", 0x00,
"y", 0x00,
" ", 0x00,
"I", 0x00,
"n", 0x00,
"c", 0x00,
".", 0x00
}
const byte USB_STRING2[0x3A] =
{
0x3A, -- bLength
USB_DT_STRING, -- bDescriptorType
"M", 0x00,
"i", 0x00,
"c", 0x00,
"r", 0x00,
"o", 0x00,
"c", 0x00,
"h", 0x00,
"i", 0x00,
"p", 0x00,
" ", 0x00,
"M", 0x00,
"a", 0x00,
"s", 0x00,
"s", 0x00,
" ", 0x00,
"S", 0x00,
"t", 0x00,
"o", 0x00,
"r", 0x00,
"a", 0x00,
"g", 0x00,
"e", 0x00,
" ", 0x00,
"D", 0x00,
"r", 0x00,
"i", 0x00,
"v", 0x00,
"e", 0x00
}
const byte USB_STRING3[0x1A] =
{
0x1A, -- bLength
USB_DT_STRING, -- bDescriptorType
"1", 0x00,
"2", 0x00,
"3", 0x00,
"4", 0x00,
"5", 0x00,
"6", 0x00,
"7", 0x00,
"8", 0x00,
"9", 0x00,
"A", 0x00,
"B", 0x00,
"C", 0x00
}
-------------------------------------------------------------------------
-- Include reset of USB driver libraries
-------------------------------------------------------------------------
include usb_drv_core
include usb_drv
procedure usb_msd_init() is
-- setup the USB device
usb_setup()
usb_enable_module()
end procedure
procedure usb_msd_flush() is
pragma inline
usb_handle_isr()
end procedure
usb_msd_init()
const byte str_welcome[] = "JALLIB MSD demo app\n"
var bit has_shown_welcome_msg = true
-- main loop
forever loop
-- poll the usb ISR function on a regular base, in order to
-- serve the USB requests
usb_msd_flush()
-- check if USB device has been configured by the HOST
if usb_is_configured() then
if !has_shown_welcome_msg then
has_shown_welcome_msg = true
print_string( serial_hw_data, str_welcome )
end if
else
has_shown_welcome_msg = false
end if
end loop
--
You received this message because you are subscribed to the Google Groups
"jallib" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/jallib?hl=en.