System.c
Go to the documentation of this file.
1 /**
2  * @file System.c
3  * @brief STM32F103C8T6 system initialisation
4  * @copyright Copyright (c) 2020 STMicroelectronics.
5  * All rights reserved.
6  * This software component is licensed by ST under Ultimate
7  * Liberty license SLA0044, the "License"; You may not use
8  * this file except in compliance with the License. You may
9  * obtain a copy of the License at: www.st.com/SLA0044
10  * @page SysOverview System overview
11  * @section GPIOConfig GPIO configuration
12  * @subsection GPIO_ADC1 ADC 1
13  *
14  * @li PB0 ---> ADC1_IN8
15  * @li PB1 ---> ADC1_IN9
16  *
17  * @subsection GPIO_I2C2 I²C 2
18  *
19  * @li PB10 ---> I2C2_SCL
20  * @li PB11 ---> I2C2_SDA
21  *
22  * @subsection GPIO_SPI1 SPI 1
23  *
24  * @li PA5 ---> SPI1_SCK
25  * @li PA6 ---> SPI1_MISO
26  * @li PA7 ---> SPI1_MOSI
27  *
28  * @subsection GPIO_TIM1 TIM 1
29  *
30  * @li PA8 ---> TIM1_CH1
31  *
32  * @subsection GPIO_OUTPUT Output
33  *
34  * @li PA0 ---> DMD OE pin
35  * @li PA1 ---> DMD SCLK pin
36  * @li PA2 ---> DMD A pin
37  * @li PA3 ---> DMD B pin
38  * @li PC13 ---> LED
39  *
40  */
41 
42 #include "stm32f1xx_hal.h"
43 #include "System.h"
44 
45 ADC_HandleTypeDef hadc1; ///< ADC 1 handle
46 I2C_HandleTypeDef hi2c2; ///< I²C 2 handle
47 SPI_HandleTypeDef hspi1; ///< SPI 1 handle
48 RTC_HandleTypeDef hrtc; ///< RTC handle
49 TIM_HandleTypeDef htim1; ///< Timer 1 handle
50 TIM_HandleTypeDef htim4; ///< Timer 4 handle (Sys-Tick)
51 
52 static void System_GPIO_Init(void);
53 static int System_TIM1_Init(void);
54 static int System_ADC1_Init(void);
55 static int System_I2C2_Init(void);
56 static int System_SPI1_Init(void);
57 static int System_RTC_Init(void);
58 
59 /**
60  * @brief Period elapsed callback in non blocking mode
61  * @note This function is called when TIM4 interrupt took place,
62  * inside HAL_TIM_IRQHandler(). It makes a direct call to
63  * HAL_IncTick() to increment a global variable "uwTick" used
64  * as application time base.
65  * @param htim : TIM handle
66  * @retval None
67  */
68 void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
69 {
70  if (TIM4 == htim->Instance)
71  {
72  HAL_IncTick();
73  }
74 }
75 
76 /**
77  * @brief System Initialisation Function
78  * @return Error code
79  * @retval 0: OK
80  * @retval -1: Error
81  */
82 int System_Init(void)
83 {
84  int nStatus = 0;
85  RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
86  RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
87  RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 };
88 
89  /* Reset of all peripherals, Initialises the Flash interface and the
90  * Systick.
91  */
92  HAL_Init();
93 
94  /** Initialises the CPU, AHB and APB busses clocks
95  */
96  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_LSE;
97  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
98  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
99  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
100  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
101  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
102  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
103  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
104 
105  if (HAL_OK != HAL_RCC_OscConfig(&RCC_OscInitStruct))
106  {
107  return -1;
108  }
109 
110  // Initialises the CPU, AHB and APB busses clocks
111  RCC_ClkInitStruct.ClockType =
112  RCC_CLOCKTYPE_HCLK |
113  RCC_CLOCKTYPE_SYSCLK |
114  RCC_CLOCKTYPE_PCLK1 |
115  RCC_CLOCKTYPE_PCLK2;
116  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
117  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
118  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
119  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
120 
121  if (HAL_OK != HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2))
122  {
123  return -1;
124  }
125 
126  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC | RCC_PERIPHCLK_ADC;
127  PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
128  PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV6;
129 
130  if (HAL_OK != HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit))
131  {
132  return -1;
133  }
134 
135  // Initialise peripherals
137 
138  nStatus = System_TIM1_Init();
139  if (0 != nStatus)
140  {
141  return nStatus;
142  }
143 
144  if (HAL_OK != HAL_TIM_Base_Start(&htim1))
145  {
146  return -1;
147  }
148 
149  nStatus = System_ADC1_Init();
150  if (0 != nStatus)
151  {
152  return nStatus;
153  }
154 
155  nStatus = System_I2C2_Init();
156  if (0 != nStatus)
157  {
158  return nStatus;
159  }
160 
161  nStatus = System_SPI1_Init();
162  if (0 != nStatus)
163  {
164  return nStatus;
165  }
166 
167  nStatus = System_RTC_Init();
168 
169  return nStatus;
170 }
171 
172 /**
173  * @brief GPIO Initialisation Function
174  */
175 static void System_GPIO_Init(void)
176 {
177  GPIO_InitTypeDef GPIO_InitStruct = { 0 };
178 
179  // GPIO Ports Clock Enable
180  __HAL_RCC_GPIOC_CLK_ENABLE();
181  __HAL_RCC_GPIOD_CLK_ENABLE();
182  __HAL_RCC_GPIOA_CLK_ENABLE();
183  __HAL_RCC_GPIOB_CLK_ENABLE();
184 
185  // LED
186  HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET);
187 
188  GPIO_InitStruct.Pin = LED_Pin;
189  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
190  GPIO_InitStruct.Pull = GPIO_PULLUP;
191  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
192 
193  HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
194 
195  // Dot Matrix Display
196  GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3;
197  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
198  GPIO_InitStruct.Pull = GPIO_PULLUP;
199  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
200 
201  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
202 }
203 
204 /**
205  * @brief Timer 1 Initialisation Function
206  * @return Error code
207  * @retval 0: OK
208  * @retval -1: Error
209  */
210 static int System_TIM1_Init(void)
211 {
212  TIM_ClockConfigTypeDef sClockSourceConfig = { 0 };
213  TIM_MasterConfigTypeDef sMasterConfig = { 0 };
214  TIM_OC_InitTypeDef sConfigOC = { 0 };
215  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = { 0 };
216 
217  extern void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim);
218 
219  htim1.Instance = TIM1;
220  htim1.Init.Prescaler = 72-1;
221  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
222  htim1.Init.Period = 0xFFFF-1;
223  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
224  htim1.Init.RepetitionCounter = 0;
225  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
226 
227  if (HAL_OK != HAL_TIM_Base_Init(&htim1))
228  {
229  return -1;
230  }
231 
232  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
233 
234  if (HAL_OK != HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig))
235  {
236  return -1;
237  }
238 
239  if (HAL_OK != HAL_TIM_OC_Init(&htim1))
240  {
241  return -1;
242  }
243 
244  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
245  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
246 
247  if (HAL_OK != HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig))
248  {
249  return -1;
250  }
251 
252  sConfigOC.OCMode = TIM_OCMODE_FORCED_ACTIVE;
253  sConfigOC.Pulse = 0;
254  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
255  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
256  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
257  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
258  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
259 
260  if (HAL_OK != HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1))
261  {
262  return -1;
263  }
264 
265  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
266  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
267  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
268  sBreakDeadTimeConfig.DeadTime = 0;
269  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
270  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
271  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
272 
273  if (HAL_OK != HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig))
274  {
275  return -1;
276  }
277 
279 
280  return 0;
281 }
282 
283 /**
284  * @brief ADC 1 Initialisation Function
285  * @return Error code
286  * @retval 0: OK
287  * @retval -1: Error
288  */
289 static int System_ADC1_Init(void)
290 {
291  ADC_ChannelConfTypeDef sConfig = { 0 };
292 
293  // Common config
294  hadc1.Instance = ADC1;
295  hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
296  hadc1.Init.ContinuousConvMode = DISABLE;
297  hadc1.Init.DiscontinuousConvMode = DISABLE;
298  hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
299  hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
300  hadc1.Init.NbrOfConversion = 4;
301 
302  if (HAL_OK != HAL_ADC_Init(&hadc1))
303  {
304  return -1;
305  }
306 
307  // Configure Regular Channel
308  sConfig.Channel = ADC_CHANNEL_8;
309  sConfig.Rank = ADC_REGULAR_RANK_1;
310  sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
311 
312  if (HAL_OK != HAL_ADC_ConfigChannel(&hadc1, &sConfig))
313  {
314  return -1;
315  }
316 
317  // Configure Regular Channel
318  sConfig.Channel = ADC_CHANNEL_9;
319  sConfig.Rank = ADC_REGULAR_RANK_2;
320 
321  if (HAL_OK != HAL_ADC_ConfigChannel(&hadc1, &sConfig))
322  {
323  return -1;
324  }
325 
326  // Configure Regular Channel
327  sConfig.Channel = ADC_CHANNEL_8;
328  sConfig.Rank = ADC_REGULAR_RANK_3;
329 
330  if (HAL_OK != HAL_ADC_ConfigChannel(&hadc1, &sConfig))
331  {
332  return -1;
333  }
334 
335  // Configure Regular Channel
336  sConfig.Rank = ADC_REGULAR_RANK_4;
337 
338  if (HAL_OK != HAL_ADC_ConfigChannel(&hadc1, &sConfig))
339  {
340  return -1;
341  }
342 
343  return 0;
344 }
345 
346 /**
347  * @brief I²C 2 Initialisation Function
348  * @return Error code
349  * @retval 0: OK
350  * @retval -1: Error
351  */
352 static int System_I2C2_Init(void)
353 {
354  hi2c2.Instance = I2C2;
355  hi2c2.Init.ClockSpeed = 100000;
356  hi2c2.Init.DutyCycle = I2C_DUTYCYCLE_2;
357  hi2c2.Init.OwnAddress1 = 0;
358  hi2c2.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
359  hi2c2.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
360  hi2c2.Init.OwnAddress2 = 0;
361  hi2c2.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
362  hi2c2.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
363 
364  if (HAL_OK != HAL_I2C_Init(&hi2c2))
365  {
366  return -1;
367  }
368 
369  return 0;
370 }
371 
372 /**
373  * @brief SPI 1 Initialisation Function
374  * @return Error code
375  * @retval 0: OK
376  * @retval -1: Error
377  */
378 static int System_SPI1_Init(void)
379 {
380  // SPI1 parameter configuration
381  hspi1.Instance = SPI1;
382  hspi1.Init.Mode = SPI_MODE_MASTER;
383  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
384  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
385  hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
386  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
387  hspi1.Init.NSS = SPI_NSS_SOFT;
388  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
389  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
390  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
391  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
392  hspi1.Init.CRCPolynomial = 10;
393 
394  if (HAL_OK != HAL_SPI_Init(&hspi1))
395  {
396  return -1;
397  }
398 
399  return 0;
400 }
401 
402 /**
403  * @brief RTC Initialisation Function
404  * @return Error code
405  * @retval 0: OK
406  * @retval -1: Error
407  */
408 static int System_RTC_Init(void)
409 {
410  RTC_TimeTypeDef sTime = { 0 };
411  RTC_DateTypeDef DateToUpdate = { 0 };
412 
413  // Initialise RTC Only
414  hrtc.Instance = RTC;
415  hrtc.Init.AsynchPrediv = RTC_AUTO_1_SECOND;
416  hrtc.Init.OutPut = RTC_OUTPUTSOURCE_NONE;
417 
418  if (HAL_OK != HAL_RTC_Init(&hrtc))
419  {
420  return -1;
421  }
422 
423  // Initialise RTC and set the Time and Date
424  sTime.Hours = 6;
425  sTime.Minutes = 30;
426  sTime.Seconds = 0;
427 
428  if (HAL_OK != HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN))
429  {
430  return -1;
431  }
432 
433  DateToUpdate.WeekDay = RTC_WEEKDAY_SATURDAY;
434  DateToUpdate.Month = RTC_MONTH_NOVEMBER;
435  DateToUpdate.Date = 23;
436  DateToUpdate.Year = 96;
437 
438  if (HAL_OK != HAL_RTC_SetDate(&hrtc, &DateToUpdate, RTC_FORMAT_BIN))
439  {
440  return -1;
441  }
442 
443  return 0;
444 }
445 
446 void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);
447 
448 /**
449  * @brief Initialis the Global MSP.
450  */
451 void HAL_MspInit(void)
452 {
453  __HAL_RCC_AFIO_CLK_ENABLE();
454  __HAL_RCC_PWR_CLK_ENABLE();
455 
456  /* System interrupt init
457  * PendSV_IRQn interrupt configuration
458  */
459  HAL_NVIC_SetPriority(PendSV_IRQn, 15, 0);
460 
461  // NOJTAG: JTAG-DP Disabled and SW-DP Enabled
462  __HAL_AFIO_REMAP_SWJ_NOJTAG();
463 }
464 
465 /**
466  * @brief ADC MSP Initialisation
467  * @param hadc: ADC handle pointer
468  */
469 void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
470 {
471  GPIO_InitTypeDef GPIO_InitStruct = { 0 };
472 
473  if(ADC1 == hadc->Instance)
474  {
475  // Peripheral clock enable
476  __HAL_RCC_ADC1_CLK_ENABLE();
477 
478  __HAL_RCC_GPIOB_CLK_ENABLE();
479 
480  /* ADC1 GPIO Configuration
481  *
482  * PB0 ---> ADC1_IN8
483  * PB1 ---> ADC1_IN9
484  */
485  GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
486  GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
487  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
488 
489  // ADC1 interrupt Init
490  HAL_NVIC_SetPriority(ADC1_2_IRQn, 5, 0);
491  HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
492  }
493 }
494 
495 /**
496  * @brief ADC MSP De-Initialisation
497  * @param hadc: ADC handle pointer
498  */
499 void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
500 {
501  if (ADC1 == hadc->Instance)
502  {
503  // Peripheral clock disable
504  __HAL_RCC_ADC1_CLK_DISABLE();
505 
506  /* ADC1 GPIO Configuration
507  *
508  * PB0 ---> ADC1_IN8
509  * PB1 ---> ADC1_IN9
510  */
511  HAL_GPIO_DeInit(GPIOB, GPIO_PIN_0 | GPIO_PIN_1);
512 
513  // ADC1 interrupt DeInit
514  HAL_NVIC_DisableIRQ(ADC1_2_IRQn);
515  }
516 }
517 
518 /**
519  * @brief I2C MSP Initialisation
520  * @param hi2c: I2C handle pointer
521  */
522 void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
523 {
524  GPIO_InitTypeDef GPIO_InitStruct = { 0 };
525  if(I2C2 == hi2c->Instance)
526  {
527  __HAL_RCC_GPIOB_CLK_ENABLE();
528 
529  /* I2C2 GPIO Configuration
530  *
531  * PB10 ---> I2C2_SCL
532  * PB11 ---> I2C2_SDA
533  */
534  GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
535  GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
536  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
537  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
538 
539  // Peripheral clock enable
540  __HAL_RCC_I2C2_CLK_ENABLE();
541 
542  // I2C2 interrupt Init
543  HAL_NVIC_SetPriority(I2C2_EV_IRQn, 5, 0);
544  HAL_NVIC_EnableIRQ(I2C2_EV_IRQn);
545  HAL_NVIC_SetPriority(I2C2_ER_IRQn, 5, 0);
546  HAL_NVIC_EnableIRQ(I2C2_ER_IRQn);
547  }
548 }
549 
550 /**
551  * @brief I2C MSP De-Initialisation
552  * @param hi2c: I2C handle pointer
553  */
554 void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
555 {
556  if(I2C2 == hi2c->Instance)
557  {
558  // Peripheral clock disable
559  __HAL_RCC_I2C2_CLK_DISABLE();
560 
561  /* I2C2 GPIO Configuration
562  *
563  * PB10 ---> I2C2_SCL
564  * PB11 ---> I2C2_SDA
565  */
566  HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11);
567 
568  // I2C2 interrupt DeInit
569  HAL_NVIC_DisableIRQ(I2C2_EV_IRQn);
570  HAL_NVIC_DisableIRQ(I2C2_ER_IRQn);
571  }
572 }
573 
574 /**
575  * @brief SPI MSP Initialisation
576  * @param hspi: SPI handle pointer
577  */
578 void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
579 {
580  GPIO_InitTypeDef GPIO_InitStruct = {0};
581 
582  if(SPI1 == hspi->Instance)
583  {
584  // Peripheral clock enable
585  __HAL_RCC_SPI1_CLK_ENABLE();
586 
587  __HAL_RCC_GPIOA_CLK_ENABLE();
588 
589  /* SPI1 GPIO Configuration
590  *
591  * PA5 ---> SPI1_SCK
592  * PA6 ---> SPI1_MISO
593  * PA7 ---> SPI1_MOSI
594  */
595  GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_7;
596  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
597  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
598  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
599 
600  GPIO_InitStruct.Pin = GPIO_PIN_6;
601  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
602  GPIO_InitStruct.Pull = GPIO_NOPULL;
603  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
604 
605  // SPI1 interrupt Init
606  HAL_NVIC_SetPriority(SPI1_IRQn, 5, 0);
607  HAL_NVIC_EnableIRQ(SPI1_IRQn);
608  }
609 }
610 
611 /**
612  * @brief SPI MSP De-Initialisation
613  * @param hspi: SPI handle pointer
614  */
615 void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
616 {
617  if(SPI1 == hspi->Instance)
618  {
619  // Peripheral clock disable
620  __HAL_RCC_SPI1_CLK_DISABLE();
621 
622  /* SPI1 GPIO Configuration
623  *
624  * PA5 ---> SPI1_SCK
625  * PA6 ---> SPI1_MISO
626  * PA7 ---> SPI1_MOSI
627  */
628  HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
629 
630  // SPI1 interrupt DeInit
631  HAL_NVIC_DisableIRQ(SPI1_IRQn);
632  }
633 }
634 
635 /**
636  * @brief TIM_Base MSP Initialisation
637  * @param htim_base: TIM_Base handle pointer
638  */
639 void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
640 {
641  if(TIM1 == htim_base->Instance)
642  {
643  // Peripheral clock enable
644  __HAL_RCC_TIM1_CLK_ENABLE();
645  }
646 }
647 
648 /**
649  * @brief TIM MSP Post-Initialisation
650  * @param htim: TIM handle pointer
651  */
652 void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
653 {
654  GPIO_InitTypeDef GPIO_InitStruct = { 0 };
655 
656  if(TIM1 == htim->Instance)
657  {
658  __HAL_RCC_GPIOA_CLK_ENABLE();
659  /* TIM1 GPIO Configuration
660  *
661  * PA8 ---> TIM1_CH1
662  */
663  GPIO_InitStruct.Pin = GPIO_PIN_8;
664  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
665  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
666  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
667  }
668 }
669 
670 /**
671  * @brief TIM_Base MSP De-Initialisation
672  * @param htim_base: TIM_Base handle pointer
673  */
674 void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
675 {
676  if(TIM1 == htim_base->Instance)
677  {
678  // Peripheral clock disable
679  __HAL_RCC_TIM1_CLK_DISABLE();
680  }
681 }
682 
683 /**
684  * @brief RTC MSP Initialisation
685  * @param hrtc: RTC handle pointer
686  */
687 void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
688 {
689  if (RTC == hrtc->Instance)
690  {
691  HAL_PWR_EnableBkUpAccess();
692 
693  // Enable BKP CLK enable for backup registers
694  __HAL_RCC_BKP_CLK_ENABLE();
695 
696  // Peripheral clock enable
697  __HAL_RCC_RTC_ENABLE();
698 
699  // RTC interrupt Init
700  HAL_NVIC_SetPriority(RTC_IRQn, 5, 0);
701  HAL_NVIC_EnableIRQ(RTC_IRQn);
702  }
703 }
704 
705 /**
706  * @brief RTC MSP De-Initialisation
707  * @param hrtc: RTC handle pointer
708  */
709 void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
710 {
711  if (RTC == hrtc->Instance)
712  {
713  // Peripheral clock disable
714  __HAL_RCC_RTC_DISABLE();
715 
716  // RTC interrupt DeInit
717  HAL_NVIC_DisableIRQ(RTC_IRQn);
718  }
719 }
720 
721 /**
722  * @brief This function configures the TIM4 as a time base source.
723  * The time source is configured to have 1ms time base with a
724  * dedicated Tick interrupt priority.
725  * @note This function is called automatically at the beginning of
726  * program after reset by HAL_Init() or at any time when clock
727  * is configured, by HAL_RCC_ClockConfig().
728  * @param TickPriority: Tick interrupt priority.
729  * @retval HAL status
730  */
731 HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
732 {
733  RCC_ClkInitTypeDef clkconfig;
734  uint32_t uwTimclock = 0;
735  uint32_t uwPrescalerValue = 0;
736  uint32_t pFLatency;
737 
738  // Configure the TIM4 IRQ priority
739  HAL_NVIC_SetPriority(TIM4_IRQn, TickPriority, 0);
740 
741  // Enable the TIM4 global Interrupt
742  HAL_NVIC_EnableIRQ(TIM4_IRQn);
743 
744  // Enable TIM4 clock
745  __HAL_RCC_TIM4_CLK_ENABLE();
746 
747  // Get clock configuration
748  HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
749 
750  // Compute TIM4 clock
751  uwTimclock = 2*HAL_RCC_GetPCLK1Freq();
752 
753  /* Compute the prescaler value to have TIM4 counter clock equal to
754  * 1MHz
755  */
756  uwPrescalerValue = (uint32_t) ((uwTimclock / 1000000) - 1);
757 
758  // Initialise TIM4
759  htim4.Instance = TIM4;
760 
761  /* Initialise TIMx peripheral as follow:
762  * + Period = [(TIM4CLK/1000) - 1]. to have a (1/1000) s time base.
763  * + Prescaler = (uwTimclock/1000000 - 1) to have a 1MHz counter clock.
764  * + ClockDivision = 0
765  * + Counter direction = Up
766  */
767  htim4.Init.Period = (1000000 / 1000) - 1;
768  htim4.Init.Prescaler = uwPrescalerValue;
769  htim4.Init.ClockDivision = 0;
770  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
771 
772  if(HAL_OK == HAL_TIM_Base_Init(&htim4))
773  {
774  // Start the TIM time Base generation in interrupt mode
775  return HAL_TIM_Base_Start_IT(&htim4);
776  }
777 
778  // Return function status
779  return HAL_ERROR;
780 }
781 
782 /**
783  * @brief Suspend Tick increment.
784  * @note Disable the tick increment by disabling TIM4 update
785  * interrupt.
786  */
787 void HAL_SuspendTick(void)
788 {
789  // Disable TIM4 update Interrupt
790  __HAL_TIM_DISABLE_IT(&htim4, TIM_IT_UPDATE);
791 }
792 
793 /**
794  * @brief Resume Tick increment.
795  * @note Enable the tick increment by Enabling TIM4 update interrupt.
796  */
797 void HAL_ResumeTick(void)
798 {
799  // Enable TIM4 Update interrupt
800  __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_UPDATE);
801 }
802 
803 /**
804  * @brief Non-maskable interrupt handler
805  */
806 void NMI_Handler(void)
807 {
808 }
809 
810 /**
811  * @brief Hard fault handler
812  */
814 {
815  while (1) {}
816 }
817 
818 /**
819  * @brief Memory management fault handler
820  */
822 {
823  while (1) {}
824 }
825 
826 /**
827  * @brief Prefetch fault, memory access fault handler
828  */
830 {
831  while (1) {}
832 }
833 
834 /**
835  * @brief Undefined instruction or illegal state handler
836  */
838 {
839  while (1) {}
840 }
841 
842 /**
843  * @brief Debug monitor handler
844  */
846 {
847 }
848 
849 /*
850  * STM32F1xx Peripheral Interrupt Handlers
851  * Add here the Interrupt Handlers for the used peripherals.
852  * For the available peripheral interrupt handler names, please refer to
853  * the startup file (startup_stm32f1xx.s).
854  */
855 
856 /**
857  * @brief ADC1 and ADC2 global interrupt handler
858  */
860 {
861  HAL_ADC_IRQHandler(&hadc1);
862 }
863 
864 /**
865  * @brief TIM4 global interrupt handler
866  */
867 void TIM4_IRQHandler(void)
868 {
869  HAL_TIM_IRQHandler(&htim4);
870 }
871 
872 /**
873  * @brief I2C2 event interrupt handler
874  */
876 {
877  HAL_I2C_EV_IRQHandler(&hi2c2);
878 }
879 
880 /**
881  * @brief I2C2 error interrupt handler.
882  */
884 {
885  HAL_I2C_ER_IRQHandler(&hi2c2);
886 }
887 
888 /**
889  * @brief SPI1 global interrupt handler
890  */
891 void SPI1_IRQHandler(void)
892 {
893  HAL_SPI_IRQHandler(&hspi1);
894 }
895 
896 /**
897  * @brief This function handles RTC global interrupt.
898  */
899 void RTC_IRQHandler(void)
900 {
901  HAL_RTCEx_RTCIRQHandler(&hrtc);
902 }
STM32F103C8T6 system initialisation.
static void System_GPIO_Init(void)
GPIO Initialisation Function.
Definition: System.c:175
void DebugMon_Handler(void)
Debug monitor handler.
Definition: System.c:845
void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
I2C MSP Initialisation.
Definition: System.c:522
#define LED_GPIO_Port
LED GPIO port.
Definition: System.h:16
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef *htim_base)
TIM_Base MSP De-Initialisation.
Definition: System.c:674
TIM_HandleTypeDef htim4
Timer 4 handle (Sys-Tick)
Definition: System.c:50
void MemManage_Handler(void)
Memory management fault handler.
Definition: System.c:821
ADC_HandleTypeDef hadc1
ADC 1 handle.
Definition: System.c:45
RTC_HandleTypeDef hrtc
RTC handle.
Definition: System.c:48
void HAL_ResumeTick(void)
Resume Tick increment.
Definition: System.c:797
void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)
SPI MSP Initialisation.
Definition: System.c:578
SPI_HandleTypeDef hspi1
SPI 1 handle.
Definition: System.c:47
#define LED_Pin
LED pin.
Definition: System.h:15
void HAL_ADC_MspInit(ADC_HandleTypeDef *hadc)
ADC MSP Initialisation.
Definition: System.c:469
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
This function configures the TIM4 as a time base source. The time source is configured to have 1ms ti...
Definition: System.c:731
void HAL_ADC_MspDeInit(ADC_HandleTypeDef *hadc)
ADC MSP De-Initialisation.
Definition: System.c:499
static int System_TIM1_Init(void)
Timer 1 Initialisation Function.
Definition: System.c:210
void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)
SPI MSP De-Initialisation.
Definition: System.c:615
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
Period elapsed callback in non blocking mode.
Definition: System.c:68
static int System_ADC1_Init(void)
ADC 1 Initialisation Function.
Definition: System.c:289
I2C_HandleTypeDef hi2c2
I²C 2 handle.
Definition: System.c:46
int System_Init(void)
System Initialisation Function.
Definition: System.c:82
void HAL_MspInit(void)
Initialis the Global MSP.
Definition: System.c:451
void NMI_Handler(void)
Non-maskable interrupt handler.
Definition: System.c:806
void HAL_SuspendTick(void)
Suspend Tick increment.
Definition: System.c:787
void HAL_RTC_MspDeInit(RTC_HandleTypeDef *hrtc)
RTC MSP De-Initialisation.
Definition: System.c:709
void HAL_RTC_MspInit(RTC_HandleTypeDef *hrtc)
RTC MSP Initialisation.
Definition: System.c:687
void I2C2_EV_IRQHandler(void)
I2C2 event interrupt handler.
Definition: System.c:875
void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
I2C MSP De-Initialisation.
Definition: System.c:554
void RTC_IRQHandler(void)
This function handles RTC global interrupt.
Definition: System.c:899
static int System_SPI1_Init(void)
SPI 1 Initialisation Function.
Definition: System.c:378
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim_base)
TIM_Base MSP Initialisation.
Definition: System.c:639
void ADC1_2_IRQHandler(void)
ADC1 and ADC2 global interrupt handler.
Definition: System.c:859
TIM_HandleTypeDef htim1
Timer 1 handle.
Definition: System.c:49
void HardFault_Handler(void)
Hard fault handler.
Definition: System.c:813
void TIM4_IRQHandler(void)
TIM4 global interrupt handler.
Definition: System.c:867
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim)
TIM MSP Post-Initialisation.
Definition: System.c:652
void I2C2_ER_IRQHandler(void)
I2C2 error interrupt handler.
Definition: System.c:883
static int System_I2C2_Init(void)
I²C 2 Initialisation Function.
Definition: System.c:352
static int System_RTC_Init(void)
RTC Initialisation Function.
Definition: System.c:408
void BusFault_Handler(void)
Prefetch fault, memory access fault handler.
Definition: System.c:829
void UsageFault_Handler(void)
Undefined instruction or illegal state handler.
Definition: System.c:837
void SPI1_IRQHandler(void)
SPI1 global interrupt handler.
Definition: System.c:891