Other Parts Discussed in Thread: C2000WARE
I am trying to replicate the SPI driver's that are given in the Application note of HI-3593.
ap-southeast-1-02900067-inspect.menlosecurity.com/.../
The driver example is written for a different controller(MC9S12XD)
I am trying to replicate two functions R_Register() and W_CommandValue():
This is the code in the app note for the two functions:
/* ------------------------------------------------------------------ / Read HI-3110 Register Read Function / ------------------------------------------------------------------ Argument(s): Register to read Return: 8-bit Register Value */ unsigned char R_Register(char Reg) { unsigned char R_Reg; SPI0CR1 = SPI0CR1 & ~SPI0CR1_SSOE_MASK; // disable auto /SS output, reset /SS Output Enable SPI0CR2 = SPI0CR2 & ~SPI0CR2_MODFEN_MASK; // disable auto /SS output, reset SPI0 Mode Fault SPI0_nSS = 0; // assert the SPI0 /SS strobe R_Reg = txrx8bits(Reg, 1); // send op code (ignore returned data byte) R_Reg = txrx8bits(0x00, 1); // send dummy data / receive Status Reg byte SPI0_nSS = 1; // negate the SPI0 /SS strobe SPI0CR1 = SPI0CR1 | SPI0CR1_SSOE_MASK; // enable auto /SS output, set /SS Output Enable SPI0CR2 = SPI0CR2 | SPI0CR2_MODFEN_MASK; // enable auto /SS output, set SPI0 Mode Fault return R_Reg; } // Write SPI Command with a Value to HI-3593 void W_CommandValue(uint8 cmd, uint8 value) { uint8 dummy; SPI0CR1 = SPI0CR1 & ~SPI0CR1_SSOE_MASK; // disable auto /SS output, reset /SS Output SPI0CR2 = SPI0CR2 & ~SPI0CR2_MODFEN_MASK; // disable auto /SS output, reset SPI0 Mode SPI0_nSS = 0; // assert the SPI0 /SS strobe dummy = SPI0SR; // clear SPI status register SPI0DR = cmd; // SPI command while (!SPI0SR_SPIF) ; dummy = SPI0DR; // read Rx data in Data Reg to reset SPIF dummy = SPI0SR; // clear SPI status register SPI0DR = value; // Reset values while (!SPI0SR_SPIF) ; dummy = SPI0DR; // read Rx data in Data Reg to reset SPIF SPI0_nSS = 1; // negate the SPI0 /SS strobe SPI0CR1 = SPI0CR1 | SPI0CR1_SSOE_MASK; // enable auto /SS output, set /SS Output Enable SPI0CR2 = SPI0CR2 | SPI0CR2_MODFEN_MASK; // enable auto /SS output, set SPI0 Mode Default }
And these are my equivallent functions:
void W_CommandValue(uint16_t cmd, uint16_t value) { //Pull the cs pin to low. GPIO_writePin(SPIA_CS_PIN, 0); //Wait for 100 us. DEVICE_DELAY_US(100); //Read the dummy data. SPI_readDataNonBlocking(A429_SPI); //Wait for 100 us. DEVICE_DELAY_US(100); //Write the command. SPI_writeDataNonBlocking(A429_SPI, cmd); //Wait for 100 us. DEVICE_DELAY_US(100); //Read the dummy data. SPI_readDataNonBlocking(A429_SPI); //Wait for 100 us. DEVICE_DELAY_US(100); //Write the value. SPI_writeDataNonBlocking(A429_SPI, value); //wait 100 us. DEVICE_DELAY_US(100); //Read the dummy data. SPI_readDataNonBlocking(A429_SPI); //wait 100 us. DEVICE_DELAY_US(100); //Pull the cs pin to high. GPIO_writePin(SPIA_CS_PIN, 1); //wait 100 us. DEVICE_DELAY_US(100); } uint16_t R_Register(uint16_t cmd) { uint16_t value = 0; //Pull the cs pin to low. GPIO_writePin(SPIA_CS_PIN, 0); //Wait for 100 us. DEVICE_DELAY_US(100); //Write the command. SPI_writeDataNonBlocking(A429_SPI, cmd); //Wait for 100 us. DEVICE_DELAY_US(100); //Read the dummy byte. SPI_readDataNonBlocking(A429_SPI); //Wait for 100 us. DEVICE_DELAY_US(100); //Transmit dummy data.z SPI_writeDataNonBlocking(A429_SPI, 0x00); //Wait for 100 us. DEVICE_DELAY_US(100); //Read the received byte. value = SPI_readDataNonBlocking(A429_SPI); //Wait for 100 us. DEVICE_DELAY_US(100); //Pull the cs pin to high. GPIO_writePin(SPIA_CS_PIN, 1); return value; }
But i am not able to communicate properly.
I tested the two functions like this:
void initHI3593()
{
//Give raising edge to HI-3593 to do a hardware reset.
GPIO_writePin(A429_CA_RESET, 0);
DEVICE_DELAY_US(100);
GPIO_writePin(A429_CA_RESET, 1);
//Write to ACLK Division Register(0x1400)
DEVICE_DELAY_US(100);
W_CommandValue(0x3800, 0x1400);
DEVICE_DELAY_US(100);
testVar = R_Register(0xD400);
}
The value of testVar should be 0x14, but i get 0x00.
Any sugesstions as to what is going wrong?