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