To use either the LED1 or LED2 lamp we need to add a wire to connect them to pin PD0. We use code that toggles hardware GPIO pin 0 of chip PortD.
The WCH-Link dongle provides enough 3volt power for our board. Our SWDIO programming pin connects to pin PD1 on the board. We are looking at the back of the ELink dongle.
Embeetle is one of those programs that you leave in its folder and open. Find the application, right-click and select open, run or execute.
This manages projects, preferences, settings and toolchains for any processors you want to program.
Click the button to CREATE Generate project. Embeetle is ready to offer you many common example programs.
We want the sample package ch32v003f4p6-evt-r0-1v1 for the WCH CH32V003F4P6 Chip. You can find more programs online under the name EVT.
On first run it will take a while to download the compiler toolchain, board configuration files and sample software. There will be progress bars during the download.
The Embeetle Studio IDE screen should open. Use the FileTree panel to locate our example files. Click source/Samples/GPIO/GPIO_Toggle/User/main.c and our editor will open the project ready to build or clean.
This is a good point to explore menu options, commands, parameters, etc.
Click BuildA new window opens showing build progress. You should read through to see what the toolchain has done with the source files and where the elf executable file is located. This is a successful build using a compiler called Xpack.
This ch32v003 program is different from Arduino and uses a software called make and outputs rv32ec risc-v code. Scroll output to the top, it tells us where the output is located.
This command will upload to our board. The red/orange text tells us about the steps in the upload. This is a successful upload.
The code is clear enough for us to try different blink delays, change IO pins.
This sample project uses C/C++ code in several modular files. If you've only programmed Arduino you will see the code is both similar and different.
Embeetle uses the compiler directive #include to know which external files to add. main.c is the software module that selects GPIOD, pin0 as our output and it jumps to software routines located in the other modules.
Here is the original main.c from our examples download. It initializes a single GPIO pin then a while loop GPIO_WriteBit from SET to RESET.
/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2022/08/08
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/*
*@Note
GPIO routine:
PD0 push-pull output.
*/
#include "debug.h"
/* Global define */
/* Global Variable */
/*********************************************************************
* @fn GPIO_Toggle_INIT
*
* @brief Initializes GPIOA.0
*
* @return none
*/
void GPIO_Toggle_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
u8 i = 0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
Delay_Init();
USART_Printf_Init(115200);
printf("SystemClk:%d\r\n", SystemCoreClock);
printf("GPIO Toggle TEST\r\n");
GPIO_Toggle_INIT();
while(1)
{
Delay_Ms(250);
GPIO_WriteBit(GPIOD, GPIO_Pin_0, (i == 0) ? (i = Bit_SET) : (i = Bit_RESET));
}
}
This code initializes the pin. Modify to select which pin will toggle.
This line delays the toggle to 250ms. Also change for different output pin or delay time.
The screens show a 'Warning' in yellow. The compiler is saying it needs to convert an integer value by casting. Look at line 58 in main.c. Red folders show their source files have not been spellchecked.
We have downloaded a folder named ch32v003f4p6-evt-r0-1v1 with sample code and libraries for the CH32V003 processor and the boards that use this chip. You can look at these files in Windows explorer.
Menu File->Open File and locate your folder. It probably downloaded to your home directory.
Open folders until you have located /ch32v003f4p6-evt-r0-1v1/source/Samples/GPIO. All of these projects can be run in Embeetle IDE when it has the GNU RISCV Xpack toolchain.
Open main.c file with a text editor like notepad. Embeetle will combine these files to make an.elf executable file that we will upload to our board.
Like MicroChip Studio, MPLab or STM32Cube IDE we generate several output files. Application.bin, Application.hex and Appliction.elf are all compiled binary executables and may be able to run your board. The elf format adds labels that make debugging easier.
Look in the folder ../ch32v003f4p6-evt-r0-1v1/build for the .elf file. This is the program file that we will upload to our board. We can do this either separately or with the Embeetle flash command.
DashboardOne stop for hardware and software. These are pulldown menus. Our chip has half the flash program storage of an Arduino Uno with the same RAM. Probe Device shows settings and not status of connected device.
Embeetle Homepage shows what toolchains and build systems are on your machine. This is a computer that also has Arduino build software installed.
GNU RISCV Xpack toolchain is around 1GByte of software, make sure you have room. Once installed you can use the toolchain with other IDEs.
Dongle DriverA driver installed in my Windows PC whenI installed WCH-Link Utility. If you experience difficulty connecting then check your Windows Device Manger.
Documentation says you can load the WCH-Link driver with Zadig. Linux may require you to add openocd package to your OS. More information on the WCH-LinkE debug probe/programmer.
This is a successful upload with Embeetle with a working dongle driver. Read the messages to learn how the software works or doesn't work.
These boards contain the same processor chip and run the same programs. Labels may say D1, DIO or SWDIO for the same pin. Some boards need you to add a jumper to connect the LED.
Comments