diff --git a/.gitignore b/.gitignore index c2f4ce9..1205a8e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,4 @@ # dot files -/.vscode/launch.json /.settings /.eide/log /.eide.usr.ctx.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..984a112 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,19 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "cwd": "${workspaceRoot}", + "type": "cortex-debug", + "request": "launch", + "name": "openocd", + "servertype": "openocd", + "executable": "build\\BC3D\\BC3D.elf", + "runToEntryPoint": "main", + "configFiles": [ + "interface/cmsis-dap.cfg", + "target/at32f413xx.cfg" + ], + "toolchainPrefix": "arm-none-eabi", + } + ] +} \ No newline at end of file diff --git a/BC3D.ATWP b/BC3D.ATWP index 49e8470..3d53aab 100644 --- a/BC3D.ATWP +++ b/BC3D.ATWP @@ -18,6 +18,14 @@ + + + + + + + + @@ -39,14 +47,6 @@ - - - - - - - - @@ -75,12 +75,6 @@ 0;0;0 - - - - - - @@ -90,22 +84,22 @@ - - + + - + 0 - 8.000000 - 0 - 1 - 50 + 16.000000 + 1 + 0 + 25 1 1 2 @@ -118,6 +112,8 @@ 4 + + @@ -128,9 +124,9 @@ - + - + @@ -143,7 +139,7 @@ true 0x200 0x400 - true + false V2.1.3 diff --git a/app/by_stepper.c b/app/by_stepper.c new file mode 100644 index 0000000..7244fa9 --- /dev/null +++ b/app/by_stepper.c @@ -0,0 +1,41 @@ +#include "by_stepper.h" + +#include +#include +#include "by_debug.h" + +// TODO 是否增加一个动作列表?一般都比较简单吧 + +struct by_stepper_t { + uint8_t dir; + stepper_speed_t speed; +}; + +struct by_stepper_t by_stepper; + +void by_stepper_init(void) +{ + memset(&by_stepper, 0x00, sizeof(by_stepper)); +} + +void by_stepper_set_dir(uint8_t dir) +{ + by_stepper.dir = dir; +} + +void by_stepper_set_speed(stepper_speed_t speed) +{ + by_stepper.speed = speed; +} + +void by_stepper_run(void) +{ + // 根据预设条件发送脉冲 + gpio_bits_write(GPIOA, GPIO_PINS_8, by_stepper.dir ? TRUE : FALSE); // DIR + gpio_bits_write(GPIOB, GPIO_PINS_15, !gpio_output_data_bit_read(GPIOB, GPIO_PINS_15)); // CLK + DWT_Delay(40U * (uint16_t)by_stepper.speed); +} + +void by_stepper_stop(void) +{ +} \ No newline at end of file diff --git a/app/by_stepper.h b/app/by_stepper.h new file mode 100644 index 0000000..8b20976 --- /dev/null +++ b/app/by_stepper.h @@ -0,0 +1,10 @@ +#include "at32f413.h" + +typedef enum stepper_speed_t { + STEPPER_SPEED_DIV0 = 0, + STEPPER_SPEED_DIV2 = 2, + STEPPER_SPEED_DIV4 = 4, + STEPPER_SPEED_DIV8 = 8, + STEPPER_SPEED_DIV16 = 16, + STEPPER_SPEED_DIV32 = 32 +} stepper_speed_t; \ No newline at end of file diff --git a/project/inc/at32f413_conf.h b/project/inc/at32f413_conf.h index 516b139..2199495 100644 --- a/project/inc/at32f413_conf.h +++ b/project/inc/at32f413_conf.h @@ -39,7 +39,7 @@ extern "C" { * */ #if !defined HEXT_VALUE -#define HEXT_VALUE ((uint32_t)8000000) /*!< value of the high speed exernal crystal in hz */ +#define HEXT_VALUE ((uint32_t)16000000) /*!< value of the high speed exernal crystal in hz */ #endif /** diff --git a/project/inc/at32f413_wk_config.h b/project/inc/at32f413_wk_config.h index d5a272d..f35a28d 100644 --- a/project/inc/at32f413_wk_config.h +++ b/project/inc/at32f413_wk_config.h @@ -56,18 +56,14 @@ extern "C" { /* add user code end exported macro */ /* Private defines -------------------------------------------------------------*/ -#define SW_IN2_PIN GPIO_PINS_10 -#define SW_IN2_GPIO_PORT GPIOB -#define SW_IN1_PIN GPIO_PINS_11 -#define SW_IN1_GPIO_PORT GPIOB #define MODE2_PIN GPIO_PINS_12 #define MODE2_GPIO_PORT GPIOB #define MODE1_PIN GPIO_PINS_13 #define MODE1_GPIO_PORT GPIOB #define MODE0_PIN GPIO_PINS_14 #define MODE0_GPIO_PORT GPIOB -#define STEP_PIN GPIO_PINS_15 -#define STEP_GPIO_PORT GPIOB +#define CLK_PIN GPIO_PINS_15 +#define CLK_GPIO_PORT GPIOB #define DIR_PIN GPIO_PINS_8 #define DIR_GPIO_PORT GPIOA #define EN_PIN GPIO_PINS_9 @@ -104,9 +100,6 @@ extern "C" { /* init tmr3 function. */ void wk_tmr3_init(void); - /* init tmr9 function. */ - void wk_tmr9_init(void); - /* init tmr10 function. */ void wk_tmr10_init(void); diff --git a/project/src/at32f413_wk_config.c b/project/src/at32f413_wk_config.c index a8c6efe..a9f8e48 100644 --- a/project/src/at32f413_wk_config.c +++ b/project/src/at32f413_wk_config.c @@ -64,8 +64,9 @@ /** * @brief system clock config program * @note the system clock is configured as follow: - * system clock (sclk) = hick / 12 * pll_mult - * system clock source = HICK_VALUE + * system clock (sclk) = hext / 2 * pll_mult + * system clock source = HEXT_VALUE + * - hext = HEXT_VALUE * - sclk = 200000000 * - ahbdiv = 1 * - ahbclk = 200000000 @@ -73,7 +74,7 @@ * - apb1clk = 100000000 * - apb2div = 2 * - apb2clk = 100000000 - * - pll_mult = 50 + * - pll_mult = 25 * - pll_range = GT72MHZ (greater than 72 mhz) * @param none * @retval none @@ -91,6 +92,14 @@ void wk_system_clock_config(void) { } + /* enable hext */ + crm_clock_source_enable(CRM_CLOCK_SOURCE_HEXT, TRUE); + + /* wait till hext is ready */ + while(crm_hext_stable_wait() == ERROR) + { + } + /* enable hick */ crm_clock_source_enable(CRM_CLOCK_SOURCE_HICK, TRUE); @@ -100,7 +109,7 @@ void wk_system_clock_config(void) } /* config pll clock resource */ - crm_pll_config(CRM_PLL_SOURCE_HICK, CRM_PLL_MULT_50, CRM_PLL_OUTPUT_RANGE_GT72MHZ); + crm_pll_config(CRM_PLL_SOURCE_HEXT_DIV, CRM_PLL_MULT_25, CRM_PLL_OUTPUT_RANGE_GT72MHZ); /* enable pll */ crm_clock_source_enable(CRM_CLOCK_SOURCE_PLL, TRUE); @@ -153,12 +162,12 @@ void wk_periph_clock_config(void) /* enable gpiob periph clock */ crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE); + /* enable gpiod periph clock */ + crm_periph_clock_enable(CRM_GPIOD_PERIPH_CLOCK, TRUE); + /* enable tmr1 periph clock */ crm_periph_clock_enable(CRM_TMR1_PERIPH_CLOCK, TRUE); - /* enable tmr9 periph clock */ - crm_periph_clock_enable(CRM_TMR9_PERIPH_CLOCK, TRUE); - /* enable tmr10 periph clock */ crm_periph_clock_enable(CRM_TMR10_PERIPH_CLOCK, TRUE); @@ -213,33 +222,27 @@ void wk_gpio_config(void) /* gpio input config */ gpio_init_struct.gpio_mode = GPIO_MODE_INPUT; - gpio_init_struct.gpio_pins = SW_IN2_PIN | SW_IN1_PIN; + gpio_init_struct.gpio_pins = GPIO_PINS_10 | GPIO_PINS_11; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init(GPIOB, &gpio_init_struct); /* gpio output config */ - gpio_bits_reset(GPIOB, MODE2_PIN | MODE1_PIN | MODE0_PIN); - gpio_bits_reset(DIR_GPIO_PORT, DIR_PIN); + gpio_bits_reset(GPIOB, MODE2_PIN | MODE1_PIN | MODE0_PIN | CLK_PIN); + gpio_bits_reset(GPIOA, DIR_PIN | EN_PIN); gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT; - gpio_init_struct.gpio_pins = MODE2_PIN | MODE1_PIN | MODE0_PIN; + gpio_init_struct.gpio_pins = MODE2_PIN | MODE1_PIN | MODE0_PIN | CLK_PIN; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; gpio_init(GPIOB, &gpio_init_struct); gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE; gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT; - gpio_init_struct.gpio_pins = DIR_PIN; + gpio_init_struct.gpio_pins = DIR_PIN | EN_PIN; gpio_init_struct.gpio_pull = GPIO_PULL_NONE; - gpio_init(DIR_GPIO_PORT, &gpio_init_struct); - - /* gpio analog config */ - gpio_init_struct.gpio_mode = GPIO_MODE_ANALOG; - gpio_init_struct.gpio_pins = EN_PIN; - gpio_init_struct.gpio_pull = GPIO_PULL_NONE; - gpio_init(EN_GPIO_PORT, &gpio_init_struct); + gpio_init(GPIOA, &gpio_init_struct); /* add user code begin gpio_config 2 */ @@ -436,67 +439,6 @@ void wk_tmr3_init(void) /* add user code end tmr3_init 2 */ } -/** - * @brief init tmr9 function. - * @param none - * @retval none - */ -void wk_tmr9_init(void) -{ - /* add user code begin tmr9_init 0 */ - - /* add user code end tmr9_init 0 */ - - gpio_init_type gpio_init_struct; - tmr_output_config_type tmr_output_struct; - gpio_default_para_init(&gpio_init_struct); - - /* add user code begin tmr9_init 1 */ - - /* add user code end tmr9_init 1 */ - - /* configure the CH2 pin */ - gpio_init_struct.gpio_pins = STEP_PIN; - gpio_init_struct.gpio_mode = GPIO_MODE_MUX; - gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL; - gpio_init_struct.gpio_pull = GPIO_PULL_NONE; - gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE; - gpio_init(STEP_GPIO_PORT, &gpio_init_struct); - - /* GPIO PIN remap */ - gpio_pin_remap_config(TMR9_GMUX, TRUE); - - /* configure counter settings */ - tmr_base_init(TMR9, 65535, 0); - tmr_cnt_dir_set(TMR9, TMR_COUNT_UP); - tmr_clock_source_div_set(TMR9, TMR_CLOCK_DIV1); - tmr_period_buffer_enable(TMR9, FALSE); - - /* configure sub-mode */ - tmr_sub_mode_select(TMR9, TMR_SUB_EXTERNAL_CLOCK_MODE_A); - - /* configure sub-mode input */ - tmr_trigger_input_select(TMR9, TMR_SUB_INPUT_SEL_IS2); - - /* configure channel 2 output settings */ - tmr_output_struct.oc_mode = TMR_OUTPUT_CONTROL_OFF; - tmr_output_struct.oc_output_state = TRUE; - tmr_output_struct.occ_output_state = FALSE; - tmr_output_struct.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH; - tmr_output_struct.occ_polarity = TMR_OUTPUT_ACTIVE_HIGH; - tmr_output_struct.oc_idle_state = FALSE; - tmr_output_struct.occ_idle_state = FALSE; - tmr_output_channel_config(TMR9, TMR_SELECT_CHANNEL_2, &tmr_output_struct); - tmr_channel_value_set(TMR9, TMR_SELECT_CHANNEL_2, 0); - tmr_output_channel_buffer_enable(TMR9, TMR_SELECT_CHANNEL_2, FALSE); - - tmr_counter_enable(TMR9, TRUE); - - /* add user code begin tmr9_init 2 */ - - /* add user code end tmr9_init 2 */ -} - /** * @brief init tmr10 function. * @param none diff --git a/project/src/main.c b/project/src/main.c index 370c7b3..6dbd982 100644 --- a/project/src/main.c +++ b/project/src/main.c @@ -29,7 +29,10 @@ /* private includes ----------------------------------------------------------*/ /* add user code begin private includes */ +#include "dwt_delay.h" + #include "by_debug.h" +#include "by_can.h" /* add user code end private includes */ /* private typedef -----------------------------------------------------------*/ @@ -63,59 +66,66 @@ /* add user code end 0 */ /** - * @brief main function. - * @param none - * @retval none - */ + * @brief main function. + * @param none + * @retval none + */ int main(void) { - /* add user code begin 1 */ + /* add user code begin 1 */ by_debug_init(); /* add user code end 1 */ - /* system clock config. */ - wk_system_clock_config(); + /* system clock config. */ + wk_system_clock_config(); - /* config periph clock. */ - wk_periph_clock_config(); + /* config periph clock. */ + wk_periph_clock_config(); - /* init debug function. */ - wk_debug_config(); + /* init debug function. */ + wk_debug_config(); - /* nvic config. */ - wk_nvic_config(); + /* nvic config. */ + wk_nvic_config(); - /* init usart2 function. */ - wk_usart2_init(); + /* init usart2 function. */ + wk_usart2_init(); - /* init exint function. */ - wk_exint_config(); + /* init exint function. */ + wk_exint_config(); - /* init tmr1 function. */ - wk_tmr1_init(); + /* init tmr1 function. */ + wk_tmr1_init(); - /* init tmr3 function. */ - wk_tmr3_init(); + /* init tmr3 function. */ + wk_tmr3_init(); - /* init tmr9 function. */ - wk_tmr9_init(); + /* init tmr10 function. */ + wk_tmr10_init(); - /* init tmr10 function. */ - wk_tmr10_init(); + /* init can1 function. */ + wk_can1_init(); - /* init can1 function. */ - wk_can1_init(); + /* init gpio function. */ + wk_gpio_config(); - /* init gpio function. */ - wk_gpio_config(); + /* add user code begin 2 */ + DWT_Init(); - /* add user code begin 2 */ LOGD("hello world"); - /* add user code end 2 */ - while (1) { - /* add user code begin 3 */ + gpio_bits_write(GPIOB, GPIO_PINS_12, FALSE); // MODE2 + gpio_bits_write(GPIOB, GPIO_PINS_13, TRUE); // MODE1 + gpio_bits_write(GPIOB, GPIO_PINS_14, TRUE); // MODE0 + gpio_bits_write(GPIOA, GPIO_PINS_9, FALSE); // EN + gpio_bits_write(GPIOA, GPIO_PINS_8, TRUE); // DIR + gpio_bits_write(GPIOB, GPIO_PINS_15, TRUE); // CLK + /* add user code end 2 */ + while(1) + { + /* add user code begin 3 */ + DWT_Delay(40); /* add user code end 3 */ - } + } }