ds3231

package
v0.34.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 15, 2025 License: BSD-3-Clause Imports: 5 Imported by: 21

Documentation

Overview

Package ds3231 provides a driver for the DS3231 RTC

Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf

Index

Constants

View Source
const (
	REG_TIMEDATE = 0x00
	REG_ALARMONE = 0x07
	REG_ALARMTWO = 0x0B

	REG_CONTROL = 0x0E
	REG_STATUS  = 0x0F
	REG_AGING   = 0x10

	REG_TEMP = 0x11

	REG_ALARMONE_SIZE = 4
	REG_ALARMTWO_SIZE = 3

	// DS3231 Control Register Bits
	A1IE  = 0
	A2IE  = 1
	INTCN = 2
	RS1   = 3
	RS2   = 4
	CONV  = 5
	BBSQW = 6
	EOSC  = 7

	// DS3231 Status Register Bits
	A1F     = 0
	A2F     = 1
	BSY     = 2
	EN32KHZ = 3
	OSF     = 7

	AlarmFlag_Alarm1    = 0x01
	AlarmFlag_Alarm2    = 0x02
	AlarmFlag_AlarmBoth = 0x03

	None          Mode = 0
	BatteryBackup Mode = 1
	Clock         Mode = 2
	AlarmOne      Mode = 3
	AlarmTwo      Mode = 4
	ModeAlarmBoth Mode = 5
)

Registers

View Source
const Address = 0x68

The I2C address which this device listens to.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alarm1Mode added in v0.34.0

type Alarm1Mode uint8

Alarm1 Modes define which parts of the set alarm time has to match the current timestamp of the clock device for alarm1 to fire

const (
	// Alarm1 fires every second
	A1_PER_SECOND Alarm1Mode = 0x0F
	// Alarm1 fires when the seconds match
	A1_SECOND Alarm1Mode = 0x0E
	// Alarm1 fires when both seconds and minutes match
	A1_MINUTE Alarm1Mode = 0x0C
	// Alarm1 fires when seconds, minutes and hours match
	A1_HOUR Alarm1Mode = 0x08
	// Alarm1 fires when seconds, minutes, hours and the day of the month match
	A1_DATE Alarm1Mode = 0x00
	// Alarm1 fires when seconds, minutes, hours and the day of the week match
	A1_DAY Alarm1Mode = 0x10
)

type Alarm2Mode added in v0.34.0

type Alarm2Mode uint8

Alarm2 Modes define which parts of the set alarm time has to match the current timestamp of the clock device for alarm2 to fire.

Alarm2 only supports matching down to the minute unlike alarm1 which supports matching down to the second.

const (
	// Alarm2 fires every minute
	A2_PER_MINUTE Alarm2Mode = 0x07
	// Alarm2 fires when the minutes match
	A2_MINUTE Alarm2Mode = 0x06
	// Alarm2 fires when both minutes and hours match
	A2_HOUR Alarm2Mode = 0x04
	// Alarm2 fires when minutes, hours and the day of the month match
	A2_DATE Alarm2Mode = 0x00
	// Alarm2 fires when minutes, hours and the day of the week match
	A2_DAY Alarm2Mode = 0x08
)

type Device

type Device struct {
	Address uint16
	// contains filtered or unexported fields
}

Device wraps an I2C connection to a DS3231 device.

func New

func New(bus drivers.I2C) Device

New creates a new DS3231 connection. The I2C bus must already be configured.

This function only creates the Device object, it does not touch the device.

func (*Device) ClearAlarm1 added in v0.34.0

func (d *Device) ClearAlarm1() error

ClearAlarm1 clears status of alarm1

func (*Device) ClearAlarm2 added in v0.34.0

func (d *Device) ClearAlarm2() error

ClearAlarm2 clears status of alarm2

func (*Device) Configure

func (d *Device) Configure() bool

Configure sets up the device for communication

func (*Device) GetSqwPinMode added in v0.34.0

func (d *Device) GetSqwPinMode() SqwPinMode

GetSqwPinMode returns the current square wave output frequency

func (*Device) IsAlarm1Fired added in v0.34.0

func (d *Device) IsAlarm1Fired() bool

IsAlarm1Fired returns true when alarm1 is firing

func (*Device) IsAlarm2Fired added in v0.34.0

func (d *Device) IsAlarm2Fired() bool

IsAlarm2Fired returns true when alarm2 is firing

func (*Device) IsEnabled32K added in v0.34.0

func (d *Device) IsEnabled32K() bool

IsEnabled32K returns true when the 32KHz output is enabled

func (*Device) IsEnabledAlarm1 added in v0.34.0

func (d *Device) IsEnabledAlarm1() bool

IsEnabledAlarm1 returns true when alarm1 is enabled

func (*Device) IsEnabledAlarm2 added in v0.34.0

func (d *Device) IsEnabledAlarm2() bool

IsEnabledAlarm2 returns true when alarm2 is enabled

func (*Device) IsRunning

func (d *Device) IsRunning() bool

IsRunning returns if the oscillator is running

func (*Device) IsTimeValid

func (d *Device) IsTimeValid() bool

IsTimeValid return true/false is the time in the device is valid

func (*Device) ReadAlarm1 added in v0.34.0

func (d *Device) ReadAlarm1() (dt time.Time, err error)

ReadAlarm1 returns the alarm1 time

func (*Device) ReadAlarm2 added in v0.34.0

func (d *Device) ReadAlarm2() (dt time.Time, err error)

ReadAlarm2 returns the alarm2 time

func (*Device) ReadTemperature

func (d *Device) ReadTemperature() (int32, error)

ReadTemperature returns the temperature in millicelsius (mC)

func (*Device) ReadTime

func (d *Device) ReadTime() (dt time.Time, err error)

ReadTime returns the date and time

func (*Device) SetAlarm1 added in v0.34.0

func (d *Device) SetAlarm1(dt time.Time, mode Alarm1Mode) error

SetAlarm1 sets alarm1 to the given time and mode

func (*Device) SetAlarm2 added in v0.34.0

func (d *Device) SetAlarm2(dt time.Time, mode Alarm2Mode) error

SetAlarm2 sets alarm2 to the given time and mode

func (*Device) SetEnabled32K added in v0.34.0

func (d *Device) SetEnabled32K(enable bool) error

SetEnabled32K sets the enabled status of the 32KHz output

func (*Device) SetEnabledAlarm1 added in v0.34.0

func (d *Device) SetEnabledAlarm1(enable bool) error

SetEnabledAlarm1 sets the enabled status of alarm1

func (*Device) SetEnabledAlarm2 added in v0.34.0

func (d *Device) SetEnabledAlarm2(enable bool) error

SetEnabledAlarm2 sets the enabled status of alarm2

func (*Device) SetRunning

func (d *Device) SetRunning(isRunning bool) error

SetRunning starts the internal oscillator

func (*Device) SetSqwPinMode added in v0.34.0

func (d *Device) SetSqwPinMode(mode SqwPinMode) error

SetSqwPinMode sets the square wave output mode to the given frequency

func (*Device) SetTime

func (d *Device) SetTime(dt time.Time) error

SetTime sets the date and time in the DS3231. The DS3231 hardware supports only a 2-digit year field, so the current year will be stored as an offset from the year 2000, which supports the year 2000 until 2100.

The DS3231 also supports a one-bit 'century' flag which is set by the chip when the year field rolls over from 99 to 00. The current code interprets this flag to be the year 2100, which appears to extend the range of years until the year 2200. However the DS3231 does not incorporate the 'century' flag in its leap year calculation, so it will incorrectly identify the year 2100 as a leap year, causing it to increment from 2100-02-28 to 2100-02-29 instead of 2100-03-01.

type Mode

type Mode uint8

type SqwPinMode added in v0.34.0

type SqwPinMode uint8

SQW Pin Modes

const (
	SQW_OFF  SqwPinMode = 0x1C
	SQW_1HZ  SqwPinMode = 0x00
	SQW_1KHZ SqwPinMode = 0x08
	SQW_4KHZ SqwPinMode = 0x10
	SQW_8KHZ SqwPinMode = 0x18
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL