periph_cpu.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2020 Koen Zandberg <koen@bergzand.net>
3  * 2023 Gunar Schorcht <gunar@schorcht.net>
4  *
5  * This file is subject to the terms and conditions of the GNU Lesser General
6  * Public License v2.1. See the file LICENSE in the top level directory for more
7  * details.
8  */
9 
10 #pragma once
11 
23 #include <inttypes.h>
24 
25 #include "cpu.h"
26 #include "clic.h"
27 #include "kernel_defines.h"
28 #include "macros/units.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
41 #define PM_NUM_MODES (3U)
59 enum {
62  GD32V_PM_IDLE = 2
63 };
64 
68 #ifndef CONFIG_PM_EWUP_USED
69 #define CONFIG_PM_EWUP_USED (0U)
70 #endif
76 typedef enum {
77  AHB,
78  APB1,
79  APB2,
80 } bus_t;
81 
85 enum {
86 #ifdef GPIOA
87  PORT_A = 0,
88 #endif
89 #ifdef GPIOB
90  PORT_B = 1,
91 #endif
92 #ifdef GPIOC
93  PORT_C = 2,
94 #endif
95 #ifdef GPIOD
96  PORT_D = 3,
97 #endif
98 #ifdef GPIOE
99  PORT_E = 4,
100 #endif
101 };
102 
103 #ifndef DOXYGEN
108 #define HAVE_GPIO_T
109 typedef uint32_t gpio_t;
115 #define GPIO_UNDEF (0xffffffff)
116 
120 #define GPIO_PIN(x, y) ((GPIOA_BASE + (x << 10)) | y)
121 
130 #define GPIO_MODE(io, pr, ot) ((io << 0) | (pr << 2) | (ot << 4))
131 
138 #define HAVE_GPIO_MODE_T
139 typedef enum {
140  GPIO_IN = GPIO_MODE(0, 1, 0),
141  GPIO_IN_PD = GPIO_MODE(0, 2, 0),
142  GPIO_IN_PU = GPIO_MODE(0, 2, 1),
143  GPIO_OUT = GPIO_MODE(3, 0, 0),
144  GPIO_OD = GPIO_MODE(3, 1, 0),
145  GPIO_OD_PU = (0xff)
146 } gpio_mode_t;
153 #define HAVE_GPIO_FLANK_T
154 typedef enum {
155  GPIO_RISING = 1,
156  GPIO_FALLING = 2,
157  GPIO_BOTH = 3
158 } gpio_flank_t;
160 #endif /* ndef DOXYGEN */
161 
165 typedef enum {
168 } gpio_af_t;
169 
176 void gpio_init_af(gpio_t pin, gpio_af_t af);
177 
183 void gpio_init_analog(gpio_t pin);
184 
185 /* Hide this from Doxygen to avoid merging implementation details into
186  * public view on type */
187 #ifndef DOXYGEN
188 
189 #define HAVE_GPIO_STATE_T
190 typedef enum {
194  GPIO_INPUT,
197 } gpio_state_t;
198 
199 #define HAVE_GPIO_PULL_T
200 typedef enum {
202  GPIO_PULL_UP,
205 } gpio_pull_t;
206 
207 #define HAVE_GPIO_PULL_STRENGTH_T
208 typedef enum {
209  GPIO_PULL_WEAKEST = 0,
210  GPIO_PULL_WEAK = 0,
211  GPIO_PULL_STRONG = 0,
214 
215 #define HAVE_GPIO_DRIVE_STRENGTH_T
216 typedef enum {
217  GPIO_DRIVE_WEAKEST = 0,
218  GPIO_DRIVE_WEAK = 0,
219  GPIO_DRIVE_STRONG = 0,
222 
223 #define HAVE_GPIO_SLEW_T
224 typedef enum {
225  GPIO_SLEW_SLOWEST = 0,
226  GPIO_SLEW_SLOW = 1,
227  GPIO_SLEW_FAST = 2,
228  GPIO_SLEW_FASTEST = 2,
229 } gpio_slew_t;
230 
231 #define HAVE_GPIO_CONF_T
232 typedef union gpio_conf_gd32v gpio_conf_t;
233 
234 #endif /* !DOXYGEN */
235 
241  uint8_t bits;
242  struct {
274  bool initial_value : 1;
275  };
276 };
277 
281 #define ADC_DEVS (2U)
282 
286 typedef struct {
287  gpio_t pin;
288  uint8_t dev;
289  uint8_t chan;
290 } adc_conf_t;
291 
295 #define DAC_CHANNEL_NUMOF (2)
296 
300 typedef struct {
301  gpio_t pin;
302  uint8_t chan;
303 } dac_conf_t;
304 
308 #define TIMER_CHANNEL_NUMOF (4U)
309 
313 #define TIMER_CHANNEL(tim, chan) *(&dev(tim)->CH0CV + (chan * 2))
314 
318 typedef struct {
319  TIMER_Type *dev;
320  uint32_t max;
321  uint32_t rcu_mask;
322  uint8_t bus;
323  uint8_t irqn;
324 } timer_conf_t;
325 
329 typedef struct {
330  USART_Type *dev;
331  uint32_t rcu_mask;
332  gpio_t rx_pin;
333  gpio_t tx_pin;
334  uint8_t bus;
335  uint8_t irqn;
336 } uart_conf_t;
337 
341 #define UART_ISR_PRIO (2)
342 
349 #define SPI_HWCS_MASK (0xffffff00)
350 
357 #define SPI_HWCS(x) (SPI_HWCS_MASK | x)
358 
362 #define SPI_CS_UNDEF (GPIO_UNDEF)
363 
364 #ifndef DOXYGEN
369 #define HAVE_SPI_CS_T
370 typedef uint32_t spi_cs_t;
372 #endif
373 
379 #define PERIPH_SPI_NEEDS_TRANSFER_BYTE
381 #define PERIPH_SPI_NEEDS_TRANSFER_REG
383 #define PERIPH_SPI_NEEDS_TRANSFER_REGS
390 #define HAVE_SPI_CLK_T
391 enum {
397 };
398 
402 typedef uint32_t spi_clk_t;
408 typedef struct {
409  SPI_Type *dev;
410  gpio_t mosi_pin;
411  gpio_t miso_pin;
412  gpio_t sclk_pin;
413  spi_cs_t cs_pin;
414  uint32_t rcumask;
415  uint8_t apbbus;
416 #ifdef MODULE_PERIPH_DMA
417  dma_t tx_dma;
418  uint8_t tx_dma_chan;
419  dma_t rx_dma;
420  uint8_t rx_dma_chan;
421 #endif
422 } spi_conf_t;
423 
429 #define PERIPH_I2C_NEED_READ_REG
431 #define PERIPH_I2C_NEED_WRITE_REG
433 #define PERIPH_I2C_NEED_READ_REGS
435 #define PERIPH_I2C_NEED_WRITE_REGS
438 #ifndef DOXYGEN
443 #define HAVE_I2C_SPEED_T
444 typedef enum {
445  I2C_SPEED_LOW = KHZ(10),
446  I2C_SPEED_NORMAL = KHZ(100),
447  I2C_SPEED_FAST = KHZ(400),
448  I2C_SPEED_FAST_PLUS = MHZ(1),
449 } i2c_speed_t;
451 #endif /* ndef DOXYGEN */
452 
456 typedef struct {
457  I2C_Type *dev;
458  i2c_speed_t speed;
459  gpio_t scl_pin;
460  gpio_t sda_pin;
461  uint32_t rcu_mask;
463 } i2c_conf_t;
464 
468 typedef struct {
469  gpio_t pin;
470  uint8_t cc_chan;
471 } pwm_chan_t;
472 
476 typedef struct {
477  TIMER_Type *dev;
478  uint32_t rcu_mask;
479  uint32_t remap;
485  uint8_t bus;
486 } pwm_conf_t;
487 
492 #define NWDT_TIME_LOWER_LIMIT (1)
493 /* Ensure the internal "count" variable stays within the uint32 bounds.
494  This variable corresponds to max_time * RTC_FREQ / MS_PER_SEC. On fe310,
495  RTC_FREQ is 32768Hz. The 15 right shift is equivalent to a division by RTC_FREQ.
496  */
497 #define NWDT_TIME_UPPER_LIMIT ((UINT32_MAX >> 15) * MS_PER_SEC + 1)
503 #define WDT_INTR_PRIORITY (PLIC_NUM_PRIORITIES)
504 
508 #define WDT_HAS_STOP (0)
509 
514 #define RTT_DEV RTC
516 #define RTT_IRQ RTC_ALARM_IRQn
517 #define RTT_IRQ_PRIORITY (2)
518 #define RTT_MAX_VALUE (0xffffffff)
520 #define RTT_MIN_FREQUENCY (1U)
527 #define USBDEV_SET_ADDR_AFTER_STATUS 0
528 #define USBDEV_NUM_ENDPOINTS 4
537 void periph_clk_en(bus_t bus, uint32_t mask);
538 
545 void periph_clk_dis(bus_t bus, uint32_t mask);
546 
554 uint32_t periph_apb_clk(bus_t bus);
555 
562 void gpio_init_af(gpio_t pin, gpio_af_t af);
563 
564 void gd32vf103_clock_init(void);
565 void gd32v_enable_irc8(void);
566 void gd32v_disable_irc8(void);
567 
568 #ifdef __cplusplus
569 }
570 #endif
571 
@ PORT_B
port B
Definition: periph_cpu.h:47
@ PORT_C
port C
Definition: periph_cpu.h:48
@ PORT_E
port E
Definition: periph_cpu.h:50
@ PORT_A
port A
Definition: periph_cpu.h:46
@ PORT_D
port D
Definition: periph_cpu.h:49
gpio_flank_t
Definition: periph_cpu.h:179
GPIO_MODE
Definition: periph_cpu.h:148
@ GPIO_OUT
select GPIO MASK as output
Definition: periph_cpu.h:164
@ GPIO_IN
select GPIO MASK as input
Definition: periph_cpu.h:163
i2c_speed_t
Definition: periph_cpu.h:275
spi_clk_t
Definition: periph_cpu.h:351
void gpio_init_af(gpio_t pin, uint8_t sel, uint8_t over)
Configure an alternate function for the given pin.
gpio_t adc_conf_t
ADC configuration wrapper.
Definition: periph_cpu.h:336
RISCV CLIC interrupt controller definitions.
gpio_af_t
Override alternative GPIO mode options.
Definition: periph_cpu.h:165
@ GPIO_AF_OUT_OD
alternate function output - open-drain
Definition: periph_cpu.h:167
@ GPIO_AF_OUT_PP
alternate function output - push-pull
Definition: periph_cpu.h:166
uint32_t periph_apb_clk(bus_t bus)
Get the actual bus clock frequency for the APB buses.
#define TIMER_CHANNEL_NUMOF
GD32V timers have 4 capture-compare channels.
Definition: periph_cpu.h:308
void gpio_init_analog(gpio_t pin)
Configure the given pin to be used as ADC input.
void periph_clk_en(bus_t bus, uint32_t mask)
Enable the given peripheral clock.
@ GD32V_PM_IDLE
IDLE mode.
Definition: periph_cpu.h:62
@ GD32V_PM_STANDBY
STANDBY mode,
Definition: periph_cpu.h:60
@ GD32V_PM_DEEPSLEEP
DEEPSLEEP mode, corresponds to STOP mode of STM32.
Definition: periph_cpu.h:61
void periph_clk_dis(bus_t bus, uint32_t mask)
Disable the given peripheral clock.
bus_t
On-Chip buses.
Definition: periph_cpu.h:76
@ APB1
Advanced Peripheral Bus 1
Definition: periph_cpu.h:78
@ AHB
Advanced High-performance Bus.
Definition: periph_cpu.h:77
@ APB2
Advanced Peripheral Bus 2
Definition: periph_cpu.h:79
enum IRQn IRQn_Type
Interrupt Number Definition.
gpio_mode_t
Available pin modes.
Definition: periph_cpu.h:91
gpio_pull_t
Enumeration of pull resistor configurations.
Definition: gpio_ll.h:257
gpio_pull_strength_t
Enumeration of pull resistor values.
Definition: gpio_ll.h:275
gpio_state_t
Enumeration of GPIO states (direction)
Definition: gpio_ll.h:165
gpio_slew_t
Enumeration of slew rate settings.
Definition: gpio_ll.h:339
gpio_drive_strength_t
Enumeration of drive strength options.
Definition: gpio_ll.h:306
typedef gpio_conf_t
GPIO pin configuration.
Definition: gpio_ll.h:423
@ GPIO_FLOATING
No pull ups nor pull downs enabled.
Definition: gpio_ll.h:258
@ GPIO_PULL_KEEP
Keep the signal at current logic level with pull up/down resistors.
Definition: gpio_ll.h:261
@ GPIO_PULL_DOWN
Pull down resistor enabled.
Definition: gpio_ll.h:260
@ GPIO_PULL_UP
Pull up resistor enabled.
Definition: gpio_ll.h:259
@ GPIO_PULL_WEAKEST
Use the weakest (highest Ohm value) resistor.
Definition: gpio_ll.h:276
@ GPIO_PULL_WEAK
Use a weak pull resistor.
Definition: gpio_ll.h:277
@ GPIO_PULL_STRONG
Use a strong pull resistor.
Definition: gpio_ll.h:278
@ GPIO_PULL_STRONGEST
Use the strongest pull resistor.
Definition: gpio_ll.h:279
@ GPIO_OUTPUT_OPEN_SOURCE
Use pin as output in open emitter configuration.
Definition: gpio_ll.h:202
@ GPIO_USED_BY_PERIPHERAL
The GPIO pin is used by a peripheral.
Definition: gpio_ll.h:221
@ GPIO_OUTPUT_OPEN_DRAIN
Use pin as output in open collector configuration.
Definition: gpio_ll.h:189
@ GPIO_OUTPUT_PUSH_PULL
Use pin as output in push-pull configuration.
Definition: gpio_ll.h:176
@ GPIO_DISCONNECT
Disconnect pin from all peripherals.
Definition: gpio_ll.h:249
@ GPIO_INPUT
Use pin as input.
Definition: gpio_ll.h:208
@ GPIO_SLEW_SLOWEST
let the output voltage level rise/fall as slow as possible
Definition: gpio_ll.h:340
@ GPIO_SLEW_FAST
let the output voltage level rise/fall fast
Definition: gpio_ll.h:343
@ GPIO_SLEW_SLOW
let the output voltage level rise/fall slowly
Definition: gpio_ll.h:342
@ GPIO_SLEW_FASTEST
let the output voltage level rise/fall as fast as possible
Definition: gpio_ll.h:344
@ GPIO_DRIVE_STRONG
Use a strong drive strength.
Definition: gpio_ll.h:309
@ GPIO_DRIVE_WEAK
Use a weak drive strength.
Definition: gpio_ll.h:308
@ GPIO_DRIVE_STRONGEST
Use the strongest drive strength.
Definition: gpio_ll.h:310
@ GPIO_DRIVE_WEAKEST
Use the weakest drive strength.
Definition: gpio_ll.h:307
unsigned int gpio_t
GPIO type identifier.
Definition: gpio.h:91
@ GPIO_FALLING
emit interrupt on falling flank
Definition: periph_cpu.h:109
@ GPIO_RISING
emit interrupt on rising flank
Definition: periph_cpu.h:110
@ GPIO_BOTH
emit interrupt on both flanks
Definition: periph_cpu.h:111
@ GPIO_OD
configure as output in open-drain mode without pull resistor
Definition: gpio.h:123
@ GPIO_IN_PU
configure as input with pull-up resistor
Definition: gpio.h:121
@ GPIO_OD_PU
configure as output in open-drain mode with pull resistor enabled
Definition: gpio.h:125
@ GPIO_IN_PD
configure as input with pull-down resistor
Definition: gpio.h:120
@ I2C_SPEED_NORMAL
normal mode: ~100 kbit/s
Definition: periph_cpu.h:277
@ I2C_SPEED_FAST_PLUS
fast plus mode: ~1000 kbit/s
Definition: periph_cpu.h:279
@ I2C_SPEED_LOW
low speed mode: ~10 kbit/s
Definition: periph_cpu.h:276
@ I2C_SPEED_FAST
fast mode: ~400 kbit/s
Definition: periph_cpu.h:278
gpio_t spi_cs_t
Chip select pin type overlaps with gpio_t so it can be casted to this.
Definition: spi.h:135
@ SPI_CLK_10MHZ
drive the SPI bus with 10MHz
Definition: periph_cpu.h:356
@ SPI_CLK_5MHZ
drive the SPI bus with 5MHz
Definition: periph_cpu.h:355
@ SPI_CLK_400KHZ
drive the SPI bus with 400KHz
Definition: periph_cpu.h:353
@ SPI_CLK_1MHZ
drive the SPI bus with 1MHz
Definition: periph_cpu.h:354
@ SPI_CLK_100KHZ
drive the SPI bus with 100KHz
Definition: periph_cpu.h:352
Adds include for missing inttype definitions.
Common macros and compiler attributes/pragmas configuration.
Native CPU header.
unsigned dma_t
DMA channel type.
uint8_t dev
ADCx - 1 device used for the channel.
Definition: periph_cpu.h:288
gpio_t pin
pin connected to the channel
Definition: periph_cpu.h:287
uint8_t chan
CPU ADC channel connected to the pin.
Definition: periph_cpu.h:289
DAC line configuration data.
Definition: periph_cpu.h:300
uint8_t chan
DAC device used for this line.
Definition: periph_cpu.h:302
gpio_t pin
pin connected to the line
Definition: periph_cpu.h:301
I2C configuration structure.
Definition: periph_cpu.h:298
uint32_t rcu_mask
bit in clock enable register
Definition: periph_cpu.h:461
IRQn_Type irqn
I2C event interrupt number.
Definition: periph_cpu.h:462
I2C_Type * dev
i2c device
Definition: periph_cpu.h:457
PWM channel.
Definition: periph_cpu.h:468
gpio_t pin
GPIO pin mapped to this channel.
Definition: periph_cpu.h:469
uint8_t cc_chan
capture compare channel used
Definition: periph_cpu.h:470
PWM device configuration.
uint32_t rcu_mask
bit in clock enable register
Definition: periph_cpu.h:478
uint32_t remap
AFIO remap mask to route periph to other pins (or zero, if not needed)
Definition: periph_cpu.h:479
gpio_af_t af
alternate function used
Definition: periph_cpu.h:484
TIMER_Type * dev
Timer used.
Definition: periph_cpu.h:477
uint8_t bus
APB bus.
Definition: periph_cpu.h:485
SPI device configuration.
Definition: periph_cpu.h:336
uint32_t rcumask
bit in the RCC peripheral enable register
Definition: periph_cpu.h:414
gpio_t sclk_pin
SCLK pin.
Definition: periph_cpu.h:412
uint8_t apbbus
APBx bus the device is connected to.
Definition: periph_cpu.h:415
SPI_Type * dev
SPI device base register address.
Definition: periph_cpu.h:409
Timer device configuration.
Definition: periph_cpu.h:263
uint8_t irqn
global IRQ channel
Definition: periph_cpu.h:323
uint8_t bus
APBx bus the timer is clock from.
Definition: periph_cpu.h:322
TIMER_Type * dev
timer device
Definition: periph_cpu.h:319
uint32_t max
maximum value to count to (16/32 bit)
Definition: periph_cpu.h:320
uint32_t rcu_mask
corresponding bit in the RCC register
Definition: periph_cpu.h:321
UART device configuration.
Definition: periph_cpu.h:217
USART_Type * dev
UART device base register address.
Definition: periph_cpu.h:330
uint8_t irqn
IRQ channel.
Definition: periph_cpu.h:335
uint32_t rcu_mask
bit in clock enable register
Definition: periph_cpu.h:331
uint8_t bus
APB bus.
Definition: periph_cpu.h:334
GPIO pin configuration for GD32V MCUs.
Definition: periph_cpu.h:240
gpio_pull_t pull
Pull resistor configuration.
Definition: periph_cpu.h:250
gpio_slew_t slew_rate
Configure the slew rate of outputs.
Definition: periph_cpu.h:260
bool initial_value
Initial value of the output.
Definition: periph_cpu.h:274
uint8_t bits
the raw bits
Definition: periph_cpu.h:241
gpio_state_t state
State of the pin.
Definition: periph_cpu.h:246
Unit helper macros.
#define MHZ(x)
A macro to return the Hz in x MHz.
Definition: units.h:48
#define KHZ(x)
A macro to return the Hz in x kHz.
Definition: units.h:43