stm32f2_hal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "stm32f2_hal.h"
  2. #include "stm32f2_hal_lowlevel.h"
  3. #include "stm32f2xx_hal_rcc.h"
  4. #include "stm32f2xx_hal_gpio.h"
  5. #include "stm32f2xx_hal_dma.h"
  6. #include "stm32f2xx_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 = 5;
  40. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
  41. #endif
  42. }
  43. void init_uart(void)
  44. {
  45. GPIO_InitTypeDef GpioInit;
  46. GpioInit.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  47. GpioInit.Mode = GPIO_MODE_AF_PP;
  48. GpioInit.Pull = GPIO_PULLUP;
  49. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  50. GpioInit.Alternate = GPIO_AF7_USART1;
  51. __GPIOA_CLK_ENABLE();
  52. HAL_GPIO_Init(GPIOA, &GpioInit);
  53. UartHandle.Instance = USART1;
  54. #if SS_VER==SS_VER_2_0
  55. UartHandle.Init.BaudRate = 230400;
  56. #else
  57. UartHandle.Init.BaudRate = 38400;
  58. #endif
  59. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  60. UartHandle.Init.StopBits = UART_STOPBITS_1;
  61. UartHandle.Init.Parity = UART_PARITY_NONE;
  62. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  63. UartHandle.Init.Mode = UART_MODE_TX_RX;
  64. __USART1_CLK_ENABLE();
  65. HAL_UART_Init(&UartHandle);
  66. }
  67. void trigger_setup(void)
  68. {
  69. __HAL_RCC_GPIOA_CLK_ENABLE();
  70. GPIO_InitTypeDef GpioInit;
  71. GpioInit.Pin = GPIO_PIN_12;
  72. GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
  73. GpioInit.Pull = GPIO_NOPULL;
  74. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  75. HAL_GPIO_Init(GPIOA, &GpioInit);
  76. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
  77. }
  78. void trigger_high(void)
  79. {
  80. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
  81. }
  82. void trigger_low(void)
  83. {
  84. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
  85. }
  86. char getch(void)
  87. {
  88. uint8_t d;
  89. while (HAL_UART_Receive(&UartHandle, &d, 1, 5000) != HAL_OK);
  90. return d;
  91. }
  92. void putch(char c)
  93. {
  94. uint8_t d = c;
  95. HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
  96. }