准备工作
安装repo
-
创建repo安装目录。
mkdir ~/bin
-
下载repo
wget https://storage.googleapis.com/git-repo-downloads/repo -P ~/bin/
-
改变执行权限
chmod a+x ~/bin/repo
-
设置环境变量,在~/.bashrc文件的最后输入
export PATH=~/bin:$PATH和export REPO_URL=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/
-
重启shell
代码下载
#mkdir xr806_openharmony
#cd xr806_openharmony
#repo init -u https://gitee.com/openharmony/manifest.git -b OpenHarmony_1.0.1_release --no-repo-verify
#repo sync -c
#repo forall -c 'git lfs pull' #下载部分大容量二进制文件
下载xr806源码
从https://gitee.com/moldy-potato-chips/devboard_device_allwinner_xr806 下载device仓内容。
从https://gitee.com/moldy-potato-chips/devboard_vendor_allwinner_xr806 下载vendor仓内容。
目录结构
device仓目录
device/xradio/xr806
├── adapter # OpenHarmony接口适配
├── BUILD.gn # GN构建脚本
├── build.sh # 启动编译脚本
├── doc # 指导文档
├── libcopy.py # SDK编译脚本
├── liteos_m # 编译工具,编译选项定义
├── os # liteos接口适配
├── target_config.h # liteos系统裁剪
└── xr_skylark # SDK平台相关文件(内核驱动)
vendor仓目录
vendor/xradio
├── LICENSE
├── README.md
└── xr806
环境配置
安装必要的库和工具
sudo apt-get install build-essential gcc g++ make zlib* libffi-dev e2fsprogs pkg-config flex bison perl bc openssl libssl-dev libelf-dev libc6-dev-amd64 binutils binutils-dev libdwarf-dev u-boot-tools mtd-utils gcc-arm-linux-gnueabi cpio device-tree-compiler
安装Python3
python3 --version
sudo apt-get install python3.8
sudo apt update && sudo apt install software-properties-common
安装hb
python3 -m pip install --user ohos-build
vim ~/.bashrc
export PATH=~/.local/bin:$PATH
source ~/.bashrc
设置编译工具链
编译链工具推荐gcc-arm-none-eabi-10-2020-q4-major。(下载网站:
https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads)
修改device/xradio/xr806/liteos_m/config.gni中的board_toolchain_prefix为实际环境下的gcc路径,以存放在tools文件夹下为例。
board_toolchain_prefix = "~/tools/gcc-arm-none-eabi-10-2020-q4-major/bin/arm-none-eabi-"
修改device/xradio/xr806/xr_skylark/gcc.mk中的CC_DIR为实际环境下的gcc路径,以存放在tools文件夹下为例。
CC_DIR := ~/tools/gcc-arm-none-eabi-10-2020-q4-major/bin
工程配置
进入SDK目录。
cd device/xradio/xr806/xr_skylark/
复制配置文件。
cp project/demo/audio_demo/gcc/defconfig .config
使用图形化界面确认配置。
make menuconfig
执行make menuconfig后,按方向键选择save保存后,选择exist退出即可。
清除过程文件。
make build_clean
生成静态库已经自动生成头文件。
make lib -j2
返回根目录编译工程。
返回根目录。
cd -
选择厂商。
hb set #回车,并选择wifi_skylark
编译系统,后续文件修改不需要重复配置。
hb build -f
SPI驱动点阵数码管
数码管采用的是MAX7219点阵模块 4合一显示屏模块,购买链接:https://item.taobao.com/item.htm?spm=a1z09.2.0.0.6fd52e8dlTXLdZ&id=534578656397&_u=nkiuie8f98
引脚连接
1.VCC --- VCC
2.GND --- GND
3.DIN --- PB04
4.CS --- PB06
5.CLK --- PB07
main.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "ohos_init.h"
#include "driver/chip/hal_gpio.h"
#include "driver/chip/hal_spi.h"
#include "kernel/os/os.h"
#include "wifi_device.h"/* GPIO */
#define GPIO_OUTPUT_PORT GPIO_PORT_A/* spi */
#define DEMO_SPI_MCLK (48 * 1000 * 1000)
#define DEMO_SPI_CS_LEVEL 0
#define DEMO_SPI_PORT SPI0
#define DEMO_SPI_CS SPI_TCTRL_SS_SEL_SS0static OS_Thread_t g_led_thread;
static OS_Thread_t g_spi_thread1;
static OS_Thread_t g_spi_thread2;static void gpio_output_init(void)
{GPIO_InitParam param;param.driving = GPIO_DRIVING_LEVEL_1;param.mode = GPIOx_Pn_F1_OUTPUT;param.pull = GPIO_PULL_NONE;HAL_GPIO_Init(GPIO_OUTPUT_PORT, GPIO_PIN_21, ¶m); //led灯对应IO
}/** buf按十六进制输出*/
static void hexdump(unsigned char *buf, unsigned int num)
{unsigned int i = 0;for (; i < num; i++){printf("%02X ", buf[i]);if ((i + 1) % 5 == 0)printf("\n");}printf("\r\n");
}static void gpio_output_ctl(uint8_t level)
{HAL_GPIO_WritePin(GPIO_OUTPUT_PORT, GPIO_PIN_21, level ? GPIO_PIN_HIGH : GPIO_PIN_LOW);
}static int spi_init(void)
{SPI_Config spi_Config;HAL_Status ret = HAL_OK;SPI_Global_Config spi_param;spi_param.cs_level = DEMO_SPI_CS_LEVEL;spi_param.mclk = DEMO_SPI_MCLK;HAL_SPI_Init(DEMO_SPI_PORT, &spi_param);spi_Config.firstBit = SPI_TCTRL_FBS_MSB;spi_Config.mode = SPI_CTRL_MODE_MASTER;spi_Config.opMode = SPI_OPERATION_MODE_POLL;spi_Config.sclk = 4000000;spi_Config.sclkMode = SPI_SCLK_Mode0;printf("spi init...\n");ret = HAL_SPI_Open(DEMO_SPI_PORT, DEMO_SPI_CS, &spi_Config, 5000);if (ret != HAL_OK){printf("spi open failed");return ret;}HAL_SPI_Config(DEMO_SPI_PORT, SPI_ATTRIBUTION_IO_MODE, SPI_IO_MODE_NORMAL);HAL_SPI_CS(DEMO_SPI_PORT, 1);return ret;
}static int spi_deinit(void)
{HAL_Status ret = HAL_OK;printf("spi deinit...\n");HAL_SPI_CS(DEMO_SPI_PORT, 0);HAL_SPI_Close(DEMO_SPI_PORT);return ret;
}static void LedThread(void *arg)
{gpio_output_init();while(1){gpio_output_ctl(1);OS_Sleep(1);gpio_output_ctl(0);OS_Sleep(1);}
}static void SpiThread1(void *arg)
{HAL_Status ret = HAL_OK;uint8_t cmd[] = {0xFF, 0x11, 0x22, 0x33, 0x00};uint8_t data[5];printf("spi demo started.\n");if (HAL_OK != spi_init()){printf("spi init failed.\n");return;}while(1){cmd[4] += 1;printf("spi write...\n");ret = HAL_SPI_TransmitReceive(DEMO_SPI_PORT, cmd, data, 5);if (ret != HAL_OK){printf("spi write failed");}printf("spi read...\n");hexdump(data,5);OS_Sleep(1);}if (HAL_OK != spi_deinit()){printf("spi deinit failed.\n");return;}
}unsigned char disp1[20][8]=
{
#if 0{0x3C,0x42,0x42,0x42,0x42,0x42,0x42,0x3C}, //0{0x10,0x30,0x50,0x10,0x10,0x10,0x10,0x38}, //1{0x7E,0x2,0x2,0x7E,0x40,0x40,0x40,0x7E}, //2{0x3E,0x2,0x2,0x3E,0x2,0x2,0x3E,0x0}, //3{0x8,0x18,0x28,0x48,0xFE,0x8,0x8,0x00}, //4{0x0,0x3C,0x20,0x20,0x3C,0x4,0x4,0x3C}, //5{0x0,0x3C,0x20,0x20,0x3C,0x24,0x24,0x3C}, //6{0x0,0x3E,0x22,0x4, 0x8, 0x8, 0x8, 0x8}, //7{0x0,0x3E,0x22,0x22,0x3E,0x22,0x22,0x3E}, //8{0x3E,0x22,0x22,0x3E,0x2,0x2,0x3E,0x00}, //9{0x0,0x8,0x14,0x22,0x3E,0x22,0x22,0x22}, //A{0x0,0x3C,0x22,0x22,0x3E,0x22,0x22,0x3C}, //B{0x0,0x3C,0x40,0x40,0x40,0x40,0x40,0x3C}, //C{0x0,0x7C,0x42,0x42,0x42,0x42,0x42,0x7C}, //D{0x7C,0x40,0x40,0x7C,0x40,0x40,0x7c,0x00}, //E{0x00,0x7C,0x40,0x40,0x7C,0x40,0x40,0x40}, //F{0x00,0x3C,0x40,0x40,0x40,0x44,0x44,0x3C}, //G{0x00,0x44,0x44,0x44,0x7C,0x44,0x44,0x44}, //H{0x00,0x10,0xFE,0x92,0x92,0xFE,0x10,0x10}, //中{0xFE,0xBA,0x92,0xBA,0x92,0x9A,0xBA,0xFE}, //国#else{0x3C,0x40,0x40,0x38,0x04,0x04,0x78,0x00}, //S{0x7C,0x10,0x10,0x10,0x10,0x10,0x10,0x00}, //T{0x10,0x10,0x28,0x28,0x38,0x6c,0x44,0x00}, //A{0x78,0x44,0x44,0x78,0x48,0x44,0x42,0x00}, //R
#endif
};#define Max7219_pinCS_Set() HAL_SPI_CS(DEMO_SPI_PORT, 1)
#define Max7219_pinCS_Clr() HAL_SPI_CS(DEMO_SPI_PORT, 0);void Write_Max7219_byte(unsigned char data)
{HAL_Status ret = HAL_OK;unsigned char rdata;ret = HAL_SPI_TransmitReceive(DEMO_SPI_PORT, &data, &rdata, 1);if (ret != HAL_OK){printf("spi write failed");}
}void Init_MAX7219(void)
{int i = 0;//译码方式:BCD码Max7219_pinCS_Clr();for(i = 0; i < 4; i++){Write_Max7219_byte(0x09);Write_Max7219_byte(0x00);}Max7219_pinCS_Set();//亮度Max7219_pinCS_Clr();for(i = 0; i < 4; i++){Write_Max7219_byte(0x0a);Write_Max7219_byte(0x03);}Max7219_pinCS_Set();//扫描界限;8个数码管显示Max7219_pinCS_Clr();for(i = 0; i < 4; i++){Write_Max7219_byte(0x0b);Write_Max7219_byte(0x07);}Max7219_pinCS_Set();//掉电模式:0,普通模式:1Max7219_pinCS_Clr();for(i = 0; i < 4; i++){Write_Max7219_byte(0x0c);Write_Max7219_byte(0x01);}Max7219_pinCS_Set();//显示测试:1;测试结束,正常显示:0Max7219_pinCS_Clr();for(i = 0; i < 4; i++){Write_Max7219_byte(0x0f);Write_Max7219_byte(0x00);}Max7219_pinCS_Set();
}static void SpiThread2(void *arg)
{printf("spi demo2 started.\n");int i, j;if (HAL_OK != spi_init()){printf("spi init failed.\n");return;}Init_MAX7219();while(1){for(i = 1; i < 9; i++){Max7219_pinCS_Clr();for(j = 0; j < 4; j++){Write_Max7219_byte(i); //写入地址,即数码管编号Write_Max7219_byte(disp1[j][i-1]); //写入数据,即数码管显示数字}Max7219_pinCS_Set();}OS_Sleep(1);}if (HAL_OK != spi_deinit()){printf("spi deinit failed.\n");return;}
}void SPITestMain(void)
{/*led flash test*/if (OS_ThreadCreate(&g_led_thread, "LedThread", LedThread, NULL,OS_THREAD_PRIO_APP, 2 * 1024) != OS_OK){printf("[ERR] Create LedThread Failed\n");}/*spi send recieve test*//*if (OS_ThreadCreate(&g_spi_thread1, "SpiThread1", SpiThread1, NULL,OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK){printf("[ERR] Create SpiThread Failed\n");}*//*spi led test*/if (OS_ThreadCreate(&g_spi_thread2, "SpiThread2", SpiThread2, NULL,OS_THREAD_PRIO_APP, 4 * 1024) != OS_OK){printf("[ERR] Create SpiThread Failed\n");}}SYS_RUN(SPITestMain);
实验结果
参考
- https://aijishu.com/a/1060000000288328
- https://aijishu.com/a/1060000000256653
- https://aijishu.com/a/1060000000256631