Skip to content

Commit

Permalink
Add: RTC time and date class
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonyang-ee committed Jul 12, 2023
1 parent 58e8709 commit 81acbe6
Show file tree
Hide file tree
Showing 18 changed files with 12,395 additions and 29 deletions.
1 change: 1 addition & 0 deletions Application/Inc/CLI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class CLI {
static int32_t cmd_idle(int32_t, char**);
static int32_t cmd_dac(int32_t, char**);
static int32_t cmd_show(int32_t, char**);
static int32_t cmd_date(int32_t, char**);

private:
uint16_t m_cmd_size;
Expand Down
2 changes: 2 additions & 0 deletions Application/Inc/Instances.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "ADC.hpp"
#include "Flash_STM32G431KB.hpp"
#include "State.hpp"
#include "RTC.hpp"

#define UART_BUFFER 64

Expand All @@ -19,6 +20,7 @@ extern Thread thread;
extern Flash flash;
extern CustomDAC dac;
extern CustomADC adc;
extern CustomRTC rtc;

extern sml::sm<StreamState> stream_sm;
extern sml::sm<MainState, sml::process_queue<std::queue>> main_sm;
Expand Down
24 changes: 24 additions & 0 deletions Application/Inc/RTC.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef APPLICATION_INC_RTC
#define APPLICATION_INC_RTC

#include "array"
#include "main.h"

class CustomRTC {
public:
CustomRTC();
~CustomRTC();
void setPort(RTC_HandleTypeDef *port);

void setTime(uint8_t hour, uint8_t minute, uint8_t second);
void setDate(uint8_t day, uint8_t month, uint8_t year);
std::array<uint8_t, 3> getDate();
std::array<uint8_t, 3> getTime();

private:
RTC_HandleTypeDef m_port;
RTC_TimeTypeDef m_time;
RTC_DateTypeDef m_date;
};

#endif /* APPLICATION_INC_RTC */
8 changes: 8 additions & 0 deletions Application/Inc/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@
extern "C" {
#endif

#include "adc.h"
#include "dac.h"
#include "dma.h"
#include "gpio.h"
#include "rtc.h"
#include "stm32g4xx_hal.h"
#include "tim.h"
#include "usart.h"


// System Functions
void Error_Handler(void);
Expand Down
25 changes: 25 additions & 0 deletions Application/Src/CLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ void CLI::init() {
lwshell_register_cmd("idle", &CLI::cmd_idle, NULL);
lwshell_register_cmd("dac", &CLI::cmd_dac, NULL);
lwshell_register_cmd("show", &CLI::cmd_show, NULL);
lwshell_register_cmd("rtc", &CLI::cmd_date, NULL);
}

/**
Expand Down Expand Up @@ -188,6 +189,30 @@ int32_t CLI::cmd_show(int32_t argc, char** argv) {
return 0;
}

int32_t CLI::cmd_date(int32_t argc, char** argv) {
// Detailed Menu
const char* help_text =
"\nDate Functions:\n"
" set\t\tSet Date\n"
" get\t\tGet Date\n\n";

if (argc == 1) {
serialCOM.sendString(help_text);
} else if (argc == 5) {
if (!strcmp(argv[1], "setdate")) {
rtc.setDate(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
} else if (!strcmp(argv[1], "settime")) {
rtc.setTime(atoi(argv[2]), atoi(argv[3]), atoi(argv[4]));
} else {
serialCOM.sendString("Unknown Command\n");
}
} else {
serialCOM.sendString("Unknown Command\n");
}

return 0;
}

// Default lwshell has only -h option
// If using git style where help is better syntax,
// Then, you need to do this help menu manually
Expand Down
44 changes: 44 additions & 0 deletions Application/Src/RTC.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

// fill all functions in this file

#include "RTC.hpp"

#include "instances.hpp"

CustomRTC::CustomRTC() {}

CustomRTC::~CustomRTC() {}

void CustomRTC::setPort(RTC_HandleTypeDef *port) { m_port = *port; }

void CustomRTC::setTime(uint8_t hour, uint8_t minute, uint8_t second) {
m_time.Hours = hour;
m_time.Minutes = minute;
m_time.Seconds = second;
if (HAL_RTC_SetTime(&m_port, &m_time, RTC_FORMAT_BIN) != HAL_OK) {
serialCOM.sendString("RTC Set Time Error\n");
}
}

void CustomRTC::setDate(uint8_t day, uint8_t month, uint8_t year) {
m_date.Date = day;
m_date.Month = month;
m_date.Year = year;
if (HAL_RTC_SetDate(&m_port, &m_date, RTC_FORMAT_BIN) != HAL_OK) {
serialCOM.sendString("RTC Set Date Error\n");
}
}

std::array<uint8_t, 3> CustomRTC::getDate() {
if (HAL_RTC_GetDate(&m_port, &m_date, RTC_FORMAT_BIN) != HAL_OK) {
serialCOM.sendString("RTC Get Date Error\n");
}
return {m_date.Date, m_date.Month, m_date.Year};
}

std::array<uint8_t, 3> CustomRTC::getTime() {
if (HAL_RTC_GetTime(&m_port, &m_time, RTC_FORMAT_BIN) != HAL_OK) {
serialCOM.sendString("RTC Get Time Error\n");
}
return {m_time.Hours, m_time.Minutes, m_time.Seconds};
}
5 changes: 5 additions & 0 deletions Application/Src/Thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ void Thread::telemetry_human() {
serialCOM.sendNumber(dac.getLevel());
serialCOM.sendString("\nADC Sensing Value:\t");
serialCOM.sendNumber(adc.volt_from_dac);
serialCOM.sendString("\nDate:\t");
serialCOM.sendNumber(rtc.getDate());
serialCOM.sendString("\nTime:\t");
serialCOM.sendNumber(rtc.getTime());
serialCOM.sendLn();

// State machine debug
// serialCOM.sendString("\n\nCurrent State:\t");
Expand Down
17 changes: 7 additions & 10 deletions Application/Src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@
#include "main.h"

#include "Instances.hpp"
#include "adc.h"
#include "dac.h"
#include "dma.h"
#include "gpio.h"
#include "tim.h"
#include "usart.h"

// Instances Objects
CLI cli{};
Expand All @@ -17,6 +11,7 @@ SerialCOM serialCOM{};
Flash flash{};
CustomDAC dac{};
CustomADC adc{};
CustomRTC rtc{};

sml::sm<StreamState> stream_sm{&thread};
sml::sm<MainState, sml::process_queue<std::queue>> main_sm{&thread, &serialCOM};
Expand All @@ -39,26 +34,28 @@ int main(void) {
MX_DAC1_Init();
MX_TIM2_Init();
MX_TIM4_Init();
MX_RTC_Init();

// Instances Dependency Injection
serialCOM.setPort(&huart2);
led_user.setPort(&htim8.Instance->CCR2);
dac.setPort(&hdac1, DAC_CHANNEL_2);
rtc.setPort(&hrtc);

serialCOM.sendString("Instance dependency injection complete\n");
serialCOM.sendString("Instance dependency injection complete\n");

// PWM Output Start
HAL_TIM_PWM_Start_IT(&htim8, TIM_CHANNEL_2);
serialCOM.sendString("PWM output Start\n");
serialCOM.sendString("PWM output Start\n");

// Serial Communication Start
HAL_UARTEx_ReceiveToIdle_IT(&huart2, serialCOM.m_rx_data, UART_BUFFER);
serialCOM.sendString("Serial communication Start\n");
serialCOM.sendString("Serial communication Start\n");

// ADC Calibration and Start
HAL_ADCEx_Calibration_Start(&hadc2, ADC_SINGLE_ENDED);
HAL_ADC_Start_DMA(&hadc2, adc.m_buffer.data(), 1);
serialCOM.sendString("ADC calibration and start\n");
serialCOM.sendString("ADC calibration and start\n");

// FreeRTOS Start
vTaskStartScheduler();
Expand Down
52 changes: 52 additions & 0 deletions Core/Inc/rtc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file rtc.h
* @brief This file contains all the function prototypes for
* the rtc.c file
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __RTC_H__
#define __RTC_H__

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

extern RTC_HandleTypeDef hrtc;

/* USER CODE BEGIN Private defines */

/* USER CODE END Private defines */

void MX_RTC_Init(void);

/* USER CODE BEGIN Prototypes */

/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif

#endif /* __RTC_H__ */

8 changes: 1 addition & 7 deletions Core/Inc/stm32g4xx_hal_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/

#ifndef CORE_INC_STM32G4XX_HAL_CONF
#define CORE_INC_STM32G4XX_HAL_CONF
#ifndef STM32G4xx_HAL_CONF_H
#define STM32G4xx_HAL_CONF_H

Expand Down Expand Up @@ -59,7 +56,7 @@
/*#define HAL_PCD_MODULE_ENABLED */
/*#define HAL_QSPI_MODULE_ENABLED */
/*#define HAL_RNG_MODULE_ENABLED */
/*#define HAL_RTC_MODULE_ENABLED */
#define HAL_RTC_MODULE_ENABLED
/*#define HAL_SAI_MODULE_ENABLED */
/*#define HAL_SMARTCARD_MODULE_ENABLED */
/*#define HAL_SMBUS_MODULE_ENABLED */
Expand Down Expand Up @@ -381,6 +378,3 @@ void assert_failed(uint8_t *file, uint32_t line);
#endif

#endif /* STM32G4xx_HAL_CONF_H */


#endif /* CORE_INC_STM32G4XX_HAL_CONF */
Loading

0 comments on commit 81acbe6

Please sign in to comment.