stm32ram16-bit8-bit

What happens if I only write 1 byte to an external 16bit register of SDRAM


I am working on a project which uses a STM32F429ZIT6 (32-bit) with an external 64MB of SDRAM (4M*16-bit registers). In the project I have seen the SDRAM accessed as:

void LCD_mirror(void)
{
unsigned long L,p;
unsigned int rgbT,rgbB;
  for(L=0;L<LCD_PIXEL_HEIGHT/2;L++)
    for(p=0;p<LCD_PIXEL_WIDTH;p++)
    {
      rgbT=*(__IO uint16_t*) (CurrentFrameBuffer + ((LCD_PIXEL_HEIGHT-L-1)*LCD_PIXEL_WIDTH)*2 + (2*p) );
      rgbB=*(__IO uint16_t*) (CurrentFrameBuffer + (L*LCD_PIXEL_WIDTH)*2 + (2*p) );
      *(__IO uint16_t*) (CurrentFrameBuffer + (L*LCD_PIXEL_WIDTH)*2 + (2*p) ) = rgbT;
      *(__IO uint16_t*) (CurrentFrameBuffer + ((LCD_PIXEL_HEIGHT-L-1)*LCD_PIXEL_WIDTH)*2 + (2*p) ) = rgbB;
    }
}

Where CurrentFrameBuffer is the start address of the SDRAM Bank 2(0xD0000000).

This is working, but I am wondering what happens if I only write 1 byte to the 16-bit register? Will the remaining 8 bits just be unused? I want to be able to write 8-bit and 16-bit without having a lot of unused space.


Solution

  • The most accurate answer is "it depends". You are welcome to make some synthetic test, you can use SysTick clocked from CPU to measure time. I can, however, give you a few pointers with regards to what you need to consider.

    First, it depends on what exactly the SDRAM IC supports, the answer to that should be in its datasheet. Additionally, every SDRAM needs to be configured upon power on (MCU writes a few commands to it before it can work with it, which I expect you know). Which means that SDRAM IC must be able to support your communication mode, but also must be initialized accordingly.

    Another variable here is the MCU's SDRAM periphal itself. It could have special configuration for 8-bit, 16-bit or 32-bit operation. Some MCU peripheral registers, including data registers, could have fixed width access (such as accessing them by byte could be invalid). This information should be in the description of said registers.

    Next point you want to consider is that every single SDRAM access has an unavoidable fixed overhead - the hardware needs to send row and column numbers over the bus, and this overhead is fixed size regardless of how much data will be sent. So in that respect the more data is sent at once, the better. Many single-byte reads will definitely waste a lot of cycles repeating rows and columns. Combine it with the fact that STM32 RAM (internal) reads always take the same time regardless if it's 8-bit read or 16-bit read or 32-bit read (I wrote a test for it), and all in all single byte operations start to sound really wasteful on many levels, while 16-bit reads and writes can often have literally no overhead at all over 8-bit reads. So what you call "unused space" could literally have no negative effect, but would require half the row/column transmissions to SDRAM.

    Additionally, data alignment could be an issue (on MCU side or on SDRAM side), if you roll with 8-bit. It could manifest differently, from not supported by SDRAM IC to taking extra time (but could be no overhead too).

    The only true way to know is to write a test and see for yourself, but all in all I would expect 8-bit configuration (if it's available) to perform worse, especially if you have a lot of reads/writes, which is a reasonable assumption for a framebuffer where you draw and which needs to be fetched and sent to the screen.