[戻る]
一括表示

USBの接続 投稿者:miya 投稿日:2016/09/28(Wed) 20:39:24 No.701

初心者です。
回答いただけますと助かります。

PIC24FJ64GC006を使用してUSB接続をしたいのですが上手くいきません。

USBDeviceInit()とUSBDeviceAttach()を実行し、
PCのデバイスマネージャーで不明なデバイスとして認識するところまでも行っておりません。

動作確認のため2ヶのLEDを点灯と点滅させました。
USBDeviceAttach()の行を削除すると、LEDは点灯と点滅をしますが。
USBDeviceAttach()の行があると、2ヶのLEDは、消灯したままです。

USBDeviceAttach()を削除せずに、DEBUG MODEを実行すると、
Target Haltedのメッセージがあり、DebugConsoleには、
下記が表示されています。
------------------
__DefaultInterrupt ( ) @ Compiler generated location : 0x2e0
User program running
User program stopped
User program stopped
------------------

プログラムは下記で試しました。
-----------------------
#include <xc.h>
#include <p24FJ64GC006.h>
#include "USB/usb.h"
#include "USB/usb_device.h"
#include "USB/usb_device_generic.h"
#include "usb_config.h"

// CONFIG4
#pragma config DSWDTPS = DSWDTPS1F // Deep Sleep Watchdog Timer Postscale Select bits (1:68719476736 (25.7 Days))
#pragma config DSWDTOSC = LPRC // DSWDT Reference Clock Select (DSWDT uses LPRC as reference clock)
#pragma config DSBOREN = OFF // Deep Sleep BOR Enable bit (DSBOR Disabled)
#pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer Enable (DSWDT Disabled)
#pragma config DSSWEN = OFF // DSEN Bit Enable (Deep Sleep operation is always disabled)
#pragma config RTCBAT = OFF // RTC Battery Operation Enable (RTC operation is discontinued in VBAT)
#pragma config PLLDIV = DIV2 // PLL Input Prescaler Select bits (Oscillator divided by 2 (8 MHz input))
#pragma config I2C2SEL = PRI // Alternate I2C2 Location Select bit (I2C2 is multiplexed to SDA2/RA3 and SCL2/RA2 )
#pragma config IOL1WAY = OFF // PPS IOLOCK Set Only Once Enable bit (The IOLOCK bit can be set and cleared using the unlock sequence)
// CONFIG3
#pragma config WPFP = WPFP127 // Write Protection Flash Page Segment Boundary (Page 127 (0x1FC00))
#pragma config SOSCSEL = OFF // SOSC Selection bits (Digital (SCLKI) mode)
#pragma config WDTWIN = PS25_0 // Window Mode Watchdog Timer Window Width Select (Watch Dog Timer Window Width is 25 percent)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset Disabled)
#pragma config WPDIS = WPDIS // Segment Write Protection Disable (Disabled)
#pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Disabled)
#pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory)
// CONFIG2
#pragma config POSCMD = EC // Primary Oscillator Select (External-Clock Mode Enabled)
#pragma config WDTCLK = LPRC // WDT Clock Source Select bits (WDT uses LPRC)
#pragma config OSCIOFCN = OFF // OSCO Pin Configuration (OSCO/CLKO/RC15 functions as CLKO (FOSC/2))
#pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor Configuration bits (Clock switching and Fail-Safe Clock Monitor are disabled)
#pragma config FNOSC = PRIPLL // Initial Oscillator Select (Primary Oscillator with PLL module (XTPLL,HSPLL, ECPLL))
#pragma config ALTADREF = AVREF_RB // External 12-Bit A/D Reference Location Select bit (AVREF+/AVREF- are mapped to RB0/RB1)
#pragma config ALTCVREF = CVREF_RB // External Comparator Reference Location Select bit (CVREF+/CVREF- are mapped to RB0/RB1)
#pragma config WDTCMX = WDTCLK // WDT Clock Source Select bits (WDT clock source is determined by the WDTCLK Configuration bits)
#pragma config IESO = OFF // Internal External Switchover (Disabled)
// CONFIG1
#pragma config WDTPS = PS32768 // Watchdog Timer Postscaler Select (1:32,768)
#pragma config FWPSA = PR128 // WDT Prescaler Ratio Select (1:128)
#pragma config WINDIS = OFF // Windowed WDT Disable (Standard Watchdog Timer)
#pragma config FWDTEN = WDT_DIS // Watchdog Timer Enable (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config ICS = PGx2 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC2/PGED2)
#pragma config LPCFG = ON // Low power regulator control (Low voltage regulator controlled in sw by RETEN bit)
#pragma config GWRP = OFF // General Segment Write Protect (Disabled)
#pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF // JTAG Port Enable (Disabled)

// USB関連バッファ ------------------------------------------------------
#pragma udata
unsigned char INPacket[64]; // USB送信バッファ
unsigned char OUTPacket[64]; // USB受信バッファ
USB_HANDLE USBGenericOutHandle;
USB_HANDLE USBGenericInHandle;

void Delayms( unsigned int t);
void USBCBSuspend(void);
void USBCBWakeFromSuspend(void);
void USBCB_SOF_Handler(void);
void USBCBErrorHandler(void);
void USBCBCheckOtherReq(void);
void USBCBStdSetDscHandler(void);
void USBCBInitEP(void);
void USBCBSendResume(void);
bool USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, uint16_t size);

#pragma code
int main(void)
{
// クロック設定 8MHz→96MHz/3=32MHz
CLKDIVbits.RCDIV = 0; // FRC Postscaler Select bits = 8MHz
CLKDIVbits.CPDIV = 0; // USB System Clock Select bits = 32MHz
CLKDIVbits.PLLEN = 1; // 96 MHz PLL Enable bit
// 入出力ポート設定 -------------------------------------------------
TRISB = 0x00FF;
TRISC = 0x1000;
TRISD = 0x0000;
TRISE = 0x0000; // Output=LED1,LED2
TRISF = 0x0000;
TRISG = 0x020C; // Input =RG2(D+),RG3(D-)

// USB初期化とAPスタート --------------------------------------------
USBDeviceInit(); // USB初期化
USBGenericInHandle = 0;
USBGenericOutHandle = 0;
// USBDeviceAttach(); // USBアタッチ許可と割り込み許可

LATEbits.LATE5 = 1; // LED点灯

while(1){
Delayms(1000);
LATEbits.LATE6 ^= 1; // LED点滅
// USB接続中なら送受信実行 --------------------------------------
if((USBDeviceState >= CONFIGURED_STATE)&&(USBSuspendControl!=1)){
}
}
}

//-----------------------------------------------------------------------
// タイマ4による1msec単位の遅延関数 Tcy=16MHz
//-----------------------------------------------------------------------
void Delayms( unsigned int t){
IFS1bits.T4IF = 0; // 割り込みフラグクリア
PR4 = 2000; // 1msec設定
T4CON = 0x8010; // タイマ4スタート プリスケーラ 1/8
while (t--) { // 繰り返し回数
while (!IFS1bits.T4IF); // 1msec待ち
IFS1bits.T4IF = 0; // 割り込みフラグクリア
}
}

void USBCBSuspend(void)
{
}
void USBCBWakeFromSuspend(void)
{
}
void USBCB_SOF_Handler(void)
{
}
void USBCBErrorHandler(void)
{
}
void USBCBCheckOtherReq(void)
{
}
void USBCBStdSetDscHandler(void)
{
}
void USBCBInitEP(void)
{
USBEnableEndpoint(USBGEN_EP_NUM,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(uint8_t*)&OUTPacket,USBGEN_EP_SIZE);
}
void USBCBSendResume(void)
{
static uint16_t delay_count;

if(USBGetRemoteWakeupStatus() == true){
// Verify that the USB bus is in fact suspended, before we send remote wakeup signalling.
if(USBIsBusSuspended() == true){
USBMaskInterrupts();
// Clock switch to settings consistent with normal USB operation.
USBCBWakeFromSuspend();
USBSuspendControl = 0;
USBBusIsSuspended = false; // So we don't execute this code again,
delay_count = 3600U;
do{
delay_count--;
}while(delay_count);
// Now drive the resume K-state signalling onto the USB bus.
USBResumeControl = 1; // Start RESUME signaling
delay_count = 1800U; // Set RESUME line for 1-13 ms
do{
delay_count--;
}while(delay_count);
USBResumeControl = 0; // Finished driving resume signalling
USBUnmaskInterrupts();
}
}
}

bool USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, uint16_t size)
{
switch( (int)event ){
case EVENT_TRANSFER:
break;
case EVENT_SOF:
USBCB_SOF_Handler();
break;
case EVENT_SUSPEND:
USBCBSuspend();
break;
case EVENT_RESUME:
USBCBWakeFromSuspend();
break;
case EVENT_CONFIGURED:
USBCBInitEP();
break;
case EVENT_SET_DESCRIPTOR:
USBCBStdSetDscHandler();
break;
case EVENT_EP0_REQUEST:
USBCBCheckOtherReq();
break;
case EVENT_BUS_ERROR:
USBCBErrorHandler();
break;
case EVENT_TRANSFER_TERMINATED:
break;
default:
break;
}
return true;
}
-----------------------------------------

Re: USBの接続 投稿者:Gokan 投稿日:2016/09/28(Wed) 21:15:15 No.702

ちょっとこれだけの情報では何とも言えませんね。
まずはマイクロチップから提供されているデモプログラムを
そのまま動かすところから始めるのが良いかと思います。


> 初心者です。
> 回答いただけますと助かります。
>
> PIC24FJ64GC006を使用してUSB接続をしたいのですが上手くいきません。
>
> USBDeviceInit()とUSBDeviceAttach()を実行し、
> PCのデバイスマネージャーで不明なデバイスとして認識するところまでも行っておりません。
>
> 動作確認のため2ヶのLEDを点灯と点滅させました。
> USBDeviceAttach()の行を削除すると、LEDは点灯と点滅をしますが。
> USBDeviceAttach()の行があると、2ヶのLEDは、消灯したままです。
>
> USBDeviceAttach()を削除せずに、DEBUG MODEを実行すると、
> Target Haltedのメッセージがあり、DebugConsoleには、
> 下記が表示されています。
> ------------------
> __DefaultInterrupt ( ) @ Compiler generated location : 0x2e0
> User program running
> User program stopped
> User program stopped
> ------------------
>
> プログラムは下記で試しました。
> -----------------------
> #include <xc.h>
> #include <p24FJ64GC006.h>
> #include "USB/usb.h"
> #include "USB/usb_device.h"
> #include "USB/usb_device_generic.h"
> #include "usb_config.h"
>
> // CONFIG4
> #pragma config DSWDTPS = DSWDTPS1F // Deep Sleep Watchdog Timer Postscale Select bits (1:68719476736 (25.7 Days))
> #pragma config DSWDTOSC = LPRC // DSWDT Reference Clock Select (DSWDT uses LPRC as reference clock)
> #pragma config DSBOREN = OFF // Deep Sleep BOR Enable bit (DSBOR Disabled)
> #pragma config DSWDTEN = OFF // Deep Sleep Watchdog Timer Enable (DSWDT Disabled)
> #pragma config DSSWEN = OFF // DSEN Bit Enable (Deep Sleep operation is always disabled)
> #pragma config RTCBAT = OFF // RTC Battery Operation Enable (RTC operation is discontinued in VBAT)
> #pragma config PLLDIV = DIV2 // PLL Input Prescaler Select bits (Oscillator divided by 2 (8 MHz input))
> #pragma config I2C2SEL = PRI // Alternate I2C2 Location Select bit (I2C2 is multiplexed to SDA2/RA3 and SCL2/RA2 )
> #pragma config IOL1WAY = OFF // PPS IOLOCK Set Only Once Enable bit (The IOLOCK bit can be set and cleared using the unlock sequence)
> // CONFIG3
> #pragma config WPFP = WPFP127 // Write Protection Flash Page Segment Boundary (Page 127 (0x1FC00))
> #pragma config SOSCSEL = OFF // SOSC Selection bits (Digital (SCLKI) mode)
> #pragma config WDTWIN = PS25_0 // Window Mode Watchdog Timer Window Width Select (Watch Dog Timer Window Width is 25 percent)
> #pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset Disabled)
> #pragma config WPDIS = WPDIS // Segment Write Protection Disable (Disabled)
> #pragma config WPCFG = WPCFGDIS // Write Protect Configuration Page Select (Disabled)
> #pragma config WPEND = WPENDMEM // Segment Write Protection End Page Select (Write Protect from WPFP to the last page of memory)
> // CONFIG2
> #pragma config POSCMD = EC // Primary Oscillator Select (External-Clock Mode Enabled)
> #pragma config WDTCLK = LPRC // WDT Clock Source Select bits (WDT uses LPRC)
> #pragma config OSCIOFCN = OFF // OSCO Pin Configuration (OSCO/CLKO/RC15 functions as CLKO (FOSC/2))
> #pragma config FCKSM = CSDCMD // Clock Switching and Fail-Safe Clock Monitor Configuration bits (Clock switching and Fail-Safe Clock Monitor are disabled)
> #pragma config FNOSC = PRIPLL // Initial Oscillator Select (Primary Oscillator with PLL module (XTPLL,HSPLL, ECPLL))
> #pragma config ALTADREF = AVREF_RB // External 12-Bit A/D Reference Location Select bit (AVREF+/AVREF- are mapped to RB0/RB1)
> #pragma config ALTCVREF = CVREF_RB // External Comparator Reference Location Select bit (CVREF+/CVREF- are mapped to RB0/RB1)
> #pragma config WDTCMX = WDTCLK // WDT Clock Source Select bits (WDT clock source is determined by the WDTCLK Configuration bits)
> #pragma config IESO = OFF // Internal External Switchover (Disabled)
> // CONFIG1
> #pragma config WDTPS = PS32768 // Watchdog Timer Postscaler Select (1:32,768)
> #pragma config FWPSA = PR128 // WDT Prescaler Ratio Select (1:128)
> #pragma config WINDIS = OFF // Windowed WDT Disable (Standard Watchdog Timer)
> #pragma config FWDTEN = WDT_DIS // Watchdog Timer Enable (WDT disabled in hardware; SWDTEN bit disabled)
> #pragma config ICS = PGx2 // Emulator Pin Placement Select bits (Emulator functions are shared with PGEC2/PGED2)
> #pragma config LPCFG = ON // Low power regulator control (Low voltage regulator controlled in sw by RETEN bit)
> #pragma config GWRP = OFF // General Segment Write Protect (Disabled)
> #pragma config GCP = OFF // General Segment Code Protect (Code protection is disabled)
> #pragma config JTAGEN = OFF // JTAG Port Enable (Disabled)
>
> // USB関連バッファ ------------------------------------------------------
> #pragma udata
> unsigned char INPacket[64]; // USB送信バッファ
> unsigned char OUTPacket[64]; // USB受信バッファ
> USB_HANDLE USBGenericOutHandle;
> USB_HANDLE USBGenericInHandle;
>
> void Delayms( unsigned int t);
> void USBCBSuspend(void);
> void USBCBWakeFromSuspend(void);
> void USBCB_SOF_Handler(void);
> void USBCBErrorHandler(void);
> void USBCBCheckOtherReq(void);
> void USBCBStdSetDscHandler(void);
> void USBCBInitEP(void);
> void USBCBSendResume(void);
> bool USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, uint16_t size);
>
> #pragma code
> int main(void)
> {
> // クロック設定 8MHz→96MHz/3=32MHz
> CLKDIVbits.RCDIV = 0; // FRC Postscaler Select bits = 8MHz
> CLKDIVbits.CPDIV = 0; // USB System Clock Select bits = 32MHz
> CLKDIVbits.PLLEN = 1; // 96 MHz PLL Enable bit
> // 入出力ポート設定 -------------------------------------------------
> TRISB = 0x00FF;
> TRISC = 0x1000;
> TRISD = 0x0000;
> TRISE = 0x0000; // Output=LED1,LED2
> TRISF = 0x0000;
> TRISG = 0x020C; // Input =RG2(D+),RG3(D-)
>
> // USB初期化とAPスタート --------------------------------------------
> USBDeviceInit(); // USB初期化
> USBGenericInHandle = 0;
> USBGenericOutHandle = 0;
> // USBDeviceAttach(); // USBアタッチ許可と割り込み許可
>
> LATEbits.LATE5 = 1; // LED点灯
>
> while(1){
> Delayms(1000);
> LATEbits.LATE6 ^= 1; // LED点滅
> // USB接続中なら送受信実行 --------------------------------------
> if((USBDeviceState >= CONFIGURED_STATE)&&(USBSuspendControl!=1)){
> }
> }
> }
>
> //-----------------------------------------------------------------------
> // タイマ4による1msec単位の遅延関数 Tcy=16MHz
> //-----------------------------------------------------------------------
> void Delayms( unsigned int t){
> IFS1bits.T4IF = 0; // 割り込みフラグクリア
> PR4 = 2000; // 1msec設定
> T4CON = 0x8010; // タイマ4スタート プリスケーラ 1/8
> while (t--) { // 繰り返し回数
> while (!IFS1bits.T4IF); // 1msec待ち
> IFS1bits.T4IF = 0; // 割り込みフラグクリア
> }
> }
>
> void USBCBSuspend(void)
> {
> }
> void USBCBWakeFromSuspend(void)
> {
> }
> void USBCB_SOF_Handler(void)
> {
> }
> void USBCBErrorHandler(void)
> {
> }
> void USBCBCheckOtherReq(void)
> {
> }
> void USBCBStdSetDscHandler(void)
> {
> }
> void USBCBInitEP(void)
> {
> USBEnableEndpoint(USBGEN_EP_NUM,USB_OUT_ENABLED|USB_IN_ENABLED|USB_HANDSHAKE_ENABLED|USB_DISALLOW_SETUP);
> USBGenericOutHandle = USBGenRead(USBGEN_EP_NUM,(uint8_t*)&OUTPacket,USBGEN_EP_SIZE);
> }
> void USBCBSendResume(void)
> {
> static uint16_t delay_count;
>
> if(USBGetRemoteWakeupStatus() == true){
> // Verify that the USB bus is in fact suspended, before we send remote wakeup signalling.
> if(USBIsBusSuspended() == true){
> USBMaskInterrupts();
> // Clock switch to settings consistent with normal USB operation.
> USBCBWakeFromSuspend();
> USBSuspendControl = 0;
> USBBusIsSuspended = false; // So we don't execute this code again,
> delay_count = 3600U;
> do{
> delay_count--;
> }while(delay_count);
> // Now drive the resume K-state signalling onto the USB bus.
> USBResumeControl = 1; // Start RESUME signaling
> delay_count = 1800U; // Set RESUME line for 1-13 ms
> do{
> delay_count--;
> }while(delay_count);
> USBResumeControl = 0; // Finished driving resume signalling
> USBUnmaskInterrupts();
> }
> }
> }
>
> bool USER_USB_CALLBACK_EVENT_HANDLER(USB_EVENT event, void *pdata, uint16_t size)
> {
> switch( (int)event ){
> case EVENT_TRANSFER:
> break;
> case EVENT_SOF:
> USBCB_SOF_Handler();
> break;
> case EVENT_SUSPEND:
> USBCBSuspend();
> break;
> case EVENT_RESUME:
> USBCBWakeFromSuspend();
> break;
> case EVENT_CONFIGURED:
> USBCBInitEP();
> break;
> case EVENT_SET_DESCRIPTOR:
> USBCBStdSetDscHandler();
> break;
> case EVENT_EP0_REQUEST:
> USBCBCheckOtherReq();
> break;
> case EVENT_BUS_ERROR:
> USBCBErrorHandler();
> break;
> case EVENT_TRANSFER_TERMINATED:
> break;
> default:
> break;
> }
> return true;
> }
> -----------------------------------------

- WebForum -