Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Add" support for gd32f10x board #109

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,894 changes: 986 additions & 908 deletions boards.txt

Large diffs are not rendered by default.

41 changes: 41 additions & 0 deletions cores/arduino/USBCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,17 @@ void EPBuffer<L>::flush()
// Only start the next transmission if the device hasn't been
// reset.
this->txWaiting = true;
#ifdef GD32F10x
if (this->ep == 0) {
// TODO: This is super suspect. There is some state-machining happening in the
// background in the usbd driver that we have to mimick here. Oof.
if (USBCore().usbDev().control.req.wLength) {
USBCore().usbDev().control.ctl_state = USBD_CTL_DATA_IN;
} else {
USBCore().usbDev().control.ctl_state = USBD_CTL_STATUS_IN;
}
}
#endif
USBCore().usbDev().drv_handler->ep_write((uint8_t*)this->buf, this->ep, this->len());
}
this->reset();
Expand Down Expand Up @@ -503,11 +514,32 @@ void handleReset(usb_dev *usbd)

USBCore_::USBCore_()
{
/*
* This is here to ensure we're in a closed state in the event the
* bootloader did not close the application. This is because when
* the application is open the alt function is active on the pin;
* if there is no external pullup, we temporarily use the D+ pin
* to signal to the host we've connected, which we can't do if the
* USB application is active.
*/
USBD_CTL |= CTL_CLOSE;

/*
* Use global ‘usbd’ here, instead of wrapped version, to avoid
* initialization loop.
*/
usb_init(&desc, ClassCore::structPtr());

/*
* After init the application may take some time to stabilize.
* This is the case if the bootloader didn't reset the application
* state before jumping to user code. If we service the interrupts
* too quickly after reset the application loses state and becomes
* unresponsive.
*/
volatile int i = 0;
for (; i<1000;) { i++; }

usbd.user_data = this;

oldResetHandler = usbd.drv_handler->ep_reset;
Expand Down Expand Up @@ -785,12 +817,21 @@ void USBCore_::transcSetup(usb_dev* usbd, uint8_t ep)
if (reqstat == REQ_SUPP) {
if (usbd->control.req.wLength == 0) {
/* USB control transfer status in stage */
#ifdef GD32F10x
usbd->control.ctl_state = USBD_CTL_STATUS_IN;
#endif
this->sendZLP(usbd, 0);
} else {
if (usbd->control.req.bmRequestType & USB_TRX_IN) {
#ifdef GD32F10x
usbd->control.ctl_state = USBD_CTL_DATA_IN;
#endif
usbd_ep_send(usbd, 0, usbd->transc_in[0].xfer_buf, usbd->transc_in[0].xfer_len);
} else {
/* USB control transfer data out stage */
#ifdef GD32F10x
usbd->control.ctl_state = USBD_CTL_DATA_OUT;
#endif
this->sendZLP(usbd, 0);
// TODO: this is a vestige of the copy from GD’s
//source. Unfortunately, it runs afoul of pluggable
Expand Down
6 changes: 4 additions & 2 deletions cores/arduino/analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ OF SUCH DAMAGE.
#define ADC_NUMS 1
#elif defined(GD32E23x)
#define ADC_NUMS 1
#elif defined(GD32F10x)
#define ADC_NUMS 3
#elif defined(GD32E50X)
#ifdef ADC2
#define ADC_NUMS 3
Expand Down Expand Up @@ -113,9 +115,9 @@ void set_dac_value(PinName pinname, uint16_t value)
DAC_[index].isactive = true;
} else {
//set dac value
#if defined(GD32F30x) || (defined(GD32F1x0) && defined(GD32F170_190)) || defined(GD32E50X)
#if defined(GD32F30x) || (defined(GD32F1x0) && defined(GD32F170_190)) || defined(GD32E50X) || defined(GD32F10x)
dac_data_set(dac_periph, DAC_ALIGN_12B_R, value);
#elif defined(GD32F10x) && !defined(GD32F170_190)
#elif defined(GD32F1x0) && !defined(GD32F170_190)
dac0_data_set(DAC_ALIGN_12B_R, value);
#elif defined(GD32F3x0)
dac_data_set(DAC_ALIGN_12B_R, value);
Expand Down
50 changes: 31 additions & 19 deletions cores/arduino/gd32/PinNames.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@
extern "C" {
#endif

#define ALT1 0x1000
#define ALT2 0x2000
#define ALT3 0x3000
#define ALT4 0x4000
#define ALT5 0x5000
#define ALT6 0x6000
#define ALT7 0x7000
#define ALTMASK 0x7000

typedef enum {
#if defined GPIOA
PORTA_0 = 0x00,
Expand Down Expand Up @@ -200,6 +209,11 @@ typedef enum {
ADC_TEMP = 0xF0,
ADC_VREF = 0xF1,

// pin names specific to the variant
#if __has_include("PinNamesVar.h")
#include "PinNamesVar.h"
#endif

NC = (int)0xFFFFFFFF
} PinName;

Expand All @@ -219,46 +233,46 @@ struct gpio_function {
configure the speed, mode, pull, speed, af and remap function of pins
the parameter function contains the configuration information,show as below
bit 0:2 gpio mode input / output / af / analog
bit 3:8 remap
bit 9:10 gpio speed
bit 11 output push-pull / open drain
bit 12:13 no pull, pull-up, pull-down
bit 14:17 channel af function
bit 18:22 channel of adc/timer/dac
bit 23 PWM channel-ON
bit 24:31 reserved
bit 3:9 remap
bit 10:11 gpio speed
bit 12 output push-pull / open drain
bit 13:14 no pull, pull-up, pull-down
bit 15:18 channel af function
bit 19:23 channel of adc/timer/dac
bit 24 PWM channel-ON
bit 25:31 reserved
*/
typedef enum {
/* pin mode */
PIN_MODE_SHIFT = 0,
PIN_MODE_MASK = 0x07,

/* pin seed */
PIN_SPEED_SHIFT = 9,
PIN_SPEED_SHIFT = 10,
PIN_SPEED_MASK = 0x03,

/* pin remap */
/* pin remap, including disable bit */
PIN_REMAP_SHIFT = 3,
PIN_REMAP_MASK = 0x3F,
PIN_REMAP_MASK = 0x7F,

/* pin output mode */
PIN_OUTPUT_MODE_SHIFT = 11,
PIN_OUTPUT_MODE_SHIFT = 12,
PIN_OUTPUT_MODE_MASK = 0x01,

/* pin pull_up_down */
PIN_PULL_STATE_SHIFT = 12,
PIN_PULL_STATE_SHIFT = 13,
PIN_PULL_STATE_MASK = 0x03,

/* pin af */
PIN_AF_SHIFT = 14,
PIN_AF_SHIFT = 15,
PIN_AF_MASK = 0xFF,

/* pin channel */
PIN_CHANNEL_SHIFT = 18,
PIN_CHANNEL_SHIFT = 19,
PIN_CHANNEL_MASK = 0x1F,

/* pin PWM channel-ON state */
PIN_CHON_SHIFT = 23,
PIN_CHON_SHIFT = 24,
PIN_CHON_MASK = 0x01,
} PinFunctionDivide;

Expand All @@ -277,16 +291,13 @@ typedef enum {
#define GD_PIN_CHANNEL_GET(X) ((X >> PIN_CHANNEL_SHIFT) & PIN_CHANNEL_MASK)
#define GD_PIN_CHON_GET(X) ((X >> PIN_CHON_SHIFT) & PIN_CHON_MASK)



// This typedef is used to extend the PinMode typedef enum
// in the ArduinoAPI, since they don't have contants
typedef enum {
INPUT_ANALOG = 99, // We assume that the Arduino core will never have 99 PinModes
OUTPUT_OPEN_DRAIN // It'd be cleaner to be able to count the size of that enum
} PinModeExtension;


typedef enum {
PIN_MODE_INPUT = 0,
PIN_MODE_AIN = 0,
Expand Down Expand Up @@ -328,6 +339,7 @@ enum {
((CHN & PIN_CHANNEL_MASK) << PIN_CHANNEL_SHIFT) | ((CHON & PIN_CHON_MASK) << PIN_CHON_SHIFT))
#define GD_PIN_FUNCTION3(MODE, ODPP, AFN) ((int)(MODE & PIN_MODE_MASK) | ((ODPP & PIN_OUTPUT_MODE_MASK) << PIN_OUTPUT_MODE_SHIFT) | ((AFN & PIN_AF_MASK) << PIN_AF_SHIFT))
#define GD_PIN_FUNCTION4(MODE, ODPP, PUPD, AFN) ((int)(MODE & PIN_MODE_MASK) | ((ODPP & PIN_OUTPUT_MODE_MASK) << PIN_OUTPUT_MODE_SHIFT) | ((PUPD & PIN_PULL_STATE_MASK) << PIN_PULL_STATE_SHIFT ) | ((AFN & PIN_AF_MASK) << PIN_AF_SHIFT))
#define GD_PIN_FUNCTION5(MODE, ODPP, PUPD, REMAP) ((int)(MODE & PIN_MODE_MASK) | ((ODPP & PIN_OUTPUT_MODE_MASK) << PIN_OUTPUT_MODE_SHIFT) | ((PUPD & PIN_PULL_STATE_MASK) << PIN_PULL_STATE_SHIFT ) | ((REMAP & PIN_REMAP_MASK) << PIN_REMAP_SHIFT))
#define GD_PIN_FUNC_ANALOG_CH(CHN) ((int)(PIN_MODE_ANALOG & PIN_MODE_MASK) |\
((CHN & PIN_CHANNEL_MASK) << PIN_CHANNEL_SHIFT) |\
(((int)PIN_PUPD_NONE & PIN_PULL_STATE_MASK) << PIN_PULL_STATE_SHIFT))
Expand Down
24 changes: 24 additions & 0 deletions cores/arduino/gd32/Source/gd32xxyy_src.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,30 @@
#include "gd32f1x0_ivref.c"
#include "gd32f1x0_can.c"
#endif /* GD32F170_190 */
#elif defined(GD32F10x)
#include "gd32f10x_adc.c"
#include "gd32f10x_bkp.c"
#include "gd32f10x_can.c"
#include "gd32f10x_crc.c"
#include "gd32f10x_dac.c"
#include "gd32f10x_dbg.c"
#include "gd32f10x_dma.c"
#include "gd32f10x_enet.c"
#include "gd32f10x_exmc.c"
#include "gd32f10x_exti.c"
#include "gd32f10x_fmc.c"
#include "gd32f10x_fwdgt.c"
#include "gd32f10x_gpio.c"
#include "gd32f10x_i2c.c"
#include "gd32f10x_misc.c"
#include "gd32f10x_pmu.c"
#include "gd32f10x_rcu.c"
#include "gd32f10x_rtc.c"
#include "gd32f10x_sdio.c"
#include "gd32f10x_spi.c"
#include "gd32f10x_timer.c"
#include "gd32f10x_usart.c"
#include "gd32f10x_wwdgt.c"
#elif defined(GD32E23x)
#include "gd32e23x_adc.c"
#include "gd32e23x_cmp.c"
Expand Down
66 changes: 66 additions & 0 deletions cores/arduino/gd32/gd32f10x_libopt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*!
\file gd32f10x_libopt.h
\brief library optional for gd32f10x

\version 2014-12-26, V1.0.0, firmware for GD32F10x
\version 2017-06-20, V2.0.0, firmware for GD32F10x
\version 2018-07-31, V2.1.0, firmware for GD32F10x
*/

/*
Copyright (c) 2018, GigaDevice Semiconductor Inc.

All rights reserved.

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.
*/

#ifndef GD32F10X_LIBOPT_H
#define GD32F10X_LIBOPT_H

#include "gd32f10x_adc.h"
#include "gd32f10x_bkp.h"
#include "gd32f10x_can.h"
#include "gd32f10x_crc.h"
#include "gd32f10x_dac.h"
#include "gd32f10x_dma.h"
#include "gd32f10x_enet.h"
#include "gd32f10x_exmc.h"
#include "gd32f10x_exti.h"
#include "gd32f10x_fmc.h"
#include "gd32f10x_gpio.h"
#include "gd32f10x_i2c.h"
#include "gd32f10x_fwdgt.h"
#include "gd32f10x_dbg.h"
#include "gd32f10x_misc.h"
#include "gd32f10x_pmu.h"
#include "gd32f10x_rcu.h"
#include "gd32f10x_rtc.h"
#include "gd32f10x_sdio.h"
#include "gd32f10x_spi.h"
#include "gd32f10x_timer.h"
#include "gd32f10x_usart.h"
#include "gd32f10x_wwdgt.h"

#endif /* GD32F10X_LIBOPT_H */
48 changes: 48 additions & 0 deletions cores/arduino/gd32/gd32f10x_remap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
__REMAP_NAME__(SPI0_REMAP) /*!< SPI0 remapping */
__REMAP_NAME__(I2C0_REMAP) /*!< I2C0 remapping */
__REMAP_NAME__(USART0_REMAP) /*!< USART0 remapping */
__REMAP_NAME__(USART1_REMAP) /*!< USART1 remapping */
__REMAP_NAME__(USART2_PARTIAL_REMAP) /*!< USART2 partial remapping */
__REMAP_NAME__(USART2_FULL_REMAP) /*!< USART2 full remapping */
__REMAP_NAME__(TIMER0_PARTIAL_REMAP) /*!< TIMER0 partial remapping */
__REMAP_NAME__(TIMER0_FULL_REMAP) /*!< TIMER0 full remapping */
__REMAP_NAME__(TIMER1_PARTIAL_REMAP0) /*!< TIMER1 partial remapping */
__REMAP_NAME__(TIMER1_PARTIAL_REMAP1) /*!< TIMER1 partial remapping */
__REMAP_NAME__(TIMER1_FULL_REMAP) /*!< TIMER1 full remapping */
__REMAP_NAME__(TIMER2_PARTIAL_REMAP) /*!< TIMER2 partial remapping */
__REMAP_NAME__(TIMER2_FULL_REMAP) /*!< TIMER2 full remapping */
__REMAP_NAME__(TIMER3_REMAP) /*!< TIMER3 remapping */
__REMAP_NAME__(PD01_REMAP) /*!< PD01 remapping */
#if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
__REMAP_NAME__(CAN_PARTIAL_REMAP) /*!< CAN partial remapping(only for GD32F10X_MD devices), GD32F10X_HD devices and GD32F10X_XD devices) */
__REMAP_NAME__(CAN_FULL_REMAP) /*!< CAN full remapping(only for GD32F10X_MD devices), GD32F10X_HD devices and GD32F10X_XD devices) */
#endif /* GD32F10X_MD||GD32F10X_HD||GD32F10X_XD */
#if (defined(GD32F10X_CL) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
__REMAP_NAME__(SPI2_REMAP) /*!< SPI2 remapping(only for GD32F10X_CL devices) */
#endif /* GD32F10X_CL||GD32F10X_HD */
#if (defined(GD32F10X_CL) || defined(GD32F10X_HD))
__REMAP_NAME__(TIMER4CH3_IREMAP) /*!< TIMER4 channel3 internal remapping(only for GD32F10X_CL devices and GD32F10X_HD devices) */
#endif /* GD32F10X_CL||GD32F10X_HD */
#if (defined(GD32F10X_MD) || defined(GD32F10X_HD) || defined(GD32F10X_XD))
__REMAP_NAME__(ADC0_ETRGRT_REMAP) /*!< ADC0 external trigger routine conversion remapping(only for GD32F10X_MD devices), GD32F10X_HD devices and GD32F10X_XD devices) */
__REMAP_NAME__(ADC1_ETRGRT_REMAP) /*!< ADC1 external trigger routine conversion remapping(only for GD32F10X_MD devices), GD32F10X_HD devices and GD32F10X_XD devices) */
#endif /* GD32F10X_MD||GD32F10X_HD||GD32F10X_XD */
__REMAP_NAME__(SWJ_NONJTRST_REMAP) /*!< full SWJ(JTAG-DP + SW-DP)),but without NJTRST */
__REMAP_NAME__(SWJ_SWDPENABLE_REMAP) /*!< JTAG-DP disabled and SW-DP enabled */
__REMAP_NAME__(SWJ_DISABLE_REMAP) /*!< JTAG-DP disabled and SW-DP disabled */
#ifdef GD32F10X_CL
__REMAP_NAME__(CAN0_PARTIAL_REMAP) /*!< CAN0 partial remapping(only for GD32F10X_CL devices) */
__REMAP_NAME__(CAN0_FULL_REMAP) /*!< CAN0 full remapping(only for GD32F10X_CL devices) */
__REMAP_NAME__(ENET_REMAP) /*!< ENET remapping(only for GD32F10X_CL devices) */
__REMAP_NAME__(CAN1_REMAP) /*!< CAN1 remapping(only for GD32F10X_CL devices) */
__REMAP_NAME__(TIMER1ITI1_REMAP) /*!< TIMER1 internal trigger 1 remapping(only for GD32F10X_CL devices) */
__REMAP_NAME__(PTP_PPS_REMAP) /*!< ethernet PTP PPS remapping(only for GD32F10X_CL devices) */
#endif /* GD32F10X_CL */
#ifdef GD32F10X_XD
__REMAP_NAME__(TIMER8_REMAP) /*!< TIMER8 remapping */
__REMAP_NAME__(TIMER9_REMAP) /*!< TIMER9 remapping */
__REMAP_NAME__(TIMER10_REMAP) /*!< TIMER10 remapping */
__REMAP_NAME__(TIMER12_REMAP) /*!< TIMER12 remapping */
__REMAP_NAME__(TIMER13_REMAP) /*!< TIMER13 remapping */
__REMAP_NAME__(EXMC_NADV_REMAP) /*!< EXMC_NADV connect/disconnect */
#endif /* GD32F10X_XD */
5 changes: 4 additions & 1 deletion cores/arduino/gd32/pinmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,11 @@ void pin_function(PinName pin, int function)
#if defined(GD32F30x) || defined(GD32F10x)|| defined(GD32E50X)
gpio_init(gpio, GD_GPIO_MODE[mode], GD_GPIO_SPEED[speed], gd_pin);
if (remap != 0) {
// MSB is disable
bool disable = remap & ~(PIN_REMAP_MASK >> 1);
remap &= PIN_REMAP_MASK >> 1;
rcu_periph_clock_enable(RCU_AF);
gpio_pin_remap_config(GD_GPIO_REMAP[remap], ENABLE);
gpio_pin_remap_config(GD_GPIO_REMAP[remap], disable ? DISABLE : ENABLE);
}
#elif defined(GD32F3x0) || defined(GD32F1x0) || defined(GD32F4xx) || defined(GD32E23x)
uint32_t af = GD_PIN_AF_GET(function);
Expand Down
3 changes: 3 additions & 0 deletions cores/arduino/gd32/pins_arduino.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ extern "C" {
uint32_t PinName_to_digital(PinName p)
{
uint32_t i = DIGITAL_PINS_NUM;
p &= ~ALTMASK;
if (GD_PORT_GET(p) <= (PORTEND - 1)) {
for (i = 0; i < DIGITAL_PINS_NUM; i++) {
if (digital_pins[i] == p) {
Expand Down Expand Up @@ -62,6 +63,7 @@ bool pin_in_analog_pins(uint32_t pin)
bool ret = false;
#if ANALOG_PINS_NUM > 0
#ifndef ANALOG_PINS_LAST
pin &= ~ALTMASK;
ret = (pin >= A0) && (pin < (A0 + ANALOG_PINS_NUM));
#else
for (uint32_t i = 0; i < ANALOG_PINS_NUM; i++) {
Expand All @@ -80,6 +82,7 @@ uint32_t digital_pin_to_analog(uint32_t pin)
uint32_t ret = ANALOG_PINS_NUM;
#if ANALOG_PINS_NUM > 0
#ifndef ANALOG_PINS_LAST
pin &= ~ALTMASK;
ret = pin - A0;
#else
for (uint32_t i = 0; i < ANALOG_PINS_NUM; i++) {
Expand Down
Loading