d_i2c.c
Go to the documentation of this file.
1 /*
2 MIT LICENSE
3 
4 Copyright 2014-2019 Inertial Sense, Inc. - http://inertialsense.com
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
7 
8 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
9 
10 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT, IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 
14 #include <asf.h>
15 #include <string.h>
16 #include <stdio.h>
17 
18 #include "d_i2c.h"
19 
20 //Configuration for TWI instance
21 #define BOARD_ID_TWI ID_TWIHS0
22 #define BOARD_TWI TWIHS0
23 #define TWI_CLK 400000
24 
25 
26 int i2cInit( void )
27 {
28  //Configure pins
31 
32  /* Enable the peripheral clock for TWI */
34 
35  /* Configure the options of TWI driver */
36  twihs_options_t opt;
38  opt.speed = TWI_CLK;
39 
41  return -1;
42 
43  return 0;
44 }
45 
46 int i2cRead(uint8_t chip_addr, uint32_t address, uint8_t address_len, void *data, uint32_t len)
47 {
48  twihs_packet_t packet_rx;
49 
50  //Configure the data packet
51  packet_rx.chip = chip_addr;
52  packet_rx.addr_length = address_len;
53  if(address_len == 3)
54  {
55  packet_rx.addr[0] = address >> 16;
56  packet_rx.addr[1] = address >> 8;
57  packet_rx.addr[2] = address;
58  }
59  else if(address_len == 2)
60  {
61  packet_rx.addr[0] = address >> 8;
62  packet_rx.addr[1] = address;
63  }
64  else if(address_len == 1)
65  {
66  packet_rx.addr[0] = address;
67  }
68  else
69  {
70  return 0;
71  }
72 
73  packet_rx.buffer = data;
74  packet_rx.length = len;
75 
76  //Read Data
77  if (twihs_master_read(BOARD_TWI, &packet_rx) != TWIHS_SUCCESS)
78  return 0;
79 
80  return len;
81 }
82 
83 int i2cWrite(uint8_t chip_addr, uint32_t address, uint8_t address_len, void *data, uint32_t len)
84 {
85  twihs_packet_t packet_tx;
86 
87  //Configure the data packet
88  packet_tx.chip = chip_addr;
89  packet_tx.addr_length = address_len;
90  if(address_len == 3)
91  {
92  packet_tx.addr[0] = address >> 16;
93  packet_tx.addr[1] = address >> 8;
94  packet_tx.addr[2] = address;
95  }
96  else if(address_len == 2)
97  {
98  packet_tx.addr[0] = address >> 8;
99  packet_tx.addr[1] = address;
100  }
101  else if(address_len == 1)
102  {
103  packet_tx.addr[0] = address;
104  }
105  else
106  {
107  return 0;
108  }
109  packet_tx.buffer = data;
110  packet_tx.length = len;
111 
112  //Write data
113  if (twihs_master_write(BOARD_TWI, &packet_tx) != TWIHS_SUCCESS)
114  return 0;
115 
116  return len;
117 }
void * buffer
Where to find the data to be transferred.
int i2cWrite(uint8_t chip_addr, uint32_t address, uint8_t address_len, void *data, uint32_t len)
Definition: d_i2c.c:83
Information concerning the data transmission.
uint32_t master_clk
MCK for TWIHS.
uint32_t addr_length
Length of the TWIHS data address segment (1-3 bytes).
int i2cInit(void)
Definition: d_i2c.c:26
#define BOARD_TWI
Definition: d_i2c.c:22
#define I2C_0_SCL_PIN
Definition: user_board.h:203
uint32_t pmc_enable_periph_clk(uint32_t ul_id)
Enable the specified peripheral clock.
Definition: pmc.c:682
#define ioport_set_pin_peripheral_mode(pin, mode)
Definition: user_board.h:312
uint32_t twihs_master_init(Twihs *p_twihs, const twihs_options_t *p_opt)
Initialize TWIHS master mode.
Definition: twihs.c:113
static uint32_t sysclk_get_peripheral_hz(void)
Retrieves the current rate in Hz of the peripheral clocks.
int i2cRead(uint8_t chip_addr, uint32_t address, uint8_t address_len, void *data, uint32_t len)
Definition: d_i2c.c:46
#define I2C_0_SDA_FLAGS
Definition: user_board.h:202
#define TWI_CLK
Definition: d_i2c.c:23
USBInterfaceDescriptor data
uint32_t length
How many bytes do we want to transfer.
#define BOARD_ID_TWI
Definition: d_i2c.c:21
uint32_t twihs_master_read(Twihs *p_twihs, twihs_packet_t *p_packet)
Read multiple bytes from a TWIHS compatible slave device.
Definition: twihs.c:270
#define I2C_0_SCL_FLAGS
Definition: user_board.h:204
uint8_t chip
TWIHS chip address to communicate with.
#define I2C_0_SDA_PIN
Definition: user_board.h:201
uint32_t speed
The baud rate of the TWIHS bus.
Autogenerated API include file for the Atmel Software Framework (ASF)
Input parameters when initializing the TWIHS module mode.
uint32_t twihs_master_write(Twihs *p_twihs, twihs_packet_t *p_packet)
Write multiple bytes to a TWIHS compatible slave device.
Definition: twihs.c:334
#define TWIHS_SUCCESS
Return codes for TWIHS APIs.
uint8_t addr[3]
TWIHS address/commands to issue to the other chip (node).


inertial_sense_ros
Author(s):
autogenerated on Sun Feb 28 2021 03:17:57