You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
1.9 KiB
C
81 lines
1.9 KiB
C
1 year ago
|
#include "p_i2c.h"
|
||
|
#include "p_usart.h"
|
||
|
#include "hal_init.h"
|
||
|
#include "pc_board.h"
|
||
|
|
||
|
|
||
|
struct i2c_m_sync_desc p_i2c_master;
|
||
|
|
||
|
void p_i2c_init(void)
|
||
|
{
|
||
|
// clock init
|
||
|
hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM3_GCLK_ID_CORE, CONF_GCLK_SERCOM3_CORE_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
|
||
|
hri_gclk_write_PCHCTRL_reg(GCLK, SERCOM3_GCLK_ID_SLOW, CONF_GCLK_SERCOM3_SLOW_SRC | (1 << GCLK_PCHCTRL_CHEN_Pos));
|
||
|
|
||
|
hri_mclk_set_APBBMASK_SERCOM3_bit(MCLK);
|
||
|
|
||
|
i2c_m_sync_init(&p_i2c_master, SERCOM3);
|
||
|
|
||
|
// port init
|
||
|
gpio_set_pin_pull_mode(I2C_MASTER_SDA,
|
||
|
// <y> Pull configuration
|
||
|
// <id> pad_pull_config
|
||
|
// <GPIO_PULL_OFF"> Off
|
||
|
// <GPIO_PULL_UP"> Pull-up
|
||
|
// <GPIO_PULL_DOWN"> Pull-down
|
||
|
GPIO_PULL_OFF);
|
||
|
|
||
|
gpio_set_pin_function(I2C_MASTER_SDA, I2C_MASTER_SDA_MUX);
|
||
|
|
||
|
gpio_set_pin_pull_mode(I2C_MASTER_SCL,
|
||
|
// <y> Pull configuration
|
||
|
// <id> pad_pull_config
|
||
|
// <GPIO_PULL_OFF"> Off
|
||
|
// <GPIO_PULL_UP"> Pull-up
|
||
|
// <GPIO_PULL_DOWN"> Pull-down
|
||
|
GPIO_PULL_OFF);
|
||
|
|
||
|
gpio_set_pin_function(I2C_MASTER_SCL, I2C_MASTER_SCL_MUX);
|
||
|
|
||
|
i2c_m_sync_enable(&p_i2c_master);
|
||
|
}
|
||
|
|
||
|
int p_i2c_write(const uint8_t* const data, uint16_t data_len)
|
||
|
{
|
||
|
return io_write(&p_i2c_master.io, data, data_len);
|
||
|
}
|
||
|
|
||
|
int p_i2c_read(uint8_t* data, uint16_t len)
|
||
|
{
|
||
|
return io_read(&p_i2c_master.io, data, len);
|
||
|
}
|
||
|
|
||
|
void p_i2c_detect(void)
|
||
|
{
|
||
|
PDEBUG(" ");
|
||
|
for(int ind = 0; ind < 16; ind++)
|
||
|
{
|
||
|
PDEBUG(" %02x", ind);
|
||
|
}
|
||
|
uint8_t data = 0x00;
|
||
|
for(int ind = 0; ind <= 119; ind++)
|
||
|
{
|
||
|
if (ind % 16 == 0)
|
||
|
{
|
||
|
PDEBUG("\r\n%02x:", ind & 0xF0);
|
||
|
}
|
||
|
i2c_m_sync_set_slaveaddr(&p_i2c_master, ind, I2C_M_SEVEN);
|
||
|
int ret = p_i2c_write(&data, 1);
|
||
|
if(ret != 1)
|
||
|
{
|
||
|
PDEBUG(" --");
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
PDEBUG(" %02x", ind);
|
||
|
}
|
||
|
}
|
||
|
PDEBUG("\r\n");
|
||
|
}
|
||
|
|