Compare commits
3 Commits
Author | SHA1 | Date |
---|---|---|
|
e063b10a80 | |
|
dd74e17915 | |
|
a9eb7a6831 |
|
@ -4,10 +4,7 @@
|
|||
"board.h": "c",
|
||||
"motor.h": "c",
|
||||
"main.h": "c",
|
||||
"app.h": "c",
|
||||
"type_traits": "c",
|
||||
"adcs.h": "c",
|
||||
"uarts.h": "c"
|
||||
"app.h": "c"
|
||||
},
|
||||
"C_Cpp.errorSquiggles": "disabled"
|
||||
}
|
|
@ -44,17 +44,10 @@ void MX_ADC_Init(void)
|
|||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);
|
||||
LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOB);
|
||||
/**ADC GPIO Configuration
|
||||
PC0 ------> ADC_IN10
|
||||
PC3 ------> ADC_IN13
|
||||
PA7 ------> ADC_IN7
|
||||
PB0 ------> ADC_IN8
|
||||
PB1 ------> ADC_IN9
|
||||
*/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = MG_ADC_IN13_Pin;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
|
@ -70,11 +63,6 @@ void MX_ADC_Init(void)
|
|||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/* ADC DMA Init */
|
||||
|
||||
/* ADC Init */
|
||||
|
@ -106,14 +94,6 @@ void MX_ADC_Init(void)
|
|||
*/
|
||||
LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_8);
|
||||
|
||||
/** Configure Regular Channel
|
||||
*/
|
||||
LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_9);
|
||||
|
||||
/** Configure Regular Channel
|
||||
*/
|
||||
LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_10);
|
||||
|
||||
/** Configure Regular Channel
|
||||
*/
|
||||
LL_ADC_REG_SetSequencerChAdd(ADC1, LL_ADC_CHANNEL_13);
|
||||
|
|
|
@ -78,6 +78,12 @@ void MX_GPIO_Init(void)
|
|||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/**/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/**/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
|
@ -144,6 +150,12 @@ void MX_GPIO_Init(void)
|
|||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
||||
|
||||
/**/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_1;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
GPIO_InitStruct.Pull = LL_GPIO_PULL_NO;
|
||||
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
||||
|
||||
/**/
|
||||
GPIO_InitStruct.Pin = LL_GPIO_PIN_2;
|
||||
GPIO_InitStruct.Mode = LL_GPIO_MODE_ANALOG;
|
||||
|
|
|
@ -154,7 +154,7 @@ void DMA1_Channel1_IRQHandler(void)
|
|||
/* USER CODE END DMA1_Channel1_IRQn 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA1_Channel1_IRQn 1 */
|
||||
adc_convert_callback(adc1);
|
||||
|
||||
/* USER CODE END DMA1_Channel1_IRQn 1 */
|
||||
}
|
||||
|
||||
|
@ -168,7 +168,7 @@ void DMA1_Channel2_3_IRQHandler(void)
|
|||
/* USER CODE END DMA1_Channel2_3_IRQn 0 */
|
||||
|
||||
/* USER CODE BEGIN DMA1_Channel2_3_IRQn 1 */
|
||||
uart_dma_reception_callback(uarts[UART_NUM_1]);
|
||||
|
||||
/* USER CODE END DMA1_Channel2_3_IRQn 1 */
|
||||
}
|
||||
|
||||
|
@ -201,8 +201,10 @@ void TIM21_IRQHandler(void)
|
|||
/* USER CODE BEGIN TIM21_IRQn 1 */
|
||||
if (LL_TIM_IsActiveFlag_UPDATE(TIM21))
|
||||
{
|
||||
__NOP();
|
||||
step_motor_update(motor);
|
||||
LL_TIM_ClearFlag_UPDATE(TIM21);
|
||||
__NOP();
|
||||
}
|
||||
/* USER CODE END TIM21_IRQn 1 */
|
||||
}
|
||||
|
@ -216,7 +218,7 @@ void USART1_IRQHandler(void)
|
|||
|
||||
/* USER CODE END USART1_IRQn 0 */
|
||||
/* USER CODE BEGIN USART1_IRQn 1 */
|
||||
uart_reception_callback(uarts[UART_NUM_1]);
|
||||
|
||||
/* USER CODE END USART1_IRQn 1 */
|
||||
}
|
||||
|
||||
|
|
|
@ -145,7 +145,7 @@
|
|||
<SetRegEntry>
|
||||
<Number>0</Number>
|
||||
<Key>ST-LINKIII-KEIL_SWO</Key>
|
||||
<Name>-US -O2254 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(0BC11477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8000 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO15 -FD20000000 -FC800 -FN1 -FF0STM32L0xx_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32L072RBTx$CMSIS\Flash\STM32L0xx_128.FLM) -WA0 -WE0 -WVCE4 -WS2710 -WM0 -WP2</Name>
|
||||
<Name>-US -O2254 -SF10000 -C0 -A0 -I0 -HNlocalhost -HP7184 -P1 -N00("ARM CoreSight SW-DP (ARM Core") -D00(0BC11477) -L00(0) -TO131090 -TC10000000 -TT10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC800 -FN1 -FF0STM32L0xx_128.FLM -FS08000000 -FL020000 -FP0($$Device:STM32L072RBTx$CMSIS\Flash\STM32L0xx_128.FLM) -WA0 -WE0 -WVCE4 -WS2710 -WM0 -WP2</Name>
|
||||
</SetRegEntry>
|
||||
</TargetDriverDllRegistry>
|
||||
<Breakpoint/>
|
||||
|
@ -158,37 +158,24 @@
|
|||
<Ww>
|
||||
<count>1</count>
|
||||
<WinNumber>1</WinNumber>
|
||||
<ItemText>test_buffer,0x10</ItemText>
|
||||
<ItemText>motor_state,0x0A</ItemText>
|
||||
</Ww>
|
||||
<Ww>
|
||||
<count>2</count>
|
||||
<WinNumber>1</WinNumber>
|
||||
<ItemText>self_test,0x0A</ItemText>
|
||||
<ItemText>motor->handle.step_motor.attribute,0x0A</ItemText>
|
||||
</Ww>
|
||||
<Ww>
|
||||
<count>3</count>
|
||||
<WinNumber>1</WinNumber>
|
||||
<ItemText>motor,0x0A</ItemText>
|
||||
<ItemText>motor_param,0x0A</ItemText>
|
||||
</Ww>
|
||||
<Ww>
|
||||
<count>4</count>
|
||||
<WinNumber>1</WinNumber>
|
||||
<ItemText>motor_param,0x0A</ItemText>
|
||||
</Ww>
|
||||
<Ww>
|
||||
<count>5</count>
|
||||
<WinNumber>1</WinNumber>
|
||||
<ItemText>detect_result,0x0A</ItemText>
|
||||
<ItemText>\\motor\../User/board/board.c\motor_state,0x0A</ItemText>
|
||||
</Ww>
|
||||
</WatchWindow1>
|
||||
<MemoryWindow1>
|
||||
<Mm>
|
||||
<WinNumber>1</WinNumber>
|
||||
<SubType>0</SubType>
|
||||
<ItemText>0x20000090</ItemText>
|
||||
<AccSizeX>0</AccSizeX>
|
||||
</Mm>
|
||||
</MemoryWindow1>
|
||||
<Tracepoint>
|
||||
<THDelay>0</THDelay>
|
||||
</Tracepoint>
|
||||
|
@ -491,7 +478,7 @@
|
|||
|
||||
<Group>
|
||||
<GroupName>User</GroupName>
|
||||
<tvExp>1</tvExp>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<cbSel>0</cbSel>
|
||||
<RteFlg>0</RteFlg>
|
||||
|
@ -787,7 +774,7 @@
|
|||
|
||||
<Group>
|
||||
<GroupName>User/board</GroupName>
|
||||
<tvExp>1</tvExp>
|
||||
<tvExp>0</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<cbSel>0</cbSel>
|
||||
<RteFlg>0</RteFlg>
|
||||
|
|
|
@ -54,7 +54,7 @@
|
|||
<CreateLib>0</CreateLib>
|
||||
<CreateHexFile>1</CreateHexFile>
|
||||
<DebugInformation>1</DebugInformation>
|
||||
<BrowseInformation>0</BrowseInformation>
|
||||
<BrowseInformation>1</BrowseInformation>
|
||||
<ListingPath></ListingPath>
|
||||
<HexFormatSelection>1</HexFormatSelection>
|
||||
<Merge32K>0</Merge32K>
|
||||
|
@ -138,7 +138,7 @@
|
|||
</Flash1>
|
||||
<bUseTDR>1</bUseTDR>
|
||||
<Flash2>BIN\UL2V8M.DLL</Flash2>
|
||||
<Flash3>"" ()</Flash3>
|
||||
<Flash3></Flash3>
|
||||
<Flash4></Flash4>
|
||||
<pFcarmOut></pFcarmOut>
|
||||
<pFcarmGrp></pFcarmGrp>
|
||||
|
@ -314,7 +314,7 @@
|
|||
</ArmAdsMisc>
|
||||
<Cads>
|
||||
<interw>1</interw>
|
||||
<Optim>1</Optim>
|
||||
<Optim>0</Optim>
|
||||
<oTime>0</oTime>
|
||||
<SplitLS>0</SplitLS>
|
||||
<OneElfS>1</OneElfS>
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -15,10 +15,8 @@ typedef struct
|
|||
typedef struct
|
||||
{
|
||||
calibration_sensor_data_t torsion_in13; // 扭力
|
||||
calibration_sensor_data_t ipressure_in7; // 压力
|
||||
calibration_sensor_data_t iflow_in8; // 流量
|
||||
calibration_sensor_data_t opressure_in9; // 压力
|
||||
calibration_sensor_data_t oflow_in10; // 流量
|
||||
calibration_sensor_data_t pressure_in7; // 压力
|
||||
calibration_sensor_data_t pressure_in8; // 流量
|
||||
} adcs_t;
|
||||
|
||||
typedef struct
|
||||
|
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
extern __IO app_t app;
|
||||
extern motor_t *motor;
|
||||
extern uint8_t detect_result;
|
||||
|
||||
static struct flow fl_adc_inspection; // ADC
|
||||
static struct flow fl_systom_inspection; // 系统
|
||||
|
@ -18,29 +17,16 @@ static uint8_t adc_inspection(struct flow *fl)
|
|||
FL_HEAD(fl);
|
||||
for (;;)
|
||||
{
|
||||
detect_result = ST_DEV_NORMAL;
|
||||
// 扭力
|
||||
app.adc.torsion_in13.original_value = get_sensor_adc(GET_TORSION_VALUE);
|
||||
app.adc.torsion_in13.calibration_value = torsion_detect();
|
||||
sensor_adc[GET_TORSION_VALUE] = S2B_UINT16(app.adc.torsion_in13.original_value);
|
||||
// 输入压力
|
||||
app.adc.ipressure_in7.original_value = get_sensor_adc(GET_INPUT_PRESS_VALUE);
|
||||
app.adc.ipressure_in7.calibration_value = pressure_detect(GET_INPUT_PRESS_VALUE);
|
||||
sensor_adc[GET_INPUT_PRESS_VALUE] = S2B_UINT16(app.adc.ipressure_in7.original_value);
|
||||
// 输出压力
|
||||
app.adc.opressure_in9.original_value = get_sensor_adc(GET_OUTPUT_PRESS_VALUE);
|
||||
app.adc.opressure_in9.calibration_value = pressure_detect(GET_OUTPUT_PRESS_VALUE);
|
||||
sensor_adc[GET_OUTPUT_PRESS_VALUE] = S2B_UINT16(app.adc.opressure_in9.original_value);
|
||||
// 输入流量
|
||||
app.adc.iflow_in8.original_value = get_sensor_adc(GET_INPUT_FLOW_VALUE);
|
||||
app.adc.iflow_in8.calibration_value = flow_detect(GET_INPUT_FLOW_VALUE);
|
||||
sensor_adc[GET_INPUT_FLOW_VALUE] = S2B_UINT16(app.adc.iflow_in8.original_value);
|
||||
// 输出流量
|
||||
app.adc.oflow_in10.original_value = get_sensor_adc(GET_OUTPUT_FLOW_VALUE);
|
||||
app.adc.oflow_in10.calibration_value = flow_detect(GET_OUTPUT_FLOW_VALUE);
|
||||
sensor_adc[GET_OUTPUT_FLOW_VALUE] = S2B_UINT16(app.adc.oflow_in10.original_value);
|
||||
|
||||
FL_LOCK_DELAY(fl, FL_CLOCK_10MSEC); /* 延时10毫秒 */
|
||||
app.adc.torsion_in13.original_value = adc_get_result_average(IN13);
|
||||
app.adc.torsion_in13.calibration_value = torsion_detect(app.adc.torsion_in13.original_value);
|
||||
// 压力
|
||||
app.adc.pressure_in7.original_value = adc_get_result_average(IN7);
|
||||
app.adc.pressure_in7.calibration_value = pressure_detect(app.adc.pressure_in7.original_value);
|
||||
// 流量
|
||||
app.adc.pressure_in8.original_value = adc_get_result_average(IN8);
|
||||
app.adc.pressure_in8.calibration_value = flow_detect(app.adc.pressure_in8.original_value);
|
||||
FL_LOCK_DELAY(fl, FL_CLOCK_100MSEC); /* 延时100毫秒 */
|
||||
}
|
||||
FL_TAIL(fl);
|
||||
}
|
||||
|
@ -50,8 +36,8 @@ static uint8_t systom_inspection(struct flow *fl)
|
|||
FL_HEAD(fl);
|
||||
for (;;)
|
||||
{
|
||||
host_rx_msg_deal();
|
||||
FL_LOCK_DELAY(fl, FL_CLOCK_10MSEC); /* 延时10毫秒 */
|
||||
motor_process();
|
||||
FL_LOCK_DELAY(fl, FL_CLOCK_100MSEC); /* 延时100毫秒 */
|
||||
}
|
||||
FL_TAIL(fl);
|
||||
}
|
||||
|
@ -61,9 +47,8 @@ static uint8_t idle_inspection(struct flow *fl)
|
|||
FL_HEAD(fl);
|
||||
for (;;)
|
||||
{
|
||||
test_process();
|
||||
cpu_percent = scheduler_time_occupancy_get(1000);
|
||||
FL_LOCK_DELAY(fl, FL_CLOCK_10MSEC);
|
||||
FL_LOCK_DELAY(fl, FL_CLOCK_SEC);
|
||||
}
|
||||
FL_TAIL(fl);
|
||||
}
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
#include "motor.h"
|
||||
|
|
@ -1,8 +0,0 @@
|
|||
#ifndef __MOTOR_APP_H__
|
||||
#define __MOTOR_APP_H__
|
||||
|
||||
|
||||
|
||||
extern void motor_process(void);
|
||||
|
||||
#endif
|
|
@ -1,170 +1,47 @@
|
|||
#include "board.h"
|
||||
#include "motor.h"
|
||||
|
||||
extern uint8_t detect_result;
|
||||
void motor_process(frame_msg_t *rx, frame_msg_t *bk);
|
||||
void sensor_process(frame_msg_t *rx, frame_msg_t *bk);
|
||||
void error_process(uint8_t err, frame_msg_t *rx, frame_msg_t *bk);
|
||||
|
||||
/************************************* 串口通讯 *************************************/
|
||||
uart_t *uarts[UART_NUM_MAX];
|
||||
__IO static BOOL execute_state = IDLE; // 数据处理标志
|
||||
static frame_msg_t rx_msg; // 接收数据句柄
|
||||
static frame_msg_t bk_msg; // 回复数据句柄
|
||||
static uint8_t host_send_buffer[UART_TXSIZE] = {0}; // 数据发送缓存
|
||||
|
||||
// 串口发送
|
||||
static void host_send_msg(uint8_t *data, uint16_t len)
|
||||
{
|
||||
uart_send_data(uarts[UART_NUM_1], data, len);
|
||||
}
|
||||
|
||||
// 数据有效性检查
|
||||
static BOOL host_data_verify(uint8_t *data, uint16_t len)
|
||||
{
|
||||
BOOL ret = TRUE;
|
||||
uint8_t ver_len = 0;
|
||||
uint8_t msg_crc = 0, ver_crc = 0;
|
||||
|
||||
// 包头包尾检查
|
||||
if (data[0] != PACKET_STX || data[1] != ST_DEV_NORMAL || data[len - 1] != PACKET_ETX)
|
||||
{
|
||||
ret = FALSE;
|
||||
return ret;
|
||||
}
|
||||
// 长度检查
|
||||
ver_len = PACKET_MIN_LEN + data[DATA_LEN_INDEX];
|
||||
if (ver_len != len)
|
||||
{
|
||||
ret = FALSE;
|
||||
return ret;
|
||||
}
|
||||
// CRC校验
|
||||
msg_crc = data[len - 2];
|
||||
ver_crc = xor_compute((uint8_t *)&data[1], (len - 3)); // 减去包头、包尾、校验
|
||||
if (msg_crc != ver_crc)
|
||||
{
|
||||
ret = FALSE;
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 数据解码
|
||||
static void host_data_decode(uint8_t *data, uint16_t len)
|
||||
{
|
||||
DBG_ASSERT(data != NULL __DBG_LINE);
|
||||
|
||||
// 获取数据长度和消息长度
|
||||
uint8_t data_len = data[DATA_LEN_INDEX];
|
||||
uint8_t msg_len = DATA_INDEX + data_len;
|
||||
// 获取消息数据
|
||||
osel_memcpy((uint8_t *)&rx_msg, data, msg_len);
|
||||
}
|
||||
|
||||
// 指令执行
|
||||
static void host_cmd_execute(frame_msg_t *rx_msg, frame_msg_t *bk_msg)
|
||||
{
|
||||
DBG_ASSERT(rx_msg != NULL __DBG_LINE);
|
||||
DBG_ASSERT(bk_msg != NULL __DBG_LINE);
|
||||
|
||||
switch (rx_msg->dev_no)
|
||||
{
|
||||
case ADC_SENSOR:
|
||||
sensor_process(rx_msg, bk_msg);
|
||||
break;
|
||||
case MOTOR:
|
||||
motor_process(rx_msg, bk_msg);
|
||||
break;
|
||||
default:
|
||||
error_process(ST_CMD_IVALID, rx_msg, bk_msg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 数据编码
|
||||
static void host_data_encode(frame_msg_t *msg)
|
||||
{
|
||||
DBG_ASSERT(msg != NULL __DBG_LINE);
|
||||
|
||||
uint8_t *pbuf = (uint8_t *)host_send_buffer;
|
||||
uint8_t index = 0, len = 0;
|
||||
|
||||
// 消息数据
|
||||
uint8_t msg_len = DATA_INDEX + msg->len;
|
||||
osel_memcpy(pbuf, (uint8_t *)msg, msg_len);
|
||||
// 校验码
|
||||
index += msg_len;
|
||||
pbuf[index] = xor_compute((uint8_t *)&pbuf[1], (msg_len - 1)); // 减去包头
|
||||
// 包尾
|
||||
index += 1;
|
||||
pbuf[index] = PACKET_ETX;
|
||||
|
||||
// 计算报文长度
|
||||
len = msg_len + 2; // 加上校验、包尾
|
||||
// 发送
|
||||
host_send_msg(pbuf, len);
|
||||
}
|
||||
|
||||
// 数据处理
|
||||
void host_rx_msg_deal(void)
|
||||
{
|
||||
if (execute_state == IDLE)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
frame_msg_t *p_rx_msg = (frame_msg_t *)&rx_msg;
|
||||
frame_msg_t *p_bk_msg = (frame_msg_t *)&bk_msg;
|
||||
|
||||
// 执行
|
||||
host_cmd_execute(p_rx_msg, p_bk_msg);
|
||||
// 回复(组包 + 发送)
|
||||
host_data_encode(p_bk_msg);
|
||||
// 复位
|
||||
osel_memset((uint8_t *)host_send_buffer, 0, UART_TXSIZE);
|
||||
osel_memset((uint8_t *)&rx_msg, 0, sizeof(frame_msg_t));
|
||||
osel_memset((uint8_t *)&bk_msg, 0, sizeof(frame_msg_t));
|
||||
execute_state = IDLE;
|
||||
detect_result = ST_DEV_NORMAL;
|
||||
}
|
||||
|
||||
// 串口接收回调
|
||||
static void host_rx_cb(uint8_t uart_index, uint8_t *data, uint16_t len)
|
||||
{
|
||||
BOOL ret = FALSE;
|
||||
|
||||
// 数据有效性检查
|
||||
ret = host_data_verify(data, len);
|
||||
// 数据解码
|
||||
if (ret == TRUE && execute_state == IDLE)
|
||||
{
|
||||
host_data_decode(data, len);
|
||||
execute_state = BUSY;
|
||||
}
|
||||
}
|
||||
|
||||
// 串口初始化
|
||||
static void host_uart_init(void)
|
||||
{
|
||||
if (uarts[UART_NUM_1] == NULL)
|
||||
{
|
||||
uarts[UART_NUM_1] = uart_create(USART1, TRUE, UART_RXSIZE, host_rx_cb, TRUE, UART_TXSIZE, NULL);
|
||||
uarts[UART_NUM_1]->uart_index = UART_NUM_1;
|
||||
uarts[UART_NUM_1]->dma = DMA1;
|
||||
uarts[UART_NUM_1]->dma_rx_channel = LL_DMA_CHANNEL_3;
|
||||
uarts[UART_NUM_1]->dma_tx_channel = LL_DMA_CHANNEL_2;
|
||||
uart_recv_en(uarts[UART_NUM_1]);
|
||||
}
|
||||
}
|
||||
|
||||
/************************************* 电机 *************************************/
|
||||
// 电机对象
|
||||
motor_t *motor;
|
||||
motor_ctrl_t *motor_param = NULL;
|
||||
uint8_t motor_state = MOTOR_IDEL;
|
||||
|
||||
adc_t adc1 = {
|
||||
.adc = ADC1,
|
||||
.dma = DMA1,
|
||||
.dma_channel = LL_DMA_CHANNEL_1,
|
||||
};
|
||||
|
||||
// 单位:N·m
|
||||
float32 torsion_detect(uint16_t adc)
|
||||
{
|
||||
float32 val = 0;
|
||||
val = (float32)adc / 4096 * 3000;
|
||||
val = (val - 1500) / 101 / (1.934 * 3) * 10;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
// 单位:kPa
|
||||
float32 pressure_detect(uint16_t adc)
|
||||
{
|
||||
float32 val = 0;
|
||||
val = (float32)adc / 4096 * 3000;
|
||||
val = (val - 600) / (10 * 15) / 16 * 1000;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
// 单位:SLPM
|
||||
float32 flow_detect(uint16_t adc)
|
||||
{
|
||||
float32 val = 0;
|
||||
val = (float32)adc / 4096 * 3000;
|
||||
val = (val - 600) / (10 * 15) / 16 * 300;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
// 电机初始化
|
||||
static void motor_init(void)
|
||||
{
|
||||
motor = motor_create(STEP_MOTOR);
|
||||
|
@ -176,57 +53,7 @@ static void motor_init(void)
|
|||
.port = ENA_GPIO_Port,
|
||||
.pin = ENA_Pin,
|
||||
};
|
||||
motor->handle.step_motor.interface.init(motor, dir, en, MIN_STEP_ANGLE, TIM21, LL_TIM_CHANNEL_CH2);
|
||||
}
|
||||
|
||||
static uint8_t motor_param_get(frame_msg_t *rx, motor_ctrl_t *param)
|
||||
{
|
||||
uint8_t state = ST_DEV_NORMAL;
|
||||
|
||||
// 检查数据长度,方向:1个字节,角度:4个字节
|
||||
if (rx->len < 5)
|
||||
{
|
||||
state = ST_CMD_IVALID;
|
||||
return state;
|
||||
}
|
||||
// 方向
|
||||
param->dir = rx->data[0];
|
||||
// 角度
|
||||
osel_memcpy((uint8_t *)¶m->angle, (uint8_t *)&rx->data[1], 4);
|
||||
param->angle = B2S_FLOAT32(param->angle);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
static uint8_t motor_ctrl(uint8_t sw, motor_ctrl_t *param)
|
||||
{
|
||||
step_motor_t *step_motor = &motor->handle.step_motor;
|
||||
uint8_t state = detect_result;
|
||||
if (sw == RUN && detect_result == ST_DEV_NORMAL)
|
||||
{
|
||||
step_motor->interface.set_angle(motor, param->angle, (dir_e)param->dir);
|
||||
}
|
||||
else
|
||||
{
|
||||
step_motor->interface.stop(motor);
|
||||
}
|
||||
return state;
|
||||
}
|
||||
|
||||
static void motor_state(frame_msg_t *rx, frame_msg_t *bk)
|
||||
{
|
||||
DBG_ASSERT(bk != NULL __DBG_LINE);
|
||||
|
||||
step_motor_t *step_motor = &motor->handle.step_motor;
|
||||
uint8_t state = ST_DEV_NORMAL;
|
||||
|
||||
// 回复数据
|
||||
bk->head = PACKET_STX;
|
||||
bk->state = state;
|
||||
bk->dev_no = rx->dev_no;
|
||||
bk->cmd_no = rx->cmd_no;
|
||||
bk->len = sizeof(uint8_t);
|
||||
bk->data[0] = step_motor->attribute.state;
|
||||
motor->handle.step_motor.interface.init(motor, dir, en, MAX_STEP_ANGLE, TIM21, LL_TIM_CHANNEL_CH2);
|
||||
}
|
||||
|
||||
void motor_process_init(void)
|
||||
|
@ -236,301 +63,38 @@ void motor_process_init(void)
|
|||
osel_memset((uint8_t *)motor_param, 0, sizeof(motor_ctrl_t));
|
||||
}
|
||||
|
||||
// 电机任务
|
||||
void motor_process(frame_msg_t *rx, frame_msg_t *bk)
|
||||
void motor_process(void)
|
||||
{
|
||||
DBG_ASSERT(rx != NULL __DBG_LINE);
|
||||
DBG_ASSERT(bk != NULL __DBG_LINE);
|
||||
step_motor_t *step_motor = &motor->handle.step_motor;
|
||||
|
||||
motor_ctrl_t *ctrl_param = motor_param;
|
||||
uint8_t state = ST_DEV_NORMAL;
|
||||
|
||||
switch (rx->cmd_no)
|
||||
switch (motor_state)
|
||||
{
|
||||
case SET_MOTOR_SPEED: // 设置电机转速
|
||||
break;
|
||||
case MOTOR_MOVE: // 运行电机
|
||||
{
|
||||
// 读取参数
|
||||
state = motor_param_get(rx, ctrl_param);
|
||||
// 运行电机
|
||||
if (state == ST_DEV_NORMAL)
|
||||
{
|
||||
state = motor_ctrl(RUN, ctrl_param);
|
||||
}
|
||||
// 回复数据
|
||||
osel_memcpy((uint8_t *)bk, (uint8_t *)rx, sizeof(frame_msg_t));
|
||||
bk->state = state;
|
||||
break;
|
||||
}
|
||||
case MOTOR_STOP: // 停止电机
|
||||
{
|
||||
// 停止电机
|
||||
state = motor_ctrl(STOP, ctrl_param);
|
||||
// 回复数据
|
||||
osel_memcpy((uint8_t *)bk, (uint8_t *)rx, sizeof(frame_msg_t));
|
||||
bk->state = state;
|
||||
break;
|
||||
}
|
||||
case MOTOR_STATE:
|
||||
motor_state(rx, bk);
|
||||
break;
|
||||
default:
|
||||
error_process(ST_CMD_IVALID, rx, bk);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/************************************* ADc传感器 *************************************/
|
||||
uint8_t detect_result = ST_DEV_NORMAL; // 数据转换结果
|
||||
uint16_t sensor_adc[GET_ALL_VALUE] = {0};
|
||||
|
||||
// AD传感器
|
||||
adc_t adc1 = {
|
||||
.adc = ADC1,
|
||||
.dma = DMA1,
|
||||
.dma_channel = LL_DMA_CHANNEL_1,
|
||||
};
|
||||
|
||||
uint16_t get_sensor_adc(uint8_t id)
|
||||
{
|
||||
uint16_t adc = 0;
|
||||
|
||||
switch (id)
|
||||
{
|
||||
case GET_TORSION_VALUE: // 扭力,单位:N·m
|
||||
adc = adc_get_result_average(IN13);
|
||||
break;
|
||||
case GET_INPUT_PRESS_VALUE: // 输入压力,单位:kPa
|
||||
adc = adc_get_result_average(IN7);
|
||||
break;
|
||||
case GET_OUTPUT_PRESS_VALUE: // 输出压力,单位:kPa
|
||||
adc = adc_get_result_average(IN9);
|
||||
break;
|
||||
case GET_INPUT_FLOW_VALUE: // 输入流量,单位:SLPM
|
||||
adc = adc_get_result_average(IN8);
|
||||
break;
|
||||
case GET_OUTPUT_FLOW_VALUE: // 输出流量,单位:SLPM
|
||||
adc = adc_get_result_average(IN10);
|
||||
break;
|
||||
}
|
||||
|
||||
return adc;
|
||||
}
|
||||
|
||||
float32 torsion_detect(void)
|
||||
{
|
||||
float32 adc = 0.0, val = 0.0;
|
||||
|
||||
adc = get_sensor_adc(GET_TORSION_VALUE);
|
||||
val = adc / 4096 * 3000;
|
||||
if (val < 914 || val > 2086)
|
||||
{
|
||||
detect_result = ST_DEV_MISS;
|
||||
}
|
||||
val = (val - 1500) / 101 / (1.934 * 3) * 10;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
float32 pressure_detect(uint8_t id)
|
||||
{
|
||||
float32 adc = 0.0, val = 0.0;
|
||||
|
||||
adc = get_sensor_adc(id);
|
||||
val = adc / 4096 * 3000;
|
||||
if (val < 500 || val > 3000)
|
||||
{
|
||||
detect_result = ST_DEV_MISS;
|
||||
}
|
||||
val = (val - 600) / (10 * 15) / 16 * 1600;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
float32 flow_detect(uint8_t id)
|
||||
{
|
||||
float32 adc = 0.0, val = 0.0;
|
||||
|
||||
adc = get_sensor_adc(id);
|
||||
val = adc / 4096 * 3000;
|
||||
if (val < 500 || val > 3000)
|
||||
{
|
||||
detect_result = ST_DEV_MISS;
|
||||
}
|
||||
val = (val - 600) / (10 * 15) / 16 * 300;
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
// 传感器任务
|
||||
void sensor_process(frame_msg_t *rx, frame_msg_t *bk)
|
||||
{
|
||||
DBG_ASSERT(rx != NULL __DBG_LINE);
|
||||
DBG_ASSERT(bk != NULL __DBG_LINE);
|
||||
|
||||
uint8_t state = ST_DEV_NORMAL;
|
||||
|
||||
// 检查指令合法性
|
||||
if (rx->cmd_no > GET_ALL_VALUE)
|
||||
{
|
||||
state = ST_CMD_IVALID;
|
||||
goto encode;
|
||||
}
|
||||
|
||||
encode:
|
||||
// 回复数据
|
||||
bk->head = PACKET_STX;
|
||||
bk->state = state;
|
||||
bk->dev_no = rx->dev_no;
|
||||
bk->cmd_no = rx->cmd_no;
|
||||
if (rx->cmd_no >= GET_ALL_VALUE)
|
||||
{
|
||||
bk->len = sizeof(uint16_t) * GET_ALL_VALUE;
|
||||
osel_memcpy((uint8_t *)bk->data, (uint8_t *)&sensor_adc, bk->len);
|
||||
}
|
||||
else
|
||||
{
|
||||
bk->len = sizeof(uint16_t);
|
||||
osel_memcpy((uint8_t *)bk->data, (uint8_t *)&sensor_adc[bk->cmd_no], bk->len);
|
||||
}
|
||||
}
|
||||
|
||||
/************************************* 测试 *************************************/
|
||||
#if SELF_TEST
|
||||
test_t self_test = {TEST_DEV_IDEL, 0};
|
||||
uint8_t test_buffer[UART_RXSIZE] = {0}; // 数据接收缓存
|
||||
#endif
|
||||
|
||||
// 传感器测试指令
|
||||
static uint8_t test_sensor_cmd(uint8_t cmd, uint8_t *buf)
|
||||
{
|
||||
osel_memset(buf, 0, UART_RXSIZE);
|
||||
|
||||
uint8_t ver_len = 0;
|
||||
uint8_t index = 0;
|
||||
frame_msg_t msg;
|
||||
msg.head = PACKET_STX;
|
||||
msg.state = ST_DEV_NORMAL;
|
||||
msg.dev_no = ADC_SENSOR;
|
||||
msg.cmd_no = cmd;
|
||||
msg.len = 0;
|
||||
index = DATA_INDEX;
|
||||
osel_memcpy(buf, (uint8_t *)&msg, index);
|
||||
// 校验
|
||||
buf[index] = xor_compute((uint8_t *)&buf[1], (index - 1)); // 减掉包头
|
||||
index += 1;
|
||||
// 包尾
|
||||
buf[index] = PACKET_ETX;
|
||||
index += 1;
|
||||
// 帧长度
|
||||
ver_len = index;
|
||||
return ver_len;
|
||||
}
|
||||
|
||||
// 电机测试指令
|
||||
static uint8_t test_motor_cmd(uint8_t cmd, uint8_t *buf)
|
||||
{
|
||||
uint8_t ver_len = 0;
|
||||
uint8_t index = 0;
|
||||
frame_msg_t msg;
|
||||
motor_ctrl_t motor_param;
|
||||
msg.head = PACKET_STX;
|
||||
msg.state = ST_DEV_NORMAL;
|
||||
msg.dev_no = MOTOR;
|
||||
msg.cmd_no = cmd;
|
||||
switch (cmd)
|
||||
{
|
||||
case MOTOR_MOVE:
|
||||
msg.len = 5; // 方向1个字节,角度4个字节
|
||||
case MOTOR_RUN:
|
||||
step_motor->interface.run(motor, (dir_e)motor_param->dir);
|
||||
motor_state = MOTOR_IDEL;
|
||||
break;
|
||||
case MOTOR_STOP:
|
||||
msg.len = 0;
|
||||
step_motor->interface.stop(motor);
|
||||
motor_state = MOTOR_IDEL;
|
||||
break;
|
||||
}
|
||||
index = DATA_INDEX;
|
||||
osel_memcpy(buf, (uint8_t *)&msg, index);
|
||||
|
||||
if (cmd == MOTOR_MOVE)
|
||||
{
|
||||
motor_param.dir = DIR_CCW;
|
||||
motor_param.angle = 90;
|
||||
motor_param.angle = S2B_FLOAT32(motor_param.angle);
|
||||
}
|
||||
osel_memcpy((uint8_t *)&buf[index], (uint8_t *)&motor_param, sizeof(motor_ctrl_t));
|
||||
index += sizeof(motor_ctrl_t);
|
||||
// 校验
|
||||
buf[index] = xor_compute((uint8_t *)&buf[1], (index - 1)); // 减掉包头
|
||||
index += 1;
|
||||
// 包尾
|
||||
buf[index] = PACKET_ETX;
|
||||
index += 1;
|
||||
// 帧长度
|
||||
ver_len = index;
|
||||
return ver_len;
|
||||
}
|
||||
|
||||
// 发送测试数据
|
||||
static void test_rx_cb(uint8_t *data, uint8_t len)
|
||||
{
|
||||
BOOL ret = FALSE;
|
||||
|
||||
// 数据有效性检查
|
||||
ret = host_data_verify(data, len);
|
||||
// 数据解码
|
||||
if (ret == TRUE && execute_state == IDLE)
|
||||
{
|
||||
host_data_decode(data, len);
|
||||
execute_state = BUSY;
|
||||
}
|
||||
}
|
||||
|
||||
// 测试任务
|
||||
void test_process(void)
|
||||
{
|
||||
uint8_t len = 0;
|
||||
uint8_t *buf = test_buffer;
|
||||
|
||||
if (self_test.dev_id >= TEST_DEV_MAX)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
switch (self_test.dev_id)
|
||||
{
|
||||
case TEST_DEV_SENSOR:
|
||||
len = test_sensor_cmd(self_test.cmd_id, buf);
|
||||
case MOTOR_STEP:
|
||||
step_motor->interface.set_angle(motor, motor_param->angle, (dir_e)motor_param->dir);
|
||||
motor_state = MOTOR_IDEL;
|
||||
break;
|
||||
case TEST_DEV_MOTOR:
|
||||
len = test_motor_cmd(self_test.cmd_id, buf);
|
||||
case MOTOR_IDEL:
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
test_rx_cb(buf, len);
|
||||
self_test.dev_id = TEST_DEV_IDEL;
|
||||
}
|
||||
|
||||
/************************************* 板卡 *************************************/
|
||||
// 板卡初始化
|
||||
void board_init(void)
|
||||
{
|
||||
motor = NULL;
|
||||
osel_memset((uint8_t *)uarts, 0, sizeof(uarts));
|
||||
my_mem_init(SRAMIN);
|
||||
|
||||
motor_init(); // 电机初始化
|
||||
adc_init(adc1); // 初始化ADC1通道,默认采集AD
|
||||
host_uart_init(); // 串口初始化
|
||||
ENABLE_TIM(TIM6); // 任务流程定时器使能
|
||||
}
|
||||
|
||||
// 不支持的任务
|
||||
void error_process(uint8_t err, frame_msg_t *rx, frame_msg_t *bk)
|
||||
{
|
||||
DBG_ASSERT(rx != NULL __DBG_LINE);
|
||||
DBG_ASSERT(bk != NULL __DBG_LINE);
|
||||
|
||||
osel_memcpy((uint8_t *)bk, (uint8_t *)rx, sizeof(frame_msg_t));
|
||||
bk->state = err;
|
||||
}
|
||||
|
|
|
@ -4,41 +4,11 @@
|
|||
#include "adcs.h"
|
||||
#include "uarts.h"
|
||||
|
||||
#define SELF_TEST 1
|
||||
|
||||
// 串口协议参数
|
||||
#define UART_RXSIZE (240u) // 接收缓冲区 240个字节
|
||||
#define UART_TXSIZE (240u) // 发送缓冲区 240个字节
|
||||
|
||||
// 包头 + 状态码 + 设备号 + 命令号 + 长度 + 数据 + 校验 + 包尾
|
||||
// 1 1 1 1 1 0~128 1 1
|
||||
#define PACKET_STX 0xff // 包头
|
||||
#define PACKET_ETX 0x3c // 包尾
|
||||
#define PACKET_
|
||||
#define DATA_LEN_INDEX 4 // 数据长度偏移
|
||||
#define DATA_INDEX 5 // 数据偏移
|
||||
// 最大数据长度
|
||||
#define DATA_MAX_LEN 128
|
||||
// 最小帧长度:包头 + 状态码 + 设备号 + 命令号 + 长度 + 校验 + 包尾
|
||||
#define PACKET_MIN_LEN 7
|
||||
// 最大帧长度
|
||||
#define PACKET_MAX_LEN (DATA_MAX_LEN + PACKET_MIN_LEN)
|
||||
|
||||
// 电机初始默认值
|
||||
#define PULSE_REV (18000.0 * 5) // 每圈脉冲数(驱动器脉冲)
|
||||
#define MIN_STEP_ANGLE (360 / PULSE_REV) // 最小步距
|
||||
|
||||
typedef enum
|
||||
{
|
||||
IDLE = 0, // 空闲
|
||||
BUSY = 1, // 忙碌
|
||||
} host_state_e;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
STOP = 0, // 停止
|
||||
RUN = 1, // 运行
|
||||
} motor_state_e;
|
||||
#define UART_RXSIZE (240u) // 接收240个字节
|
||||
#define UART_TXSIZE (240u) // 发送240个字节
|
||||
#define PULSE_REV (18000.0 * 5) /* 每圈脉冲数 */
|
||||
#define MAX_STEP_ANGLE (360 / PULSE_REV) /* 最小步距(360/PULSE_REV) */
|
||||
#define ANGLE 180 // 步进电机180度,转盘转动1度
|
||||
|
||||
typedef enum
|
||||
{
|
||||
|
@ -50,62 +20,13 @@ typedef enum
|
|||
UART_NUM_MAX,
|
||||
} uart_num_e;
|
||||
|
||||
// 设备号
|
||||
typedef enum
|
||||
{
|
||||
ADC_SENSOR, // 传感器
|
||||
MOTOR, // 电机
|
||||
} dev_id_e;
|
||||
|
||||
// 电机命令号
|
||||
typedef enum
|
||||
{
|
||||
SET_MOTOR_SPEED, // 设置电机速度
|
||||
MOTOR_MOVE, // 电机启动(参数1:方向,参数2:角度)
|
||||
MOTOR_STOP, // 电机停止
|
||||
MOTOR_STATE, // 电机状态
|
||||
} motor_cmd_e;
|
||||
|
||||
// 传感器命令号
|
||||
typedef enum
|
||||
{
|
||||
GET_TORSION_VALUE, // 扭力
|
||||
GET_INPUT_PRESS_VALUE, // 输入压力
|
||||
GET_OUTPUT_PRESS_VALUE, // 输出压力
|
||||
GET_INPUT_FLOW_VALUE, // 输入流量
|
||||
GET_OUTPUT_FLOW_VALUE, // 输出流量
|
||||
GET_ALL_VALUE, // 全部
|
||||
} sensor_cmd_e;
|
||||
|
||||
// 状态码
|
||||
typedef enum
|
||||
{
|
||||
ST_DEV_NORMAL, // 正常
|
||||
ST_DEV_BUSY, // 忙碌
|
||||
ST_DEV_MISS, // 故障
|
||||
ST_CMD_IVALID, // 无效
|
||||
} status_code_e;
|
||||
|
||||
// 测试
|
||||
typedef enum
|
||||
{
|
||||
TEST_DEV_IDEL,
|
||||
TEST_DEV_SENSOR,
|
||||
TEST_DEV_MOTOR,
|
||||
TEST_DEV_MAX,
|
||||
} test_dev_e;
|
||||
|
||||
#pragma pack(1)
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t head; // 包头
|
||||
uint8_t state; // 状态
|
||||
uint8_t dev_no; // 设备号
|
||||
uint8_t cmd_no; // 命令号
|
||||
uint8_t len; // 长度
|
||||
uint8_t data[DATA_MAX_LEN]; // 数据
|
||||
} frame_msg_t;
|
||||
MOTOR_IDEL,
|
||||
MOTOR_RUN,
|
||||
MOTOR_STOP,
|
||||
MOTOR_STEP,
|
||||
} motor_process_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
|
@ -113,23 +34,11 @@ typedef struct
|
|||
float32 angle;
|
||||
} motor_ctrl_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
uint8_t dev_id;
|
||||
uint8_t cmd_id;
|
||||
} test_t;
|
||||
|
||||
#pragma pack()
|
||||
|
||||
extern uint16_t sensor_adc[GET_ALL_VALUE];
|
||||
|
||||
extern void board_init(void);
|
||||
extern uint16_t get_sensor_adc(uint8_t id);
|
||||
extern float32 torsion_detect(void);
|
||||
extern float32 pressure_detect(uint8_t id);
|
||||
extern float32 flow_detect(uint8_t id);
|
||||
extern void host_rx_msg_deal(void);
|
||||
extern void motor_process_init(void);
|
||||
extern void test_process(void);
|
||||
extern void motor_process(void);
|
||||
extern float32 torsion_detect(uint16_t adc);
|
||||
extern float32 pressure_detect(uint16_t adc);
|
||||
extern float32 flow_detect(uint16_t adc);
|
||||
|
||||
#endif // __BOARD_H__
|
||||
|
|
|
@ -20,8 +20,9 @@ static void step_motor_run(motor_t *motor, dir_e dir)
|
|||
handle->attribute.dir = dir;
|
||||
handle->attribute.en = TRUE;
|
||||
dir == DIR_CCW ? handle->gpios.dir->set(*handle->gpios.dir) : handle->gpios.dir->reset(*handle->gpios.dir);
|
||||
LL_TIM_EnableIT_UPDATE(handle->pwm_timer);
|
||||
// LL_TIM_EnableIT_CC2(handle->pwm_timer);
|
||||
PWM_START(handle->pwm_timer, handle->pwm_channel);
|
||||
handle->attribute.state = TRUE;
|
||||
}
|
||||
|
||||
static void step_motor_stop(motor_t *motor)
|
||||
|
@ -33,7 +34,8 @@ static void step_motor_stop(motor_t *motor)
|
|||
handle->attribute.angle = 0;
|
||||
handle->attribute.pulse_count = 0;
|
||||
PWM_STOP(handle->pwm_timer, handle->pwm_channel);
|
||||
handle->attribute.state = FALSE;
|
||||
LL_TIM_DisableIT_UPDATE(handle->pwm_timer);
|
||||
LL_TIM_ClearFlag_UPDATE(TIM21);
|
||||
if (handle->interface.stop_cb != NULL)
|
||||
{
|
||||
handle->interface.stop_cb(motor);
|
||||
|
@ -62,10 +64,6 @@ static void step_motor_set_angle(motor_t *motor, float32 angle, dir_e dir)
|
|||
}
|
||||
}
|
||||
|
||||
static void step_motor_set_speed(motor_t *motor, uint32_t psc)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 步进电机中断处理函数
|
||||
* @param {motor_t} *motor
|
||||
|
@ -92,7 +90,8 @@ void step_motor_update(motor_t *motor)
|
|||
if (handle->attribute.pulse_count <= 0) /* 当脉冲数等于0的时候 代表需要发送的脉冲个数已完成,停止定时器输出 */
|
||||
{
|
||||
LOG_PRINT("累计旋转的角度:%d\r\n", (int)(handle->attribute.add_pulse_count * handle->attribute.min_step_angle)); /* 打印累计转动了多少角度 */
|
||||
handle->interface.stop(motor); /* 停止接口一输出 */
|
||||
handle->interface.stop(motor);
|
||||
/* 停止接口一输出 */
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -109,7 +108,6 @@ motor_t *motor_create(motor_type_e motor_type)
|
|||
motor->handle.step_motor.interface.run = step_motor_run;
|
||||
motor->handle.step_motor.interface.stop = step_motor_stop;
|
||||
motor->handle.step_motor.interface.set_angle = step_motor_set_angle;
|
||||
motor->handle.step_motor.interface.set_speed = step_motor_set_speed;
|
||||
break;
|
||||
|
||||
default:
|
||||
|
|
|
@ -15,8 +15,8 @@ typedef struct MOTOR motor_t;
|
|||
|
||||
typedef enum
|
||||
{
|
||||
DIR_CW = 0, /* 逆时针旋转 */
|
||||
DIR_CCW, /* 顺时针旋转 */
|
||||
DIR_CCW = 0, /* 逆时针旋转 */
|
||||
DIR_CW, /* 顺时针旋转 */
|
||||
} dir_e;
|
||||
typedef enum
|
||||
{
|
||||
|
@ -39,19 +39,18 @@ typedef struct
|
|||
void (*stop)(motor_t *motor); // 停止
|
||||
void (*set_angle)(motor_t *motor, float32 angle, dir_e dir); // 将角度转换成脉冲个数并运行
|
||||
void (*stop_cb)(motor_t *motor); // 停止回调函数,在stop中执行
|
||||
void (*set_speed)(motor_t *motor, uint32_t psc);
|
||||
|
||||
} step_motor_interface_t;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
float32 min_step_angle; /* 最小步距角 */
|
||||
int angle; /* 设置需要旋转的角度 */
|
||||
float32 angle; /* 设置需要旋转的角度 */
|
||||
dir_e dir; /* 方向 */
|
||||
uint8_t en; /* 使能 */
|
||||
__IO uint32_t pulse_count; /* 脉冲个数记录 */
|
||||
__IO int add_pulse_count; /* 脉冲个数累计 */
|
||||
__IO uint32_t step_angle; /* 步距 */
|
||||
uint8_t state; // 运动状态
|
||||
} step_motor_attribute_t;
|
||||
|
||||
typedef struct
|
||||
|
|
|
@ -74,8 +74,8 @@ typedef enum
|
|||
// IN6,
|
||||
IN7,
|
||||
IN8,
|
||||
IN9,
|
||||
IN10,
|
||||
// IN9,
|
||||
// IN10,
|
||||
// IN11,
|
||||
// IN12,
|
||||
IN13,
|
||||
|
|
|
@ -24,31 +24,4 @@
|
|||
} while (__LINE__ == -1);
|
||||
|
||||
#define WATCHDOG_RESET() LL_IWDG_ReloadCounter(IWDG)
|
||||
|
||||
/**
|
||||
* @brief Clears the flags of a DMA channel
|
||||
* @param DMAX DMAx register base
|
||||
* @param CHx DMA channel number
|
||||
* @param Flag a boolean variable that indicates if the transfer is complete
|
||||
* @note This function should be called within the interrupt service routine of the DMA channel
|
||||
*/
|
||||
#define DMA_ClEAR_FLAG(DMAX, CHx, Flag) \
|
||||
do \
|
||||
{ \
|
||||
if (LL_DMA_IsActiveFlag_TC##CHx(DMAX)) \
|
||||
{ \
|
||||
LL_DMA_ClearFlag_TC##CHx(DMAX); \
|
||||
LL_DMA_ClearFlag_GI##CHx(DMAX); \
|
||||
Flag = TRUE; \
|
||||
} \
|
||||
if (LL_DMA_IsActiveFlag_TE##CHx(DMAX)) \
|
||||
{ \
|
||||
LL_DMA_ClearFlag_TE##CHx(DMAX); \
|
||||
} \
|
||||
if (LL_DMA_IsActiveFlag_GI##CHx(DMAX)) \
|
||||
{ \
|
||||
LL_DMA_ClearFlag_GI##CHx(DMAX); \
|
||||
} \
|
||||
} while (__LINE__ == -1)
|
||||
|
||||
#endif // __BSP_H__
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
{ \
|
||||
LL_TIM_EnableCounter(TIMx); \
|
||||
LL_TIM_CC_EnableChannel(TIMx, CHx); \
|
||||
LL_TIM_EnableIT_UPDATE(TIMx); \
|
||||
} while (__LINE__ == -1)
|
||||
|
||||
// 停止PWM输出
|
||||
|
@ -17,7 +16,6 @@
|
|||
{ \
|
||||
LL_TIM_DisableCounter(TIMx); \
|
||||
LL_TIM_CC_DisableChannel(TIMx, CHx); \
|
||||
LL_TIM_ClearFlag_UPDATE(TIMx); \
|
||||
} while (__LINE__ == -1)
|
||||
|
||||
// 修改比较值,修改占空比
|
||||
|
|
|
@ -41,79 +41,6 @@
|
|||
} \
|
||||
} while (__LINE__ == -1)
|
||||
|
||||
/**
|
||||
* @file uarts.c
|
||||
* @brief This file contains the implementation of DMA_CLEAR_FLAG_TC_CHANNEL macro.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Clear the Transfer Complete (TC) flag of a specific DMA channel.
|
||||
*
|
||||
* @param dma The DMA peripheral.
|
||||
* @param channel The DMA channel number.
|
||||
*/
|
||||
#define DMA_CLEAR_FLAG_TC_CHANNEL(dma, channel) \
|
||||
switch (channel) \
|
||||
{ \
|
||||
case 1: \
|
||||
DMA_ClEAR_FLAG_TC(dma, 1); \
|
||||
break; \
|
||||
case 2: \
|
||||
DMA_ClEAR_FLAG_TC(dma, 2); \
|
||||
break; \
|
||||
case 3: \
|
||||
DMA_ClEAR_FLAG_TC(dma, 3); \
|
||||
break; \
|
||||
case 4: \
|
||||
DMA_ClEAR_FLAG_TC(dma, 4); \
|
||||
break; \
|
||||
case 5: \
|
||||
DMA_ClEAR_FLAG_TC(dma, 5); \
|
||||
break; \
|
||||
case 6: \
|
||||
DMA_ClEAR_FLAG_TC(dma, 6); \
|
||||
break; \
|
||||
case 7: \
|
||||
DMA_ClEAR_FLAG_TC(dma, 7); \
|
||||
break; \
|
||||
default: \
|
||||
break; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clear the Transfer Error (TE) flag for the specified DMA channel.
|
||||
*
|
||||
* @param dma The DMA peripheral.
|
||||
* @param channel The DMA channel number.
|
||||
*/
|
||||
#define DMA_CLEAR_FLAG_TE_CHANNEL(dma, channel) \
|
||||
switch (channel) \
|
||||
{ \
|
||||
case 1: \
|
||||
DMA_ClEAR_FLAG_TE(dma, 1); \
|
||||
break; \
|
||||
case 2: \
|
||||
DMA_ClEAR_FLAG_TE(dma, 2); \
|
||||
break; \
|
||||
case 3: \
|
||||
DMA_ClEAR_FLAG_TE(dma, 3); \
|
||||
break; \
|
||||
case 4: \
|
||||
DMA_ClEAR_FLAG_TE(dma, 4); \
|
||||
break; \
|
||||
case 5: \
|
||||
DMA_ClEAR_FLAG_TE(dma, 5); \
|
||||
break; \
|
||||
case 6: \
|
||||
DMA_ClEAR_FLAG_TE(dma, 6); \
|
||||
break; \
|
||||
case 7: \
|
||||
DMA_ClEAR_FLAG_TE(dma, 7); \
|
||||
break; \
|
||||
default: \
|
||||
break; \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 创建一个UART设备
|
||||
* @param {USART_TypeDef} *huart USART总线设备句柄
|
||||
|
@ -203,8 +130,6 @@ void uart_recv_en(uart_t *uart)
|
|||
LL_DMA_SetMemoryAddress(uart->dma, uart->dma_tx_channel, (uint32_t)uart->txbuf);
|
||||
LL_DMA_EnableIT_TC(uart->dma, uart->dma_tx_channel);
|
||||
LL_USART_EnableDMAReq_TX(uart->huart);
|
||||
|
||||
uart->tx_dma_ok = TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -244,9 +169,8 @@ void uart_send_data(uart_t *uart, uint8_t *data, uint16_t len)
|
|||
DBG_ASSERT(data != NULL __DBG_LINE);
|
||||
DBG_ASSERT(len > 0 __DBG_LINE);
|
||||
uint8_t count = 0;
|
||||
if (TRUE == uart->tx_dma_en && uart->tx_dma_ok == TRUE)
|
||||
if (TRUE == uart->tx_dma_en)
|
||||
{
|
||||
uart->tx_dma_ok = FALSE;
|
||||
osel_memcpy(uart->txbuf, data, len); // 拷贝数据到发送缓冲区
|
||||
LL_DMA_DisableChannel(uart->dma, uart->dma_tx_channel);
|
||||
// 配置数据长度
|
||||
|
@ -256,7 +180,6 @@ void uart_send_data(uart_t *uart, uint8_t *data, uint16_t len)
|
|||
}
|
||||
else
|
||||
{
|
||||
count = 0;
|
||||
for (uint16_t i = 0; i < len; i++)
|
||||
{
|
||||
count = 0;
|
||||
|
@ -268,7 +191,6 @@ void uart_send_data(uart_t *uart, uint8_t *data, uint16_t len)
|
|||
}
|
||||
}
|
||||
LL_USART_TransmitData8(uart->huart, data[i]);
|
||||
}
|
||||
count = 0;
|
||||
while (!LL_USART_IsActiveFlag_TC(uart->huart))
|
||||
{
|
||||
|
@ -279,6 +201,7 @@ void uart_send_data(uart_t *uart, uint8_t *data, uint16_t len)
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief 接收中断回调函数
|
||||
|
@ -365,12 +288,13 @@ void uart_dma_reception_callback(uart_t *uart)
|
|||
LL_DMA_DisableChannel(uart->dma, uart->dma_tx_channel);
|
||||
|
||||
// 清除发送中断标志位
|
||||
DMA_CLEAR_FLAG_TC_CHANNEL(uart->dma, uart->dma_tx_channel);
|
||||
DMA_ClEAR_FLAG_TC(uart->dma, 2);
|
||||
DMA_ClEAR_FLAG_TC(uart->dma, 7);
|
||||
|
||||
// 使能发送中断,用于关闭发送使能引脚
|
||||
LL_USART_EnableIT_TC(uart->huart); // 使能发送中断,用于关闭发送使能引脚
|
||||
uart->tx_dma_ok = TRUE;
|
||||
|
||||
// 清除传输错误标志
|
||||
DMA_CLEAR_FLAG_TE_CHANNEL(uart->dma, uart->dma_tx_channel);
|
||||
// 清除接收中断标志位
|
||||
DMA_ClEAR_FLAG_TE(uart->dma, 2);
|
||||
DMA_ClEAR_FLAG_TE(uart->dma, 7);
|
||||
}
|
||||
|
|
|
@ -1,132 +1,47 @@
|
|||
/**
|
||||
* @file uarts.h
|
||||
* @brief Header file for UARTs module.
|
||||
*
|
||||
* This file contains the definitions and function prototypes for UARTs module.
|
||||
* The UARTs module provides functions for creating and managing UART instances,
|
||||
* enabling reception, sending data, and handling interrupts.
|
||||
*/
|
||||
|
||||
#ifndef __UARTS_H__
|
||||
#define __UARTS_H__
|
||||
|
||||
#include "lib.h"
|
||||
#include "main.h"
|
||||
|
||||
/**
|
||||
* @brief Callback function type for UART receive interrupt.
|
||||
*
|
||||
* This function type is used to define the callback function for UART receive interrupt.
|
||||
* The callback function is called when data is received on the UART.
|
||||
*
|
||||
* @param uart_index The index of the UART.
|
||||
* @param data The received data.
|
||||
* @param len The length of the received data.
|
||||
*/
|
||||
//
|
||||
typedef void (*rx_interupt_cb_t)(uint8_t uart_index, uint8_t *data, uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief Callback function type for UART transmit complete.
|
||||
*
|
||||
* This function type is used to define the callback function for UART transmit complete.
|
||||
* The callback function is called when the UART transmission is complete.
|
||||
*/
|
||||
//
|
||||
typedef void (*tx_complete_cb_t)(void);
|
||||
|
||||
/**
|
||||
* @brief Enumeration for UART status.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
UART_OK = 0x00u, /**< The action was successful. */
|
||||
UART_ERROR = 0xFFu /**< Generic error. */
|
||||
} uart_status_e;
|
||||
|
||||
/**
|
||||
* @brief Structure representing a UART instance.
|
||||
*/
|
||||
//
|
||||
typedef struct
|
||||
{
|
||||
uint8_t uart_index; /**< The index of the UART. */
|
||||
USART_TypeDef *huart; /**< The UART peripheral. */
|
||||
DMA_TypeDef *dma; /**< The DMA peripheral. */
|
||||
uint32_t dma_rx_channel; /**< The DMA receive channel. */
|
||||
uint32_t dma_tx_channel; /**< The DMA transmit channel. */
|
||||
uint16_t rx_index; /**< The receive data index. */
|
||||
BOOL rx_dma_en; /**< Flag indicating if DMA reception is enabled. */
|
||||
uint8_t *rxbuf; /**< The receive buffer. */
|
||||
uint16_t rxsize; /**< The size of the receive buffer. */
|
||||
uint16_t tx_index; /**< The transmit data index. */
|
||||
BOOL tx_dma_en; /**< Flag indicating if DMA transmission is enabled. */
|
||||
uint8_t *txbuf; /**< The transmit buffer. */
|
||||
uint16_t txsize; /**< The size of the transmit buffer. */
|
||||
__IO BOOL tx_dma_ok; /**< Flag indicating if DMA transmission is complete. */
|
||||
rx_interupt_cb_t rx_interupt_cb; /**< The receive interrupt callback function. */
|
||||
tx_complete_cb_t tx_complete_cb; /**< The transmit complete callback function. */
|
||||
uint8_t uart_index; // 串口索引
|
||||
USART_TypeDef *huart;
|
||||
DMA_TypeDef *dma; // 外部设置
|
||||
uint32_t dma_rx_channel; // 外部设置
|
||||
uint32_t dma_tx_channel; // 外部设置
|
||||
|
||||
uint16_t rx_index; // 接收数据索引
|
||||
BOOL rx_dma_en;
|
||||
uint8_t *rxbuf;
|
||||
uint16_t rx_sta;
|
||||
uint16_t rxsize;
|
||||
|
||||
uint16_t tx_index;
|
||||
BOOL tx_dma_en;
|
||||
uint8_t *txbuf;
|
||||
uint16_t txsize;
|
||||
|
||||
// 接收中断回调
|
||||
rx_interupt_cb_t rx_interupt_cb;
|
||||
// 发生完成回调
|
||||
tx_complete_cb_t tx_complete_cb;
|
||||
|
||||
} uart_t;
|
||||
|
||||
/**
|
||||
* @brief Creates a UART instance.
|
||||
*
|
||||
* This function creates a UART instance with the specified parameters.
|
||||
*
|
||||
* @param huart The UART peripheral.
|
||||
* @param rx_dma_en Flag indicating if DMA reception is enabled.
|
||||
* @param rxsize The size of the receive buffer.
|
||||
* @param rx_cb The receive interrupt callback function.
|
||||
* @param tx_dma_en Flag indicating if DMA transmission is enabled.
|
||||
* @param txsize The size of the transmit buffer.
|
||||
* @param tx_complete_cb The transmit complete callback function.
|
||||
* @return The created UART instance.
|
||||
*/
|
||||
extern uart_t *uart_create(USART_TypeDef *huart, BOOL rx_dma_en, uint16_t rxsize, rx_interupt_cb_t rx_cb,
|
||||
BOOL tx_dma_en, uint16_t txsize, tx_complete_cb_t tx_complete_cb);
|
||||
|
||||
/**
|
||||
* @brief Frees the resources of a UART instance.
|
||||
*
|
||||
* This function frees the resources allocated for a UART instance.
|
||||
*
|
||||
* @param uart The UART instance to free.
|
||||
*/
|
||||
extern void uart_free(uart_t *uart);
|
||||
|
||||
/**
|
||||
* @brief Enables UART reception.
|
||||
*
|
||||
* This function enables reception on the specified UART instance.
|
||||
*
|
||||
* @param uart The UART instance.
|
||||
*/
|
||||
extern void uart_recv_en(uart_t *uart);
|
||||
|
||||
/**
|
||||
* @brief Sends data over UART.
|
||||
*
|
||||
* This function sends the specified data over the specified UART instance.
|
||||
*
|
||||
* @param uart The UART instance.
|
||||
* @param data The data to send.
|
||||
* @param len The length of the data.
|
||||
*/
|
||||
extern void uart_send_data(uart_t *uart, uint8_t *data, uint16_t len);
|
||||
|
||||
/**
|
||||
* @brief UART receive interrupt callback.
|
||||
*
|
||||
* This function is the interrupt callback for UART receive interrupt.
|
||||
*
|
||||
* @param uart The UART instance.
|
||||
*/
|
||||
extern void uart_reception_callback(uart_t *uart);
|
||||
|
||||
/**
|
||||
* @brief DMA receive interrupt callback.
|
||||
*
|
||||
* This function is the interrupt callback for DMA receive interrupt.
|
||||
*
|
||||
* @param uart The UART instance.
|
||||
*/
|
||||
extern void uart_dma_reception_callback(uart_t *uart);
|
||||
BOOL tx_dma_en, uint16_t txsize, tx_complete_cb_t tx_complete_cb); // 创建uart
|
||||
extern void uart_free(uart_t *uart); // 释放uart资源
|
||||
extern void uart_recv_en(uart_t *uart); // 使能接收
|
||||
extern void uart_send_data(uart_t *uart, uint8_t *data, uint16_t len); // 发送数据
|
||||
extern void uart_reception_callback(uart_t *uart); // 接收中断回调
|
||||
extern void uart_dma_reception_callback(uart_t *uart); // DMA接收中断回调
|
||||
|
||||
#endif // __UARTS_H__
|
||||
|
|
34
motor.ioc
34
motor.ioc
|
@ -57,22 +57,20 @@ Mcu.Name=STM32L072R(B-Z)Tx
|
|||
Mcu.Package=LQFP64
|
||||
Mcu.Pin0=PH0-OSC_IN
|
||||
Mcu.Pin1=PH1-OSC_OUT
|
||||
Mcu.Pin10=PB15
|
||||
Mcu.Pin11=PA9
|
||||
Mcu.Pin12=PA10
|
||||
Mcu.Pin13=PA13
|
||||
Mcu.Pin14=PA14
|
||||
Mcu.Pin15=VP_SYS_VS_Systick
|
||||
Mcu.Pin16=VP_TIM6_VS_ClockSourceINT
|
||||
Mcu.Pin2=PC0
|
||||
Mcu.Pin3=PC3
|
||||
Mcu.Pin4=PA7
|
||||
Mcu.Pin5=PB0
|
||||
Mcu.Pin6=PB1
|
||||
Mcu.Pin7=PB12
|
||||
Mcu.Pin8=PB13
|
||||
Mcu.Pin9=PB14
|
||||
Mcu.PinsNb=17
|
||||
Mcu.Pin10=PA10
|
||||
Mcu.Pin11=PA13
|
||||
Mcu.Pin12=PA14
|
||||
Mcu.Pin13=VP_SYS_VS_Systick
|
||||
Mcu.Pin14=VP_TIM6_VS_ClockSourceINT
|
||||
Mcu.Pin2=PC3
|
||||
Mcu.Pin3=PA7
|
||||
Mcu.Pin4=PB0
|
||||
Mcu.Pin5=PB12
|
||||
Mcu.Pin6=PB13
|
||||
Mcu.Pin7=PB14
|
||||
Mcu.Pin8=PB15
|
||||
Mcu.Pin9=PA9
|
||||
Mcu.PinsNb=15
|
||||
Mcu.ThirdPartyNb=0
|
||||
Mcu.UserConstants=
|
||||
Mcu.UserName=STM32L072RBTx
|
||||
|
@ -101,8 +99,6 @@ PA9.Mode=Asynchronous
|
|||
PA9.Signal=USART1_TX
|
||||
PB0.Mode=IN8
|
||||
PB0.Signal=ADC_IN8
|
||||
PB1.Mode=IN9
|
||||
PB1.Signal=ADC_IN9
|
||||
PB12.GPIOParameters=GPIO_Speed,PinState,GPIO_Label
|
||||
PB12.GPIO_Label=ENA
|
||||
PB12.GPIO_Speed=GPIO_SPEED_FREQ_HIGH
|
||||
|
@ -120,8 +116,6 @@ PB15.GPIOParameters=GPIO_PuPd,GPIO_Label
|
|||
PB15.GPIO_Label=STOPPER
|
||||
PB15.GPIO_PuPd=GPIO_PULLUP
|
||||
PB15.Signal=GPXTI15
|
||||
PC0.Mode=IN10
|
||||
PC0.Signal=ADC_IN10
|
||||
PC3.GPIOParameters=GPIO_PuPd,GPIO_Label,GPIO_Mode
|
||||
PC3.GPIO_Label=MG_ADC_IN13
|
||||
PC3.GPIO_Mode=GPIO_MODE_ANALOG
|
||||
|
|
Reference in New Issue