Contiki-NG
i2c-arch.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
3 * Copyright (c) 2020, George Oikonomou - http://www.spd.gr
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the copyright holder nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
29 * OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31/*---------------------------------------------------------------------------*/
32/**
33 * \addtogroup cc13xx-cc26xx-cpu
34 * @{
35 *
36 * \defgroup cc13xx-cc26xx-i2c CC13xx/CC26xx I2C HAL
37 * @{
38 *
39 * \file
40 * Implementation of the I2C HAL driver for CC13xx/CC26xx.
41 *
42 * \author
43 * Edvard Pettersen <e.pettersen@ti.com>
44 * \author
45 * George Oikonomou <george@contiki-ng.org>
46 */
47/*---------------------------------------------------------------------------*/
48#ifndef I2C_ARCH_H_
49#define I2C_ARCH_H_
50/*---------------------------------------------------------------------------*/
51#include "contiki.h"
52#include "board-conf.h"
53/*---------------------------------------------------------------------------*/
54#include <Board.h>
55
56#include <ti/devices/DeviceFamily.h>
57#include DeviceFamily_constructPath(driverlib/cpu.h)
58
59#include <ti/drivers/I2C.h>
60
61#include <stddef.h>
62#include <stdbool.h>
63#include <stdint.h>
64/*---------------------------------------------------------------------------*/
65/**
66 * \brief One-time initialisation of the I2C Driver
67 *
68 * This function must be called before any other I2C driver calls.
69 */
70static inline void
72{
73 I2C_init();
74}
75
76/**
77 * \brief Open and lock the I2C Peripheral for use
78 * \param index The index of the I2C controller
79 * \return An I2C Handle if successful, or NULL if an error occurs
80 *
81 * Must be called before each I2C transaction.
82 *
83 * When the function returns successfully, i2c_handle will be non-NULL and can
84 * be used in subsequent calls to perform an I2C transaction, for example with
85 * i2c_arch_write_read().
86 *
87 * index can take values among Board_I2Cx e.g. Board_I2C0
88 *
89 * At the end of the transaction, the caller should call i2c_arch_release() in
90 * order to allow other code files to use the I2C module
91 */
92I2C_Handle i2c_arch_acquire(uint_least8_t index);
93
94/**
95 * \brief Release the I2C Peripheral for other modules to use
96 * \param i2c_handle A pointer to an I2C handle
97 *
98 * Must be called after the end of each I2C transaction in order to allow
99 * other modules to use the I2C controller. The i2c_handle is obtained by
100 * an earlier call to i2c_arch_acquire()
101 */
102void i2c_arch_release(I2C_Handle i2c_handle);
103
104/**
105 * \brief Setup and peform an I2C transaction.
106 * \param i2c_handle The I2C handle to use for this transaction
107 * \param slave_addr The address of the slave device on the I2C bus
108 * \param wbuf Write buffer during the I2C transation.
109 * \param wcount How many bytes in the write buffer
110 * \param rbuf Input buffer during the I2C transation.
111 * \param rcount How many bytes to read into rbuf.
112 * \retval true The I2C operation was successful
113 * \retval false The I2C operation failed
114 */
115bool i2c_arch_write_read(I2C_Handle i2c_handle, uint_least8_t slave_addr,
116 void *wbuf, size_t wcount, void *rbuf, size_t rcount);
117
118/**
119 * \brief Perform a write-only I2C transaction.
120 * \param i2c_handle The I2C handle to use for this transaction
121 * \param slave_addr The address of the slave device on the I2C bus
122 * \param wbuf Write buffer during the I2C transaction.
123 * \param wcount How many bytes in the write buffer
124 * \retval true The I2C operation was successful
125 * \retval false The I2C operation failed
126 */
127static inline bool
128i2c_arch_write(I2C_Handle i2c_handle, uint_least8_t slave_addr,
129 void *wbuf, size_t wcount)
130{
131 return i2c_arch_write_read(i2c_handle, slave_addr, wbuf, wcount, NULL, 0);
132}
133
134/**
135 * \brief Perform a read-only I2C transaction.
136 * \param i2c_handle The I2C handle to use for this transaction
137 * \param slave_addr The address of the slave device on the I2C bus
138 * \param rbuf Input buffer during the I2C transaction.
139 * \param rcount How many bytes to read into rbuf.
140 * \retval true The I2C operation was successful
141 * \retval false The I2C operation failed
142 */
143static inline bool
144i2c_arch_read(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *rbuf,
145 size_t rcount)
146{
147 return i2c_arch_write_read(i2c_handle, slave_addr, NULL, 0, rbuf, rcount);
148}
149/*---------------------------------------------------------------------------*/
150#endif /* I2C_ARCH_H_ */
151/*---------------------------------------------------------------------------*/
152/**
153 * @}
154 * @}
155 */
static bool i2c_arch_read(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *rbuf, size_t rcount)
Perform a read-only I2C transaction.
Definition: i2c-arch.h:144
static void i2c_arch_init(void)
One-time initialisation of the I2C Driver.
Definition: i2c-arch.h:71
bool i2c_arch_write_read(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *wbuf, size_t wcount, void *rbuf, size_t rcount)
Setup and peform an I2C transaction.
Definition: i2c-arch.c:53
static bool i2c_arch_write(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *wbuf, size_t wcount)
Perform a write-only I2C transaction.
Definition: i2c-arch.h:128
I2C_Handle i2c_arch_acquire(uint_least8_t index)
Open and lock the I2C Peripheral for use.
Definition: i2c-arch.c:84
void i2c_arch_release(I2C_Handle i2c_handle)
Release the I2C Peripheral for other modules to use.
Definition: i2c-arch.c:74