Skip to content

USBD add core-power and Vbus handle #50

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
21 changes: 13 additions & 8 deletions include/unicore-mx/usbd/usbd.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ enum usbd_speed {

typedef enum usbd_speed usbd_speed;

/** Optional features */
enum usbd_backend_features{
USBD_FEATURE_NONE = 0,
USBD_PHY_EXT = (1 << 0),
USBD_VBUS_SENSE = (1 << 1),
USBD_VBUS_EXT = (1 << 2)
//* provide usb-core auto power-up/down on connect/disconnect
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"//*" (IMO) Please C style comment only (code consistancy).
@danielinux @brabo what do you think?

, USBD_USE_POWERDOWN = (1<<3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(IHMO) ", USBD_USE_POWERDOWN" code consistancy. "," not placed there.
(IHMO) "(1<<3)" , "(1 << 2)", "(1 << 0)," space missing in "1" "<<" "3" code consistancy.

@brabo @danielinux Can you please give me a hand at code style / consistancy work.

};

struct usbd_backend_config {
/** Number of endpoints. (Including control endpoint) */
uint8_t ep_count;
Expand All @@ -145,13 +155,8 @@ struct usbd_backend_config {
/** Device speed */
usbd_speed speed;

/** Optional features */
enum {
USBD_FEATURE_NONE = 0,
USBD_PHY_EXT = (1 << 0),
USBD_VBUS_SENSE = (1 << 1),
USBD_VBUS_EXT = (1 << 2)
} feature;
/** Optional features see usbd_backend_features*/
unsigned int feature;
};

typedef struct usbd_backend_config usbd_backend_config;
Expand Down Expand Up @@ -279,7 +284,7 @@ enum usbd_transfer_flags {
USBD_FLAG_PACKET_PER_FRAME_MASK = (0x3 << 5)
};

typedef enum usbd_transfer_flags usbd_transfer_flags;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly ive got a warning here? enum and typedef - with same name. its confusing me, i newer see such trick.
i`ve saw overriding enum item name with macro definition, but override enum by typedef - is it vlalid practice for C?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

@alexrayne alexrayne May 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, i know about namespaces, and that code was compiled succesfully.
when i ask about "valid practice for C" i mean - is it good? is it helpful? can it lead to some errors?
the name "usbd_transfer_flags" imho nott very suitable for enum. such name also can be imagine for struct of a binary set of flags.
anyway, if you insist - let`s revert this typedef back to code.

Copy link
Author

@alexrayne alexrayne May 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you forgot to denote
+typedef unsigned char usbd_transfer_flags;
ARMCC compiler generates warning when we try to assign to enum variable integer value (with set of flags).
therefore i denote for usbd_transfer_flags as numeric type

typedef unsigned char usbd_transfer_flags;

/**
* USB Transfer status
Expand Down
12 changes: 12 additions & 0 deletions lib/usbd/usbd.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,19 @@ void usbd_poll(usbd_device *dev, uint32_t us)

void usbd_disconnect(usbd_device *dev, bool disconnect)
{
if (!disconnect) {
// power-up on connect
if ((dev->config->feature & USBD_USE_POWERDOWN) != 0)
usbd_enable(dev, true);
}
if (dev->backend->disconnect) {
dev->backend->disconnect(dev, disconnect);
}
if (disconnect) {
// power-down after disconnect
if ((dev->config->feature & USBD_USE_POWERDOWN) != 0)
usbd_enable(dev, false);
}
}

void usbd_ep_prepare(usbd_device *dev, uint8_t addr, usbd_ep_type type,
Expand Down Expand Up @@ -224,6 +234,8 @@ bool usbd_is_vbus(usbd_device *dev){
}

void usbd_enable(usbd_device *dev, bool onoff){
if (!onoff)
usbd_disconnect(dev, false);
if (dev->backend->power_control)
dev->backend->power_control(dev, (onoff)? usbd_paActivate : usbd_paShutdown );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Application code
= usbd_disconnect(dev, true)
== dev->backend->disconnect(dev, true);
== usbd_enable(dev, false);
=== usbd_disconnect(dev, false);
==== usbd_enable(dev, true);
===== dev->backend->power_control(dev, usbd_paActivate);
==== dev->backend->disconnect(dev, false);
=== dev->backend->power_control(dev, usbd_paShutdown );

Look at the flow of control.
At the end, NOT disconnected with SHUTDOWN power control.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don`t understand you - enable(false) MUST not shutdown?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i see, thank you

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for your new code.

=usbd_disconnect(dev, true)   <----------------\
==dev->backend->disconnect(dev, true);         | RECURSIVE CALL! STACK OVERFLOW!
==usbd_enable(dev, false);                     |
===usbd_disconnect(dev, true)   ---------------/
===dev->backend->power_control(dev, usbd_paShutdown);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, see alredy. more trouble now bother me - cantt understand how to wake up FS from powerdown, cause it want normaly detect online and reset after ungating core clock

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, resolve it. but not preferable way - not like what i write

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When usbd_enable(dev, false) is called, it disconnect and shutdown. but when usbd_enable(dev, true) is called, it only power-up (it remove the disconnect condition).

Copy link
Author

@alexrayne alexrayne May 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, this was confused me too. disconnection MUST be provided before powerdown. but fro stm DWC core it is mean that USB enter to non-connectable state. imho, name 'disconnect' - not very suitable for this operation.
i`ll change behaviour to restore disconnection state on powerdown complete.
i not sure, is it good to place disconnection before powerdown into backend->power_control, instead of botherig it on frontend?

Expand Down
5 changes: 3 additions & 2 deletions lib/usbd/usbd_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,11 @@ struct usbd_device {
#endif
};

typedef enum {
enum _usbd_power_action{
usbd_paShutdown
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Enum labels are uppercase. (see coding style for full information)
  • Without compelling reason, please dont use typedef. (see coding style for full information)
    If you really want, you can later do typedef enum usbd_power_action usbd_power_action.

, usbd_paActivate
} usbd_power_action;
};
typedef enum _usbd_power_action usbd_power_action;

/* Functions provided by the hardware abstraction. */
struct usbd_backend {
Expand Down