+ Post New Thread
Results 1 to 11 of 11
  1. #1
    Advanced Member level 4
    Points: 7,065, Level: 20
    Achievements:
    7 years registered

    Join Date
    Jun 2009
    Location
    California
    Posts
    1,308
    Helped
    22 / 22
    Points
    7,065
    Level
    20

    How to fix FatFS compilation on STM32 ?

    Guys,

    I tried to compile FatFS on STM32 but got this error, how to fix it ?
    thanks

    Code:
    ..\Middlewares\Third_Party\FatFs\src\diskio.c(443): error:  #167: argument of type "const BYTE *" is incompatible with parameter of type "BYTE *"

    •   AltAdvertisement

        
       

  2. #2
    Super Moderator
    Points: 260,224, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,441
    Helped
    13826 / 13826
    Points
    260,224
    Level
    100

    Re: How to fix FatFS compilation on STM32 ?

    Probably a problem of compiler options.

    FatFs is using const attribute e.g. for write buffers, if you want to assign a non constant object to it, you need either to relax type checking or use a type cast.



  3. #3
    Advanced Member level 4
    Points: 7,065, Level: 20
    Achievements:
    7 years registered

    Join Date
    Jun 2009
    Location
    California
    Posts
    1,308
    Helped
    22 / 22
    Points
    7,065
    Level
    20

    Re: How to fix FatFS compilation on STM32 ?

    Quote Originally Posted by FvM View Post
    Probably a problem of compiler options.

    FatFs is using const attribute e.g. for write buffers, if you want to assign a non constant object to it, you need either to relax type checking or use a type cast.
    Here's the function :
    Code:
    #if _USE_WRITE == 1
    DRESULT disk_write(BYTE pdrv, 
      const 
    	BYTE *buff, DWORD sector, UINT count)
    {
      DRESULT res;
    	if (pdrv || !count) return RES_PARERR;
    	if (Stat & STA_NOINIT) return RES_NOTRDY;
    	if (Stat & STA_PROTECT) return RES_WRPRT;
    
    	if (!(CardType & CT_BLOCK)) sector *= 512;	/* Convert to byte address if needed */
    
    	if (count == 1) {	/* Single block write */
    		if ((send_cmd(CMD24, sector) == 0)	/* WRITE_BLOCK */
    			&& xmit_datablock(buff, 0xFE))
    			count = 0;
    	}
    	else {				/* Multiple block write */
    		if (CardType & CT_SDC) send_cmd(ACMD23, count);
    		if (send_cmd(CMD25, sector) == 0) {	/* WRITE_MULTIPLE_BLOCK */
    			do {
    				if (!xmit_datablock(buff, 0xFC)) break;
    				buff += 512;
    			} while (--count);
    			if (!xmit_datablock(0, 0xFD))	/* STOP_TRAN token */
    				count = 1;
    		}
    	}
    	deselect();
    
    	return count ? RES_ERROR : RES_OK;
     	  //res = disk.drv[pdrv]->disk_write(buff, sector, count);
      //return res;
    }
    #endif /* _USE_WRITE == 1 */
    I got error on :
    xmit_datablock(buff, 0xFE))

    ....
    you need either to relax type checking or use a type cast


    How can I do that ?



  4. #4
    Super Moderator
    Points: 260,224, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,441
    Helped
    13826 / 13826
    Points
    260,224
    Level
    100

    Re: How to fix FatFS compilation on STM32 ?

    In recent fatfs implementations that I reviewed, xmit_datablock() has also const BYTE * buff parameter, thus the problem doesn't occur.

    I'm not sure if GCC has a global option to relax type checking for const pointers. I would use a type cast (BYTE *).



    •   AltAdvertisement

        
       

  5. #5
    Advanced Member level 4
    Points: 7,065, Level: 20
    Achievements:
    7 years registered

    Join Date
    Jun 2009
    Location
    California
    Posts
    1,308
    Helped
    22 / 22
    Points
    7,065
    Level
    20

    Re: How to fix FatFS compilation on STM32 ?

    Quote Originally Posted by FvM View Post
    In recent fatfs implementations that I reviewed, xmit_datablock() has also const BYTE * buff parameter, thus the problem doesn't occur.

    I'm not sure if GCC has a global option to relax type checking for const pointers. I would use a type cast (BYTE *).
    if I put :
    Code:
    static
    int xmit_datablock (
    	  const BYTE *buff,	/* 512 byte data block to be transmitted */
    	BYTE token			/* Data/Stop token */
    )
    {
    	BYTE resp;
    
    
    	if (!wait_ready(500)) return 0;
    
    	xchg_spi(token);					/* Xmit data token */
    	if (token != 0xFD) {	/* Is data token */
    		xmit_spi_multi(buff, 512);		/* Xmit the data block to the MMC */
    		xchg_spi(0xFF);					/* CRC (Dummy) */
    		xchg_spi(0xFF);
    		resp = xchg_spi(0xFF);			/* Reveive data response */
    		if ((resp & 0x1F) != 0x05)		/* If not accepted, return with error */
    			return 0;
    	}
    
    	return 1;
    }
    I got :
    Code:
    ..\Middlewares\Third_Party\FatFs\src\diskio.c(237): error:  #167: argument of type "const BYTE *" is incompatible with parameter of type "uint8_t *"
    \
    because of :
    xmit_spi_multi(buff, 512);

    Code:
    /* Send a data block fast */
    static
    void xmit_spi_multi (
    	 uint8_t *p,	/* Data block to be sent */
    	UINT cnt		
    )
    {
    	while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY); 
    	HAL_SPI_Transmit(&hspi2,p,cnt,100);
    }
    
    /* Receive a data block fast */
    static
    void rcvr_spi_multi (
    	BYTE *p,	/* Data buffer */
    	UINT cnt	/* Size of data block (must be multiple of 2) */
    )
    {
    	while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY); 
    	HAL_SPI_Receive(&hspi2,p,cnt,100);
    }
    I don't think I can change uint8_t *p ....to const BYTE....because it's from HAL STM32CubeMX...

    - - - Updated - - -

    I change to BYTE, I got :
    Code:
    ..\Middlewares\Third_Party\FatFs\src\diskio.c(237): error:  #167: argument of type "const BYTE *" is incompatible with parameter of type "BYTE *"
    - - - Updated - - -

    Code:
    static
    void xmit_spi_multi (
    	 const BYTE *p,	/* Data block to be sent */
    	UINT cnt		
    )
    {
    	while(HAL_SPI_GetState(&hspi2) != HAL_SPI_STATE_READY); 
    	HAL_SPI_Transmit(&hspi2,p,cnt,100);
    }
    
    
    }
    ..\Middlewares\Third_Party\FatFs\src\diskio.c(132) : error: #167: argument of type "const BYTE *" is incompatible with parameter of type "uint8_t *"



    •   AltAdvertisement

        
       

  6. #6
    Super Moderator
    Points: 260,224, Level: 100
    Awards:
    1st Helpful Member

    Join Date
    Jan 2008
    Location
    Bochum, Germany
    Posts
    45,441
    Helped
    13826 / 13826
    Points
    260,224
    Level
    100

    Re: How to fix FatFS compilation on STM32 ?

    Just normal C stuff...



  7. #7
    Advanced Member level 4
    Points: 7,065, Level: 20
    Achievements:
    7 years registered

    Join Date
    Jun 2009
    Location
    California
    Posts
    1,308
    Helped
    22 / 22
    Points
    7,065
    Level
    20

    Re: How to fix FatFS compilation on STM32 ?

    Quote Originally Posted by FvM View Post
    Just normal C stuff...
    How can I fix it without changing the variable type ? thanks



  8. #8
    Full Member level 3
    Points: 4,634, Level: 16

    Join Date
    Apr 2001
    Location
    California, USA
    Posts
    188
    Helped
    25 / 25
    Points
    4,634
    Level
    16

    Re: How to fix FatFS compilation on STM32 ?

    Have you tried to cast const BTYE * to (BYTE *)?



  9. #9
    Advanced Member level 4
    Points: 7,065, Level: 20
    Achievements:
    7 years registered

    Join Date
    Jun 2009
    Location
    California
    Posts
    1,308
    Helped
    22 / 22
    Points
    7,065
    Level
    20

    Re: How to fix FatFS compilation on STM32 ?

    Quote Originally Posted by cfant View Post
    Have you tried to cast const BTYE * to (BYTE *)?
    Code:
    static
    int xmit_datablock (
    	  const BYTE *buff,	/* 512 byte data block to be transmitted */
    	BYTE token			/* Data/Stop token */
    )
    {
    	BYTE resp;
    
    
    	if (!wait_ready(500)) return 0;
    
    	xchg_spi(token);					/* Xmit data token */
    	if (token != 0xFD) {	/* Is data token */
    		xmit_spi_multi(buff, 512);		/* Xmit the data block to the MMC */
    		xchg_spi(0xFF);					/* CRC (Dummy) */
    		xchg_spi(0xFF);
    		resp = xchg_spi(0xFF);			/* Reveive data response */
    		if ((resp & 0x1F) != 0x05)		/* If not accepted, return with error */
    			return 0;
    	}
    
    	return 1;
    }
    const BYTE *buff to (BYTE *buff) ??

    - - - Updated - - -

    It works fine if I erased "const"
    Code:
    //#if	_USE_WRITE
    static
    int xmit_datablock (
    	   BYTE *buff,	/* 512 byte data block to be transmitted */
    	BYTE token			/* Data/Stop token */
    )
    {
    	BYTE resp;
    
    
    	if (!wait_ready(500)) return 0;
    
    	xchg_spi(token);					/* Xmit data token */
    	if (token != 0xFD) {	/* Is data token */
    		xmit_spi_multi(buff, 512);		/* Xmit the data block to the MMC */
    		xchg_spi(0xFF);					/* CRC (Dummy) */
    		xchg_spi(0xFF);
    		resp = xchg_spi(0xFF);			/* Reveive data response */
    		if ((resp & 0x1F) != 0x05)		/* If not accepted, return with error */
    			return 0;
    	}
    
    	return 1;
    }
    - - - Updated - - -

    I got this error if I deleted all const :

    ..\Middlewares\Third_Party\FatFs\src\ff.c(2728): error: #167: argument of type "const BYTE *" is incompatible with parameter of type "BYTE *"



  10. #10
    Advanced Member level 4
    Points: 7,828, Level: 21

    Join Date
    Jan 2015
    Posts
    1,097
    Helped
    346 / 346
    Points
    7,828
    Level
    21

    Re: How to fix FatFS compilation on STM32 ?

    Did you also update the declaration in the header?
    Susan



    •   AltAdvertisement

        
       

  11. #11
    Full Member level 1
    Points: 2,800, Level: 12
    gamegurus's Avatar
    Join Date
    Jan 2005
    Posts
    97
    Helped
    19 / 19
    Points
    2,800
    Level
    12

    Re: How to fix FatFS compilation on STM32 ?

    Consider using CMSIS compliant ARM library to avoid such issues.



--[[ ]]--