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.

cc2640r2f的spi数据接收

Other Parts Discussed in Thread: CC2640R2F

cc2640r2f的spi程序烧录进去发送为一百位数据,只能接收到前十位。程序在其他芯片上可以正常发送接收,硬件烧录其他程序也能接收完整的数据。为什么烧录发送一百位数据的程序就只能接收到十位??

  • 若是可以的话,能给出关键代码吗?
  • #define MSGSIZE 102
    #define MSG ("12345butty12345xjf666aaaaaaaaaaaaxjf66661234567890abcdefghijklmxjf666777888999hakkkkkjjjjjhhhhhffppp")
    void *mainThread(void *arg0)
    {
    RF_Params rfParams;
    RF_Params_init(&rfParams);


    UART_Handle uart;
    UART_Params uartParams;
    UART_init();

    /* Create a UART with data processing off. */

    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode = UART_RETURN_FULL;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.baudRate = 115200;

    uart = UART_open(Board_UART0, &uartParams);

    if (uart == NULL) {
    /* UART_open() failed */
    while (1);
    }


    SPI_Handle spi;
    SPI_Params spiParams;
    SPI_Transaction spiTransaction;
    char transmitBuffer[MSGSIZE];
    // char receiveBuffer[MSGSIZE];
    bool transferOK;
    SPI_init();
    SPI_Params_init(&spiParams);
    spiParams.dataSize = 8;
    spiParams.frameFormat = SPI_POL0_PHA0;
    spiParams.mode = SPI_SLAVE;
    spiParams.transferMode = SPI_MODE_BLOCKING;
    spiParams.bitRate = 115200;
    spiParams.transferTimeout = SPI_WAIT_FOREVER;
    spiParams.transferCallbackFxn = NULL;

    spi = SPI_open(Board_SPI0, &spiParams);
    if (spi == NULL) {
    while (1); // SPI_open() failed
    }
    // Fill in transmitBuffer
    memset(transmitBuffer,'\0', MSGSIZE);
    strncpy(transmitBuffer,MSG,strlen(MSG));

    spiTransaction.count = MSGSIZE;
    char send[MSGSIZE];
    int n = 48;
    while(1)
    {

    n = (n>57)? 48:n;
    transmitBuffer[MSGSIZE-2]= (char)n;
    n++;
    spiTransaction.txBuf = (void *)transmitBuffer;
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK) {
    // Error in SPI or transfer already in progress.
    while (1);
    }


    UART_write(uart,"justTx", sizeof("justTx"));

    memset(send,'\0',MSGSIZE);
    strncpy(send,(char *)spiTransaction.txBuf,MSGSIZE);
    UART_write(uart,send,MSGSIZE);
  • void *mainThread(void *arg0)
    {
    RF_Params rfParams;
    RF_Params_init(&rfParams);

    UART_Handle uart;
    UART_Params uartParams;
    UART_init();

    // char buffer[10];

    /* Create a UART with data processing off. */

    UART_Params_init(&uartParams);
    uartParams.writeDataMode = UART_DATA_BINARY;
    uartParams.readDataMode = UART_DATA_BINARY;
    uartParams.readReturnMode = UART_RETURN_FULL;
    uartParams.readEcho = UART_ECHO_OFF;
    uartParams.baudRate = 115200;

    uart = UART_open(Board_UART0, &uartParams);

    if (uart == NULL) {
    /* UART_open() failed */
    while (1);
    }

    SPI_Handle spi;
    SPI_Params spiParams;
    SPI_Transaction spiTransaction;
    // uint8_t transmitBuffer[MSGSIZE];
    char receiveBuffer[MSGSIZE];
    bool transferOK;
    SPI_init(); // Initialize the SPI driver
    SPI_Params_init(&spiParams); // Initialize SPI parameters
    spiParams.dataSize = 8; // 8-bit data size
    spiParams.frameFormat = SPI_POL0_PHA0;
    spiParams.mode = SPI_MASTER;// SPI_MASTER SPI_SLAVE
    spiParams.transferMode = SPI_MODE_BLOCKING;
    spiParams.bitRate = 115200;
    spiParams.transferTimeout = SPI_WAIT_FOREVER;
    spiParams.transferCallbackFxn = NULL;
    //spiParams.transferTimeout = 14400;

    spi = SPI_open(Board_SPI0, &spiParams);
    if (spi == NULL) {
    UART_write(uart,"spiopenfail", sizeof("spiopenfail"));
    while (1); // SPI_open() failed
    }
    UART_write(uart,"spiopenok", sizeof("spiopenok"));

    spiTransaction.count = MSGSIZE;
    //spiTransaction.txBuf = (void *)transmitBuffer;
    spiTransaction.rxBuf = (void *)receiveBuffer;
    char rx[MSGSIZE+1];

    while(1)
    {
    //
    sleep(1);
    UART_write(uart,"masterR", sizeof("masterR"));
    memset(receiveBuffer,'\0',MSGSIZE);
    transferOK = SPI_transfer(spi, &spiTransaction);

    if (!transferOK) {
    UART_write(uart,"spitransferfail", sizeof("spitransferfail"));
    // Error in SPI or transfer already in progress.
    while (1);
    }

    UART_write(uart,"spitransferok", sizeof("spitransferok"));
    //sleep(1);
    memset(rx,'\0',MSGSIZE+1);
    strncpy(rx,(char *)spiTransaction.rxBuf,strlen(receiveBuffer));
    UART_write(uart,rx,MSGSIZE);
  • 请问能发现什么问题吗?
  • 您可以看一下这一个官方说明

    When the SPI is opened, the dataSize value determines the element types of txBuf and rxBuf. If the dataSize is from 4 to 8 bits, the driver assumes the data buffers are of type uint8_t (unsigned char). If the dataSize is from 8 to 16 bits, the driver assumes the data buffers are of type uint16_t (unsigned short). If the dataSize is greater than 16 bits, the driver assumes the data buffers are uint32_t (unsigned long). Some SPI driver implementations may not support all data sizes; refer to device specific SPI implementation documentation for details on what data sizes are supported.

    下面链接的SPI Transactions

    dev.ti.com/.../_s_p_i_8h.html
  • 附上相关示例代码

    Example transferring 12-bit SPI frames. The transmit and receive buffers are of type uint16_t.

    SPI_Transaction spiTransaction;
    uint16_t transmitBuffer[BUFSIZE];
    uint16_t receiveBuffer[BUFSIZE];
    bool transferOK;
    
    SPI_Params_init(&spiParams);
    spiParams.dataSize = 12;
    spi = SPI_open(Board_SPI0, &spiParams);
    ...
    spiTransaction.count = someIntegerValue;
    spiTransaction.txBuf = transmitBuffer;
    spiTransaction.rxBuf = receiveBuffer;
    
    transferOK = SPI_transfer(spi, &spiTransaction);
    if (!transferOK) {
    // Error in SPI or transfer already in progress.
    }

  • 下面是从机Receive 100 bytes over SPI in SPI_MODE_BLOCKING模式

    SPI_Handle handle;
    SPI_Params params;
    SPI_Transaction transaction;
    uint8_t rxBuf[100];     // Receive buffer
    // Init SPI and specify non-default parameters
    SPI_Params_init(&params);
    params.bitRate     = 1000000;
    params.frameFormat = SPI_POL1_PHA1;
    params.mode        = SPI_SLAVE;
    // Configure the transaction
    transaction.count = 100;
    transaction.txBuf = NULL;
    transaction.rxBuf = rxBuf;
    // Open the SPI and perform the transfer
    handle = SPI_open(Board_SPI, &params);
    SPI_transfer(handle, &transaction);