• 【MM32F5270开发板试用】四、SPI的驱动,先点个屏幕


    本篇文章来自极术社区与灵动组织的MM32F5270开发板评测活动,更多开发板试用活动请关注极术社区网站。作者:Magicoe是攻城狮

    对于MM32F5270在rt-thread上的SPI驱动添加一如既往的简单

    添加rtt component文件夹下关于spi的驱动框架文件,当然我们会用到spi去做SD卡的读取,所以呢spi_msd.c也得添加上来, 读卡的部分咱们下章再说

    rtconfig.h中添加宏定义

    #define RT_USING_SPI
    #define RT_USING_SPI_MSD // 如果需要SPI接SD卡
    
    #define BSP_USING_SPI
    #define BSP_USING_SPI1
    #define BSP_USING_SPI3
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    之后就是加入drv_spi.c啦,似乎spi的驱动比I2C长很多,这里就长话短说,具体可以参考其他xx32的bsp
    SPI的引脚 时钟 功能初始化必不可少

    数据收发函数咱也不能落下

    接下来是SPI数据收发和对应spi接口关联的结构体定义

    #if defined(BSP_USING_SPI1)
    static struct lpc_spi spi1 = 
    {
        .base = SPI1
    }; 
    
    static struct rt_spi_bus spi1_bus = 
    {
        .parent.user_data = &spi1
    }; 
    #endif
    
    
    #if defined(BSP_USING_SPI3)
    static struct lpc_spi spi3 = 
    {
        .base = SPI3
    }; 
    
    static struct rt_spi_bus spi3_bus = 
    {
        .parent.user_data = &spi3
    }; 
    #endif
    
    static struct rt_spi_ops lpc_spi_ops = 
    {
        .configure = spi_configure, 
        .xfer      = spixfer
    }; 
    
    static struct rt_spi_ops lpc_spi3_ops = 
    {
        .configure = spi_configure, 
        .xfer      = spixfer
    };
    
    • 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

    完成以上种种,基本上SPI在MM32F5270的rt-thread上的移植就算完成了,接下来咱们点个SPI的LCD屏幕测试下。
    /-------------------- 点个屏幕 --------------------/
    我的这块LCD屏幕驱动IC是ILI9341,2.4寸320x240的,有很多参考例程,咱们就不赘述了,列举简单的API 函数,自己丰富完善即可

    先说LCD相关的复位RES,片选CS,命令数据选择DC,背光BL,但都是用第二篇pin脚的那章介绍的内容来的,具体四个引脚配置如下,对应了pin的8/9/10/11,我自己排的,挺方便的

    #define LCD_RES_PIN           8
    #define LCD_CS_PIN            9
    #define LCD_DC_PIN            10
    #define LCD_BL_PIN            11
    
    • 1
    • 2
    • 3
    • 4

    lcd咱们用rt_hw_lcd_config函数挂载到device spi10上

    static int rt_hw_lcd_config(void)
    {
        spi_dev_lcd = (struct rt_spi_device *)rt_device_find("spi10");
    
        /* config spi */
        {
            struct rt_spi_configuration cfg;
            cfg.data_width = 8;
            cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
            cfg.max_hz = 50 * 1000 * 1000;
    
            rt_spi_configure(spi_dev_lcd, &cfg);
        }
    
        return RT_EOK;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    具体的读写LCD是命令的还是数据的,我就不多写了,就是控制个DC那个口。

    具体LCD的初始化用的这个函数 int rt_hw_spi_lcd_init(void)
    囊括了GPIO的初始化啥的,LCD寄存器的初始化,一般厂家给,怼着写就成

    其他画点,画线,画圆圈啥的API就参考rtt提供的一些例子吧,我懒,这个例子我塞了很多类似的API

    如果一切顺利,到这里编译下载运行,就可以看到LCD屏幕背光点亮,开始刷屏了。但是咱们没完,弄个烟花测测LCD
    说实话这个firework的源码我也忘了从哪里来的了,看头文件作者是LEGION,感谢他,希望是他。
    这里干脆附上所有的代码~

    /*
     * Copyright (c) 2006-2020, RT-Thread Development Team
     *
     * SPDX-License-Identifier: Apache-2.0
     *
     * Change Logs:
     * Date           Author       Notes
     * 2020-10-19     LEGION       the first version
     */
    
    #include "drv_spi_ili9341.h"
    #include "firework.h"
    #include "image.h"
    #include 
    
    #if 1
    struct fireWorks{
        uint16_t  *x;
        uint16_t  *y;
        uint16_t  y1;
        struct my_image *firework;
    };
    
    uint16_t color_t = 0x00;
    static void firework_init(struct fireWorks *firework, struct my_image *image1, struct my_circle *flower, struct my_image *image2, int i)
    {
    #if 0
        *firework->x = rand() % 145 + 5;
        *firework->y = rand() % 20  + 90;
        firework->y1 = rand() % 50;
    #else
        *firework->x = rand() % 220 + 5;
        *firework->y = rand() % 20  + 290;
        firework->y1 = rand() % 50;
    #endif    
        creat_Image(firework->firework, image1, 4 * i, 0, Square);
        creat_Image(flower, image2, 18, 18 + 36 * (i % 4), Circle);
        flower->r = 2;
    }
    
    
    static void firework_thread_entry(void *f0)
    {
        struct my_image  *image1   = image_init(40, 7, Square);
        get_image(image1, (uint16_t *)gImage_shoot);
        struct my_circle *s1 = image_init(1, 18, Circle);       //ь»¨
        struct fireWorks *firework = (struct fireWorks *)rt_malloc(sizeof(struct fireWorks)); //ь»¨µ¯
        firework->x = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->y = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->firework = image_init(3, 7, Square);
        firework->firework->x = firework->x;
        firework->firework->y = firework->y;
        s1->x = firework->x;
        s1->y = firework->y;
        uint16_t r1;
        int i;
        while(1)
        {
            i = rand() % 10;
            firework_init(firework, image1, s1, f0, i);
            r1 = rand() % 8 + 10;
            do{
                LCD_Image(firework->firework);
                rt_thread_mdelay(4 * i + 5);
                LCD_Fill(*firework->x, *firework->y, firework->firework->width, firework->firework->high, color_t);
                if(*firework->y == firework->y1)break;
            }while((*firework->y)--);
            do{
                LCD_Circle(s1);
                rt_thread_mdelay(5 * i + 20);
                s1->r++;
            }while(s1->r < r1);
            LCD_Fill_Circle(s1, color_t);
            rt_thread_mdelay(500 * i);
        }
    }
    
    static void firework_thread_entry1(void *f0)
    {
        struct my_image  *image1   = image_init(40, 7, Square);
        get_image(image1, (uint16_t *)gImage_shoot);
        struct my_circle *s1 = image_init(1, 18, Circle);       //ь»¨
        struct fireWorks *firework = (struct fireWorks *)rt_malloc(sizeof(struct fireWorks)); //ь»¨µ¯
        firework->x = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->y = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->firework = image_init(3, 7, Square);
        firework->firework->x = firework->x;
        firework->firework->y = firework->y;
        s1->x = firework->x;
        s1->y = firework->y;
        uint16_t r1;
        int i;
        while(1)
        {
            i = rand() % 10;
            firework_init(firework, image1, s1, f0, i);
            r1 = rand() % 8 + 10;
            do{
                LCD_Image(firework->firework);
                rt_thread_mdelay(8 * i + 5);
                LCD_Fill(*firework->x, *firework->y, firework->firework->width, firework->firework->high, color_t);
                if(*firework->y == firework->y1)break;
            }while((*firework->y)--);
            do{
                LCD_Circle(s1);
                rt_thread_mdelay(6 * i + 20);
                s1->r++;
            }while(s1->r < r1);
            LCD_Fill_Circle(s1, color_t);
            rt_thread_mdelay(300 * i);
        }
    }
    
    static void firework_thread_entry2(void *f0)
    {
        struct my_image  *image1   = image_init(40, 7, Square);
        get_image(image1, (uint16_t *)gImage_shoot);
        struct my_circle *s1 = image_init(1, 18, Circle);       //ь»¨
        struct fireWorks *firework = (struct fireWorks *)rt_malloc(sizeof(struct fireWorks)); //ь»¨µ¯
        firework->x = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->y = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->firework = image_init(3, 7, Square);
        firework->firework->x = firework->x;
        firework->firework->y = firework->y;
        s1->x = firework->x;
        s1->y = firework->y;
        uint16_t r1;
        int i;
        while(1)
        {
            i = rand() % 10;
            firework_init(firework, image1, s1, f0, i);
            r1 = rand() % 8 + 10;
            do{
                LCD_Image(firework->firework);
                rt_thread_mdelay(2 * i + 5);
                LCD_Fill(*firework->x, *firework->y, firework->firework->width, firework->firework->high, color_t);
                if(*firework->y == firework->y1)break;
            }while((*firework->y)--);
            do{
                LCD_Circle(s1);
                rt_thread_mdelay(4 * i + 20);
                s1->r++;
            }while(s1->r < r1);
            LCD_Fill_Circle(s1, color_t);
            rt_thread_mdelay(200 * i);
        }
    }
    
    
    static void firework_thread_entry3(void *f0)
    {
        struct my_image  *image1   = image_init(40, 7, Square);
        get_image(image1, (uint16_t *)gImage_shoot);
        struct my_circle *s1 = image_init(1, 18, Circle);       //ь»¨
        struct fireWorks *firework = (struct fireWorks *)rt_malloc(sizeof(struct fireWorks)); //ь»¨µ¯
        firework->x = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->y = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->firework = image_init(3, 7, Square);
        firework->firework->x = firework->x;
        firework->firework->y = firework->y;
        s1->x = firework->x;
        s1->y = firework->y;
        uint16_t r1;
        int i;
        while(1)
        {
            i = rand() % 8;
            firework_init(firework, image1, s1, f0, i);
            r1 = rand() % 8 + 10;
            do{
                LCD_Image(firework->firework);
                rt_thread_mdelay(15 * i + 5);
                LCD_Fill(*firework->x, *firework->y, firework->firework->width, firework->firework->high, color_t);
                if(*firework->y == firework->y1)break;
            }while((*firework->y)--);
            do{
                LCD_Circle(s1);
                rt_thread_mdelay(20 * i + 20);
                s1->r++;
            }while(s1->r < r1);
            LCD_Fill_Circle(s1, color_t);
            rt_thread_mdelay(350 * i);
        }
    }
    
    static void firework_thread_entry4(void *f0)
    {
        struct my_image  *image1   = image_init(40, 7, Square);
        get_image(image1, (uint16_t *)gImage_shoot);
        struct my_circle *s1 = image_init(1, 18, Circle);       //ь»¨
        struct fireWorks *firework = (struct fireWorks *)rt_malloc(sizeof(struct fireWorks)); //ь»¨µ¯
        firework->x = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->y = (uint16_t *)rt_malloc(sizeof(uint16_t));
        firework->firework = image_init(3, 7, Square);
        firework->firework->x = firework->x;
        firework->firework->y = firework->y;
        s1->x = firework->x;
        s1->y = firework->y;
        uint16_t r1;
        int i;
        while(1)
        {
            i = rand() % 8;
            firework_init(firework, image1, s1, f0, i);
            r1 = rand() % 8 + 10;
            do{
                LCD_Image(firework->firework);
                rt_thread_mdelay(5 * i + 5);
                LCD_Fill(*firework->x, *firework->y, firework->firework->width, firework->firework->high, color_t);
                if(*firework->y == firework->y1)break;
            }while((*firework->y)--);
            do{
                LCD_Circle(s1);
                rt_thread_mdelay(10 * i + 20);
                s1->r++;
            }while(s1->r < r1);
            LCD_Fill_Circle(s1, color_t);
            rt_kprintf("color_addr = %x\r\n", &color_t);
            rt_kprintf("color = %d\r\n", color_t);
    
            rt_thread_mdelay(250 * i);
        }
    }
    
    static int firework_Init(void)
    {
        struct my_image  *f0 = image_init(36, 144, Square);
        get_image(f0, (uint16_t *)gImage_flower0);
    #if 1
        rt_thread_t firework_thread = rt_thread_create("fk0",  firework_thread_entry,  f0, 1024, 5, 10);
        if (firework_thread != RT_NULL)
        {
            rt_thread_startup(firework_thread);
        }
    #endif
    #if 1
        rt_thread_t firework_thread1 = rt_thread_create("fk1", firework_thread_entry1, f0, 1024, 6, 10);
        if (firework_thread1 != RT_NULL)
        {
            rt_thread_startup(firework_thread1);
        }
    #endif
    #if 1
        rt_thread_t firework_thread2 = rt_thread_create("fk2", firework_thread_entry2, f0, 1024, 7, 10);
        if (firework_thread2 != RT_NULL)
        {
            rt_thread_startup(firework_thread2);
        }
    #endif
    #if 1
        rt_thread_t firework_thread3 = rt_thread_create("fk3", firework_thread_entry3, f0, 1024, 8, 10);
        if (firework_thread3 != RT_NULL)
        {
            rt_thread_startup(firework_thread3);
        }
    #endif
    #if 1
        rt_thread_t firework_thread4 = rt_thread_create("fk4", firework_thread_entry4, f0, 1024, 9, 10);
        if (firework_thread4 != RT_NULL)
        {
            rt_thread_startup(firework_thread4);
        }
    #endif
        LCD_Fill(0, 0, 240, 320, 0x00);
        return 0;
    }
    
    INIT_APP_EXPORT(firework_Init);
    #endif
    
    • 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

    编译下载看效果~ 点些

    【MM32F5270开发板试用】四、SPI的驱动,先点个屏幕 - 极术社区 - 连接开发者与智能计算生态​aijishu.com/a/1060000000349559

    祝贺 MM32F5270 一把

  • 相关阅读:
    声纹技术(四):声纹识别的工程部署
    微店构造订单页面,自动化抢票
    做一个最新版的淘宝客返利程序源码有多难?
    JAVA毕设项目社交的健身网课平台服务器端(java+VUE+Mybatis+Maven+Mysql)
    安排项目宣讲日程得到最多的宣讲场次
    【IoT-卫朋】智能硬件 | 产品按键设计
    【八】Linux成神之路
    二叉树实现表达式求值(C++)
    Java编程:给定字符串数组,找出各个字符串中出现的公共字符。
    Redis Cluster高可用集群原理
  • 原文地址:https://blog.csdn.net/weixin_47569031/article/details/127426789