stm32f0_hal.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "stm32f0_hal.h"
  2. #include "stm32f0_hal_lowlevel.h"
  3. #include "stm32f0xx_hal_rcc.h"
  4. #include "stm32f0xx_hal_gpio.h"
  5. #include "stm32f0xx_hal_dma.h"
  6. #include "stm32f0xx_hal_uart.h"
  7. #include "stm32f0xx_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);
  36. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
  37. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  38. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  39. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
  40. #endif
  41. }
  42. void init_uart(void)
  43. {
  44. __HAL_RCC_GPIOA_CLK_ENABLE();
  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_AF1_USART1;
  51. HAL_GPIO_Init(GPIOA, &GpioInit);
  52. __HAL_RCC_USART1_CLK_ENABLE();
  53. __HAL_RCC_USART1_CONFIG(RCC_USART1CLKSOURCE_SYSCLK);
  54. UartHandle.Instance = USART1;
  55. #if SS_VER==SS_VER_2_0
  56. UartHandle.Init.BaudRate = 230400;
  57. #else
  58. UartHandle.Init.BaudRate = 38400;
  59. #endif
  60. UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  61. UartHandle.Init.StopBits = UART_STOPBITS_1;
  62. UartHandle.Init.Parity = UART_PARITY_NONE;
  63. UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  64. UartHandle.Init.Mode = UART_MODE_TX_RX;
  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, 50) != HAL_OK)
  90. USART1->ICR |= (1 << 3); // make sure overrun error is cleared, otherwise can stall here
  91. return d;
  92. }
  93. void putch(char c)
  94. {
  95. uint8_t d = c;
  96. HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
  97. }