stm320518_eval_cec.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  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. /* Includes ------------------------------------------------------------------*/
  34. #include "stm320518_eval_cec.h"
  35. /** @addtogroup Utilities
  36. * @{
  37. */
  38. /** @addtogroup STM32_EVAL
  39. * @{
  40. */
  41. /** @addtogroup STM320518_EVAL
  42. * @{
  43. */
  44. /** @defgroup STM320518_EVAL_CEC
  45. * @brief This file includes the CEC Stack driver for HDMI-CEC Module
  46. * of STM320518-EVAL board.
  47. * @{
  48. */
  49. /** @defgroup STM320518_EVAL_CEC_Private_Types
  50. * @{
  51. */
  52. /**
  53. * @}
  54. */
  55. /** @defgroup STM320518_EVAL_CEC_Private_Defines
  56. * @{
  57. */
  58. /* Uncomment the define to use the LSE as a source clock */
  59. /* #define LSE_SOURCE_CLK */
  60. /**
  61. * @}
  62. */
  63. /** @defgroup STM320518_EVAL_CEC_Private_Macros
  64. * @{
  65. */
  66. /**
  67. * @}
  68. */
  69. /** @defgroup STM320518_EVAL_CEC_Private_Variables
  70. * @{
  71. */
  72. __IO uint32_t ReceivedFrame = 0;
  73. __IO uint32_t SendFrame = 0;
  74. uint32_t BufferCount = 0, TxCounter = 0, RxCounter = 0;
  75. __IO uint8_t BufferPointer[15];
  76. __IO uint32_t ReceiveStatus = 0;
  77. __IO uint32_t SendStatus = 0;
  78. __IO uint8_t TransErrorCode = 0;
  79. uint8_t MyLogicalAddress = 0;
  80. uint16_t MyPhysicalAddress = 0;
  81. __IO uint8_t DeviceType = 0;
  82. #ifdef HDMI_CEC_USE_DDC
  83. uint8_t pBuffer[256];
  84. __IO uint16_t NumByteToRead = 255;
  85. #endif
  86. __IO uint8_t CECDevicesNumber = 0;
  87. HDMI_CEC_Message HDMI_CEC_TX_MessageStructPrivate;
  88. HDMI_CEC_Message HDMI_CEC_RX_MessageStructPrivate;
  89. HDMI_CEC_Message HDMI_CEC_TX_MessageStructure;
  90. __IO uint8_t FeatureOpcode = 0;
  91. __IO uint8_t AbortReason = 0;
  92. __IO uint8_t DeviceCount = 0;
  93. HDMI_CEC_Map HDMI_CEC_MapStruct;
  94. HDMI_CEC_Map HDMI_CEC_DeviceMap[14];
  95. /* CEC follower addresses */
  96. uint8_t* HDMI_CEC_Follower_String[13][2] =
  97. {
  98. {(uint8_t *)" TV ", (uint8_t *)"0"},
  99. {(uint8_t *)" Recording Device 1 ", (uint8_t *)"0"},
  100. {(uint8_t *)" Recording Device 2 ", (uint8_t *)"0"},
  101. {(uint8_t *)" Tuner 1 ", (uint8_t *)"0"},
  102. {(uint8_t *)" Playback Device 1 ", (uint8_t *)"0"},
  103. {(uint8_t *)" Audio System ", (uint8_t *)"0"},
  104. {(uint8_t *)" Tuner 2 ", (uint8_t *)"0"},
  105. {(uint8_t *)" Tuner 3 ", (uint8_t *)"0"},
  106. {(uint8_t *)" Playback Device 2 ", (uint8_t *)"0"},
  107. {(uint8_t *)" Recording Device 3 ", (uint8_t *)"0"},
  108. {(uint8_t *)" Tuner 4 ", (uint8_t *)"0"},
  109. {(uint8_t *)" Playback Device 3 ", (uint8_t *)"0"},
  110. {(uint8_t *)" Broadcast ", (uint8_t *)"1"}
  111. };
  112. /* EDID & CEA861 Extension */
  113. const uint8_t EDID_Structure[256] =
  114. {
  115. /*-------------------------- Block0: 128Bytes ------------------------------*/
  116. /* Header */
  117. 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
  118. /* Vendor & Product Identification */
  119. 0x4C, 0x2D, 0x57, 0x03, 0x00, 0x00, 0x00, 0x00, 0x22, 0x11,
  120. /* EDID Structure Version & Revision */
  121. 0x01, 0x03,
  122. /* Basic Display Parameters & Features */
  123. 0x80, 0x10, 0x09, 0x78, 0x0A,
  124. /* Color Characteristics */
  125. 0x1D, 0xED, 0xA7, 0x53, 0x34, 0xAC, 0x25, 0x12, 0x47, 0x4A,
  126. /* Established Timings */
  127. 0x20, 0x00, 0x00,
  128. /* Standard Timings */
  129. 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  130. 0x01, 0x01, 0x01,
  131. /* Byte Data Blocks*/
  132. 0x02, 0x3A, 0x80, 0x18, 0x71, 0x38, 0x2D, 0x40, 0x58, 0x2C, 0x45, 0x00, 0xA0,
  133. 0x5A, 0x00, 0x00, 0x00, 0x1E, 0x01, 0x1D, 0x00, 0x72, 0x51, 0xD0, 0x1E, 0x20,
  134. 0x6E, 0x28, 0x55, 0x00, 0xA0, 0x5A, 0x00, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00,
  135. 0xFD, 0x00, 0x17, 0x3D, 0x1A, 0x44, 0x17, 0x00, 0x0A, 0x20, 0x20, 0x20, 0x20,
  136. 0x20, 0x20, 0x00, 0x00, 0x00, 0xFC, 0x00, 0x53, 0x41, 0x4D, 0x53, 0x55, 0x4E,
  137. 0x47, 0x0A, 0x20, 0x20, 0x20, 0x20, 0x20,
  138. /* Extension Block Count */
  139. 0x01,
  140. /* Checksum C */
  141. 0xF3,
  142. /* ------------------------ Block1: 128Bytes------------------------------- */
  143. /* Refer to CEA 861 Standard for definitions */
  144. /* CEA 861 EXTENSION Block – Version 3 */
  145. 0x02, 0x03,
  146. /* Byte number within this block where the 18-byte DTDs begin */
  147. 0x21,
  148. /* Number of DTDs present */
  149. 0x00,
  150. /* Start of Data Block Collection */
  151. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  152. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  153. /* IEEE Registration Identifier */
  154. 0x03, 0x0C, 0x00,
  155. /* Vendor Specific Data: Physical Address */
  156. 0x10, 0x00,
  157. /* Byte Data Blocks */
  158. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  159. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  160. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  161. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  162. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  164. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  165. 0x00, 0x00, 0x00, 0x00, 0x00
  166. };
  167. /**
  168. * @}
  169. */
  170. /** @defgroup STM320518_EVAL_CEC_Private_Function_Prototypes
  171. * @{
  172. */
  173. static HDMI_CEC_Error SinkPhysicalAddressDiscovery(void);
  174. static HDMI_CEC_Error SourcePhysicalAddressDiscovery(void);
  175. static HDMI_CEC_Error LogicalAddressAllocation(void);
  176. /**
  177. * @}
  178. */
  179. /** @defgroup STM320518_EVAL_CEC_Private_Functions
  180. * @{
  181. */
  182. /**
  183. * @brief Initializes the HDMI CEC.
  184. * @param None
  185. * @retval HDMI_CEC_Error: CEC Error code
  186. */
  187. HDMI_CEC_Error HDMI_CEC_Init(void)
  188. {
  189. GPIO_InitTypeDef GPIO_InitStructure;
  190. CEC_InitTypeDef CEC_InitStructure;
  191. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  192. uint8_t sendcount = 0;
  193. uint32_t idx = 0;
  194. #ifdef LSE_SOURCE_CLK
  195. /* LSE */
  196. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
  197. PWR_BackupAccessCmd(ENABLE);
  198. RCC_LSEConfig(RCC_LSE_ON);
  199. while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
  200. {}
  201. RCC_CECCLKConfig(RCC_CECCLK_LSE);
  202. #endif
  203. for(idx=0; idx<12; idx++)
  204. {
  205. HDMI_CEC_Follower_String[idx][1] = (uint8_t *)"0";
  206. }
  207. /* Enable CEC and PWR clocks */
  208. RCC_APB1PeriphClockCmd(RCC_APB1Periph_CEC | RCC_APB1Periph_PWR, ENABLE);
  209. /* Enable CEC_LINE_GPIO clocks */
  210. RCC_AHBPeriphClockCmd(HDMI_CEC_LINE_GPIO_CLK, ENABLE);
  211. /* Configure CEC_LINE_GPIO as Output open drain */
  212. GPIO_InitStructure.GPIO_Pin = HDMI_CEC_LINE_PIN;
  213. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  214. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  215. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  216. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
  217. GPIO_Init(HDMI_CEC_LINE_GPIO_PORT, &GPIO_InitStructure);
  218. GPIO_PinAFConfig(HDMI_CEC_LINE_GPIO_PORT, GPIO_PinSource10, GPIO_AF_0);
  219. /* CEC DeInit */
  220. CEC_DeInit();
  221. /* Configure CEC */
  222. CEC_InitStructure.CEC_SignalFreeTime = CEC_SignalFreeTime_Standard;
  223. CEC_InitStructure.CEC_RxTolerance = CEC_RxTolerance_Standard;
  224. CEC_InitStructure.CEC_StopReception = CEC_StopReception_Off;
  225. CEC_InitStructure.CEC_BitRisingError = CEC_BitRisingError_Off;
  226. CEC_InitStructure.CEC_LongBitPeriodError = CEC_LongBitPeriodError_Off;
  227. CEC_InitStructure.CEC_BRDNoGen = CEC_BRDNoGen_Off;
  228. CEC_InitStructure.CEC_SFTOption = CEC_SFTOption_Off;
  229. CEC_Init(&CEC_InitStructure);
  230. /* Disable Listen Mode */
  231. CEC_ListenModeCmd(DISABLE);
  232. /* Enable CEC */
  233. CEC_Cmd(ENABLE);
  234. if (HDMI_CEC_ROOT == 0x1)
  235. {
  236. /* Logical Address Allocation */
  237. sendcount = 0;
  238. errorstatus = LogicalAddressAllocation();
  239. while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
  240. {
  241. sendcount++;
  242. errorstatus = LogicalAddressAllocation();
  243. }
  244. if (errorstatus != HDMI_CEC_OK)
  245. {
  246. /* Device Unregistered */
  247. return(errorstatus);
  248. }
  249. HDMI_CEC_CheckConnectedDevices();
  250. /* Set the CEC initiator address */
  251. CEC_OwnAddressConfig(MyLogicalAddress);
  252. /* Sink Physical Address discovery */
  253. errorstatus = SinkPhysicalAddressDiscovery();
  254. if (errorstatus != HDMI_CEC_OK)
  255. {
  256. /* Device not connected (Physical Address lost) */
  257. return(errorstatus);
  258. }
  259. }
  260. else
  261. {
  262. /* Source Physical Address discovery */
  263. errorstatus = SourcePhysicalAddressDiscovery();
  264. if (errorstatus != HDMI_CEC_OK)
  265. {
  266. /* Device not connected (Physical Address lost) */
  267. return(errorstatus);
  268. }
  269. /* Logical Address Allocation */
  270. sendcount = 0;
  271. errorstatus = LogicalAddressAllocation();
  272. while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
  273. {
  274. sendcount++;
  275. errorstatus = LogicalAddressAllocation();
  276. }
  277. if (errorstatus != HDMI_CEC_OK)
  278. {
  279. /* Device Unregistered */
  280. return(errorstatus);
  281. }
  282. HDMI_CEC_CheckConnectedDevices();
  283. /* Set the CEC initiator address */
  284. CEC_OwnAddressConfig(MyLogicalAddress);
  285. }
  286. /* Activate CEC interrupts associated to the set of TX flags */
  287. CEC_ITConfig(CEC_IT_TXEND|CEC_IT_TXBR,ENABLE);
  288. /* Activate CEC interrupts associated to the set of RX flags */
  289. CEC_ITConfig(CEC_IT_RXEND|CEC_IT_RXBR,ENABLE);
  290. /* Activate CEC interrupts associated to the set of TX error */
  291. CEC_ITConfig(CEC_IT_TXACKE|CEC_IT_TXERR|CEC_IT_TXUDR|CEC_IT_ARBLST,ENABLE);
  292. /* Activate CEC interrupts associated to the set of RX error */
  293. CEC_ITConfig(CEC_IT_RXACKE|CEC_IT_LBPE|CEC_IT_SBPE|CEC_IT_BRE|CEC_IT_RXOVR,ENABLE);
  294. /* Report physical address */
  295. errorstatus = HDMI_CEC_ReportPhysicalAddress();
  296. sendcount = 0;
  297. RxCounter = 0;
  298. while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
  299. {
  300. sendcount++;
  301. errorstatus = HDMI_CEC_ReportPhysicalAddress();
  302. }
  303. if (errorstatus != HDMI_CEC_OK)
  304. {
  305. /* Device Unregistred */
  306. return(errorstatus);
  307. }
  308. return errorstatus;
  309. }
  310. /**
  311. * @brief Transmit message by taking data from typedef struct CEC_Meassage
  312. * @param CEC_TX_MessageStructure: pointer to an CEC_Message structure that contains
  313. * the message to be sent.
  314. * @retval HDMI_CEC_Error: CEC Error code
  315. */
  316. HDMI_CEC_Error HDMI_CEC_TransmitMessage(HDMI_CEC_Message *HDMI_CEC_TX_MessageStructure)
  317. {
  318. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  319. __IO uint32_t count = 0, j = 0;
  320. SendFrame = 0;
  321. SendStatus = 0;
  322. TxCounter = 0;
  323. BufferCount = 0;
  324. HDMI_CEC_TX_MessageStructPrivate = *HDMI_CEC_TX_MessageStructure;
  325. /* Initialize BufferPointer */
  326. for (j = 0; j < 15; j++)
  327. {
  328. BufferPointer[j] = 0;
  329. }
  330. BufferPointer[0] = HDMI_CEC_TX_MessageStructPrivate.Opcode;
  331. for (BufferCount = 1; BufferCount < HDMI_CEC_TX_MessageStructPrivate.TxMessageLength + 1; BufferCount++)
  332. {
  333. BufferPointer[BufferCount] = HDMI_CEC_TX_MessageStructPrivate.Operande[BufferCount - 1];
  334. }
  335. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  336. /* Write single Data in the TX Buffer to Transmit through the CEC peripheral */
  337. CEC_SendData(HDMI_CEC_TX_MessageStructPrivate.Header);
  338. /* Initiate Message Transmission */
  339. CEC_StartOfMessage();
  340. while ((SendFrame == 0) && (count < HDMI_CEC_TIMEOUT_VALUE))
  341. {
  342. count++;
  343. }
  344. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  345. {
  346. errorstatus = HDMI_CEC_TIMEOUT;
  347. return(errorstatus);
  348. }
  349. if (SendStatus == 0)
  350. {
  351. errorstatus = (HDMI_CEC_Error) TransErrorCode;
  352. }
  353. return errorstatus;
  354. }
  355. /**
  356. * @brief Get the ISR register status.
  357. * @param None
  358. * @retval HDMI_CEC_Error: CEC Error code
  359. */
  360. HDMI_CEC_Error HDMI_CEC_GetErrorStatus (void)
  361. {
  362. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  363. /* RX Overrun error case*/
  364. if (CEC_GetFlagStatus(CEC_FLAG_RXOVR) != RESET)
  365. {
  366. errorstatus = HDMI_CEC_RX_OVERRUN;
  367. CEC_ClearFlag(CEC_FLAG_RXOVR);
  368. }
  369. /* Bit Rising error case */
  370. if (CEC_GetFlagStatus(CEC_FLAG_BRE) != RESET)
  371. {
  372. errorstatus = HDMI_CEC_RX_BIT_RISING;
  373. CEC_ClearFlag(CEC_FLAG_BRE);
  374. }
  375. /* Short Bit Period error case */
  376. if (CEC_GetFlagStatus(CEC_FLAG_SBPE) != RESET)
  377. {
  378. errorstatus = HDMI_CEC_RX_SHORT_BIT_PERIOD;
  379. CEC_ClearFlag(CEC_FLAG_SBPE);
  380. }
  381. /* Long Bit Period error case */
  382. if (CEC_GetFlagStatus(CEC_FLAG_LBPE) != RESET)
  383. {
  384. errorstatus = HDMI_CEC_RX_LONG_BIT_PERIOD;
  385. CEC_ClearFlag(CEC_FLAG_LBPE);
  386. }
  387. /* RX Acknowledge error case */
  388. if (CEC_GetFlagStatus(CEC_FLAG_RXACKE) != RESET)
  389. {
  390. errorstatus = HDMI_CEC_RX_ACKNOWLEDGE;
  391. CEC_ClearFlag(CEC_FLAG_RXACKE);
  392. }
  393. /* Arbitration Lost error case */
  394. if (CEC_GetFlagStatus(CEC_FLAG_ARBLST) != RESET)
  395. {
  396. errorstatus = HDMI_CEC_ARBITRATION_LOST;
  397. CEC_ClearFlag(CEC_FLAG_ARBLST);
  398. }
  399. /* TX underrun error case */
  400. if (CEC_GetFlagStatus(CEC_FLAG_TXUDR) != RESET)
  401. {
  402. errorstatus = HDMI_CEC_TX_UNDERRUN;
  403. CEC_ClearFlag(CEC_FLAG_TXUDR);
  404. }
  405. /* TX error case */
  406. if (CEC_GetFlagStatus(CEC_FLAG_TXERR) != RESET)
  407. {
  408. errorstatus = HDMI_CEC_TX_ERROR;
  409. CEC_ClearFlag(CEC_FLAG_TXERR);
  410. }
  411. /* TX Acknowledge error case */
  412. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  413. {
  414. errorstatus = HDMI_CEC_TX_ACKNOWLEDGE;
  415. CEC_ClearFlag(CEC_FLAG_TXACKE);
  416. }
  417. return errorstatus;
  418. }
  419. /**
  420. * @brief Allows to process all the interrupts that are high.
  421. * @param None
  422. * @retval None
  423. */
  424. void HDMI_CEC_ProcessIRQSrc(void)
  425. {
  426. /********************** Reception *******************************************/
  427. /* Check if a reception error occured */
  428. if (CEC->ISR & (CEC_IT_RXACKE | CEC_IT_LBPE | CEC_IT_SBPE | CEC_IT_BRE | CEC_IT_RXOVR))
  429. {
  430. /* Set receive status bit (Error) */
  431. ReceiveStatus = 0;
  432. ReceivedFrame = 1;
  433. CEC->ISR = (CEC_IT_RXACKE | CEC_IT_LBPE | CEC_IT_SBPE | CEC_IT_BRE | CEC_IT_RXOVR);
  434. RxCounter = 0;
  435. }
  436. if (CEC_GetITStatus(CEC_IT_RXBR))
  437. {
  438. /* Check if the byte received is a Header */
  439. if (RxCounter == 0)
  440. {
  441. HDMI_CEC_RX_MessageStructPrivate.Header = CEC_ReceiveData();
  442. RxCounter ++;
  443. }
  444. else if(RxCounter == 1) /* Receive opcode in the reception buffer */
  445. {
  446. HDMI_CEC_RX_MessageStructPrivate.Opcode = CEC_ReceiveData();
  447. RxCounter++;
  448. }
  449. else /* Receive each byte except header and opcode in the reception buffer */
  450. {
  451. HDMI_CEC_RX_MessageStructPrivate.Operande[RxCounter-2] = CEC_ReceiveData();
  452. RxCounter++;
  453. }
  454. CEC_ClearITPendingBit(CEC_IT_RXBR);
  455. }
  456. /* Check if the byte received is the last one of the message */
  457. if (CEC_GetITStatus(CEC_IT_RXEND))
  458. {
  459. if (RxCounter == 1)
  460. {
  461. HDMI_CEC_RX_MessageStructPrivate.RxMessageLength = RxCounter-1;
  462. }
  463. else
  464. {
  465. HDMI_CEC_RX_MessageStructPrivate.RxMessageLength = RxCounter-2;
  466. ReceivedFrame = 1;
  467. }
  468. /* Set the receive status */
  469. ReceiveStatus = SUCCESS;
  470. RxCounter = 0;
  471. /* Clear all reception flags */
  472. CEC_ClearITPendingBit(CEC_IT_RXEND);
  473. }
  474. /********************** Transmission ****************************************/
  475. /* Check if a transmission error occurred */
  476. if (CEC->ISR & (CEC_IT_TXACKE | CEC_IT_TXERR | CEC_IT_TXUDR | CEC_IT_ARBLST))
  477. {
  478. TransErrorCode = HDMI_CEC_GetErrorStatus();
  479. CEC->ISR = (CEC_IT_TXACKE | CEC_IT_TXERR | CEC_IT_TXUDR | CEC_IT_ARBLST);
  480. SendFrame = 1;
  481. SendStatus = 0;
  482. TxCounter = 0;
  483. }
  484. /* Check if end of message bit is set in the data to be transmitted */
  485. if (CEC_GetITStatus(CEC_IT_TXEND))
  486. {
  487. CEC_ClearITPendingBit(CEC_IT_TXEND | CEC_IT_TXBR);
  488. SendFrame = 1;
  489. SendStatus = SUCCESS;
  490. }
  491. /* Check if data byte has been sent */
  492. else if (CEC_GetITStatus(CEC_IT_TXBR))
  493. {
  494. /* Set EOM bit if the byte to be transmitted is the last one of the Transmit Buffer */
  495. if (TxCounter == (HDMI_CEC_TX_MessageStructPrivate.TxMessageLength))
  496. {
  497. CEC_EndOfMessage();
  498. CEC_SendData(BufferPointer[TxCounter++]);
  499. }
  500. else
  501. {
  502. /* Put the byte in the TX Buffer */
  503. CEC_SendData(BufferPointer[TxCounter++]);
  504. }
  505. CEC_ClearITPendingBit(CEC_IT_TXBR);
  506. }
  507. }
  508. /**
  509. * @brief Report physical address to all other devices thus allowing any
  510. device to create a map of the network.
  511. * @param None
  512. * @retval HDMI_CEC_Error: CEC Error code.
  513. */
  514. HDMI_CEC_Error HDMI_CEC_ReportPhysicalAddress(void)
  515. {
  516. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  517. HDMI_CEC_Message HDMI_CEC_TX_Message;
  518. HDMI_CEC_TX_Message.Header = ((MyLogicalAddress << 4) | 0xF);
  519. HDMI_CEC_TX_Message.Opcode = HDMI_CEC_OPCODE_REPORT_PHYSICAL_ADDRESS;
  520. HDMI_CEC_TX_Message.Operande[0] = MyPhysicalAddress >> 8;
  521. HDMI_CEC_TX_Message.Operande[1] = MyPhysicalAddress & 0xFF;
  522. HDMI_CEC_TX_Message.Operande[2] = DeviceType;
  523. HDMI_CEC_TX_Message.TxMessageLength = 0x03;
  524. errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_Message);
  525. return errorstatus;
  526. }
  527. /**
  528. * @brief Handle CEC command receive callback.
  529. * @note When receiving the STANDBY Opcode command, the system is entered in
  530. * Stop mode and when wakeup, the PLL is configured as system clock and
  531. * the HSI is selected as PLL source.
  532. * @param None
  533. * @retval None
  534. */
  535. void HDMI_CEC_CommandCallBack(void)
  536. {
  537. uint8_t i = 0, sendcount = 0;
  538. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  539. ErrorStatus HSEStartUpStatus;
  540. switch (HDMI_CEC_RX_MessageStructPrivate.Opcode)
  541. {
  542. case HDMI_CEC_OPCODE_REPORT_PHYSICAL_ADDRESS:
  543. HDMI_CEC_MapStruct.PhysicalAddress_A = HDMI_CEC_RX_MessageStructPrivate.Operande[1] >> 4;
  544. HDMI_CEC_MapStruct.PhysicalAddress_B = HDMI_CEC_RX_MessageStructPrivate.Operande[1] & 0x0F;
  545. HDMI_CEC_MapStruct.PhysicalAddress_C = HDMI_CEC_RX_MessageStructPrivate.Operande[0] >> 4;
  546. HDMI_CEC_MapStruct.PhysicalAddress_D = HDMI_CEC_RX_MessageStructPrivate.Operande[0] & 0x0F;
  547. HDMI_CEC_MapStruct.LogicalAddress = (HDMI_CEC_RX_MessageStructPrivate.Header >> 0x4) & 0x0F;
  548. HDMI_CEC_MapStruct.DeviceType = HDMI_CEC_RX_MessageStructPrivate.Operande[2];
  549. HDMI_CEC_DeviceMap[DeviceCount] = HDMI_CEC_MapStruct;
  550. HDMI_CEC_Follower_String[(HDMI_CEC_DeviceMap[DeviceCount].LogicalAddress)][1] = (uint8_t *)"1";
  551. DeviceCount++;
  552. break;
  553. case HDMI_CEC_OPCODE_STANDBY:
  554. /* Request to enter Stop mode */
  555. PWR_EnterSTOPMode(PWR_Regulator_ON, PWR_STOPEntry_WFI);
  556. /* Enable HSE */
  557. RCC_HSEConfig(RCC_HSE_ON);
  558. /* Wait till HSE is ready */
  559. HSEStartUpStatus = RCC_WaitForHSEStartUp();
  560. if (HSEStartUpStatus == SUCCESS)
  561. {
  562. /* Enable PLL */
  563. RCC_PLLCmd(ENABLE);
  564. /* Wait till PLL is ready */
  565. while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  566. {}
  567. /* Select PLL as system clock source */
  568. RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  569. /* Wait till PLL is used as system clock source */
  570. while (RCC_GetSYSCLKSource() != 0x08)
  571. {}
  572. }
  573. break;
  574. case HDMI_CEC_OPCODE_GET_CEC_VERSION:
  575. /* Send the Used CEC version */
  576. HDMI_CEC_TX_MessageStructPrivate.Header = ((MyLogicalAddress << 4) | HDMI_CEC_RX_MessageStructPrivate.Header >> 4);
  577. HDMI_CEC_TX_MessageStructPrivate.Opcode = HDMI_CEC_OPCODE_CEC_VERSION;
  578. HDMI_CEC_TX_MessageStructPrivate.Operande[0] = HDMI_CEC_VERSION; /* CEC Version */
  579. HDMI_CEC_TX_MessageStructPrivate.TxMessageLength = 0x01;
  580. errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
  581. /* Retransmit message until 5 time */
  582. while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
  583. {
  584. sendcount++;
  585. errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
  586. }
  587. break;
  588. case HDMI_CEC_OPCODE_GIVE_PHYSICAL_ADDRESS:
  589. /* Send the Physical address */
  590. errorstatus = HDMI_CEC_ReportPhysicalAddress();
  591. sendcount = 0;
  592. /* Retransmit message until 5 time */
  593. while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
  594. {
  595. sendcount++;
  596. errorstatus = HDMI_CEC_ReportPhysicalAddress();
  597. }
  598. break;
  599. case HDMI_CEC_OPCODE_FEATURE_ABORT:
  600. /* The device doesn't support the requested message type, or that it cannot
  601. execute it at the present time. */
  602. FeatureOpcode = HDMI_CEC_RX_MessageStructPrivate.Operande[0];
  603. AbortReason = HDMI_CEC_RX_MessageStructPrivate.Operande[1];
  604. break;
  605. case HDMI_CEC_OPCODE_GIVE_OSD_NAME:
  606. /* Send the OSD name = STM320518 CEC */
  607. HDMI_CEC_TX_MessageStructPrivate.Header = ((MyLogicalAddress << 4) | HDMI_CEC_RX_MessageStructPrivate.Header >> 4);
  608. HDMI_CEC_TX_MessageStructPrivate.Opcode = HDMI_CEC_OPCODE_SET_OSD_NAME;
  609. /* STM320518 */
  610. HDMI_CEC_TX_MessageStructPrivate.Operande[0] = 0x53;
  611. HDMI_CEC_TX_MessageStructPrivate.Operande[1] = 0x54;
  612. HDMI_CEC_TX_MessageStructPrivate.Operande[2] = 0x4D;
  613. HDMI_CEC_TX_MessageStructPrivate.Operande[3] = 0x33;
  614. HDMI_CEC_TX_MessageStructPrivate.Operande[4] = 0x32;
  615. HDMI_CEC_TX_MessageStructPrivate.Operande[5] = 0x30;
  616. HDMI_CEC_TX_MessageStructPrivate.Operande[6] = 0x35;
  617. HDMI_CEC_TX_MessageStructPrivate.Operande[7] = 0x31;
  618. HDMI_CEC_TX_MessageStructPrivate.Operande[8] = 0x38;
  619. HDMI_CEC_TX_MessageStructPrivate.Operande[9] = 0x20;
  620. /* CEC */
  621. HDMI_CEC_TX_MessageStructPrivate.Operande[10] = 0x43;
  622. HDMI_CEC_TX_MessageStructPrivate.Operande[11] = 0x45;
  623. HDMI_CEC_TX_MessageStructPrivate.Operande[12] = 0x43;
  624. HDMI_CEC_TX_MessageStructPrivate.TxMessageLength = 13;
  625. errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
  626. sendcount = 0;
  627. /* Retransmit message until 5 time */
  628. while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
  629. {
  630. sendcount++;
  631. errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
  632. }
  633. break;
  634. case HDMI_CEC_OPCODE_ROUTING_CHANGE:
  635. for (i = 0;i < 0x14;i++)
  636. {
  637. if ((HDMI_CEC_DeviceMap[i].PhysicalAddress_A == HDMI_CEC_RX_MessageStructPrivate.Operande[1] >> 4) &&
  638. (HDMI_CEC_DeviceMap[i].PhysicalAddress_B == HDMI_CEC_RX_MessageStructPrivate.Operande[1]&0x0F) &&
  639. (HDMI_CEC_DeviceMap[i].PhysicalAddress_C == HDMI_CEC_RX_MessageStructPrivate.Operande[0] >> 4) &&
  640. (HDMI_CEC_DeviceMap[i].PhysicalAddress_D == (HDMI_CEC_RX_MessageStructPrivate.Operande[0]&0x0F)))
  641. {
  642. HDMI_CEC_MapStruct.LogicalAddress = (HDMI_CEC_RX_MessageStructPrivate.Header >> 0x4) & 0x0F;
  643. HDMI_CEC_MapStruct.DeviceType = HDMI_CEC_RX_MessageStructPrivate.Operande[2];
  644. HDMI_CEC_DeviceMap[i] = HDMI_CEC_MapStruct;
  645. }
  646. }
  647. break;
  648. default:
  649. /* Send Abort feature */
  650. HDMI_CEC_TX_MessageStructPrivate.Header = ((MyLogicalAddress << 4) | HDMI_CEC_RX_MessageStructPrivate.Header >> 4);
  651. HDMI_CEC_TX_MessageStructPrivate.Opcode = HDMI_CEC_OPCODE_FEATURE_ABORT;
  652. HDMI_CEC_TX_MessageStructPrivate.Operande[0] = 0x02; /* Defines command to be performed */
  653. HDMI_CEC_TX_MessageStructPrivate.Operande[1] = HDMI_CEC_REFUSED; /* Reason for abort feature */
  654. HDMI_CEC_TX_MessageStructPrivate.TxMessageLength = 0x02;
  655. errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
  656. sendcount = 0;
  657. /* Retransmit message until 5 time */
  658. while ((errorstatus != HDMI_CEC_OK) && sendcount < 0x5)
  659. {
  660. sendcount++;
  661. errorstatus = HDMI_CEC_TransmitMessage(&HDMI_CEC_TX_MessageStructPrivate);
  662. }
  663. break;
  664. }
  665. }
  666. /**
  667. * @brief Check the connected CEC devices.
  668. * @param None
  669. * @retval HDMI_CEC_Error
  670. */
  671. HDMI_CEC_Error HDMI_CEC_CheckConnectedDevices(void)
  672. {
  673. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  674. uint32_t count = 0, i = 1;
  675. /*------------------------------- TV device --------------------------------*/
  676. CEC_EndOfMessage();
  677. CEC_SendData((MyLogicalAddress << 4) | 0x0);
  678. /* Start of message */
  679. CEC_StartOfMessage();
  680. /* Wait till the header message is sent */
  681. while (count < HDMI_CEC_TIMEOUT_VALUE)
  682. {
  683. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  684. {
  685. errorstatus = HDMI_CEC_GetErrorStatus();
  686. CEC_ClearFlag( CEC_FLAG_TXACKE);
  687. break;
  688. }
  689. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  690. {
  691. errorstatus = HDMI_CEC_OK;
  692. break;
  693. }
  694. count++;
  695. }
  696. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  697. {
  698. errorstatus = HDMI_CEC_TIMEOUT;
  699. return(errorstatus);
  700. }
  701. if (errorstatus == HDMI_CEC_OK)
  702. {
  703. HDMI_CEC_Follower_String[0][1] = (uint8_t *)"1";
  704. i++;
  705. errorstatus = HDMI_CEC_OK;
  706. }
  707. /* Clear CEC ISR register */
  708. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  709. /*---------------------------- Recording device 1 --------------------------*/
  710. CEC_EndOfMessage();
  711. CEC_SendData((MyLogicalAddress << 4)|0x1);
  712. /* Start of message */
  713. CEC_StartOfMessage();
  714. /* Wait till the header message is sent */
  715. while (count < HDMI_CEC_TIMEOUT_VALUE)
  716. {
  717. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  718. {
  719. errorstatus = HDMI_CEC_GetErrorStatus();
  720. CEC_ClearFlag( CEC_FLAG_TXACKE);
  721. break;
  722. }
  723. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  724. {
  725. errorstatus = HDMI_CEC_OK;
  726. break;
  727. }
  728. count++;
  729. }
  730. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  731. {
  732. errorstatus = HDMI_CEC_TIMEOUT;
  733. return(errorstatus);
  734. }
  735. if (errorstatus == HDMI_CEC_OK)
  736. {
  737. HDMI_CEC_Follower_String[1][1] = (uint8_t *)"1";
  738. i++;
  739. errorstatus = HDMI_CEC_OK;
  740. }
  741. /* Clear CEC ISR register */
  742. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  743. /*---------------------------- Recording device 2 --------------------------*/
  744. CEC_EndOfMessage();
  745. CEC_SendData((MyLogicalAddress << 4) | 0x2);
  746. /* Start of message */
  747. CEC_StartOfMessage();
  748. /* Wait till the header message is sent */
  749. while (count < HDMI_CEC_TIMEOUT_VALUE)
  750. {
  751. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  752. {
  753. errorstatus = HDMI_CEC_GetErrorStatus();
  754. CEC_ClearFlag( CEC_FLAG_TXACKE);
  755. break;
  756. }
  757. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  758. {
  759. errorstatus = HDMI_CEC_OK;
  760. break;
  761. }
  762. count++;
  763. }
  764. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  765. {
  766. errorstatus = HDMI_CEC_TIMEOUT;
  767. return(errorstatus);
  768. }
  769. if (errorstatus == HDMI_CEC_OK)
  770. {
  771. HDMI_CEC_Follower_String[2][1] = (uint8_t *)"1";
  772. i++;
  773. errorstatus = HDMI_CEC_OK;
  774. }
  775. /* Clear CEC ISR register */
  776. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  777. /*------------------------------ Tuner 1 -----------------------------------*/
  778. CEC_EndOfMessage();
  779. CEC_SendData((MyLogicalAddress << 4) | 0x3);
  780. /* Start of message */
  781. CEC_StartOfMessage();
  782. /* Wait till the header message is sent */
  783. while (count < HDMI_CEC_TIMEOUT_VALUE)
  784. {
  785. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  786. {
  787. errorstatus = HDMI_CEC_GetErrorStatus();
  788. CEC_ClearFlag( CEC_FLAG_TXACKE);
  789. break;
  790. }
  791. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  792. {
  793. errorstatus = HDMI_CEC_OK;
  794. break;
  795. }
  796. count++;
  797. }
  798. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  799. {
  800. errorstatus = HDMI_CEC_TIMEOUT;
  801. return(errorstatus);
  802. }
  803. if (errorstatus == HDMI_CEC_OK)
  804. {
  805. HDMI_CEC_Follower_String[3][1] = (uint8_t *)"1";
  806. i++;
  807. errorstatus = HDMI_CEC_OK;
  808. }
  809. /* Clear CEC ISR register */
  810. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  811. /*---------------------------- Playback device 1 ---------------------------*/
  812. CEC_EndOfMessage();
  813. CEC_SendData((MyLogicalAddress << 4) | 0x4);
  814. /* Start of message */
  815. CEC_StartOfMessage();
  816. /* Wait till the header message is sent */
  817. while (count < HDMI_CEC_TIMEOUT_VALUE)
  818. {
  819. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  820. {
  821. errorstatus = HDMI_CEC_GetErrorStatus();
  822. CEC_ClearFlag(CEC_FLAG_TXACKE);
  823. break;
  824. }
  825. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  826. {
  827. errorstatus = HDMI_CEC_OK;
  828. break;
  829. }
  830. count++;
  831. }
  832. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  833. {
  834. errorstatus = HDMI_CEC_TIMEOUT;
  835. return(errorstatus);
  836. }
  837. if (errorstatus == HDMI_CEC_OK)
  838. {
  839. HDMI_CEC_Follower_String[4][1] = (uint8_t *)"1";
  840. i++;
  841. errorstatus = HDMI_CEC_OK;
  842. }
  843. /* Clear CEC ISR register */
  844. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  845. /*-------------------------------- Audio system ----------------------------*/
  846. CEC_EndOfMessage();
  847. CEC_SendData((MyLogicalAddress << 4) | 0x5);
  848. /* Start of message */
  849. CEC_StartOfMessage();
  850. /* Wait till the header message is sent */
  851. while (count < HDMI_CEC_TIMEOUT_VALUE)
  852. {
  853. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  854. {
  855. errorstatus = HDMI_CEC_GetErrorStatus();
  856. CEC_ClearFlag(CEC_FLAG_TXACKE);
  857. break;
  858. }
  859. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  860. {
  861. errorstatus = HDMI_CEC_OK;
  862. break;
  863. }
  864. count++;
  865. }
  866. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  867. {
  868. errorstatus = HDMI_CEC_TIMEOUT;
  869. return(errorstatus);
  870. }
  871. if (errorstatus == HDMI_CEC_OK)
  872. {
  873. HDMI_CEC_Follower_String[5][1] = (uint8_t *)"1";
  874. i++;
  875. errorstatus = HDMI_CEC_OK;
  876. }
  877. /* Clear CEC ISR register */
  878. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  879. /*--------------------------------- Tuner 2 --------------------------------*/
  880. CEC_EndOfMessage();
  881. CEC_SendData((MyLogicalAddress << 4) | 0x6);
  882. /* Start of message */
  883. CEC_StartOfMessage();
  884. /* Wait till the header message is sent */
  885. while (count < HDMI_CEC_TIMEOUT_VALUE)
  886. {
  887. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  888. {
  889. errorstatus = HDMI_CEC_GetErrorStatus();
  890. CEC_ClearFlag(CEC_FLAG_TXACKE);
  891. break;
  892. }
  893. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  894. {
  895. errorstatus = HDMI_CEC_OK;
  896. break;
  897. }
  898. count++;
  899. }
  900. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  901. {
  902. errorstatus = HDMI_CEC_TIMEOUT;
  903. return(errorstatus);
  904. }
  905. if (errorstatus == HDMI_CEC_OK)
  906. {
  907. HDMI_CEC_Follower_String[6][1] = (uint8_t *)"1";
  908. i++;
  909. errorstatus = HDMI_CEC_OK;
  910. }
  911. /* Clear CEC ISR register */
  912. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  913. /*--------------------------------- Tuner 3 --------------------------------*/
  914. CEC_EndOfMessage();
  915. CEC_SendData((MyLogicalAddress << 4) | 0x7);
  916. /* Start of message */
  917. CEC_StartOfMessage();
  918. /* Wait till the header message is sent */
  919. while (count < HDMI_CEC_TIMEOUT_VALUE)
  920. {
  921. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  922. {
  923. errorstatus = HDMI_CEC_GetErrorStatus();
  924. CEC_ClearFlag(CEC_FLAG_TXACKE);
  925. break;
  926. }
  927. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  928. {
  929. errorstatus = HDMI_CEC_OK;
  930. break;
  931. }
  932. count++;
  933. }
  934. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  935. {
  936. errorstatus = HDMI_CEC_TIMEOUT;
  937. return(errorstatus);
  938. }
  939. if (errorstatus == HDMI_CEC_OK)
  940. {
  941. HDMI_CEC_Follower_String[7][1] = (uint8_t *)"1";
  942. i++;
  943. errorstatus = HDMI_CEC_OK;
  944. }
  945. /* Clear CEC ISR register */
  946. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  947. /*---------------------------- Playback device 2 ---------------------------*/
  948. CEC_EndOfMessage();
  949. CEC_SendData((MyLogicalAddress << 4) | 0x8);
  950. /* Start of message */
  951. CEC_StartOfMessage();
  952. /* Wait till the header message is sent */
  953. while(count < HDMI_CEC_TIMEOUT_VALUE)
  954. {
  955. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  956. {
  957. errorstatus = HDMI_CEC_GetErrorStatus();
  958. CEC_ClearFlag(CEC_FLAG_TXACKE);
  959. break;
  960. }
  961. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  962. {
  963. errorstatus = HDMI_CEC_OK;
  964. break;
  965. }
  966. count++;
  967. }
  968. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  969. {
  970. errorstatus = HDMI_CEC_TIMEOUT;
  971. return(errorstatus);
  972. }
  973. if (errorstatus == HDMI_CEC_OK)
  974. {
  975. HDMI_CEC_Follower_String[8][1] = (uint8_t *)"1";
  976. i++;
  977. errorstatus = HDMI_CEC_OK;
  978. }
  979. /* Clear CEC ISR register */
  980. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  981. /*---------------------------- Recording device 3 --------------------------*/
  982. CEC_EndOfMessage();
  983. CEC_SendData((MyLogicalAddress << 4) | 0x9);
  984. /* Start of message */
  985. CEC_StartOfMessage();
  986. /* Wait till the header message is sent */
  987. while(count < HDMI_CEC_TIMEOUT_VALUE)
  988. {
  989. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  990. {
  991. errorstatus = HDMI_CEC_GetErrorStatus();
  992. CEC_ClearFlag(CEC_FLAG_TXACKE);
  993. break;
  994. }
  995. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  996. {
  997. errorstatus = HDMI_CEC_OK;
  998. break;
  999. }
  1000. count++;
  1001. }
  1002. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1003. {
  1004. errorstatus = HDMI_CEC_TIMEOUT;
  1005. return(errorstatus);
  1006. }
  1007. if (errorstatus == HDMI_CEC_OK)
  1008. {
  1009. HDMI_CEC_Follower_String[9][1] = (uint8_t *)"1";
  1010. i++;
  1011. errorstatus = HDMI_CEC_OK;
  1012. }
  1013. /* Clear CEC ISR register */
  1014. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  1015. /*-------------------------------- Tuner 4 ---------------------------------*/
  1016. CEC_EndOfMessage();
  1017. CEC_SendData((MyLogicalAddress << 4) | 0xA);
  1018. /* Start of message */
  1019. CEC_StartOfMessage();
  1020. /* Wait till the header message is sent */
  1021. while (count < HDMI_CEC_TIMEOUT_VALUE)
  1022. {
  1023. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  1024. {
  1025. errorstatus = HDMI_CEC_GetErrorStatus();
  1026. CEC_ClearFlag(CEC_FLAG_TXACKE);
  1027. break;
  1028. }
  1029. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1030. {
  1031. errorstatus = HDMI_CEC_OK;
  1032. break;
  1033. }
  1034. count++;
  1035. }
  1036. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1037. {
  1038. errorstatus = HDMI_CEC_TIMEOUT;
  1039. return(errorstatus);
  1040. }
  1041. if (errorstatus == HDMI_CEC_OK)
  1042. {
  1043. HDMI_CEC_Follower_String[10][1] = (uint8_t *)"1";
  1044. i++;
  1045. errorstatus = HDMI_CEC_OK;
  1046. }
  1047. /* Clear CEC ISR register */
  1048. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  1049. /*---------------------------- Playback device 3 ---------------------------*/
  1050. CEC_EndOfMessage();
  1051. CEC_SendData((MyLogicalAddress << 4) | 0xB);
  1052. /* Start of message */
  1053. CEC_StartOfMessage();
  1054. /* Wait till the header message is sent */
  1055. while(count < HDMI_CEC_TIMEOUT_VALUE)
  1056. {
  1057. if (CEC_GetFlagStatus(CEC_FLAG_TXACKE) != RESET)
  1058. {
  1059. errorstatus = HDMI_CEC_GetErrorStatus();
  1060. CEC_ClearFlag(CEC_FLAG_TXACKE);
  1061. break;
  1062. }
  1063. else if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1064. {
  1065. errorstatus = HDMI_CEC_OK;
  1066. break;
  1067. }
  1068. count++;
  1069. }
  1070. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1071. {
  1072. errorstatus = HDMI_CEC_TIMEOUT;
  1073. return(errorstatus);
  1074. }
  1075. if (errorstatus == HDMI_CEC_OK)
  1076. {
  1077. HDMI_CEC_Follower_String[11][1] = (uint8_t *)"1";
  1078. i++;
  1079. errorstatus = HDMI_CEC_OK;
  1080. }
  1081. /* Clear CEC ISR register */
  1082. CEC_ClearFlag(CEC_FLAG_TXBR|CEC_FLAG_TXEND);
  1083. CECDevicesNumber = i - 1;
  1084. return errorstatus;
  1085. }
  1086. /**
  1087. * @brief Sink Physical address discovery.
  1088. * @param None
  1089. * @retval HDMI_CEC_Error: CEC Error code.
  1090. */
  1091. static HDMI_CEC_Error SinkPhysicalAddressDiscovery(void)
  1092. {
  1093. uint32_t DataNum=0;
  1094. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  1095. uint32_t i = 0;
  1096. I2C_InitTypeDef I2C_InitStructure;
  1097. GPIO_InitTypeDef GPIO_InitStructure;
  1098. /*------------------------------ Physical address discovery ----------------*/
  1099. /* if HDMI_CEC_ROOT == 0x1*/
  1100. MyPhysicalAddress = 0x0000;
  1101. /* The HDMI-CEC here is configured as sink or as a repeater. The configuration
  1102. of the +5V power signal and the HPD should be well configured.
  1103. Implement here the EDID Structure to be sent to the HDMI source.
  1104. For more details please refer to the HDMI specification.
  1105. The EDID structure should be sent to the device source using the DDC Channel
  1106. and using the HPD signal. */
  1107. /* Enable CEC_I2C clocks */
  1108. RCC_APB1PeriphClockCmd(HDMI_CEC_SINK_I2C_CLK, ENABLE);
  1109. /* Enable CEC_I2C_GPIO and CEC_HPD_GPIO clocks */
  1110. RCC_AHBPeriphClockCmd(HDMI_CEC_SINK_I2C_GPIO_CLK | HDMI_CEC_HPD_SINK_GPIO_CLK, ENABLE);
  1111. I2C_DeInit(HDMI_CEC_SINK_I2C);
  1112. GPIO_PinAFConfig(HDMI_CEC_SINK_I2C_GPIO_PORT, HDMI_CEC_SINK_I2C_SCL_PIN_SOURCE, GPIO_AF_1);
  1113. GPIO_PinAFConfig(HDMI_CEC_SINK_I2C_GPIO_PORT, HDMI_CEC_SINK_I2C_SDA_PIN_SOURCE, GPIO_AF_1);
  1114. /* Set PF6/PF7 as OD AF - I2C2_SCL/I2C2_SDA of Source*/
  1115. GPIO_InitStructure.GPIO_Pin = HDMI_CEC_SINK_I2C_SDA_PIN | HDMI_CEC_SINK_I2C_SCL_PIN;
  1116. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  1117. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  1118. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  1119. GPIO_Init(HDMI_CEC_SINK_I2C_GPIO_PORT, &GPIO_InitStructure);
  1120. /* Set PA11 as PP - HPD */
  1121. GPIO_InitStructure.GPIO_Pin = HDMI_CEC_HPD_SINK_PIN;
  1122. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  1123. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  1124. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  1125. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  1126. GPIO_Init(HDMI_CEC_HPD_SINK_GPIO_PORT, &GPIO_InitStructure);
  1127. RCC_I2CCLKConfig(RCC_I2C1CLK_HSI);
  1128. HDMI_CEC_SINK_I2C->CR1 &= (uint16_t)(~I2C_CR1_NOSTRETCH);
  1129. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  1130. I2C_InitStructure.I2C_Timing = HDMI_CEC_I2C_TIMING;
  1131. I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  1132. /* Initialize the I2C_DigitalFilter member */
  1133. I2C_InitStructure.I2C_DigitalFilter = 0x00;
  1134. I2C_InitStructure.I2C_OwnAddress1 = HDMI_CEC_I2C_SLAVE_ADDRESS7;
  1135. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  1136. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  1137. I2C_Init(HDMI_CEC_SINK_I2C, &I2C_InitStructure);
  1138. /* Set the Hot plug detect signal */
  1139. HDMI_CEC_HPD_HIGH();
  1140. /* The HDMI-CEC here is configured as source or as a repeater. The configuration
  1141. of the +5V power signal and the HPD should be well configured.
  1142. The source should wait for HPD and then read the EDID structure. */
  1143. while(GPIO_ReadInputDataBit(HDMI_CEC_HPD_SINK_GPIO_PORT, HDMI_CEC_HPD_SINK_PIN) == RESET)
  1144. {
  1145. }
  1146. /* Wait for 100 ms after HPD was received */
  1147. for(i = 0; i <0x5FFFFF; i++)
  1148. {
  1149. }
  1150. /* Enable HDMI_CEC_SINK_I2C */
  1151. I2C_Cmd(HDMI_CEC_SINK_I2C, ENABLE);
  1152. /* Wait until ADDR is set */
  1153. while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_ADDR));
  1154. /* Clear ADDR flag */
  1155. HDMI_CEC_SINK_I2C->ICR = I2C_ICR_ADDRCF;
  1156. while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_RXNE));
  1157. HDMI_CEC_SINK_I2C->RXDR;
  1158. /* Wait until ADDR is set */
  1159. while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_ADDR));
  1160. /* Clear ADDR flag */
  1161. HDMI_CEC_SINK_I2C->ICR = I2C_ICR_ADDRCF;
  1162. while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_TXIS));
  1163. while (DataNum != 256)
  1164. {
  1165. if (HDMI_CEC_SINK_I2C->ISR & I2C_ISR_TXIS)
  1166. {
  1167. /* Write data to TXDR */
  1168. HDMI_CEC_SINK_I2C->TXDR = (uint8_t)(EDID_Structure[DataNum++]);
  1169. }
  1170. }
  1171. while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_STOPF));
  1172. while (!(HDMI_CEC_SINK_I2C->ISR & I2C_ISR_NACKF));
  1173. /* Clear STOPF and NACKF flag */
  1174. HDMI_CEC_SINK_I2C->ICR = ( I2C_ICR_STOPCF | I2C_ICR_NACKCF) ;
  1175. return errorstatus;
  1176. }
  1177. /**
  1178. * @brief Source Physical address discovery.
  1179. * @param None
  1180. * @retval HDMI_CEC_Error: CEC Error code.
  1181. */
  1182. static HDMI_CEC_Error SourcePhysicalAddressDiscovery(void)
  1183. {
  1184. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  1185. #ifdef HDMI_CEC_USE_DDC
  1186. uint32_t DataNum=0;
  1187. GPIO_InitTypeDef GPIO_InitStructure;
  1188. I2C_InitTypeDef I2C_InitStructure;
  1189. /* Enable CEC_I2C clocks */
  1190. RCC_APB1PeriphClockCmd(HDMI_CEC_I2C_CLK, ENABLE);
  1191. /* Enable CEC_I2C_GPIO and CEC_HPD_GPIO clocks */
  1192. RCC_AHBPeriphClockCmd(HDMI_CEC_I2C_GPIO_CLK | HDMI_CEC_HPD_SOURCE_GPIO_CLK, ENABLE);
  1193. GPIO_PinAFConfig(HDMI_CEC_I2C_GPIO_PORT,HDMI_CEC_I2C_SCL_PIN_SOURCE,GPIO_AF_1);
  1194. GPIO_PinAFConfig(HDMI_CEC_I2C_GPIO_PORT,HDMI_CEC_I2C_SDA_PIN_SOURCE,GPIO_AF_1);
  1195. /* Configure CEC_I2C_SCL_PIN and CEC_I2C_SDA_PIN as Output open drain */
  1196. GPIO_InitStructure.GPIO_Pin = HDMI_CEC_I2C_SCL_PIN | HDMI_CEC_I2C_SDA_PIN;
  1197. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  1198. GPIO_Init(HDMI_CEC_I2C_GPIO_PORT, &GPIO_InitStructure);
  1199. /* This configuration is only when the HDMI CEC is configured as source.
  1200. The HDMI source has to provide the +5V Power signal to the sink.
  1201. Then, the source will wait for HPD signal to be asserted from the sink.
  1202. Once the HPD signal is detected the source shall read the EDID structure
  1203. through the DDC channel. */
  1204. /* Configure CEC_HPD_GPIO as Input pull down */
  1205. GPIO_InitStructure.GPIO_Pin = HDMI_CEC_HPD_SOURCE_PIN;
  1206. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  1207. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
  1208. GPIO_Init(HDMI_CEC_HPD_SOURCE_GPIO_PORT, &GPIO_InitStructure);
  1209. /* I2C configuration */
  1210. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  1211. I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  1212. I2C_InitStructure.I2C_DigitalFilter = 0x00;
  1213. I2C_InitStructure.I2C_OwnAddress1 = 0x00;
  1214. I2C_InitStructure.I2C_Ack =I2C_Ack_Enable;
  1215. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  1216. I2C_InitStructure.I2C_Timing = HDMI_CEC_I2C_TIMING;
  1217. /* I2C configuration */
  1218. I2C_Init(HDMI_CEC_I2C, &I2C_InitStructure);
  1219. /* I2C Peripheral Enable */
  1220. I2C_Cmd(HDMI_CEC_I2C, ENABLE);
  1221. /* Configure slave address, nbytes, reload and generate start */
  1222. HDMI_CEC_I2C->CR2 = (uint32_t)(0xA0) | (uint32_t)(1 << 16) | I2C_CR2_START ;
  1223. /* Wait until TXIS is set */
  1224. while (!(HDMI_CEC_I2C->ISR & I2C_ISR_TXIS));
  1225. /* Send memory address */
  1226. HDMI_CEC_I2C->TXDR = (uint8_t)(0x00);
  1227. /* Wait until TC is set */
  1228. while (!(HDMI_CEC_I2C->ISR & I2C_ISR_TC));
  1229. /* Update CR2 : set Nbytes and reload */
  1230. HDMI_CEC_I2C->CR2 = (uint32_t)(0xA0) | I2C_CR2_RD_WRN | I2C_CR2_START | (uint32_t)(255 << 16) | I2C_CR2_RELOAD;
  1231. /* Wait until all data are received */
  1232. while (DataNum != 255)
  1233. {
  1234. /* Wait until RXNE flag is set */
  1235. while (!(HDMI_CEC_I2C->ISR & I2C_ISR_RXNE));
  1236. /* Read data from RXDR */
  1237. pBuffer[DataNum]= (uint8_t)HDMI_CEC_I2C->RXDR;
  1238. /* Update number of received data */
  1239. DataNum++;
  1240. }
  1241. /* Wait until TCR flag is set */
  1242. while (!(HDMI_CEC_I2C->ISR & I2C_ISR_TCR));
  1243. /* Update CR2 : set Nbytes and end mode */
  1244. HDMI_CEC_I2C->CR2 = (uint32_t)(1 << 16) | I2C_CR2_AUTOEND;
  1245. while (!(HDMI_CEC_I2C->ISR & I2C_ISR_RXNE));
  1246. /* Read data from RXDR */
  1247. pBuffer[DataNum]= (uint8_t)HDMI_CEC_I2C->RXDR;
  1248. /* Wait until STOPF flag is set */
  1249. while (!(HDMI_CEC_I2C->ISR & I2C_ISR_STOPF));
  1250. /* Clear STOPF flag */
  1251. HDMI_CEC_I2C->ICR = I2C_ICR_STOPCF;
  1252. MyPhysicalAddress = ((pBuffer[158] << 8) | pBuffer[159]);
  1253. #else
  1254. MyPhysicalAddress = 0x1000;
  1255. #endif
  1256. return errorstatus;
  1257. }
  1258. /**
  1259. * @brief Allocate the logical address.
  1260. * @param None
  1261. * @retval HDMI_CEC_Error: CEC Error code.
  1262. */
  1263. static HDMI_CEC_Error LogicalAddressAllocation(void)
  1264. {
  1265. HDMI_CEC_Error errorstatus = HDMI_CEC_OK;
  1266. uint32_t count = 0;
  1267. /*------------------ Logical address allocation ----------------------------*/
  1268. /* Get the device type */
  1269. /* Device type = CEC_TV */
  1270. if (DeviceType == HDMI_CEC_TV)
  1271. {
  1272. if (HDMI_CEC_ROOT)
  1273. {
  1274. MyLogicalAddress = 0x00;
  1275. }
  1276. else
  1277. {
  1278. CEC_OwnAddressConfig(0xE); /* Own address = 0xE */
  1279. CEC_EndOfMessage();
  1280. CEC_SendData(0xEE);
  1281. /* Start of message */
  1282. CEC_StartOfMessage();
  1283. /* Wait till the header message is sent */
  1284. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1285. {
  1286. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1287. {
  1288. break;
  1289. }
  1290. count++;
  1291. }
  1292. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1293. {
  1294. errorstatus = HDMI_CEC_TIMEOUT;
  1295. return(errorstatus);
  1296. }
  1297. errorstatus = HDMI_CEC_GetErrorStatus();
  1298. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1299. {
  1300. MyLogicalAddress = 0x0E;
  1301. errorstatus = HDMI_CEC_OK;
  1302. }
  1303. else if (errorstatus == HDMI_CEC_OK)
  1304. {
  1305. MyLogicalAddress = 0x0F;
  1306. errorstatus = HDMI_CEC_DEVICE_UNREGISTRED;
  1307. }
  1308. }
  1309. /* Clear CEC ISR register */
  1310. CEC_ClearFlag(CEC_FLAG_TXEND);
  1311. }
  1312. /* Device type = CEC_RECORDING */
  1313. if (DeviceType == HDMI_CEC_RECORDING)
  1314. {
  1315. CEC_OwnAddressConfig(0x1); /* Own address = 0x1 */
  1316. CEC_EndOfMessage();
  1317. CEC_SendData(0x11);
  1318. /* Start of message */
  1319. CEC_StartOfMessage();
  1320. /* Wait till the header message is sent */
  1321. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1322. {
  1323. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1324. {
  1325. break;
  1326. }
  1327. count++;
  1328. }
  1329. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1330. {
  1331. errorstatus = HDMI_CEC_TIMEOUT;
  1332. return(errorstatus);
  1333. }
  1334. errorstatus = HDMI_CEC_GetErrorStatus();
  1335. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1336. {
  1337. MyLogicalAddress = 0x01;
  1338. errorstatus = HDMI_CEC_OK;
  1339. }
  1340. else if (errorstatus == HDMI_CEC_OK)
  1341. {
  1342. /* Clear CEC ISR register */
  1343. CEC_ClearFlag(CEC_FLAG_TXEND|CEC_FLAG_TXACKE|CEC_FLAG_TXBR);
  1344. /* Clear the OAR */
  1345. CEC_OwnAddressClear();
  1346. CEC_OwnAddressConfig(0x2); /* Own address = 0x2 */
  1347. CEC_EndOfMessage();
  1348. CEC_SendData(0x22);
  1349. /* Start of message */
  1350. CEC_StartOfMessage();
  1351. count = 0;
  1352. /* Wait till the header message is sent */
  1353. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1354. {
  1355. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1356. {
  1357. break;
  1358. }
  1359. count++;
  1360. }
  1361. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1362. {
  1363. errorstatus = HDMI_CEC_TIMEOUT;
  1364. return(errorstatus);
  1365. }
  1366. errorstatus = HDMI_CEC_GetErrorStatus();
  1367. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1368. {
  1369. MyLogicalAddress = 0x02;
  1370. errorstatus = HDMI_CEC_OK;
  1371. }
  1372. else if (errorstatus == HDMI_CEC_OK)
  1373. {
  1374. /* Clear CEC ISR register */
  1375. CEC_ClearFlag( CEC_FLAG_TXEND);
  1376. CEC_OwnAddressClear();
  1377. CEC_OwnAddressConfig(0x9); /* Own address = 0x9 */
  1378. CEC_EndOfMessage();
  1379. CEC_SendData(0x99);
  1380. /* Start of message */
  1381. CEC_StartOfMessage();
  1382. count = 0;
  1383. /* Wait till the header message is sent */
  1384. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1385. {
  1386. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1387. {
  1388. break;
  1389. }
  1390. count++;
  1391. }
  1392. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1393. {
  1394. errorstatus = HDMI_CEC_TIMEOUT;
  1395. return(errorstatus);
  1396. }
  1397. errorstatus = HDMI_CEC_GetErrorStatus();
  1398. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1399. {
  1400. MyLogicalAddress = 0x09;
  1401. errorstatus = HDMI_CEC_OK;
  1402. }
  1403. else if (errorstatus == HDMI_CEC_OK)
  1404. {
  1405. MyLogicalAddress = 0x0F;
  1406. errorstatus = HDMI_CEC_DEVICE_UNREGISTRED;
  1407. }
  1408. }
  1409. }
  1410. /* Clear CEC ISR register */
  1411. CEC_ClearFlag(CEC_FLAG_TXEND|CEC_FLAG_TXACKE|CEC_FLAG_TXBR);
  1412. }
  1413. /* Device type = CEC_TUNER */
  1414. if (DeviceType == HDMI_CEC_TUNER)
  1415. {
  1416. CEC_OwnAddressConfig(0x3); /* Own address = 0x3 */
  1417. CEC_EndOfMessage();
  1418. CEC_SendData(0x33);
  1419. /* Start of message */
  1420. CEC_StartOfMessage();
  1421. count = 0;
  1422. /* Wait till the header message is sent */
  1423. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1424. {
  1425. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1426. {
  1427. break;
  1428. }
  1429. count++;
  1430. }
  1431. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1432. {
  1433. errorstatus = HDMI_CEC_TIMEOUT;
  1434. return(errorstatus);
  1435. }
  1436. errorstatus = HDMI_CEC_GetErrorStatus();
  1437. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1438. {
  1439. MyLogicalAddress = 0x03;
  1440. errorstatus = HDMI_CEC_OK;
  1441. }
  1442. else if (errorstatus == HDMI_CEC_OK)
  1443. {
  1444. /* Clear CEC ISR register */
  1445. CEC_ClearFlag(CEC_FLAG_TXEND);
  1446. /* Clear the OAR */
  1447. CEC_OwnAddressClear();
  1448. CEC_OwnAddressConfig(0x6); /* Own address = 0x6 */
  1449. CEC_EndOfMessage();
  1450. CEC_SendData(0x66);
  1451. /* Start of message */
  1452. CEC_StartOfMessage();
  1453. count = 0;
  1454. /* Wait till the header message is sent */
  1455. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1456. {
  1457. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1458. {
  1459. break;
  1460. }
  1461. count++;
  1462. }
  1463. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1464. {
  1465. errorstatus = HDMI_CEC_TIMEOUT;
  1466. return(errorstatus);
  1467. }
  1468. errorstatus = HDMI_CEC_GetErrorStatus();
  1469. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1470. {
  1471. MyLogicalAddress = 0x06;
  1472. errorstatus = HDMI_CEC_OK;
  1473. }
  1474. else if (errorstatus == HDMI_CEC_OK)
  1475. {
  1476. /* Clear CEC ISR register */
  1477. CEC_ClearFlag(CEC_FLAG_TXEND);
  1478. /* Clear the OAR */
  1479. CEC_OwnAddressClear();
  1480. CEC_OwnAddressConfig(0x7); /* Own address = 0x7 */
  1481. CEC_EndOfMessage();
  1482. CEC_SendData(0x77);
  1483. /* Start of message */
  1484. CEC_StartOfMessage();
  1485. count = 0;
  1486. /* Wait till the header message is sent */
  1487. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1488. {
  1489. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1490. {
  1491. break;
  1492. }
  1493. count++;
  1494. }
  1495. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1496. {
  1497. errorstatus = HDMI_CEC_TIMEOUT;
  1498. return(errorstatus);
  1499. }
  1500. errorstatus = HDMI_CEC_GetErrorStatus();
  1501. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1502. {
  1503. MyLogicalAddress = 0x07;
  1504. errorstatus = HDMI_CEC_OK;
  1505. }
  1506. else if (errorstatus == HDMI_CEC_OK)
  1507. {
  1508. /* Clear CEC ISR register */
  1509. CEC_ClearFlag(CEC_FLAG_TXEND);
  1510. /* Clear the OAR */
  1511. CEC_OwnAddressClear();
  1512. CEC_OwnAddressConfig(0xA); /* Own address = 0xA */
  1513. CEC_EndOfMessage();
  1514. CEC_SendData(0xAA);
  1515. /* Start of message */
  1516. CEC_StartOfMessage();
  1517. count = 0;
  1518. /* Wait till the header message is sent */
  1519. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1520. {
  1521. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1522. {
  1523. break;
  1524. }
  1525. count++;
  1526. }
  1527. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1528. {
  1529. errorstatus = HDMI_CEC_TIMEOUT;
  1530. return(errorstatus);
  1531. }
  1532. errorstatus = HDMI_CEC_GetErrorStatus();
  1533. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1534. {
  1535. MyLogicalAddress = 0x0A;
  1536. errorstatus = HDMI_CEC_OK;
  1537. }
  1538. else if (errorstatus == HDMI_CEC_OK)
  1539. {
  1540. MyLogicalAddress = 0x0F;
  1541. errorstatus = HDMI_CEC_DEVICE_UNREGISTRED;
  1542. }
  1543. }
  1544. }
  1545. }
  1546. /* Clear CEC ISR register */
  1547. CEC_ClearFlag(CEC_FLAG_TXEND);
  1548. }
  1549. /* Device type = CEC_PLAYBACK */
  1550. if (DeviceType == HDMI_CEC_PLAYBACK)
  1551. {
  1552. CEC_OwnAddressConfig(0x4); /* Own address = 0x4 */
  1553. CEC_EndOfMessage();
  1554. CEC_SendData(0x44);
  1555. /* Start of message */
  1556. CEC_StartOfMessage();
  1557. count = 0;
  1558. /* Wait till the header message is sent */
  1559. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1560. {
  1561. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1562. {
  1563. break;
  1564. }
  1565. count++;
  1566. }
  1567. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1568. {
  1569. errorstatus = HDMI_CEC_TIMEOUT;
  1570. return(errorstatus);
  1571. }
  1572. errorstatus = HDMI_CEC_GetErrorStatus();
  1573. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1574. {
  1575. MyLogicalAddress = 0x04;
  1576. errorstatus = HDMI_CEC_OK;
  1577. }
  1578. else if (errorstatus == HDMI_CEC_OK)
  1579. {
  1580. /* Clear CEC ISR register */
  1581. CEC_ClearFlag(CEC_FLAG_TXEND);
  1582. /* Clear the OAR */
  1583. CEC_OwnAddressClear();
  1584. CEC_OwnAddressConfig(0x8); /* Own address = 0x8 */
  1585. CEC_EndOfMessage();
  1586. CEC_SendData(0x88);
  1587. /* Start of message */
  1588. CEC_StartOfMessage();
  1589. count = 0;
  1590. /* Wait till the header message is sent */
  1591. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1592. {
  1593. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1594. {
  1595. break;
  1596. }
  1597. count++;
  1598. }
  1599. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1600. {
  1601. errorstatus = HDMI_CEC_TIMEOUT;
  1602. return(errorstatus);
  1603. }
  1604. errorstatus = HDMI_CEC_GetErrorStatus();
  1605. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1606. {
  1607. MyLogicalAddress = 0x08;
  1608. errorstatus = HDMI_CEC_OK;
  1609. }
  1610. else if (errorstatus == HDMI_CEC_OK)
  1611. {
  1612. /* Clear CEC ISR register */
  1613. CEC_ClearFlag(CEC_FLAG_TXEND);
  1614. /* Clear the OAR */
  1615. CEC_OwnAddressClear();
  1616. CEC_OwnAddressConfig(0xB); /* Own address = 0xBB */
  1617. CEC_EndOfMessage();
  1618. CEC_SendData(0xBB);
  1619. /* Start of message */
  1620. CEC_StartOfMessage();
  1621. count = 0;
  1622. /* Wait till the header message is sent */
  1623. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1624. {
  1625. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1626. {
  1627. break;
  1628. }
  1629. count++;
  1630. }
  1631. /* Clear the OAR */
  1632. CEC_OwnAddressClear();
  1633. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1634. {
  1635. errorstatus = HDMI_CEC_TIMEOUT;
  1636. return(errorstatus);
  1637. }
  1638. errorstatus = HDMI_CEC_GetErrorStatus();
  1639. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1640. {
  1641. MyLogicalAddress = 0x0B;
  1642. errorstatus = HDMI_CEC_OK;
  1643. }
  1644. else if (errorstatus == HDMI_CEC_OK)
  1645. {
  1646. MyLogicalAddress = 0x0F;
  1647. errorstatus = HDMI_CEC_DEVICE_UNREGISTRED;
  1648. }
  1649. }
  1650. }
  1651. /* Clear CEC ISR register */
  1652. CEC_ClearFlag(CEC_FLAG_TXEND);
  1653. }
  1654. /* Device type = CEC Audio System */
  1655. if (DeviceType == HDMI_CEC_AUDIOSYSTEM)
  1656. {
  1657. CEC_OwnAddressConfig(0x5); /* Own address = 0x5 */
  1658. CEC_EndOfMessage();
  1659. CEC_SendData(0x55);
  1660. /* Start of message */
  1661. CEC_StartOfMessage();
  1662. count = 0;
  1663. /* Wait till the header message is sent */
  1664. while ((CEC_GetFlagStatus(CEC_FLAG_TXACKE) == RESET) && (count < HDMI_CEC_TIMEOUT_VALUE))
  1665. {
  1666. if(CEC_GetFlagStatus(CEC_FLAG_TXEND) != RESET)
  1667. {
  1668. break;
  1669. }
  1670. count++;
  1671. }
  1672. if (count >= HDMI_CEC_TIMEOUT_VALUE)
  1673. {
  1674. errorstatus = HDMI_CEC_TIMEOUT;
  1675. return(errorstatus);
  1676. }
  1677. errorstatus = HDMI_CEC_GetErrorStatus();
  1678. if (errorstatus == HDMI_CEC_TX_ACKNOWLEDGE)
  1679. {
  1680. MyLogicalAddress = 0x05;
  1681. errorstatus = HDMI_CEC_OK;
  1682. }
  1683. else if (errorstatus == HDMI_CEC_OK)
  1684. {
  1685. MyLogicalAddress = 0x0F;
  1686. errorstatus = HDMI_CEC_DEVICE_UNREGISTRED;
  1687. }
  1688. /* Clear CEC ISR register */
  1689. CEC_ClearFlag(CEC_FLAG_TXEND);
  1690. }
  1691. return errorstatus;
  1692. }
  1693. /**
  1694. * @}
  1695. */
  1696. /**
  1697. * @}
  1698. */
  1699. /**
  1700. * @}
  1701. */
  1702. /**
  1703. * @}
  1704. */
  1705. /**
  1706. * @}
  1707. */
  1708. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/