本帖最后由 sujingliang 于 2024-10-31 09:54 编辑
一、准备
例程中有一个SPI读写flash的例子:phy6222_v313_0512\example\peripheral\spiflash
拿它做模板修改下驱动LCD,copy一份spiflash到spilcd,加了一个LCD驱动文件,去掉了spiflash.c
2、OSAL初始化和事件处理函数
没做修改,还是利用spi_demo_ProcessEvent
- __ATTR_SECTION_SRAM__ const pTaskEventHandlerFn tasksArr[] =
- {
- LL_ProcessEvent,
- spi_demo_ProcessEvent,
- };
- __ATTR_SECTION_SRAM__ const uint8 tasksCnt = sizeof( tasksArr ) / sizeof( tasksArr[0] );
- uint16* tasksEvents;
复制代码 这里也是,还是用spi_demo_Init来初始化task
- void osalInitTasks( void )
- {
- uint8 taskID = 0;
- tasksEvents = (uint16*)osal_mem_alloc( sizeof( uint16 ) * tasksCnt);
- osal_memset( tasksEvents, 0, (sizeof( uint16 ) * tasksCnt));
- LL_Init( taskID++);
- /* Application */
- spi_demo_Init(taskID);
- }
复制代码
3、spiflash_demo.c
没有改文件名,还用原例程的名字。
有两个SPI模块可选,这里选择SPI0
- hal_spi_t spiflash_spi =
- {
- .spi_index = SPI0, //SPI 模块选择
- };
复制代码 SPI0的初始化数据
- spi_Cfg_t spi_cfg =
- {
- .sclk_pin = GPIO_P07,
- .ssn_pin = GPIO_DUMMY, //GPIO_DUMMY
- .MOSI = GPIO_P24,
- .MISO = GPIO_P23,
- .baudrate = 8000000, //SPI 波特率
- .spi_tmod = SPI_TXD, //SPI 使用场景
- .spi_scmod = SPI_MODE0, //SPI 工作模式
- .spi_dfsmod = SPI_1BYTE, //每次SPI 1个byte
- #if DMAC_USE
- .dma_tx_enable = false,
- .dma_rx_enable = false,
- #endif
- .int_mode = false, //是否中断模式
- .force_cs = true, //true手动设置cs
- .evt_handler = spi_cb, //SPI 传输完成回调函数
- };
复制代码
DMA初始化数据
- HAL_DMA_t dma_cfg =
- {
- .dma_channel = DMA_CH_0,
- .evt_handler = dma_cb,
- };
复制代码- void spi_cb(spi_evt_t* evt)
- {
- }
- void dma_cb(DMA_CH_t ch)
- {
- }
复制代码
OSAL的TASK初始化函数,每隔1秒中触发一个TIMER_1S_ONCE事件,但是TIMER_1S_ONCE事件基本不做任何事情。
- void spi_demo_Init( uint8 task_id )
- {
- spi_TaskID = task_id;
- hal_dma_init();
- spi_lcd_init();
-
- osal_start_reload_timer( spi_TaskID, TIMER_1S_ONCE, 2000);
- }
复制代码
事件处理函数,可以看到TIMER_1S_ONCE,只打印个LOG
- uint16 spi_demo_ProcessEvent( uint8 task_id, uint16 events )
- {
- if (events & TIMER_1S_ONCE )
- {
- LOG("\n2s:recycle mode\n");
- return (events ^ TIMER_1S_ONCE);
- }
- return 0;
- }
复制代码
spi_lcd_init(),初始化SPI、DMA_CHANNEL、LCD
- void spi_lcd_init(void)
- {
- uint8_t retval = PPlus_SUCCESS;
-
- //配置使能 SPI 模块
- retval = hal_spi_bus_init(&spiflash_spi,spi_cfg);
- if(retval != PPlus_SUCCESS)
- {
- LOG("spi init err!please check it!\n");
- }
- retval = hal_dma_init_channel(dma_cfg);
- if(retval != PPlus_SUCCESS)
- {
- LOG("dma init err!please check it!\n");
- }
-
- ST7735_Init();
- }
复制代码
4、ST7735.C
CS、DC、RST、背光定义和驱动
- #define LCD_CS GPIO_P20 //
- #define LCD_DC GPIO_P03 //
- #define LCD_RST GPIO_P02 //
- #define LCD_LED GPIO_P34 //
- #define LCD_CS_SET() hal_gpio_write(LCD_CS,1);
- #define LCD_CS_RESET() hal_gpio_write(LCD_CS,0);
- #define LCD_DC_SET() hal_gpio_write(LCD_DC,1);
- #define LCD_DC_RESET() hal_gpio_write(LCD_DC,0);
- #define LCD_RST_SET() hal_gpio_write(LCD_RST,1);
- #define LCD_RST_RESET() hal_gpio_write(LCD_RST,0);
- #define LCD_LED_SET() hal_gpio_write(LCD_LED,1);
- #define LCD_LED_RESET() hal_gpio_write(LCD_LED,0);
复制代码 利用hal_spi_send_byte函数向SPI总线发送1BTYE数据
- void LCD_SPI_TxData_Polling(uint8_t *Buffer, uint8_t Length)
- {
- uint8_t i = 0, Data = 0;
- for (i = 0; i < Length; i++)
- {
- hal_spi_send_byte(&spiflash_spi,Buffer[i]);
- }
- }
复制代码 写指令、写数据、发送连续数据
- /*写指令*/
- void LCD_WriteIndex(uint8_t index)
- {
- LCD_CS_RESET();
- LCD_DC_RESET();
- LCD_SPI_TxData_Polling(&index,1);
- LCD_CS_SET();
- }
- /*写数据*/
- void LCD_WriteData(uint8_t *Buffer, uint8_t Length)
- {
- LCD_CS_RESET();
- LCD_DC_SET();
- LCD_SPI_TxData_Polling(Buffer,Length);
- LCD_CS_SET();
- }
- void ST7735_SendData(uint8_t cmd, uint8_t *data, uint16_t length)
- {
- LCD_WriteIndex(cmd);
- LCD_WriteData(data,length);
- }
复制代码 ST7735_Init
- void ST7735_Init(void)
- {
- char buf[16];
- LCD_LED_SET();
-
-
- ST7735_HardReset();
- ST7735_SendInitStr();
- GUI_Clear(COLOR_Red);
-
- WaitMs(1000);
- GUI_Clear(COLOR_Green);
-
- WaitMs(1000);
- GUI_Clear(COLOR_Blue);
- GUI_WriteASCII8x16(10,10,"PB-03F ",COLOR_White,COLOR_Blue);
- GUI_WriteASCII8x16(10,30,"ST7735",COLOR_White,COLOR_Blue);
- GUI_WriteASCII8x16(10,50,"SPI Drive LCD",COLOR_White,COLOR_Blue);
- WaitMs(1000);
- }
复制代码
5、实际效果
|