C/C++ 开发
本章节包含以下部分,请按需阅读:
配置开发环境
请参考 安装和配置 Pico C/C++ 开发环境教程 下载安装 Pico VS Code。
示例程序
C/C++ 示例程序位于 示例程序包 的 examples\C 目录中。
| 示例程序 | 基础例程说明 | 依赖库 |
|---|---|---|
| 01_GUI | 液晶 GUI 显示程序 | - |
| 02_ES8311 | 开发板音频测试程序 | - |
| 03_FatFs | FAT 文件系统,SD 卡 支持 SPI/SDIO 通讯 | - |
01_GUI
【程序说明】
- 使用 SPI 与 液晶通讯,并通过 GUI 实现显示文本和图片等功能。
【硬件连接】
- 使用 USB 线把板子接入电脑
【代码分析】
底层硬件接口
我们对硬件操作进行了底层的封装,由于硬件平台不一样,内部的实现是不一样的,如果需要了解内部实现可以去对应的目录中查看,在 DEV_Config.c(.h) 可以看到很多定义,在目录:lib\Config
-
模块初始化与退出的处理
void DEV_Module_Init(void);
void DEV_Module_Exit(void);提示这里是处理使用液晶屏前与使用完之后一些 GPIO 的处理。
-
GPIO 读写
void DEV_Digital_Write(uint_16 Pin, uint_8 Value);
uint_8 DEV_Digital_Read(uint_16 Pin); -
SPI 写数据
void DEV_SPI_WriteByte(uint_8 Value);
上层应用
对于屏幕而言,如果需要进行画图、显示中英文字符、显示图片等怎么办,这些都是上层应用做的。这有很多小伙伴有问到一些图形的处理,我们这里提供了一些基本的功能 在如下的目录中可以找到 GUI,在目录:lib\GUI\GUI_Paint.c(.h)
在如下目录下是 GUI 依赖的字符字体,在目录:lib\Fonts
-
新建图像属性:新建一个图像属性,这个属性包括图像缓存的名称、宽度、高度、翻转角度、颜色
void Paint_NewImage(uint16_t *image, uint16_t Width, uint16_t Height, uint16_t Rotate, uint16_t Color)
参数:
image: 图像缓存的名称,实际上是一个指向图像缓存首地址的指针;
Width: 图像缓存的宽度;
Height: 图像缓存的高度;
Rotate: 图像的翻转的角度
Color: 图像的初始颜色; -
选择图像缓存:选择图像缓存,选择的目的是你可以创建多个图像属性,图像缓存可以存在多个,你可以选择你所创建的每一张图像
void Paint_SelectImage(uint8_t *image)
参数:
image: 图像缓存的名称,实际上是一个指向图像缓存首地址的指针; -
图像旋转:设置选择好的图像的旋转角度,最好使用在
Paint_SelectImage()后,可以选择旋转 0、90、180、270 度void Paint_SetRotate(uint16_t Rotate)
参数:
Rotate: 图像选择角度,可以选择 ROTATE_0、ROTATE_90、ROTATE_180、ROTATE_270 分别对应 0、90、180、270 度提示不同选择角度下,坐标对应起始像素点不同,这里以 1.14 为例,四张图,按顺序为 0°, 90°, 180°, 270°。仅做为参考。

-
图像镜像翻转:设置选择好的图像的镜像翻转,可以选择不镜像、关于水平镜像、关于垂直镜像、关于图像中心镜像。
void Paint_SetMirroring(uint8_t mirror)
参数:
mirror: 图像的镜像方式,可以选择 MIRROR_NONE、MIRROR_HORIZONTAL、MIRROR_VERTICAL、MIRROR_ORIGIN 分别对应不镜像、关于水平镜像、关于垂直镜像、关于图像中心镜像 -
设置点在缓存中显示位置和颜色:这里是 GUI 最核心的一个函数、处理点在缓存中显示位置和颜色。
void Paint_SetPixel(uint16_t Xpoint, uint16_t Ypoint, uint16_t Color)
参数:
Xpoint: 点在图像缓存中 X 位置
Ypoint: 点在图像缓存中 Y 位置
Color: 点显示的颜色 -
图像缓存填充颜色:把图像缓存填充为某颜色,一般作为屏幕刷白的作用。
void Paint_Clear(uint16_t Color)
参数:
Color: 填充的颜色 -
图像缓存部分窗口填充颜色:把图像缓存的某部分窗口填充为某颜色,一般作为窗口刷白的作用,常用于时间的显示,刷白上一秒。
void Paint_ClearWindows(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend, uint16_t Color)
参数:
Xstart: 窗口的 X 起点坐标
Ystart: 窗口的 Y 起点坐标
Xend: 窗口的 X 终点坐标
Yend: 窗口的 Y 终点坐标
Color: 填充的颜色 -
画点:在图像缓存中,在(Xpoint, Ypoint)上画点,可以选择颜色,点的大小,点的风格
void Paint_DrawPoint(uint16_t Xpoint, uint16_t Ypoint, uint16_t Color, DOT_PIXEL Dot_Pixel, DOT_STYLE Dot_Style)
参数:
Xpoint: 点的 X 坐标
Ypoint: 点的 Y 坐标
Color: 填充的颜色
Dot_Pixel: 点的大小,提供默认的 8 种大小点
typedef enum {
DOT_PIXEL_1X1 = 1, // 1 x 1
DOT_PIXEL_2X2 , // 2 X 2
DOT_PIXEL_3X3 , // 3 X 3
DOT_PIXEL_4X4 , // 4 X 4
DOT_PIXEL_5X5 , // 5 X 5
DOT_PIXEL_6X6 , // 6 X 6
DOT_PIXEL_7X7 , // 7 X 7
DOT_PIXEL_8X8 , // 8 X 8
} DOT_PIXEL;
Dot_Style: 点的风格,大小扩充方式是以点为中心扩大还是以点为左下角往右上扩大
typedef enum {
DOT_FILL_AROUND = 1,
DOT_FILL_RIGHTUP,
} DOT_STYLE; -
画线:在图像缓存中,从 (Xstart, Ystart) 到 (Xend, Yend) 画线,可以选择颜色,线的宽度,线的风格
void Paint_DrawLine(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend, uint16_t Color, LINE_STYLE Line_Style , LINE_STYLE Line_Style)
参数:
Xstart: 线的 X 起点坐标
Ystart: 线的 Y 起点坐标
Xend: 线的 X 终点坐标
Yend: 线的 Y 终点坐标
Color: 填充的颜色
Line_width: 线的宽度,提供默认的 8 种宽度
typedef enum {
DOT_PIXEL_1X1 = 1, // 1 x 1
DOT_PIXEL_2X2 , // 2 X 2
DOT_PIXEL_3X3 , // 3 X 3
DOT_PIXEL_4X4 , // 4 X 4
DOT_PIXEL_5X5 , // 5 X 5
DOT_PIXEL_6X6 , // 6 X 6
DOT_PIXEL_7X7 , // 7 X 7
DOT_PIXEL_8X8 , // 8 X 8
} DOT_PIXEL;
Line_Style: 线的风格,选择线是以直线连接还是以虚线的方式连接
typedef enum {
LINE_STYLE_SOLID = 0,
LINE_STYLE_DOTTED,
} LINE_STYLE; -
画矩形:在图像缓存中,从 (Xstart, Ystart) 到 (Xend, Yend) 画一个矩形,可以选择颜色,线的宽度,是否填充矩形内部
void Paint_DrawRectangle(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend, uint16_t Color, DOT_PIXEL Line_width, DRAW_FILL Draw_Fill)
参数:
Xstart: 矩形的 X 起点坐标
Ystart: 矩形的 Y 起点坐标
Xend: 矩形的 X 终点坐标
Yend: 矩形的 Y 终点坐标
Color: 填充的颜色
Line_width: 矩形四边的宽度,提供默认的 8 种宽度
typedef enum {
DOT_PIXEL_1X1 = 1, // 1 x 1
DOT_PIXEL_2X2 , // 2 X 2
DOT_PIXEL_3X3 , // 3 X 3
DOT_PIXEL_4X4 , // 4 X 4
DOT_PIXEL_5X5 , // 5 X 5
DOT_PIXEL_6X6 , // 6 X 6
DOT_PIXEL_7X7 , // 7 X 7
DOT_PIXEL_8X8 , // 8 X 8
} DOT_PIXEL;
Draw_Fill: 填充,是否填充矩形的内部
typedef enum {
DRAW_FILL_EMPTY = 0,
DRAW_FILL_FULL,
} DRAW_FILL; -
画圆:在图像缓存中,以 (X_Center Y_Center) 为圆心,画一个半径为 Radius 的圆,可以选择颜色,线的宽度,是否填充圆内部
void Paint_DrawCircle(uint16_t X_Center, uint16_t Y_Center, uint16_t Radius, uint16_t Color, DOT_PIXEL Line_width, DRAW_FILL Draw_Fill)
参数:
X_Center: 圆心的 X 坐标
Y_Center: 圆心的 Y 坐标
Radius: 圆的半径
Color: 填充的颜色
Line_width: 圆弧的宽度,提供默认的 8 种宽度
typedef enum {
DOT_PIXEL_1X1 = 1, // 1 x 1
DOT_PIXEL_2X2 , // 2 X 2
DOT_PIXEL_3X3 , // 3 X 3
DOT_PIXEL_4X4 , // 4 X 4
DOT_PIXEL_5X5 , // 5 X 5
DOT_PIXEL_6X6 , // 6 X 6
DOT_PIXEL_7X7 , // 7 X 7
DOT_PIXEL_8X8 , // 8 X 8
} DOT_PIXEL;
Draw_Fill: 填充,是否填充圆的内部
typedef enum {
DRAW_FILL_EMPTY = 0,
DRAW_FILL_FULL,
} DRAW_FILL; -
写 Ascii 字符:在图像缓存中,在 (Xstart Ystart) 为左顶点,写一个 Ascii 字符,可以选择 Ascii 码可视字符字库、字体前景色、字体背景色
void Paint_DrawChar(uint16_t Xstart, uint16_t Ystart, const uint8_t Ascii_Char, sFONT* Font, uint16_t Color_Foreground, uint16_t Color_Background)
参数:
Xstart: 字符的左顶点 X 坐标
Ystart: 字体的左顶点 Y 坐标
Ascii_Char:Ascii 字符
Font: Ascii 码可视字符字库,在 Fonts 文件夹中提供了以下字体:
font8:5*8 的字体
font12:7*12 的字体
font16:11*16 的字体
font20:14*20 的字体
font24:17*24 的字体
Color_Foreground: 字体颜色
Color_Background: 背景颜色 -
写英文字符串:在图像缓存中,在 (Xstart Ystart) 为左顶点,写一串英文字符,可以选择 Ascii 码可视字符字库、字体前景色、字体背景色
void Paint_DrawString_EN(uint16_t Xstart, uint16_t Ystart, const uint8_t * pString, sFONT* Font, uint16_t Color_Foreground, uint16_t Color_Background)
参数:
Xstart: 字符的左顶点 X 坐标
Ystart: 字体的左顶点 Y 坐标
pString: 字符串,字符串是一个指针
Font: Ascii 码可视字符字库,在 Fonts 文件夹中提供了以下字体:
font8:5*8 的字体
font12:7*12 的字体
font16:11*16 的字体
font20:14*20 的字体
font24:17*24 的字体
Color_Foreground: 字体颜色
Color_Background: 背景颜色 -
写中文字符串:在图像缓存中,在 (Xstart Ystart) 为左顶点,写一串中文字符,可以选择 GB2312 编码字符字库、字体前景色、字体背景色
void Paint_DrawString_CN(uint16_t Xstart, uint16_t Ystart, const uint8_t * pString, cFONT* font, uint16_t Color_Foreground, uint16_t Color_Background)
参数:
Xstart: 字符的左顶点 X 坐标
Ystart: 字体的左顶点 Y 坐标
pString: 字符串,字符串是一个指针
Font: GB2312 编码字符字库,在 Fonts 文件夹中提供了以下字体:
font12CN:ascii 字符字体 11*21,中文字体 16*21
font24CN:ascii 字符字体 24*41,中文字体 32*41
Color_Foreground: 字体颜色
Color_Background: 背景颜色 -
写数字:在图像缓存中,在 (Xstart Ystart) 为左顶点,写一串数字,可以选择 Ascii 码可视字符字库、字体前景色、字体背景色
void Paint_DrawNum(uint16_t Xpoint, uint16_t Ypoint, uint32_t Nummber, sFONT* Font, uint16_t Digit,uint16_t Color_Foreground, uint16_t Color_Background);
参数:
Xstart: 字符的左顶点 X 坐标
Ystart: 字体的左顶点 Y 坐标
Nummber: 显示的数字,这里使用的是 32 位长的 int 型保存,可以最大显示到 2147483647
Font: Ascii 码可视字符字库,在 Fonts 文件夹中提供了以下字体:
font8:5*8 的字体
font12:7*12 的字体
font16:11*16 的字体
font20:14*20 的字体
font24:17*24 的字体
Digit: 显示小数点位数
Color_Foreground: 字体颜色
Color_Background: 背景颜色 -
显示时间:在图像缓存中,在 (Xstart Ystart) 为左顶点,显示一段时间,可以选择 Ascii 码可视字符字库、字体前景色、字体背景色;
void Paint_DrawTime(uint16_t Xstart, uint16_t Ystart, PAINT_TIME *pTime, sFONT* Font, uint16_t Color_Background, uint16_t Color_Foreground)
参数:
Xstart: 字符的左顶点 X 坐标
Ystart: 字体的左顶点 Y 坐标
pTime: 显示的时间,这里定义好了一个时间的结构体,只要把时分秒各位数传给参数;
Font: Ascii 码可视字符字库,在 Fonts 文件夹中提供了以下字体:
font8:5*8 的字体
font12:7*12 的字体
font16:11*16 的字体
font20:14*20 的字体
font24:17*24 的字体
Color_Foreground: 字体颜色
Color_Background: 背景颜色
【运行效果】
- 使用 VS Code 导入并编译 01_GUI 工程,编译完成后,烧录
build目录下 uf2 尾缀文件,或直接烧录firmware\C目录下 01_GUI.uf2 进行快速验证。
02_ES8311
【程序说明】
- 使用 PIO 模拟的 I2S 与 ES8311 通讯,实现音频的输入与输出。
【硬件连接】
- 连接喇叭
- 使用 USB 线把板子接入电脑
【代码分析】
Es8311_Init():初始化 ES8311。Es8311_Sample_Frequency_Config():配置采样率。Es8311_Microphone_Config():配置麦克风。Es8311_Microphone_Gain_Set():设置麦克风增益。Es8311_Voice_Volume_Set():设置音量。Sine_440hz_Out():输出 440 Hz 正弦波。Happy_Birthday_Out():生日快乐电子乐。Loopback_Test():录音播放测试。Music_Out():播放音乐。
【运行效果】
- 使用 VS Code 导入并编译 02_ES8311 工程,编译完成后,烧录
build目录下 uf2 尾缀文件,或直接烧录02_ES8311\uf2目录下 uf2 尾缀文件进行快速验证。
03_FatFs
【程序说明】
- 使用 SPI 或 PIO 模拟的 SDIO 与 SD 卡通讯,实现类似于 busybox 或 DOS 的命令行界面。
【硬件连接】
- 插入 SD 卡
- 使用 USB 线把板子接入电脑
【代码分析】
sd_init_driver():初始化 SD 卡驱动。getchar_timeout_us():获取串口输入。process_stdio():处理串口输入。
【运行效果】
-
使用 putty 或者 mobaxterm 等终端工具,打开开发板对应的 USB 串行端口
-
按 Enter 键启动命令行界面 (CLI)。您应该会看到类似这样的提示:
> -
输入 help 指令可以得到可用指令,如下
> help
setrtc <DD> <MM> <YY> <hh> <mm> <ss>:
Set Real Time Clock
Parameters: new date (DD MM YY) new time in 24-hour format (hh mm ss)
e.g.:setrtc 16 3 21 0 4 0
date:
Print current date and time
lliot <drive#>:
!DESTRUCTIVE! Low Level I/O Driver Test
e.g.: lliot 1
format [<drive#:>]:
Creates an FAT/exFAT volume on the logical drive.
e.g.: format 0:
mount [<drive#:>]:
Register the work area of the volume
e.g.: mount 0:
unmount <drive#:>:
Unregister the work area of the volume
chdrive <drive#:>:
Changes the current directory of the logical drive.
<path> Specifies the directory to be set as current directory.
e.g.: chdrive 1:
getfree [<drive#:>]:
Print the free space on drive
cd <path>:
Changes the current directory of the logical drive.
<path> Specifies the directory to be set as current directory.
e.g.: cd 1:/dir1
mkdir <path>:
Make a new directory.
<path> Specifies the name of the directory to be created.
e.g.: mkdir /dir1
ls:
List directory
cat <filename>:
Type file contents
simple:
Run simple FS tests
big_file_test <pathname> <size in bytes> <seed>:
Writes random data to file <pathname>.
<size in bytes> must be multiple of 512.
e.g.: big_file_test bf 1048576 1
or: big_file_test big3G-3 0xC0000000 3
cdef:
Create Disk and Example Files
Expects card to be already formatted and mounted
start_logger:
Start Data Log Demo
stop_logger:
Stop Data Log Demo