Skip to content

Commit 249dce6

Browse files
Matthew Metsdeadprogram
authored andcommitted
Add PWM peripheral documentation to reference/machine
1 parent abfdac5 commit 249dce6

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

‎content/docs/reference/machine.md‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,85 @@ func (uart *UART) WriteByte(c byte) error
320320

321321
Write a single byte to the UART output.
322322

323+
## PWM
324+
325+
```go
326+
type PWMConfig struct {
327+
Period uint64
328+
}
329+
```
330+
331+
The `PWMConfig` struct contains configuration for the PWM peripheral.
332+
333+
* `Period` is the period of the PWM signal, in nanoseconds.
334+
335+
```go
336+
type PWM struct {
337+
// values are unexported or vary by chip
338+
}
339+
340+
var (
341+
PWM0 = PWM{...}
342+
PWM1 = PWM{...}
343+
PWM2 = PWM{...}
344+
)
345+
```
346+
347+
The PWM struct corresponds to a single PWM/timer hardware peripheral, which controls 1 or more output channels. This interface exposes a simple PWM model, that can generate outputs with a variable period and duty cycle.
348+
349+
Note: As of TinyGo 0.41, the name of the PWM peripheral type is not consistent across supported platforms. This could be: PWM, TCC, LEDCPWM, timerType, and pwmGroup. The name of the exported structs could be: Timerx, TCCx, PWMx, TIMx.
350+
351+
For more information, see the [PWM tutorial](../tutorials/pwm)
352+
353+
```go
354+
func (pwm PWM) Configure(config PWMConfig) error
355+
```
356+
357+
Enables and configures the PWM peripheral for use. Does not enable any output channels.
358+
359+
```go
360+
func (pwm PWM) SetPeriod(period uint64) error
361+
```
362+
363+
Update the period of the PWM timer, in nanoseconds.
364+
365+
```go
366+
func (pwm PWM) Period() uint64
367+
```
368+
369+
Get the period of the PWM timer, in nanoseconds.
370+
371+
```go
372+
func (pwm PWM) Top() uint32
373+
```
374+
375+
Get the top (highest value before rollover) of the PWM.
376+
377+
```go
378+
func (pwm PWM) Counter() uint32
379+
```
380+
381+
Get the current value of the PWM counter.
382+
383+
```go
384+
func (pwm PWM) Channel(pin Pin) (uint8, error)
385+
```
386+
387+
Configures the specified pin for PWM mode, and assigns a PWM channel to it.
388+
389+
```go
390+
func (pwm PWM) SetInverting(channel uint8, inverting bool)
391+
```
392+
393+
Configure whether the ouput should be inverted. Normally, the PWM output will go high at the beginning of a cycle, then low when the counter is equal to the channel compare value. If inverted, the channel will start low, then turn high at the compare value.
394+
395+
```go
396+
func (pwm PWM) Set(channel uint8, value uint32)
397+
```
398+
399+
Update the compare value for the specified channel. To calculate the comparison value from a desired duty cycle, multiply the duty cycle by the value of Top().
400+
401+
323402
## Watchdog
324403

325404
```go

0 commit comments

Comments
 (0)