SAMD21G18A SPI Help

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all, I'm trying to get an SAMD21G18A to write to an MCP4131 digipot. I tired the asf code example for SPI but it is not working. Where am I going wrong?

 

 /*

  * File:       Main.c

  * Author:     Armstrong Subero

  * Controller: SAMD21G18A w/INT OSC @ 48 MHz 3.3v

  * Program:    01_Pushbutton

  * Compiler:   ARM/GNU (v 5.3.1, Atmel Studio v7.0)

  *

  * Program Version: 1.0

  *        * Added:

  *        * Issues:

  *

  * 

  * Program Description: Turns on an LED when a pushbutton is pressed

  *

  * Hardware Description: An LED is connected via a 1k resistor to PIN PA23 and a Pushbutton connected to PIN_PB23 10K pullup

  *

  *   Created on November, 1st , 2016, 1:13 PM

  */

 

 

/*******************************************************************************

*Includes and defines

*******************************************************************************/

 

// Atmel Software Framework

#include <asf.h>

#include <stdio.h>

#include <stdlib.h>

 

#define LED_0_PIN PIN_PA23

#define SW_0_PIN    PIN_PB23

 

#define SLAVE_SELECT_PIN PIN_PA16

 

struct spi_module spi_master_instance;

struct spi_slave_inst slave;

 

void configure_spi_master(void);

void digipot_write(uint8_t data);

 

 

/*******************************************************************************

 * Function: void config_pins(void)

 *

 * Returns: Nothing

 *

 * Description: Configures IO Pins

 *

 *******************************************************************************/

 

static void config_pins(void)

{

 

 // General Pin config

 struct port_config config_port_pin;

 port_get_config_defaults(&config_port_pin);

 

 // Pushbutton Config

 config_port_pin.direction  = PORT_PIN_DIR_INPUT;

 config_port_pin.input_pull = PORT_PIN_PULL_UP;

 port_pin_set_config(SW_0_PIN, &config_port_pin);

 

 // LED Config

 config_port_pin.direction = PORT_PIN_DIR_OUTPUT;

 port_pin_set_config(LED_0_PIN, &config_port_pin);

}

 

void configure_spi_master(void)

{

struct spi_config config_spi_master;

struct spi_slave_inst_config slave_dev_config;

/* Configure and initialize software device instance of peripheral slave */

spi_slave_inst_get_config_defaults(&slave_dev_config);

slave_dev_config.ss_pin = SLAVE_SELECT_PIN;

spi_attach_slave(&slave, &slave_dev_config);

/* Configure, initialize and enable SERCOM SPI module*/

config_spi_master.mux_setting = SPI_SIGNAL_MUX_SETTING_A;

config_spi_master.mode_specific.master.baudrate = 1000000;

config_spi_master.pinmux_pad0 = PINMUX_PA04D_SERCOM0_PAD0;

config_spi_master.pinmux_pad1 = PINMUX_PA05D_SERCOM0_PAD1;

config_spi_master.pinmux_pad2 = PINMUX_UNUSED;

config_spi_master.pinmux_pad3 = PINMUX_UNUSED;

spi_init(&spi_master_instance, SERCOM0, &config_spi_master);

spi_enable(&spi_master_instance);

}

 

 

void digipot_write(uint8_t data)

{

uint8_t data2 = 0x00;

// set CS low

spi_select_slave(&spi_master_instance, &slave, false);

spi_write_buffer(&spi_master_instance, data2,1);

spi_write_buffer(&spi_master_instance, data,1);

// set CS high

spi_select_slave(&spi_master_instance, &slave, true);

}

 

 

 

/*******************************************************************************

 * Function: int main(void)

 *

 * Returns: Nothing

 *

 * Description: Initializations for main

 *

 *******************************************************************************/

 

int main(void)

{

system_init();

config_pins();

configure_spi_master();

while (true) {

digipot_write(100); 

delay_ms(500);

}

return 0;

}

 

 

Asubero

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
// set CS low

spi_select_slave(&spi_master_instance, &slave, false);

The last arg to spi_select_slave is not about the logic level on the CS signal, you need 'true'  to select the slave.

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Lajon wrote:
The last arg to spi_select_slave is not about the logic level on the CS signal

One could argue that it is - because the CS signal is active-low ...

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't get that sorry, the program above would be ok if passing false resulted in a low level on CS.

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I mean that CS is active low; ie, the low level is the "true" (or "asserted") state for CS

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah the MCP4131 is active low..idk whats going on..I tried SPI with the ST7735 as well...my scope shows everything looking well..so I guess it has to be some obsure setting..I even tried lowering the baud rate...does anyone have verifyable working SPI code for the SAMD21?

Asubero

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So you did change to use

spi_select_slave(&spi_master_instance, &slave, true);

to select the slave already? But I guess if everything looks ok on the scope the problem can't be the SAMD21 code. 

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sigh..tried three different spi devices now...still not working...

Asubero