Advertisement
Guest User

Untitled

a guest
Dec 20th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.87 KB | None | 0 0
  1. /**
  2. ******************************************************************************
  3. * @file main.c
  4. * @author Ac6
  5. * @version V1.0
  6. * @date 01-December-2013
  7. * @brief Default main function.
  8. ******************************************************************************
  9. */
  10.  
  11.  
  12. #include "stm32l0xx.h"
  13. #include "flash_if.h"
  14. #include "stm32l0xx_nucleo.h"
  15.  
  16. /* UART handler declaration */
  17. UART_HandleTypeDef UartHandle;
  18.  
  19. /* CRC handler declaration */
  20. CRC_HandleTypeDef CrcHandle;
  21. uint32_t vector_t[48];
  22.  
  23. /* Private function prototypes -----------------------------------------------*/
  24. static void COM_Init(void);
  25. void SystemClock_Config(void);
  26.  
  27. /**
  28. * @brief Interrupt vector table relocation
  29. * @param address: The original address in program memory
  30. * @retval None
  31. */
  32. void Relocate_NVIC(uint32_t address)
  33. {
  34. uint32_t i;
  35.  
  36. /* It is important to make sure the vector table stays put when playing with memory mapping */
  37. for (i = 0;i < 48;i++)
  38. {
  39. vector_t[i] = *((uint32_t*)(address + (i << 2)));
  40. }
  41. SCB->VTOR = (uint32_t)vector_t;
  42. }
  43.  
  44. void Serial_PutString(uint8_t *p_string)
  45. {
  46. uint16_t length = 0;
  47.  
  48. while (p_string[length] != '\0')
  49. {
  50. length++;
  51. }
  52. HAL_UART_Transmit(&UartHandle, p_string, length, 100);
  53. }
  54.  
  55. int main(void)
  56. {
  57. /* Relocate vector table to RAM for safer context switch later */
  58. Relocate_NVIC(NVIC_VT_FLASH_B1);
  59.  
  60. __HAL_RCC_SYSCFG_CLK_ENABLE();
  61. /* STM32L0xx HAL library initialization:
  62. */
  63. HAL_Init();
  64.  
  65. /* Configure the system clock to 32 MHz */
  66. SystemClock_Config();
  67.  
  68. /* Initialization of the UART driver */
  69. COM_Init();
  70.  
  71. while(1){
  72. Serial_PutString((uint8_t *)"Waiting for the file to be sent ... (press 'a' to abort)\n\r");
  73. }
  74. }
  75.  
  76. void SystemClock_Config(void)
  77. {
  78. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  79. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  80.  
  81. /* Enable Power Control clock */
  82. __HAL_RCC_PWR_CLK_ENABLE();
  83.  
  84. /* The voltage scaling allows optimizing the power consumption when the device is
  85. clocked below the maximum system frequency, to update the voltage scaling value
  86. regarding system frequency refer to product datasheet. */
  87. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  88.  
  89. /* Disable Power Control clock */
  90. __HAL_RCC_PWR_CLK_DISABLE();
  91.  
  92. /* Enable HSE Oscillator */
  93. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  94. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  95. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  96. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  97. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8;
  98. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  99. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  100. {
  101. /* Initialization Error */
  102. while (1)
  103. {}
  104. }
  105.  
  106. /* Select PLL as system clock source and configure the HCLK, PCLK1 and PCLK2
  107. clocks dividers */
  108. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  109. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  110. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  111. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  112. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  113. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  114. {
  115. /* Initialization Error */
  116. while (1)
  117. {}
  118. }
  119. }
  120.  
  121. void COM_Init(void)
  122. {
  123. /* UART configured as follow:
  124. - BaudRate = 115200 baud
  125. - Word Length = 8 Bits
  126. - One Stop Bit
  127. - No parity
  128. - Hardware flow control disabled (RTS and CTS signals)
  129. - Receive and transmit enabled
  130. */
  131. UartHandle.Instance = USART1;
  132. UartHandle.Init.BaudRate = 115200;
  133. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  134. UartHandle.Init.StopBits = UART_STOPBITS_1;
  135. UartHandle.Init.Parity = UART_PARITY_NONE;
  136. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  137. UartHandle.Init.Mode = UART_MODE_TX_RX;
  138.  
  139.  
  140.  
  141. __HAL_RCC_CRC_CLK_ENABLE();
  142.  
  143. /* CRC configured */
  144. CrcHandle.Instance = CRC;
  145.  
  146. /* The CRC-16-CCIT polynomial is used */
  147. CrcHandle.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
  148. CrcHandle.Init.GeneratingPolynomial = 0x1021;
  149. CrcHandle.Init.CRCLength = CRC_POLYLENGTH_16B;
  150.  
  151. /* The zero init value is used */
  152. CrcHandle.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_DISABLE;
  153. CrcHandle.Init.InitValue = 0;
  154.  
  155. /* The input data are not inverted */
  156. CrcHandle.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_NONE;
  157.  
  158. /* The output data are not inverted */
  159. CrcHandle.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_DISABLE;
  160.  
  161. /* The input data are 32-bit long words */
  162. CrcHandle.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
  163.  
  164. if (HAL_CRC_Init(&CrcHandle) != HAL_OK)
  165. {
  166. /* Initialization Error */
  167. while (1)
  168. {}
  169. }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement