Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- ** This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * COPYRIGHT(c) 2019 STMicroelectronics
- *
- * Redistribution and use in source and binary forms, with or without modification,
- * are permitted provided that the following conditions are met:
- * 1. Redistributions of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
- * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
- * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
- * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include <string.h>
- #include <stdlib.h>
- #include <stdarg.h>
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- #define BUF_SIZE 256
- #define BUF_DATA_SIZE 512
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- ADC_HandleTypeDef hadc1;
- DMA_HandleTypeDef hdma_adc1;
- UART_HandleTypeDef huart2;
- /* USER CODE BEGIN PV */
- volatile uint16_t Buf_Data[BUF_DATA_SIZE];
- volatile uint8_t Buf_Rx[BUF_SIZE];
- volatile uint8_t Buf_Tx[BUF_SIZE];
- volatile uint8_t Busy_Rx = 0, Empty_Rx = 0, Busy_Tx = 0, Empty_Tx = 0;
- volatile uint8_t temp = 0;
- volatile uint8_t endFrameDetected = 0;
- uint8_t peak_max = 0;
- uint16_t peak_min = 4095;
- volatile uint8_t tryb;
- volatile uint8_t pomiar;
- volatile uint8_t tick;
- volatile uint8_t dataIndex;
- uint16_t values[255];
- volatile float voltage;
- volatile float avg;
- struct ring_buffer *buffer;
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void MX_DMA_Init(void);
- static void MX_USART2_UART_Init(void);
- static void MX_ADC1_Init(void);
- void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
- if (huart->Instance == USART2) {
- Empty_Rx++;
- if (Buf_Rx[Empty_Rx - 1] == ';') {
- endFrameDetected = 1;
- }
- if (Empty_Rx >= BUF_SIZE) {
- Empty_Rx = 0;
- }
- }
- HAL_UART_Receive_IT(&huart2, &Buf_Rx[Empty_Rx], 1);
- }
- void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) {
- if (huart->Instance == USART2) {
- if (Busy_Tx != Empty_Tx) {
- temp = Buf_Tx[Busy_Tx];
- Busy_Tx++;
- if (Busy_Tx >= BUF_SIZE) {
- Busy_Tx = 0;
- }
- HAL_UART_Transmit_IT(&huart2, &temp, 1);
- }
- }
- }
- void HAL_ADC_ConvHalfCpltCallback(ADC_HandleTypeDef* hadc1) {
- // dataIndex = 0;
- if (tick == 1 && pomiar == 1) {
- uint8_t data[20];
- float peakmin = 0, peakmax = 0;
- if (tryb == 0) {
- for (uint16_t i = 0; i < 255; i++) {
- voltage = Buf_Data[i] * 3.3f / 4095.0f;
- avg += voltage;
- avg /= 256;
- values[i] = avg;
- }
- } else if (tryb == 1) {
- for (uint16_t i = 0; i < 255; i++) {
- if (peak_max < Buf_Data[i]) {
- peak_max = Buf_Data[i];
- }
- voltage = peak_max * 3.3f / 4095.0f;
- avg = voltage / sqrt(2);
- values[i] = avg;
- }
- }
- }
- }
- void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc1) {
- // dataIndex = 255;
- if (tick == 1 && pomiar == 1) {
- uint8_t data[20];
- float peakmin = 0, peakmax = 0;
- if (tryb == 0) {
- for (uint16_t i = 0; i < 255; i++) {
- voltage = Buf_Data[i] * 3.3f / 4095.0f;
- avg += voltage;
- avg /= 256;
- values[i] = avg;
- }
- } else if (tryb == 1) {
- for (uint16_t i = 0; i < 255; i++) {
- if (peak_max < Buf_Data[i]) {
- peak_max = Buf_Data[i];
- }
- voltage = peak_max * 3.3f / 4095.0f;
- avg = voltage / sqrt(2);
- values[i] = avg;
- }
- }
- }
- }
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- unsigned char crc8(unsigned char poly, unsigned char* data, int size) {
- unsigned char crc = 0x00;
- int bit;
- while (size--) {
- crc ^= *data++;
- for (bit = 0; bit < 8; bit++) {
- if (crc & 0x80) {
- crc = (crc << 1) ^ poly;
- } else {
- crc <<= 1;
- }
- }
- }
- return crc;
- }
- void frame_decoder() {
- char frame_buffer[255];
- char frame[127];
- char frame_escape[127];
- uint8_t i = 0;
- while (Busy_Rx != Empty_Rx) {
- frame_buffer[i++] = Buf_Rx[Busy_Rx++];
- if (Busy_Rx >= BUF_SIZE) {
- Busy_Rx = 0;
- }
- }
- // poczatek ramki
- uint8_t k = 0;
- uint8_t startDetected = 0;
- for (uint8_t j = 0; j < i; j++) {
- if (frame_buffer[j] == ':') { // szukam znaku startu ramki
- startDetected = 1;
- k = 0;
- } else if (startDetected == 1) {
- frame[k++] = frame_buffer[j];
- }
- }
- if (startDetected != 0) {
- //deescapowanie ramki
- uint8_t m = 0;
- uint8_t escapeFlag = 0;
- for (uint8_t l = 0; l < k; l++) {
- if (frame[l] == 0x3F) {
- escapeFlag = 1;
- } else if (escapeFlag == 0) {
- frame_escape[m++] = frame[l];
- } else {
- escapeFlag = 0;
- frame_escape[m++] = frame[l] ^ 0x20;
- }
- }
- // sprawdzam czy ramka nie jest za mała i za duża
- if (m > 2 && m < 126) {
- uint8_t checksum = frame_escape[m - 2]; // sume kontrolna nalezy sprawdzac, ale w celach debugu nie sprawdzam
- execute_command(frame_escape, m - 2);
- }
- }
- }
- void frame_send(uint8_t *senddata, uint8_t length) {
- uint8_t frame[128];
- uint8_t i;
- for (i = 0; i < length; i++) {
- if (i > 127) {
- length = 127;
- break; // jesli podamy za duzo danych to wysle tylko poczatek
- }
- frame[i] = senddata[i];
- }
- frame[length] = crc8(0xAA, frame, length); //crc
- uint8_t frame_escape[128];
- uint8_t m = 1;
- frame_escape[0] = ':';
- for (uint8_t l = 0; l < length + 1; l++) {
- if (frame[l] == 0x3A || frame[l] == 0x3B || frame[l] == 0x3F) {
- frame_escape[m++] = 0x3F;
- frame_escape[m++] = frame[l] ^ 0x20;
- } else {
- frame_escape[m++] = frame[l];
- }
- }
- frame_escape[m] = ';';
- uint8_t idx = Empty_Tx;
- for (int i = 0; i < m + 1; i++) {
- Buf_Tx[idx] = frame_escape[i];
- idx++;
- if (idx > 255) {
- idx = 0;
- }
- }
- __disable_irq();
- if (Busy_Tx == Empty_Tx
- && __HAL_UART_GET_FLAG(&huart2, UART_FLAG_TXE) == SET) {
- Empty_Tx = idx;
- temp = Buf_Tx[Busy_Tx];
- Busy_Tx++;
- if (Busy_Tx > 255) {
- Busy_Tx = 0;
- }
- HAL_UART_Transmit_IT(&huart2, &temp, 1);
- } else {
- Empty_Tx = idx;
- }
- __enable_irq();
- }
- void execute_command(char *data_in, uint8_t length) {
- if (strncmp("pomiardc", data_in, length) == 0) {
- tryb = 0;
- pomiar = 1;
- frame_send("OK", 2);
- } else if (strncmp("pomiarpeak", data_in, length) == 0) {
- tryb = 1;
- pomiar = 1;
- frame_send("OK", 2);
- } else if (strncmp("pomiarrms", data_in, length) == 0) {
- tryb = 2;
- pomiar = 1;
- frame_send("OK", 2);
- } else if (strncmp("stop", data_in, length) == 0) {
- pomiar = 0;
- frame_send("stopped", 7);
- } else {
- frame_send("unknown command", 15);
- }
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void) {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
- /* Configure the system clock */
- SystemClock_Config();
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_DMA_Init();
- MX_USART2_UART_Init();
- MX_ADC1_Init();
- /* USER CODE BEGIN 2 */
- HAL_ADC_Start_DMA(&hadc1, (uint16_t*) Buf_Data, 512);
- HAL_UART_Receive_IT(&huart2, &Buf_Rx[Empty_Rx], 1);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1) {
- uint8_t data[20];
- /* USER CODE END WHILE */
- if (endFrameDetected) {
- endFrameDetected = 0;
- frame_decoder();
- }
- if (tick == 1) {
- for (uint16_t i = 0; i < 255; i++) {
- sprintf(data, "ADC = %.3fV", values[i]);
- tick = 0;
- frame_send(data, 12);
- avg = 0;
- }
- }
- /* USER CODE BEGIN 3 */
- }
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void) {
- RCC_OscInitTypeDef RCC_OscInitStruct = { 0 };
- RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 };
- RCC_PeriphCLKInitTypeDef PeriphClkInit = { 0 };
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
- Error_Handler();
- }
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
- | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) {
- Error_Handler();
- }
- PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADC;
- PeriphClkInit.AdcClockSelection = RCC_ADCPCLK2_DIV2;
- if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) {
- Error_Handler();
- }
- }
- /**
- * @brief ADC1 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_ADC1_Init(void) {
- /* USER CODE BEGIN ADC1_Init 0 */
- /* USER CODE END ADC1_Init 0 */
- ADC_ChannelConfTypeDef sConfig = { 0 };
- /* USER CODE BEGIN ADC1_Init 1 */
- /* USER CODE END ADC1_Init 1 */
- /**Common config
- */
- hadc1.Instance = ADC1;
- hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE;
- hadc1.Init.ContinuousConvMode = ENABLE;
- hadc1.Init.DiscontinuousConvMode = DISABLE;
- hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
- hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
- hadc1.Init.NbrOfConversion = 1;
- if (HAL_ADC_Init(&hadc1) != HAL_OK) {
- Error_Handler();
- }
- /**Configure Regular Channel
- */
- sConfig.Channel = ADC_CHANNEL_0;
- sConfig.Rank = ADC_REGULAR_RANK_1;
- sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
- if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) {
- Error_Handler();
- }
- /* USER CODE BEGIN ADC1_Init 2 */
- /* USER CODE END ADC1_Init 2 */
- }
- /**
- * @brief USART2 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_USART2_UART_Init(void) {
- /* USER CODE BEGIN USART2_Init 0 */
- /* USER CODE END USART2_Init 0 */
- /* USER CODE BEGIN USART2_Init 1 */
- /* USER CODE END USART2_Init 1 */
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 9600;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
- if (HAL_UART_Init(&huart2) != HAL_OK) {
- Error_Handler();
- }
- /* USER CODE BEGIN USART2_Init 2 */
- /* USER CODE END USART2_Init 2 */
- }
- /**
- * Enable DMA controller clock
- */
- static void MX_DMA_Init(void) {
- /* DMA controller clock enable */
- __HAL_RCC_DMA1_CLK_ENABLE()
- ;
- /* DMA interrupt init */
- /* DMA1_Channel1_IRQn interrupt configuration */
- HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 0, 0);
- HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void) {
- GPIO_InitTypeDef GPIO_InitStruct = { 0 };
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOA_CLK_ENABLE()
- ;
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOA, GPIO_PIN_5, GPIO_PIN_RESET);
- /*Configure GPIO pin : PA5 */
- GPIO_InitStruct.Pin = GPIO_PIN_5;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void) {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement