Created
December 25, 2023 14:14
-
-
Save PL125/074752dd6625a67bcd650b195b33355e to your computer and use it in GitHub Desktop.
First tests on a RISC-V GD32VF103CBT6 (LILYGO T-Display)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifdef __cplusplus | |
| extern "C" { | |
| #endif | |
| #include "gd32vf103_rcu.h" | |
| #include "gd32vf103_gpio.h" | |
| #include "gd32vf103_usart.h" | |
| #ifdef __cplusplus | |
| } | |
| #endif | |
| #define R_LED_ON() gpio_bit_reset(GPIOC, GPIO_PIN_13) | |
| #define R_LED_OFF() gpio_bit_set(GPIOC, GPIO_PIN_13) | |
| #define G_LED_ON() gpio_bit_reset(GPIOA, GPIO_PIN_1) | |
| #define G_LED_OFF() gpio_bit_set(GPIOA, GPIO_PIN_1) | |
| #define B_LED_ON() gpio_bit_reset(GPIOA, GPIO_PIN_2) | |
| #define B_LED_OFF() gpio_bit_set(GPIOA, GPIO_PIN_2) | |
| void UART_Config() { | |
| rcu_periph_clock_enable(RCU_GPIOA); | |
| gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9); | |
| gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10); | |
| rcu_periph_clock_enable(RCU_USART0); | |
| usart_deinit(USART0); | |
| usart_baudrate_set(USART0, 115200U); | |
| usart_parity_config(USART0, USART_PM_NONE); | |
| usart_word_length_set(USART0, USART_WL_8BIT); | |
| usart_stop_bit_set(USART0, USART_STB_1BIT); | |
| usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE); | |
| usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE); | |
| usart_receive_config(USART0, USART_RECEIVE_ENABLE); | |
| usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); | |
| usart_enable(USART0); | |
| } | |
| void Serial_SendByte(const uint8_t Byte) { | |
| usart_data_transmit(USART0, Byte); | |
| while (usart_flag_get(USART0, USART_FLAG_TC) == RESET); | |
| } | |
| void Serial_SendString(const char *String) { | |
| while (*String != '\0') { | |
| Serial_SendByte(*String++); | |
| } | |
| } | |
| void Serial_Printf(const char *format, ...) { | |
| char String[100] = {0}; | |
| if ((format != nullptr) && (strlen(format) != 0) && (strlen(format) < sizeof(String))) { | |
| va_list arg; | |
| va_start(arg, format); | |
| /*char *String = nullptr; | |
| if (vasprintf(&String, format, arg) > 0) { | |
| Serial_SendString(String); | |
| } | |
| free(String);*/ | |
| vsprintf(String, format, arg); | |
| Serial_SendString(String); | |
| va_end(arg); | |
| } | |
| } | |
| void setup() { | |
| UART_Config(); | |
| rcu_periph_clock_enable(RCU_GPIOA); | |
| rcu_periph_clock_enable(RCU_GPIOC); | |
| gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1); | |
| gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2); | |
| gpio_init(GPIOC, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_13); | |
| R_LED_OFF(); | |
| G_LED_OFF(); | |
| B_LED_OFF(); | |
| Serial_Printf("\n\rStarting up\n\n\r"); | |
| Serial_Printf("Product id: %08X\r\n", | |
| (unsigned int) *(uint32_t*) FMC_PID); | |
| Serial_Printf("Unique device id: %08X:%08X:%08X\r\n", | |
| (unsigned int) *(uint32_t*) 0x1FFFF7E8, | |
| (unsigned int) *(uint32_t*) 0x1FFFF7EC, | |
| (unsigned int) *(uint32_t*) 0x1FFFF7F0); | |
| Serial_Printf("Flash density: %d Kbytes, sram density: %d Kbytes\r\n", | |
| (unsigned int) *(uint32_t*) 0x1FFFF7E0 & 0xFFFF, | |
| (unsigned int) *(uint32_t*) 0x1FFFF7E0 >> 16); | |
| Serial_Printf("CK_SYS: %lu Hz\r\n", | |
| rcu_clock_freq_get(CK_SYS)); | |
| Serial_Printf("CK_AHB: %lu Hz\r\n", | |
| rcu_clock_freq_get(CK_AHB)); | |
| Serial_Printf("CK_APB1: %lu Hz\r\n", | |
| rcu_clock_freq_get(CK_APB1)); | |
| Serial_Printf("CK_APB2: %lu Hz\r\n", | |
| rcu_clock_freq_get(CK_APB2)); | |
| } | |
| void loop() { | |
| auto delay = [](uint32_t delay = 0xFFFFFF) -> void | |
| { | |
| for (volatile uint32_t i = 0; i < delay; i++) { | |
| asm volatile("nop"); | |
| } | |
| }; | |
| R_LED_ON(); | |
| delay(); | |
| R_LED_OFF(); | |
| delay(0xFFFFF); | |
| G_LED_ON(); | |
| delay(); | |
| G_LED_OFF(); | |
| delay(0xFFFFF); | |
| B_LED_ON(); | |
| delay(); | |
| B_LED_OFF(); | |
| delay(0xFFFFF); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment