/* * Copyright (c) 2006-2022, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2020-08-07 NU-LL first version */ #include #ifdef BSP_USING_LCD_SPI #include #include #include #define DRV_DEBUG #define LOG_TAG "drv.lcd" #include #define LCD_DEVICE(dev) (struct drv_lcd_device*)(dev) struct drv_lcd_device { struct rt_device parent; struct rt_device_graphic_info lcd_info; struct rt_semaphore lcd_lock; /* 0:front_buf is being used 1: back_buf is being used*/ rt_uint8_t cur_buf; rt_uint8_t *front_buf; rt_uint8_t *back_buf; }; struct drv_lcd_device _lcd; static rt_err_t drv_lcd_init(struct rt_device *device) { struct drv_lcd_device *lcd = LCD_DEVICE(device); /* nothing, right now */ lcd = lcd; return RT_EOK; } static rt_err_t drv_lcd_control(struct rt_device *device, int cmd, void *args) { struct drv_lcd_device *lcd = LCD_DEVICE(device); switch (cmd) { case RTGRAPHIC_CTRL_RECT_UPDATE: { LCD_FillRGBRect(0, 0, _lcd.lcd_info.framebuffer, _lcd.lcd_info.width, _lcd.lcd_info.height); } break; case RTGRAPHIC_CTRL_GET_INFO: { struct rt_device_graphic_info *info = (struct rt_device_graphic_info *)args; RT_ASSERT(info != RT_NULL); info->pixel_format = lcd->lcd_info.pixel_format; info->bits_per_pixel = 16; info->width = lcd->lcd_info.width; info->height = lcd->lcd_info.height; info->framebuffer = lcd->lcd_info.framebuffer; } break; } return RT_EOK; } rt_err_t stm32_lcd_init(struct drv_lcd_device *lcd) { rt_err_t result; struct rt_spi_device *spi_device; struct rt_spi_configuration cfg; /* attach the device to spi bus*/ spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device)); RT_ASSERT(spi_device != RT_NULL); result = rt_spi_bus_attach_device(spi_device, LCD_SPI_DEV_NAME, LCD_SPI_BUS_NAME, (void *)RT_NULL); if (result != RT_EOK) { LOG_E("%s attach to %s faild, %d\n", LCD_SPI_DEV_NAME, LCD_SPI_BUS_NAME, result); } RT_ASSERT(result == RT_EOK); cfg.data_width = 8; cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB | RT_SPI_NO_CS | RT_SPI_3WIRE; cfg.max_hz = 20 * 1000 *1000; /* 20M */ rt_spi_configure(spi_device, &cfg); return result; } #ifdef RT_USING_DEVICE_OPS const static struct rt_device_ops lcd_ops = { drv_lcd_init, RT_NULL, RT_NULL, RT_NULL, RT_NULL, drv_lcd_control }; #endif int drv_lcd_hw_init(void) { rt_err_t result = RT_EOK; struct rt_device *device = &_lcd.parent; /* memset _lcd to zero */ memset(&_lcd, 0x00, sizeof(_lcd)); /* init lcd_lock semaphore */ result = rt_sem_init(&_lcd.lcd_lock, "lcd_lock", 0, RT_IPC_FLAG_FIFO); if (result != RT_EOK) { LOG_E("init semaphore failed!\n"); result = -RT_ENOMEM; goto __exit; } /* config LCD dev info */ _lcd.lcd_info.height = LCD_HEIGHT; _lcd.lcd_info.width = LCD_WIDTH; _lcd.lcd_info.bits_per_pixel = LCD_BITS_PER_PIXEL; _lcd.lcd_info.pixel_format = LCD_PIXEL_FORMAT; /* malloc memory for Triple Buffering */ _lcd.lcd_info.framebuffer = rt_malloc(LCD_BUF_SIZE); _lcd.back_buf = rt_malloc(LCD_BUF_SIZE); _lcd.front_buf = rt_malloc(LCD_BUF_SIZE); if (_lcd.lcd_info.framebuffer == RT_NULL || _lcd.back_buf == RT_NULL || _lcd.front_buf == RT_NULL) { LOG_E("init frame buffer failed!\n"); result = -RT_ENOMEM; goto __exit; } /* memset buff to 0xFF */ memset(_lcd.lcd_info.framebuffer, 0xFF, LCD_BUF_SIZE); memset(_lcd.back_buf, 0xFF, LCD_BUF_SIZE); memset(_lcd.front_buf, 0xFF, LCD_BUF_SIZE); device->type = RT_Device_Class_Graphic; #ifdef RT_USING_DEVICE_OPS device->ops = &lcd_ops; #else device->init = drv_lcd_init; device->control = drv_lcd_control; #endif /* register lcd device */ rt_device_register(device, "lcd", RT_DEVICE_FLAG_RDWR); /* init stm32 LTDC */ if (stm32_lcd_init(&_lcd) != RT_EOK) { result = -RT_ERROR; goto __exit; } else { LCD_SetBrightness(MAX_BRIGHTNESS); } __exit: if (result != RT_EOK) { rt_sem_delete(&_lcd.lcd_lock); if (_lcd.lcd_info.framebuffer) { rt_free(_lcd.lcd_info.framebuffer); } if (_lcd.back_buf) { rt_free(_lcd.back_buf); } if (_lcd.front_buf) { rt_free(_lcd.front_buf); } } return result; } INIT_DEVICE_EXPORT(drv_lcd_hw_init); #ifdef DRV_DEBUG #ifdef FINSH_USING_MSH int lcd_test() { struct drv_lcd_device *lcd; lcd = (struct drv_lcd_device *)rt_device_find("lcd"); while (1) { /* red */ for (int i = 0; i < LCD_BUF_SIZE / 2; i++) { lcd->lcd_info.framebuffer[2 * i] = 0x00; lcd->lcd_info.framebuffer[2 * i + 1] = 0xF8; } lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL); rt_thread_mdelay(1000); /* green */ for (int i = 0; i < LCD_BUF_SIZE / 2; i++) { lcd->lcd_info.framebuffer[2 * i] = 0xE0; lcd->lcd_info.framebuffer[2 * i + 1] = 0x07; } lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL); rt_thread_mdelay(1000); /* blue */ for (int i = 0; i < LCD_BUF_SIZE / 2; i++) { lcd->lcd_info.framebuffer[2 * i] = 0x1F; lcd->lcd_info.framebuffer[2 * i + 1] = 0x00; } lcd->parent.control(&lcd->parent, RTGRAPHIC_CTRL_RECT_UPDATE, RT_NULL); rt_thread_mdelay(1000); } } MSH_CMD_EXPORT(lcd_test, lcd_test); #endif /* FINSH_USING_MSH */ #endif /* DRV_DEBUG */ #endif /* BSP_USING_LCD */