Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.91 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* USER CODE END Header */
  40.  
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "main.h"
  43.  
  44. /* Private includes ----------------------------------------------------------*/
  45. /* USER CODE BEGIN Includes */
  46. #include <string.h>
  47. #include <stdlib.h>
  48. #include <stdarg.h>
  49. /* USER CODE END Includes */
  50.  
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* USER CODE BEGIN PTD */
  53.  
  54. /* USER CODE END PTD */
  55.  
  56. /* Private define ------------------------------------------------------------*/
  57. /* USER CODE BEGIN PD */
  58. #define BUF_SIZE 256
  59. #define BUF_DATA_SIZE 512
  60.  
  61. /* USER CODE END PD */
  62.  
  63. /* Private macro -------------------------------------------------------------*/
  64. /* USER CODE BEGIN PM */
  65.  
  66. /* USER CODE END PM */
  67.  
  68. /* Private variables ---------------------------------------------------------*/
  69. ADC_HandleTypeDef hadc1;
  70. DMA_HandleTypeDef hdma_adc1;
  71. UART_HandleTypeDef huart2;
  72.  
  73. /* USER CODE BEGIN PV */
  74. volatile uint16_t Buf_Data[BUF_DATA_SIZE];
  75.  
  76. volatile uint8_t Buf_Rx[BUF_SIZE];
  77. volatile uint8_t Buf_Tx[BUF_SIZE];
  78. volatile uint8_t Busy_Rx = 0, Empty_Rx = 0, Busy_Tx = 0, Empty_Tx = 0;
  79. volatile uint8_t temp = 0;
  80. volatile uint8_t endFrameDetected = 0;
  81.  
  82. uint8_t peak_max = 0;
  83. uint16_t peak_min = 4095;
  84.  
  85. volatile uint8_t tryb;
  86. volatile uint8_t pomiar;
  87. volatile uint8_t tick;
  88. volatile uint8_t dataIndex;
  89.  
  90. uint16_t values[255];
  91.  
  92. volatile float voltage;
  93. volatile float avg;
  94.  
  95. struct ring_buffer *buffer;
  96. /* USER CODE END PV */
  97.  
  98. /* Private function prototypes -----------------------------------------------*/
  99. void SystemClock_Config(void);
  100. static void MX_GPIO_Init(void);
  101. static void MX_DMA_Init(void);
  102. static void MX_USART2_UART_Init(void);
  103. static void MX_ADC1_Init(void);
  104.  
  105. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  106. if (huart->Instance == USART2) {
  107. Empty_Rx++;
  108. if (Buf_Rx[Empty_Rx - 1] == ';') {
  109. endFrameDetected = 1;
  110. }
  111. if (Empty_Rx >= BUF_SIZE) {
  112. Empty_Rx = 0;
  113. }
  114. }
  115. HAL_UART_Receive_IT(&huart2, &Buf_Rx[Empty_Rx], 1);
  116. }
  117.  
  118. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
  119. if (huart->Instance == USART2) {
  120. if (Busy_Tx != Empty_Tx) {
  121. temp = Buf_Tx[Busy_Tx];
  122. Busy_Tx++;
  123. if (Busy_Tx >= BUF_SIZE) {
  124. Busy_Tx = 0;
  125. }
  126. HAL_UART_Transmit_IT(&huart2, &temp, 1);
  127. }
  128. }
  129. }
  130.  
  131. void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc1) {
  132. // dataIndex = 0;
  133. if (tick == 1 && pomiar == 1) {
  134. uint8_t data[20];
  135. float peakmin = 0, peakmax = 0;
  136. if (tryb == 0) {
  137. for (uint16_t i = 0; i < 255; i++) {
  138. voltage = Buf_Data[i] * 3.3f / 4095.0f;
  139. avg += voltage;
  140. avg /= 256;
  141. values[i] = avg;
  142. }
  143. } else if (tryb == 1) {
  144. for (uint16_t i = 0; i < 255; i++) {
  145. if (peak_max < Buf_Data[i]) {
  146. peak_max = Buf_Data[i];
  147. }
  148. voltage = peak_max * 3.3f / 4095.0f;
  149. avg = voltage / sqrt(2);
  150. values[i] = avg;
  151. }
  152. }
  153. }
  154. }
  155.  
  156. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc1) {
  157. // dataIndex = 255;
  158. if (tick == 1 && pomiar == 1) {
  159. uint8_t data[20];
  160. float peakmin = 0, peakmax = 0;
  161. if (tryb == 0) {
  162. for (uint16_t i = 0; i < 255; i++) {
  163. voltage = Buf_Data[i] * 3.3f / 4095.0f;
  164. avg += voltage;
  165. avg /= 256;
  166. values[i] = avg;
  167. }
  168. } else if (tryb == 1) {
  169. for (uint16_t i = 0; i < 255; i++) {
  170. if (peak_max < Buf_Data[i]) {
  171. peak_max = Buf_Data[i];
  172. }
  173. voltage = peak_max * 3.3f / 4095.0f;
  174. avg = voltage / sqrt(2);
  175. values[i] = avg;
  176. }
  177. }
  178. }
  179.  
  180. }
  181.  
  182. /* USER CODE BEGIN PFP */
  183. /* USER CODE END PFP */
  184.  
  185. /* Private user code ---------------------------------------------------------*/
  186. /* USER CODE BEGIN 0 */
  187. unsigned char crc8(unsigned char poly, unsigned char* data, int size) {
  188. unsigned char crc = 0x00;
  189. int bit;
  190.  
  191. while (size--) {
  192. crc ^= *data++;
  193. for (bit = 0; bit < 8; bit++) {
  194. if (crc & 0x80) {
  195. crc = (crc << 1) ^ poly;
  196. } else {
  197. crc <<= 1;
  198. }
  199. }
  200. }
  201. return crc;
  202. }
  203.  
  204. void frame_decoder() {
  205. char frame_buffer[255];
  206. char frame[127];
  207. char frame_escape[127];
  208. uint8_t i = 0;
  209. while (Busy_Rx != Empty_Rx) {
  210. frame_buffer[i++] = Buf_Rx[Busy_Rx++];
  211. if (Busy_Rx >= BUF_SIZE) {
  212. Busy_Rx = 0;
  213. }
  214. }
  215.  
  216. // poczatek ramki
  217. uint8_t k = 0;
  218. uint8_t startDetected = 0;
  219. for (uint8_t j = 0; j < i; j++) {
  220. if (frame_buffer[j] == ':') { // szukam znaku startu ramki
  221. startDetected = 1;
  222. k = 0;
  223. } else if (startDetected == 1) {
  224. frame[k++] = frame_buffer[j];
  225. }
  226. }
  227. if (startDetected != 0) {
  228. //deescapowanie ramki
  229. uint8_t m = 0;
  230. uint8_t escapeFlag = 0;
  231. for (uint8_t l = 0; l < k; l++) {
  232. if (frame[l] == 0x3F) {
  233. escapeFlag = 1;
  234. } else if (escapeFlag == 0) {
  235. frame_escape[m++] = frame[l];
  236. } else {
  237. escapeFlag = 0;
  238. frame_escape[m++] = frame[l] ^ 0x20;
  239. }
  240. }
  241.  
  242. // sprawdzam czy ramka nie jest za mała i za duża
  243. if (m > 2 && m < 126) {
  244. uint8_t checksum = frame_escape[m - 2]; // sume kontrolna nalezy sprawdzac, ale w celach debugu nie sprawdzam
  245. execute_command(frame_escape, m - 2);
  246. }
  247. }
  248. }
  249.  
  250. void frame_send(uint8_t *senddata, uint8_t length) {
  251. uint8_t frame[128];
  252. uint8_t i;
  253. for (i = 0; i < length; i++) {
  254. if (i > 127) {
  255. length = 127;
  256. break; // jesli podamy za duzo danych to wysle tylko poczatek
  257. }
  258. frame[i] = senddata[i];
  259. }
  260. frame[length] = crc8(0xAA, frame, length); //crc
  261.  
  262. uint8_t frame_escape[128];
  263. uint8_t m = 1;
  264. frame_escape[0] = ':';
  265. for (uint8_t l = 0; l < length + 1; l++) {
  266. if (frame[l] == 0x3A || frame[l] == 0x3B || frame[l] == 0x3F) {
  267. frame_escape[m++] = 0x3F;
  268. frame_escape[m++] = frame[l] ^ 0x20;
  269. } else {
  270. frame_escape[m++] = frame[l];
  271. }
  272. }
  273.  
  274. frame_escape[m] = ';';
  275. uint8_t idx = Empty_Tx;
  276. for (int i = 0; i < m + 1; i++) {
  277. Buf_Tx[idx] = frame_escape[i];
  278. idx++;
  279. if (idx > 255) {
  280. idx = 0;
  281. }
  282. }
  283. __disable_irq();
  284.  
  285. if (Busy_Tx == Empty_Tx
  286. && __HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE) == SET) {
  287. Empty_Tx = idx;
  288. temp = Buf_Tx[Busy_Tx];
  289. Busy_Tx++;
  290. if (Busy_Tx > 255) {
  291. Busy_Tx = 0;
  292. }
  293. HAL_UART_Transmit_IT(&huart2, &temp, 1);
  294. } else {
  295. Empty_Tx = idx;
  296. }
  297. __enable_irq();
  298. }
  299.  
  300. void execute_command(char *data_in, uint8_t length) {
  301. if (strncmp("pomiardc", data_in, length) == 0) {
  302. tryb = 0;
  303. pomiar = 1;
  304. frame_send("OK", 2);
  305. } else if (strncmp("pomiarpeak", data_in, length) == 0) {
  306. tryb = 1;
  307. pomiar = 1;
  308. frame_send("OK", 2);
  309. } else if (strncmp("pomiarrms", data_in, length) == 0) {
  310. tryb = 2;
  311. pomiar = 1;
  312. frame_send("OK", 2);
  313. } else if (strncmp("stop", data_in, length) == 0) {
  314. pomiar = 0;
  315. frame_send("stopped", 7);
  316. } else {
  317. frame_send("unknown command", 15);
  318. }
  319. }
  320.  
  321. /* USER CODE END 0 */
  322.  
  323. /**
  324. * @brief The application entry point.
  325. * @retval int
  326. */
  327. int main(void) {
  328. /* USER CODE BEGIN 1 */
  329. /* USER CODE END 1 */
  330.  
  331. /* MCU Configuration--------------------------------------------------------*/
  332.  
  333. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  334. HAL_Init();
  335.  
  336. /* USER CODE BEGIN Init */
  337.  
  338. /* USER CODE END Init */
  339.  
  340. /* Configure the system clock */
  341. SystemClock_Config();
  342.  
  343. /* USER CODE BEGIN SysInit */
  344.  
  345. /* USER CODE END SysInit */
  346.  
  347. /* Initialize all configured peripherals */
  348. MX_GPIO_Init();
  349. MX_DMA_Init();
  350. MX_USART2_UART_Init();
  351. MX_ADC1_Init();
  352.  
  353. /* USER CODE BEGIN 2 */
  354. HAL_ADC_Start_DMA(&hadc1, (uint16_t*) Buf_Data, 512);
  355. HAL_UART_Receive_IT(&huart2, &Buf_Rx[Empty_Rx], 1);
  356. /* USER CODE END 2 */
  357.  
  358. /* Infinite loop */
  359. /* USER CODE BEGIN WHILE */
  360. while (1) {
  361. uint8_t data[20];
  362.  
  363. /* USER CODE END WHILE */
  364. if (endFrameDetected) {
  365. endFrameDetected = 0;
  366. frame_decoder();
  367. }
  368. if (tick == 1) {
  369. for (uint16_t i = 0; i < 255; i++) {
  370. sprintf(data, "ADC = %.3fV", values[i]);
  371. tick = 0;
  372. frame_send(data, 12);
  373. avg = 0;
  374. }
  375. }
  376. /* USER CODE BEGIN 3 */
  377. }
  378. /* USER CODE END 3 */
  379. }
  380.  
  381. /**
  382. * @brief System Clock Configuration
  383. * @retval None
  384. */
  385. void SystemClock_Config(void) {
  386. RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
  387. RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
  388. RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 };
  389.  
  390. /**Initializes the CPU, AHB and APB busses clocks
  391. */
  392. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  393. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  394. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  395. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  396. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  397. Error_Handler();
  398. }
  399. /**Initializes the CPU, AHB and APB busses clocks
  400. */
  401. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  402. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  403. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  404. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  405. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  406. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  407.  
  408. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
  409. Error_Handler();
  410. }
  411. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  412. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  413. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
  414. Error_Handler();
  415. }
  416. }
  417.  
  418. /**
  419. * @brief ADC1 Initialization Function
  420. * @param None
  421. * @retval None
  422. */
  423. static void MX_ADC1_Init(void) {
  424.  
  425. /* USER CODE BEGIN ADC1_Init 0 */
  426.  
  427. /* USER CODE END ADC1_Init 0 */
  428.  
  429. ADC_ChannelConfTypeDef sConfig = { 0 };
  430.  
  431. /* USER CODE BEGIN ADC1_Init 1 */
  432.  
  433. /* USER CODE END ADC1_Init 1 */
  434. /**Common config
  435. */
  436. hadc1.Instance = ADC1;
  437. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  438. hadc1.Init.ContinuousConvMode = ENABLE;
  439. hadc1.Init.DiscontinuousConvMode = DISABLE;
  440. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  441. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  442. hadc1.Init.NbrOfConversion = 1;
  443. if (HAL_ADC_Init(&hadc1) != HAL_OK) {
  444. Error_Handler();
  445. }
  446. /**Configure Regular Channel
  447. */
  448. sConfig.Channel = ADC_CHANNEL_0;
  449. sConfig.Rank = ADC_REGULAR_RANK_1;
  450. sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  451. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
  452. Error_Handler();
  453. }
  454. /* USER CODE BEGIN ADC1_Init 2 */
  455.  
  456. /* USER CODE END ADC1_Init 2 */
  457.  
  458. }
  459.  
  460. /**
  461. * @brief USART2 Initialization Function
  462. * @param None
  463. * @retval None
  464. */
  465. static void MX_USART2_UART_Init(void) {
  466.  
  467. /* USER CODE BEGIN USART2_Init 0 */
  468.  
  469. /* USER CODE END USART2_Init 0 */
  470.  
  471. /* USER CODE BEGIN USART2_Init 1 */
  472.  
  473. /* USER CODE END USART2_Init 1 */
  474. huart2.Instance = USART2;
  475. huart2.Init.BaudRate = 9600;
  476. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  477. huart2.Init.StopBits = UART_STOPBITS_1;
  478. huart2.Init.Parity = UART_PARITY_NONE;
  479. huart2.Init.Mode = UART_MODE_TX_RX;
  480. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  481. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  482. if (HAL_UART_Init(&huart2) != HAL_OK) {
  483. Error_Handler();
  484. }
  485. /* USER CODE BEGIN USART2_Init 2 */
  486.  
  487. /* USER CODE END USART2_Init 2 */
  488.  
  489. }
  490.  
  491. /**
  492. * Enable DMA controller clock
  493. */
  494. static void MX_DMA_Init(void) {
  495. /* DMA controller clock enable */
  496. __HAL_RCC_DMA1_CLK_ENABLE()
  497. ;
  498.  
  499. /* DMA interrupt init */
  500. /* DMA1_Channel1_IRQn interrupt configuration */
  501. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  502. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  503.  
  504. }
  505.  
  506. /**
  507. * @brief GPIO Initialization Function
  508. * @param None
  509. * @retval None
  510. */
  511. static void MX_GPIO_Init(void) {
  512. GPIO_InitTypeDef GPIO_InitStruct = { 0 };
  513.  
  514. /* GPIO Ports Clock Enable */
  515. __HAL_RCC_GPIOA_CLK_ENABLE()
  516. ;
  517.  
  518. /*Configure GPIO pin Output Level */
  519. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  520.  
  521. /*Configure GPIO pin : PA5 */
  522. GPIO_InitStruct.Pin = GPIO_PIN_5;
  523. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  524. GPIO_InitStruct.Pull = GPIO_NOPULL;
  525. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  526. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  527.  
  528. }
  529.  
  530. /* USER CODE BEGIN 4 */
  531.  
  532. /* USER CODE END 4 */
  533.  
  534. /**
  535. * @brief This function is executed in case of error occurrence.
  536. * @retval None
  537. */
  538. void Error_Handler(void) {
  539. /* USER CODE BEGIN Error_Handler_Debug */
  540. /* User can add his own implementation to report the HAL error return state */
  541.  
  542. /* USER CODE END Error_Handler_Debug */
  543. }
  544.  
  545. #ifdef USE_FULL_ASSERT
  546. /**
  547. * @brief Reports the name of the source file and the source line number
  548. * where the assert_param error has occurred.
  549. * @param file: pointer to the source file name
  550. * @param line: assert_param error line source number
  551. * @retval None
  552. */
  553. void assert_failed(uint8_t *file, uint32_t line)
  554. {
  555. /* USER CODE BEGIN 6 */
  556. /* User can add his own implementation to report the file name and line number,
  557. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  558. /* USER CODE END 6 */
  559. }
  560. #endif /* USE_FULL_ASSERT */
  561.  
  562. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement