Library 51- Chrom-ART Accelerator (DMA2D) graphic library on STM32F429-Discovery
ST’s Chrom-ARTTM Accelerator function or DMA2D is supported in this library. This is powerful tool in STM32F429/39 or STM32F427/29 devices. It is based on DMA transmission from graphic to memory in top possible speed. This can be used to draw moving objects, rectangles, circles very fast without a lot of work.
My DMA2D GRAPHIC library was designed in a way that can support any LCD. Because anything that DMA2D knows is writing “data” (color) in memory at some locations. You still need some way to transmit data from memory to LCD. So library was designed that you can custom set start memory address, LCD width, LCD height and LCD orientation. Then the magic to writing in memory can start.
In case of STM32F429-Discovery, external SD-RAM is connected to MCU. In this case, SD-RAM has some logical address, in our case is 0xD0000000. DMA2D just knows that there is starting address and that LCD is 240px width and 320px height. When you want rectangle on x = 10, y = 10 location, library will calculate memory location where to transmit data for you.
Also, you need separate driver to transmit your data from memory to LCD. In case of STM32F429-Discovery board, LTDC periperal is in use. Again, LTDC peripheral knows start address of LCD memory (0xD0000000) and it can start work.
These 2 settings (LCD width, LCD height, memory start address) are by default set for STM32F429-Discovery compatibility, but you can set it to your needs any time. More below.
Library
Features
- LCD independent
- LCD orientation supported
- Easy to port to another application with just 3 settings
- Writing to memory with DMA2D fastest speed
- Supported on STM32F429-Discovery board
- Draw rectangles, lines, circles, triangles, rounded rectangles with fastest speed
- Unlimited layers if they are supported by your peripheral to transmit data to LCD
- In case of STM32F429-Discovery, LTDC can use 2 layers
- Memory offset is used for new address
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx DMA2D
- TM
- defines.h
- defines.h
If you want to use this powerfull DMA2D tool on your custom application to work with memory, you need to specify some defines.
1 2 3 4 5 6 7 8 9 10 11 12 |
/* Default LCD width in pixels */ #define DMA2D_GRAPHIC_LCD_WIDTH 240 /* Default LCD height in pixels */ #define DMA2D_GRAPHIC_LCD_HEIGHT 320 /* Number of LCD pixels */ #define DMA2D_GRAPHIC_PIXELS DMA2D_GRAPHIC_LCD_WIDTH * DMA2D_GRAPHIC_LCD_HEIGHT /* RAM Start address for LCD */ /* On STM32F429-Discovery, this is address for SDRAM which operate with LCD and LTDC peripheral */ #define DMA2D_GRAPHIC_RAM_ADDR 0xD0000000 |
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
/** * Struct for polygon line * * Parameters: * - uint16_t X: * X coordinate for poly line * - uint16_t Y: * Y coordinate for poly line */ typedef struct { uint16_t X; uint16_t Y; } TM_DMA2DRAPHIC_Poly_t; /** * Initialize and prepare DMA2D for working. * * This function has to be called before anything can be used */ void TM_DMA2DGRAPHIC_Init(void); /** * Set layer for LCD * * This functions just works in memory, so when you set layer, * basically just address offset is used. * To show anything on LCD you need LTDC or anything else for transmission * * Parameters: * - uint8_t layer_number: * Layer number, starting from 1 to infinity. * * No return */ void TM_DMA2DGRAPHIC_SetLayer(uint8_t layer_number); /** * Set orientation for DMA2D peripheral * * You have to match DMA2D graphic library with your LCD orientation in order to display correct. * * Parameters: * - uint8_t orientation: * - 0: Normal, no orientation. * - 1: 180 Degrees orientation * - 2: 90 Degrees orientation * - 3: 270 Degrees orientation */ /* 0: normal, 1: 180%, 2: 90 degrees, 3: 20 degrees */ void TM_DMA2DGRAPHIC_SetOrientation(uint8_t orientation); /** * Fill entire LCD layer with custom color * * Parameters: * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_Fill(uint32_t color); /** * Draw single pixel on currently active layer * * Parameters: * - uint16_t x: * X coordinate on LCD * - uint16_t y: * Y coordinate on LCD * - uint32_t color: * Pixel color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawPixel(uint16_t x, uint16_t y, uint32_t color); /** * Get single pixel on currently active layer * * Parameters: * - uint16_t x: * X coordinate on LCD * - uint16_t y: * Y coordinate on LCD * * RGB565 color format returned */ uint32_t TM_DMA2DGRAPHIC_GetPixel(uint16_t x, uint16_t y); /** * Draw vertical line on currently active layer * * Parameters: * - int16_t x: * X coordinate on LCD * - int16_t y: * Y coordinate on LCD * - uint16_t length: * Vertical line length in pixels * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawVerticalLine(int16_t x, int16_t y, uint16_t length, uint32_t color); /** * Draw Horizontal line on currently active layer * * Parameters: * - int16_t x: * X coordinate on LCD * - int16_t y: * Y coordinate on LCD * - uint16_t length: * Horizontal line length in pixels * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawHorizontalLine(int16_t x, int16_t y, uint16_t length, uint32_t color); /** * Draw line on currently active layer * * Parameters: * - int16_t x1: * X1 coordinate on LCD * - int16_t y1: * Y1 coordinate on LCD * - int16_t x2: * X1 coordinate on LCD * - int16_t y2: * Y1 coordinate on LCD * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawLine(int16_t x1, int16_t y1, int16_t x2, int16_t y2, uint32_t color); /** * Draw polygon line on currently active layer * * Parameters: * - TM_DMA2DRAPHIC_Poly_t* Coordinates: * Pointer to TM_DMA2DRAPHIC_Poly_t array of coordinates * - uint16_t count: * Number of coordinates * - uint32_t color: * color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawPolyLine(TM_DMA2DRAPHIC_Poly_t* Coordinates, uint16_t count, uint32_t color); /** * Draw rectangle on currently active layer * * Parameters: * - uint16_t x: * Top left X location for rectangle on LCD * - uint16_t y: * Top left Y location for rectangle on LCD * - uint16_t width: * Rectangle width in pixels * - uint16_t height: * Rectangle height in pixels * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawRectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t color); /** * Draw filled rectangle on currently active layer * * Parameters: * - uint16_t x: * Top left X location for rectangle on LCD * - uint16_t y: * Top left Y location for rectangle on LCD * - uint16_t width: * Rectangle width in pixels * - uint16_t height: * Rectangle height in pixels * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawFilledRectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint32_t color); /** * Draw rounded rectangle on currently active layer * * Parameters: * - uint16_t x: * Top left X location for rectangle on LCD * - uint16_t y: * Top left Y location for rectangle on LCD * - uint16_t width: * Rectangle width in pixels * - uint16_t height: * Rectangle height in pixels * - uint16_t r: * Corner radius in pixels * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawRoundedRectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t r, uint32_t color); /** * Draw filled rounded rectangle on currently active layer * * Parameters: * - uint16_t x: * Top left X location for rectangle on LCD * - uint16_t y: * Top left Y location for rectangle on LCD * - uint16_t width: * Rectangle width in pixels * - uint16_t height: * Rectangle height in pixels * - uint16_t r: * Corner radius in pixels * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawFilledRoundedRectangle(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint16_t r, uint32_t color); /** * Draw circle on currently active layer * * Parameters: * - uint16_t x0: * X coordinate of circle center on LCD * - uint16_t y0: * Y coordinate of circle center on LCD * - uint16_t r: * Circle radius * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawCircle(uint16_t x0, uint16_t y0, uint16_t r, uint32_t color); /** * Draw filed circle on currently active layer * * Parameters: * - uint16_t x0: * X coordinate of circle center on LCD * - uint16_t y0: * Y coordinate of circle center on LCD * - uint16_t r: * Circle radius * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawFilledCircle(uint16_t x0, uint16_t y0, uint16_t r, uint32_t color); /** * Draw triangle on currently active layer * * Parameters: * - uint16_t x1: * First point, X coordinate * - uint16_t y1: * First point, Y coordinate * - uint16_t x2: * Second point, X coordinate * - uint16_t y2: * Second point, Y coordinate * - uint16_t x3: * Third point, X coordinate * - uint16_t y3: * Third point, Y coordinate * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint32_t color); /** * Draw filled triangle on currently active layer * * Parameters: * - uint16_t x1: * First point, X coordinate * - uint16_t y1: * First point, Y coordinate * - uint16_t x2: * Second point, X coordinate * - uint16_t y2: * Second point, Y coordinate * - uint16_t x3: * Third point, X coordinate * - uint16_t y3: * Third point, Y coordinate * - uint32_t color: * Color in RGB565 format * * No return */ void TM_DMA2DGRAPHIC_DrawFilledTriangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t x3, uint16_t y3, uint32_t color); |
Example
In example, DMA2D graphic library was used with connection to ILI9341 LCD via LTDC transfer from memory to LCD. DMA2D is used to draw graphic to memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
/** * Keil project example for ST's Chrom-ART Accelerator (DMA2D) * * Works only on STM32F429-Discovery! * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_dma2d_graphic.h" #include "tm_stm32f4_ili9341_ltdc.h" #include "tm_stm32f4_fonts.h" #include "tm_stm32f4_delay.h" #include <stdio.h> int main(void) { /* Set coordinates for poly line */ TM_DMA2DRAPHIC_Poly_t Coordinates[] = { {10, 10}, {15, 65}, {200, 200}, {150, 30}, {10, 10} }; /* Initialize system */ SystemInit(); /* Initialize delay functions */ TM_DELAY_Init(); /* Initialize ILI9341 with LTDC */ /* By default layer 1 is used */ TM_ILI9341_Init(); /* Initialize DMA2D GRAPHIC library */ TM_DMA2DGRAPHIC_Init(); /* Set ILI9341 Orientation */ TM_ILI9341_Rotate(TM_ILI9341_Orientation_Portrait_2); /* Set DMA2D GRAPHIC library Orientation */ TM_DMA2DGRAPHIC_SetOrientation((uint8_t)TM_ILI9341_Orientation_Portrait_2); /* Layer 1 */ /* Draw red rectangle at X = 10, Y = 10 position, width = 100 and height = 50px */ TM_DMA2DGRAPHIC_DrawRectangle(10, 10, 100, 50, GRAPHIC_COLOR_RED); /* Draw filled blue rectangle at X = 100, Y = 100 position, width = 100 and height = 50px */ TM_DMA2DGRAPHIC_DrawFilledRectangle(100, 100, 100, 50, GRAPHIC_COLOR_BLUE); /* Draw rounded rectangle */ TM_DMA2DGRAPHIC_DrawRoundedRectangle(110, 110, 80, 30, 10, GRAPHIC_COLOR_GREEN); /* Go to layer 2, set address offset in memory for LCD */ TM_DMA2DGRAPHIC_SetLayer(2); /* Fill layer 2 */ TM_DMA2DGRAPHIC_Fill(GRAPHIC_COLOR_YELLOW); /* Draw poly line */ TM_DMA2DGRAPHIC_DrawPolyLine(Coordinates, 5, GRAPHIC_COLOR_BLACK); /* Draw filled circle */ TM_DMA2DGRAPHIC_DrawFilledCircle(100, 170, 40, GRAPHIC_COLOR_CYAN); while (1) { /* Change display layer on LCD using LTDC transfer */ TM_ILI9341_ChangeLayers(); /* Delay */ Delayms(1000); } } |
Project available on Github, download library below.
Recent comments