关于最近遇到的问题
用的黑金家的开发板,USB芯片是CY7C68013A,目前要实现的功能是,将FPGA产生的随机数通过USB传输到上位机。
于是我将EP2设置成IN,使用同步fifo将数据通过EP2传输至PC机。
目前遇到的问题是,signal-tap可以看见数据的变化,但是上位机无法接受到数据。
图一,EZ-USB中的pipe显示是EP2-OUT,可是我设置的是IN啊,所以不知道如何才能更改,所以transfer也一直失败,我尝试改成过使用EP6传输,依然是失败,所以不知道如何解决了。文末附上fifo_wr.v和bulkloop.c的代码

1 //----------------------------------------------------------------------------- 2 // File: bulkloop.c 3 // Contents: Hooks required to implement USB peripheral function. 4 // 5 // $Archive: /USB/Examples/FX2LP/bulkloop/bulkloop.c $ 6 // 7 // 8 //----------------------------------------------------------------------------- 9 // Copyright (c) 2011, Cypress Semiconductor Corporation All rights reserved 10 //----------------------------------------------------------------------------- 11 #pragma NOIV // Do not generate interrupt vectors 12 13 #include "..\inc\fx2.h" 14 #include "..\inc\fx2regs.h" 15 #include "..\inc\syncdly.h" // SYNCDELAY macro 16 17 extern BOOL GotSUD; // Received setup data flag 18 extern BOOL Sleep; 19 extern BOOL Rwuen; 20 extern BOOL Selfpwr; 21 22 BYTE Configuration; // Current configuration 23 BYTE AlternateSetting; // Alternate settings 24 25 #define VR_NAKALL_ON 0xD0 26 #define VR_NAKALL_OFF 0xD1 27 28 //----------------------------------------------------------------------------- 29 // Task Dispatcher hooks 30 // The following hooks are called by the task dispatcher. 31 //----------------------------------------------------------------------------- 32 33 void TD_Init(void) // Called once at startup 34 { 35 // set the CPU clock to 48MHz, Default 12MHz(Page 333) 36 //CPUCS = 0x02; //12MHZ CLKOUT ENALBE 37 //CPUCS = 0x0a; //24MHZ CLKOUT ENALBE 38 CPUCS = 0x12; //48MHZ CLKOUT ENALBE,时钟不反向,CLKOUT PIN驱动,有时钟输出; 39 SYNCDELAY; 40 41 //Interface Configure(Page 334) 42 IFCONFIG =0x03; //选择为外部时钟,且时钟频率为30MHz,且为同步slaveFIFO模式,输入IFCLK(5~48MHz)(0000_0011) 43 //IFCONFIG =0x0B;//选择为外部时钟,且为异步slaveFIFO模式,不需要IFCLK 44 SYNCDELAY; 45 46 //Configure REVCTL for Chip Revision Control(Page 344) 47 REVCTL = 0x03; //Cypress highly recommends setting both bits to 1 48 SYNCDELAY; 49 50 Rwuen = TRUE; // Enable remote-wakeup 51 52 53 // Registers which require a synchronization delay, see section 15.14 54 // FIFORESET FIFOPINPOLAR 55 // INPKTEND OUTPKTEND 56 // EPxBCH:L REVCTL 57 // GPIFTCB3 GPIFTCB2 58 // GPIFTCB1 GPIFTCB0 59 // EPxFIFOPFH:L EPxAUTOINLENH:L 60 // EPxFIFOCFG EPxGPIFFLGSEL 61 // PINFLAGSxx EPxFIFOIRQ 62 // EPxFIFOIE GPIFIRQ 63 // GPIFIE GPIFADRH:L 64 // UDMACRCH:L EPxGPIFTRIG 65 // GPIFTRIG 66 67 // Note: The pre-REVE EPxGPIFTCH/L register are affected, as well... 68 // ...these have been replaced by GPIFTC[B3:B0] registers 69 70 // default: all endpoints have their VALID bit set 71 // default: TYPE1 = 1 and TYPE0 = 0 --> BULK 72 // default: EP2 and EP4 DIR bits are 0 (OUT direction) 73 // default: EP6 and EP8 DIR bits are 1 (IN direction) 74 // default: EP2, EP4, EP6, and EP8 are double buffered 75 76 77 78 79 // we are just using the default values, yes this is not necessary... 80 EP1OUTCFG = 0xA0; 81 EP1INCFG = 0xA0; 82 SYNCDELAY; // see TRM section 15.14 83 EP2CFG = 0xE0; 84 SYNCDELAY; 85 EP4CFG = 0x60; // disabled... 86 SYNCDELAY; 87 EP6CFG = 0x60; // disabled... 88 SYNCDELAY; 89 EP8CFG = 0x60; // disabled... 90 91 92 93 //=========================大大的分割线=================================// 94 //复位FIFO,先复位端点,再清空端点(Page 340) 95 SYNCDELAY; 96 FIFORESET = 0x80;// activate NAK-ALL to avoid race conditions 97 SYNCDELAY; 98 FIFORESET = 0x02;// reset, FIFO 2 99 SYNCDELAY; 100 FIFORESET = 0x04;// reset, FIFO 4 101 SYNCDELAY; 102 FIFORESET = 0x06;// reset, FIFO 6 103 SYNCDELAY; 104 FIFORESET = 0x08;// reset, FIFO 8 105 SYNCDELAY; 106 FIFORESET = 0x00;// deactivate NAK-AL 107 SYNCDELAY; 108 109 110 //Configure the EPxFIFOCFG(Page 349) 111 EP2FIFOCFG = 0x08; // autoin, 8 Bit Wide,0000_1000 自动使能IN 112 // EP2FIFOCFG = 0x09; // autoin, 16 Bit Wide 113 SYNCDELAY; 114 EP4FIFOCFG = 0x00; // no-autoOUT, bytewide 115 SYNCDELAY; 116 EP6FIFOCFG = 0x00; // no-autoOUT, bytewide 117 SYNCDELAY; 118 EP8FIFOCFG = 0x00; // no-autoOUT, bytewide 119 SYNCDELAY; 120 121 //-------------------------------------------------------- 122 123 124 //Configure PIN Polarity 125 PORTACFG = 0x40; //IFCOG[1:0] = 11(Slave FIFO Mode), Set PORTACFG[6] to USE PA7-SLCS (Page 275) 0100_0000 126 SYNCDELAY; 127 FIFOPINPOLAR = 0x04; //BIT[5:0] = {PKTEND, SLOE, SLRD, SLWR, EMPTY, FULL}--0000_0100 128 //Set SLWR High Valid; PKTEND,SLOE,SLRD EMPTY, FULL Low Active(Page 342) 129 SYNCDELAY; 130 131 132 //-------------------------------------------------------- 133 //设置为Autoin时的自动传输包大小(Page 239) 134 SYNCDELAY; 135 EP2AUTOINLENH = 0x02; // EZ-USB automatically commits data in 512-byte chunks 0000_0010 136 // EP2AUTOINLENH = 0x04; // EZ-USB automatically commits data in 1024-byte chunks 137 SYNCDELAY; 138 EP2AUTOINLENL = 0x00; 139 SYNCDELAY; 140 141 //Set Autopointer, enable dual autopointer(Page 215-216) 142 AUTOPTRSETUP = 0x01; 143 144 145 //FLAGA - User-Programmable Level; FLAGB - FIFO Full, FLAGC - FIFO Empty: (L: Valid)(Page 223) 146 PINFLAGSAB = 0x00;//0x8a; 147 SYNCDELAY; 148 PINFLAGSCD = 0x00;//0x08; 149 SYNCDELAY; 150 151 152 153 154 155 //===========================大大的分割线===============================// 156 /* 157 // out endpoints do not come up armed 158 159 // since the defaults are double buffered we must write dummy byte counts twice 160 SYNCDELAY; 161 EP2BCL = 0x80; // arm EP2OUT by writing byte count w/skip. 162 SYNCDELAY; 163 EP2BCL = 0x80; 164 SYNCDELAY; 165 EP4BCL = 0x80; // arm EP4OUT by writing byte count w/skip. 166 SYNCDELAY; 167 EP4BCL = 0x80; 168 169 */ 170 171 // enable dual autopointer feature 172 //AUTOPTRSETUP |= 0x01; 173 174 } 175 176 177 void TD_Poll(void) // Called repeatedly while the device is idle 178 { 179 180 } 181 182 BOOL TD_Suspend(void) // Called before the device goes into suspend mode 183 { 184 return(TRUE); 185 } 186 187 BOOL TD_Resume(void) // Called after the device resumes 188 { 189 return(TRUE); 190 } 191 192 //----------------------------------------------------------------------------- 193 // Device Request hooks 194 // The following hooks are called by the end point 0 device request parser. 195 //----------------------------------------------------------------------------- 196 197 BOOL DR_GetDescriptor(void) 198 { 199 return(TRUE); 200 } 201 202 BOOL DR_SetConfiguration(void) // Called when a Set Configuration command is received 203 { 204 Configuration = SETUPDAT[2]; 205 return(TRUE); // Handled by user code 206 } 207 208 BOOL DR_GetConfiguration(void) // Called when a Get Configuration command is received 209 { 210 EP0BUF[0] = Configuration; 211 EP0BCH = 0; 212 EP0BCL = 1; 213 return(TRUE); // Handled by user code 214 } 215 216 BOOL DR_SetInterface(void) // Called when a Set Interface command is received 217 { 218 AlternateSetting = SETUPDAT[2]; 219 return(TRUE); // Handled by user code 220 } 221 222 BOOL DR_GetInterface(void) // Called when a Set Interface command is received 223 { 224 EP0BUF[0] = AlternateSetting; 225 EP0BCH = 0; 226 EP0BCL = 1; 227 return(TRUE); // Handled by user code 228 } 229 230 BOOL DR_GetStatus(void) 231 { 232 return(TRUE); 233 } 234 235 BOOL DR_ClearFeature(void) 236 { 237 return(TRUE); 238 } 239 240 BOOL DR_SetFeature(void) 241 { 242 return(TRUE); 243 } 244 245 BOOL DR_VendorCmnd(void) 246 { 247 BYTE tmp; 248 249 switch (SETUPDAT[1]) 250 { 251 case VR_NAKALL_ON: 252 tmp = FIFORESET; 253 tmp |= bmNAKALL; 254 SYNCDELAY; 255 FIFORESET = tmp; 256 break; 257 case VR_NAKALL_OFF: 258 tmp = FIFORESET; 259 tmp &= ~bmNAKALL; 260 SYNCDELAY; 261 FIFORESET = tmp; 262 break; 263 default: 264 return(TRUE); 265 } 266 267 return(FALSE); 268 } 269 270 //----------------------------------------------------------------------------- 271 // USB Interrupt Handlers 272 // The following functions are called by the USB interrupt jump table. 273 //----------------------------------------------------------------------------- 274 275 // Setup Data Available Interrupt Handler 276 void ISR_Sudav(void) interrupt 0 277 { 278 GotSUD = TRUE; // Set flag 279 EZUSB_IRQ_CLEAR(); 280 USBIRQ = bmSUDAV; // Clear SUDAV IRQ 281 } 282 283 // Setup Token Interrupt Handler 284 void ISR_Sutok(void) interrupt 0 285 { 286 EZUSB_IRQ_CLEAR(); 287 USBIRQ = bmSUTOK; // Clear SUTOK IRQ 288 } 289 290 void ISR_Sof(void) interrupt 0 291 { 292 EZUSB_IRQ_CLEAR(); 293 USBIRQ = bmSOF; // Clear SOF IRQ 294 } 295 296 void ISR_Ures(void) interrupt 0 297 { 298 // whenever we get a USB reset, we should revert to full speed mode 299 pConfigDscr = pFullSpeedConfigDscr; 300 ((CONFIGDSCR xdata *) pConfigDscr)->type = CONFIG_DSCR; 301 pOtherConfigDscr = pHighSpeedConfigDscr; 302 ((CONFIGDSCR xdata *) pOtherConfigDscr)->type = OTHERSPEED_DSCR; 303 304 EZUSB_IRQ_CLEAR(); 305 USBIRQ = bmURES; // Clear URES IRQ 306 } 307 308 void ISR_Susp(void) interrupt 0 309 { 310 Sleep = TRUE; 311 EZUSB_IRQ_CLEAR(); 312 USBIRQ = bmSUSP; 313 } 314 315 void ISR_Highspeed(void) interrupt 0 316 { 317 if (EZUSB_HIGHSPEED()) 318 { 319 pConfigDscr = pHighSpeedConfigDscr; 320 ((CONFIGDSCR xdata *) pConfigDscr)->type = CONFIG_DSCR; 321 pOtherConfigDscr = pFullSpeedConfigDscr; 322 ((CONFIGDSCR xdata *) pOtherConfigDscr)->type = OTHERSPEED_DSCR; 323 } 324 325 EZUSB_IRQ_CLEAR(); 326 USBIRQ = bmHSGRANT; 327 } 328 void ISR_Ep0ack(void) interrupt 0 329 { 330 } 331 void ISR_Stub(void) interrupt 0 332 { 333 } 334 void ISR_Ep0in(void) interrupt 0 335 { 336 } 337 void ISR_Ep0out(void) interrupt 0 338 { 339 } 340 void ISR_Ep1in(void) interrupt 0 341 { 342 } 343 void ISR_Ep1out(void) interrupt 0 344 { 345 } 346 void ISR_Ep2inout(void) interrupt 0 347 { 348 } 349 void ISR_Ep4inout(void) interrupt 0 350 { 351 } 352 void ISR_Ep6inout(void) interrupt 0 353 { 354 } 355 void ISR_Ep8inout(void) interrupt 0 356 { 357 } 358 void ISR_Ibn(void) interrupt 0 359 { 360 } 361 void ISR_Ep0pingnak(void) interrupt 0 362 { 363 } 364 void ISR_Ep1pingnak(void) interrupt 0 365 { 366 } 367 void ISR_Ep2pingnak(void) interrupt 0 368 { 369 } 370 void ISR_Ep4pingnak(void) interrupt 0 371 { 372 } 373 void ISR_Ep6pingnak(void) interrupt 0 374 { 375 } 376 void ISR_Ep8pingnak(void) interrupt 0 377 { 378 } 379 void ISR_Errorlimit(void) interrupt 0 380 { 381 } 382 void ISR_Ep2piderror(void) interrupt 0 383 { 384 } 385 void ISR_Ep4piderror(void) interrupt 0 386 { 387 } 388 void ISR_Ep6piderror(void) interrupt 0 389 { 390 } 391 void ISR_Ep8piderror(void) interrupt 0 392 { 393 } 394 void ISR_Ep2pflag(void) interrupt 0 395 { 396 } 397 void ISR_Ep4pflag(void) interrupt 0 398 { 399 } 400 void ISR_Ep6pflag(void) interrupt 0 401 { 402 } 403 void ISR_Ep8pflag(void) interrupt 0 404 { 405 } 406 void ISR_Ep2eflag(void) interrupt 0 407 { 408 } 409 void ISR_Ep4eflag(void) interrupt 0 410 { 411 } 412 void ISR_Ep6eflag(void) interrupt 0 413 { 414 } 415 void ISR_Ep8eflag(void) interrupt 0 416 { 417 } 418 void ISR_Ep2fflag(void) interrupt 0 419 { 420 } 421 void ISR_Ep4fflag(void) interrupt 0 422 { 423 } 424 void ISR_Ep6fflag(void) interrupt 0 425 { 426 } 427 void ISR_Ep8fflag(void) interrupt 0 428 { 429 } 430 void ISR_GpifComplete(void) interrupt 0 431 { 432 } 433 void ISR_GpifWaveform(void) interrupt 0 434 { 435 }
1 module fifo_wr( 2 clk , 3 rst_n , 4 //Other signal 5 6 //input 7 fifo_full ,//flag 8 load , 9 // fifo_empty ,//flag 10 //output 11 fifo_wr ,//fifo_wr signal 12 fifo_data ,//fifo write data 13 fifoadr //select fifo 14 ); 15 16 //参数定义 17 parameter DATA_W = 16; 18 parameter STATE_W = 3; 19 20 parameter IDLE = 0; 21 parameter S1 = 1; 22 parameter S2 = 2; 23 parameter S3 = 3; 24 parameter S4 = 4; 25 26 27 //输入信号定义 28 input clk ; 29 input rst_n ; 30 31 input fifo_full ; 32 input load ; 33 //input fifo_empty ; 34 35 //输出信号定义 36 output fifo_wr ; 37 output[DATA_W-1:0] fifo_data ; 38 output[2-1:0] fifoadr ; 39 40 //输出信号reg定义 41 reg fifo_wr ; 42 reg [DATA_W-1:0] fifo_data ; 43 reg [2-1:0] fifoadr ; 44 45 //中间信号定义 46 reg clkin ; 47 reg [STATE_W-1:0] state_c ; 48 reg [STATE_W-1:0] state_n ; 49 50 wire start_s3 ; 51 wire end_s4 ; 52 53 wire [DATA_W-1:0] rand_num; 54 55 56 //First div clk by 2 57 always @(posedge clk or negedge rst_n)begin 58 if(rst_n==1'b0)begin 59 clkin <= 'b1; 60 end 61 else begin 62 clkin<= ~clkin; 63 end 64 end 65 66 67 //三段式状态机 68 //第一个进程,同步时序always模块,格式化描述次态寄存器迁移到现态寄存器(不需更改) 69 always@(posedge clkin or negedge rst_n)begin 70 if(!rst_n)begin 71 state_c <= IDLE; 72 end 73 else begin 74 state_c <= state_n; 75 end 76 end 77 78 //第二个进程,组合逻辑always模块,描述状态转移条件判断 79 always@(*)begin 80 case(state_c) 81 IDLE:begin 82 state_n = S1; 83 end 84 S1:begin 85 state_n = S2 ; 86 end 87 S2:begin 88 if(start_s3)begin 89 state_n = S3; 90 end 91 else begin 92 state_n = state_c; 93 end 94 end 95 S3:begin 96 state_n = S4; 97 end 98 S4:begin 99 if(end_s4)begin 100 state_n = S2; 101 end 102 else begin 103 state_n = IDLE; 104 end 105 end 106 default:begin 107 state_n = IDLE; 108 end 109 endcase 110 end 111 //assign start_s1 = state_c==IDLE && ; 112 assign start_s3 = state_c==S2 && fifo_full; 113 assign end_s4 = state_c==S3 && fifo_full; 114 //assign end_s2 = state_c==S2 && ; 115 116 117 //第三个进程,同步时序always模块,格式化描述寄存器输出(可有多个输出) 118 119 //fifoadr 此处应该考虑写状态fifoadr和非写状态的fifoadr是不一样的 120 always @(posedge clkin or negedge rst_n)begin 121 if(rst_n==1'b0 )begin 122 fifoadr <= 2'b 00; 123 end 124 else begin 125 fifoadr <= 2'b 00;//10指的是EP2 126 end 127 //else if(state_c==S1)begin 128 // fifoadr <= 2'b00;//目前还不知道具体选哪个 129 // end 130 end 131 132 //fifo_wr 133 always @(posedge clkin or negedge rst_n)begin 134 if(!rst_n)begin 135 fifo_wr <=1'b0; //初始化 136 end 137 else if(state_c==S3)begin 138 fifo_wr <= 1'b1; 139 end 140 else begin 141 fifo_wr <= 1'b0; 142 end 143 end 144 145 146 147 //fifo_data 148 always @(posedge clkin or negedge rst_n)begin 149 if(rst_n==1'b0)begin 150 fifo_data <= 'hff; 151 end 152 else if(state_c == S3)begin 153 fifo_data <= rand_num ; 154 end 155 end 156 157 158 //例化RanGen 159 RanGen uut_RanGen( 160 .clk (clk ), 161 .rst_n (rst_n ), 162 .load (load ), 163 .rand_num (rand_num) 164 165 166 167 ); 168 169 170 endmodule
浙公网安备 33010602011771号