SAM4S Explained Pro TWI

Moderator: nferre

Posts: 3
Joined: Tue Aug 11, 2015 6:33 am

SAM4S Explained Pro TWI

Tue Aug 11, 2015 6:51 am


I'm working on SAM4S Explained Pro Board and new to this. I wrote the code for interfacing gyroscope L344200D to this board through TWI0. Im using Atmel studio 6.2. The code is compiling and I'm able to download it into board also.
BUT when I power ON the board, I'm not getting anything at TWCK0 or TWD0 pin.
I have connected 10K pull ups also.
Please let me know where I'm going wrong.
The code is shown below...

Code: Select all

#include "asf.h"
#include "stdio_serial.h"
#include "conf_board.h"

#define TWI_CLK 400000		//Fast mode 400 khz

#define Gyro_ADDR 0x68		//Gyroscope device address

#define WHO_AM_I 0x0F
#define CTRL_REG1 0x20
#define CTRL_REG2 0x21
#define CTRL_REG3 0x22
#define CTRL_REG4 0x23
#define CTRL_REG5 0x24
#define OUT_X_L 0x28		// x LSB
#define OUT_X_H 0x29		// x MSB
#define OUT_Y_L 0x2A		// y LSB
#define OUT_Y_H 0x2B		// y MSB
#define OUT_Z_L 0x2C		// z LSB
#define OUT_Z_H 0x2D		// Z MSB

	/*Input parameters when initializing the TWI module mode*/
	twi_options_t opt;
	/*Information concerning the data transmission*/
	twi_packet_t packet_rx, packet_tx;

int16_t gyro_read_x()
	uint8_t data_received = 0;
	int16_t gyro_x = 0;
	twi_packet_t packet_rx = {
		.addr_length	= sizeof(uint8_t),
		.chip		= Gyro_ADDR,
		.buffer	= &data_received,
		.length	= sizeof(uint8_t)
	packet_rx.addr[0] = OUT_X_H;
	while (twi_master_read(TWI_CHANNEL, &packet_rx) != TWI_SUCCESS);
	gyro_x = data_received << 8;
	packet_rx.addr[0] = OUT_X_L;
	while (twi_master_read(TWI_CHANNEL, &packet_rx) != TWI_SUCCESS);
	gyro_x |= data_received;
	return gyro_x;

int main (void)
	uint8_t data_received = 0;
	/* Initialize the SAM system */
	/* Initialize the board */
	/* Configure systick for 1 ms */
//	SysTick_Config(sysclk_get_cpu_hz() / 1000);

	/* Enable the peripheral clock for TWI */
	/* Configure the options of TWI driver */
	opt.master_clk = sysclk_get_cpu_hz();
	opt.speed      = TWI_CLK;
	memset((void *)&opt, 0, sizeof(opt));
	// Init TWI Master
	twi_master_init(TWI_CHANNEL, &opt);

	uint8_t command=0x0F;
			/* Configure the data packet to be transmitted */
			packet_tx.chip        = Gyro_ADDR;
			packet_tx.addr[0]     = CTRL_REG1;
			packet_tx.addr_length = sizeof(uint8_t);
			packet_tx.buffer      = &command;
			packet_tx.length      = sizeof(uint8_t);
	while(twi_master_write(TWI_CHANNEL, &packet_tx) != TWI_SUCCESS);
		/* Configure the data packet to be received */
	packet_rx.chip        = Gyro_ADDR;			// Gyro chip address
	packet_rx.addr[0]     = WHO_AM_I;			// Register to be read
	packet_rx.addr_length = sizeof (uint8_t);		// size of register to be read
	packet_rx.buffer      = &data_received;		// Buffer to store received data
	packet_rx.length      = sizeof (uint8_t);		// size of data to be received
	while(twi_master_read(TWI_CHANNEL, &packet_rx) != TWI_SUCCESS);
	int16_t x_rot = gyro_read_x();
Posts: 1
Joined: Thu Nov 05, 2015 2:33 pm

Re: SAM4S Explained Pro TWI

Mon Dec 07, 2015 2:44 pm

I am working with Atmel SAM4S xpalined pro evaluation kit and Atmel studio 6.2. I want to interface a sensor BME280 pressure and humidity sensor. I want to know how I configure it at atmel studio. where I will save the .c, .h, .support files?

Return to “SAM9-based”

Who is online

Users browsing this forum: No registered users and 2 guests