5 years, 1 month ago.

Cannot write image data to SD card using Nucle-f401re

Hi everyone, I'm working on a project with Nucle-f401re based on a IR sensor and my scope is to save a binar image, received from the sensor, on SD card in bit map format. I'm using the following code:

#include "mbed.h"
#include "SDFileSystem.h"


SDFileSystem sd(SPI_MOSI, SPI_MISO, SPI_SCK, D4, "sd");


FATFS SDFatFs;
FIL MyFile;
FRESULT res;
uint32_t  bytesread;
uint16_t lepton_image[60][80];

void writeBMPFile(FIL bmpFile,uint16_t  imageData[60][80], int width, int height, int pitch)
{
    uint8_t file[14] = {
        'B','M', // magic
        0,0,0,0, // size in bytes
        0,0, // app data
        0,0, // app data
        54,0,0,0 // start of data offset
    };
    uint8_t info[40] = {
        40,0,0,0, // info hd size
        0,0,0,0, // width
        0,0,0,0, // heigth
        1,0, // number color planes
        24,0, // bits per pixel
        0,0,0,0, // compression is none
        0,0,0,0, // image bits size
        0x13,0x0B,0,0, // horz resoluition in pixel / m
        0x13,0x0B,0,0, // vert resolutions (0x03C3 = 96 dpi, 0x0B13 = 72 dpi)
        0,0,0,0, // #colors in pallete
        0,0,0,0, // #important colors
    };

    uint32_t padSize  = (4-(width*3)%4)%4;
    uint32_t sizeData = width*height*3 + height*padSize;
    uint32_t sizeAll  = sizeData + sizeof(file) + sizeof(info);

    file[ 2] = (uint8_t)((sizeAll      ) & 0xFF);
    file[ 3] = (uint8_t)((sizeAll >>  8) & 0xFF);
    file[ 4] = (uint8_t)((sizeAll >> 16) & 0xFF);
    file[ 5] = (uint8_t)((sizeAll >> 24) & 0xFF);
    info[ 4] = (uint8_t)((width      ) & 0xFF);
    info[ 5] = (uint8_t)((width >>  8) & 0xFF);
    info[ 6] = (uint8_t)((width >> 16) & 0xFF);
    info[ 7] = (uint8_t)((width >> 24) & 0xFF);
    info[ 8] = (uint8_t)((height      ) & 0xFF);
    info[ 9] = (uint8_t)((height >>  8) & 0xFF);
    info[10] = (uint8_t)((height >> 16) & 0xFF);
    info[11] = (uint8_t)((height >> 24) & 0xFF);
    info[20] = (uint8_t)((sizeData      ) & 0xFF);
    info[21] = (uint8_t)((sizeData >>  8) & 0xFF);
    info[22] = (uint8_t)((sizeData >> 16) & 0xFF);
    info[23] = (uint8_t)((sizeData >> 24) & 0xFF);

    res=f_open(&bmpFile, "image_now.bmp",   FA_CREATE_ALWAYS| FA_WRITE );
    if(res!= FR_OK) {
        pc.printf("Failed to open file\n");
    }

    res=f_write(&bmpFile, (uint8_t *)file, 14, &bytesread);
    if(res!= FR_OK) {
        pc.printf("Failed to write file\n");
    }
    
 
    res=f_write(&bmpFile, (uint8_t *)info, 40, &bytesread);
    if(res!= FR_OK) {
        pc.printf("Failed to write file\n");
    }
    

    uint8_t pad[3]= {0,0,0};
    imageData += (height - 1) * pitch;

    uint8_t ok;

    for (int y =0; y < 60; y++) {

        for (int x = 0; x < 80; x++) {
           
             ok=uint8_t (imageData[y][x]>>8 & 0xFF);
             pc.printf("ok");
             f_write(&bmpFile, (uint8_t *)ok,1, &bytesread);
             f_write(&bmpFile, (uint8_t *)ok,1, &bytesread);
             f_write(&bmpFile, (uint8_t *)ok,1, &bytesread);
       }

        
        res=f_write(&bmpFile, (uint8_t *)pad, 3, &bytesread);
        if(res!= FR_OK) {
          pc.printf("Failed to write file\n");
        }
        imageData -= pitch;
          }

    f_close(&bmpFile);
}

The problems is related to the fact that the sensor's image is a 60x80 matrix of unsigned int where each pixel is 16 bit long and represents a level of greyscale, So I must convert each pixel in to a vector of three 8 bit long equal elements [R,G,B] (having the grey) to create a bit map image. To that end I realized the 'writeBMPFile' function, but when the program executionon on the micro-controller meets the last 'for loop' of the function, where the single element of the image's matrix is assigned to a variable (similar to vector's element) (that's the pixel) that could be written on SD card for three times to create the RGB pixel, the micro stops working and it is necessary to reset the eval board. I noticed that this problem is present in correspondence of the assignment 'ok=uint8_t (imageData[y][x]>>8 & 0xFF)' , where program stops being executed, and it don't persist when the f_write is deleted. Does anyone have any ideas about it?

Be the first to answer this question.