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

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" );
        }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JaLLs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值