0

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

Nome
  • 27
  • 8
  • 3
    And how are we supposed to help if you don't show any relevant information? See [ask] and follow the advice and include a [mcve]! Sidenote: Your two macros looks like "the maximum and the maximum-maximum". This is confusing and not a good idea. – too honest for this site Feb 06 '17 at 12:24
  • I don't know if it is intended, but you initialized only 6 bytes of spi_tx_buffer and the size of it (MAX_NUM_OF_BYTES_IN_BUF) seems to be 8 – aldarel Feb 06 '17 at 12:54
  • i can get only one char types 0xd4 on my DSO how to increase number of bytes or values? – Nome Feb 06 '17 at 13:35

1 Answers1

1

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 */
}
blsmit5728
  • 434
  • 3
  • 11
  • Thank for your reply sir this is very usfull for me if i want to add cs pin during sending will be low after three bytes high how can implement in our codes – Nome Feb 07 '17 at 16:31
  • Actually we want send 3 bytes one byte is write cmd 0xd4 second byte is register number and third byte is registers values during sending 3 bytes CS pin is low after last byet CS must be high .there are total 30 registers 30+30+30 = 90 bytes want to send by kindly help me thanks – Nome Feb 07 '17 at 16:38
  • If you need to add a CS, think of each for loop as a SPI write (which it is) and add your chip select around the contents of the for loop – blsmit5728 Feb 08 '17 at 13:02
  • Thanks for your reply if our buff size is 90 bytes and i < 3 it can send only three bytes values i am confused about how every 3 bytes cs high and then low ? – Nome Feb 08 '17 at 17:03
  • I have edited above my new C file kindly check where i am wrong thanks – Nome Feb 08 '17 at 19:37
  • See my edit above. I will not do any more as it would be doing it for you. – blsmit5728 Feb 09 '17 at 18:11