Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BSP_DISCO_F746NG FatFS mbed
main.cpp
00001 #include "mbed.h" 00002 #define USE_STM32746G_DISCOVERY 00003 #include "stm32f7xx_hal.h" 00004 #include "stm32746g_discovery.h" 00005 #include "stm32746g_discovery_sd.h" 00006 #include "string.h" 00007 #include "stm32746g_discovery_lcd.h" 00008 #include "ff_gen_drv.h" 00009 #include "sd_diskio.h" 00010 00011 #define FRAME_BUFFER 0xC0000000 00012 #define SD_IN_BUFFER 0xC0140000 00013 #define SD_OUT_BUFFER 0xC0280000 00014 00015 FATFS SDFatFs; 00016 FIL MyFile; 00017 00018 00019 00020 00021 static void LCD_LL_ConvertFrameToARGB8888(void *pSrc, void *pDst); 00022 static void LCD_LL_ConvertFrameToRGB888(void *pSrc, void *pDst); 00023 extern SD_HandleTypeDef uSdHandle; 00024 DMA2D_HandleTypeDef hdma2d_disco; 00025 00026 Serial pc(USBTX, USBRX); 00027 uint32_t i = 0, image_width = 0, image_height = 0; 00028 const char image_header[] = "P6\n# Generated by STM32\n480 272\n255\n"; 00029 00030 char SDPath[4]; 00031 char text [40]; 00032 char c; 00033 int main(void) 00034 { 00035 FRESULT res; 00036 uint32_t bytesread; 00037 uint32_t line_cnt=0; 00038 pc.baud(115200); 00039 00040 BSP_LCD_Init(); 00041 BSP_LCD_DisplayOff(); 00042 BSP_LCD_LayerDefaultInit(0, FRAME_BUFFER); 00043 BSP_LCD_SelectLayer(0); 00044 BSP_LCD_DisplayOn(); 00045 00046 FATFS_LinkDriver(&SD_Driver, SDPath); 00047 00048 res = f_mount(&SDFatFs, (TCHAR const*)SDPath, 0); 00049 if(res!= FR_OK) 00050 pc.printf("Failed to mount SD\n"); 00051 00052 res = f_open(&MyFile, "image.ppm", FA_READ); 00053 if(res!= FR_OK) 00054 pc.printf("Failed to open file\n"); 00055 00056 // Read image header and get image size 00057 while(res==FR_OK) { 00058 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread); 00059 if (c!='#') 00060 line_cnt++; 00061 if(line_cnt==2) { 00062 text[0] = c; 00063 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread); 00064 i = 1; 00065 while(c!=' ') { 00066 text[i]=c; 00067 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread); 00068 i++; 00069 } 00070 text[i]=0; 00071 image_width = atoi(text); 00072 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread); 00073 i = 0; 00074 while(c!='\n') { 00075 text[i]=c; 00076 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread); 00077 i++; 00078 } 00079 text[i]=0; 00080 image_height = atoi(text); 00081 } else { 00082 while(c!='\n') 00083 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread); 00084 } 00085 if(line_cnt == 3) 00086 break; 00087 00088 } 00089 00090 if(image_width != 480 || image_height != 272) 00091 pc.printf("Image size should be 480x272"); 00092 00093 // Read image as 512 byte blocks 00094 for(i = 0; i < image_width*image_width*3; i = i + 512) { 00095 res = f_read(&MyFile, (uint8_t *)(SD_IN_BUFFER+i) , 512, (UINT*)&bytesread); 00096 } 00097 00098 // Correct color order 00099 for(i = 0; i < image_width*image_width*3; i = i + 3) { 00100 c = *(char *)(SD_IN_BUFFER+i+2); 00101 *(char *)(SD_IN_BUFFER+i+2)= *(char *)(SD_IN_BUFFER+i); 00102 *(char *)(SD_IN_BUFFER+i) = c; 00103 } 00104 f_close(&MyFile); 00105 00106 // Print image on LCD 00107 LCD_LL_ConvertFrameToARGB8888((uint32_t *)(SD_IN_BUFFER), (uint32_t *)(FRAME_BUFFER)); 00108 00109 00110 // Transfer image to a buffer 00111 LCD_LL_ConvertFrameToRGB888((uint32_t *)(FRAME_BUFFER), (uint32_t *)(SD_OUT_BUFFER)); 00112 00113 // Correct color order 00114 for(i = 0; i < image_width*image_width*3; i = i + 3) { 00115 c = *(char *)(SD_OUT_BUFFER+i+2); 00116 *(char *)(SD_OUT_BUFFER+i+2)= *(char *)(SD_OUT_BUFFER+i); 00117 *(char *)(SD_OUT_BUFFER+i) = c; 00118 } 00119 00120 res = f_open(&MyFile, "image_ou.ppm", FA_CREATE_ALWAYS | FA_WRITE); 00121 if(res!= FR_OK) 00122 pc.printf("Failed to open file\n"); 00123 00124 // Write image header 00125 res = f_write(&MyFile, image_header, sizeof(image_header), &bytesread); 00126 if(res!= FR_OK) 00127 pc.printf("Failed to write file 1\n"); 00128 00129 // Write image as 512 byte blocks 00130 for(i = 0; i < 480*272*3; i = i + 512) { 00131 res = f_write(&MyFile, (uint8_t *)(SD_OUT_BUFFER+i), 512, &bytesread); 00132 if(res!= FR_OK) 00133 pc.printf("Failed to write file\n"); 00134 } 00135 00136 pc.printf("Done!\n"); 00137 f_close(&MyFile); 00138 FATFS_UnLinkDriver(SDPath); 00139 00140 while (1) { 00141 } 00142 } 00143 extern "C" { 00144 void BSP_SDMMC_IRQHandler(void) 00145 { 00146 HAL_SD_IRQHandler(&uSdHandle); 00147 } 00148 void BSP_SDMMC_DMA_Tx_IRQHandler(void) 00149 { 00150 HAL_DMA_IRQHandler(uSdHandle.hdmatx); 00151 } 00152 void BSP_SDMMC_DMA_Rx_IRQHandler(void) 00153 { 00154 HAL_DMA_IRQHandler(uSdHandle.hdmarx); 00155 } 00156 static void LCD_LL_ConvertFrameToARGB8888(void *pSrc, void *pDst) 00157 { 00158 /* Enable DMA2D clock */ 00159 __HAL_RCC_DMA2D_CLK_ENABLE(); 00160 00161 /* Configure the DMA2D Mode, Color Mode and output offset */ 00162 hdma2d_disco.Init.Mode = DMA2D_M2M_PFC; 00163 hdma2d_disco.Init.ColorMode = DMA2D_ARGB8888; 00164 hdma2d_disco.Init.OutputOffset = 0; 00165 00166 /* Foreground Configuration */ 00167 hdma2d_disco.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA; 00168 hdma2d_disco.LayerCfg[1].InputAlpha = 0xFF; 00169 hdma2d_disco.LayerCfg[1].InputColorMode = CM_RGB888; 00170 hdma2d_disco.LayerCfg[1].InputOffset = 0; 00171 00172 hdma2d_disco.Instance = DMA2D; 00173 00174 /* DMA2D Initialization */ 00175 if(HAL_DMA2D_Init(&hdma2d_disco) == HAL_OK) { 00176 if(HAL_DMA2D_ConfigLayer(&hdma2d_disco, 1) == HAL_OK) { 00177 if (HAL_DMA2D_Start(&hdma2d_disco, (uint32_t)pSrc, (uint32_t)pDst, 480, 272) == HAL_OK) { 00178 /* Polling For DMA transfer */ 00179 HAL_DMA2D_PollForTransfer(&hdma2d_disco, 10); 00180 } 00181 } 00182 } else { 00183 00184 } 00185 } 00186 static void LCD_LL_ConvertFrameToRGB888(void *pSrc, void *pDst) 00187 { 00188 /* Enable DMA2D clock */ 00189 __HAL_RCC_DMA2D_CLK_ENABLE(); 00190 00191 /* Configure the DMA2D Mode, Color Mode and output offset */ 00192 hdma2d_disco.Init.Mode = DMA2D_M2M_PFC; 00193 hdma2d_disco.Init.ColorMode = DMA2D_RGB888; 00194 hdma2d_disco.Init.OutputOffset = 0; 00195 00196 /* Foreground Configuration */ 00197 hdma2d_disco.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA; 00198 hdma2d_disco.LayerCfg[1].InputAlpha = 0xFF; 00199 hdma2d_disco.LayerCfg[1].InputColorMode = CM_ARGB8888; 00200 hdma2d_disco.LayerCfg[1].InputOffset = 0; 00201 00202 hdma2d_disco.Instance = DMA2D; 00203 00204 /* DMA2D Initialization */ 00205 if(HAL_DMA2D_Init(&hdma2d_disco) == HAL_OK) { 00206 if(HAL_DMA2D_ConfigLayer(&hdma2d_disco, 1) == HAL_OK) { 00207 if (HAL_DMA2D_Start(&hdma2d_disco, (uint32_t)pSrc, (uint32_t)pDst, 480, 272) == HAL_OK) { 00208 /* Polling For DMA transfer */ 00209 HAL_DMA2D_PollForTransfer(&hdma2d_disco, 10); 00210 } 00211 } 00212 } else { 00213 00214 } 00215 } 00216 00217 }
Generated on Sun Jul 31 2022 19:26:57 by
1.7.2