1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
/*
* Copyright : (C) 2022 Phytium Information Technology, Inc.
* All Rights Reserved.
*
* This program is OPEN SOURCE software: you can redistribute it and/or modify it
* under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
* either version 1.0 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Phytium Public License for more details.
*
*
* FilePath: fi2c.c
* Date: 2022-02-10 14:53:42
* LastEditTime: 2022-02-18 08:36:58
* Description: This files is for
*
* Modify History:
* Ver Who Date Changes
* ----- ------ -------- --------------------------------------
*/
/***************************** Include Files *********************************/
#include <string.h>
#include "fio.h"
#include "ferror_code.h"
#include "ftypes.h"
#include "fdebug.h"
#include "fi2c_hw.h"
#include "fi2c.h"
/************************** Constant Definitions *****************************/
/**************************** Type Definitions *******************************/
/***************** Macros (Inline Functions) Definitions *********************/
#define FI2C_DEBUG_TAG "I2C"
#define FI2C_ERROR(format, ...) FT_DEBUG_PRINT_E(FI2C_DEBUG_TAG, format, ##__VA_ARGS__)
#define FI2C_INFO(format, ...) FT_DEBUG_PRINT_I(FI2C_DEBUG_TAG, format, ##__VA_ARGS__)
#define FI2C_DEBUG(format, ...) FT_DEBUG_PRINT_D(FI2C_DEBUG_TAG, format, ##__VA_ARGS__)
/************************** Function Prototypes ******************************/
static FError FI2cReset(FI2c *instance_p);
/************************** Variable Definitions *****************************/
static const char *FI2C_ERROR_CODE_MSG[FI2C_NUM_OF_ERR_CODE] =
{
"FI2C_SUCCESS : fi2c success",
"FI2C_ERR_INVAL_PARM : fi2c invalid input parameters",
"FI2C_ERR_NOT_READY : fi2c driver not ready",
"FI2C_ERR_TIMEOUT : fi2c wait timeout",
"FI2C_ERR_NOT_SUPPORT : fi2c non support operation",
"FI2C_ERR_INVAL_STATE : fi2c invalid state"
};
/*****************************************************************************/
/* 此文件主要为了完成用户对外接口,用户可以使用这些接口直接开始工作 */
/* - 包括用户API的定义和实现
- 同时包含必要的OPTION方法,方便用户进行配置
- 如果驱动可以直接进行I/O操作,在此源文件下可以将API 进行实现 */
/**
* @name: FI2cCfgInitialize
* @msg: 完成I2C驱动实例的初始化,使之可以使用
* @param {FI2c} *instance_p I2C驱动实例数据
* @param {FI2cConfig} *cofig_p I2C驱动配置数据
* @return SUCCESS if initialization was successful
* ERROR
*/
FError FI2cCfgInitialize(FI2c *instance_p, const FI2cConfig *input_config_p)
{
FASSERT(instance_p && input_config_p);
FError ret = FI2C_SUCCESS;
/*
* If the device is started, disallow the initialize and return a Status
* indicating it is started. This allows the user to de-initialize the device
* and reinitialize, but prevents a user from inadvertently
* initializing.
*/
if (FT_COMPONENT_IS_READY == instance_p->is_ready)
{
FI2C_ERROR("device is already initialized!!!");
return FI2C_ERR_INVAL_STATE;
}
/*
* Set default values and configuration data, including setting the
* callback handlers to stubs so the system will not crash should the
* application not assign its own callbacks.
*/
FI2cDeInitialize(instance_p);
instance_p->config = *input_config_p;
/*
* Reset the device.
*/
ret = FI2cReset(instance_p);
if (FI2C_SUCCESS == ret)
{
instance_p->is_ready = FT_COMPONENT_IS_READY;
}
return ret;
}
/**
* @name: FI2cDeInitialize
* @msg: 完成I2C驱动实例去使能,清零实例数据
* @return {*}
* @param {FI2c} *instance_p
*/
void FI2cDeInitialize(FI2c *instance_p)
{
FASSERT(instance_p);
instance_p->is_ready = 0;
memset(instance_p, 0, sizeof(*instance_p));
}
/**
* @name: FI2cReset
* @msg: 重置I2C控制器
* @return {*}
* @param {FI2c} *instance_p, I2C驱动实例数据
*/
static FError FI2cReset(FI2c *instance_p)
{
FASSERT(instance_p);
FError ret = FI2C_SUCCESS;
FI2cConfig *config_p = &instance_p->config;
uintptr base_addr = config_p->base_addr;
u32 reg_val = 0;
ret = FI2cSetEnable(base_addr, FALSE); /* disable i2c ctrl */
if (FI2C_MASTER == config_p->work_mode)
{
reg_val |= (config_p->use_7bit_addr) ? FI2C_CON_MASTER_ADR_7BIT : FI2C_CON_MASTER_ADR_10BIT ;
reg_val |= FI2C_CON_SLAVE_DISABLE;
reg_val |= FI2C_CON_MASTER_MODE;
reg_val |= FI2C_CON_RESTART_EN;
}
else
{
reg_val |= (config_p->use_7bit_addr) ? FI2C_CON_SLAVE_ADR_7BIT : FI2C_CON_SLAVE_ADR_10BIT;
reg_val &= (~FI2C_CON_MASTER_MODE);
reg_val |= FI2C_CON_SLAVE_MODE;
}
reg_val |= FI2C_CON_STD_SPEED;
FI2C_WRITE_REG32(base_addr, FI2C_CON_OFFSET, reg_val);
FI2C_WRITE_REG32(base_addr, FI2C_RX_TL_OFFSET, 0);
FI2C_WRITE_REG32(base_addr, FI2C_TX_TL_OFFSET, 0);
FI2C_SET_INTRRUPT_MASK(base_addr, 0); /* disable all intr */
ret = FI2cSetSpeed(base_addr, config_p->speed_rate);
if (FI2C_SUCCESS == ret)
ret = FI2cSetEnable(base_addr, TRUE); /* enable i2c ctrl */
/* if init successed, and i2c is in slave mode, set slave address */
if ((FI2C_SUCCESS == ret) && (FI2C_SLAVE == config_p->work_mode))
ret = FI2cSetSar(base_addr, config_p->slave_addr);
return ret;
}
/**
* @name: FI2cErrorToMessage
* @msg: 获取I2C模块错误码对应的错误信息
* @return {const char *}, 错误码信息,NULL表示失败
* @param {FError} error, I2C输入错误码
*/
const char *FI2cErrorToMessage(FError error)
{
const char *msg = NULL;
if (FI2C_SUCCESS != error && (FI2C_ERR_CODE_PREFIX != error & (FT_ERRCODE_SYS_MODULE_MASK | FT_ERRCODE_SUB_MODULE_MASK)))
{
/* if input error do not belong to this module */
return msg;
}
u32 index = error & FT_ERRCODE_TAIL_VALUE_MASK;
if (index < FI2C_NUM_OF_ERR_CODE)
{
msg = FI2C_ERROR_CODE_MSG[index];
}
return msg;
}
|