134 lines
4.4 KiB
C
134 lines
4.4 KiB
C
#include "fram.h"
|
|
#include "string.h"
|
|
|
|
#include <stdio.h>
|
|
#include "esp_log.h"
|
|
#include "driver/i2c.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_system.h"
|
|
#include "sdkconfig.h"
|
|
#include "driver/gpio.h"
|
|
|
|
static const char *TAG = "i2c-simple-example";
|
|
|
|
#define I2C_MASTER_SCL_IO 1 /*!< GPIO number used for I2C master clock */
|
|
#define I2C_MASTER_SDA_IO 2 /*!< GPIO number used for I2C master data */
|
|
#define I2C_MASTER_NUM 0 /*!< I2C master i2c port number, the number of i2c peripheral interfaces available will depend on the chip */
|
|
#define I2C_MASTER_FREQ_HZ 400000 /*!< I2C master clock frequency */
|
|
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
|
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
|
|
#define I2C_MASTER_TIMEOUT_MS 1000
|
|
|
|
#define MPU9250_SENSOR_ADDR 0x68 /*!< Slave address of the MPU9250 sensor */
|
|
#define MPU9250_WHO_AM_I_REG_ADDR 0x75 /*!< Register addresses of the "who am I" register */
|
|
|
|
#define MPU9250_PWR_MGMT_1_REG_ADDR 0x6B /*!< Register addresses of the power managment register */
|
|
#define MPU9250_RESET_BIT 7
|
|
|
|
#define WRITE_BIT I2C_MASTER_WRITE
|
|
#define ACK_CHECK_EN 0x1
|
|
#define TAG "FREM"
|
|
|
|
/**
|
|
* @brief Read a sequence of bytes from a MPU9250 sensor registers
|
|
*/
|
|
// static esp_err_t mpu9250_register_read(uint8_t reg_addr, uint8_t *data, size_t len)
|
|
// {
|
|
// return i2c_master_write_read_device(I2C_MASTER_NUM, MPU9250_SENSOR_ADDR, ®_addr, 1, data, len, I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS);
|
|
// }
|
|
|
|
/**
|
|
* @brief Write a byte to a MPU9250 sensor register
|
|
*/
|
|
// static esp_err_t mpu9250_register_write_byte(uint8_t reg_addr, uint8_t data)
|
|
// {
|
|
// int ret;
|
|
// uint8_t write_buf[2] = {reg_addr, data};
|
|
|
|
// ret = i2c_master_write_to_device(I2C_MASTER_NUM, MPU9250_SENSOR_ADDR, write_buf, sizeof(write_buf), I2C_MASTER_TIMEOUT_MS / portTICK_RATE_MS);
|
|
|
|
// return ret;
|
|
// }
|
|
|
|
/**
|
|
* @brief i2c master initialization
|
|
*/
|
|
esp_err_t i2c_master_init(void)
|
|
{
|
|
int i2c_master_port = I2C_MASTER_NUM;
|
|
|
|
i2c_config_t conf = {
|
|
.mode = I2C_MODE_MASTER,
|
|
.sda_io_num = I2C_MASTER_SDA_IO,
|
|
.scl_io_num = I2C_MASTER_SCL_IO,
|
|
.sda_pullup_en = GPIO_PULLUP_ENABLE,
|
|
.scl_pullup_en = GPIO_PULLUP_ENABLE,
|
|
.master.clk_speed = I2C_MASTER_FREQ_HZ,
|
|
};
|
|
|
|
i2c_param_config(i2c_master_port, &conf);
|
|
|
|
return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
|
|
}
|
|
|
|
int fram_write(uint16_t addr, void * buf,uint32_t len)
|
|
{
|
|
esp_err_t ret;
|
|
uint8_t slave_addr = (0xA0) | ((addr >> 8) & 0x7);/*page select*/
|
|
uint8_t low_addr = addr &0xff;
|
|
// return HAL_I2C_Mem_Write(&hi2c1,slave_addr,addr&0xff,I2C_MEMADD_SIZE_8BIT,buf,len,200);
|
|
// ret = i2c_master_write_to_device(I2C_MASTER_NUM, slave_addr, txbuf, len+1, 10);
|
|
// // ret = i2c_master_write_read_device(I2C_MASTER_NUM, slave_addr, buf, len, 10);
|
|
// // assert(ret == ESP_OK);
|
|
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
|
|
i2c_master_start(cmd);
|
|
i2c_master_write_byte(cmd, slave_addr | WRITE_BIT, ACK_CHECK_EN);
|
|
i2c_master_write(cmd, &low_addr, 1, ACK_CHECK_EN);
|
|
i2c_master_write(cmd, buf, len, ACK_CHECK_EN);
|
|
i2c_master_stop(cmd);
|
|
ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 1000 / portTICK_PERIOD_MS);
|
|
if (ret != ESP_OK) {
|
|
printf("esp_err : %s \n", esp_err_to_name(ret));
|
|
return ret;
|
|
}
|
|
ESP_LOGI(TAG,"fram_write succeed\n");
|
|
i2c_cmd_link_delete(cmd);
|
|
return ret;
|
|
}
|
|
int fram_read(uint16_t addr,void * buf,uint32_t len)
|
|
{
|
|
// esp_err_t err = i2c_param_config(i2c_master_port, &conf);
|
|
|
|
esp_err_t ret;
|
|
uint8_t slave_addr = (0xA0>>1) | ((addr >> 8) & 0x7);/*page select*/
|
|
uint8_t low_addr = addr &0xff;
|
|
// return HAL_I2C_Mem_Read(&hi2c1,slave_addr,addr&0xff,I2C_MEMADD_SIZE_8BIT,buf,len,200);
|
|
ret = i2c_master_write_read_device(I2C_MASTER_NUM, slave_addr, &low_addr,1, buf, len, 10);
|
|
if (ret != ESP_OK) {
|
|
ESP_LOGE(TAG,"esp_err : %s \n", esp_err_to_name(ret));
|
|
return ret;
|
|
}
|
|
ESP_LOGI(TAG,"fram_read succeed\n");
|
|
// ESP_LOGI(TAG, "buf value : %d \n ", buf[0]);
|
|
return 0;
|
|
}
|
|
|
|
void save_para(void)
|
|
{
|
|
|
|
}
|
|
void save_encode(void)
|
|
{
|
|
|
|
}
|
|
void read_para()
|
|
{
|
|
|
|
}
|
|
|
|
void clear_para()
|
|
{
|
|
|
|
}
|