Created
February 1, 2022 10:40
-
-
Save sago35/529d5dd5cac4804456ae371768146c14 to your computer and use it in GitHub Desktop.
TinyGo + Wio Terminal + LCD_BACKLIGHT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"device/sam" | |
"log" | |
"machine" | |
"time" | |
) | |
func main() { | |
err := run() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
func run() error { | |
err := initialize() | |
if err != nil { | |
return err | |
} | |
led := machine.LED | |
led.Configure(machine.PinConfig{Mode: machine.PinOutput}) | |
brightness := uint8(0) | |
for { | |
led.Toggle() | |
if true { | |
setBrightness(brightness) | |
brightness = (brightness + 10) % maxBrightness | |
} | |
time.Sleep(100 * time.Millisecond) | |
} | |
return nil | |
} | |
var ( | |
maxBrightness = uint8(200) | |
currentBrightness = uint8(50) | |
) | |
func initialize() error { | |
// enable peripheral clocks | |
sam.GCLK.PCHCTRL[sam.PCHCTRL_GCLK_TC0].SetBits(sam.GCLK_PCHCTRL_CHEN) | |
for !sam.GCLK.PCHCTRL[sam.PCHCTRL_GCLK_TC0].HasBits(sam.GCLK_PCHCTRL_CHEN) { | |
} | |
sam.GCLK.PCHCTRL[sam.PCHCTRL_GCLK_EVSYS0].SetBits(sam.GCLK_PCHCTRL_CHEN) | |
for !sam.GCLK.PCHCTRL[sam.PCHCTRL_GCLK_EVSYS0].HasBits(sam.GCLK_PCHCTRL_CHEN) { | |
} | |
sam.GCLK.PCHCTRL[sam.PCHCTRL_GCLK_CCL].SetBits(sam.GCLK_PCHCTRL_CHEN) | |
for !sam.GCLK.PCHCTRL[sam.PCHCTRL_GCLK_CCL].HasBits(sam.GCLK_PCHCTRL_CHEN) { | |
} | |
sam.MCLK.APBAMASK.SetBits(sam.MCLK_APBAMASK_TC0_) | |
sam.MCLK.APBBMASK.SetBits(sam.MCLK_APBBMASK_EVSYS_) | |
sam.MCLK.APBCMASK.SetBits(sam.MCLK_APBCMASK_CCL_) | |
// /* Configure PORT */ | |
sam.PORT.GROUP[2].DIRSET.Set(1 << 5) | |
sam.PORT.GROUP[2].EVCTRL.Set(0x85) // PC05, OUT | |
// /* Configure EVSYS */ | |
sam.EVSYS.USER[1].Set(0x01) // Channel0 -> PORT_EV0 | |
sam.EVSYS.CHANNEL[0].CHANNEL.Set(0x74 | (0x02 << 8) | (0x00 << 10)) // CCL_LUTOUT0, ASYNCHRONOUS, NO_EVT_OUTPUT | |
// /* Configure CCL */ | |
sam.CCL.CTRL.Set(1 << 0) // SWRST | |
sam.CCL.SEQCTRL[0].Set(0x00) // Disable SEQCTRL | |
sam.CCL.LUTCTRL[0].Set((0xAA << 24) | (0x01 << 22) | (0x06 << 8) | (0x01 << 1)) // TRUTH=0xAA, LUTEO, INSEL0=0x06(TC), ENABLE | |
sam.CCL.CTRL.Set(1 << 1) // ENABLE | |
// /* Configure TC0 */ | |
sam.TC0_COUNT8.CTRLA.Set(1 << 0) // SWRST | |
for sam.TC0_COUNT8.CTRLA.HasBits(1 << 0) { | |
} | |
sam.TC0_COUNT8.CTRLA.Set((1 << 2) | (1 << 4) | (4 << 8)) // MODE=COUNT8, PRESCALER=DIV16, PRESCSYNC=PRESC | |
sam.TC0_COUNT8.WAVE.Set(0x02) // WAVEGEN=NPWM | |
sam.TC0_COUNT8.CTRLBSET.Set(1 << 1) // LUPD | |
sam.TC0_COUNT8.PER.Set(maxBrightness) | |
sam.TC0_COUNT8.CC[0].Set(currentBrightness) | |
sam.TC0_COUNT8.CC[1].Set(0) | |
sam.TC0_COUNT8.DBGCTRL.SetBits(sam.TC_COUNT8_DBGCTRL_DBGRUN) | |
sam.TC0_COUNT8.INTFLAG.Set(0x33) | |
for sam.TC0_COUNT8.SYNCBUSY.HasBits(0x80 | 0x40 | 0x10 | 0x0F) { | |
} | |
sam.TC0_COUNT8.CTRLA.Set(1 << 1) // ENABLE | |
for sam.TC0_COUNT8.SYNCBUSY.HasBits(0x02) { | |
} | |
return nil | |
} | |
func setBrightness(brightness uint8) { | |
if brightness < maxBrightness { | |
currentBrightness = brightness | |
} | |
sam.TC0_COUNT8.CC[0].Set(currentBrightness) | |
for sam.TC0_COUNT8.SYNCBUSY.HasBits(0x40) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TC0 + EVSYS + CCL を用いて LCD_BACKLIGHT の明るさ制御 (ON / OFF の Duty 制御) を行う例。
元のソースは以下。
https://github.com/ciniml/WioTerminal_BackLight