[ASF] [C] [SPI] [UC3] Problem with setting up clock pin

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

Hello, people.

I use a userboard with UC3C2256. I've managed to use a simple output on pins and also a USB CDC interface (virtual COM).

Now I need to setup SPI and I2C/TWI interfaces, but I can get no clock signal on SCK (SPI) or TWCK (TWI). I'd really appreciate any help/suggestions. Here's my code:

 

user_board.h

#define BOARD_OSC0_HZ             12000000
#define BOARD_OSC0_TYPE           XOSC_TYPE_XTAL
#define BOARD_OSC0_IS_XTAL        true
#define BOARD_OSC0_STARTUP_US     0

conf_spi_master.h

//! Default Config Spi Master Delay BCS
#define CONFIG_SPI_MASTER_DELAY_BCS            0

//! Default Config Spi Master Bits per Transfer Definition
#define CONFIG_SPI_MASTER_BITS_PER_TRANSFER    16

//! Default Config Spi Master Delay BCT
#define CONFIG_SPI_MASTER_DELAY_BCT            0

//! Default Config Spi Master Delay BS
#define CONFIG_SPI_MASTER_DELAY_BS             0

//! Default Config Spi Master Dummy Field
#define CONFIG_SPI_MASTER_DUMMY                0x0

conf_clock.h

#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_OSC0

/* Fbus = Fsys / (2 ^ BUS_div) */
#define CONFIG_SYSCLK_CPU_DIV         0
#define CONFIG_SYSCLK_PBA_DIV         0
#define CONFIG_SYSCLK_PBB_DIV         0
#define CONFIG_SYSCLK_PBC_DIV         0

#define CONFIG_USBCLK_SOURCE        USBCLK_SRC_PLL1

/* Fusb = Fsys / USB_div */
#define CONFIG_USBCLK_DIV             1

#define CONFIG_PLL0_SOURCE            PLL_SRC_OSC0

/* Fpll0 = (Fclk * PLL_mul) / PLL_div */
#define CONFIG_PLL0_MUL               1
#define CONFIG_PLL0_DIV               1

#define CONFIG_PLL1_SOURCE          PLL_SRC_OSC0

/* Fpll1 = (Fclk * PLL_mul) / PLL_div */
#define CONFIG_PLL1_MUL               (48000000 / BOARD_OSC0_HZ)
#define CONFIG_PLL1_DIV               1

conf_twim.h

#if UC3C
// These defines are missing from or wrong in the toolchain header file ip_xxx.h or part.h
#	if !defined(AVR32_TWIM0_GROUP)
#		define AVR32_TWIM0_GROUP         25
#	else
#		warning "Duplicate define(s) to remove from the ASF"
#	endif
#	if !defined(AVR32_TWIM1_GROUP)
#		define AVR32_TWIM1_GROUP         26
#	else
#		warning "Duplicate define(s) to remove from the ASF"
#	endif
#	if !defined(AVR32_TWIM2_GROUP)
#		define AVR32_TWIM2_GROUP         45
#	else
#		warning "Duplicate define(s) to remove from the ASF"
#	endif
#	define CONF_TWIM_IRQ_LINE          AVR32_TWIM1_IRQ
#	define CONF_TWIM_IRQ_GROUP         AVR32_TWIM1_GROUP
#	define CONF_TWIM_IRQ_LEVEL         1

main.c


#include <asf.h>
#include "conf_usb.h"
#include "string.h"
#include <stdlib.h>

#define APP 0

static bool my_flag_autorize_cdc_transfert = false;

char bufferForCDC[256];
int bufferForCDCSize = 256;

void ledSignal(int num)
{
	for(int i = 0; i < num; i++)
	{
		if(i % 2 == 0)
		{
			ioport_set_pin_level(AVR32_PIN_PD11, IOPORT_PIN_LEVEL_LOW);
		}
		else
		{
			ioport_set_pin_level(AVR32_PIN_PD11, IOPORT_PIN_LEVEL_HIGH);
		}
		cpu_delay_ms(100, F_CPU);
	}
}

bool my_callback_cdc_enable(void)
{
	my_flag_autorize_cdc_transfert = true;
	return true;
}

void my_callback_cdc_disable(void)
{
	my_flag_autorize_cdc_transfert = false;
}

void task(char* str, int len, bool isStopOnZero)
{
	if (my_flag_autorize_cdc_transfert) {
		for(int i = 0; i < len; i++)
		{
			if(isStopOnZero && str[i] == 0)
			{
				return;
			}
			udi_cdc_putc(str[i]);
		}
	}
}

#define PORT_SIZE 32

#define MY_PIN_DIOD AVR32_PIN_PD11

#define MY_PIN_E2 AVR32_PIN_PA08

#define MY_PIN_A0 AVR32_PIN_PA23
#define MY_PIN_A1 AVR32_PIN_PA22
#define MY_PIN_A2 AVR32_PIN_PA21
#define MY_PIN_A3 AVR32_PIN_PA20

#define SPI0 &AVR32_SPI0

#define MY_PIN_SPI_MOSI AVR32_PIN_PD00
#define MY_PIN_SPI_MISO AVR32_PIN_PD01
#define MY_PIN_SPI_SCK AVR32_PIN_PD02
#define MY_PIN_SPI_SS AVR32_PIN_PD03

int thermalSensor1Data = 0;
int thermalSensor2Data = 0;
int thermalSensor3Data = 0;
int thermalSensor4Data = 0;

struct spi_device deviceSPI =
{
	.id = 0
};
int64_t baudrateSPI = BOARD_OSC0_HZ;
int64_t lengthSPIRead = 1;
uint16_t dataSPIRead[1];

#define TWI0 &AVR32_TWIM0
#define TWI1 &AVR32_TWIM1

#define MY_PIN_TWI_TWD0 AVR32_PIN_PC02
#define MY_PIN_TWI_TWCK0 AVR32_PIN_PC03
#define MY_PIN_TWI_TWD1 AVR32_PIN_PC04
#define MY_PIN_TWI_TWCK1 AVR32_PIN_PC05

void togglePin(int pin, bool level)
{
	ioport_set_port_level(pin / PORT_SIZE, 1 << (pin % PORT_SIZE), level);
}

int64_t chipSelectSPI(int index)
{
	togglePin(MY_PIN_A0, false);
	togglePin(MY_PIN_A1, false);
	togglePin(MY_PIN_A2, false);
	switch(index)
	{
		case 1:
			break;
		case 2:
			togglePin(MY_PIN_A0, true);
			break;
		case 3:
			togglePin(MY_PIN_A1, true);
			break;
		case 4:
			togglePin(MY_PIN_A2, true);
			break;
	}
	togglePin(MY_PIN_E2, true);
	spi_select_device(&AVR32_SPI0, &deviceSPI);
	cpu_delay_ms(1000, F_CPU);
}

void chipDeselectSPI(void)
{
	togglePin(MY_PIN_E2, false);
	spi_deselect_device(&AVR32_SPI0, &deviceSPI);
}

int64_t readThermalSensorSPI(int index)
{
	chipSelectSPI(index);
	if(spi_read_packet(&AVR32_SPI0, dataSPIRead, lengthSPIRead) == STATUS_OK)
	{
		chipDeselectSPI();
		return dataSPIRead[0];
	}
	else
	{
		chipDeselectSPI();
	}
}

int main (void)
{
	// BOARD
	board_init();
	sysclk_init();
	// USB
	irq_initialize_vectors();
	cpu_irq_enable();
	// SPI
	static const gpio_map_t TWI_GPIO_MAP =
	{
		{AVR32_TWIMS0_TWD_0_0_PIN, AVR32_TWIMS0_TWD_0_0_FUNCTION},
		{AVR32_TWIMS0_TWCK_0_0_PIN, AVR32_TWIMS0_TWCK_0_0_FUNCTION},
		{AVR32_TWIMS1_TWD_0_0_PIN, AVR32_TWIMS1_TWD_0_0_FUNCTION},
		{AVR32_TWIMS1_TWCK_0_0_PIN, AVR32_TWIMS1_TWCK_0_0_FUNCTION},
		{AVR32_SPI0_MISO_0_0_PIN, AVR32_SPI0_MISO_0_0_FUNCTION},
		{AVR32_SPI0_MOSI_0_0_PIN, AVR32_SPI0_MOSI_0_0_FUNCTION},
		{AVR32_SPI0_NPCS_0_0_PIN, AVR32_SPI0_NPCS_0_0_FUNCTION},
		{AVR32_SPI0_SCK_0_0_PIN, AVR32_SPI0_SCK_0_0_FUNCTION}
	};
	gpio_enable_module(TWI_GPIO_MAP, sizeof(TWI_GPIO_MAP) / sizeof(TWI_GPIO_MAP[0]));

	spi_master_init(&AVR32_SPI0);
	spi_master_setup_device(&AVR32_SPI0, &deviceSPI, SPI_MODE_3, baudrateSPI, MY_PIN_SPI_SS);
	spi_enable(&AVR32_SPI0);

	// PINS
	ioport_set_pin_dir(MY_PIN_DIOD, IOPORT_DIR_OUTPUT);

	ioport_set_pin_dir(MY_PIN_A0, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(MY_PIN_A1, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(MY_PIN_A2, IOPORT_DIR_OUTPUT);
	ioport_set_pin_dir(MY_PIN_A3, IOPORT_DIR_OUTPUT);

	ioport_set_pin_dir(MY_PIN_E2, IOPORT_DIR_OUTPUT);

	ioport_set_pin_dir(MY_PIN_SPI_SS, IOPORT_DIR_INPUT);
	ioport_set_pin_mode(MY_PIN_SPI_SS, IOPORT_MODE_PULLUP);

	ioport_set_pin_dir(MY_PIN_SPI_MOSI, IOPORT_DIR_OUTPUT);
	ioport_set_pin_level(MY_PIN_SPI_MOSI, 1);
	ioport_set_pin_dir(MY_PIN_SPI_MISO, IOPORT_DIR_INPUT);
	ioport_set_pin_dir(MY_PIN_SPI_SCK, IOPORT_DIR_OUTPUT);
	ioport_set_pin_level(MY_PIN_SPI_SCK, 1);

	// TWI

	twi_master_options_t optionsTWIM = { .speed = 50000, .chip = 0x46 };
	twi_master_setup(TWI1, &optionsTWIM);

	int8_t statusTWISetup;
	status_code_t statusTWIPackage;
	int64_t lengthTWIPacket = 2;
	uint8_t dataTWIRead[2];
	twi_package_t packageTWIRead =
	{
		.addr = 0x0,
		.addr_length = 1,
		.chip = 0x46,
		.buffer = dataTWIRead,
		.length = lengthTWIPacket,
	};

	#if APP == 0
	ledSignal(5);
	udc_start();
	while(true)
	{
		cpu_delay_ms(1000, F_CPU);
		ledSignal(5);

		int statusTWIMread = twi_master_read(TWI1, &packageTWIRead);

		bool b = spi_is_rx_ready(&AVR32_SPI0);
		thermalSensor1Data = readThermalSensorSPI(1);
		thermalSensor2Data = readThermalSensorSPI(2);
		thermalSensor3Data = readThermalSensorSPI(3);
		thermalSensor4Data = readThermalSensorSPI(4);
		sprintf(bufferForCDC, "SPI rx ready: %d\n\r", b);
		task(bufferForCDC, bufferForCDCSize, true);
		sprintf(bufferForCDC, "Thermal sensor 1 data: %d\n\r", thermalSensor1Data);
		task(bufferForCDC, bufferForCDCSize, true);
		sprintf(bufferForCDC, "Thermal sensor 2 data: %d\n\r", thermalSensor2Data);
		task(bufferForCDC, bufferForCDCSize, true);
		sprintf(bufferForCDC, "Thermal sensor 3 data: %d\n\r", thermalSensor3Data);
		task(bufferForCDC, bufferForCDCSize, true);
		sprintf(bufferForCDC, "Thermal sensor 4 data: %d\n\r", thermalSensor4Data);
		task(bufferForCDC, bufferForCDCSize, true);
		sprintf(bufferForCDC, "TWI read status: %d\n\r", statusTWIMread);
		task(bufferForCDC, bufferForCDCSize, true);
		sprintf(bufferForCDC, "TWI data[0]: %d\n\r", dataTWIRead[0]);
		task(bufferForCDC, bufferForCDCSize, true);
		sprintf(bufferForCDC, "TWI data[1]: %d\n\r", dataTWIRead[1]);
		task(bufferForCDC, bufferForCDCSize, true);
		sprintf(bufferForCDC, "******\n\r", thermalSensor4Data);
		task(bufferForCDC, bufferForCDCSize, true);
	}
	#elif APP == 1

	#endif

}

 

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

you probably have not set up SPI register properly, check your enable bit on your SPI register..