123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© Copyright (c) 2021 STMicroelectronics.
- * All rights reserved.</center></h2>
- *
- * This software component is licensed by ST under BSD 3-Clause license,
- * the "License"; You may not use this file except in compliance with the
- * License. You may obtain a copy of the License at:
- * opensource.org/licenses/BSD-3-Clause
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "fatfs.h"
-
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "math.h"
- #include "stdbool.h"
- #include "string.h"
- #include <stdio.h>
- #include "fatfs_sd.h"
- #include <stdarg.h>
- /* USER CODE END Includes */
-
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
-
- /* USER CODE END PTD */
-
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
-
- /* USER CODE END PD */
-
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
-
- /* USER CODE END PM */
-
- /* Private variables ---------------------------------------------------------*/
- SPI_HandleTypeDef hspi1;
- ADC_HandleTypeDef hadc;
- RTC_HandleTypeDef hrtc;
- UART_HandleTypeDef huart2;
-
- /* USER CODE BEGIN PV */
- RTC_TimeTypeDef sTime;
- RTC_DateTypeDef sDate;
- RTC_AlarmTypeDef sAlarmA, sAlarmB;
- static volatile uint16_t gLastError;
- static volatile bool gButtonPressed = FALSE;
-
- //Nuremberg coordinates
- int latitude_nbg = 49;
- int longitude_nbg = 11;
-
- //German UTC time,summer (+2) and winter (+1)
- int UTC_DER_sum = 2;
- int UTC_DER_win = 1;
- bool winterTime = true;
-
- int DaysInMonth[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- int DaysInMonthLeapYear[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- bool leapYear = false;
- int singleStepsFor180Deg = 100; // The stepper motor needs 200 single steps for 360 deg, equals 100 steps for 180 deg
- int leapsFor180Deg = 5; // Determines how big the amount of single steps is to complete 180 degrees of rotation
- bool alarmSunriseFlag = false;
- bool alarmSunsetFlag = false;
- bool makeStepFlag = false;
-
- typedef struct {
- int hours;
- int minutes;
- int seconds;
- int weekDay;
- int month;
- int day;
- int year;
- } timeAndDate;
-
- typedef struct {
- char hours[10];
- char minutes[10];
- char seconds[10];
- char *fullTimeStamp;
- } timeStamp;
-
- timeStamp time;
-
- // SD CARD Variables
- FATFS fs;
- FATFS *pfs;
- FIL fil;
- FRESULT fres;
- DWORD fre_clust;
- uint32_t totalSpace, freeSpace;
- char buffer[100];
- uint16_t AD_RES;
- int num;
-
- //Variable name for txt file
- char txtVar[19];
-
- /* USER CODE END PV */
-
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_USART2_UART_Init(void);
- static void MX_RTC_Init(void);
- static void MX_SPI1_Init(void);
- static void MX_ADC_Init(void);
- static void MyFlagInterruptHandler(void);
- void ButtonHandler(void);
- /* USER CODE BEGIN PFP */
-
- /* USER CODE END PFP */
-
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /*******************************************************************************
- * Function Name : deg_to_rad
- * Description : converts degrees to radians
- * Return : angle in radians
- *******************************************************************************/
- double deg_to_rad(double deg)
- {
- double rad = deg*(M_PI/180);
- return rad;
- }
-
- /*******************************************************************************
- * Function Name : rad_to_deg
- * Description : converts radians to degrees
- * Return : angle in degrees
- *******************************************************************************/
- double rad_to_deg(double rad)
- {
- double deg = rad*(180/M_PI);
- return deg;
- }
-
- /*******************************************************************************
- * Function Name : leap_year_check
- * Description : checks if year is a leap year
- * Return : false: no leap year, true: leap year
- *******************************************************************************/
- void leap_year_check(int initialyear)
- {
- int year = initialyear;
- if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
- {
- leapYear = true;
- }
- else
- {
- leapYear = false;
- }
-
- }
-
- /*******************************************************************************
- * Function Name : calc_day_of_year
- * Description : calculates the day of year
- * Return : day of year (1.1.. = 1, 2.1.. = 2,...)
- * Source : https://overiq.com/c-examples/c-program-to-calculate-the-day-of-year-from-the-date/
- *******************************************************************************/
- int calc_day_of_year(int day, int mon, int year)
- {
- int days_in_feb = 28;
- int doy = day; //day of year
-
- // check for leap year
- //bool leap_year = leap_year_check(year);
- if(leapYear == true)
- {
- days_in_feb = 29;
- }
-
- switch(mon)
- {
- case 2:
- doy += 31;
- break;
- case 3:
- doy += 31+days_in_feb;
- break;
- case 4:
- doy += days_in_feb+62;
- break;
- case 5:
- doy += days_in_feb+92;
- break;
- case 6:
- doy += days_in_feb+123;
- break;
- case 7:
- doy += days_in_feb+153;
- break;
- case 8:
- doy += days_in_feb+184;
- break;
- case 9:
- doy += days_in_feb+215;
- break;
- case 10:
- doy += days_in_feb+245;
- break;
- case 11:
- doy += days_in_feb+276;
- break;
- case 12:
- doy += days_in_feb+306;
- break;
- }
- return doy;
- }
-
- /*******************************************************************************
- * Function Name : calc_sunrise_sunset
- * Description : calculates the sunrise and sunset time of a specific date
- * Source : General Solar Position Calculations, NOAA Global Monitoring Division
- *******************************************************************************/
- void calc_sunrise_sunset(timeAndDate* initialDate, timeAndDate* sunriseStruct, timeAndDate* sunsetStruct, timeAndDate* tomorrowsDate)
- {
- double gamma = 0;
- double eqtime = 0;
- double decl = 0;
- //double decl_deg = 0;
- double zenith_sun = 0;
- double lat_nbg_rad = 0;
- double ha = 0;
- double sunrise = 0;
- double sunset = 0;
- double ha_deg = 0;
- int sunrise_h = 0;
- int sunset_h = 0;
- double sunrise_min = 0;
- double sunset_min = 0;
- int int_sunrise_min = 0;
- int int_sunset_min = 0;
-
- int day = initialDate->day;
- int month = initialDate->month;
- int year = initialDate->year;
-
- //day of year calculation
- int day_of_year = calc_day_of_year(day, month, year);
-
- // fractional year (γ) in radians
- // check for leap year
- //leap_year = leap_year_check(year);
- if(leapYear == false)
- {
- //The back part of the formula was omitted, because there is no difference in the result
- gamma = ((2 * M_PI)/365)*(day_of_year - 1);
- } else {
- //The back part of the formula was omitted, because there is no difference in the result
- gamma = ((2 * M_PI)/366)*(day_of_year - 1);
- }
-
- //Equation of time in minutes
- eqtime = 229.18*(0.000075 + 0.001868*cos(gamma) - 0.032077*sin(gamma) - 0.014615*cos(2*gamma) - 0.040849*sin(2*gamma));
-
- //Solar declination angle in radians
- decl = 0.006918 - 0.399912*cos(gamma) + 0.070257*sin(gamma) - 0.006758*cos(2*gamma) + 0.000907*sin(2*gamma) - 0.002697*cos(3*gamma) + 0.00148*sin(3*gamma);
-
- //Solar declination angle in degrees
- //decl_deg = rad_to_deg(decl);
-
- //Hour angle in degrees, positive number corresponds to sunrise, negative to sunset
- //special case of sunrise or sunset, the zenith is set to 90.833Deg
- zenith_sun = deg_to_rad(90.833);
- //Latitude of Nuernberg in rad
- lat_nbg_rad = deg_to_rad(latitude_nbg);
- ha = acos((cos(zenith_sun)/(cos(lat_nbg_rad)*cos(decl)))-(tan(lat_nbg_rad)*tan(decl)));
- ha_deg = rad_to_deg(ha);
-
- //UTC time of sunrise (or sunset) in minutes
- sunrise = (720-4*(longitude_nbg+ha_deg)-eqtime);
- sunset = 720-4*(longitude_nbg-ha_deg)-eqtime;
-
- //Convert sunrise (or sunset) UTC time in hours
- sunrise = sunrise/60;
- sunset = sunset/60;
-
- //Seperate hours and minutes
- sunrise_h = floor(sunrise);
- sunrise_min = sunrise - sunrise_h;
- //Cut off after two decimal places
- int_sunrise_min = floor(sunrise_min * 100.0);
- if (int_sunrise_min >= 60)
- {
- sunrise_h = sunrise_h + 1;
- int_sunrise_min = int_sunrise_min - 60;
- }
- sunset_h = floor(sunset);
- sunset_min = sunset - sunset_h;
- //Cut off after two decimal places
- int_sunset_min = floor(sunset_min * 100.0);
- if (int_sunset_min >= 60)
- {
- sunset_h = sunset_h + 1;
- int_sunset_min = int_sunset_min - 60;
- }
-
- //Add time difference from German time to UTC Time
- //Private variable winterTime must be initialized accordingly
- if (winterTime)
- {
- sunrise_h = sunrise_h + UTC_DER_win;
- sunset_h = sunset_h + UTC_DER_win;
- } else {
- sunrise_h = sunrise_h + UTC_DER_sum;
- sunset_h = sunset_h + UTC_DER_sum;
- }
-
- sunriseStruct->hours = sunrise_h;
- sunriseStruct->minutes = int_sunrise_min;
-
- sunsetStruct->hours = sunset_h;
- sunsetStruct->minutes = int_sunset_min;
-
- sunriseStruct->day = sunsetStruct->day = tomorrowsDate->day;
- sunriseStruct->weekDay = sunsetStruct->weekDay = tomorrowsDate->weekDay;
- sunriseStruct->month = sunsetStruct->month = tomorrowsDate->month;
- sunriseStruct->year = sunsetStruct->year = tomorrowsDate->year;
- }
-
- /*******************************************************************************
- * Function Name : calc_tomorrows_date
- * Description : calculates tomorrow's date
- * Source : https://github.com/vyacht/stm32/blob/master/vynmea/rtc.c
- *******************************************************************************/
- void calc_tomorrows_date(timeAndDate* initialDate, timeAndDate* tomorrowsDate)
- {
- int yearToUse[12];
-
- if (leapYear == true){
- memcpy(yearToUse, DaysInMonthLeapYear, sizeof yearToUse);
- } else {
- memcpy(yearToUse, DaysInMonth, sizeof yearToUse);
- }
-
- int day = initialDate->day;
- int wday = initialDate->weekDay;
- int month = initialDate->month;
- int year = initialDate->year;
-
- day++; // next day
- wday++; // next weekday
- if(wday == 8)
- {
- wday = 1; // Monday
- }
- if(day > yearToUse[month-1])
- { // next month
- day = 1;
- month++;
- }
- if(day > 31 && month == 12) // next year
- {
- day = 1;
- month = 1;
- year++;
- }
-
- tomorrowsDate->day = day;
- tomorrowsDate->weekDay = wday;
- tomorrowsDate->month = month;
- tomorrowsDate->year = year;
-
- }
-
- /*******************************************************************************
- * Function Name : set_Alarm
- * Description : sets the wake up Alarm
- *******************************************************************************/
- void set_alarm(int h, int min, int weekDay, char* alarm, RTC_AlarmTypeDef* alarmInstance)
- {
- /** Enable the Alarm A*/
-
- alarmInstance->AlarmTime.Hours = h;
- alarmInstance->AlarmTime.Minutes = min;
- alarmInstance->AlarmTime.Seconds = 0;
- alarmInstance->AlarmTime.SubSeconds = 0;
- alarmInstance->AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- alarmInstance->AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
- alarmInstance->AlarmMask = RTC_ALARMMASK_NONE; //only by specific time
- alarmInstance->AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
- alarmInstance->AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
- alarmInstance->AlarmDateWeekDay = weekDay;
-
- if (strcmp("A", alarm) == 0) {
- alarmInstance->Alarm = RTC_ALARM_A;
- } else {
- alarmInstance->Alarm = RTC_ALARM_B;
- }
-
- if (HAL_RTC_SetAlarm_IT(&hrtc, alarmInstance, RTC_FORMAT_BIN) != HAL_OK)
- {
- Error_Handler();
- }
-
-
- }
-
- void get_time(timeStamp *time){
-
- char str[200];
-
- if (HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK)
- {
- sprintf(time->hours, "%d", sTime.Hours);
- sprintf(time->minutes, "%d", sTime.Minutes);
- sprintf(time->seconds, "%d", sTime.Seconds);
- }
-
- strcpy(str, time->hours);
- strcat(str, ":");
- strcat(str, time->minutes);
- strcat(str, ":");
- strcat(str, time->seconds);
- strcat(str, " | ");
-
- time->fullTimeStamp = str;
- }
-
- // sending to UART
- void transmit_uart(char *string){
- //char str[200];
- //get_time(&time);
- //strcat(time.fullTimeStamp, string);
- //strcpy(str, time.fullTimeStamp);
- uint8_t len = strlen(string);
- HAL_UART_Transmit(&huart2, (uint8_t*) string, len, 200);
- }
-
- void set_time_and_date(timeAndDate *timeanddate){
-
- if (HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN) == HAL_OK)
- {
- timeanddate->hours = sTime.Hours;
- timeanddate->minutes = sTime.Minutes;
- timeanddate->seconds = sTime.Seconds;
- }
-
- if (HAL_RTC_GetDate(&hrtc, &sDate, RTC_FORMAT_BIN) == HAL_OK)
- {
- timeanddate->weekDay = sDate.WeekDay;
- timeanddate->month = sDate.Month;
- timeanddate->day = sDate.Date;
- timeanddate->year = 2000 + sDate.Year;
- }
- }
-
- int calc_interval_duration(timeAndDate *sunrise, timeAndDate *sunset){
- int duration_h=0;
- int duration_m=0;
-
- duration_h = sunset->hours - sunrise->hours;
- duration_m = sunset->minutes - sunrise->minutes;
-
- if (duration_m < 0) {
- duration_h = duration_h - 1;
- duration_m = 60 - sunrise->minutes + sunset->minutes;
- }
-
- return (duration_h * 60 + duration_m) / leapsFor180Deg;
- }
-
- char int_to_char_and_concat(int amount, char* separator, ...){
- va_list argumentlist;
- va_start(argumentlist, separator);
- char str[200] = "";
-
- while (amount--){
- char arg = va_arg(argumentlist, char);
- strcat(str, arg);
- strcat(str, separator);
- }
-
- return str;
- }
-
- /* USER CODE END 0 */
-
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
-
- /* USER CODE END 1 */
-
- /* MCU Configuration--------------------------------------------------------*/
-
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
-
- /* USER CODE BEGIN Init */
-
- /* USER CODE END Init */
-
- /* Configure the system clock */
- SystemClock_Config();
-
- /* USER CODE BEGIN SysInit */
-
- /* USER CODE END SysInit */
-
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USART2_UART_Init();
- MX_RTC_Init();
- MX_SPI1_Init();
- MX_FATFS_Init();
- MX_ADC_Init();
- /* USER CODE BEGIN 2 */
-
- //######### Inits of the Motor control library #########
-
- /* Set the L6208 library to use 1 device */
- BSP_MotorControl_SetNbDevices(BSP_MOTOR_CONTROL_BOARD_ID_L6208, 1);
- BSP_MotorControl_Init(BSP_MOTOR_CONTROL_BOARD_ID_L6208, NULL);
-
- /* Attach the function MyFlagInterruptHandler (defined below) to the flag interrupt */
- BSP_MotorControl_AttachFlagInterrupt(MyFlagInterruptHandler);
-
- /* Attach the function MyErrorHandler (defined below) to the error Handler*/
- BSP_MotorControl_AttachErrorHandler(Error_Handler);
-
- /* Set Systick Interrupt priority highest to ensure no lock by using HAL_Delay */
- HAL_NVIC_SetPriority(SysTick_IRQn, 0x0, 0x0);
-
- /* Configure KEY Button */
- BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_EXTI);
-
- /* Disable the power bridges after initialization */
- BSP_MotorControl_CmdDisable(0);
-
- //######### Mount SD-Card #########
-
- fres = f_mount(&fs, "", 0);
- if (fres == FR_OK) {
- transmit_uart("SD card is mounted successfully!\r\n");
- } else if (fres != FR_OK) {
- transmit_uart("SD card is not mounted!\r\n");
- }
-
- //######### Variable inits #########
-
- timeAndDate sunrise, sunset, wakeUpTimeForStep, tomorrowsDate, initialDate;
- sunrise = sunset = wakeUpTimeForStep = tomorrowsDate = initialDate = (timeAndDate) {\
- 0,
- 0,
- 0,
- 0,
- 0,
- 0,
- 0
- };
-
- int32_t pos=0;
- uint32_t freqPwm=0;
- uint32_t timeToNextStep_m=0;
- uint32_t alarmB_h = 0;
- uint32_t alarmB_m = 0;
- uint32_t alarmB_wd = 0;
- uint32_t stepsToMake = singleStepsFor180Deg / leapsFor180Deg; // The amount of single steps to make to complete 180/5 degrees
-
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
-
- //######### Motor settings and motor test #########
-
- HAL_Delay(2000);
-
- transmit_uart("Resetting motor position and calculating new dates and times.\r\n");
-
- freqPwm = BSP_MotorControl_GetBridgeInputPwmFreq(0);
-
- BSP_MotorControl_SetBridgeInputPwmFreq(0, freqPwm>>1);
-
- pos = BSP_MotorControl_GetPosition(0);
-
- BSP_MotorControl_SetHome(0, pos);
-
- BSP_MotorControl_SelectStepMode(0, STEP_MODE_FULL);
-
- BSP_MotorControl_Move(0, FORWARD, stepsToMake);
- BSP_MotorControl_WaitWhileActive(0);
-
- BSP_MotorControl_Move(0, FORWARD, stepsToMake);
- BSP_MotorControl_WaitWhileActive(0);
-
- BSP_MotorControl_Move(0, FORWARD, stepsToMake);
- BSP_MotorControl_WaitWhileActive(0);
-
- BSP_MotorControl_GoHome(0);
- BSP_MotorControl_WaitWhileActive(0);
-
- /* USER CODE END WHILE */
-
- /* USER CODE BEGIN 3 */
- set_time_and_date(&initialDate);
-
- leap_year_check(initialDate.year);
-
- calc_tomorrows_date(&initialDate, &tomorrowsDate);
-
- //Calculate sunrise and sunset time for tomorrow
- calc_sunrise_sunset(&initialDate, &sunrise, &sunset, &tomorrowsDate);
-
- //Test code
- sunrise.hours = 14;
- sunrise.minutes = 0;
- sunrise.weekDay = 7;
-
- sunset.hours = 14;
- sunset.minutes = 30;
- sunset.weekDay = 7;
-
- //Calculate the time for next motor step in minutes
- timeToNextStep_m = calc_interval_duration(&sunrise, &sunset);
-
- // Set Alarm for sunrise
-
- transmit_uart("Setting alarm for sunrise.\r\n");
- set_alarm(sunrise.hours, sunrise.minutes, sunrise.weekDay, "A", &sAlarmA);
-
- HAL_Delay(2000);
-
- transmit_uart("Entering sleep mode.\r\n");
- HAL_SuspendTick();
- HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
- HAL_ResumeTick();
-
- if (alarmSunriseFlag == true) {
-
- transmit_uart("Sunrise statement entered.\r\n");
- // Reset the flags
- alarmSunsetFlag = false;
- alarmSunriseFlag = false;
-
- // The alarm for the next step is incremented from sunrise as the initial time.
- alarmB_h = sunrise.hours;
- alarmB_m = sunrise.minutes;
- alarmB_wd = sunrise.weekDay;
-
- // Set Alarm for sunset, it overwrites the alarm for sunrise because the sunrise already happenend
- // The timeframes for both alarms dont overlap so 1 alarm is enough
- transmit_uart("Setting alarm for sunset.\r\n");
- set_alarm(sunset.hours, sunset.minutes, sunset.weekDay, "A", &sAlarmA);
-
- HAL_Delay(2000);
-
- while (alarmSunsetFlag != true) {
-
- transmit_uart("|--------------------------------------------------------|\r\n\r\n");
- // Increment alarm time with the precalculated timeToNextStep
- int minAdd_tmp=0;
-
- minAdd_tmp = alarmB_m + timeToNextStep_m;
-
- // Consider minutes overflow ^= hours + 1
- if (minAdd_tmp > 60) {
- alarmB_h = alarmB_h + 1;
- alarmB_m = minAdd_tmp - 60;
- } else {
- alarmB_m = minAdd_tmp;
- }
-
- transmit_uart("Setting alarm for next step.\r\n");
- set_alarm(alarmB_h, alarmB_m, alarmB_wd, "B", &sAlarmB);
-
- HAL_Delay(2000);
-
- transmit_uart("Entering sleep mode.\r\n");
- HAL_SuspendTick();
- HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON, PWR_SLEEPENTRY_WFI);
- HAL_ResumeTick();
-
- if (makeStepFlag) {
- transmit_uart("Making a step.\r\n");
- BSP_MotorControl_Move(0, FORWARD, stepsToMake);
- BSP_MotorControl_WaitWhileActive(0);
- }
-
- makeStepFlag = true;
-
- transmit_uart("\r\n");
-
- };
- }
-
- BSP_MotorControl_GoHome(0);
- BSP_MotorControl_WaitWhileActive(0);
- }
- /* USER CODE END 3 */
- }
-
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
-
- /** Configure the main internal regulator output voltage
- */
- __HAL_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- //RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.HSICalibrationValue = 16;
- RCC_OscInitStruct.LSIState = RCC_LSI_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 16;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
-
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
- PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- /**
- * @brief RTC Initialization Function
- * @param None
- * @retval None
- */
- static void MX_RTC_Init(void)
- {
-
- /* USER CODE BEGIN RTC_Init 0 */
-
- /* USER CODE END RTC_Init 0 */
-
- RTC_TimeTypeDef sTime = {0};
- RTC_DateTypeDef sDate = {0};
- //RTC_AlarmTypeDef sAlarm = {0};
-
- /* USER CODE BEGIN RTC_Init 1 */
-
- /* USER CODE END RTC_Init 1 */
- /** Initialize RTC Only
- */
- hrtc.Instance = RTC;
- hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
- hrtc.Init.AsynchPrediv = 127;
- hrtc.Init.SynchPrediv = 255;
- hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
- hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
- hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
- if (HAL_RTC_Init(&hrtc) != HAL_OK)
- {
- Error_Handler();
- }
-
- /* USER CODE BEGIN Check_RTC_BKUP */
-
- /* USER CODE END Check_RTC_BKUP */
-
- /** Initialize RTC and set the Time and Date
- */
- sTime.Hours = 13;
- sTime.Minutes = 56;
- sTime.Seconds = 10;
- sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
- sTime.StoreOperation = RTC_STOREOPERATION_RESET;
- if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)
- {
- Error_Handler();
- }
- sDate.WeekDay = RTC_WEEKDAY_SUNDAY;
- sDate.Month = RTC_MONTH_FEBRUARY;
- sDate.Date = 21;
- sDate.Year = 21;
-
- if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)
- {
- Error_Handler();
- }
- }
-
- /**
- * @brief USART2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_USART2_UART_Init(void)
- {
-
- /* USER CODE BEGIN USART2_Init 0 */
-
- /* USER CODE END USART2_Init 0 */
-
- /* USER CODE BEGIN USART2_Init 1 */
-
- /* USER CODE END USART2_Init 1 */
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN USART2_Init 2 */
-
- /* USER CODE END USART2_Init 2 */
-
- }
-
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
-
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
-
- /*Configure GPIO pin Output Level */
- //HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);
-
- /*Configure GPIO pin : B1_Pin */
- GPIO_InitStruct.Pin = B1_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
-
- /*Configure GPIO pin : LD2_Pin */
- //GPIO_InitStruct.Pin = LD2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- //HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);
-
- }
-
- /* USER CODE BEGIN 4 */
- /**
- * @brief Alarm callback
- * @param hrtc: RTC handle
- * @retval None
- */
- void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
- {
- /* Alarm generation */
- alarmSunriseFlag = true;
- alarmSunsetFlag = true;
- transmit_uart("Alarm A Callback triggered.\r\n");
- transmit_uart("Setting sunrise and sunset flags.\r\n");
- }
-
- void HAL_RTCEx_AlarmBEventCallback(RTC_HandleTypeDef *hrtc)
- {
- /* Alarm generation */
- makeStepFlag = true;
- transmit_uart("Alarm B Callback triggered.\r\n");
- transmit_uart("Setting makeStep flag.\r\n");
- }
-
- /**
- * @brief This function is the User handler for the flag interrupt
- * @param None
- * @retval None
- */
- void MyFlagInterruptHandler(void)
- {
- //When EN pin is forced low by a failure, configure the GPIO as an ouput low
- BSP_MotorControl_CmdDisable(0);
- }
-
- void ButtonHandler(void)
- {
- gButtonPressed = TRUE;
- /* Let 200 ms before clearing the IT for key debouncing */
- HAL_Delay(200);
- __HAL_GPIO_EXTI_CLEAR_IT(KEY_BUTTON_PIN);
- HAL_NVIC_ClearPendingIRQ(KEY_BUTTON_EXTI_IRQn);
- }
-
- /* USER CODE END 4 */
-
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
-
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
-
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|