Skip to content

Commit 70b3ece

Browse files
aykevldeadprogram
authored andcommitted
rp2040: add machine.ReadTemperature
Replace ADCChannel.ReadTemperature() with a simple ReadTemperature function. Not all chips will have a temperature sensor that is read by sampling an ADC channel. The replacement ReadTemperature is simpler and more generic to other chip families. This breaks chips that were relying on the previous ReadTemperature method. I hope it won't break a lot of existing code. If it does, a fallback can be added.
1 parent 86ea216 commit 70b3ece

File tree

4 files changed

+39
-64
lines changed

4 files changed

+39
-64
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ endif
611611
@$(MD5SUM) test.hex
612612
$(TINYGO) build -size short -o test.hex -target=challenger-rp2040 examples/blinky1
613613
@$(MD5SUM) test.hex
614-
$(TINYGO) build -size short -o test.hex -target=trinkey-qt2040 examples/adc_rp2040
614+
$(TINYGO) build -size short -o test.hex -target=trinkey-qt2040 examples/temp
615615
@$(MD5SUM) test.hex
616616
# test pwm
617617
$(TINYGO) build -size short -o test.hex -target=itsybitsy-m0 examples/pwm

src/examples/adc_rp2040/adc.go

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/examples/temp/temp.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Read the internal temperature sensor of the chip.
2+
3+
package main
4+
5+
import (
6+
"fmt"
7+
"machine"
8+
"time"
9+
)
10+
11+
type celsius float32
12+
13+
func (c celsius) String() string {
14+
return fmt.Sprintf("%4.1f℃", c)
15+
}
16+
17+
func main() {
18+
for {
19+
temp := celsius(float32(machine.ReadTemperature()) / 1000)
20+
println("temperature:", temp.String())
21+
time.Sleep(time.Second)
22+
}
23+
}

src/machine/machine_rp2040_adc.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ const (
1717
adc0_CH ADCChannel = iota
1818
adc1_CH
1919
adc2_CH
20-
adc3_CH // Note: GPIO29 not broken out on pico board
21-
ADC_TEMP_SENSOR // Internal temperature sensor channel
20+
adc3_CH // Note: GPIO29 not broken out on pico board
21+
adcTempSensor // Internal temperature sensor channel
2222
)
2323

2424
// Used to serialise ADC sampling
@@ -75,19 +75,17 @@ func (a ADC) GetADCChannel() (c ADCChannel, err error) {
7575
return c, err
7676
}
7777

78-
// Configure sets the channel's associated pin to analog input mode or powers on the temperature sensor for ADC_TEMP_SENSOR.
78+
// Configure sets the channel's associated pin to analog input mode.
7979
// The powered on temperature sensor increases ADC_AVDD current by approximately 40 μA.
8080
func (c ADCChannel) Configure(config ADCConfig) error {
8181
if config.Reference != 0 {
8282
adcAref = config.Reference
8383
}
84-
if p, err := c.Pin(); err == nil {
85-
p.Configure(PinConfig{Mode: PinAnalog})
86-
}
87-
if c == ADC_TEMP_SENSOR {
88-
// Enable temperature sensor bias source
89-
rp.ADC.CS.SetBits(rp.ADC_CS_TS_EN)
84+
p, err := c.Pin()
85+
if err != nil {
86+
return err
9087
}
88+
p.Configure(PinConfig{Mode: PinAnalog})
9189
return nil
9290
}
9391

@@ -112,13 +110,16 @@ func (c ADCChannel) getVoltage() uint32 {
112110
}
113111

114112
// ReadTemperature does a one-shot sample of the internal temperature sensor and returns a milli-celsius reading.
115-
// Only works on the ADC_TEMP_SENSOR channel. aka AINSEL=4. Other channels will return 0
116-
func (c ADCChannel) ReadTemperature() (millicelsius uint32) {
117-
if c != ADC_TEMP_SENSOR {
118-
return
113+
func ReadTemperature() (millicelsius int32) {
114+
if rp.ADC.CS.Get()&rp.ADC_CS_EN == 0 {
115+
InitADC()
119116
}
117+
118+
// Enable temperature sensor bias source
119+
rp.ADC.CS.SetBits(rp.ADC_CS_TS_EN)
120+
120121
// T = 27 - (ADC_voltage - 0.706)/0.001721
121-
return (27000<<16 - (c.getVoltage()-706<<16)*581) >> 16
122+
return (27000<<16 - (int32(adcTempSensor.getVoltage())-706<<16)*581) >> 16
122123
}
123124

124125
// waitForReady spins waiting for the ADC peripheral to become ready.

0 commit comments

Comments
 (0)