之前多通过software_i2c 进行i2c对接。只需要
1. 配置好SDA ,SCL ,确保i2c通讯正常(一般可以通过验证chip id 来判断)。
2. 然后按第三方模块(比如心率血氧模块)要求调用适当的timer ,就可以正常工作了。
后面再调试MK8000的跟随云台,标签端要加gsensor(DA217) ,使用i2c通讯。
看了下sdk i2c demo 用的硬件方式。
1. 原接口:
//uint32_t i2c_read(uint8_t slave_addr, uint16_t reg_addr);
read 是一次性读4字节的,而一般i2c是读1字节,这里需要调整。
//void i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint32_t data);
/*
* Copyright (c) 2019-2023 Beijing Hanwei Innovation Technology Ltd. Co. and
* its subsidiaries and affiliates (collectly called MKSEMI).
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form, except as embedded into an MKSEMI
* integrated circuit in a product or a software update for such product,
* must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* 3. Neither the name of MKSEMI nor the names of its contributors may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* 4. This software, with or without modification, must only be used with a
* MKSEMI integrated circuit.
*
* 5. Any software provided in binary form under this license must not be
* reverse engineered, decompiled, modified and/or disassembled.
*
* THIS SOFTWARE IS PROVIDED BY MKSEMI "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL MKSEMI OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "mk_i2c.h"
#include "mk_trace.h"
#include "mk_reset.h"
#include "mk_calib.h"
#include "mk_wdt.h"
#include "mk_misc.h"
#include "libc_rom.h"
#include "board.h"
#define TARGET_ADDR 0x15
#define TEST_PORT I2C_ID0
#define SLAVE_TEST 0
volatile static uint8_t rx_done = 0;
volatile static uint8_t tx_done = 0;
static void i2c_read_callback(void *dev, uint32_t err_code)
{
rx_done = 1;
if (err_code)
{
LOG_INFO(TRACE_MODULE_APP, "I2C read error code %x\r\n", err_code);
}
else
{
LOG_INFO(TRACE_MODULE_APP, "I2C read done\r\n");
}
}
static void i2c_write_callback(void *dev, uint32_t err_code)
{
tx_done = 1;
if (err_code)
{
LOG_INFO(TRACE_MODULE_APP, "I2C write error code %x\r\n", err_code);
}
else
{
LOG_INFO(TRACE_MODULE_APP, "I2C write done\r\n");
}
}
#if SLAVE_TEST == 0
static uint32_t i2c_read(uint8_t slave_addr, uint16_t reg_addr)
{
uint8_t tx_buf[2];
uint8_t rx_buf[4];
tx_buf[0] = reg_addr & 0xff;
tx_buf[1] = (reg_addr >> 8) & 0xff;
#if 1
tx_done = 0;
i2c_master_send(TEST_PORT, slave_addr, tx_buf, 2, i2c_write_callback);
while (tx_done == 0)
{
}
delay_us(1000);
rx_done = 0;
i2c_master_receive(TEST_PORT, slave_addr, rx_buf, 4, i2c_read_callback);
while (rx_done == 0)
{
}
#else
rx_done = 0;
i2c_master_transfer(TEST_PORT, slave_addr, tx_buf, 2, rx_buf, 4, i2c_read_callback);
while (rx_done == 0)
{
}
#endif
return (uint32_t)((rx_buf[3] << 24) | (rx_buf[2] << 16) | (rx_buf[1] << 8) | rx_buf[0]);
}
static void i2c_write(uint8_t slave_addr, uint16_t reg_addr, uint32_t data)
{
uint8_t tx_buf[6];
tx_buf[0] = reg_addr & 0xff;
tx_buf[1] = (reg_addr >> 8) & 0xff;
tx_buf[2] = data & 0xff;
tx_buf[3] = (data >> 8) & 0xff;
tx_buf[4] = (data >> 16) & 0xff;
tx_buf[5] = (data >> 24) & 0xff;
tx_done = 0;
i2c_master_send(TEST_PORT, slave_addr, tx_buf, 6, i2c_write_callback);
while (tx_done == 0)
{
}
}
#endif
int main(void)
{
board_clock_run();
board_pins_config();
board_debug_console_open(TRACE_PORT_UART0);
// Reset reason
reset_cause_get();
reset_cause_clear();
// Chip calibration
calib_chip();
// Disable watchdog timer
wdt_close(WDT_ID0);
LOG_INFO(TRACE_MODULE_APP, "I2C example\r\n");
gpio_open();
board_configure();
struct I2C_CFG_T usr_i2c_cfg =
{
#if SLAVE_TEST
.mode = I2C_SLAVE,
.local_addr = TARGET_ADDR,
.target_addr = MK_DEV_ADDRESS,
#else
.mode = I2C_MASTER,
.local_addr = MK_DEV_ADDRESS,
.target_addr = TARGET_ADDR,
#endif
.speed_mode = I2C_SPEED_STANDARD,
.addr_mode = I2C_7BIT_ADDR,
.rx_level = I2C_RXFIFO_CHAR_1,
.tx_level = I2C_TXFIFO_CHAR_1,
.int_rx = true,
.int_tx = true,
};
i2c_open(TEST_PORT, &usr_i2c_cfg);
while (1)
{
#if SLAVE_TEST
uint8_t rx_buf[6];
rx_done = 0;
i2c_slave_receive(TEST_PORT, rx_buf, 6, i2c_read_callback);
while (rx_done == 0)
{
}
// receive 2bytes register address data
rx_done = 0;
i2c_slave_receive(TEST_PORT, rx_buf, 2, i2c_read_callback);
while (rx_done == 0)
{
}
tx_done = 0;
i2c_slave_send(TEST_PORT, &rx_buf[2], 4, i2c_write_callback);
while (tx_done == 0)
{
}
#else
i2c_write(TARGET_ADDR, 0x0, 0x11223344);
delay_us(1000);
uint32_t data = i2c_read(TARGET_ADDR, 0x0);
if (data == 0x11223344)
{
LOG_INFO(TRACE_MODULE_APP, "I2C write and read success\r\n");
}
else
{
LOG_INFO(TRACE_MODULE_APP, "I2C write and read mismatch %x\r\n", data);
}
delay_us(1000000);
#endif
}
}
2.现接口:改为了1字节发送,
uint8_t i2c_read(uint8_t slave_addr, uint8_t reg_addr, uint8_t *rxbuf, int len)
{
uint8_t tx_buf[2];
tx_buf[0] = reg_addr & 0xff;
#if 1
tx_done = 0;
i2c_master_send(TEST_PORT, slave_addr, tx_buf, 1, i2c_write_callback);
while (tx_done == 0)
{
}
delay_us(1000);
rx_done = 0;
i2c_master_receive(TEST_PORT, slave_addr, rxbuf, len, i2c_read_callback);
while (rx_done == 0)
{
}
#else
rx_done = 0;
i2c_master_transfer(TEST_PORT, slave_addr, tx_buf, 2, rx_buf, 4, i2c_read_callback);
while (rx_done == 0)
{
}
#endif
return 0;
}
void i2c_write(uint8_t slave_addr, uint8_t reg_addr, uint8_t data)
{
uint8_t tx_buf[6];
tx_buf[0] = reg_addr & 0xff;
tx_buf[1] = data & 0xff;
tx_done = 0;
i2c_master_send(TEST_PORT, slave_addr, tx_buf, 2, i2c_write_callback);
while (tx_done == 0)
{
}
}
数据处理和initiate:
s8_m mir3da_register_read(u8_m addr, u8_m *data_m, u8_m len)
{
//lcd_i2c_readReg(i2c_addr, addr, 1, data_m, len);
int res ;
res = i2c_read(TARGET_ADDR, addr, data_m, len);
//mk_printf("data_m=%x\r", res);
return res;
}
s8_m mir3da_register_write(u8_m addr, u8_m data_m)
{
// lcd_i2c_writeReg(i2c_addr, addr, 1, &data_m, 1);
i2c_write(TARGET_ADDR, addr, data_m);
return 0;
}
s8_m mir3da_init(void){
s8_m res = 0;
u8_m data_m = 0;
LOG_AOA(TRACE_MODULE_APP," mir3da_init\r\n");
// mir_IO_config(GPIO_I2cHandler);
//Retry 3 times
//res = mir3da_register_read(NSA_REG_SPI_I2C,&mir3da_id,1);
mir3da_register_read(NSA_REG_WHO_AM_I,&data_m,1);
mk_printf("data_m = 0x%02x\r\n",data_m);
LOG_AOA(TRACE_MODULE_APP, "data_m = 0x%02x\r\n",data_m);
if(data_m != 0x13)
{
i2c_addr |= 0x02;
res = mir3da_register_read(NSA_REG_WHO_AM_I,&data_m,1);
// mk_printf("data_m = 0x%02x\r\n",data_m);
if(data_m != 0x13)
{
res = mir3da_register_read(NSA_REG_WHO_AM_I,&data_m,1);
if(data_m != 0x13)
{
// mk_printf("------mir3da read chip id error= %x-----\r\n",data_m);
return -1;
}
}
}
res |= mir3da_register_write(NSA_REG_POWERMODE_BW, 0x38);
sys_timer_delay_ms(10);
res |= mir3da_register_write(NSA_REG_ODR_AXIS_DISABLE, 0x06); //ODR = 125hz
res |= mir3da_register_write(NSA_REG_G_RANGE, 0x02); //+/-2G,14bit
res |= mir3da_register_write(NSA_REG_ACTIVE_DURATION, 0x00);
res |= mir3da_register_write(NSA_REG_ACTIVE_THRESHOLD,0x0a); //threshold value ???
res |= mir3da_register_write(NSA_REG_POWERMODE_BW, 0x04); //normal mode
res |= mir3da_register_write(NSA_REG_INTERRUPT_MAPPING1,0x04);
res |= mir3da_register_write(NSA_REG_STEP_FILTER,0x80);
res |= mir3da_register_write(NSA_REG_INTERRUPT_SETTINGS1,0x87);
mk_printf("mir3da id %02X\r\n", data_m);
return res;
}
附:读 gsensor x, y, z 坐标值
//Read three axis data, 1024 LSB = 1 g
s8_m mir3da_read_data(s16_m *x, s16_m *y, s16_m *z)
{
u8_m tmp_data[6] = {0};
if (mir3da_register_read(NSA_REG_ACC_X_LSB, tmp_data,6) != 0) {
return -1;
}
*x = ((s16_m)(tmp_data[1] << 8 | tmp_data[0]))>> 3;
*y = ((s16_m)(tmp_data[3] << 8 | tmp_data[2]))>> 3;
*z = ((s16_m)(tmp_data[5] << 8 | tmp_data[4]))>> 3;
mk_printf("Read XYZ raw data x=%d y=%d z=%d ,",*x,*y,*z);
return 0;
}
后面要注意开启宏:
// I2C work mode
#define I2C_INT_MODE_EN (1)