GONEC_ARTERY_I2c_smbus/i2c_smbus.c

208 lines
6.3 KiB
C

//
// Created by cfif on 11.02.23.
//
#include "i2c_application.h"
#include "delay_sec.h"
#include "stdbool.h"
/** @addtogroup AT32F437_periph_examples
* @{
*/
/** @addtogroup 437_I2C_communication_smbus I2C_communication_smbus
* @{
*/
#define I2C_TIMEOUT 0xFFFF
//#define I2Cx_CLKCTRL 0xB170FFFF //10K
//#define I2Cx_CLKCTRL 0xC0E06969 //50K
#define I2Cx_CLKCTRL 0x80504C4E //100K
//#define I2Cx_CLKCTRL 0x30F03C6B //200K
//0x6A
#define I2Cx_ADDRESS (0x60 << 1)
#define I2Cx_PORT I2C3
#define I2Cx_CLK CRM_I2C3_PERIPH_CLOCK
#define I2Cx_DMA DMA1
#define I2Cx_DMA_CLK CRM_DMA1_PERIPH_CLOCK
#define I2Cx_SCL_GPIO_CLK CRM_GPIOF_PERIPH_CLOCK
#define I2Cx_SCL_GPIO_PIN GPIO_PINS_14
#define I2Cx_SCL_GPIO_PinsSource GPIO_PINS_SOURCE14
#define I2Cx_SCL_GPIO_PORT GPIOF
#define I2Cx_SCL_GPIO_MUX GPIO_MUX_4
#define I2Cx_SDA_GPIO_CLK CRM_GPIOF_PERIPH_CLOCK
#define I2Cx_SDA_GPIO_PIN GPIO_PINS_15
#define I2Cx_SDA_GPIO_PinsSource GPIO_PINS_SOURCE15
#define I2Cx_SDA_GPIO_PORT GPIOF
#define I2Cx_SDA_GPIO_MUX GPIO_MUX_4
i2c_handle_type hi2cx;
void i2c_lowlevel_init(i2c_handle_type *hi2c) {
gpio_init_type gpio_init_structure;
if (hi2c->i2cx == I2Cx_PORT) {
/* i2c periph clock enable */
crm_periph_clock_enable(I2Cx_CLK, TRUE);
crm_periph_clock_enable(I2Cx_SCL_GPIO_CLK, TRUE);
crm_periph_clock_enable(I2Cx_SDA_GPIO_CLK, TRUE);
/* gpio configuration */
gpio_pin_mux_config(I2Cx_SCL_GPIO_PORT, I2Cx_SCL_GPIO_PinsSource, I2Cx_SCL_GPIO_MUX);
gpio_pin_mux_config(I2Cx_SDA_GPIO_PORT, I2Cx_SDA_GPIO_PinsSource, I2Cx_SDA_GPIO_MUX);
/* configure i2c pins: scl */
gpio_init_structure.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
gpio_init_structure.gpio_mode = GPIO_MODE_MUX;
gpio_init_structure.gpio_out_type = GPIO_OUTPUT_OPEN_DRAIN;
gpio_init_structure.gpio_pull = GPIO_PULL_UP;
gpio_init_structure.gpio_pins = I2Cx_SCL_GPIO_PIN;
gpio_init(I2Cx_SCL_GPIO_PORT, &gpio_init_structure);
/* configure i2c pins: sda */
gpio_init_structure.gpio_pins = I2Cx_SDA_GPIO_PIN;
gpio_init(I2Cx_SDA_GPIO_PORT, &gpio_init_structure);
/* config i2c */
i2c_init(hi2c->i2cx, 0, I2Cx_CLKCTRL);
i2c_own_address1_set(hi2c->i2cx, I2C_ADDRESS_MODE_7BIT, I2Cx_ADDRESS);
}
}
const uint16_t V = 1700;
uint8_t reg0[2] = {0x0, V & 7};
uint8_t reg1[2] = {0x1, V >> 3};
uint8_t reg2[2] = {0x2, 0x1};
uint8_t reg3[2] = {0x3, 70};
uint8_t reg_read;
bool sendI2c() {
i2c_status_type i2c_status;
hi2cx.i2cx = I2Cx_PORT;
// i2c config
i2c_config(&hi2cx);
uint8_t adr = 1;
// 0 - запись
// 1 - чтение
// Reg0 - Напряжение
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg0, 2, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg0, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_receive(&hi2cx, I2Cx_ADDRESS | 1, &reg_read, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
// Reg1 - Напряжение
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg1, 2, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg1, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_receive(&hi2cx, I2Cx_ADDRESS | 1, &reg_read, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
// Reg3 - Ток
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg3, 2, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg3, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_receive(&hi2cx, I2Cx_ADDRESS | 1, &reg_read, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
// Reg2 - Запуск
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg2, 2, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg2, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_receive(&hi2cx, I2Cx_ADDRESS | 1, &reg_read, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
// delay_ms(20);
// Reg2
if ((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, reg2, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
if ((i2c_status = i2c_master_receive(&hi2cx, I2Cx_ADDRESS | 1, &reg_read, 1, I2C_TIMEOUT)) != I2C_OK) {
return false;
}
int a = 0;
// // start the request reception process
// if((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, tx_buf, 1, I2C_TIMEOUT)) != I2C_OK)
// {
// return false;
// }
//
// // start the request reception process
// if((i2c_status = i2c_master_receive(&hi2cx, I2Cx_ADDRESS | 1, rx_buf, 1, I2C_TIMEOUT)) != I2C_OK)
// {
// return false;
// }
// // start the request reception process
// if((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, tx_buf, 2, I2C_TIMEOUT)) != I2C_OK)
// {
// return false;
// }
// // start the request reception process
// if((i2c_status = i2c_master_transmit(&hi2cx, I2Cx_ADDRESS | 0, tx_buf, 1, I2C_TIMEOUT)) != I2C_OK)
// {
// return false;
// }
//
// // start the request reception process
// if((i2c_status = i2c_master_receive(&hi2cx, I2Cx_ADDRESS | 1, rx_buf, 1, I2C_TIMEOUT)) != I2C_OK)
// {
// return false;
// }
/*
// start the request reception process
if((i2c_status = i2c_smbus_master_transmit(&hi2cx, I2Cx_ADDRESS | 1, tx_buf, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
{
return false;
}
// start the request reception process
if((i2c_status = i2c_smbus_master_receive(&hi2cx, I2Cx_ADDRESS | 1, rx_buf, BUF_SIZE, I2C_TIMEOUT)) != I2C_OK)
{
return false;
}
*/
return true;
}