summaryrefslogtreecommitdiff
path: root/bsp/samd21/sam_d2x_asflib/common/services/twi/twihs_master.h
blob: a620f3db20cdd6afbd4707df98088abcfc41feb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/**
 * \file
 *
 * \brief TWIHS Master Mode management
 *
 * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 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.
 *
 * 4. This software may only be redistributed and used in connection with an
 *    Atmel microcontroller product.
 *
 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
 */
#ifndef TWIHS_MASTER_H_INCLUDED
#define TWIHS_MASTER_H_INCLUDED

#include <parts.h>
#include <compiler.h>

#if (SAMG || SAMV70 || SAMV71 || SAME70 || SAMS70)
# include "sam_twihs/twihs_master.h"
#else
# error Unsupported chip type
#endif

/**
 *
 * \defgroup twihs_group Two Wire-interface High Speed(TWIHS)
 *
 * This is the common API for TWIHS. Additional features are available
 * in the documentation of the specific modules.
 *
 * See \ref twihs_quickstart.
 *
 * \section twihs_group_platform Platform Dependencies
 *
 * The TWIHS API is partially chip- or platform-specific. While all
 * platforms provide mostly the same functionality, there are some
 * variations around how different bus types and clock tree structures
 * are handled.
 *
 * The following functions are available on all platforms, but there may
 * be variations in the function signature (i.e. parameters) and
 * behaviour. These functions are typically called by platform-specific
 * parts of drivers, and applications that aren't intended to be
 * portable:
 *   - Master TWIHS Module initialization
 *   \code status_code_t twihs_master_setup(*twihs_module_pointer, twihs_master_options_t *opt) \endcode
 *   - Enables TWIHS Module
 *   \code void twihs_master_enable(*twihs_module_pointer) \endcode
 *   - Disables TWIHS Module
 *   \code void twihs_master_disable(*twihs_module_pointer) \endcode
 *   - Read data from a slave device
 *   \code status_code_t twihs_master_read(*twihs_module_pointer, twihs_package_t *package) \endcode
 *   - Write data from to a slave device
 *   \code status_code_t twihs_master_write(*twihs_module_pointer, twihs_package_t *package) \endcode
 *
 * @{
 */

/**
 * \typedef twihs_master_t
 * This type can be used independently to refer to TWIHS master module for the
 * architecture used. It refers to the correct type definition for the
 * architecture.
 */

//! @}

/**
 * \page twihs_quickstart Quickstart guide for Common service TWIHS
 *
 * This is the quickstart guide for the \ref twihs_group "Common service TWIHS",
 * with step-by-step instructions on how to configure and use the driver in a
 * selection of use cases.
 *
 * The use cases contain several code fragments. The code fragments in the
 * steps for setup can be copied into a custom initialization function, while
 * the steps for usage can be copied into, e.g., the main application function.
 *
 * \section twihs_basic_use_case Basic use case
 * In the most basic use case, the TWIHS module is configured for
 * - Master operation
 * - addressing one slave device of the bus at address 0x50
 * - TWIHS clock of 400kHz
 * - polled read/write handling
 *
 * \section twihs_basic_use_case_setup Setup steps
 * \subsection twihs_basic_use_case_setup_code Example code
 * Add to your application C-file, for example in SAM series chip:
 * \code
	  void twihs_init(void)
	  {
	    twihs_master_options_t opt = {
	        .speed = 400000,
	        .chip  = 0x50
	    };

	    twihs_master_setup(&TWIM0, &opt);
	  }
\endcode
 *
 * \subsection twihs_basic_use_case_setup_flow Workflow
 * -# Ensure that board_init() has configured selected I/Os for TWIHS function.
 * -# Ensure that \ref conf_twim.h is present for the driver.
 *   - \note This file is only for the driver and should not be included by the
 * user.
 * -# Define and initialize config structs for TWIHS module in your TWIHS initialization
 * function, for example in SAM series chip:
 *   - \code
	twihs_master_options_t opt = {
	  .speed = 400000,
	  .chip  = 0x50
	}; \endcode
 *   - field \ref speed sets the baudrate of the TWIHS bus
 *   - field \ref chip sets the address of the slave device you want to communicate with
 * -# Call twihs_master_setup and optionally check its return code
 *   - \note The config structs can be reused for other TWIHS modules
 * after this step. Simply reconfigure and write to others modules.
 *
 * \section twihs_basic_use_case_usage Usage steps
 * \subsection twihs_basic_use_case_usage_code_writing Example code : Writing to a slave device
 * Use in application C-file, for example in SAM series chip:
 * \code
	  const uint8_t test_pattern[] = {0x55,0xA5,0x5A,0x77,0x99};

	  twihs_package_t packet_write = {
	    .addr         = EEPROM_MEM_ADDR,      // TWIHS slave memory address data
	    .addr_length  = sizeof (uint16_t),    // TWIHS slave memory address data size
	    .chip         = EEPROM_BUS_ADDR,      // TWIHS slave bus address
	    .buffer       = (void *)test_pattern, // transfer data source buffer
	    .length       = sizeof(test_pattern)  // transfer data size (bytes)
	  };

	  while (twihs_master_write(&TWIM0, &packet_write) != TWI_SUCCESS);
\endcode
 *
 * \subsection twihs_basic_use_case_usage_flow Workflow
 * -# Prepare the data you want to send to the slave device:
 *   - \code const uint8_t test_pattern[] = {0x55,0xA5,0x5A,0x77,0x99}; \endcode
 * -# Prepare a twihs_package_t structure
 *   \code twihs_package_t packet_write; \endcode
 * Fill all the fields of the structure :
 *  - addr is the address in the slave device
 *  - addr_length is the size of the address in the slave (support for large TWI memory devices)
 *  - chip sets the 7 bit address of the slave device you want to communicate with
 *  - buffer is a pointer on the data to write to slave
 *  - length is the number of data to write
 *
 * -# Finally, call twihs_master_write \code twihs_master_write(&TWIM0, &packet_write); \endcode
 * and optionally check its return value for TWI_SUCCESS.
 * \subsection twihs_basic_use_case_usage_code_reading Example code : Reading from a slave device
 * Use in application C-file, for example in SAM series chip:
 * \code
	   uint8_t data_received[10];

	   twihs_package_t packet_read = {
	     .addr         = EEPROM_MEM_ADDR,      // TWIHS slave memory address data
	     .addr_length  = sizeof (uint16_t),    // TWIHS slave memory address data size
	     .chip         = EEPROM_BUS_ADDR,      // TWIHS slave bus address
	     .buffer       = data_received,        // transfer data destination buffer
	     .length       = 10                    // transfer data size (bytes)
	   };
	   // Perform a multi-byte read access then check the result.
	   if(twihs_master_read(&TWIM0, &packet_read) == TWI_SUCCESS){
	     //Check read content
	     if(data_received[0]==0x55)
	       do_something();
	   }
\endcode
 *
 * \subsection twihs_basic_use_case_usage_flow Workflow
 * -# Prepare a data buffer that will receive the data from the slave device:
 *   \code uint8_t data_received[10]; \endcode
 * -# Prepare a twihs_package_t structure
 *   \code twihs_package_t packet_read; \endcode
 * Fill all the fields of the structure :
 *  - addr is the address in the slave device
 *  - addr_length is the size of the address in the slave (support for large TWI memory devices)
 *  - chip sets the 7 bit address of the slave device you want to communicate with
 *  - buffer is a pointer on the data buffer that will receive the data from the slave device
 *  - length is the number of data to read
 *
 * -# Finally, call twihs_master_read \code twihs_master_read(&TWIM0, &packet_read); \endcode
 * and optionally check its return value for TWI_SUCCESS.
 * the data read from the device are now in data_received.
 */

#endif /* TWI_MASTER_H_INCLUDED */