Atmel SAM4 Series Application Note
Atmel SAM4 Series Application Note

Atmel SAM4 Series Application Note

Twi master mode driver. asf programmers manual

Advertisement

Quick Links

AT07334: SAM4 TWI Master Mode Driver

SAM4 TWI Master Mode Driver

This documents describe the usage of the driver for the TWI (Two-Wire Interface)
®
Master on the Atmel
SAM4 family of devices. It describes usage in an I
compatible manner and does not provide any guidence for the use of SMBus.
Prequisites
Module Overview
Examples
API Overview
Special Considerations
Extra Information
APPLICATION NOTE
ASF PROGRAMMERS MANUAL
2
C
42274A-MCU-05/2014

Advertisement

Table of Contents
loading

Summary of Contents for Atmel SAM4 Series

  • Page 1: Sam4 Twi Master Mode Driver

    This documents describe the usage of the driver for the TWI (Two-Wire Interface) ® Master on the Atmel SAM4 family of devices. It describes usage in an I compatible manner and does not provide any guidence for the use of SMBus.
  • Page 2: Table Of Contents

    Table of Contents SAM4 TWI Master Mode Driver ............1 Software License ................4 1. Prequisites ..................5 2. Module Overview ................6 2.1. TWI Bus Topology ..............6 3. Examples ..................7 4. API Overview ................8 4.1. Variable and Type Definitions ............8 4.1.1.
  • Page 3 7.3. Connections for Board: SAM4L Xplained Pro ........20 7.4. Connections for Board: SAM4L-EK ..........20 7.5. Description ................20 7.6. Usage ..................20 8. Quick Start Guide ............... 22 8.1. Use Cases ................22 8.2. TWIM Basic Usage ..............22 8.3.
  • Page 4: Software License

    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. The name of Atmel may not be used to endorse or promote products derived from this software without specific prior written permission.
  • Page 5: Prequisites

    Prequisites There are no prequisites for this module. AT07334: SAM4 TWI Master Mode Driver [APPLICATION NOTE] 42274A-MCU-05/2014...
  • Page 6: Module Overview

    Module Overview This driver provides access to the main features of the TWIM controller. The two wire interface connects components via a two-wire serial bus, each device, on the bus, has a unique ID. The TWI modules are programmable as masters with sequential or single-byte access. High speed mode capability is also supported.
  • Page 7: Examples

    Examples ● TWIM Master Example ● Quick Start Guide AT07334: SAM4 TWI Master Mode Driver [APPLICATION NOTE] 42274A-MCU-05/2014...
  • Page 8: Api Overview

    API Overview Variable and Type Definitions 4.1.1 Type twim_transfer_status_t typedef enum twim_transfer_status twim_transfer_status_t twim_transfer_status for enumeration definitions. 4.1.2 Type twim_callback_t typedef void(* twim_callback_t )(Twim *) Structure Definitions 4.2.1 Struct twim_config This is the configuration structure for the TWI Master device. It is used as an argument for twim_set_config provide the desired configurations for the module.
  • Page 9: Struct Twim_Package

    Macro Definitions 4.3.1 TWI Driver Compatibility These codes are defined for SAM devices to simplify the porting of existing code from other Atmel devices. For the definition of technical terms and acronyms refer to Acronyms and the appropriate device datasheet.
  • Page 10: Macro Twi_Fast_Mode_Plus_Speed

    4.3.1.4 Macro twi_probe #define twi_probe 4.3.2 Macro TWI_FAST_MODE_PLUS_SPEED #define TWI_FAST_MODE_PLUS_SPEED TWI Fast Mode Plus mode. 4.3.3 Macro TWI_FAST_MODE_SPEED #define TWI_FAST_MODE_SPEED Value to set TWI to Fast speed mode. 4.3.4 Macro TWI_HIGH_SPEED_MODE_SPEED #define TWI_HIGH_SPEED_MODE_SPEED Value to set TWI to High speed plus mode. 4.3.5 Macro TWI_STD_MODE_SPEED #define TWI_STD_MODE_SPEED...
  • Page 11: Macro Twim_Scr_Nak_Mask

    #define TWIM_LOW_POWER_ENABLE Enable TWIM Low Power Transfer by default. 4.3.9 Macro TWIM_SCR_NAK_MASK #define TWIM_SCR_NAK_MASK Mask to clear the DNAK and ANAK bits in the Status Clear register. 4.3.10 Macro TWIM_SR_NAK_MASK #define TWIM_SR_NAK_MASK Status Register mask to test for NAKs in data or address phase. 4.3.11 Macro TWIM_SR_STD_MASK #define TWIM_SR_STD_MASK...
  • Page 12: Function Twim_Clear_Status()

    status_code_t twi_master_write( Twim * twim, struct twim_package * package) Table 4-5. Parameters Data direction Parameter name Description [in] twim Base address of the TWIM [in] package Package information and data Table 4-6. Return Values Return value Description STATUS_OK If all bytes were send successfully ERR_IO_ERROR NACK received or Bus Arbitration lost 4.4.3...
  • Page 13: Function Twim_Disable_Interrupt()

    Twim * twim) Table 4-9. Parameters Data direction Parameter name Description [in] twim Base address of the TWIM instance 4.4.6 Function twim_disable_interrupt() Disable the TWIM interrupts and clear their status. void twim_disable_interrupt( Twim * twim, uint32_t interrupt_source) Write the value passed in interrupt_source into the Interrupt Disable Register. Each bit clears the corresponding bit in the Interrupt Mask Register and the Status Clear Register.
  • Page 14: Function Twim_Get_Interrupt_Mask()

    Data direction Parameter name Description [in] interrupt_source The TWI interrupt to be enabled 4.4.9 Function twim_get_interrupt_mask() Return the current content of the Interrupt Mask Register. uint32_t twim_get_interrupt_mask( Twim * twim) Table 4-13. Parameters Data direction Parameter name Description [in] twim Base address of the TWIM Returns TWIM interrupt mask.
  • Page 15: Function Twim_Probe()

    Data direction Parameter name Description [in] read True if it's a read trasnfer 4.4.12 Function twim_probe() Test if a device answers for a given TWI address. status_code_t twim_probe( Twim * twim, uint32_t chip_addr) Table 4-16. Parameters Data direction Parameter name Description [in] twim...
  • Page 16: Function Twim_Set_Hsmode_Speed()

    Table 4-19. Parameters Data direction Parameter name Description [in] twim Base address of the TWIM [in] config Options for initializing the TWIM module Table 4-20. Return Values Return value Description STATUS_OK Transaction is successful ERR_INVALID_ARG Invalid arg resulting in wrong CWGR Exponential 4.4.15 Function twim_set_hsmode_speed() Set the TWI bus speed in conjunction with the clock frequency in high speed mode.
  • Page 17: Enumeration Definitions

    Data direction Parameter name Description [in] The current running system clock frequency [in] cycles Clock cycles for data setup count Table 4-24. Return Values Return value Description STATUS_OK Transaction is successful ERR_INVALID_ARG Invalid arg resulting in wrong CWGR Exponential Enumeration Definitions 4.5.1 Enum twim_transfer_status Table 4-25.
  • Page 18: Special Considerations

    Special Considerations NONE. AT07334: SAM4 TWI Master Mode Driver [APPLICATION NOTE] 42274A-MCU-05/2014...
  • Page 19: Extra Information

    Extra Information Acronyms Term Definition Status Clear Register Negative Acknowledgement DNAK Negative Acknowledgement int TWI data phase ANAK Negative Acknowledgement int TWI address phase ARBLST Arbitration lost to a higher priority bus master in TWI multi-master mode Interupt Enable Register AT07334: SAM4 TWI Master Mode Driver [APPLICATION NOTE] 42274A-MCU-05/2014...
  • Page 20: Twim Master Example

    TWIM Master Example Purpose This is an example of how to use the TWIM driver to access an EEPROM. Requirements The program needs a TWI-compatible EEPROM connected as shown below: Connections for Board: SAM4L Xplained Pro Board EEPROM TWIMS3 TWD(PB14,EXT2/PIN11 TWIMS3 TWCK(PB15,EXT2/PIN12) Connections for Board: SAM4L-EK...
  • Page 21 -- TWIM Master Example -- -- xxxxxx-xx -- Compiled: xxx xx xxxx xx:xx:xx -- AT07334: SAM4 TWI Master Mode Driver [APPLICATION NOTE] 42274A-MCU-05/2014...
  • Page 22: Quick Start Guide

    Quick Start Guide This is the quick start guide for the SAM4 TWI Master Mode Driver, with step-by-step instructions on how to configure and use the driver for a specific use case.The code examples can be copied into the main application loop or any other function that will need to control the TWIM module.
  • Page 23: Usage Steps

    Note The read/write functions will enable and disable the corresponding interrupt sources. Usage Steps 8.4.1 twim_basic_usage_code We can send data to the target slave device. Firstly, the data package should be prepared. In one data package, several items should be set; the target slave address, the internal address (if needed), the length of the internal address (if needed), the data buffer to be written, and the length of the data buffer.
  • Page 24: Index

    Index Enumeration Definitions twim_transfer_status, Function Definitions twim_clear_status, twim_default_callback, twim_disable, twim_disable_interrupt, twim_enable, twim_enable_interrupt, twim_get_interrupt_mask, twim_get_status, twim_pdca_transfer_prepare, twim_probe, twim_set_callback, twim_set_config, twim_set_hsmode_speed, twim_set_speed, twi_master_read, twi_master_write, Macro Definitions TWIM_IER_NAK_MASK, TWIM_IER_STD_MASK, TWIM_LOW_POWER_ENABLE, TWIM_SCR_NAK_MASK, TWIM_SR_NAK_MASK, TWIM_SR_STD_MASK, TWI_FAST_MODE_PLUS_SPEED, TWI_FAST_MODE_SPEED, TWI_HIGH_SPEED_MODE_SPEED, twi_master_init, twi_options_t, twi_package_t, twi_probe, TWI_STD_MODE_SPEED, Structure Definitions twim_config, twim_package, Type Definitions...
  • Page 25: Document Revision History

    Document Revision History Doc. Rev. Date Comments 42274A 05/2014 Initial document release AT07334: SAM4 TWI Master Mode Driver [APPLICATION NOTE] 42274A-MCU-05/2014...
  • Page 26 Disclaimer: The information in this document is provided in connection with Atmel products. No license, express or implied, by estoppel or otherwise, to any intellectual property right is granted by this document or in connection with the sale of Atmel products. EXCEPT AS SET FORTH IN THE ATMEL TERMS AND CONDITIONS OF SALES LOCATED ON THE ATMEL WEBSITE, ATMEL ASSUMES NO LIABILITY WHATSOEVER AND DISCLAIMS ANY EXPRESS, IMPLIED OR STATUTORY WARRANTY RELATING TO ITS PRODUCTS INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.

Table of Contents