Matheus 0 Posted February 15, 2013 Share Posted February 15, 2013 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! Quote Link to post Share on other sites
energia 485 Posted February 15, 2013 Share Posted February 15, 2013 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). calinp, Matheus and zborgerd 3 Quote Link to post Share on other sites
OzGrant 22 Posted February 17, 2013 Share Posted February 17, 2013 G'day Energia (sounds funny addressing you this way, sort of saying G'day God) Anyway as a Stellaris SPI question has been raised on this forum. How do you change the SPI defaults, so that MOSI(0) is used instead of MOSI(2) Quote Link to post Share on other sites
energia 485 Posted February 18, 2013 Share Posted February 18, 2013 SPI.setModule(0). This will get you module 0 with MOSI on PA_5, MISO on PA_4 and, CS on PA_3. OzGrant 1 Quote Link to post Share on other sites
OzGrant 22 Posted February 19, 2013 Share Posted February 19, 2013 Tks, can now work out why the SPI doesn't work, but at least got the correct pins toggling. Quote Link to post Share on other sites
Matheus 0 Posted March 2, 2013 Author Share Posted March 2, 2013 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! Quote Link to post Share on other sites
energia 485 Posted March 2, 2013 Share Posted March 2, 2013 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); } Matheus 1 Quote Link to post Share on other sites
Matheus 0 Posted March 2, 2013 Author Share Posted March 2, 2013 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... Quote Link to post Share on other sites
Rei Vilo 695 Posted March 2, 2013 Share Posted March 2, 2013 Should the pin map be updated? Quote Link to post Share on other sites
energia 485 Posted March 3, 2013 Share Posted March 3, 2013 Yes please :-) Quote Link to post Share on other sites
Rei Vilo 695 Posted March 3, 2013 Share Posted March 3, 2013 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! Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.