stm32f1_hal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "stm32f1_hal.h"
  2. #include "stm32f1_hal_lowlevel.h"
  3. #include "stm32f1xx_hal_rcc.h"
  4. #include "stm32f1xx_hal_gpio.h"
  5. #include "stm32f1xx_hal_dma.h"
  6. #include "stm32f1xx_hal_uart.h"
  7. #include "stm32f1xx_hal_flash.h"
  8. UART_HandleTypeDef UartHandle;
  9. void platform_init(void)
  10. {
  11. //HAL_Init();
  12. #ifdef USE_INTERNAL_CLK
  13. RCC_OscInitTypeDef RCC_OscInitStruct;
  14. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  15. RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
  16. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  17. RCC_OscInitStruct.PLL.PLLSource = RCC_PLL_NONE;
  18. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  19. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  20. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  21. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  22. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  23. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  24. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  25. uint32_t flash_latency = 0;
  26. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
  27. #else
  28. RCC_OscInitTypeDef RCC_OscInitStruct;
  29. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI;
  30. RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  31. RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
  32. RCC_OscInitStruct.PLL.PLLSource = RCC_PLL_NONE;
  33. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  34. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  35. RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
  36. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  37. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  38. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  39. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  40. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
  41. #endif
  42. }
  43. void init_uart(void)
  44. {
  45. __HAL_RCC_GPIOA_CLK_ENABLE();
  46. GPIO_InitTypeDef GpioInit;
  47. GpioInit.Pin = GPIO_PIN_9;
  48. GpioInit.Mode = GPIO_MODE_AF_PP;
  49. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  50. HAL_GPIO_Init(GPIOA, &GpioInit);
  51. GpioInit.Pin = GPIO_PIN_10;
  52. GpioInit.Mode = GPIO_MODE_AF_INPUT;
  53. HAL_GPIO_Init(GPIOA, &GpioInit);
  54. __HAL_RCC_USART1_CLK_ENABLE();
  55. UartHandle.Instance = USART1;
  56. #if SS_VER==SS_VER_2_0
  57. UartHandle.Init.BaudRate = 230400;
  58. #else
  59. UartHandle.Init.BaudRate = 38400;
  60. #endif
  61. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  62. UartHandle.Init.StopBits = UART_STOPBITS_1;
  63. UartHandle.Init.Parity = UART_PARITY_NONE;
  64. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  65. UartHandle.Init.Mode = UART_MODE_TX_RX;
  66. HAL_UART_Init(&UartHandle);
  67. }
  68. void trigger_setup(void)
  69. {
  70. __HAL_RCC_GPIOA_CLK_ENABLE();
  71. GPIO_InitTypeDef GpioInit;
  72. GpioInit.Pin = GPIO_PIN_12;
  73. GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
  74. GpioInit.Pull = GPIO_NOPULL;
  75. GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
  76. HAL_GPIO_Init(GPIOA, &GpioInit);
  77. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
  78. }
  79. void trigger_high(void)
  80. {
  81. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
  82. }
  83. void trigger_low(void)
  84. {
  85. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
  86. }
  87. char getch(void)
  88. {
  89. uint8_t d;
  90. while(HAL_UART_Receive(&UartHandle, &d, 1, 5000) != HAL_OK);
  91. return d;
  92. }
  93. void putch(char c)
  94. {
  95. uint8_t d = c;
  96. HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
  97. }