Read MMC5633 Product ID

/*
 * Copyright (c) 2019-2021 Mauna Kea Semiconductor Holdings.
 * All rights reserved.
 *
 */

#include "mk_i2c.h"
#include "mk_trace.h"
#include "mk_wdt.h"
#include "mk_gpio.h"
#include "board.h"
#include "pin_mux.h"
#include "clock_config.h"

/*
MMC5633:
	I2C Address:0x30
	Product ID REG Address: 0x39, Value: 0x80
*/

/*
I2C_STATE_RESET = 0x00U,
I2C_STATE_READY = 0x01U,
I2C_STATE_BUSY_TX = 0x10U,
I2C_STATE_BUSY_RX = 0x20U,
I2C_STATE_BUSY_TRX = 0x30U,
I2C_STATE_TIMEOUT = 0x40U,
I2C_STATE_ERROR = 0x80U,
 */

#define MMC5633_PID_REG_ADDR		0x39

#define TARGET_ADDR 0x30
#define TEST_PORT I2C_ID0
#define SLAVE_TEST 0

static uint8_t rx_done = 0;
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 1
static uint8_t i2c_read_new(uint8_t slave_addr, uint8_t reg_addr)
{
	uint8_t tx_buf[1] = {0};
	uint8_t rx_buf[1] = {0x5A};

	tx_buf[0] = reg_addr;

	tx_done = 0;
	i2c_master_send(TEST_PORT, slave_addr, tx_buf, 1, i2c_write_callback);
	while(tx_done == 0)
		;

	rx_done = 0;
	i2c_master_receive(TEST_PORT, slave_addr, rx_buf, 1, i2c_read_callback);
	while (rx_done == 0)
		;

	return rx_buf[0];
}
#endif


#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();

    // Disable watchdog timer
    wdt_close(WDT_ID0);
    LOG_INFO(TRACE_MODULE_APP, "WDT close\r\n");

    const 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,
    };

    LOG_INFO(TRACE_MODULE_APP, "Compile:%s:%s\r\n", __DATE__, __TIME__);

    i2c_open(TEST_PORT, &usr_i2c_cfg);

    //Firstly, we should provide power for sensors!
    gpio_open();
    gpio_pin_set_dir(GPIO_PIN_8, GPIO_DIR_OUT, 1);
    gpio_pin_set(GPIO_PIN_8);

    //uint8_t read_addr = (uint8_t)TARGET_ADDR << 1 | 0x01;
    uint8_t read_addr = TARGET_ADDR;

    while (1)
    {
    	delay_us(1000000);

#if 0
    	uint32_t data = i2c_read_new(TARGET_ADDR, MMC5633_PID_REG_ADDR);
    	LOG_INFO(TRACE_MODULE_APP, "address: 0x%X, ID:0x%X\r\n", TARGET_ADDR, data);
#else
    	uint32_t data = i2c_read_new(read_addr, MMC5633_PID_REG_ADDR);
    	LOG_INFO(TRACE_MODULE_APP, "address: 0x%X, ID:0x%X\r\n", read_addr, data);
#endif
    }

    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
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值