Sming Framework API
Sming - Open Source framework for high efficiency WiFi SoC ESP8266 native development with C++ language.
ArduCAM.h
1 /*
2  /*
3  ArduCAM.h - Arduino library support for CMOS Image Sensor
4  Copyright (C)2011-2015 ArduCAM.com. All right reserved
5 
6  Basic functionality of this library are based on the demo-code provided by
7  ArduCAM.com. You can find the latest version of the library at
8  http://www.ArduCAM.com
9 
10  Now supported controllers:
11  - OV7670
12  - MT9D111
13  - OV7675
14  - OV2640
15  - OV3640
16  - OV5642
17  - OV7660
18  - OV7725
19  - MT9M112
20  - MT9V111
21  - OV5640
22  - MT9M001
23  - MT9T112
24  - MT9D112
25 
26  We will add support for many other sensors in next release.
27 
28  Supported MCU platform
29  - Theoretically support all Arduino families
30  - Arduino UNO R3 (Tested)
31  - Arduino MEGA2560 R3 (Tested)
32  - Arduino Leonardo R3 (Tested)
33  - Arduino Nano (Tested)
34  - Arduino DUE (Tested)
35  - Arduino Yun (Tested)
36  - Raspberry Pi (Tested)
37  - ESP8266-12 (Tested)
38 
39  If you make any modifications or improvements to the code, I would appreciate
40  that you share the code with me so that I might include it in the next release.
41  I can be contacted through http://www.ArduCAM.com
42 
43  This library is free software; you can redistribute it and/or
44  modify it under the terms of the GNU Lesser General Public
45  License as published by the Free Software Foundation; either
46  version 2.1 of the License, or (at your option) any later version.
47 
48  This library is distributed in the hope that it will be useful,
49  but WITHOUT ANY WARRANTY; without even the implied warranty of
50  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
51  Lesser General Public License for more details.
52 
53  You should have received a copy of the GNU Lesser General Public
54  License along with this library; if not, write to the Free Software
55  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
56 */
57 
58 /*------------------------------------
59  Revision History:
60  2012/09/20 V1.0.0 by Lee first release
61  2012/10/23 V1.0.1 by Lee Resolved some timing issue for the Read/Write Register
62  2012/11/29 V1.1.0 by Lee Add support for MT9D111 sensor
63  2012/12/13 V1.2.0 by Lee Add support for OV7675 sensor
64  2012/12/28 V1.3.0 by Lee Add support for OV2640,OV3640,OV5642 sensors
65  2013/02/26 V2.0.0 by Lee New Rev.B shield hardware, add support for FIFO control
66  and support Mega1280/2560 boards
67  2013/05/28 V2.1.0 by Lee Add support all drawing functions derived from UTFT library
68  2013/08/24 V3.0.0 by Lee Support ArudCAM shield Rev.C hardware, features SPI interface and low power mode.
69  Support almost all series of Arduino boards including DUE.
70  2014/03/09 V3.1.0 by Lee Add the more impressive example sketches.
71  Optimise the OV5642 settings, improve image quality.
72  Add live preview before JPEG capture.
73  Add play back photos one by one after BMP capture.
74  2014/05/01 V3.1.1 by Lee Minor changes to add support Arduino IDE for linux distributions.
75  2014/09/30 V3.2.0 by Lee Improvement on OV5642 camera dirver.
76  2014/10/06 V3.3.0 by Lee Add OV7660,OV7725 camera support.
77  2015/02/27 V3.4.0 by Lee Add the support for Arduino Yun board, update the latest UTFT library for ArduCAM.
78  2015/06/09 V3.4.1 by Lee Minor changes and add some comments
79  2015/06/19 V3.4.2 by Lee Add support for MT9M112 camera.
80  2015/06/20 V3.4.3 by Lee Add support for MT9V111 camera.
81  2015/06/22 V3.4.4 by Lee Add support for OV5640 camera.
82  2015/06/22 V3.4.5 by Lee Add support for MT9M001 camera.
83  2015/08/05 V3.4.6 by Lee Add support for MT9T112 camera.
84  2015/08/08 V3.4.7 by Lee Add support for MT9D112 camera.
85  2015/09/20 V3.4.8 by Lee Add support for ESP8266 processor.
86 --------------------------------------*/
87 
88 
89 #ifndef ArduCAM_H
90 #define ArduCAM_H
91 
92 #include "Arduino.h"
93 #include <pins_arduino.h>
94 
95 #if defined (__AVR__)
96 #define cbi(reg, bitmask) *reg &= ~bitmask
97 #define sbi(reg, bitmask) *reg |= bitmask
98 #define pulse_high(reg, bitmask) sbi(reg, bitmask); cbi(reg, bitmask);
99 #define pulse_low(reg, bitmask) cbi(reg, bitmask); sbi(reg, bitmask);
100 
101 #define cport(port, data) port &= data
102 #define sport(port, data) port |= data
103 
104 #define swap(type, i, j) {type t = i; i = j; j = t;}
105 
106 #define fontbyte(x) pgm_read_byte(&cfont.font[x])
107 
108 #define regtype volatile uint8_t
109 #define regsize uint8_t
110 
111 #endif
112 
113 #if defined(__arm__)
114 
115 #define cbi(reg, bitmask) *reg &= ~bitmask
116 #define sbi(reg, bitmask) *reg |= bitmask
117 #define pulse_high(reg, bitmask) sbi(reg, bitmask); cbi(reg, bitmask);
118 #define pulse_low(reg, bitmask) cbi(reg, bitmask); sbi(reg, bitmask);
119 
120 #define cport(port, data) port &= data
121 #define sport(port, data) port |= data
122 
123 #define swap(type, i, j) {type t = i; i = j; j = t;}
124 
125 #define fontbyte(x) cfont.font[x]
126 
127 #define regtype volatile uint32_t
128 #define regsize uint32_t
129 #define PROGMEM
130 
131 
132  #define pgm_read_byte(x) (*((char *)x))
133 // #define pgm_read_word(x) (*((short *)(x & 0xfffffffe)))
134  #define pgm_read_word(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x)))
135  #define pgm_read_byte_near(x) (*((char *)x))
136  #define pgm_read_byte_far(x) (*((char *)x))
137 // #define pgm_read_word_near(x) (*((short *)(x & 0xfffffffe))
138 // #define pgm_read_word_far(x) (*((short *)(x & 0xfffffffe)))
139  #define pgm_read_word_near(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x)))
140  #define pgm_read_word_far(x) ( ((*((unsigned char *)x + 1)) << 8) + (*((unsigned char *)x))))
141  #define PSTR(x) x
142  #if defined F
143  #undef F
144  #endif
145  #define F(X) (X)
146 
147 
148 #endif
149 
150 #if defined(ESP8266) || defined(__ESP8266_EX__)
151 
152 #define cbi(reg, bitmask) digitalWrite(bitmask, LOW)
153 #define sbi(reg, bitmask) digitalWrite(bitmask, HIGH)
154 #define pulse_high(reg, bitmask) sbi(reg, bitmask); cbi(reg, bitmask);
155 #define pulse_low(reg, bitmask) cbi(reg, bitmask); sbi(reg, bitmask);
156 
157 #define cport(port, data) port &= data
158 #define sport(port, data) port |= data
159 
160 #define swap(type, i, j) {type t = i; i = j; j = t;}
161 
162 #define fontbyte(x) cfont.font[x]
163 
164 #define regtype volatile uint32_t
165 #define regsize uint32_t
166 
167 #endif
168 
169 /****************************************************/
170 /* Sensor related definition */
171 /****************************************************/
172 #define BMP 0
173 #define JPEG 1
174 
175 #define OV7670 0
176 #define MT9D111_A 1
177 #define OV7675 2
178 #define OV5642 3
179 #define OV3640 4
180 #define OV2640 5
181 #define OV9655 6
182 #define MT9M112 7
183 #define OV7725 8
184 #define OV7660 9
185 #define MT9M001 10
186 #define OV5640 11
187 #define MT9D111_B 12
188 #define OV9650 13
189 #define MT9V111 14
190 #define MT9T112 15
191 #define MT9D112 16
192 
193 #define OV2640_160x120 0 //160x120
194 #define OV2640_176x144 1 //176x144
195 #define OV2640_320x240 2 //320x240
196 #define OV2640_352x288 3 //352x288
197 #define OV2640_640x480 4 //640x480
198 #define OV2640_800x600 5 //800x600
199 #define OV2640_1024x768 6 //1024x768
200 #define OV2640_1280x1024 7 //1280x1024
201 #define OV2640_1600x1200 8 //1600x1200
202 
203 #define OV5642_320x240 1 //320x240
204 #define OV5642_640x480 2 //640x480
205 #define OV5642_1280x720 3 //1280x720
206 #define OV5642_1920x1080 4 //1920x1080
207 #define OV5642_2048x1563 5 //2048x1563
208 #define OV5642_2592x1944 6 //2592x1944
209 
210 /****************************************************/
211 /* I2C Control Definition */
212 /****************************************************/
213 #define I2C_ADDR_8BIT 0
214 #define I2C_ADDR_16BIT 1
215 #define I2C_REG_8BIT 0
216 #define I2C_REG_16BIT 1
217 #define I2C_DAT_8BIT 0
218 #define I2C_DAT_16BIT 1
219 
220 /* Register initialization tables for SENSORs */
221 /* Terminating list entry for reg */
222 #define SENSOR_REG_TERM_8BIT 0xFF
223 #define SENSOR_REG_TERM_16BIT 0xFFFF
224 /* Terminating list entry for val */
225 #define SENSOR_VAL_TERM_8BIT 0xFF
226 #define SENSOR_VAL_TERM_16BIT 0xFFFF
227 
228 /****************************************************/
229 /* ArduChip related definition */
230 /****************************************************/
231 #define RWBIT 0x80 //READ AND WRITE BIT IS BIT[7]
232 
233 #define ARDUCHIP_TEST1 0x00 //TEST register
234 #define ARDUCHIP_TEST2 0x01 //TEST register
235 
236 #define ARDUCHIP_FRAMES 0x01 //Bit[2:0]Number of frames to be captured
237 
238 #define ARDUCHIP_MODE 0x02 //Mode register
239 #define MCU2LCD_MODE 0x00
240 #define CAM2LCD_MODE 0x01
241 #define LCD2MCU_MODE 0x02
242 
243 #define ARDUCHIP_TIM 0x03 //Timming control
244 #define HREF_LEVEL_MASK 0x01 //0 = High active , 1 = Low active
245 #define VSYNC_LEVEL_MASK 0x02 //0 = High active , 1 = Low active
246 #define LCD_BKEN_MASK 0x04 //0 = Enable, 1 = Disable
247 #define DELAY_MASK 0x08 //0 = no delay, 1 = delay one clock
248 #define MODE_MASK 0x10 //0 = LCD mode, 1 = FIFO mode
249 #define FIFO_PWRDN_MASK 0x20 //0 = Normal operation, 1 = FIFO power down
250 #define LOW_POWER_MODE 0x40 //0 = Normal mode, 1 = Low power mode
251 
252 #define ARDUCHIP_FIFO 0x04 //FIFO and I2C control
253 #define FIFO_CLEAR_MASK 0x01
254 #define FIFO_START_MASK 0x02
255 #define FIFO_RDPTR_RST_MASK 0x10
256 #define FIFO_WRPTR_RST_MASK 0x20
257 
258 #define ARDUCHIP_GPIO 0x06 //GPIO Write Register
259 #define GPIO_RESET_MASK 0x01 //0 = default state, 1 = Sensor reset IO value
260 #define GPIO_PWDN_MASK 0x02 //0 = Sensor power down IO value, 1 = Sensor power enable IO value
261 
262 #define BURST_FIFO_READ 0x3C //Burst FIFO read operation
263 #define SINGLE_FIFO_READ 0x3D //Single FIFO read operation
264 
265 #define ARDUCHIP_REV 0x40 //ArduCHIP revision
266 #define VER_LOW_MASK 0x3F
267 #define VER_HIGH_MASK 0xC0
268 
269 #define ARDUCHIP_TRIG 0x41 //Trigger source
270 #define VSYNC_MASK 0x01
271 #define SHUTTER_MASK 0x02
272 #define CAP_DONE_MASK 0x08
273 
274 #define FIFO_SIZE1 0x42 //Camera write FIFO size[7:0] for burst to read
275 #define FIFO_SIZE2 0x43 //Camera write FIFO size[15:8]
276 #define FIFO_SIZE3 0x44 //Camera write FIFO size[18:16]
277 
278 
279 /****************************************************/
280 
281 
282 /****************************************************************/
283 /* define a structure for sensor register initialization values */
284 /****************************************************************/
285 struct sensor_reg {
286  uint16_t reg;
287  uint16_t val;
288 };
289 
290 class ArduCAM
291 {
292  public:
293  ArduCAM();
294  ArduCAM(byte model,int CS);
295  void InitCAM();
296 
297  void CS_HIGH(void);
298  void CS_LOW(void);
299 
300  void flush_fifo(void);
301  void start_capture(void);
302  void clear_fifo_flag(void);
303  uint8_t read_fifo(void);
304 
305  uint8_t read_reg(uint8_t addr);
306  void write_reg(uint8_t addr, uint8_t data);
307 
308  uint32_t read_fifo_length(void);
309  void set_fifo_burst(void);
310  void set_bit(uint8_t addr, uint8_t bit);
311  void clear_bit(uint8_t addr, uint8_t bit);
312  uint8_t get_bit(uint8_t addr, uint8_t bit);
313  void set_mode(uint8_t mode);
314 
315  int wrSensorRegs(const struct sensor_reg*);
316  int wrSensorRegs8_8(const struct sensor_reg*);
317  int wrSensorRegs8_16(const struct sensor_reg*);
318  int wrSensorRegs16_8(const struct sensor_reg*);
319  int wrSensorRegs16_16(const struct sensor_reg*);
320 
321  byte wrSensorReg(int regID, int regDat);
322  byte wrSensorReg8_8(int regID, int regDat);
323  byte wrSensorReg8_16(int regID, int regDat);
324  byte wrSensorReg16_8(int regID, int regDat);
325  byte wrSensorReg16_16(int regID, int regDat);
326 
327  byte rdSensorReg8_8(uint8_t regID, uint8_t* regDat);
328  byte rdSensorReg16_8(uint16_t regID, uint8_t* regDat);
329  byte rdSensorReg8_16(uint8_t regID, uint16_t* regDat);
330  byte rdSensorReg16_16(uint16_t regID, uint16_t* regDat);
331 
332  void OV2640_set_JPEG_size(uint8_t size);
333  void OV5642_set_JPEG_size(uint8_t size);
334  void set_format(byte fmt);
335  byte get_format();
336 
337  int bus_write(int address, int value);
338  uint8_t bus_read(int address);
339  protected:
340  regtype *P_CS;
341  regsize B_CS;
342  byte m_fmt;
343  byte sensor_model;
344  byte sensor_addr;
345 
346 };
347 
348 #endif
Definition: ArduCAM.h:285
Definition: ArduCAM.h:290