source: svn/trunk/newcon3bcm2_21bu/dta/src/settop_api/bsettop_rfm.c

Last change on this file was 2, checked in by phkim, 11 years ago

1.phkim

  1. revision copy newcon3sk r27
  • Property svn:executable set to *
File size: 10.2 KB
Line 
1/***************************************************************************
2 *     Copyright (c) 2003-2008, Broadcom Corporation
3 *     All Rights Reserved
4 *     Confidential Property of Broadcom Corporation
5 *
6 *  THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE
7 *  AGREEMENT  BETWEEN THE USER AND BROADCOM.  YOU HAVE NO RIGHT TO USE OR
8 *  EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT.
9 *
10 * $brcm_Workfile: $
11 * $brcm_Revision: $
12 * $brcm_Date: $
13 *
14 * Module Description:
15 *
16 * Revision History:
17 *
18 * $brcm_Log: $
19 *
20 *
21 ***************************************************************************/
22#include "bsettop_rfm.h"
23#include "gist.h"
24#include "bi2c.h"
25#include "breg_i2c.h"
26#include "bstd.h"
27#include "bos_task_priorities.h"
28#if (BCHP_CHIP==7552)
29#include "brfm.h"
30#endif
31//#define RFM_USE_INT 1
32
33#ifdef RFM_USE_INT
34#include "bsettop_gpio.h"
35#endif
36
37BDBG_MODULE(brfm);              /* Register software module with debug interface */
38
39#define BRFM_I2C_BUS 2
40#define BRFM_I2C_ADDR 0x65
41#define BRFM_TASK_STACK_WORDS 256
42
43#if (BCHP_CHIP==7552)
44#if 1/*janzy@20120731*/
45#define RFM_GPIO_NUM    BGIO_PinId_eAgpio09
46#else
47#define RFM_GPIO_NUM    BGIO_PinId_eAgpio20
48#endif
49#else
50#define RFM_GPIO_NUM    BGIO_PinId_eGpio100
51#endif
52
53struct brfm
54{
55        bool inUSE;
56#if (BCHP_CHIP==7552)
57        BRFM_Handle rfmHandle;
58#else
59        BI2C_ChannelHandle i2cChannelHandle;
60        BREG_I2C_Handle i2cRegHandle;
61        uint16_t chipAddr;
62#endif
63        BGIO_Pin_Handle pinHandle;
64        BGIO_PinValue lastValue;
65        b_task_t task_h;
66#ifdef RFM_USE_INT
67        bgpio_t gpioHandle;
68        b_queue_t queue;
69        b_event_t events[4];
70#endif
71
72#ifdef ACB612
73        bool rfm;               /* rfm CH3/4 for ACB612 */
74#endif
75        unsigned int task_stack[BRFM_TASK_STACK_WORDS];
76};
77
78#if (BCHP_CHIP==7552)
79struct brfm s_rfm = { false, NULL, NULL,BGIO_PinValue_eInvalid, 0};
80#else
81struct brfm s_rfm = { false, NULL, NULL,BRFM_I2C_ADDR,NULL,BGIO_PinValue_eInvalid, 0};
82#endif
83
84#ifdef RFM_USE_INT
85static void switch_callback(void *context)
86{
87        brfm_t p_rfm = (brfm_t)context;
88        b_event_t event;
89        bos_post_event(p_rfm->queue, &event);
90}
91#endif
92
93#ifndef ACB612
94/***************************************************************************
95Summary:
96        Thread to monitor RFM switch.  Due to how the RF4CE handles GPIO interrupts
97        and to possibly implement debounce logic it is simpler to poll for RFM switch
98        status than make it interrupt driven.
99 ***************************************************************************/
100static void rfm_task(void *arg)
101{
102        brfm_t p_rfm = (brfm_t)arg;
103#if (BCHP_CHIP==7552)
104        BRFM_OutputChannel chNbr;
105#else
106        uint8_t data_wr[4];
107#endif
108        BERR_Code err;
109        p_rfm->lastValue = 0xFF; /* forces an update */
110        bos_sleep(1999); /* sleep for 2sec */
111        while(1)
112        {
113                BGIO_PinValue pinValue;
114                BDBG_MSG(("%s:%d",__FUNCTION__,__LINE__));
115#ifdef RFM_USE_INT
116                bos_pend_event(p_rfm->queue, -1);
117#endif
118
119#ifndef ACB612
120                if (BGIO_Pin_GetValue(p_rfm->pinHandle,&pinValue) == BERR_SUCCESS)
121#else
122                pinValue = p_rfm->rfm ? BGIO_PinValue_e1 : BGIO_PinValue_e0;
123#endif
124                {
125                        if (pinValue != p_rfm->lastValue)
126                        {
127                                p_rfm->lastValue = pinValue;
128                                BDBG_ERR(("%s:%d, %d BGIO_Pin_GetValue = %d",__FUNCTION__,__LINE__,100,p_rfm->lastValue));
129                                if (p_rfm->lastValue == BGIO_PinValue_e1)
130                                {
131#if (BCHP_CHIP==7552)
132                                        chNbr = BRFM_OutputChannel_eCh4;       
133#else
134                                        data_wr[2] = 0x21; /* CH 4 */
135#endif
136                                }
137                                else
138                                {
139#if (BCHP_CHIP==7552)
140                                        chNbr = BRFM_OutputChannel_eCh3;
141#else
142                                        data_wr[2] = 0x1E; /* CH 3 */
143#endif
144                                }
145#if (BCHP_CHIP==7552)
146                                err = BRFM_SetModulationType(p_rfm->rfmHandle, BRFM_ModulationType_eNtscOpenCable, chNbr);
147                                if (err != BERR_SUCCESS) {
148                                        BDBG_ERR(("%s:%d, BRFM_SetModulationType failed", __FUNCTION__,__LINE__));
149                                }
150#else
151                                data_wr[3] = 0xA0;
152                                err = BREG_I2C_WriteNoAddr(p_rfm->i2cRegHandle,p_rfm->chipAddr,&data_wr[2],2);
153                                if (err != BERR_SUCCESS)
154                                {
155                                        BDBG_ERR(("%s:%d, BREG_I2C_WriteNoAddr failed = %d(0x%08x)",__FUNCTION__,__LINE__,err,err));
156                                }
157                //data_wr[0] = 0x80;
158                //data_wr[1] = 0x00;
159                                bos_sleep(2000);
160                                data_wr[3] = 0xA3;
161                                err = BREG_I2C_WriteNoAddr(p_rfm->i2cRegHandle,p_rfm->chipAddr,&data_wr[2],2);
162                                //err = BREG_I2C_WriteNoAddr(p_rfm->i2cRegHandle,p_rfm->chipAddr,&data_wr[0],4);
163                                if (err != BERR_SUCCESS)
164                {
165                    BDBG_ERR(("%s:%d, BREG_I2C_WriteNoAddr failed = %d(0x%08x)",__FUNCTION__,__LINE__,err,err));
166                }
167#endif
168                        }
169                }
170#ifndef RFM_USE_INT
171                BKNI_Sleep(500);
172#endif
173        }
174}
175#endif
176
177/*
178Summary:
179Open and allocate RFM resources.
180
181Description:
182On most platforms, the rfm_id is simply an index of the rf modulator.
183
184 */
185brfm_t brfm_open(
186                bobject_t rfm_id        /* - index used to identify a particular rfm */
187                )
188
189{
190        BERR_Code err;
191#ifndef ACB612
192        b_task_params task_params;
193#endif
194        BGIO_PinValue pinValue;
195#if (BCHP_CHIP==7552)
196        BRFM_Settings rfmSettings;
197        BRFM_OutputChannel chNbr;
198#else
199        BI2C_ChannelSettings defChnSettings;
200        uint8_t data_wr[4];
201#endif
202
203        brfm_t p_rfm;
204#ifdef RFM_USE_INT
205        b_event_t event;
206        bgpio_settings gpio_settings;
207#endif
208
209        if (s_rfm.inUSE)
210        {
211                return NULL;
212        }
213        p_rfm = &s_rfm;
214
215#if (BCHP_CHIP==7552)   
216        BRFM_GetDefaultSettings(&rfmSettings, GetCHP());
217        err = BRFM_Open(&p_rfm->rfmHandle, GetCHP(), GetREG(), GetINT(), &rfmSettings);
218        if (err != BERR_SUCCESS) {
219                BDBG_WRN(("Fail to open RFM module"));
220                return NULL;
221        }               
222#else
223        BI2C_GetChannelDefaultSettings( GetI2C(), BRFM_I2C_BUS, &defChnSettings );
224        defChnSettings.intMode = true; 
225        defChnSettings.clkRate = BI2C_Clk_eClk400Khz;
226        BI2C_OpenChannel( GetI2C(), &(p_rfm->i2cChannelHandle), BRFM_I2C_BUS, &defChnSettings );
227        BDBG_ASSERT(p_rfm->i2cChannelHandle);
228        BI2C_CreateI2cRegHandle (p_rfm->i2cChannelHandle, &(p_rfm->i2cRegHandle));
229        BDBG_ASSERT(p_rfm->i2cRegHandle);
230#endif
231
232        err = BGIO_Pin_Create(GetGIO(),RFM_GPIO_NUM,&p_rfm->pinHandle);
233        if (err != BERR_SUCCESS)
234        {
235                brfm_close(p_rfm);
236                return NULL;
237        }
238
239#if (BCHP_CHIP==7552)
240        chNbr = BRFM_OutputChannel_eCh3;
241#else
242        /* Configure modulator */
243        data_wr[0] = 0x90;
244        data_wr[1] = 0x00;
245        data_wr[2] = 0x1E;
246#endif
247#ifndef ACB612
248        if (BGIO_Pin_GetValue(p_rfm->pinHandle,&pinValue) == BERR_SUCCESS)
249#else
250        pinValue = p_rfm->rfm ? BGIO_PinValue_e1 : BGIO_PinValue_e0;
251#endif
252        {
253                if (pinValue == BGIO_PinValue_e1)
254                {
255#if (BCHP_CHIP==7552)
256                        chNbr = BRFM_OutputChannel_eCh4;       
257#else
258                        data_wr[2] = 0x21; /* CH 4 */
259#endif
260                }
261        }
262#if (BCHP_CHIP==7552)
263        err = BRFM_SetModulationType(p_rfm->rfmHandle, BRFM_ModulationType_eNtscOpenCable, chNbr);
264        if (err != BERR_SUCCESS)
265        {
266                BDBG_WRN(("%s:%d BRFM_SetModulationType failed", __FUNCTION__, __LINE__));
267        }
268#else
269        data_wr[3] = 0xA0;
270        err = BREG_I2C_WriteNoAddr(p_rfm->i2cRegHandle,p_rfm->chipAddr,data_wr,4);
271        if (err != BERR_SUCCESS)
272        {
273                brfm_close(p_rfm);
274                return NULL;
275        }
276#endif
277
278#ifdef RFM_USE_INT
279        bos_create_queue(&p_rfm->queue, p_rfm->events, 4);
280#if (BCHP_CHIP==7552)
281        gpio_settings.type = eGPIO_AStandard;
282#else
283        gpio_settings.type = eGPIO_Standard;
284#endif
285        gpio_settings.mode = eGPIO_Input;
286        gpio_settings.intMode = eGPIO_Edge;
287        gpio_settings.user_callback = switch_callback;
288        gpio_settings.user_callback_context =p_rfm;
289        p_rfm->gpioHandle = bgpio_open(RFM_GPIO_NUM, &gpio_settings);
290#endif
291
292        p_rfm->inUSE = true;
293
294/* ACB612 doesn't have CH3/4 button, but software control, so don't need a task to monitor the GPIO */
295#ifndef ACB612
296        task_params.name = "RFM_TASK";
297        task_params.priority = RFM_PRIORITY;
298        task_params.stack = p_rfm->task_stack;
299        task_params.stack_size = BRFM_TASK_STACK_WORDS;
300        bos_start_task(&(p_rfm->task_h),&task_params,rfm_task,p_rfm);
301#endif
302
303#ifdef RFM_USE_INT
304        /* set initial RF channel */
305        bos_post_event(p_rfm->queue, &event);
306#endif
307        return p_rfm;
308}
309
310/*
311Summary:
312Close the rfm.
313 */
314void brfm_close(
315                brfm_t rfm      /* - handle returned by brfm_open */
316                )
317{
318        BDBG_ASSERT(rfm);
319        if (rfm->pinHandle)
320                BGIO_Pin_Destroy(rfm->pinHandle);
321#if (BCHP_CHIP==7552)
322        if (rfm->rfmHandle)
323                BRFM_Close(rfm->rfmHandle);
324#else
325        if (rfm->i2cRegHandle)
326                BI2C_CloseI2cRegHandle(rfm->i2cRegHandle);
327        if (rfm->i2cChannelHandle)
328                BI2C_CloseChannel(rfm->i2cChannelHandle);
329#endif
330#ifdef RFM_USE_INT
331        if (rfm->gpioHandle)
332                bgpio_close(rfm->gpioHandle);
333#endif
334        if (rfm->task_h)
335                bos_stop_task(rfm->task_h);
336        rfm->pinHandle = NULL;
337#if (BCHP_CHIP==7552)
338        rfm->rfmHandle = NULL;
339#else
340        rfm->i2cRegHandle = NULL;
341        rfm->i2cChannelHandle = NULL;
342#endif
343        rfm->task_h = 0;
344#ifdef RFM_USE_INT
345        rfm->gpioHandle = NULL;
346        bos_delete_queue(&rfm->queue);
347#endif
348        rfm->inUSE = false;
349}
350
351/*
352 * Summary:
353 * power management for RFM block
354 */
355void brfm_standby(brfm_t rfm, bool standby)
356{
357        if (standby) {
358                BRFM_Standby(rfm->rfmHandle);
359        }
360        else {
361                BRFM_Resume(rfm->rfmHandle);
362        }       
363}
364
365void buser_AOV_input_LED(brfm_t rfm, ledStatus_t eLED)
366{       
367        BERR_Code err;
368        static BGIO_Pin_Handle pinLED_R = NULL;
369        static BGIO_Pin_Handle pinLED_G = NULL;
370        pinLED_G = rfm->pinHandle;
371        if(pinLED_R == NULL)
372        {
373                err = BGIO_Pin_Create(GetGIO(),BGIO_PinId_eAgpio04,&pinLED_R);
374                if (err != BERR_SUCCESS)
375                {
376                        BDBG_ERR(("BGIO_PinId_eAgpio04 Fail"));
377                        return;
378                }
379                BGIO_Pin_SetType(pinLED_R,BGIO_PinType_ePushPull);
380                BGIO_Pin_SetType(pinLED_G,BGIO_PinType_ePushPull);
381        }
382
383
384        switch(eLED)
385        {
386                case eLED_Status_R:
387                        BGIO_Pin_PushPull_SetValue(pinLED_G,BGIO_PinValue_e0);
388                        BGIO_Pin_PushPull_SetValue(pinLED_R,BGIO_PinValue_e1);
389                        break;
390
391                case eLED_Status_G:
392                BGIO_Pin_PushPull_SetValue(pinLED_G,BGIO_PinValue_e1);
393                BGIO_Pin_PushPull_SetValue(pinLED_R,BGIO_PinValue_e0);
394                        break;
395                       
396                case eLED_Status_OFF:
397                BGIO_Pin_PushPull_SetValue(pinLED_G,BGIO_PinValue_e0);
398                        BGIO_Pin_PushPull_SetValue(pinLED_R,BGIO_PinValue_e0);
399                        break;
400                case eLED_Status_RG:
401                        BGIO_Pin_PushPull_SetValue(pinLED_G,BGIO_PinValue_e1);
402                BGIO_Pin_PushPull_SetValue(pinLED_R,BGIO_PinValue_e1);
403                        break;
404
405        }
406
407        return;
408}
409
410/*
411 * Summary:
412 *      change CH3 or CH4
413 */
414void brfm_set_ch3(brfm_t p_rfm, bool ch)
415{
416#ifdef ACB612
417        BRFM_OutputChannel chNbr;
418
419        if (!p_rfm) {
420                BDBG_ERR(("%s: Not initialized",__func__));
421                return;
422        }       
423        s_rfm.rfm = ch ? BGIO_PinValue_e1 : BGIO_PinValue_e0;
424
425        if (p_rfm->rfm != p_rfm->lastValue)
426        {
427                p_rfm->lastValue = p_rfm->rfm;
428                BDBG_ERR(("%s: previous %s",__func__,p_rfm->lastValue ? "CH4" : "CH3"));
429                if (BGIO_PinValue_e1 == p_rfm->rfm)
430                {
431                        chNbr = BRFM_OutputChannel_eCh4;       
432                }
433                else
434                {
435                        chNbr = BRFM_OutputChannel_eCh3;
436                }
437                if (BERR_SUCCESS != BRFM_SetModulationType(p_rfm->rfmHandle, BRFM_ModulationType_eNtscOpenCable, chNbr)) {
438                        BDBG_ERR(("%s BRFM_SetModulationType failed", __func__));
439                }
440        }
441#endif
442}
Note: See TracBrowser for help on using the repository browser.