F746NG fatfs and micro sd example
Dependencies: BSP_DISCO_F746NG FatFS mbed
This is a quick and dirty example about how to use micro sd card module with FatFs on STM32746G Discovery board. I simply use the FatFs library downloaded with DFP for STM32F7. The project shows how to read PPM image file from sd card, display on LCD and writing image to sd card.
Revision 2:19c2835ce409, committed 2018-02-04
- Comitter:
- buyukesmeli
- Date:
- Sun Feb 04 10:12:09 2018 +0000
- Parent:
- 1:28e2f24b11b3
- Commit message:
- Color and output image bugs are fixed
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 28e2f24b11b3 -r 19c2835ce409 main.cpp --- a/main.cpp Sat Feb 03 14:43:35 2018 +0000 +++ b/main.cpp Sun Feb 04 10:12:09 2018 +0000 @@ -12,11 +12,11 @@ #define SD_IN_BUFFER 0xC0140000 #define SD_OUT_BUFFER 0xC0280000 -FATFS SDFatFs; -FIL MyFile; +FATFS SDFatFs; +FIL MyFile; - + static void LCD_LL_ConvertFrameToARGB8888(void *pSrc, void *pDst); static void LCD_LL_ConvertFrameToRGB888(void *pSrc, void *pDst); @@ -25,12 +25,11 @@ Serial pc(USBTX, USBRX); uint32_t i = 0, image_width = 0, image_height = 0; -const char image_header[] = "P6\n# Generated by STM32\n272 480\n255\n"; +const char image_header[] = "P6\n# Generated by STM32\n480 272\n255\n"; char SDPath[4]; char text [40]; char c; - int main(void) { FRESULT res; @@ -45,17 +44,17 @@ BSP_LCD_DisplayOn(); FATFS_LinkDriver(&SD_Driver, SDPath); - + res = f_mount(&SDFatFs, (TCHAR const*)SDPath, 0); if(res!= FR_OK) pc.printf("Failed to mount SD\n"); - + res = f_open(&MyFile, "image.ppm", FA_READ); if(res!= FR_OK) pc.printf("Failed to open file\n"); - - while(1) { - + + // Read image header and get image size + while(res==FR_OK) { res = f_read(&MyFile, &c, 1, (UINT*)&bytesread); if (c!='#') line_cnt++; @@ -87,35 +86,53 @@ break; } - + if(image_width != 480 || image_height != 272) - pc.printf("Image size should be 272x480"); + pc.printf("Image size should be 480x272"); + + // Read image as 512 byte blocks + for(i = 0; i < image_width*image_width*3; i = i + 512) { + res = f_read(&MyFile, (uint8_t *)(SD_IN_BUFFER+i) , 512, (UINT*)&bytesread); + } - for(i = 0; i < image_width*image_width*3; i = i + 512) { - res = f_read(&MyFile, (void *)(SD_IN_BUFFER+i) , 512, (UINT*)&bytesread); + // Correct color order + for(i = 0; i < image_width*image_width*3; i = i + 3) { + c = *(char *)(SD_IN_BUFFER+i+2); + *(char *)(SD_IN_BUFFER+i+2)= *(char *)(SD_IN_BUFFER+i); + *(char *)(SD_IN_BUFFER+i) = c; } f_close(&MyFile); + // Print image on LCD LCD_LL_ConvertFrameToARGB8888((uint32_t *)(SD_IN_BUFFER), (uint32_t *)(FRAME_BUFFER)); + + + // Transfer image to a buffer + LCD_LL_ConvertFrameToRGB888((uint32_t *)(FRAME_BUFFER), (uint32_t *)(SD_OUT_BUFFER)); - - LCD_LL_ConvertFrameToRGB888((uint32_t *)(FRAME_BUFFER), (uint32_t *)(SD_OUT_BUFFER)); + // Correct color order + for(i = 0; i < image_width*image_width*3; i = i + 3) { + c = *(char *)(SD_OUT_BUFFER+i+2); + *(char *)(SD_OUT_BUFFER+i+2)= *(char *)(SD_OUT_BUFFER+i); + *(char *)(SD_OUT_BUFFER+i) = c; + } res = f_open(&MyFile, "image_ou.ppm", FA_CREATE_ALWAYS | FA_WRITE); if(res!= FR_OK) pc.printf("Failed to open file\n"); - - res = f_write(&MyFile, image_header, 15, &bytesread); + // Write image header + res = f_write(&MyFile, image_header, sizeof(image_header), &bytesread); if(res!= FR_OK) pc.printf("Failed to write file 1\n"); + // Write image as 512 byte blocks for(i = 0; i < 480*272*3; i = i + 512) { res = f_write(&MyFile, (uint8_t *)(SD_OUT_BUFFER+i), 512, &bytesread); if(res!= FR_OK) pc.printf("Failed to write file\n"); } - + pc.printf("Done!\n"); f_close(&MyFile); FATFS_UnLinkDriver(SDPath);