Forum Discussion

naand's avatar
naand
Icon for New Contributor rankNew Contributor
1 year ago

NIOS II - FW doesn't work with compiler optimization turned on

Dear support team,
I have a problem with the behavior of the firmware if the project is built in release mode with optimization enabled.
The problem occurs during the write-to-flash routine. I use Generic Serial Flash Interface Intel® FPGA IP and was inspired by the intel_generic_serial_flash_interface_top example to create the code to write a block of data to the Flash. In the example, the memcpy function transfers data into the Flash. I don't know if the critical point of the function is the memcpy, but if optimization is turned on, the firmware seems to perform no write operation to the Flash: the function returns no errors, so it makes me believe that everything was executed correctly, but after a check, there wasn't the expected data in memory. While with the same code built with optimization turned off, the firmware works properly.
The flash_write_function, which generates the problem, is attached.
My questions are:
  1. Why this behavior?
  2. Is it possible to use memcpy as a write function on Flash?
  3. Is it possible to turn off optimization for a block of code?
I'm using:
  • Quartus Prima Version 20.1.1
  • Nios II/f processor
  • Eclipse IDE (version: Mars.2 Release (4.5.2) - build id: 20160218-0600)
  • Toolchain used:

The NIOS data cache is disabled, whereas the instruction cache is enabled (see below). I use only the IORD and IOWR functions to perform registers and peripherals accesses.

Here are the Nios II Application Properties:
Thanks,

Best regards.

naand

15 Replies

  • Hi,

    We are glad that pragma is working.

    Currently, this is the only optimization bug we are aware of. In the meantime, can we know what flash are you using? (the function will change based on flash vendor)

    It seems using volatile not helping in here maybe the function is too small not because variable is optimized. Can you try this ?

    https://stackoverflow.com/questions/1474030/how-can-i-tell-gcc-not-to-inline-a-function#:~:text=You%20want%20the%20gcc%20%2Dspecific,the%20function%20call%20is%20live.

    By the way, can you share your pragma example? We are interested to understand how it is used.

    Thank you.

  • naand's avatar
    naand
    Icon for New Contributor rankNew Contributor

    Hi,

    I'm using the Micron Flash MT25QL256ABA8E12-1SIT.

    I tried the solution proposed in the StackOverflow link: the __attribute__ ((noinline)) works, so this is another solution.

    My problem is only with the optimization of the flash_read_flag_status_register function, so this is my example of the #pragma directive:

    #pragma GCC push_options

    #pragma GCC optimize("O0")
    alt_u32 flash_read_flag_status_register(T_flash_state *st)
    {
    IOWR(st->csr_base, FLASH_CSR_FL_CMD_SET, 0x00001870);
    IOWR(st->csr_base, FLASH_CSR_FL_CMD_CTRL, 0x1);
    return IORD(st->csr_base, FLASH_CSR_FL_CMD_RD0);
    }
    #pragma GCC pop_options

    My code works even with the highest level of GCC optimization (-O3) if I use the #pragma solution.

    Thanks.

    Best Regards,

    naand

  • Hi,


    We are glad to hear that both #pragma and __attribute__ ((noinline)) works for your issue.

    As for now problem had been solved, we will continue to transition this thread to community support.

    Thank you for the example given, will be helpful for others in future.


    I’m glad that your question has been addressed, I now transition this thread to community support. If you have a new question, Please login to ‘https://supporttickets.intel.com’, view details of the desire request, and post a feed/response within the next 15 days to allow me to continue to support you. After 15 days, this thread will be transitioned to community support. The community users will be able to help you on your follow-up questions.