Browse Source

Transfer Usart Init to board module.

Vladimir N. Shilov 3 years ago
parent
commit
9277e950ca
2 changed files with 84 additions and 135 deletions
  1. 59 54
      Src/board.c
  2. 25 81
      Src/main.c

+ 59 - 54
Src/board.c

@@ -1,4 +1,8 @@
 #include "board.h"
+#include "stm32g0xx_ll_rcc.h"
+#include "stm32g0xx_ll_bus.h"
+#include "stm32g0xx_ll_gpio.h"
+#include "stm32g0xx_ll_usart.h"
 
 /* private functions */
 static void TIM1_Init(void);
@@ -6,38 +10,11 @@ static void TIM3_Init(void);
 static void TIM14_Init(void);
 static void TIM16_Init(void);
 static void TIM17_Init(void);
+static void USART1_UART_Init(void);
 
 /* Board perephireal Configuration  */
 void Board_Init(void)
 {
-  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
-  RCC->APBENR2 |= RCC_APBENR2_SYSCFGEN;
-  RCC->APBENR1 |= RCC_APBENR1_PWREN;
-
-  /* Peripheral interrupt init*/
-  /* RCC_IRQn interrupt configuration */
-  NVIC_SetPriority(RCC_IRQn, 0);
-  NVIC_EnableIRQ(RCC_IRQn);
-
-  /* Configure the system clock */
-  SystemClock_Config();
-
-  /* Processor uses sleep as its low power mode */
-  SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
-  /* DisableSleepOnExit */
-  SCB->SCR &= ~((uint32_t)SCB_SCR_SLEEPONEXIT_Msk);
-
-  /* в дейтвительности нигде не используеться? */
-  //LL_RCC_GetSystemClocksFreq(&rcc_clocks); :::
-  /* Get SYSCLK frequency */
-  ///RCC_Clocks->SYSCLK_Frequency = RCC_GetSystemClockFreq();
-  /* HCLK clock frequency */
-  ///RCC_Clocks->HCLK_Frequency   = RCC_GetHCLKClockFreq(RCC_Clocks->SYSCLK_Frequency);
-  /* PCLK1 clock frequency */
-  ///RCC_Clocks->PCLK1_Frequency  = RCC_GetPCLK1ClockFreq(RCC_Clocks->HCLK_Frequency);
-
-  /* Initialize all configured peripherals */
-
   /* Start RGB & Tube Power PWM */
 	TIM1_Init();
   TIM3_Init();
@@ -46,6 +23,8 @@ void Board_Init(void)
 
   //TIM16_Init();
   //TIM17_Init();
+
+  USART1_UART_Init();
 }
 
 /**
@@ -55,49 +34,38 @@ void Board_Init(void)
 void SystemClock_Config(void)
 {
   /* HSI configuration and activation */
-  RCC->CR |= RCC_CR_HSIKERON; // Enable HSI even in stop mode
-  while((RCC->CR & RCC_CR_HSIRDY) == 0)
+  LL_RCC_HSI_Enable();
+  while(LL_RCC_HSI_IsReady() != 1)
   {
   }
 
   /* Main PLL configuration and activation */
-  RCC->PLLCFGR &= ~(RCC_PLLCFGR_PLLSRC | RCC_PLLCFGR_PLLM | RCC_PLLCFGR_PLLN | RCC_PLLCFGR_PLLR);
-  RCC->PLLCFGR |= (RCC_PLLCFGR_PLLSRC_HSI | (9 << RCC_PLLCFGR_PLLN_Pos) | RCC_PLLCFGR_PLLR_2 | RCC_PLLCFGR_PLLR_0);
-  RCC->PLLCFGR |= RCC_PLLCFGR_PLLREN; // RCC_PLL_EnableDomain_SYS
-  RCC->CR |= RCC_CR_PLLON; // RCC_PLL_Enable
-  while((RCC->CR & RCC_CR_PLLRDY) == 0)
+  LL_RCC_PLL_ConfigDomain_SYS(LL_RCC_PLLSOURCE_HSI, LL_RCC_PLLM_DIV_2, 9, LL_RCC_PLLR_DIV_3);
+  LL_RCC_PLL_Enable();
+  LL_RCC_PLL_EnableDomain_SYS();
+  while(LL_RCC_PLL_IsReady() != 1)
   {
   }
 
   /* Set AHB prescaler*/
-  //RCC->CFGR &= ~(RCC_CFGR_HPRE);
-  //RCC->CFGR |= 0x00000000U;
+  LL_RCC_SetAHBPrescaler(LL_RCC_SYSCLK_DIV_1);
 
   /* Sysclk activation on the main PLL */
-  RCC->CFGR &= RCC_CFGR_SW;
-  RCC->CFGR |= RCC_CFGR_SW_1;
-  while((RCC->CFGR & RCC_CFGR_SWS) != RCC_CFGR_SWS_1)
+  LL_RCC_SetSysClkSource(LL_RCC_SYS_CLKSOURCE_PLL);
+  while(LL_RCC_GetSysClkSource() != LL_RCC_SYS_CLKSOURCE_STATUS_PLL)
   {
   }
 
-  /* Set APB1 prescaler !!! uncorrect !!! */
-  //RCC->CFGR &= RCC_CFGR_PPRE;
-  //RCC->CFGR |= 0x00000000U;
+  /* Set APB1 prescaler*/
+  LL_RCC_SetAPB1Prescaler(LL_RCC_APB1_DIV_1);
 
-#ifdef USES_SYSTICK
-  /* Configure the SysTick to have interrupt in 1ms time base */
-  SysTick->LOAD  = (uint32_t)((24000000 / 1000) - 1UL);  /* set reload register */
-  SysTick->VAL   = 0UL;                                  /* Load the SysTick Counter Value */
-  SysTick->CTRL  = SysTick_CTRL_CLKSOURCE_Msk |
-                   SysTick_CTRL_ENABLE_Msk;              /* Enable the Systick Timer */
-#endif
+  LL_Init1msTick(24000000);
 
   /* Update CMSIS variable (which can be updated also through SystemCoreClockUpdate function) */
-  SystemCoreClock = 24000000;
+  LL_SetSystemCoreClock(24000000);
 
-  /* SetI2CClockSource */
-  RCC->CCIPR &= ~(RCC_CCIPR_I2C1SEL);
-  RCC->CCIPR |= RCC_CCIPR_I2C1SEL_1;
+  /* Select I2C clock source */
+  LL_RCC_SetI2CClockSource(LL_RCC_I2C1_CLKSOURCE_HSI);
 }
 
 /**
@@ -348,3 +316,40 @@ static void TIM17_Init(void)
   /* Enable IRQ */
   TIM17->DIER = TIM_DIER_UIE;
 }
+
+/**
+  * @brief USART1 Initialization Function
+  * @param None
+  * @retval None
+  */
+static void USART1_UART_Init(void)
+{
+  /* Peripheral clock enable */
+  RCC->APBENR2 |= RCC_APBENR2_USART1EN;
+  RCC->IOPENR |= RCC_IOPENR_GPIOBEN;
+
+  /**USART1 GPIO Configuration
+  PB6   ------> USART1_TX
+  PB7   ------> USART1_RX
+  */
+  GPIO_SetPinMode(GPIOB, GPIO_PIN_6, GPIO_MODE_AFF);
+  GPIO_SetPinSpeed(GPIOB, GPIO_PIN_6, GPIO_OSPEED_HI);
+
+  GPIO_SetPinMode(GPIOB, GPIO_PIN_7, GPIO_MODE_AFF);
+  GPIO_SetPinSpeed(GPIOB, GPIO_PIN_7, GPIO_OSPEED_HI);
+
+  /* USART1 interrupt Init */
+  NVIC_SetPriority(USART1_IRQn, 0);
+  NVIC_EnableIRQ(USART1_IRQn);
+
+  USART1->CR1 |= (USART_CR1_TE |USART_CR1_RE);
+  USART1->BRR = 138;
+
+  /* USART1 Enable */
+  USART1->CR1 |= USART_CR1_UE;
+
+  /* Polling USART1 initialisation */
+  while((!(USART1->ISR & USART_ISR_TEACK)) || (!(USART1->ISR & USART_ISR_REACK)))
+  {
+  }
+}

+ 25 - 81
Src/main.c

@@ -51,7 +51,7 @@ typedef enum {
 
 /* USER CODE BEGIN PV */
 volatile flag_t Flag = {0};
-//static LL_RCC_ClocksTypeDef rcc_clocks;
+static LL_RCC_ClocksTypeDef rcc_clocks;
 /**
  * Nixi Tube cathodes map in Byte Array:
  * {E0 E9 E8 E7 E6 E5 E4 E3}
@@ -94,7 +94,6 @@ static void MX_GPIO_Init(void);
 static void MX_DMA_Init(void);
 static void MX_I2C1_Init(void);
 static void MX_SPI1_Init(void);
-static void MX_USART1_UART_Init(void);
 /* USER CODE BEGIN PFP */
 static void showDigits(uint8_t * dig);
 static void sensor_Init(void);
@@ -121,19 +120,41 @@ int main(void)
 
   /* MCU Configuration--------------------------------------------------------*/
 
+  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
+
+  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_SYSCFG);
+  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
+
   /* System interrupt init*/
 
+  /* Peripheral interrupt init*/
+  /* RCC_IRQn interrupt configuration */
+  NVIC_SetPriority(RCC_IRQn, 0);
+  NVIC_EnableIRQ(RCC_IRQn);
+
   /* USER CODE BEGIN Init */
-	Board_Init();
+
   /* USER CODE END Init */
 
+  /* Configure the system clock */
+  SystemClock_Config();
+
+  /* USER CODE BEGIN SysInit */
+  LL_LPM_EnableSleep();
+  LL_LPM_DisableSleepOnExit();
+  LL_RCC_GetSystemClocksFreq(&rcc_clocks);
+  /* USER CODE END SysInit */
+
   /* Initialize all configured peripherals */
   MX_GPIO_Init();
   MX_DMA_Init();
   MX_I2C1_Init();
   MX_SPI1_Init();
-  MX_USART1_UART_Init();
   /* USER CODE BEGIN 2 */
+  /* Initialize onBoard Hardware */
+  Board_Init();
+
+  /* Initialize Scheduler */
   RTOS_Init();
 
   /* Initialize Event State Machine */
@@ -450,83 +471,6 @@ static void MX_SPI1_Init(void)
 
 }
 
-/**
-  * @brief USART1 Initialization Function
-  * @param None
-  * @retval None
-  */
-static void MX_USART1_UART_Init(void)
-{
-
-  /* USER CODE BEGIN USART1_Init 0 */
-
-  /* USER CODE END USART1_Init 0 */
-
-  LL_USART_InitTypeDef USART_InitStruct = {0};
-
-  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
-
-  /* Peripheral clock enable */
-  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_USART1);
-
-  LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
-  /**USART1 GPIO Configuration
-  PB6   ------> USART1_TX
-  PB7   ------> USART1_RX
-  */
-  GPIO_InitStruct.Pin = LL_GPIO_PIN_6;
-  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
-  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
-  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
-  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
-  GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
-  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
-  GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
-  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
-  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
-  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
-  GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
-  GPIO_InitStruct.Alternate = LL_GPIO_AF_0;
-  LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
-
-  /* USART1 interrupt Init */
-  NVIC_SetPriority(USART1_IRQn, 0);
-  NVIC_EnableIRQ(USART1_IRQn);
-
-  /* USER CODE BEGIN USART1_Init 1 */
-
-  /* USER CODE END USART1_Init 1 */
-  USART_InitStruct.PrescalerValue = LL_USART_PRESCALER_DIV1;
-  USART_InitStruct.BaudRate = 115200;
-  USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
-  USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
-  USART_InitStruct.Parity = LL_USART_PARITY_NONE;
-  USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
-  USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
-  USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
-  LL_USART_Init(USART1, &USART_InitStruct);
-  LL_USART_SetTXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
-  LL_USART_SetRXFIFOThreshold(USART1, LL_USART_FIFOTHRESHOLD_1_8);
-  LL_USART_DisableFIFO(USART1);
-  LL_USART_ConfigAsyncMode(USART1);
-
-  /* USER CODE BEGIN WKUPType USART1 */
-
-  /* USER CODE END WKUPType USART1 */
-
-  LL_USART_Enable(USART1);
-
-  /* Polling USART1 initialisation */
-  while((!(LL_USART_IsActiveFlag_TEACK(USART1))) || (!(LL_USART_IsActiveFlag_REACK(USART1))))
-  {
-  }
-  /* USER CODE BEGIN USART1_Init 2 */
-
-  /* USER CODE END USART1_Init 2 */
-
-}
-
 /**
   * Enable DMA controller clock
   */