Hi!
Does anyone have an example using SAM4 as a twi slave?
Hi!
Does anyone have an example using SAM4 as a twi slave?
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
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.
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.
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.
I've now gotten it to work
Jolly good.
Now please mark the solution - see Tip #5 in my signature, below: