123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- #include "stm32f1_hal.h"
- #include "stm32f1_hal_lowlevel.h"
- #include "stm32f1xx_hal_rcc.h"
- #include "stm32f1xx_hal_gpio.h"
- #include "stm32f1xx_hal_dma.h"
- #include "stm32f1xx_hal_uart.h"
- #include "stm32f1xx_hal_flash.h"
- UART_HandleTypeDef UartHandle;
- void platform_init(void)
- {
- //HAL_Init();
- #ifdef USE_INTERNAL_CLK
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSEState = RCC_HSE_OFF;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLL_NONE;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- uint32_t flash_latency = 0;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, flash_latency);
- #else
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE | RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
- RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLL_NONE;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0);
- #endif
- }
- void init_uart(void)
- {
- __HAL_RCC_GPIOA_CLK_ENABLE();
- GPIO_InitTypeDef GpioInit;
- GpioInit.Pin = GPIO_PIN_9;
- GpioInit.Mode = GPIO_MODE_AF_PP;
- GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GpioInit);
-
- GpioInit.Pin = GPIO_PIN_10;
- GpioInit.Mode = GPIO_MODE_AF_INPUT;
- HAL_GPIO_Init(GPIOA, &GpioInit);
- __HAL_RCC_USART1_CLK_ENABLE();
- UartHandle.Instance = USART1;
- #if SS_VER==SS_VER_2_0
- UartHandle.Init.BaudRate = 230400;
- #else
- UartHandle.Init.BaudRate = 38400;
- #endif
- UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
- UartHandle.Init.StopBits = UART_STOPBITS_1;
- UartHandle.Init.Parity = UART_PARITY_NONE;
- UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- UartHandle.Init.Mode = UART_MODE_TX_RX;
- HAL_UART_Init(&UartHandle);
- }
- void trigger_setup(void)
- {
- __HAL_RCC_GPIOA_CLK_ENABLE();
-
- GPIO_InitTypeDef GpioInit;
- GpioInit.Pin = GPIO_PIN_12;
- GpioInit.Mode = GPIO_MODE_OUTPUT_PP;
- GpioInit.Pull = GPIO_NOPULL;
- GpioInit.Speed = GPIO_SPEED_FREQ_HIGH;
- HAL_GPIO_Init(GPIOA, &GpioInit);
-
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
- }
- void trigger_high(void)
- {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, SET);
- }
- void trigger_low(void)
- {
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_12, RESET);
- }
- char getch(void)
- {
- uint8_t d;
- while(HAL_UART_Receive(&UartHandle, &d, 1, 5000) != HAL_OK);
- return d;
- }
- void putch(char c)
- {
- uint8_t d = c;
- HAL_UART_Transmit(&UartHandle, &d, 1, 5000);
- }
|