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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/**************************************************************************//**
*
* @copyright (C) 2020 Nuvoton Technology Corp. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2020-11-11 Wayne First version
*
******************************************************************************/
#include <rtthread.h>
#include "NuMicro.h"
#define ERROR rt_kprintf
#define RETRY_COUNT 3
/*---------------------------------------------------------------------------------------------------------*/
/* Global variables */
/*---------------------------------------------------------------------------------------------------------*/
static uint8_t g_u8DeviceAddr = 0xB0;
static uint8_t g_uPageNum;
static uint8_t g_u8RegAddr;
static uint8_t g_u8WriteData;
static volatile uint8_t g_u8RxData;
static volatile uint8_t g_u8DataLen;
static volatile uint8_t g_u8EndFlag = 0;
static void I2C0_MasterRx(uint32_t u32Status)
{
if (u32Status == 0x08)
{
/* START has been transmitted and prepare SLA+W */
I2C0->DAT = g_u8DeviceAddr | (g_uPageNum << 1);
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
}
else if (u32Status == 0x18)
{
/* SLA+W has been transmitted and ACK has been received */
I2C0->DAT = g_u8RegAddr;
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
}
else if (u32Status == 0x20)
{
/* SLA+W has been transmitted and NACK has been received */
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STA | I2C_CTL_STO | I2C_CTL_SI);
}
else if (u32Status == 0x28)
{
/* DATA has been transmitted and ACK has been received */
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STA | I2C_CTL_SI); // repeat start
}
else if (u32Status == 0x10)
{
/* Repeat START has been transmitted and prepare SLA+R */
I2C0->DAT = (g_u8DeviceAddr | (g_uPageNum << 1)) | 0x01;
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
}
else if (u32Status == 0x40)
{
/* SLA+R has been transmitted and ACK has been received */
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
}
else if (u32Status == 0x58)
{
/* DATA has been received and NACK has been returned */
g_u8RxData = I2C0->DAT;
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STO | I2C_CTL_SI);
g_u8EndFlag = 1;
}
else
{
ERROR("Status 0x%x is NOT processed\n", u32Status);
}
}
static void I2C0_MasterTx(uint32_t u32Status)
{
if (u32Status == 0x08)
{
/* START has been transmitted */
I2C0->DAT = g_u8DeviceAddr | (g_uPageNum << 1); /* Write SLA+W to Register I2CDAT */
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
}
else if (u32Status == 0x18)
{
/* SLA+W has been transmitted and ACK has been received */
I2C0->DAT = g_u8RegAddr;
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
}
else if (u32Status == 0x20)
{
/* SLA+W has been transmitted and NACK has been received */
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STA | I2C_CTL_STO | I2C_CTL_SI);
}
else if (u32Status == 0x28)
{
/* DATA has been transmitted and ACK has been received */
if ((g_u8DataLen == 0) && (g_u8EndFlag == 0))
{
I2C0->DAT = g_u8WriteData;
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_SI;
g_u8DataLen++;
}
else
{
g_u8DataLen = 0;
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | (I2C_CTL_STO | I2C_CTL_SI);
g_u8EndFlag = 1;
}
}
else
{
ERROR("Status 0x%x is NOT processed\n", u32Status);
}
}
static uint32_t ma35d1_write_i2c_data(uint32_t u32Addr, uint32_t u32Data)
{
uint32_t I2C_TIME_OUT_COUNT = 0x20000;
uint32_t u32Status;
uint32_t u32time_out = 0;
g_uPageNum = 0;
if (g_uPageNum == 0xff)
{
ERROR("\n address error !!\n");
return 0;
}
g_u8RegAddr = u32Addr;
g_u8WriteData = u32Data;
g_u8EndFlag = 0x0;
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_STA;
while (1)
{
if (I2C0->CTL0 & I2C_CTL_SI)
{
u32time_out = 0;
u32Status = I2C0->STATUS0;
I2C0_MasterTx(u32Status);
}
if (g_u8EndFlag)
{
break;
}
u32time_out++;
if (u32time_out > I2C_TIME_OUT_COUNT)
{
ERROR("i2c Write Time Out!\n");
return 0; // error
}
}
return 1;
}
static uint32_t ma35d1_read_i2c_data(uint32_t u32Addr, uint32_t *pu32Data)
{
uint32_t I2C_TIME_OUT_COUNT = 0x200000;
uint32_t u32Status;
uint32_t u32time_out = 0;
g_uPageNum = 0;
if (g_uPageNum == 0xff)
{
ERROR("\n address error !!\n");
return 0;
}
g_u8RegAddr = u32Addr;
g_u8EndFlag = 0x0;
I2C0->CTL0 = (I2C0->CTL0 & ~0x3c) | I2C_CTL_STA;
while (1)
{
if (I2C0->CTL0 & I2C_CTL_SI)
{
u32time_out = 0;
u32Status = I2C0->STATUS0;
I2C0_MasterRx(u32Status);
}
if (g_u8EndFlag)
{
break;
}
u32time_out++;
if (u32time_out > I2C_TIME_OUT_COUNT)
{
ERROR("i2c Read Time Out!\n");
return 1; // error
}
}
*pu32Data = g_u8RxData;
return 2;
}
static uint32_t ma35d1_read_pmic_data(uint32_t u32Addr, uint32_t *pu32Data)
{
uint32_t j = RETRY_COUNT;
while (j-- > 0)
{
if (ma35d1_read_i2c_data(u32Addr, pu32Data) == 2)
{
break;
}
}
if (j <= 0)
{
ERROR("\n READ ERROR! \n");
return 0;
}
return 1;
}
static uint32_t ma35d1_write_pmic_data(uint32_t u32Addr, uint32_t u32Data)
{
uint32_t j = RETRY_COUNT;
while (j-- > 0)
{
if (ma35d1_write_i2c_data(u32Addr, u32Data) == 1)
{
break;
}
}
if (j <= 0)
{
ERROR("\n WRITE ERROR [%d]! \n", j);
return 0;
}
return 1;
}
static void ma35d1_i2c0_init(uint32_t sys_clk)
{
unsigned long clk_rate;
uint32_t u32Div, speed;
outpw((void *)0x40460208, inpw((void *)0x40460208) | (0x3fff << 16)); // enable GPIO clock
outpw((void *)0x40460210, inpw((void *)0x40460210) | (0x1 << 0)); // I2C0 CLK
outpw(0x40460098, ((inpw(0x40460098) & ~0x0f000000) | (0x6 << 24))); // PD.6 I2C0_SDA
outpw(0x40460098, ((inpw(0x40460098) & ~0xf0000000) | (0x6 << 28))); // PD.7 I2C0_CLK
outpw(0x400400F0, 0x5 << 12); // pull high
/* i2c_clk = 100KHz */
clk_rate = sys_clk;
speed = 100 * 1000;
/* assume speed above 1000 are Hz-specified */
if (speed > 1000) speed = speed / 1000;
if (speed > 400) speed = 400;
u32Div = (uint32_t)(((clk_rate * 10U) / (speed * 4U) + 5U) / 10U - 1U);
I2C0->CLKDIV = u32Div;
I2C0->CTL0 = I2C0->CTL0 | I2C_CTL0_I2CEN_Msk; /* i2c enable */
}
uint32_t ma35d1_set_cpu_voltage(uint32_t sys_clk, uint32_t u32Vol)
{
/*
RegValue = (Voltage - 0.3) / 0.01
RegValue(hex) Voltage(v)
0x5B 1.22
0x5C 1.23
0x5D 1.24
0x5F 1.25
0x60 1.26
0x61 1.27
0x62 1.28
0x63 1.29
0x64 1.30
0x65 1.31
0x66 1.32
0x67 1.33
0x68 1.34
0x69 1.35
*/
uint32_t u32Data = 0;
if ((u32Vol < 0x59) || (u32Vol > 0x68))
return 0;
ma35d1_i2c0_init(sys_clk);
ma35d1_write_pmic_data(0xA4, u32Vol);
ma35d1_read_pmic_data(0xA4, &u32Data);
return (u32Data == u32Vol);
}
|