Skip to content

Commit

Permalink
Add support for setting and reading timer (#8)
Browse files Browse the repository at this point in the history
* Add ioctl command for reading and writing BM8563_TIMER_CONTROL
* Add ioctl command for reading and writing BM8563_TIMER
* Add unit tests for reading and writing timer

uint8_t count = 10;
uint8_t control = BM8563_TIMER_ENABLE | BM8563_TIMER_1HZ;

bm8563_ioctl(&bm, BM8563_TIMER_WRITE, &count);
bm8563_ioctl(&bm, BM8563_TIMER_CONTROL_WRITE, &control);
  • Loading branch information
tuupola committed Jun 9, 2020
1 parent d16866a commit 4cf248d
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 timers ([#8](https://github.com/tuupola/bm8563/pull/8)).
- 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)).
Expand Down
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,44 @@ bm8563_init(&bm);
bm8563_ioctl(&bm, BM8563_ALARM_READ, &rtc_alarm);
```
## Set RTC timer
```c
#include "bm8563.h"
#include "user_i2c.h"
uint8_t count, control;
bm8563_t bm;
/* Add pointers to user provided functions. */
bm.read = &user_i2c_read;
bm.write = &user_i2c_write;
bm8563_init(&bm);
/* Create a 10 second timer. */
count = 10;
control = BM8563_TIMER_ENABLE | BM8563_TIMER_1HZ;
bm8563_ioctl(&bm, BM8563_TIMER_WRITE, &count);
bm8563_ioctl(&bm, BM8563_TIMER_CONTROL_WRITE, &control);
/* Prints "Timer!" every 10 seconds. */
while (1) {
bm8563_ioctl(&bm, BM8563_CONTROL_STATUS2_READ, &control);
/* Check for timer flag. */
if (control & BM8563_TF) {
printf("Timer!\n");
/* Clear timer flag. */
tmp &= ~BM8563_TF;
bm8563_ioctl(&bm, BM8563_CONTROL_STATUS2_WRITE, &tmp);
}
}
```


## License

The MIT License (MIT). Please see [License File](LICENSE.txt) for more information.
4 changes: 4 additions & 0 deletions bm8563.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,17 @@ bm8563_err_t bm8563_ioctl(const bm8563_t *bm, int16_t command, void *buffer)

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

case BM8563_CONTROL_STATUS1_WRITE:
case BM8563_CONTROL_STATUS2_WRITE:
case BM8563_TIMER_CONTROL_WRITE:
case BM8563_TIMER_WRITE:
return bm->write(
bm->handle, BM8563_ADDRESS, reg, (uint8_t *)buffer, 1
);
Expand Down
12 changes: 12 additions & 0 deletions bm8563.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,25 @@ extern "C" {
#define BM8563_ALARM_NONE (0xff)
#define BM8563_ALARM_SIZE (0x04)

#define BM8563_TIMER_CONTROL (0x0e)
#define BM8563_TIMER_ENABLE (0b10000000)
#define BM8563_TIMER_4_096KHZ (0b00000000)
#define BM8563_TIMER_64HZ (0b00000001)
#define BM8563_TIMER_1HZ (0b00000010)
#define BM8563_TIMER_1_60HZ (0b00000011)
#define BM8563_TIMER (0x0f)

/* 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)
#define BM8563_TIMER_CONTROL_READ (0x0e00)
#define BM8563_TIMER_CONTROL_WRITE (0x0e01)
#define BM8563_TIMER_READ (0x0f00)
#define BM8563_TIMER_WRITE (0x0f01)

/* Status codes. */
#define BM8563_ERROR_NOTTY (-1)
Expand Down
24 changes: 24 additions & 0 deletions tests/unit.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ TEST should_read_and_write_alarm(void) {
PASS();
}

TEST should_read_and_write_timer(void) {
uint8_t count = 10;
uint8_t reg = BM8563_TIMER_ENABLE | BM8563_TIMER_1HZ;

bm8563_t bm;
bm.read = &mock_i2c_read;
bm.write = &mock_i2c_write;

ASSERT(BM8563_OK == bm8563_init(&bm));
ASSERT(BM8563_OK == bm8563_ioctl(&bm, BM8563_TIMER_WRITE, &count));
ASSERT(BM8563_OK == bm8563_ioctl(&bm, BM8563_TIMER_CONTROL_WRITE, &reg));

count = 0;
reg = 0;

ASSERT(BM8563_OK == bm8563_ioctl(&bm, BM8563_TIMER_READ, &count));
ASSERT(BM8563_OK == bm8563_ioctl(&bm, BM8563_TIMER_CONTROL_READ, &reg));
ASSERT(10 == count);
ASSERT((BM8563_TIMER_ENABLE | BM8563_TIMER_1HZ) == reg);

PASS();
}

GREATEST_MAIN_DEFS();

int main(int argc, char **argv) {
Expand All @@ -168,6 +191,7 @@ int main(int argc, char **argv) {
RUN_TEST(should_read_and_write_time);
RUN_TEST(should_handle_century);
RUN_TEST(should_read_and_write_alarm);
RUN_TEST(should_read_and_write_timer);

GREATEST_MAIN_END();
}

0 comments on commit 4cf248d

Please sign in to comment.