当前位置:   article > 正文

【XR806开发板试用】SPI驱动数码管显示_spi_tctrl_fbs_msb

spi_tctrl_fbs_msb

【XR806开发板试用】SPI驱动数码管显示

准备工作

安装repo
  • 创建repo安装目录。

    mkdir ~/bin
    
    • 1
  • 下载repo

    wget https://storage.googleapis.com/git-repo-downloads/repo -P ~/bin/
    
    • 1
  • 改变执行权限

    chmod a+x ~/bin/repo
    
    • 1
  • 设置环境变量,在~/.bashrc文件的最后输入

    export PATH=~/bin:$PATH和export REPO_URL=https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/
    
    • 1
  • 重启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'   #下载部分大容量二进制文件
  • 1
  • 2
  • 3
  • 4
  • 5

下载xr806源码

从https://gitee.com/moldy-potato-chips/devboard_device_allwinner_xr806 下载device仓内容。
从https://gitee.com/moldy-potato-chips/devboard_vendor_allwinner_xr806 下载vendor仓内容。
  • 1
  • 2
目录结构

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平台相关文件(内核驱动)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

vendor仓目录

vendor/xradio
├── LICENSE
├── README.md
└── xr806
  • 1
  • 2
  • 3
  • 4

环境配置

安装必要的库和工具
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
  • 1
安装Python3
python3 --version
sudo apt-get install python3.8
sudo apt update && sudo apt install software-properties-common
  • 1
  • 2
  • 3

安装hb

python3 -m pip install --user ohos-build
vim ~/.bashrc
export PATH=~/.local/bin:$PATH
source ~/.bashrc
  • 1
  • 2
  • 3
  • 4

设置编译工具链

编译链工具推荐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-"
  • 1

修改device/xradio/xr806/xr_skylark/gcc.mk中的CC_DIR为实际环境下的gcc路径,以存放在tools文件夹下为例。

CC_DIR := ~/tools/gcc-arm-none-eabi-10-2020-q4-major/bin
  • 1

工程配置

进入SDK目录。

cd device/xradio/xr806/xr_skylark/
  • 1

复制配置文件。

cp project/demo/audio_demo/gcc/defconfig .config
  • 1

使用图形化界面确认配置。

make menuconfig
  • 1

执行make menuconfig后,按方向键选择save保存后,选择exist退出即可。

清除过程文件。

make build_clean
  • 1

生成静态库已经自动生成头文件。

make lib -j2
  • 1

返回根目录编译工程。

返回根目录。

cd - 
  • 1

选择厂商。

hb set  #回车,并选择wifi_skylark
  • 1

编译系统,后续文件修改不需要重复配置。

hb build -f
  • 1

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
  • 1
  • 2
  • 3
  • 4
  • 5

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_SS0

static 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, &param);   //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,普通模式:1
    Max7219_pinCS_Clr();
    for(i = 0; i < 4; i++)
    {
        Write_Max7219_byte(0x0c);
        Write_Max7219_byte(0x01);
    }
    Max7219_pinCS_Set();
    //显示测试:1;测试结束,正常显示:0
    Max7219_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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 227
  • 228
  • 229
  • 230
  • 231
  • 232
  • 233
  • 234
  • 235
  • 236
  • 237
  • 238
  • 239
  • 240
  • 241
  • 242
  • 243
  • 244
  • 245
  • 246
  • 247
  • 248
  • 249
  • 250
  • 251
  • 252
  • 253
  • 254
  • 255
  • 256
  • 257
  • 258
  • 259
  • 260
  • 261
  • 262
  • 263
  • 264
  • 265
  • 266
  • 267
  • 268
  • 269
  • 270
  • 271
  • 272
  • 273
  • 274
  • 275
  • 276
  • 277
  • 278
  • 279
  • 280
  • 281
  • 282
  • 283
  • 284
  • 285
  • 286
  • 287
  • 288
  • 289
  • 290
  • 291
  • 292
  • 293
  • 294
  • 295

实验结果

在这里插入图片描述

参考

  1. https://aijishu.com/a/1060000000288328
  2. https://aijishu.com/a/1060000000256653
  3. https://aijishu.com/a/1060000000256631
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/288292
推荐阅读
相关标签
  

闽ICP备14008679号