unicore-mx icon indicating copy to clipboard operation
unicore-mx copied to clipboard

USBD add core-power and Vbus handle

Open alexrayne opened this issue 8 years ago • 15 comments

here provided enhances on usbd-driver for:

  1. Vbus sensing - <usbd_is_vbus> method and callback. This helps to detect cable plug in/out, or it can be USBpowering detection
  2. USBcore power/clock domain control - <usbd_enable> method and <power_control> backend implementation for STM32F4xxx. This intends to power-saving by usb-core and PHY stops.

alexrayne avatar Feb 04 '17 10:02 alexrayne

  • VBUS value reading (your current code look ok)
  • various session callback {connected, suspended, resumed, disconnected}. on suspend, disconnected, the USB periph can go into a state that can reduce power consuption of periop and allow detecting SOF. (it should be atleast able to detect activity from host) two way of implementation:
    • [breaking] usbd_register_suspend_callback() and usbd_register_resume_callback() already exists. we can remove them and pass an enum as state to session callback.
    • [non-breaking] make two new function usbd_register_connected_callback() and usbd_register_disconnected_callback() for the missing functionality.
  • USB enable/disable. enable or disable the usb periph communication with host. we can drop usbd_disconnect(). [ on _init() the periph is by default enabled for communcation ]
  • not strongly related: we already have a SOF callback (maybe pass frame number value as 2nd argument)

kuldeepdhaka avatar Feb 08 '17 12:02 kuldeepdhaka

various session callback ...

in my version of unicoremx there no callback concected/disconected - what you talk about? imho control over usbcore power mode should be delegated to user, or must be noted by usbd_conf features.

... we can drop usbd_disconnect(). ...

imho this function made an different thing, vs power control - it initiates session disconnect. but your proposal well - this function can be followed by disable call, that powerdown usbcore after session disconnects. imho this functionality should be denoted by a feature set in usbd_conf_xx structure, to provide backport compatibility with existing code.

alexrayne avatar Feb 08 '17 15:02 alexrayne

Yes, there are no connected/disconnected callback. (dont exists), they need to be implemented.

I think, we should do things transparently for user till they make sense (ie don't get in their way). for example: if user has requested to disable communication with host, it is a good thing that the stack should put the periph in power down mode [saves power]. i see no point in providing a function to put periph in powerdown mode manually (instead of automatically doing it for user).

kuldeepdhaka avatar Feb 09 '17 12:02 kuldeepdhaka

there s possible problem with powerdown - no event generates,no callbacks invokes. Therefore if i powerdown usbcore, suspend callback not affected!

in y board when i plugoff USBcable, session callback invokes and powerdown core. That is all. IMHO here colud be suspend action notified too.

alexrayne avatar Feb 09 '17 12:02 alexrayne

i see no point in providing a function to put periph in powerdown mode manually (instead of automatically doing it for user).

IMHO, user should no bruteforces against library for control over periferia. library should provide API as smart as uer requests. If somebody need manual control - publish this functions not a bad. Maybe automatic power downing is good for default, and it can be disabled by a feature . but i`m aware about code that alredy writen with old library. maybe should be defined some macro, like USBD_FEATURE_POWERDOWN_DEFAULT, that choose behaviour. can you imagine some good name for this macro?

alexrayne avatar Feb 09 '17 12:02 alexrayne

What are the possible usecases of manually controlling power down of usb periph?

The stack can provide an abstraction over power down mode via session management {connected, disconnected, suspended, resumed}. For example, after suspend, application code can either either save power (or not save power) but the stack can do [power down] (mundane task) transparently.

kuldeepdhaka avatar Feb 09 '17 13:02 kuldeepdhaka

Ta present i misunderstand differense between suspend/resume and connect/disconnect operations. can you explain? what happen on invoking suspend and disconnect?

alexrayne avatar Feb 09 '17 15:02 alexrayne

@alexrayne USB 2.0 specs (usb_20.pdf) page 240 screenshot_2017-02-09_21-45-20

kuldeepdhaka avatar Feb 09 '17 16:02 kuldeepdhaka

Thank you for this picture. There almost see that suspended is surely not an powerdown mode. but where threr is an mode whre Vbus down - maybe it is "attached" ?

As i remember we discuss about enable vs [dis]connect semantic of usbd api? i agree about that functionality of this methods are close. but here is aspects. disconnect in current implementation makes an logical bus disconnection, after that we can reconfigure usb OTG (or not) for say - host mode, and reconnect again. But if we make power-down in disconnection, usbcore comes not accesible, until powerup. So for reconfigure, we need power enable without connection?

alexrayne avatar Feb 09 '17 19:02 alexrayne

afaiu, Taking power from VBUS dont not mean that a device is connected [ ie self power device]. (yea, Inrush current could be a hint for "something attached"). the actual detection occur via D+ and D- pull-{up/down}.

USB Host control (master) the suspend and resume process. device only have to make sure that it implement the protocol correctly (reduce power consumption and wait for a SOF to detect resume signal).

usbd_disconnect() -> dont communicate with Host. usbd_enable() - disable clocking of periph etc.. (ultimately lead to loss of communicate with host) i would vote for usbd_disconnect() because of (my) intution with function name and its already in api.

I haven't though on reconfiguring api (host mode -> device mode and vice versa - OTG Mode) yet :)

kuldeepdhaka avatar Feb 09 '17 21:02 kuldeepdhaka

because of (my) intution with function name

my intuition - disconnect say nothing about device operational mode, its about connection status. enable(false) says that device should turnoff, and therefore disconnects all connections.

should we fight about names? why this semantics both cant be present?

alexrayne avatar Feb 09 '17 22:02 alexrayne

should we fight about names? why this semantics both cant be present?

But oh boy, bike shedding is fun!

Requirement for the API

  • stop communication with host
  • OTG mode support (switch between Host and device mode)
  • Reduce power consumption.
  • anything else?

kuldeepdhaka avatar Feb 09 '17 23:02 kuldeepdhaka

Kuldeep:

  1. about backend api - one contain method <disconnect (bool )> - but this name confusing me. is it same as <connect(!bool)>?

  2. about handling power on\off on session disconect - in wich sequence should be called on_session callback and backend.power_control(usbd_paActivate)? have you any mention on it?

alexrayne avatar Feb 19 '17 14:02 alexrayne

kuldeep, enums was rewrites with your prefered style

alexrayne avatar Feb 20 '17 22:02 alexrayne

@kuldeepdhaka any input on this?

danielinux avatar May 20 '17 08:05 danielinux