Categorygithub.com/cleroux/rtc
modulepackage
0.1.1
Repository: https://github.com/cleroux/rtc.git
Documentation: pkg.go.dev

# README

rtc

Go module for using a hardware Real-Time Clock (RTC) device in Linux.

Installation

$ go get github.com/cleroux/rtc

Usage

rtc.NewTicker() and rtc.NewTimer() provide high-level interfaces inspired by Go's time.NewTicker() and time.NewTimer() with the obvious difference being that the rtc functions trigger directly from the RTC's hardware interrupts.

The following example creates a ticker that fires at 2 Hz.

t, err := rtc.NewTicker("/dev/rtc", 2)
if err != nil {
  panic(err)
}
defer t.Stop()

for {
  select {
  case tick := <-t.Chan:
    fmt.Printf("Tick.  Frame:%d Time:%v Delta:%v Missed:%d\n", tick.Frame, tick.Time, tick.Delta, tick.Missed)
  }
}

The following example sets an alarm for 5 seconds in the future and waits for the alarm to fire.

t, err := rtc.NewTimer("/dev/rtc", time.Now().Add(time.Second * 5))
if err != nil {
  panic(err)
}
defer t.Stop()

select {
case alarm := <-t.Chan:
  fmt.Printf("Alarm.  Time:%v\n", alarm.Time)
}

If more flexible programming of the RTC is needed, rtc.NewRTC() instantiates an object that exposes all RTC functionality. The RTC device file is kept open until Close() is called.

c, err := rtc.NewRTC("/dev/rtc")
if err != nil {
  return err
}
defer c.Close()

t, err := c.Time()

Lastly, for convenience, static utility functions are also provided. These functions are ideal when just a single function or operation is necessary because they open and close the RTC device. For example, if reading the RTC's time as in the following example:

t, err := rtc.Time("/dev/rtc")
if err != nil {
  panic(err)
}
fmt.Printf("Current time: %v\n", t)

References

[1] The Linux kernel user's and administrator's guide: Real Time Clock (RTC) Drivers for Linux
[2] rtc - Linux manual page

License

See the LICENSE file for license rights and limitations (MIT).

# Functions

Alarm returns the alarm time for the specified real-time clock device.
CancelWakeAlarm cancels the wake alarm for the specified real-time clock device.
Clocks returns a list of real-time clocks in the system.
Epoch reads the epoch from the specified real-time clock device.
Frequency returns the frequency of the specified real-time clock device.
NewRTC opens a real-time clock device.
No description provided by the author
TODO: Timer resolution limited to 1 second TODO: What to do if d < 1 second? TODO: Consider mimicking the time.After() patterns.
NewTimerAt creates a timer that will trigger an alarm at the given time.
SetAlarm sets the alarm time for the specified real-time clock device.
SetAlarmInterrupt enables or disables the alarm interrupt for the specified real-time clock device.
Epoch sets the epoch on the specified real-time clock device.
SetFrequency sets the periodic interrupt frequency of the specified real-time clock device.
SetPeriodicInterrupt enables or disables periodic interrupts for the specified real-time clock device.
SetTime sets the time for the specified real-time clock device.
SetUpdateInterrupt enables or disables the update interrupt for the specified real-time clock device.
SetWakeAlarm sets the wake alarm time for the specified real-time clock device.
Time reads the time from the specified real-time clock device.
WakeAlarm returns the current state of the wake alarm for the specified real-time clock device.

# Structs

No description provided by the author
No description provided by the author