examples/usbserial: Check if usbserial device exist before register

This commit is contained in:
Kevin Zhou 2024-08-19 16:07:57 +08:00 committed by Petro Karashchenko
parent 5a04b9a7c9
commit ab67cb1911

View File

@ -120,29 +120,30 @@ static const char g_shortmsg[] = "Hello, World!!\n";
static const char g_longmsg[] = static const char g_longmsg[] =
"The Spanish Armada a Speech by Queen Elizabeth I of England\n" "The Spanish Armada a Speech by Queen Elizabeth I of England\n"
"Addressed to the English army at Tilbury Fort - 1588\n" "Addressed to the English army at Tilbury Fort - 1588\n"
"My loving people, we have been persuaded by some, that are careful of our " "My loving people, we have been persuaded by some, that are careful of "
"safety, to take heed how we commit ourselves to armed multitudes, for fear " "our safety, to take heed how we commit ourselves to armed multitudes, "
"of treachery; but I assure you, I do not desire to live to distrust my " "or fear of treachery; but I assure you, I do not desire to live to "
"faithful and loving people.\n" "distrust my faithful and loving people.\n"
"Let tyrants fear; I have always so behaved myself that, under God, I have " "Let tyrants fear; I have always so behaved myself that, under God, I "
"placed my chiefest strength and safeguard in the loyal hearts and good will " "have placed my chiefest strength and safeguard in the loyal hearts and "
"of my subjects. And therefore I am come amongst you at this time, not as for " "good will of my subjects. And therefore I am come amongst you at this "
"my recreation or sport, but being resolved, in the midst and heat of the " "time, not as for my recreation or sport, but being resolved, in the "
"battle, to live or die amongst you all; to lay down, for my God, and for " "midst and heat of the battle, to live or die amongst you all; to lay "
"my kingdom, and for my people, my honour and my blood, even the dust.\n" "down, for my God, and for my kingdom, and for my people, my honour and "
"I know I have but the body of a weak and feeble woman; but I have the heart " "my blood, even the dust.\n"
"of a king, and of a king of England, too; and think foul scorn that Parma " "I know I have but the body of a weak and feeble woman; but I have the "
"or Spain, or any prince of Europe, should dare to invade the borders of my " "heart of a king, and of a king of England, too; and think foul scorn "
"realms: to which, rather than any dishonour should grow by me, I myself will " "hat Parma or Spain, or any prince of Europe, should dare to invade the "
"take up arms; I myself will be your general, judge, and rewarder of every " "borders of my realms: to which, rather than any dishonour should grow "
"one of your virtues in the field.\n" "by me, I myself will take up arms; I myself will be your general, "
"judge, and rewarder of every one of your virtues in the field.\n"
"I know already, by your forwardness, that you have deserved rewards and " "I know already, by your forwardness, that you have deserved rewards and "
"crowns; and we do assure you, on the word of a prince, they shall be duly " "crowns; and we do assure you, on the word of a prince, they shall be "
"paid you. In the mean my lieutenant general shall be in my stead, than whom " "duly paid you. In the mean my lieutenant general shall be in my stead, "
"never prince commanded a more noble and worthy subject; not doubting by " "than whom never prince commanded a more noble and worthy subject; not "
"your obedience to my general, by your concord in the camp, and by your " "doubting by your obedience to my general, by your concord in the camp, "
"valour in the field, we shall shortly have a famous victory over the enemies " "and by your valour in the field, we shall shortly have a famous victory "
"of my God, of my kingdom, and of my people.\n"; "over the enemies of my God, of my kingdom, and of my people.\n";
#endif #endif
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY #ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
@ -191,10 +192,14 @@ int main(int argc, FAR char *argv[])
#endif #endif
ssize_t nbytes; ssize_t nbytes;
#ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY #ifndef CONFIG_EXAMPLES_USBSERIAL_INONLY
int i, j, k; int i;
int j;
int k;
#endif #endif
int ret; int ret;
if (access(USBSER_DEVNAME, F_OK) < 0)
{
/* Initialize the USB serial driver */ /* Initialize the USB serial driver */
printf("usbserial_main: Registering USB serial driver\n"); printf("usbserial_main: Registering USB serial driver\n");
@ -218,16 +223,17 @@ int main(int argc, FAR char *argv[])
ret = boardctl(BOARDIOC_USBDEV_CONTROL, (uintptr_t)&ctrl); ret = boardctl(BOARDIOC_USBDEV_CONTROL, (uintptr_t)&ctrl);
if (ret < 0) if (ret < 0)
{ {
printf("usbserial_main: ERROR: Failed to create the USB serial device: %d\n", printf("usbserial_main: ERROR: Failed to create the USB serial "
-ret); "device: %d\n", -ret);
return 1; return 1;
} }
printf("usbserial_main: Successfully registered the serial driver\n"); printf("usbserial_main: Successfully registered the serial driver\n");
}
#if defined(CONFIG_USBDEV_TRACE) && CONFIG_USBDEV_TRACE_INITIALIDSET != 0 #if defined(CONFIG_USBDEV_TRACE) && CONFIG_USBDEV_TRACE_INITIALIDSET != 0
/* If USB tracing is enabled and tracing of initial USB events is specified, /* If USB tracing is enabled and tracing of initial USB events is
* then dump all collected trace data to stdout * specified, then dump all collected trace data to stdout
*/ */
sleep(5); sleep(5);
@ -267,7 +273,9 @@ int main(int argc, FAR char *argv[])
} }
} }
/* If USB tracing is enabled, then dump all collected trace data to stdout */ /* If USB tracing is enabled, then dump all collected trace data
* to stdout
*/
dumptrace(); dumptrace();
} }
@ -312,7 +320,9 @@ int main(int argc, FAR char *argv[])
} }
} }
/* If USB tracing is enabled, then dump all collected trace data to stdout */ /* If USB tracing is enabled, then dump all collected trace data
* to stdout
*/
dumptrace(); dumptrace();
} }
@ -425,7 +435,8 @@ int main(int argc, FAR char *argv[])
if (j + k < nbytes) if (j + k < nbytes)
{ {
if (g_iobuffer[j+k] >= 0x20 && g_iobuffer[j+k] < 0x7f) if (g_iobuffer[j + k] >= 0x20 &&
g_iobuffer[j + k] < 0x7f)
{ {
printf("%c", g_iobuffer[j + k]); printf("%c", g_iobuffer[j + k]);
} }
@ -453,7 +464,9 @@ int main(int argc, FAR char *argv[])
sleep(5); sleep(5);
#endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */ #endif /* CONFIG_EXAMPLES_USBSERIAL_INONLY */
/* If USB tracing is enabled, then dump all collected trace data to stdout */ /* If USB tracing is enabled, then dump all collected trace data
* to stdout
*/
dumptrace(); dumptrace();
} }