This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Tool/software: Code Composer Studio
hi, in my project I set the processor as master and I use a hi-6138 module as a slave.
To set the slave device, I need to write and read some values in the registers of the sensor. I can see the data I sent with the help of an oscilloscope, but I cannot read any meaningful data (reading 0x0000) because this is why I need to read while initializing the device. so if I can't do the reading function correctly, I can't initialize the sensor.
here are my spi settings /* SPI Pins: * 57: CS * 56: CLK * 8: MOSI * 17: MISO * */ GPIO_setMasterCore(8, GPIO_CORE_CPU1); GPIO_setPinConfig(GPIO_8_SPISIMOA); GPIO_setMasterCore(17, GPIO_CORE_CPU1); GPIO_setPinConfig(GPIO_17_SPISOMIA); GPIO_setMasterCore(56, GPIO_CORE_CPU1); GPIO_setPinConfig(GPIO_56_SPICLKA); GPIO_setAnalogMode(57, GPIO_ANALOG_DISABLED); GPIO_setPadConfig(57, GPIO_PIN_TYPE_STD); GPIO_setDirectionMode(57, GPIO_DIR_MODE_OUT); void spiSetup(void) { SysCtl_enablePeripheral(SYSCTL_PERIPH_CLK_SPIA); /* Disable Module */ SPI_disableModule(SPIA_BASE); /* SPI Configuration */ SPI_setConfig(SPIA_BASE, DEVICE_LSPCLK_FREQ, SPI_PROT_POL0PHA0 , SPI_MODE_MASTER, 500000, 8); /* Enable FIFO */ SPI_enableFIFO(SPIA_BASE); /* Reset FIFOs */ SPI_resetRxFIFO(SPIA_BASE); SPI_resetTxFIFO(SPIA_BASE); /* Set FIFO Levels */ SPI_setFIFOInterruptLevel(SPIA_BASE, SPI_FIFO_TX1, SPI_FIFO_RX1); // 1 YERİNE 3 /* Clear Interrupts */ Interrupt_register(INT_SPIA_RX, &spiRxFIFOISR); SPI_clearInterruptStatus(SPIA_BASE, SPI_INT_RXFF); Interrupt_clearACKGroup(INTERRUPT_ACK_GROUP6); SPI_enableModule(SPIA_BASE); EINT; ERTM; }
and register writes function code:
unsigned char Write_6131LowReg(unsigned char reg_number, unsigned short data, unsigned char irq_mgmt) { unsigned short i; //unsigned short dummy; volatile unsigned short bufferTX[2]; bufferTX[0] = (data); // upper data byte 4800 bufferTX[1] = data<<8; // lower data byte // Assert SPI chip select GPIO_writePin(57,0); // Send 8-bit SPI op code = 0x80 + reg_number SPI_writeDataNonBlocking(SPIA_BASE,((0x80 + reg_number)<<8)); while(SPI_isBusy(SPIA_BASE)){} DEVICE_DELAY_US(5); // Read and discard received data char in Rx buffer dummym1 = SPI_readDataNonBlocking(SPIA_BASE); // transmit 2 data bytes for (i = 0; i < 2; i++) { // transmit next byte SPI_writeDataNonBlocking(SPIA_BASE,bufferTX[i]); while(SPI_isBusy(SPIA_BASE)){} DEVICE_DELAY_US(5); // Read and discard received data char in Rx buffer dummym = SPI_readDataNonBlocking(SPIA_BASE); } // negate slave chip select while(SPI_isBusy(SPIA_BASE)){} DEVICE_DELAY_US(25); GPIO_writePin(57,1); // faster than PIO_Set(pinNss); return('P'); } and read register function code unsigned short Read_6131LowReg(unsigned char reg_number, unsigned char irq_mgmt) { unsigned short i; // Assert SPI chip select GPIO_writePin(57,0); // Send 8-bit SPI op code = reg_number << 2 SPI_writeDataNonBlocking(SPIA_BASE,(reg_number << 10)); while(SPI_isBusy(SPIA_BASE)){} DEVICE_DELAY_US(5); // Read received data char in Rx buffer, discard/overwrite below bufferRX[0] = SPI_readDataNonBlocking(SPIA_BASE); // receive 2 bytes register data, transmit 2 dummy data bytes for (i = 0; i < 2; i++) //SEND dummy data SPI_writeDataNonBlocking(SPIA_BASE,0xFFFF); while(SPI_isBusy(SPIA_BASE)){} DEVICE_DELAY_US(5); bufferRX[i] = SPI_readDataNonBlocking(SPIA_BASE);// & 0xFF; } while(SPI_isBusy(SPIA_BASE)){} DEVICE_DELAY_US(25); GPIO_writePin(57,1); data = (unsigned int)bufferRX[0] << 8; data |= (unsigned int)(bufferRX[1]&0x00ff); return data; }
I can't find where I made a mistake. I had to add some delay (to complete the communication) during the communication. but still it didn't work. I use Gpio57 as the CE pin and set it as gpio (not as spiaste). and the enable pin stays low throughout the clock, there is no error in the scope view.
HELP PLEASE...
Hi there,
It is a little tricky to work with 8-bit data sizes on the SPI.
Keep in mind that when you write a data value to the 16-bit SPI transmit register, the upper bits bits will get shifted out first. If you have configured the SPI for 16-bit data size, this is no problem. You can write the 16-bit data directly to the transmit register, the SPI will generate 16 clocks, and those 16 bits will be shifted out correctly. It will also simultaneously receive 16 bits in the receive register.
However, when you configure the SPI for 8-bit data size (like you have), you must first shift the data 8 bits to the left (<<) such that it is written to the upper byte of the transmit register. The SPI will then generate 8 clocks and shift the upper 8 bits in the SPI transmit register. It will also simultaneously receive 8 bits in the receive register.
Looking at your code, I can see that you are missing that shift (data<<8) when you call SPI_writeDataNonBlocking().
Note that when you read data from the SPI receive register, no shifting is required. The 8 bits received by the SPI will be located in the lower byte of the register.
Please try that fix and let me know if your problem is solved.
actually, I split the data for "SPI_writeDataNonBlocking ()" written in "unsigned char Write_6131LowReg" function, like this:
bufferTX [0] = (data); // upper data byte 4800
bufferTX [1] = data << 8; // lower data byte
First I loaded the 16-bit data directly and made the transfer of the high byte, then I shifted the low byte and made it transferred. I can see it by loopback or in the scope view.
then, for the "SPI_writeDataNonBlocking ()" function is written in the "unsigned short Read_6131LowReg" function, I did not scroll because the typed data are meaningless dummy data.
you mean, will this data stay in the shift register and cause problems?
thanks a lot
Merve,
If I understand you right, it looks like C2000.SPI (Master) is transmitting what you would expect to transfer to slave. So, the problem shouldn't be with C2000 SPI peripheral, I would then check hi-6138 datasheet to see whether your command structure is matching with what slave is expecting.
Regards,
Manoj