Jump to content
43oh

SPI pin map for Stellaris Launchpad (LM4F120)


Recommended Posts

I'm trying to read data from an adxl345 accelerometer. The example code that comes with Energia doesn't seen to work.

 

So, I'm trying to use this code from sparkfun: http://www.sparkfun.com/tutorials/240. It was made for Arduino, but since Energia uses the same libraries I thought it would work as it work on arduino.

 

My problem is to know the correct pins to comunicate. The arduino board has only one set of SPI pins, although the Stellaris board has four sets.

 

I want to know how to configure the SPI comunication pins. I'm kind of a beginner with Stellaris, so forgive if it is a stupid question!

Link to post
Share on other sites

Not a stupid question at all. The pin mapping for the Stellaris Launchpad can be found here: https://github.com/energia/Energia/wiki/Hardware

The default SPI pins are PB_6 (MISO), PB_7 (MOSI) and, PB_4 (SCK). Note that in the example code there the Chip Select pin is mapped to pin 10 (int CS=10;). If you leave this as default then on the Stellaris Launchpad this would be PA_7 (See pin mapping).

Link to post
Share on other sites
  • 2 weeks later...

Not a stupid question at all. The pin mapping for the Stellaris Launchpad can be found here: https://github.com/energia/Energia/wiki/Hardware

The default SPI pins are PB_6 (MISO), PB_7 (MOSI) and, PB_4 (SCK). Note that in the example code there the Chip Select pin is mapped to pin 10 (int CS=10;). If you leave this as default then on the Stellaris Launchpad this would be PA_7 (See pin mapping).

 

Thank energia, but it didn't work... Maybe some changes in the code are needed, because with arduino it works fine, but with stellaris I just get noise!

Link to post
Share on other sites

Well, I was wrong. The default pin would be PA_5 which is PIN 8. This is what is in the code and the pin diagram has an error in it.

So, I would make CS pin 8 and try again. If that still does not work then replace the last 2 functions in the original Sketch (http://sparkfun.com/tutorial/ADXL/ADXL345_Basic.pde) with the code below. I will have to change the SPI library in the next release to be strictly like the Wiring/Arduino implementation.

 

//This function will write a value to a register on the ADXL345.
//Parameters:
//  char registerAddress - The register to write a value to
//  char value - The value to be written to the specified register.
void writeRegister(char registerAddress, char value){
  //Set Chip Select pin low to signal the beginning of an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the register address over SPI.
  SPI.transfer(registerAddress, SPI_CONTINUE);
  //Transfer the desired register value over SPI.
  SPI.transfer(value, SPI_CONTINUE);
  //Set the Chip Select pin high to signal the end of an SPI packet.
  digitalWrite(CS, HIGH);
}

//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
//  char registerAddress - The register addresse to start the read sequence from.
//  int numBytes - The number of registers that should be read.
//  char * values - A pointer to a buffer where the results of the operation should be stored.
void readRegister(char registerAddress, int numBytes, char * values){
  //Since we're performing a read operation, the most significant bit of the register address should be set.
  char address = 0x80 | registerAddress;
  //If we're doing a multi-byte read, bit 6 needs to be set as well.
  if(numBytes > 1)address = address | 0x40;
  
  //Set the Chip select pin low to start an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the starting register address that needs to be read.
  SPI.transfer(address, SPI_CONTINUE);
  //Continue to read registers until we've read the number specified, storing the results to the input buffer.
  for(int i=0; i<numBytes; i++){
    values[i] = SPI.transfer(0x00, SPI_CONTINUE);
  }
  //Set the Chips Select pin high to end the SPI packet.
  digitalWrite(CS, HIGH);
}
Link to post
Share on other sites

 

Well, I was wrong. The default pin would be PA_5 which is PIN 8. This is what is in the code and the pin diagram has an error in it.

So, I would make CS pin 8 and try again. If that still does not work then replace the last 2 functions in the original Sketch (http://sparkfun.com/tutorial/ADXL/ADXL345_Basic.pde) with the code below. I will have to change the SPI library in the next release to be strictly like the Wiring/Arduino implementation.

 

//This function will write a value to a register on the ADXL345.
//Parameters:
//  char registerAddress - The register to write a value to
//  char value - The value to be written to the specified register.
void writeRegister(char registerAddress, char value){
  //Set Chip Select pin low to signal the beginning of an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the register address over SPI.
  SPI.transfer(registerAddress, SPI_CONTINUE);
  //Transfer the desired register value over SPI.
  SPI.transfer(value, SPI_CONTINUE);
  //Set the Chip Select pin high to signal the end of an SPI packet.
  digitalWrite(CS, HIGH);
}

//This function will read a certain number of registers starting from a specified address and store their values in a buffer.
//Parameters:
//  char registerAddress - The register addresse to start the read sequence from.
//  int numBytes - The number of registers that should be read.
//  char * values - A pointer to a buffer where the results of the operation should be stored.
void readRegister(char registerAddress, int numBytes, char * values){
  //Since we're performing a read operation, the most significant bit of the register address should be set.
  char address = 0x80 | registerAddress;
  //If we're doing a multi-byte read, bit 6 needs to be set as well.
  if(numBytes > 1)address = address | 0x40;
  
  //Set the Chip select pin low to start an SPI packet.
  digitalWrite(CS, LOW);
  //Transfer the starting register address that needs to be read.
  SPI.transfer(address, SPI_CONTINUE);
  //Continue to read registers until we've read the number specified, storing the results to the input buffer.
  for(int i=0; i<numBytes; i++){
    values[i] = SPI.transfer(0x00, SPI_CONTINUE);
  }
  //Set the Chips Select pin high to end the SPI packet.
  digitalWrite(CS, HIGH);
}

 

 

Thank you again, but I think it worked on pin 10. I think the protoboard connection was not correct, I tried again and worked, although the values printed on serial monitor are not the same as on arduino. I will try replacing the function and see if there is any difference...

Link to post
Share on other sites

Could please fix the SPI.cpp file? 

 

It still defines PA_5 = pin 8 as MOSI for SPI port (0).

 

static const unsigned long g_ulSSIConfig[4][4] =
{
    {GPIO_PA2_SSI0CLK, GPIO_PA3_SSI0FSS, GPIO_PA4_SSI0RX, GPIO_PA5_SSI0TX},
    {GPIO_PF2_SSI1CLK, GPIO_PF3_SSI1FSS, GPIO_PF0_SSI1RX, GPIO_PF1_SSI1TX},
    {GPIO_PB4_SSI2CLK, GPIO_PB5_SSI2FSS, GPIO_PB6_SSI2RX, GPIO_PB7_SSI2TX},
    {GPIO_PD0_SSI3CLK, GPIO_PD1_SSI3FSS, GPIO_PD2_SSI3RX, GPIO_PD3_SSI3TX},};
}

 

consistent with 


 

  8 PA_5 MOSI (0) 
13 PA_4 MISO (0)
12 PA_3 CS (0)
11 PA_2 SCK (0)
 

 

Thanks!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...