Skip to content

Commit

Permalink
Receive RTC time from DAQ App
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeOxley authored Mar 20, 2024
1 parent 35d7387 commit 2b1e3c1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
10 changes: 6 additions & 4 deletions common/phal_F4_F7/rtc/rtc.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
// Registers are in line 10400 of the stm32f407xx.h file
uint8_t PHAL_configureRTC(RTC_timestamp_t* initial_time, bool force_time)
{

// Enable the LSI always (CSR is reset unlike the BDCR register)
RCC->CSR |= RCC_CSR_LSION;
// TODO: make timeout
while (!(RCC->CSR & RCC_CSR_LSIRDY));

// Check if already initialized
if (!force_time && RTC->ISR & RTC_ISR_INITS) return true;

Expand Down Expand Up @@ -42,10 +48,6 @@ uint8_t PHAL_configureRTC(RTC_timestamp_t* initial_time, bool force_time)
// Software reset backup power domain
// RCC->BDCR |= RCC_BDCR_BDRST;

RCC->CSR |= RCC_CSR_LSION;
// TODO: make timeout
while (!(RCC->CSR & RCC_CSR_LSIRDY));

RCC->BDCR &= ~(RCC_BDCR_RTCSEL); // Clear RTCSEL bits
RCC->BDCR |= RCC_BDCR_RTCSEL_1; // select LSI
RCC->BDCR |= RCC_BDCR_RTCEN;
Expand Down
2 changes: 2 additions & 0 deletions common/phal_F4_F7/rtc/rtc.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ typedef struct
RTC_time_t time;
} RTC_timestamp_t;

#define RTC_CONV_TO_BCD(v) (((v / 10) << 4) | (v % 10))

bool PHAL_getTimeRTC(RTC_timestamp_t *currentTimestamp);
uint8_t PHAL_configureRTC(RTC_timestamp_t* initial_time, bool force_time);

Expand Down
21 changes: 21 additions & 0 deletions source/daq/daq_hub/daq_hub.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static void eth_send_udp_periodic(void);
static void eth_rx_tcp_periodic(void);
static bool get_log_enable(void);
static void conv_tcp_frame_to_can_msg(tcp_can_frame_t *t, CanMsgTypeDef_t *c);
static void conv_tcp_frame_to_rtc(tcp_can_frame_t *t, RTC_timestamp_t *time);

// TODO: use can parse somehow to check for daq enable signal, etc

Expand Down Expand Up @@ -104,6 +105,7 @@ void daq_loop(void)
uint32_t tic;
uint32_t cont;
uint32_t last_hb_toggle_ms = 0;
RTC_timestamp_t time;
bool msg_valid;

while (PER == GREAT)
Expand Down Expand Up @@ -153,6 +155,11 @@ void daq_loop(void)
case TCP_CMD_STOP_LOG:
dh.log_enable_tcp = false;
break;
case TCP_CMD_SYNC_TIME:
// Extract time from message and configure RTC
conv_tcp_frame_to_rtc(rx_msg_a, &time);
PHAL_configureRTC(&time, true);
break;
default:
break;
}
Expand All @@ -175,6 +182,20 @@ void daq_loop(void)

}

static void conv_tcp_frame_to_rtc(tcp_can_frame_t *t, RTC_timestamp_t *time)
{
tcp_time_frame_t *tcp_time = (tcp_time_frame_t *) t->data;
time->time.hours_bcd = RTC_CONV_TO_BCD(tcp_time->hours);
time->time.minutes_bcd = RTC_CONV_TO_BCD(tcp_time->minutes);
time->time.seconds_bcd = RTC_CONV_TO_BCD(tcp_time->seconds);
time->date.day_bcd = RTC_CONV_TO_BCD(tcp_time->day);
time->date.weekday = 0; // not used
time->date.month_bcd = RTC_CONV_TO_BCD(tcp_time->month);
time->date.year_bcd = RTC_CONV_TO_BCD(tcp_time->year);
// assumes 24H time format
time->time.time_format = RTC_FORMAT_24_HOUR;
}

static void conv_tcp_frame_to_can_msg(tcp_can_frame_t *t, CanMsgTypeDef_t *c)
{
// returns if message valid for can bus sending
Expand Down
13 changes: 13 additions & 0 deletions source/daq/daq_hub/daq_hub.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ typedef enum
TCP_CMD_CAN_FRAME = 0,
TCP_CMD_START_LOG = 1,
TCP_CMD_STOP_LOG = 3,
TCP_CMD_SYNC_TIME = 4,
} tcp_cmd_t;

// TODO: add on bus_id
Expand All @@ -73,6 +74,18 @@ typedef struct __attribute__((packed))
uint8_t data[8]; //!< message data
} tcp_can_frame_t;

typedef struct __attribute__((packed))
{
uint8_t seconds;
uint8_t minutes;
uint8_t hours;
uint8_t day;
RTC_MONTH_t month;
uint8_t year;
uint8_t _padding1;
uint8_t _padding2;
} tcp_time_frame_t;

typedef enum
{
SD_IDLE,
Expand Down
2 changes: 1 addition & 1 deletion source/daq/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ int main()
SysTick_Config(SystemCoreClock / 1000);
NVIC_EnableIRQ(SysTick_IRQn);

if (!PHAL_configureRTC(&start_time, true))
if (!PHAL_configureRTC(&start_time, false))
HardFault_Handler();


Expand Down

0 comments on commit 2b1e3c1

Please sign in to comment.