I am trying to establish SPI communication with TPS92520EVM-133 and arduino nano board so that I can read/write to registers without using the GUI mentioned in data sheet.
When I try to read any register either I get 0 or FFFF.
Please help me out with the solution.
I have attached my code here.
//#include <SoftSPI.h> #include <SPI.h> #include <Wire.h> #include<sys/types.h> //#include<sys/stat.h> #include <fcntl.h> //#include <serialprintf.h> //#include <SoftWire.h> //#include <printf.h> //SoftSPI SPI(8, A4, A5); #define kNOERROR 0 #define kREADERROR 1 #define kWRITEERROR 2 const uint32_t READ= 0x00; const uint32_t WRITE = 0x80; const uint32_t PARITY_MASK = 0x00; const uint32_t ADDRESS_MASK = 0x7E; const uint16_t Chipselect = 11; // PB1 unsigned long nextTime; void setup() { SPI.begin(); SPI.setClockDivider(SPI_CLOCK_DIV16); pinMode(Chipselect, OUTPUT); digitalWrite(Chipselect, HIGH); //uint32_t unused; //Read(0x0, unused); Serial.begin(115200); //SPI.setClockDivider(SPI_CLOCK_DIV8); nextTime = millis(); } void loop() { uint16_t CR1; uint16_t CR2; uint16_t CR3; uint32_t SR1; uint32_t SR2; uint32_t SR3; uint32_t CTR; uint16_t cr1,cr2,cr3; uint16_t packet; // Every 1/2 second, toggle the state of the LED and read the ACS37800 if (nextTime < millis()) { packet=assembleSPICmd_520(0x00,0x05,0x00); ReadWrite(packet,CR1); packet=assembleSPICmd_520(0x01,0x00,0x10); ReadWrite(packet,CR2); cr1=CR1&0xFFFF; Serial.print("cr1=\r"); Serial.print(cr1,HEX); Serial.print("\n"); cr2=CR2&0xFFFF; Serial.print("cr2=\r"); Serial.print(cr2,HEX); Serial.print("\n"); nextTime = millis() + 500L; } } uint16_t ReadWrite(uint16_t address, uint16_t& value) { uint16_t results = kNOERROR; uint8_t command = (uint8_t) (address & 0xFF00); uint8_t data = (uint8_t) (address & 0x00FF); SPI.beginTransaction(SPISettings(1000000, LSBFIRST, SPI_MODE0)); digitalWrite(Chipselect, LOW); SPI.transfer(command); SPI.transfer(data); digitalWrite(Chipselect, HIGH); delayMicroseconds(4); digitalWrite(Chipselect, LOW); SPI.transfer(command); SPI.transfer(0); SPI.transfer(0); SPI.transfer(0); SPI.transfer(0); digitalWrite(Chipselect, HIGH); delayMicroseconds(4); digitalWrite(Chipselect, LOW); value = (uint16_t)SPI.transfer(0); value |= (uint16_t)SPI.transfer(0) << 8; digitalWrite(Chipselect, HIGH); SPI.endTransaction(); return results; } uint16_t assembleSPICmd_520( uint16_t write, uint16_t address, uint8_t data) { uint16_t assembledCmd = 0; // Build this to shift through parity calculation uint16_t parity = 0; // Parity bit calculated here uint16_t packet = 0; // This will be what we send if(write) { assembledCmd |= 0x8000; // Set CMD = 1 } assembledCmd |= (((address << 9) & 0x7E00) | ( uint16_t)(data & 0x00FF)); packet = assembledCmd; // Calculate parity while(assembledCmd > 0) { // Count the number of 1s in the LSb if(assembledCmd & 0x0001) { parity++; } // Shift right assembledCmd >>= 1; } // If the LSb is a 0 (even # of 1s), we need to add the odd parity bit if(!(parity & 0x0001)) { packet |= (1 << 8); } return(packet); }