初始提交
This commit is contained in:
@@ -0,0 +1,287 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "ads8321.h"
|
||||
|
||||
#include "main.h"
|
||||
#include "Param/param.h"
|
||||
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
#define DPRINT(fmt, args...) printf(fmt, ##args)
|
||||
//#define DPRINT(fmt, args...)
|
||||
|
||||
extern UART_HandleTypeDef huart4;
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim1;
|
||||
|
||||
extern SPI_HandleTypeDef hspi3;
|
||||
extern SPI_HandleTypeDef hspi2;
|
||||
|
||||
|
||||
int _giSampleInProgress = 0;
|
||||
|
||||
uint32_t _gaulAdcValue[2];
|
||||
int _gaiFlag[2];
|
||||
|
||||
volatile uint32_t _gulSamplePosUm = 0;
|
||||
volatile uint32_t _gulSampleTimeUs = 0;
|
||||
volatile adcDataQueueDef gtAdcDataQueue;
|
||||
volatile uint32_t gulAdcSampleIndex;
|
||||
volatile uint32_t ulSampleStartTime = 0;
|
||||
volatile uint32_t ulSampleDuration = 0;
|
||||
|
||||
typedef struct {
|
||||
uint32_t ulTick;
|
||||
uint32_t ulCnt;
|
||||
} tickTimeDef;
|
||||
|
||||
volatile int32_t _gslEncCnt = 0;
|
||||
tickTimeDef _gtLastTrigTime;
|
||||
int _giEncoderTrigFlag = 0;
|
||||
uint32_t gulEncoderSpeed = 0; /// 0.1mm/s
|
||||
|
||||
int giScanDirection = 0;
|
||||
|
||||
uint32_t gulPacketIndex = 0;
|
||||
uint32_t gulPacketAck = 0;
|
||||
|
||||
static uint32_t _gaulAdcBuf[100];
|
||||
static uint32_t _gaulAdcBuf2[100];
|
||||
static uint32_t _gulAdcIndex = 0;
|
||||
static uint32_t _gulAdcSampleCount = 0;
|
||||
|
||||
static void adcDataQueueReset(void);
|
||||
void adcUpdateParam(void);
|
||||
|
||||
int getAdcValue(uint32_t adc_val)
|
||||
{
|
||||
uint32_t rxDataOK = adc_val >> 2;
|
||||
int16_t val;
|
||||
memcpy(&val, &rxDataOK, sizeof(val));
|
||||
|
||||
int ret = val + 32767;
|
||||
if (ret < 0) {
|
||||
ret = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static void spiTransmitReceiveOnce(int index)
|
||||
{
|
||||
static uint32_t dummy = 0;
|
||||
_gaiFlag[0] = 0;
|
||||
_gaiFlag[1] = 0;
|
||||
HAL_SPI_Receive_IT(&hspi2, (uint8_t *)&_gaulAdcBuf[index], 1);
|
||||
HAL_SPI_TransmitReceive_IT(&hspi3, (uint8_t *)&dummy, (uint8_t *)&_gaulAdcBuf2[index], 1);
|
||||
}
|
||||
|
||||
static void data_average()
|
||||
{
|
||||
uint32_t ulData1 = 0;
|
||||
uint32_t ulData2 = 0;
|
||||
for (int i = 0; i < _gulAdcSampleCount; i++) {
|
||||
ulData1 += getAdcValue(_gaulAdcBuf[i]);
|
||||
ulData2 += getAdcValue(_gaulAdcBuf2[i]);
|
||||
}
|
||||
_gaulAdcValue[0] = ulData1 / _gulAdcSampleCount;
|
||||
_gaulAdcValue[1] = ulData2 / _gulAdcSampleCount;
|
||||
}
|
||||
|
||||
static void adcDataQueuePut(void);
|
||||
|
||||
static void checkDataReady()
|
||||
{
|
||||
if (_gaiFlag[0] && _gaiFlag[1]) {
|
||||
_gulAdcIndex++;
|
||||
if (_gulAdcIndex < _gulAdcSampleCount) {
|
||||
spiTransmitReceiveOnce(_gulAdcIndex);
|
||||
return;
|
||||
} else if (_gulAdcIndex == _gulAdcSampleCount) {
|
||||
data_average();
|
||||
adcDataQueuePut();
|
||||
// printf("%d\r\n", adcDataQueueLen());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
if (hspi == &hspi2) {
|
||||
_gaiFlag[0] = 1;
|
||||
checkDataReady();
|
||||
}
|
||||
}
|
||||
|
||||
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
|
||||
{
|
||||
if (hspi == &hspi3) {
|
||||
_gaiFlag[1] = 1;
|
||||
checkDataReady();
|
||||
}
|
||||
}
|
||||
|
||||
static void adcStart(int avrg_count)
|
||||
{
|
||||
_gtLastTrigTime.ulCnt = TIM13->CNT;
|
||||
_gtLastTrigTime.ulTick = uwTick;
|
||||
|
||||
if (avrg_count > 100) {
|
||||
avrg_count = 100;
|
||||
}
|
||||
|
||||
_gulAdcSampleCount = avrg_count;
|
||||
_gulAdcIndex = 0;
|
||||
|
||||
spiTransmitReceiveOnce(_gulAdcIndex);
|
||||
}
|
||||
|
||||
static void adcDataQueuePut(void)
|
||||
{
|
||||
uint32_t ulData1 = _gaulAdcValue[0];
|
||||
uint32_t ulData2 = _gaulAdcValue[1];
|
||||
|
||||
volatile adcDataUnitDef *ptData = >AdcDataQueue.atQueue[gtAdcDataQueue.ulWritePos];
|
||||
|
||||
gulAdcSampleIndex++;
|
||||
|
||||
if (gulAdcSampleIndex >= 5) {
|
||||
ptData->aulData[0] = (uint16_t)ulData1;
|
||||
ptData->aulData[1] = (uint16_t)ulData2;
|
||||
|
||||
gtAdcDataQueue.ulWritePos++;
|
||||
if (gtAdcDataQueue.ulWritePos >= ADC_QUEUE_LEN) {
|
||||
gtAdcDataQueue.ulWritePos = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setTimerPeriod(uint32_t ulPeriod)
|
||||
{
|
||||
htim3.Init.Period = ulPeriod - 1;
|
||||
|
||||
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void adcDataQueueReset(void)
|
||||
{
|
||||
gtAdcDataQueue.ulReadPos = 0;
|
||||
gtAdcDataQueue.ulWritePos = 0;
|
||||
}
|
||||
|
||||
uint16_t adcDataQueueLen(void)
|
||||
{
|
||||
uint16_t ulLength = 0;
|
||||
|
||||
if (gtAdcDataQueue.ulWritePos == gtAdcDataQueue.ulReadPos) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (gtAdcDataQueue.ulWritePos > gtAdcDataQueue.ulReadPos)
|
||||
{
|
||||
ulLength = gtAdcDataQueue.ulWritePos - gtAdcDataQueue.ulReadPos;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulLength = ADC_QUEUE_LEN - gtAdcDataQueue.ulReadPos + gtAdcDataQueue.ulWritePos;
|
||||
}
|
||||
|
||||
return ulLength;
|
||||
}
|
||||
|
||||
void adcSampleStart(void)
|
||||
{
|
||||
if (_giSampleInProgress) {
|
||||
return;
|
||||
}
|
||||
printf("sample start \r\n");
|
||||
adcUpdateParam();
|
||||
__disable_irq();
|
||||
adcDataQueueReset();
|
||||
|
||||
gulAdcSampleIndex = 0;
|
||||
ulSampleStartTime = osKernelGetTickCount();
|
||||
__enable_irq();
|
||||
_gslEncCnt = 0;
|
||||
|
||||
if (gptSysParam->ucUseEncoder == 0) {
|
||||
printf("time sample \r\n");
|
||||
HAL_TIM_OC_Stop_IT(&htim1, TIM_CHANNEL_3);
|
||||
HAL_TIM_OC_Stop_IT(&htim1, TIM_CHANNEL_4);
|
||||
HAL_TIM_Base_Start_IT(&htim3);
|
||||
} else {
|
||||
HAL_TIM_Base_Stop_IT(&htim3);
|
||||
TIM1->CNT = 10000;
|
||||
HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_3);
|
||||
HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_4);
|
||||
printf("start encoder\n");
|
||||
}
|
||||
|
||||
_giSampleInProgress = 1;
|
||||
}
|
||||
|
||||
void adcSampleStop(void)
|
||||
{
|
||||
if (!_giSampleInProgress) {
|
||||
return;
|
||||
}
|
||||
|
||||
printf("sample stop \r\n");
|
||||
|
||||
HAL_TIM_Base_Stop_IT(&htim3);
|
||||
HAL_TIM_OC_Stop_IT(&htim1, TIM_CHANNEL_3);
|
||||
HAL_TIM_OC_Stop_IT(&htim1, TIM_CHANNEL_4);
|
||||
_giEncoderTrigFlag = 0;
|
||||
|
||||
_giSampleInProgress = 0;
|
||||
}
|
||||
|
||||
void setEncoderInterval(uint16_t usPeriod)
|
||||
{
|
||||
TIM_OC_InitTypeDef sConfigOC = {0};
|
||||
sConfigOC.OCMode = TIM_OCMODE_TIMING;
|
||||
sConfigOC.Pulse = 10000 - usPeriod;
|
||||
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
||||
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
||||
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sConfigOC.Pulse = 10000 + usPeriod;
|
||||
if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
TIM1->CNT = 10000;
|
||||
}
|
||||
|
||||
void adcUpdateParam(void)
|
||||
{
|
||||
setEncoderInterval(gptSysParam->usAdcinterval);
|
||||
|
||||
setTimerPeriod(gptSysParam->usAdcPeriod);
|
||||
}
|
||||
|
||||
void TIM3_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
adcStart(gptSysParam->ucAverageCount);
|
||||
}
|
||||
|
||||
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
if (htim->Instance == TIM1) {
|
||||
/**
|
||||
* @brief 编码器中断
|
||||
*/
|
||||
TIM1->CNT = 10000;
|
||||
adcStart(gptSysParam->ucAverageCount);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
#ifndef ADS8321_H
|
||||
#define ADS8321_H
|
||||
|
||||
#include "main.h"
|
||||
|
||||
#define ADC_CHANNEL_NUM 2
|
||||
#define ADC_QUEUE_LEN 6000
|
||||
#define PACKET_DATA_UNIT_NUM 6000
|
||||
|
||||
#pragma pack(1)
|
||||
typedef struct __adcDataUnitDef {
|
||||
uint16_t aulData[ADC_CHANNEL_NUM];
|
||||
} adcDataUnitDef;
|
||||
|
||||
typedef struct __adcDataQueueDef {
|
||||
uint32_t ulReadPos;
|
||||
uint32_t ulWritePos;
|
||||
adcDataUnitDef atQueue[ADC_QUEUE_LEN];
|
||||
} adcDataQueueDef;
|
||||
#pragma pack()
|
||||
|
||||
extern volatile adcDataQueueDef gtAdcDataQueue;
|
||||
|
||||
uint16_t adcDataQueueLen(void);
|
||||
void adcSampleStart(void);
|
||||
void adcSampleStop(void);
|
||||
|
||||
#endif // ADS8321_H
|
||||
Reference in New Issue
Block a user