SDG+USBHost(Mouse) Sample

Dependencies:   Sound_Generator USBHost_custom

Fork of SDG_Mouse_Sample by GR-PEACH_producer_meeting

Information

Japanese version is available in lower part of this page.
このページの後半に日本語版が用意されています.

What is this?

This program is a demonstration that sounds the sound by mouse operation by using USBHost(Mouse) and Sound Generator.

Settings

Close JP3 of GR-PEACH.
/media/uploads/RyoheiHagimoto/sdg-mouse.jpg

Operation

operationeffect
Right clickSounds
Left clickReset to base tone (C)
Moves the mouse to the rightLower the sound
Moves the mouse to the leftHigher the sound
Center cursorAdjust the sensitivity.
Reset the reference value in the click.

Others

The default setting of serial communication (baud rate etc.) in mbed is shown the following link.
Please refer to the link and change the settings of your PC terminal software.
The default value of baud rate in mbed is 9600, and this application uses baud rate 9600.
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication


概要

このプログラムは、USBHost(Mouse) + Sound Generatorで、マウス操作による擬似笛デモです。

設定

GR-PEACHのJP3をショートする必要があります。
/media/uploads/RyoheiHagimoto/sdg-mouse.jpg

操作方法

操作内容
右クリック音出力開始
左クリック基準音(ド)にリセット
マウス右移動高音になります
マウス左移動低音になります
センターカーソル音高低の変化量調整(クリックで基準値にリセット)

Others

mbedのシリアル通信(ボーレート等)のデフォルト設定は以下のリンクに示しています。
リンクを参考に、お使いのPCターミナルソフトの設定を変更して下さい。
mbedでのボーレートのデフォルト値は9600で、このサンプルではボーレート9600を使います。
https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication

USBHost/USBEndpoint.cpp

Committer:
samux
Date:
2013-03-14
Revision:
8:93da8ea2708b
Parent:
4:b320d68e98e7
Child:
13:b58a2204422f

File content as of revision 8:93da8ea2708b:

/* mbed USBHost Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "dbg.h"
#include "USBEndpoint.h"

void USBEndpoint::init(HCED * hced_, ENDPOINT_TYPE type_, ENDPOINT_DIRECTION dir_, uint32_t size, uint8_t ep_number, HCTD* td_list_[2])
{
    hced = hced_;
    type = type_;
    dir = dir_;
    setup = (type == CONTROL_ENDPOINT) ? true : false;

    //TDs have been allocated by the host
    memcpy((HCTD**)td_list, td_list_, sizeof(HCTD*)*2); //TODO: Maybe should add a param for td_list size... at least a define
    memcpy(td_list_[0], 0, sizeof(HCTD));
    memcpy(td_list_[1], 0, sizeof(HCTD));
    
    td_list[0]->ep = this;
    td_list[1]->ep = this;

    hced->control = 0;
    //Empty queue
    hced->tailTD = td_list[0];
    hced->headTD = td_list[0];
    hced->nextED = 0;

    address = (ep_number & 0x7F) | ((dir - 1) << 7);

    hced->control = ((ep_number & 0x7F) << 7)                         // Endpoint address
                    | (type != CONTROL_ENDPOINT ? ( dir << 11) : 0 )  // direction : Out = 1, 2 = In
                    | ((size & 0x3ff) << 16);                         // MaxPkt Size

    transfer_len = 0;
    transferred = 0;
    buf_start = 0;
    nextEp = NULL;

    td_current = td_list[0];
    td_next = td_list[1];
    
    intf_nb = 0;

    state = USB_TYPE_IDLE;
}

void USBEndpoint::setSize(uint32_t size)
{
    hced->control &= ~(0x3ff << 16);
    hced->control |= (size << 16);
}


void USBEndpoint::setDeviceAddress(uint8_t addr)
{
    hced->control &= ~(0x7f);
    hced->control |= (addr & 0x7F);
}

void USBEndpoint::setSpeed(uint8_t speed)
{
    hced->control &= ~(1 << 13);
    hced->control |= (speed << 13);
}

//Only for control Eps
void USBEndpoint::setNextToken(uint32_t token)
{
    switch (token) {
        case TD_SETUP:
            dir = OUT;
            setup = true;
            break;
        case TD_IN:
            dir = IN;
            setup = false;
            break;
        case TD_OUT:
            dir = OUT;
            setup = false;
            break;
    }
}

struct {
    USB_TYPE type;
    const char * str;
} static type_string[] = {
/*0*/   {USB_TYPE_OK, "USB_TYPE_OK"},
        {USB_TYPE_CRC_ERROR, "USB_TYPE_CRC_ERROR"},
        {USB_TYPE_BIT_STUFFING_ERROR, "USB_TYPE_BIT_STUFFING_ERROR"},
        {USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR, "USB_TYPE_DATA_TOGGLE_MISMATCH_ERROR"},
        {USB_TYPE_STALL_ERROR, "USB_TYPE_STALL_ERROR"},
/*5*/   {USB_TYPE_DEVICE_NOT_RESPONDING_ERROR, "USB_TYPE_DEVICE_NOT_RESPONDING_ERROR"},
        {USB_TYPE_PID_CHECK_FAILURE_ERROR, "USB_TYPE_PID_CHECK_FAILURE_ERROR"},
        {USB_TYPE_UNEXPECTED_PID_ERROR, "USB_TYPE_UNEXPECTED_PID_ERROR"},
        {USB_TYPE_DATA_OVERRUN_ERROR, "USB_TYPE_DATA_OVERRUN_ERROR"},
        {USB_TYPE_DATA_UNDERRUN_ERROR, "USB_TYPE_DATA_UNDERRUN_ERROR"},
/*10*/  {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
        {USB_TYPE_ERROR, "USB_TYPE_ERROR"},
        {USB_TYPE_BUFFER_OVERRUN_ERROR, "USB_TYPE_BUFFER_OVERRUN_ERROR"},
        {USB_TYPE_BUFFER_UNDERRUN_ERROR, "USB_TYPE_BUFFER_UNDERRUN_ERROR"},
        {USB_TYPE_DISCONNECTED, "USB_TYPE_DISCONNECTED"},
/*15*/  {USB_TYPE_FREE, "USB_TYPE_FREE"},
        {USB_TYPE_IDLE, "USB_TYPE_IDLE"},
        {USB_TYPE_PROCESSING, "USB_TYPE_PROCESSING"},
        {USB_TYPE_ERROR, "USB_TYPE_ERROR"}
};

void USBEndpoint::setState(uint8_t st) {
    if (st > 18)
        return;
    state = type_string[st].type;
}


const char * USBEndpoint::getStateString() {
    return type_string[state].str;
}

void USBEndpoint::queueTransfer()
{
    transfer_len = (uint32_t)td_current->bufEnd - (uint32_t)td_current->currBufPtr + 1;
    transferred = transfer_len;
    buf_start = (uint8_t *)td_current->currBufPtr;

    //Now add this free TD at this end of the queue
    state = USB_TYPE_PROCESSING;
    td_current->nextTD = td_next;
    hced->tailTD = td_next;
}

void USBEndpoint::unqueueTransfer(volatile HCTD * td)
{
    td->control=0;
    td->currBufPtr=0;
    td->bufEnd=0;
    td->nextTD=0;
    hced->headTD = (HCTD *)((uint32_t)hced->tailTD | ((uint32_t)hced->headTD & 0x2)); //Carry bit
    td_current = td_next;
    td_next = td;
}

void USBEndpoint::queueEndpoint(USBEndpoint * ed)
{
    nextEp = ed;
    hced->nextED = (ed == NULL) ? 0 : ed->getHCED();
}