This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

HAL Flash Write DMA usage

Other Parts Discussed in Thread: CC2540

HalFlashWrite puts the DMA to work, but then it puts the CPU in a while loop until the DMA operation is finished instead of freeing the CPU to go into low power mode I think

The line " while (FCTL & 0x80); " seems to imply that we wait for the DMA to finish, but this bit should go high after the first word is written. I'm not sure what we gain from this. Its a false sense of security. 

If the CPU really is tied up while the DMA is in operation, I need to know why so that any changes don't interrupt the rest of the stack. If not, then why was "while (FCTL & 0x80); " written? 

  • Hi Peter,

    The DMA is used to write to flash because the CPU will get stalled if it runs out of Flash and the Flash write is activated. The alternative is to move the write algorithm to RAM and execute it out of there.

    It's inefficient to set up the DMA to read small blocks of data, while it becomes more efficient with larger blocks. I guess simplicity and fast small reads were given preference when HALFlashRead was written.

    I don't know why the address argument is given differently in the two functions. There is of course a direct mapping between them for any given device.

    HALFlashWrite does not need to set the XDATA bank as the XDATA addressing is not used to write to flash. The 16-bit FADDRL/H register sets the physical address for flash write/erase operations directly in the flash controller.

    Peder

  • Hello Peder,

    I would like to use HalFlashRead() and HalFlashWrite() on cc2540 128K internal flash and I cannot understand how to declare the parameters of addresses for these two functions. I would like to write something and then to read it , in order to veri fy the process.

    I follow the the user guide : www.ti.com/.../swru191f.pdf
    but it cannot help me . I see especially the pages 25-28, as well as the Chapter 6. For example, in the section 6.3.1, there is a part of code on how to erase a page, and this concerns the HalFlashErase() which is declared in the hal_flash.c of SimpleBLEPeripheral (TI stack), for example. There is no example on how to call the HalFlashRead() or HalFlashWrite(), with the correct parameters.

    Could you please help with any guideline or indicating to me other source in order to understand the read/write process of internal flash of cc2540 of 128K flash?

    Thank you in advance.
  • Hi Emocs,

    I found an example of simple buffer write/read using HalFlashRead() and HalFlashWrite(). I hope that helps.

    /* Hal Drivers */
    #include "hal_types.h"
    #include "hal_flash.h"
    
    #define NUM_BUFFER_BYTES 4
    #define PATTERN 0xAA
    
    
    void per_memset(uint8* dataBuffer, uint8 data, uint16 numBytes) {
      for (uint16 i=0; i<numBytes; i++) {
        dataBuffer[i] = data;
      }
    }
    
    uint16 _errors = 0;
    
    uint16 per_memcheck(uint8* dataBuffer, uint8 data, uint16 numBytes) {
    
      for (uint16 i=0; i<numBytes; i++) {
        if (dataBuffer[i] != data) {
          _errors++;
        }
      }
      return _errors;
    }
    
    
      uint8 dataBuffer[NUM_BUFFER_BYTES]; 
      uint8 dataBufferVerif[NUM_BUFFER_BYTES]; 
    
    uint16 main( void )
    {
      
      HAL_BOARD_INIT();
      
      MEMCTR = 0x00;
      
      P1 = 0x00;
      P1DIR = 0x03;
      uint8 pageNum = 1;
      
      
      per_memset( dataBuffer, PATTERN, NUM_BUFFER_BYTES );
      
      HalFlashWrite( (uint16)pageNum << 9, dataBuffer, NUM_BUFFER_BYTES / HAL_FLASH_WORD_SIZE );
      
      HalFlashRead( pageNum, 0, dataBufferVerif, NUM_BUFFER_BYTES );
    
      uint16 rd_error = per_memcheck( dataBufferVerif, PATTERN, NUM_BUFFER_BYTES );
     
      if (rd_error) {
        P1 |= 0x01;
      } else {
        P1 |= 0x02;
      }
        
      
    
      return 0;
    }