Go Down

Topic: Arduino Zero Sercom1 SPI Slave (Read 11762 times) previous topic - next topic

hhbcd

Thanks!

It's really helpful.


Hi hhbcd,

On my Windows based machine the register definitions are located at:

C:\Users\Computer\AppData\Local\Arduino15\packages\tools\arduino\CMSIS\4.0.0-atmel\Device\ATMEL\samd21\include\

In the "include" directory, there are two further directories: "instance" and "component".

Instance contains the register definitions for each peripheral instance, for example tcc0.h, tcc1.h, etc...

Component contains the structures and definitions for the peripheral's register bitfields, for example tcc.h.

MatthewHoworko

  • Changed DOPO to 0, to select PAD0 for MISO (corresponding to PA12:MISO on the board)
Hi Maverick123,

DOPO should be set to 0x3, which corresponds to PAD[0]: MISO , PAD[3]: SCK, PAD[1]: SS. The other option (0x0) will not select the correct SCK and SS pads.

Code: [Select]

SERCOM4->SPI.CTRLA.bit.DOPO = 0x3;


More info can be found on page 438 of the complete datasheet.

psshetty

void spi_slave_init()
{
  /* MOSI(DI)-PAD0,SCK-PAD1,SS-PAD2,MISO(DO)-PAD3
  SPI in slave mode */
  spi_slave_pin_init();
  SERCOM1->SPI.CTRLA.reg = SERCOM_SPI_CTRLA_DOPO(0x2) |
  SERCOM_SPI_CTRLA_MODE_SPI_SLAVE ;
 
  /* SPI receiver enabled */
  SERCOM1->SPI.CTRLB.reg = SERCOM_SPI_CTRLB_RXEN ;
  SERCOM1->SPI.CTRLB.bit.PLOADEN = 0x1;
  /* synchronization busy */
  while(SERCOM1->SPI.SYNCBUSY.bit.CTRLB);
  /* SERCOM1 enabled */
  SERCOM1->SPI.CTRLA.reg |= SERCOM_SPI_CTRLA_ENABLE;
  /* synchronization busy */
  // while(SERCOM1->SPI.SYNCBUSY.reg & SERCOM_SPI_SYNCBUSY_ENABLE);
  /* SERCOM1 interrupt handler mapped to callback handler 'SERCOM1_App_Handler' */
  // _sercom_set_handler (SERCOM_INTRANCE_INDEX, (sercom_handler_t) SERCOM1_App_Handler);
  /* SERCOM1 handler enabled */
  // system_interrupt_enable(SERCOM1_IRQn);
 
  SERCOM1->SPI.CTRLA.bit.DORD = 0; //MSB first
  //SerialUSB.println( SERCOM1->SPI.CTRLA.bit.DORD);
  SERCOM1->SPI.CTRLA.bit.CPOL = 0; //SCK is low when idle, leading edge is rising edge
  SERCOM1->SPI.CTRLA.bit.CPHA = 0; //data sampled on leading sck edge and changed on a trailing sck edge
  SERCOM1->SPI.CTRLA.bit.FORM = 0x0; //Frame format = SPI
  SERCOM1->SPI.CTRLA.bit.DIPO = 0x3; //DATA PAD0 MOSI is used as input (slave mode)
  SERCOM1->SPI.CTRLA.bit.DOPO = 0x0; //DATA PAD3 MISO is used as output
  SERCOM1->SPI.CTRLA.bit.MODE = 0x2; //SPI in Slave mode
 
  //SERCOM1->SPI.CTRLA.bit.IBON = 0x1; //Buffer Overflow notification
  //SERCOM1->SPI.CTRLA.bit.RUNSTDBY = 1; //wake on receiver complete
 
  //Set up SPI control B register
  SERCOM1->SPI.CTRLB.bit.RXEN = 0x1; //Enable Receiver
  SERCOM1->SPI.CTRLB.bit.SSDE = 0x1; //Slave Selecte Detection Enabled
  SERCOM1->SPI.CTRLB.bit.CHSIZE = 0; //character size 8 Bit
  //SERCOM1->SPI.CTRLB.bit.PLOADEN = 0x1;
  //SerialUSB.println(SERCOM1->SPI.CTRLB.bit.PLOADEN);
  //SERCOM1->SPI.CTRLB.reg = SERCOM_SPI_CTRLB_RXEN | SERCOM_SPI_CTRLB_PLOADEN;
 
}

void spi_slave_pin_init()
{
  /* pin18 pull-resistor is set into high */
  PORT->Group[0].PINCFG[18].reg = PORT_PINCFG_INEN | PORT_PINCFG_PULLEN;
  PORT->Group[0].OUTSET.reg = (1u << 18);
  pin_set_peripheral_function(PINMUX_PA16C_SERCOM1_PAD0); //MOSI
  pin_set_peripheral_function(PINMUX_PA17C_SERCOM1_PAD1); //SCK
  pin_set_peripheral_function(PINMUX_PA18C_SERCOM1_PAD2); //SS
  pin_set_peripheral_function(PINMUX_PA19C_SERCOM1_PAD3); //MISO
}
static void pin_set_peripheral_function(uint32_t pinmux)
{
  uint8_t port = (uint8_t)((pinmux >> 16)/32);
  PORT->Group[port].PMUX[((pinmux >> 16) - (port*32))/2].reg &= ~(0xF << (4 * ((pinmux >>
   16) & 0x01u)));
  PORT->Group[port].PMUX[((pinmux >> 16) - (port*32))/2].reg |= (uint8_t)((pinmux &
  0x0000FFFF) << (4 * ((pinmux >> 16) & 0x01u)));
  PORT->Group[port].PINCFG[((pinmux >> 16) - (port*32))].bit.PMUXEN = 1;
}

This is the code i had used for slave setting
 From master Im sending 1 byte in slave side 3 bytes has to be transfer as response for 1 byte of master.
But in master im getting first byte as master data which is sent by master initially..so that slave's third byte will be lost can any one help me out ....

Go Up