Skip to content

Comments

esp3s3 & esp32c3 PWM implements#5215

Open
dimajolkin wants to merge 7 commits intotinygo-org:devfrom
dimajolkin:esp32s3-pwm
Open

esp3s3 & esp32c3 PWM implements#5215
dimajolkin wants to merge 7 commits intotinygo-org:devfrom
dimajolkin:esp32s3-pwm

Conversation

@dimajolkin
Copy link

@dimajolkin dimajolkin commented Feb 22, 2026

Added implements for esp32s3 & esp32c3 PWM.
tested with an oscilloscope:
1.bmp

TDeck lilygo brightness control was on pwm.

@deadprogram
Copy link
Member

Do ADC next! :)

@dimajolkin
Copy link
Author

@deadprogram I'm not sure if you'll like it, but I added an alias for spi2 as spi0 for esp32c3 to test the pass, and it was easier too)

@dimajolkin
Copy link
Author

It's funny, but I really need ADC)

@deadprogram
Copy link
Member

Unformatted:
  src/machine/machine_esp32xx_pwm.go
make: *** [GNUmakefile:207: fmt-check] Error 1

@deadprogram
Copy link
Member

Shouldn't xiao_eso32s3.go‎ be xiao_esp32s3.go‎?

@dimajolkin
Copy link
Author

Shouldn't xiao_eso32s3.go‎ be xiao_esp32s3.go‎?

Sorry)

@deadprogram
Copy link
Member

@dimajolkin can you please rename that file?

@dimajolkin
Copy link
Author

@dimajolkin can you please rename that file?

forgot push)

}
freq := uint64(1e9) / period
dutyRes := uint8(10)
if freq < 100 {
Copy link
Member

Choose a reason for hiding this comment

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

switch {
    case freq < 100:
        dutyRes = 14
    case freq < 1000:
        dutyRes = 12
    case freq > 100_000:
        dutyRes = 8
}

signal := pwm.SigOutBase + uint32(ch)
pin.configure(PinConfig{Mode: PinOutput}, signal) // GPIO matrix: pin <- LEDC_LS_SIG_OUTn

baseOff := uintptr(ch) * ledcChannelBlockSize
Copy link
Member

Choose a reason for hiding this comment

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

Maybe better to add a function with a switch statement instead of the pointer arithmetic?


baseOff := uintptr(ch) * ledcChannelBlockSize
conf0 := (*volatile.Register32)(unsafe.Add(unsafe.Pointer(&esp.LEDC.CH0_CONF0), baseOff))
conf0.Set((uint32(pwm.timerNum) << 0) | (1 << 2) | (0 << 3)) // timer_sel, sig_out_en=1, idle_lv=0
Copy link
Member

Choose a reason for hiding this comment

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

Better to use consts like esp.LEDC_CH_CONF0_TIMER_SEL_Pos, esp.LEDC_CH_CONF0_SIG_OUT_EN,etc.

But best to use esp.LEDC.SetCH0_CONF0_TIMER_SEL(), esp.LEDC.SetCH0_CONF0_SIG_OUT_EN(), and other setter functions.

value = top
}
dutyVal := value << ledcDutyFracBits
baseOff := uintptr(channel) * ledcChannelBlockSize
Copy link
Member

Choose a reason for hiding this comment

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

See comment above.

@deadprogram
Copy link
Member

@dimajolkin I will admit it is a little more verbose, but very little can go wrong. What do you think? Do you like it better?

@dimajolkin
Copy link
Author

I rewatched the recording and removed the direct links. Instead, I used channel setter registry everywhere.
I had to separate channel settings on different devices, but this has its advantages! :)

@dimajolkin
Copy link
Author

dimajolkin commented Feb 24, 2026

@dimajolkin I will admit it is a little more verbose, but very little can go wrong. What do you think? Do you like it better?

Great edits, I liked it, thanks! I'll try to do this in ADC right away)
If anything, I retested on esp32s3 & esp32c3

@deadprogram
Copy link
Member

Great edits, I liked it, thanks! I'll try to do this in ADC right away)

Already added the xiao-esp32c3 to the TinyHCI based on all this excellent work. I will add ADC tests once that is implemented.

https://github.com/tinygo-org/tinygo/pull/5220/checks?check_run_id=64707450827

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants