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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#include "TW9990.h"
#include "dr7f701441.dvf.h"
#include "GPIO.h"
#include "r_dev_api.h"
#include "Watchdog.h"
#include "r_gpio_api.h"
/*延时时间us*/
#define Delayus 6
//18
/* 将I2C0的SDA设置为输入*/
static void TW9990_SDA_IN(void)
{
r_dev_PinConfig_t port_config;
port_config.Port = 44; /* port number*/
port_config.Pin = 10; /* pin number*/
port_config.Func = 0; /* pin function 0: GPIO, 1-4 alternative function*/
port_config.Dir = R_DEV_PIN_IN; /* pin direction, see <r_dev_PinDirection_t>*/
port_config.Feedback = 1; /* 1: physical (output) pin level provided to input register, 0: disabled*/
port_config.Pull = R_DEV_PIN_PULLNO; /* internal pull up configuration, see <r_dev_PinPull_t>*/
port_config.OpenDrain = 0; /* 1: open drain emulation, 0: push-pull*/
port_config.CurrLimit = 1; /* 1: high level current limitation, 0: low level current limitation*/
port_config.HiSpeed = 1; /* 1: Speed limitation disabled (drive strength control off), 0: Speed limit enabled*/
port_config.InputType = R_DEV_PIN_CMOS1; /* possible types of input characteristics, see <r_dev_PinInput_t>*/
R_DEV_PinConfig(&port_config);
}
/* 将I2C0的SDA设置为输出*/
static void TW9990_SDA_OUT(void)
{
r_dev_PinConfig_t port_config;
port_config.Port = 44; /* port number */
port_config.Pin = 10; /* pin number */
port_config.Func = 0; /* pin function 0: GPIO, 1-4 alternative function*/
port_config.Dir = R_DEV_PIN_OUT; /* pin direction, see <r_dev_PinDirection_t>*/
port_config.Feedback = 1; /* 1: physical (output) pin level provided to input register, 0: disabled*/
port_config.Pull = R_DEV_PIN_PULLNO; /* internal pull up configuration, see <r_dev_PinPull_t>*/
port_config.OpenDrain = 0; /* 1: open drain emulation, 0: push-pull*/
port_config.CurrLimit = 1; /* 1: high level current limitation, 0: low level current limitation */
port_config.HiSpeed = 1; /* 1: Speed limitation disabled (drive strength control off), 0: Speed limit enabled */
port_config.InputType = R_DEV_PIN_CMOS1; /* possible types of input characteristics, see <r_dev_PinInput_t>*/
R_DEV_PinConfig(&port_config);
}
static void TW9990_RSTB_OUT(void)
{
r_dev_PinConfig_t port_config;
port_config.Port = 42; /* port number */
port_config.Pin = 9; /* pin number */
port_config.Func = 0; /* pin function 0: GPIO, 1-4 alternative function*/
port_config.Dir = R_DEV_PIN_OUT; /* pin direction, see <r_dev_PinDirection_t>*/
port_config.Feedback = 1; /* 1: physical (output) pin level provided to input register, 0: disabled*/
port_config.Pull = R_DEV_PIN_PULLNO; /* internal pull up configuration, see <r_dev_PinPull_t>*/
port_config.OpenDrain = 0; /* 1: open drain emulation, 0: push-pull*/
port_config.CurrLimit = 1; /* 1: high level current limitation, 0: low level current limitation */
port_config.HiSpeed = 1; /* 1: Speed limitation disabled (drive strength control off), 0: Speed limit enabled */
port_config.InputType = R_DEV_PIN_CMOS1; /* possible types of input characteristics, see <r_dev_PinInput_t>*/
R_DEV_PinConfig(&port_config);
}
uint8_t TW9990_SDA_GetValue(void)
{
uint8_t i;
i = R_GPIO_PinRead(44, 10);
return i;
}
void TW9990_IIC_Start(void)
{
TW9990_SDA_OUT();
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SDA_HIGH;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_HIGH;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SDA_LOW;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_LOW;
TW9990_IIC_DelayUs(300);
}
/*-------------------------------------------------------------------------
* Function Name : bsp_IIC_Stop
* Description : stop
* Input : None
* Output : None
* Return : None
* onther :
--------------------------------------------------------------------------*/
void TW9990_IIC_Stop(void)
{
TW9990_SDA_OUT();
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_LOW;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SDA_LOW;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_HIGH;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SDA_HIGH;
TW9990_IIC_DelayUs(300);
}
void TW9990_IIC_DelayUs(uint32_t time)
{
uint32_t i;
for (i = 0; i < (time *2); i++)
{
R_DEV_Nop();
WDT_Clear();
}
}
void TW9990_IIC_ACK(void)
{
TW9990_SDA_OUT();
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SDA_LOW;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_HIGH;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_LOW;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SDA_HIGH;
TW9990_IIC_DelayUs(Delayus);
}
void TW9990_IIC_NACK(void)
{
TW9990_SDA_OUT();
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SDA_HIGH;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_HIGH;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_LOW;
TW9990_IIC_DelayUs(Delayus);
}
uint8_t TW9990_IIC_WaitAck(void)
{
uint8_t ack = 0x01;
uint16_t i = IIC_WaitOverTime;
TW9990_SDA_IN();
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_HIGH;
TW9990_IIC_DelayUs(Delayus);
while ((ack) && (--i != 0))
{
ack = TW9990_SDA_GetValue();
WDT_Clear();
}
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_LOW;
TW9990_IIC_DelayUs(Delayus);
return (ack);
}
uint8_t TW9990_IIC_SendByte(uint8_t Data)
{
uint8_t temp;
uint8_t d = Data;
TW9990_SDA_OUT();
TW9990_IIC_DelayUs(Delayus);
for (temp = 8; temp != 0; temp--)
{
if (d & 0x80)
{
TW9990_IIC_SDA_HIGH;
}
else
{
TW9990_IIC_SDA_LOW;
}
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_HIGH;
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_LOW;
TW9990_IIC_DelayUs(Delayus);
d = d <<1;
TW9990_IIC_DelayUs(Delayus);
}
TW9990_IIC_DelayUs(Delayus);
return 1;
}
uint8_t TW9990_IIC_ReadByte(void)
{
uint8_t temp;
uint8_t rbyte = 0;
uint8_t SDA = 0;
TW9990_SDA_IN();
TW9990_IIC_DelayUs(Delayus);
for (temp = 8; temp != 0; temp--)
{
TW9990_IIC_SCL_HIGH;
TW9990_IIC_DelayUs(Delayus);
rbyte = rbyte << 1;
TW9990_IIC_DelayUs(Delayus);
SDA = TW9990_SDA_GetValue();
rbyte = rbyte | ((uint8_t)(SDA));
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SCL_LOW;
TW9990_IIC_DelayUs(Delayus);
}
return(rbyte);
}
uint8_t TW9990_IIC_WritePage(uint8_t PhyAdd, uint8_t MemAdd, uint8_t *Data, uint8_t len)
{
uint8_t b = 3;
uint8_t i;
uint8_t *OldData;
OldData = Data;
do
{
Data = OldData;
TW9990_IIC_Start();
TW9990_IIC_SendByte(PhyAdd);
if (TW9990_IIC_WaitAck())
{
TW9990_IIC_Stop();
b--;
if (b)
{
continue;
}
else
{
break;
}
}
TW9990_IIC_SendByte(MemAdd);
if (TW9990_IIC_WaitAck())
{
TW9990_IIC_Stop();
b--;
if (b)
{
continue;
}
else
{
break;
}
}
for (i = 0; i < len; i++)
{
TW9990_IIC_SendByte(*Data);
if (TW9990_IIC_WaitAck())
{
TW9990_IIC_Stop();
break;
}
Data++;
}
TW9990_IIC_Stop();
return 1;
} while (1);
return 0;
}
uint8_t TW9990_IIC_ReadPage(uint8_t PhyAdd, uint8_t MemAdd, uint8_t *Data, uint8_t len)
{
uint8_t b = 3;
uint8_t i;
do
{
TW9990_IIC_Start();
TW9990_IIC_SendByte(PhyAdd);
if (TW9990_IIC_WaitAck())
{
TW9990_IIC_Stop();
b--;
if (b)
{
continue;
}
else
{
break;
}
}
TW9990_IIC_SendByte(MemAdd);
if (TW9990_IIC_WaitAck())
{
TW9990_IIC_Stop();
b--;
if (b)
{
continue;
}
else
{
break;
}
}
TW9990_IIC_Start();
TW9990_IIC_SendByte(PhyAdd | 0x01);
if (TW9990_IIC_WaitAck())
{
TW9990_IIC_Stop();
b--;
if (b)
{
continue;
}
else
{
break;
}
}
for (i = 0 ; i < (len - 1); i++)
{
*Data = TW9990_IIC_ReadByte();
TW9990_IIC_ACK();
Data++;
}
*Data = TW9990_IIC_ReadByte();
TW9990_IIC_NACK();
TW9990_IIC_Stop();
return 1;
} while (1);
return 0;
}
static void WriteReg(uint8_t addr, uint8_t data)
{
TW9990_IIC_WritePage(0x8a, addr, &data, 1);
}
static uint8_t ReadReg(uint8_t addr)
{
uint8_t data;
TW9990_IIC_ReadPage(0x8a, addr, &data, 1);
return data;
}
static msm_camera_i2c_reg_conf_t TW9992_PAL_Int[] =
{
0x8a, 0x00, //for now is same as NTSC_Int
0x02, 0x40,
0x03, 0xa0,
0x12, 0x21,
0x1A, 0x0A,
0x20, 0xA0,
0x2C, 0x40,
0x35, 0xA0,
0xff, 0xff
};
void I2C_INIT(void)
{
DC12V_EN_MCU = 0;
VCC18_EN_MCU = 0;
TW9990_IIC_DelayUs(Delayus * 100);
DC12V_EN_MCU = 1; /* 24V转12V使能 */
TW9990_IIC_DelayUs(Delayus * 100);
VCC18_EN_MCU = 1; /* 5V转1.8V使能 */
TW9990_IIC_DelayUs(Delayus * 1000);
TW9990_RSTB = 0;
TW9990_IIC_DelayUs(Delayus * 1000);
TW9990_IIC_DelayUs(Delayus * 1000);
TW9990_RSTB = 1;
}
unsigned char TW9990_CCIR_NTSC_DataSet[] =
{
0x8a, 0x00, //for now is same as NTSC_Int
0x02, 0x40,
0x03, 0xa0,
0x12, 0x21,
0x1A, 0x0A,
0x20, 0xA0,
0x2C, 0x40,
0x35, 0xA0,
0x2B, 0x99, //20211027 增加(芯片厂技术支持说能增强抗干扰)
0x4C, 0xE0, //20211027 增加(芯片厂技术支持说能增强抗干扰)
0xff, 0xff
};
void I2CDeviceInitialize(unsigned char *RegSet)
{
unsigned char index, val;
RegSet += 2;
while (( RegSet[0] != 0xFF ) || ( RegSet[1] != 0xFF ))
{ // 0xff, 0xff is end of data
index = *RegSet;
val = *(RegSet + 1);
TW9990_IIC_WritePage(0x8a, index, &val, 1);
RegSet += 2;
}
}
void STBoff(void)
{
WriteReg(0xAF, 0x80);
WriteReg(0xB0, 0x00);
WriteReg(0x0F, 0x00);
//Printf("\n===== STB OFF =====");
}
//uint8_t EEE = 50;
void InitTW9990(void)
{
I2CDeviceInitialize(TW9990_CCIR_NTSC_DataSet);
//EEE = ReadReg(0x6f);
}
void TW9990_IIC_Sleep(void)
{
TW9990_SDA_OUT();
TW9990_IIC_DelayUs(Delayus);
TW9990_IIC_SDA_HIGH;
TW9990_IIC_SCL_HIGH;
}