PageRenderTime 8ms CodeModel.GetById 13ms app.highlight 84ms RepoModel.GetById 5ms app.codeStats 0ms

/STM32F0xx_StdPeriph_Lib_V1.0.0/Utilities/STM32_EVAL/STM320518_EVAL/stm320518_eval_cec.c

https://github.com/rurume/stm32-test
C | 2044 lines | 1316 code | 336 blank | 392 comment | 358 complexity | 1d98fcf6242b29116aa9cd8372328463 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1/**
   2  ******************************************************************************
   3  * @file    stm320518_eval_cec.c
   4  * @author  MCD Application Team
   5  * @version V1.0.0
   6  * @date    20-April-2012
   7  * @brief   This file provides all the STM320518-EVAL HDMI-CEC firmware functions.
   8  *
   9  *          ===================================================================
  10  *          Notes: 
  11  *           - This driver runs only on the STM320518-EVAL RevB
  12  *          ===================================================================
  13  *     
  14  ******************************************************************************
  15  * @attention
  16  *
  17  * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  18  *
  19  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  20  * You may not use this file except in compliance with the License.
  21  * You may obtain a copy of the License at:
  22  *
  23  *        http://www.st.com/software_license_agreement_liberty_v2
  24  *
  25  * Unless required by applicable law or agreed to in writing, software 
  26  * distributed under the License is distributed on an "AS IS" BASIS, 
  27  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  28  * See the License for the specific language governing permissions and
  29  * limitations under the License.
  30  *
  31  ******************************************************************************
  32  */
  33
  34/* Includes ------------------------------------------------------------------*/
  35#include "stm320518_eval_cec.h"

  36
  37/** @addtogroup Utilities
  38  * @{
  39  */
  40
  41/** @addtogroup STM32_EVAL
  42  * @{
  43  */ 
  44
  45/** @addtogroup STM320518_EVAL
  46  * @{
  47  */
  48  
  49/** @defgroup STM320518_EVAL_CEC
  50  * @brief This file includes the CEC Stack driver for HDMI-CEC Module
  51  *        of STM320518-EVAL board.
  52  * @{
  53  */
  54
  55/** @defgroup STM320518_EVAL_CEC_Private_Types
  56  * @{
  57  */
  58
  59/**
  60  * @}
  61  */
  62
  63/** @defgroup STM320518_EVAL_CEC_Private_Defines
  64  * @{
  65  */
  66/* Uncomment the define to use the LSE as a source clock */
  67/* #define LSE_SOURCE_CLK */ 
  68
  69/**
  70  * @}
  71  */
  72
  73/** @defgroup STM320518_EVAL_CEC_Private_Macros
  74  * @{
  75  */
  76/**
  77  * @}
  78  */
  79
  80/** @defgroup STM320518_EVAL_CEC_Private_Variables
  81  * @{
  82  */
  83__IO uint32_t ReceivedFrame = 0;
  84__IO uint32_t SendFrame = 0;
  85uint32_t BufferCount = 0, TxCounter = 0, RxCounter = 0;
  86__IO uint8_t BufferPointer[15];
  87__IO uint32_t ReceiveStatus = 0;
  88__IO uint32_t SendStatus = 0;
  89__IO uint8_t TransErrorCode = 0;
  90uint8_t MyLogicalAddress = 0;
  91uint16_t MyPhysicalAddress = 0;
  92__IO uint8_t DeviceType = 0;
  93#ifdef HDMI_CEC_USE_DDC

  94uint8_t pBuffer[256];
  95__IO uint16_t NumByteToRead = 255;
  96#endif

  97
  98__IO uint8_t CECDevicesNumber = 0;
  99HDMI_CEC_Message HDMI_CEC_TX_MessageStructPrivate;
 100HDMI_CEC_Message HDMI_CEC_RX_MessageStructPrivate;
 101HDMI_CEC_Message HDMI_CEC_TX_MessageStructure;
 102
 103__IO uint8_t FeatureOpcode = 0;
 104__IO uint8_t AbortReason = 0;
 105__IO uint8_t DeviceCount = 0;
 106
 107HDMI_CEC_Map HDMI_CEC_MapStruct;
 108HDMI_CEC_Map HDMI_CEC_DeviceMap[14];
 109
 110/* CEC follower addresses */
 111uint8_t* HDMI_CEC_Follower_String[13][2] =
 112  {
 113    {(uint8_t *)"         TV         ", (uint8_t *)"0"},
 114    {(uint8_t *)" Recording Device 1 ", (uint8_t *)"0"},
 115    {(uint8_t *)" Recording Device 2 ", (uint8_t *)"0"},
 116    {(uint8_t *)"      Tuner 1       ", (uint8_t *)"0"},
 117    {(uint8_t *)"  Playback Device 1 ", (uint8_t *)"0"},
 118    {(uint8_t *)"    Audio System    ", (uint8_t *)"0"},
 119    {(uint8_t *)"      Tuner 2       ", (uint8_t *)"0"},
 120    {(uint8_t *)"      Tuner 3       ", (uint8_t *)"0"},
 121    {(uint8_t *)"  Playback Device 2 ", (uint8_t *)"0"},
 122    {(uint8_t *)" Recording Device 3 ", (uint8_t *)"0"},
 123    {(uint8_t *)"      Tuner 4       ", (uint8_t *)"0"},
 124    {(uint8_t *)"  Playback Device 3 ", (uint8_t *)"0"},
 125    {(uint8_t *)"      Broadcast     ", (uint8_t *)"1"}
 126  };
 127
 128/* EDID & CEA861 Extension */
 129const uint8_t EDID_Structure[256] =
 130{
 131  /*-------------------------- Block0: 128Bytes ------------------------------*/
 132  /* Header */
 133  0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 
 134  /* Vendor & Product Identification */
 135  0x4C, 0x2D, 0x57, 0x03, 0x00, 0x00, 0x00, 0x00, 0x22, 0x11,
 136  /* EDID Structure Version & Revision */ 
 137  0x01, 0x03, 
 138  /* Basic Display Parameters & Features */
 139  0x80, 0x10, 0x09, 0x78, 0x0A, 
 140  /* Color Characteristics */
 141  0x1D, 0xED, 0xA7, 0x53, 0x34, 0xAC, 0x25, 0x12, 0x47, 0x4A, 
 142  /* Established Timings */
 143  0x20, 0x00, 0x00, 
 144  /* Standard Timings */
 145  0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
 146  0x01, 0x01, 0x01, 
 147  /* Byte Data Blocks*/
 148  0x02, 0x3A, 0x80, 0x18, 0x71, 0x38, 0x2D, 0x40, 0x58, 0x2C, 0x45, 0x00, 0xA0, 
 149  0x5A, 0x00, 0x00, 0x00, 0x1E, 0x01, 0x1D, 0x00, 0x72, 0x51, 0xD0, 0x1E, 0x20, 
 150  0x6E, 0x28, 0x55, 0x00, 0xA0, 0x5A, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 
 151  0xFD, 0x00, 0x17, 0x3D, 0x1A, 0x44, 0x17, 0x00, 0x0A, 0x20, 0x20, 0x20, 0x20, 
 152  0x20, 0x20, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x53, 0x41, 0x4D, 0x53, 0x55, 0x4E, 
 153  0x47, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20, 
 154  /* Extension Block Count */ 
 155  0x01,
 156  /* Checksum C */
 157  0xF3, 
 158  /* ------------------------ Block1: 128Bytes------------------------------- */
 159  /* Refer to CEA 861 Standard for definitions */
 160  /* CEA 861 EXTENSION Block – Version 3 */
 161  0x02, 0x03, 
 162  /* Byte number within this block where the 18-byte DTDs begin */
 163  0x21, 
 164  /* Number of DTDs present */
 165  0x00,
 166  /* Start of Data Block Collection */
 167  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
 168  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 169  /* IEEE Registration Identifier */
 170  0x03, 0x0C, 0x00,
 171  /* Vendor Specific Data: Physical Address */
 172  0x10, 0x00,
 173  /* Byte Data Blocks */
 174  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
 175  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
 176  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
 177  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
 178  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
 179  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
 180  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 
 181  0x00, 0x00, 0x00, 0x00, 0x00
 182};
 183
 184/**
 185  * @}
 186  */
 187
 188
 189/** @defgroup STM320518_EVAL_CEC_Private_Function_Prototypes
 190  * @{
 191  */
 192static HDMI_CEC_Error SinkPhysicalAddressDiscovery(void);
 193static HDMI_CEC_Error SourcePhysicalAddressDiscovery(void);
 194static HDMI_CEC_Error LogicalAddressAllocation(void);
 195
 196
 197/**
 198  * @}
 199  */
 200
 201
 202/** @defgroup STM320518_EVAL_CEC_Private_Functions
 203  * @{
 204  */
 205
 206/**
 207  * @brief  Initializes the HDMI CEC.
 208  * @param  None
 209  * @retval HDMI_CEC_Error: CEC Error code
 210  */
 211HDMI_CEC_Error HDMI_CEC_Init(void)
 212{
 213  GPIO_InitTypeDef GPIO_InitStructure;
 214  CEC_InitTypeDef CEC_InitStructure;
 215  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
 216  uint8_t sendcount = 0;
 217  uint32_t  idx = 0;
 218  
 219#ifdef LSE_SOURCE_CLK

 220  /* LSE */
 221  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
 222  PWR_BackupAccessCmd(ENABLE);
 223  
 224  RCC_LSEConfig(RCC_LSE_ON);
 225  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
 226  {}
 227
 228  RCC_CECCLKConfig(RCC_CECCLK_LSE);
 229#endif  

 230  
 231  for(idx=0; idx<12; idx++)
 232  {
 233    HDMI_CEC_Follower_String[idx][1] = (uint8_t *)"0";
 234  }
 235  
 236  /* Enable CEC and PWR clocks */
 237  RCC_APB1PeriphClockCmd(RCC_APB1Periph_CEC | RCC_APB1Periph_PWR, ENABLE);
 238
 239  /* Enable CEC_LINE_GPIO clocks */
 240  RCC_AHBPeriphClockCmd(HDMI_CEC_LINE_GPIO_CLK, ENABLE);
 241
 242  /* Configure CEC_LINE_GPIO as Output open drain */
 243  GPIO_InitStructure.GPIO_Pin = HDMI_CEC_LINE_PIN;
 244  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
 245  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
 246  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
 247  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
 248  GPIO_Init(HDMI_CEC_LINE_GPIO_PORT, &GPIO_InitStructure);
 249  
 250  GPIO_PinAFConfig(HDMI_CEC_LINE_GPIO_PORT, GPIO_PinSource10, GPIO_AF_0); 
 251  
 252  /* CEC DeInit */
 253  CEC_DeInit();
 254
 255  /* Configure CEC */
 256  CEC_InitStructure.CEC_SignalFreeTime = CEC_SignalFreeTime_Standard;
 257  CEC_InitStructure.CEC_RxTolerance = CEC_RxTolerance_Standard;
 258  CEC_InitStructure.CEC_StopReception = CEC_StopReception_Off;
 259  CEC_InitStructure.CEC_BitRisingError = CEC_BitRisingError_Off;
 260  CEC_InitStructure.CEC_LongBitPeriodError = CEC_LongBitPeriodError_Off;
 261  CEC_InitStructure.CEC_BRDNoGen = CEC_BRDNoGen_Off;
 262  CEC_InitStructure.CEC_SFTOption = CEC_SFTOption_Off;
 263  CEC_Init(&CEC_InitStructure);
 264  
 265  /* Disable Listen Mode */
 266  CEC_ListenModeCmd(DISABLE);
 267  
 268  /* Enable CEC */
 269  CEC_Cmd(ENABLE);
 270
 271  if (HDMI_CEC_ROOT == 0x1)
 272  {
 273    /* Logical Address Allocation */
 274    sendcount = 0;
 275    errorstatus = LogicalAddressAllocation();
 276    
 277    while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
 278    {
 279      sendcount++;
 280      errorstatus = LogicalAddressAllocation();
 281    }
 282    
 283    if (errorstatus != HDMI_CEC_OK)
 284    {
 285      /* Device Unregistered */
 286      return(errorstatus);
 287    }
 288    
 289    HDMI_CEC_CheckConnectedDevices();
 290    
 291    /* Set the CEC initiator address */
 292    CEC_OwnAddressConfig(MyLogicalAddress);
 293    
 294    /* Sink Physical Address discovery */
 295    errorstatus = SinkPhysicalAddressDiscovery();
 296    
 297    if (errorstatus != HDMI_CEC_OK)
 298    {
 299      /* Device not connected (Physical Address lost) */
 300      return(errorstatus);
 301    }
 302  }
 303  else
 304  {
 305    /* Source Physical Address discovery */
 306    errorstatus = SourcePhysicalAddressDiscovery();
 307    
 308    if (errorstatus != HDMI_CEC_OK)
 309    {
 310      /* Device not connected (Physical Address lost) */
 311      return(errorstatus);
 312    }
 313    
 314    /* Logical Address Allocation */
 315    sendcount = 0;
 316    errorstatus = LogicalAddressAllocation();
 317    
 318    while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
 319    {
 320      sendcount++;
 321      errorstatus = LogicalAddressAllocation();
 322    }
 323    
 324    if (errorstatus != HDMI_CEC_OK)
 325    {
 326      /* Device Unregistered */
 327      return(errorstatus);
 328    }
 329    
 330    HDMI_CEC_CheckConnectedDevices();
 331    
 332    /* Set the CEC initiator address */
 333    CEC_OwnAddressConfig(MyLogicalAddress);
 334  }  
 335  
 336  /* Activate CEC interrupts associated to the set of TX flags */
 337  CEC_ITConfig(CEC_IT_TXEND|CEC_IT_TXBR,ENABLE);
 338  
 339  /* Activate CEC interrupts associated to the set of RX flags */
 340  CEC_ITConfig(CEC_IT_RXEND|CEC_IT_RXBR,ENABLE);
 341  
 342  /* Activate CEC interrupts associated to the set of TX error */
 343  CEC_ITConfig(CEC_IT_TXACKE|CEC_IT_TXERR|CEC_IT_TXUDR|CEC_IT_ARBLST,ENABLE);
 344  
 345  /* Activate CEC interrupts associated to the set of RX error */
 346  CEC_ITConfig(CEC_IT_RXACKE|CEC_IT_LBPE|CEC_IT_SBPE|CEC_IT_BRE|CEC_IT_RXOVR,ENABLE);
 347  
 348  /* Report physical address */
 349  errorstatus = HDMI_CEC_ReportPhysicalAddress();
 350  sendcount = 0;
 351  RxCounter = 0;
 352  
 353  while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
 354  {
 355    sendcount++;
 356    errorstatus = HDMI_CEC_ReportPhysicalAddress();
 357  }
 358
 359  if (errorstatus != HDMI_CEC_OK)
 360  {
 361    /* Device Unregistred */
 362    return(errorstatus);
 363  }
 364  return errorstatus;
 365}
 366
 367/**
 368  * @brief  Transmit message by taking data from typedef struct CEC_Meassage
 369  * @param  CEC_TX_MessageStructure: pointer to an CEC_Message structure that contains
 370  *         the message to be sent.
 371  * @retval HDMI_CEC_Error: CEC Error code
 372  */
 373HDMI_CEC_Error HDMI_CEC_TransmitMessage(HDMI_CEC_Message *HDMI_CEC_TX_MessageStructure)
 374{
 375  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
 376  __IO uint32_t count = 0, j = 0;
 377
 378  SendFrame = 0;
 379  SendStatus = 0;
 380  TxCounter = 0;
 381  BufferCount = 0;
 382
 383  HDMI_CEC_TX_MessageStructPrivate = *HDMI_CEC_TX_MessageStructure;
 384
 385  /* Initialize BufferPointer */
 386  for (j = 0; j < 15; j++)
 387  {
 388    BufferPointer[j] = 0;
 389  }
 390
 391  BufferPointer[0] =  HDMI_CEC_TX_MessageStructPrivate.Opcode;
 392
 393  for (BufferCount = 1; BufferCount < HDMI_CEC_TX_MessageStructPrivate.TxMessageLength + 1; BufferCount++)
 394  {
 395    BufferPointer[BufferCount] = HDMI_CEC_TX_MessageStructPrivate.Operande[BufferCount - 1];
 396  }
 397
 398  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
 399
 400  /* Write single Data in the TX Buffer to Transmit through the CEC peripheral */
 401  CEC_SendData(HDMI_CEC_TX_MessageStructPrivate.Header);
 402
 403  /* Initiate Message Transmission */
 404  CEC_StartOfMessage();
 405
 406  while ((SendFrame == 0) && (count < HDMI_CEC_TIMEOUT_VALUE))
 407  {
 408    count++;
 409  }
 410  if (count >= HDMI_CEC_TIMEOUT_VALUE)
 411  {
 412    errorstatus = HDMI_CEC_TIMEOUT;
 413    return(errorstatus);
 414  }
 415
 416  if (SendStatus == 0)
 417  {
 418    errorstatus = (HDMI_CEC_Error) TransErrorCode;
 419  }
 420  return errorstatus;
 421}
 422
 423/**
 424  * @brief  Get the ISR register status.
 425  * @param  None
 426  * @retval HDMI_CEC_Error: CEC Error code
 427  */
 428HDMI_CEC_Error HDMI_CEC_GetErrorStatus (void)
 429{
 430  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
 431
 432  /* RX Overrun error case*/
 433  if (CEC_GetFlagStatus(CEC_FLAG_RXOVR) != RESET)
 434  {
 435    errorstatus = HDMI_CEC_RX_OVERRUN;
 436    CEC_ClearFlag(CEC_FLAG_RXOVR);
 437  }
 438  /* Bit Rising error case */
 439  if (CEC_GetFlagStatus(CEC_FLAG_BRE) != RESET)
 440  {
 441    errorstatus = HDMI_CEC_RX_BIT_RISING;
 442    CEC_ClearFlag(CEC_FLAG_BRE);
 443  }
 444  /* Short Bit Period error case */
 445  if (CEC_GetFlagStatus(CEC_FLAG_SBPE) != RESET)
 446  {
 447    errorstatus = HDMI_CEC_RX_SHORT_BIT_PERIOD;
 448    CEC_ClearFlag(CEC_FLAG_SBPE);
 449  }
 450  /* Long Bit Period error case */
 451  if (CEC_GetFlagStatus(CEC_FLAG_LBPE) != RESET)
 452  {
 453    errorstatus = HDMI_CEC_RX_LONG_BIT_PERIOD;
 454    CEC_ClearFlag(CEC_FLAG_LBPE);
 455  }
 456  /* RX Acknowledge error case */
 457  if (CEC_GetFlagStatus(CEC_FLAG_RXACKE) != RESET)
 458  {
 459    errorstatus = HDMI_CEC_RX_ACKNOWLEDGE;
 460    CEC_ClearFlag(CEC_FLAG_RXACKE);
 461  }
 462  /* Arbitration Lost error case */
 463  if (CEC_GetFlagStatus(CEC_FLAG_ARBLST) != RESET)
 464  {
 465    errorstatus = HDMI_CEC_ARBITRATION_LOST;
 466    CEC_ClearFlag(CEC_FLAG_ARBLST);
 467  }
 468  /* TX underrun error case */
 469  if (CEC_GetFlagStatus(CEC_FLAG_TXUDR) != RESET)
 470  {
 471    errorstatus = HDMI_CEC_TX_UNDERRUN;
 472    CEC_ClearFlag(CEC_FLAG_TXUDR);
 473  }
 474  /* TX error case */
 475  if (CEC_GetFlagStatus(CEC_FLAG_TXERR) != RESET)
 476  {
 477    errorstatus = HDMI_CEC_TX_ERROR;
 478    CEC_ClearFlag(CEC_FLAG_TXERR);
 479  }
 480  /* TX Acknowledge error case */
 481  if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
 482  {
 483    errorstatus = HDMI_CEC_TX_ACKNOWLEDGE;
 484    CEC_ClearFlag(CEC_FLAG_TXACKE);
 485  } 
 486  return errorstatus;
 487}
 488
 489/**
 490  * @brief  Allows to process all the interrupts that are high.
 491  * @param  None
 492  * @retval None
 493  */
 494void HDMI_CEC_ProcessIRQSrc(void)
 495{
 496  /********************** Reception *******************************************/
 497  /* Check if a reception error occured */
 498  if (CEC->ISR & (CEC_IT_RXACKE | CEC_IT_LBPE | CEC_IT_SBPE | CEC_IT_BRE | CEC_IT_RXOVR))
 499  {
 500    /* Set receive status bit (Error) */
 501    ReceiveStatus  = 0;
 502    ReceivedFrame  = 1;
 503    CEC->ISR = (CEC_IT_RXACKE | CEC_IT_LBPE | CEC_IT_SBPE | CEC_IT_BRE | CEC_IT_RXOVR);
 504    RxCounter = 0;
 505  }
 506  
 507  if (CEC_GetITStatus(CEC_IT_RXBR))
 508  { 
 509    /* Check if the byte received is a Header */
 510    if (RxCounter == 0)
 511    {
 512      HDMI_CEC_RX_MessageStructPrivate.Header = CEC_ReceiveData();
 513      RxCounter ++;
 514    }
 515    else if(RxCounter == 1)   /* Receive opcode in the reception buffer */
 516    {
 517      HDMI_CEC_RX_MessageStructPrivate.Opcode = CEC_ReceiveData();
 518      RxCounter++;
 519    }
 520    else /* Receive each byte except header and opcode in the reception buffer */
 521    {
 522      HDMI_CEC_RX_MessageStructPrivate.Operande[RxCounter-2] = CEC_ReceiveData();
 523      RxCounter++;
 524    }  
 525    CEC_ClearITPendingBit(CEC_IT_RXBR);
 526  }
 527  
 528  /* Check if the byte received is the last one of the message */
 529  if (CEC_GetITStatus(CEC_IT_RXEND))
 530  {  
 531    if (RxCounter == 1)
 532    {
 533      
 534      HDMI_CEC_RX_MessageStructPrivate.RxMessageLength = RxCounter-1;
 535    } 
 536    else
 537    {
 538      HDMI_CEC_RX_MessageStructPrivate.RxMessageLength = RxCounter-2;
 539      ReceivedFrame = 1;
 540    }
 541 
 542    /* Set the receive status */
 543    ReceiveStatus = SUCCESS;
 544    RxCounter = 0;
 545    
 546    /* Clear all reception flags */
 547    CEC_ClearITPendingBit(CEC_IT_RXEND);
 548  }
 549  
 550  /********************** Transmission ****************************************/
 551  /* Check if a transmission error occurred */
 552  if (CEC->ISR & (CEC_IT_TXACKE | CEC_IT_TXERR | CEC_IT_TXUDR | CEC_IT_ARBLST))
 553  { 
 554    TransErrorCode = HDMI_CEC_GetErrorStatus();
 555    CEC->ISR = (CEC_IT_TXACKE | CEC_IT_TXERR | CEC_IT_TXUDR | CEC_IT_ARBLST);
 556    SendFrame = 1;
 557    SendStatus = 0;
 558    TxCounter = 0;
 559  }
 560  
 561  /* Check if end of message bit is set in the data to be transmitted */
 562  if (CEC_GetITStatus(CEC_IT_TXEND))
 563  {
 564    CEC_ClearITPendingBit(CEC_IT_TXEND | CEC_IT_TXBR);
 565    SendFrame = 1;
 566    SendStatus = SUCCESS;
 567  }
 568  /* Check if data byte has been sent */
 569  else if (CEC_GetITStatus(CEC_IT_TXBR))
 570  {
 571    /* Set EOM bit if the byte to be transmitted is the last one of the Transmit Buffer */
 572    if (TxCounter == (HDMI_CEC_TX_MessageStructPrivate.TxMessageLength))
 573    {
 574      CEC_EndOfMessage();
 575      CEC_SendData(BufferPointer[TxCounter++]);
 576    }
 577    else
 578    {
 579      /* Put the byte in the TX Buffer */
 580      CEC_SendData(BufferPointer[TxCounter++]);
 581    }
 582    CEC_ClearITPendingBit(CEC_IT_TXBR);
 583  }
 584}
 585
 586/**
 587  * @brief  Report physical address to all other devices thus allowing any
 588            device to create a map of the network.
 589  * @param  None
 590  * @retval HDMI_CEC_Error: CEC Error code.
 591  */
 592HDMI_CEC_Error HDMI_CEC_ReportPhysicalAddress(void)
 593{
 594  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
 595  HDMI_CEC_Message HDMI_CEC_TX_Message;
 596
 597  HDMI_CEC_TX_Message.Header = ((MyLogicalAddress << 4) | 0xF);
 598  HDMI_CEC_TX_Message.Opcode = HDMI_CEC_OPCODE_REPORT_PHYSICAL_ADDRESS;
 599  HDMI_CEC_TX_Message.Operande[0] = MyPhysicalAddress >> 8;
 600  HDMI_CEC_TX_Message.Operande[1] = MyPhysicalAddress & 0xFF;
 601  HDMI_CEC_TX_Message.Operande[2] = DeviceType;
 602  HDMI_CEC_TX_Message.TxMessageLength = 0x03;
 603
 604  errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_Message);
 605
 606  return  errorstatus;
 607}
 608
 609/**
 610  * @brief  Handle CEC command receive callback.
 611  * @note   When receiving the STANDBY Opcode command, the system is entered in
 612  *         Stop mode and when wakeup, the PLL is configured as system clock and
 613  *         the HSI is selected as PLL source.      
 614  * @param  None
 615  * @retval None
 616  */
 617void HDMI_CEC_CommandCallBack(void)
 618{
 619  uint8_t i = 0, sendcount = 0;
 620  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
 621  ErrorStatus HSEStartUpStatus;
 622  
 623  switch (HDMI_CEC_RX_MessageStructPrivate.Opcode)
 624  {
 625    case HDMI_CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
 626      HDMI_CEC_MapStruct.PhysicalAddress_A = HDMI_CEC_RX_MessageStructPrivate.Operande[1] >> 4;
 627      HDMI_CEC_MapStruct.PhysicalAddress_B = HDMI_CEC_RX_MessageStructPrivate.Operande[1] & 0x0F;
 628      HDMI_CEC_MapStruct.PhysicalAddress_C = HDMI_CEC_RX_MessageStructPrivate.Operande[0] >> 4;
 629      HDMI_CEC_MapStruct.PhysicalAddress_D = HDMI_CEC_RX_MessageStructPrivate.Operande[0] & 0x0F;
 630      HDMI_CEC_MapStruct.LogicalAddress = (HDMI_CEC_RX_MessageStructPrivate.Header >> 0x4) & 0x0F;
 631      HDMI_CEC_MapStruct.DeviceType = HDMI_CEC_RX_MessageStructPrivate.Operande[2];
 632      HDMI_CEC_DeviceMap[DeviceCount] = HDMI_CEC_MapStruct;
 633      HDMI_CEC_Follower_String[(HDMI_CEC_DeviceMap[DeviceCount].LogicalAddress)][1] = (uint8_t *)"1";
 634      DeviceCount++;
 635      break;
 636
 637    case HDMI_CEC_OPCODE_STANDBY:
 638     
 639      /* Request to enter Stop mode */
 640      PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFI);
 641     
 642      /* Enable HSE */
 643      RCC_HSEConfig(RCC_HSE_ON);
 644      
 645      /* Wait till HSE is ready */
 646      HSEStartUpStatus = RCC_WaitForHSEStartUp();
 647      if (HSEStartUpStatus == SUCCESS)
 648      {
 649        /* Enable PLL */
 650        RCC_PLLCmd(ENABLE);
 651        /* Wait till PLL is ready */
 652        while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
 653        {}
 654        /* Select PLL as system clock source */
 655        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
 656        /* Wait till PLL is used as system clock source */
 657        while (RCC_GetSYSCLKSource() != 0x08)
 658        {}
 659      }
 660      break;
 661
 662    case HDMI_CEC_OPCODE_GET_CEC_VERSION:
 663      /* Send the Used CEC version */
 664      HDMI_CEC_TX_MessageStructPrivate.Header = ((MyLogicalAddress << 4) | HDMI_CEC_RX_MessageStructPrivate.Header >> 4);
 665      HDMI_CEC_TX_MessageStructPrivate.Opcode = HDMI_CEC_OPCODE_CEC_VERSION;
 666      HDMI_CEC_TX_MessageStructPrivate.Operande[0] = HDMI_CEC_VERSION; /* CEC Version */
 667      HDMI_CEC_TX_MessageStructPrivate.TxMessageLength = 0x01;
 668      errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
 669
 670      /* Retransmit message until 5 time */
 671      while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
 672      {
 673        sendcount++;
 674        errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
 675      }
 676      break;
 677
 678    case HDMI_CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
 679      /* Send the Physical address */
 680      errorstatus = HDMI_CEC_ReportPhysicalAddress();
 681      sendcount = 0;
 682      /* Retransmit message until 5 time */
 683      while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
 684      {
 685        sendcount++;
 686        errorstatus = HDMI_CEC_ReportPhysicalAddress();
 687      }
 688      break;
 689
 690    case HDMI_CEC_OPCODE_FEATURE_ABORT:
 691      /* The device doesn't support the requested message type, or that it cannot
 692      execute it at the present time. */
 693      FeatureOpcode = HDMI_CEC_RX_MessageStructPrivate.Operande[0];
 694      AbortReason = HDMI_CEC_RX_MessageStructPrivate.Operande[1];
 695      break;
 696
 697    case HDMI_CEC_OPCODE_GIVE_OSD_NAME:
 698      /* Send the OSD name = STM320518 CEC */
 699      HDMI_CEC_TX_MessageStructPrivate.Header = ((MyLogicalAddress << 4) | HDMI_CEC_RX_MessageStructPrivate.Header >> 4);
 700      HDMI_CEC_TX_MessageStructPrivate.Opcode = HDMI_CEC_OPCODE_SET_OSD_NAME;
 701      /* STM320518 */
 702      HDMI_CEC_TX_MessageStructPrivate.Operande[0] = 0x53;
 703      HDMI_CEC_TX_MessageStructPrivate.Operande[1] = 0x54;
 704      HDMI_CEC_TX_MessageStructPrivate.Operande[2] = 0x4D;
 705      HDMI_CEC_TX_MessageStructPrivate.Operande[3] = 0x33;
 706      HDMI_CEC_TX_MessageStructPrivate.Operande[4] = 0x32;
 707      HDMI_CEC_TX_MessageStructPrivate.Operande[5] = 0x30;
 708      HDMI_CEC_TX_MessageStructPrivate.Operande[6] = 0x35;
 709      HDMI_CEC_TX_MessageStructPrivate.Operande[7] = 0x31;
 710      HDMI_CEC_TX_MessageStructPrivate.Operande[8] = 0x38;
 711      HDMI_CEC_TX_MessageStructPrivate.Operande[9] = 0x20;
 712      /* CEC */
 713      HDMI_CEC_TX_MessageStructPrivate.Operande[10] = 0x43;
 714      HDMI_CEC_TX_MessageStructPrivate.Operande[11] = 0x45;
 715      HDMI_CEC_TX_MessageStructPrivate.Operande[12] = 0x43;
 716      HDMI_CEC_TX_MessageStructPrivate.TxMessageLength = 13;
 717      errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
 718      sendcount = 0;
 719      /* Retransmit message until 5 time */
 720      while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
 721      {
 722        sendcount++;
 723        errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
 724      }
 725      break;
 726
 727    case HDMI_CEC_OPCODE_ROUTING_CHANGE:
 728      for (i = 0;i < 0x14;i++)
 729      {
 730        if ((HDMI_CEC_DeviceMap[i].PhysicalAddress_A == HDMI_CEC_RX_MessageStructPrivate.Operande[1] >> 4) &&
 731            (HDMI_CEC_DeviceMap[i].PhysicalAddress_B == HDMI_CEC_RX_MessageStructPrivate.Operande[1]&0x0F) &&
 732            (HDMI_CEC_DeviceMap[i].PhysicalAddress_C == HDMI_CEC_RX_MessageStructPrivate.Operande[0] >> 4) &&
 733            (HDMI_CEC_DeviceMap[i].PhysicalAddress_D == (HDMI_CEC_RX_MessageStructPrivate.Operande[0]&0x0F)))
 734        {
 735          HDMI_CEC_MapStruct.LogicalAddress = (HDMI_CEC_RX_MessageStructPrivate.Header >> 0x4) & 0x0F;
 736          HDMI_CEC_MapStruct.DeviceType = HDMI_CEC_RX_MessageStructPrivate.Operande[2];
 737          HDMI_CEC_DeviceMap[i] = HDMI_CEC_MapStruct;
 738        }
 739      }
 740      break;
 741
 742    default:
 743      /* Send Abort feature */
 744      HDMI_CEC_TX_MessageStructPrivate.Header = ((MyLogicalAddress << 4) | HDMI_CEC_RX_MessageStructPrivate.Header >> 4);
 745      HDMI_CEC_TX_MessageStructPrivate.Opcode = HDMI_CEC_OPCODE_FEATURE_ABORT;
 746      HDMI_CEC_TX_MessageStructPrivate.Operande[0] = 0x02; /* Defines command to be performed */
 747      HDMI_CEC_TX_MessageStructPrivate.Operande[1] = HDMI_CEC_REFUSED; /* Reason for abort feature */
 748      HDMI_CEC_TX_MessageStructPrivate.TxMessageLength = 0x02;
 749      errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
 750      sendcount = 0;
 751      /* Retransmit message until 5 time */
 752      while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
 753      {
 754        sendcount++;
 755        errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
 756      }
 757      break;
 758  }
 759}
 760
 761/**
 762  * @brief  Check the connected CEC devices.
 763  * @param  None
 764  * @retval HDMI_CEC_Error
 765  */
 766HDMI_CEC_Error HDMI_CEC_CheckConnectedDevices(void)
 767{
 768  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
 769  uint32_t count = 0, i = 1;
 770 
 771  /*------------------------------- TV device --------------------------------*/
 772  CEC_EndOfMessage();
 773  
 774  CEC_SendData((MyLogicalAddress << 4) | 0x0);   
 775  
 776  /* Start of message */
 777  CEC_StartOfMessage();
 778
 779  /* Wait till the header message is sent */
 780  while (count < HDMI_CEC_TIMEOUT_VALUE)
 781  {
 782    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
 783    { 
 784      errorstatus = HDMI_CEC_GetErrorStatus();
 785      CEC_ClearFlag( CEC_FLAG_TXACKE);
 786      break;
 787    }
 788    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
 789    {
 790      errorstatus = HDMI_CEC_OK;
 791      break;
 792    }
 793    count++;
 794  }
 795
 796  if (count >= HDMI_CEC_TIMEOUT_VALUE)
 797  {
 798    errorstatus = HDMI_CEC_TIMEOUT;
 799    return(errorstatus);
 800  }
 801
 802  if (errorstatus == HDMI_CEC_OK)
 803  {
 804    HDMI_CEC_Follower_String[0][1] = (uint8_t *)"1";
 805    i++;
 806    errorstatus = HDMI_CEC_OK;
 807  }
 808
 809  /* Clear CEC ISR register */
 810  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
 811
 812  /*---------------------------- Recording device 1 --------------------------*/
 813  CEC_EndOfMessage();
 814  
 815  CEC_SendData((MyLogicalAddress << 4)|0x1);
 816  
 817  /* Start of message */
 818  CEC_StartOfMessage();
 819
 820  /* Wait till the header message is sent */
 821  while (count < HDMI_CEC_TIMEOUT_VALUE)
 822  {
 823    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
 824    { 
 825      errorstatus = HDMI_CEC_GetErrorStatus();
 826      CEC_ClearFlag( CEC_FLAG_TXACKE);
 827      break;
 828    }
 829    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
 830    {
 831      errorstatus = HDMI_CEC_OK;
 832      break;
 833    }
 834    count++;
 835  }
 836  if (count >= HDMI_CEC_TIMEOUT_VALUE)
 837  {
 838    errorstatus = HDMI_CEC_TIMEOUT;
 839    return(errorstatus);
 840  }
 841
 842  if (errorstatus == HDMI_CEC_OK)
 843  {
 844    HDMI_CEC_Follower_String[1][1] = (uint8_t *)"1";
 845    i++;
 846    errorstatus = HDMI_CEC_OK;
 847  }
 848
 849  /* Clear CEC ISR register */
 850  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
 851  
 852  /*---------------------------- Recording device 2 --------------------------*/ 
 853  CEC_EndOfMessage();
 854  
 855  CEC_SendData((MyLogicalAddress << 4) | 0x2);
 856 
 857  /* Start of message */
 858  CEC_StartOfMessage();
 859
 860  /* Wait till the header message is sent */
 861  while (count < HDMI_CEC_TIMEOUT_VALUE)
 862  {  
 863    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
 864    {
 865      errorstatus = HDMI_CEC_GetErrorStatus(); 
 866      CEC_ClearFlag( CEC_FLAG_TXACKE);
 867      break;
 868    }   
 869    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
 870    {
 871      errorstatus = HDMI_CEC_OK;
 872      break;
 873    }
 874    count++;
 875  }
 876
 877  if (count >= HDMI_CEC_TIMEOUT_VALUE)
 878  {
 879    errorstatus = HDMI_CEC_TIMEOUT;
 880    return(errorstatus);
 881  }
 882
 883  if (errorstatus == HDMI_CEC_OK)
 884  {
 885    HDMI_CEC_Follower_String[2][1] = (uint8_t *)"1";
 886    i++;
 887    errorstatus = HDMI_CEC_OK;
 888  }
 889
 890  /* Clear CEC ISR register */
 891  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
 892
 893  /*------------------------------ Tuner 1 -----------------------------------*/
 894  CEC_EndOfMessage();
 895  
 896  CEC_SendData((MyLogicalAddress << 4) | 0x3); 
 897  
 898  /* Start of message */
 899  CEC_StartOfMessage();
 900
 901  /* Wait till the header message is sent */
 902  while (count < HDMI_CEC_TIMEOUT_VALUE)
 903  {
 904    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
 905    {
 906      errorstatus = HDMI_CEC_GetErrorStatus();
 907      CEC_ClearFlag( CEC_FLAG_TXACKE);
 908      break;
 909    }    
 910    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
 911    {
 912      errorstatus = HDMI_CEC_OK;
 913      break;
 914    }
 915    count++;
 916  }
 917  if (count >= HDMI_CEC_TIMEOUT_VALUE)
 918  {
 919    errorstatus = HDMI_CEC_TIMEOUT;
 920    return(errorstatus);
 921  }
 922
 923  if (errorstatus == HDMI_CEC_OK)
 924  {
 925    HDMI_CEC_Follower_String[3][1] = (uint8_t *)"1";
 926    i++;
 927    errorstatus = HDMI_CEC_OK;
 928  }
 929  /* Clear CEC ISR register */
 930  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
 931  
 932  /*---------------------------- Playback device 1 ---------------------------*/
 933  CEC_EndOfMessage();
 934  
 935  CEC_SendData((MyLogicalAddress << 4) | 0x4); 
 936  
 937  /* Start of message */
 938  CEC_StartOfMessage();
 939
 940  /* Wait till the header message is sent */
 941  while (count < HDMI_CEC_TIMEOUT_VALUE)
 942  {
 943    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
 944    { 
 945      errorstatus = HDMI_CEC_GetErrorStatus();
 946      CEC_ClearFlag(CEC_FLAG_TXACKE);
 947      break;
 948    }
 949    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
 950    {
 951      errorstatus = HDMI_CEC_OK;
 952      break;
 953    }
 954    count++;
 955  }
 956
 957  if (count >= HDMI_CEC_TIMEOUT_VALUE)
 958  {
 959    errorstatus = HDMI_CEC_TIMEOUT;
 960    return(errorstatus);
 961  }
 962
 963  if (errorstatus == HDMI_CEC_OK)
 964  {
 965    HDMI_CEC_Follower_String[4][1] = (uint8_t *)"1";
 966    i++;
 967    errorstatus = HDMI_CEC_OK;
 968  }
 969  /* Clear CEC ISR register */
 970  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
 971
 972  /*-------------------------------- Audio system ----------------------------*/
 973  CEC_EndOfMessage();
 974
 975  CEC_SendData((MyLogicalAddress << 4) | 0x5); 
 976  
 977  /* Start of message */
 978  CEC_StartOfMessage();
 979
 980  /* Wait till the header message is sent */
 981  while (count < HDMI_CEC_TIMEOUT_VALUE)
 982  {
 983    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
 984    { 
 985      errorstatus = HDMI_CEC_GetErrorStatus();
 986      CEC_ClearFlag(CEC_FLAG_TXACKE);
 987      break;
 988    }
 989    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
 990    {
 991      errorstatus = HDMI_CEC_OK;
 992      break;
 993    }
 994    count++;
 995  }
 996
 997  if (count >= HDMI_CEC_TIMEOUT_VALUE)
 998  {
 999    errorstatus = HDMI_CEC_TIMEOUT;
1000    return(errorstatus);
1001  }
1002
1003  if (errorstatus == HDMI_CEC_OK)
1004  {
1005    HDMI_CEC_Follower_String[5][1] = (uint8_t *)"1";
1006    i++;
1007    errorstatus = HDMI_CEC_OK;
1008  }
1009  
1010  /* Clear CEC ISR register */
1011  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
1012
1013  /*--------------------------------- Tuner 2 --------------------------------*/
1014  CEC_EndOfMessage();
1015  
1016  CEC_SendData((MyLogicalAddress << 4) | 0x6); 
1017  
1018  /* Start of message */
1019  CEC_StartOfMessage();
1020
1021  /* Wait till the header message is sent */
1022  while (count < HDMI_CEC_TIMEOUT_VALUE)
1023  {
1024    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
1025    { 
1026      errorstatus = HDMI_CEC_GetErrorStatus();
1027      CEC_ClearFlag(CEC_FLAG_TXACKE);
1028      break;
1029    }
1030    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1031    {
1032      errorstatus = HDMI_CEC_OK;
1033      break;
1034    }
1035    count++;
1036  }
1037
1038  if (count >= HDMI_CEC_TIMEOUT_VALUE)
1039  {
1040    errorstatus = HDMI_CEC_TIMEOUT;
1041    return(errorstatus);
1042  }
1043
1044  if (errorstatus == HDMI_CEC_OK)
1045  {
1046    HDMI_CEC_Follower_String[6][1] = (uint8_t *)"1";
1047    i++;
1048    errorstatus = HDMI_CEC_OK;
1049  }
1050  /* Clear CEC ISR register */
1051  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
1052
1053  /*--------------------------------- Tuner 3 --------------------------------*/
1054  CEC_EndOfMessage();
1055  
1056  CEC_SendData((MyLogicalAddress << 4) | 0x7); 
1057  
1058  /* Start of message */
1059  CEC_StartOfMessage();
1060
1061  /* Wait till the header message is sent */
1062  while (count < HDMI_CEC_TIMEOUT_VALUE)
1063  {
1064    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
1065    { 
1066      errorstatus = HDMI_CEC_GetErrorStatus();
1067      CEC_ClearFlag(CEC_FLAG_TXACKE);
1068      break;
1069    }
1070    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1071    {
1072      errorstatus = HDMI_CEC_OK;
1073      break;
1074    }
1075    count++;
1076  }
1077  if (count >= HDMI_CEC_TIMEOUT_VALUE)
1078  {
1079    errorstatus = HDMI_CEC_TIMEOUT;
1080    return(errorstatus);
1081  }
1082
1083  if (errorstatus == HDMI_CEC_OK)
1084  {
1085    HDMI_CEC_Follower_String[7][1] = (uint8_t *)"1";
1086    i++;
1087    errorstatus = HDMI_CEC_OK;
1088  }
1089  
1090  /* Clear CEC ISR register */
1091  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
1092
1093  /*---------------------------- Playback device 2 ---------------------------*/  
1094  CEC_EndOfMessage();
1095  
1096  CEC_SendData((MyLogicalAddress << 4) | 0x8);  
1097  
1098  /* Start of message */
1099  CEC_StartOfMessage();
1100
1101  /* Wait till the header message is sent */
1102  while(count < HDMI_CEC_TIMEOUT_VALUE)
1103  {
1104    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
1105    { 
1106      errorstatus = HDMI_CEC_GetErrorStatus();
1107      CEC_ClearFlag(CEC_FLAG_TXACKE);
1108      break;
1109    }
1110    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1111    {
1112      errorstatus = HDMI_CEC_OK;
1113      break;
1114    }
1115    count++;
1116  }
1117
1118  if (count >= HDMI_CEC_TIMEOUT_VALUE)
1119  {
1120    errorstatus = HDMI_CEC_TIMEOUT;
1121    return(errorstatus);
1122  }
1123
1124  if (errorstatus == HDMI_CEC_OK)
1125  {
1126    HDMI_CEC_Follower_String[8][1] = (uint8_t *)"1";
1127    i++;
1128    errorstatus = HDMI_CEC_OK;
1129  }
1130  /* Clear CEC ISR register */
1131  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
1132
1133  /*---------------------------- Recording device 3 --------------------------*/
1134  CEC_EndOfMessage();
1135  
1136  CEC_SendData((MyLogicalAddress << 4) | 0x9); 
1137  
1138  /* Start of message */
1139  CEC_StartOfMessage();
1140
1141  /* Wait till the header message is sent */
1142  while(count < HDMI_CEC_TIMEOUT_VALUE)
1143  {
1144    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
1145    { 
1146      errorstatus = HDMI_CEC_GetErrorStatus();
1147      CEC_ClearFlag(CEC_FLAG_TXACKE);
1148      break;
1149    }
1150    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1151    {
1152      errorstatus = HDMI_CEC_OK;
1153      break;
1154    }
1155    count++;
1156  }
1157
1158  if (count >= HDMI_CEC_TIMEOUT_VALUE)
1159  {
1160    errorstatus = HDMI_CEC_TIMEOUT;
1161    return(errorstatus);
1162  }
1163
1164  if (errorstatus == HDMI_CEC_OK)
1165  {
1166    HDMI_CEC_Follower_String[9][1] = (uint8_t *)"1";
1167    i++;
1168    errorstatus = HDMI_CEC_OK;
1169  }
1170  /* Clear CEC ISR register */
1171  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
1172
1173  /*-------------------------------- Tuner 4 ---------------------------------*/
1174  CEC_EndOfMessage();
1175  
1176  CEC_SendData((MyLogicalAddress << 4) | 0xA); 
1177  
1178  /* Start of message */
1179  CEC_StartOfMessage();
1180
1181  /* Wait till the header message is sent */
1182  while (count < HDMI_CEC_TIMEOUT_VALUE)
1183  {
1184    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
1185    { 
1186      errorstatus = HDMI_CEC_GetErrorStatus();
1187      CEC_ClearFlag(CEC_FLAG_TXACKE);
1188      break;
1189    }
1190    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1191    {
1192      errorstatus = HDMI_CEC_OK;
1193      break;
1194    }
1195    count++;
1196  }
1197  
1198  if (count >= HDMI_CEC_TIMEOUT_VALUE)
1199  {
1200    errorstatus = HDMI_CEC_TIMEOUT;
1201    return(errorstatus);
1202  }
1203
1204  if (errorstatus == HDMI_CEC_OK)
1205  {
1206    HDMI_CEC_Follower_String[10][1] = (uint8_t *)"1";
1207    i++;
1208    errorstatus = HDMI_CEC_OK;
1209  }
1210  /* Clear CEC ISR register */
1211  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
1212
1213  /*---------------------------- Playback device 3 ---------------------------*/
1214  CEC_EndOfMessage();
1215  
1216  CEC_SendData((MyLogicalAddress << 4) | 0xB);
1217  
1218  /* Start of message */
1219  CEC_StartOfMessage();
1220
1221  /* Wait till the header message is sent */
1222  while(count < HDMI_CEC_TIMEOUT_VALUE)
1223  {
1224    if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
1225    { 
1226      errorstatus = HDMI_CEC_GetErrorStatus();
1227      CEC_ClearFlag(CEC_FLAG_TXACKE);
1228      break;
1229    }
1230    else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1231    {
1232      errorstatus = HDMI_CEC_OK;
1233      break;
1234    }
1235    count++;
1236  }
1237
1238  if (count >= HDMI_CEC_TIMEOUT_VALUE)
1239  {
1240    errorstatus = HDMI_CEC_TIMEOUT;
1241    return(errorstatus);
1242  }
1243
1244  if (errorstatus == HDMI_CEC_OK)
1245  {
1246    HDMI_CEC_Follower_String[11][1] = (uint8_t *)"1";
1247    i++;
1248    errorstatus = HDMI_CEC_OK;
1249  }
1250  /* Clear CEC ISR register */
1251  CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
1252
1253  CECDevicesNumber = i - 1;
1254
1255  return errorstatus;
1256}
1257
1258/**
1259  * @brief  Sink Physical address discovery.
1260  * @param  None
1261  * @retval HDMI_CEC_Error: CEC Error code.
1262  */
1263static HDMI_CEC_Error SinkPhysicalAddressDiscovery(void)
1264{
1265  uint32_t DataNum=0;
1266  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
1267    uint32_t  i = 0;
1268  I2C_InitTypeDef  I2C_InitStructure;
1269  GPIO_InitTypeDef GPIO_InitStructure;
1270  
1271  /*------------------------------ Physical address discovery ----------------*/
1272  /* if HDMI_CEC_ROOT == 0x1*/
1273  MyPhysicalAddress = 0x0000;
1274  /* The HDMI-CEC here is configured as sink or as a repeater. The configuration
1275  of the +5V power signal and the HPD should be well configured.
1276  Implement here the EDID Structure to be sent to the HDMI source.
1277  For more details please refer to the HDMI specification. 
1278  The EDID structure should be sent to the device source using the DDC Channel
1279  and using the HPD signal. */ 
1280  
1281  /* Enable CEC_I2C clocks */
1282  RCC_APB1PeriphClockCmd(HDMI_CEC_SINK_I2C_CLK, ENABLE);
1283  
1284  /* Enable CEC_I2C_GPIO and CEC_HPD_GPIO clocks */
1285  RCC_AHBPeriphClockCmd(HDMI_CEC_SINK_I2C_GPIO_CLK | HDMI_CEC_HPD_SINK_GPIO_CLK, ENABLE);
1286  
1287  I2C_DeInit(HDMI_CEC_SINK_I2C);
1288  
1289  GPIO_PinAFConfig(HDMI_CEC_SINK_I2C_GPIO_PORT, HDMI_CEC_SINK_I2C_SCL_PIN_SOURCE, GPIO_AF_1); 
1290  GPIO_PinAFConfig(HDMI_CEC_SINK_I2C_GPIO_PORT, HDMI_CEC_SINK_I2C_SDA_PIN_SOURCE, GPIO_AF_1);   
1291  
1292  /* Set PF6/PF7 as OD AF - I2C2_SCL/I2C2_SDA of Source*/
1293  GPIO_InitStructure.GPIO_Pin = HDMI_CEC_SINK_I2C_SDA_PIN | HDMI_CEC_SINK_I2C_SCL_PIN;
1294  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	
1295  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
1296  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;  
1297  GPIO_Init(HDMI_CEC_SINK_I2C_GPIO_PORT, &GPIO_InitStructure);
1298  
1299  /* Set PA11 as PP - HPD */    
1300  GPIO_InitStructure.GPIO_Pin = HDMI_CEC_HPD_SINK_PIN;
1301  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;	
1302  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
1303  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
1304  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;  
1305  GPIO_Init(HDMI_CEC_HPD_SINK_GPIO_PORT, &GPIO_InitStructure);
1306  
1307  RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);     
1308  
1309  HDMI_CEC_SINK_I2C->CR1 &= (uint16_t)(~I2C_CR1_NOSTRETCH); 
1310  
1311  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
1312  I2C_InitStructure.I2C_Timing = HDMI_CEC_I2C_TIMING;		
1313  I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
1314  /* Initialize the I2C_DigitalFilter member */
1315  I2C_InitStructure.I2C_DigitalFilter = 0x00;
1316  I2C_InitStructure.I2C_OwnAddress1 = HDMI_CEC_I2C_SLAVE_ADDRESS7;   
1317  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
1318  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
1319  I2C_Init(HDMI_CEC_SINK_I2C, &I2C_InitStructure);
1320  
1321  /* Set the Hot plug detect signal */
1322  HDMI_CEC_HPD_HIGH(); 
1323  
1324  /* The HDMI-CEC here is configured as source or as a repeater. The configuration
1325  of the +5V power signal and the HPD should be well configured.
1326  The source should wait for HPD and then read the EDID structure. */
1327  while(GPIO_ReadInputDataBit(HDMI_CEC_HPD_SINK_GPIO_PORT, HDMI_CEC_HPD_SINK_PIN) == RESET)
1328  {
1329  }
1330  
1331  /* Wait for 100 ms after HPD was received */
1332  for(i = 0; i <0x5FFFFF; i++)
1333  {
1334  }
1335  
1336  /* Enable HDMI_CEC_SINK_I2C */
1337  I2C_Cmd(HDMI_CEC_SINK_I2C, ENABLE); 
1338  
1339  /* Wait until ADDR is set */
1340  while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_ADDR));
1341  
1342  /* Clear ADDR flag */
1343  HDMI_CEC_SINK_I2C->ICR = I2C_ICR_ADDRCF;
1344  
1345  while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_RXNE));
1346  
1347  HDMI_CEC_SINK_I2C->RXDR; 
1348  
1349  /* Wait until ADDR is set */
1350  while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_ADDR));
1351  
1352  /* Clear ADDR flag */
1353  HDMI_CEC_SINK_I2C->ICR = I2C_ICR_ADDRCF;
1354  
1355  while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_TXIS));
1356  
1357  while (DataNum != 256)
1358  {               
1359    if (HDMI_CEC_SINK_I2C->ISR & I2C_ISR_TXIS)
1360    {
1361      /* Write data to TXDR */
1362      HDMI_CEC_SINK_I2C->TXDR = (uint8_t)(EDID_Structure[DataNum++]);
1363    }   
1364  }
1365  
1366  while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_STOPF));
1367  while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_NACKF));
1368  
1369  /* Clear STOPF and NACKF flag */
1370  HDMI_CEC_SINK_I2C->ICR = ( I2C_ICR_STOPCF | I2C_ICR_NACKCF) ;
1371  
1372  return errorstatus;
1373}
1374
1375/**
1376  * @brief  Source Physical address discovery.
1377  * @param  None
1378  * @retval HDMI_CEC_Error: CEC Error code.
1379  */
1380static HDMI_CEC_Error SourcePhysicalAddressDiscovery(void)
1381{
1382  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
1383  
1384#ifdef HDMI_CEC_USE_DDC  

1385  uint32_t DataNum=0;
1386  GPIO_InitTypeDef GPIO_InitStructure; 
1387  I2C_InitTypeDef  I2C_InitStructure;
1388  
1389  /* Enable CEC_I2C clocks */
1390  RCC_APB1PeriphClockCmd(HDMI_CEC_I2C_CLK, ENABLE);
1391  
1392  /* Enable CEC_I2C_GPIO and CEC_HPD_GPIO clocks */
1393  RCC_AHBPeriphClockCmd(HDMI_CEC_I2C_GPIO_CLK | HDMI_CEC_HPD_SOURCE_GPIO_CLK, ENABLE);
1394  
1395  GPIO_PinAFConfig(HDMI_CEC_I2C_GPIO_PORT,HDMI_CEC_I2C_SCL_PIN_SOURCE,GPIO_AF_1);
1396  GPIO_PinAFConfig(HDMI_CEC_I2C_GPIO_PORT,HDMI_CEC_I2C_SDA_PIN_SOURCE,GPIO_AF_1);
1397  
1398  /* Configure CEC_I2C_SCL_PIN and CEC_I2C_SDA_PIN  as Output open drain */
1399  GPIO_InitStructure.GPIO_Pin = HDMI_CEC_I2C_SCL_PIN | HDMI_CEC_I2C_SDA_PIN;
1400  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
1401  GPIO_Init(HDMI_CEC_I2C_GPIO_PORT, &GPIO_InitStructure);
1402 
1403/* This configuration is only when the HDMI CEC is configured as source.
1404   The HDMI source has to provide the +5V Power signal to the sink. 
1405   Then, the source will wait for HPD signal to be asserted from the sink.
1406   Once the HPD signal is detected the source shall read the EDID structure 
1407   through the DDC channel. */
1408  /* Configure CEC_HPD_GPIO as Input pull down */
1409  GPIO_InitStructure.GPIO_Pin = HDMI_CEC_HPD_SOURCE_PIN;
1410  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
1411  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
1412  GPIO_Init(HDMI_CEC_HPD_SOURCE_GPIO_PORT, &GPIO_InitStructure);
1413      
1414  /* I2C configuration */
1415  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
1416  I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
1417  I2C_InitStructure.I2C_DigitalFilter = 0x00;
1418  I2C_InitStructure.I2C_OwnAddress1 = 0x00;
1419  I2C_InitStructure.I2C_Ack =I2C_Ack_Enable;
1420  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
1421  I2C_InitStructure.I2C_Timing = HDMI_CEC_I2C_TIMING;
1422  
1423  /* I2C configuration */
1424  I2C_Init(HDMI_CEC_I2C, &I2C_InitStructure);
1425    
1426  /* I2C Peripheral Enable */
1427  I2C_Cmd(HDMI_CEC_I2C, ENABLE);
1428    
1429  /* Configure slave address, nbytes, reload and generate start */
1430  HDMI_CEC_I2C->CR2 = (uint32_t)(0xA0) | (uint32_t)(1 << 16) | I2C_CR2_START ;
1431  
1432  /* Wait until TXIS is set */
1433  while (!(HDMI_CEC_I2C->ISR & I2C_ISR_TXIS));
1434  
1435    /* Send memory address */
1436    HDMI_CEC_I2C->TXDR = (uint8_t)(0x00);
1437    
1438    /* Wait until TC is set */
1439    while (!(HDMI_CEC_I2C->ISR & I2C_ISR_TC));
1440    
1441    /* Update CR2 : set Nbytes and reload */        
1442    HDMI_CEC_I2C->CR2 = (uint32_t)(0xA0) | I2C_CR2_RD_WRN |  I2C_CR2_START | (uint32_t)(255 << 16) | I2C_CR2_RELOAD;     
1443    
1444    /* Wait until all data are received */
1445    while (DataNum != 255)
1446    {        
1447      /* Wait until RXNE flag is set */
1448      while (!(HDMI_CEC_I2C->ISR & I2C_ISR_RXNE));
1449
1450      /* Read data from RXDR */
1451      pBuffer[DataNum]= (uint8_t)HDMI_CEC_I2C->RXDR;
1452      
1453      /* Update number of received data */
1454      DataNum++;
1455    }
1456    
1457    /* Wait until TCR flag is set */     
1458    while (!(HDMI_CEC_I2C->ISR & I2C_ISR_TCR));
1459
1460    /* Update CR2 : set Nbytes and end mode */
1461    HDMI_CEC_I2C->CR2 = (uint32_t)(1 << 16) | I2C_CR2_AUTOEND; 
1462    
1463    while (!(HDMI_CEC_I2C->ISR & I2C_ISR_RXNE));
1464    
1465    /* Read data from RXDR */
1466    pBuffer[DataNum]= (uint8_t)HDMI_CEC_I2C->RXDR;
1467    
1468    /* Wait until STOPF flag is set */      
1469    while (!(HDMI_CEC_I2C->ISR & I2C_ISR_STOPF));
1470    
1471    /* Clear STOPF flag */
1472    HDMI_CEC_I2C->ICR = I2C_ICR_STOPCF;
1473 
1474    MyPhysicalAddress = ((pBuffer[158] << 8) | pBuffer[159]);
1475#else

1476    MyPhysicalAddress = 0x1000;  
1477#endif

1478   
1479  return errorstatus;
1480}
1481
1482/**
1483  * @brief  Allocate the logical address.
1484  * @param  None
1485  * @retval HDMI_CEC_Error: CEC Error code.
1486  */
1487static HDMI_CEC_Error LogicalAddressAllocation(void)
1488{
1489  HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
1490  uint32_t count = 0;
1491  
1492  /*------------------ Logical address allocation ----------------------------*/
1493  /* Get the device type */
1494  /* Device type = CEC_TV */
1495  if (DeviceType == HDMI_CEC_TV)
1496  {
1497    if (HDMI_CEC_ROOT)
1498    {
1499      MyLogicalAddress = 0x00;
1500    }
1501    else
1502    {
1503      CEC_OwnAddressConfig(0xE); /* Own address = 0xE */
1504
1505      CEC_EndOfMessage();
1506            
1507      CEC_SendData(0xEE);
1508      
1509      /* Start of message */
1510      CEC_StartOfMessage();
1511      
1512      /* Wait till the header message is sent */
1513      while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
1514      {
1515        if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1516        {
1517          break;
1518        }
1519        count++;
1520      }
1521      
1522      if (count >= HDMI_CEC_TIMEOUT_VALUE)
1523      {
1524        errorstatus = HDMI_CEC_TIMEOUT;
1525        return(errorstatus);
1526      }
1527      
1528      errorstatus = HDMI_CEC_GetErrorStatus();
1529      
1530      if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
1531      {
1532        MyLogicalAddress = 0x0E;
1533        errorstatus = HDMI_CEC_OK;
1534      }
1535      else if (errorstatus == HDMI_CEC_OK)
1536      {
1537        MyLogicalAddress = 0x0F;
1538        errorstatus = HDMI_CEC_DEVICE_UNREGISTRED;
1539      }
1540    }
1541    /* Clear CEC ISR register */
1542    CEC_ClearFlag(CEC_FLAG_TXEND);
1543  }
1544  
1545  /* Device type = CEC_RECORDING */
1546  if (DeviceType == HDMI_CEC_RECORDING)
1547  {
1548    CEC_OwnAddressConfig(0x1); /* Own address = 0x1 */
1549    
1550    CEC_EndOfMessage();
1551    
1552    CEC_SendData(0x11);  
1553    
1554    /* Start of message */
1555    CEC_StartOfMessage();
1556    
1557    /* Wait till the header message is sent */
1558    while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
1559    {
1560      if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1561      {
1562        break;
1563      }
1564      count++;
1565    }
1566    
1567    if (count >= HDMI_CEC_TIMEOUT_VALUE)
1568    {
1569      errorstatus = HDMI_CEC_TIMEOUT;
1570      return(errorstatus);
1571    }
1572    
1573    errorstatus = HDMI_CEC_GetErrorStatus();
1574    
1575    if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
1576    {
1577      MyLogicalAddress = 0x01;
1578      errorstatus = HDMI_CEC_OK;
1579    }
1580    else if (errorstatus == HDMI_CEC_OK)
1581    {
1582      /* Clear CEC ISR register */
1583      CEC_ClearFlag(CEC_FLAG_TXEND|CEC_FLAG_TXACKE|CEC_FLAG_TXBR);
1584      
1585      /* Clear the OAR */
1586      CEC_OwnAddressClear();
1587      
1588      CEC_OwnAddressConfig(0x2); /* Own address = 0x2 */
1589      
1590      CEC_EndOfMessage();
1591      
1592      CEC_SendData(0x22);
1593      
1594      /* Start of message */
1595      CEC_StartOfMessage();
1596      
1597      count = 0;
1598      
1599      /* Wait till the header message is sent */
1600      while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
1601      {
1602        if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1603        {
1604          break;
1605        }
1606        count++;
1607      }
1608      
1609      if (count >= HDMI_CEC_TIMEOUT_VALUE)
1610      {
1611        errorstatus = HDMI_CEC_TIMEOUT;
1612        return(errorstatus);
1613      }
1614      errorstatus = HDMI_CEC_GetErrorStatus();
1615      
1616      if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
1617      {
1618        MyLogicalAddress = 0x02;
1619        errorstatus = HDMI_CEC_OK;
1620      }
1621      else if (errorstatus == HDMI_CEC_OK)
1622      {
1623        /* Clear CEC ISR register */
1624        CEC_ClearFlag( CEC_FLAG_TXEND);
1625        
1626        CEC_OwnAddressClear();
1627        
1628        CEC_OwnAddressConfig(0x9); /* Own address = 0x9 */
1629
1630        CEC_EndOfMessage();
1631                
1632        CEC_SendData(0x99);
1633        
1634        /* Start of message */
1635        CEC_StartOfMessage();
1636        
1637        count = 0;
1638        
1639        /* Wait till the header message is sent */
1640        while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
1641        {
1642          if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
1643          {
1644            break;
1645          }
1646          count++;
1647        }
1648        
1649        if (count >= HDMI_CEC_TIMEOUT_VALUE)
1650        {
1651          errorstatus = HDMI_CEC_TIMEOUT;
1652          return(errorstatus);
1653        }
1654        errorstatus = HDMI_CEC_GetErrorStatus();
1655        
1656        if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
1657        {
1658          MyLogicalAddress = 0x09;
1659          errorstatus = HDMI_CEC_OK;
1660        }
1661        else if (errorstatus == HDMI_CEC_OK)
1662        {
1663          MyLogicalAddress = 0x0F;
1664          errorstatus = HDMI_CEC_DEVICE_UNREGISTRED;
1665        }
1666      }
1667    }
1668    /* Clear CEC ISR register */
1669    CEC_ClearFlag(CEC_FLAG_TXEND|CEC_FLAG_TXACKE|CEC_FLAG_TXBR);
1670  }
1671  
1672  /* Device type = CEC_TUNER */
1673  if (DeviceTyp

Large files files are truncated, but you can click here to view the full file