Код
#define BUF_SIZE 4096
static cyg_uint8 rx_buf[2][BUF_SIZE+1], tx_buf[BUF_SIZE+1];
// --------------------------------------------------------------------------
// Thread receives packets from the USB and sends them out the serial port
// It uses a buffered stdio input, an un-buffered low-level file output.
// This isn't terribly efficient, but rather an example of both methods.
void usb2ser_func(cyg_addrword_t data)
{
int n;
unsigned ibuf, next_buf;
DBG("Usb2Ser: Thread starting\n");
// Give the USB receiver an adequate buffer.
// setvbuf(rxf, usb2ser_buf, _IOFBF, BUF_SIZE);
ibuf = 0;
while (1) {
// ----- Wait for the host to configure -----
DBG("Usb2Ser: Waiting for USB configuration\n");
usbs_serial_wait_until_configured();
cyg_thread_delay((cyg_tick_count_t) 10);
// ----- While configured read data & send out serial port -----
DBG("Usb2Ser: USB configured\n");
usbs_serial_start_rx(&usbs_ser0, rx_buf[ibuf], BUF_SIZE);
while (usbs_serial_is_configured()) {
n = usbs_serial_wait_for_rx(&usbs_ser0);
next_buf = ibuf ^ 1;
usbs_serial_start_rx(&usbs_ser0, rx_buf[next_buf], BUF_SIZE);
if (n < 0) {
DBG("*** I/O Error: %d ***\n", n);
}
else {
rx_buf[ibuf][n] = '\0';
DBG("%s", rx_buf[ibuf]);
}
ibuf = next_buf;
}
}
}
// --------------------------------------------------------------------------
// Thread receives packets from the serial port and sends them out the USB
// It uses a buffered stdio input, an un-buffered low-level file output.
// This isn't terribly efficient, but rather an example of both methods.
void ser2usb_func(cyg_addrword_t data)
{
int n;
DBG("Ser2Usb: Thread starting\n");
while (1) {
// ----- Wait for the host to configure -----
DBG("Ser2Usb: Waiting for USB configuration\n");
usbs_serial_wait_until_configured();
cyg_thread_delay((cyg_tick_count_t) 10);
// ----- While configured read data & send out serial port -----
DBG("Ser2Usb: USB configured\n");
while (usbs_serial_is_configured()) {
n = 5;
memset(tx_buf, 0xAA, n);
usbs_serial_tx(&usbs_ser0, tx_buf, n);
cyg_thread_delay((cyg_tick_count_t) 1000);
}
}
}
// --------------------------------------------------------------------------
// Application Startup
// --------------------------------------------------------------------------
int main(void)
{
DBG("Entering usb_serial_send function\n");
cyg_thread_create(4, usb2ser_func, (cyg_addrword_t) 0,
"Usb2Serial", (void *) stack[0], THREAD_STACK_SIZE,
&usb2ser_thread, &thread[0]);
cyg_thread_create(4, ser2usb_func, (cyg_addrword_t) 1,
"Serial2Usb", (void *) stack[1], THREAD_STACK_SIZE,
&ser2usb_thread, &thread[1]);
// Start USB subsystem
usbs_serial_start();
// Start the threads running.
cyg_thread_resume(usb2ser_thread);
cyg_thread_resume(ser2usb_thread);
#if 1
char ch = 0;
while (ch != 'q') {
printf("\r\nPress `q' to quit");
scanf("%c", &ch);
}
#else
for (;;) {
}
#endif
CYGACC_CALL_IF_RESET();
//CYGACC_CALL_IF_MONITOR_RETURN(0); // return to RedBoot
return 0;
}
static cyg_uint8 rx_buf[2][BUF_SIZE+1], tx_buf[BUF_SIZE+1];
// --------------------------------------------------------------------------
// Thread receives packets from the USB and sends them out the serial port
// It uses a buffered stdio input, an un-buffered low-level file output.
// This isn't terribly efficient, but rather an example of both methods.
void usb2ser_func(cyg_addrword_t data)
{
int n;
unsigned ibuf, next_buf;
DBG("Usb2Ser: Thread starting\n");
// Give the USB receiver an adequate buffer.
// setvbuf(rxf, usb2ser_buf, _IOFBF, BUF_SIZE);
ibuf = 0;
while (1) {
// ----- Wait for the host to configure -----
DBG("Usb2Ser: Waiting for USB configuration\n");
usbs_serial_wait_until_configured();
cyg_thread_delay((cyg_tick_count_t) 10);
// ----- While configured read data & send out serial port -----
DBG("Usb2Ser: USB configured\n");
usbs_serial_start_rx(&usbs_ser0, rx_buf[ibuf], BUF_SIZE);
while (usbs_serial_is_configured()) {
n = usbs_serial_wait_for_rx(&usbs_ser0);
next_buf = ibuf ^ 1;
usbs_serial_start_rx(&usbs_ser0, rx_buf[next_buf], BUF_SIZE);
if (n < 0) {
DBG("*** I/O Error: %d ***\n", n);
}
else {
rx_buf[ibuf][n] = '\0';
DBG("%s", rx_buf[ibuf]);
}
ibuf = next_buf;
}
}
}
// --------------------------------------------------------------------------
// Thread receives packets from the serial port and sends them out the USB
// It uses a buffered stdio input, an un-buffered low-level file output.
// This isn't terribly efficient, but rather an example of both methods.
void ser2usb_func(cyg_addrword_t data)
{
int n;
DBG("Ser2Usb: Thread starting\n");
while (1) {
// ----- Wait for the host to configure -----
DBG("Ser2Usb: Waiting for USB configuration\n");
usbs_serial_wait_until_configured();
cyg_thread_delay((cyg_tick_count_t) 10);
// ----- While configured read data & send out serial port -----
DBG("Ser2Usb: USB configured\n");
while (usbs_serial_is_configured()) {
n = 5;
memset(tx_buf, 0xAA, n);
usbs_serial_tx(&usbs_ser0, tx_buf, n);
cyg_thread_delay((cyg_tick_count_t) 1000);
}
}
}
// --------------------------------------------------------------------------
// Application Startup
// --------------------------------------------------------------------------
int main(void)
{
DBG("Entering usb_serial_send function\n");
cyg_thread_create(4, usb2ser_func, (cyg_addrword_t) 0,
"Usb2Serial", (void *) stack[0], THREAD_STACK_SIZE,
&usb2ser_thread, &thread[0]);
cyg_thread_create(4, ser2usb_func, (cyg_addrword_t) 1,
"Serial2Usb", (void *) stack[1], THREAD_STACK_SIZE,
&ser2usb_thread, &thread[1]);
// Start USB subsystem
usbs_serial_start();
// Start the threads running.
cyg_thread_resume(usb2ser_thread);
cyg_thread_resume(ser2usb_thread);
#if 1
char ch = 0;
while (ch != 'q') {
printf("\r\nPress `q' to quit");
scanf("%c", &ch);
}
#else
for (;;) {
}
#endif
CYGACC_CALL_IF_RESET();
//CYGACC_CALL_IF_MONITOR_RETURN(0); // return to RedBoot
return 0;
}
Это измененный пример тестового исходника из пакета eCos.
Вот что выдает приложение при подключении к ПК.
Код
Entering usb_serial_send function
### 4:Reset ###
Usb2Ser: Thread star### 132:<null> ###
ting
Usb2Ser: Waiting for USB configuration
Ser2Usb: Thread starting
Ser2Usb: Waiting for USB configuration
Press `q' to quit### 4:Reset ###
### 3:Powered ###
### 4:Reset ###
### 3:Powered ###
### 4:Reset ###
### 5:Addressed ###
### 6:Configured ###
### 6:Configured ###
Usb2Ser: USB configured
Ser2Usb: USB configured
### 4:Reset ###
Usb2Ser: Thread star### 132:<null> ###
ting
Usb2Ser: Waiting for USB configuration
Ser2Usb: Thread starting
Ser2Usb: Waiting for USB configuration
Press `q' to quit### 4:Reset ###
### 3:Powered ###
### 4:Reset ###
### 3:Powered ###
### 4:Reset ###
### 5:Addressed ###
### 6:Configured ###
### 6:Configured ###
Usb2Ser: USB configured
Ser2Usb: USB configured
Прошу помочь, на что нужно обратить внимание.
Спасибо