初始提交
This commit is contained in:
@@ -0,0 +1,853 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "ddc112.h"
|
||||
#include "GPIO_HAL/gpioHal.h"
|
||||
#include "main.h"
|
||||
#include "Param/param.h"
|
||||
#include "IAP/iap.h"
|
||||
|
||||
#define EXTERN_CAP_VALUE 270 ///< pF
|
||||
#define REF_VOLTAGE 4.096 ///< V
|
||||
|
||||
extern TIM_HandleTypeDef htim3;
|
||||
extern TIM_HandleTypeDef htim2;
|
||||
extern TIM_HandleTypeDef htim4;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
IO_ID_RANGE0 = 0,
|
||||
IO_ID_RANGE1,
|
||||
IO_ID_RANGE2,
|
||||
|
||||
IO_ID_DXMIT,
|
||||
IO_ID_DCLK,
|
||||
IO_ID_DOUT,
|
||||
IO_ID_DVALID,
|
||||
|
||||
IO_ID_CONV,
|
||||
IO_ID_TEST,
|
||||
|
||||
} IO_INDEX_DEF;
|
||||
|
||||
typedef struct __moduleIoTypeDef
|
||||
{
|
||||
gpioHalDef tPort; ///< 端口
|
||||
gpioHalModeDef eMode; ///< 端口配置模式
|
||||
uint8_t ucInitLevel; ///< 初始电平
|
||||
} moduleIoTypeDef;
|
||||
|
||||
static const moduleIoTypeDef _gatIoArray[] = {
|
||||
{{GPIOD, GPIO_PIN_4}, GPIO_Mode_Out_PP, 0}, ///< RANGE0
|
||||
{{GPIOD, GPIO_PIN_3}, GPIO_Mode_Out_PP, 0}, ///< RANGE1
|
||||
{{GPIOD, GPIO_PIN_2}, GPIO_Mode_Out_PP, 0}, ///< RANGE2
|
||||
|
||||
{{GPIOB, GPIO_PIN_7}, GPIO_Mode_Out_PP, 1}, ///< DXMIT,active low
|
||||
{{GPIOC, GPIO_PIN_10}, GPIO_Mode_Out_PP, 0}, ///< DCLK,normaly low
|
||||
{{GPIOC, GPIO_PIN_11}, GPIO_Mode_IPU, 0}, ///< DOUT
|
||||
{{GPIOC, GPIO_PIN_12}, GPIO_Mode_IPU, 0}, ///< DVALID,active low
|
||||
|
||||
{{GPIOB, GPIO_PIN_5}, GPIO_Mode_IPU, 0}, ///< CONV,output for mastrt,input for slave
|
||||
{{GPIOD, GPIO_PIN_5}, GPIO_Mode_Out_PP, 0}, ///< High for master to output sync,Low for slave to input sync
|
||||
|
||||
{NULL, 0},
|
||||
};
|
||||
|
||||
struct
|
||||
{
|
||||
uint32_t ulAdcClk;
|
||||
uint32_t ulConvPeriod; ///< us
|
||||
DDC_RANG_DEF eRange;
|
||||
float fCapValue; ///< pF
|
||||
} _gtModuleInfo;
|
||||
|
||||
|
||||
#pragma pack(1)
|
||||
typedef struct __transferPacketDef
|
||||
{
|
||||
uint16_t usHeaderA;
|
||||
uint16_t usHeaderB;
|
||||
uint16_t usQueueRemainDataLen;
|
||||
uint16_t usLen;
|
||||
adcDataUnitDef atData[PACKET_DATA_UNIT_NUM];
|
||||
} transferPacketDef;
|
||||
#pragma pack()
|
||||
|
||||
static transferPacketDef _gtTmpPkt;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
WORK_MODE_IDLE = 0,
|
||||
WORK_MODE_CMD_SAMPLE,
|
||||
WORK_MODE_PIN_SAMPLE,
|
||||
WORK_MODE_SEND_DATA,
|
||||
WORK_MODE_WAIT,
|
||||
} DDC112_WORK_MODE_DEF;
|
||||
|
||||
volatile adcDataQueueDef gtAdcDataQueue;
|
||||
volatile uint32_t gulAdcSampleIndex;
|
||||
volatile uint32_t ulSampleStartTime = 0;
|
||||
volatile uint32_t ulSampleDuration = 0;
|
||||
volatile int _iWorkMode = WORK_MODE_IDLE;
|
||||
|
||||
typedef struct {
|
||||
uint32_t ulTick;
|
||||
uint32_t ulCnt;
|
||||
} tickTimeDef;
|
||||
|
||||
tickTimeDef _gtLastTrigTime;
|
||||
int _giEncoderTrigFlag = 0;
|
||||
uint32_t gulEncoderSpeed = 0; /// 0.1mm/s
|
||||
|
||||
static void resetAdcDataQueue(void);
|
||||
|
||||
static void _delay()
|
||||
{
|
||||
__asm("nop");
|
||||
__asm("nop");
|
||||
__asm("nop");
|
||||
__asm("nop");
|
||||
__asm("nop");
|
||||
__asm("nop");
|
||||
}
|
||||
|
||||
static void _ioInit(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void _dvalidInterruptInit(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static void _readChannel(void)
|
||||
{
|
||||
uint8_t ucVal;
|
||||
uint8_t i;
|
||||
uint32_t ulData1 = 0, ulData2 = 0;
|
||||
static uint32_t ulData1_old = 0, ulData2_old = 0;
|
||||
|
||||
// printf("readChannel start\n");
|
||||
GPIO_HAL_LOW(&_gatIoArray[IO_ID_DCLK].tPort);
|
||||
GPIO_HAL_LOW(&_gatIoArray[IO_ID_DXMIT].tPort);
|
||||
_delay();
|
||||
|
||||
for (i = 0; i < 20; i++)
|
||||
{
|
||||
ulData1 <<= 1;
|
||||
_delay();
|
||||
GPIO_HAL_HIGH(&_gatIoArray[IO_ID_DCLK].tPort);
|
||||
_delay();
|
||||
ucVal = GPIO_HAL_READ(&_gatIoArray[IO_ID_DOUT].tPort);
|
||||
GPIO_HAL_LOW(&_gatIoArray[IO_ID_DCLK].tPort);
|
||||
if (ucVal)
|
||||
{
|
||||
++ulData1;
|
||||
}
|
||||
}
|
||||
|
||||
for (i = 0; i < 20; i++)
|
||||
{
|
||||
ulData2 <<= 1;
|
||||
_delay();
|
||||
GPIO_HAL_HIGH(&_gatIoArray[IO_ID_DCLK].tPort);
|
||||
_delay();
|
||||
ucVal = GPIO_HAL_READ(&_gatIoArray[IO_ID_DOUT].tPort);
|
||||
GPIO_HAL_LOW(&_gatIoArray[IO_ID_DCLK].tPort);
|
||||
if (ucVal)
|
||||
{
|
||||
++ulData2;
|
||||
}
|
||||
}
|
||||
|
||||
_delay();
|
||||
|
||||
GPIO_HAL_HIGH(&_gatIoArray[IO_ID_DXMIT].tPort);
|
||||
|
||||
if (gulAdcSampleIndex >= 10)
|
||||
{
|
||||
volatile adcDataUnitDef *ptData = >AdcDataQueue.atQueue[gtAdcDataQueue.ulWritePos];
|
||||
|
||||
if (gptSysParam->ucFilterMode == 0)
|
||||
{
|
||||
ptData->aulData[0] = (uint16_t)(ulData1 / 16);
|
||||
ptData->aulData[1] = (uint16_t)(ulData2 / 16);
|
||||
}
|
||||
else
|
||||
{
|
||||
ptData->aulData[0] = (uint16_t)((ulData1 + ulData1_old) / 2 / 16);
|
||||
ptData->aulData[1] = (uint16_t)((ulData2 + ulData2_old) / 2 / 16);
|
||||
}
|
||||
|
||||
gtAdcDataQueue.ulWritePos++;
|
||||
if (gtAdcDataQueue.ulWritePos >= ADC_QUEUE_LEN)
|
||||
{
|
||||
gtAdcDataQueue.ulWritePos = 0;
|
||||
}
|
||||
}
|
||||
|
||||
ulData1_old = ulData1;
|
||||
ulData2_old = ulData2;
|
||||
}
|
||||
|
||||
void ddc112SetRange(DDC_RANG_DEF eRange)
|
||||
{
|
||||
if (eRange & 0x01)
|
||||
{
|
||||
GPIO_HAL_HIGH(&_gatIoArray[IO_ID_RANGE0].tPort);
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_HAL_LOW(&_gatIoArray[IO_ID_RANGE0].tPort);
|
||||
}
|
||||
|
||||
if (eRange & 0x02)
|
||||
{
|
||||
GPIO_HAL_HIGH(&_gatIoArray[IO_ID_RANGE1].tPort);
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_HAL_LOW(&_gatIoArray[IO_ID_RANGE1].tPort);
|
||||
}
|
||||
|
||||
if (eRange & 0x04)
|
||||
{
|
||||
GPIO_HAL_HIGH(&_gatIoArray[IO_ID_RANGE2].tPort);
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_HAL_LOW(&_gatIoArray[IO_ID_RANGE2].tPort);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ddc112ValueToCurrent
|
||||
* @param ulValue
|
||||
* @return 电流,单位uA
|
||||
*/
|
||||
float ddc112ValueToCurrent(uint32_t ulValue)
|
||||
{
|
||||
double lfVol;
|
||||
double lfTmp;
|
||||
#define ZERO_VALUE 0x1000
|
||||
#define FULL_VALUE 0xFFFFF
|
||||
#define FS_FACTOR 0.96
|
||||
if (ulValue == 0x1000)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
lfVol = REF_VOLTAGE * FS_FACTOR;
|
||||
|
||||
if (ulValue > ZERO_VALUE)
|
||||
{
|
||||
ulValue -= ZERO_VALUE;
|
||||
|
||||
lfVol = lfVol * ulValue / FULL_VALUE;
|
||||
|
||||
lfTmp = lfVol * _gtModuleInfo.fCapValue / _gtModuleInfo.ulConvPeriod; ///< uA
|
||||
|
||||
return (float)lfTmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
ulValue = ZERO_VALUE - ulValue;
|
||||
lfVol = lfVol * ulValue / FULL_VALUE;
|
||||
|
||||
lfTmp = lfVol * _gtModuleInfo.fCapValue / _gtModuleInfo.ulConvPeriod; ///< uA
|
||||
lfTmp = -lfTmp;
|
||||
return (float)lfTmp;
|
||||
}
|
||||
}
|
||||
|
||||
void tim3PeriodInit(uint32_t ulPeriod)
|
||||
{
|
||||
htim3.Init.Period = ulPeriod - 1;
|
||||
|
||||
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
}
|
||||
|
||||
void pwmConvInit(uint32_t ulPeriod)
|
||||
{
|
||||
TIM_MasterConfigTypeDef sMasterConfig = {0};
|
||||
TIM_OC_InitTypeDef sConfigOC = {0};
|
||||
|
||||
tim3PeriodInit(ulPeriod * 2 - 1);
|
||||
|
||||
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
|
||||
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
|
||||
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sConfigOC.OCMode = TIM_OCMODE_PWM1;
|
||||
sConfigOC.Pulse = ulPeriod - 1;
|
||||
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
|
||||
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
|
||||
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
/* USER CODE BEGIN TIM3_Init 2 */
|
||||
|
||||
/* USER CODE END TIM3_Init 2 */
|
||||
HAL_TIM_MspPostInit(&htim3);
|
||||
}
|
||||
|
||||
void ddc112SetTestMode(uint8_t ucMode)
|
||||
{
|
||||
if (ucMode)
|
||||
{
|
||||
GPIO_HAL_HIGH(&_gatIoArray[IO_ID_TEST].tPort);
|
||||
debug("enable ddc112 test mode");
|
||||
}
|
||||
else
|
||||
{
|
||||
GPIO_HAL_LOW(&_gatIoArray[IO_ID_TEST].tPort);
|
||||
debug("disable ddc112 test mode");
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* DDC112数据采集就绪
|
||||
*/
|
||||
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
|
||||
{
|
||||
if (GPIO_Pin == MCU_ADC_DVAILD_Pin)
|
||||
{
|
||||
gulAdcSampleIndex++;
|
||||
_readChannel();
|
||||
}
|
||||
}
|
||||
|
||||
static void resetAdcDataQueue(void)
|
||||
{
|
||||
gtAdcDataQueue.ulReadPos = 0;
|
||||
gtAdcDataQueue.ulWritePos = 0;
|
||||
}
|
||||
|
||||
static uint16_t getBufferedDataLength(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;
|
||||
}
|
||||
|
||||
static void base64_encode(const unsigned char *input, size_t input_len, char *output)
|
||||
{
|
||||
static const char base64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
int i = 0, j = 0;
|
||||
unsigned char char_array_3[3], char_array_4[4];
|
||||
|
||||
while (input_len--)
|
||||
{
|
||||
char_array_3[i++] = *(input++);
|
||||
if (i == 3)
|
||||
{
|
||||
char_array_4[0] = (char_array_3[0] >> 2) & 0x3F;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) | (char_array_3[1] >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0F) << 2) | (char_array_3[2] >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3F;
|
||||
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
output[j++] = base64_table[char_array_4[i]];
|
||||
}
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i)
|
||||
{
|
||||
for (int k = i; k < 3; k++)
|
||||
{
|
||||
char_array_3[k] = '\0';
|
||||
}
|
||||
|
||||
char_array_4[0] = (char_array_3[0] >> 2) & 0x3F;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) | (char_array_3[1] >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0F) << 2) | (char_array_3[2] >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3F;
|
||||
|
||||
for (int k = 0; k < i + 1; k++)
|
||||
{
|
||||
output[j++] = base64_table[char_array_4[k]];
|
||||
}
|
||||
|
||||
while (i++ < 3)
|
||||
{
|
||||
output[j++] = '=';
|
||||
}
|
||||
}
|
||||
output[j] = '\0'; // Null-terminate the output string
|
||||
}
|
||||
|
||||
static int sendDataPacket(uint8_t *pucData, uint16_t usLen)
|
||||
{
|
||||
static char aucBase64Buf[45 * 1024];
|
||||
static char aucUdpBuf[64 * 1024];
|
||||
|
||||
if (usLen <= 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (usLen > 41 * 1024)
|
||||
{
|
||||
printf("data too long\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
base64_encode(pucData, usLen, aucBase64Buf);
|
||||
sprintf(aucUdpBuf, "{\"type\":\"data_points\",\"name\":\"daq\",\"data\":\"%s\"}", aucBase64Buf);
|
||||
|
||||
#if 0
|
||||
if (udpSendBroadcast(aucUdpBuf, strlen(aucUdpBuf)) < 0)
|
||||
{
|
||||
printf("udp send failed \r\n");
|
||||
return -1;
|
||||
}
|
||||
#else
|
||||
// udpSendTo(aucUdpBuf, strlen(aucUdpBuf),
|
||||
// "192.168.8.12",
|
||||
// 50009);
|
||||
udpSendTo(aucUdpBuf, strlen(aucUdpBuf),
|
||||
"192.168.100.100",
|
||||
50009);
|
||||
osDelay(100);
|
||||
udpSendTo(aucUdpBuf, strlen(aucUdpBuf),
|
||||
"192.168.100.10",
|
||||
50009);
|
||||
#endif
|
||||
|
||||
printf("send ok \r\n");
|
||||
|
||||
return usLen;
|
||||
}
|
||||
|
||||
static int makeDataPacket(void)
|
||||
{
|
||||
uint16_t ulPos = 0;
|
||||
uint16_t i = 0;
|
||||
uint16_t ulLen = getBufferedDataLength();
|
||||
uint16_t send_size = 0;
|
||||
|
||||
if (ulLen < 5)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
ulLen -= 5;
|
||||
|
||||
for (i = 0; i < ulLen; ++i)
|
||||
{
|
||||
ulPos = gtAdcDataQueue.ulReadPos + i;
|
||||
if (ulPos >= ADC_QUEUE_LEN)
|
||||
{
|
||||
ulPos -= ADC_QUEUE_LEN;
|
||||
}
|
||||
volatile adcDataUnitDef *ptData = >AdcDataQueue.atQueue[ulPos];
|
||||
memcpy(&_gtTmpPkt.atData[i], (uint8_t *)ptData, sizeof(adcDataUnitDef));
|
||||
}
|
||||
|
||||
_gtTmpPkt.usHeaderA = 0xAA55;
|
||||
_gtTmpPkt.usHeaderB = 0x55CC;
|
||||
_gtTmpPkt.usLen = ulLen;
|
||||
printf("_gtTmpPkt.usLen : %d , _gtTmpPkt.usQueueRemainDataLen : %d \r\n", _gtTmpPkt.usLen, _gtTmpPkt.usQueueRemainDataLen);
|
||||
|
||||
send_size = 8 + sizeof(adcDataUnitDef) * ulLen;
|
||||
|
||||
return send_size;
|
||||
}
|
||||
|
||||
void encoderModeInit()
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/**TIM3 GPIO Configuration
|
||||
PB5 ------> TIM3_CH2
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_5;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void encoderModeDeinit()
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
|
||||
/**TIM3 GPIO Configuration
|
||||
PB5 ------> TIM3_CH2
|
||||
*/
|
||||
GPIO_InitStruct.Pin = GPIO_PIN_5;
|
||||
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
|
||||
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
|
||||
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void _sampleStart(void)
|
||||
{
|
||||
if (gptSysParam->usDdc112Period > 300) {
|
||||
encoderModeDeinit();
|
||||
_readChannel();
|
||||
|
||||
resetAdcDataQueue();
|
||||
|
||||
gulAdcSampleIndex = 0;
|
||||
ulSampleStartTime = osKernelGetTickCount();
|
||||
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);
|
||||
} else {
|
||||
_readChannel();
|
||||
__disable_irq();
|
||||
encoderModeInit();
|
||||
resetAdcDataQueue();
|
||||
|
||||
gulAdcSampleIndex = 0;
|
||||
ulSampleStartTime = osKernelGetTickCount();
|
||||
__enable_irq();
|
||||
}
|
||||
}
|
||||
|
||||
void _sampleStop(void)
|
||||
{
|
||||
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_2);
|
||||
_giEncoderTrigFlag = 0;
|
||||
HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, GPIO_PIN_RESET);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ddc112Start
|
||||
* @return 1:成功,0:失败
|
||||
*/
|
||||
int ddc112Start(uint16_t delay, uint32_t ulDuration)
|
||||
{
|
||||
if (_iWorkMode == WORK_MODE_IDLE) {
|
||||
osDelay(delay);
|
||||
_sampleStart();
|
||||
_iWorkMode = WORK_MODE_CMD_SAMPLE;
|
||||
// printf("ddc112Start\r\n");
|
||||
ulSampleDuration = ulDuration;
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief ddc112Stop
|
||||
* @return 1:成功,0:失败
|
||||
*/
|
||||
int ddc112Stop(void)
|
||||
{
|
||||
if (_iWorkMode == 1)
|
||||
{
|
||||
_iWorkMode = 0;
|
||||
_sampleStop();
|
||||
printf("ddc112Stop\r\n");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void test_correct()
|
||||
{
|
||||
ddc112SetTestMode(1);
|
||||
osDelay(100);
|
||||
|
||||
_sampleStart();
|
||||
osDelay(100);
|
||||
uint16_t ulPos = 0;
|
||||
uint16_t i = 0;
|
||||
uint16_t ulLen = getBufferedDataLength();
|
||||
uint16_t send_size = 0;
|
||||
|
||||
if (ulLen == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
for (i = 0; i < ulLen; ++i)
|
||||
{
|
||||
ulPos = gtAdcDataQueue.ulReadPos + i;
|
||||
if (ulPos >= ADC_QUEUE_LEN)
|
||||
{
|
||||
ulPos -= ADC_QUEUE_LEN;
|
||||
}
|
||||
volatile adcDataUnitDef *ptData = >AdcDataQueue.atQueue[ulPos];
|
||||
memcpy(&_gtTmpPkt.atData[i], (uint8_t *)ptData, sizeof(adcDataUnitDef));
|
||||
}
|
||||
}
|
||||
|
||||
static void ddc112_task(void *arg)
|
||||
{
|
||||
_iWorkMode = WORK_MODE_IDLE;
|
||||
while (1)
|
||||
{
|
||||
switch (_iWorkMode)
|
||||
{
|
||||
case WORK_MODE_IDLE: // 空闲状态
|
||||
if (gptSysParam->usPinSampleTime > 0) {
|
||||
if (HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_8) == GPIO_PIN_RESET)
|
||||
{
|
||||
if (gptSysParam->usPinSampleDelay > 0) {
|
||||
osDelay(gptSysParam->usPinSampleDelay);
|
||||
}
|
||||
ulSampleDuration = gptSysParam->usPinSampleTime;
|
||||
if (ulSampleDuration > 16000) {
|
||||
ulSampleDuration = 16000;
|
||||
}
|
||||
_sampleStart();
|
||||
_iWorkMode = WORK_MODE_PIN_SAMPLE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case WORK_MODE_CMD_SAMPLE: // 命令采样状态
|
||||
case WORK_MODE_PIN_SAMPLE: // 引脚采样状态
|
||||
if (osKernelGetTickCount() > ulSampleStartTime + ulSampleDuration)
|
||||
{
|
||||
_sampleStop();
|
||||
_iWorkMode = WORK_MODE_SEND_DATA;
|
||||
printf("sample timeout \r\n");
|
||||
printf("sample data length: %d\r\n", getBufferedDataLength());
|
||||
}
|
||||
else if (getBufferedDataLength() > PACKET_DATA_UNIT_NUM)
|
||||
{
|
||||
_sampleStop();
|
||||
_iWorkMode = WORK_MODE_SEND_DATA;
|
||||
printf("sample data length: %d\r\n", getBufferedDataLength());
|
||||
}
|
||||
break;
|
||||
case WORK_MODE_SEND_DATA: // 发送数据状态
|
||||
{
|
||||
int ret = makeDataPacket();
|
||||
if (ret > 0)
|
||||
{
|
||||
sendDataPacket((uint8_t *)&_gtTmpPkt, ret);
|
||||
}
|
||||
resetAdcDataQueue();
|
||||
_iWorkMode = WORK_MODE_WAIT;
|
||||
}
|
||||
break;
|
||||
case WORK_MODE_WAIT: // 发送数据状态
|
||||
{
|
||||
if (gptSysParam->usPinSampleTime == 0) {
|
||||
_iWorkMode = WORK_MODE_IDLE;
|
||||
} else if (HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_8) == GPIO_PIN_SET) {
|
||||
_iWorkMode = WORK_MODE_IDLE;
|
||||
}
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
osDelay(1);
|
||||
}
|
||||
}
|
||||
|
||||
void aimSpeedSet(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(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
sConfigOC.Pulse = 10000 + usPeriod;
|
||||
if (HAL_TIM_OC_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
|
||||
{
|
||||
Error_Handler();
|
||||
}
|
||||
TIM2->CNT = 10000;
|
||||
}
|
||||
|
||||
void ddc112UpdateParam(void)
|
||||
{
|
||||
ddc112SetRange(gptSysParam->ucDdc112Range);
|
||||
if (gptSysParam->usDdc112Period < 300) {
|
||||
aimSpeedSet(gptSysParam->usDdc112Period);
|
||||
} else {
|
||||
pwmConvInit(gptSysParam->usDdc112Period);
|
||||
}
|
||||
if (gptSysParam->ucTestMode)
|
||||
{
|
||||
ddc112SetTestMode(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
ddc112SetTestMode(0);
|
||||
}
|
||||
}
|
||||
|
||||
void ddc112Init(void)
|
||||
{
|
||||
static const float afRangeCaps[] = {
|
||||
EXTERN_CAP_VALUE,
|
||||
12.5, 25, 37.5, 50, 62.5, 75, 87.5};
|
||||
_gtModuleInfo.eRange = (DDC_RANG_DEF)(gptSysParam->ucDdc112Range);
|
||||
_gtModuleInfo.ulConvPeriod = gptSysParam->usDdc112Period;
|
||||
_gtModuleInfo.ulAdcClk = 15000000;
|
||||
_gtModuleInfo.fCapValue = afRangeCaps[_gtModuleInfo.eRange];
|
||||
|
||||
_ioInit();
|
||||
|
||||
ddc112UpdateParam();
|
||||
printf("ddc112 pwm conv period: %d\r\n", _gtModuleInfo.ulConvPeriod);
|
||||
|
||||
_dvalidInterruptInit();
|
||||
|
||||
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1); ///< 启动定时器4通道1
|
||||
HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL);
|
||||
HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_3);
|
||||
HAL_TIM_OC_Start_IT(&htim2, TIM_CHANNEL_4);
|
||||
TIM2->CNT = 10000;
|
||||
|
||||
// 创建任务
|
||||
const osThreadAttr_t ddc112_task_attributes = {
|
||||
.name = "ddc112_task",
|
||||
.stack_size = 4096 * 4,
|
||||
.priority = (osPriority_t)osPriorityNormal,
|
||||
};
|
||||
if (osThreadNew(ddc112_task, NULL, &ddc112_task_attributes) == NULL)
|
||||
{
|
||||
printf("failed to create ddc112_task\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("create ddc112_task success\n");
|
||||
}
|
||||
}
|
||||
|
||||
void TIM3_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
uint32_t getDeltaTime(tickTimeDef *last, tickTimeDef *now)
|
||||
{
|
||||
if (last->ulTick == 0) {
|
||||
return 0;
|
||||
}
|
||||
if (now->ulTick < last->ulTick) {
|
||||
return 0;
|
||||
} else if (now->ulTick == last->ulTick) {
|
||||
if (now->ulCnt < last->ulCnt) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return (now->ulTick - last->ulTick) * 1000 + (now->ulCnt - last->ulCnt);
|
||||
}
|
||||
|
||||
void channelHandler(uint32_t ulVal)
|
||||
{
|
||||
tickTimeDef tim;
|
||||
|
||||
if (_giEncoderTrigFlag > 10) {
|
||||
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
|
||||
}
|
||||
|
||||
tim.ulCnt = TIM13->CNT;
|
||||
tim.ulTick = uwTick;
|
||||
|
||||
uint32_t ulTim = getDeltaTime(&_gtLastTrigTime, &tim);
|
||||
|
||||
if (ulTim > 0) {
|
||||
gulEncoderSpeed = gptSysParam->usDdc112Period * 10000 / ulTim;
|
||||
}
|
||||
|
||||
if (gulEncoderSpeed >= gptSysParam->usDdc112Period * 10 * 8 / 10) {
|
||||
if (_giEncoderTrigFlag < 100) {
|
||||
_giEncoderTrigFlag++;
|
||||
}
|
||||
}
|
||||
|
||||
// printf("pulse 40, %u, flag %d\n", gulEncoderSpeed, _giEncoderTrigFlag);
|
||||
|
||||
_gtLastTrigTime = tim;
|
||||
}
|
||||
|
||||
void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
if (htim->Instance == TIM2) {
|
||||
uint32_t ulVal = TIM2->CNT;
|
||||
TIM2->CNT = 10000;
|
||||
channelHandler(ulVal);
|
||||
} else if (htim == &htim3) {
|
||||
// gulAdcSampleIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
void TIM2_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
||||
{
|
||||
tickTimeDef tim;
|
||||
|
||||
if (_giEncoderTrigFlag > 10) {
|
||||
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);
|
||||
}
|
||||
|
||||
tim.ulCnt = TIM13->CNT;
|
||||
tim.ulTick = uwTick;
|
||||
|
||||
uint32_t ulVal = getDeltaTime(&_gtLastTrigTime, &tim);
|
||||
|
||||
gulEncoderSpeed = gptSysParam->usDdc112Period * 10000 / ulVal;
|
||||
|
||||
if (gulEncoderSpeed >= gptSysParam->usDdc112Period * 10 * 8 / 10) {
|
||||
if (_giEncoderTrigFlag < 100) {
|
||||
_giEncoderTrigFlag++;
|
||||
}
|
||||
} else {
|
||||
_giEncoderTrigFlag = 0;
|
||||
}
|
||||
|
||||
// printf("pulse 40, %u, flag %d\n", gulEncoderSpeed, _giEncoderTrigFlag);
|
||||
|
||||
_gtLastTrigTime = tim;
|
||||
}
|
||||
|
||||
// void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
|
||||
//{
|
||||
// if(htim->Instance == TIM2)
|
||||
// {
|
||||
// if((ulEnc_sta & 0x80) == 0)
|
||||
// {
|
||||
// if(ulEnc_sta & 0x40)
|
||||
// {
|
||||
// ulEnc_sta |= 0x80;
|
||||
// ulEnc =HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);
|
||||
// TIM_RESET_CAPTUREPOLARITY(&htim2,TIM_CHANNEL_1);
|
||||
// }
|
||||
// else {
|
||||
// ulEnc_sta = 0;
|
||||
// ulEnc = 0;
|
||||
// ulEnc_sta = 0x40;
|
||||
// __HAL_TIM_DISABLE(&htim2);
|
||||
// __HAL_TIM_SET_COUNTER(&htim2,0);
|
||||
// TIM_RESET_CAPTUREPOLARITY(&htim2,TIM_CHANNEL_1);
|
||||
// __HAL_TIM_ENABLE(&htim2);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user