avr: time runs too slow after machine.Timer0.Configure changes the prescaler

Đang mở
#6,071 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
arduino, go
Lĩnh vực
backend, embedded-iot

Hướng nghiên cứu

Start in src/runtime/runtime_avr.go at currentNanosecondsInTick and inspect how the TCCR0B clock select bits map to the Timer0 prescaler. The reproduction uses machine.Timer0.Configure and time.Sleep on ATmega328P; run it on an AVR target and compare measured sleep duration at prescaler 1 and 64. Done means runtime time functions keep accurate durations after changing the prescaler.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

On ATmega328P and ATmega1280/2560, the runtime monotonic clock uses Timer0. machine.Timer0.Configure can change the Timer0 prescaler, and then it calls adjustMonotonicTimer to update the tick length. The tick length is only correct with prescaler 1, which is the runtime default. With any other PWM period, time.Sleep, time.Now and time.Since run too slow.

Steps to reproduce
package main

import (
	"machine"
	"time"
)

func main() {
	machine.D8.Configure(machine.PinConfig{Mode: machine.PinOutput})
	machine.Timer0.Configure(machine.PWMConfig{Period: 1024000}) // prescaler 64
	for {
		machine.D8.High()
		time.Sleep(10 * time.Millisecond)
		machine.D8.Low()
		time.Sleep(10 * time.Millisecond)
	}
}

tinygo flash -target arduino (TinyGo 0.44.0), then measure D8.

Expected

D8 changes state every 10 ms.

Actual

D8 changes state every 1920 ms. Measured with a logic analyser on an ATmega328P (Freetronics Eleven, Uno clone) at 16 MHz:

PWM period Prescaler Sleep(10ms) measured Error
16 µs (default) 1 10.09 ms none
1024 µs 64 1920 ms 192× too slow
4096 µs 256 about 10 s 1024× too slow

The PWM output on D6 had the correct periods (16.1 µs, 1024.0 µs, 4095.8 µs), so the hardware runs at the requested prescaler. Only the runtime's time is wrong.

Cause

currentNanosecondsInTick in src/runtime/runtime_avr.go uses the CS0[2:0] select bits of TCCR0B as the prescaler divider, and it divides by that value where it must multiply. For CS0 = 3 (prescaler 64) it uses 1/3 where it must use 64, so the tick length is 192 times too small. The CS0 values are in the TCCR0B "Clock Select Bit Description" table of the ATmega328P datasheet. Only CS0 = 1 (prescaler 1) gives the correct value.

When this is fixed, #6070 can also occur.

Ngôn ngữ chính
Go
Star
17.8k
Fork
1.1k
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
380

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của tinygo-org/tinygo

Tất cả issue của tinygo-org/tinygo

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.