Advertisement
Guest User

Initiator

a guest
Aug 12th, 2021
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 21.16 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * @file           : main.c
  5.   * @brief          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22.  
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25.  
  26. //main ex
  27. #include "platform/port.h"
  28. app_t   app;
  29. //end of main ex
  30.  
  31. #include <stdio.h>
  32. #include <string.h>
  33.  
  34. #include "decadriver/deca_device_api.h"
  35. #include "decadriver/deca_regs.h"
  36. #include "platform/stdio.h"
  37. #include "platform/deca_spi.h"
  38. #include "platform/port.h"
  39. /* USER CODE END Includes */
  40.  
  41. /* Private typedef -----------------------------------------------------------*/
  42. /* USER CODE BEGIN PTD */
  43.  
  44. /* USER CODE END PTD */
  45.  
  46. /* Private define ------------------------------------------------------------*/
  47. /* USER CODE BEGIN PD */
  48.  
  49. /* Example application name and version to display. */
  50. #define APP_NAME "SS TWR INIT v1.3\r\n"
  51.  
  52. /* Inter-ranging delay period, in milliseconds. */
  53. #define RNG_DELAY_MS 1000
  54.  
  55. /* Default antenna delay values for 64 MHz PRF. See NOTE 2 below. */
  56. #define TX_ANT_DLY 16505
  57. #define RX_ANT_DLY 16505
  58.  
  59. /* UWB microsecond (uus) to device time unit (dtu, around 15.65 ps) conversion factor.
  60.  * 1 uus = 512 / 499.2 µs and 1 µs = 499.2 * 128 dtu. */
  61. #define UUS_TO_DWT_TIME 65536
  62.  
  63. /* Delay between frames, in UWB microseconds. See NOTE 1 below. */
  64. #define POLL_TX_TO_RESP_RX_DLY_UUS 140
  65. /* Receive response timeout. See NOTE 5 below. */
  66. #define RESP_RX_TIMEOUT_UUS 210
  67.  
  68. /* Speed of light in air, in metres per second. */
  69. #define SPEED_OF_LIGHT 299702547
  70.  
  71.  
  72. /* USER CODE END PD */
  73.  
  74. /* Private macro -------------------------------------------------------------*/
  75. /* USER CODE BEGIN PM */
  76.  
  77. /* USER CODE END PM */
  78.  
  79. /* Private variables ---------------------------------------------------------*/
  80. SPI_HandleTypeDef hspi1;
  81.  
  82. TIM_HandleTypeDef htim1;
  83.  
  84. UART_HandleTypeDef huart2;
  85.  
  86. /* USER CODE BEGIN PV */
  87. /* Default communication configuration. We use here EVK1000's mode 4. See NOTE 1 below. */
  88. static dwt_config_t config = {
  89.     5,               /* Channel number. */
  90.     DWT_PRF_64M,     /* Pulse repetition frequency. */
  91.     DWT_PLEN_128,    /* Preamble length. Used in TX only. */
  92.     DWT_PAC8,        /* Preamble acquisition chunk size. Used in RX only. */
  93.     9,               /* TX preamble code. Used in TX only. */
  94.     9,               /* RX preamble code. Used in RX only. */
  95.     0,               /* 0 to use standard SFD, 1 to use non-standard SFD. */
  96.     DWT_BR_6M8,      /* Data rate. */
  97.     DWT_PHRMODE_STD, /* PHY header mode. */
  98.  
  99.     (129 + 8 - 8)    /* SFD timeout (preamble length + 1 + SFD length - PAC size). Used in RX only. */
  100. };
  101.  
  102. /* Frames used in the ranging process. See NOTE 3 below. */
  103. static uint8 tx_poll_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'W', 'A', 'V', 'E', 0xE0, 0, 0};
  104. static uint8 rx_resp_msg[] = {0x41, 0x88, 0, 0xCA, 0xDE, 'V', 'E', 'W', 'A', 0xE1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  105. /* Length of the common part of the message (up to and including the function code, see NOTE 3 below). */
  106. #define ALL_MSG_COMMON_LEN 10
  107. /* Indexes to access some of the fields in the frames defined above. */
  108. #define ALL_MSG_SN_IDX 2
  109. #define RESP_MSG_POLL_RX_TS_IDX 10
  110. #define RESP_MSG_RESP_TX_TS_IDX 14
  111. #define RESP_MSG_TS_LEN 4
  112. /* Frame sequence number, incremented after each transmission. */
  113. static uint8 frame_seq_nb = 0;
  114.  
  115. /* Buffer to store received response message.
  116.  * Its size is adjusted to longest frame that this example code is supposed to handle. */
  117. #define RX_BUF_LEN 20
  118. static uint8 rx_buffer[RX_BUF_LEN];
  119.  
  120. /* Hold copy of status register state here for reference so that it can be examined at a debug breakpoint. */
  121. static uint32 status_reg = 0;
  122.  
  123. /* Hold copies of computed time of flight and distance here for reference so that it can be examined at a debug breakpoint. */
  124. static double tof;
  125. static double distance;
  126.  
  127. /* String used to display measured distance over UART. */
  128. char dist_str[16] = {0};
  129.  
  130. /* Declaration of static functions. */
  131. static void resp_msg_get_ts(uint8 *ts_field, uint32 *ts);
  132.  
  133. uint8_t xtaltrim;
  134.  
  135.  
  136. /* USER CODE END PV */
  137.  
  138. /* Private function prototypes -----------------------------------------------*/
  139. void SystemClock_Config(void);
  140. static void MX_GPIO_Init(void);
  141. static void MX_USART2_UART_Init(void);
  142. static void MX_SPI1_Init(void);
  143. static void MX_TIM1_Init(void);
  144. /* USER CODE BEGIN PFP */
  145.  
  146. /* USER CODE END PFP */
  147.  
  148. /* Private user code ---------------------------------------------------------*/
  149. /* USER CODE BEGIN 0 */
  150.  
  151. /* USER CODE END 0 */
  152.  
  153. /**
  154.   * @brief  The application entry point.
  155.   * @retval int
  156.   */
  157. int main(void)
  158. {
  159.   /* USER CODE BEGIN 1 */
  160.  
  161.   /* USER CODE END 1 */
  162.  
  163.   /* MCU Configuration--------------------------------------------------------*/
  164.  
  165.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  166.   HAL_Init();
  167.  
  168.   /* USER CODE BEGIN Init */
  169.  
  170.   /* USER CODE END Init */
  171.  
  172.   /* Configure the system clock */
  173.   SystemClock_Config();
  174.  
  175.   /* USER CODE BEGIN SysInit */
  176.  
  177.   /* USER CODE END SysInit */
  178.  
  179.   /* Initialize all configured peripherals */
  180.   MX_GPIO_Init();
  181.   MX_USART2_UART_Init();
  182.   MX_SPI1_Init();
  183.   MX_TIM1_Init();
  184.   /* USER CODE BEGIN 2 */
  185.  
  186.   setup_DW1000RSTnIRQ(0);
  187.  
  188.   stdio_init(&huart2);
  189.  
  190.   HAL_TIM_Base_Init(&htim1);
  191.  
  192.   /* Display application name. */
  193.     stdio_write(APP_NAME);
  194.     stdio_write("\033[s"); // Save cursor position
  195.  
  196.     /* Reset and initialise DW1000.
  197.      * For initialisation, DW1000 clocks must be temporarily set to crystal speed. After initialisation SPI rate can be increased for optimum
  198.      * performance. */
  199.     reset_DW1000(); /* Target specific drive of RSTn line into DW1000 low for a period. */
  200.     port_set_dw1000_slowrate();
  201.     if (dwt_initialise(DWT_LOADUCODE) == DWT_ERROR)
  202.     {
  203.         stdio_write("INIT FAILED");
  204.         while (1)
  205.         { };
  206.     }
  207.     port_set_dw1000_fastrate();
  208.  
  209.     /* Configure DW1000. See NOTE 6 below. */
  210.     dwt_configure(&config);
  211.  
  212.     /* Apply default antenna delay value. See NOTE 2 below. */
  213.     dwt_setrxantennadelay(RX_ANT_DLY);
  214.     dwt_settxantennadelay(TX_ANT_DLY);
  215.  
  216.     /* Set expected response's delay and timeout. See NOTE 1 and 5 below.
  217.      * As this example only handles one incoming frame with always the same delay and timeout, those values can be set here once for all. */
  218.     dwt_setrxaftertxdelay(POLL_TX_TO_RESP_RX_DLY_UUS);
  219.     dwt_setrxtimeout(RESP_RX_TIMEOUT_UUS);
  220.  
  221. //    xtaltrim = dwt_getxtaltrim();
  222. //    dwt_setxtaltrim(31);
  223.  
  224.  
  225.   /* USER CODE END 2 */
  226.  
  227.   /* Infinite loop */
  228.   /* USER CODE BEGIN WHILE */
  229.   while (1)
  230.   {
  231.  
  232.  
  233.  
  234.       /* Write frame data to DW1000 and prepare transmission. See NOTE 7 below. */
  235.              tx_poll_msg[ALL_MSG_SN_IDX] = frame_seq_nb;
  236.              dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_TXFRS);
  237.              dwt_writetxdata(sizeof(tx_poll_msg), tx_poll_msg, 0); /* Zero offset in TX buffer. */
  238.              dwt_writetxfctrl(sizeof(tx_poll_msg), 0, 1); /* Zero offset in TX buffer, ranging. */
  239.  
  240.              /* Start transmission, indicating that a response is expected so that reception is enabled automatically after the frame is sent and the delay
  241.               * set by dwt_setrxaftertxdelay() has elapsed. */
  242.              dwt_starttx(DWT_START_TX_IMMEDIATE | DWT_RESPONSE_EXPECTED);
  243.  
  244.              /* We assume that the transmission is achieved correctly, poll for reception of a frame or error/timeout. See NOTE 8 below. */
  245.              while (!((status_reg = dwt_read32bitreg(SYS_STATUS_ID)) & (SYS_STATUS_RXFCG | SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR)))
  246.              { };
  247.  
  248.              /* Increment frame sequence number after transmission of the poll message (modulo 256). */
  249.              frame_seq_nb++;
  250.  
  251.              if (status_reg & SYS_STATUS_RXFCG)
  252.              {
  253.                  uint32 frame_len;
  254.  
  255.                  /* Clear good RX frame event in the DW1000 status register. */
  256.                  dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_RXFCG);
  257.  
  258.                  /* A frame has been received, read it into the local buffer. */
  259.                  frame_len = dwt_read32bitreg(RX_FINFO_ID) & RX_FINFO_RXFLEN_MASK;
  260.                  if (frame_len <= RX_BUF_LEN)
  261.                  {
  262.                      dwt_readrxdata(rx_buffer, frame_len, 0);
  263.                  }
  264.  
  265.                  /* Check that the frame is the expected response from the companion "SS TWR responder" example.
  266.                   * As the sequence number field of the frame is not relevant, it is cleared to simplify the validation of the frame. */
  267.                  rx_buffer[ALL_MSG_SN_IDX] = 0;
  268.                  if (memcmp(rx_buffer, rx_resp_msg, ALL_MSG_COMMON_LEN) == 0)
  269.                  {
  270.                      uint32 poll_tx_ts, resp_rx_ts, poll_rx_ts, resp_tx_ts;
  271.                      int32 rtd_init, rtd_resp;
  272.                      float clockOffsetRatio ;
  273.  
  274.                      /* Retrieve poll transmission and response reception timestamps. See NOTE 9 below. */
  275.                      poll_tx_ts = dwt_readtxtimestamplo32();
  276.                      resp_rx_ts = dwt_readrxtimestamplo32();
  277.  
  278.                      /* Read carrier integrator value and calculate clock offset ratio. See NOTE 11 below. */
  279.                      clockOffsetRatio = dwt_readcarrierintegrator() * (FREQ_OFFSET_MULTIPLIER * HERTZ_TO_PPM_MULTIPLIER_CHAN_2 / 1.0e6) ;
  280.  
  281.                      /* Get timestamps embedded in response message. */
  282.                      resp_msg_get_ts(&rx_buffer[RESP_MSG_POLL_RX_TS_IDX], &poll_rx_ts);
  283.                      resp_msg_get_ts(&rx_buffer[RESP_MSG_RESP_TX_TS_IDX], &resp_tx_ts);
  284.  
  285.                      /* Compute time of flight and distance, using clock offset ratio to correct for differing local and remote clock rates */
  286.                      rtd_init = resp_rx_ts - poll_tx_ts;
  287.                      rtd_resp = resp_tx_ts - poll_rx_ts;
  288.  
  289.                      tof = ((rtd_init - rtd_resp * (1 - clockOffsetRatio)) / 2.0) * DWT_TIME_UNITS;
  290.                      distance = tof * SPEED_OF_LIGHT;
  291.  
  292.                      /* Display computed distance. */
  293.                      sprintf(dist_str, "DIST: %3.2f m", distance);
  294.                      stdio_write(dist_str);
  295.                      stdio_write("\033[u"); // Restore last cursor position
  296.                  }
  297.              }
  298.              else
  299.              {
  300.                  /* Clear RX error/timeout events in the DW1000 status register. */
  301.                  dwt_write32bitreg(SYS_STATUS_ID, SYS_STATUS_ALL_RX_TO | SYS_STATUS_ALL_RX_ERR);
  302.  
  303.                  /* Reset RX to properly reinitialise LDE operation. */
  304.                  dwt_rxreset();
  305.              }
  306.  
  307.              /* Execute a delay between ranging exchanges. */
  308.              Sleep(RNG_DELAY_MS);
  309.  
  310.  
  311.     /* USER CODE END WHILE */
  312.  
  313.     /* USER CODE BEGIN 3 */
  314.   }
  315.   /* USER CODE END 3 */
  316. }
  317.  
  318. /**
  319.   * @brief System Clock Configuration
  320.   * @retval None
  321.   */
  322. void SystemClock_Config(void)
  323. {
  324.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  325.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  326.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  327.  
  328.   /** Initializes the RCC Oscillators according to the specified parameters
  329.   * in the RCC_OscInitTypeDef structure.
  330.   */
  331.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  332.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  333.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  334.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  335.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  336.   RCC_OscInitStruct.PLL.PLLM = 1;
  337.   RCC_OscInitStruct.PLL.PLLN = 10;
  338.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  339.   RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  340.   RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  341.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  342.   {
  343.     Error_Handler();
  344.   }
  345.   /** Initializes the CPU, AHB and APB buses clocks
  346.   */
  347.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  348.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  349.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  350.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  351.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  352.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  353.  
  354.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  355.   {
  356.     Error_Handler();
  357.   }
  358.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  359.   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  360.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  361.   {
  362.     Error_Handler();
  363.   }
  364.   /** Configure the main internal regulator output voltage
  365.   */
  366.   if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  367.   {
  368.     Error_Handler();
  369.   }
  370. }
  371.  
  372. /**
  373.   * @brief SPI1 Initialization Function
  374.   * @param None
  375.   * @retval None
  376.   */
  377. static void MX_SPI1_Init(void)
  378. {
  379.  
  380.   /* USER CODE BEGIN SPI1_Init 0 */
  381.  
  382.   /* USER CODE END SPI1_Init 0 */
  383.  
  384.   /* USER CODE BEGIN SPI1_Init 1 */
  385.  
  386.   /* USER CODE END SPI1_Init 1 */
  387.   /* SPI1 parameter configuration*/
  388.   hspi1.Instance = SPI1;
  389.   hspi1.Init.Mode = SPI_MODE_MASTER;
  390.   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  391.   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  392.   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  393.   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  394.   hspi1.Init.NSS = SPI_NSS_SOFT;
  395.   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
  396.   hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  397.   hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  398.   hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  399.   hspi1.Init.CRCPolynomial = 7;
  400.   hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  401.   hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  402.   if (HAL_SPI_Init(&hspi1) != HAL_OK)
  403.   {
  404.     Error_Handler();
  405.   }
  406.   /* USER CODE BEGIN SPI1_Init 2 */
  407.  
  408.   /* USER CODE END SPI1_Init 2 */
  409.  
  410. }
  411.  
  412. /**
  413.   * @brief TIM1 Initialization Function
  414.   * @param None
  415.   * @retval None
  416.   */
  417. static void MX_TIM1_Init(void)
  418. {
  419.  
  420.   /* USER CODE BEGIN TIM1_Init 0 */
  421.  
  422.   /* USER CODE END TIM1_Init 0 */
  423.  
  424.   TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  425.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  426.   TIM_OC_InitTypeDef sConfigOC = {0};
  427.   TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
  428.  
  429.   /* USER CODE BEGIN TIM1_Init 1 */
  430.  
  431.   /* USER CODE END TIM1_Init 1 */
  432.   htim1.Instance = TIM1;
  433.   htim1.Init.Prescaler = 0;
  434.   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  435.   htim1.Init.Period = 65535;
  436.   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  437.   htim1.Init.RepetitionCounter = 0;
  438.   htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  439.   if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  440.   {
  441.     Error_Handler();
  442.   }
  443.   if (HAL_TIM_OC_Init(&htim1) != HAL_OK)
  444.   {
  445.     Error_Handler();
  446.   }
  447.   sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
  448.   sSlaveConfig.InputTrigger = TIM_TS_ITR0;
  449.   if (HAL_TIM_SlaveConfigSynchro(&htim1, &sSlaveConfig) != HAL_OK)
  450.   {
  451.     Error_Handler();
  452.   }
  453.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  454.   sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  455.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  456.   if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  457.   {
  458.     Error_Handler();
  459.   }
  460.   sConfigOC.OCMode = TIM_OCMODE_TIMING;
  461.   sConfigOC.Pulse = 0;
  462.   sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  463.   sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  464.   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  465.   sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  466.   sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  467.   if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  468.   {
  469.     Error_Handler();
  470.   }
  471.   sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  472.   sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  473.   sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  474.   sBreakDeadTimeConfig.DeadTime = 0;
  475.   sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  476.   sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  477.   sBreakDeadTimeConfig.BreakFilter = 0;
  478.   sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
  479.   sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
  480.   sBreakDeadTimeConfig.Break2Filter = 0;
  481.   sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  482.   if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  483.   {
  484.     Error_Handler();
  485.   }
  486.   /* USER CODE BEGIN TIM1_Init 2 */
  487.  
  488.   /* USER CODE END TIM1_Init 2 */
  489.   HAL_TIM_MspPostInit(&htim1);
  490.  
  491. }
  492.  
  493. /**
  494.   * @brief USART2 Initialization Function
  495.   * @param None
  496.   * @retval None
  497.   */
  498. static void MX_USART2_UART_Init(void)
  499. {
  500.  
  501.   /* USER CODE BEGIN USART2_Init 0 */
  502.  
  503.   /* USER CODE END USART2_Init 0 */
  504.  
  505.   /* USER CODE BEGIN USART2_Init 1 */
  506.  
  507.   /* USER CODE END USART2_Init 1 */
  508.   huart2.Instance = USART2;
  509.   huart2.Init.BaudRate = 115200;
  510.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  511.   huart2.Init.StopBits = UART_STOPBITS_1;
  512.   huart2.Init.Parity = UART_PARITY_NONE;
  513.   huart2.Init.Mode = UART_MODE_TX_RX;
  514.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  515.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  516.   huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  517.   huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  518.   if (HAL_UART_Init(&huart2) != HAL_OK)
  519.   {
  520.     Error_Handler();
  521.   }
  522.   /* USER CODE BEGIN USART2_Init 2 */
  523.  
  524.   /* USER CODE END USART2_Init 2 */
  525.  
  526. }
  527.  
  528. /**
  529.   * @brief GPIO Initialization Function
  530.   * @param None
  531.   * @retval None
  532.   */
  533. static void MX_GPIO_Init(void)
  534. {
  535.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  536.  
  537.   /* GPIO Ports Clock Enable */
  538.   __HAL_RCC_GPIOC_CLK_ENABLE();
  539.   __HAL_RCC_GPIOH_CLK_ENABLE();
  540.   __HAL_RCC_GPIOA_CLK_ENABLE();
  541.   __HAL_RCC_GPIOB_CLK_ENABLE();
  542.  
  543.   /*Configure GPIO pin Output Level */
  544.   HAL_GPIO_WritePin(GPIOA, SMPS_EN_Pin|SMPS_V1_Pin|SMPS_SW_Pin|DW_RESET_Pin, GPIO_PIN_RESET);
  545.  
  546.   /*Configure GPIO pin Output Level */
  547.   HAL_GPIO_WritePin(LD4_GPIO_Port, LD4_Pin, GPIO_PIN_RESET);
  548.  
  549.   /*Configure GPIO pin Output Level */
  550.   HAL_GPIO_WritePin(DW_NSS_GPIO_Port, DW_NSS_Pin, GPIO_PIN_SET);
  551.  
  552.   /*Configure GPIO pin : B1_Pin */
  553.   GPIO_InitStruct.Pin = B1_Pin;
  554.   GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  555.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  556.   HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);
  557.  
  558.   /*Configure GPIO pins : SMPS_EN_Pin SMPS_V1_Pin SMPS_SW_Pin DW_NSS_Pin */
  559.   GPIO_InitStruct.Pin = SMPS_EN_Pin|SMPS_V1_Pin|SMPS_SW_Pin|DW_NSS_Pin;
  560.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  561.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  562.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  563.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  564.  
  565.   /*Configure GPIO pin : SMPS_PG_Pin */
  566.   GPIO_InitStruct.Pin = SMPS_PG_Pin;
  567.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  568.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  569.   HAL_GPIO_Init(SMPS_PG_GPIO_Port, &GPIO_InitStruct);
  570.  
  571.   /*Configure GPIO pin : LD4_Pin */
  572.   GPIO_InitStruct.Pin = LD4_Pin;
  573.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  574.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  575.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  576.   HAL_GPIO_Init(LD4_GPIO_Port, &GPIO_InitStruct);
  577.  
  578.   /*Configure GPIO pin : DW_RESET_Pin */
  579.   GPIO_InitStruct.Pin = DW_RESET_Pin;
  580.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
  581.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  582.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  583.   HAL_GPIO_Init(DW_RESET_GPIO_Port, &GPIO_InitStruct);
  584.  
  585.   /*Configure GPIO pin : DW_IRQn_Pin */
  586.   GPIO_InitStruct.Pin = DW_IRQn_Pin;
  587.   GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  588.   GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  589.   HAL_GPIO_Init(DW_IRQn_GPIO_Port, &GPIO_InitStruct);
  590.  
  591.   /* EXTI interrupt init*/
  592.   HAL_NVIC_SetPriority(EXTI15_10_IRQn, 0, 0);
  593.   HAL_NVIC_EnableIRQ(EXTI15_10_IRQn);
  594.  
  595. }
  596.  
  597. /* USER CODE BEGIN 4 */
  598. static void resp_msg_get_ts(uint8 *ts_field, uint32 *ts)
  599. {
  600.     int i;
  601.     *ts = 0;
  602.     for (i = 0; i < RESP_MSG_TS_LEN; i++)
  603.     {
  604.         *ts += ts_field[i] << (i * 8);
  605.     }
  606. }
  607. /* USER CODE END 4 */
  608.  
  609. /**
  610.   * @brief  This function is executed in case of error occurrence.
  611.   * @retval None
  612.   */
  613. void Error_Handler(void)
  614. {
  615.   /* USER CODE BEGIN Error_Handler_Debug */
  616.   /* User can add his own implementation to report the HAL error return state */
  617.   __disable_irq();
  618.   while (1)
  619.   {
  620.   }
  621.   /* USER CODE END Error_Handler_Debug */
  622. }
  623.  
  624. #ifdef  USE_FULL_ASSERT
  625. /**
  626.   * @brief  Reports the name of the source file and the source line number
  627.   *         where the assert_param error has occurred.
  628.   * @param  file: pointer to the source file name
  629.   * @param  line: assert_param error line source number
  630.   * @retval None
  631.   */
  632. void assert_failed(uint8_t *file, uint32_t line)
  633. {
  634.   /* USER CODE BEGIN 6 */
  635.   /* User can add his own implementation to report the file name and line number,
  636.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  637.   /* USER CODE END 6 */
  638. }
  639. #endif /* USE_FULL_ASSERT */
  640.  
  641. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  642.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement