Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
sensirion/sensirion_i2c.c@0:c8ecd653066c, 2021-08-16 (annotated)
- Committer:
- Picard22
- Date:
- Mon Aug 16 13:00:07 2021 +0000
- Revision:
- 0:c8ecd653066c
Sensirion_Test
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Picard22 | 0:c8ecd653066c | 1 | /* |
| Picard22 | 0:c8ecd653066c | 2 | * Copyright (c) 2018, Sensirion AG |
| Picard22 | 0:c8ecd653066c | 3 | * All rights reserved. |
| Picard22 | 0:c8ecd653066c | 4 | * |
| Picard22 | 0:c8ecd653066c | 5 | * Redistribution and use in source and binary forms, with or without |
| Picard22 | 0:c8ecd653066c | 6 | * modification, are permitted provided that the following conditions are met: |
| Picard22 | 0:c8ecd653066c | 7 | * |
| Picard22 | 0:c8ecd653066c | 8 | * * Redistributions of source code must retain the above copyright notice, this |
| Picard22 | 0:c8ecd653066c | 9 | * list of conditions and the following disclaimer. |
| Picard22 | 0:c8ecd653066c | 10 | * |
| Picard22 | 0:c8ecd653066c | 11 | * * Redistributions in binary form must reproduce the above copyright notice, |
| Picard22 | 0:c8ecd653066c | 12 | * this list of conditions and the following disclaimer in the documentation |
| Picard22 | 0:c8ecd653066c | 13 | * and/or other materials provided with the distribution. |
| Picard22 | 0:c8ecd653066c | 14 | * |
| Picard22 | 0:c8ecd653066c | 15 | * * Neither the name of Sensirion AG nor the names of its |
| Picard22 | 0:c8ecd653066c | 16 | * contributors may be used to endorse or promote products derived from |
| Picard22 | 0:c8ecd653066c | 17 | * this software without specific prior written permission. |
| Picard22 | 0:c8ecd653066c | 18 | * |
| Picard22 | 0:c8ecd653066c | 19 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| Picard22 | 0:c8ecd653066c | 20 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| Picard22 | 0:c8ecd653066c | 21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| Picard22 | 0:c8ecd653066c | 22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| Picard22 | 0:c8ecd653066c | 23 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| Picard22 | 0:c8ecd653066c | 24 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| Picard22 | 0:c8ecd653066c | 25 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| Picard22 | 0:c8ecd653066c | 26 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| Picard22 | 0:c8ecd653066c | 27 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| Picard22 | 0:c8ecd653066c | 28 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| Picard22 | 0:c8ecd653066c | 29 | * POSSIBILITY OF SUCH DAMAGE. |
| Picard22 | 0:c8ecd653066c | 30 | */ |
| Picard22 | 0:c8ecd653066c | 31 | |
| Picard22 | 0:c8ecd653066c | 32 | #include "sensirion_i2c.h" |
| Picard22 | 0:c8ecd653066c | 33 | #include "sensirion_common.h" |
| Picard22 | 0:c8ecd653066c | 34 | #include "sensirion_config.h" |
| Picard22 | 0:c8ecd653066c | 35 | #include "sensirion_i2c_hal.h" |
| Picard22 | 0:c8ecd653066c | 36 | |
| Picard22 | 0:c8ecd653066c | 37 | uint8_t sensirion_i2c_generate_crc(const uint8_t* data, uint16_t count) { |
| Picard22 | 0:c8ecd653066c | 38 | uint16_t current_byte; |
| Picard22 | 0:c8ecd653066c | 39 | uint8_t crc = CRC8_INIT; |
| Picard22 | 0:c8ecd653066c | 40 | uint8_t crc_bit; |
| Picard22 | 0:c8ecd653066c | 41 | |
| Picard22 | 0:c8ecd653066c | 42 | /* calculates 8-Bit checksum with given polynomial */ |
| Picard22 | 0:c8ecd653066c | 43 | for (current_byte = 0; current_byte < count; ++current_byte) { |
| Picard22 | 0:c8ecd653066c | 44 | crc ^= (data[current_byte]); |
| Picard22 | 0:c8ecd653066c | 45 | for (crc_bit = 8; crc_bit > 0; --crc_bit) { |
| Picard22 | 0:c8ecd653066c | 46 | if (crc & 0x80) |
| Picard22 | 0:c8ecd653066c | 47 | crc = (crc << 1) ^ CRC8_POLYNOMIAL; |
| Picard22 | 0:c8ecd653066c | 48 | else |
| Picard22 | 0:c8ecd653066c | 49 | crc = (crc << 1); |
| Picard22 | 0:c8ecd653066c | 50 | } |
| Picard22 | 0:c8ecd653066c | 51 | } |
| Picard22 | 0:c8ecd653066c | 52 | return crc; |
| Picard22 | 0:c8ecd653066c | 53 | } |
| Picard22 | 0:c8ecd653066c | 54 | |
| Picard22 | 0:c8ecd653066c | 55 | int8_t sensirion_i2c_check_crc(const uint8_t* data, uint16_t count, |
| Picard22 | 0:c8ecd653066c | 56 | uint8_t checksum) { |
| Picard22 | 0:c8ecd653066c | 57 | if (sensirion_i2c_generate_crc(data, count) != checksum) |
| Picard22 | 0:c8ecd653066c | 58 | return CRC_ERROR; |
| Picard22 | 0:c8ecd653066c | 59 | return NO_ERROR; |
| Picard22 | 0:c8ecd653066c | 60 | } |
| Picard22 | 0:c8ecd653066c | 61 | |
| Picard22 | 0:c8ecd653066c | 62 | int16_t sensirion_i2c_general_call_reset(void) { |
| Picard22 | 0:c8ecd653066c | 63 | const uint8_t data = 0x06; |
| Picard22 | 0:c8ecd653066c | 64 | return sensirion_i2c_hal_write(0, &data, (uint16_t)sizeof(data)); |
| Picard22 | 0:c8ecd653066c | 65 | } |
| Picard22 | 0:c8ecd653066c | 66 | |
| Picard22 | 0:c8ecd653066c | 67 | uint16_t sensirion_i2c_fill_cmd_send_buf(uint8_t* buf, uint16_t cmd, |
| Picard22 | 0:c8ecd653066c | 68 | const uint16_t* args, |
| Picard22 | 0:c8ecd653066c | 69 | uint8_t num_args) { |
| Picard22 | 0:c8ecd653066c | 70 | uint8_t i; |
| Picard22 | 0:c8ecd653066c | 71 | uint16_t idx = 0; |
| Picard22 | 0:c8ecd653066c | 72 | |
| Picard22 | 0:c8ecd653066c | 73 | buf[idx++] = (uint8_t)((cmd & 0xFF00) >> 8); |
| Picard22 | 0:c8ecd653066c | 74 | buf[idx++] = (uint8_t)((cmd & 0x00FF) >> 0); |
| Picard22 | 0:c8ecd653066c | 75 | |
| Picard22 | 0:c8ecd653066c | 76 | for (i = 0; i < num_args; ++i) { |
| Picard22 | 0:c8ecd653066c | 77 | buf[idx++] = (uint8_t)((args[i] & 0xFF00) >> 8); |
| Picard22 | 0:c8ecd653066c | 78 | buf[idx++] = (uint8_t)((args[i] & 0x00FF) >> 0); |
| Picard22 | 0:c8ecd653066c | 79 | |
| Picard22 | 0:c8ecd653066c | 80 | uint8_t crc = sensirion_i2c_generate_crc((uint8_t*)&buf[idx - 2], |
| Picard22 | 0:c8ecd653066c | 81 | SENSIRION_WORD_SIZE); |
| Picard22 | 0:c8ecd653066c | 82 | buf[idx++] = crc; |
| Picard22 | 0:c8ecd653066c | 83 | } |
| Picard22 | 0:c8ecd653066c | 84 | return idx; |
| Picard22 | 0:c8ecd653066c | 85 | } |
| Picard22 | 0:c8ecd653066c | 86 | |
| Picard22 | 0:c8ecd653066c | 87 | int16_t sensirion_i2c_read_words_as_bytes(uint8_t address, uint8_t* data, |
| Picard22 | 0:c8ecd653066c | 88 | uint16_t num_words) { |
| Picard22 | 0:c8ecd653066c | 89 | int16_t ret; |
| Picard22 | 0:c8ecd653066c | 90 | uint16_t i, j; |
| Picard22 | 0:c8ecd653066c | 91 | uint16_t size = num_words * (SENSIRION_WORD_SIZE + CRC8_LEN); |
| Picard22 | 0:c8ecd653066c | 92 | uint16_t word_buf[SENSIRION_MAX_BUFFER_WORDS]; |
| Picard22 | 0:c8ecd653066c | 93 | uint8_t* const buf8 = (uint8_t*)word_buf; |
| Picard22 | 0:c8ecd653066c | 94 | |
| Picard22 | 0:c8ecd653066c | 95 | ret = sensirion_i2c_hal_read(address, buf8, size); |
| Picard22 | 0:c8ecd653066c | 96 | if (ret != NO_ERROR) |
| Picard22 | 0:c8ecd653066c | 97 | return ret; |
| Picard22 | 0:c8ecd653066c | 98 | |
| Picard22 | 0:c8ecd653066c | 99 | /* check the CRC for each word */ |
| Picard22 | 0:c8ecd653066c | 100 | for (i = 0, j = 0; i < size; i += SENSIRION_WORD_SIZE + CRC8_LEN) { |
| Picard22 | 0:c8ecd653066c | 101 | |
| Picard22 | 0:c8ecd653066c | 102 | ret = sensirion_i2c_check_crc(&buf8[i], SENSIRION_WORD_SIZE, |
| Picard22 | 0:c8ecd653066c | 103 | buf8[i + SENSIRION_WORD_SIZE]); |
| Picard22 | 0:c8ecd653066c | 104 | if (ret != NO_ERROR) |
| Picard22 | 0:c8ecd653066c | 105 | return ret; |
| Picard22 | 0:c8ecd653066c | 106 | |
| Picard22 | 0:c8ecd653066c | 107 | data[j++] = buf8[i]; |
| Picard22 | 0:c8ecd653066c | 108 | data[j++] = buf8[i + 1]; |
| Picard22 | 0:c8ecd653066c | 109 | } |
| Picard22 | 0:c8ecd653066c | 110 | |
| Picard22 | 0:c8ecd653066c | 111 | return NO_ERROR; |
| Picard22 | 0:c8ecd653066c | 112 | } |
| Picard22 | 0:c8ecd653066c | 113 | |
| Picard22 | 0:c8ecd653066c | 114 | int16_t sensirion_i2c_read_words(uint8_t address, uint16_t* data_words, |
| Picard22 | 0:c8ecd653066c | 115 | uint16_t num_words) { |
| Picard22 | 0:c8ecd653066c | 116 | int16_t ret; |
| Picard22 | 0:c8ecd653066c | 117 | uint8_t i; |
| Picard22 | 0:c8ecd653066c | 118 | |
| Picard22 | 0:c8ecd653066c | 119 | ret = sensirion_i2c_read_words_as_bytes(address, (uint8_t*)data_words, |
| Picard22 | 0:c8ecd653066c | 120 | num_words); |
| Picard22 | 0:c8ecd653066c | 121 | if (ret != NO_ERROR) |
| Picard22 | 0:c8ecd653066c | 122 | return ret; |
| Picard22 | 0:c8ecd653066c | 123 | |
| Picard22 | 0:c8ecd653066c | 124 | for (i = 0; i < num_words; ++i) { |
| Picard22 | 0:c8ecd653066c | 125 | const uint8_t* word_bytes = (uint8_t*)&data_words[i]; |
| Picard22 | 0:c8ecd653066c | 126 | data_words[i] = ((uint16_t)word_bytes[0] << 8) | word_bytes[1]; |
| Picard22 | 0:c8ecd653066c | 127 | } |
| Picard22 | 0:c8ecd653066c | 128 | |
| Picard22 | 0:c8ecd653066c | 129 | return NO_ERROR; |
| Picard22 | 0:c8ecd653066c | 130 | } |
| Picard22 | 0:c8ecd653066c | 131 | |
| Picard22 | 0:c8ecd653066c | 132 | int16_t sensirion_i2c_write_cmd(uint8_t address, uint16_t command) { |
| Picard22 | 0:c8ecd653066c | 133 | uint8_t buf[SENSIRION_COMMAND_SIZE]; |
| Picard22 | 0:c8ecd653066c | 134 | |
| Picard22 | 0:c8ecd653066c | 135 | sensirion_i2c_fill_cmd_send_buf(buf, command, NULL, 0); |
| Picard22 | 0:c8ecd653066c | 136 | return sensirion_i2c_hal_write(address, buf, SENSIRION_COMMAND_SIZE); |
| Picard22 | 0:c8ecd653066c | 137 | } |
| Picard22 | 0:c8ecd653066c | 138 | |
| Picard22 | 0:c8ecd653066c | 139 | int16_t sensirion_i2c_write_cmd_with_args(uint8_t address, uint16_t command, |
| Picard22 | 0:c8ecd653066c | 140 | const uint16_t* data_words, |
| Picard22 | 0:c8ecd653066c | 141 | uint16_t num_words) { |
| Picard22 | 0:c8ecd653066c | 142 | uint8_t buf[SENSIRION_MAX_BUFFER_WORDS]; |
| Picard22 | 0:c8ecd653066c | 143 | uint16_t buf_size; |
| Picard22 | 0:c8ecd653066c | 144 | |
| Picard22 | 0:c8ecd653066c | 145 | buf_size = |
| Picard22 | 0:c8ecd653066c | 146 | sensirion_i2c_fill_cmd_send_buf(buf, command, data_words, num_words); |
| Picard22 | 0:c8ecd653066c | 147 | return sensirion_i2c_hal_write(address, buf, buf_size); |
| Picard22 | 0:c8ecd653066c | 148 | } |
| Picard22 | 0:c8ecd653066c | 149 | |
| Picard22 | 0:c8ecd653066c | 150 | int16_t sensirion_i2c_delayed_read_cmd(uint8_t address, uint16_t cmd, |
| Picard22 | 0:c8ecd653066c | 151 | uint32_t delay_us, uint16_t* data_words, |
| Picard22 | 0:c8ecd653066c | 152 | uint16_t num_words) { |
| Picard22 | 0:c8ecd653066c | 153 | int16_t ret; |
| Picard22 | 0:c8ecd653066c | 154 | uint8_t buf[SENSIRION_COMMAND_SIZE]; |
| Picard22 | 0:c8ecd653066c | 155 | |
| Picard22 | 0:c8ecd653066c | 156 | sensirion_i2c_fill_cmd_send_buf(buf, cmd, NULL, 0); |
| Picard22 | 0:c8ecd653066c | 157 | ret = sensirion_i2c_hal_write(address, buf, SENSIRION_COMMAND_SIZE); |
| Picard22 | 0:c8ecd653066c | 158 | if (ret != NO_ERROR) |
| Picard22 | 0:c8ecd653066c | 159 | return ret; |
| Picard22 | 0:c8ecd653066c | 160 | |
| Picard22 | 0:c8ecd653066c | 161 | if (delay_us) |
| Picard22 | 0:c8ecd653066c | 162 | sensirion_i2c_hal_sleep_usec(delay_us); |
| Picard22 | 0:c8ecd653066c | 163 | |
| Picard22 | 0:c8ecd653066c | 164 | return sensirion_i2c_read_words(address, data_words, num_words); |
| Picard22 | 0:c8ecd653066c | 165 | } |
| Picard22 | 0:c8ecd653066c | 166 | |
| Picard22 | 0:c8ecd653066c | 167 | int16_t sensirion_i2c_read_cmd(uint8_t address, uint16_t cmd, |
| Picard22 | 0:c8ecd653066c | 168 | uint16_t* data_words, uint16_t num_words) { |
| Picard22 | 0:c8ecd653066c | 169 | return sensirion_i2c_delayed_read_cmd(address, cmd, 0, data_words, |
| Picard22 | 0:c8ecd653066c | 170 | num_words); |
| Picard22 | 0:c8ecd653066c | 171 | } |
| Picard22 | 0:c8ecd653066c | 172 | |
| Picard22 | 0:c8ecd653066c | 173 | uint16_t sensirion_i2c_add_command_to_buffer(uint8_t* buffer, uint16_t offset, |
| Picard22 | 0:c8ecd653066c | 174 | uint16_t command) { |
| Picard22 | 0:c8ecd653066c | 175 | buffer[offset++] = (uint8_t)((command & 0xFF00) >> 8); |
| Picard22 | 0:c8ecd653066c | 176 | buffer[offset++] = (uint8_t)((command & 0x00FF) >> 0); |
| Picard22 | 0:c8ecd653066c | 177 | return offset; |
| Picard22 | 0:c8ecd653066c | 178 | } |
| Picard22 | 0:c8ecd653066c | 179 | |
| Picard22 | 0:c8ecd653066c | 180 | uint16_t sensirion_i2c_add_uint32_t_to_buffer(uint8_t* buffer, uint16_t offset, |
| Picard22 | 0:c8ecd653066c | 181 | uint32_t data) { |
| Picard22 | 0:c8ecd653066c | 182 | buffer[offset++] = (uint8_t)((data & 0xFF000000) >> 24); |
| Picard22 | 0:c8ecd653066c | 183 | buffer[offset++] = (uint8_t)((data & 0x00FF0000) >> 16); |
| Picard22 | 0:c8ecd653066c | 184 | buffer[offset] = sensirion_i2c_generate_crc( |
| Picard22 | 0:c8ecd653066c | 185 | &buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE); |
| Picard22 | 0:c8ecd653066c | 186 | offset++; |
| Picard22 | 0:c8ecd653066c | 187 | buffer[offset++] = (uint8_t)((data & 0x0000FF00) >> 8); |
| Picard22 | 0:c8ecd653066c | 188 | buffer[offset++] = (uint8_t)((data & 0x000000FF) >> 0); |
| Picard22 | 0:c8ecd653066c | 189 | buffer[offset] = sensirion_i2c_generate_crc( |
| Picard22 | 0:c8ecd653066c | 190 | &buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE); |
| Picard22 | 0:c8ecd653066c | 191 | offset++; |
| Picard22 | 0:c8ecd653066c | 192 | |
| Picard22 | 0:c8ecd653066c | 193 | return offset; |
| Picard22 | 0:c8ecd653066c | 194 | } |
| Picard22 | 0:c8ecd653066c | 195 | |
| Picard22 | 0:c8ecd653066c | 196 | uint16_t sensirion_i2c_add_int32_t_to_buffer(uint8_t* buffer, uint16_t offset, |
| Picard22 | 0:c8ecd653066c | 197 | int32_t data) { |
| Picard22 | 0:c8ecd653066c | 198 | return sensirion_i2c_add_uint32_t_to_buffer(buffer, offset, (uint32_t)data); |
| Picard22 | 0:c8ecd653066c | 199 | } |
| Picard22 | 0:c8ecd653066c | 200 | |
| Picard22 | 0:c8ecd653066c | 201 | uint16_t sensirion_i2c_add_uint16_t_to_buffer(uint8_t* buffer, uint16_t offset, |
| Picard22 | 0:c8ecd653066c | 202 | uint16_t data) { |
| Picard22 | 0:c8ecd653066c | 203 | buffer[offset++] = (uint8_t)((data & 0xFF00) >> 8); |
| Picard22 | 0:c8ecd653066c | 204 | buffer[offset++] = (uint8_t)((data & 0x00FF) >> 0); |
| Picard22 | 0:c8ecd653066c | 205 | buffer[offset] = sensirion_i2c_generate_crc( |
| Picard22 | 0:c8ecd653066c | 206 | &buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE); |
| Picard22 | 0:c8ecd653066c | 207 | offset++; |
| Picard22 | 0:c8ecd653066c | 208 | |
| Picard22 | 0:c8ecd653066c | 209 | return offset; |
| Picard22 | 0:c8ecd653066c | 210 | } |
| Picard22 | 0:c8ecd653066c | 211 | |
| Picard22 | 0:c8ecd653066c | 212 | uint16_t sensirion_i2c_add_int16_t_to_buffer(uint8_t* buffer, uint16_t offset, |
| Picard22 | 0:c8ecd653066c | 213 | int16_t data) { |
| Picard22 | 0:c8ecd653066c | 214 | return sensirion_i2c_add_uint16_t_to_buffer(buffer, offset, (uint16_t)data); |
| Picard22 | 0:c8ecd653066c | 215 | } |
| Picard22 | 0:c8ecd653066c | 216 | |
| Picard22 | 0:c8ecd653066c | 217 | uint16_t sensirion_i2c_add_float_to_buffer(uint8_t* buffer, uint16_t offset, |
| Picard22 | 0:c8ecd653066c | 218 | float data) { |
| Picard22 | 0:c8ecd653066c | 219 | union { |
| Picard22 | 0:c8ecd653066c | 220 | uint32_t uint32_data; |
| Picard22 | 0:c8ecd653066c | 221 | float float_data; |
| Picard22 | 0:c8ecd653066c | 222 | } convert; |
| Picard22 | 0:c8ecd653066c | 223 | |
| Picard22 | 0:c8ecd653066c | 224 | convert.float_data = data; |
| Picard22 | 0:c8ecd653066c | 225 | |
| Picard22 | 0:c8ecd653066c | 226 | buffer[offset++] = (uint8_t)((convert.uint32_data & 0xFF000000) >> 24); |
| Picard22 | 0:c8ecd653066c | 227 | buffer[offset++] = (uint8_t)((convert.uint32_data & 0x00FF0000) >> 16); |
| Picard22 | 0:c8ecd653066c | 228 | buffer[offset] = sensirion_i2c_generate_crc( |
| Picard22 | 0:c8ecd653066c | 229 | &buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE); |
| Picard22 | 0:c8ecd653066c | 230 | offset++; |
| Picard22 | 0:c8ecd653066c | 231 | buffer[offset++] = (uint8_t)((convert.uint32_data & 0x0000FF00) >> 8); |
| Picard22 | 0:c8ecd653066c | 232 | buffer[offset++] = (uint8_t)((convert.uint32_data & 0x000000FF) >> 0); |
| Picard22 | 0:c8ecd653066c | 233 | buffer[offset] = sensirion_i2c_generate_crc( |
| Picard22 | 0:c8ecd653066c | 234 | &buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE); |
| Picard22 | 0:c8ecd653066c | 235 | offset++; |
| Picard22 | 0:c8ecd653066c | 236 | |
| Picard22 | 0:c8ecd653066c | 237 | return offset; |
| Picard22 | 0:c8ecd653066c | 238 | } |
| Picard22 | 0:c8ecd653066c | 239 | |
| Picard22 | 0:c8ecd653066c | 240 | uint16_t sensirion_i2c_add_bytes_to_buffer(uint8_t* buffer, uint16_t offset, |
| Picard22 | 0:c8ecd653066c | 241 | uint8_t* data, |
| Picard22 | 0:c8ecd653066c | 242 | uint16_t data_length) { |
| Picard22 | 0:c8ecd653066c | 243 | uint16_t i; |
| Picard22 | 0:c8ecd653066c | 244 | |
| Picard22 | 0:c8ecd653066c | 245 | if (data_length % SENSIRION_WORD_SIZE != 0) { |
| Picard22 | 0:c8ecd653066c | 246 | return BYTE_NUM_ERROR; |
| Picard22 | 0:c8ecd653066c | 247 | } |
| Picard22 | 0:c8ecd653066c | 248 | |
| Picard22 | 0:c8ecd653066c | 249 | for (i = 0; i < data_length; i += 2) { |
| Picard22 | 0:c8ecd653066c | 250 | buffer[offset++] = data[i]; |
| Picard22 | 0:c8ecd653066c | 251 | buffer[offset++] = data[i + 1]; |
| Picard22 | 0:c8ecd653066c | 252 | |
| Picard22 | 0:c8ecd653066c | 253 | buffer[offset] = sensirion_i2c_generate_crc( |
| Picard22 | 0:c8ecd653066c | 254 | &buffer[offset - SENSIRION_WORD_SIZE], SENSIRION_WORD_SIZE); |
| Picard22 | 0:c8ecd653066c | 255 | offset++; |
| Picard22 | 0:c8ecd653066c | 256 | } |
| Picard22 | 0:c8ecd653066c | 257 | |
| Picard22 | 0:c8ecd653066c | 258 | return offset; |
| Picard22 | 0:c8ecd653066c | 259 | } |
| Picard22 | 0:c8ecd653066c | 260 | |
| Picard22 | 0:c8ecd653066c | 261 | int16_t sensirion_i2c_write_data(uint8_t address, const uint8_t* data, |
| Picard22 | 0:c8ecd653066c | 262 | uint16_t data_length) { |
| Picard22 | 0:c8ecd653066c | 263 | return sensirion_i2c_hal_write(address, data, data_length); |
| Picard22 | 0:c8ecd653066c | 264 | } |
| Picard22 | 0:c8ecd653066c | 265 | |
| Picard22 | 0:c8ecd653066c | 266 | int16_t sensirion_i2c_read_data_inplace(uint8_t address, uint8_t* buffer, |
| Picard22 | 0:c8ecd653066c | 267 | uint16_t expected_data_length) { |
| Picard22 | 0:c8ecd653066c | 268 | int16_t error; |
| Picard22 | 0:c8ecd653066c | 269 | uint16_t i, j; |
| Picard22 | 0:c8ecd653066c | 270 | uint16_t size = (expected_data_length / SENSIRION_WORD_SIZE) * |
| Picard22 | 0:c8ecd653066c | 271 | (SENSIRION_WORD_SIZE + CRC8_LEN); |
| Picard22 | 0:c8ecd653066c | 272 | |
| Picard22 | 0:c8ecd653066c | 273 | if (expected_data_length % SENSIRION_WORD_SIZE != 0) { |
| Picard22 | 0:c8ecd653066c | 274 | return BYTE_NUM_ERROR; |
| Picard22 | 0:c8ecd653066c | 275 | } |
| Picard22 | 0:c8ecd653066c | 276 | |
| Picard22 | 0:c8ecd653066c | 277 | error = sensirion_i2c_hal_read(address, buffer, size); |
| Picard22 | 0:c8ecd653066c | 278 | if (error) { |
| Picard22 | 0:c8ecd653066c | 279 | return error; |
| Picard22 | 0:c8ecd653066c | 280 | } |
| Picard22 | 0:c8ecd653066c | 281 | |
| Picard22 | 0:c8ecd653066c | 282 | for (i = 0, j = 0; i < size; i += SENSIRION_WORD_SIZE + CRC8_LEN) { |
| Picard22 | 0:c8ecd653066c | 283 | |
| Picard22 | 0:c8ecd653066c | 284 | error = sensirion_i2c_check_crc(&buffer[i], SENSIRION_WORD_SIZE, |
| Picard22 | 0:c8ecd653066c | 285 | buffer[i + SENSIRION_WORD_SIZE]); |
| Picard22 | 0:c8ecd653066c | 286 | if (error) { |
| Picard22 | 0:c8ecd653066c | 287 | return error; |
| Picard22 | 0:c8ecd653066c | 288 | } |
| Picard22 | 0:c8ecd653066c | 289 | buffer[j++] = buffer[i]; |
| Picard22 | 0:c8ecd653066c | 290 | buffer[j++] = buffer[i + 1]; |
| Picard22 | 0:c8ecd653066c | 291 | } |
| Picard22 | 0:c8ecd653066c | 292 | |
| Picard22 | 0:c8ecd653066c | 293 | return NO_ERROR; |
| Picard22 | 0:c8ecd653066c | 294 | } |