forked from logzhan/RobotHardware-UESTC
239 lines
7.1 KiB
C
239 lines
7.1 KiB
C
/* USER CODE BEGIN Header */
|
|
/**
|
|
******************************************************************************
|
|
* @file : main.c
|
|
* @brief : Main program body
|
|
******************************************************************************
|
|
* @attention
|
|
*
|
|
* Copyright (c) 2024 STMicroelectronics.
|
|
* All rights reserved.
|
|
*
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
* in the root directory of this software component.
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
*
|
|
******************************************************************************
|
|
*/
|
|
/* USER CODE END Header */
|
|
/* Includes ------------------------------------------------------------------*/
|
|
#include <stdio.h>
|
|
#include "main.h"
|
|
#include "usart.h"
|
|
#include "i2c.h"
|
|
#include "usb_device.h"
|
|
#include "usbd_customhid.h"
|
|
|
|
#define pb_printf(format,...) printf("" __FILE__ ":%d - " format "\r\n", __LINE__, ##__VA_ARGS__)
|
|
|
|
/* External variables --------------------------------------------------------*/
|
|
extern USBD_HandleTypeDef hUsbDeviceFS;
|
|
uint8_t report[64];
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
void SystemClock_Config(void);
|
|
void UsbSendPackageReport(Sensor_t* pack, uint8_t* report);
|
|
static void MX_GPIO_Init(void);
|
|
|
|
/**
|
|
* @brief The application entry point.
|
|
* @retval int
|
|
*/
|
|
int main(void)
|
|
{
|
|
HAL_Init();
|
|
SystemClock_Config();
|
|
MX_GPIO_Init();
|
|
HC_I2C_Init();
|
|
MX_USART3_UART_Init();
|
|
MX_USB_DEVICE_Init();
|
|
|
|
Sensor_t data;
|
|
memset(&data, 0, sizeof(data));
|
|
while (1)
|
|
{
|
|
HC_I2C_Write();
|
|
for(int i = 0; i < 12; i++){
|
|
GPIO_PinState pinStateA1 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1);
|
|
GPIO_PinState pinStateA2 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2);
|
|
GPIO_PinState pinStateA3 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3);
|
|
GPIO_PinState pinStateA4 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4);
|
|
HAL_Delay(10);
|
|
GPIO_PinState pinStateAD1 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1);
|
|
GPIO_PinState pinStateAD2 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2);
|
|
GPIO_PinState pinStateAD3 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3);
|
|
GPIO_PinState pinStateAD4 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4);
|
|
|
|
if(pinStateA1 == pinStateAD1 && pinStateA1 == GPIO_PIN_RESET){
|
|
data.p[0] = 1;
|
|
}else{
|
|
data.p[0] = 0;
|
|
}
|
|
if(pinStateA1 == pinStateAD2 && pinStateA2 == GPIO_PIN_RESET){
|
|
data.p[1] = 1;
|
|
}else{
|
|
data.p[1] = 0;
|
|
}
|
|
if(pinStateA1 == pinStateAD3 && pinStateA3 == GPIO_PIN_RESET){
|
|
data.p[2] = 1;
|
|
}else{
|
|
data.p[2] = 0;
|
|
}
|
|
if(pinStateA1 == pinStateAD4 && pinStateA4 == GPIO_PIN_RESET){
|
|
data.p[3] = 1;
|
|
}
|
|
else{
|
|
data.p[3] = 0;
|
|
}
|
|
UsbSendPackageReport(&data, report);
|
|
pb_printf("Dist = %f mm, collision %d,%d,%d,%d\n", data.dist, pinStateAD1,pinStateAD2,pinStateAD3,pinStateAD4);
|
|
}
|
|
data.dist = HC_I2C_Read();
|
|
HAL_Delay(30);
|
|
for(int i = 0; i < 3; i++){
|
|
HAL_Delay(10);
|
|
UsbSendPackageReport(&data, report);
|
|
}
|
|
}
|
|
/* USER CODE END 3 */
|
|
}
|
|
|
|
//void ReadCollisionGPIO(uint8_t* info){
|
|
// GPIO_PinState pinStateA1 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_1);
|
|
// GPIO_PinState pinStateA2 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_2);
|
|
// GPIO_PinState pinStateA3 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_3);
|
|
// GPIO_PinState pinStateA4 = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_4);
|
|
//}
|
|
|
|
|
|
/**
|
|
* @brief 把UWB的测距包信息拷贝到缓存中并发送
|
|
* @date 2024/04/17
|
|
* @author Zhanli. 719901725@qq.com
|
|
* @retval None
|
|
*/
|
|
void UsbSendPackageReport(Sensor_t* pack, uint8_t* report){
|
|
memset(report, 0, 64);
|
|
memcpy(report, pack, sizeof(Sensor_t));
|
|
// USB发送报文
|
|
USBD_CUSTOM_HID_SendReport(&hUsbDeviceFS, report, 64);
|
|
}
|
|
|
|
/**
|
|
* @brief System Clock Configuration
|
|
* @retval None
|
|
*/
|
|
void SystemClock_Config(void)
|
|
{
|
|
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
|
|
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
|
|
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
|
|
|
|
/** Initializes the RCC Oscillators according to the specified parameters
|
|
* in the RCC_OscInitTypeDef structure.
|
|
*/
|
|
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
|
|
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
|
|
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
|
|
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
|
|
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
|
|
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
|
|
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
|
|
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
|
|
/** Initializes the CPU, AHB and APB buses clocks
|
|
*/
|
|
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|
|
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
|
|
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
|
|
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
|
|
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
|
|
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
|
|
|
|
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
|
|
PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
|
|
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief GPIO Initialization Function
|
|
* @param None
|
|
* @retval None
|
|
*/
|
|
static void MX_GPIO_Init(void)
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
/* USER CODE BEGIN MX_GPIO_Init_1 */
|
|
/* USER CODE END MX_GPIO_Init_1 */
|
|
|
|
/* GPIO Ports Clock Enable */
|
|
__HAL_RCC_GPIOD_CLK_ENABLE();
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
__HAL_RCC_GPIOB_CLK_ENABLE();
|
|
|
|
/*Configure GPIO pin Output Level */
|
|
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1, GPIO_PIN_RESET);
|
|
|
|
/*Configure GPIO pins : PA1 PA2 PA3 PA4 */
|
|
GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/*Configure GPIO pins : PB0 PB1 */
|
|
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
|
|
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* USER CODE BEGIN MX_GPIO_Init_2 */
|
|
/* USER CODE END MX_GPIO_Init_2 */
|
|
}
|
|
|
|
/* USER CODE BEGIN 4 */
|
|
|
|
/* USER CODE END 4 */
|
|
|
|
/**
|
|
* @brief This function is executed in case of error occurrence.
|
|
* @retval None
|
|
*/
|
|
void Error_Handler(void)
|
|
{
|
|
/* USER CODE BEGIN Error_Handler_Debug */
|
|
/* User can add his own implementation to report the HAL error return state */
|
|
__disable_irq();
|
|
while (1)
|
|
{
|
|
}
|
|
/* USER CODE END Error_Handler_Debug */
|
|
}
|
|
|
|
#ifdef USE_FULL_ASSERT
|
|
/**
|
|
* @brief Reports the name of the source file and the source line number
|
|
* where the assert_param error has occurred.
|
|
* @param file: pointer to the source file name
|
|
* @param line: assert_param error line source number
|
|
* @retval None
|
|
*/
|
|
void assert_failed(uint8_t *file, uint32_t line)
|
|
{
|
|
/* USER CODE BEGIN 6 */
|
|
/* User can add his own implementation to report the file name and line number,
|
|
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
|
|
/* USER CODE END 6 */
|
|
}
|
|
#endif /* USE_FULL_ASSERT */
|