Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ioctl commands for control status registers #5

Merged
merged 8 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ All notable changes to this project will be documented in this file, in reverse
## [0.5.0](https://github.com/tuupola/bm8563/compare/0.4.0...master) - unreleased

### Added
- Support for reading and writing alarms ([#4](https://github.com/tuupola/bm8563/pull/4)).
- Support for reading and writing control status registers ([#5](https://github.com/tuupola/bm8563/pull/5)).
- Support for reading and writing alarms ([#4](https://github.com/tuupola/bm8563/pull/4), [#5](https://github.com/tuupola/bm8563/pull/5)).
- Support for low voltage warnings ([#3](https://github.com/tuupola/bm8563/pull/3)).

## 0.4.0 - 2020-20-05
Expand Down
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ bm8563_write(&bm, &rtc);
#include "bm8563.h"
#include "user_i2c.h"
uint8_t tmp;
struct tm rtc_alarm;
bm8563_t bm;
Expand All @@ -93,9 +94,19 @@ rtc_alarm.tm_mday = BM8563_ALARM_NONE;
rtc_alarm.tm_wday = BM8563_ALARM_NONE;
bm8563_ioctl(&bm, BM8563_ALARM_SET, &rtc_alarm);
/* Later check if alarm is triggered. */
bm8563_ioctl(&bm, BM8563_CONTROL_STATUS2_READ, &tmp);
if (tmp & BM8563_AF) {
printf("Got alarm!");
};
/* And clear the alarm flag. */
tmp &= ~BM8563_AF;
bm8563_ioctl(&bm, BM8563_CONTROL_STATUS2_WRITE, &tmp);
```

## Read RTC alarm
## Read currently set RTC alarm

```c
#include <time.h>
Expand Down
51 changes: 33 additions & 18 deletions bm8563.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ bm8563_err_t bm8563_init(const bm8563_t *bm)
uint8_t clear = 0x00;
int32_t status;

status = bm->write(bm->handle, BM8563_ADDRESS, BM8563_CONTROL_STATUS_1, &clear, 1);
status = bm->write(bm->handle, BM8563_ADDRESS, BM8563_CONTROL_STATUS1, &clear, 1);
if (BM8563_OK != status) {
return status;
}
return bm->write(bm->handle, BM8563_ADDRESS, BM8563_CONTROL_STATUS_2, &clear, 1);
return bm->write(bm->handle, BM8563_ADDRESS, BM8563_CONTROL_STATUS2, &clear, 1);
}

bm8563_err_t bm8563_read(const bm8563_t *bm, struct tm *time)
Expand Down Expand Up @@ -172,15 +172,15 @@ bm8563_err_t bm8563_ioctl(const bm8563_t *bm, int16_t command, void *argument)

/* 0..59 */
if (BM8563_ALARM_NONE == time->tm_min) {
buffer[0] = 0x00;
buffer[0] = BM8563_ALARM_DISABLE;
} else {
buffer[0] = decimal2bcd(time->tm_min);
buffer[0] |= BM8563_ALARM_ENABLE;
}

/* 0..23 */
if (BM8563_ALARM_NONE == time->tm_hour) {
buffer[1] = 0x00;
buffer[1] = BM8563_ALARM_DISABLE;
} else {
buffer[1] = decimal2bcd(time->tm_hour);
buffer[1] &= 0b00111111;
Expand All @@ -189,7 +189,7 @@ bm8563_err_t bm8563_ioctl(const bm8563_t *bm, int16_t command, void *argument)

/* 1..31 */
if (BM8563_ALARM_NONE == time->tm_mday) {
buffer[2] = 0x00;
buffer[2] = BM8563_ALARM_DISABLE;
} else {
buffer[2] = decimal2bcd(time->tm_mday);
buffer[2] &= 0b00111111;
Expand All @@ -198,7 +198,7 @@ bm8563_err_t bm8563_ioctl(const bm8563_t *bm, int16_t command, void *argument)

/* 0..6 */
if (BM8563_ALARM_NONE == time->tm_mday) {
buffer[3] = 0x00;
buffer[3] = BM8563_ALARM_DISABLE;
} else {
buffer[3] = decimal2bcd(time->tm_wday);
buffer[3] &= 0b00000111;
Expand All @@ -223,39 +223,54 @@ bm8563_err_t bm8563_ioctl(const bm8563_t *bm, int16_t command, void *argument)
return status;
}

if (BM8563_ALARM_ENABLE & buffer[0]) {
if (BM8563_ALARM_DISABLE & buffer[0]) {
time->tm_min = BM8563_ALARM_NONE;
} else {
buffer[0] &= 0b01111111;
time->tm_min = bcd2decimal(buffer[0]);
} else {
time->tm_min = BM8563_ALARM_NONE;
}

/* 0..23 */
if (BM8563_ALARM_ENABLE & buffer[1]) {
if (BM8563_ALARM_DISABLE & buffer[1]) {
time->tm_hour = BM8563_ALARM_NONE;
} else {
buffer[1] &= 0b00111111;
time->tm_hour = bcd2decimal(buffer[1]);
} else {
time->tm_hour = BM8563_ALARM_NONE;
}

/* 1..31 */
if (BM8563_ALARM_ENABLE & buffer[2]) {
if (BM8563_ALARM_DISABLE & buffer[2]) {
time->tm_mday = BM8563_ALARM_NONE;
} else {
buffer[2] &= 0b00111111;
time->tm_mday = bcd2decimal(buffer[2]);
} else {
time->tm_mday = BM8563_ALARM_NONE;
}

/* 0..6 */
if (BM8563_ALARM_ENABLE & buffer[3]) {
if (BM8563_ALARM_DISABLE & buffer[3]) {
time->tm_wday = BM8563_ALARM_NONE;
} else {
buffer[3] &= 0b00000111;
time->tm_wday = bcd2decimal(buffer[3]);
} else {
time->tm_wday = BM8563_ALARM_NONE;
}

return BM8563_OK;
break;

case BM8563_CONTROL_STATUS1_READ:
case BM8563_CONTROL_STATUS2_READ:
return bm->read(
bm->handle, BM8563_ADDRESS, reg, (uint8_t *)argument, 1
);
break;

case BM8563_CONTROL_STATUS1_WRITE:
case BM8563_CONTROL_STATUS2_WRITE:
return bm->write(
bm->handle, BM8563_ADDRESS, reg, (uint8_t *)argument, 1
);
break;

}

return BM8563_ERROR_NOTTY;
Expand Down
11 changes: 8 additions & 3 deletions bm8563.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ extern "C" {
#include <time.h>

#define BM8563_ADDRESS (0x51)
#define BM8563_CONTROL_STATUS_1 (0x00)
#define BM8563_CONTROL_STATUS1 (0x00)
#define BM8563_TESTC (0b00001000)
#define BM8563_STOP (0b00100000)
#define BM8563_TEST1 (0b10000000)
#define BM8563_CONTROL_STATUS_2 (0x01)
#define BM8563_CONTROL_STATUS2 (0x01)
#define BM8563_TIE (0b00000001)
#define BM8563_AIE (0b00000010)
#define BM8563_TF (0b00000100)
Expand All @@ -66,13 +66,18 @@ extern "C" {
#define BM8563_HOUR_ALARM (0x0a)
#define BM8563_DAY_ALARM (0x0b)
#define BM8563_WEEKDAY_ALARM (0x0c)
#define BM8563_ALARM_ENABLE (0b10000000)
#define BM8563_ALARM_DISABLE (0b10000000)
#define BM8563_ALARM_ENABLE (0b00000000)
#define BM8563_ALARM_NONE (0xff)
#define BM8563_ALARM_SIZE (0x04)

/* IOCTL commands */
#define BM8563_ALARM_SET (0x0900)
#define BM8563_ALARM_READ (0x0901)
#define BM8563_CONTROL_STATUS1_READ (0x0000)
#define BM8563_CONTROL_STATUS1_WRITE (0x0001)
#define BM8563_CONTROL_STATUS2_READ (0x0100)
#define BM8563_CONTROL_STATUS2_WRITE (0x0101)

/* Status codes. */
#define BM8563_ERROR_NOTTY (-1)
Expand Down