Skip to content

Fix alarm registers, time masks, 32kHz output mask, and add NULL pointer checks - #3

Open
CupoMeridio wants to merge 2 commits into
eepj:mainfrom
CupoMeridio:main
Open

Fix alarm registers, time masks, 32kHz output mask, and add NULL pointer checks#3
CupoMeridio wants to merge 2 commits into
eepj:mainfrom
CupoMeridio:main

Conversation

@CupoMeridio

Copy link
Copy Markdown

Hi @eepj,

I've been using this library in some projects and found a few minor bugs and hardware register collisions.
Some of these fixes were identified and provided by my university, and then I consolidated them with
additional safety checks to improve the library's overall robustness.

Here is a summary of the improvements:

  1. Fixed Alarm 2 Register Collision:
    In DS3231_SetAlarm2Mode(), the code was reading Alarm 1 registers (DS3231_A1_...) instead of Alarm 2
    registers (DS3231_A2_...). This was corrupting Alarm 1 configuration when setting up Alarm 2. Corrected
    them to reference the proper Alarm 2 registers.
  2. Fixed Alarm Minutes & Seconds Masks:
    Updated the BCD encoding mask from 0x3f to 0x7f in DS3231_SetAlarm2Minute(), DS3231_SetAlarm1Second(), and
    DS3231_SetAlarm1Minute(). The previous 0x3f mask was stripping away the 6th bit (worth 40 in BCD format),
    preventing alarms from being set for any time value greater than 39 minutes/seconds.
  3. Fixed 32kHz Output Bitmask:
    In DS3231_Enable32kHzOutput(), updated the status register masking from 0xfb to 0xf7. The old mask was
    clearing the BSY bit (bit 2) instead of preparing the EN32KHZ bit (bit 3).
  4. Added Safety NULL Pointer Checks:
    Added a defensive check if (!_ds3231_ui2c) inside DS3231_SetRegByte and DS3231_GetRegByte. This prevents
    sudden microcontrollor crashes (HardFaults) if any driver function is called before DS3231_Init().

These changes are fully backward-compatible and do not break the existing examples.

Thanks for the library!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant