Bluetooth BLE---DA14683용 IIC 호스트 통신 C 소스

3132 단어 DialogDA14683
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 

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 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 


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


좋은 웹페이지 즐겨찾기