KEILC++

keil .c.h在相同路径不用配置路径。

我们可以使用 static关键字来把类成员定义为静态的。当我们声明类的成员为静态时,这意味着无论创建多少个类的对象,静态成员都只有一个副本。

typedef void (*mainFun_t)(void *);

定义一个名为mainFun_t的函数指针

定义结构体指针必须使用molloc

C语言错误提示

#if defined (XXX) XXXXXXXXXXXXXX #else #error "XXX" #endif

BSP不需要父类,一个外设对应一个.c和.h,对应一个父类。全局类声明放在main.c,并用宏定义区分不同的芯片,并提示错误,不需要单独的全局.c文件,工程结构:BSP TASK OS INT SYS CMSIS Startup GD32LIB Doc,自己创建的源文件要放在对应的文件夹里面。使用new关键字更加方便。路径配置到工程里面,使用VScode和Git进行编辑和管理。自己创建的.c.h命名全部大写,模块的类名全部大写,成员函数首字母大写,全局的前面加g,变量和数组小写。IO下面的子类就包含LED类。

IO_F450.cpp改成GPIO_F450.cpp

#pragma once

#include "IO_F450.hpp"
#include "main.h"

IO_F450::IO_F450(uint32_t _GPIOx, uint32_t _io_pin)
{
	GPIOx = _GPIOx;
	io_pin = _io_pin;
}

void IO_F450::Set(void)
{
	gpio_bit_write(GPIOx,io_pin,SET);
}

void IO_F450::Reset(void)
{
	gpio_bit_write(GPIOx,io_pin,RESET);
}

void IO_F450::Toggle(void)
{
	gpio_bit_toggle(GPIOx,io_pin);
}

uint8_t IO_F450::Gets(void)
{
	return gpio_input_bit_get(GPIOx, io_pin);
}

void IO_F450::Init(void)
{
	//A4
	rcu_periph_clock_enable(RCU_GPIOA);
	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);
	
	//B0
	rcu_periph_clock_enable(RCU_GPIOB);
	gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);

	//B1 B8 B9
	gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_9);
	gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_9);
	
	//C6 C7 C8
	rcu_periph_clock_enable(RCU_GPIOC);
	gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8);
	
	//C12
	gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_12);
	gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
	
	//D9 D10 D11 D12
	rcu_periph_clock_enable(RCU_GPIOD);
	gpio_mode_set(GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12);
	
	//D13 D0 D1
	gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_1);
	gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_1);
	
	//E7 E8 E12 E15 E0 E1
	rcu_periph_clock_enable(RCU_GPIOE);
	gpio_mode_set(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_12|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1);
	gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_12|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1);
	
	//F6 F7 F8
	rcu_periph_clock_enable(RCU_GPIOF);
	gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8);
	gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8);
	
	//G3
	rcu_periph_clock_enable(RCU_GPIOG);
	gpio_mode_set(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_3);
	gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
}

IO_F450.hpp

#pragma once

#include <stdint.h>
#include "bsp/bsp.hpp"
#include "main.h"

class IO_F450: public IO
{   
    private:
    uint32_t GPIOx;
    uint32_t io_pin;
    
    public:
//    IO_F4(){}
    IO_F450(uint32_t _GPIOx, uint32_t _io_pin);
    void Set(void);
    void Reset(void);
    void Toggle(void);
    uint8_t Gets(void);
    void Init(void);   
};

IO.hpp

#pragma once

#include <stdint.h>

class IO
{
    public:
    IO(){}
    virtual void Set(void){}
    virtual void Reset(void){}
    virtual void Toggle(void){}
    virtual uint8_t Gets(void){return 0;}
    virtual void Init(void){}   
};

board.c

keil .c.h在相同路径不用配置路径。

我们可以使用 static关键字来把类成员定义为静态的。当我们声明类的成员为静态时,这意味着无论创建多少个类的对象,静态成员都只有一个副本。

typedef void (*mainFun_t)(void *);

定义一个名为mainFun_t的函数指针

定义结构体指针必须使用molloc

C语言错误提示

#if defined (XXX) XXXXXXXXXXXXXX #else #error "XXX" #endif

BSP不需要父类,一个外设对应一个.c和.h,对应一个父类。全局类声明放在main.c,并用宏定义区分不同的芯片,并提示错误,不需要单独的全局.c文件,工程结构:BSP TASK OS INT SYS CMSIS Startup GD32LIB Doc,自己创建的源文件要放在对应的文件夹里面。使用new关键字更加方便。路径配置到工程里面,使用VScode和Git进行编辑和管理。自己创建的.c.h命名全部大写,模块的类名全部大写,成员函数首字母大写,全局的前面加g,变量和数组小写。IO下面的子类就包含LED类。

IO_F450.cpp改成GPIO_F450.cpp

#pragma once

#include "IO_F450.hpp"
#include "main.h"

IO_F450::IO_F450(uint32_t _GPIOx, uint32_t _io_pin)
{
	GPIOx = _GPIOx;
	io_pin = _io_pin;
}

void IO_F450::Set(void)
{
	gpio_bit_write(GPIOx,io_pin,SET);
}

void IO_F450::Reset(void)
{
	gpio_bit_write(GPIOx,io_pin,RESET);
}

void IO_F450::Toggle(void)
{
	gpio_bit_toggle(GPIOx,io_pin);
}

uint8_t IO_F450::Gets(void)
{
	return gpio_input_bit_get(GPIOx, io_pin);
}

void IO_F450::Init(void)
{
	//A4
	rcu_periph_clock_enable(RCU_GPIOA);
	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);
	
	//B0
	rcu_periph_clock_enable(RCU_GPIOB);
	gpio_mode_set(GPIOB, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_0);

	//B1 B8 B9
	gpio_mode_set(GPIOB, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_9);
	gpio_output_options_set(GPIOB, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1|GPIO_PIN_8|GPIO_PIN_9);
	
	//C6 C7 C8
	rcu_periph_clock_enable(RCU_GPIOC);
	gpio_mode_set(GPIOC, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8);
	
	//C12
	gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_12);
	gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
	
	//D9 D10 D11 D12
	rcu_periph_clock_enable(RCU_GPIOD);
	gpio_mode_set(GPIOD, GPIO_MODE_INPUT, GPIO_PUPD_NONE, GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12);
	
	//D13 D0 D1
	gpio_mode_set(GPIOD, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_1);
	gpio_output_options_set(GPIOD, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13|GPIO_PIN_0|GPIO_PIN_1);
	
	//E7 E8 E12 E15 E0 E1
	rcu_periph_clock_enable(RCU_GPIOE);
	gpio_mode_set(GPIOE, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_12|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1);
	gpio_output_options_set(GPIOE, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_12|GPIO_PIN_15|GPIO_PIN_0|GPIO_PIN_1);
	
	//F6 F7 F8
	rcu_periph_clock_enable(RCU_GPIOF);
	gpio_mode_set(GPIOF, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8);
	gpio_output_options_set(GPIOF, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8);
	
	//G3
	rcu_periph_clock_enable(RCU_GPIOG);
	gpio_mode_set(GPIOG, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_3);
	gpio_output_options_set(GPIOG, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
}

IO_F450.hpp

#pragma once

#include <stdint.h>
#include "bsp/bsp.hpp"
#include "main.h"

class IO_F450: public IO
{   
    private:
    uint32_t GPIOx;
    uint32_t io_pin;
    
    public:
//    IO_F4(){}
    IO_F450(uint32_t _GPIOx, uint32_t _io_pin);
    void Set(void);
    void Reset(void);
    void Toggle(void);
    uint8_t Gets(void);
    void Init(void);   
};

IO.hpp

#pragma once

#include <stdint.h>

class IO
{
    public:
    IO(){}
    virtual void Set(void){}
    virtual void Reset(void){}
    virtual void Toggle(void){}
    virtual uint8_t Gets(void){return 0;}
    virtual void Init(void){}   
};

board.c

/*
 * Copyright (c) 2006-2019, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-05-24                  the first version
 */

#include <rthw.h>
#include <rtthread.h>
#include "systick.h"
#include "system_gd32f4xx.h"

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
/*
 * Please modify RT_HEAP_SIZE if you enable RT_USING_HEAP
 * the RT_HEAP_SIZE max value = (sram size - ZI size), 1024 means 1024 bytes
 */
#define RT_HEAP_SIZE (15*1024)
static rt_uint8_t rt_heap[RT_HEAP_SIZE];

RT_WEAK void *rt_heap_begin_get(void)
{
    return rt_heap;
}

RT_WEAK void *rt_heap_end_get(void)
{
    return rt_heap + RT_HEAP_SIZE;
}
#endif

void rt_os_tick_callback(void)
{
    rt_interrupt_enter();
    
    rt_tick_increase();

    rt_interrupt_leave();
}

/* cortex-m 架构使用 SysTick_Handler() */
void SysTick_Handler(void)
{
    rt_os_tick_callback();
}

/**
 * This function will initial your board.
 */
void rt_hw_board_init(void)
{
//#error "TODO 1: OS Tick Configuration."
    /* 
     * TODO 1: OS Tick Configuration
     * Enable the hardware timer and call the rt_os_tick_callback function
     * periodically with the frequency RT_TICK_PER_SECOND. 
     */
	
	 /* 1、系统、时钟初始化 */
	SystemInit();
//  HAL_Init(); // 初始化 HAL 库
//  SystemClock_Config(); // 配置系统时钟
//  SystemCoreClockUpdate(); // 对系统时钟进行更新

  /* 2、OS Tick 频率配置,RT_TICK_PER_SECOND = 1000 表示 1ms 触发一次中断 */
	systick_config();
//  _SysTick_Config(SystemCoreClock / RT_TICK_PER_SECOND);

    /* Call components board initial (use INIT_BOARD_EXPORT()) */
#ifdef RT_USING_COMPONENTS_INIT
    rt_components_board_init();
#endif

#if defined(RT_USING_USER_MAIN) && defined(RT_USING_HEAP)
    rt_system_heap_init(rt_heap_begin_get(), rt_heap_end_get());
#endif
}

#ifdef RT_USING_CONSOLE

static int uart_init(void)
{
#error "TODO 2: Enable the hardware uart and config baudrate."
    return 0;
}
INIT_BOARD_EXPORT(uart_init);

void rt_hw_console_output(const char *str)
{
#error "TODO 3: Output the string 'str' through the uart."
}

#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值