Files
moyuhai 41a602f89c V1.0
2026-06-09 16:39:17 +08:00

87 lines
2.6 KiB
C

/*!
* \file i2c_master.c
*
* \brief Target i2c master implementation
*
* \copyright Revised BSD License, see section \ref LICENSE.
*
* \code
*
* _ __ _ ________ _
* | |/ /(_)___ / ____/ /_ (_)___
* | // / __ \/ / / __ \/ / __ \
* / |/ / / / / /___/ / / / / /_/ /
* /_/|_/_/_/ /_/\____/_/ /_/_/ .___/
* /_/
* (C) 2022-2025 XinChip
*
* \endcode
*
* \author ( XinChip ) Alex-J
*
* \author ( XinChip )
*/
/*-----------------------------------------------------------------------------------
INCLUDE HEADE FILES
------------------------------------------------------------------------------------*/
#include "i2c_master.h"
#include "xc6xxx_hal_i2c.h"
/*------------------------------------------------------------------------------------
Macros
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Global Variables
-------------------------------------------------------------------------------------*/
/*------------------------------------------------------------------------------------
Functions
-------------------------------------------------------------------------------------*/
/**
* @brief i2c_master_demo
* @details
* @param void
* @retval void
*/
void i2c_master_demo(void) {
DEBUG("i2c_master_test \n");
uint8_t txbuff[32];
uint8_t rxbuff[32];
I2C_InitTypeDef I2C_InitStruct = {0};
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Mux = GPIO_Mux0;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Int = NOT_INT;
GPIO_InitStruct.Pin = GPIO_5;
GPIO_InitStruct.Dir = GPIO_DIR_OUTPUT;
GPIO_InitStruct.FunSel = I2C_SCL;
GPIO_Init(&GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_4;
GPIO_InitStruct.Dir = GPIO_DIR_INPUT;
GPIO_InitStruct.FunSel = I2C_SDA;
GPIO_Init(&GPIO_InitStruct);
I2C_InitStruct.I2C_ClockSpeed = I2C_ClockSpeed_400K;
I2C_InitStruct.I2C_Mode = I2C_Mode_MASTER;
I2C_InitStruct.I2C_OwnAddress1 = 0xA0 >> 1;
I2C_Init(XC_I2C, &I2C_InitStruct);
for (int i = 0; i < 32; i++) {
txbuff[i] = i;
}
I2C_Master_Write(XC_I2C, (uint8_t *)txbuff, sizeof(txbuff));
I2C_Master_Read(XC_I2C, (uint8_t *)rxbuff, sizeof(rxbuff));
DEBUG("reve:");
for (int i = 0; i < sizeof(rxbuff); i++) {
DEBUG("%d-", rxbuff[i]);
}
DEBUG("\n");
}