Skip to content

Commit

Permalink
Merge pull request #334 from Remus7/master
Browse files Browse the repository at this point in the history
Real Time Clock support and examples
  • Loading branch information
ppannuto authored Oct 18, 2023
2 parents 94f572c + 8bcf09e commit 63400a7
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/rtc_app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Makefile for user application

# Specify this directory relative to the current application.
TOCK_USERLAND_BASE_DIR = ../../

# Which files to compile.
C_SRCS := $(wildcard *.c)

# Include userland master makefile. Contains rules and flags for actually
# building the application.
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk
31 changes: 31 additions & 0 deletions examples/rtc_app/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <rtc.h>
#include <stdio.h>
#include <timer.h>

int main(void){
// Initialises a date struct with a certain timestamp
struct Date date = {
.year = 2023,
.month = JANUARY,
.day = 1,

.day_of_week = MONDAY,
.hour = 12,
.minute = 30,
.seconds = 1
};

set_date(&date);
// Clock has a small delay before starting to count seconds
delay_ms(2000);

while (1) {
get_date(&date);
printf("Date: {year: %d, month: %d, day: %d, day_of_week: %d, hour: %d, minute: %d, seconds: %d}\n\n", date.year,
date.month, date.day, date.day_of_week, date.hour, date.minute, date.seconds);

// This delay uses an underlying timer in the kernel
delay_ms(1000);
}
return 0;
}
48 changes: 48 additions & 0 deletions libtock/rtc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "rtc.h"
#include <stdio.h>
#include <stdlib.h>

int date, time;

static void rtc_cb(int status __attribute__ ((unused)),
int upcall1,
int upcall2,
void* ud __attribute__ ((unused))){
date = upcall1;
time = upcall2;
}

int get_date(struct Date *put_date){
subscribe_return_t sub = subscribe(DRIVER_NUM_RTC, 0, rtc_cb, malloc(sizeof(struct DateTime)));
if (!sub.success) {
return tock_status_to_returncode(sub.status);
}

syscall_return_t rval = command(DRIVER_NUM_RTC, 1, 0, 0);
if (!(rval.type == 128)) {
printf("%d", rval.type);
}

struct Date date_result = {
.year = date % (1 << 21) / (1 << 9),
.month = date % (1 << 9) / (1 << 5),
.day = date % (1 << 5),

.day_of_week = time % (1 << 20) / (1 << 17),
.hour = time % (1 << 17) / (1 << 12),
.minute = time % (1 << 12) / (1 << 6),
.seconds = time % (1 << 6)
};

*put_date = date_result;
return tock_command_return_novalue_to_returncode(rval);
}

int set_date(const struct Date *set_date){
date = set_date->year * (1 << 9) + set_date->month * (1 << 5) + set_date->day;
time = set_date->day_of_week *
(1 << 17) + set_date->hour * (1 << 12) + set_date->minute * (1 << 6) + set_date->seconds;

syscall_return_t rval = command(DRIVER_NUM_RTC, 2, date, time);
return tock_command_return_novalue_to_returncode(rval);
}
66 changes: 66 additions & 0 deletions libtock/rtc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#pragma once

#include "tock.h"

#ifdef __cplusplus
extern "C" {
#endif

#define DRIVER_NUM_RTC 0x00090007

#define JANUARY 1
#define FEBRUARY 2
#define MARCH 3
#define APRIL 4
#define MAY 5
#define JUNE 6
#define JULY 7
#define AUGUST 8
#define SEPTEMBER 9
#define OCTOBER 10
#define NOVEMBER 11
#define DECEMBER 12

#define SUNDAY 0
#define MONDAY 1
#define TUESDAY 2
#define WENSDAY 3
#define THURSDAY 4
#define FRIDAY 5
#define SATURDAY 6

// Date structure to store date and time
struct Date{
int year;
int month;
int day;
int day_of_week;
int hour;
int minute;
int seconds;
};

// DateTime codifies Date structure into two u32 (int) numbers
// date: first number (year, month, day_of_the_month):
// - last 5 bits store the day_of_the_month
// - previous 4 bits store the month
// - previous 12 bits store the year
// time: second number (day_of_the_week, hour, minute, seconds):
// - last 6 bits store the seconds
// - previous 6 store the minute
// - previous 5 store the hour
// - previous 3 store the day_of_the_week
struct DateTime{
int date;
int time;
};

// Fetches current date from registers and writes it to get_date
int get_date(struct Date *get_date);

// Writes set_date into the registers
int set_date(const struct Date *set_date);

#ifdef __cplusplus
}
#endif
Binary file removed simple-ble/build/cortex-m0/simple-ble.a
Binary file not shown.
Binary file removed simple-ble/build/cortex-m3/simple-ble.a
Binary file not shown.
Binary file removed simple-ble/build/cortex-m4/simple-ble.a
Binary file not shown.
Binary file removed simple-ble/build/cortex-m7/simple-ble.a
Binary file not shown.

0 comments on commit 63400a7

Please sign in to comment.