Revision: 1131 Author: [email protected] Date: Thu Jul 30 15:19:57 2009 Log: [No log message] http://code.google.com/p/jallib/source/detail?r=1131
Added: /trunk/project/pic18f14k50_usb_io/examples/blog_part3.jal ======================================= --- /dev/null +++ /trunk/project/pic18f14k50_usb_io/examples/blog_part3.jal Thu Jul 30 15:19:57 2009 @@ -0,0 +1,325 @@ +-- Title: USB HID keyboard device demo +-- Author: Albert Faber, Copyright (c) 2008..2009, all rights reserved. +-- Adapted-by: - +-- Compiler: >=2.4j +-- +-- This file is part of jallib (http://jallib.googlecode.com) +-- Released under the BSD license (http://www.opensource.org/licenses/bsd-license.php) +-- +-- Description: This application emulates a keyboard USB human interface device +-- (HID), once the USB device has been conifgured and the USB HID device is recognized by +-- the host operating systems, it will generate keyboard chars if pin RB5 is low +-- +-- +-- Sources: http://www.usb.org for USB specifications +-- +-- Notes: compiled with following flags +-- -loader18 -no-fuse +-- use -no-variable-reuse when debugging flags are +-- set due to a compiler issue +-- +-- ------------------------------------------------------ +-- ------------------------------------------------------ +-- +-- +-- +-- This file has been generated from: +-- * board: board_18f14k50_af.jal +-- * test : test_usb_hid_keyboard.jal +-- + +-- chip setup +include 18f14k50 + +-- even though the external crystal is 12 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 + + +include delay + +const bit USB_DEBUG = false +const bit USB_DEBUG_HIGH = false +const bit USB_HID_DEBUG = false +const bit USB_HID_DEBUG_HIGH = false +const byte USB_HID_ENDPOINT = 0x01 + +include usb_defs + +const bit USB_EP0 = 1 +const byte USB_EP0_OUT_SIZE = 8 +const word USB_EP0_OUT_ADDR = ( USB_BASE_ADDRESS + 0x0010 ) +const byte USB_EP0_IN_SIZE = 8 +const word USB_EP0_IN_ADDR = ( USB_EP0_OUT_ADDR + USB_EP0_OUT_SIZE ) + +const bit USB_EP1 = 1 +const byte USB_EP1_OUT_SIZE = 8 +const word USB_EP1_OUT_ADDR = ( USB_EP0_IN_ADDR + USB_EP0_IN_SIZE ) +const byte USB_EP1_IN_SIZE = 8 +const word USB_EP1_IN_ADDR = ( USB_EP1_OUT_ADDR + USB_EP1_OUT_SIZE ) + +var volatile byte usb_ep1in_buf[ 8 ] at USB_EP1_IN_ADDR + +const bit USB_EP2 = 0 +const byte USB_EP2_OUT_SIZE = 8 +const word USB_EP2_OUT_ADDR = 0x0000 +const byte USB_EP2_IN_SIZE = 8 +const word USB_EP2_IN_ADDR = 0x0000 + +const bit USB_EP3 = 0 +const byte USB_EP3_OUT_SIZE = 8 +const word USB_EP3_OUT_ADDR = 0x0000 +const byte USB_EP3_IN_SIZE = 8 +const word USB_EP3_IN_ADDR = 0x0000 + +const byte USB_DEVICE_DESCRIPTOR[USB_DEVICE_DESCRIPTOR_SIZE] = { + USB_DEVICE_DESCRIPTOR_SIZE, -- 18 bytes long + USB_DT_DEVICE, -- DEVICE 01h + 0x00, + 0x02, -- usb version 2.00 + 0x00, -- class + 0x00, -- subclass + 0x00, -- protocol + USB_EP0_OUT_SIZE, -- max packet size for end point 0 + 0xd8, + 0x04, -- Microchip's vendor + 0x55, + 0x00, -- Microchip keyboard demo + 0x01, + 0x00, -- version 1.0 of the product + 0x01, -- string 1 for manufacturer + 0x02, -- string 2 for product + 0x00, -- string 3 for serial number + 0x01 -- number of configurations +} + + +const byte USB_HID_REPORT1[]= +{ + 0x05, 0x01, -- USAGE_PAGE (Generic Desktop) + 0x09, 0x06, -- USAGE (Keyboard) + 0xa1, 0x01, -- COLLECTION (Application) + 0x05, 0x07, -- USAGE_PAGE (Keyboard) + 0x19, 0xe0, -- USAGE_MINIMUM (Keyboard LeftControl) + 0x29, 0xe7, -- USAGE_MAXIMUM (Keyboard Right GUI) + 0x15, 0x00, -- LOGICAL_MINIMUM (0) + 0x25, 0x01, -- LOGICAL_MAXIMUM (1) + 0x75, 0x01, -- REPORT_SIZE (1) + 0x95, 0x08, -- REPORT_COUNT (8) + 0x81, 0x02, -- INPUT (Data,Var,Abs) + 0x95, 0x01, -- REPORT_COUNT (1) + 0x75, 0x08, -- REPORT_SIZE (8) + 0x81, 0x03, -- INPUT (Cnst,Var,Abs) + 0x95, 0x05, -- REPORT_COUNT (5) + 0x75, 0x01, -- REPORT_SIZE (1) + 0x05, 0x08, -- USAGE_PAGE (LEDs) + 0x19, 0x01, -- USAGE_MINIMUM (Num Lock) + 0x29, 0x05, -- USAGE_MAXIMUM (Kana) + 0x91, 0x02, -- OUTPUT (Data,Var,Abs) + 0x95, 0x01, -- REPORT_COUNT (1) + 0x75, 0x03, -- REPORT_SIZE (3) + 0x91, 0x03, -- OUTPUT (Cnst,Var,Abs) + 0x95, 0x06, -- REPORT_COUNT (6) + 0x75, 0x08, -- REPORT_SIZE (8) + 0x15, 0x00, -- LOGICAL_MINIMUM (0) + 0x25, 0x65, -- LOGICAL_MAXIMUM (101) + 0x05, 0x07, -- USAGE_PAGE (Keyboard) + 0x19, 0x00, -- USAGE_MINIMUM (Reserved (no event indicated)) + 0x29, 0x65, -- USAGE_MAXIMUM (Keyboard Application) + 0x81, 0x00, -- INPUT (Data,Ary,Abs) + 0xc0 +} +const USB_CONFIGURATION_DESCRIPTOR_SIZE = 0x09 + 0x09 + 0x09 + 0x07 + +const byte USB_CONFIGURATION_DESCRIPTOR[ USB_CONFIGURATION_DESCRIPTOR_SIZE ]= +{ + -- configuration descriptor - - - - - - - - - - + 0x09, -- length, + USB_DT_CONFIGURATION, -- descriptor_type + + USB_CONFIGURATION_DESCRIPTOR_SIZE, + 0x00, -- total_length; + + 0x01, -- num_interfaces, + 0x01, -- configuration_value, + 0x00, -- configuration_string_id, + 0b10000000, -- attributes (bus powered, no remote wake up) + 100, -- max_power; (200ma) + + -- interface descriptor - - - - - - - - - - - - + 0x09, -- length, + USB_DT_INTERFACE, -- descriptor_type, + 0x00, -- interface_number, (starts at zero) + 0x00, -- alternate_setting, (no alternatives) + 0x01, -- num_endpoints, + USB_HID_INTF, -- interface_class, (HID) + USB_BOOT_INTF_SUBCLASS, -- interface_subclass, (boot) + USB_HID_PROTOCOL_KEYBOARD, -- interface_protocol, (keyboard) + 0x00, -- interface_string_id; + + -- hid descriptor - - - - - - - - - - - - - - - + 0x09, -- length, + USB_DT_HID, -- descriptor_type; + 0x11, + 0x01, -- hid_spec in BCD (1.11) + 0x00, -- country_code, (0=not country specific) + 0x01, -- num_class_descriptors, (1) + USB_DT_HID_REPORT, -- class_descriptor_type; (0x22 = report) + (count( USB_HID_REPORT1 ) & 0xFF ), + (count( USB_HID_REPORT1 ) >> 8 ), + + 0x07, -- length, + USB_DT_ENDPOINT, -- descriptor_type, + 0b10000001, -- endpoint_address, (Endpoint 1, IN) + USB_EPT_INT, -- attributes; (Interrupt) + USB_EP1_IN_SIZE, + 0x00, -- max_packet_size + 0x01 -- interval (1ms) +} + +const byte USB_STRING0[] = +{ + 0x04, -- bLength + USB_DT_STRING, -- bDescriptorType + 0x09, -- wLANGID[0] (low byte) + 0x04 -- wLANGID[0] (high byte) +} + +const byte USB_STRING1[0x36] = +{ + 0x36, -- 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, + " ", 0x00, + "I", 0x00, + "n", 0x00, + "c", 0x00, + ".", 0x00 +} + + + +const byte USB_STRING2[42] = +{ + 42, -- bLength + USB_DT_STRING, -- bDescriptorType + "J", 0x00, + "A", 0x00, + "L", 0x00, + " ", 0x00, + "H", 0x00, + "I", 0x00, + "D", 0x00, + " ", 0x00, + "K", 0x00, + "e", 0x00, + "b", 0x00, + "o", 0x00, + "a", 0x00, + "r", 0x00, + " ", 0x00, + " ", 0x00, + "D", 0x00, + "e", 0x00, + "m", 0x00, + "o", 0x00 +} + +-- include remaining USB libraries +include usb_drv_core +include usb_drv + +-- HID report layout (see USB specification for more details) +-- 0 Modifier byte +-- bit +-- 0 LEFT CTRL +-- 1 LEFT SHIFT +-- 2 LEFT ALT +-- 3 LEFT GUI +-- 4 RIGHT CTRL +-- 5 RIGHT SHIFT +-- 6 RIGHT ALT +-- 7 RIGHT GUI +-- 1 reserved +-- 2 keycode array (0) +-- 3 keycode array (1) +-- 4 keycode array (2) +-- 5 keycode array (3) +-- 6 keycode array (4) +-- 7 keycode array (5) +var byte hid_report_in[8] = { 0,0,0,0,0,0,0,0 } + +var byte key_value = 4 +var bit sw3 is pin_c2 +var bit latched_sw3 = sw3 + +--var bit send_key_stroke = false + + +-- disable analog unit, all ports set to digital +enable_digital_io() + +-- setup the USB device +usb_setup() + +-- enable USB device +usb_enable_module() + +-- set input where switch is attached to input +pin_c2_direction = input + +-- main loop +forever loop + -- poll the usb ISR function on a regular base, in order to + -- serve the USB requests + usb_handle_isr() + + -- check if USB device has been configured by the HOST + if usb_is_configured() then + + -- prepare the HID buffer + if ( sw3 != latched_sw3 ) then + + latched_sw3 = sw3 + + if ( sw3 == low )then + hid_report_in[2] = key_value + key_value = key_value + 1 + if ( key_value == 40 ) then + key_value = 4 + end if + else + hid_report_in[2] = 0 + end if + + -- Send the 8 byte packet over USB to the host. + usb_send_data(USB_HID_ENDPOINT, hid_report_in, count( hid_report_in ) , low ) + + -- debounce + delay_1ms(50) + end if + 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 -~----------~----~----~----~------~----~------~--~---
