Help with QSPI write function

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Help with QSPI write function

Jump to solution
2,003 Views
JaimeR
Contributor III

I am following the QSPI example about the EEPROM and the M52259EVB, I have an oscilloscope plugged to QSPI_DIN, QSPI_SCK and QSPI_PCS0. I am debugging the example but when I execute the function       result = write(fd, send_buffer, len);    clock pin and DOUT pin never moves.

 

What could be the problem if I am executing from example provided from freescale?

Does anyone have had the same problem?

Does anyone have an simple and concrete example of writing and reading from QSPI?

 

Regards

 

0 Kudos
1 Solution
667 Views
CarlFST60L
Senior Contributor II

Hi,

 

I havnt had any problems with my SPI drivers, though I didnt really play with the demo so much.

 

Here is some code copied out of my SPI driver which is based on the example supplied

//-----------------------------------------------------------------------------------------------------------------------
//Compile time tests
#if ! BSPCFG_ENABLE_QSPI
#error This application requires BSPCFG_ENABLE_QSPI defined non-zero in user_config.h. Please recompile kernel with this option.
#endif
//Global to driver file
static FILE_PTR SPI_fd;      //CDN10072009 used to store the file pointer to the QSPI driver
const char SLIC_SPI_PORT[] = "qspi:";    //CDN16072009

uchar SPI_Init(void){
 uchar byte = 0;
 uint_32 param = 0;

 //Open SPI
  SPI_fd = fopen(SLIC_SPI_PORT, (pointer)(QSPI_DEVICE_MASTER_MODE));
  if (SPI_fd == NULL)
  {
    printf("\n Failed to open the SPI Controller: QSPI ALLOCATION ERROR...");
   return FALSE;
  }

  // Set QSPI Baud rate
  param = 4000000;
  printf ("\nChanging the Baud frequency to 4000000Hz...");
  ioctl(SPI_fd, IO_IOCTL_SERIAL_SET_BAUD, &param);  

  // Display current Baud
  ioctl(SPI_fd, IO_IOCTL_SERIAL_GET_BAUD, &param);
  printf (" QSPI freq is now %dHz.\n", param);
 
  //Set transfer modes
  param = (QSPI_TRANSFER_MODE_2);
  ioctl(SPI_fd, IO_IOCTL_QSPI_SET_TRANSFER_MODE, &param);

       //Start with CS in the high state as it is no preset to high.
  param = QSPI_CHIP_SELECT_SET_1;
  ioctl(SPI_fd, IO_IOCTL_QSPI_SET_CHIP_SELECT_STATE, &param);

        return TRUE;
}
void SPI_Write_Byte(uchar){
 //write to SPI, CS is handled automatically.
  if(write(SPI_fd, &byte, 1) == IO_ERROR) printf("\nERROR: QSPI WRITE
FAILED in SPI_Init.\n");
}
byte SPI_Read_Byte(void){
    if(read(SPI_fd, &byte, 1) == IO_ERROR) printf("\nERROR: QSPI WRITE FAILED.\n");

 printf("SPI DOut: %X\n", byte);
        return byte;
}

 

Also, be sure to re compilie the BSP/PSP with the  BSPCFG_ENABLE_QSPI otherwise it wont work


To test that the SPI is working, you can simply tie the SDI pin to ground and the result of the above code will print 0x00, tie it to 3.3V and you will see 0xFF.

 

Message Edited by CarlFST60L on 2009-07-22 09:23 AM
Message Edited by CarlFST60L on 2009-07-22 09:26 AM

View solution in original post

0 Kudos
3 Replies
668 Views
CarlFST60L
Senior Contributor II

Hi,

 

I havnt had any problems with my SPI drivers, though I didnt really play with the demo so much.

 

Here is some code copied out of my SPI driver which is based on the example supplied

//-----------------------------------------------------------------------------------------------------------------------
//Compile time tests
#if ! BSPCFG_ENABLE_QSPI
#error This application requires BSPCFG_ENABLE_QSPI defined non-zero in user_config.h. Please recompile kernel with this option.
#endif
//Global to driver file
static FILE_PTR SPI_fd;      //CDN10072009 used to store the file pointer to the QSPI driver
const char SLIC_SPI_PORT[] = "qspi:";    //CDN16072009

uchar SPI_Init(void){
 uchar byte = 0;
 uint_32 param = 0;

 //Open SPI
  SPI_fd = fopen(SLIC_SPI_PORT, (pointer)(QSPI_DEVICE_MASTER_MODE));
  if (SPI_fd == NULL)
  {
    printf("\n Failed to open the SPI Controller: QSPI ALLOCATION ERROR...");
   return FALSE;
  }

  // Set QSPI Baud rate
  param = 4000000;
  printf ("\nChanging the Baud frequency to 4000000Hz...");
  ioctl(SPI_fd, IO_IOCTL_SERIAL_SET_BAUD, &param);  

  // Display current Baud
  ioctl(SPI_fd, IO_IOCTL_SERIAL_GET_BAUD, &param);
  printf (" QSPI freq is now %dHz.\n", param);
 
  //Set transfer modes
  param = (QSPI_TRANSFER_MODE_2);
  ioctl(SPI_fd, IO_IOCTL_QSPI_SET_TRANSFER_MODE, &param);

       //Start with CS in the high state as it is no preset to high.
  param = QSPI_CHIP_SELECT_SET_1;
  ioctl(SPI_fd, IO_IOCTL_QSPI_SET_CHIP_SELECT_STATE, &param);

        return TRUE;
}
void SPI_Write_Byte(uchar){
 //write to SPI, CS is handled automatically.
  if(write(SPI_fd, &byte, 1) == IO_ERROR) printf("\nERROR: QSPI WRITE
FAILED in SPI_Init.\n");
}
byte SPI_Read_Byte(void){
    if(read(SPI_fd, &byte, 1) == IO_ERROR) printf("\nERROR: QSPI WRITE FAILED.\n");

 printf("SPI DOut: %X\n", byte);
        return byte;
}

 

Also, be sure to re compilie the BSP/PSP with the  BSPCFG_ENABLE_QSPI otherwise it wont work


To test that the SPI is working, you can simply tie the SDI pin to ground and the result of the above code will print 0x00, tie it to 3.3V and you will see 0xFF.

 

Message Edited by CarlFST60L on 2009-07-22 09:23 AM
Message Edited by CarlFST60L on 2009-07-22 09:26 AM
0 Kudos
667 Views
JaimeR
Contributor III

Thanks for the code. I am testing it right now and it is working fine(I had a problem with the baud rate), I see you wrote

    //write to SPI, CS is handled automatically.

 What does this mean?, where do you define which CS is going to be active?

Also, in my application I will be using 3 SPI devices and 2 Chip Selects will be GPIO pins from the MCU(

since CS 2 and 3 pins will be used for USB purpose), so I need to avoid the usage of CS 2 and 3 for

Chip Select and use other 2 pins instead. Is this possible?, Any ideas of where to make the change?

 

0 Kudos
667 Views
CarlFST60L
Senior Contributor II

Hi Jamie,

 

When I said "CS is handled automatically", I just mean that it will toggle the selected CS (QSPI_CSX) automatically. Actually, if you have the CS clear prior to read/write, it will not clear on entry set on exit, it will ignore it, but if its set on entry it will clear / set it.

 

As for changing the SPI CS, if you right click one of the IO_IOCTL_QSPI_XXXXX definitions and right click and select "go to macro declaration of IO_IOCTL_QSPI_SET_CHIP_SELECT_STATE" it will take you straight to qspi.h where all the options are.

 

For making your own CS on other pins, you will need to follow back the code that looks after "QSPI_CS0" and work out how that works and modifiy and make your own BSP and/or PSP as the QSPI doesnt appear to have a way of installing the SPI. You may actually need to modifiy it completely if the processor looks after the CS as part of its internal SPI interface... For this you will need to work out who the MQX drivers work.

0 Kudos