teste

Dependencies:   mbed BSP_DISCO_F746NG FatFS

Committer:
nextronic
Date:
Sat Aug 24 12:32:52 2019 +0000
Revision:
3:d9c0b24b6f84
Parent:
2:19c2835ce409
firsye Commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
buyukesmeli 0:020195991898 1 #include "mbed.h"
buyukesmeli 0:020195991898 2 #define USE_STM32746G_DISCOVERY
buyukesmeli 0:020195991898 3 #include "stm32f7xx_hal.h"
buyukesmeli 0:020195991898 4 #include "stm32746g_discovery.h"
buyukesmeli 0:020195991898 5 #include "stm32746g_discovery_sd.h"
buyukesmeli 0:020195991898 6 #include "string.h"
buyukesmeli 0:020195991898 7 #include "stm32746g_discovery_lcd.h"
buyukesmeli 0:020195991898 8 #include "ff_gen_drv.h"
buyukesmeli 0:020195991898 9 #include "sd_diskio.h"
buyukesmeli 0:020195991898 10
buyukesmeli 0:020195991898 11 #define FRAME_BUFFER 0xC0000000
buyukesmeli 0:020195991898 12 #define SD_IN_BUFFER 0xC0140000
buyukesmeli 0:020195991898 13 #define SD_OUT_BUFFER 0xC0280000
buyukesmeli 0:020195991898 14
buyukesmeli 2:19c2835ce409 15 FATFS SDFatFs;
buyukesmeli 2:19c2835ce409 16 FIL MyFile;
buyukesmeli 0:020195991898 17
buyukesmeli 0:020195991898 18
buyukesmeli 2:19c2835ce409 19
buyukesmeli 0:020195991898 20
buyukesmeli 0:020195991898 21 static void LCD_LL_ConvertFrameToARGB8888(void *pSrc, void *pDst);
buyukesmeli 0:020195991898 22 static void LCD_LL_ConvertFrameToRGB888(void *pSrc, void *pDst);
buyukesmeli 0:020195991898 23 extern SD_HandleTypeDef uSdHandle;
buyukesmeli 0:020195991898 24 DMA2D_HandleTypeDef hdma2d_disco;
buyukesmeli 0:020195991898 25
buyukesmeli 0:020195991898 26 Serial pc(USBTX, USBRX);
buyukesmeli 0:020195991898 27 uint32_t i = 0, image_width = 0, image_height = 0;
buyukesmeli 2:19c2835ce409 28 const char image_header[] = "P6\n# Generated by STM32\n480 272\n255\n";
buyukesmeli 0:020195991898 29
buyukesmeli 0:020195991898 30 char SDPath[4];
buyukesmeli 0:020195991898 31 char text [40];
buyukesmeli 0:020195991898 32 char c;
buyukesmeli 0:020195991898 33 int main(void)
buyukesmeli 0:020195991898 34 {
buyukesmeli 0:020195991898 35 FRESULT res;
buyukesmeli 0:020195991898 36 uint32_t bytesread;
buyukesmeli 0:020195991898 37 uint32_t line_cnt=0;
buyukesmeli 0:020195991898 38 pc.baud(115200);
buyukesmeli 0:020195991898 39
buyukesmeli 0:020195991898 40 BSP_LCD_Init();
buyukesmeli 0:020195991898 41 BSP_LCD_DisplayOff();
buyukesmeli 0:020195991898 42 BSP_LCD_LayerDefaultInit(0, FRAME_BUFFER);
buyukesmeli 0:020195991898 43 BSP_LCD_SelectLayer(0);
buyukesmeli 0:020195991898 44 BSP_LCD_DisplayOn();
buyukesmeli 0:020195991898 45
buyukesmeli 0:020195991898 46 FATFS_LinkDriver(&SD_Driver, SDPath);
buyukesmeli 2:19c2835ce409 47
buyukesmeli 0:020195991898 48 res = f_mount(&SDFatFs, (TCHAR const*)SDPath, 0);
buyukesmeli 0:020195991898 49 if(res!= FR_OK)
buyukesmeli 0:020195991898 50 pc.printf("Failed to mount SD\n");
buyukesmeli 2:19c2835ce409 51
nextronic 3:d9c0b24b6f84 52 res = f_open(&MyFile, "img.ppm", FA_READ);
buyukesmeli 0:020195991898 53 if(res!= FR_OK)
buyukesmeli 0:020195991898 54 pc.printf("Failed to open file\n");
nextronic 3:d9c0b24b6f84 55
buyukesmeli 2:19c2835ce409 56 // Read image header and get image size
buyukesmeli 2:19c2835ce409 57 while(res==FR_OK) {
buyukesmeli 0:020195991898 58 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread);
buyukesmeli 0:020195991898 59 if (c!='#')
buyukesmeli 0:020195991898 60 line_cnt++;
buyukesmeli 0:020195991898 61 if(line_cnt==2) {
buyukesmeli 0:020195991898 62 text[0] = c;
buyukesmeli 0:020195991898 63 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread);
buyukesmeli 0:020195991898 64 i = 1;
buyukesmeli 0:020195991898 65 while(c!=' ') {
buyukesmeli 0:020195991898 66 text[i]=c;
buyukesmeli 0:020195991898 67 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread);
buyukesmeli 0:020195991898 68 i++;
buyukesmeli 0:020195991898 69 }
buyukesmeli 0:020195991898 70 text[i]=0;
buyukesmeli 0:020195991898 71 image_width = atoi(text);
buyukesmeli 0:020195991898 72 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread);
buyukesmeli 0:020195991898 73 i = 0;
buyukesmeli 0:020195991898 74 while(c!='\n') {
buyukesmeli 0:020195991898 75 text[i]=c;
buyukesmeli 0:020195991898 76 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread);
buyukesmeli 0:020195991898 77 i++;
buyukesmeli 0:020195991898 78 }
buyukesmeli 0:020195991898 79 text[i]=0;
buyukesmeli 0:020195991898 80 image_height = atoi(text);
buyukesmeli 0:020195991898 81 } else {
buyukesmeli 0:020195991898 82 while(c!='\n')
buyukesmeli 0:020195991898 83 res = f_read(&MyFile, &c, 1, (UINT*)&bytesread);
buyukesmeli 0:020195991898 84 }
buyukesmeli 0:020195991898 85 if(line_cnt == 3)
buyukesmeli 0:020195991898 86 break;
buyukesmeli 0:020195991898 87
buyukesmeli 0:020195991898 88 }
buyukesmeli 2:19c2835ce409 89
buyukesmeli 0:020195991898 90 if(image_width != 480 || image_height != 272)
buyukesmeli 2:19c2835ce409 91 pc.printf("Image size should be 480x272");
buyukesmeli 2:19c2835ce409 92
buyukesmeli 2:19c2835ce409 93 // Read image as 512 byte blocks
buyukesmeli 2:19c2835ce409 94 for(i = 0; i < image_width*image_width*3; i = i + 512) {
buyukesmeli 2:19c2835ce409 95 res = f_read(&MyFile, (uint8_t *)(SD_IN_BUFFER+i) , 512, (UINT*)&bytesread);
buyukesmeli 2:19c2835ce409 96 }
buyukesmeli 0:020195991898 97
buyukesmeli 2:19c2835ce409 98 // Correct color order
buyukesmeli 2:19c2835ce409 99 for(i = 0; i < image_width*image_width*3; i = i + 3) {
buyukesmeli 2:19c2835ce409 100 c = *(char *)(SD_IN_BUFFER+i+2);
buyukesmeli 2:19c2835ce409 101 *(char *)(SD_IN_BUFFER+i+2)= *(char *)(SD_IN_BUFFER+i);
buyukesmeli 2:19c2835ce409 102 *(char *)(SD_IN_BUFFER+i) = c;
buyukesmeli 0:020195991898 103 }
buyukesmeli 0:020195991898 104 f_close(&MyFile);
buyukesmeli 0:020195991898 105
buyukesmeli 2:19c2835ce409 106 // Print image on LCD
buyukesmeli 0:020195991898 107 LCD_LL_ConvertFrameToARGB8888((uint32_t *)(SD_IN_BUFFER), (uint32_t *)(FRAME_BUFFER));
buyukesmeli 2:19c2835ce409 108
buyukesmeli 2:19c2835ce409 109
buyukesmeli 2:19c2835ce409 110 // Transfer image to a buffer
buyukesmeli 2:19c2835ce409 111 LCD_LL_ConvertFrameToRGB888((uint32_t *)(FRAME_BUFFER), (uint32_t *)(SD_OUT_BUFFER));
buyukesmeli 0:020195991898 112
buyukesmeli 2:19c2835ce409 113 // Correct color order
buyukesmeli 2:19c2835ce409 114 for(i = 0; i < image_width*image_width*3; i = i + 3) {
buyukesmeli 2:19c2835ce409 115 c = *(char *)(SD_OUT_BUFFER+i+2);
buyukesmeli 2:19c2835ce409 116 *(char *)(SD_OUT_BUFFER+i+2)= *(char *)(SD_OUT_BUFFER+i);
buyukesmeli 2:19c2835ce409 117 *(char *)(SD_OUT_BUFFER+i) = c;
buyukesmeli 2:19c2835ce409 118 }
buyukesmeli 0:020195991898 119
buyukesmeli 0:020195991898 120 res = f_open(&MyFile, "image_ou.ppm", FA_CREATE_ALWAYS | FA_WRITE);
buyukesmeli 0:020195991898 121 if(res!= FR_OK)
buyukesmeli 0:020195991898 122 pc.printf("Failed to open file\n");
buyukesmeli 0:020195991898 123
buyukesmeli 2:19c2835ce409 124 // Write image header
buyukesmeli 2:19c2835ce409 125 res = f_write(&MyFile, image_header, sizeof(image_header), &bytesread);
buyukesmeli 0:020195991898 126 if(res!= FR_OK)
buyukesmeli 0:020195991898 127 pc.printf("Failed to write file 1\n");
buyukesmeli 0:020195991898 128
buyukesmeli 2:19c2835ce409 129 // Write image as 512 byte blocks
buyukesmeli 0:020195991898 130 for(i = 0; i < 480*272*3; i = i + 512) {
buyukesmeli 0:020195991898 131 res = f_write(&MyFile, (uint8_t *)(SD_OUT_BUFFER+i), 512, &bytesread);
buyukesmeli 0:020195991898 132 if(res!= FR_OK)
buyukesmeli 0:020195991898 133 pc.printf("Failed to write file\n");
buyukesmeli 0:020195991898 134 }
buyukesmeli 2:19c2835ce409 135
buyukesmeli 1:28e2f24b11b3 136 pc.printf("Done!\n");
nextronic 3:d9c0b24b6f84 137
nextronic 3:d9c0b24b6f84 138
nextronic 3:d9c0b24b6f84 139
nextronic 3:d9c0b24b6f84 140 /*
nextronic 3:d9c0b24b6f84 141 char ch;
nextronic 3:d9c0b24b6f84 142
nextronic 3:d9c0b24b6f84 143 res = f_open(&MyFile, "data.txt", FA_READ);
nextronic 3:d9c0b24b6f84 144 if(res!= FR_OK)
nextronic 3:d9c0b24b6f84 145 pc.printf("Failed to open file\n");
nextronic 3:d9c0b24b6f84 146
nextronic 3:d9c0b24b6f84 147 else
nextronic 3:d9c0b24b6f84 148 pc.printf("open file ok\n");
nextronic 3:d9c0b24b6f84 149 */
nextronic 3:d9c0b24b6f84 150
nextronic 3:d9c0b24b6f84 151
nextronic 3:d9c0b24b6f84 152
nextronic 3:d9c0b24b6f84 153 FILE* fp = fopen("/data.txt", "r");
nextronic 3:d9c0b24b6f84 154 pc.printf("%s\n", (!fp ? "Fail :(" : "OK"));
nextronic 3:d9c0b24b6f84 155 if (!fp)
nextronic 3:d9c0b24b6f84 156 //error("error: %s (%d)\n", strerror(errno), -errno);
nextronic 3:d9c0b24b6f84 157 pc.printf("ARARA");
nextronic 3:d9c0b24b6f84 158 while (!feof(fp)) {
nextronic 3:d9c0b24b6f84 159 int c = fgetc(fp);
nextronic 3:d9c0b24b6f84 160 pc.printf("%c", c);
nextronic 3:d9c0b24b6f84 161 }
nextronic 3:d9c0b24b6f84 162 pc.printf("\r\n");
nextronic 3:d9c0b24b6f84 163 fclose(fp);
nextronic 3:d9c0b24b6f84 164
nextronic 3:d9c0b24b6f84 165
nextronic 3:d9c0b24b6f84 166
nextronic 3:d9c0b24b6f84 167
nextronic 3:d9c0b24b6f84 168
nextronic 3:d9c0b24b6f84 169
nextronic 3:d9c0b24b6f84 170
nextronic 3:d9c0b24b6f84 171
nextronic 3:d9c0b24b6f84 172
buyukesmeli 0:020195991898 173 f_close(&MyFile);
buyukesmeli 0:020195991898 174 FATFS_UnLinkDriver(SDPath);
buyukesmeli 0:020195991898 175
buyukesmeli 0:020195991898 176 while (1) {
buyukesmeli 0:020195991898 177 }
buyukesmeli 0:020195991898 178 }
buyukesmeli 0:020195991898 179 extern "C" {
buyukesmeli 0:020195991898 180 void BSP_SDMMC_IRQHandler(void)
buyukesmeli 0:020195991898 181 {
buyukesmeli 0:020195991898 182 HAL_SD_IRQHandler(&uSdHandle);
buyukesmeli 0:020195991898 183 }
buyukesmeli 0:020195991898 184 void BSP_SDMMC_DMA_Tx_IRQHandler(void)
buyukesmeli 0:020195991898 185 {
buyukesmeli 0:020195991898 186 HAL_DMA_IRQHandler(uSdHandle.hdmatx);
buyukesmeli 0:020195991898 187 }
buyukesmeli 0:020195991898 188 void BSP_SDMMC_DMA_Rx_IRQHandler(void)
buyukesmeli 0:020195991898 189 {
buyukesmeli 0:020195991898 190 HAL_DMA_IRQHandler(uSdHandle.hdmarx);
buyukesmeli 0:020195991898 191 }
buyukesmeli 0:020195991898 192 static void LCD_LL_ConvertFrameToARGB8888(void *pSrc, void *pDst)
buyukesmeli 0:020195991898 193 {
buyukesmeli 0:020195991898 194 /* Enable DMA2D clock */
buyukesmeli 0:020195991898 195 __HAL_RCC_DMA2D_CLK_ENABLE();
buyukesmeli 0:020195991898 196
buyukesmeli 0:020195991898 197 /* Configure the DMA2D Mode, Color Mode and output offset */
buyukesmeli 0:020195991898 198 hdma2d_disco.Init.Mode = DMA2D_M2M_PFC;
buyukesmeli 0:020195991898 199 hdma2d_disco.Init.ColorMode = DMA2D_ARGB8888;
buyukesmeli 0:020195991898 200 hdma2d_disco.Init.OutputOffset = 0;
buyukesmeli 0:020195991898 201
buyukesmeli 0:020195991898 202 /* Foreground Configuration */
buyukesmeli 0:020195991898 203 hdma2d_disco.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
buyukesmeli 0:020195991898 204 hdma2d_disco.LayerCfg[1].InputAlpha = 0xFF;
buyukesmeli 0:020195991898 205 hdma2d_disco.LayerCfg[1].InputColorMode = CM_RGB888;
buyukesmeli 0:020195991898 206 hdma2d_disco.LayerCfg[1].InputOffset = 0;
buyukesmeli 0:020195991898 207
buyukesmeli 0:020195991898 208 hdma2d_disco.Instance = DMA2D;
buyukesmeli 0:020195991898 209
buyukesmeli 0:020195991898 210 /* DMA2D Initialization */
buyukesmeli 0:020195991898 211 if(HAL_DMA2D_Init(&hdma2d_disco) == HAL_OK) {
buyukesmeli 0:020195991898 212 if(HAL_DMA2D_ConfigLayer(&hdma2d_disco, 1) == HAL_OK) {
buyukesmeli 0:020195991898 213 if (HAL_DMA2D_Start(&hdma2d_disco, (uint32_t)pSrc, (uint32_t)pDst, 480, 272) == HAL_OK) {
buyukesmeli 0:020195991898 214 /* Polling For DMA transfer */
buyukesmeli 0:020195991898 215 HAL_DMA2D_PollForTransfer(&hdma2d_disco, 10);
buyukesmeli 0:020195991898 216 }
buyukesmeli 0:020195991898 217 }
buyukesmeli 0:020195991898 218 } else {
buyukesmeli 0:020195991898 219
buyukesmeli 0:020195991898 220 }
buyukesmeli 0:020195991898 221 }
buyukesmeli 0:020195991898 222 static void LCD_LL_ConvertFrameToRGB888(void *pSrc, void *pDst)
buyukesmeli 0:020195991898 223 {
buyukesmeli 0:020195991898 224 /* Enable DMA2D clock */
buyukesmeli 0:020195991898 225 __HAL_RCC_DMA2D_CLK_ENABLE();
buyukesmeli 0:020195991898 226
buyukesmeli 0:020195991898 227 /* Configure the DMA2D Mode, Color Mode and output offset */
buyukesmeli 0:020195991898 228 hdma2d_disco.Init.Mode = DMA2D_M2M_PFC;
buyukesmeli 0:020195991898 229 hdma2d_disco.Init.ColorMode = DMA2D_RGB888;
buyukesmeli 0:020195991898 230 hdma2d_disco.Init.OutputOffset = 0;
buyukesmeli 0:020195991898 231
buyukesmeli 0:020195991898 232 /* Foreground Configuration */
buyukesmeli 0:020195991898 233 hdma2d_disco.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
buyukesmeli 0:020195991898 234 hdma2d_disco.LayerCfg[1].InputAlpha = 0xFF;
buyukesmeli 0:020195991898 235 hdma2d_disco.LayerCfg[1].InputColorMode = CM_ARGB8888;
buyukesmeli 0:020195991898 236 hdma2d_disco.LayerCfg[1].InputOffset = 0;
buyukesmeli 0:020195991898 237
buyukesmeli 0:020195991898 238 hdma2d_disco.Instance = DMA2D;
buyukesmeli 0:020195991898 239
buyukesmeli 0:020195991898 240 /* DMA2D Initialization */
buyukesmeli 0:020195991898 241 if(HAL_DMA2D_Init(&hdma2d_disco) == HAL_OK) {
buyukesmeli 0:020195991898 242 if(HAL_DMA2D_ConfigLayer(&hdma2d_disco, 1) == HAL_OK) {
buyukesmeli 0:020195991898 243 if (HAL_DMA2D_Start(&hdma2d_disco, (uint32_t)pSrc, (uint32_t)pDst, 480, 272) == HAL_OK) {
buyukesmeli 0:020195991898 244 /* Polling For DMA transfer */
buyukesmeli 0:020195991898 245 HAL_DMA2D_PollForTransfer(&hdma2d_disco, 10);
buyukesmeli 0:020195991898 246 }
buyukesmeli 0:020195991898 247 }
buyukesmeli 0:020195991898 248 } else {
buyukesmeli 0:020195991898 249
buyukesmeli 0:020195991898 250 }
buyukesmeli 0:020195991898 251 }
buyukesmeli 0:020195991898 252
buyukesmeli 0:020195991898 253 }