TWRADCDAC-LTC in MQX 4.0

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

TWRADCDAC-LTC in MQX 4.0

Jump to solution
715 Views
aisebastian
Contributor II

Hi everyone,

I need to read from TWRADCDAC-LTC from a K60F120M board, and I am doing it via spi0. I have a working setup with MQX 3.8, and now I am trying to port it to MQX 4.0, but I can't get the spi driver install to work.

Here is working code for MQX 3.8:

//Prepare the initialization struct  

   init->CHANNEL = 2;

   init->CS = cspin;

   init->TRANSFER_MODE = transfermode;

   init->BAUD_RATE = baudrate;

   init->CLOCK_SPEED = sysclock;

   init->CLOCK_POL_PHASE = clockmode;

   init->RX_BUFFER_SIZE = 1;

   init->TX_BUFFER_SIZE = 1;

  

   //Set the pins to their secondary function

  /* clock gate */

  SIM_SCGC6 |= SIM_SCGC6_DSPI0_MASK|SIM_SCGC6_DSPI1_MASK;

  SIM_SCGC3 |= SIM_SCGC3_DSPI2_MASK;

  SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTB_MASK;

  /* pin mux */

  PORTD_PCR11 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR11 |= PORT_PCR_MUX(2); //SPI2_PCS0

  PORTD_PCR12 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR12 |= PORT_PCR_MUX(2); //SPI2_SCK

  PORTD_PCR13 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR13 |= PORT_PCR_MUX(2); //SPI2_SOUT

  PORTD_PCR14 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR14 |= PORT_PCR_MUX(2); //SPI2_SIN  

  

   //The fopen call doesn't install the driver with our initialization

   _dspi_polled_install("spi0:", init); //Modified-for-Kinetis

SPI_INIT_STRUCT init_struct;

  SPI_PARAM_STRUCT params;

  DSPI_INIT_STRUCT dinit;

  dinit.CHANNEL = 2;

  dinit.CLOCK_SOURCE = sysclock;

  params.BAUDRATE = baudrate;

  params.CS = cspin;

  params.MODE = clockmode;

  params.ATTR = DSPI_ATTR_USE_ISR;

  params.DUMMY_PATTERN = 0xFFFFFFFF;

  init_struct.DEVIF = &_spi_dspi_devif;

  init_struct.DEVIF_INIT = &dinit;

  init_struct.PARAMS = params;

   //Set the pins to their secondary function

  /* clock gate */

  SIM_SCGC6 |= SIM_SCGC6_DSPI0_MASK|SIM_SCGC6_DSPI1_MASK;

  SIM_SCGC3 |= SIM_SCGC3_DSPI2_MASK;

  SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTB_MASK;

  /* pin mux */

  PORTD_PCR11 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR11 |= PORT_PCR_MUX(2); //SPI2_PCS0

  PORTD_PCR12 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR12 |= PORT_PCR_MUX(2); //SPI2_SCK

  PORTD_PCR13 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR13 |= PORT_PCR_MUX(2); //SPI2_SOUT

  PORTD_PCR14 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR14 |= PORT_PCR_MUX(2); //SPI2_SIN  

  

   //The fopen call doesn't install the driver with our initialization

  _io_spi_install("spi0:", &init_struct);

and I replaced it with this:

SPI_INIT_STRUCT init_struct;

  SPI_PARAM_STRUCT params;

  DSPI_INIT_STRUCT dinit;

  dinit.CHANNEL = 2;

  dinit.CLOCK_SOURCE = sysclock;

  params.BAUDRATE = baudrate;

  params.CS = cspin;

  params.MODE = clockmode;

  params.ATTR = DSPI_ATTR_USE_ISR;

  params.DUMMY_PATTERN = 0xFFFFFFFF;

  init_struct.DEVIF = &_spi_dspi_devif;

  init_struct.DEVIF_INIT = &dinit;

  init_struct.PARAMS = params;

   //Set the pins to their secondary function

  /* clock gate */

  SIM_SCGC6 |= SIM_SCGC6_DSPI0_MASK|SIM_SCGC6_DSPI1_MASK;

  SIM_SCGC3 |= SIM_SCGC3_DSPI2_MASK;

  SIM_SCGC5 |= SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTB_MASK;

  /* pin mux */

  PORTD_PCR11 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR11 |= PORT_PCR_MUX(2); //SPI2_PCS0

  PORTD_PCR12 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR12 |= PORT_PCR_MUX(2); //SPI2_SCK

  PORTD_PCR13 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR13 |= PORT_PCR_MUX(2); //SPI2_SOUT

  PORTD_PCR14 &= ~PORT_PCR_MUX_MASK;

  PORTD_PCR14 |= PORT_PCR_MUX(2); //SPI2_SIN  

   //The fopen call doesn't install the driver with our initialization

  _io_spi_install("spi0:", &init_struct);

But it isn't working. It gives no error at all, it seems to communicate with the ADC, but it doesn't write nor read the values.

Any hints would be greatly appreciated.

Thanks,

Sebastian

0 Kudos
1 Solution
443 Views
aisebastian
Contributor II

I figured it out, pins PTE3 and PTE1 needed to be set on ALT7 function, not ALT2. They were reversed in ALT2.

View solution in original post

0 Kudos
3 Replies
443 Views
aisebastian
Contributor II

Nevermind,

apparently I don't need to install it. I had mapped the pins for spi2 to spi0 on mqx3.8, and now It works with fopen("spi2:").

However, I now have another issue. I tried switching it to spi1, and did all the jumper settings on TWRADCDAC-LTC (J25, J9, J11, J12, J13),but I get no response from it. Inspecting the debug header, I have no activity on SPI1_CLK.

I have the clocks enabled with

SIM_SCGC5 |= SIM_SCGC5_PORTE_MASK | SIM_SCGC5_PORTB_MASK | SIM_SCGC5_PORTD_MASK | SIM_SCGC5_PORTA_MASK | SIM_SCGC5_PORTC_MASK;

And also, the pins are set to their alt2 function:

PORTE_PCR4 &= ~PORT_PCR_MUX_MASK;

PORTE_PCR4 |= PORT_PCR_MUX(2);

PORTE_PCR3 &= ~PORT_PCR_MUX_MASK;

PORTE_PCR3 |= PORT_PCR_MUX(2);

PORTE_PCR2 &= ~PORT_PCR_MUX_MASK;

PORTE_PCR2 |= PORT_PCR_MUX(2);

PORTE_PCR1 &= ~PORT_PCR_MUX_MASK;

PORTE_PCR1 |= PORT_PCR_MUX(2);

It seems as if I have no CLK on spi1. I can't figure out what I am missing.

Thanks,

Sebastian

0 Kudos
444 Views
aisebastian
Contributor II

I figured it out, pins PTE3 and PTE1 needed to be set on ALT7 function, not ALT2. They were reversed in ALT2.

0 Kudos
443 Views
Martin_
NXP Employee
NXP Employee

Sebastian, the default GPIO settings for SPI can be found in your BSP init_gpio.c:

c:\Freescale\Freescale_MQX_4_0\mqx\source\bsp\twrk60f120m\init_gpio.c

It is expected you would need to change it to fit your own BSP/HW.

In the file there is the function _bsp_dspi_io_init(number), which is called by MQX IO sub-system in response to the application level fopen("spi<num>:1");

This function also enables the clock for the SPI module. Default BSP configuration of the spi io driver can be found in:

c:\Freescale\Freescale_MQX_4_0\mqx\source\bsp\twrk60f120m\init_spi.c

0 Kudos