767 lines
19 KiB
C
767 lines
19 KiB
C
#include "cmsis_os.h"
|
|
#include "adc.h"
|
|
#include "GPIO_HAL/gpioHal.h"
|
|
#include "main.h"
|
|
#include "Param/param.h"
|
|
#include "IAP/iap.h"
|
|
|
|
#include <lwip/sockets.h>
|
|
#include "lwip/sys.h"
|
|
#include "lwip/api.h"
|
|
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
|
|
#include "UDP/udpp.h"
|
|
|
|
extern TIM_HandleTypeDef htim3;
|
|
extern TIM_HandleTypeDef htim1;
|
|
extern ADC_HandleTypeDef hadc1;
|
|
extern ADC_HandleTypeDef hadc2;
|
|
|
|
typedef enum
|
|
{
|
|
WORK_MODE_IDLE = 0,
|
|
WORK_MODE_CMD_SAMPLE,
|
|
WORK_MODE_PIN_SAMPLE,
|
|
WORK_MODE_SEND_DATA,
|
|
WORK_MODE_WAIT,
|
|
} WORK_MODE_DEF;
|
|
|
|
typedef enum
|
|
{
|
|
ADC_TRIG_NONE = 0,
|
|
ADC_TRIG_TIM3,
|
|
ADC_TRIG_TIM1_1,
|
|
ADC_TRIG_TIM1_2,
|
|
} ADC_TRIG_DEF;
|
|
|
|
#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;
|
|
|
|
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;
|
|
volatile int _iWorkMode = WORK_MODE_IDLE;
|
|
|
|
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 void resetAdcDataQueue(void);
|
|
int net_print(const char *format, ...);
|
|
static int sendDataPacket(uint8_t *pucData, uint16_t usLen);
|
|
|
|
//#define NET_PRINT(fmt, args...) net_print(fmt, ##args)
|
|
#define NET_PRINT(fmt, args...)
|
|
|
|
static uint32_t getEncoderPos(uint32_t ulVal)
|
|
{
|
|
uint32_t ulPos = ulVal;
|
|
if (ulPos >= 10000) {
|
|
ulPos -= 10000;
|
|
} else {
|
|
ulPos = 10000 - ulPos;
|
|
}
|
|
|
|
ulPos += (_gslEncCnt * 1000);
|
|
|
|
return ulPos;
|
|
}
|
|
|
|
static void _readChannel(void)
|
|
{
|
|
uint32_t ulTime = _gulSampleTimeUs;
|
|
uint32_t ulData1 = _gaulAdcValue[0];
|
|
uint32_t ulData2 = _gaulAdcValue[1];
|
|
|
|
adcDataUnitDef *ptData = >AdcDataQueue.atQueue[gtAdcDataQueue.ulWritePos];
|
|
|
|
gulAdcSampleIndex++;
|
|
|
|
if (gulAdcSampleIndex >= 5) {
|
|
ptData->aulData[0] = ulData1;
|
|
ptData->aulData[1] = ulData2;
|
|
// ptData->ulTime = ulTime;
|
|
// ptData->ulPos = _gulSamplePosUm;
|
|
|
|
gtAdcDataQueue.ulWritePos++;
|
|
if (gtAdcDataQueue.ulWritePos >= ADC_QUEUE_LEN) {
|
|
gtAdcDataQueue.ulWritePos = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
void tim3PeriodInit(uint32_t ulPeriod)
|
|
{
|
|
htim3.Init.Period = ulPeriod - 1;
|
|
|
|
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
|
|
{
|
|
Error_Handler();
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
int tcpSendTo(uint8_t *buf, int len, char *addr, uint16_t port) {
|
|
int sockfd;
|
|
struct sockaddr_in tcpToAddr;
|
|
int res;
|
|
|
|
// 创建 TCP 套接字
|
|
sockfd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (sockfd < 0) {
|
|
perror("socket creation failed");
|
|
return -1;
|
|
}
|
|
|
|
// 设置目标地址
|
|
memset(&tcpToAddr, 0, sizeof(tcpToAddr));
|
|
tcpToAddr.sin_family = AF_INET;
|
|
inet_pton(AF_INET, addr, &tcpToAddr.sin_addr.s_addr);
|
|
tcpToAddr.sin_port = htons(port);
|
|
|
|
// 建立连接
|
|
if (connect(sockfd, (struct sockaddr *)&tcpToAddr, sizeof(tcpToAddr)) < 0) {
|
|
perror("connection failed");
|
|
close(sockfd);
|
|
return -1;
|
|
}
|
|
|
|
// 发送数据
|
|
res = send(sockfd, buf, len, 0);
|
|
if (res < 0) {
|
|
perror("send failed");
|
|
close(sockfd);
|
|
return -1;
|
|
}
|
|
|
|
// 关闭连接
|
|
close(sockfd);
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
// net_print 函数实现
|
|
int net_print(const char *format, ...) {
|
|
// 定义一个足够大的缓冲区来存放格式化后的字符串
|
|
char buffer[1024]; // 根据需要调整大小
|
|
|
|
va_list args;
|
|
va_start(args, format);
|
|
|
|
// 使用 vsnprintf 格式化输出到 buffer
|
|
int len = vsnprintf(buffer, sizeof(buffer), format, args);
|
|
va_end(args);
|
|
|
|
// 检查输出长度是否超出缓冲区
|
|
if (len < 0 || len >= sizeof(buffer)) {
|
|
// 格式化失败或输出过长
|
|
return -1;
|
|
}
|
|
if (buffer[len - 1] != '\n') {
|
|
buffer[len] = '\n';
|
|
len++;
|
|
}
|
|
|
|
printf(buffer);
|
|
// 发送数据包
|
|
udpSendTo((uint8_t *)buffer, (uint16_t)len,
|
|
"192.168.100.100",
|
|
50009);
|
|
}
|
|
|
|
void adcDataPacketAck(uint32_t ulPid)
|
|
{
|
|
gulPacketAck = ulPid;
|
|
}
|
|
|
|
static int sendDataPacket(uint8_t *pucData, uint16_t usLen)
|
|
{
|
|
char *dir = "unknown";
|
|
static char aucBase64Buf[45 * 1024];
|
|
static char aucUdpBuf[63 * 1024];
|
|
|
|
if (usLen <= 0)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
if (usLen > 41 * 1024)
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
gulPacketIndex++;
|
|
|
|
memset(aucBase64Buf, 0, sizeof(aucBase64Buf));
|
|
memset(aucUdpBuf, 0, sizeof(aucUdpBuf));
|
|
|
|
base64_encode(pucData, usLen, aucBase64Buf);
|
|
if (giScanDirection == 10) {
|
|
dir = "up";
|
|
} else if (giScanDirection == -10) {
|
|
dir = "down";
|
|
}
|
|
snprintf(aucUdpBuf, sizeof(aucUdpBuf),
|
|
"{\"type\":\"data_points\",\"name\":\"daq\",\"ip\":\"%s\",\"direction\":\"%s\",\"pid\":%u,\"data\":\"%s\"}",
|
|
gptSysParam->acIp,
|
|
dir, gulPacketIndex,
|
|
aucBase64Buf);
|
|
|
|
gulPacketAck = 0;
|
|
|
|
if (gptSysParam->ucTestMode) {
|
|
NET_PRINT("to send data:%d\n", strlen(aucUdpBuf) + 1);
|
|
udpSendTo(aucUdpBuf, strlen(aucUdpBuf) + 1,
|
|
"192.168.100.100",
|
|
50009);
|
|
osDelay(100);
|
|
}
|
|
|
|
udpSendBigPacket(aucUdpBuf, strlen(aucUdpBuf) + 1,
|
|
"192.168.100.10",
|
|
50009);
|
|
|
|
osDelay(500);
|
|
|
|
if (gulPacketAck != gulPacketIndex) {
|
|
printf("noack retry \r\n");
|
|
udpSendBigPacket(aucUdpBuf, strlen(aucUdpBuf) + 1,
|
|
"192.168.100.10",
|
|
50009);
|
|
} else {
|
|
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)
|
|
{
|
|
NET_PRINT("not enouth len:%d\r\n", ulLen);
|
|
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));
|
|
}
|
|
|
|
_gtTmpPkt.usHeaderA = 0xAA55;
|
|
_gtTmpPkt.usHeaderB = 0x55CC;
|
|
_gtTmpPkt.usQueueRemainDataLen = 0x00;
|
|
_gtTmpPkt.usLen = ulLen;
|
|
NET_PRINT("_gtTmpPkt.usLen : %d , _gtTmpPkt.usQueueRemainDataLen : %d \r\n", _gtTmpPkt.usLen, _gtTmpPkt.usQueueRemainDataLen);
|
|
|
|
send_size = 8 + sizeof(adcDataUnitDef) * ulLen;
|
|
|
|
return send_size;
|
|
}
|
|
|
|
void adcTriggerSelect(ADC_TRIG_DEF trigger)
|
|
{
|
|
switch (trigger) {
|
|
case ADC_TRIG_NONE:
|
|
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
|
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
|
break;
|
|
case ADC_TRIG_TIM3:
|
|
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO;
|
|
hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T3_TRGO;
|
|
break;
|
|
case ADC_TRIG_TIM1_1:
|
|
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO;
|
|
hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO;
|
|
break;
|
|
case ADC_TRIG_TIM1_2:
|
|
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO2;
|
|
hadc2.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO2;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
HAL_ADC_Init(&hadc1);
|
|
HAL_ADC_Init(&hadc2);
|
|
}
|
|
|
|
|
|
void _sampleStart(void)
|
|
{
|
|
__disable_irq();
|
|
resetAdcDataQueue();
|
|
|
|
gulAdcSampleIndex = 0;
|
|
ulSampleStartTime = osKernelGetTickCount();
|
|
__enable_irq();
|
|
_gslEncCnt = 0;
|
|
|
|
if (gptSysParam->usDdc112Period > 300) {
|
|
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");
|
|
}
|
|
}
|
|
|
|
void _sampleStop(void)
|
|
{
|
|
if (gptSysParam->usDdc112Period > 300) {
|
|
HAL_TIM_Base_Stop_IT(&htim3);
|
|
} else {
|
|
HAL_TIM_OC_Stop_IT(&htim1, TIM_CHANNEL_3);
|
|
HAL_TIM_OC_Stop_IT(&htim1, TIM_CHANNEL_4);
|
|
_giEncoderTrigFlag = 0;
|
|
}
|
|
if (gptSysParam->ucTestMode == 0) {
|
|
HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, GPIO_PIN_RESET);
|
|
}
|
|
}
|
|
|
|
void mcuAdcInit(void)
|
|
{
|
|
adcUpdateParam();
|
|
|
|
// HAL_ADC_Start_DMA(&hadc1, (uint32_t *)(&ADCxValues[64]), 128);
|
|
// HAL_ADC_Start_DMA(&hadc2, (uint32_t *)(&ADCxValues[80]), 128);
|
|
|
|
HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
|
|
HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED);
|
|
|
|
HAL_ADCEx_Calibration_Start(&hadc2, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
|
|
HAL_ADCEx_Calibration_Start(&hadc2, ADC_CALIB_OFFSET_LINEARITY, ADC_SINGLE_ENDED);
|
|
|
|
// HAL_ADC_Start(&hadc1);
|
|
// HAL_ADC_Start(&hadc2);
|
|
|
|
// HAL_ADC_Start_IT(&hadc1);
|
|
// HAL_ADC_Start_IT(&hadc2);
|
|
|
|
printf("adc start\r\n");
|
|
}
|
|
|
|
int adcSampleStart(uint16_t delay, uint32_t ulDuration, uint8_t dir)
|
|
{
|
|
if (_iWorkMode == WORK_MODE_IDLE) {
|
|
HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, GPIO_PIN_SET);
|
|
if (delay > 0) {
|
|
osDelay(delay);
|
|
}
|
|
if (dir == 1) {
|
|
giScanDirection = 10;
|
|
} else if (dir == 2) {
|
|
giScanDirection = -10;
|
|
} else {
|
|
giScanDirection = 0;
|
|
}
|
|
_sampleStart();
|
|
_iWorkMode = WORK_MODE_CMD_SAMPLE;
|
|
printf("ddc112Start\r\n");
|
|
ulSampleDuration = ulDuration;
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int adcSampleStop(void)
|
|
{
|
|
if (_iWorkMode == 1)
|
|
{
|
|
_iWorkMode = 0;
|
|
_sampleStop();
|
|
printf("ddc112Stop\r\n");
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int giAdcThreadStatus = 0;
|
|
static void sample_task(void *arg)
|
|
{
|
|
_iWorkMode = WORK_MODE_IDLE;
|
|
|
|
osDelay(500);
|
|
|
|
while (1)
|
|
{
|
|
switch (_iWorkMode)
|
|
{
|
|
case WORK_MODE_IDLE: // 空闲状态
|
|
if (gptSysParam->usPinSampleTime > 0) {
|
|
if (HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_8) == GPIO_PIN_RESET)
|
|
{
|
|
HAL_GPIO_WritePin(RELAY_GPIO_Port, RELAY_Pin, GPIO_PIN_SET);
|
|
if (gptSysParam->usPinSampleDelay > 0) {
|
|
osDelay(gptSysParam->usPinSampleDelay);
|
|
}
|
|
ulSampleDuration = gptSysParam->usPinSampleTime;
|
|
if (ulSampleDuration > 8000) {
|
|
ulSampleDuration = 8000;
|
|
}
|
|
_sampleStart();
|
|
_iWorkMode = WORK_MODE_PIN_SAMPLE;
|
|
}
|
|
}
|
|
giAdcThreadStatus = 0;
|
|
break;
|
|
case WORK_MODE_CMD_SAMPLE: // 命令采样状态
|
|
case WORK_MODE_PIN_SAMPLE: // 引脚采样状态
|
|
if ((getBufferedDataLength() >= ulSampleDuration) || (osKernelGetTickCount() > ulSampleStartTime + ulSampleDuration * 2))
|
|
{
|
|
_sampleStop();
|
|
_iWorkMode = WORK_MODE_SEND_DATA;
|
|
NET_PRINT("sample timeout \r\n");
|
|
NET_PRINT("sample data length: %d\r\n", getBufferedDataLength());
|
|
}
|
|
else if (getBufferedDataLength() > PACKET_DATA_UNIT_NUM)
|
|
{
|
|
_sampleStop();
|
|
_iWorkMode = WORK_MODE_SEND_DATA;
|
|
NET_PRINT("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;
|
|
giScanDirection = 0;
|
|
}
|
|
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(&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)
|
|
{
|
|
if (gptSysParam->usDdc112Period < 300) {
|
|
aimSpeedSet(gptSysParam->usDdc112Period);
|
|
} else {
|
|
tim3PeriodInit(gptSysParam->usDdc112Period);
|
|
}
|
|
}
|
|
|
|
void sampleInit(void)
|
|
{
|
|
htim1.Instance->CNT = 10000;
|
|
HAL_TIM_Base_Start(&htim1);
|
|
|
|
mcuAdcInit();
|
|
printf("adc period: %d\r\n", gptSysParam->usDdc112Period);
|
|
|
|
// 创建任务
|
|
const osThreadAttr_t sample_task_attributes = {
|
|
.name = "sample_task",
|
|
.stack_size = 4096 * 4,
|
|
.priority = (osPriority_t)osPriorityNormal,
|
|
};
|
|
if (osThreadNew(sample_task, NULL, &sample_task_attributes) == NULL)
|
|
{
|
|
printf("failed to create sample_task\n");
|
|
}
|
|
else
|
|
{
|
|
printf("create sample_task success\n");
|
|
}
|
|
|
|
|
|
// HAL_TIM_Base_Stop_IT(&htim3);
|
|
TIM1->CNT = 10000;
|
|
HAL_TIM_Encoder_Start(&htim1, TIM_CHANNEL_ALL);
|
|
// HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_3);
|
|
// HAL_TIM_OC_Start_IT(&htim1, TIM_CHANNEL_4);
|
|
}
|
|
|
|
void TIM3_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
|
|
{
|
|
// printf("TIM3 %d %d\n", uwTick, TIM13->CNT);
|
|
HAL_ADC_Start_IT(&hadc1);
|
|
HAL_ADC_Start_IT(&hadc2);
|
|
_gaiFlag[0] = 0;
|
|
_gaiFlag[1] = 0;
|
|
}
|
|
|
|
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;
|
|
_gulSampleTimeUs = TIM13->CNT;
|
|
_gulSampleTimeUs += uwTick * 1000;
|
|
_gulSamplePosUm = getEncoderPos(ulVal);
|
|
|
|
if (ulVal > 10000) {
|
|
if (gptSysParam->ucFilterMode == 0) {
|
|
if ((giScanDirection >= 0) && (giScanDirection < 10)) {
|
|
giScanDirection++;
|
|
}
|
|
} else {
|
|
if ((giScanDirection <= 0) && (giScanDirection > -10)) {
|
|
giScanDirection--;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (ulVal < 10000) {
|
|
if (gptSysParam->ucFilterMode == 0) {
|
|
if ((giScanDirection <= 0) && (giScanDirection > -10)) {
|
|
giScanDirection--;
|
|
}
|
|
} else {
|
|
if ((giScanDirection >= 0) && (giScanDirection < 10)) {
|
|
giScanDirection++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (1/*_giEncoderTrigFlag > 10*/) {
|
|
_gaiFlag[0] = 0;
|
|
_gaiFlag[1] = 0;
|
|
HAL_ADC_Start_IT(&hadc1);
|
|
HAL_ADC_Start_IT(&hadc2);
|
|
}
|
|
|
|
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 == TIM1) {
|
|
uint32_t ulVal = TIM1->CNT;
|
|
#if 0
|
|
uint32_t ulTmp = 10000;
|
|
if (ulVal >= (10000 + gptSysParam->usDdc112Period)) {
|
|
ulTmp = ulVal - gptSysParam->usDdc112Period;
|
|
} else if (ulVal <= (10000 - gptSysParam->usDdc112Period)) {
|
|
ulTmp = gptSysParam->usDdc112Period + ulVal;
|
|
} else {
|
|
printf("%d, %u\n", htim->Channel ,ulVal);
|
|
return;
|
|
}
|
|
TIM1->CNT = ulTmp;
|
|
#else
|
|
TIM1->CNT = 10000;
|
|
++_gslEncCnt;
|
|
#endif
|
|
channelHandler(ulVal);
|
|
} else if (htim == &htim3) {
|
|
// gulAdcSampleIndex++;
|
|
}
|
|
}
|
|
|
|
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
|
{
|
|
// uint32_t ulVal = TIM1->CNT;
|
|
if (hadc == &hadc1) {
|
|
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);
|
|
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
|
|
_gaulAdcValue[0] = HAL_ADC_GetValue(&hadc1);
|
|
_gaiFlag[0] = 1;
|
|
} else if (hadc == &hadc2) {
|
|
// HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_10);
|
|
// HAL_GPIO_WritePin(GPIOA, GPIO_PIN_10, GPIO_PIN_RESET);
|
|
_gaulAdcValue[1] = HAL_ADC_GetValue(&hadc2);
|
|
_gaiFlag[1] = 1;
|
|
}
|
|
|
|
if (_gaiFlag[0] && _gaiFlag[1]) {
|
|
_readChannel();
|
|
// printf("%d, %u\n", _gslEncCnt * 40, ulVal);
|
|
}
|
|
}
|
|
|
|
|
|
|