风云再起 · 2022年06月25日

【GD32F310开发板试用】GD32F310移植TencentOS-tiny

开发板环境相关

  1. 官网下载软硬件资源包
    0001.png
    0002.png
  2. Keil相关设置
  • 兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\Projects\01_GPIO_Running_LED\MDK-ARM 下面GD32F310K_START.uvproj 加一个x变成 GD32F310K_START.uvprojx 然后Keil打开
    0003.png
  • 一些相关设置
    0004.png
    0005.png
    0006.png
    同时还要在配置中勾选支持C99模式:
    0012.png
  1. 开始移植TencentOS-tiny
  • TencentOS tiny的源码已经开源,github下载地址为:https://github.com/Tencent/Te...
  • 只介绍TencentOS tiny的内核移植,所以这里只需要用到 arch、board、kernel、osal四个目录下的源码。
    0007.png
  • 指定头文件路径
    0008.png
  • tos/arch的C文件对应: 兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\arch\arm\arm-v8m\common 和 兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\arch\arm\arm-v8m\cortex-m33\armcc
    0009.png
  • tos/kernel的C文件对应: 兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\kernel\core
    0010.png
  • tos/cmsis_os的C文件对应: 兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\osal\cmsis_os
    0011.png
  • 新建配置文件: \兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\TOS_CONFIG\tos_config.h

    #ifndef _TOS_CONFIG_H_
    #define  _TOS_CONFIG_H_
    
    #include "gd32f3x0.h"
    
    #define TOS_CFG_TASK_PRIO_MAX           10u
    
    #define TOS_CFG_ROUND_ROBIN_EN          1u
    
    #define TOS_CFG_OBJECT_VERIFY_EN        1u
    
    #define TOS_CFG_TASK_DYNAMIC_CREATE_EN  0u
    
    #define TOS_CFG_EVENT_EN                1u
    
    #define TOS_CFG_MMBLK_EN                1u
    
    #define TOS_CFG_MMHEAP_EN               0u
    
    #define TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE        0x400
    
    #define TOS_CFG_MUTEX_EN                1u
    
    #define TOS_CFG_TIMER_EN                1u
    
    #define TOS_CFG_PWR_MGR_EN              0u
    
    #define TOS_CFG_TICKLESS_EN             0u
    
    #define TOS_CFG_SEM_EN                  1u
    
    #define TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN      1u
    
    #define TOS_CFG_FAULT_BACKTRACE_EN      0u
    
    #define TOS_CFG_IDLE_TASK_STK_SIZE      128u
    
    #define TOS_CFG_CPU_TICK_PER_SECOND     1000u
    
    #define TOS_CFG_CPU_CLOCK               (SystemCoreClock)
    
    #define TOS_CFG_TIMER_AS_PROC           1u
    
    #define TOS_CFG_MAIL_QUEUE_EN           1u
    
    #endif
  • 兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\Projects\01_GPIO_Running_LED\gd32f3x0_it.c修改如下

    /*!
      \file    gd32f3x0_it.c
      \brief   interrupt service routines
      
      \version 2022-03-06, V1.0.0, demo for GD32F3x0
    */
    
    /*
      Copyright (c) 2022, GigaDevice Semiconductor Inc.
    
      Redistribution and use in source and binary forms, with or without modification, 
    are permitted provided that the following conditions are met:
    
      1. Redistributions of source code must retain the above copyright notice, this 
         list of conditions and the following disclaimer.
      2. Redistributions in binary form must reproduce the above copyright notice, 
         this list of conditions and the following disclaimer in the documentation 
         and/or other materials provided with the distribution.
      3. Neither the name of the copyright holder nor the names of its contributors 
         may be used to endorse or promote products derived from this software without 
         specific prior written permission.
    
      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
    IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
    OF SUCH DAMAGE.
    */
    
    #include "gd32f3x0_it.h"
    #include "systick.h"
    #include "tos_k.h"
    
    /*!
      \brief      this function handles NMI exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    void NMI_Handler(void)
    {
    }
    
    /*!
      \brief      this function handles HardFault exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    void HardFault_Handler(void)
    {
      /* if Hard Fault exception occurs, go to infinite loop */
      while(1){
      }
    }
    
    /*!
      \brief      this function handles MemManage exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    void MemManage_Handler(void)
    {
      /* if Memory Manage exception occurs, go to infinite loop */
      while(1){
      }
    }
    
    /*!
      \brief      this function handles BusFault exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    void BusFault_Handler(void)
    {
      /* if Bus Fault exception occurs, go to infinite loop */
      while(1){
      }
    }
    
    /*!
      \brief      this function handles UsageFault exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    void UsageFault_Handler(void)
    {
      /* if Usage Fault exception occurs, go to infinite loop */
      while(1){
      }
    }
    
    /*!
      \brief      this function handles SVC exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    void SVC_Handler(void)
    {
    }
    
    /*!
      \brief      this function handles DebugMon exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    void DebugMon_Handler(void)
    {
    }
    
    /*!
      \brief      this function handles PendSV exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    
    //__weak void PendSV_Handler(void)
    //{
    //}
    
    /*!
      \brief      this function handles SysTick exception
      \param[in]  none
      \param[out] none
      \retval     none
    */
    void SysTick_Handler(void)
    {
      //delay_decrement();
    //systick_inc();
      //HAL_IncTick();
    if (tos_knl_is_running())
    {
      tos_knl_irq_enter();
      tos_tick_handler();
      tos_knl_irq_leave();
    }
    }
  • 兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\arch\arm\arm-v8m\cortex-m33\armcc\port_c.c修改
    0014.png
  • 兆易创新GD32F310G-START\GD32F3x0_Demo_Suites_V2.2.0\GD32310K_START_Demo_Suites\Projects\01_GPIO_Running_LED\main.c修改

    /*!
      \file    main.c
      \brief   GPIO Running LED
      
      \version 2022-03-06, V1.0.0, demo for GD32F3x0
    */
    
    /*
      Copyright (c) 2022, GigaDevice Semiconductor Inc.
    
      Redistribution and use in source and binary forms, with or without modification, 
    are permitted provided that the following conditions are met:
    
      1. Redistributions of source code must retain the above copyright notice, this 
         list of conditions and the following disclaimer.
      2. Redistributions in binary form must reproduce the above copyright notice, 
         this list of conditions and the following disclaimer in the documentation 
         and/or other materials provided with the distribution.
      3. Neither the name of the copyright holder nor the names of its contributors 
         may be used to endorse or promote products derived from this software without 
         specific prior written permission.
    
      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
    WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
    IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
    INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
    NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
    WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
    OF SUCH DAMAGE.
    */
    
    #include "gd32f3x0.h"
    #include "gd32f310k_start.h"
    #include "systick.h"
    #include "gd32f3x0_usart.h"
    #include "gd32f3x0_gpio.h"
    
    /*!
      \brief      main function
      \param[in]  none
      \param[out] none
      \retval     none
    */
    
    #include "cmsis_os.h"
    
    #include <stdio.h>      
    void usart0_gpio_config(void);
    void usart0_config(void);
    
    k_task_t task;
    k_stack_t task_stack[1024];
    
    void test_task(void *Parameter)
    {
      while(1)
      {
          printf("hello world!\r\n");
          tos_task_delay(1000);
      }
    }
    
    
    int main(void)
    {  
      systick_config();
      
          usart0_gpio_config();
      usart0_config();
      
          k_err_t err;
         
      printf("Welcome to TencentOS tiny\r\n");
    
      tos_knl_init(); // TOS Tiny kernel initialize
                                          
      err = tos_task_create(&task, 
                            "task1", 
                            test_task,
                            NULL, 
                            2, 
                            task_stack,
                            1024,
                            20);
      if(err != K_ERR_NONE)
          printf("TencentOS Create task fail! code : %d \r\n",err);
      
      tos_knl_start(); // Start TOS Tiny
      
      
      
      /* enable the LED GPIO clock */
      rcu_periph_clock_enable(RCU_GPIOA);
      /* configure led GPIO port */ 
      gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8);
      gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
      
      gpio_bit_reset(GPIOA, GPIO_PIN_8);
      
      while(1){
          /* turn on led */
          gpio_bit_write(GPIOA, GPIO_PIN_8, SET);
          delay_1ms(1000);
          
          /* turn off led */
          gpio_bit_write(GPIOA, GPIO_PIN_8, RESET);
          delay_1ms(1000);
      }
    }
    
    //GPIO
    void usart0_gpio_config(void)
    {
      /* enable COM GPIO clock */
      rcu_periph_clock_enable(RCU_GPIOA);
    
      /* connect port to USARTx_Tx */
      gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);
    
      /* connect port to USARTx_Rx */
      gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);
    
      /* configure USART Tx as alternate function push-pull */
      gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9);
      gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9);
    
      /* configure USART Rx as alternate function push-pull */
      gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10);
      gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);
    }
    //USART0 
    void usart0_config(void)
    {
      /* enable USART clock */
      rcu_periph_clock_enable(RCU_USART0);
    
      /* USART configure */
      usart_deinit(USART0);
      usart_word_length_set(USART0, USART_WL_8BIT);
      usart_stop_bit_set(USART0, USART_STB_1BIT);
      usart_parity_config(USART0, USART_PM_NONE);
      usart_baudrate_set(USART0, 9600U);
      usart_receive_config(USART0, USART_RECEIVE_ENABLE);
      usart_transmit_config(USART0, USART_TRANSMIT_ENABLE);
    
      usart_enable(USART0);
    }
    
    /* retarget the C library printf function to the USART */
    int fputc(int ch, FILE *f)
    {
      usart_data_transmit(USART0, (uint8_t) ch);
      while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
      return ch;
    }
  • 最后编译与烧录
    0666.png

移植已成功,可以愉快的使用了!!!

推荐阅读
关注数
0
文章数
4
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息