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

Implement get_fattime #160

Merged
merged 19 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion src/commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ command_func_t* const cmd_table[NUM_PRIMARY_CMDS + 1][NUM_SECONDARY_CMDS_MAX + 1
},
{ // 12 SENSORS
// 0 1 RTC_SETTIME 2 RTC_SETDIGIT 3 RTC_GETTIME
Undefined, RTC_SetTime, RTC_SetDigit, RTC_GetTime,
Undefined, RTC_CmdSetTime, RTC_SetDigit, RTC_CmdGetTime,
Hrushi20 marked this conversation as resolved.
Show resolved Hide resolved
// 4 RTC_GETDIGIT 5 HCSR04 6 AM2302 7 BMP180
RTC_GetDigit, Unimplemented, Unimplemented, Unimplemented,
// 8 TSL2591 9 TCD1304 10 11
Expand Down
97 changes: 84 additions & 13 deletions src/helpers/rtc.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdint.h>
#include <time.h>

#include "../bus/i2c/i2c.h"
#include "../bus/uart/uart.h"
Expand All @@ -8,24 +9,72 @@
#include "light.h"
#include "rtc.h"

response_t RTC_SetTime(void) {

uint8_t data_to_bcd(uint8_t data){
Hrushi20 marked this conversation as resolved.
Show resolved Hide resolved
uint8_t bcd = data;

if(data >= 10){
uint8_t left = left / 10;
uint8_t right = right % 10;
bcd = (left << 4) | right;
}
return bcd;
}

uint8_t bcd_to_data(uint8_t bcd){
Hrushi20 marked this conversation as resolved.
Show resolved Hide resolved
uint8_t right = (0xF & bcd);
uint8_t left = (bcd >> 4) & 0xF;

left *= 10;
left <<=4;
return left & right;
}

response_t RTC_SetTime(uint32_t *unix_timestamp) {
Hrushi20 marked this conversation as resolved.
Show resolved Hide resolved

time_t timestamp = (time_t) *unix_timestamp;
struct tm *tm_info;

tm_info = gmtime(&timestamp);
uint8_t sec = tm_info->tm_sec;
uint8_t min = tm_info->tm_min;
uint8_t hours = tm_info->tm_hour;
uint8_t day = tm_info->tm_wday + 1;
uint8_t date = tm_info->tm_mday;
uint8_t month = tm_info->tm_mon + 1;

if(tm_info->tm_year < 2000 || tm_info->tm_year > 2099)
Hrushi20 marked this conversation as resolved.
Show resolved Hide resolved
return ARGUMENT_ERROR;

uint8_t year = (tm_info->tm_year + 1900) % 100;

if(sec == 60)
sec = 0;

// Default 24 hrs format.
uint8_t buffer[9];
buffer[0] = DS1307_DATA_REG_SECONDS;
buffer[1] = UART1_Read() & 0x7F; // seconds
buffer[2] = UART1_Read(); // minutes
buffer[3] = UART1_Read(); // hours
buffer[4] = UART1_Read(); // day
buffer[5] = UART1_Read(); // date
buffer[6] = UART1_Read(); // month
buffer[7] = UART1_Read(); // year
buffer[8] = UART1_Read(); // control
buffer[1] = data_to_bcd(sec) & ~(1<<7); // seconds
Hrushi20 marked this conversation as resolved.
Show resolved Hide resolved
buffer[2] = data_to_bcd(min); // minutes
buffer[3] = (data_to_bcd(hours) & (1<<5)); // hours (hrs format)
buffer[4] = data_to_bcd(day); // day
buffer[5] = data_to_bcd(date); // date
buffer[6] = data_to_bcd(month); // month
buffer[7] = data_to_bcd(year); // year
buffer[8] = 0; // control

I2C_InitializeIfNot(I2C_BAUD_RATE_100KHZ, I2C_ENABLE_INTERRUPTS);

return I2C_BulkWrite(buffer, 9, DS1307_I2C_DEVICE_ADDRESS);
}

response_t RTC_CmdSetTime(void) {
uint32_t unix_timestamp = UART1_read_u32();

response_t res = RTC_SetTime(&unix_timestamp);
UART1_Write(res);
Hrushi20 marked this conversation as resolved.
Show resolved Hide resolved
return res;
}

response_t RTC_SetDigit(void) {

uint8_t buffer[2];
Expand All @@ -37,20 +86,42 @@ response_t RTC_SetDigit(void) {
return I2C_BulkWrite(buffer, 2, DS1307_I2C_DEVICE_ADDRESS);
}

response_t RTC_GetTime(void) {
response_t RTC_GetTime(uint32_t* unix_timestamp) {

uint8_t buffer[7];
struct tm tm_info;

I2C_InitializeIfNot(I2C_BAUD_RATE_100KHZ, I2C_ENABLE_INTERRUPTS);

if(I2C_BulkRead(DS1307_DATA_REG_SECONDS, DS1307_I2C_DEVICE_ADDRESS, buffer, 7) == SUCCESS) {
uint8_t i;
for (i = 0; i < sizeof(buffer); i++) UART1_Write(buffer[i]);

// Need to convert from bcd to int.
tm_info.tm_sec = bcd_to_data(buffer[0]);
tm_info.tm_min = bcd_to_data(buffer[1]);
tm_info.tm_hour = bcd_to_data(buffer[2]);
tm_info.tm_wday = bcd_to_data(buffer[3] - 1);
tm_info.tm_mday = bcd_to_data(buffer[4]);
tm_info.tm_mon = bcd_to_data(buffer[5] - 1);
tm_info.tm_year = bcd_to_data(2000 + buffer[6]);

tm_info.tm_sec = tm_info.tm_sec & ~(1 << 7);

uint32_t timestamp = (uint32_t) mktime(&tm_info);
*unix_timestamp = timestamp;
} else return FAILED;

return SUCCESS;
}

response_t RTC_CmdGetTime(void){
uint32_t unix_timestamp;
response_t res = RTC_GetTime(&unix_timestamp);

// What if error occurs here, Returns fail.
UART1_write_u32(unix_timestamp);
return res;
}

response_t RTC_GetDigit(void) {

uint8_t buffer[1];
Expand Down
11 changes: 7 additions & 4 deletions src/helpers/rtc.h
Hrushi20 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ extern "C" {
*
* @return SUCCESS, FAILED
*/
response_t RTC_SetTime(void);

response_t RTC_SetTime(uint32_t* unix_timestamp);

response_t RTC_CmdSetTime(void);

/**
* @brief Updates a single time parameter in DS1307 real-time clock
*
Expand Down Expand Up @@ -97,8 +99,9 @@ extern "C" {
*
* @return SUCCESS, FAILED
*/
response_t RTC_GetTime(void);

response_t RTC_GetTime(uint32_t* unix_timestamp);

response_t RTC_CmdGetTime(void);
/**
* @brief Fetch a single time parameter from DS1307 real-time clock
*
Expand Down