amiro-lld / drivers / P9221R / v1 / alld_P9221R.c @ d55455fa
History | View | Annotate | Download (6.193 KB)
| 1 | 6ef38c23 | Julia Niermann | /*
|
|---|---|---|---|
| 2 | AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
|
||
| 3 | Copyright (C) 2016..2019 Thomas Schöpping et al.
|
||
| 4 | |||
| 5 | This program is free software: you can redistribute it and/or modify
|
||
| 6 | it under the terms of the GNU Lesser General Public License as published by
|
||
| 7 | the Free Software Foundation, either version 3 of the License, or
|
||
| 8 | (at your option) any later version.
|
||
| 9 | |||
| 10 | This program is distributed in the hope that it will be useful,
|
||
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
| 13 | GNU Lesser General Public License for more details.
|
||
| 14 | |||
| 15 | You should have received a copy of the GNU Lesser General Public License
|
||
| 16 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
| 17 | */
|
||
| 18 | |||
| 19 | /**
|
||
| 20 | e7af07e7 | Julia Niermann | * @file alld_P9221R.c
|
| 21 | 6ef38c23 | Julia Niermann | *
|
| 22 | d55455fa | Julia Niermann | * @brief qi charger
|
| 23 | 6ef38c23 | Julia Niermann | *
|
| 24 | * @{
|
||
| 25 | */
|
||
| 26 | |||
| 27 | 54dbb62d | Julia Niermann | #include <alld_P9221R.h> |
| 28 | 6ef38c23 | Julia Niermann | |
| 29 | /******************************************************************************/
|
||
| 30 | /* LOCAL DEFINITIONS */
|
||
| 31 | /******************************************************************************/
|
||
| 32 | |||
| 33 | /******************************************************************************/
|
||
| 34 | /* EXPORTED VARIABLES */
|
||
| 35 | /******************************************************************************/
|
||
| 36 | |||
| 37 | /******************************************************************************/
|
||
| 38 | /* LOCAL TYPES */
|
||
| 39 | /******************************************************************************/
|
||
| 40 | |||
| 41 | /******************************************************************************/
|
||
| 42 | /* LOCAL VARIABLES */
|
||
| 43 | /******************************************************************************/
|
||
| 44 | |||
| 45 | /******************************************************************************/
|
||
| 46 | /* LOCAL FUNCTIONS */
|
||
| 47 | /******************************************************************************/
|
||
| 48 | |||
| 49 | /******************************************************************************/
|
||
| 50 | /* EXPORTED FUNCTIONS */
|
||
| 51 | /******************************************************************************/
|
||
| 52 | |||
| 53 | /**
|
||
| 54 | * @brief Read the value of one or more of the registers.
|
||
| 55 | * @param[in] i2cd i2c driver
|
||
| 56 | * @param[in] PRd p9221r driver
|
||
| 57 | * @param[in] addr register address
|
||
| 58 | * @param[out] data register content
|
||
| 59 | * @param[in] num number of subsequent registers to read
|
||
| 60 | * @param[in] timeout timeout
|
||
| 61 | * @return An indicator whether the call was successfull
|
||
| 62 | */
|
||
| 63 | 9797f02a | Julia Niermann | apalExitStatus_t p9221r_lld_read_register(const P9221RDriver* const p9221r, const p9221r_lld_register_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout) |
| 64 | 6ef38c23 | Julia Niermann | {
|
| 65 | apalDbgAssert(p9221r != NULL);
|
||
| 66 | apalDbgAssert(p9221r->i2cd != NULL);
|
||
| 67 | apalDbgAssert(data != NULL);
|
||
| 68 | |||
| 69 | 73345246 | Julia Niermann | uint8_t tx[2] = {(addr & 0xFF00) >> 8, addr & 0x00FF}; |
| 70 | |||
| 71 | apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), tx, 2, data, num, timeout);
|
||
| 72 | c4db2363 | Julia Niermann | |
| 73 | 6ef38c23 | Julia Niermann | return status;
|
| 74 | } |
||
| 75 | |||
| 76 | /**
|
||
| 77 | * @brief Write the value of one or more of the registers.
|
||
| 78 | * @param[in] i2cd i2c driver
|
||
| 79 | * @param[in] PRd p9221r driver
|
||
| 80 | * @param[in] addr register address
|
||
| 81 | * @param[in] data data to write
|
||
| 82 | * @param[in] num number of subsequent registers to read
|
||
| 83 | * @param[in] timeout timeout
|
||
| 84 | * @return An indicator whether the call was successfull
|
||
| 85 | */
|
||
| 86 | 2344a771 | Julia Niermann | apalExitStatus_t p9221r_lld_write_register(const P9221RDriver* const p9221r, const p9221r_lld_register_t addr, const uint16_t* const data, const uint8_t num, const apalTime_t timeout) |
| 87 | 6ef38c23 | Julia Niermann | {
|
| 88 | apalDbgAssert(p9221r != NULL);
|
||
| 89 | apalDbgAssert(p9221r->i2cd != NULL);
|
||
| 90 | apalDbgAssert(data != NULL);
|
||
| 91 | |||
| 92 | uint8_t buffer[1+2*num]; |
||
| 93 | buffer[0] = addr;
|
||
| 94 | for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) { |
||
| 95 | buffer[dataIdx*2+1] = data[dataIdx] >> 8; |
||
| 96 | buffer[dataIdx*2+2] = data[dataIdx] & (0x00FFu); |
||
| 97 | } |
||
| 98 | 2344a771 | Julia Niermann | return apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), buffer, 1+2*num, NULL, 0, timeout); |
| 99 | 6ef38c23 | Julia Niermann | } |
| 100 | |||
| 101 | 9797f02a | Julia Niermann | /**
|
| 102 | 9be47646 | Julia Niermann | * @brief Read the alignment.
|
| 103 | 9797f02a | Julia Niermann | * @param[in] PRd p9221r driver
|
| 104 | 9be47646 | Julia Niermann | * @param[out] alignment alignment register content
|
| 105 | 9797f02a | Julia Niermann | * @param[in] timeout timeout
|
| 106 | * @return An indicator whether the call was successfull
|
||
| 107 | */
|
||
| 108 | 9be47646 | Julia Niermann | apalExitStatus_t p9221r_lld_read_alignment(const P9221RDriver* const p9221r, int8_t* const alignment, const apalTime_t timeout) |
| 109 | 9797f02a | Julia Niermann | {
|
| 110 | 9be47646 | Julia Niermann | apalExitStatus_t status = p9221r_lld_read_register(p9221r, P9221R_LLD_REGISTER_ALIGNMENT_X, (uint8_t*) alignment, 2, timeout);
|
| 111 | 9797f02a | Julia Niermann | |
| 112 | return status;
|
||
| 113 | } |
||
| 114 | |||
| 115 | /**
|
||
| 116 | * @brief Read the voltage.
|
||
| 117 | * @param[in] i2cd i2c driver
|
||
| 118 | * @param[in] PRd p9221r driver
|
||
| 119 | * @param[out] voltage voltage register content
|
||
| 120 | * @param[in] timeout timeout
|
||
| 121 | * @return An indicator whether the call was successfull
|
||
| 122 | */
|
||
| 123 | c89b966e | Ruben Lipperts | apalExitStatus_t p9221r_lld_read_voltage(const P9221RDriver* const p9221r, float* const voltage, const apalTime_t timeout) |
| 124 | 9797f02a | Julia Niermann | {
|
| 125 | c89b966e | Ruben Lipperts | uint8_t buffer[2];
|
| 126 | uint16_t value; |
||
| 127 | |||
| 128 | 9be47646 | Julia Niermann | apalExitStatus_t status = p9221r_lld_read_register(p9221r, P9221R_LLD_I2C_ADDR_FIXED, buffer, 2, timeout);
|
| 129 | |||
| 130 | 247b3413 | Julia Niermann | value = ((buffer[1] & 0x0F) << 8) | buffer[0]; // 4MSB are in buffer[1] so we shift and mask, 8LSB are in buffer[0] |
| 131 | *voltage = (((float) value) * 6 * 2.1f)/4095.0f; |
||
| 132 | 9797f02a | Julia Niermann | |
| 133 | return status;
|
||
| 134 | } |
||
| 135 | |||
| 136 | /**
|
||
| 137 | * @brief Read the current.
|
||
| 138 | * @param[in] i2cd i2c driver
|
||
| 139 | * @param[in] PRd p9221r driver
|
||
| 140 | * @param[out] current register content
|
||
| 141 | * @param[in] timeout timeout
|
||
| 142 | * @return An indicator whether the call was successfull
|
||
| 143 | */
|
||
| 144 | c89b966e | Ruben Lipperts | apalExitStatus_t p9221r_lld_read_current(const P9221RDriver* const p9221r, float* const current, const apalTime_t timeout) |
| 145 | 9797f02a | Julia Niermann | {
|
| 146 | c89b966e | Ruben Lipperts | uint8_t buffer[2];
|
| 147 | uint16_t value; |
||
| 148 | |||
| 149 | 9be47646 | Julia Niermann | apalExitStatus_t status = p9221r_lld_read_register(p9221r, P9221R_LLD_REGISTER_IOUT_CURRENT_LSB, buffer, 2, timeout);
|
| 150 | c89b966e | Ruben Lipperts | value = (buffer[1] << 8) | buffer[0]; // 8MSB are in buffer[1] so we shift, 8LSB are in buffer[0] |
| 151 | 247b3413 | Julia Niermann | *current = (((float) value) * 2 * 2.1f)/4095.0f; |
| 152 | 9797f02a | Julia Niermann | |
| 153 | return status;
|
||
| 154 | } |
||
| 155 | |||
| 156 | e7af07e7 | Julia Niermann | /** @} */
|