Gem_isp_ae.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // =============================================================================
  2. // File : Gem_isp_ae.c
  3. // Version : v1.0
  4. // Author : Honglei Zhu
  5. // Date : 2014.12.5
  6. // -----------------------------------------------------------------------------
  7. // Description :
  8. //
  9. // -----------------------------------------------------------------------------
  10. #include "Gem_isp_ae.h"
  11. #include "Gem_isp_io.h"
  12. #include "Gem_isp_sensor.h"
  13. #include "ark_isp_exposure_cmos.h"
  14. #include "ark_isp_exposure.h"
  15. #include "Gem_isp_eris.h"
  16. // ae:etime_H8:0 etime_M8:20 etime_L8:48 again8:0 dgain8:64
  17. extern cmos_exposure_t isp_exposure;
  18. //static OS_EVENT ae_event;
  19. void isp_ae_event_init (void)
  20. {
  21. //OS_EVENT_Create (&ae_event);
  22. }
  23. void isp_ae_event_exit (void)
  24. {
  25. //OS_EVENT_Delete (&ae_event);
  26. }
  27. void isp_ae_event_set (void)
  28. {
  29. //OS_EVENT_Set (&ae_event);
  30. }
  31. void isp_ae_event_reset (void)
  32. {
  33. //OS_EVENT_Reset (&ae_event);
  34. }
  35. void isp_ae_event_wait (void)
  36. {
  37. //OS_EVENT_Wait (&ae_event);
  38. //if(OS_EVENT_WaitTimed (&ae_event, 60*1000)) // 60s
  39. //{
  40. // XM_printf ("timeout, AE can't stable within 60s\n");
  41. //}
  42. }
  43. void isp_ae_window_weight_write (auto_exposure_ptr_t ae, u8_t win_weight[3][3])
  44. {
  45. unsigned int data;
  46. ae->window_weight[0][0] = win_weight[0][0];
  47. ae->window_weight[0][1] = win_weight[0][1];
  48. ae->window_weight[0][2] = win_weight[0][2];
  49. ae->window_weight[1][0] = win_weight[1][0];
  50. ae->window_weight[1][1] = win_weight[1][1];
  51. ae->window_weight[1][2] = win_weight[1][2];
  52. ae->window_weight[2][0] = win_weight[2][0];
  53. ae->window_weight[2][1] = win_weight[2][1];
  54. ae->window_weight[2][2] = win_weight[2][2];
  55. data = (ae->window_weight[0][0]<<0) | (ae->window_weight[0][1]<<4) |
  56. (ae->window_weight[0][2]<<8) | (ae->window_weight[1][0]<<12) |
  57. (ae->window_weight[1][1]<<16) | (ae->window_weight[1][2]<<20) |
  58. (ae->window_weight[2][0]<<24) | (ae->window_weight[2][1]<<28);
  59. Gem_write ((GEM_AE0_BASE+0x04), data);
  60. data = (ae->window_weight[2][2]<<0);
  61. Gem_write ((GEM_AE0_BASE+0x08), data);
  62. }
  63. // �ֶ���ֵд��
  64. void isp_histogram_bands_write (u8_t histoBand[4])
  65. {
  66. unsigned int data;
  67. data = (histoBand[0]<<0) | (histoBand[1]<<8) |
  68. (histoBand[2]<<16) | (histoBand[3]<<24);
  69. Gem_write ((GEM_AE0_BASE+0x00), data);
  70. }
  71. static volatile unsigned int isp_hist_data1 = 0;
  72. static volatile unsigned int isp_hist_data2 = 0;
  73. void isp_histogram_bands_data (unsigned int data1, unsigned int data2)
  74. {
  75. isp_hist_data1 = data1;
  76. isp_hist_data2 = data2;
  77. }
  78. // 5����Ϣ����
  79. void isp_histogram_bands_read (histogram_band_t bands[5], isp_ae_ptr_t isp_ae)
  80. {
  81. #if 0
  82. unsigned int data1, data2;
  83. //XM_lock();
  84. data1 = isp_hist_data1; //Gem_read (GEM_AE1_BASE+0x04);
  85. data2 = isp_hist_data2; //Gem_read (GEM_AE1_BASE+0x08);
  86. //XM_unlock ();
  87. bands[0].value = ((data1 >> 0) & 0xfff) << 4;
  88. bands[1].value = ((data1 >> 16) & 0xfff) << 4;
  89. bands[2].value = 0;
  90. bands[3].value = ((data2 >> 0) & 0xfff) << 4;
  91. bands[4].value = ((data2 >> 16) & 0xfff) << 4;
  92. #else
  93. //XM_lock();
  94. bands[0].value = isp_ae->histoGram[0] << 4;
  95. bands[1].value = isp_ae->histoGram[1] << 4;
  96. bands[2].value = 0;
  97. bands[3].value = isp_ae->histoGram[3] << 4;
  98. bands[4].value = isp_ae->histoGram[4] << 4;
  99. //XM_unlock ();
  100. #endif
  101. }
  102. u32_t isp_ae_lum_read (void)
  103. {
  104. return Gem_read (GEM_AE1_BASE+0x00) & 0xFF;
  105. }
  106. void isp_ae_init (isp_ae_ptr_t p_ae)
  107. {
  108. arkn141_isp_ae_initialize (&isp_exposure);
  109. //isp_ae_init_io (p_ae);
  110. //XM_printf ("\r\np_ae->opt_jitterxx no.1= ", p_ae->opt_jitter);
  111. }
  112. void isp_ae_init_io (isp_ae_ptr_t p_ae)
  113. {
  114. unsigned int data;
  115. //data = 256 & 0x0000ffff;
  116. //Gem_write ((GEM_AE0_BASE+0x00), data);
  117. data = (p_ae->histoBand[0]<<0) | (p_ae->histoBand[1]<<8) |
  118. (p_ae->histoBand[2]<<16) | (p_ae->histoBand[3]<<24);
  119. Gem_write ((GEM_AE0_BASE+0x00), data);
  120. data = (p_ae->winWeight[0][0]<<0) | (p_ae->winWeight[0][1]<<4) |
  121. (p_ae->winWeight[0][2]<<8) | (p_ae->winWeight[1][0]<<12) |
  122. (p_ae->winWeight[1][1]<<16) | (p_ae->winWeight[1][2]<<20) |
  123. (p_ae->winWeight[2][0]<<24) | (p_ae->winWeight[2][1]<<28);
  124. Gem_write ((GEM_AE0_BASE+0x04), data);
  125. data = (p_ae->winWeight[2][2]<<0);
  126. Gem_write ((GEM_AE0_BASE+0x08), data);
  127. }
  128. static int ae_info_output_enable = 0;
  129. isp_ae_frame_record_t ae_rcd;
  130. void isp_ae_info_output_enable (int enable)
  131. {
  132. ae_info_output_enable = enable;
  133. }
  134. void isp_ae_run (isp_ae_ptr_t p_ae)
  135. {
  136. int ret;
  137. isp_ae_frame_record_t* ae_record = &ae_rcd;
  138. // ����Զ�AE�Ƿ���
  139. if(!isp_get_auto_run_state(ISP_AUTO_RUN_AE))
  140. goto ae_info_output;
  141. ret = arkn141_isp_ae_run (&isp_exposure);
  142. if(ret) // �ع����ȶ�
  143. {
  144. isp_ae_event_set ();
  145. }
  146. ae_record->hist[0] = isp_exposure.cmos_ae.histogram.bands[0].value;
  147. ae_record->hist[1] = isp_exposure.cmos_ae.histogram.bands[1].value;
  148. ae_record->hist[2] = isp_exposure.cmos_ae.histogram.bands[2].value;
  149. ae_record->hist[3] = isp_exposure.cmos_ae.histogram.bands[3].value;
  150. ae_record->hist[4] = isp_exposure.cmos_ae.histogram.bands[4].value;
  151. ae_record->balance = isp_exposure.cmos_ae.histogram.hist_balance;
  152. ae_record->error = isp_exposure.cmos_ae.histogram.hist_error;
  153. //ae_record->lum = (u16_t)isp_ae_lum_read();
  154. ae_record->lum = p_ae->lumCurr; // (u16_t)isp_ae_lum_read();
  155. ae_record->ae_compensation = isp_exposure.cmos_ae.ae_compensation;
  156. ae_record->ae_black_target = isp_exposure.cmos_ae.ae_black_target;
  157. ae_record->ae_bright_target = isp_exposure.cmos_ae.ae_bright_target;
  158. ae_record->increment_step = isp_exposure.cmos_ae.increment_step;
  159. ae_record->increment_offset = isp_exposure.cmos_ae.increment_offset;
  160. ae_record->increment_max = isp_exposure.cmos_ae.increment_max;
  161. ae_record->increment_min = isp_exposure.cmos_ae.increment_min;
  162. ae_record->exposure = isp_exposure.exposure;
  163. ae_record->last_exposure = isp_exposure.last_exposure;
  164. ae_record->increment = isp_exposure.cmos_ae.increment;
  165. ae_record->physical_exposure = isp_exposure.physical_exposure; // ���һ�ε������ع�ֵ
  166. ae_record->sensor_inttime = isp_exposure.cmos_inttime.exposure_ashort;
  167. ae_record->sensor_again = isp_exposure.cmos_gain.again;
  168. ae_record->sensor_dgain = isp_exposure.cmos_gain.dgain;
  169. ae_record->sensor_aindex = isp_exposure.cmos_gain.aindex;
  170. ae_record->sensor_dindex = isp_exposure.cmos_gain.dindex;
  171. ae_record->sensor_again_shift = isp_exposure.cmos_gain.again_shift;
  172. ae_record->sensor_dgain_shift = isp_exposure.cmos_gain.dgain_shift;
  173. ae_record->hist_error_min = isp_exposure.cmos_ae.histogram.hist_error_min;
  174. ae_record->hist_error_max = isp_exposure.cmos_ae.histogram.hist_error_max;
  175. ae_record->hist_error_one = isp_exposure.cmos_ae.histogram.hist_error_one;
  176. ae_record->hist_error_last = isp_exposure.cmos_ae.histogram.hist_error_last;
  177. ae_info_output:
  178. // �����ع���Ϣ���
  179. if(ae_info_output_enable )
  180. {
  181. //XM_printf ("\nexp_count = %d\n", ae_record->index);
  182. XM_printf ("\nbalance = %6d, error = %6d, lum=%d, min=%d, max=%d, one=%d, last=%d\n",
  183. ae_record->balance, ae_record->error, ae_record->lum,
  184. ae_record->hist_error_min, ae_record->hist_error_max, ae_record->hist_error_one, ae_record->hist_error_last);
  185. XM_printf ("compensation = %3d, black = %d, bright = %d\n",
  186. ae_record->ae_compensation,
  187. ae_record->ae_black_target,
  188. ae_record->ae_bright_target
  189. );
  190. XM_printf ("\thist[5]= 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
  191. ae_record->hist[0],
  192. ae_record->hist[1],
  193. ae_record->hist[2],
  194. ae_record->hist[3],
  195. ae_record->hist[4]);
  196. XM_printf ("\tstep = %4d, offset = %10d, max = %10d, min = %10d\n",
  197. ae_record->increment_step,
  198. ae_record->increment_offset,
  199. ae_record->increment_max,
  200. ae_record->increment_min);
  201. XM_printf ("\texposure = %10d, new_exposure = %10d, increment = %10d\n",
  202. ae_record->last_exposure,
  203. ae_record->exposure,
  204. ae_record->increment);
  205. XM_printf ("\tlog_exposure=%10d, inttime=%5d, again*1024=%d, dgain*1024=%d\n",
  206. (u32_t)ae_record->physical_exposure,
  207. ae_record->sensor_inttime,
  208. ae_record->sensor_again * 1024 / (1 << ae_record->sensor_again_shift),
  209. ae_record->sensor_dgain * 1024 / (1 << ae_record->sensor_dgain_shift)
  210. );
  211. }
  212. }
  213. #if 0
  214. void isp_ae_get_current_exp (unsigned int *inttime,
  215. float *again,
  216. float *dgain
  217. )
  218. {
  219. //OS_EnterRegion (); // ��ֹ�����л�, ��������һ����
  220. *inttime = isp_exposure.cmos_inttime.exposure_ashort;
  221. *again = (float)isp_exposure.cmos_gain.again / (1 << isp_exposure.cmos_gain.again_shift);
  222. *dgain = (float)isp_exposure.cmos_gain.dgain / (1 << isp_exposure.cmos_gain.dgain_shift);
  223. //OS_LeaveRegion ();
  224. }
  225. // �����ֶ��ع����, ����һ���Բ���
  226. void isp_ae_set_current_exp (unsigned int inttime,
  227. float again,
  228. float dgain
  229. )
  230. {
  231. //OS_EnterRegion (); // ��ֹ�����л�, ��������һ����
  232. isp_exposure.cmos_inttime.exposure_ashort = inttime;
  233. isp_exposure.cmos_gain.again = (unsigned int)(again * (1 << isp_exposure.cmos_gain.again_shift));
  234. isp_exposure.cmos_gain.dgain = (unsigned int)(dgain * (1 << isp_exposure.cmos_gain.dgain_shift));
  235. isp_cmos_inttime_update_manual (&isp_exposure);
  236. //OS_LeaveRegion ();
  237. }
  238. #endif
  239. void isp_ae_get_max_gain (unsigned int *again, unsigned int *dgain)
  240. {
  241. //OS_EnterRegion ();
  242. if(isp_exposure.cmos_sensor.cmos_max_gain_get)
  243. isp_exposure.cmos_sensor.cmos_max_gain_get (&isp_exposure.cmos_gain, again, dgain);
  244. //OS_LeaveRegion ();
  245. }
  246. void isp_ae_set_max_gain (unsigned int again, unsigned int dgain)
  247. {
  248. //OS_EnterRegion ();
  249. if(isp_exposure.cmos_sensor.cmos_max_gain_set)
  250. isp_exposure.cmos_sensor.cmos_max_gain_set (&isp_exposure.cmos_gain, again, dgain);
  251. //OS_LeaveRegion ();
  252. }
  253. void isp_ae_info_read (isp_ae_ptr_t p_ae)
  254. {
  255. unsigned char i, j;
  256. unsigned int data0, data1, data2;
  257. // histoBand
  258. data0 = Gem_read (GEM_AE0_BASE+0x00);
  259. p_ae->histoBand[0] = (unsigned char)((data0 >> 0));
  260. p_ae->histoBand[1] = (unsigned char)((data0 >> 8));
  261. p_ae->histoBand[2] = (unsigned char)((data0 >> 16));
  262. p_ae->histoBand[3] = (unsigned char)((data0 >> 24));
  263. // winWeight
  264. data0 = Gem_read (GEM_AE0_BASE+0x04);
  265. p_ae->winWeight[0][0] = (unsigned char)((data0 >> 0) & 0x0F);
  266. p_ae->winWeight[0][1] = (unsigned char)((data0 >> 4) & 0x0F);
  267. p_ae->winWeight[0][2] = (unsigned char)((data0 >> 8) & 0x0F);
  268. p_ae->winWeight[1][0] = (unsigned char)((data0 >> 12) & 0x0F);
  269. p_ae->winWeight[1][1] = (unsigned char)((data0 >> 16) & 0x0F);
  270. p_ae->winWeight[1][2] = (unsigned char)((data0 >> 20) & 0x0F);
  271. p_ae->winWeight[2][0] = (unsigned char)((data0 >> 24) & 0x0F);
  272. p_ae->winWeight[2][1] = (unsigned char)((data0 >> 28) & 0x0F);
  273. data0 = Gem_read (GEM_AE0_BASE+0x08);
  274. p_ae->winWeight[2][2] = (unsigned char)((data0 >> 0) & 0x0F);
  275. // lumCurr
  276. data0 = Gem_read (GEM_AE1_BASE+0x00);
  277. data1 = Gem_read (GEM_AE1_BASE+0x04);
  278. data2 = Gem_read (GEM_AE1_BASE+0x08);
  279. p_ae->lumCurr = data0 & 0xff;
  280. // histoGram
  281. p_ae->histoGram[0] = (data1 >> 0) & 0xfff;
  282. p_ae->histoGram[1] = (data1 >> 16) & 0xfff;
  283. p_ae->histoGram[3] = (data2 >> 0) & 0xfff;
  284. p_ae->histoGram[4] = (data2 >> 16) & 0xfff;
  285. p_ae->histoGram[2] = p_ae->histoGram[0]+p_ae->histoGram[1]+
  286. p_ae->histoGram[3]+p_ae->histoGram[4];
  287. if(p_ae->histoGram[2] >= 4095)
  288. p_ae->histoGram[2] = 0;
  289. else
  290. p_ae->histoGram[2] = 4095 - p_ae->histoGram[2];
  291. /*
  292. XM_printf ("p_ae->lum_avg = %d", p_ae->lumCurr);
  293. XM_printf ("\r\np_ae->histo_gram[0] = %d", p_ae->histoGram[0]);
  294. XM_printf ("\r\np_ae->histo_gram[1] = %d", p_ae->histoGram[1]);
  295. XM_printf ("\r\np_ae->histo_gram[2] = %d", p_ae->histoGram[2]);
  296. XM_printf ("\r\np_ae->histo_gram[3] = %d", p_ae->histoGram[3]);
  297. XM_printf ("\r\np_ae->histo_gram[4] = %d\r\n", p_ae->histoGram[4]);
  298. */
  299. }
  300. void isp_ae_sts2_read (isp_ae_ptr_t p_ae)
  301. {
  302. unsigned int data;
  303. unsigned int i, j, data3[3][3], data4[3][3];
  304. data3[0][0] = Gem_read (GEM_AE2_BASE+0x00);
  305. data4[0][0] = Gem_read (GEM_AE2_BASE+0x04);
  306. data3[0][1] = Gem_read (GEM_AE2_BASE+0x08);
  307. data4[0][1] = Gem_read (GEM_AE2_BASE+0x0c);
  308. data3[0][2] = Gem_read (GEM_AE2_BASE+0x10);
  309. data4[0][2] = Gem_read (GEM_AE2_BASE+0x14);
  310. data3[1][0] = Gem_read (GEM_AE2_BASE+0x18);
  311. data4[1][0] = Gem_read (GEM_AE2_BASE+0x1c);
  312. data3[1][1] = Gem_read (GEM_AE2_BASE+0x20);
  313. data4[1][1] = Gem_read (GEM_AE2_BASE+0x24);
  314. data3[1][2] = Gem_read (GEM_AE2_BASE+0x28);
  315. data4[1][2] = Gem_read (GEM_AE2_BASE+0x2c);
  316. data3[2][0] = Gem_read (GEM_AE2_BASE+0x30);
  317. data4[2][0] = Gem_read (GEM_AE2_BASE+0x34);
  318. data3[2][1] = Gem_read (GEM_AE2_BASE+0x38);
  319. data4[2][1] = Gem_read (GEM_AE2_BASE+0x3c);
  320. data3[2][2] = Gem_read (GEM_AE2_BASE+0x40);
  321. data4[2][2] = Gem_read (GEM_AE2_BASE+0x44);
  322. for (i = 0; i < 3; i++)
  323. {
  324. for (j = 0; j < 3; j++)
  325. {
  326. unsigned int tmp;
  327. p_ae->yavg_s[i][j] = (data3[i][j] >> 24);
  328. p_ae->histogram_s[i][j][0] = (data3[i][j] >> 0) & 0xfff;
  329. p_ae->histogram_s[i][j][1] = (data3[i][j] >> 12) & 0xfff;
  330. p_ae->histogram_s[i][j][3] = (data4[i][j] >> 0) & 0xfff;
  331. p_ae->histogram_s[i][j][4] = (data4[i][j] >> 12) & 0xfff;
  332. tmp = p_ae->histogram_s[i][j][0] + p_ae->histogram_s[i][j][1] + p_ae->histogram_s[i][j][3] + p_ae->histogram_s[i][j][4];
  333. if(tmp > 4095)
  334. {
  335. //printf ("%d > 4095\n", tmp);
  336. tmp = 4095;
  337. }
  338. //p_ae->histogram_s[i][j][2] = 4095 - p_ae->histogram_s[i][j][0] - p_ae->histogram_s[i][j][1]
  339. // - p_ae->histogram_s[i][j][3] - p_ae->histogram_s[i][j][4];
  340. p_ae->histogram_s[i][j][2] = 4095 - tmp;
  341. }
  342. }
  343. // ��ȡerisֱ��ͼ��Ϣ
  344. p_ae->eris_hist_thresh = Gem_read (GEM_ERIS_HIST_BASE+0x00);
  345. p_ae->eris_hist_statics[0] = Gem_read (GEM_ERIS_INFO_BASE+0x00);
  346. p_ae->eris_hist_statics[1] = Gem_read (GEM_ERIS_INFO_BASE+0x04);
  347. p_ae->eris_hist_statics[2] = Gem_read (GEM_ERIS_INFO_BASE+0x08);
  348. p_ae->eris_hist_statics[3] = Gem_read (GEM_ERIS_INFO_BASE+0x0c);
  349. p_ae->eris_hist_statics[4] = Gem_read (GEM_ERIS_INFO_BASE+0x10);
  350. p_ae->eris_yavg = Gem_read (GEM_ERIS_INFO_BASE+0x14);
  351. }
  352. void isp_ae_yavg_s_read (isp_ae_ptr_t p_ae)
  353. {
  354. p_ae->yavg_s[0][0] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x00) >> 24);
  355. p_ae->yavg_s[0][1] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x08) >> 24);
  356. p_ae->yavg_s[0][2] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x10) >> 24);
  357. p_ae->yavg_s[1][0] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x18) >> 24);
  358. p_ae->yavg_s[1][1] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x20) >> 24);
  359. p_ae->yavg_s[1][2] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x28) >> 24);
  360. p_ae->yavg_s[2][0] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x30) >> 24);
  361. p_ae->yavg_s[2][1] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x38) >> 24);
  362. p_ae->yavg_s[2][2] = (unsigned short)(Gem_read (GEM_AE2_BASE+0x40) >> 24);
  363. p_ae->eris_yavg = Gem_read (GEM_ERIS_INFO_BASE+0x14);
  364. }