From c236823d65c8354b17969a55ed671a1c7ff2602f Mon Sep 17 00:00:00 2001 From: David Barksdale Date: Tue, 2 Aug 2016 07:26:25 -0500 Subject: Trying to get USB support working --- laser-tag software/CMakeLists.txt | 44 +- laser-tag software/README | 11 +- laser-tag software/disk.c | 724 +++++++++++++++++++++++++++++++ laser-tag software/disk.h | 100 +++++ laser-tag software/epaper.c | 7 +- laser-tag software/main.c | 35 +- laser-tag software/usb_descriptor.c | 819 ++++++++++++++++++++++++++++++++++++ laser-tag software/usb_descriptor.h | 225 ++++++++++ 8 files changed, 1945 insertions(+), 20 deletions(-) create mode 100644 laser-tag software/disk.c create mode 100644 laser-tag software/disk.h create mode 100644 laser-tag software/usb_descriptor.c create mode 100644 laser-tag software/usb_descriptor.h diff --git a/laser-tag software/CMakeLists.txt b/laser-tag software/CMakeLists.txt index fa8937d..6fac7c7 100644 --- a/laser-tag software/CMakeLists.txt +++ b/laser-tag software/CMakeLists.txt @@ -34,7 +34,7 @@ SET(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -g -mcpu=cortex-m0plus -mt SET(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g -mcpu=cortex-m0plus -mthumb -MMD -MP -Wall -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -Os -mapcs -std=gnu99") # DEBUG LD FLAGS -SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g --specs=nano.specs -lm -Wall -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -Os -mthumb -mapcs -Xlinker --gc-sections -Xlinker -static -Xlinker -z -Xlinker muldefs -Xlinker --defsym=__stack_size__=0x300 -Xlinker --defsym=__heap_size__=0x200") +SET(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} -g --specs=nano.specs -lm -Wall -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -Os -mthumb -mapcs -Xlinker --gc-sections -Xlinker -static -Xlinker -z -Xlinker muldefs -Xlinker --defsym=__stack_size__=0x300 -Xlinker --defsym=__heap_size__=0x200 -Xlinker --defsym=__ram_vector_table__=1") # RELEASE ASM FLAGS SET(CMAKE_ASM_FLAGS_RELEASE "${CMAKE_ASM_FLAGS_RELEASE} -mcpu=cortex-m0plus -mthumb -Wall -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -Os -mapcs -std=gnu99") @@ -43,7 +43,7 @@ SET(CMAKE_ASM_FLAGS_RELEASE "${CMAKE_ASM_FLAGS_RELEASE} -mcpu=cortex-m0plus -mt SET(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -mcpu=cortex-m0plus -mthumb -MMD -MP -Wall -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -Os -mapcs -std=gnu99") # RELEASE LD FLAGS -SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} --specs=nano.specs -lm -Wall -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -Os -mthumb -mapcs -Xlinker --gc-sections -Xlinker -static -Xlinker -z -Xlinker muldefs -Xlinker --defsym=__stack_size__=0x300 -Xlinker --defsym=__heap_size__=0x200") +SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} --specs=nano.specs -lm -Wall -fno-common -ffunction-sections -fdata-sections -ffreestanding -fno-builtin -Os -mthumb -mapcs -Xlinker --gc-sections -Xlinker -static -Xlinker -z -Xlinker muldefs -Xlinker --defsym=__stack_size__=0x300 -Xlinker --defsym=__heap_size__=0x200 -Xlinker --defsym=__ram_vector_table__=1") # ASM MACRO SET(CMAKE_ASM_FLAGS_DEBUG "${CMAKE_ASM_FLAGS_DEBUG} -DDEBUG") @@ -68,6 +68,18 @@ IF(CMAKE_BUILD_TYPE MATCHES Debug) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/devices) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/devices/MKL27Z4/include) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/devices/MKL27Z4/startup) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/adapter/sources) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/adapter/sources/sdk) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/include/frdmkl27z) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/include) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/classes/common) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/classes/include) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/classes/include/config) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/classes/msd) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/controller/khci) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/controller) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/hal) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/include) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/hal/inc) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/drivers/inc) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/system/inc) @@ -80,6 +92,18 @@ ELSEIF(CMAKE_BUILD_TYPE MATCHES Release) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/devices) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/devices/MKL27Z4/include) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/devices/MKL27Z4/startup) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/adapter/sources) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/adapter/sources/sdk) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/include/frdmkl27z) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/include) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/classes/common) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/classes/include) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/classes/include/config) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/classes/msd) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/controller/khci) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/sources/controller) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/hal) + INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/include) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/hal/inc) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/drivers/inc) INCLUDE_DIRECTORIES(${ProjDirPath}/../KSDK_1.2.0/platform/system/inc) @@ -89,6 +113,10 @@ ENDIF() # ADD_EXECUTABLE ADD_EXECUTABLE(hello_world + "${ProjDirPath}/../KSDK_1.2.0/platform/utilities/src/fsl_debug_console.c" + "${ProjDirPath}/../KSDK_1.2.0/platform/utilities/inc/fsl_debug_console.h" + "${ProjDirPath}/../KSDK_1.2.0/platform/utilities/src/print_scan.c" + "${ProjDirPath}/../KSDK_1.2.0/platform/utilities/src/print_scan.h" "${ProjDirPath}/../KSDK_1.2.0/platform/devices/MKL27Z4/startup/gcc/startup_MKL27Z4.S" "${ProjDirPath}/../KSDK_1.2.0/platform/devices/MKL27Z4/startup/system_MKL27Z4.c" "${ProjDirPath}/../KSDK_1.2.0/platform/devices/startup.c" @@ -100,16 +128,26 @@ ADD_EXECUTABLE(hello_world "${ProjDirPath}/../KSDK_1.2.0/platform/drivers/src/spi/fsl_spi_irq.c" "${ProjDirPath}/main.c" "${ProjDirPath}/epaper.c" + "${ProjDirPath}/epaper.h" "${ProjDirPath}/text.c" + "${ProjDirPath}/text.h" "${ProjDirPath}/radio.c" + "${ProjDirPath}/radio.h" + "${ProjDirPath}/RFM69registers.h" + "${ProjDirPath}/disk.c" + "${ProjDirPath}/disk.h" + "${ProjDirPath}/usb_descriptor.c" + "${ProjDirPath}/usb_descriptor.h" ) SET_TARGET_PROPERTIES(hello_world PROPERTIES OUTPUT_NAME "hello_world.elf") TARGET_LINK_LIBRARIES(hello_world -Wl,--start-group) # LIBRARIES IF(CMAKE_BUILD_TYPE MATCHES Debug) + TARGET_LINK_LIBRARIES(hello_world ${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/build/armgcc/usbd_sdk_frdmkl27z_bm/debug/libusbd_bm.a) TARGET_LINK_LIBRARIES(hello_world ${ProjDirPath}/../KSDK_1.2.0/lib/ksdk_platform_lib/armgcc/KL27Z4/debug/libksdk_platform.a) ELSEIF(CMAKE_BUILD_TYPE MATCHES Release) + TARGET_LINK_LIBRARIES(hello_world ${ProjDirPath}/../KSDK_1.2.0/usb/usb_core/device/build/armgcc/usbd_sdk_frdmkl27z_bm/release/libusbd_bm.a) TARGET_LINK_LIBRARIES(hello_world ${ProjDirPath}/../KSDK_1.2.0/lib/ksdk_platform_lib/armgcc/KL27Z4/release/libksdk_platform.a) ENDIF() @@ -127,3 +165,5 @@ SET(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -Xlinker # BIN AND HEX ADD_CUSTOM_COMMAND(TARGET hello_world POST_BUILD COMMAND ${CMAKE_OBJCOPY} -Oihex ${EXECUTABLE_OUTPUT_PATH}/hello_world.elf ${EXECUTABLE_OUTPUT_PATH}/hello_world.hex) ADD_CUSTOM_COMMAND(TARGET hello_world POST_BUILD COMMAND ${CMAKE_OBJCOPY} -Obinary ${EXECUTABLE_OUTPUT_PATH}/hello_world.elf ${EXECUTABLE_OUTPUT_PATH}/hello_world.bin) + +# vim: set expandtab ts=4 sw=4: diff --git a/laser-tag software/README b/laser-tag software/README index 2573f83..600f97c 100644 --- a/laser-tag software/README +++ b/laser-tag software/README @@ -1,10 +1,13 @@ Build instructions for Ubuntu: 0) apt-get install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi 1) export ARMGCC_DIR=/usr - cd KSDK_1.2.0/lib/ksdk_platform_lib/armgcc/KL27Z4 - ./build_debug.sh -2) cd "laser-tag software" - ./build_debug.sh + cd ../KSDK_1.2.0/lib/ksdk_platform_lib/armgcc/KL27Z4 + ./build_all.sh + cd - +2) cd ../KSDK_1.2.0/usb/usb_core/device/build/armgcc/usbd_sdk_frdmkl27z_bm + ./build_all.sh + cd - +3) ./build_debug.sh -or- ./build_release.sh Load instructions Kinetis Bootloader: 0) Download the Kinetis Bootloader[0]. (registration required) diff --git a/laser-tag software/disk.c b/laser-tag software/disk.c new file mode 100644 index 0000000..84b1c3f --- /dev/null +++ b/laser-tag software/disk.c @@ -0,0 +1,724 @@ +/**HEADER******************************************************************** + * + * Copyright (c) 2008, 2013 - 2014 Freescale Semiconductor; + * All Rights Reserved + * + * Copyright (c) 1989-2008 ARC International; + * All Rights Reserved + * + *************************************************************************** + * + * THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS 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. + * + ************************************************************************** + * + * $FileName: disk.c$ + * $Version : + * $Date : + * + * Comments: + * + * @brief RAM Disk has been emulated via this Mass Storage Demo + *****************************************************************************/ + +/****************************************************************************** + * Includes + *****************************************************************************/ +#include "usb_device_config.h" +#include "usb.h" +#include "usb_device_stack_interface.h" +#include "disk.h" +#include "adapter_cfg.h" +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) +#include "fsl_device_registers.h" +#include "fsl_clock_manager.h" +//#include "board.h" +#include "fsl_debug_console.h" +#if (TWR_K64F120M || FRDM_K64F || TWR_K60D100M || TWR_K21F120M || TWR_K65F180M) +#include "fsl_mpu_hal.h" +#endif +#include "fsl_port_hal.h" +#include +#include +#endif + +#if SD_CARD_APP +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM) +#include "sd_esdhc_kinetis.h" +#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) +#include "fsl_sdhc_card.h" +#include "fsl_gpio_driver.h" +#include "gpio_pins.h" +#include "board.h" +#else +#include "sd.h" +#endif +#endif + +#if USBCFG_DEV_COMPOSITE +#error This application requires USBCFG_DEV_COMPOSITE defined zero in usb_device_config.h. Please recompile usbd with this option. +#endif + +#if !USBCFG_DEV_MSC +#error This application requires USBCFG_DEV_MSC defined none zero in usb_device_config.h. Please recompile usbd with this option. +#endif + +#if HIGH_SPEED + +#if !USBCFG_DEV_EHCI +#error This application requires USBCFG_DEV_EHCI defined none zero in usb_device_config.h. Please recompile usbd with this option. +#endif + +#endif + +extern void Main_Task(uint32_t param); +#define MAIN_TASK 10 + +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX) +TASK_TEMPLATE_STRUCT MQX_template_list[] = +{ + { MAIN_TASK, Main_Task, 2 * 3000L, 9L, "Main", MQX_AUTO_START_TASK, 0, 0 }, + { 0L, 0L, 0L, 0L, 0L, 0L, 0, 0 } +}; + +MQX_FILE_PTR g_sdcard_handle; +#endif +/***************************************************************************** + * Constant and Macro's - None + *****************************************************************************/ + +/***************************************************************************** + * Global Functions Prototypes + *****************************************************************************/ +void TestApp_Init(void); + +/**************************************************************************** + * Global Variables + ****************************************************************************/ +/* Add all the variables needed for disk.c to this structure */ +extern usb_desc_request_notify_struct_t desc_callback; +usb_application_callback_struct_t msc_application_callback; +usb_vendor_req_callback_struct_t vend_req_callback; +usb_class_specific_callback_struct_t class_specific_callback; + +msc_config_struct_t g_msd_config; +#if (RAM_DISK_APP) +/* Here set storage_disk value to put the 24k buffer to RW region, + if putting in ZI and 64k ram the ZI region cannot be put in upper/bottom 32k ram for iar */ +disk_struct_t g_disk = +{ + .storage_disk = {0x01}, +}; +#else +disk_struct_t g_disk; +#endif + +#if SD_CARD_APP + #define USE_SDHC_PROTOCOL (1) + #define USE_SPI_PROTOCOL (0) +#endif + +/***************************************************************************** + * Local Types - None + *****************************************************************************/ + +/***************************************************************************** + * Local Functions Prototypes + *****************************************************************************/ +void USB_App_Device_Callback(uint8_t event_type, void* val, void* arg); +uint8_t USB_App_Class_Callback +(uint8_t event_type, + uint16_t value, + uint8_t ** data, + uint32_t* size, + void* arg + ); +void Disk_App(void); +/***************************************************************************** + * Local Variables + *****************************************************************************/ +#if SD_CARD_APP +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) +#define SDCARD_BLOCK_SIZE_POWER (9) +sdhc_card_t card = {0}; +sdhc_host_t host = {0}; +#endif +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM || OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) +uint32_t g_msc_bulk_out_buff_temp[MSD_RECV_BUFFER_NUM][MSD_RECV_BUFFER_SIZE >> 2]; +uint32_t g_msc_bulk_in_buff_temp[MSD_SEND_BUFFER_SIZE >> 2]; +#endif +uint8_t *g_msc_bulk_out_buff; +uint8_t *g_msc_bulk_in_buff; +#endif +/***************************************************************************** + * Local Functions + *****************************************************************************/ +#if SD_CARD_APP +os_msgq_handle write_buffer_msg_handler; + +/****************************************************************************** + * + * @name get_write_buffer + * + * @brief This function get the buffer for writing + * + * @param None + * + * @return buffer pointer (return NULL when there is no idle buffer ) + ** + *****************************************************************************/ +uint32_t* get_write_buffer(void) +{ + uint32_t msg; + /* wait forever for RTOS; return immediately for bm */ + if (OS_MsgQ_recv(write_buffer_msg_handler, (void*)&msg, OS_MSGQ_RECEIVE_BLOCK_ON_EMPTY, 0) == OS_MSGQ_OK) + { + return (uint32_t*)msg; + } + return NULL; +} + +/****************************************************************************** + * + * @name get_write_buffer + * + * @brief This function get the buffer for writing + * + * @param buffer : The buffer pointer + * + * @return None + ** + *****************************************************************************/ +void free_write_buffer(uint32_t* buffer) +{ + uint32_t address = (uint32_t)buffer; + OS_MsgQ_send(write_buffer_msg_handler, (void*)(&address), 0); +} +#endif + +#if MUTILE_BUFFER && (((OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) && (USE_RTOS)) || (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX)) +os_msgq_handle write_msg_handler; +os_mutex_handle sdhc_mutex; + +static void write_task(void *arg) +{ + uint8_t* buff_ptr; + uint32_t offset; + uint32_t size; + uint32_t msg[3]; +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) + sdhc_status_t error_code = kStatus_SDHC_NoError; +#else + _mqx_uint error_code = MQX_OK; +#endif + + while (OS_MsgQ_recv(write_msg_handler, (void*)&msg, OS_MSGQ_RECEIVE_BLOCK_ON_EMPTY, 0) == OS_MSGQ_OK) + { + buff_ptr = (uint8_t*)msg[0]; + offset = msg[1]; + size = msg[2]; + + OS_Mutex_lock(sdhc_mutex); +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) + error_code = SDCARD_DRV_WriteBlocks(&card, buff_ptr, offset >> SDCARD_BLOCK_SIZE_POWER, size >> SDCARD_BLOCK_SIZE_POWER); +#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX) + error_code = sd_write_device_sectors(g_sdcard_handle, offset >> SDCARD_BLOCK_SIZE_POWER, size >> SDCARD_BLOCK_SIZE_POWER, 3, buff_ptr, NULL); +#endif + OS_Mutex_unlock(sdhc_mutex); + free_write_buffer((uint32_t*)buff_ptr); +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) + if (error_code != kStatus_SDHC_NoError) +#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX) + if (error_code != MQX_OK) +#endif + { + g_disk.read_write_error = 1; + USB_PRINTF("Write SD card error, error = 0x%x.\tPlease check recv buff size(must be less than 128 sectors).\r\n", error_code); + } + } +} +#endif + +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK && SD_CARD_APP) +#if defined(FSL_RTOS_MQX) +#include "fsl_sdhc_driver.h" +void SDHC_MQX_IRQHandler(void) +{ +#if defined BSP_FSL_SDHC_USING_IRQ + SDHC_DRV_DoIrq(0); +#endif +} +#endif + +/* don't use interrupt to detect the sdcard */ +gpio_input_pin_user_config_t sdhc_detect_pin[2]; + +void sdhc_detect_io_init(void) +{ + for (int i = 0; i < 2; ++i) + { + sdhc_detect_pin[i].pinName = sdhcCdPin[i].pinName; + sdhc_detect_pin[i].config.isPullEnable = sdhcCdPin[i].config.isPullEnable; + sdhc_detect_pin[i].config.pullSelect = sdhcCdPin[i].config.pullSelect; + sdhc_detect_pin[i].config.isPassiveFilterEnabled = sdhcCdPin[i].config.isPassiveFilterEnabled; + sdhc_detect_pin[i].config.interrupt = kPortIntDisabled; /* don't use interrupt */ + } + + GPIO_DRV_Init(sdhc_detect_pin, NULL); +} + +uint8_t sdhc_detect(void) +{ + uint32_t value = 0; + if (sdhc_detect_pin[0].config.pullSelect == kPortPullUp) /* pull up */ + { + value = GPIO_DRV_ReadPinInput(sdhc_detect_pin[0].pinName); + return (!value); + } + else /* pull down */ + { + return (GPIO_DRV_ReadPinInput(sdhc_detect_pin[0].pinName)); + } +} + +uint8_t SD_Init(void) +{ + sdhc_user_config_t config = {0}; + /* enable SDHC module instance */ + CLOCK_SYS_EnableSdhcClock(0); + /* disable mpu for kSdhcTransModeAdma2 */ +#if (TWR_K64F120M || FRDM_K64F || TWR_K60D100M || TWR_K21F120M || TWR_K65F180M) + MPU_HAL_Disable(MPU); +#endif + /* initialize user sdhc configuration structure */ + config.transMode = kSdhcTransModeAdma2; //kSdhcTransModePio; + config.clock = SDMMC_CLK_100KHZ; + config.cdType = kSdhcCardDetectGpio; + /* initialize the SDHC driver with the user configuration */ + if (SDHC_DRV_Init(BOARD_SDHC_INSTANCE, &host, &config) != kStatus_SDHC_NoError) + { + USB_PRINTF("SDHC_DRV_Init failed\r\n"); + return 0; + } + +#if defined (FSL_RTOS_MQX) + OS_install_isr(SDHC_IRQn, SDHC_MQX_IRQHandler, NULL); +#endif +#if (USE_RTOS) + OS_intr_init(SDHC_IRQn, 5, 0, TRUE); +#endif + + /* A card is detected, now initialize the card driver */ + if (kStatus_SDHC_NoError != SDCARD_DRV_Init(&host, &card)) + { + USB_PRINTF("SDCARD_DRV_Init failed\r\n"); + SDHC_DRV_Shutdown(card.hostInstance); + return 0; + } + return 1; +} + +#endif + +/****************************************************************************** + * + * @name Disk_App + * + * @brief + * + * @param None + * + * @return None + * + *****************************************************************************/ +void Disk_App(void) +{ + /* User Code */ + return; +} + +/****************************************************************************** + * + * @name USB_App_Device_Callback + * + * @brief This function handles the callback + * + * @param pointer : + * @param event_type : value of the event + * @param val : gives the configuration value + * + * @return None + * + *****************************************************************************/ +void USB_App_Device_Callback(uint8_t event_type, void* val, void* arg) +{ + if (event_type == USB_DEV_EVENT_BUS_RESET) + { + g_disk.start_app = FALSE; + if (USB_OK == USB_Class_MSC_Get_Speed(g_disk.app_handle, &g_disk.speed)) + { + USB_Desc_Set_Speed(g_disk.app_handle, g_disk.speed); + } + } + else if (event_type == USB_DEV_EVENT_ENUM_COMPLETE) + { + g_disk.start_app = TRUE; + } + else if (event_type == USB_DEV_EVENT_ERROR) + { + /* add user code for error handling */ + } + else if (event_type == USB_MSC_DEVICE_GET_SEND_BUFF_INFO) + { + if (NULL != val) + { +#if SD_CARD_APP + *((uint32_t *)val) = (uint32_t)MSD_SEND_BUFFER_SIZE; +#elif RAM_DISK_APP + *((uint32_t *)val) = (uint32_t)DISK_SIZE_NORMAL; +#endif + } + } + else if (event_type == USB_MSC_DEVICE_GET_RECV_BUFF_INFO) + { + if (NULL != val) + { +#if SD_CARD_APP + *((uint32_t *)val) = (uint32_t)MSD_RECV_BUFFER_SIZE; +#elif RAM_DISK_APP + *((uint32_t *)val) = (uint32_t)DISK_SIZE_NORMAL; +#endif + } + } + + return; +} + +/****************************************************************************** + * + * @name USB_App_Class_Callback + * + * @brief This function handles the callback + * + * @param pointer : + * @param event_type : value of the event + * @param val : gives the configuration value + * + * @return None + * + *****************************************************************************/ +uint8_t USB_App_Class_Callback +(uint8_t event_type, + uint16_t value, + uint8_t ** data, + uint32_t* size, + void* arg +) +{ + lba_app_struct_t* lba_data_ptr; + uint8_t * prevent_removal_ptr; + device_lba_info_struct_t* device_lba_info_ptr; + uint8_t error = USB_OK; +#if SD_CARD_APP && (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX) + _mqx_uint error_code = MQX_OK; +#endif +#if SD_CARD_APP && (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) + sdhc_status_t error_code = kStatus_SDHC_NoError; +#endif + + if (g_disk.read_write_error) + { + return USBERR_ERROR; + } + + switch(event_type) + { + case USB_DEV_EVENT_DATA_RECEIVED: + /* Add User defined code -- if required*/ + lba_data_ptr = (lba_app_struct_t*) size; +#if RAM_DISK_APP +#elif SD_CARD_APP + if (lba_data_ptr->size == 0) + free_write_buffer((uint32_t*)lba_data_ptr->buff_ptr); + else + { +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM) + SD_Write_Block(lba_data_ptr); + free_write_buffer((uint32_t*)lba_data_ptr->buff_ptr); +#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) +#if MUTILE_BUFFER && (USE_RTOS) + uint32_t msg[3] = {(uint32_t)lba_data_ptr->buff_ptr, lba_data_ptr->offset, lba_data_ptr->size}; + OS_MsgQ_send(write_msg_handler, (void*)(msg), 0); +#else + error_code = SDCARD_DRV_WriteBlocks(&card, lba_data_ptr->buff_ptr, lba_data_ptr->offset >> SDCARD_BLOCK_SIZE_POWER, + lba_data_ptr->size >> SDCARD_BLOCK_SIZE_POWER); + free_write_buffer((uint32_t*)lba_data_ptr->buff_ptr); + if (error_code != kStatus_SDHC_NoError) + { + error = USBERR_ERROR; + g_disk.read_write_error=1; + USB_PRINTF("Write SD card error, error = 0x%x.\tPlease check recv buff size(must be less than 128 sectors).\r\n", error_code); + } +#endif + +#else +#if MUTILE_BUFFER + uint32_t msg[3] = {(uint32_t)lba_data_ptr->buff_ptr, lba_data_ptr->offset, lba_data_ptr->size}; + OS_MsgQ_send(write_msg_handler, (void*)(msg), 0); +#else + error_code = sd_write_device_sectors(g_sdcard_handle, (lba_data_ptr->offset >> SDCARD_BLOCK_SIZE_POWER), + (lba_data_ptr->size >> SDCARD_BLOCK_SIZE_POWER) , 3, lba_data_ptr->buff_ptr,NULL); + free_write_buffer((uint32_t*)lba_data_ptr->buff_ptr); + if (error_code != MQX_OK) + { + error = USBERR_ERROR; + g_disk.read_write_error=1; + USB_PRINTF("Write SD card error, error = 0x%x.\tPlease check recv buff size(must be less than 128 sectors).\r\n", error_code); + } +#endif +#endif + } +#endif + break; + case USB_DEV_EVENT_SEND_COMPLETE: + /* Add User defined code -- if required*/ + lba_data_ptr = (lba_app_struct_t*) size; + if (lba_data_ptr->size != 0) + { + /* read data from mass storage device to driver buffer */ +#if RAM_DISK_APP + if(data != NULL) + { + *data = g_disk.storage_disk + lba_data_ptr->offset; + } +#elif SD_CARD_APP +#endif + } + break; + case USB_MSC_START_STOP_EJECT_MEDIA: + /* Code to be added by user for starting, stopping or + ejecting the disk drive. e.g. starting/stopping the motor in + case of CD/DVD*/ + break; + case USB_MSC_DEVICE_READ_REQUEST: + lba_data_ptr = (lba_app_struct_t*) size; + +#if RAM_DISK_APP + if(data != NULL) + { + *data = g_disk.storage_disk + lba_data_ptr->offset; + } +#elif SD_CARD_APP + if(data != NULL) + { + *data = g_msc_bulk_in_buff; + } + lba_data_ptr->buff_ptr = g_msc_bulk_in_buff; +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM) + SD_Read_Block(lba_data_ptr); +#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) +#if MUTILE_BUFFER && (USE_RTOS) + OS_Mutex_lock(sdhc_mutex); +#endif + error_code = SDCARD_DRV_ReadBlocks(&card, lba_data_ptr->buff_ptr, lba_data_ptr->offset >> SDCARD_BLOCK_SIZE_POWER, + lba_data_ptr->size >> SDCARD_BLOCK_SIZE_POWER); +#if MUTILE_BUFFER && (USE_RTOS) + OS_Mutex_unlock(sdhc_mutex); +#endif + if (error_code != kStatus_SDHC_NoError) + { + error = USBERR_ERROR; + g_disk.read_write_error=1; + USB_PRINTF("Read SD card error, error = 0x%x.\tPlease check send buff size(must be less than 128 sectors).\r\n", error_code); + } +#else +#if MUTILE_BUFFER + OS_Mutex_lock(sdhc_mutex); +#endif + error_code = sd_read_device_sectors(g_sdcard_handle, (lba_data_ptr->offset >> SDCARD_BLOCK_SIZE_POWER), + (lba_data_ptr->size >> SDCARD_BLOCK_SIZE_POWER) , 3, lba_data_ptr->buff_ptr,NULL); +#if MUTILE_BUFFER + OS_Mutex_unlock(sdhc_mutex); +#endif + if (error_code != MQX_OK) + { + error = USBERR_ERROR; + g_disk.read_write_error=1; + USB_PRINTF("Read SD card error, error = 0x%x.\tPlease check send buff size(must be less than 128 sectors).\r\n", error_code); + } +#endif +#endif + break; + case USB_MSC_DEVICE_WRITE_REQUEST: + lba_data_ptr = (lba_app_struct_t*) size; +#if RAM_DISK_APP + if(data != NULL) + { + *data = g_disk.storage_disk + lba_data_ptr->offset; + } +#elif SD_CARD_APP + if(data != NULL) + { + /* get_write_buffer should return NULL when there are no buffers for writing */ + *data = (uint8_t*)get_write_buffer(); + } +#endif + break; + case USB_MSC_DEVICE_FORMAT_COMPLETE: + break; + case USB_MSC_DEVICE_REMOVAL_REQUEST: + prevent_removal_ptr = (uint8_t *) size; + if (SUPPORT_DISK_LOCKING_MECHANISM) + { + g_disk.disk_lock = *prevent_removal_ptr; + } + else if ((!SUPPORT_DISK_LOCKING_MECHANISM) && (!(*prevent_removal_ptr))) + { + /*there is no support for disk locking and removal of medium is enabled*/ + /* code to be added here for this condition, if required */ + } + break; + case USB_MSC_DEVICE_GET_INFO: + device_lba_info_ptr = (device_lba_info_struct_t*) size; +#if RAM_DISK_APP + device_lba_info_ptr->total_lba_device_supports = TOTAL_LOGICAL_ADDRESS_BLOCKS_NORMAL; + device_lba_info_ptr->length_of_each_lab_of_device = LENGTH_OF_EACH_LAB; +#elif SD_CARD_APP +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM) + SD_Card_Info(&device_lba_info_ptr->total_lba_device_supports, + &device_lba_info_ptr->length_of_each_lab_of_device); +#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) + device_lba_info_ptr->total_lba_device_supports = card.blockCount; + device_lba_info_ptr->length_of_each_lab_of_device = card.blockSize; +#else + sd_get_block_size(g_sdcard_handle, &device_lba_info_ptr->length_of_each_lab_of_device); + sd_get_blocks_num(g_sdcard_handle, &device_lba_info_ptr->total_lba_device_supports); +#endif +#endif + device_lba_info_ptr->num_lun_supported = LOGICAL_UNIT_SUPPORTED; + break; + default: + break; + } + + return error; +} + +/****************************************************************************** + * + * @name TestApp_Init + * + * @brief This function is the entry for mouse (or other usage) + * + * @param None + * + * @return None + ** + *****************************************************************************/ + +void APP_init(void) +{ +#if SD_CARD_APP + +#if ((OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM) || (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK)) + g_msc_bulk_in_buff = (uint8_t*)g_msc_bulk_in_buff_temp; + g_msc_bulk_out_buff = (uint8_t*)g_msc_bulk_out_buff_temp; +#elif (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX) + g_msc_bulk_in_buff = (uint8_t*)OS_Mem_alloc_uncached_align(MSD_SEND_BUFFER_SIZE, 32); + g_msc_bulk_out_buff = (uint8_t*)OS_Mem_alloc_uncached_align(MSD_RECV_BUFFER_SIZE * MSD_RECV_BUFFER_NUM, 32); +#endif + +#endif + +#if SD_CARD_APP + USB_PRINTF("Please insert SD card\n"); +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM || OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_SDK) +#if USE_SDHC_PROTOCOL + sdhc_detect_io_init(); +#endif + while (!sdhc_detect()) + { + ; + } /* SD Card inserted */ + if(!SD_Init()) return; /* Initialize SD_CARD and SPI Interface */ +#else + g_sdcard_handle = sd_init(); + +#endif + USB_PRINTF("\nSD card inserted!\n"); + +#endif + OS_Mem_zero(&g_disk, sizeof(disk_struct_t)); + + OS_Mem_zero(&g_msd_config, sizeof(msc_config_struct_t)); + + msc_application_callback.callback = USB_App_Device_Callback; + msc_application_callback.arg = &g_disk.app_handle; + + /* Register the callbacks to lower layers */ + g_msd_config.msc_application_callback = msc_application_callback; + g_msd_config.vendor_req_callback = vend_req_callback; + g_msd_config.class_specific_callback.callback = USB_App_Class_Callback; + g_msd_config.class_specific_callback.arg = &g_disk.app_handle; + g_msd_config.desc_callback_ptr = &desc_callback; + + g_disk.speed = USB_SPEED_FULL; + /* Finally, Initialize the device and USB Stack layers*/ + USB_Class_MSC_Init(CONTROLLER_ID, &g_msd_config, &g_disk.app_handle); + +#if SD_CARD_APP + write_buffer_msg_handler = OS_MsgQ_create((MSD_RECV_BUFFER_NUM + 1), 1); //msg_size = 4Bytes + for (int i = 0; i < MSD_RECV_BUFFER_NUM; ++i) + { + uint32_t address = (uint32_t)(&g_msc_bulk_out_buff[i * MSD_RECV_BUFFER_SIZE]); + OS_MsgQ_send(write_buffer_msg_handler, (void*)(&address), 0); + } +#endif +} + +void APP_task() +{ + if (g_disk.read_write_error) + { + return; + } + Disk_App(); +} + +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_MQX) +/*FUNCTION*---------------------------------------------------------------- + * + * Function Name : Main_Task + * Returned Value : None + * Comments : + * First function called. Calls Test_App + * callback functions. + * + *END*--------------------------------------------------------------------*/ +void Main_Task +( + uint32_t param + ) +{ + UNUSED_ARGUMENT (param) + APP_init(); +#if MUTILE_BUFFER + /* write sdhc task */ + sdhc_mutex = OS_Mutex_create(); + write_msg_handler = OS_MsgQ_create((MSD_RECV_BUFFER_NUM), 3);/* msg_size = 12Bytes */ + OS_Task_create(write_task, NULL, 8L, 2000L, "write_task", NULL); +#endif +} +#endif + +/* EOF */ diff --git a/laser-tag software/disk.h b/laser-tag software/disk.h new file mode 100644 index 0000000..a8ddb20 --- /dev/null +++ b/laser-tag software/disk.h @@ -0,0 +1,100 @@ +/**HEADER******************************************************************** + * + * Copyright (c) 2008, 2013 - 2014 Freescale Semiconductor; + * All Rights Reserved + * + * Copyright (c) 1989-2008 ARC International; + * All Rights Reserved + * + *************************************************************************** + * + * THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS 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. + * + ************************************************************************** + * + * $FileName: disk.h$ + * $Version : + * $Date : + * + * Comments: + * + * @brief The file contains Macro's and functions needed by the disk + * application + * + *****************************************************************************/ + +#ifndef _DISK_H +#define _DISK_H 1 + +#include "usb_descriptor.h" +/****************************************************************************** + * Constants - None + *****************************************************************************/ + +/****************************************************************************** + * Macro's + *****************************************************************************/ +#define RAM_DISK_APP (1) +#define SD_CARD_APP (0) + +#if RAM_DISK_APP + /* Length of Each Logical Address Block */ + #define LENGTH_OF_EACH_LAB (512) + /* total number of logical blocks present */ + #define TOTAL_LOGICAL_ADDRESS_BLOCKS_NORMAL (4) + /* Net Disk Size */ + #define DISK_SIZE_NORMAL (TOTAL_LOGICAL_ADDRESS_BLOCKS_NORMAL * LENGTH_OF_EACH_LAB) +#endif + +#define LOGICAL_UNIT_SUPPORTED (1) + +#define SUPPORT_DISK_LOCKING_MECHANISM (0) /*1: TRUE; 0:FALSE*/ + +#if SD_CARD_APP + #define MUTILE_BUFFER (1) +#endif +#if MUTILE_BUFFER + #define MSD_RECV_BUFFER_NUM (2) + #else + #define MSD_RECV_BUFFER_NUM (1) +#endif +#define MSD_RECV_BUFFER_SIZE (512) +#define MSD_SEND_BUFFER_SIZE (512) +/***************************************************************************** + * Global variables + *****************************************************************************/ + +/****************************************************************************** + * Types + *****************************************************************************/ +typedef struct _disk_variable_struct +{ + msd_handle_t app_handle; + uint32_t start_app; + uint16_t speed; +#if RAM_DISK_APP + uint8_t storage_disk[DISK_SIZE_NORMAL]; +#endif + uint8_t disk_lock; + uint8_t read_write_error; +} disk_struct_t; + +/***************************************************************************** + * Global Functions + *****************************************************************************/ +extern void APP_init(void); +extern void Disk_App(void); + +#endif + +/* EOF */ diff --git a/laser-tag software/epaper.c b/laser-tag software/epaper.c index fb4d8ef..8bd1dbf 100644 --- a/laser-tag software/epaper.c +++ b/laser-tag software/epaper.c @@ -1,4 +1,5 @@ #include "epaper.h" +#include "fsl_debug_console.h" #include "fsl_gpio_driver.h" #include "fsl_spi_master_driver.h" @@ -20,7 +21,7 @@ static const gpio_output_pin_user_config_t pinCS = { }; static const spi_master_user_config_t spiConfig = { - .bitsPerSec = 2000000, /* 2 MHz, max is 20 MHz */ + .bitsPerSec = 20000000, /* 20 MHz, max is 20 MHz */ .polarity = kSpiClockPolarity_ActiveHigh, .phase = kSpiClockPhase_FirstEdge, .direction = kSpiMsbFirst, @@ -116,7 +117,7 @@ static void EPD_Delay(uint32_t ms) for (; ms > 0; --ms) { /* XXX This is really stupid */ volatile int i; - for (i = 0; i < 306; ++i); + for (i = 0; i < 306 * 48; ++i); } } @@ -203,7 +204,7 @@ static void EPD_frame_repeat(const uint8_t *data, uint8_t fixed_value, { /* TODO this needs to repeat for about 630ms */ int i; - for (i = 0; i < 2; ++i) { + for (i = 0; i < 4; ++i) { EPD_frame(data, fixed_value, stage); } } diff --git a/laser-tag software/main.c b/laser-tag software/main.c index 4007f2d..36a6ab6 100644 --- a/laser-tag software/main.c +++ b/laser-tag software/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015 David Barksdale + * Copyright (c) 2015 - 2016 David Barksdale * Copyright (c) 2013 - 2014, Freescale Semiconductor, Inc. * All rights reserved. * @@ -29,12 +29,12 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "disk.h" #include "epaper.h" -#include "text.h" -#include "radio.h" #include "fsl_clock_manager.h" #include "fsl_cmp_driver.h" #include "fsl_dac_driver.h" +#include "fsl_debug_console.h" #include "fsl_dma_driver.h" #include "fsl_flexio_driver.h" #include "fsl_gpio_driver.h" @@ -44,11 +44,12 @@ #include "fsl_pit_driver.h" #include "fsl_smc_hal.h" #include "fsl_tpm_driver.h" +#include "radio.h" +#include "text.h" //////////////////////////// // Graphics resources - #include "aha.xbm" #include "dc24.xbm" #include "my_name_is.xbm" @@ -215,7 +216,7 @@ static cmp_dac_config_t g_cmpDacConf = { /* LPUART0 config */ static lpuart_user_config_t g_lpuartConfig = { - .clockSource = kClockLpuartSrcMcgIrClk, + .clockSource = kClockLpuartSrcIrc48M, .baudRate = 9600, .parityMode = kLpuartParityEven, .stopBitCount = kLpuartOneStopBit, @@ -243,7 +244,7 @@ static flexio_timer_config_t g_timerConfig = { .timena = kFlexioTimerEnableOnTriggerHigh, .tstop = kFlexioTimerStopBitDisabled, .tstart = kFlexioTimerStartBitDisabled, - .timcmp = (32 * 2 - 1) << 8 | (2 - 1), // 32 bits at 2 MHz + .timcmp = (32 * 2 - 1) << 8 | (12 - 1), // 32 bits at 2 MHz }; static flexio_shifter_config_t g_shifterConfig = { @@ -486,7 +487,7 @@ int main (void) { /* enable clock for PORTs */ CLOCK_SYS_EnablePortClock(PORTA_IDX); - //CLOCK_SYS_EnablePortClock(PORTB_IDX); + CLOCK_SYS_EnablePortClock(PORTB_IDX); CLOCK_SYS_EnablePortClock(PORTC_IDX); CLOCK_SYS_EnablePortClock(PORTD_IDX); CLOCK_SYS_EnablePortClock(PORTE_IDX); @@ -495,7 +496,15 @@ int main (void) SMC_HAL_SetProtection(SMC, kAllowPowerModeAll); /* Set system clock configuration. */ - CLOCK_SYS_SetConfiguration(&g_defaultClockConfigVlpr); + CLOCK_SYS_SetConfiguration(&g_defaultClockConfigRun); + + /* Break everything */ + OSA_Init(); + + /* Setup Debug console on LPUART0 on PTB17 */ + PORT_HAL_SetMuxMode(PORTB, 17, kPortMuxAlt3); + CLOCK_SYS_SetLpuartSrc(0, kClockLpuartSrcIrc48M); + DbgConsole_Init(0, 9600, kDebugConsoleLPUART); /* Initialize LPTMR */ lptmr_state_t lptmrState; @@ -539,7 +548,7 @@ int main (void) /* Setup FlexIO for the WS2812B */ FLEXIO_Type *fiobase = g_flexioBase[0]; - CLOCK_SYS_SetFlexioSrc(0, kClockFlexioSrcMcgIrClk); + CLOCK_SYS_SetFlexioSrc(0, kClockFlexioSrcIrc48M); FLEXIO_DRV_Init(0, &g_flexioConfig); FLEXIO_HAL_ConfigureTimer(fiobase, 0, &g_timerConfig); FLEXIO_HAL_ConfigureShifter(fiobase, 0, &g_shifterConfig); @@ -562,7 +571,7 @@ int main (void) .triggerSource = kTpmTrigSel0, }; TPM_DRV_Init(0, &tmpConfig); - TPM_DRV_SetClock(0, kTpmClockSourceModuleMCGIRCLK, kTpmDividedBy1); + TPM_DRV_SetClock(0, kTpmClockSourceModuleHighFreq, kTpmDividedBy8); /* Blank LED just in case, saves power */ led(0x00, 0x00, 0x00); @@ -572,6 +581,7 @@ int main (void) /* Throw up first image */ int ret = EPD_Draw(NULL, images[current_image]); + debug_printf("Returned from EPD_Draw %d\r\n", ret); if (-1 == ret) { led(0xff, 0x00, 0x00); } else if (-2 == ret) { @@ -583,11 +593,14 @@ int main (void) } blank_led = 30; - ret = radio_init(); + ret = 0; //radio_init(); if (0 == ret) { led(0x22, 0x00, 0x22); } + /* No good can come of this */ + //APP_init(); + /* We're done, everything else is triggered through interrupts */ for(;;) { if (cue_next_image) { diff --git a/laser-tag software/usb_descriptor.c b/laser-tag software/usb_descriptor.c new file mode 100644 index 0000000..29d7a76 --- /dev/null +++ b/laser-tag software/usb_descriptor.c @@ -0,0 +1,819 @@ +/**HEADER******************************************************************** + * + * Copyright (c) 2008, 2013 - 2015 Freescale Semiconductor; + * All Rights Reserved + * + * Copyright (c) 1989-2008 ARC International; + * All Rights Reserved + * + *************************************************************************** + * + * THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS 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. + * + ************************************************************************** + * + * $FileName: usb_descriptor.c$ + * $Version : + * $Date : + * + * Comments: + * + * @brief The file contains USB descriptors and functions + * + *****************************************************************************/ + +/****************************************************************************** + * Includes + *****************************************************************************/ +#include "usb_device_config.h" +#include "usb.h" +#include "usb_device_stack_interface.h" +#include "usb_descriptor.h" + +extern void USB_App_Class_Callback +(uint8_t event_type, + uint16_t value, + uint8_t ** data, + uint32_t* size, + void* arg +) ; +/***************************************************************************** + * Constant and Macro's + *****************************************************************************/ +usb_ep_struct_t ep[MSC_DESC_ENDPOINT_COUNT] = +{ + { + BULK_IN_ENDPOINT, + USB_BULK_PIPE, + USB_SEND, + BULK_IN_ENDP_PACKET_SIZE + }, + { + BULK_OUT_ENDPOINT, + USB_BULK_PIPE, + USB_RECV, + BULK_OUT_ENDP_PACKET_SIZE + } +}; + +/* structure containing details of all the endpoints used by this device */ +usb_endpoints_t usb_desc_ep = +{ + MSC_DESC_ENDPOINT_COUNT, + ep +}; + +static usb_if_struct_t usb_if[1]; +static device_lba_info_struct_t usb_msc_lba_info_struct; +static const usb_class_struct_t usb_dec_class = +{ + USB_CLASS_MSC, + { + 1, /* Count */ + usb_if + } +}; + +uint8_t g_device_descriptor[DEVICE_DESCRIPTOR_SIZE] = +{ + /* "Device Descriptor Size */ + DEVICE_DESCRIPTOR_SIZE, + /* "Device" Type of descriptor */ + USB_DEVICE_DESCRIPTOR, + /* BCD USB version */ + USB_uint_16_low(BCD_USB_VERSION), USB_uint_16_high(BCD_USB_VERSION), + /* Device Class is indicated in the interface descriptors */ + DEVICE_DESC_DEVICE_CLASS, + /* Device Subclass is indicated in the interface descriptors */ + DEVICE_DESC_DEVICE_SUBCLASS, + /* Device Protocol */ + DEVICE_DESC_DEVICE_PROTOCOL, + /* Max Packet size */ + CONTROL_MAX_PACKET_SIZE, + /* Vendor ID */ + 0xa2, 0x15, + /* Product ID */ + 0x00, 0x02, + /* BCD Device version */ + 0x02, 0x00, + /* Manufacturer string index */ + 0x01, + /* Product string index */ + 0x02, + /* Serial number string index */ + 0x03, + /* Number of configurations */ + DEVICE_DESC_NUM_CONFIG_SUPPOTED +}; + +uint8_t g_config_descriptor[CONFIG_DESC_SIZE] = +{ + /* Configuration Descriptor Size - always 9 bytes*/ + CONFIG_ONLY_DESC_SIZE, + /* "Configuration" type of descriptor */ + USB_CONFIG_DESCRIPTOR, + /* Total length of the Configuration descriptor */ + USB_uint_16_low(CONFIG_DESC_SIZE), USB_uint_16_high(CONFIG_DESC_SIZE), + /* NumInterfaces */ + CONFIG_DESC_NUM_INTERFACES_SUPPOTED, + /* Configuration Value */ + 1, + /* Configuration Description String Index */ + 0, + /* Attributes.support RemoteWakeup and self power */ + (USB_DESC_CFG_ATTRIBUTES_D7_POS) | (USBCFG_DEV_SELF_POWER << USB_DESC_CFG_ATTRIBUTES_SELF_POWERED_SHIFT) | (USBCFG_DEV_REMOTE_WAKEUP << USB_DESC_CFG_ATTRIBUTES_REMOTE_WAKEUP_SHIFT), + /* Current draw from bus */ + CONFIG_DESC_CURRENT_DRAWN, + + /* INTERFACE DESCRIPTOR */ + IFACE_ONLY_DESC_SIZE, + USB_IFACE_DESCRIPTOR, + /* bInterfaceNumber */ + 0x00, + /* bAlternateSetting */ + 0x00, + (uint8_t) MSC_DESC_ENDPOINT_COUNT, + /* Interface Class */ + MASS_STORAGE_CLASS, + /* Interface Subclass*/ + SCSI_TRANSPARENT_COMMAND_SET, + /* Interface Protocol*/ + BULK_ONLY_PROTOCOL, + /* Interface Description String Index*/ + 0x00, + + /*Endpoint descriptor */ + ENDP_ONLY_DESC_SIZE, + USB_ENDPOINT_DESCRIPTOR, + BULK_IN_ENDPOINT | (USB_SEND << 7), + USB_BULK_PIPE, + USB_uint_16_low(BULK_IN_ENDP_PACKET_SIZE), + USB_uint_16_high(BULK_IN_ENDP_PACKET_SIZE), + 0x00,/* This value is ignored for Bulk ENDPOINT */ + + /*Endpoint descriptor */ + ENDP_ONLY_DESC_SIZE, + USB_ENDPOINT_DESCRIPTOR, + BULK_OUT_ENDPOINT | (USB_RECV << 7), + USB_BULK_PIPE, + USB_uint_16_low(BULK_OUT_ENDP_PACKET_SIZE), + USB_uint_16_high(BULK_OUT_ENDP_PACKET_SIZE), + 0x00 /* This value is ignored for Bulk ENDPOINT */ +}; + +#if HIGH_SPEED +uint8_t g_device_qualifier_descriptor[DEVICE_QUALIFIER_DESCRIPTOR_SIZE] = +{ + /* Device Qualifier Descriptor Size */ + DEVICE_QUALIFIER_DESCRIPTOR_SIZE, + /* Type of Descriptor */ + USB_DEVQUAL_DESCRIPTOR, + /* BCD USB version */ + USB_uint_16_low(BCD_USB_VERSION), USB_uint_16_high(BCD_USB_VERSION), + /* bDeviceClass */ + DEVICE_DESC_DEVICE_CLASS, + /* bDeviceSubClass */ + DEVICE_DESC_DEVICE_SUBCLASS, + /* bDeviceProtocol */ + DEVICE_DESC_DEVICE_PROTOCOL, + /* bMaxPacketSize0 */ + CONTROL_MAX_PACKET_SIZE, + /* bNumConfigurations */ + DEVICE_OTHER_DESC_NUM_CONFIG_SUPPOTED, + /* Reserved : must be zero */ + 0x00 +}; + +uint8_t g_other_speed_config_descriptor[OTHER_SPEED_CONFIG_DESCRIPTOR_SIZE] = +{ + CONFIG_ONLY_DESC_SIZE, + /* This is a Other speed config descr */ + USB_OTHER_SPEED_DESCRIPTOR, + /* Total length of the Configuration descriptor */ + USB_uint_16_low(CONFIG_DESC_SIZE), USB_uint_16_high(CONFIG_DESC_SIZE), + CONFIG_DESC_NUM_INTERFACES_SUPPOTED, + /*value used to select this configuration : Configuration Value */ + 1, + /* Configuration Description String Index*/ + 0, + /*Attributes.support RemoteWakeup and self power*/ + (USB_DESC_CFG_ATTRIBUTES_D7_POS) | (USBCFG_DEV_SELF_POWER << USB_DESC_CFG_ATTRIBUTES_SELF_POWERED_SHIFT) | (USBCFG_DEV_REMOTE_WAKEUP << USB_DESC_CFG_ATTRIBUTES_REMOTE_WAKEUP_SHIFT), + /* Current draw from bus */ + CONFIG_DESC_CURRENT_DRAWN, + + /* INTERFACE DESCRIPTOR */ + IFACE_ONLY_DESC_SIZE, + USB_IFACE_DESCRIPTOR, + /* bInterfaceNumber */ + 0x00, + /* bAlternateSetting */ + 0x00, + (uint8_t)MSC_DESC_ENDPOINT_COUNT, + /* Interface Class */ + MASS_STORAGE_CLASS, + /* Interface Subclass*/ + SCSI_TRANSPARENT_COMMAND_SET, + /* Interface Protocol*/ + BULK_ONLY_PROTOCOL, + /* Interface Description String Index*/ + 0x00, + + /*Endpoint descriptor */ + ENDP_ONLY_DESC_SIZE, + USB_ENDPOINT_DESCRIPTOR, + BULK_IN_ENDPOINT|(USB_SEND << 7), + USB_BULK_PIPE, + USB_uint_16_low(OTHER_SPEED_BULK_IN_ENDP_PACKET_SIZE), + USB_uint_16_high(OTHER_SPEED_BULK_IN_ENDP_PACKET_SIZE), + 0x00,/* This value is ignored for Bulk ENDPOINT */ + + /*Endpoint descriptor */ + ENDP_ONLY_DESC_SIZE, + USB_ENDPOINT_DESCRIPTOR, + BULK_OUT_ENDPOINT|(USB_RECV << 7), + USB_BULK_PIPE, + USB_uint_16_low(OTHER_SPEED_BULK_OUT_ENDP_PACKET_SIZE), + USB_uint_16_high(OTHER_SPEED_BULK_OUT_ENDP_PACKET_SIZE), + 0x00 /* This value is ignored for Bulk ENDPOINT */ +}; +#endif + +uint8_t USB_STR_0[USB_STR_0_SIZE + USB_STR_DESC_SIZE] = +{ sizeof(USB_STR_0), + USB_STRING_DESCRIPTOR, + 0x09, + 0x04 /*equivalent to 0x0409*/ +}; + +/* Manufacturer string */ +uint8_t USB_STR_1[USB_STR_1_SIZE + USB_STR_DESC_SIZE] = +{ sizeof(USB_STR_1), + USB_STRING_DESCRIPTOR, + 'F', 0, + 'R', 0, + 'E', 0, + 'E', 0, + 'S', 0, + 'C', 0, + 'A', 0, + 'L', 0, + 'E', 0, + ' ', 0, + 'S', 0, + 'E', 0, + 'M', 0, + 'I', 0, + 'C', 0, + 'O', 0, + 'N', 0, + 'D', 0, + 'U', 0, + 'C', 0, + 'T', 0, + 'O', 0, + 'R', 0, + ' ', 0, + 'I', 0, + 'N', 0, + 'C', 0, + '.', 0 +}; + +/* Product string */ +uint8_t USB_STR_2[USB_STR_2_SIZE + USB_STR_DESC_SIZE] = +{ sizeof(USB_STR_2), + USB_STRING_DESCRIPTOR, + 'M', 0, + 'Q', 0, + 'X', 0, + ' ', 0, + 'M', 0, + 'A', 0, + 'S', 0, + 'S', 0, + ' ', 0, + 'S', 0, + 'T', 0, + 'O', 0, + 'R', 0, + 'A', 0, + 'G', 0, + 'E', 0 +}; + +/* Serial number string */ +uint8_t USB_STR_3[USB_STR_3_SIZE + USB_STR_DESC_SIZE] = +{ sizeof(USB_STR_3), + USB_STRING_DESCRIPTOR, + '0', 0, + '1', 0, + '2', 0, + '3', 0, + '4', 0, + '5', 0, + '6', 0, + '7', 0, + '8', 0, + '9', 0, + 'A', 0, + 'B', 0, + 'C', 0, + 'D', 0, + 'E', 0, + 'F', 0 +}; + +uint8_t USB_STR_n[USB_STR_n_SIZE + USB_STR_DESC_SIZE] = +{ sizeof(USB_STR_n), + USB_STRING_DESCRIPTOR, + 'B', 0, + 'A', 0, + 'D', 0, + ' ', 0, + 'S', 0, + 'T', 0, + 'R', 0, + 'I', 0, + 'N', 0, + 'G', 0, + ' ', 0, + 'I', 0, + 'N', 0, + 'D', 0, + 'E', 0, + 'X', 0 +}; + +uint32_t g_std_desc_size[USB_MAX_STD_DESCRIPTORS + 1] = +{ 0, + DEVICE_DESCRIPTOR_SIZE, + CONFIG_DESC_SIZE, + 0, /* string */ + 0, /* Interface */ + 0, /* Endpoint */ +#if HIGH_SPEED + DEVICE_QUALIFIER_DESCRIPTOR_SIZE, + OTHER_SPEED_CONFIG_DESCRIPTOR_SIZE +#else + 0, /* Device Qualifier */ + 0 /* other speed config */ +#endif +}; + +uint8_t *g_std_descriptors[USB_MAX_STD_DESCRIPTORS + 1] = +{ + NULL, + g_device_descriptor, + g_config_descriptor, + NULL, /* string */ + NULL, /* Interface */ + NULL, /* Endpoint */ +#if HIGH_SPEED + g_device_qualifier_descriptor, + g_other_speed_config_descriptor +#else + NULL, /* Device Qualifier */ + NULL /* other speed config*/ +#endif +}; + +uint8_t g_string_desc_size[USB_MAX_STRING_DESCRIPTORS + 1] = +{ sizeof(USB_STR_0), + sizeof(USB_STR_1), + sizeof(USB_STR_2), + sizeof(USB_STR_3), + sizeof(USB_STR_n) +}; + +uint8_t *g_string_descriptors[USB_MAX_STRING_DESCRIPTORS + 1] = +{ USB_STR_0, + USB_STR_1, + USB_STR_2, + USB_STR_3, + USB_STR_n +}; + +usb_language_t usb_language[USB_MAX_SUPPORTED_INTERFACES] = +{ { (uint16_t) 0x0409, g_string_descriptors, g_string_desc_size } }; + +usb_all_languages_t g_languages = +{ USB_STR_0, sizeof(USB_STR_0), USB_MAX_LANGUAGES_SUPPORTED, + usb_language +}; + +uint8_t g_valid_config_values[USB_MAX_CONFIG_SUPPORTED + 1] = { 0, 1 }; + +/**************************************************************************** + * Global Variables + ****************************************************************************/ +static uint8_t g_alternate_interface[USB_MAX_SUPPORTED_INTERFACES]; + +/***************************************************************************** + * Local Types - None + *****************************************************************************/ + +/***************************************************************************** + * Local Functions Prototypes + *****************************************************************************/ + +/***************************************************************************** + * Local Variables + *****************************************************************************/ +#if 0 +DESC_CALLBACK_FUNCTIONS_STRUCT desc_callback = +{ + 0xFF, + USB_Desc_Get_Descriptor, + USB_Desc_Get_Endpoints, + USB_Desc_Get_Interface, + USB_Desc_Set_Interface, + USB_Desc_Valid_Configation, + USB_Desc_Remote_Wakeup, + NULL, + NULL +}; +#endif + +/***************************************************************************** + * Local Functions - None + *****************************************************************************/ + +/***************************************************************************** + * Global Functions + *****************************************************************************/ +/**************************************************************************//*! + * + * @name USB_Desc_Get_Descriptor + * + * @brief The function returns the corresponding descriptor + * + * @param handle: handle + * @param type: type of descriptor requested + * @param str_num: string index for string descriptor + * @param index: string descriptor language Id + * @param descriptor: output descriptor pointer + * @param size: size of descriptor returned + * + * @return USB_OK When Success + * USBERR_INVALID_REQ_TYPE when Error + *****************************************************************************/ +uint8_t USB_Desc_Get_Descriptor +( + uint32_t handle, // this is not of type usb_device_handle because of some bug in CW 7.1 RegABI + uint8_t type, + uint8_t str_num, + uint16_t index, + uint8_t * *descriptor, + uint32_t *size +) +{ + UNUSED_ARGUMENT (handle) + + /* string descriptors are handled separately */ + if (type == USB_STRING_DESCRIPTOR) + { + if (index == 0) + { + /* return the string and size of all languages */ + *descriptor = (uint8_t *) g_languages.languages_supported_string; + *size = g_languages.languages_supported_size; + } + else + { + uint8_t lang_id = 0; + uint8_t lang_index = USB_MAX_LANGUAGES_SUPPORTED; + + for (; lang_id < USB_MAX_LANGUAGES_SUPPORTED; lang_id++) + { + /* check whether we have a string for this language */ + if (index == g_languages.usb_language[lang_id].language_id) + { /* check for max descriptors */ + if (str_num < USB_MAX_STRING_DESCRIPTORS) + { /* setup index for the string to be returned */ + lang_index = str_num; + } + break; + } + } + + /* set return val for descriptor and size */ + *descriptor = (uint8_t *) + g_languages.usb_language[lang_id].lang_desc[lang_index]; + *size = + g_languages.usb_language[lang_id].lang_desc_size[lang_index]; + } + } + else if (type < USB_MAX_STD_DESCRIPTORS + 1) + { + /* set return val for descriptor and size*/ + *descriptor = (uint8_t *) g_std_descriptors[type]; + + /* if there is no descriptor then return error */ + if (*descriptor == NULL) + { + return USBERR_INVALID_REQ_TYPE; + } + *size = g_std_desc_size[type]; + } + else /* invalid descriptor */ + { + return USBERR_INVALID_REQ_TYPE; + } + return USB_OK; +} + +/**************************************************************************//*! + * + * @name USB_Desc_Get_Interface + * + * @brief The function returns the alternate interface + * + * @param handle: handle + * @param interface: interface number + * @param alt_interface: output alternate interface + * + * @return USB_OK When Success + * USBERR_INVALID_REQ_TYPE when Error + *****************************************************************************/ +uint8_t USB_Desc_Get_Interface +( + uint32_t handle, + uint8_t interface, + uint8_t * alt_interface +) +{ + UNUSED_ARGUMENT (handle) + /* if interface valid */ + if (interface < USB_MAX_SUPPORTED_INTERFACES) + { + /* get alternate interface*/ + *alt_interface = g_alternate_interface[interface]; + return USB_OK; + } + + return USBERR_INVALID_REQ_TYPE; +} + +/**************************************************************************//*! + * + * @name USB_Desc_Set_Interface + * + * @brief The function sets the alternate interface + * + * @param handle: handle + * @param interface: interface number + * @param alt_interface: input alternate interface + * + * @return USB_OK When Success + * USBERR_INVALID_REQ_TYPE when Error + *****************************************************************************/ +uint8_t USB_Desc_Set_Interface +( + uint32_t handle, + uint8_t interface, + uint8_t alt_interface +) +{ + UNUSED_ARGUMENT (handle) + /* if interface valid */ + if (interface < USB_MAX_SUPPORTED_INTERFACES) + { + /* set alternate interface*/ + g_alternate_interface[interface] = alt_interface; + return USB_OK; + } + + return USBERR_INVALID_REQ_TYPE; +} + +/**************************************************************************//*! + * + * @name USB_Desc_Valid_Configation + * + * @brief The function checks whether the configuration parameter + * input is valid or not + * + * @param handle handle + * @param config_val configuration value + * + * @return TRUE When Valid + * FALSE When Error + *****************************************************************************/ +bool USB_Desc_Valid_Configation(uint32_t handle, uint16_t config_val) +{ + uint8_t loop_index = 0; + UNUSED_ARGUMENT (handle) + /* check with only supported val right now */ + while (loop_index < (USB_MAX_CONFIG_SUPPORTED + 1)) + { + if (config_val == g_valid_config_values[loop_index]) + { + return TRUE; + } + loop_index++; + } + return FALSE; +} + +/**************************************************************************//*! + * + * @name USB_Desc_Remote_Wakeup + * + * @brief The function checks whether the remote wakeup is supported or not + * + * @param handle: handle + * + * @return REMOTE_WAKEUP_SUPPORT (TRUE) - if remote wakeup supported + *****************************************************************************/ +bool USB_Desc_Remote_Wakeup(uint32_t handle) +{ + UNUSED_ARGUMENT (handle) + return USBCFG_DEV_REMOTE_WAKEUP; +} + +#if 0 +/**************************************************************************//*! + * + * @name USB_Desc_Get_Endpoints + * + * @brief The function returns with the list of all non control endpoints used + * + * @param handle: handle + * + * @return pointer to usb_endpoints_t + *****************************************************************************/ +usb_endpoints_t *USB_Desc_Get_Endpoints(uint32_t handle) +{ + UNUSED_ARGUMENT (handle) + return &usb_desc_ep; +} +#endif + +/**************************************************************************//*! + * + * @name USB_Set_Configuration + * + * @brief The function set the configuration value of device + * + * + * @param handle handle + * @param config_val configuration value + * + * @return TRUE When Valid + * FALSE When Error + *****************************************************************************/ +uint8_t USB_Set_Configuration +( + msd_handle_t handle, uint8_t config + +) +{ + UNUSED_ARGUMENT (handle) + /* TODO */ + /* if a device has two or more configuration, should set the configuration*/ + return USB_OK; +} + +//usb_if_struct_t usb_qq[5] = {1,usb_desc_ep}; + +/**************************************************************************//*! + * + * @name USB_Desc_Get_Entity + * + * @brief The function retrieves the entity specified by type. + * + * @param handle handle + * + * @return USB_OK - if success + *****************************************************************************/ +uint8_t USB_Desc_Get_Entity(msd_handle_t handle, entity_type type, uint32_t * object) +{ + + switch(type) + { + case USB_CLASS_INFO: + usb_if[0].index = 1; + usb_if[0].endpoints = usb_desc_ep; + *object = (unsigned long) &usb_dec_class; + break; + + case USB_MSC_LBA_INFO: + + USB_App_Class_Callback(USB_MSC_DEVICE_GET_INFO, USB_REQ_VAL_INVALID, NULL, (uint32_t *) &usb_msc_lba_info_struct, NULL); + *object = (unsigned long) &usb_msc_lba_info_struct; + break; + + default: + + break; + }/* End Switch */ + return USB_OK; + +} + +/**************************************************************************//*! + * + * @name USB_Desc_Set_Speed + * + * @brief The function is used to set device speed + * + * @param handle: handle + * @param speed: speed + * + * @return USB_OK When Success + * USBERR_INVALID_REQ_TYPE when Error + *****************************************************************************/ +uint8_t USB_Desc_Set_Speed +( + uint32_t handle, + uint16_t speed + ) +{ + descriptor_union_t ptr1, ptr2; + UNUSED_ARGUMENT(handle); + + ptr1.pntr = g_config_descriptor; + ptr2.pntr = g_config_descriptor + CONFIG_DESC_SIZE; + + while (ptr1.word < ptr2.word) + { + if (ptr1.common->bDescriptorType == USB_DESC_TYPE_EP) + { + if (USB_SPEED_HIGH == speed) + { + if (ptr1.ndpt->bEndpointAddress & 0x80) + { + ptr1.ndpt->wMaxPacketSize[0] = USB_uint_16_low(HS_BULK_IN_ENDP_PACKET_SIZE); + ptr1.ndpt->wMaxPacketSize[1] = USB_uint_16_high(HS_BULK_IN_ENDP_PACKET_SIZE); + } + else + { + ptr1.ndpt->wMaxPacketSize[0] = USB_uint_16_low(HS_BULK_OUT_ENDP_PACKET_SIZE); + ptr1.ndpt->wMaxPacketSize[1] = USB_uint_16_high(HS_BULK_OUT_ENDP_PACKET_SIZE); + } + } + else + { + if (ptr1.ndpt->bEndpointAddress & 0x80) + { + ptr1.ndpt->wMaxPacketSize[0] = USB_uint_16_low(FS_BULK_IN_ENDP_PACKET_SIZE); + ptr1.ndpt->wMaxPacketSize[1] = USB_uint_16_high(FS_BULK_IN_ENDP_PACKET_SIZE); + } + else + { + ptr1.ndpt->wMaxPacketSize[0] = USB_uint_16_low(FS_BULK_OUT_ENDP_PACKET_SIZE); + ptr1.ndpt->wMaxPacketSize[1] = USB_uint_16_high(FS_BULK_OUT_ENDP_PACKET_SIZE); + } + } + } + ptr1.word += ptr1.common->bLength; + } + + for (int i = 0; i < MSC_DESC_ENDPOINT_COUNT; i++) + { + if (USB_SPEED_HIGH == speed) + { + if (USB_SEND == ep[i].direction) + { + ep[i].size = HS_BULK_IN_ENDP_PACKET_SIZE; + } + else + { + ep[i].size = HS_BULK_OUT_ENDP_PACKET_SIZE; + } + } + else + { + if (USB_SEND == ep[i].direction) + { + ep[i].size = FS_BULK_IN_ENDP_PACKET_SIZE; + } + else + { + ep[i].size = FS_BULK_OUT_ENDP_PACKET_SIZE; + } + } + } + + return USB_OK; +} + +usb_desc_request_notify_struct_t desc_callback = +{ + USB_Desc_Get_Descriptor, + USB_Desc_Get_Interface, + USB_Desc_Set_Interface, + USB_Set_Configuration, + USB_Desc_Get_Entity +}; +/* EOF */ diff --git a/laser-tag software/usb_descriptor.h b/laser-tag software/usb_descriptor.h new file mode 100644 index 0000000..b3228ce --- /dev/null +++ b/laser-tag software/usb_descriptor.h @@ -0,0 +1,225 @@ +/**HEADER******************************************************************** + * + * Copyright (c) 2008, 2013 Freescale Semiconductor; + * All Rights Reserved + * + * Copyright (c) 1989-2008 ARC International; + * All Rights Reserved + * + *************************************************************************** + * + * THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS 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. + * + ************************************************************************** + * + * $FileName: usb_descriptor.h$ + * $Version : + * $Date : + * + * Comments: + * + * @brief + * + *****************************************************************************/ + +#ifndef _USB_DESCRIPTOR_H +#define _USB_DESCRIPTOR_H 1 + +/****************************************************************************** + * Includes + *****************************************************************************/ +#include "adapter_cfg.h" +#if (OS_ADAPTER_ACTIVE_OS == OS_ADAPTER_BM) +#include "user_config.h" +#endif +#include "usb_class.h" +#include "usb_device_config.h" +#include "usb.h" +#include "usb_class_msc.h" + +/****************************************************************************** + * Constants - None + *****************************************************************************/ + +/****************************************************************************** + * Macro's + *****************************************************************************/ +#define BCD_USB_VERSION (0x0200) + +#define HIGH_SPEED (0) + +#if HIGH_SPEED +#define CONTROLLER_ID USB_CONTROLLER_EHCI_0 +#else +#define CONTROLLER_ID USB_CONTROLLER_KHCI_0 +#endif +//#define BUS_POWERED (0x80) +//#define SELF_POWERED (0x40) +#define HS_BULK_IN_ENDP_PACKET_SIZE (512) +#define HS_BULK_OUT_ENDP_PACKET_SIZE (512) +#define FS_BULK_IN_ENDP_PACKET_SIZE (64) +#define FS_BULK_OUT_ENDP_PACKET_SIZE (64) +/* Implementation Specific Macros */ +#define MSC_DESC_ENDPOINT_COUNT (2) + +#define BULK_IN_ENDPOINT (1) +#if HIGH_SPEED +#define BULK_IN_ENDP_PACKET_SIZE (HS_BULK_IN_ENDP_PACKET_SIZE)/* max supported is 64 for FS and 512 for HS*/ +#else +#define BULK_IN_ENDP_PACKET_SIZE (FS_BULK_IN_ENDP_PACKET_SIZE)/* max supported is 64 for FS and 512 for HS*/ +#endif +#define BULK_OUT_ENDPOINT (2) +/* + * In RedStripe IP DMA Transaction fails + * if Packet Size is configured more than 160. + * As per USB 2.0 spec MAX packet Size has to be configured to 512 bytes. + * Hence compliance fails. + */ +#if HIGH_SPEED +#define BULK_OUT_ENDP_PACKET_SIZE (HS_BULK_OUT_ENDP_PACKET_SIZE)/* max supported is 64 and 512 for HS*/ +#else +#define BULK_OUT_ENDP_PACKET_SIZE (FS_BULK_OUT_ENDP_PACKET_SIZE)/* max supported is 64 and 512 for HS*/ +#endif + +#if (BULK_OUT_ENDP_PACKET_SIZE < 32) +#error "BULK_OUT_ENDP_PACKET_SIZE cannot be less than 32 for MSD" +#endif + +#if (!HIGH_SPEED) + #if((BULK_OUT_ENDP_PACKET_SIZE > 64) || (BULK_IN_ENDP_PACKET_SIZE > 64)) + #error "Bulk Endpoint Packet Size greater than 64 is not allowed for NON-HIGH SPEED DEVICES" + #endif +#else + #if((BULK_OUT_ENDP_PACKET_SIZE > 512) || (BULK_IN_ENDP_PACKET_SIZE > 512)) + #error "Bulk Endpoint Packet Size greater than 512 is not allowed for HIGH SPEED DEVICES" + #endif +#endif + +#if HIGH_SPEED + /* Here Other speed Configuration is for FULL SPEED */ + #define OTHER_SPEED_BULK_IN_ENDP_PACKET_SIZE (64)/* max supported is 64 for FS and 512 for HS*/ + #define OTHER_SPEED_BULK_OUT_ENDP_PACKET_SIZE (64)/* max supported is 64 and 512 for HS*/ +#endif + +/* Various descriptor sizes */ +#define DEVICE_DESCRIPTOR_SIZE (18) +#define CONFIG_ONLY_DESC_SIZE (9) +#define IFACE_ONLY_DESC_SIZE (9) +#define ENDP_ONLY_DESC_SIZE (7) + +#define CONFIG_DESC_SIZE (CONFIG_ONLY_DESC_SIZE + \ + IFACE_ONLY_DESC_SIZE + \ + ENDP_ONLY_DESC_SIZE * MSC_DESC_ENDPOINT_COUNT) + +#if HIGH_SPEED + #define DEVICE_QUALIFIER_DESCRIPTOR_SIZE (10) + #define OTHER_SPEED_CONFIG_DESCRIPTOR_SIZE (CONFIG_DESC_SIZE) +#endif + +/* Max descriptors provided by the Application */ +#define USB_MAX_STD_DESCRIPTORS (7) + +/* Max configuration supported by the Application */ +#define USB_MAX_CONFIG_SUPPORTED (1) + +/* Max string descriptors supported by the Application */ +#define USB_MAX_STRING_DESCRIPTORS (4) + +/* Max language codes supported by the USB */ +#define USB_MAX_LANGUAGES_SUPPORTED (1) + +/* string descriptors sizes */ +#define USB_STR_DESC_SIZE (2) +#define USB_STR_0_SIZE (2) +#define USB_STR_1_SIZE (56) +#define USB_STR_2_SIZE (32) +#define USB_STR_3_SIZE (32) +#define USB_STR_n_SIZE (32) + +/* descriptors codes */ +#define USB_DEVICE_DESCRIPTOR (1) +#define USB_CONFIG_DESCRIPTOR (2) +#define USB_STRING_DESCRIPTOR (3) +#define USB_IFACE_DESCRIPTOR (4) +#define USB_ENDPOINT_DESCRIPTOR (5) +#define USB_DEVQUAL_DESCRIPTOR (6) + +#define USB_MAX_SUPPORTED_INTERFACES (1) + +#if HIGH_SPEED + #define USB_DEVQUAL_DESCRIPTOR (6) + #define USB_OTHER_SPEED_DESCRIPTOR (7) +#endif + +#define DEVICE_DESC_DEVICE_CLASS (0x00) +#define DEVICE_DESC_DEVICE_SUBCLASS (0x00) +#define DEVICE_DESC_DEVICE_PROTOCOL (0x00) +#define DEVICE_DESC_NUM_CONFIG_SUPPOTED (0x01) +/* Keep the following macro Zero if you don't Support Other Speed Configuration + If you support Other Speeds make it 0x01 */ +#define DEVICE_OTHER_DESC_NUM_CONFIG_SUPPOTED (0x00) +#define CONFIG_DESC_NUM_INTERFACES_SUPPOTED (0x01) +#define CONFIG_DESC_CURRENT_DRAWN (0x32) + +/* mass storage specific macros */ +#define MASS_STORAGE_CLASS (0x08) +#define SCSI_TRANSPARENT_COMMAND_SET (0x06) +#define BULK_ONLY_PROTOCOL (0x50) +#define PERIPHERAL_DEVICE_TYPE (0x00) +/* Direct Access Block Device : Magnetic and Flash Drives*/ +#define PERIPHERAL_QUALIFIER_SHIFT (5) +#define PERIPHERAL_QUALIFIER (0) +#define SPC_VERSION (4)/*SPC3 is 5; SPC2 is 4*/ +#define REMOVABLE_MEDIUM_BIT (1) +#define REMOVABLE_MEDIUM_BIT_SHIFT (7) +#define ADDITIONAL_LENGTH (0x20) +/*set to 0x20H if returning 36 bytes of inquiry data*/ +/****************************************************************************** + * Types + *****************************************************************************/ +/****************************************************************************** + * Global Functions + *****************************************************************************/ +extern uint8_t USB_Desc_Get_Descriptor( + uint32_t handle, + uint8_t type, + uint8_t str_num, + uint16_t index, + uint8_t * *descriptor, + uint32_t *size); + +extern uint8_t USB_Desc_Get_Interface( + uint32_t handle, + uint8_t interface, + uint8_t * alt_interface); + +extern uint8_t USB_Desc_Set_Interface( + uint32_t handle, + uint8_t interface, + uint8_t alt_interface); + +extern bool USB_Desc_Valid_Configation(uint32_t handle, + uint16_t config_val); + +extern bool USB_Desc_Remote_Wakeup(uint32_t handle); + +extern usb_endpoints_t *USB_Desc_Get_Endpoints(uint32_t handle); + +uint8_t USB_Desc_Set_Speed +( + uint32_t handle, + uint16_t speed + ); +#endif + +/* EOF */ -- cgit v1.2.3-18-g5258