Nome
Nome

Reputation: 27

pic32mx230 spi numbers of bytes

This one below i make new code for pic32mx230 SPI module harmony design i need to send 96 bytes
register values and every 3 bytes starting CS will Low to High can anyone help
us where i am wrong?

 `APP_DATA appData;
  static uint8_t __attribute__ ((aligned (8))) app_spi_tx_buffer[128]  =0xD4,0x1E,
    0x00
    0xD4,0x1D,0x80,\
    0xD4,0x1C,0x09,\
    0xD4,0x1B,0x00,\
    0xD4,0x1A,0x00,\
    0xD4,0x19,0x64,\
    0xD4,0x18,0x18,\
    0xD4,0x17,0x00,\
    0xD4,0x16,0x80,\
    0xD4,0x15,0x00,\
    0xD4,0x14,0x00,\
    0xD4,0x13,0x00,\
    0xD4,0x12,0x00,\
    0xD4,0x11,0x00,\
    0xD4,0x10,0x00,\
    0xD4,0x0F,0x00,\
    0xD4,0x0E,0x80,\
    0xD4,0x0D,0xE8,\
    0xD4,0x0C,0x18,\
    0xD4,0x0B,0x00,\
    0xD4,0x0A,0x01,\
    0xD4,0x09,0xF0,\
    0xD4,0x08,0x00,\
    0xD4,0x07,0x00,\
    0xD4,0x06,0x53,\
    0xD4,0x05,0x00,\
    0xD4,0x04,0x01,\
    0xD4,0x03,0x04,\
    0xD4,0x02,0xAA,\
    0xD4,0x01,0xAA,\
    0xD4,0x00,0xAB,\
    0xD4,0x1B,0x00,\
    0xD4,0x1D,0x81};


  void Modulator_SPI_Enable ( void )
   {


  MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN); //start clock from here 
  TIME_DelayUs(1);
  MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN); //start clock from here 
  TIME_DelayUs(1);
  MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
  TIME_DelayUs(1);
  TIME_DelayMs(20);

  }


 /* state machine for the SPI */
    static void SPI_Task(void)
{
   unsigned char i;
  /* run the state machine here for SPI */
   switch (appData.spiStateMachine)
{
    default:
    case APP_SPI_STATE_START:
        /* set the state to 'wait' early so that the interrupt doesn't
            finish fast and write the state and then is overwritten */
        appData.spiStateMachine =  APP_SPI_STATE_WAIT;

        MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);    //start clock from here 
        for (i=0; i<3; i++)
        {
            app_spi_tx_buffer[i];
        MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
        }
        appData.drvSPIBufferHandle = DRV_SPI_BufferAddWrite(appData.handleSPI0,
            app_spi_tx_buffer, sizeof(app_spi_tx_buffer),
            0, 0);

        if (DRV_SPI_BUFFER_HANDLE_INVALID == appData.drvSPIBufferHandle)
        {

            /* try again if we get a bad handle */
            appData.spiStateMachine =  APP_SPI_STATE_START;
        }
    break;

    case APP_SPI_STATE_WAIT:
    {
 if ( DRV_SPI_BufferStatus(appData.drvSPIBufferHandle) &    DRV_SPI_BUFFER_EVENT_COMPLETE)
        {
            appData.spiStateMachine = APP_SPI_STATE_DONE;
        }
    }
    break;

    case APP_SPI_STATE_DONE:
    break;
   }
 }

 void APP_Initialize ( void )
{
/* Place the App state machine in its initial state. */
appData.state = APP_STATE_INIT;
Modulator_SPI_Enable();
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); //  CS  will be low 

appData.handleSPI0 = DRV_HANDLE_INVALID;

/* TODO: Initialize your application's state machine and other
 * parameters.
 */
}


 void APP_Tasks ( void )
 {

/* Check the application's current state. */
switch ( appData.state )
  {
    /* Application's initial state. */
    case APP_STATE_INIT:
    {
        bool appInitialized = true;


        if (DRV_HANDLE_INVALID == appData.handleSPI0)
        {
            appData.handleSPI0 = DRV_SPI_Open(0, DRV_IO_INTENT_WRITE);
            appInitialized &= (DRV_HANDLE_INVALID != appData.handleSPI0);
        }

        if (appInitialized)
        {
            /* initialize the SPI state machine */
            appData.spiStateMachine = APP_SPI_STATE_START;

            appData.state = APP_STATE_SERVICE_TASKS;
        }
        break;
    }

    case APP_STATE_SERVICE_TASKS:
    {
        /* run the state machine for servicing the SPI */
        SPI_Task();

        break;
    }

    /* TODO: implement your application state machine.*/


    /* The default state should never be executed. */
    default:
    {
        /* TODO: Handle error in application's state machine. */
        break;
    }
  }
  }

Thanks

Nome

Upvotes: 0

Views: 345

Answers (1)

blsmit5728
blsmit5728

Reputation: 444

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++)
{
    /* CS = HIGH */
    SPI1BUF=buff[i];
    while(SPI1STATbits.SPIBUSY);
    // read the SDI line
    unsigned char x = SPI1BUF;
    /* CS = LOW */
}

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

EDIT:

I will not do you project for you, here is my example with a bit more example code.

unsigned char buff[128]={0xD4,0x04,0x03,0x02,0x01,0x1E.....};
unsigned char i;
SPI1CON = 0x8020; // SPI on and SPI Master see 61106G.pdf PIC32 FRM
unsigned char j = 0;
for(i = 0; i < 128; i++)
{
    /* CS = HIGH */
    if (j == 0)            
        MOD_SPI_CS_DESELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
    SPI1BUF=buff[i];
    while(SPI1STATbits.SPIBUSY);
    // read the SDI line
    unsigned char x = SPI1BUF;
    if ( j == 2)
    {
       j = 0;
       MOD_SPI_CS_SELECT(SPI_SLAVE_1_CS_PORT_ID,SPI_SLAVE_1_CS_PORT_PIN);
    }
    else
       j++;
    /* CS = LOW */
}

Upvotes: 1

Related Questions