对于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
之后就是加入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
};
完成以上种种,基本上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
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;
}
具体的读写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 <stdlib.h>
#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
编译下载看效果~ 点些祝贺 MM32F5270 一把