本篇文章來自極術社區與靈動組織的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
之后就是加入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
};
#endifstatic 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 1rt_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 1rt_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 1rt_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 1rt_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 1rt_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);}
#endifLCD_Fill(0, 0, 240, 320, 0x00);return 0;
}INIT_APP_EXPORT(firework_Init);
#endif
編譯下載看效果~ 點些
【MM32F5270開發板試用】四、SPI的驅動,先點個屏幕 - 極術社區 - 連接開發者與智能計算生態?aijishu.com/a/1060000000349559
祝賀 MM32F5270 一把
總結
以上是生活随笔為你收集整理的【MM32F5270开发板试用】四、SPI的驱动,先点个屏幕的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。