MQX SPI chip select problem

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

MQX SPI chip select problem

Jump to solution
1,320 Views
pk2
Contributor III

I am having a problem using the SPI CS signals in my K60 application.  My application uses SPI0 and addresses three devices using 3 CS signals (CS0, CS1, and CS2).  I read some other posts here and thought I had things working for CS0 and CS1.  However CS2 does not toggle when it should.   I am using MQX4.1 and CW 10.4. 

In my bsp I changed init_gpio.c by modifying case 0 in the _bsp_dspi_io_init function as shown below.

_mqx_int _bsp_dspi_io_init

(

    uint32_t dev_num

)

{

    SIM_MemMapPtr   sim = SIM_BASE_PTR;

    PORT_MemMapPtr  pctl;

    switch (dev_num)

    {

        case 0:

           

//Modified Code for 3 chip select SPI0 which is on PORTC of the K60.

           

           

            /* Configure GPIOC for DSPI0 peripheral function */

           

            pctl = (PORT_MemMapPtr)PORTC_BASE_PTR;

            pctl->PCR[4] = PORT_PCR_MUX(2);     /* DSPI0.PCS0   */

            pctl->PCR[3] = PORT_PCR_MUX(2);     /* DSPI0.PCS1   */

            pctl->PCR[2] = PORT_PCR_MUX(2);     /* DSPI0.PCS2   */

            pctl->PCR[5] = PORT_PCR_MUX(2);     /* DSPI0.SCK    */

            pctl->PCR[6] = PORT_PCR_MUX(2);     /* DSPI0.SOUT   */

            pctl->PCR[7] = PORT_PCR_MUX(2);     /* DSPI0.SIN    */

            /* Enable clock gate to DSPI0 module */

            sim->SCGC6 |= SIM_SCGC6_DSPI0_MASK;

            break;

           

In my application I open/close the SPI0 port depending upon which of the 3 devices I need to address. Using the below code:

  switch (chip_sel)

  {

case (0):

          ezp_spi_handle = fopen("spi0:1", NULL);

              break;

  case (1):

            ezp_spi_handle = fopen("spi0:2", NULL);

            break;

  case (2):

            ezp_spi_handle = fopen("spi0:3", NULL);

          break;

  default:

            break;                //

    

  }

 

  When the application runs, I get a valid file handle for all 3 cases.  When "spi0:3" is used, I am expecting CS2 to toggle but CS 1 toggles!!

 

  Are there any examples of how to properly configure a BSP to use more than 2 chip SPI select signals with MQX.  

Tags (4)
0 Kudos
1 Solution
704 Views
pk2
Contributor III

In "spi0:n", the n is a bit mask that gets shifted into some chip select register.  In my case, spi0:1 = CS0, spi0:2 = CS1, and spi0:4 = CS2.   When spi0:3 was specified, both CS0 and CS1 are selected.  The correct code is

case (0):

            ezp_spi_handle = fopen("spi0:1", NULL);

              break;

  case (1):

            ezp_spi_handle = fopen("spi0:2", NULL);

            break;

  case (2):

            ezp_spi_handle = fopen("spi0:4", NULL);

          break;

View solution in original post

0 Kudos
2 Replies
705 Views
pk2
Contributor III

In "spi0:n", the n is a bit mask that gets shifted into some chip select register.  In my case, spi0:1 = CS0, spi0:2 = CS1, and spi0:4 = CS2.   When spi0:3 was specified, both CS0 and CS1 are selected.  The correct code is

case (0):

            ezp_spi_handle = fopen("spi0:1", NULL);

              break;

  case (1):

            ezp_spi_handle = fopen("spi0:2", NULL);

            break;

  case (2):

            ezp_spi_handle = fopen("spi0:4", NULL);

          break;

0 Kudos
704 Views
danielchen
NXP TechSupport
NXP TechSupport

Hi

I think the index should start from 0 when you  fopen the device

  case (0):

            ezp_spi_handle = fopen("spi0:0", NULL);

              break;

  case (1):

            ezp_spi_handle = fopen("spi0:1", NULL);

            break;

  case (2):

            ezp_spi_handle = fopen("spi0:2", NULL);

          break;


Have a great day,
Daniel

0 Kudos