/ components / micropython / port / src / omv / gc0328.c
gc0328.c
   1  /* Copyright 2018 Canaan Inc.
   2   *
   3   * Licensed under the Apache License, Version 2.0 (the "License");
   4   * you may not use this file except in compliance with the License.
   5   * You may obtain a copy of the License at
   6   *
   7   *     http://www.apache.org/licenses/LICENSE-2.0
   8   *
   9   * Unless required by applicable law or agreed to in writing, software
  10   * distributed under the License is distributed on an "AS IS" BASIS},
  11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12   * See the License for the specific language governing permissions and
  13   * limitations under the License.
  14   */
  15  #include <stdio.h>
  16  #include "gc0328.h"
  17  #include "dvp.h"
  18  #include "plic.h"
  19  #include "sleep.h"
  20  #include "sensor.h"
  21  #include "mphalport.h"
  22  #include "cambus.h"
  23  #include "printf.h"
  24  
  25  enum
  26  {
  27      GC0328_RGB_Gamma_m0 = 0,
  28      GC0328_RGB_Gamma_m1,
  29      GC0328_RGB_Gamma_m2,
  30      GC0328_RGB_Gamma_m3,
  31      GC0328_RGB_Gamma_m4,
  32      GC0328_RGB_Gamma_m5,
  33      GC0328_RGB_Gamma_m6,
  34      GC0328_RGB_Gamma_night,
  35  	GC0328_RGB_Gamma_cap,
  36  	GC0328_RGB_Gamma_test
  37  };
  38  
  39  enum
  40  {
  41  	GC0328_Y_Gamma_default,
  42  	GC0328_Y_Gamma_10,
  43      GC0328_Y_Gamma_09,
  44      GC0328_Y_Gamma_08,
  45      GC0328_Y_Gamma_07,
  46      GC0328_Y_Gamma_06,
  47      GC0328_Y_Gamma_05,
  48  
  49  };
  50  
  51  #define GC0328_RGB_Gamma GC0328_RGB_Gamma_test
  52  #define GC0328_Y_Gamma  GC0328_Y_Gamma_default
  53  
  54  /** The default register settings**/
  55  uint8_t sensor_default_regs[][2] = {
  56  	{0xfe , 0x80},  //reset
  57  	{0xfe , 0x80},  //reset
  58  	{0xfc , 0x16},  //set digital clock
  59  	{0xfc , 0x16},  //set digital clock
  60  	{0xfc , 0x16},  //set digital clock
  61  	{0xfc , 0x16},  //set digital clock
  62  
  63  	{0xfe , 0x00},  //select page0
  64  	{0x4f , 0x00},  //AEC disable
  65  	{0x42 , 0x00},  //ABW disable
  66  	{0x03 , 0x00},  //clear Exposure time MSB[11:8]
  67  	{0x04 , 0xc0},  //clear Exposure time LSB[7:0]
  68  	{0x77 , 0x62},  //set AWB red gain
  69  	{0x78 , 0x40},  //set AWB green gain 
  70  	{0x79 , 0x4d},  //set AWB blue gain
  71  
  72  	{0x05, 0x00}, 	
  73  	{0x06, 0x80},
  74  	{0x07, 0x00},
  75  	{0x08, 0x10},
  76  
  77  	{0xfe, 0x01},
  78  	{0x29, 0x00},
  79  	{0x2a, 0x96},
  80  
  81  	{0x2b, 0x02},
  82  	{0x2c, 0x00},
  83  	{0x2d, 0x02},
  84  	{0x2e, 0x00},
  85  	{0x2f, 0x02},
  86  	{0x30, 0x00},
  87  	{0x31, 0x02},
  88  	{0x32, 0x00},
  89  	{0xfe, 0x00},
  90  
  91  	{0xfe , 0x01},  
  92  	{0x4f , 0x00},  
  93  	{0x4c , 0x01},  
  94  	{0xfe , 0x00},  
  95  	//////////////////////////////
  96  	///////////AWB///////////
  97  	////////////////////////////////
  98  	{0xfe , 0x01},
  99  	{0x51 , 0x80},
 100  	{0x52 , 0x12},
 101  	{0x53 , 0x80},
 102  	{0x54 , 0x60},
 103  	{0x55 , 0x01},
 104  	{0x56 , 0x06},
 105  	{0x5b , 0x02},
 106  	{0x61 , 0xdc},
 107  	{0x62 , 0xdc},
 108  	{0x7c , 0x71},
 109  	{0x7d , 0x00},
 110  	{0x76 , 0x00},
 111  	{0x79 , 0x20},
 112  	{0x7b , 0x00},  
 113  	{0x70 , 0xFF},
 114  	{0x71 , 0x00},
 115  	{0x72 , 0x10},
 116  	{0x73 , 0x40},
 117  	{0x74 , 0x40},
 118  
 119  	{0x50 , 0x00},
 120  	{0xfe , 0x01},  
 121  	{0x4f , 0x00},  
 122  	{0x4c , 0x01},  
 123  	{0x4f , 0x00},  
 124  	{0x4f , 0x00},  
 125  	{0x4f , 0x00},  
 126  	{0x4d , 0x36},  
 127  	{0x4e , 0x02},  
 128  	{0x4e , 0x02},  
 129  	{0x4d , 0x44},  
 130  	{0x4e , 0x02},
 131  	{0x4e , 0x02},
 132  	{0x4e , 0x02},  
 133  	{0x4e , 0x02},  
 134  	{0x4d , 0x53},  
 135  	{0x4e , 0x08},  
 136  	{0x4e , 0x08},  
 137  	{0x4e , 0x02},  
 138  	{0x4d , 0x63},  
 139  	{0x4e , 0x08},  
 140  	{0x4e , 0x08},  
 141  	{0x4d , 0x73},  
 142  	{0x4e , 0x20},  
 143  	{0x4d , 0x83},  
 144  	{0x4e , 0x20},  
 145  	{0x4f , 0x01},  
 146  
 147  	{0x50 , 0x88},
 148  	{0xfe , 0x00},  
 149  
 150  	////////////////////////////////////////////////
 151  	////////////     BLK      //////////////////////
 152  	////////////////////////////////////////////////
 153  	{0x27 , 0x00},  
 154  	{0x2a , 0x40},  
 155  	{0x2b , 0x40},  
 156  	{0x2c , 0x40},  
 157  	{0x2d , 0x40},  
 158  
 159  
 160  	//////////////////////////////////////////////
 161  	////////// page  0    ////////////////////////
 162  	//////////////////////////////////////////////
 163  	{0xfe , 0x00},  
 164  	{0x0d , 0x01},  
 165  	{0x0e , 0xe8},  
 166  	{0x0f , 0x02},  
 167  	{0x10 , 0x88},  
 168  	{0x09 , 0x00},  
 169  	{0x0a , 0x00},  
 170  	{0x0b , 0x00},  
 171  	{0x0c , 0x00},  
 172  	{0x16 , 0x00},
 173  	{0x17 , 0x14},
 174  	{0x18 , 0x0e},  
 175  	{0x19 , 0x06},  
 176  
 177  	{0x1b , 0x48},  
 178  	{0x1f , 0xC8},  
 179  	{0x20 , 0x01},  
 180  	{0x21 , 0x78},  
 181  	{0x22 , 0xb0},  
 182  	{0x23 , 0x04}, 
 183  	{0x24 , 0x11},  //output pin abilityof driver config
 184  	{0x26 , 0x00},  
 185  
 186  
 187  	//crop mode | It should be disable
 188  	{0x50 , 0x01}, 
 189  		{0x51, 0x00},
 190  		{0x52, 0x00},
 191  		{0x53, 0x00},
 192  		{0x54, 0x00},
 193  		{0x55, 0x00},
 194  		{0x56, 0xf0},
 195  		{0x57, 0x01},
 196  		{0x58, 0x40},
 197  		{0x59, 0x22},
 198  		{0x5a, 0x03},
 199  		{0x5b, 0x00},
 200  		{0x5c, 0x00},
 201  		{0x5d, 0x00},
 202  		{0x5e, 0x00},
 203  		{0x5f, 0x00},
 204  		{0x60, 0x00},
 205  		{0x61, 0x00},
 206  		{0x62, 0x00},
 207  
 208  	//global gain for range
 209  	{0x70 , 0x85},  
 210  
 211  	////////////////////////////////////////////////
 212  	////////////     block enable      /////////////
 213  	////////////////////////////////////////////////
 214  	{0x40 , 0x7f},
 215  	{0x41 , 0x26},
 216  	{0x42 , 0xff},
 217  	{0x45 , 0x00},  
 218  	{0x44 , 0x00},
 219  	{0x46 , 0x03},
 220  
 221  	{0x4b , 0x01},  
 222  	{0x50 , 0x01},
 223  
 224  	//DN & EEINTP
 225  	{0x7e , 0x0a}, 
 226  	{0x7f , 0x03}, 
 227  	{0x80 , 0x27}, 
 228  	{0x81 , 0x15}, 
 229  	{0x82 , 0x90},
 230  	{0x83 , 0x02},
 231  	{0x84 , 0x23}, 
 232  	{0x90 , 0x2c}, 
 233  	{0x92 , 0x02},
 234  	{0x94 , 0x02},
 235  	{0x95 , 0x35},  
 236  
 237  	///////YCP
 238  	{0xd1 , 0x32},
 239  	{0xd2 , 0x32},
 240  	{0xdd , 0x18},
 241  	{0xde , 0x32},
 242  	{0xe4 , 0x88},
 243  	{0xe5 , 0x40},  
 244  	{0xd7 , 0x0e},  
 245  
 246  	//ABB
 247  
 248  	/////////////////////////////
 249  	//////////////// GAMMA //////
 250  	/////////////////////////////
 251  
 252  	//GC0328_RGB_Gamma
 253  	#if (GC0328_RGB_Gamma == GC0328_RGB_Gamma_m0)
 254  		{0xfe, 0x00},
 255  		{0xBF, 0x0E},
 256  		{0xc0, 0x1C},
 257  		{0xc1, 0x34},
 258  		{0xc2, 0x48},
 259  		{0xc3, 0x5A},
 260  		{0xc4, 0x6B},
 261  		{0xc5, 0x7B},
 262  		{0xc6, 0x95},
 263  		{0xc7, 0xAB},
 264  		{0xc8, 0xBF},
 265  		{0xc9, 0xCE},
 266  		{0xcA, 0xD9},
 267  		{0xcB, 0xE4},
 268  		{0xcC, 0xEC},
 269  		{0xcD, 0xF7},
 270  		{0xcE, 0xFD},
 271  		{0xcF, 0xFF},
 272  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_m1)
 273  		//smallest gamma curve
 274  		{0xfe, 0x00},
 275  		{0xbf, 0x06},
 276  		{0xc0, 0x12},
 277  		{0xc1, 0x22},
 278  		{0xc2, 0x35},
 279  		{0xc3, 0x4b},
 280  		{0xc4, 0x5f},
 281  		{0xc5, 0x72},
 282  		{0xc6, 0x8d},
 283  		{0xc7, 0xa4},
 284  		{0xc8, 0xb8},
 285  		{0xc9, 0xc8},
 286  		{0xca, 0xd4},
 287  		{0xcb, 0xde},
 288  		{0xcc, 0xe6},
 289  		{0xcd, 0xf1},
 290  		{0xce, 0xf8},
 291  		{0xcf, 0xfd},
 292  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_m2)
 293  		{0xfe , 0x00},
 294  		{0xBF, 0x08},
 295  		{0xc0, 0x0F},
 296  		{0xc1, 0x21},
 297  		{0xc2, 0x32},
 298  		{0xc3, 0x43},
 299  		{0xc4, 0x50},
 300  		{0xc5, 0x5E},
 301  		{0xc6, 0x78},
 302  		{0xc7, 0x90},
 303  		{0xc8, 0xA6},
 304  		{0xc9, 0xB9},
 305  		{0xcA, 0xC9},
 306  		{0xcB, 0xD6},
 307  		{0xcC, 0xE0},
 308  		{0xcD, 0xEE},
 309  		{0xcE, 0xF8},
 310  		{0xcF, 0xFF},
 311  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_m3)
 312  		{0xfe , 0x00},
 313  		{0xBF, 0x0B},
 314  		{0xc0, 0x16},
 315  		{0xc1, 0x29},
 316  		{0xc2, 0x3C},
 317  		{0xc3, 0x4F},
 318  		{0xc4, 0x5F},
 319  		{0xc5, 0x6F},
 320  		{0xc6, 0x8A},
 321  		{0xc7, 0x9F},
 322  		{0xc8, 0xB4},
 323  		{0xc9, 0xC6},
 324  		{0xcA, 0xD3},
 325  		{0xcB, 0xDD},
 326  		{0xcC, 0xE5},
 327  		{0xcD, 0xF1},
 328  		{0xcE, 0xFA},
 329  		{0xcF, 0xFF},
 330  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_m4)
 331  		{0xfe , 0x00},
 332  		{0xBF, 0x0E},
 333  		{0xc0, 0x1C},
 334  		{0xc1, 0x34},
 335  		{0xc2, 0x48},
 336  		{0xc3, 0x5A},
 337  		{0xc4, 0x6B},
 338  		{0xc5, 0x7B},
 339  		{0xc6, 0x95},
 340  		{0xc7, 0xAB},
 341  		{0xc8, 0xBF},
 342  		{0xc9, 0xCE},
 343  		{0xcA, 0xD9},
 344  		{0xcB, 0xE4},
 345  		{0xcC, 0xEC},
 346  		{0xcD, 0xF7},
 347  		{0xcE, 0xFD},
 348  		{0xcF, 0xFF},
 349  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_m5)
 350  		{0xfe , 0x00},
 351  		{0xBF, 0x10},
 352  		{0xc0, 0x20},
 353  		{0xc1, 0x38},
 354  		{0xc2, 0x4E},
 355  		{0xc3, 0x63},
 356  		{0xc4, 0x76},
 357  		{0xc5, 0x87},
 358  		{0xc6, 0xA2},
 359  		{0xc7, 0xB8},
 360  		{0xc8, 0xCA},
 361  		{0xc9, 0xD8},
 362  		{0xcA, 0xE3},
 363  		{0xcB, 0xEB},
 364  		{0xcC, 0xF0},
 365  		{0xcD, 0xF8},
 366  		{0xcE, 0xFD},
 367  		{0xcF, 0xFF},
 368  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_m6)
 369  		// largest gamma curve
 370  		{0xfe , 0x00},
 371  		{0xBF, 0x14},
 372  		{0xc0, 0x28},
 373  		{0xc1, 0x44},
 374  		{0xc2, 0x5D},
 375  		{0xc3, 0x72},
 376  		{0xc4, 0x86},
 377  		{0xc5, 0x95},
 378  		{0xc6, 0xB1},
 379  		{0xc7, 0xC6},
 380  		{0xc8, 0xD5},
 381  		{0xc9, 0xE1},
 382  		{0xcA, 0xEA},
 383  		{0xcB, 0xF1},
 384  		{0xcC, 0xF5},
 385  		{0xcD, 0xFB},
 386  		{0xcE, 0xFE},
 387  		{0xcF, 0xFF},
 388  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_night)
 389  		//Gamma for night mode
 390  		{0xfe , 0x00},
 391  		{0xBF, 0x0B},
 392  		{0xc0, 0x16},
 393  		{0xc1, 0x29},
 394  		{0xc2, 0x3C},
 395  		{0xc3, 0x4F},
 396  		{0xc4, 0x5F},
 397  		{0xc5, 0x6F},
 398  		{0xc6, 0x8A},
 399  		{0xc7, 0x9F},
 400  		{0xc8, 0xB4},
 401  		{0xc9, 0xC6},
 402  		{0xcA, 0xD3},
 403  		{0xcB, 0xDD},
 404  		{0xcC, 0xE5},
 405  		{0xcD, 0xF1},
 406  		{0xcE, 0xFA},
 407  		{0xcF, 0xFF},
 408  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_cap)
 409  		{0xfe , 0x00},
 410  		{0xbf , 0x10},
 411  		{0xc0 , 0x1c},
 412  		{0xc1 , 0x33},
 413  		{0xc2 , 0x48},
 414  		{0xc3 , 0x5a},
 415  		{0xc4 , 0x6b},
 416  		{0xc5 , 0x7b},
 417  		{0xc6 , 0x95},
 418  		{0xc7 , 0xab},
 419  		{0xc8 , 0xbf},
 420  		{0xc9 , 0xcd},
 421  		{0xca , 0xd9},
 422  		{0xcb , 0xe3},
 423  		{0xcc , 0xeb},
 424  		{0xcd , 0xf7},
 425  		{0xce , 0xfd},
 426  		{0xcf , 0xff},
 427  	#elif (GC0328_RGB_Gamma == GC0328_RGB_Gamma_test)
 428  		{0xfe , 0x00},
 429  		{0xbf , 0x00},
 430  		{0xc0 , 0x01},
 431  		{0xc1 , 0x02},
 432  		{0xc2 , 0x03},
 433  		{0xc3 , 0x04},
 434  		{0xc4 , 0x05},
 435  		{0xc5 , 0x06},
 436  		{0xc6 , 0x07},
 437  		{0xc7 , 0x08},
 438  		{0xc8 , 0x09},
 439  		{0xc9 , 0x0a},
 440  		{0xca , 0x0b},
 441  		{0xcb , 0x0c},
 442  		{0xcc , 0x0d},
 443  		{0xcd , 0x0e},
 444  		{0xce , 0x0f},
 445  		{0xcf , 0xff},
 446  	#endif
 447  
 448  	///Y gamma
 449  	#if (GC0328_Y_Gamma == GC0328_Y_Gamma_05)
 450  	//0.5
 451  	{0xfe , 0x00},  
 452  	{0x63 , 0x00},  
 453  	{0x64 , 0x49},  
 454  	{0x65 , 0x68},  
 455  	{0x66 , 0x80},  
 456  	{0x67 , 0x93},  
 457  	{0x68 , 0xa5},  
 458  	{0x69 , 0xb5},  
 459  	{0x6a , 0xc3},  
 460  	{0x6b , 0xd1},  
 461  	{0x6c , 0xdd},  
 462  	{0x6d , 0xe9},  
 463  	{0x6e , 0xf5},  
 464  	{0x6f , 0xFF},
 465  	#elif (GC0328_Y_Gamma == GC0328_Y_Gamma_06)
 466  	//0.6
 467  	{0xfe , 0x00},  
 468  	{0x63,0x0 },
 469  	{0x64,0x39},
 470  	{0x65,0x57},
 471  	{0x66,0x6F},
 472  	{0x67,0x84},
 473  	{0x68,0x97},
 474  	{0x69,0xA8},
 475  	{0x6a,0xB9},
 476  	{0x6b,0xC8},
 477  	{0x6c,0xD7},
 478  	{0x6d,0xE5},
 479  	{0x6e,0xF2},
 480  	{0x6f,0xff},
 481  
 482  	#elif (GC0328_Y_Gamma == GC0328_Y_Gamma_07)
 483  	//0.7
 484  	{0xfe , 0x00},  
 485  	{0x63,0x0  },
 486  	{0x64,0x2C },
 487  	{0x65,0x49 },
 488  	{0x66,0x61 },
 489  	{0x67,0x76 },
 490  	{0x68,0x8A },
 491  	{0x69,0x9D },
 492  	{0x6a,0xAF },
 493  	{0x6b,0xC0 },
 494  	{0x6c,0xD1 },
 495  	{0x6d,0xE1 },
 496  	{0x6e,0xF0 },
 497  	{0x6f,0xff },
 498  	#elif (GC0328_Y_Gamma == GC0328_Y_Gamma_08)
 499  	//0.8
 500  	{0xfe , 0x00},
 501  	{0x63,0x0 },
 502  	{0x64,0x23},
 503  	{0x65,0x3D},
 504  	{0x66,0x54},
 505  	{0x67,0x6A},
 506  	{0x68,0x7F},
 507  	{0x69,0x93},
 508  	{0x6a,0xA6},
 509  	{0x6b,0xB9},
 510  	{0x6c,0xCB},
 511  	{0x6d,0xDD},
 512  	{0x6e,0xEE},
 513  	{0x6f,0xff},
 514  	#elif (GC0328_Y_Gamma == GC0328_Y_Gamma_09)
 515  	//0.9
 516  	{0xfe , 0x00},
 517  	{0x63,0x0 },
 518  	{0x64,0x1B},
 519  	{0x65,0x33},
 520  	{0x66,0x49},
 521  	{0x67,0x5F},
 522  	{0x68,0x74},
 523  	{0x69,0x89},
 524  	{0x6a,0x9D},
 525  	{0x6b,0xB1},
 526  	{0x6c,0xC5},
 527  	{0x6d,0xD9},
 528  	{0x6e,0xEC},
 529  	{0x6f,0xff},
 530  	#elif (GC0328_Y_Gamma == GC0328_Y_Gamma_10)
 531  	//0.9
 532  	{0xfe , 0x00},
 533  	{0x63,0x0 },
 534  	{0x64,0x15},
 535  	{0x65,0x2A},
 536  	{0x66,0x40},
 537  	{0x67,0x55},
 538  	{0x68,0x6A},
 539  	{0x69,0x80},
 540  	{0x6a,0x95},
 541  	{0x6b,0xAA},
 542  	{0x6c,0xC0},
 543  	{0x6d,0xD5},
 544  	{0x6e,0xEA},
 545  	{0x6f,0xff},
 546  	#elif (GC0328_Y_Gamma == GC0328_Y_Gamma_default)
 547  	{0xfe , 0x00},
 548  	{0x63,0   },
 549  	{0x64,0x10},
 550  	{0x65,0x1c},
 551  	{0x66,0x30},
 552  	{0x67,0x43},
 553  	{0x68,0x54},
 554  	{0x69,0x65},
 555  	{0x6a,0x75},
 556  	{0x6b,0x93},
 557  	{0x6c,0xb0},
 558  	{0x6d,0xcb},
 559  	{0x6e,0xe6},
 560  	{0x6f,0xff},
 561  
 562  
 563  	#endif
 564  
 565  
 566  	//////ASDE
 567  	{0xfe , 0x01},  
 568  	{0x18 , 0x02},  
 569  	{0xfe , 0x00},  
 570  	{0x98 , 0x00},  
 571  	{0x9b , 0x20},  
 572  	{0x9c , 0x80},  
 573  	{0xa4 , 0x10},  
 574  	{0xa8 , 0xB0},  
 575  	{0xaa , 0x40},  
 576  	{0xa2 , 0x23},  
 577  	{0xad , 0x01},  
 578  
 579  		//////////////////////////////////////////////
 580  		////////// AEC    ////////////////////////
 581  		//////////////////////////////////////////////
 582  	{0xfe , 0x01},
 583  	{0x9c , 0x02},
 584  	{0x08 , 0xa0},
 585  	{0x09 , 0xe8},
 586  				
 587  	{0x10 , 0x00},
 588  	{0x11 , 0x11},
 589  	{0x12 , 0x10},
 590  	{0x13 , 0x80},
 591  	{0x15 , 0xfc},
 592  	{0x18 , 0x03},
 593  	{0x21 , 0xc0},
 594  	{0x22 , 0x60},
 595  	{0x23 , 0x30},
 596  	{0x25 , 0x00},
 597  	{0x24 , 0x14},
 598  				
 599  				
 600  	//////////////////////////////////////
 601  	////////////LSC//////////////////////
 602  	//////////////////////////////////////
 603  	//gc0328 Alight lsc reg setting list
 604  				
 605  	{0xfe , 0x01}, 
 606  	{0xc0 , 0x10}, 
 607  	{0xc1 , 0x0c}, 
 608  	{0xc2 , 0x0a}, 
 609  	{0xc6 , 0x0e}, 
 610  	{0xc7 , 0x0b}, 
 611  	{0xc8 , 0x0a}, 
 612  	{0xba , 0x26}, 
 613  	{0xbb , 0x1c}, 
 614  	{0xbc , 0x1d}, 
 615  	{0xb4 , 0x23}, 
 616  	{0xb5 , 0x1c}, 
 617  	{0xb6 , 0x1a}, 
 618  	{0xc3 , 0x00}, 
 619  	{0xc4 , 0x00},
 620  	{0xc5 , 0x00},
 621  	{0xc9 , 0x00},
 622  	{0xca , 0x00},
 623  	{0xcb , 0x00},
 624  	{0xbd , 0x00},
 625  	{0xbe , 0x00},
 626  	{0xbf , 0x00},
 627  	{0xb7 , 0x07},
 628  	{0xb8 , 0x05},
 629  	{0xb9 , 0x05},
 630  	{0xa8 , 0x07},
 631  	{0xa9 , 0x06},
 632  	{0xaa , 0x00},
 633  	{0xab , 0x04},
 634  	{0xac , 0x00},
 635  	{0xad , 0x02},
 636  	{0xae , 0x0d},
 637  	{0xaf , 0x05},
 638  	{0xb0 , 0x00},
 639  	{0xb1 , 0x07},
 640  	{0xb2 , 0x03},
 641  	{0xb3 , 0x00},
 642  	{0xa4 , 0x00},
 643  	{0xa5 , 0x00},
 644  	{0xa6 , 0x00},
 645  	{0xa7 , 0x00},
 646  	{0xa1 , 0x3c},
 647  	{0xa2 , 0x50},
 648  	{0xfe , 0x00},
 649  
 650  	///cct
 651  	{0xB1 , 0x04},  
 652  	{0xB2 , 0xfd},  
 653  	{0xB3 , 0xfc},  
 654  	{0xB4 , 0xf0},  
 655  	{0xB5 , 0x05},  
 656  	{0xB6 , 0xf0},  
 657  
 658  
 659  
 660  	{0xfe , 0x00},
 661  	{0x27 , 0xf7},
 662  	{0x28 , 0x7F},
 663  	{0x29 , 0x20},
 664  	{0x33 , 0x20},
 665  	{0x34 , 0x20},
 666  	{0x35 , 0x20},
 667  	{0x36 , 0x20},
 668  	{0x32 , 0x08},
 669  
 670  	{0x47 , 0x00},
 671  	{0x48 , 0x00},
 672  					
 673  	{0xfe , 0x01},
 674  	{0x79 , 0x00},
 675  	{0x7d , 0x00},
 676  	{0x50 , 0x88},
 677  	{0x5b , 0x0c},
 678  	{0x76 , 0x8f},
 679  	{0x80 , 0x70},
 680  	{0x81 , 0x70},
 681  	{0x82 , 0xb0},
 682  	{0x70 , 0xff},
 683  	{0x71 , 0x00},
 684  	{0x72 , 0x28},
 685  	{0x73 , 0x0b},  
 686  	{0x74 , 0x0b},  
 687  					
 688  	{0xfe , 0x00},  
 689  	{0x70 , 0x45},
 690  	{0x4f , 0x01},
 691  	{0xf1 , 0x07},
 692  	{0xf2 , 0x01}, 
 693  
 694  	{0x00, 0x00},
 695  };
 696  
 697  static const uint8_t qqvga_config[][2] = { //k210 
 698      {0xfe , 0x00},
 699      // window
 700          //windowing mode
 701  	{0x09 , 0x00},
 702      {0x0a , 0x00},
 703  	{0x0b , 0x00},
 704  	{0x0c , 0x00},
 705      {0x0d , 0x01},
 706  	{0x0e , 0xe8},
 707  	{0x0f , 0x02},
 708  	{0x10 , 0x88},
 709          //crop mode 
 710      {0x50 , 0x01},
 711      {0x51, 0x00},
 712      {0x52, 0x00},
 713      {0x53, 0x00},
 714      {0x54, 0x00},
 715      {0x55, 0x00},
 716      {0x56, 0x78},
 717      {0x57, 0x00},
 718      {0x58, 0xA0},
 719      //subsample 1/4
 720      {0x59, 0x44},
 721      {0x5a, 0x03},
 722      {0x5b, 0x00},
 723      {0x5c, 0x00},
 724      {0x5d, 0x00},
 725      {0x5e, 0x00},
 726      {0x5f, 0x00},
 727      {0x60, 0x00},
 728      {0x61, 0x00},
 729      {0x62, 0x00},
 730  
 731      {0x00, 0x00}
 732  };
 733  
 734  static const uint8_t qvga_config[][2] = { //k210 
 735      {0xfe , 0x00},
 736      // window
 737          //windowing mode
 738  	{0x09 , 0x00},
 739      {0x0a , 0x00},
 740  	{0x0b , 0x00},
 741  	{0x0c , 0x00},
 742      {0x0d , 0x01},
 743  	{0x0e , 0xe8},
 744  	{0x0f , 0x02},
 745  	{0x10 , 0x88},
 746          //crop mode 
 747      {0x50 , 0x01},
 748      // {0x51, 0x00},
 749      // {0x52, 0x78},
 750      // {0x53, 0x00},
 751      // {0x54, 0xa0},
 752      // {0x55, 0x00},
 753      // {0x56, 0xf0},
 754      // {0x57, 0x01},
 755      // {0x58, 0x40},
 756      //subsample 1/2
 757      {0x59, 0x22},
 758      {0x5a, 0x00},
 759      {0x5b, 0x00},
 760      {0x5c, 0x00},
 761      {0x5d, 0x00},
 762      {0x5e, 0x00},
 763      {0x5f, 0x00},
 764      {0x60, 0x00},
 765      {0x61, 0x00},
 766      {0x62, 0x00},
 767  
 768      {0x00, 0x00}
 769  };
 770  
 771  static const uint8_t B240X240_config[][2] = { //k210 
 772      {0xfe , 0x00},
 773      // window
 774          //windowing mode
 775  	{0x09 , 0x00},
 776      {0x0a , 0x00},
 777  	{0x0b , 0x00},
 778  	{0x0c , 0x00},
 779      {0x0d , 0x01},
 780  	{0x0e , 0xe8},
 781  	{0x0f , 0x02},
 782  	{0x10 , 0x88},
 783          //crop mode 
 784      {0x50 , 0x01},
 785      {0x51, 0x00},
 786      {0x52, 0x00},
 787      {0x53, 0x00},
 788      {0x54, 0x28},
 789      {0x55, 0x00},
 790      {0x56, 0xf0},
 791      {0x57, 0x00},
 792      {0x58, 0xf0},
 793      //subsample 1/2
 794      {0x59, 0x22},
 795      {0x5a, 0x00},
 796      {0x5b, 0x00},
 797      {0x5c, 0x00},
 798      {0x5d, 0x00},
 799      {0x5e, 0x00},
 800      {0x5f, 0x00},
 801      {0x60, 0x00},
 802      {0x61, 0x00},
 803      {0x62, 0x00},
 804  
 805      {0x00, 0x00}
 806  };
 807  
 808  static const uint8_t vga_config[][2] = { //k210 
 809      {0xfe, 0x00},
 810      {0x4b, 0x8b},
 811      {0x50, 0x01},
 812      {0x51, 0x00},
 813      {0x52, 0x00},
 814      {0x53, 0x00},
 815      {0x54, 0x00},
 816      {0x55, 0x01},
 817      {0x56, 0xe0},
 818      {0x57, 0x02},
 819      {0x58, 0x80},
 820      {0x59, 0x11},
 821      {0x5a, 0x02},
 822      {0x5b, 0x00},
 823      {0x5c, 0x00},
 824      {0x5d, 0x00},
 825      {0x5e, 0x00},
 826      {0x5f, 0x00},
 827      {0x60, 0x00},
 828      {0x61, 0x00},
 829      {0x62, 0x00},
 830  };
 831  
 832  static int gc0328_read_reg(sensor_t *sensor, uint8_t reg_addr)
 833  {
 834      uint8_t reg_data;
 835      if (cambus_readb(sensor->slv_addr, reg_addr, &reg_data) != 0) {
 836          return -1;
 837      }
 838      return reg_data;
 839  }
 840  
 841  static int gc0328_write_reg(sensor_t *sensor, uint8_t reg_addr, uint16_t reg_data)
 842  {
 843      return cambus_writeb(sensor->slv_addr, reg_addr, (uint8_t)reg_data);
 844  }
 845  
 846  
 847  static int gc0328_set_pixformat(sensor_t *sensor, pixformat_t pixformat)
 848  {
 849      return 0;
 850  }
 851  
 852  static int gc0328_set_framesize(sensor_t *sensor, framesize_t framesize)
 853  {
 854      int ret=0;
 855      uint16_t w = resolution[framesize][0];
 856      uint16_t h = resolution[framesize][1];
 857  
 858      int i=0;
 859      const uint8_t (*regs)[2];
 860  
 861  	if(framesize == FRAMESIZE_QQVGA)
 862  	{
 863  		regs = qqvga_config;
 864  	}
 865  	else if (framesize == FRAMESIZE_240X240){
 866  		regs = B240X240_config;
 867  	}
 868      else if ((w <= 320) && (h <= 240)) {
 869          regs = qvga_config;
 870      } else {
 871          regs = vga_config;
 872      }
 873  
 874      while (regs[i][0]) {
 875          cambus_writeb(sensor->slv_addr, regs[i][0], regs[i][1]);
 876          i++;
 877      }
 878      /* delay n ms */
 879      mp_hal_delay_ms(30);
 880  	dvp_set_image_size(w, h);
 881      return ret;
 882  }
 883  
 884  static int gc0328_set_framerate(sensor_t *sensor, framerate_t framerate)
 885  {
 886      return 0;
 887  }
 888  
 889  #define NUM_CONTRAST_LEVELS (5)
 890  static uint8_t contrast_regs[NUM_CONTRAST_LEVELS][2]={
 891  	{0x80, 0x00},
 892  	{0x80, 0x20},
 893  	{0x80, 0x40},
 894  	{0x80, 0x60},
 895  	{0x80, 0x80}
 896  };
 897  
 898  static int gc0328_set_contrast(sensor_t *sensor, int level)
 899  {
 900      int ret=0;
 901  
 902      level += (NUM_CONTRAST_LEVELS / 2);
 903      if (level < 0 || level > NUM_CONTRAST_LEVELS) {
 904          return -1;
 905      }
 906  	cambus_writeb(sensor->slv_addr, 0xfe, 0x00);
 907  	cambus_writeb(sensor->slv_addr, 0xd4, contrast_regs[level][0]);
 908  	cambus_writeb(sensor->slv_addr, 0xd3, contrast_regs[level][1]);
 909      return ret;
 910  }
 911  
 912  static int gc0328_set_brightness(sensor_t *sensor, int level)
 913  {
 914      int ret = 0;
 915      return ret;
 916  }
 917  
 918  #define NUM_SATURATION_LEVELS (5)
 919  static uint8_t saturation_regs[NUM_SATURATION_LEVELS][3]={
 920  	{0x00, 0x00, 0x00},
 921  	{0x10, 0x10, 0x10},
 922  	{0x20, 0x20, 0x20},
 923  	{0x30, 0x30, 0x30},
 924  	{0x40, 0x40, 0x40},
 925  };
 926  static int gc0328_set_saturation(sensor_t *sensor, int level)
 927  {
 928      int ret = 0;
 929      level += (NUM_CONTRAST_LEVELS / 2);
 930      if (level < 0 || level > NUM_CONTRAST_LEVELS) {
 931          return -1;
 932      }
 933  	cambus_writeb(sensor->slv_addr, 0xfe, 0x00);
 934  	cambus_writeb(sensor->slv_addr, 0xd0, saturation_regs[level][0]);
 935  	cambus_writeb(sensor->slv_addr, 0xd1, saturation_regs[level][1]);
 936  	cambus_writeb(sensor->slv_addr, 0xd2, saturation_regs[level][2]);
 937      return ret;
 938  }
 939  
 940  static int gc0328_set_gainceiling(sensor_t *sensor, gainceiling_t gainceiling)
 941  {
 942      int ret = 0;
 943  
 944      return ret;
 945  }
 946  
 947  static int gc0328_set_quality(sensor_t *sensor, int qs)
 948  {
 949      int ret = 0;
 950  
 951      return ret;
 952  }
 953  
 954  static int gc0328_set_colorbar(sensor_t *sensor, int enable)
 955  {
 956      int ret = 0;
 957      return ret;
 958  }
 959  
 960  static int gc0328_set_auto_gain(sensor_t *sensor, int enable, float gain_db, float gain_db_ceiling)
 961  {
 962     int ret = 0;
 963     return ret;
 964  }
 965  
 966  static int gc0328_get_gain_db(sensor_t *sensor, float *gain_db)
 967  {
 968      int ret = 0;
 969      return ret;
 970  }
 971  
 972  static int gc0328_set_auto_exposure(sensor_t *sensor, int enable, int exposure_us)
 973  {
 974      int ret = 0;
 975  	uint8_t temp;
 976  	cambus_writeb(sensor->slv_addr, 0xfe, 0x00);
 977  	cambus_readb(sensor->slv_addr, 0x4f, &temp);
 978  	if(enable != 0)
 979  	{
 980  		cambus_writeb(sensor->slv_addr,0x4f, temp|0x01); // enable
 981  		if(exposure_us != -1)
 982  		{
 983  			cambus_writeb(sensor->slv_addr, 0xfe, 0x01);
 984  			cambus_writeb(sensor->slv_addr,0x2f, (uint8_t)(((uint16_t)exposure_us)>>8));
 985  			cambus_writeb(sensor->slv_addr,0x30, (uint8_t)(((uint16_t)exposure_us)));
 986  		}
 987  	}
 988  	else
 989  	{
 990  		cambus_writeb(sensor->slv_addr,0x4f, temp&0xfe); // disable
 991  		if(exposure_us != -1)
 992  		{
 993  			cambus_writeb(sensor->slv_addr, 0xfe, 0x01);
 994  			cambus_writeb(sensor->slv_addr,0x2f, (uint8_t)(((uint16_t)exposure_us)>>8));
 995  			cambus_writeb(sensor->slv_addr,0x30, (uint8_t)(((uint16_t)exposure_us)));
 996  		}
 997  	}
 998      return ret;
 999  }
1000  
1001  static int gc0328_get_exposure_us(sensor_t *sensor, int *exposure_us)
1002  {
1003      int ret = 0;
1004      return ret;
1005  }
1006  
1007  static int gc0328_set_auto_whitebal(sensor_t *sensor, int enable, float r_gain_db, float g_gain_db, float b_gain_db)
1008  {
1009      int ret = 0;
1010  	uint8_t temp;
1011  	cambus_writeb(sensor->slv_addr, 0xfe, 0x00);
1012  	cambus_readb(sensor->slv_addr, 0x42, &temp);
1013  	if(enable != 0)
1014  	{
1015  		cambus_writeb(sensor->slv_addr,0x42, temp|0x02); // enable
1016  		if(!isnanf(r_gain_db))
1017  			cambus_writeb(sensor->slv_addr,0x80, (uint8_t)r_gain_db); //limit
1018  		if(!isnanf(g_gain_db))
1019  			cambus_writeb(sensor->slv_addr,0x81, (uint8_t)g_gain_db);
1020  		if(!isnanf(b_gain_db))
1021  			cambus_writeb(sensor->slv_addr,0x82, (uint8_t)b_gain_db);
1022  	}
1023  	else
1024  	{
1025  		cambus_writeb(sensor->slv_addr,0x42, temp&0xfd); // disable
1026  		if(!isnanf(r_gain_db))
1027  			cambus_writeb(sensor->slv_addr,0x77, (uint8_t)r_gain_db);
1028  		if(!isnanf(g_gain_db))
1029  			cambus_writeb(sensor->slv_addr,0x78, (uint8_t)g_gain_db);
1030  		if(!isnanf(b_gain_db))
1031  			cambus_writeb(sensor->slv_addr,0x79, (uint8_t)b_gain_db);
1032  	}
1033      return ret;
1034  }
1035  
1036  static int gc0328_get_rgb_gain_db(sensor_t *sensor, float *r_gain_db, float *g_gain_db, float *b_gain_db)
1037  {
1038      int ret = 0;
1039      return ret;
1040  }
1041  
1042  static int gc0328_set_hmirror(sensor_t *sensor, int enable)
1043  {
1044      uint8_t data;
1045      cambus_readb(GC0328_ADDR, 0x17, &data);
1046  	data = data & 0xFC;
1047      if(enable){
1048          data = data | 0x01 | (sensor->vflip ? 0x02 : 0x00);
1049      }else{
1050          data = data | (sensor->vflip ? 0x02 : 0x00);
1051      }
1052      return cambus_writeb(GC0328_ADDR, 0x17, data);
1053  }
1054  
1055  static int gc0328_set_vflip(sensor_t *sensor, int enable)
1056  {
1057      uint8_t data;
1058      cambus_readb(GC0328_ADDR, 0x17, &data);
1059  	data = data & 0xFC;
1060      if(enable){
1061          data = data | 0x02 | (sensor->hmirror ? 0x01 : 0x00);
1062      }else{
1063          data = data | (sensor->hmirror ? 0x01 : 0x00);
1064      }
1065      return cambus_writeb(GC0328_ADDR, 0x17, data);
1066  }
1067  
1068  
1069  int gc0328_reset(sensor_t* sensor)
1070  {
1071      uint16_t index = 0;
1072      
1073      cambus_writeb(GC0328_ADDR, 0xfe, 0x01);
1074      for (index = 0; sensor_default_regs[index][0]; index++)
1075      {
1076          if(sensor_default_regs[index][0] == 0xff){
1077              mp_hal_delay_ms(sensor_default_regs[index][1]);
1078              continue;
1079          }
1080          // mp_printf(&mp_plat_print, "0x12,0x%02x,0x%02x,\r\n", sensor_default_regs[index][0], sensor_default_regs[index][1]);//debug
1081          cambus_writeb(GC0328_ADDR, sensor_default_regs[index][0], sensor_default_regs[index][1]);
1082          // mp_hal_delay_ms(1);
1083      }
1084      return 0;
1085  }
1086  
1087  int gc0328_set_windowing(framesize_t framesize, int x, int y, int w, int h)
1088  {
1089  	if(framesize == FRAMESIZE_QVGA)
1090  	{
1091  		cambus_writeb(GC0328_ADDR, 0xfe, 0x00);
1092  		cambus_writeb(GC0328_ADDR, 0x51, (y>>8) & 0xff);
1093  		cambus_writeb(GC0328_ADDR, 0x52, y & 0xff);
1094  		cambus_writeb(GC0328_ADDR, 0x53, (x>>8) & 0xff);
1095  		cambus_writeb(GC0328_ADDR, 0x54, x & 0xff);
1096  		cambus_writeb(GC0328_ADDR, 0x55, (h>>8) & 0x01);
1097  		cambus_writeb(GC0328_ADDR, 0x56, h & 0xff);
1098  		cambus_writeb(GC0328_ADDR, 0x57, (w>>8) & 0x03);
1099  		cambus_writeb(GC0328_ADDR, 0x58, w & 0xff);
1100  	}
1101  	return 0;
1102  }
1103  
1104  int gc0328_init(sensor_t *sensor)
1105  {
1106      //Initialize sensor structure.
1107      sensor->gs_bpp              = 2;
1108      sensor->reset               = gc0328_reset;
1109      sensor->read_reg            = gc0328_read_reg;
1110      sensor->write_reg           = gc0328_write_reg;
1111      sensor->set_pixformat       = gc0328_set_pixformat;
1112      sensor->set_framesize       = gc0328_set_framesize;
1113      sensor->set_framerate       = gc0328_set_framerate;
1114      sensor->set_contrast        = gc0328_set_contrast;
1115      sensor->set_brightness      = gc0328_set_brightness;
1116      sensor->set_saturation      = gc0328_set_saturation;
1117      sensor->set_gainceiling     = gc0328_set_gainceiling;
1118      sensor->set_quality         = gc0328_set_quality;
1119      sensor->set_colorbar        = gc0328_set_colorbar;
1120      sensor->set_auto_gain       = gc0328_set_auto_gain;
1121      sensor->get_gain_db         = gc0328_get_gain_db;
1122      sensor->set_auto_exposure   = gc0328_set_auto_exposure;
1123      sensor->get_exposure_us     = gc0328_get_exposure_us;
1124      sensor->set_auto_whitebal   = gc0328_set_auto_whitebal;
1125      sensor->get_rgb_gain_db     = gc0328_get_rgb_gain_db;
1126      sensor->set_hmirror         = gc0328_set_hmirror;
1127      sensor->set_vflip           = gc0328_set_vflip;
1128  	sensor->set_windowing       = gc0328_set_windowing;
1129  
1130      // Set sensor flags
1131      SENSOR_HW_FLAGS_SET(sensor, SENSOR_HW_FLAGS_VSYNC, 0);
1132      SENSOR_HW_FLAGS_SET(sensor, SENSOR_HW_FLAGS_HSYNC, 0);
1133      SENSOR_HW_FLAGS_SET(sensor, SENSOR_HW_FLAGS_PIXCK, 1);
1134      SENSOR_HW_FLAGS_SET(sensor, SENSOR_HW_FLAGS_FSYNC, 0);
1135      SENSOR_HW_FLAGS_SET(sensor, SENSOR_HW_FLAGS_JPEGE, 1);
1136  
1137      return 0;
1138  }