void InitConsole(void) {
// Enable GPIO port A which is used for UART0 pins.
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
// Configure the pin muxing for UART0 functions on port A0 and A1.
// This step is not necessary if your part does not support pin muxing.
GPIOPinConfigure(GPIO_PA0_U0RX);
GPIOPinConfigure(GPIO_PA1_U0TX);
// Enable UART0 so that we can configure the clock.
SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
// Use the internal 16MHz oscillator as the UART clock source.
UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
// Select the alternate (UART) function for these pins.
GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
// Initialize the UART for console I/O.
UARTStdioConfig(0, 115200, 16000000);
}
void InitSPI(void) {
// The SSI2 peripheral must be enabled for use.
SysCtlPeripheralEnable(SYSCTL_PERIPH_SSI2);
// Enable Port D
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);
// SETUP SPI CS Pin (to output)
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, GPIO_PIN_5);
// Set SPI CS to HIGH (active-low)
GPIOPinWrite(GPIO_PORTB_BASE, GPIO_PIN_6, 0xFF);
GPIOPinConfigure(GPIO_PD3_SSI2CLK);
GPIOPinConfigure(GPIO_PD2_SSI2FSS);
GPIOPinConfigure(GPIO_PD0_SSI2DAT1);
GPIOPinConfigure(GPIO_PD1_SSI2DAT0);
// Configure the GPIO settings for the SSI pins.
GPIOPinTypeSSI(GPIO_PORTD_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 |
GPIO_PIN_3);
// Configure and enable the SSI port for SPI master mode. Use SSI0,
// system clock supply, idle clock level low and active low clock in
// freescale SPI mode, master mode, 1MHz SSI frequency, and 8-bit data.
// For SPI mode, you can set the polarity of the SSI clock when the SSI
// unit is idle. You can also configure what clock edge you want to
// capture data on. Please reference the datasheet for more information on
// the different SPI modes.
SSIConfigSetExpClk(SSI2_BASE, , SSI_FRF_MOTO_MODE_3,
SSI_MODE_MASTER, 1000000, 8);
// Enable the SSI0 module.
SSIEnable(SSI2_BASE);
// Read any residual data from the SSI port. This makes sure the receive
// FIFOs are empty, so we don't read any unwanted junk. This is done here
// because the SPI SSI mode is full-duplex, which allows you to send and
// receive at the same time. The SSIDataGetNonBlocking function returns
// "true" when data was returned, and "false" when no data was returned.
// The "non-blocking" function checks if there is any data in the receive
// FIFO and does not "hang" if there isn't.
uint32_t scrap;
while(SSIDataGetNonBlocking(SSI2_BASE, &scrap));
}
int main(void) {
// Set the clocking to run directly from the external crystal/oscillator.
SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);
// Set up UART Serial Output
InitConsole();
// Set up SSI2 for SPI Communication
InitSPI();
UARTprintf("............................................\n");
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00);
SSIDataPut(SSI2_BASE, 0x31); // DATA_FORMAT
while(SSIBusy(SSI2_BASE));
SSIDataPut(SSI2_BASE, 0x01); // +/- 4g
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0xFF);
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00);
SSIDataPut(SSI2_BASE, 0x2D); // POWER_CTL
while(SSIBusy(SSI2_BASE));
SSIDataPut(SSI2_BASE, 0x08);
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0xFF);
while (1) {
char values[10];
int16_t x, y, z;
char address = 0x80 | DATAX0 | 0x40; //Set MSB for read, start at X0 and enable multi-byte
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0x00);;
SSIDataPut(SSI2_BASE, address); // ask for data
for(int i=0; i < 6; i++){
SSIDataPut(SSI2_BASE, 0x00);
while(SSIBusy(SSI2_BASE));
SSIDataGet(SSI2_BASE, &values[i]);
}
GPIOPinWrite(GPIO_PORTD_BASE, GPIO_PIN_2, 0xFF);
for(int i = 0; i < 6; i++){
UARTprintf("%i\t", values[i]);
}
UARTprintf("\n");
//The X value is stored in values[1] and values[2].
x = ((int16_t)values[1]<<8)|(int16_t)values[0];
//The Y value is stored in values[2] and values[3].
y = ((int16_t)values[3]<<8)|(int16_t)values[2];
//The Z value is stored in values[4] and values[5].
z = ((int16_t)values[5]<<8)|(int16_t)values[4];
UARTprintf("%i\t\t%i\t\t%i\n", x, y, z);
SysCtlDelay(50 * (SysCtlClockGet() /1000 /3));
}
return(0); // will never return
when i execute this program i am getting output as "242" for allthe axis.
can any one suggest me where am i going wrong...