Compare commits
10 Commits
b7b439f611
...
ac40d80777
| Author | SHA1 | Date | |
|---|---|---|---|
| ac40d80777 | |||
| 9a50f27261 | |||
| f6993e7841 | |||
| 2fe603778a | |||
| e368d24a15 | |||
| 4ab76d6839 | |||
| 097dd5714b | |||
| 58a0bcc42c | |||
| f3228690bf | |||
| 94d4ffa136 |
+8
-8
@@ -1,7 +1,7 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
include(cmake/toolchain.cmake)
|
||||
|
||||
project(gd32e23_template)
|
||||
project(gd32e23x_template)
|
||||
|
||||
enable_language(C)
|
||||
enable_language(CXX)
|
||||
@@ -10,7 +10,7 @@ enable_language(ASM)
|
||||
# Use custom startup.S
|
||||
set(TARGET_STARTUP_ASM ${CMAKE_SOURCE_DIR}/startup/startup_gd32e23x.S)
|
||||
# Use custom linker script
|
||||
set(TARGET_LD_SCRIPT ${CMAKE_SOURCE_DIR}/ld/gd_e230f4_gcc.ld)
|
||||
set(TARGET_LD_SCRIPT ${CMAKE_SOURCE_DIR}/ld/gd32e23x_gcc.ld)
|
||||
# Add GD SDK
|
||||
add_subdirectory(sdk)
|
||||
|
||||
@@ -18,14 +18,14 @@ set(TARGET_C_SRC
|
||||
${CMAKE_SOURCE_DIR}/src/main.c
|
||||
${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c
|
||||
${CMAKE_SOURCE_DIR}/src/systick.c
|
||||
${CMAKE_SOURCE_DIR}/src/wc_bldc_control.c
|
||||
${CMAKE_SOURCE_DIR}/src/peripheral.c
|
||||
)
|
||||
|
||||
add_executable(gd32e23_template ${TARGET_C_SRC})
|
||||
add_executable(gd32e23x_template ${TARGET_C_SRC})
|
||||
|
||||
target_link_libraries(gd32e23_template GD32E23X_SDK)
|
||||
target_include_directories(gd32e23_template PUBLIC inc)
|
||||
target_link_libraries(gd32e23x_template GD32E23X_SDK)
|
||||
target_include_directories(gd32e23x_template PUBLIC inc)
|
||||
|
||||
# Generate .bin and .hex
|
||||
generate_binary_file(gd32e23_template)
|
||||
generate_hex_file(gd32e23_template)
|
||||
generate_binary_file(gd32e23x_template)
|
||||
generate_hex_file(gd32e23x_template)
|
||||
|
||||
+19783
File diff suppressed because it is too large
Load Diff
+19664
File diff suppressed because it is too large
Load Diff
@@ -1,2 +1,63 @@
|
||||
# gd32_temp
|
||||
from mo10
|
||||
# gd32e23x_template
|
||||
本项目为GD32E230Fx系列的基于Clion的CMake开发的工程模板。本人暂未入门,强行上强度,放弃keil,拥抱开源。遂尝试使用arm-none-eabi-gcc进行开发。
|
||||
有幸寻得[@mo10 ](https://github.com/mo10)大佬的帮助,本项目的基础目录架构与CMakeLists.txt与toolchain.cmake均为大佬提供。
|
||||
|
||||
## 关于C标准库的printf的重写
|
||||
在Keil开发中,ARMClang有自己的microLIB,所以直接调用,然后重写fputc函数即可,但在gcc中需要重写`_write`函数,本项目模板中已经在`main.c`中完成重写。
|
||||
同时需要添加`--spaces=nano.spaces`编译参数。
|
||||
但是printf本身占用flash比较大,建议谨慎使用,尤其是本项目搭建时候采用的型号为`GD32E230F4V6`内存非常有限,重写后加上spaces设置,目前能用。
|
||||
|
||||
## 添加源文件与头文件
|
||||
在`ProjectDir/CMakeLists.txt`中21行左右,添加对应源文件即可。
|
||||
|
||||
```cmake
|
||||
set(TARGET_C_SRC
|
||||
${CMAKE_SOURCE_DIR}/src/main.c
|
||||
${CMAKE_SOURCE_DIR}/src/gd32e23x_it.c
|
||||
${CMAKE_SOURCE_DIR}/src/systick.c
|
||||
${CMAKE_SOURCE_DIR}/src/peripheral.c
|
||||
)
|
||||
```
|
||||
## 关于链接脚本
|
||||
注意芯片选型, 不同型号的芯片 FLASH 和 RAM 大小不同。需要修改链接脚本`ld/gd32e23x_gcc.ld`
|
||||
|
||||
| 芯片型号 | FLASH | RAM |
|
||||
|------------|-------|-----|
|
||||
| GD32E230F4 | 16K | 4K |
|
||||
| GD32E230F8 | 64K | 8K |
|
||||
|
||||
```linkerscript
|
||||
/* memory map */
|
||||
MEMORY
|
||||
{
|
||||
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 16K
|
||||
RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 4K
|
||||
}
|
||||
```
|
||||
|
||||
## 关于Startup文件
|
||||
|
||||
[@mo10 ](https://github.com/mo10)大佬提供的一些想法,具体如下。但是我目前还没测试到实际的影响和作用范围,所以暂未同步进来,仅作为备忘内容
|
||||
```asm
|
||||
Reset_Handler:
|
||||
ldr r0, =_sp
|
||||
mov sp, r0
|
||||
ldr r0, =_end
|
||||
msr msplim, r0
|
||||
/* copy the data segment into ram */
|
||||
movs r1, #0
|
||||
b LoopCopyDataInit
|
||||
```
|
||||
|
||||
|
||||
## Ref
|
||||
|
||||
1. 参考LD/Startup
|
||||
|
||||
[https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/linker.ld](https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/linker.ld)
|
||||
|
||||
[https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/startup.s](https://github.com/Noveren/gd32e23x-template/blob/main/gd32e23x/template/startup.s)
|
||||
|
||||
2. 官方LD/Startup
|
||||
|
||||
使用Embedded Builder工具生成的C标准库生成的模板
|
||||
|
||||
@@ -105,4 +105,5 @@ set(CMAKE_ASM_FLAGS_RELEASE "-DNDEBUG -O3") # -flto
|
||||
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${TARGET_CFLAGS_HARDWARE} ${TARGET_CFLAGS_EXTRA}")
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TARGET_CFLAGS_HARDWARE} ${TARGET_CXXFLAGS_EXTRA}")
|
||||
set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} ${CMAKE_C_FLAGS} -x assembler-with-cpp")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-specs=nosys.specs -Wl,--gc-sections ${TARGET_LDFLAGS_EXTRA}")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "--specs=nano.specs --specs=nosys.specs -Wall -Wextra -Wl,--gc-sections ${TARGET_LDFLAGS_EXTRA}")
|
||||
# -fsigned-char maybe
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,11 @@
|
||||
//
|
||||
// Created by yelv1 on 24-9-22.
|
||||
//
|
||||
|
||||
#ifndef PERIPHERAL_H
|
||||
#define PERIPHERAL_H
|
||||
|
||||
void usart_config(void);
|
||||
void led_blink_config(void);
|
||||
|
||||
#endif //PERIPHERAL_H
|
||||
@@ -1,57 +0,0 @@
|
||||
//
|
||||
// Created by dell on 24-9-20.
|
||||
//
|
||||
|
||||
#ifndef WC_BLDC_CONTROL_H
|
||||
#define WC_BLDC_CONTROL_H
|
||||
|
||||
#include "gd32e23x.h"
|
||||
|
||||
#define RS485_GPIO_RCU RCU_GPIOA
|
||||
#define RS485_COM_RCU RCU_USART0
|
||||
#define RS485_PORT GPIOA
|
||||
#define RS485_DI_PIN GPIO_PIN_2
|
||||
#define RS485_RO_PIN GPIO_PIN_3
|
||||
#define RS485_RE_PIN GPIO_PIN_4
|
||||
#define RS485_COM USART0
|
||||
|
||||
#define GPIO_PORT_SPEED_CTRL GPIOA
|
||||
#define GPIO_PIN_SPEED_CTRL GPIO_PIN_10
|
||||
|
||||
#define GPIO_PORT_SPEED_FB GPIOA
|
||||
#define GPIO_PIN_SPEED_FB GPIO_PIN_9
|
||||
|
||||
#define GPIO_PORT_DRV_ENABLE GPIOF
|
||||
#define GPIO_PIN_DRV_ENABLE GPIO_PIN_1
|
||||
|
||||
#define GPIO_PORT_MOTOR_DIR GPIOF
|
||||
#define GPIO_PIN_MOTOR_DIR GPIO_PIN_0
|
||||
|
||||
#define RCU_SPEED_CTL RCU_GPIOA
|
||||
#define RCU_SPEED_FB RCU_GPIOA
|
||||
#define RCU_DRV_ENABLE RCU_GPIOF
|
||||
#define RCU_MOTOR_DIR RCU_GPIOF
|
||||
|
||||
#define TIMER_SPEED_CTL TIMER0
|
||||
#define TIMER_CH_SPEED_CTL TIMER_CH_2
|
||||
#define RCU_TIMER_SPEED_CTL RCU_TIMER0
|
||||
|
||||
void led_blink_config(void);
|
||||
|
||||
|
||||
|
||||
/* configure RS485 port & RE/DE Pin */
|
||||
void rs485_com_config(void);
|
||||
/* Set transmit enabel */
|
||||
void rs485_transmit_enable(void);
|
||||
/* Set receive enabel */
|
||||
void rs485_receive_enable(void);
|
||||
|
||||
void bldc_set_pwm(uint8_t pwm);
|
||||
void bldc_config(void);
|
||||
void bldc_enable_set(bit_status status);
|
||||
|
||||
|
||||
void timer2_config(void);
|
||||
|
||||
#endif //WC_BLDC_CONTROL_H
|
||||
@@ -34,7 +34,7 @@ SECTIONS
|
||||
KEEP (*(.fini))
|
||||
|
||||
. = ALIGN(4);
|
||||
/* the symbol ��_etext�� will be defined at the end of code section */
|
||||
/* the symbol '_etext' will be defined at the end of code section */
|
||||
_etext = .;
|
||||
} >FLASH
|
||||
|
||||
@@ -87,26 +87,26 @@ SECTIONS
|
||||
.data :
|
||||
{
|
||||
. = ALIGN(4);
|
||||
/* the symbol ��_sdata�� will be defined at the data section end start */
|
||||
/* the symbol '_sdata' will be defined at the data section end start */
|
||||
_sdata = .;
|
||||
*(.data)
|
||||
*(.data*)
|
||||
. = ALIGN(4);
|
||||
/* the symbol ��_edata�� will be defined at the data section end */
|
||||
/* the symbol '_edata' will be defined at the data section end */
|
||||
_edata = .;
|
||||
} >RAM AT> FLASH
|
||||
|
||||
. = ALIGN(4);
|
||||
.bss :
|
||||
{
|
||||
/* the symbol ��_sbss�� will be defined at the bss section start */
|
||||
/* the symbol '_sbss' will be defined at the bss section start */
|
||||
_sbss = .;
|
||||
__bss_start__ = _sbss;
|
||||
*(.bss)
|
||||
*(.bss*)
|
||||
*(COMMON)
|
||||
. = ALIGN(4);
|
||||
/* the symbol ��_ebss�� will be defined at the bss section end */
|
||||
/* the symbol '_ebss' will be defined at the bss section end */
|
||||
_ebss = .;
|
||||
__bss_end__ = _ebss;
|
||||
} >RAM
|
||||
+13
-17
@@ -1,18 +1,16 @@
|
||||
/*!
|
||||
\file main.c
|
||||
\brief led spark with systick, USART print and key example
|
||||
|
||||
|
||||
\version 2024-02-22, V2.1.0, firmware for GD32E23x
|
||||
*/
|
||||
|
||||
#include "main.h"
|
||||
#include <stdio.h>
|
||||
#include "gd32e23x.h"
|
||||
#include "systick.h"
|
||||
|
||||
#include "gd32e23x_libopt.h"
|
||||
|
||||
#include "wc_bldc_control.h"
|
||||
#include "peripheral.h"
|
||||
|
||||
/*!
|
||||
\brief main function
|
||||
@@ -20,31 +18,29 @@
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* configure systick */
|
||||
systick_config();
|
||||
|
||||
// led_config();
|
||||
usart_config();
|
||||
led_blink_config();
|
||||
|
||||
rs485_com_config();
|
||||
|
||||
bldc_config();
|
||||
bldc_set_pwm(80);
|
||||
delay_ms(5000);
|
||||
bldc_enable_set(SET);
|
||||
printf("system start!\r\n");
|
||||
|
||||
while(1){
|
||||
|
||||
printf("hello world!\r\n");
|
||||
delay_ms(5000);
|
||||
}
|
||||
}
|
||||
|
||||
/* retarget the C library printf function to the USART */
|
||||
int fputc(int ch, FILE *f)
|
||||
int _write (int fd, char *pBuffer, int size)
|
||||
{
|
||||
usart_data_transmit(USART0, (uint8_t)ch);
|
||||
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
||||
return ch;
|
||||
for (int i = 0; i < size; i++)
|
||||
{
|
||||
usart_data_transmit(USART0, (uint8_t)pBuffer[i]);
|
||||
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
//
|
||||
// Created by yelv1 on 24-9-22.
|
||||
//
|
||||
#include "peripheral.h"
|
||||
#include "gd32e23x.h"
|
||||
|
||||
void usart_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
rcu_periph_clock_enable(RCU_USART0);
|
||||
|
||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_3);
|
||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_3);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_3);
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_2);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_2);
|
||||
|
||||
usart_deinit(USART0);
|
||||
usart_baudrate_set(USART0, 115200U);
|
||||
usart_receive_config(USART0, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_enable(USART0);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_4);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_4);
|
||||
|
||||
gpio_bit_write(GPIOA, GPIO_PIN_4, SET);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief led blink configuration
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void led_blink_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOB);
|
||||
|
||||
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1);
|
||||
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
|
||||
gpio_bit_write(GPIOB, GPIO_PIN_1, SET);
|
||||
|
||||
rcu_periph_clock_enable(RCU_TIMER13);
|
||||
timer_deinit(RCU_TIMER13);
|
||||
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_initpara.prescaler =7199;
|
||||
timer_initpara.alignedmode =TIMER_COUNTER_EDGE;
|
||||
timer_initpara.counterdirection =TIMER_COUNTER_UP;
|
||||
timer_initpara.period =999;
|
||||
timer_initpara.clockdivision =TIMER_CKDIV_DIV1;
|
||||
timer_init(TIMER13, &timer_initpara);
|
||||
|
||||
timer_auto_reload_shadow_enable(TIMER13);
|
||||
timer_interrupt_enable(TIMER13, TIMER_INT_UP);
|
||||
nvic_irq_enable(TIMER13_IRQn, 0);
|
||||
timer_enable(TIMER13);
|
||||
}
|
||||
@@ -1,227 +0,0 @@
|
||||
//
|
||||
// Created by dell on 24-9-20.
|
||||
//
|
||||
|
||||
#include "wc_bldc_control.h"
|
||||
#include "gd32e23x.h"
|
||||
|
||||
/*!
|
||||
\brief led blink configuration
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void led_blink_config(void){
|
||||
rcu_periph_clock_enable(RCU_GPIOB);
|
||||
|
||||
gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1);
|
||||
gpio_output_options_set(GPIOB, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
|
||||
gpio_bit_write(GPIOB, GPIO_PIN_1, SET);
|
||||
|
||||
rcu_periph_clock_enable(RCU_TIMER13);
|
||||
timer_deinit(RCU_TIMER13);
|
||||
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_initpara.prescaler =7199;
|
||||
timer_initpara.alignedmode =TIMER_COUNTER_EDGE;
|
||||
timer_initpara.counterdirection =TIMER_COUNTER_UP;
|
||||
timer_initpara.period =999;
|
||||
timer_initpara.clockdivision =TIMER_CKDIV_DIV1;
|
||||
timer_init(TIMER13, &timer_initpara);
|
||||
|
||||
timer_auto_reload_shadow_enable(TIMER13);
|
||||
timer_interrupt_enable(TIMER13, TIMER_INT_UP);
|
||||
nvic_irq_enable(TIMER13_IRQn, 0);
|
||||
timer_enable(TIMER13);
|
||||
}
|
||||
|
||||
/*!
|
||||
\brief configure RS485 port & RE/DE Pin
|
||||
\param[in] none
|
||||
\param[out] none
|
||||
\retval none
|
||||
*/
|
||||
void rs485_com_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RS485_GPIO_RCU);
|
||||
rcu_periph_clock_enable(RS485_COM_RCU);
|
||||
|
||||
gpio_af_set(RS485_PORT, GPIO_AF_1, RS485_RO_PIN | RS485_DI_PIN);
|
||||
|
||||
/* configure USART Tx as alternate function push-pull */
|
||||
gpio_mode_set(RS485_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_DI_PIN);
|
||||
gpio_output_options_set(RS485_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_DI_PIN);
|
||||
|
||||
gpio_mode_set(RS485_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, RS485_RO_PIN);
|
||||
gpio_output_options_set(RS485_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, RS485_RO_PIN);
|
||||
|
||||
/* USART configure */
|
||||
usart_deinit(RS485_COM);
|
||||
usart_baudrate_set(RS485_COM, 115200U);
|
||||
usart_receive_config(RS485_COM, USART_RECEIVE_ENABLE);
|
||||
usart_transmit_config(RS485_COM, USART_TRANSMIT_ENABLE);
|
||||
|
||||
usart_enable(RS485_COM);
|
||||
|
||||
gpio_mode_set(RS485_PORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, RS485_RE_PIN);
|
||||
gpio_output_options_set(RS485_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, RS485_RE_PIN);
|
||||
|
||||
gpio_bit_write(RS485_PORT, RS485_RE_PIN, SET);
|
||||
}
|
||||
|
||||
void rs485_transmit_enable(void)
|
||||
{
|
||||
gpio_bit_write(RS485_PORT, RS485_RE_PIN, SET);
|
||||
}
|
||||
|
||||
void rs485_receive_enable(void)
|
||||
{
|
||||
gpio_bit_write(RS485_PORT, RS485_RE_PIN, RESET);
|
||||
}
|
||||
|
||||
uint8_t speed_pwm = 0; //bldc default pwm is 30
|
||||
|
||||
void bldc_set_pwm(uint8_t pwm)
|
||||
{
|
||||
if (pwm > 0 && pwm <= 100)
|
||||
{
|
||||
speed_pwm = pwm;
|
||||
timer_channel_output_pulse_value_config(TIMER0, TIMER_CH_2, 12*speed_pwm);
|
||||
}
|
||||
}
|
||||
|
||||
void bldc_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_SPEED_CTL | RCU_SPEED_FB);
|
||||
rcu_periph_clock_enable(RCU_DRV_ENABLE | RCU_MOTOR_DIR);
|
||||
|
||||
/* ------------------------------
|
||||
-----BLDC speed control GPIO-----
|
||||
-----GPIOA_10 -- */
|
||||
gpio_mode_set(GPIO_PORT_SPEED_CTRL, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_SPEED_CTRL);
|
||||
gpio_output_options_set(GPIO_PORT_SPEED_CTRL, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_SPEED_CTRL);
|
||||
gpio_af_set(GPIO_PORT_SPEED_CTRL, GPIO_AF_2, GPIO_PIN_SPEED_CTRL);
|
||||
|
||||
/* ------------------------------
|
||||
-----BLDC speed report GPIO------
|
||||
-----GPIOA_9 --- */
|
||||
// gpio_mode_set(GPIO_PORT_SPEED_FB, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_SPEED_FB);
|
||||
// gpio_output_options_set(GPIO_PORT_SPEED_FB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_SPEED_FB);
|
||||
// gpio_af_set(GPIO_PORT_SPEED_FB, GPIO_AF_2, GPIO_PIN_SPEED_FB);
|
||||
|
||||
/* ------------------------------
|
||||
-----BLDC enable GPIO------------
|
||||
-----GPIOF_1 --------- */
|
||||
gpio_mode_set(GPIO_PORT_DRV_ENABLE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_DRV_ENABLE);
|
||||
gpio_output_options_set(GPIO_PORT_DRV_ENABLE, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_DRV_ENABLE);
|
||||
gpio_bit_reset(GPIO_PORT_DRV_ENABLE, GPIO_PIN_DRV_ENABLE);
|
||||
|
||||
/* ------------------------------
|
||||
-----BLDC forward/reverse GPIO---
|
||||
-----GPIOF_0 ---
|
||||
-----no need control for ---
|
||||
----- WM7040-24V*/
|
||||
gpio_mode_set(GPIO_PORT_MOTOR_DIR, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_MOTOR_DIR);
|
||||
gpio_output_options_set(GPIO_PORT_MOTOR_DIR, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_MOTOR_DIR);
|
||||
gpio_bit_set(GPIO_PORT_MOTOR_DIR, GPIO_PIN_MOTOR_DIR);
|
||||
|
||||
// gpio_bit_write(GPIOA, GPIO_PIN_10, RESET);
|
||||
|
||||
timer_parameter_struct timer_initpara;
|
||||
timer_oc_parameter_struct timer_ocinitpara;
|
||||
// timer_ic_parameter_struct timer_icinitpara;
|
||||
|
||||
rcu_periph_clock_enable(RCU_TIMER_SPEED_CTL);
|
||||
timer_deinit(TIMER_SPEED_CTL);
|
||||
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
timer_initpara.prescaler =59;
|
||||
timer_initpara.alignedmode =TIMER_COUNTER_EDGE;
|
||||
timer_initpara.counterdirection =TIMER_COUNTER_UP;
|
||||
timer_initpara.period =1199;
|
||||
timer_initpara.clockdivision =TIMER_CKDIV_DIV1;
|
||||
timer_init(TIMER_SPEED_CTL, &timer_initpara);
|
||||
|
||||
timer_channel_output_struct_para_init(&timer_ocinitpara);
|
||||
timer_ocinitpara.outputstate =TIMER_CCX_ENABLE;
|
||||
timer_ocinitpara.outputnstate =TIMER_CCXN_DISABLE;
|
||||
timer_ocinitpara.ocpolarity =TIMER_OC_POLARITY_HIGH;
|
||||
timer_ocinitpara.ocnpolarity =TIMER_OCN_POLARITY_HIGH;
|
||||
timer_ocinitpara.ocidlestate =TIMER_OC_IDLE_STATE_LOW;
|
||||
timer_ocinitpara.ocnidlestate =TIMER_OCN_IDLE_STATE_LOW;
|
||||
timer_channel_output_config(TIMER_SPEED_CTL, TIMER_CH_SPEED_CTL, &timer_ocinitpara);
|
||||
|
||||
timer_channel_output_pulse_value_config(TIMER_SPEED_CTL, TIMER_CH_SPEED_CTL, 12* speed_pwm);
|
||||
timer_channel_output_mode_config(TIMER_SPEED_CTL, TIMER_CH_SPEED_CTL, TIMER_OC_MODE_PWM0);
|
||||
timer_channel_output_shadow_config(TIMER_SPEED_CTL, TIMER_CH_SPEED_CTL, TIMER_OC_SHADOW_DISABLE);
|
||||
|
||||
timer_primary_output_config(TIMER_SPEED_CTL, ENABLE);
|
||||
timer_auto_reload_shadow_enable(TIMER_SPEED_CTL);
|
||||
|
||||
timer_enable(TIMER_SPEED_CTL);
|
||||
|
||||
nvic_irq_enable(TIMER0_Channel_IRQn, 1);
|
||||
}
|
||||
|
||||
void bldc_enable_set(bit_status status)
|
||||
{
|
||||
gpio_bit_write(GPIOF, GPIO_PIN_1, status);
|
||||
}
|
||||
|
||||
void timer2_config(void)
|
||||
{
|
||||
rcu_periph_clock_enable(RCU_GPIOA);
|
||||
rcu_periph_clock_enable(RCU_CFGCMP);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_9);
|
||||
|
||||
nvic_irq_enable(EXTI4_15_IRQn, 2U);
|
||||
|
||||
syscfg_exti_line_config(EXTI_SOURCE_GPIOA, EXTI_SOURCE_PIN9);
|
||||
exti_init(EXTI_9, EXTI_INTERRUPT, EXTI_TRIG_FALLING);
|
||||
exti_interrupt_flag_clear(EXTI_9);
|
||||
|
||||
gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_6);
|
||||
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_6);
|
||||
gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_6);
|
||||
|
||||
timer_ic_parameter_struct timer_icinitpara;
|
||||
timer_parameter_struct timer_initpara;
|
||||
|
||||
/* enable the TIMER clock */
|
||||
rcu_periph_clock_enable(RCU_TIMER2);
|
||||
/* disable a TIMER */
|
||||
timer_deinit(TIMER2);
|
||||
/* initialize TIMER init parameter struct */
|
||||
timer_struct_para_init(&timer_initpara);
|
||||
/* TIMER2 configuration */
|
||||
timer_initpara.prescaler = 71;
|
||||
timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
|
||||
timer_initpara.counterdirection = TIMER_COUNTER_UP;
|
||||
timer_initpara.period = 65535;
|
||||
timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
|
||||
timer_init(TIMER2, &timer_initpara);
|
||||
|
||||
/* TIMER2 configuration */
|
||||
/* initialize TIMER channel input parameter struct */
|
||||
timer_channel_input_struct_para_init(&timer_icinitpara);
|
||||
/* TIMER2 CH0 input capture configuration */
|
||||
timer_icinitpara.icpolarity = TIMER_IC_POLARITY_RISING;
|
||||
timer_icinitpara.icselection = TIMER_IC_SELECTION_DIRECTTI;
|
||||
timer_icinitpara.icprescaler = TIMER_IC_PSC_DIV1;
|
||||
timer_icinitpara.icfilter = 0x0;
|
||||
timer_input_capture_config(TIMER2,TIMER_CH_0,&timer_icinitpara);
|
||||
|
||||
/* auto-reload preload enable */
|
||||
timer_auto_reload_shadow_enable(TIMER2);
|
||||
/* clear channel 0 interrupt bit */
|
||||
timer_interrupt_flag_clear(TIMER2,TIMER_INT_FLAG_CH0);
|
||||
/* channel 0 interrupt enable */
|
||||
timer_interrupt_enable(TIMER2,TIMER_INT_CH0);
|
||||
|
||||
/* TIMER2 counter enable */
|
||||
timer_enable(TIMER2);
|
||||
|
||||
nvic_irq_enable(TIMER2_IRQn, 3U);
|
||||
}
|
||||
Reference in New Issue
Block a user