123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file gpio.c
- * @brief This file provides code for the configuration
- * of all used GPIO pins.
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2023 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 "gpio.h"
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /*----------------------------------------------------------------------------*/
- /* Configure GPIO */
- /*----------------------------------------------------------------------------*/
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /** Configure pins as
- * Analog
- * Input
- * Output
- * EVENT_OUT
- * EXTI
- */
- void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOC_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_12|GPIO_PIN_3|GPIO_PIN_4
- |GPIO_PIN_5|GPIO_PIN_6, GPIO_PIN_RESET);
- /*Configure GPIO pins : PA0 PA1 PA2 PA3
- PA4 PA5 PA6 PA7 */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
- |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pins : PB0 PB12 PB3 PB4
- PB5 PB6 */
- GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_12|GPIO_PIN_3|GPIO_PIN_4
- |GPIO_PIN_5|GPIO_PIN_6;
- 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 2 */
- uint8_t GetPaInputStatus(void)
- {
- HAL_Delay(10);
- uint8_t gpio_idr_byte = GPIOA->IDR & 0xFF; // 获取GPIOA的低8位状态
- gpio_idr_byte ^= 0xFF;
- return gpio_idr_byte;
- }
- uint8_t GetPbOutputStatus(void)
- {
- HAL_Delay(10);
- uint8_t gpio_odr_byte = (GPIOB->ODR >> 3) & 0x0F; // 读取GPIOB的3-6位的状态
- return gpio_odr_byte;
- }
- void updatePbStatus(uint16_t temp)
- {
- if(temp == UINT16_MAX) {
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
- } else {
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, ((temp & 0x01) == 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, ((temp & 0x02) == 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, ((temp & 0x04) == 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
- HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, ((temp & 0x08) == 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
- }
- }
- // 计算CRC校验值
- uint16_t calculate_crc(uint8_t* buffer, uint8_t length)
- {
- uint16_t crc = 0xFFFF;
- for (int i = 0; i < length; i++) {
- crc ^= buffer[i];
- for (int j = 0; j < 8; j++) {
- if (crc & 0x0001) {
- crc = (crc >> 1) ^ 0xA001;
- } else {
- crc >>= 1;
- }
- }
- }
- return crc;
- }
- /* USER CODE END 2 */
|