forked from igrr/esp32-cam-demo
-
Notifications
You must be signed in to change notification settings - Fork 4
/
sccb.c
80 lines (70 loc) · 1.74 KB
/
sccb.c
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
/*
* This file is part of the OpenMV project.
* Copyright (c) 2013/2014 Ibrahim Abdelkader <[email protected]>
* This work is licensed under the MIT license, see the file LICENSE for details.
*
* SCCB (I2C like) driver.
*
*/
#include <stdbool.h>
#include "wiring.h"
#include "sccb.h"
#include "twi.h"
#include <stdio.h>
#define SCCB_FREQ (100000) // We don't need fast I2C. 100KHz is fine here.
#define TIMEOUT (1000) /* Can't be sure when I2C routines return. Interrupts
while polling hardware may result in unknown delays. */
int SCCB_Init(int pin_sda, int pin_scl)
{
twi_init(pin_sda, pin_scl);
return 0;
}
uint8_t SCCB_Probe()
{
uint8_t reg = 0x00;
uint8_t slv_addr = 0x00;
for (uint8_t i=0; i<127; i++) {
if (twi_writeTo(i, ®, 1, true) == 0) {
slv_addr = i;
break;
}
if (i!=126) {
systick_sleep(1); // Necessary for OV7725 camera (not for OV2640).
}
}
return slv_addr;
}
uint8_t SCCB_Read(uint8_t slv_addr, uint8_t reg)
{
uint8_t data=0;
__disable_irq();
int rc = twi_writeTo(slv_addr, ®, 1, true);
if (rc != 0) {
data = 0xff;
}
else {
rc = twi_readFrom(slv_addr, &data, 1, true);
if (rc != 0) {
data=0xFF;
}
}
__enable_irq();
if (rc != 0) {
printf("SCCB_Read [%02x] failed rc=%d\n", reg, rc);
}
return data;
}
uint8_t SCCB_Write(uint8_t slv_addr, uint8_t reg, uint8_t data)
{
uint8_t ret=0;
uint8_t buf[] = {reg, data};
__disable_irq();
if(twi_writeTo(slv_addr, buf, 2, true) != 0) {
ret=0xFF;
}
__enable_irq();
if (ret != 0) {
printf("SCCB_Write [%02x]=%02x failed\n", reg, data);
}
return ret;
}