Nome Nome - 1 year ago 154
C Question

pic32mx230 spi numbers of bytes

we want to send 6 bytes through PIC32MX230 SPI
if we define in Header

`#define MAX_NUM_OF_BYTES 6
#define MAX_NUM_OF_BYTES_IN_BUF (MAX_NUM_OF_BYTES + 2)`


i have checked my DSO it seems only one first byte are send i am confused how to increase number of bytes to send through SPI

Thanks

Nome

SPI_DATA_TYPE__attribute__((coherent))modulatorData.spi_tx_buffer[MAX_NUM_OF_BYTES_IN_BUF] =
{ 0xD4,0x04,0x03,0x02,0x01,0x1E};
/* Client Configure data */
DRV_SPI_CLIENT_DATA cfgObj = { .baudRate = 0, .operationStarting =
MOD_BufferEventHandlerMaster, .operationEnded = NULL, };

void MOD_BufferEventHandlerMaster(DRV_SPI_BUFFER_EVENT buffEvent,
DRV_SPI_BUFFER_HANDLE hBufferEvent, void* context)
{
switch (buffEvent)
{
case DRV_SPI_BUFFER_EVENT_PROCESSING:
{
if ((context == gBufferContext.MasterTXBufferRef1) && modData.masterState == MOD_STATE_MASTER_WAIT_FOR_SLAVE_1_WR_COMPLETION))
{
MOD_SPI_CS_DESELECT(SPI_SLAVE_2_CS_PORT_ID,SPI_SLAVE_2_CS_PORT_PIN);
TXDIS_DSELECT();
MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
}

}
break;

case DRV_SPI_BUFFER_EVENT_COMPLETE:
{
if ((context == gBufferContext.MasterTXBufferRef1)
&& (modData.masterState
== MOD_STATE_MASTER_WAIT_FOR_SLAVE_1_WR_COMPLETION))
{

MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,
SPI_SLAVE_1_CS_PORT_PIN);
modData.masterState = MOD_STATE_MASTER_IDLE;
modData.state = MOD_STATE_VERIFY_SLAVES_DATA;
// DRV_SPI_Close(modData.drvMasterHandle);
}

}
break;
/* Buffer event has some error */
case DRV_SPI_BUFFER_EVENT_ERROR:
{
/* Transfer failed */
LED_DSELECT();
}
break;

default:
break;
}

}

void SPI_Task(void)
{
modData.masterCfgData = &cfgObj;

//uint8_t num_of_bytes=0;

/* run the state machine here for SPI */
switch (modData.masterState)
{
/* Master TX to Slave ADRF6755 */
case MOD_STATE_MASTER_WR_SLAVE_1:
{
DRV_SPI_ClientConfigure(modData.drvMasterHandle,
modData.masterCfgData);
/* Populate the application Tx buffer for MASTER to TX on Slave 1.
Buffer holds values from 0 to 63 */

/* populate the buffer context with TX buffer ptr for the Buffer Event Handler */
gBufferContext.MasterTXBufferRef1 = &drvMasterTXbuffer1[0];
modData.masterState =
MOD_STATE_MASTER_WAIT_FOR_SLAVE_1_WR_COMPLETION;

/* Add Master transmit buffer to master TX buffer and transmit*/
modData.drvMasterTxBufHandle = DRV_SPI_BufferAddWrite(
modData.drvMasterHandle,
(SPI_DATA_TYPE *) &drvMasterTXbuffer1[0], MAX_NUM_OF_BYTES,
MOD_BufferEventHandlerMaster,
gBufferContext.MasterTXBufferRef1);
}
break;
case MOD_STATE_MASTER_WAIT_FOR_SLAVE_1_WR_COMPLETION:
{
/* wait till the Master write to slave 1 is over */
Nop();
}
break;
case MOD_STATE_MASTER_IDLE:
{
/* Master work is done */
Nop();
}
break;
default:
break;

}
}

void MOD_Initialize(void)
{

/* Place the App state machine in its initial state. */
modData.state = MOD_STATE_INIT;
modData.masterState = MOD_STATE_MASTER_IDLE;
modData.drvMasterHandle = DRV_HANDLE_INVALID;
Modulator_SPI_Enable();

/* keep both SS line inactive */
MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID, SPI_SLAVE_1_CS_PORT_PIN);

/* initiate the LED states */
// LED_DSELECT();
}

void MOD_Tasks(void)
{
uint8_t count1, count2 = 0;
/* Check the application's current state. */
switch (modData.state)
{
/* Application's initial state. */
case MOD_STATE_INIT:
{
// bool appInitialized = true;

if (modData.drvMasterHandle != DRV_HANDLE_INVALID)
{
/* ALL SPI instances are properly opened ,so start first transfer */
modData.state = MOD_STATE_START_TRANSFER_1;
}
/* Open the SPI Driver */
else if (modData.drvMasterHandle == DRV_HANDLE_INVALID)
{

modData.drvMasterHandle = DRV_SPI_Open(0, DRV_IO_INTENT_WRITE);
}

}
break;

case MOD_STATE_START_TRANSFER_1:
{
/* Initiate data transfer from SPI1 Master to slave */
modData.masterState = MOD_STATE_MASTER_WR_SLAVE_1;
modData.state = MOD_STATE_TRANSFER_DATA_BTWN_MASTER_SLAVE;
}
break;

case MOD_STATE_TRANSFER_DATA_BTWN_MASTER_SLAVE:

{
/* Run Slave Task */
// MOD_SPI_SLAVE_TASK();
/* Run Master Task */
SPI_Task();

}
break;

case MOD_STATE_VERIFY_SLAVES_DATA:
{
/* Verify the first transfer */
for (count1 = 0; count1 <= MAX_NUM_OF_BYTES; count1++)
{
if (drvMasterTXbuffer1[count1] != drvMasterTXbuffer1[count2])
{
break;
}
}
if (count1 == MAX_NUM_OF_BYTES)
{
/* Transfer 1 is successful */
LED_SELECT();

}

/* Error check */
if (count1 != MAX_NUM_OF_BYTES || count2 != MAX_NUM_OF_BYTES)

{
LED_SELECT(); //if select led glowing..if Deselect is OFF
}

else
{
LED_DSELECT();
}
modData.masterState = MOD_STATE_IDLE;
}
break;

case MOD_STATE_IDLE:
{

Nop();

}
break;

/* The default state should never be executed */

default:
{
break;

}
}

}

Answer Source

Sometimes the best answer is the simplest:

unsigned char buff={0xD4,0x04,0x03,0x02,0x01,0x1E};
unsigned char i;
SPI1CON = 0x8020; // SPI on and SPI Master see 61106G.pdf PIC32 FRM
for(i = 0; i < 6; i++)
{
    SPI1BUF=buff[i];
    while(SPI1STATbits.SPIBUSY);
    // read the SDI line
    unsigned char x = SPI1BUF;
}

You'll need to choose the right SPIXCON for the SPI bus you are attached to in your schematic.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download