SAMA5D4 Xplained Ultra Board BSP
guowenxue
2019-08-19 2e7235d10c6dbff81960282e1a1e2e798f9b8db8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/******************************************************************************
 *
 * Copyright(c) 2007 - 2011 Realtek Corporation. All rights reserved.
 *                                        
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
 *
 *
 ******************************************************************************/
 
//============================================================
// include files
//============================================================
 
#include "../odm_precomp.h"
 
#if (RTL8188E_SUPPORT == 1)
 
VOID
ODM_DIG_LowerBound_88E(
    IN        PDM_ODM_T        pDM_Odm
)
{
    pDIG_T        pDM_DigTable = &pDM_Odm->DM_DigTable;
 
    if(pDM_Odm->AntDivType == CG_TRX_HW_ANTDIV)
    {
        pDM_DigTable->rx_gain_range_min = (u1Byte) pDM_DigTable->AntDiv_RSSI_max;
        ODM_RT_TRACE(pDM_Odm,ODM_COMP_ANT_DIV, ODM_DBG_LOUD, ("ODM_DIG_LowerBound_88E(): pDM_DigTable->AntDiv_RSSI_max=%d \n",pDM_DigTable->AntDiv_RSSI_max));
    }
    //If only one Entry connected
}
 
 
//3============================================================
//3 Dynamic Primary CCA
//3============================================================
 
VOID
odm_PrimaryCCA_Init(
    IN        PDM_ODM_T        pDM_Odm)
{
    pPri_CCA_T        PrimaryCCA = &(pDM_Odm->DM_PriCCA);  
    PrimaryCCA->DupRTS_flag = 0;
    PrimaryCCA->intf_flag = 0;
    PrimaryCCA->intf_type = 0;
    PrimaryCCA->Monitor_flag = 0;
    PrimaryCCA->PriCCA_flag = 0;
}
 
BOOLEAN
ODM_DynamicPrimaryCCA_DupRTS(
    IN        PDM_ODM_T        pDM_Odm
    )
{
    pPri_CCA_T        PrimaryCCA = &(pDM_Odm->DM_PriCCA);  
    
    return    PrimaryCCA->DupRTS_flag;
}
 
VOID
odm_DynamicPrimaryCCA(
    IN        PDM_ODM_T        pDM_Odm
    )
{
    PADAPTER    Adapter =  pDM_Odm->Adapter;    // for NIC
 
#if (DM_ODM_SUPPORT_TYPE & (ODM_WIN))    
    PRT_WLAN_STA    pEntry;
#endif    
 
    PFALSE_ALARM_STATISTICS        FalseAlmCnt = &(pDM_Odm->FalseAlmCnt);
    pPri_CCA_T        PrimaryCCA = &(pDM_Odm->DM_PriCCA);  
    
    BOOLEAN        Is40MHz;
    BOOLEAN        Client_40MHz = FALSE, Client_tmp = FALSE;      // connected client BW  
    BOOLEAN        bConnected = FALSE;        // connected or not
    static u1Byte    Client_40MHz_pre = 0;
    static u8Byte    lastTxOkCnt = 0;
    static u8Byte    lastRxOkCnt = 0;
    static u4Byte    Counter = 0;
    static u1Byte    Delay = 1;
    u8Byte        curTxOkCnt;
    u8Byte        curRxOkCnt;
    u1Byte        SecCHOffset;
    u1Byte        i;
    
#if((DM_ODM_SUPPORT_TYPE==ODM_ADSL) ||( DM_ODM_SUPPORT_TYPE==ODM_CE))
    return;
#endif
 
    if(pDM_Odm->SupportICType != ODM_RTL8188E) 
        return;
 
    Is40MHz = *(pDM_Odm->pBandWidth);
    SecCHOffset = *(pDM_Odm->pSecChOffset);
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Second CH Offset = %d\n", SecCHOffset));
    
#if (DM_ODM_SUPPORT_TYPE == ODM_WIN)
    if(Is40MHz==1)
        SecCHOffset = SecCHOffset%2+1;     // NIC's definition is reverse to AP   1:secondary below,  2: secondary above
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Second CH Offset = %d\n", SecCHOffset));
    //3 Check Current WLAN Traffic
    curTxOkCnt = Adapter->TxStats.NumTxBytesUnicast - lastTxOkCnt;
    curRxOkCnt = Adapter->RxStats.NumRxBytesUnicast - lastRxOkCnt;
    lastTxOkCnt = Adapter->TxStats.NumTxBytesUnicast;
    lastRxOkCnt = Adapter->RxStats.NumRxBytesUnicast;    
#elif (DM_ODM_SUPPORT_TYPE == ODM_AP)
    //3 Check Current WLAN Traffic
    curTxOkCnt = *(pDM_Odm->pNumTxBytesUnicast)-lastTxOkCnt;
    curRxOkCnt = *(pDM_Odm->pNumRxBytesUnicast)-lastRxOkCnt;
    lastTxOkCnt = *(pDM_Odm->pNumTxBytesUnicast);
    lastRxOkCnt = *(pDM_Odm->pNumRxBytesUnicast);
#endif
 
    //==================Debug Message====================
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("TP = %llu\n", curTxOkCnt+curRxOkCnt));
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Is40MHz = %d\n", Is40MHz));
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("BW_LSC = %d\n", FalseAlmCnt->Cnt_BW_LSC));
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("BW_USC = %d\n", FalseAlmCnt->Cnt_BW_USC));
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("CCA OFDM = %d\n", FalseAlmCnt->Cnt_OFDM_CCA));
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("CCA CCK = %d\n", FalseAlmCnt->Cnt_CCK_CCA));
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("OFDM FA = %d\n", FalseAlmCnt->Cnt_Ofdm_fail));
    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("CCK FA = %d\n", FalseAlmCnt->Cnt_Cck_fail));
    //================================================
    
#if (DM_ODM_SUPPORT_TYPE == ODM_WIN)
    if (ACTING_AS_AP(Adapter))   // primary cca process only do at AP mode
#endif
    {
        
    #if (DM_ODM_SUPPORT_TYPE == ODM_WIN)
        ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("ACTING as AP mode=%d\n", ACTING_AS_AP(Adapter)));
        //3 To get entry's connection and BW infomation status. 
        for(i=0;i<ASSOCIATE_ENTRY_NUM;i++)
        {
            if(IsAPModeExist(Adapter)&&GetFirstExtAdapter(Adapter)!=NULL)
                pEntry=AsocEntry_EnumStation(GetFirstExtAdapter(Adapter), i);
            else
                pEntry=AsocEntry_EnumStation(GetDefaultAdapter(Adapter), i);
            if(pEntry!=NULL)
            {
                Client_tmp = pEntry->BandWidth;   // client BW
                ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Client_BW=%d\n", Client_tmp));
                if(Client_tmp>Client_40MHz)
                    Client_40MHz = Client_tmp;     // 40M/20M coexist => 40M priority is High
                
                if(pEntry->bAssociated)
                {
                    bConnected=TRUE;    // client is connected or not
                    break;
                }
            }
            else
            {
                break;
            }
        }
#elif (DM_ODM_SUPPORT_TYPE == ODM_AP)
        //3 To get entry's connection and BW infomation status. 
 
        PSTA_INFO_T pstat;
 
        for(i=0; i<ODM_ASSOCIATE_ENTRY_NUM; i++)
        {
            pstat = pDM_Odm->pODM_StaInfo[i];
            if(IS_STA_VALID(pstat) )
            {            
                Client_tmp = pstat->tx_bw;  
                if(Client_tmp>Client_40MHz)
                    Client_40MHz = Client_tmp;     // 40M/20M coexist => 40M priority is High
                
                bConnected = TRUE;
            }
        }
#endif
        ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("bConnected=%d\n", bConnected));
        ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Is Client 40MHz=%d\n", Client_40MHz));
        //1 Monitor whether the interference exists or not 
        if(PrimaryCCA->Monitor_flag == 1)
        {
            if(SecCHOffset == 1)       // secondary channel is below the primary channel
            {
                if((FalseAlmCnt->Cnt_OFDM_CCA > 500)&&(FalseAlmCnt->Cnt_BW_LSC > FalseAlmCnt->Cnt_BW_USC+500))
                {
                    if(FalseAlmCnt->Cnt_Ofdm_fail > FalseAlmCnt->Cnt_OFDM_CCA>>1)
                    {
                        PrimaryCCA->intf_type = 1;
                        PrimaryCCA->PriCCA_flag = 1;
                        ODM_SetBBReg(pDM_Odm, 0xc6c, BIT8|BIT7, 2);   // USC MF
                        if(PrimaryCCA->DupRTS_flag == 1)
                            PrimaryCCA->DupRTS_flag = 0;
                    }
                    else
                    {
                        PrimaryCCA->intf_type = 2;
                        if(PrimaryCCA->DupRTS_flag == 0)
                            PrimaryCCA->DupRTS_flag = 1;
                    }
                
                }
                else   // interferecne disappear
                {
                    PrimaryCCA->DupRTS_flag = 0;
                    PrimaryCCA->intf_flag = 0;
                    PrimaryCCA->intf_type = 0;
                }
            }
            else if(SecCHOffset == 2)  // secondary channel is above the primary channel
            {
                if((FalseAlmCnt->Cnt_OFDM_CCA > 500)&&(FalseAlmCnt->Cnt_BW_USC > FalseAlmCnt->Cnt_BW_LSC+500))
                {
                    if(FalseAlmCnt->Cnt_Ofdm_fail > FalseAlmCnt->Cnt_OFDM_CCA>>1)
                    {
                        PrimaryCCA->intf_type = 1;
                        PrimaryCCA->PriCCA_flag = 1;
                        ODM_SetBBReg(pDM_Odm, 0xc6c, BIT8|BIT7, 1);  // LSC MF
                        if(PrimaryCCA->DupRTS_flag == 1)
                            PrimaryCCA->DupRTS_flag = 0;
                    }
                    else
                    {
                        PrimaryCCA->intf_type = 2;
                        if(PrimaryCCA->DupRTS_flag == 0)
                            PrimaryCCA->DupRTS_flag = 1;
                    }
                
                }
                else   // interferecne disappear
                {
                    PrimaryCCA->DupRTS_flag = 0;
                    PrimaryCCA->intf_flag = 0;
                    PrimaryCCA->intf_type = 0;
                }
 
 
            }
            PrimaryCCA->Monitor_flag = 0;
        }
 
        //1 Dynamic Primary CCA Main Function
        if(PrimaryCCA->Monitor_flag == 0)
        {
            if(Is40MHz)            // if RFBW==40M mode which require to process primary cca
            {
                //2 STA is NOT Connected
                if(!bConnected)
                {
                    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("STA NOT Connected!!!!\n"));
            
                    if(PrimaryCCA->PriCCA_flag == 1)        // reset primary cca when STA is disconnected
                    {
                        PrimaryCCA->PriCCA_flag = 0;
                        ODM_SetBBReg(pDM_Odm, 0xc6c, BIT8|BIT7, 0);
                    }
                    if(PrimaryCCA->DupRTS_flag == 1)        // reset Duplicate RTS when STA is disconnected
                        PrimaryCCA->DupRTS_flag = 0;
 
                    if(SecCHOffset == 1)   // secondary channel is below the primary channel
                    {
                        if((FalseAlmCnt->Cnt_OFDM_CCA > 800)&&(FalseAlmCnt->Cnt_BW_LSC*5 > FalseAlmCnt->Cnt_BW_USC*9))
                        {
                            PrimaryCCA->intf_flag = 1;    // secondary channel interference is detected!!!
                            if(FalseAlmCnt->Cnt_Ofdm_fail > FalseAlmCnt->Cnt_OFDM_CCA>>1)
                                PrimaryCCA->intf_type = 1;        // interference is shift
                            else
                                PrimaryCCA->intf_type = 2;        // interference is in-band
                        }
                        else    
                        {
                            PrimaryCCA->intf_flag = 0;
                            PrimaryCCA->intf_type = 0;  
                        }
                    }
                    else if(SecCHOffset == 2)    // secondary channel is above the primary channel
                    {
                        if((FalseAlmCnt->Cnt_OFDM_CCA > 800)&&(FalseAlmCnt->Cnt_BW_USC*5 > FalseAlmCnt->Cnt_BW_LSC*9))
                        {
                            PrimaryCCA->intf_flag = 1;    // secondary channel interference is detected!!!
                            if(FalseAlmCnt->Cnt_Ofdm_fail > FalseAlmCnt->Cnt_OFDM_CCA>>1)
                                PrimaryCCA->intf_type = 1;        // interference is shift
                            else
                                PrimaryCCA->intf_type = 2;        // interference is in-band
                        }
                        else    
                        {
                            PrimaryCCA->intf_flag = 0;
                            PrimaryCCA->intf_type = 0;  
                        }
                    }
                    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("PrimaryCCA=%d\n",PrimaryCCA->PriCCA_flag));
                    ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Intf_Type=%d\n", PrimaryCCA->intf_type));
                }
                //2 STA is Connected
                else
                {
                    if(Client_40MHz == 0)        //3 // client BW = 20MHz
                    {
                        if(PrimaryCCA->PriCCA_flag == 0)
                        {
                            PrimaryCCA->PriCCA_flag = 1;
                            if(SecCHOffset==1)
                                ODM_SetBBReg(pDM_Odm, 0xc6c, BIT8|BIT7, 2);
                            else if(SecCHOffset==2)
                                ODM_SetBBReg(pDM_Odm, 0xc6c, BIT8|BIT7, 1);
                        }
                        ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("STA Connected 20M!!! PrimaryCCA=%d\n", PrimaryCCA->PriCCA_flag));
                    }
                    else        //3 // client BW = 40MHz
                    {
                        if(PrimaryCCA->intf_flag == 1)    // interference is detected!!
                        {
                            if(PrimaryCCA->intf_type == 1)
                            {
                                if(PrimaryCCA->PriCCA_flag!=1)
                                {
                                    PrimaryCCA->PriCCA_flag = 1;
                                    if(SecCHOffset==1)
                                        ODM_SetBBReg(pDM_Odm, 0xc6c, BIT8|BIT7, 2);
                                    else if(SecCHOffset==2)
                                        ODM_SetBBReg(pDM_Odm, 0xc6c, BIT8|BIT7, 1);
                                }
                            }
                            else if(PrimaryCCA->intf_type == 2)
                            {
                                if(PrimaryCCA->DupRTS_flag!=1)
                                    PrimaryCCA->DupRTS_flag = 1;
                            }
                        }
                        else   // if intf_flag==0
                        {
                            if((curTxOkCnt+curRxOkCnt)<10000)   //idle mode or TP traffic is very low
                            {
                                if(SecCHOffset == 1)
                                {
                                    if((FalseAlmCnt->Cnt_OFDM_CCA > 800)&&(FalseAlmCnt->Cnt_BW_LSC*5 > FalseAlmCnt->Cnt_BW_USC*9))
                                    {
                                        PrimaryCCA->intf_flag = 1;
                                        if(FalseAlmCnt->Cnt_Ofdm_fail > FalseAlmCnt->Cnt_OFDM_CCA>>1)        
                                            PrimaryCCA->intf_type = 1;        // interference is shift
                                        else
                                            PrimaryCCA->intf_type = 2;        // interference is in-band
                                    }
                                }
                                else if(SecCHOffset == 2)
                                {
                                    if((FalseAlmCnt->Cnt_OFDM_CCA > 800)&&(FalseAlmCnt->Cnt_BW_USC*5 > FalseAlmCnt->Cnt_BW_LSC*9))
                                    {
                                        PrimaryCCA->intf_flag = 1;
                                        if(FalseAlmCnt->Cnt_Ofdm_fail > FalseAlmCnt->Cnt_OFDM_CCA>>1)        
                                            PrimaryCCA->intf_type = 1;        // interference is shift
                                        else
                                            PrimaryCCA->intf_type = 2;        // interference is in-band
                                    }
 
                                }
                            }
                            else     // TP Traffic is High
                            {
                                if(SecCHOffset == 1)
                                {
                                    if(FalseAlmCnt->Cnt_BW_LSC > (FalseAlmCnt->Cnt_BW_USC+500))
                                    {    
                                        if(Delay == 0)    // add delay to avoid interference occurring abruptly, jump one time
                                        {
                                            PrimaryCCA->intf_flag = 1;
                                            if(FalseAlmCnt->Cnt_Ofdm_fail > FalseAlmCnt->Cnt_OFDM_CCA>>1)
                                                PrimaryCCA->intf_type = 1;        // interference is shift
                                            else
                                                PrimaryCCA->intf_type = 2;        // interference is in-band
                                            Delay = 1;
                                        }
                                        else
                                            Delay = 0;
                                    }    
                                }    
                                else if(SecCHOffset == 2)
                                {
                                    if(FalseAlmCnt->Cnt_BW_USC > (FalseAlmCnt->Cnt_BW_LSC+500))
                                    {    
                                        if(Delay == 0)    // add delay to avoid interference occurring abruptly
                                        {
                                            PrimaryCCA->intf_flag = 1;
                                            if(FalseAlmCnt->Cnt_Ofdm_fail > FalseAlmCnt->Cnt_OFDM_CCA>>1)
                                                PrimaryCCA->intf_type = 1;        // interference is shift
                                            else
                                                PrimaryCCA->intf_type = 2;        // interference is in-band
                                            Delay = 1;
                                        }
                                        else
                                            Delay = 0;
                                    }    
                                }
                            }
                        }
                        ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Primary CCA=%d\n", PrimaryCCA->PriCCA_flag));
                        ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Duplicate RTS=%d\n", PrimaryCCA->DupRTS_flag));
                    }
 
                }// end of connected
            }
        }
        //1 Dynamic Primary CCA Monitor Counter
        if((PrimaryCCA->PriCCA_flag == 1)||(PrimaryCCA->DupRTS_flag == 1))
        {
            if(Client_40MHz == 0)     // client=20M no need to monitor primary cca flag  
            {
                Client_40MHz_pre = Client_40MHz;
                return;
            }
            Counter++;
            ODM_RT_TRACE(pDM_Odm,ODM_COMP_DYNAMIC_PRICCA, ODM_DBG_LOUD, ("Counter=%d\n", Counter));
            if((Counter == 30)||((Client_40MHz -Client_40MHz_pre)==1))      // Every 60 sec to monitor one time
            {
                PrimaryCCA->Monitor_flag = 1;     // monitor flag is triggered!!!!!
                if(PrimaryCCA->PriCCA_flag == 1)
                {
                    PrimaryCCA->PriCCA_flag = 0;
                    ODM_SetBBReg(pDM_Odm, 0xc6c, BIT8|BIT7, 0);
                }
                Counter = 0;
            }
        }
    }
 
    Client_40MHz_pre = Client_40MHz;
}
#else //#if (RTL8188E_SUPPORT == 1)
 
VOID
odm_PrimaryCCA_Init(
    IN        PDM_ODM_T        pDM_Odm)
{
}
VOID
odm_DynamicPrimaryCCA(
    IN        PDM_ODM_T        pDM_Odm
    )
{
}
BOOLEAN
ODM_DynamicPrimaryCCA_DupRTS(
    IN        PDM_ODM_T        pDM_Odm
    )
{
    return FALSE;
}
#endif //#if (RTL8188E_SUPPORT == 1)