gpio.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file gpio.c
  5. * @brief This file provides code for the configuration
  6. * of all used GPIO pins.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "gpio.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. /*----------------------------------------------------------------------------*/
  25. /* Configure GPIO */
  26. /*----------------------------------------------------------------------------*/
  27. /* USER CODE BEGIN 1 */
  28. /* USER CODE END 1 */
  29. /** Configure pins as
  30. * Analog
  31. * Input
  32. * Output
  33. * EVENT_OUT
  34. * EXTI
  35. */
  36. void MX_GPIO_Init(void)
  37. {
  38. GPIO_InitTypeDef GPIO_InitStruct = {0};
  39. /* GPIO Ports Clock Enable */
  40. __HAL_RCC_GPIOD_CLK_ENABLE();
  41. __HAL_RCC_GPIOC_CLK_ENABLE();
  42. __HAL_RCC_GPIOA_CLK_ENABLE();
  43. __HAL_RCC_GPIOB_CLK_ENABLE();
  44. /*Configure GPIO pin Output Level */
  45. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_12|GPIO_PIN_3|GPIO_PIN_4
  46. |GPIO_PIN_5|GPIO_PIN_6, GPIO_PIN_RESET);
  47. /*Configure GPIO pins : PA0 PA1 PA2 PA3
  48. PA4 PA5 PA6 PA7 */
  49. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  50. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  51. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  52. GPIO_InitStruct.Pull = GPIO_NOPULL;
  53. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  54. /*Configure GPIO pins : PB0 PB12 PB3 PB4
  55. PB5 PB6 */
  56. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_12|GPIO_PIN_3|GPIO_PIN_4
  57. |GPIO_PIN_5|GPIO_PIN_6;
  58. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  59. GPIO_InitStruct.Pull = GPIO_NOPULL;
  60. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  61. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  62. }
  63. /* USER CODE BEGIN 2 */
  64. uint8_t GetPaInputStatus(void)
  65. {
  66. HAL_Delay(10);
  67. uint8_t gpio_idr_byte = GPIOA->IDR & 0xFF; // 获取GPIOA的低8位状态
  68. gpio_idr_byte ^= 0xFF;
  69. return gpio_idr_byte;
  70. }
  71. uint8_t GetPbOutputStatus(void)
  72. {
  73. HAL_Delay(10);
  74. uint8_t gpio_odr_byte = (GPIOB->ODR >> 3) & 0x0F; // 读取GPIOB的3-6位的状态
  75. return gpio_odr_byte;
  76. }
  77. void updatePbStatus(uint16_t temp)
  78. {
  79. if(temp == UINT16_MAX) {
  80. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
  81. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
  82. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
  83. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
  84. } else {
  85. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, ((temp & 0x01) == 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  86. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, ((temp & 0x02) == 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  87. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, ((temp & 0x04) == 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  88. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, ((temp & 0x08) == 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  89. }
  90. }
  91. // 计算CRC校验值
  92. uint16_t calculate_crc(uint8_t* buffer, uint8_t length)
  93. {
  94. uint16_t crc = 0xFFFF;
  95. for (int i = 0; i < length; i++) {
  96. crc ^= buffer[i];
  97. for (int j = 0; j < 8; j++) {
  98. if (crc & 0x0001) {
  99. crc = (crc >> 1) ^ 0xA001;
  100. } else {
  101. crc >>= 1;
  102. }
  103. }
  104. }
  105. return crc;
  106. }
  107. /* USER CODE END 2 */