先说排查出最终原因:ESP8266,貌似必须用  scl: D2   sda: D4引脚,如果用已定义好的D1,D2引脚 则会读取不到时间

以下代码是使用自带的i2csoft库读写Rx8025T,时钟芯片代码

以下读取到,逻辑分析仪情况

package main
 
import (
    "machine"
    "time"
    "tinygo.org/x/drivers/i2csoft"
    "led/lg"
    //"tinygo.org/x/drivers/delay"
)

const(
RX8025T_I2C_ADDR=0x32   //RX8025T  I2C地址,读写地址为左移1位  <<1,读取地址为0x65,写地址为0x64
RX8025T_REG_SECOND=0x00 // BCD value 00 -> 59
RX8025T_REG_MINUTE=0x01 // BCD value 00 -> 59
RX8025T_REG_HOUR= 0x02 // BCD value 00 -> 23
RX8025T_REG_WEEKDAY=0x03 // bit 0->6: sunday->saturday
RX8025T_REG_DAY=0x04 // BCD value 01 -> 31
RX8025T_REG_MONTH=0x05 // BCD value 01 -> 12
RX8025T_REG_YEAR=0x06 // BCD value 00 -> 99
RX8025T_REG_RAM=0x07 // R/W accessible for any data in the range from 00 h to FF h
RX8025T_REG_ALARM_MIN=0x08 // BCD value 00 -> 59
RX8025T_REG_ALARM_HOUR=0x09 // BCD value 00 -> 23
RX8025T_REG_ALARM_W_OR_D=0x0A // WEEK: bit 0->6: sunday->saturday
RX8025T_REG_TIMER_COUNTER0=0x0B // Fixed-cycle timer control registers to set the preset countdown value 
// for the fixed-cycle timer interrupt. (COUNTER1, COUNTER0) = 12bit number
RX8025T_REG_TIMER_COUNTER1=0x0C // When control register changes from 001h to 000h, the /INT pin goes to
RX8025T_REG_EXTEN =0x0D
RX8025T_EXTEN_TEST=0x80 // TEST bit. value should always be "0"
RX8025T_EXTEN_WADA= 0x40 // Week Alarm/Day Alarm bit. R/W to specify either WEEK or DAY 
// as the target of the alarm interrupt function. 0:WEEK, 1:DAY
RX8025T_EXTEN_USEL=0x20 // Update Interrupt Select bit. R/W specify either "second" 
// or "minute" as update interrupt, 0:second update, 1:minute update
RX8025T_EXTEN_TE=0x10 // Timer Enable bit. controls the start/stop setting for the fixed-cycle 
// timer interrupt function. 1:start, 0:stop
RX8025T_EXTEN_FSEL1=0x08 // FOUT frequency Select bits to set the FOUT frequency. (FSEL1, FSEL0)
RX8025T_EXTEN_FSEL0=0x04 //    0,0:32.768KHz, 0,1:1024Hz, 1,0:1Hz, 1,1:32.768KHz
RX8025T_EXTEN_TSEL1=0x02 // Timer Select bits to set the countdown period (source clock) for the 
// fixed-cycle timer interrupt (TSEL1, TSEL0)
RX8025T_EXTEN_TSEL0=0x01 //    0,0:4096Hz, 0,1:64Hz, 1,0:1Hz(per second), 1,1:1/60Hz(per minute)

RX8025T_RTC_STATUS= 0x0E
RX8025T_FLAG_UF= 0x20 // Update Flag. 0 -> 1 when  a time update interrupt event has
// occurred. 1 is retained until a 0 is written
RX8025T_FLAG_TF=0x10 // Timer Flag. 0 -> 1 when a fixed-cycle timer interrupt 
// event has occurred, 1 is retained until a 0 is written
RX8025T_FLAG_AF=0x08 // Alarm Flag. 0 -> 1 when an alarm interrupt event has occurred
// 1 is retained until a 0 is written
RX8025T_FLAG_VLF=0x02 // Voltage Low Flag. 0 -> 1 when data loss occurs, e.g. a supply
// voltage drop, 
RX8025T_FLAG_VDET=0x01 // Voltage Detection Flag. 0 -> 1 when stop the temperature compensation
// such as due to a supply voltage drop, 1 is retained until a 0 is written

RX8025T_REG_CONTROL=0x0F
RX8025T_CONTR_CSEL1=0x80 // Compensation interval (CSEL1, CSEL0)
RX8025T_CONTR_CSEL0=0x40 //    0,0:0.5s, 0,1:2s(default), 1,0:10s, 1,1:30s
RX8025T_CONTR_UIE=0x20 // Update Interrupt Enable, 0:off, 1:on
RX8025T_CONTR_TIE=0x10 // Timer Interrupt Enable
RX8025T_CONTR_AIE=0x08 // Alarm Interrupt Enable
RX8025T_CONTR_RESET=0x01 // Writing a "1" to this bit stops the counter operation and
)
var tt=[]byte{0x30,0x59,0x12,0x02,0x09,0x08,0x22} //秒 分 时 周 天 月 年
var tt1=[]byte{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
var r=make([]byte, 7)
var w=make([]byte, 2)

var i2c=i2csoft.New(machine.D2, machine.D4)//!!!必须是D2,D4.。。。。

func main(){
    i2c.Configure(i2csoft.I2CConfig{
        Frequency: 400e3,//400e3,
    })
    i2c.SetBaudRate(115200)
    RX8025T_Init()
    RX8025T_SetTime()

    for{
        RX8025T_GetTime()
       delay(1000)
    }
}
func toBCD(x int) byte{
var n0,n1 byte
n0=byte(x%10)
n1=byte((x/10)%10)
return (n1 << 4) | n0
}
func RX8025T_Write(reg uint8,data []byte){
    legacy.WriteRegister(i2c,0x32,reg,data)
}
// 初始化, 清除标志位, 关闭中断
func RX8025T_Init(){
    RX8025T_Write(0x0E, []byte{0x00})
    RX8025T_Write(0x0f, []byte{0x40})
    RX8025T_Write(0x0D, []byte{0x42})

}
 
// 读取时间
func RX8025T_GetTime(){
  i2c.Tx(0x32,[]byte{0x00},r)
   //i2c.Tx(0x32,nil,r)
   println(r[5])
   //println(toBCD(2023))

}
 
// 设置时间
func RX8025T_SetTime(){
    RX8025T_Write(0x00, tt)
    RX8025T_Write(0x08, tt1)

}

func delay(t time.Duration){
time.Sleep(time.Millisecond * t)
}

 

以下为自己百度看了很多源码,根据C51代码移植来的

package main
 
import (
    "machine"
    "time"
"tinygo.org/x/drivers/delay"
)
const(
scl=machine.D2
sda=machine.D4
 
    WRITE=0x64
READ=0x65
)
var EEbuf=[]byte{0x42,0x00,0x40}
 
func main() {
scl.Configure(machine.PinConfig{Mode: machine.PinOutput})
//var tt=[]byte{0x30,0x59,0x12,0x02,0x09,0x08,0x22}
//var tt1=[]byte{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}
writeone1(0x0E)
readone1()
 
writeone(0x0D,0x00)
writeone(0x0E,0x00)
writeone(0x0F,0x40)
 
//delay.Sleep(time.Millisecond * 1)
 
EEbuf=[]byte{0x30,0x59,0x12,0x02,0x09,0x08,0x22}
writeEEone(0x00,7)
 
for{
readEEone(0x00,7)
println(EEbuf[0])
time.Sleep(time.Millisecond * 1000)
}
}
 
func wait(){
delay.Sleep(5* time.Microsecond)
}
//i2c开始
func IC_start(){ 
sda.Configure(machine.PinConfig{Mode: machine.PinOutput}) 
sda.High() 
wait()  
scl.High() /** Send Start **/ 
wait()
sda.Low() 
wait()
scl.Low() 
wait()
//i2c结束
func IC_stop(){
sda.Configure(machine.PinConfig{Mode: machine.PinOutput})  
sda.Low() 
wait()
scl.High() 
wait()  
sda.High() 
//i2c等待响应
func IC_wait_ack() uint8{
var r uint8
sda.Configure(machine.PinConfig{Mode: machine.PinInput})
scl.High() 
    wait()
if sda.Get(){r=0}else{r=1}
scl.Low() 
wait()
    return r
}
//i2c响应
func IC_ack(){
    sda.Configure(machine.PinConfig{Mode: machine.PinOutput})  
    sda.Low() 
scl.High() 
    wait()
scl.Low() 
    wait()
}
//i2c无响应
func IC_nack(){
    sda.Configure(machine.PinConfig{Mode: machine.PinOutput})  
    sda.High()
    wait()
scl.High() 
    wait()
scl.Low() 
 
}
//写一个字节
func IC_WriteByte(wdata uint8){ 
 var i uint8
 sda.Configure(machine.PinConfig{Mode: machine.PinOutput}) 
for i=0;i<8;i++ { 
if (wdata&0x80)>0 {sda.High()}else{sda.Low()}
scl.High()
wait()
scl.Low()
wait()
wdata<<=1
 
}
//读取一个字节
func IC_ReadByte() uint8{
var i,IC_data uint8
IC_data=0
 for i=0;i<8;i++ {
scl.High()
wait()
IC_data<<=1 
sda.Configure(machine.PinConfig{Mode: machine.PinInput}) 
if sda.Get() {
IC_data++
}
scl.Low()
wait() 
 } 
 return IC_data
 
func readone1() uint8{ 
 var data uint8
IC_start()
IC_WriteByte(READ) 
IC_wait_ack()
data=IC_ReadByte()
IC_nack()
IC_stop()
return data
 
func writeone1(addr uint8) int8{ 
IC_start()
IC_WriteByte(WRITE)
IC_wait_ack()
IC_WriteByte(addr)
IC_wait_ack()
IC_stop()
return 0
}
 
 
 
func readone(addr uint8) uint8{ 
var data uint8
IC_start()
IC_WriteByte(WRITE)
IC_wait_ack()
IC_WriteByte(addr)
IC_wait_ack()
IC_WriteByte(READ) 
IC_wait_ack()
data=IC_ReadByte()
IC_nack()
IC_stop()
return data
 
func writeone(addr uint8,data uint8) int8{ 
IC_start()
IC_WriteByte(WRITE)
IC_wait_ack()
IC_WriteByte(addr)
IC_wait_ack()
IC_WriteByte(data)
IC_wait_ack()
IC_stop()
return 0
}
 
func readEEone(addr uint8, num uint8) int8{ 
 var i uint8
IC_start()
IC_WriteByte(WRITE)
IC_wait_ack()
IC_WriteByte(addr)
IC_wait_ack()
IC_stop()
 
IC_start() 
IC_WriteByte(READ) 
IC_wait_ack()
 
 for i=0;i<num;i++ { 
EEbuf[i]=IC_ReadByte()
if i != num-1 {IC_ack()}
    }
IC_nack()
IC_stop()
return 0
 
func writeEEone(addr uint8,num uint8) int8{ 
var i uint8
IC_start()
IC_WriteByte(WRITE)
IC_wait_ack()
IC_WriteByte(addr)
IC_wait_ack()
for i=0;i<num;i++ { 
IC_WriteByte(EEbuf[i])
IC_wait_ack()
 } 
IC_stop()
return 0
}