Skip to content

Commit

Permalink
os/arch/arm/src/amebasmart: allow booting normally when USB not attached
Browse files Browse the repository at this point in the history
1. allow it to boot normally even with no USB attached, logs during USB disconnect period will be abandon
2. some of the boot up logs are missing because USB device is not ready yet
3. once USB attached, logs and tash can be input from usb port
  • Loading branch information
zhongnuo-tang authored and sunghan-chang committed Dec 13, 2024
1 parent 05b63a1 commit 511aa52
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 38 deletions.
62 changes: 25 additions & 37 deletions os/arch/arm/src/amebasmart/amebasmart_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ static _sema cdc_acm_attach_status_changed_sema;
extern int cdc_acm_ready_flag;
static uint8_t receive_data;
static int received_flag = 0;
static int attach_status = -1;
/****************************************************************************
* Private Type Definitions
****************************************************************************/
Expand Down Expand Up @@ -358,22 +359,28 @@ static void usb_xmitchars(FAR uart_dev_t *dev)
}
if (nbytes == CONFIG_CDC_ACM_BULK_IN_XFER_SIZE) {
nbytes = 0;
usbd_cdc_acm_transmit(buffer, CONFIG_CDC_ACM_BULK_IN_XFER_SIZE);
while (sem_wait(&g_usbdev.txsem) != 0) {
/* The only case that an error should occur here is if the wait was awakened
* by a signal.
*/
ASSERT(errno == EINTR);
/*we should only call usb TX when device is attached and ready, else abandon the data*/
if (attach_status == USBD_ATTACH_STATUS_ATTACHED && cdc_acm_ready_flag) {
usbd_cdc_acm_transmit(buffer, CONFIG_CDC_ACM_BULK_IN_XFER_SIZE);
while (sem_wait(&g_usbdev.txsem) != 0) {
/* The only case that an error should occur here is if the wait was awakened
* by a signal.
*/
ASSERT(errno == EINTR);
}
}
priv->tx_level-=CONFIG_CDC_ACM_BULK_IN_XFER_SIZE;
}
}
usbd_cdc_acm_transmit(buffer, nbytes);
while (sem_wait(&g_usbdev.txsem) != 0) {
/* The only case that an error should occur here is if the wait was awakened
* by a signal.
*/
ASSERT(errno == EINTR);
/*we should only call usb TX when device is attached and ready, else abandon the data*/
if (attach_status == USBD_ATTACH_STATUS_ATTACHED && cdc_acm_ready_flag) {
usbd_cdc_acm_transmit(buffer, nbytes);
while (sem_wait(&g_usbdev.txsem) != 0) {
/* The only case that an error should occur here is if the wait was awakened
* by a signal.
*/
ASSERT(errno == EINTR);
}
}
priv->tx_level-=nbytes;
rtw_mfree(buffer,0);
Expand Down Expand Up @@ -690,7 +697,11 @@ static void amebasmart_cdc_acm_cb_status_changed(uint8_t status)
{
// struct amebasmart_usbdev_s *dev = (struct amebasmart_usbdev_s *)priv;
// DEBUGASSERT(dev);

/*when detached, we need to clear the rdy flag*/
attach_status = status;
if (status == USBD_ATTACH_STATUS_DETACHED) {
cdc_acm_ready_flag = -1;
}
#if CONFIG_AMEBASMART_USBD_CDC_ACM_HOTPLUG
cdc_acm_attach_status = status;
rtw_up_sema(&cdc_acm_attach_status_changed_sema);
Expand Down Expand Up @@ -758,31 +769,14 @@ int amebasmart_up_usbinitialize(struct amebasmart_usbdev_s *priv)
return ret;
}

/*It takes sometime for USB initialization to complete, so during boot up stage, it /dev/console will be
register with serial first to prevent opening an empty path, then once usb is ready, re-register it to usb and
open the fd*/
static int register_usb(void)
void register_usb(void)
{
while(!cdc_acm_ready_flag){
usleep(1000);
}
USB_DEV.isconsole = true;
uart_register("/dev/ttyACM0", &USB_DEV);
/*unregister /dev/console because serial was sharing the same fd during serial initialization*/
unregister_driver("/dev/console");
uart_register("/dev/console", &USB_DEV);
int fd;
fd = open("/dev/console", O_RDWR);
if(fd >= 0) {
dup2(fd, 1);
dup2(fd, 2);
return OK;
} else {
return -1;
}

}

void usb_initialize(void)
{
struct amebasmart_usbdev_s *priv = NULL;
Expand All @@ -794,10 +788,4 @@ void usb_initialize(void)
if (ret != 0) {
udbg("amebasmart usb init fail\n");
}
ret = register_usb();
if (ret != OK) {
udbg("usb register failed");
}
}


5 changes: 4 additions & 1 deletion os/arch/arm/src/armv7-a/arm_initialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,10 @@ void up_initialize(void)
#ifdef USE_SERIALDRIVER
up_serialinit();
#endif

#ifdef CONFIG_AMEBASMART_USBDEVICE
/*if USB device enabled, we will unregister loguart and register /dev/console to usb*/
register_usb();
#endif
/* Initialize the console device driver (if it is other than the standard
* serial driver).
*/
Expand Down

0 comments on commit 511aa52

Please sign in to comment.