please, help me
I have used SPI0 in expansion header J15 to connect to ADXL345 sensor.
4 pins is used: SIMO, SOMI, CLK and SCSn_[4] (chip selection) in J15.
But, it did not wor, can everyone help me solve proplem
here is my code:
#include <string.h>
#include "soc_C6748.h"
#include "hw_psc_C6748.h"
#include "lcdkC6748.h"
#include "uart.h"
#include "uartStdio.h"
#include "spi.h"
#include "psc.h"
#include "interrupt.h"
#include "ADXL345.h"
#include "stdio.h"
#define SIMO_SOMI_CLK_CS 0x00000E10
#define CHAR_LENGTH 0x8
static void SPIConfigDataFmtReg(unsigned int dataFormat);
static void SpiTransfer(void);
static void SetUpInt(void);
static void SetUpSPI(void);
static void SetUpADXL345(void);
static void GetData(void);
static void Delay(unsigned int N);
void SPIIsr(void);
volatile unsigned int flag = 1;
unsigned int tx_len;
unsigned int rx_len;
unsigned char *p_tx;
volatile unsigned char *p_rx;
unsigned char tx_data[260];
unsigned char rx_data[260];
char RES[8];
void main(void) {
unsigned int i;
PSCModuleControl(SOC_PSC_0_REGS,HW_PSC_SPI0,
PSC_POWERDOMAIN_ALWAYS_ON,PSC_MDCTL_NEXT_ENABLE);
UARTStdioInit();
UARTPuts("Hello ",-1);
SPIPinMuxSetup(0);
SPI0CSPinMuxSetup(4);
SetUpInt();
SetUpSPI();
SetUpADXL345();
Delay(100000);
// get data
GetData();
for(i=0;i<13;i++){
sprintf(RES,"%d",rx_data[i]);
UARTPuts(RES,-1);
}
}
static void GetData(void){
tx_data[0] = (DATAX0|0xC0);
tx_data[1] = 0x00;
tx_data[3] =0x00;
tx_data[4] =0x00;
tx_data[5] =0x00;
tx_data[6] =0x00;
tx_data[7] = (DATAX0|0xC0);
tx_data[8] = 0x00;
tx_data[9] =0x00;
tx_data[10] =0x00;
tx_data[11] =0x00;
tx_data[12] =0x00;
rx_len = 13;
tx_len = 13;
SPIDat1Config(SOC_SPI_0_REGS,
(SPI_CSHOLD | SPI_DATA_FORMAT3), 0x10);
SpiTransfer();
}
static void SetUpADXL345(){
tx_data[0] = DATA_FORMAT;
tx_data[1] = (SSI_4WIRE|FULL_RES|JUS_RIGHT|RANGE16);
tx_data[2] = BW_RATE;
tx_data[3] = BW_3200HZ;
tx_data[4] = POWER_CTL;
tx_data[5] = MEASURE;
tx_len = 6;
rx_len = 6;
SPIDat1Config(SOC_SPI_0_REGS,(SPI_CSHOLD|SPI_DATA_FORMAT3),0x10);
SpiTransfer();
}
static void SetUpInt(void){
IntDSPINTCInit();
IntRegister(C674X_MASK_INT4,SPIIsr);
IntEventMap(C674X_MASK_INT4,SYS_INT_SPI0_INT);
IntEnable(C674X_MASK_INT4);
IntGlobalEnable();
}
static void SetUpSPI(void){
unsigned char cs = 0x10;
unsigned char dcs = 0x10;
unsigned int val = SIMO_SOMI_CLK_CS;
SPIReset(SOC_SPI_0_REGS);
SPIOutOfReset(SOC_SPI_0_REGS);
SPIModeConfigure(SOC_SPI_0_REGS, SPI_MASTER_MODE);
SPIClkConfigure(SOC_SPI_0_REGS,150000000,2000000,SPI_DATA_FORMAT3);
SPIPinControl(SOC_SPI_0_REGS,0,0,&val);
SPIDefaultCSSet(SOC_SPI_0_REGS,dcs);
SPIConfigDataFmtReg(SPI_DATA_FORMAT3);
SPIDat1Config(SOC_SPI_0_REGS,(SPI_CSHOLD|SPI_DATA_FORMAT3),cs);
SPIIntLevelSet(SOC_SPI_0_REGS,SPI_RECV_INTLVL|SPI_TRANSMIT_INTLVL);
SPIEnable(SOC_SPI_0_REGS);
}
static void SPIConfigDataFmtReg(unsigned int dataFormat){
SPIConfigClkFormat(SOC_SPI_0_REGS,
SPI_CLK_POL_HIGH|SPI_CLK_OUTOFPHASE,
dataFormat);
SPIShiftMsbFirst(SOC_SPI_0_REGS,dataFormat);
SPICharLengthSet(SOC_SPI_0_REGS,CHAR_LENGTH,dataFormat);
}
void Delay(unsigned int N){
while(N--);
}
static void SpiTransfer(void){
p_tx = &tx_data[0];
p_rx = &rx_data[0];
SPIIntEnable(SOC_SPI_0_REGS,SPI_RECV_INT|SPI_TRANSMIT_INT);
while(flag);
flag =1;
SPIDat1Config(SOC_SPI_0_REGS,SPI_DATA_FORMAT3,0x10);
}
void SPIIsr(void){
unsigned int intCode = 0;
IntEventClear(SYS_INT_SPI0_INT);
intCode = SPIInterruptVectorGet(SOC_SPI_0_REGS);
while(intCode){
if(intCode == SPI_TX_BUF_EMPTY){
tx_len--;
SPITransmitData1(SOC_SPI_0_REGS,*p_tx);
p_tx++;
if(!tx_len){
SPIIntDisable(SOC_SPI_0_REGS,SPI_TRANSMIT_INT);
}
}
if(intCode == SPI_RECV_FULL){
rx_len--;
*p_rx = (char)SPIDataReceive(SOC_SPI_0_REGS);
p_rx++;
if(!rx_len){
flag = 0;
SPIIntDisable(SOC_SPI_0_REGS,SPI_RECV_INT);
}
}
intCode = SPIInterruptVectorGet(SOC_SPI_0_REGS);
}
}
// below is ADXL345.h file including registers of ADXL345 and some mode to set registers of the sensor
/*
* ADXL345.h
*
* Created on: 30-07-2013
* Author: Trung Trinh
*/
#ifndef ADXL345_H_
#define ADXL345_H_
#define BW_RATE 0x2C
#define POWER_CTL 0x2D
#define DATA_FORMAT 0x31
#define DATAX0 0x32
#define DATAX1 0x33
#define DATAY0 0x34
#define DATAY1 0x35
#define DATAZ0 0x36
#define DATAZ1 0x37
#define INT_ENABLE 0x2E
#define INT_MAP 0x2F
#define INT_SOURCE 0x30
//some mode selection for sensor
// 1.For DATA_FORMAT register of sensor
#define SELF_TEST 0x80
#define SSI_4WIRE 0x00 //using 4 wires for SSI
#define SSI_3WIRE 0x40 //using 3 wires
#define FULL_RES 0x08
#define JUS_LEFT 0x04
#define JUS_RIGHT 0x00
#define RANGE2 0x00 //2g
#define RANGE4 0x01 //4g
#define RANGE8 0x02 //8g
#define RANGE16 0x03 //16g
// 2.For POWER_CTL register
#define MEASURE 0x08 // measure mode from standby mode
//`3.For BW_RATE register
#define BW_3200HZ 0x0F
#define BW_1600HZ 0x0E
#define BW_800HZ 0x0D
#endif /* ADXL345_H_ */