Files
STMGyro/Core/Src/gyro_mes.c
2022-02-02 10:53:58 +03:00

148 lines
4.0 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* gyro_mes.c
*
* Created on: 30 янв. 2022 г.
* Author: fedos
*/
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : gyro_mes.c
* @brief : Body of program message to gyro
******************************************************************************
* @attention
*
* Copyright (c) 2022 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "gyro_mes.h"
#include "my_defs.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
uint8_t gencrc(uint8_t *pData, size_t nLen);
/* USER CODE END PFP */
/* Private function code\-----------------------------------------------*/
/* USER CODE BEGIN PFC */
uint8_t gencrc(uint8_t *pData, size_t nLen)
{
uint8_t crc = 0x0;
size_t i;
for (i = 0; i < nLen; i++) crc += pData[i];
crc = 0xFF - crc;
return crc;
}
/* USER CODE END PFC */
/**
* @brief Packing command messages for gyroscope
* @retval None
*/
int8_t comand_mes(uint8_t* pOut, size_t nLen)
{
uint8_t mes = 0;
if (nLen < 6u) return (-ERROR_SIZE_SMALL);
mes = COM_MES_MODE << 7
| COM_MES_CBIT << 6
| COM_MES_RATE_RANGE << 5
| ((COM_MES_RATE_RANGE_VAL >> 1) & 1) << 4
| ((COM_MES_RATE_RANGE_VAL >> 0) & 1) << 3
| 0 << 2
| 0 << 1
| 0 << 0;
pOut[0] = mes;
int i;
for(i = 1; i < 6; i++) pOut[i] = 0;
pOut[5] = gencrc(pOut, 5);
return 0 ;
}
/**
* @brief Unpacking status messages from gyroscope
* @retval None
*/
int8_t status_mes(uint8_t* pMes, size_t nLen, struct sData *pOut)
{
uint8_t rate_range_val = 0;
uint16_t rate_val = 0;
uint16_t temp_val = 0;
if (nLen < 6u) return (-ERROR_SIZE_SMALL);
if(pMes[5] != gencrc(pMes, 5)) return (-ERROR_CRC);
if(((pMes[0] >> 7) & 1) != 0) return (-ERROR_FAIL);
if(((pMes[0] >> 6) & 1) != 0 &&
((pMes[0] >> 5) & 1) != 0) return (-ERROR_IDENTIFIER);
if(((pMes[0] >> 4) & 1) != 0) return (-ERROR_INVALID);
if(((pMes[0] >> 3) & 1) != COM_MES_CBIT) return (-ERROR_CBIT);
if(((pMes[0] >> 2) & 1) != 0) return (-ERROR_NORM);
if(COM_MES_RATE_RANGE == COM_MES_RATE_RANGE_INT) rate_range_val = COM_MES_RATE_RANGE_75;
else rate_range_val = COM_MES_RATE_RANGE_VAL;
if(((pMes[0] >> 1) & 1) != ((rate_range_val >> 1) & 1) &&
((pMes[0] >> 0) & 1) != ((rate_range_val >> 1) & 1)) return (-ERROR_RATE);
rate_val = ((uint16_t)pMes[1] << 8) | pMes[2];
temp_val = ((uint16_t)pMes[3] << 8) | pMes[4];
if(COM_MES_RATE_RANGE_VAL == COM_MES_RATE_RANGE_75) pOut->rate = (int16_t)rate_val / SCALE_RATE_RANGE_75;
else if(COM_MES_RATE_RANGE_VAL == COM_MES_RATE_RANGE_150) pOut->rate = (int16_t)rate_val / SCALE_RATE_RANGE_150;
else if(COM_MES_RATE_RANGE_VAL == COM_MES_RATE_RANGE_300) pOut->rate = (int16_t)rate_val / SCALE_RATE_RANGE_300;
else if(COM_MES_RATE_RANGE_VAL == COM_MES_RATE_RANGE_900) pOut->rate = (int16_t)rate_val / SCALE_RATE_RANGE_900;
pOut->temp = (temp_val - 531) / 2.75;
return 0;
}