7 years, 4 months ago.

spi initialising not working.nucle_f401RE with PLC IC

hello, i am working on a PLC communication device,which transmits data over powerline.I am using Nucleo F401RE with my PLC module.using SPI i am trying to communicate.but the initail SPI initalising for the data transmission is not working.

Would anyone be helpfull!! Thanks in aAdvance.

Here is the code:

#include "mbed.h"

#define SPI_REG_BFR_SIZE         0x0100
#define SPI_REG_WRBUF_SPC_AVA    0x0200
#define SPI_REG_RDBUF_BYTE_AVA   0x0300
#define SPI_REG_SPI_CONFIG       0x0400
#define SPI_REG_INTR_CAUSE       0x0C00
#define SPI_REG_INTR_ENABLE      0x0D00
#define SPI_REG_SIGNATURE        0x1A00
#define SPI_INIT_ADDR            0x0000
#define REG_VAL                  0xAA55
//PA_9,PA_10

Serial pc(USBTX, USBRX);     // tx, rx
//SPI device(SPI_MOSI, SPI_MISO, SPI_SCK);
SPI device(PB_5, PB_4, PB_3);
DigitalOut cs(PA_4);
void plc_transmit();
void plc_receive();

int rec_data,rcv=0,ava=0;
 
int main() {
    
    pc.baud(115200);
    cs=1;                
  
    device.format(16,3);                                                /* SPI 16 bit & Mode3 */
    device.frequency(12000000);                                         /* SPI Clock 12 MHz */
    pc.printf("Checking PC Transmission");
    cs=0;
    device.write(SPI_REG_SIGNATURE); 
    cs=1;                                   /* Read the SIG REG and ignores first value */
    wait_us(50);
    cs=0; 
    rec_data = device.write(SPI_REG_SIGNATURE); 
    cs=1;                        /* Reading SIG REG again */
    wait_us(50);
    if(rec_data == REG_VAL)  
    pc.printf("SPI Intialisation OK");     /* Check the Reset Value */
    while(1);
    {
     plc_transmit();
     plc_receive();
    }
}

void plc_transmit()
{
  if(pc.readable())          /* Check PC data Availablity */ 
  {
    rec_data = pc.getc();    /* Reading the avaialble data*/
    pc.putc(rec_data);
    rcv = 1;
  }
  if(rcv==1)
  {
   ava = device.write(SPI_REG_WRBUF_SPC_AVA);   /* Reading the write Buffer */
   device.write(SPI_REG_BFR_SIZE);              /* Write available byte to Bfr size Reg*/
   device.write(1);
   device.write(SPI_INIT_ADDR); 
   device.write(rec_data);
  }
}

void plc_receive()
{
 ava=device.write(SPI_REG_RDBUF_BYTE_AVA);      /* Reading the read Buffer */
 pc.putc(ava);
 device.write(SPI_REG_BFR_SIZE);                /* Write available byte to Bfr size Reg*/
 device.write(ava); 
 if(ava>0)                                      /* Check the avaialable data is valid */
 {
  rec_data=device.write(SPI_INIT_ADDR);         /* Start Reading from Intial Address */
  //pc.putc(rec_data);                          /* Transmitt through PC */
  }
}

1 Answer

7 years, 4 months ago.

Hi. Which PLC IC are you using ? Some ideas to test..

insert at line 39 the following code for debugging:

   pc.printf("SPI word received: ");
   pc.putc(rec_data>>8);    // print high byte of received data from SPI bus
   pc.putc(rec_data&0xff);  // print low byte of received data from SPI bus

then try again. Moving forward, other ideas are to try with a different SPI mode and/or SPI speed as the bus speed may be too high for this SPI interfaced controller IC. Try with a slower SPI Clock speed of 1 Mhz and then move to the Mode 0 of the SPI format. Any chance you have a logic analyzer ? If not, consider to pickup a Salae or similar from Ebay - many are very low cost and they can greatly reduce your debug time by placing the SPI interface under test with a SPI module.

   device.format(16,0);         /* SPI 16 bit & Mode0 */
   device.frequency(1000000);  // test with 1 Mhz speed for the SPI clock  

http://www.ebay.com/itm/USB-Logic-Analyzer-24MHz-8-Channel-UART-IIC-SPI-Debug-for-MCU-FPGA-ARM-/191685084604?hash=item2ca15245bc:g:ymsAAOSwLF1YBC6y

Please post your results and supply more details on your PLC IC.