PX4 Firmware
PX4 Autopilot Software http://px4.io
SMBus.hpp
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright (c) 2018 PX4 Development Team. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
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
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * 3. Neither the name PX4 nor the names of its contributors may be
16  * used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
26  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  *
32  ****************************************************************************/
33 
34 /**
35  * @file SMBus.hpp
36  * SMBus v2.0 protocol implementation.
37  *
38  * @author Jacob Dahl <dahl.jakejacob@gmail.com>
39  */
40 
41 #include <drivers/device/i2c.h>
42 
43 #include <string.h>
44 
45 #define SMBUS_PEC_POLYNOMIAL 0x07 ///< Polynomial for calculating PEC
46 
47 class SMBus : public device::I2C
48 {
49 public:
50  SMBus(int bus_num, uint16_t address);
51  ~SMBus() override = default;
52 
53  /**
54  * @brief Sends a block write command.
55  * @param cmd_code The command code.
56  * @param data The data to be written.
57  * @param length The number of bytes being written.
58  * @return Returns PX4_OK on success, -errno on failure.
59  */
60  int block_write(const uint8_t cmd_code, void *data, uint8_t byte_count, bool use_pec);
61 
62  /**
63  * @brief Sends a block read command.
64  * @param cmd_code The command code.
65  * @param data The returned data.
66  * @param length The number of bytes being read
67  * @return Returns PX4_OK on success, -errno on failure.
68  */
69  int block_read(const uint8_t cmd_code, void *data, const uint8_t length, bool use_pec);
70 
71  /**
72  * @brief Sends a read word command.
73  * @param cmd_code The command code.
74  * @param data The 2 bytes of returned data plus a 1 byte CRC if used.
75  * @return Returns PX4_OK on success, -errno on failure.
76  */
77  int read_word(const uint8_t cmd_code, uint16_t &data);
78 
79  /**
80  * @brief Sends a write word command.
81  * @param cmd_code The command code.
82  * @param data The 2 bytes of data to be transfered.
83  * @return Returns PX4_OK on success, -errno on failure.
84  */
85  int write_word(const uint8_t cmd_code, uint16_t data);
86 
87  /**
88  * @brief Calculates the PEC from the data.
89  * @param buffer The buffer that stores the data to perform the CRC over.
90  * @param length The number of bytes being written.
91  * @return Returns PX4_OK on success, -errno on failure.
92  */
93  uint8_t get_pec(uint8_t *buffer, uint8_t length);
94 
95 };
int block_read(const uint8_t cmd_code, void *data, const uint8_t length, bool use_pec)
Sends a block read command.
Definition: SMBus.cpp:93
SMBus(int bus_num, uint16_t address)
Definition: SMBus.cpp:48
int block_write(const uint8_t cmd_code, void *data, uint8_t byte_count, bool use_pec)
Sends a block write command.
Definition: SMBus.cpp:120
int read_word(const uint8_t cmd_code, uint16_t &data)
Sends a read word command.
Definition: SMBus.cpp:53
int write_word(const uint8_t cmd_code, uint16_t data)
Sends a write word command.
Definition: SMBus.cpp:77
uint8_t * data
Definition: dataman.cpp:149
uint8_t get_pec(uint8_t *buffer, uint8_t length)
Calculates the PEC from the data.
Definition: SMBus.cpp:151
~SMBus() override=default
Definition: SMBus.hpp:47
Base class for devices connected via I2C.