蓝牙BLE---DA14683的IIC主机通信C源码

版权声明:转载请注明出处。技术交流加微信:life5270 https://blog.csdn.net/JaLLs/article/details/84873407

demo_i2c.h

/*
 * demo_i2c.h
 *
 *  Created on: 2018年12月7日
 *      Author: Jim
 */

#ifndef SDK_PERIPHERALS_INCLUDE_DEMO_I2C_H_
#define SDK_PERIPHERALS_INCLUDE_DEMO_I2C_H_
#include <stdbool.h>

void demo_i2c_init(void);
void i2c_read_reg(uint8_t reg, uint8_t *val, uint8_t len);
void i2c_write_reg(uint8_t reg, const uint8_t *val, uint8_t len);
#endif /* SDK_PERIPHERALS_INCLUDE_DEMO_I2C_H_ */

demo_i2c.c

/**
 ****************************************************************************************
 *
 * @file demo_i2c.c
 *
 * @brief I2C demo (hw_i2c driver)
 *
 * Copyright (C) 2015 Dialog Semiconductor.
 * This computer program includes Confidential, Proprietary Information
 * of Dialog Semiconductor. All Rights Reserved.
 *
 ****************************************************************************************
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <osal.h>
#include <platform_devices.h>
#include <hw_i2c.h>
#include <resmgmt.h>
#include <demo_i2c.h>


#define CFG_GPIO_I2C1_SCL_PORT          (HW_GPIO_PORT_3)
#define CFG_GPIO_I2C1_SCL_PIN           (HW_GPIO_PIN_5)
#define CFG_GPIO_I2C1_SDA_PORT          (HW_GPIO_PORT_1)
#define CFG_GPIO_I2C1_SDA_PIN           (HW_GPIO_PIN_2)


#define SLAVE_ADDRESS    0x4F


void demo_i2c_init(void)
{
        /*
         * Initialize I2C controller in master mode with standard communication speed (100 kb/s) and
         * transfer in 7-bit addressing mode.
         */
        static const i2c_config cfg = {
                .speed = HW_I2C_SPEED_STANDARD,
                .mode = HW_I2C_MODE_MASTER,
                .addr_mode = HW_I2C_ADDRESSING_7B,
                .address = SLAVE_ADDRESS,
        };

        hw_i2c_init(HW_I2C1, &cfg);

        srand(OS_GET_TICK_COUNT());
}


void i2c_write_reg(uint8_t reg, const uint8_t *val, uint8_t len)
{
        size_t wr_status = 0;
        HW_I2C_ABORT_SOURCE abrt_src = HW_I2C_ABORT_NONE;

        /*
         * The first writing byte informs to which register rest data will be written.
         */
        hw_i2c_write_byte(HW_I2C1, reg);
        wr_status = hw_i2c_write_buffer_sync(HW_I2C1, val, len, &abrt_src, HW_I2C_F_WAIT_FOR_STOP);
        if ((wr_status < (ssize_t)len) || (abrt_src != HW_I2C_ABORT_NONE)) {
                printf("write failure" );
        }
}

void i2c_read_reg(uint8_t reg, uint8_t *val, uint8_t len)
{
        size_t rd_status = 0;
        HW_I2C_ABORT_SOURCE abrt_src = HW_I2C_ABORT_NONE;

        /*
         * Before reading values from sensor registers we need to send one byte information to it
         * to inform which sensor register will be read now.
         */
        hw_i2c_write_byte(HW_I2C1, reg);
        rd_status = hw_i2c_read_buffer_sync(HW_I2C1, val, len, &abrt_src, HW_I2C_F_NONE);
        if ((rd_status < (size_t)len) || (abrt_src != HW_I2C_ABORT_NONE)) {
                printf("read failure" );
        }
}

猜你喜欢

转载自blog.csdn.net/JaLLs/article/details/84873407