1.多任务运行说明
此次测试是在之前MultiTimer的基础上增加ILI9341 TFT屏驱动,并同时运行led灯闪+串口打印+TFT屏显示三个任务,看起来像是同时运行的,实际上是利用了MultiTimer的功能进行时间片轮询操作。MultiTimer的移植可以参考我前面发过的一篇文章(链接)
2.TFT屏驱动移植
TFT屏驱动采用GDF310的硬件SPI,IO口接法如下;由于都是数据往屏幕写,没有涉及返回的数据,实际上PA6<--SPI0_MISO可以不接;另外就是注意PA5只连到了JP2的中间脚,如果接线到JP7或者JP8上的PA5,需要在JP2上街跳线帽;我的线是接到JP8的PA5脚的,因此用跳线帽将JP2的PA5与Dx相连;
PA5 SPI0_SCK
PA6 SPI0_MISO
PA7 SPI0_MOSI
PB5 LCD_CS
PB4 LCD_RST
PB3 LCD_D/C
驱动代码我参考了GD32F150R的SPI_TFT_LCD_Driver,运气比较好,拿过来小改了一下就可以跑了;以下是屏幕测试代码,完整代码已上传,可以通过此链接下载
void tft_test_func(void)
{
switch(tft_test.state)
{
case 0:
num_test2();
break;
case 1:
font_test2_1();
break;
case 2:
font_test2_2();
break;
case 3:
color_test2(BLUE,YELLOW,"Yellow");
break;
case 4:
color_test2(BLACK,WHITE,"White");
break;
case 5:
color_test2(WHITE,BLACK,"Black");
break;
case 6:
color_test2(WHITE,RED,"Red");
break;
case 7:
color_test2(BLUE,GREEN,"Green");
break;
case 8:
color_test2(YELLOW,BLUE,"Blue");
break;
case 9:
draw_test2();
break;
default:
break;
}
tft_test.state++;
if(tft_test.state >= 10)
{
tft_test.state = 0;
}
}
3.主函数部分
总共创建了4个任务,全部由MultiTimerYield()进行调度运行,主程序部分代码如下,可以看到液晶屏显示的任务是放到exampleTimer1Callback()中运行了,定时周期为2s。
#include <stdio.h>
#include "gd32f3x0.h"
#include "gd32f310k_start.h"
#include "systick.h"
#include "MultiTimer.h"
#include "app.h"
#include "hal_uart.h"
#include "tft_test.h"
#include "lcd_driver.h"
MultiTimer timer1;
MultiTimer timer2;
MultiTimer timer3;
MultiTimer timer4;
void exampleTimer1Callback(MultiTimer* timer, void *userData)
{
printf("exampleTimer1Callback-> %s.\r\n", (char*)userData);
MultiTimerStart(timer, 2000, exampleTimer1Callback, userData);
// gpio_bit_toggle(GPIOA, GPIO_PIN_8);
tft_test_func();
}
void exampleTimer2Callback(MultiTimer* timer, void *userData)
{
printf("exampleTimer2Callback-> %s.\r\n", (char*)userData);
}
void exampleTimer3Callback(MultiTimer* timer, void *userData)
{
printf("exampleTimer3Callback-> %s.\r\n", (char*)userData);
MultiTimerStart(timer, 4567, exampleTimer3Callback, userData);
}
void exampleTimer4Callback(MultiTimer* timer, void *userData)
{
MultiTimerStart(timer, 500, exampleTimer4Callback, userData);
gpio_bit_toggle(GPIOA, GPIO_PIN_8);
}
/*!
\brief main function
\param[in] none
\param[out] none
\retval none
*/
int main(void)
{
systick_config();
/* enable the LED GPIO clock */
rcu_periph_clock_enable(RCU_GPIOA);
rcu_periph_clock_enable(RCU_GPIOB);
/* configure led GPIO port */
gpio_mode_set(GPIOA, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_8);
gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_8);
gpio_bit_reset(GPIOA, GPIO_PIN_8);
/* initilize the com */
usart0_gpio_config();
usart0_config();
/* initialize lcd */
lcd_init();
MultiTimerInstall(PlatformTicksGetFunc);
MultiTimerStart(&timer1, 2000, exampleTimer1Callback, "2000ms CYCLE timer");
MultiTimerStart(&timer2, 5000, exampleTimer2Callback, "5000ms ONCE timer");
MultiTimerStart(&timer3, 3456, exampleTimer3Callback, "3456ms delay start, 4567ms CYCLE timer");
MultiTimerStart(&timer4, 1000, exampleTimer4Callback, "500ms CYCLE timer");
while(1){
MultiTimerYield();
}
}