SAM4S as TWI slave example?

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

Hi!

Does anyone have an example using SAM4 as a twi slave?

This topic has a solution.
Last Edited: Mon. Nov 30, 2020 - 08:29 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have attached an example for the TWIS (https://ww1.microchip.com/downloads/en/AppNotes/Atmel-42273-SAM4-TWI-Slave-Mode-Driver_ApplicationNote_AT07335.pdf) from ASF-3.45, but you should be able to find these in your ASF distribution

 

Ilya

Attachment(s): 

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

Thanks for the reply. I've now gotten it to work so I can send and receive to my slave.

 

Still have a question though: slaves always have registers for calibrating and setting up the device. How would I implement that? As it is now, it only has has its main address where it reads the bytes I send at it.

I'm running FreeRTOS by the way.

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

wqvist wrote:

Thanks for the reply. I've now gotten it to work so I can send and receive to my slave.

Its nice to hear that you have got the basic communication working like it is supposed to be.

 

Actually you are sort of lucky because I am also working on a project whereby I am using 2 SAM4S based boards (ATSAM4S-EK2 and ATMSAM4S xplained) and FreeRTOS running on both boards, so I may be able to share information/ideas/code.

 

qqvist wrote:

Still have a question though: slaves always have registers for calibrating and setting up the device. How would I implement that? As it is now, it only has has its main address where it reads the bytes I send at it.

I'm running FreeRTOS by the way.

If I were you in this situation I would  have a general set_config() function along the lines of

uint32_t atsam4s_set_config(uint8_t uc_config)
{
    return atsam4s_read_register(ATSAM4S_CONFIG_REG, p_config);
}

static uint32_t atsam4s_read_register(uint32_t ul_reg_index, uint8_t *p_reg)
{
	twi_package_t rx = {
		.chip = BOARD_ATSAM4S_ADDR,
		.addr = {ul_reg_index},
		.addr_length = 1,
		.buffer = p_reg_value
	};

	if (ul_reg_index == ATSAM4S_CONF_REG) {
		rx.length = 1;
	} else {
		rx.length = 2;
	}

	return twi_master_read(BOARD_ATSAM4S_TWI_INSTANCE, &rx);
}

static uint32_t atsam4s_write_register(uint32_t ul_reg_index,
		uint8_t *p_reg_value)

{
	twi_package_t tx = {
		.chip = BOARD_ATSAM4S_ADDR,
		.addr = {ul_reg_index},
		.addr_length = 1,
		.buffer = p_reg_value
	};

	if (ul_reg_index == ATSAM4S_CONF_REG) {
		tx.length = 1;
	} else {
		tx.length = 2;
	}

	return twi_master_write(BOARD_ATSAM4S_TWI_INSTANCE, &tx);
}		

Mind you, this code is untested and is meant only to give you the general direction. This is a quick and dirty way of doing what you want.

 

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

wqvist wrote:
I've now gotten it to work

Jolly good.

 

Now please mark the solution - see Tip #5 in my signature, below:

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...