{"action":"opened","issue":{"url":"https://api.github.com/repos/contiki-os/contiki/issues/1505","repository_url":"https://api.github.com/repos/contiki-os/contiki","labels_url":"https://api.github.com/repos/contiki-os/contiki/issues/1505/labels{/name}","comments_url":"https://api.github.com/repos/contiki-os/contiki/issues/1505/comments","events_url":"https://api.github.com/repos/contiki-os/contiki/issues/1505/events","html_url":"https://github.com/contiki-os/contiki/issues/1505","id":132011502,"number":1505,"title":"cc2538: pwm: Improve API","user":{"login":"bthebaudeau","id":5755442,"avatar_url":"https://avatars.githubusercontent.com/u/5755442?v=3","gravatar_id":"","url":"https://api.github.com/users/bthebaudeau","html_url":"https://github.com/bthebaudeau","followers_url":"https://api.github.com/users/bthebaudeau/followers","following_url":"https://api.github.com/users/bthebaudeau/following{/other_user}","gists_url":"https://api.github.com/users/bthebaudeau/gists{/gist_id}","starred_url":"https://api.github.com/users/bthebaudeau/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bthebaudeau/subscriptions","organizations_url":"https://api.github.com/users/bthebaudeau/orgs","repos_url":"https://api.github.com/users/bthebaudeau/repos","events_url":"https://api.github.com/users/bthebaudeau/events{/privacy}","received_events_url":"https://api.github.com/users/bthebaudeau/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/contiki-os/contiki/labels/Discussion","name":"Discussion","color":"006b75"},{"url":"https://api.github.com/repos/contiki-os/contiki/labels/enhancement","name":"enhancement","color":"84b6eb"},{"url":"https://api.github.com/repos/contiki-os/contiki/labels/Platform%20/%20CPU%20/%20ARM","name":"Platform / CPU / ARM","color":"fbca04"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2016-02-07T21:01:42Z","updated_at":"2016-02-07T21:01:42Z","closed_at":null,"body":"This issue is a discussion about the CC2538 PWM driver API improvements suggested in #1419 to @alignan following #1399.\r\n\r\nI have regrouped my suggestions below.\r\n\r\nAs further improvements to the PWM driver, I think that it would be good to simplify the API to use only the following functions:\r\n - `pwm_start(timer, a_b, port, pin, freq, polarity)`, which would be the equivalent of `pwm_enable(0%)` + `pwm_set_direction(polarity)` + `pwm_stop()` with the existing API,\r\n - `pwm_set_duty_cycle(timer, a_b, duty_cycle)`, which would dynamically adjust the duty cycle without stopping the timer, except for 0% and 100%, which would be the equivalent of `pwm_stop()` with the existing API,\r\n - `pwm_stop(timer, a_b)`, which would be the equivalent of `pwm_disable()` with the existing API, the used port and pin having been saved by `pwm_start()` in order not to have to pass them twice, or they could also be derived from the MUX + GPIO registers.\r\n\r\nThe rationale for this is that there should be a PWM session between `pwm_start()` and `pwm_stop()` during which the chosen pin is always generating a PWM signal between 0% and 100%. Apart from the duty cycle, all the PWM settings should be constant during a session. During this PWM session, the regular usage of a PWM requires to be able to change the duty cycle without stopping the timer, so that this operation is simple, quick and glitch-free. `pwm_set_duty_cycle()` could automatically start and stop the PWM timer when switching between 0%/100% and the other duty cycle values, in order to optimize power consumption. After `pwm_stop()`, the end of the PWM session should revert the timer and the pin to their reset states.\r\n\r\nIt would probably be more efficient to use a fractional data type rather than a percentage to express the duty cycle passed to this API, because:\r\n - % are very human while the duty cycle values passed to this API will probably be software-generated rather than human-generated, especially if the duty cycle has to change in real time,\r\n - % are inefficient for the CPU, requiring a division, while binary-oriented representations would only require a bit shift instead,\r\n - the accuracy would be better and could better benefit from the timer resolution if the frequency is well chosen.\r\n\r\nIt would also be great to have the following options:\r\n - hardware synchronization of timers,\r\n - rounding the start counter value to a power of 2, minus 1, preferably to get full 8- or 16-bit resolution for the duty cycle (using the 8-bit prescaler can help here, and the accuracy of the frequency is often unimportant for PWM signals), in order to ease the use of duty cycle values resulting from computations,\r\n - updating the duty cycle through µDMA."}}