Browse Source

V2.0

ctrl_sig_generator_by_stm32
zhaohe 1 year ago
parent
commit
065e357ceb
  1. 2
      .cproject
  2. 2
      Core/Src/main.c
  3. 101
      bak/pluse_generator.c
  4. 13
      bak/pluse_generator.h
  5. 2
      light_src_ctrl_stm32_port.ioc
  6. 4
      usrc/config_service.c
  7. 4
      usrc/config_service.h
  8. 143
      usrc/pluse_generator.c
  9. 24
      usrc/umain.c

2
.cproject

@ -43,7 +43,6 @@
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel.4539504" name="Debug level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel" useByScannerDiscovery="false" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.debuglevel.value.g0" valueType="enumerated"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.1999440254" name="Optimization level" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level" useByScannerDiscovery="false" value="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.optimization.level.value.ofast" valueType="enumerated"/>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.definedsymbols.1824133732" name="Define symbols (-D)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.definedsymbols" useByScannerDiscovery="false" valueType="definedSymbols">
<listOptionValue builtIn="false" value="DEBUG"/>
<listOptionValue builtIn="false" value="USE_HAL_DRIVER"/>
<listOptionValue builtIn="false" value="STM32F103xE"/>
</option>
@ -57,6 +56,7 @@
<listOptionValue builtIn="false" value="../Drivers/CMSIS/Include"/>
</option>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.warnings.extra.35757150" name="Enable extra warning flags (-Wextra)" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.warnings.extra" useByScannerDiscovery="false" value="true" valueType="boolean"/>
<option id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.slow_flash_data.1505987394" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.option.slow_flash_data" value="false" valueType="boolean"/>
<inputType id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c.1688112573" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.c.compiler.input.c"/>
</tool>
<tool id="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler.333702566" name="MCU G++ Compiler" superClass="com.st.stm32cube.ide.mcu.gnu.managedbuild.tool.cpp.compiler">

2
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)
{

101
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;
}

13
bak/pluse_generator.h

@ -0,0 +1,13 @@
#pragma once
#include <stdint.h>
#include "zlib.h"
#ifdef __cplusplus
extern "C" {
#endif
void PluseGenerator_init();
void PluseGenerator_updatePraram();
#ifdef __cplusplus
}
#endif

2
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

4
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;
}

4
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;

143
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;
}
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);
}
}

24
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);
}

Loading…
Cancel
Save