|
| 1 | +#include <hardware/i2c.h> |
| 2 | +/* |
| 3 | + * Copyright (c) 2018, Sensirion AG |
| 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 are met: |
| 8 | + * |
| 9 | + * * Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * |
| 16 | + * * Neither the name of Sensirion AG nor the names of its |
| 17 | + * contributors may be used to endorse or promote products derived from |
| 18 | + * this software without specific prior written permission. |
| 19 | + * |
| 20 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 21 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 23 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 24 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | + * POSSIBILITY OF SUCH DAMAGE. |
| 31 | + */ |
| 32 | + |
| 33 | +#include "sensirion_common.h" |
| 34 | +#include "sensirion_config.h" |
| 35 | +#include "sensirion_i2c_hal.h" |
| 36 | + |
| 37 | +/* |
| 38 | + * INSTRUCTIONS |
| 39 | + * ============ |
| 40 | + * |
| 41 | + * Implement all functions where they are marked as IMPLEMENT. |
| 42 | + * Follow the function specification in the comments. |
| 43 | + */ |
| 44 | + |
| 45 | +/** |
| 46 | + * Select the current i2c bus by index. |
| 47 | + * All following i2c operations will be directed at that bus. |
| 48 | + * |
| 49 | + * THE IMPLEMENTATION IS OPTIONAL ON SINGLE-BUS SETUPS (all sensors on the same |
| 50 | + * bus) |
| 51 | + * |
| 52 | + * @param bus_idx Bus index to select |
| 53 | + * @returns 0 on success, an error code otherwise |
| 54 | + */ |
| 55 | +int16_t sensirion_i2c_hal_select_bus(uint8_t bus_idx) { |
| 56 | + /* TODO:IMPLEMENT or leave empty if all sensors are located on one single |
| 57 | + * bus |
| 58 | + */ |
| 59 | + return NOT_IMPLEMENTED_ERROR; |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * Initialize all hard- and software components that are needed for the I2C |
| 64 | + * communication. |
| 65 | + */ |
| 66 | +void sensirion_i2c_hal_init(void) { |
| 67 | + /* TODO:IMPLEMENT */ |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Release all resources initialized by sensirion_i2c_hal_init(). |
| 72 | + */ |
| 73 | +void sensirion_i2c_hal_free(void) { |
| 74 | + /* TODO:IMPLEMENT or leave empty if no resources need to be freed */ |
| 75 | +} |
| 76 | + |
| 77 | +/** |
| 78 | + * Execute one read transaction on the I2C bus, reading a given number of bytes. |
| 79 | + * If the device does not acknowledge the read command, an error shall be |
| 80 | + * returned. |
| 81 | + * |
| 82 | + * @param address 7-bit I2C address to read from |
| 83 | + * @param data pointer to the buffer where the data is to be stored |
| 84 | + * @param count number of bytes to read from I2C and store in the buffer |
| 85 | + * @returns 0 on success, error code otherwise |
| 86 | + */ |
| 87 | +int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) { |
| 88 | + int status = i2c_read_blocking(i2c_default, address, data, count, false); |
| 89 | + if (status == 0) |
| 90 | + return 1; |
| 91 | + else |
| 92 | + return 0; |
| 93 | +} |
| 94 | + |
| 95 | +/** |
| 96 | + * Execute one write transaction on the I2C bus, sending a given number of |
| 97 | + * bytes. The bytes in the supplied buffer must be sent to the given address. If |
| 98 | + * the slave device does not acknowledge any of the bytes, an error shall be |
| 99 | + * returned. |
| 100 | + * |
| 101 | + * @param address 7-bit I2C address to write to |
| 102 | + * @param data pointer to the buffer containing the data to write |
| 103 | + * @param count number of bytes to read from the buffer and send over I2C |
| 104 | + * @returns 0 on success, error code otherwise |
| 105 | + */ |
| 106 | +int8_t sensirion_i2c_hal_write(uint8_t address, const uint8_t* data, |
| 107 | + uint16_t count) { |
| 108 | + // I2C Default is used (I2C0). |
| 109 | + int status = i2c_write_blocking(i2c_default, address, data, count, true); |
| 110 | + |
| 111 | + if (status == 0) |
| 112 | + return 1; |
| 113 | + else |
| 114 | + return 0; |
| 115 | +} |
| 116 | + |
| 117 | +/** |
| 118 | + * Sleep for a given number of microseconds. The function should delay the |
| 119 | + * execution for at least the given time, but may also sleep longer. |
| 120 | + * |
| 121 | + * Despite the unit, a <10 millisecond precision is sufficient. |
| 122 | + * |
| 123 | + * @param useconds the sleep time in microseconds |
| 124 | + */ |
| 125 | +void sensirion_i2c_hal_sleep_usec(uint32_t useconds) { |
| 126 | + sleep_ms(useconds / 1000); |
| 127 | +} |
0 commit comments