stm32f3_hal.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "stm32f3_hal.h"
  2. #include "stm32f3_hal_lowlevel.h"
  3. #include "stm32f3xx_hal_rcc.h"
  4. #include "stm32f3xx_hal_gpio.h"
  5. #include "stm32f3xx_hal_dma.h"
  6. #include "stm32f3xx_hal_uart.h"
  7. UART_HandleTypeDef UartHandle;
  8. void platform_init(void)
  9. {
  10. //HAL_Init();
  11. #ifdef USE_INTERNAL_CLK
  12. RCC_OscInitTypeDef RCC_OscInitStruct;
  13. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  14. RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  15. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  16. RCC_OscInitStruct.PLL.PLLSource = RCC_PLL_NONE;
  17. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  18. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  19. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  20. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  21. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  22. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  23. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  24. uint32_t flash_latency = 0;
  25. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
  26. #else
  27. RCC_OscInitTypeDef RCC_OscInitStruct;
  28. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI;
  29. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  30. RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
  31. RCC_OscInitStruct.PLL.PLLSource = RCC_PLL_NONE;
  32. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  33. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  34. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  35. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  36. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  37. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  38. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  39. uint32_t flash_latency = 0;
  40. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
  41. #endif
  42. #if (PLATFORM==CWLITEARM)
  43. __HAL_RCC_GPIOC_CLK_ENABLE();
  44. GPIO_InitTypeDef GpioInit;
  45. GpioInit.Pin = GPIO_PIN_13 | GPIO_PIN_14;
  46. GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
  47. GpioInit.Pull = GPIO_NOPULL;
  48. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  49. HAL_GPIO_Init(GPIOC, &GpioInit);
  50. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, SET);
  51. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, SET);
  52. #endif
  53. }
  54. void init_uart(void)
  55. {
  56. GPIO_InitTypeDef GpioInit;
  57. GpioInit.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  58. GpioInit.Mode = GPIO_MODE_AF_PP;
  59. GpioInit.Pull = GPIO_PULLUP;
  60. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  61. GpioInit.Alternate = GPIO_AF7_USART1;
  62. __GPIOA_CLK_ENABLE();
  63. HAL_GPIO_Init(GPIOA, &GpioInit);
  64. UartHandle.Instance = USART1;
  65. #if SS_VER==SS_VER_2_1
  66. UartHandle.Init.BaudRate = 230400;
  67. #else
  68. UartHandle.Init.BaudRate = 38400;
  69. #endif
  70. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  71. UartHandle.Init.StopBits = UART_STOPBITS_1;
  72. UartHandle.Init.Parity = UART_PARITY_NONE;
  73. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  74. UartHandle.Init.Mode = UART_MODE_TX_RX;
  75. __USART1_CLK_ENABLE();
  76. HAL_UART_Init(&UartHandle);
  77. }
  78. void trigger_setup(void)
  79. {
  80. __HAL_RCC_GPIOA_CLK_ENABLE();
  81. GPIO_InitTypeDef GpioInit;
  82. GpioInit.Pin = GPIO_PIN_12;
  83. GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
  84. GpioInit.Pull = GPIO_NOPULL;
  85. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  86. HAL_GPIO_Init(GPIOA, &GpioInit);
  87. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
  88. }
  89. void trigger_high(void)
  90. {
  91. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
  92. }
  93. void trigger_low(void)
  94. {
  95. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
  96. }
  97. char getch(void)
  98. {
  99. uint8_t d;
  100. while (HAL_UART_Receive(&UartHandle, &d, 1, 5000) != HAL_OK)
  101. USART1->ICR |= (1 << 3);
  102. //putch(d);
  103. return d;
  104. }
  105. void putch(char c)
  106. {
  107. uint8_t d = c;
  108. HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
  109. }
  110. #if (PLATFORM==CWLITEARM)
  111. void change_err_led(int x)
  112. {
  113. if (x)
  114. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, RESET);
  115. else
  116. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, SET);
  117. }
  118. void change_ok_led(int x)
  119. {
  120. if (x)
  121. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, RESET);
  122. else
  123. HAL_GPIO_WritePin(GPIOC, GPIO_PIN_14, SET);
  124. }
  125. #else
  126. void change_err_led(int x)
  127. {
  128. }
  129. void change_ok_led(int x)
  130. {
  131. }
  132. #endif //PLATFORM==CWLITEARM