diff --git a/.cproject b/.cproject
index 6939cb5..6c23bdc 100644
--- a/.cproject
+++ b/.cproject
@@ -43,7 +43,6 @@
@@ -57,6 +56,7 @@
+
diff --git a/Core/Src/main.c b/Core/Src/main.c
index 5f3932c..372a3fd 100644
--- a/Core/Src/main.c
+++ b/Core/Src/main.c
@@ -216,7 +216,7 @@ static void MX_TIM7_Init(void)
htim7.Instance = TIM7;
htim7.Init.Prescaler = 71;
htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim7.Init.Period = 99;
+ htim7.Init.Period = 65534;
htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
{
diff --git a/bak/pluse_generator.c b/bak/pluse_generator.c
new file mode 100644
index 0000000..4f0b3f6
--- /dev/null
+++ b/bak/pluse_generator.c
@@ -0,0 +1,101 @@
+#include "pluse_generator.h"
+
+#include "config_service.h"
+
+extern TIM_HandleTypeDef PLUSE_CTRL_TIMER;
+
+static zgpio_t trigger_io;
+static zgpio_t ch_io[4];
+static zgpio_t chx_io;
+static int32_t m_cnt;
+
+static uint32_t m_pluse_width_cnt;
+static uint32_t m_pluse_interval_cnt;
+
+static uint32_t m_pluse_num;
+static uint32_t m_pluse_state; // 0:低,1:高
+
+static inline void set_io_state(int off, bool state);
+
+/*******************************************************************************
+ * 中断 *
+ *******************************************************************************/
+void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
+ if (GPIO_Pin == trigger_io.pinoff) {
+ // 启动定时器,同时触发第一个波形
+
+ // 这里之所以设置成-1,是因为不知道啥原因,第一个脉冲会少一个定时器周期
+ m_cnt = -1;
+
+ m_pluse_state = 1;
+ m_pluse_num = 0;
+ __HAL_TIM_SET_COUNTER(&PLUSE_CTRL_TIMER, 1);
+ HAL_TIM_Base_Start_IT(&PLUSE_CTRL_TIMER);
+ set_io_state(0, true);
+ }
+}
+
+static inline void pluse_wave_schedule() {
+ if (m_pluse_state == 0) {
+ if (m_cnt >= m_pluse_interval_cnt) {
+ m_pluse_state = 1;
+ m_pluse_num++;
+ m_cnt = 0;
+ set_io_state(m_pluse_num, 1);
+ }
+ }
+
+ else if (m_pluse_state == 1) {
+ if (m_cnt >= m_pluse_width_cnt) {
+ m_pluse_state = 0;
+ m_cnt = 0;
+ set_io_state(m_pluse_num, 0);
+ if (m_pluse_num == 3) {
+ HAL_TIM_Base_Stop(&PLUSE_CTRL_TIMER);
+ }
+
+ if (m_pluse_interval_cnt == 0) {
+ m_pluse_state = 1;
+ m_pluse_num++;
+ m_cnt = 0;
+ set_io_state(m_pluse_num, 1);
+ }
+ }
+ }
+}
+
+void tim7_irq_trigger() {
+ m_cnt++;
+ pluse_wave_schedule();
+}
+
+/*******************************************************************************
+ * FUNCTION *
+ *******************************************************************************/
+static inline void set_io_state(int off, bool state) {
+ zgpio_write(&ch_io[off], state);
+ zgpio_write(&chx_io, state);
+}
+
+void PluseGenerator_init() {
+ //
+
+ ZASSERT(&PLUSE_CTRL_TIMER == &htim7);
+ ZASSERT(PLUSE_CTRL_TIMER.Init.AutoReloadPreload = 100);
+ ZASSERT(PLUSE_CTRL_TIMER.Init.Prescaler = 71);
+ // PLUSE_CTRL_TIMER.Init.
+
+ PluseGenerator_updatePraram();
+
+ zgpio_init_as_output(&ch_io[0], PLUSE_OUTPUT_CH0, kxs_gpio_nopull, false, false);
+ zgpio_init_as_output(&ch_io[1], PLUSE_OUTPUT_CH1, kxs_gpio_nopull, false, false);
+ zgpio_init_as_output(&ch_io[2], PLUSE_OUTPUT_CH2, kxs_gpio_nopull, false, false);
+ zgpio_init_as_output(&ch_io[3], PLUSE_OUTPUT_CH3, kxs_gpio_nopull, false, false);
+ zgpio_init_as_output(&chx_io, PLUSE_OUTPUT_CHX, kxs_gpio_nopull, false, false);
+ zgpio_init_as_input(&trigger_io, PLUSE_TRIGGER, kxs_gpio_pulldown, kxs_gpio_rising_irq, false);
+}
+
+void PluseGenerator_updatePraram() {
+ m_pluse_width_cnt = config_get()->pulse_width_us * 10;
+ m_pluse_interval_cnt = config_get()->pulse_interval_us * 10;
+}
\ No newline at end of file
diff --git a/bak/pluse_generator.h b/bak/pluse_generator.h
new file mode 100644
index 0000000..76edfbe
--- /dev/null
+++ b/bak/pluse_generator.h
@@ -0,0 +1,13 @@
+#pragma once
+#include
+
+#include "zlib.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+void PluseGenerator_init();
+void PluseGenerator_updatePraram();
+#ifdef __cplusplus
+}
+#endif
\ No newline at end of file
diff --git a/light_src_ctrl_stm32_port.ioc b/light_src_ctrl_stm32_port.ioc
index b833dbc..52622a8 100644
--- a/light_src_ctrl_stm32_port.ioc
+++ b/light_src_ctrl_stm32_port.ioc
@@ -167,7 +167,7 @@ SH.S_TIM8_CH4.0=TIM8_CH4,PWM Generation4 CH4
SH.S_TIM8_CH4.ConfNb=1
TIM7.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
TIM7.IPParameters=AutoReloadPreload,Prescaler,Period
-TIM7.Period=99
+TIM7.Period=65534
TIM7.Prescaler=71
TIM8.AutoReloadPreload=TIM_AUTORELOAD_PRELOAD_ENABLE
TIM8.Channel-PWM\ Generation1\ CH1=TIM_CHANNEL_1
diff --git a/usrc/config_service.c b/usrc/config_service.c
index 372210b..ad444aa 100644
--- a/usrc/config_service.c
+++ b/usrc/config_service.c
@@ -7,8 +7,8 @@ static config_t _default_val_config;
static void create_default_config(config_t *default_cfg) { //
default_cfg->light_intensity = 50;
- default_cfg->pulse_width_ms = 25;
- default_cfg->pulse_interval_ms = 1;
+ default_cfg->pulse_width_us = 25;
+ default_cfg->pulse_interval_us = 2;
default_cfg->cmd_echo = 1;
}
diff --git a/usrc/config_service.h b/usrc/config_service.h
index 3050e3d..2bc7d23 100644
--- a/usrc/config_service.h
+++ b/usrc/config_service.h
@@ -9,8 +9,8 @@ extern "C" {
typedef struct {
uint32_t config_mark;
uint32_t light_intensity; // 光强 0->100
- uint32_t pulse_width_ms; // 脉冲宽度
- uint32_t pulse_interval_ms; // 脉冲间隔
+ uint32_t pulse_width_us; // 脉冲宽度
+ uint32_t pulse_interval_us; // 脉冲间隔
uint32_t cmd_echo; // 命令回显
uint32_t checksum; //
} config_t;
diff --git a/usrc/pluse_generator.c b/usrc/pluse_generator.c
index 54f49aa..19675a7 100644
--- a/usrc/pluse_generator.c
+++ b/usrc/pluse_generator.c
@@ -4,69 +4,102 @@
extern TIM_HandleTypeDef PLUSE_CTRL_TIMER;
+#define ZARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
+
static zgpio_t trigger_io;
static zgpio_t ch_io[4];
static zgpio_t chx_io;
-static int32_t m_cnt;
-
-static uint32_t m_pluse_width_cnt;
-static uint32_t m_pluse_interval_cnt;
-
-static uint32_t m_pluse_num;
-static uint32_t m_pluse_state; // 0:低,1:高
static inline void set_io_state(int off, bool state);
+void tim7_irq_trigger() {}
+
+typedef enum {
+ kaction_set_io0_high = 0,
+ kaction_set_io0_low,
+ kaction_set_io1_high,
+ kaction_set_io1_low,
+ kaction_set_io2_high,
+ kaction_set_io2_low,
+ kaction_set_io3_high,
+ kaction_set_io3_low,
+} step_action_t;
+
+typedef struct {
+ step_action_t action;
+ int32_t tp;
+} step_t;
+
+step_t m_steps[] = { //
+ {kaction_set_io0_high, 0}, //
+ {kaction_set_io0_low, 0}, //
+ {kaction_set_io1_high, 0}, //
+ {kaction_set_io1_low, 0}, //
+ {kaction_set_io2_high, 0}, //
+ {kaction_set_io2_low, 0}, //
+ {kaction_set_io3_high, 0}, //
+ {kaction_set_io3_low, 0}};
+
/*******************************************************************************
* 中断 *
*******************************************************************************/
-void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
- if (GPIO_Pin == trigger_io.pinoff) {
- // 启动定时器,同时触发第一个波形
-
- // 这里之所以设置成-1,是因为不知道啥原因,第一个脉冲会少一个定时器周期
- m_cnt = -1;
- m_pluse_state = 1;
- m_pluse_num = 0;
- __HAL_TIM_SET_COUNTER(&PLUSE_CTRL_TIMER, 1);
- HAL_TIM_Base_Start_IT(&PLUSE_CTRL_TIMER);
- set_io_state(0, true);
+void prv_delay_us(uint32_t us) {
+ for (uint32_t j = 0; j < us; j++) {
+ for (uint32_t i = 0; i < 350; i++) {
+ __NOP();
+ }
}
}
-static inline void pluse_wave_schedule() {
- if (m_pluse_state == 0) {
- if (m_cnt >= m_pluse_interval_cnt) {
- m_pluse_state = 1;
- m_pluse_num++;
- m_cnt = 0;
- set_io_state(m_pluse_num, 1);
- }
+void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
+ if (GPIO_Pin != trigger_io.pinoff) {
+ return;
}
+ // 启动定时器,同时触发第一个波形
+
+ // 这里之所以设置成-1,是因为不知道啥原因,第一个脉冲会少一个定时器周期
+ __HAL_TIM_SET_COUNTER(&PLUSE_CTRL_TIMER, 0);
+ HAL_TIM_Base_Start(&PLUSE_CTRL_TIMER);
+
+ // set_io_state(0, true);
+ uint32_t counter = 0;
+ for (uint32_t i = 0; i < ZARRAY_SIZE(m_steps); i++) {
+ while ((int32_t)counter < m_steps[i].tp) {
+ counter = __HAL_TIM_GET_COUNTER(&PLUSE_CTRL_TIMER);
+ }
- else if (m_pluse_state == 1) {
- if (m_cnt >= m_pluse_width_cnt) {
- m_pluse_state = 0;
- m_cnt = 0;
- set_io_state(m_pluse_num, 0);
- if (m_pluse_num == 3) {
- HAL_TIM_Base_Stop(&PLUSE_CTRL_TIMER);
- }
-
- if (m_pluse_interval_cnt == 0) {
- m_pluse_state = 1;
- m_pluse_num++;
- m_cnt = 0;
- set_io_state(m_pluse_num, 1);
- }
+ switch (m_steps[i].action) {
+ case kaction_set_io0_high:
+ set_io_state(0, true);
+ break;
+ case kaction_set_io0_low:
+ set_io_state(0, false);
+ break;
+ case kaction_set_io1_high:
+ set_io_state(1, true);
+ break;
+ case kaction_set_io1_low:
+ set_io_state(1, false);
+ break;
+ case kaction_set_io2_high:
+ set_io_state(2, true);
+ break;
+ case kaction_set_io2_low:
+ set_io_state(2, false);
+ break;
+ case kaction_set_io3_high:
+ set_io_state(3, true);
+ break;
+ case kaction_set_io3_low:
+ set_io_state(3, false);
+ break;
+ default:
+ break;
}
}
-}
-void tim7_irq_trigger() {
- m_cnt++;
- pluse_wave_schedule();
+ HAL_TIM_Base_Stop(&PLUSE_CTRL_TIMER);
}
/*******************************************************************************
@@ -74,6 +107,7 @@ void tim7_irq_trigger() {
*******************************************************************************/
static inline void set_io_state(int off, bool state) {
zgpio_write(&ch_io[off], state);
+ // zdelay_us(1);
zgpio_write(&chx_io, state);
}
@@ -81,7 +115,6 @@ void PluseGenerator_init() {
//
ZASSERT(&PLUSE_CTRL_TIMER == &htim7);
- ZASSERT(PLUSE_CTRL_TIMER.Init.AutoReloadPreload = 100);
ZASSERT(PLUSE_CTRL_TIMER.Init.Prescaler = 71);
// PLUSE_CTRL_TIMER.Init.
@@ -96,6 +129,18 @@ void PluseGenerator_init() {
}
void PluseGenerator_updatePraram() {
- m_pluse_width_cnt = config_get()->pulse_width_ms * 10;
- m_pluse_interval_cnt = config_get()->pulse_interval_ms * 10;
-}
\ No newline at end of file
+ config_t* cfg = config_get();
+
+ m_steps[kaction_set_io0_high].tp = 1 + (cfg->pulse_width_us + cfg->pulse_interval_us) * 0;
+ m_steps[kaction_set_io0_low].tp = 1 + (cfg->pulse_width_us + cfg->pulse_interval_us) * 0 + cfg->pulse_width_us;
+ m_steps[kaction_set_io1_high].tp = 1 + (cfg->pulse_width_us + cfg->pulse_interval_us) * 1;
+ m_steps[kaction_set_io1_low].tp = 1 + (cfg->pulse_width_us + cfg->pulse_interval_us) * 1 + cfg->pulse_width_us;
+ m_steps[kaction_set_io2_high].tp = 1 + (cfg->pulse_width_us + cfg->pulse_interval_us) * 2;
+ m_steps[kaction_set_io2_low].tp = 1 + (cfg->pulse_width_us + cfg->pulse_interval_us) * 2 + cfg->pulse_width_us;
+ m_steps[kaction_set_io3_high].tp = 1 + (cfg->pulse_width_us + cfg->pulse_interval_us) * 3;
+ m_steps[kaction_set_io3_low].tp = 1 + (cfg->pulse_width_us + cfg->pulse_interval_us) * 3 + cfg->pulse_width_us;
+
+ for (uint32_t i = 0; i < ZARRAY_SIZE(m_steps); i++) {
+ printf("m_steps[%d].tp = %d\n", i, m_steps[i].tp);
+ }
+}
diff --git a/usrc/umain.c b/usrc/umain.c
index 435aab1..47a23dd 100644
--- a/usrc/umain.c
+++ b/usrc/umain.c
@@ -75,20 +75,20 @@ void prv_process_uart_rx_data() {
cfg->light_intensity = val;
LightIntensityCtrl_updatePraram();
printf("OK\n");
- } else if (strcmp(param, "pulse_width_ms") == 0) {
+ } else if (strcmp(param, "pulse_width_us") == 0) {
if (val <= 0 || val > 100) {
- printf("FAIL,pulse_width_ms should be in range 1-100\n");
+ printf("FAIL,pulse_width_us should be in range 1-100\n");
return;
}
- cfg->pulse_width_ms = val;
+ cfg->pulse_width_us = val;
PluseGenerator_updatePraram();
printf("OK\n");
- } else if (strcmp(param, "pulse_interval_ms") == 0) {
+ } else if (strcmp(param, "pulse_interval_us") == 0) {
if (val > 100) {
- printf("FAIL,pulse_interval_ms should be in range 0-100\n");
+ printf("FAIL,pulse_interval_us should be in range 0-100\n");
return;
}
- cfg->pulse_interval_ms = val;
+ cfg->pulse_interval_us = val;
PluseGenerator_updatePraram();
printf("OK\n");
} else if (strcmp(param, "cmd_echo") == 0) {
@@ -107,11 +107,11 @@ void prv_process_uart_rx_data() {
const char *param = argc[1];
if (strcmp(param, "light_intensity") == 0) {
printf("OK,%ld\n", cfg->light_intensity);
- } else if (strcmp(param, "pulse_width_ms") == 0) {
- printf("OK,%ld\n", cfg->pulse_width_ms);
+ } else if (strcmp(param, "pulse_width_us") == 0) {
+ printf("OK,%ld\n", cfg->pulse_width_us);
PluseGenerator_updatePraram();
- } else if (strcmp(param, "pulse_interval_ms") == 0) {
- printf("OK,%ld\n", cfg->pulse_interval_ms);
+ } else if (strcmp(param, "pulse_interval_us") == 0) {
+ printf("OK,%ld\n", cfg->pulse_interval_us);
PluseGenerator_updatePraram();
} else if (strcmp(param, "cmd_echo") == 0) {
printf("OK,%ld\n", cfg->cmd_echo);
@@ -136,8 +136,8 @@ void prv_process_uart_rx_data() {
config_t *cfg = config_get();
printf("------ param list ------\n");
printf(" light_intensity : %ld\n", cfg->light_intensity);
- printf(" pulse_width_ms : %ld\n", cfg->pulse_width_ms);
- printf(" pulse_interval_ms : %ld\n", cfg->pulse_interval_ms);
+ printf(" pulse_width_us : %ld\n", cfg->pulse_width_us);
+ printf(" pulse_interval_us : %ld\n", cfg->pulse_interval_us);
printf(" cmd_echo : %ld\n", cfg->cmd_echo);
}