Forum: Mikrocontroller und Digitale Elektronik OnBoard RTC auf dem STM32F411E Discovery


von Aa B. (aaab)


Lesenswert?

Hallo,

ich versuche auf meinem STM32F411VE den onboard-RTC in betrieb zu 
nehmen. Ich möchte die gelesene Seconds werte auf dem COM Port ausgeben. 
Irgendwie die Werte kommen nicht aktuell auf dem Terminal Programm, ich 
muss den uC immer neu Resetten damit die Werte sich aktualisieren. Die 
Werte sind allerdings plausibel(0-59 in BCD Format). Ich glaube ich habe 
die RTC und UART richtig initialisiert, UART auf jeden Fall, weil ich 
konnte ein normale Text-String ausgeben. Ich weiß es nicht was genau ich 
falsch mache. Vielleicht hat eine oder der anderer sich damit 
beschäftigt und kann ein paar Tipps geben.


Host OS: Linux (Ubuntu 64-bit),

Target: STM32F411E-DISCO,

Controller: STM32F411VET6U,

Tool-chain: MxCube, SWSTM32 IDE basierend auf Eclipse, ST-Link Utility 
um den .bin file zu flashen.

Hier ist mein Source code:

1
/**
2
  ******************************************************************************
3
  * File Name          : main.c
4
  * Description        : Main program body
5
  **********************************************************************************************************************************************************
6
  */
7
/* Includes ------------------------------------------------------------------*/
8
#include "main.h"
9
#include "stm32f4xx_hal.h"
10
#include "stm32f4xx_hal_uart.h"
11
12
/* Private variables ---------------------------------------------------------*/
13
RTC_HandleTypeDef hrtc;
14
UART_HandleTypeDef huart2;
15
16
17
void SystemClock_Config(void);
18
static void MX_GPIO_Init(void);
19
static void MX_RTC_Init(void);
20
//static void MX_USART2_UART_Init(void);
21
22
static void MX_USART2_UART_Init(void)
23
{
24
25
  huart2.Instance = USART2;
26
  huart2.Init.BaudRate = 9600;
27
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
28
  huart2.Init.StopBits = UART_STOPBITS_1;
29
  huart2.Init.Parity = UART_PARITY_NONE;
30
  huart2.Init.Mode = UART_MODE_TX_RX;
31
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
32
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
33
  if (HAL_UART_Init(&huart2) != HAL_OK)
34
  {
35
    _Error_Handler(__FILE__, __LINE__);
36
  }
37
38
}
39
40
41
int main(void)
42
{
43
44
  RTC_TimeTypeDef st_time;
45
  uint8_t sec=0;
46
47
  HAL_Init();
48
49
  SystemClock_Config();
50
51
52
  /*Initialise USART2*/
53
  MX_GPIO_Init();
54
  HAL_UART_MspInit(&huart2);
55
  MX_USART2_UART_Init();
56
57
  /*Initialise RTC*/
58
  MX_RTC_Init();
59
  HAL_RTC_MspInit(&hrtc);
60
  HAL_RTC_Init(&hrtc);   
61
62
  while (1)
63
  {
64
65
66
      HAL_RTC_GetTime(&hrtc, &st_time, RTC_FORMAT_BCD);
67
      HAL_Delay(5);
68
      sec=st_time.Seconds;
69
      HAL_UART_Transmit(&huart2 ,&sec , 1,100);
70
      HAL_Delay(400);
71
72
73
74
  }
75
76
77
}
78
79
/** System Clock Configuration
80
*/
81
void SystemClock_Config(void)
82
{
83
84
  RCC_OscInitTypeDef RCC_OscInitStruct;
85
  RCC_ClkInitTypeDef RCC_ClkInitStruct;
86
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
87
88
    /**Configure the main internal regulator output voltage 
89
    */
90
  __HAL_RCC_PWR_CLK_ENABLE();
91
92
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
93
94
    /**Initializes the CPU, AHB and APB busses clocks 
95
    */
96
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
97
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
98
  RCC_OscInitStruct.HSICalibrationValue = 16;
99
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
100
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
101
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
102
  RCC_OscInitStruct.PLL.PLLM = 8;
103
  RCC_OscInitStruct.PLL.PLLN = 192;
104
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
105
  RCC_OscInitStruct.PLL.PLLQ = 8;
106
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
107
  {
108
    _Error_Handler(__FILE__, __LINE__);
109
  }
110
111
    /**Initializes the CPU, AHB and APB busses clocks 
112
    */
113
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
114
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
115
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
116
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
117
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
118
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
119
120
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
121
  {
122
    _Error_Handler(__FILE__, __LINE__);
123
  }
124
125
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
126
  PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
127
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
128
  {
129
    _Error_Handler(__FILE__, __LINE__);
130
  }
131
132
    /**Configure the Systick interrupt time 
133
    */
134
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
135
136
    /**Configure the Systick 
137
    */
138
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
139
140
  /* SysTick_IRQn interrupt configuration */
141
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
142
}
143
144
/* RTC init function */
145
static void MX_RTC_Init(void)
146
{
147
148
  RTC_TimeTypeDef sTime;
149
  RTC_DateTypeDef sDate;
150
151
    /**Initialize RTC Only 
152
    */
153
  hrtc.Instance = RTC;
154
  hrtc.Init.HourFormat = RTC_HOURFORMAT_12;
155
  hrtc.Init.AsynchPrediv = 127;
156
  hrtc.Init.SynchPrediv = 255;
157
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
158
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
159
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
160
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
161
  {
162
    _Error_Handler(__FILE__, __LINE__);
163
  }
164
165
    /**Initialize RTC and set the Time and Date 
166
    */
167
  if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2){
168
  sTime.Hours = 0x1;
169
  sTime.Minutes = 0x0;
170
  sTime.Seconds = 0x0;
171
  sTime.TimeFormat = RTC_HOURFORMAT12_AM;
172
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
173
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
174
  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
175
  {
176
    _Error_Handler(__FILE__, __LINE__);
177
  }
178
179
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
180
  sDate.Month = RTC_MONTH_JANUARY;
181
  sDate.Date = 0x1;
182
  sDate.Year = 0x4;
183
184
  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
185
  {
186
    _Error_Handler(__FILE__, __LINE__);
187
  }
188
189
    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);
190
  }
191
192
}
193
194
/** Configure pins as 
195
        * Analog 
196
        * Input 
197
        * Output
198
        * EVENT_OUT
199
        * EXTI
200
*/
201
static void MX_GPIO_Init(void)
202
{
203
204
  /* GPIO Ports Clock Enable */
205
  __HAL_RCC_GPIOA_CLK_ENABLE();
206
207
208
}
209
210
211
void _Error_Handler(char * file, int line)
212
{
213
 
214
  while(1) 
215
  {
216
  }
217
 
218
}
219
220
#ifdef USE_FULL_ASSERT
221
222
void assert_failed(uint8_t* file, uint32_t line)
223
{
224
  
225
226
}

von pegel (Gast)


Lesenswert?

Nach GetTime muss auch noch ein GetDate ausgeführt werden.

von Aa B. (aaab)


Lesenswert?

pegel schrieb:
> Nach GetTime muss auch noch ein GetDate ausgeführt werden.

Hey, cool, dass hat geklappt, ist dass in den RM beschrieben?

von Aa B. (aaab)


Lesenswert?

Aa B. schrieb:
> pegel schrieb:
>> Nach GetTime muss auch noch ein GetDate ausgeführt werden.
>
> Hey, cool, dass hat geklappt, ist dass in den RM beschrieben?

Ja ist es,

"You must call HAL_RTC_GetDate() after
HAL_RTC_GetTime() to unlock the values in the higher-order
calendar shadow registers to ensure consistency between the
time and date values. Reading RTC current time locks the
values in calendar shadow registers until Current date is read."

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.