发现论坛里好多都是基于51单片机设计的,分享一下手里关于STM32的设计资料,同时楼主也在学习相关知识
通过QT 实现将数据传输至ARM FSMC至FPGA
单片机源程序如下:
- #include "stm32f10x_lib.h"
- #include "usb_lib.h"
- #include "usb_desc.h"
- #include "hw_config.h"
- #include "usb_pwr.h"
- #include "queue.h"
- #include "string.h"
- #include "stdio.h"
- extern u16 count_out;
- extern u8 buffer_out[VIRTUAL_COM_PORT_DATA_SIZE];
- extern u8 buffer_in[VIRTUAL_COM_PORT_DATA_SIZE];
- extern u16 count_in;
- extern u8 USART1_Tx_Char;
- extern void set_fpga(u8 * dat,u16 len);
- extern void rd_fpga(u8 * dat);
- extern void set_flash(u8 * dat,u16 len);
- extern void rd_flash(u8 * dat);
- void usb_send(u8 *buf,u16 len);
- vu8 Flag_UARTSendOver = 1;
- vu8 Flag_USBSendOver=1;
- vu8 Flag_USBDataReceive = 0;
- u8 QueueWriteFull(DataQueue *Buf, QUEUE_DATA_TYPE Data, u8 Mod)
- {
- Mod = Mod;
- //Uart1_PutString("Queue Full\n" , strlen("Queue Full\n"));
- if(!Flag_UARTSendOver)
- USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
- while(!Flag_UARTSendOver);
- if( QUEUE_OK == QueueRead(&USART1_Tx_Char, (void *)buf_usb_to_uart))
- {
- USART_SendData(USART1, USART1_Tx_Char);
- }
- return QueueWrite((void *)Buf, Data); // 数据重新入队
- }
- void InitQueue(void)
- {
- //初始化USB接收队列
- QueueCreate((void *)buf_usb_to_uart,sizeof(buf_usb_to_uart),NULL,(u8 (*)())QueueWriteFull);
- //初始化USB 发送队列
- QueueCreate((void *)buf_uart_to_usb,sizeof(buf_uart_to_usb),NULL,(u8 (*)())QueueWriteFull);
- }
- void usb_recv_proc()
- {
- //usb_send(buffer_out,count_out);
-
- if(( buffer_out[0] == 's')&&(buffer_out[1] == 'f'))
- {
- set_fpga(&buffer_out[2],count_out);
- }
- else if(( buffer_out[0] == 'r')&&(buffer_out[1] == 'f'))
- {
- rd_fpga(&buffer_out[2]);
- }
- else if(( buffer_out[0] == 'f')&&(buffer_out[1] == 's'))
- {
- set_flash(&buffer_out[2],count_out);
- }
- else if(( buffer_out[0] == 'f')&&(buffer_out[1] == 'r'))
- {
- rd_flash(&buffer_out[2]);
- }
- }
- void usb_recv_chk()
- {
- if(Flag_USBDataReceive)
- {
- count_out = GetEPRxCount(ENDP3); //获得接收到的数据长度
- PMAToUserBufferCopy(buffer_out, ENDP3_RXADDR, count_out); //将数据从USB EP3 RX的缓冲区拷贝到用户指定的数组中
- usb_recv_proc();
- SetEPRxValid(ENDP3); //完成拷贝后置有效状态,从而EP3发送ACK主机可以进行下一个数据包的发送
- Flag_USBDataReceive=0;
- }
- }
- void usb_send_chk()
- {
- u16 i,count,recvCount;
- u8 temp;
- if(Flag_USBSendOver)
- {
- Flag_USBSendOver=0;
- recvCount=0;
- count = QueueNData((void *)buf_uart_to_usb);
- if( count > 64 )
- {
- for(i=0;i<64;i++)
- {
- if( QUEUE_OK == QueueRead(&temp, (void *)buf_uart_to_usb) )
- buffer_in[recvCount++] = temp;
- }
- UserToPMABufferCopy(buffer_in, ENDP1_TXADDR, recvCount);
- SetEPTxCount(ENDP1,recvCount);
- SetEPTxValid(ENDP1);
- }
- else
- {
- for(i=0;i<count;i++)
- {
- if( QUEUE_OK == QueueRead(&temp, (void *)buf_uart_to_usb) )
- buffer_in[recvCount++] = temp;
- }
- UserToPMABufferCopy(buffer_in, ENDP1_TXADDR, recvCount);
- SetEPTxCount(ENDP1,recvCount);
- SetEPTxValid(ENDP1);
- }
- }
- }
- void usb_send(u8 *buf,u16 len)
- {
- while(len--)
- {
- QueueWrite((void *)buf_uart_to_usb,*(buf++));
- }
- }
- extern void my_dac_init();
- extern void pwm_init();
- int main(void)
- {
- Set_System();
- USB_Cable_Config(DISABLE);
- InitQueue();
- Set_USBClock();
- USB_Interrupts_Config();
- USB_Init();
- pwm_init();
- //my_dac_init();
- while (1)
- {
- if ( bDeviceState == CONFIGURED )
- {
- usb_recv_chk();
- usb_send_chk();
- }
- }
- }
- #ifdef DEBUG
- void assert_failed(u8* file, u32 line)
- {
- /* Infinite loop */
- while (1)
- {}
- }
- #endif
- //不使用半主机模式
- #if 1 //如果没有这段,则需要在target选项中选择使用USE microLIB
- #pragma import(__use_no_semihosting)
- struct __FILE
- {
- int handle;
- };
- FILE __stdout;
- _sys_exit(int x)
- {
- x = x;
- }
- #endif
- int fputc(int ch, FILE *f)
- {
- QueueWrite((void *)buf_uart_to_usb,ch);
- return ch;
- }
- /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
复制代码
所有资料51hei提供下载:
MyUSB.7z
(1.17 MB, 下载次数: 18)
|