mkiiskila commented on a change in pull request #757: HW driver: drv2605l URL: https://github.com/apache/mynewt-core/pull/757#discussion_r166528378
########## File path: hw/drivers/drv2605/src/drv2605.c ########## @@ -0,0 +1,872 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * resarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include <string.h> +#include <errno.h> +#include <assert.h> + +#include "defs/error.h" +#include "os/os.h" +#include "sysinit/sysinit.h" +#include "hal/hal_i2c.h" +#include "hal/hal_gpio.h" +#include "drv2605/drv2605.h" +#include "drv2605_priv.h" + +#if MYNEWT_VAL(DRV2605_LOG) +#include "log/log.h" +#endif + +#if MYNEWT_VAL(DRV2605_STATS) +#include "stats/stats.h" +#endif + +#if MYNEWT_VAL(DRV2605_STATS) +/* Define the stats section and records */ +STATS_SECT_START(drv2605_stat_section) + STATS_SECT_ENTRY(errors) +STATS_SECT_END + +/* Define stat names for querying */ +STATS_NAME_START(drv2605_stat_section) + STATS_NAME(drv2605_stat_section, errors) +STATS_NAME_END(drv2605_stat_section) + +/* Global variable used to hold stats data */ +STATS_SECT_DECL(drv2605_stat_section) g_drv2605stats; +#endif + +#if MYNEWT_VAL(DRV2605_LOG) +#define LOG_MODULE_DRV2605 (306) +#define DRV2605_INFO(...) LOG_INFO(&_log, LOG_MODULE_DRV2605, __VA_ARGS__) +#define DRV2605_ERR(...) LOG_ERROR(&_log, LOG_MODULE_DRV2605, __VA_ARGS__) +static struct log _log; +#else +#define DRV2605_INFO(...) +#define DRV2605_ERR(...) +#endif + + +/** + * Writes a single byte to the specified register + * + * @param The Sesnsor interface + * @param The register address to write to + * @param The value to write + * + * @return 0 on success, non-zero error on failure. + */ +int +drv2605_write8(struct sensor_itf *itf, uint8_t reg, uint8_t value) +{ + int rc; + uint8_t payload[2] = { reg, value}; + + struct hal_i2c_master_data data_struct = { + .address = itf->si_addr, + .len = 2, + .buffer = payload + }; + + rc = hal_i2c_master_write(itf->si_num, &data_struct, OS_TICKS_PER_SEC, 1); + if (rc) { + DRV2605_ERR("Failed to write to 0x%02X:0x%02X with value 0x%02X\n", + data_struct.address, reg, value); +#if MYNEWT_VAL(DRV2605_STATS) + STATS_INC(g_drv2605stats, errors); +#endif + } + + return rc; +} + +/** + * Writes a multiple bytes to the specified register + * + * @param The Sesnsor interface + * @param The register address to write to + * @param The data buffer to write from + * + * @return 0 on success, non-zero error on failure. + */ +int +drv2605_writelen(struct sensor_itf *itf, uint8_t reg, uint8_t *buffer, + uint8_t len) +{ + int rc; + uint8_t payload[20] = { reg, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0}; + + struct hal_i2c_master_data data_struct = { + .address = itf->si_addr, + .len = len + 1, + .buffer = payload + }; + + memcpy(&payload[1], buffer, len); Review comment: Maybe check that len bytes does not overflow the buffer? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
