camera.cpp
上传用户:qiulin1960
上传日期:2013-10-16
资源大小:2844k
文件大小:42k
源码类别:

Windows CE

开发平台:

Windows_Unix

  1. #include <windows.h>
  2. #include <nkintr.h>
  3. #include <oalintr.h>
  4. //#include <p2.h>
  5. #include <pm.h>
  6. #include "pmplatform.h"
  7. #include "s2440.h"
  8. #include "camif.h"
  9. #include "camera.h"
  10. #define MSG_EN_1 0
  11. #define MSG_EN_2 0
  12. //#define RETAILMSG(a,b) RETAILMSG(1,b)
  13. #define DOTNET_DRIVER 1 // 0:PPC, 1:CE.NET
  14. #define CAPTURE_TIME 30
  15. #define DISPLAY_SCHEDULE 0
  16. #define U8 unsigned char
  17. #define U16 unsigned short
  18. #define U32 unsigned int
  19. #define PORT_A 1
  20. #define PORT_B 0
  21. // Added 25 May 2004 for debug
  22. #ifdef DEBUG
  23. #define ZONE_INIT 1
  24. #define ZONE_THREAD 1
  25. #endif
  26. #define YCbCrtoR(Y,Cb,Cr) (1000*Y + 1540*(Cr-128))/1000
  27. #define YCbCrtoG(Y,Cb,Cr) (1000*Y - 459*(Cb-128) - 183*(Cr-128))/1000
  28. #define YCbCrtoB(Y,Cb,Cr) (1000*Y + 1856*(Cb-128))/1000
  29. volatile IOPreg  *s2440IOP = (IOPreg *)IOP_BASE;
  30. volatile CAMreg  *s2440CAM = (CAMreg *)CAM_BASE;
  31. volatile INTreg  *s2440INT = (INTreg *)INT_BASE;
  32. volatile CLKPWRreg *s2440PWR = (CLKPWRreg *)CLKPWR_BASE;
  33. volatile IICreg *s2440IIC = (IICreg *)IIC_BASE;;
  34. unsigned int DisplayTime = INFINITE;
  35. //unsigned char DisplayEnable=0;
  36. unsigned char buffer_num=0xff; // ping pong buffer
  37. unsigned char image_size = 2; // 1: QCIF, 2:CIF (default)
  38. //#define DRIVER_PREVIEW_ENABLE 2 // 0: onTimeTek, 1: Preview, 2:Samsung Camcorder
  39. unsigned char DRIVER_PREVIEW_ENABLE = 2; // 0: onTimeTek, 1: Preview, 2:Samsung Camcorder
  40. unsigned int frame_count=0; // for MPEG4
  41. DWORD Tick_GET_FRAME_CUR;
  42. DWORD Tick_GET_FRAME_PREV;
  43. DWORD Tick_COPY_FRAME;
  44. HANDLE CameraThread;
  45. HANDLE CameraEvent;
  46. static BOOL mInitialized = FALSE;
  47. unsigned char codec_flag=0;
  48. unsigned char rgb_flag=0;
  49. unsigned int y_address,cb_address,cr_address;
  50. unsigned int rgb_address;
  51. extern void Camera_Initialize();
  52. extern volatile IOPreg *s2440IOP;
  53. extern volatile IICreg *s2440IIC;
  54. void Virtual_Alloc(); // Virtual allocation
  55. void Camera_Clock(unsigned char divide); // set default value
  56. void Camif_Capture(int cap_a, int cap_b); // A port, B port
  57. void Display_Cam_Image(U32 pos_x, U32 pos_y, U32 size_x, U32 size_y, U8 port);
  58. void Copy_Cam_Image(U8 * pBufOut, U32 size_x, U32 size_y, U8 port);
  59. void Samsung_camcoder(U8 *pBufOut);
  60. void Samsung_camcoder_pr(U8 *pBufOut);
  61. void Buffer_codec_info_update();
  62. void Buffer_preview_info_update();
  63. BOOL Cam_Init();
  64. BOOL CamClockOn(BOOL bOnOff);
  65. //void CamCaptureStart(U32 mode);
  66. void CamInit(U32 CoDstWidth, U32 CoDstHeight, U32 PrDstWidth, U32 PrDstHeight, 
  67. U32 WinHorOffset, U32 WinVerOffset,  U32 CoFrameBuffer, U32 PrFrameBuffer);
  68. DWORD CameraCaptureThread(void);
  69. BOOL InitInterruptThread();
  70. CEDEVICE_POWER_STATE m_Dx;
  71. #ifdef DEBUG
  72. DBGPARAM dpCurSettings = {
  73.     TEXT("CAMERA"), {
  74.         TEXT("0"),TEXT("1"),TEXT("2"),TEXT("3"),
  75.         TEXT("4"),TEXT("5"),TEXT("6"),TEXT("7"),
  76.         TEXT("8"),TEXT("9"),TEXT("10"),TEXT("11"),
  77.         TEXT("12"),TEXT("Function"),TEXT("Init"),TEXT("Error")},
  78.     0x8000  // Errors only, by default
  79. }; 
  80. #endif
  81. static void Delay(USHORT count)
  82. {
  83. volatile int i, j = 0;
  84. volatile static int loop = S2440FCLK/100000;
  85. for(;count > 0;count--)
  86. for(i=0;i < loop; i++) { j++; }
  87. }
  88. /********************************************************
  89.  CalculateBurstSize - Calculate the busrt lengths
  90.  
  91.  Description:
  92.  - dstHSize: the number of the byte of H Size.
  93.  
  94. */
  95. void CalculateBurstSize(unsigned int hSize,unsigned int *mainBurstSize,unsigned int *remainedBurstSize)
  96. {
  97. unsigned int tmp;
  98. tmp=(hSize/4)%16;
  99. switch(tmp) {
  100. case 0:
  101. *mainBurstSize=16;
  102. *remainedBurstSize=16;
  103. break;
  104. case 4:
  105. *mainBurstSize=16;
  106. *remainedBurstSize=4;
  107. break;
  108. case 8:
  109. *mainBurstSize=16;
  110. *remainedBurstSize=8;
  111. break;
  112. default: 
  113. tmp=(hSize/4)%8;
  114. switch(tmp) {
  115. case 0:
  116. *mainBurstSize=8;
  117. *remainedBurstSize=8;
  118. break;
  119. case 4:
  120. *mainBurstSize=8;
  121. *remainedBurstSize=4;
  122. default:
  123. *mainBurstSize=4;
  124. tmp=(hSize/4)%4;
  125. *remainedBurstSize= (tmp) ? tmp: 4;
  126. break;
  127. }
  128. break;
  129. }          
  130. }
  131. /********************************************************
  132.  CalculatePrescalerRatioShift - none
  133.  
  134.  Description:
  135.  - none
  136.  
  137. */
  138. void CalculatePrescalerRatioShift(unsigned int SrcSize, unsigned int DstSize, unsigned int *ratio,unsigned int *shift)
  139. {
  140. if(SrcSize>=64*DstSize) {
  141. // Uart_Printf("ERROR: out of the prescaler range: SrcSize/DstSize = %d(< 64)rn",SrcSize/DstSize);
  142. while(1);
  143. }
  144. else if(SrcSize>=32*DstSize) {
  145. *ratio=32;
  146. *shift=5;
  147. }
  148. else if(SrcSize>=16*DstSize) {
  149. *ratio=16;
  150. *shift=4;
  151. }
  152. else if(SrcSize>=8*DstSize) {
  153. *ratio=8;
  154. *shift=3;
  155. }
  156. else if(SrcSize>=4*DstSize) {
  157. *ratio=4;
  158. *shift=2;
  159. }
  160. else if(SrcSize>=2*DstSize) {
  161. *ratio=2;
  162. *shift=1;
  163. }
  164. else {
  165. *ratio=1;
  166. *shift=0;
  167. }    
  168. }
  169. void Camera_Clock(unsigned char divide)
  170. {
  171. unsigned int camclk;
  172. // Set camera clock for camera processor
  173. s2440PWR->rCAMDIVN = (s2440PWR->rCAMDIVN & ~(0x1f)) | (1<<4) | (divide & 0xf); // CAMCLK is divided..
  174. camclk = 48000000/(((s2440PWR->rCAMDIVN&0xf) + 1)*2);
  175. RETAILMSG(1,(TEXT("CAM clock:%drn"),camclk));
  176. }
  177. /*
  178. void CamCaptureStart(unsigned int mode)
  179. if(mode&CAM_CODEC_SCALER_CAPTURE_ENABLE_BIT) {
  180. s2440CAM->rCICOSCCTRL|=CAM_CODEC_SACLER_START_BIT;
  181. }
  182. if(mode&CAM_PVIEW_SCALER_CAPTURE_ENABLE_BIT) {
  183. s2440CAM->rCIPRSCCTRL|=CAM_PVIEW_SACLER_START_BIT;
  184. }
  185.     
  186. s2440CAM->rCIIMGCPT|=(CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT|mode);
  187. }
  188. */
  189. void Camif_Capture(int cap_a, int cap_b)
  190. {
  191. //RETAILMSG(1,(_T("Camif_Capture(%d, %d)rn"), cap_a, cap_b));
  192. // S3C2440A
  193. s2440CAM->rCIIMGCPT &= ~(CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT);
  194. if(cap_b == CAPTURE_ON) 
  195. {
  196. //Codec capture start
  197. s2440CAM->rCICOSCCTRL |=(CAM_CODEC_SACLER_START_BIT);
  198. s2440CAM->rCIIMGCPT |=(CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT)|(CAM_CODEC_SCALER_CAPTURE_ENABLE_BIT);
  199. else if (cap_b == CAPTURE_OFF)
  200. {
  201. s2440CAM->rCICOSCCTRL &= ~(CAM_CODEC_SACLER_START_BIT);
  202. s2440CAM->rCIIMGCPT &= ~(CAM_CODEC_SCALER_CAPTURE_ENABLE_BIT);
  203. }
  204. if(cap_a == CAPTURE_ON) 
  205. {  
  206. // Preview capture Start
  207. s2440CAM->rCIPRSCCTRL |=(CAM_PVIEW_SACLER_START_BIT);
  208. s2440CAM->rCIIMGCPT |=(CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT)|(CAM_PVIEW_SCALER_CAPTURE_ENABLE_BIT);
  209. }
  210. else if (cap_a == CAPTURE_OFF)
  211. {
  212. s2440CAM->rCIPRSCCTRL &= ~(CAM_PVIEW_SACLER_START_BIT);
  213. s2440CAM->rCIIMGCPT &= ~(CAM_PVIEW_SCALER_CAPTURE_ENABLE_BIT);
  214. }
  215. if ((cap_a == CAPTURE_ON) || (cap_b == CAPTURE_ON))
  216. {
  217. s2440CAM->rCIIMGCPT |= (CAM_CAMIF_GLOBAL_CAPTURE_ENABLE_BIT);
  218. }
  219. }
  220. DWORD CameraCaptureThread(void)
  221. {
  222. unsigned char tmp=0;
  223. static unsigned int time,old_time;
  224. static unsigned int cam_intr;
  225. while(1)
  226. {
  227. WaitForSingleObject(CameraEvent, DisplayTime);
  228. RETAILMSG(MSG_EN_1,(_T("CameraCaptureThread(%d)++rn"), frame_count));
  229. #if 0
  230. if (frame_count <= 2) {
  231. frame_count++;
  232. // Enable camera interrupt
  233. s2440INT->rINTSUBMSK &= ~(BIT_SUB_CAM_P|BIT_SUB_CAM_C);
  234. s2440INT->rINTMSK &= ~BIT_CAM;
  235. continue;
  236. }
  237. #endif
  238. //if( DisplayEnable )
  239. {
  240. frame_count++;
  241. if (s2440INT->rINTSUBMSK & BIT_SUB_CAM_C)
  242. {
  243. cam_intr = BIT_SUB_CAM_C;
  244. //RETAILMSG(1,(_T("CAM_C, ts %drn"), GetTickCount()));
  245. }
  246. else if (s2440INT->rINTSUBMSK & BIT_SUB_CAM_P)
  247. {
  248. cam_intr = BIT_SUB_CAM_P;
  249. //RETAILMSG(1,(_T("CAM_P, ts %drn"), GetTickCount()));
  250. }
  251. // EINT20 to measure time
  252. // s2440IOP->rGPGDAT |= (1<<12);
  253. // time = GetTickCount();
  254. // RETAILMSG(1,(TEXT("+time:%drn"),(time - old_time)));
  255. // delay for capture
  256. //Sleep(CAPTURE_TIME); // polling mode
  257. // display the image
  258. if ((DRIVER_PREVIEW_ENABLE == 1) & (cam_intr == BIT_SUB_CAM_P))
  259. Display_Cam_Image(64, 64, QCIF_XSIZE, QCIF_YSIZE, PORT_A);
  260. //else if (DRIVER_PREVIEW_ENABLE == 2)
  261. if (cam_intr == BIT_SUB_CAM_C)
  262. {
  263. Buffer_codec_info_update();
  264. }
  265. if (cam_intr == BIT_SUB_CAM_P)
  266. {
  267. Buffer_preview_info_update();
  268. }
  269. // Enable camera interrupt
  270. s2440INT->rINTSUBMSK &= ~(BIT_SUB_CAM_P|BIT_SUB_CAM_C);
  271. s2440INT->rINTMSK &= ~BIT_CAM;
  272. /*
  273. if (DRIVER_PREVIEW_ENABLE == 1)
  274. Camif_Capture(CAPTURE_ON, CAPTURE_OFF);
  275. else if (DRIVER_PREVIEW_ENABLE == 2)
  276. Camif_Capture(CAPTURE_OFF, CAPTURE_ON);
  277. */
  278. // EINT20 to measure time
  279. // s2440IOP->rGPGDAT &= ~(1<<12);
  280. // old_time = GetTickCount();
  281. // RETAILMSG(1,(TEXT("-time:%drn"),(old_time-time)));
  282. }
  283. }
  284. }
  285. void Virtual_Alloc()
  286. {
  287.     // GPIO Virtual alloc
  288. s2440IOP = (volatile IOPreg *) VirtualAlloc(0,sizeof(IOPreg),MEM_RESERVE, PAGE_NOACCESS);
  289. if(s2440IOP == NULL) {
  290. RETAILMSG(1,(TEXT("For s2440IOP: VirtualAlloc failed!rn")));
  291. }
  292. else {
  293. if(!VirtualCopy((PVOID)s2440IOP,(PVOID)(IOP_BASE),sizeof(IOPreg),PAGE_READWRITE | PAGE_NOCACHE )) {
  294. RETAILMSG(1,(TEXT("For s2440IOP: VirtualCopy failed!rn")));
  295. }
  296. }
  297.     // IIC Virtual alloc
  298. s2440IIC = (volatile IICreg *) VirtualAlloc(0,sizeof(IICreg),MEM_RESERVE, PAGE_NOACCESS);
  299. if(s2440IIC == NULL) {
  300. RETAILMSG(1,(TEXT("For s2440IIC: VirtualAlloc failed!rn")));
  301. }
  302. else {
  303. if(!VirtualCopy((PVOID)s2440IIC,(PVOID)(IIC_BASE),sizeof(IICreg),PAGE_READWRITE | PAGE_NOCACHE )) {
  304. RETAILMSG(1,(TEXT("For s2440IIC: VirtualCopy failed!rn")));
  305. }
  306. }
  307.     // Camera Virtual alloc
  308. s2440CAM = (volatile CAMreg *) VirtualAlloc(0,sizeof(CAMreg),MEM_RESERVE, PAGE_NOACCESS);
  309. if(s2440CAM == NULL) {
  310. RETAILMSG(1,(TEXT("For s2440CAM: VirtualAlloc failed!rn")));
  311. }
  312. else {
  313. if(!VirtualCopy((PVOID)s2440CAM,(PVOID)(CAM_BASE),sizeof(CAMreg),PAGE_READWRITE | PAGE_NOCACHE )) {
  314. RETAILMSG(1,(TEXT("For s2440CAM: VirtualCopy failed!rn")));
  315. }
  316. }
  317. // Interrupt Virtual alloc
  318. s2440INT = (volatile INTreg *) VirtualAlloc(0,sizeof(INTreg),MEM_RESERVE, PAGE_NOACCESS);
  319. if(s2440INT == NULL) {
  320. RETAILMSG(1,(TEXT("For s2440INT: VirtualAlloc failed!rn")));
  321. }
  322. else {
  323. if(!VirtualCopy((PVOID)s2440INT,(PVOID)(INT_BASE),sizeof(INTreg),PAGE_READWRITE | PAGE_NOCACHE )) {
  324. RETAILMSG(1,(TEXT("For s2440INT: VirtualCopy failed!rn")));
  325. }
  326. }
  327. // PWM clock Virtual alloc
  328. s2440PWR = (volatile CLKPWRreg *) VirtualAlloc(0,sizeof(CLKPWRreg),MEM_RESERVE, PAGE_NOACCESS);
  329. if(s2440PWR == NULL) {
  330. RETAILMSG(1,(TEXT("For s2440PWR: VirtualAlloc failed!rn")));
  331. }
  332. else {
  333. if(!VirtualCopy((PVOID)s2440PWR,(PVOID)(CLKPWR_BASE),sizeof(CLKPWRreg),PAGE_READWRITE | PAGE_NOCACHE )) {
  334. RETAILMSG(1,(TEXT("For s2440PWR: VirtualCopy failed!rn")));
  335. }
  336. }
  337. }
  338. void Display_Cam_Image(U32 pos_x, U32 pos_y, U32 size_x, U32 size_y, U8 port)
  339. {
  340. U8 *buffer_rgb;
  341. U32 y;
  342. int temp;
  343. //unsigned short *ptr = (unsigned short *)(FRAMEBUF_BASE+0x5dc0);
  344. static unsigned short transfer_data[QCIF_XSIZE*QCIF_YSIZE];
  345. static unsigned int time,old_time;
  346. //RETAILMSG(1,(_T("Display_Cam_Image()rn")));
  347. // if (port)
  348. {
  349. temp = (s2440CAM->rCIPRSTATUS>>26)&3;
  350. temp = (temp + 2) % 4;
  351. switch (temp)
  352. {
  353. case 0:
  354. buffer_rgb = (U8 *)s2440CAM->rCIPRCLRSA1;
  355. break;
  356. case 1:
  357. buffer_rgb = (U8 *)s2440CAM->rCIPRCLRSA2;
  358. break;
  359. case 2:
  360. buffer_rgb = (U8 *)s2440CAM->rCIPRCLRSA3;
  361. break;
  362. case 3:
  363. buffer_rgb = (U8 *)s2440CAM->rCIPRCLRSA4;
  364. break;
  365. default :
  366. buffer_rgb = (U8 *)s2440CAM->rCIPRCLRSA1;
  367. break;
  368. }
  369. }
  370. RETAILMSG(MSG_EN_1,(_T("preview buf index = %drn"), temp));
  371. #if (DOTNET_DRIVER)
  372. SetKMode(TRUE);
  373. #endif
  374. buffer_rgb += VIRTUAL_OFFSET;
  375. // time = GetTickCount();
  376. // RETAILMSG(1,(TEXT("+:%drn"),(time - old_time)));
  377. #if 1
  378. for (y=0;y<(size_y-2);y++) // YCbCr 4:2:0 format
  379. {
  380. //memcpy((void *)(FRAMEBUF_BASE+0x5e00+y*240*2),(void *)buffer_rgb,(QCIF_XSIZE)*2);
  381. memcpy((void *)(FRAMEBUF_BASE+ (240*pos_y + pos_x) + y*240*2),(void *)buffer_rgb,(QCIF_XSIZE)*2);
  382. buffer_rgb += (QCIF_XSIZE*2);
  383. }
  384. #endif
  385. #if (DOTNET_DRIVER)
  386. SetKMode(FALSE);
  387. #endif
  388. // old_time = GetTickCount();
  389. // RETAILMSG(1,(TEXT("-:%drn"),(old_time - time)));
  390. }
  391. BOOL WINAPI  
  392. DllEntry(HANDLE hinstDLL, 
  393. DWORD dwReason, 
  394. LPVOID /* lpvReserved */)
  395. {
  396. switch(dwReason)
  397. {
  398. case DLL_PROCESS_ATTACH:
  399. DEBUGREGISTER((HINSTANCE)hinstDLL);
  400. DEBUGMSG(ZONE_INIT,(TEXT("CAMERA: DLL_PROCESS_ATTACHrn")));
  401. return TRUE;
  402. case DLL_THREAD_ATTACH:
  403. DEBUGMSG(ZONE_THREAD,(TEXT("CAMERA: DLL_THREAD_ATTACHrn")));
  404. break;
  405. case DLL_THREAD_DETACH:
  406. DEBUGMSG(ZONE_THREAD,(TEXT("CAMERA: DLL_THREAD_DETACHrn")));
  407. break;
  408. case DLL_PROCESS_DETACH:
  409. DEBUGMSG(ZONE_INIT,(TEXT("CAMERA: DLL_PROCESS_DETACHrn")));
  410. break;
  411. #ifdef UNDER_CE
  412. case DLL_PROCESS_EXITING:
  413. DEBUGMSG(ZONE_INIT,(TEXT("CAMERA: DLL_PROCESS_EXITINGrn")));
  414. break;
  415. case DLL_SYSTEM_STARTED:
  416. DEBUGMSG(ZONE_INIT,(TEXT("CAMERA: DLL_SYSTEM_STARTEDrn")));
  417. break;
  418. #endif
  419. }
  420. return TRUE;
  421. }
  422. //-----------------------------------------------------------------------------
  423. //-----------------------------------------------------------------------------
  424. BOOL CIS_Deinit(DWORD hDeviceContext)
  425. {
  426. BOOL bRet = TRUE;
  427. RETAILMSG(1,(TEXT("CAMERA: CIS_Deinitrn")));
  428. s2440INT->rINTMSK |= BIT_CAM;
  429. s2440INT->rINTSUBMSK |= (BIT_SUB_CAM_P|BIT_SUB_CAM_C);
  430. Camif_Capture(CAPTURE_OFF, CAPTURE_OFF);
  431. //DisplayEnable = 0;
  432. DRIVER_PREVIEW_ENABLE = 2;
  433. CloseHandle(CameraThread);
  434. VirtualFree((void*)s2440IOP, sizeof(IOPreg), MEM_RELEASE);
  435. VirtualFree((void*)s2440CAM, sizeof(CAMreg), MEM_RELEASE);
  436. VirtualFree((void*)s2440INT, sizeof(INTreg), MEM_RELEASE);
  437. VirtualFree((void*)s2440PWR, sizeof(CLKPWRreg), MEM_RELEASE);
  438. VirtualFree((void*)s2440IIC, sizeof(IICreg), MEM_RELEASE);
  439. return TRUE;
  440. BOOL InitInterruptThread()
  441. {
  442. DWORD         threadID;                         // thread ID
  443. BOOL bSuccess;
  444.     CameraEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
  445.     
  446.     if (!CameraEvent)
  447.     {
  448.      return FALSE;
  449.     }
  450. bSuccess = InterruptInitialize(SYSINTR_CAM, CameraEvent, NULL, 0);
  451.     if (!bSuccess) 
  452.     {
  453.         RETAILMSG(1,(TEXT("Fail to initialize camera interrupt eventrn")));
  454.         return FALSE;
  455.     }    
  456.     CameraThread = CreateThread(NULL,
  457.                                  0,
  458.                                  (LPTHREAD_START_ROUTINE)CameraCaptureThread,
  459.                                  0,
  460.                                  0,
  461.                                  &threadID);
  462.     
  463.     if (NULL == CameraThread ) {
  464.      RETAILMSG(1,(TEXT("Create Camera Thread Failrn")));
  465.     }
  466. RETAILMSG(1,(_T("CAMERA.DLL::InterruptThread Initialized.rn")));
  467. return TRUE;
  468. }
  469. BOOL CamClockOn(BOOL bOnOff)
  470. {
  471. // Camera clock
  472. if (!bOnOff)
  473. s2440PWR->rCLKCON &= ~(1<<19); // Camera clock disable
  474. s2440PWR->rCLKCON |= (1<<19); // Camera clock enable
  475. Camera_Clock(CAM_CLK_DIV);
  476. RETAILMSG(1,(_T("CamClockOn = %drn"), bOnOff));
  477. Delay(1000);
  478. return TRUE;
  479. }
  480. BOOL CamGpioInit()
  481. {
  482. s2440IOP->rGPJCON = 0x2aaaaaa;
  483. s2440IOP->rGPJDAT = 0;
  484. s2440IOP->rGPJUP = 0x1fff;
  485. return TRUE;
  486. }
  487. void CAM_IF_Reset()
  488. {
  489. // This functin is used on power handler operation.
  490. // So, you should not use Kernel API functions as like as "Sleep()".
  491. //
  492. // Camera (FIMC2.0) I/F Reset
  493. //
  494. //s2440CAM->rCTRL_C = (1<<19); // 2440X
  495. s2440CAM->rCIGCTRL |= (1<<31);
  496. // Don't modify this delay time
  497. RETAILMSG(1,(TEXT("Camera I/F Resetrn")));
  498. Delay(100);
  499. //s2440CAM->rCTRL_C = (0<<19);
  500. s2440CAM->rCIGCTRL &= ~(1<<31);
  501. // Wait for Camera module initialization
  502. Delay(1000);
  503. }
  504. void Camera_Module_Reset()
  505. {
  506. //s2440CAM->rCTRL_C = (1<<19); // 2440X
  507. s2440CAM->rCIGCTRL |= (1<<30);
  508. // Don't modify this delay time
  509. RETAILMSG(1,(TEXT("Camera Module Resetrn")));
  510. //Delay(10);
  511. Sleep(1);
  512. //s2440CAM->rCTRL_C = (0<<19);
  513. s2440CAM->rCIGCTRL &= ~(1<<30);
  514. // Wait for Camera module initialization
  515. //Delay(100);
  516. Sleep(10);
  517. // Samsung Camera need delay time between camera clock enable and camera reset.
  518. //RETAILMSG(1,(TEXT("You need delay timern")));
  519. //Delay(1000);
  520. Sleep(10);
  521. }
  522. BOOL Cam_Init()
  523. {
  524.     //
  525.     // 1. Camera IO setup
  526.     //
  527. CamGpioInit();
  528. // 2. Camera i/f reset
  529.     CAM_IF_Reset();
  530.     //
  531.     // 3. Camera Clock setup
  532.     //
  533. CamClockOn(TRUE);
  534. // 4. Camera Module Reset
  535. Camera_Module_Reset();
  536. // 5. set register of camera module through IIC 
  537. Camera_Initialize();
  538. // to check time
  539. s2440IOP->rGPGCON &= ~(0x3<<24);
  540. s2440IOP->rGPGCON |= (0x1<<24); // EINT20
  541. if (image_size == 1)
  542. CamInit(QCIF_XSIZE, QCIF_YSIZE, QCIF_XSIZE, QCIF_YSIZE, 112, 20, COPIFRAMEBUFFER_B, COPIFRAMEBUFFER_A);
  543. else if (image_size == 2)
  544. CamInit(CIF_XSIZE, CIF_YSIZE, QCIF_XSIZE, QCIF_YSIZE, 112, 20, COPIFRAMEBUFFER_B, COPIFRAMEBUFFER_A);
  545. //RETAILMSG(1,(_T("CamInit().. donern")));
  546. return TRUE;
  547. }
  548. DWORD CIS_Init(DWORD dwContext)
  549. {
  550. // 1. Virtual Alloc
  551. Virtual_Alloc();
  552. Cam_Init();
  553. if (!InitInterruptThread())
  554. {
  555.         RETAILMSG(1,(TEXT("Fail to initialize camera interrupt eventrn")));
  556.         return FALSE;
  557.     }    
  558. m_Dx = (_CEDEVICE_POWER_STATE)D0;
  559. DevicePowerNotify(_T("CIS1:"),(_CEDEVICE_POWER_STATE)D0, POWER_NAME);
  560. mInitialized = TRUE;
  561. return TRUE;
  562. }
  563. //-----------------------------------------------------------------------------
  564. //-----------------------------------------------------------------------------
  565. BOOL CIS_IOControl(DWORD hOpenContext, 
  566.    DWORD dwCode, 
  567.    PBYTE pBufIn, 
  568.    DWORD dwLenIn, 
  569.    PBYTE pBufOut, 
  570.    DWORD dwLenOut, 
  571.    PDWORD pdwActualOut)
  572. {
  573.     BOOL RetVal = TRUE;
  574.     DWORD dwErr = ERROR_SUCCESS;    
  575. static unsigned int time=0,old_time=0;
  576. switch (dwCode)
  577. {
  578. //-----------------------------------------------------------------------------------------
  579. case IOCTL_POWER_CAPABILITIES: 
  580.         {
  581.             PPOWER_CAPABILITIES ppc;
  582. RETAILMSG(1, (TEXT("CIS: IOCTL_POWER_CAPABILITIESrn")));   
  583.             
  584. if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(POWER_CAPABILITIES)) ) {
  585.                 RetVal = FALSE;
  586.                 dwErr = ERROR_INVALID_PARAMETER;
  587.                 break;
  588.             }
  589.             ppc = (PPOWER_CAPABILITIES)pBufOut;
  590.             
  591.             memset(ppc, 0, sizeof(POWER_CAPABILITIES));
  592.             // support D0, D4 
  593.             ppc->DeviceDx = 0x11;
  594.             // Report our power consumption in uAmps rather than mWatts. 
  595.             ppc->Flags = POWER_CAP_PREFIX_MICRO | POWER_CAP_UNIT_AMPS;
  596.             
  597. // 25 m = 25000 uA
  598.             // TODO: find out a more accurate value
  599. ppc->Power[D0] = 25000;
  600.             
  601.             *pdwActualOut = sizeof(POWER_CAPABILITIES);
  602.         } break;
  603. case IOCTL_POWER_SET: 
  604.         {
  605.             CEDEVICE_POWER_STATE NewDx;
  606.             if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(CEDEVICE_POWER_STATE)) ) {
  607.                 RetVal = FALSE;
  608.                 dwErr = ERROR_INVALID_PARAMETER;
  609.                 break;
  610.             }
  611.             
  612.             NewDx = *(PCEDEVICE_POWER_STATE)pBufOut;
  613.             if ( VALID_DX(NewDx) ) {
  614.                 switch ( NewDx ) {
  615.                 case D0:
  616.                     if (m_Dx != D0) {
  617.                         CIS_PowerUp(hOpenContext);
  618.                         m_Dx = D0;
  619.                     }
  620.                     break;
  621.                 default:
  622.                     if (m_Dx != (_CEDEVICE_POWER_STATE)D4) {
  623.                         CIS_PowerDown(hOpenContext);
  624.                         m_Dx = (_CEDEVICE_POWER_STATE)D4;
  625.                     }
  626.                     break;
  627.                 }
  628.                 // return our state
  629.                 *(PCEDEVICE_POWER_STATE)pBufOut = m_Dx;
  630.                 RETAILMSG(1, (TEXT("CIS: IOCTL_POWER_SET: D%u rn"), NewDx));
  631.                 *pdwActualOut = sizeof(CEDEVICE_POWER_STATE);
  632.             } else {
  633.                 RetVal = FALSE;
  634.                 dwErr = ERROR_INVALID_PARAMETER;
  635.             }
  636.             
  637.         } break;
  638.         case IOCTL_POWER_GET: 
  639.             if ( !pdwActualOut || !pBufOut || (dwLenOut < sizeof(CEDEVICE_POWER_STATE)) ) {
  640.                 RetVal = FALSE;
  641.                 dwErr = ERROR_INVALID_PARAMETER;
  642.                 break;
  643.             }
  644. *(PCEDEVICE_POWER_STATE)pBufOut = m_Dx;
  645.             RETAILMSG(1, (TEXT("CIS: IOCTL_POWER_GET: D%u rn"), m_Dx));
  646.             *pdwActualOut = sizeof(CEDEVICE_POWER_STATE);
  647.         break;
  648. //-----------------------------------------------------------------------------------------
  649. case IOCTL_CAM_SHOW :
  650. // Charlie. Show Menu
  651. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SHOW(%x)rn"),dwLenIn));
  652. break;
  653. case IOCTL_CAM_HIDE :
  654. // Charlie. Close display window
  655. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_HIDErn")));
  656. break;
  657. case IOCTL_CAM_SETPOS : 
  658. time = GetTickCount();
  659. // RETAILMSG(MSG_EN_1,(TEXT("Capture time:%d msecrn"), (time-old_time)));
  660. RETAILMSG(MSG_EN_1,(TEXT("Capture time:%d msecrn"), (time)));
  661. old_time = time;
  662. break;
  663. case CAM_IOCTL_MOVIE_START: // for MPEG4
  664. case IOCTL_CAM_CONT : 
  665. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_CONTrn")));
  666. // Charlie. Play
  667. //DisplayEnable = 1;
  668. // Enable camera interrupt
  669. s2440INT->rINTMSK &= ~BIT_CAM;
  670. s2440INT->rINTSUBMSK &= ~(BIT_SUB_CAM_P|BIT_SUB_CAM_C);
  671. #if 0
  672. if (DRIVER_PREVIEW_ENABLE == 1)
  673. {
  674. Camif_Capture(CAPTURE_ON, CAPTURE_OFF);
  675. else if (DRIVER_PREVIEW_ENABLE == 2)
  676. {
  677. Camif_Capture(CAPTURE_OFF, CAPTURE_ON);
  678. }
  679. #else
  680. Camif_Capture(CAPTURE_ON, CAPTURE_ON);
  681. #endif
  682. //DisplayTime = DISPLAY_SCHEDULE; // polling mode
  683. //SetEvent(CameraEvent);
  684. s2440IOP->rGPGCON &= ~(0x3<<24);
  685. s2440IOP->rGPGCON |= (0x1<<24); // EINT20
  686. frame_count = 0; // for MPEG4
  687. break;
  688. case CAM_IOCTL_MOVIE_STOP: // for MPEG4
  689. case IOCTL_CAM_STOP : 
  690. // Charlie. Stop
  691. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_STOPrn")));
  692. // Disable camera interrupt
  693. s2440INT->rINTMSK |= BIT_CAM;
  694. s2440INT->rINTSUBMSK |= (BIT_SUB_CAM_P|BIT_SUB_CAM_C);
  695. if (s2440INT->rINTPND & BIT_CAM) s2440INT->rINTPND |= BIT_CAM;
  696. s2440INT->rSRCPND |= BIT_CAM;
  697. s2440INT->rSUBSRCPND |= (BIT_SUB_CAM_P|BIT_SUB_CAM_C);
  698. Camif_Capture(CAPTURE_OFF, CAPTURE_OFF);
  699. //DisplayEnable = 0;
  700. DRIVER_PREVIEW_ENABLE = 2;
  701. DisplayTime = INFINITE;
  702. break;
  703. // for MPEG4
  704. case CAM_IOCTL_GET_LATEST_FRAME:
  705. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:CAM_IOCTL_GET_LATEST_FRAMErn")));
  706. //RETAILMSG(MSG_EN_1,(TEXT("pBufIn = 0x%xrn"), pBufIn));
  707. //RETAILMSG(MSG_EN_1,(TEXT("no function available, TBDrn")));
  708. if (frame_count < 2) {
  709. RETAILMSG(1,(TEXT("CAMERA:CAM_IOCTL_GET_LATEST_FRAME - frame not available!!!rn")));
  710. return false;
  711. }
  712. Tick_GET_FRAME_PREV = Tick_GET_FRAME_CUR;
  713. Tick_GET_FRAME_CUR = GetTickCount();
  714. Copy_Cam_Image(pBufOut, QCIF_XSIZE, QCIF_YSIZE, PORT_A);
  715. break;
  716. case CAM_IOCTL_SAMSUNG_CAM: // ID=0x520
  717. RETAILMSG(MSG_EN_1,(_T("CAM_IOCTL_SAMSUNG_CAMrn")));
  718. Samsung_camcoder(pBufOut);
  719. break;
  720. case CAM_IOCTL_SAMSUNG_CAM_PR: // ID=0x522
  721. RETAILMSG(MSG_EN_1,(_T("CAM_IOCTL_SAMSUNG_CAM_PRrn")));
  722. Samsung_camcoder_pr(pBufOut);
  723. break;
  724. case CAM_IOCTL_SAMSUNG_PREVIEW_START : // ID=0x523
  725. DRIVER_PREVIEW_ENABLE = 1;
  726. RETAILMSG(MSG_EN_1,(_T("CAM_IOCTL_SAMSUNG_PREVIEW_START(%x)rn"),*pBufOut, DRIVER_PREVIEW_ENABLE));
  727. break;
  728. case CAM_IOCTL_SAMSUNG_PREVIEW_STOP : // ID=0x524
  729. DRIVER_PREVIEW_ENABLE = 2;
  730. RETAILMSG(MSG_EN_1,(_T("CAM_IOCTL_SAMSUNG_PREVIEW_STOP(%x)rn"),*pBufOut, DRIVER_PREVIEW_ENABLE));
  731. break;
  732. case IOCTL_CAM_SETGAMMA :
  733. if( *pBufOut == 1 ){
  734. DRIVER_PREVIEW_ENABLE = 1;
  735. }
  736. else if( *pBufOut == 2 ){
  737. DRIVER_PREVIEW_ENABLE = 2;
  738. }
  739. RETAILMSG(MSG_EN_1,(_T("IOCTL_CAM_SETGAMMA:*pBufOut(%x):DRIVER_PREVIEW_ENABLE(%x)rn"),*pBufOut, DRIVER_PREVIEW_ENABLE));
  740. break;
  741. case CAM_IOCTL_IMAGE_SIZE: // ID=0x521
  742. if( *pBufOut == 1 ){
  743. image_size = 1;
  744. }
  745. else if( *pBufOut == 2 ){
  746. image_size = 2;
  747. }
  748. RETAILMSG(MSG_EN_1,(TEXT("image_size:%d,0x%x,0x%xrn"),image_size,pBufOut,*pBufOut));
  749. s2440INT->rINTMSK |= BIT_CAM;
  750. s2440INT->rINTSUBMSK |= (BIT_SUB_CAM_P|BIT_SUB_CAM_C);
  751. Camif_Capture(CAPTURE_OFF, CAPTURE_OFF);
  752. if (image_size == 1)
  753. CamInit(QCIF_XSIZE, QCIF_YSIZE, QCIF_XSIZE, QCIF_YSIZE, 112, 20, COPIFRAMEBUFFER_B, COPIFRAMEBUFFER_A);
  754. else if (image_size == 2)
  755. CamInit(CIF_XSIZE, CIF_YSIZE, QCIF_XSIZE, QCIF_YSIZE, 112, 20, COPIFRAMEBUFFER_B, COPIFRAMEBUFFER_A);
  756. break;
  757. case IOCTL_CAM_COPY :
  758. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_COPY(dwLenIn:%x)rn"), dwLenIn));
  759. break;
  760. case IOCTL_CAM_SETCOLKEY :
  761. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETCOLKEYrn")));
  762. break;
  763. case IOCTL_CAM_SETALPHA :
  764. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETALPHArn")));
  765. break;
  766. case IOCTL_CAM_GETINFO :
  767. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_GETINFOrn")));
  768. break;
  769. case IOCTL_CAM_SETSCALE :
  770. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETSCALErn")));
  771. break;
  772. case IOCTL_CAM_SETHUE :
  773. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETHUErn")));
  774. break;
  775. #if 0
  776. case IOCTL_CAM_SETGAMMA :
  777. // Get which ping -pong buffer have data
  778. //DisplayEnable = 0;
  779. Sleep(200);
  780. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_BUFFER:%drn"),buffer_num));
  781. break;
  782. #endif
  783. case IOCTL_CAM_SETWBCOORD : 
  784. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETWBCOORDrn")));
  785. break;
  786. case IOCTL_CAM_SETAELIMIT :
  787. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETAELIMITrn")));
  788. break;
  789. case IOCTL_CAM_SETADCOFS :
  790. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETADCOFSrn")));
  791. break;
  792. case IOCTL_CAM_SETWBGAIN :
  793. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETWBGAINrn")));
  794. break;
  795. case IOCTL_CAM_SETCBCRGAIN :
  796. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETCBCRGAINrn")));
  797. break;
  798. case IOCTL_CAM_SETLEGAIN :
  799. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETLEGAINrn")));
  800. break;
  801. case IOCTL_CAM_SETBRIGHTNESS :
  802. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETBRIGHTNESSrn")));
  803. break;
  804. case IOCTL_CAM_SETCLIP :
  805. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETCLIPrn")));
  806. break;
  807. case IOCTL_CAM_SETSLICELMT :
  808. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETSLICELMTrn")));
  809. break;
  810. case IOCTL_CAM_WBACCLMT :
  811. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_WBACCLMTrn")));
  812. break;
  813. case IOCTL_CAM_CSEDGELMT :
  814. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_CSEDGELMTrn")));
  815. break;
  816. case IOCTL_CAM_ENVINT :
  817. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_ENVINTrn")));
  818. break;
  819. case IOCTL_CAM_DISVINT :
  820. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_DISVINTrn")));
  821. break;
  822. case IOCTL_CAM_SETCAMERA :
  823. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:IOCTL_CAM_SETCAMERArn")));
  824. /*
  825. RETAILMSG(MSG_EN_1,(_T("---------------------------------------rn")));
  826. RETAILMSG(MSG_EN_1,(_T("ImgCptEn_PRSC = %drn"), (s2440CAM->rCIPRSTATUS>>21)&0x1));
  827. RETAILMSG(MSG_EN_1,(_T("PrScalerStart = %drn"), (s2440CAM->rCIPRSCCTRL>>15)&0x1));
  828. RETAILMSG(MSG_EN_1,(_T("---------------------------------------rn")));
  829. RETAILMSG(MSG_EN_1,(_T("CoScalerStart  = %drn"), (s2440CAM->rCICOSCCTRL>>15)&0x1));
  830. RETAILMSG(MSG_EN_1,(_T("VSYNC          = %drn"), (s2440CAM->rCICOSTATUS>>28)&0x1));
  831. RETAILMSG(MSG_EN_1,(_T("ImgCptEn_Camif = %drn"), (s2440CAM->rCICOSTATUS>>22)&0x1));
  832. RETAILMSG(MSG_EN_1,(_T("ImgCptEn_CoSC  = %drn"), (s2440CAM->rCICOSTATUS>>21)&0x1));
  833. RETAILMSG(MSG_EN_1,(_T("VSYNC_A  = %drn"), (s2440CAM->rCICOSTATUS>>20)&0x1));
  834. RETAILMSG(MSG_EN_1,(_T("VSYNC_B  = %drn"), (s2440CAM->rCICOSTATUS>>19)&0x1));
  835. RETAILMSG(MSG_EN_1,(_T("---------------------------------------rn")));
  836. RETAILMSG(MSG_EN_1,(_T("ImgCptEn      = %drn"), (s2440CAM->rCIIMGCPT>>31)&0x1));
  837. RETAILMSG(MSG_EN_1,(_T("ImgCptEn_CoSc = %drn"), (s2440CAM->rCIIMGCPT>>30)&0x1));
  838. RETAILMSG(MSG_EN_1,(_T("ImgCptEn_PrSc = %drn"), (s2440CAM->rCIIMGCPT>>29)&0x1));
  839. RETAILMSG(MSG_EN_1,(_T("----------------- %d ----------------------rn"), mInitialized));
  840. SetEvent(CameraEvent);
  841. */
  842. break;
  843. default : 
  844. RETAILMSG(MSG_EN_1,(TEXT("CAMERA:Ioctl code = 0x%xrn"), dwCode));
  845. return FALSE;
  846. }
  847. return TRUE;
  848. //-----------------------------------------------------------------------------
  849. //-----------------------------------------------------------------------------
  850. DWORD CIS_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
  851. {
  852. RETAILMSG(1,(TEXT("CAMERA: CIS_Openrn")));
  853. DRIVER_PREVIEW_ENABLE = 2;
  854. return TRUE;
  855. //-----------------------------------------------------------------------------
  856. //-----------------------------------------------------------------------------
  857. BOOL CIS_Close(DWORD hOpenContext)
  858. {
  859. RETAILMSG(1,(TEXT("CAMERA: CIS_Closern")));
  860. return TRUE;
  861. //-----------------------------------------------------------------------------
  862. //-----------------------------------------------------------------------------
  863. void CIS_PowerDown(DWORD hDeviceContext)
  864. {
  865. RETAILMSG(1,(TEXT("CAMERA: CIS_PowerDownrn")));
  866. m_Dx = (_CEDEVICE_POWER_STATE)D4;
  867. Camif_Capture(CAPTURE_OFF, CAPTURE_OFF);
  868. CamClockOn(FALSE);
  869. //RETAILMSG(1,(TEXT("CAMERA: CIS_PowerDown, m_Dx = D%u, init %d rn"), m_Dx, mInitialized));
  870. //-----------------------------------------------------------------------------
  871. //-----------------------------------------------------------------------------
  872. void CIS_PowerUp(DWORD hDeviceContext)
  873. {
  874. RETAILMSG(1,(TEXT("CAMERA: CIS_PowerUprn")));
  875. m_Dx = (_CEDEVICE_POWER_STATE)D0;
  876.     //
  877.     // 1. Camera IO setup
  878.     //
  879. CamGpioInit();
  880. //
  881.     // 2. Camera Clock setup
  882.     //
  883. CamClockOn(TRUE);
  884. // 3. Camera i/f reset
  885.     CAM_IF_Reset();
  886. if (image_size == 1)
  887. CamInit(QCIF_XSIZE, QCIF_YSIZE, QCIF_XSIZE, QCIF_YSIZE, 112, 20, COPIFRAMEBUFFER_B, COPIFRAMEBUFFER_A);
  888. else if (image_size == 2)
  889. CamInit(CIF_XSIZE, CIF_YSIZE, QCIF_XSIZE, QCIF_YSIZE, 112, 20, COPIFRAMEBUFFER_B, COPIFRAMEBUFFER_A);
  890. RETAILMSG(1,(TEXT("CAMERA: CIS_PowerUp, m_Dx = D%urn"), m_Dx));
  891. //-----------------------------------------------------------------------------
  892. //-----------------------------------------------------------------------------
  893. DWORD CIS_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
  894. {
  895. RETAILMSG(1,(TEXT("CAMERA: CIS_Readrn")));
  896. return TRUE;
  897. //-----------------------------------------------------------------------------
  898. //-----------------------------------------------------------------------------
  899. DWORD CIS_Seek(DWORD hOpenContext, long Amount, DWORD Type)
  900. {
  901. RETAILMSG(1,(TEXT("CAMERA: CIS_Seekrn")));
  902. return 0;
  903. //-----------------------------------------------------------------------------
  904. //-----------------------------------------------------------------------------
  905. DWORD CIS_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
  906. {
  907. RETAILMSG(1,(TEXT("CAMERA: CIS_Writern")));
  908. return 0;
  909. }
  910. // for MPEG4
  911. void Copy_Cam_Image(U8 *pBufOut, U32 size_x, U32 size_y, U8 port)
  912. {
  913. U8 *buffer_y, *buffer_cb, *buffer_cr;
  914. U32 Y_size, C_size, P_size;
  915. int temp;
  916. U8 *pImage;
  917. static unsigned int frame_timestamp_prev = GetTickCount();
  918. static int frame_bank_prev = -1;
  919. YUVINFO yuvinfo;
  920. RETAILMSG(MSG_EN_1,(_T("Copy_Cam_Imagern")));
  921. //s2440IOP->rGPFDAT &= ~(1<<6);
  922. pImage = pBufOut;
  923. if (image_size == 1) // QCIF
  924. Y_size = QCIF_XSIZE*QCIF_YSIZE;
  925. else if (image_size == 2) // CIF
  926. Y_size = CIF_XSIZE*CIF_YSIZE;
  927. C_size = Y_size/4;
  928. P_size = Y_size + C_size*2;
  929. {
  930. temp = (s2440CAM->rCICOSTATUS>>26)&3;
  931. temp = (temp + 2) % 4;
  932. RETAILMSG(MSG_EN_1,(_T("Copy_Cam_Image %drn"), temp));
  933. switch (temp)
  934. {
  935. case 0:
  936. buffer_y = (U8 *)s2440CAM->rCICOYSA1;
  937. buffer_cb = (U8 *)s2440CAM->rCICOCBSA1;
  938. buffer_cr = (U8 *)s2440CAM->rCICOCRSA1;
  939. break;
  940. case 1:
  941. buffer_y = (U8 *)s2440CAM->rCICOYSA2;
  942. buffer_cb = (U8 *)s2440CAM->rCICOCBSA2;
  943. buffer_cr = (U8 *)s2440CAM->rCICOCRSA2;
  944. break;
  945. case 2:
  946. buffer_y = (U8 *)s2440CAM->rCICOYSA3;
  947. buffer_cb = (U8 *)s2440CAM->rCICOCBSA3;
  948. buffer_cr = (U8 *)s2440CAM->rCICOCRSA3;
  949. break;
  950. case 3:
  951. buffer_y = (U8 *)s2440CAM->rCICOYSA4;
  952. buffer_cb = (U8 *)s2440CAM->rCICOCBSA4;
  953. buffer_cr = (U8 *)s2440CAM->rCICOCRSA4;
  954. break;
  955. default :
  956. buffer_y = (U8 *)s2440CAM->rCICOYSA1;
  957. buffer_cb = (U8 *)s2440CAM->rCICOCBSA1;
  958. buffer_cr = (U8 *)s2440CAM->rCICOCRSA1;
  959. break;
  960. }
  961. #if (DOTNET_DRIVER)
  962. SetKMode(TRUE);
  963. #endif
  964. buffer_y += VIRTUAL_ADDR_OFFSET;
  965. buffer_cb += VIRTUAL_ADDR_OFFSET;
  966. buffer_cr += VIRTUAL_ADDR_OFFSET;
  967. RETAILMSG(MSG_EN_1,(_T("buffer_y = 0x%xrn"), buffer_y));
  968. RETAILMSG(MSG_EN_1,(_T("buffer_cb = 0x%xrn"), buffer_cb));
  969. RETAILMSG(MSG_EN_1,(_T("buffer_cr = 0x%xrn"), buffer_cr));
  970. if (image_size == 1) // QCIF
  971. {
  972. yuvinfo.frame_width = QCIF_XSIZE;
  973. yuvinfo.frame_height = QCIF_YSIZE;
  974. }
  975. else if (image_size == 2) // CIF
  976. {
  977. yuvinfo.frame_width = CIF_XSIZE;
  978. yuvinfo.frame_height = CIF_YSIZE;
  979. }
  980. if (temp == frame_bank_prev) yuvinfo.frame_stamp = frame_timestamp_prev;
  981. else 
  982. {
  983. yuvinfo.frame_stamp = GetTickCount();
  984. frame_timestamp_prev = yuvinfo.frame_stamp;
  985. frame_bank_prev = temp;
  986. }
  987. RETAILMSG(MSG_EN_1,(_T("pBufOut 0 offset = 0x%xrn"), pImage));
  988. memcpy(pImage, &yuvinfo, sizeof(YUVINFO));
  989. pImage += sizeof(YUVINFO);
  990. RETAILMSG(MSG_EN_1,(_T("pBufOut Y = 0x%xrn"), pImage));
  991. memcpy(pImage, buffer_y, Y_size);
  992. pImage += (Y_size);
  993. RETAILMSG(MSG_EN_1,(_T("pBufOut cb = 0x%xrn"), pImage));
  994. memcpy(pImage, buffer_cb, C_size);
  995. pImage += C_size;
  996. RETAILMSG(MSG_EN_1,(_T("pBufOut cr = 0x%xrn"), pImage));
  997. memcpy(pImage, buffer_cr, C_size);
  998. // pBufOut += (size_x*size_y)/4;
  999. #if (DOTNET_DRIVER)
  1000. SetKMode(FALSE);
  1001. #endif
  1002. // RETAILMSG(MSG_EN_1,(TEXT("1:buffer_y:0x%x,buffer_cb:0x%x,buffer_cr:0x%xrn"),buffer_y,buffer_cb,buffer_cr));
  1003. //s2440IOP->rGPFDAT |= (1<<6);
  1004. Tick_COPY_FRAME = GetTickCount();
  1005. RETAILMSG(MSG_EN_1,(_T("COPY_Tick = %drn"), (Tick_COPY_FRAME-Tick_GET_FRAME_CUR)));
  1006. RETAILMSG(MSG_EN_1,(_T("Frame_Tick = %drn"), (Tick_GET_FRAME_CUR-Tick_GET_FRAME_PREV)));
  1007. // RETAILMSG(1,(_T("Tick_COPY_FRAME = %drn"), Tick_COPY_FRAME));
  1008. // RETAILMSG(1,(_T("Tick_GET_FRAME_PREV = %drn"), Tick_GET_FRAME_PREV));
  1009. // RETAILMSG(1,(_T("Tick_GET_FRAME_CUR = %drn"), Tick_GET_FRAME_CUR));
  1010. }
  1011. void Samsung_camcoder(U8 *pBufOut)
  1012. {
  1013. U8 *pImage;
  1014. PINGPONG caminfo;
  1015. pImage = pBufOut;
  1016. if( codec_flag )
  1017. {
  1018. caminfo.flag = 1;
  1019. caminfo.y_address = y_address;
  1020. caminfo.cb_address = cb_address;
  1021. caminfo.cr_address = cr_address;
  1022. memcpy(pImage, &caminfo, sizeof(PINGPONG));
  1023. codec_flag = 0;
  1024. }
  1025. }
  1026. void Samsung_camcoder_pr(U8 *pBufOut)
  1027. {
  1028. U8 *pImage;
  1029. PINGPONG_PR prinfo;
  1030. pImage = pBufOut;
  1031. if ( rgb_flag )
  1032. {
  1033. prinfo.flag = 1;
  1034. prinfo.rgb_address = rgb_address;
  1035. memcpy(pImage, &prinfo, sizeof(PINGPONG_PR));
  1036. rgb_flag = 0;
  1037. }
  1038. }
  1039. void Buffer_codec_info_update()
  1040. {
  1041. U32 Y_size, C_size, P_size;
  1042. int temp;
  1043. unsigned int buffer_y, buffer_cb, buffer_cr;
  1044. if ( image_size == 1 )
  1045. Y_size = QCIF_XSIZE*QCIF_YSIZE;
  1046. else if ( image_size == 2 )
  1047. Y_size = CIF_XSIZE*CIF_YSIZE;
  1048. C_size = Y_size/4;
  1049. P_size = Y_size + C_size*2;
  1050. temp = (s2440CAM->rCICOSTATUS>>26)&3;
  1051. temp = (temp + 2) % 4;
  1052. RETAILMSG(MSG_EN_2,(_T("codec index = %d, size %drn"), temp, image_size));
  1053. switch (temp)
  1054. {
  1055. case 0:
  1056. buffer_y = s2440CAM->rCICOYSA1;
  1057. buffer_cb = s2440CAM->rCICOCBSA1;
  1058. buffer_cr = s2440CAM->rCICOCRSA1;
  1059. break;
  1060. case 1:
  1061. buffer_y = s2440CAM->rCICOYSA2;
  1062. buffer_cb = s2440CAM->rCICOCBSA2;
  1063. buffer_cr = s2440CAM->rCICOCRSA2;
  1064. break;
  1065. case 2:
  1066. buffer_y = s2440CAM->rCICOYSA3;
  1067. buffer_cb = s2440CAM->rCICOCBSA3;
  1068. buffer_cr = s2440CAM->rCICOCRSA3;
  1069. break;
  1070. case 3:
  1071. buffer_y = s2440CAM->rCICOYSA4;
  1072. buffer_cb = s2440CAM->rCICOCBSA4;
  1073. buffer_cr = s2440CAM->rCICOCRSA4;
  1074. break;
  1075. default :
  1076. buffer_y = s2440CAM->rCICOYSA1;
  1077. buffer_cb = s2440CAM->rCICOCBSA1;
  1078. buffer_cr = s2440CAM->rCICOCRSA1;
  1079. break;
  1080. }
  1081. buffer_y += VIRTUAL_ADDR_OFFSET;
  1082. buffer_cb += VIRTUAL_ADDR_OFFSET;
  1083. buffer_cr += VIRTUAL_ADDR_OFFSET;
  1084. #if 0
  1085. RETAILMSG(MSG_EN_1,(_T("buffer_y = 0x%xrn"), buffer_y));
  1086. RETAILMSG(MSG_EN_1,(_T("buffer_cb = 0x%xrn"), buffer_cb));
  1087. RETAILMSG(MSG_EN_1,(_T("buffer_cr = 0x%xrn"), buffer_cr));
  1088. #endif
  1089. if( codec_flag ) RETAILMSG(MSG_EN_1,(_T("Buffer is not readrn")));
  1090. codec_flag = 1;
  1091. y_address = buffer_y;
  1092. cb_address = buffer_cb;
  1093. cr_address = buffer_cr;
  1094. }
  1095. void Buffer_preview_info_update()
  1096. {
  1097. // U32 Y_size;
  1098. int temp;
  1099. unsigned int buffer_rgb;
  1100. /*
  1101. if ( image_size == 1 )
  1102. Y_size = QCIF_XSIZE*QCIF_YSIZE;
  1103. else if ( image_size == 2 )
  1104. Y_size = CIF_XSIZE*CIF_YSIZE;
  1105. */
  1106. temp = (s2440CAM->rCIPRSTATUS>>26)&3;
  1107. temp = (temp + 2) % 4;
  1108. RETAILMSG(MSG_EN_2,(_T("preview index = %d, size %drn"), temp, image_size));
  1109. switch (temp)
  1110. {
  1111. case 0:
  1112. buffer_rgb = s2440CAM->rCIPRCLRSA1;
  1113. break;
  1114. case 1:
  1115. buffer_rgb = s2440CAM->rCIPRCLRSA2;
  1116. break;
  1117. case 2:
  1118. buffer_rgb = s2440CAM->rCIPRCLRSA3;
  1119. break;
  1120. case 3:
  1121. buffer_rgb = s2440CAM->rCIPRCLRSA4;
  1122. break;
  1123. default :
  1124. buffer_rgb = s2440CAM->rCIPRCLRSA1;
  1125. break;
  1126. }
  1127. buffer_rgb += VIRTUAL_ADDR_OFFSET;
  1128. #if 0
  1129. RETAILMSG(MSG_EN_1,(_T("buffer_rgb = 0x%xrn"), buffer_rgb));
  1130. #endif
  1131. if( rgb_flag ) RETAILMSG(MSG_EN_1,(_T("Buffer is not readrn")));
  1132. rgb_flag = 1;
  1133. rgb_address = buffer_rgb;
  1134. }
  1135. void CamInit(U32 CoDstWidth, U32 CoDstHeight, U32 PrDstWidth, U32 PrDstHeight, 
  1136. U32 WinHorOffset, U32 WinVerOffset,  U32 CoFrameBuffer, U32 PrFrameBuffer)
  1137. {
  1138. U32 WinOfsEn;
  1139. U32 divisor, multiplier;
  1140. U32 MainBurstSizeY, RemainedBurstSizeY, MainBurstSizeC, RemainedBurstSizeC, MainBurstSizeRGB, RemainedBurstSizeRGB;
  1141. U32 H_Shift, V_Shift, PreHorRatio, PreVerRatio, MainHorRatio, MainVerRatio;
  1142. U32 SrcWidth, SrcHeight;
  1143. U32 ScaleUp_H_Co, ScaleUp_V_Co, ScaleUp_H_Pr, ScaleUp_V_Pr;
  1144. //constant for calculating codec dma address
  1145. if(CAM_CODEC_OUTPUT)
  1146. divisor=2; //CCIR-422
  1147. else
  1148. divisor=4; //CCIR-420
  1149. //constant for calculating preview dma address
  1150. if(CAM_PVIEW_OUTPUT)
  1151. multiplier=4;
  1152. else
  1153. multiplier=2;
  1154. if(WinHorOffset==0 && WinVerOffset==0)
  1155. WinOfsEn=0;
  1156. else
  1157. WinOfsEn=1;
  1158. SrcWidth=CAM_SRC_HSIZE-WinHorOffset*2;
  1159. SrcHeight=CAM_SRC_VSIZE-WinVerOffset*2;
  1160. if(SrcWidth>=CoDstWidth) ScaleUp_H_Co=0; //down
  1161. else ScaleUp_H_Co=1; //up
  1162. if(SrcHeight>=CoDstHeight) ScaleUp_V_Co=0;
  1163. else ScaleUp_V_Co=1;
  1164. if(SrcWidth>=PrDstWidth) ScaleUp_H_Pr=0; //down
  1165. else ScaleUp_H_Pr=1; //up
  1166. if(SrcHeight>=PrDstHeight) ScaleUp_V_Pr=0;
  1167. else ScaleUp_V_Pr=1;
  1168. ////////////////// common control setting
  1169. s2440CAM->rCIGCTRL |= (1<<26)|(0<<27); // inverse PCLK, test pattern
  1170. s2440CAM->rCIWDOFST = (1<<30)|(0xf<<12); // clear overflow 
  1171. s2440CAM->rCIWDOFST = 0;
  1172. s2440CAM->rCIWDOFST=(WinOfsEn<<31)|(WinHorOffset<<16)|(WinVerOffset);
  1173. s2440CAM->rCISRCFMT=(CAM_ITU601<<31)|(0<<30)|(0<<29)|(CAM_SRC_HSIZE<<16)|(CAM_ORDER_YCBYCR<<14)|(CAM_SRC_VSIZE);
  1174. //s2440CAM->rCISRCFMT=(CAM_ITU656<<31)|(0<<30)|(0<<29)|(CAM_SRC_HSIZE<<16)|(CAM_ORDER_CBYCRY<<14)|(CAM_SRC_VSIZE);
  1175. ////////////////// codec port setting
  1176. s2440CAM->rCICOYSA1=CoFrameBuffer;
  1177. s2440CAM->rCICOYSA2=s2440CAM->rCICOYSA1+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
  1178. s2440CAM->rCICOYSA3=s2440CAM->rCICOYSA2+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
  1179. s2440CAM->rCICOYSA4=s2440CAM->rCICOYSA3+CoDstWidth*CoDstHeight+2*CoDstWidth*CoDstHeight/divisor;
  1180. s2440CAM->rCICOCBSA1=s2440CAM->rCICOYSA1+CoDstWidth*CoDstHeight;
  1181. s2440CAM->rCICOCBSA2=s2440CAM->rCICOYSA2+CoDstWidth*CoDstHeight;
  1182. s2440CAM->rCICOCBSA3=s2440CAM->rCICOYSA3+CoDstWidth*CoDstHeight;
  1183. s2440CAM->rCICOCBSA4=s2440CAM->rCICOYSA4+CoDstWidth*CoDstHeight;
  1184. s2440CAM->rCICOCRSA1=s2440CAM->rCICOCBSA1+CoDstWidth*CoDstHeight/divisor;
  1185. s2440CAM->rCICOCRSA2=s2440CAM->rCICOCBSA2+CoDstWidth*CoDstHeight/divisor;
  1186. s2440CAM->rCICOCRSA3=s2440CAM->rCICOCBSA3+CoDstWidth*CoDstHeight/divisor;
  1187. s2440CAM->rCICOCRSA4=s2440CAM->rCICOCBSA4+CoDstWidth*CoDstHeight/divisor;
  1188.     
  1189. s2440CAM->rCICOTRGFMT=(CAM_CODEC_IN_422<<31)|(CAM_CODEC_OUTPUT<<30)|(CoDstWidth<<16)|(CAM_FLIP_NORMAL<<14)|(CoDstHeight);
  1190. CalculateBurstSize(CoDstWidth, &MainBurstSizeY, &RemainedBurstSizeY);
  1191. CalculateBurstSize(CoDstWidth/2, &MainBurstSizeC, &RemainedBurstSizeC);
  1192. s2440CAM->rCICOCTRL=(MainBurstSizeY<<19)|(RemainedBurstSizeY<<14)|(MainBurstSizeC<<9)|(RemainedBurstSizeC<<4);
  1193. CalculatePrescalerRatioShift(SrcWidth, CoDstWidth, &PreHorRatio, &H_Shift);
  1194. CalculatePrescalerRatioShift(SrcHeight, CoDstHeight, &PreVerRatio, &V_Shift);
  1195. MainHorRatio=(SrcWidth<<8)/(CoDstWidth<<H_Shift);
  1196. MainVerRatio=(SrcHeight<<8)/(CoDstHeight<<V_Shift);
  1197.     
  1198. s2440CAM->rCICOSCPRERATIO=((10-H_Shift-V_Shift)<<28)|(PreHorRatio<<16)|(PreVerRatio);
  1199. s2440CAM->rCICOSCPREDST=((SrcWidth/PreHorRatio)<<16)|(SrcHeight/PreVerRatio); 
  1200. s2440CAM->rCICOSCCTRL=(CAM_SCALER_BYPASS_OFF<<31)|(ScaleUp_H_Co<<30)|(ScaleUp_V_Co<<29)|(MainHorRatio<<16)|(MainVerRatio);
  1201. s2440CAM->rCICOTAREA=CoDstWidth*CoDstHeight;
  1202. ///////////////// preview port setting
  1203. s2440CAM->rCIPRCLRSA1=PrFrameBuffer;
  1204. s2440CAM->rCIPRCLRSA2=s2440CAM->rCIPRCLRSA1+PrDstWidth*PrDstHeight*multiplier;
  1205. s2440CAM->rCIPRCLRSA3=s2440CAM->rCIPRCLRSA2+PrDstWidth*PrDstHeight*multiplier;
  1206. s2440CAM->rCIPRCLRSA4=s2440CAM->rCIPRCLRSA3+PrDstWidth*PrDstHeight*multiplier;
  1207. s2440CAM->rCIPRTRGFMT=(PrDstWidth<<16)|(CAM_FLIP_NORMAL<<14)|(PrDstHeight);
  1208. if (CAM_PVIEW_OUTPUT==CAM_RGB24B)
  1209. CalculateBurstSize(PrDstWidth*4, &MainBurstSizeRGB, &RemainedBurstSizeRGB);
  1210. else // RGB16B
  1211. CalculateBurstSize(PrDstWidth*2, &MainBurstSizeRGB, &RemainedBurstSizeRGB);
  1212.     s2440CAM->rCIPRCTRL=(MainBurstSizeRGB<<19)|(RemainedBurstSizeRGB<<14);
  1213. CalculatePrescalerRatioShift(SrcWidth, PrDstWidth, &PreHorRatio, &H_Shift);
  1214. CalculatePrescalerRatioShift(SrcHeight, PrDstHeight, &PreVerRatio, &V_Shift);
  1215. MainHorRatio=(SrcWidth<<8)/(PrDstWidth<<H_Shift);
  1216. MainVerRatio=(SrcHeight<<8)/(PrDstHeight<<V_Shift);
  1217. s2440CAM->rCIPRSCPRERATIO=((10-H_Shift-V_Shift)<<28)|(PreHorRatio<<16)|(PreVerRatio);  
  1218. s2440CAM->rCIPRSCPREDST=((SrcWidth/PreHorRatio)<<16)|(SrcHeight/PreVerRatio);
  1219. s2440CAM->rCIPRSCCTRL=(1<<31)|(CAM_RGB16B<<30)|(ScaleUp_H_Pr<<29)|(ScaleUp_V_Pr<<28)|(MainHorRatio<<16)|(MainVerRatio);
  1220.     
  1221. s2440CAM->rCIPRTAREA= PrDstWidth*PrDstHeight;
  1222. }