Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.83 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. volatile uint8_t tryb;
  83. volatile uint8_t pomiar;
  84. volatile uint8_t tick;
  85. volatile uint8_t dataIndex;
  86.  
  87. volatile float voltage;
  88. volatile float avg;
  89.  
  90. struct ring_buffer *buffer;
  91. /* USER CODE END PV */
  92.  
  93. /* Private function prototypes -----------------------------------------------*/
  94. void SystemClock_Config(void);
  95. static void MX_GPIO_Init(void);
  96. static void MX_DMA_Init(void);
  97. static void MX_USART2_UART_Init(void);
  98. static void MX_ADC1_Init(void);
  99.  
  100. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
  101. if (huart->Instance == USART2) {
  102. Empty_Rx++;
  103. if (Buf_Rx[Empty_Rx - 1] == ';') {
  104. endFrameDetected = 1;
  105. }
  106. if (Empty_Rx >= BUF_SIZE) {
  107. Empty_Rx = 0;
  108. }
  109. }
  110. HAL_UART_Receive_IT(&huart2, &Buf_Rx[Empty_Rx], 1);
  111. }
  112.  
  113. void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
  114. if (huart->Instance == USART2) {
  115. if (Busy_Tx != Empty_Tx) {
  116. temp = Buf_Tx[Busy_Tx];
  117. Busy_Tx++;
  118. if (Busy_Tx >= BUF_SIZE) {
  119. Busy_Tx = 0;
  120. }
  121. HAL_UART_Transmit_IT(&huart2, &temp, 1);
  122. }
  123. }
  124. }
  125.  
  126. void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc1) {
  127. // dataIndex = 0;
  128. if (tick == 1) {
  129. uint8_t data[20];
  130. if (tryb == 0 && pomiar == 1) {
  131. for (uint16_t i = 0; i < 255; i++) {
  132. voltage = Buf_Data[i] * 3.3f / 4095.0f;
  133. avg += voltage;
  134. }
  135. avg /= 256;
  136.  
  137. sprintf(data, "ADC = %.3fV", avg);
  138. tick = 0;
  139. frame_send(data, 12);
  140. avg = 0;
  141. }
  142. }
  143. }
  144.  
  145. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc1) {
  146. // dataIndex = 255;
  147. if (tick == 1) {
  148. uint8_t data[20];
  149. if (tryb == 0 && pomiar == 1) {
  150. for (uint16_t i = 0; i < 255; i++) {
  151. voltage = Buf_Data[i] * 3.3f / 4095.0f;
  152. avg += voltage;
  153. }
  154. avg /= 256;
  155.  
  156. sprintf(data, "ADC = %.3fV", avg);
  157. tick = 0;
  158. frame_send(data, 12);
  159. avg = 0;
  160. }
  161. }
  162. }
  163.  
  164. /* USER CODE BEGIN PFP */
  165. /* USER CODE END PFP */
  166.  
  167. /* Private user code ---------------------------------------------------------*/
  168. /* USER CODE BEGIN 0 */
  169. unsigned char crc8(unsigned char poly, unsigned char* data, int size) {
  170. unsigned char crc = 0x00;
  171. int bit;
  172.  
  173. while (size--) {
  174. crc ^= *data++;
  175. for (bit = 0; bit < 8; bit++) {
  176. if (crc & 0x80) {
  177. crc = (crc << 1) ^ poly;
  178. } else {
  179. crc <<= 1;
  180. }
  181. }
  182. }
  183. return crc;
  184. }
  185.  
  186. void frame_decoder() {
  187. char frame_buffer[255];
  188. char frame[127];
  189. char frame_escape[127];
  190. uint8_t i = 0;
  191. while (Busy_Rx != Empty_Rx) {
  192. frame_buffer[i++] = Buf_Rx[Busy_Rx++];
  193. if (Busy_Rx >= BUF_SIZE) {
  194. Busy_Rx = 0;
  195. }
  196. }
  197.  
  198. // poczatek ramki
  199. uint8_t k = 0;
  200. uint8_t startDetected = 0;
  201. for (uint8_t j = 0; j < i; j++) {
  202. if (frame_buffer[j] == ':') { // szukam znaku startu ramki
  203. startDetected = 1;
  204. k = 0;
  205. } else if (startDetected == 1) {
  206. frame[k++] = frame_buffer[j];
  207. }
  208. }
  209. if (startDetected != 0) {
  210. //deescapowanie ramki
  211. uint8_t m = 0;
  212. uint8_t escapeFlag = 0;
  213. for (uint8_t l = 0; l < k; l++) {
  214. if (frame[l] == 0x3F) {
  215. escapeFlag = 1;
  216. } else if (escapeFlag == 0) {
  217. frame_escape[m++] = frame[l];
  218. } else {
  219. escapeFlag = 0;
  220. frame_escape[m++] = frame[l] ^ 0x20;
  221. }
  222. }
  223.  
  224. // sprawdzam czy ramka nie jest za mała i za duża
  225. if (m > 2 && m < 126) {
  226. uint8_t checksum = frame_escape[m - 2];
  227. char dane[127];
  228. uint8_t n = 0;
  229. for (uint8_t o = 0; o < m - 2; o++) {
  230. dane[n++] = frame_escape[o];
  231. }
  232.  
  233. execute_command(dane);
  234. }
  235. }
  236. }
  237.  
  238. void frame_send(uint8_t *senddata, uint8_t length) {
  239. uint8_t frame[128];
  240. uint8_t i;
  241. for (i = 0; i < length; i++) {
  242. frame[i] = senddata[i];
  243. }
  244. frame[length] = crc8(0x77, frame, length); //crc
  245.  
  246. uint8_t frame_escape[128];
  247. uint8_t m = 1;
  248. frame_escape[0] = ':';
  249. for (uint8_t l = 0; l < length + 1; l++) {
  250. if (frame[l] == 0x3A || frame[l] == 0x3B || frame[l] == 0x3F) {
  251. frame_escape[m++] = 0x3F;
  252. frame_escape[m++] = frame[l] ^ 0x20;
  253. } else {
  254. frame_escape[m++] = frame[l];
  255. }
  256. }
  257.  
  258. frame_escape[m + 1] = ';';
  259. uint8_t idx = Empty_Tx;
  260. for (int i = 0; i < m + 1; i++) {
  261. Buf_Tx[idx] = frame_escape[i];
  262. idx++;
  263. if (idx > 255) {
  264. idx = 0;
  265. }
  266. }
  267. __disable_irq();
  268.  
  269. if (Busy_Tx == Empty_Tx
  270. && __HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE) == SET) {
  271. Empty_Tx = idx;
  272. temp = Buf_Tx[Busy_Tx];
  273. Busy_Tx++;
  274. if (Busy_Tx > 255) {
  275. Busy_Tx = 0;
  276. }
  277. HAL_UART_Transmit_IT(&huart2, &temp, 1);
  278. } else {
  279. Empty_Tx = idx;
  280. }
  281. __enable_irq();
  282. }
  283.  
  284. void execute_command(char *data_in) {
  285. if (strncmp("pomiardc", data_in, 8) == 0) {
  286. tryb = 0;
  287. pomiar = 1;
  288. frame_send("OK", 2);
  289. } else if (strncmp("pomiarpeak", data_in, 10) == 0) {
  290. tryb = 1;
  291. pomiar = 1;
  292. frame_send("OK", 2);
  293. } else if (strncmp("pomiarrms", data_in, 9) == 0) {
  294. tryb = 2;
  295. pomiar = 1;
  296. frame_send("OK", 2);
  297. } else if (strncmp("stop", data_in, 4) == 0) {
  298. pomiar = 0;
  299. frame_send("stopped", 8);
  300. } else {
  301. frame_send("unknown command", 15);
  302. }
  303. }
  304.  
  305. void send(char *format, ...) {
  306. uint8_t buffer[256];
  307. uint8_t idx = Empty_Tx;
  308. va_list argList;
  309. va_start(argList, format);
  310. vsprintf(buffer, format, argList);
  311. va_end(argList);
  312.  
  313. for (int i = 0; i < strlen(buffer); i++) {
  314. Buf_Tx[idx] = buffer[i];
  315. idx++;
  316. if (idx > 255) {
  317. idx = 0;
  318. }
  319. }
  320. __disable_irq();
  321.  
  322. if (Busy_Tx == Empty_Tx
  323. && __HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE) == SET) {
  324. Empty_Tx = idx;
  325. temp = Buf_Tx[Busy_Tx];
  326. Busy_Tx++;
  327. if (Busy_Tx > 255) {
  328. Busy_Tx = 0;
  329. }
  330. HAL_UART_Transmit_IT(&huart2, &temp, 1);
  331. } else {
  332. Empty_Tx = idx;
  333. }
  334. __enable_irq();
  335. }
  336.  
  337. void send_hello() {
  338. send("I am working!\n\r");
  339. }
  340.  
  341. /* USER CODE END 0 */
  342.  
  343. /**
  344. * @brief The application entry point.
  345. * @retval int
  346. */
  347. int main(void) {
  348. /* USER CODE BEGIN 1 */
  349. /* USER CODE END 1 */
  350.  
  351. /* MCU Configuration--------------------------------------------------------*/
  352.  
  353. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  354. HAL_Init();
  355.  
  356. /* USER CODE BEGIN Init */
  357.  
  358. /* USER CODE END Init */
  359.  
  360. /* Configure the system clock */
  361. SystemClock_Config();
  362.  
  363. /* USER CODE BEGIN SysInit */
  364.  
  365. /* USER CODE END SysInit */
  366.  
  367. /* Initialize all configured peripherals */
  368. MX_GPIO_Init();
  369. MX_DMA_Init();
  370. MX_USART2_UART_Init();
  371. MX_ADC1_Init();
  372.  
  373. /* USER CODE BEGIN 2 */
  374. HAL_ADC_Start_DMA(&hadc1, (uint16_t*) Buf_Data, 512);
  375. send_hello();
  376. HAL_UART_Receive_IT(&huart2, &Buf_Rx[Empty_Rx], 1);
  377. /* USER CODE END 2 */
  378.  
  379. /* Infinite loop */
  380. /* USER CODE BEGIN WHILE */
  381. while (1) {
  382.  
  383. /* USER CODE END WHILE */
  384. if (endFrameDetected) {
  385. endFrameDetected = 0;
  386. frame_decoder();
  387. }
  388. /* USER CODE BEGIN 3 */
  389. }
  390. /* USER CODE END 3 */
  391. }
  392.  
  393. /**
  394. * @brief System Clock Configuration
  395. * @retval None
  396. */
  397. void SystemClock_Config(void) {
  398. RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
  399. RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
  400. RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 };
  401.  
  402. /**Initializes the CPU, AHB and APB busses clocks
  403. */
  404. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  405. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  406. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  407. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  408. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
  409. Error_Handler();
  410. }
  411. /**Initializes the CPU, AHB and APB busses clocks
  412. */
  413. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
  414. | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
  415. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  416. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  417. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  418. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  419.  
  420. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
  421. Error_Handler();
  422. }
  423. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
  424. PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
  425. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
  426. Error_Handler();
  427. }
  428. }
  429.  
  430. /**
  431. * @brief ADC1 Initialization Function
  432. * @param None
  433. * @retval None
  434. */
  435. static void MX_ADC1_Init(void) {
  436.  
  437. /* USER CODE BEGIN ADC1_Init 0 */
  438.  
  439. /* USER CODE END ADC1_Init 0 */
  440.  
  441. ADC_ChannelConfTypeDef sConfig = { 0 };
  442.  
  443. /* USER CODE BEGIN ADC1_Init 1 */
  444.  
  445. /* USER CODE END ADC1_Init 1 */
  446. /**Common config
  447. */
  448. hadc1.Instance = ADC1;
  449. hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
  450. hadc1.Init.ContinuousConvMode = ENABLE;
  451. hadc1.Init.DiscontinuousConvMode = DISABLE;
  452. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  453. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  454. hadc1.Init.NbrOfConversion = 1;
  455. if (HAL_ADC_Init(&hadc1) != HAL_OK) {
  456. Error_Handler();
  457. }
  458. /**Configure Regular Channel
  459. */
  460. sConfig.Channel = ADC_CHANNEL_0;
  461. sConfig.Rank = ADC_REGULAR_RANK_1;
  462. sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
  463. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
  464. Error_Handler();
  465. }
  466. /* USER CODE BEGIN ADC1_Init 2 */
  467.  
  468. /* USER CODE END ADC1_Init 2 */
  469.  
  470. }
  471.  
  472. /**
  473. * @brief USART2 Initialization Function
  474. * @param None
  475. * @retval None
  476. */
  477. static void MX_USART2_UART_Init(void) {
  478.  
  479. /* USER CODE BEGIN USART2_Init 0 */
  480.  
  481. /* USER CODE END USART2_Init 0 */
  482.  
  483. /* USER CODE BEGIN USART2_Init 1 */
  484.  
  485. /* USER CODE END USART2_Init 1 */
  486. huart2.Instance = USART2;
  487. huart2.Init.BaudRate = 9600;
  488. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  489. huart2.Init.StopBits = UART_STOPBITS_1;
  490. huart2.Init.Parity = UART_PARITY_NONE;
  491. huart2.Init.Mode = UART_MODE_TX_RX;
  492. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  493. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  494. if (HAL_UART_Init(&huart2) != HAL_OK) {
  495. Error_Handler();
  496. }
  497. /* USER CODE BEGIN USART2_Init 2 */
  498.  
  499. /* USER CODE END USART2_Init 2 */
  500.  
  501. }
  502.  
  503. /**
  504. * Enable DMA controller clock
  505. */
  506. static void MX_DMA_Init(void) {
  507. /* DMA controller clock enable */
  508. __HAL_RCC_DMA1_CLK_ENABLE()
  509. ;
  510.  
  511. /* DMA interrupt init */
  512. /* DMA1_Channel1_IRQn interrupt configuration */
  513. HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
  514. HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
  515.  
  516. }
  517.  
  518. /**
  519. * @brief GPIO Initialization Function
  520. * @param None
  521. * @retval None
  522. */
  523. static void MX_GPIO_Init(void) {
  524. GPIO_InitTypeDef GPIO_InitStruct = { 0 };
  525.  
  526. /* GPIO Ports Clock Enable */
  527. __HAL_RCC_GPIOA_CLK_ENABLE()
  528. ;
  529.  
  530. /*Configure GPIO pin Output Level */
  531. HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
  532.  
  533. /*Configure GPIO pin : PA5 */
  534. GPIO_InitStruct.Pin = GPIO_PIN_5;
  535. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  536. GPIO_InitStruct.Pull = GPIO_NOPULL;
  537. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  538. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  539.  
  540. }
  541.  
  542. /* USER CODE BEGIN 4 */
  543.  
  544. /* USER CODE END 4 */
  545.  
  546. /**
  547. * @brief This function is executed in case of error occurrence.
  548. * @retval None
  549. */
  550. void Error_Handler(void) {
  551. /* USER CODE BEGIN Error_Handler_Debug */
  552. /* User can add his own implementation to report the HAL error return state */
  553.  
  554. /* USER CODE END Error_Handler_Debug */
  555. }
  556.  
  557. #ifdef USE_FULL_ASSERT
  558. /**
  559. * @brief Reports the name of the source file and the source line number
  560. * where the assert_param error has occurred.
  561. * @param file: pointer to the source file name
  562. * @param line: assert_param error line source number
  563. * @retval None
  564. */
  565. void assert_failed(uint8_t *file, uint32_t line)
  566. {
  567. /* USER CODE BEGIN 6 */
  568. /* User can add his own implementation to report the file name and line number,
  569. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  570. /* USER CODE END 6 */
  571. }
  572. #endif /* USE_FULL_ASSERT */
  573.  
  574. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement