主题中讨论的其他器件:LAUNCHXL-CC26X2R1、
大家好、
我想 通过 SPI 将一个 ADS127L01连接到我的 LAUNCHXL-CC26X2R1。 首先、我只想读取 ADS127L01中的寄存器来测试 SPI 连接。 (地址00h 上的 ID 寄存器)
我根据 SDK 上的 spimaster 示例编写了代码。
这是我的代码。
#define THREADSTACKSIZE (1024)
#define SPI_MSG_LENGTH (1)
static Display_Handle display;
uint16_t masterRxBuffer[SPI_MSG_LENGTH];
uint16_t masterTxBuffer[SPI_MSG_LENGTH]; //unsigned char
uint32_t i;
/*
* ======== masterThread ========
* Master SPI sends a message to slave while simultaneously receiving a
* message from the slave.
*/
void *masterThread(void *arg0)
{
SPI_Handle masterSpi;
SPI_Params spiParams;
SPI_Transaction transaction;
// uint32_t i;
bool transferOK;
int32_t status;
/* Open SPI as master (default) */
SPI_Params_init(&spiParams);
spiParams.frameFormat = SPI_POL0_PHA1;
spiParams.bitRate = 200000;
spiParams.dataSize = 8;
spiParams.mode = SPI_MASTER;
masterSpi = SPI_open(CONFIG_SPI_MASTER, &spiParams);
if (masterSpi == NULL) {
Display_printf(display, 0, 0, "Error initializing master SPI\n");
while (1);
}
else {
Display_printf(display, 0, 0, "Master SPI initialized\n");
}
/* Copy message to transmit buffer */
for (i = 0; i < 100000; i++) {
/* Initialize master SPI transaction structure */
masterTxBuffer[0] = 0x2000;
transaction.count = SPI_MSG_LENGTH;
transaction.txBuf = (void *) masterTxBuffer;
transaction.rxBuf = (void *) masterRxBuffer;
/* Perform SPI transfer */
transferOK = SPI_transfer(masterSpi, &transaction);
if (transferOK) {
Display_printf(display, 0, 0, "Master received: %d ",masterRxBuffer[0]);
}
else {
Display_printf(display, 0, 0, "Unsuccessful master SPI transfer");
}
/* Sleep for a bit before starting the next SPI transfer */
sleep(1);
}
SPI_close(masterSpi);
Display_printf(display, 0, 0, "\nDone");
return (NULL);
}
/*
* ======== mainThread ========
*/
void *mainThread(void *arg0)
{
pthread_t thread0;
pthread_attr_t attrs;
struct sched_param priParam;
int retc;
int detachState;
/* Call driver init functions. */
Display_init();
GPIO_init();
SPI_init();
/* Open the display for output */
display = Display_open(Display_Type_UART, NULL);
if (display == NULL) {
/* Failed to open display driver */
while (1);
}
Display_printf(display, 0, 0, "Starting the SPI master example");
Display_printf(display, 0, 0, "This example requires external wires to be "
"connected to the header pins. Please see the Board.html for details.\n");
/* Create application threads */
pthread_attr_init(&attrs);
detachState = PTHREAD_CREATE_DETACHED;
/* Set priority and stack size attributes */
retc = pthread_attr_setdetachstate(&attrs, detachState);
if (retc != 0) {
/* pthread_attr_setdetachstate() failed */
while (1);
}
retc |= pthread_attr_setstacksize(&attrs, THREADSTACKSIZE);
if (retc != 0) {
/* pthread_attr_setstacksize() failed */
while (1);
}
/* Create master thread */
priParam.sched_priority = 1;
pthread_attr_setschedparam(&attrs, &priParam);
retc = pthread_create(&thread0, &attrs, masterThread, NULL);
if (retc != 0) {
/* pthread_create() failed */
while (1);
}
return (NULL);
}
但 masterRxBuffer 不符合预期(数据表中提到的 x3h)。
我将4个引脚(SCLK、MOSI、MISO、CS)从 ADS127L01连接到 LAUNCHXL-CC26X2R1、 并开始设置为1 、并设置为0。我还应该做什么?
适用于 spimaster 示例的 SDK 版本:SimpleLink CC13x2 26x2 SDK (5.30.01.01)
Code Composer Studio 版本:11.1.0.00011
提前感谢
