STM32F0xx Standard Peripherals Firmware Library
|
STM32F0xx_StdPeriph_Examples/CEC/CEC_ListenMode/main.c
Go to the documentation of this file.
00001 /** 00002 ****************************************************************************** 00003 * @file CEC/CEC_ListenMode/main.c 00004 * @author MCD Application Team 00005 * @version V1.4.0 00006 * @date 24-July-2014 00007 * @brief Main program body 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> 00012 * 00013 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); 00014 * You may not use this file except in compliance with the License. 00015 * You may obtain a copy of the License at: 00016 * 00017 * http://www.st.com/software_license_agreement_liberty_v2 00018 * 00019 * Unless required by applicable law or agreed to in writing, software 00020 * distributed under the License is distributed on an "AS IS" BASIS, 00021 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00022 * See the License for the specific language governing permissions and 00023 * limitations under the License. 00024 * 00025 ****************************************************************************** 00026 */ 00027 00028 /* Includes ------------------------------------------------------------------*/ 00029 #include "main.h" 00030 00031 /** @addtogroup STM32F0xx_StdPeriph_Examples 00032 * @{ 00033 */ 00034 00035 /** @addtogroup CEC_ListenMode 00036 * @{ 00037 */ 00038 00039 /* Private typedef -----------------------------------------------------------*/ 00040 /* Private define ------------------------------------------------------------*/ 00041 /* Private macro -------------------------------------------------------------*/ 00042 /* Private variables ---------------------------------------------------------*/ 00043 00044 uint8_t MyLogicalAddress1 = 0x0, MyFollowerAddress = 0x0; 00045 uint8_t HeaderBlockValueToSend = 0; 00046 __IO uint8_t TransferStatus = 0; 00047 extern uint8_t ReceiveBuffer[10]; 00048 extern __IO uint8_t ReceivedFrame1; 00049 extern uint8_t rcv_inc; 00050 00051 /* Private function prototypes -----------------------------------------------*/ 00052 static void CEC_Config(void); 00053 00054 /* Private functions ---------------------------------------------------------*/ 00055 00056 /** 00057 * @brief Main program. 00058 * @param None 00059 * @retval None 00060 */ 00061 int main(void) 00062 { 00063 /*!< At this stage the microcontroller clock setting is already configured, 00064 this is done through SystemInit() function which is called from startup 00065 file (startup_stm32f0xx.s) before to branch to application main. 00066 To reconfigure the default setting of SystemInit() function, refer to 00067 system_stm32f0xx.c file 00068 */ 00069 00070 /* Initialize the LCD */ 00071 #ifdef USE_STM320518_EVAL 00072 STM320518_LCD_Init(); 00073 #else 00074 STM32072B_LCD_Init(); 00075 #endif /* USE_STM320518_EVAL */ 00076 00077 /* Initialize LEDs available on STM320518-EVAL board */ 00078 /* Configure LED1, LED2, LED3 and LED4 */ 00079 STM_EVAL_LEDInit(LED1); 00080 STM_EVAL_LEDInit(LED2); 00081 STM_EVAL_LEDInit(LED3); 00082 STM_EVAL_LEDInit(LED4); 00083 00084 #if defined (DEVICE_1) 00085 MyLogicalAddress1 = DEVICE_ADDRESS_1; 00086 MyFollowerAddress = DEVICE_ADDRESS_2; 00087 #elif defined (DEVICE_2) 00088 MyLogicalAddress1 = DEVICE_ADDRESS_2; 00089 MyFollowerAddress = DEVICE_ADDRESS_1; 00090 #elif defined (DEVICE_3) 00091 MyLogicalAddress1 = DEVICE_ADDRESS_3; 00092 #endif /* DEVICE_1 */ 00093 00094 /* CEC configuration */ 00095 CEC_Config(); 00096 00097 #if defined (DEVICE_3) 00098 LCD_Clear(LCD_COLOR_WHITE); 00099 LCD_SetBackColor(LCD_COLOR_BLACK); 00100 LCD_SetTextColor(LCD_COLOR_WHITE); 00101 LCD_DisplayStringLine(LCD_LINE_0, (uint8_t *)" Spy CEC Device "); 00102 #else 00103 00104 /* Configure the Joystick button and its associated EXTI Line */ 00105 STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_EXTI); 00106 STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_EXTI); 00107 STM_EVAL_PBInit(Button_UP, BUTTON_MODE_EXTI); 00108 STM_EVAL_PBInit(Button_DOWN, BUTTON_MODE_EXTI); 00109 00110 /* Build the Header block to send */ 00111 HeaderBlockValueToSend = (((MyLogicalAddress1 & 0xF) << 4)|(MyFollowerAddress & 0xF)); 00112 00113 /* Clear the LCD */ 00114 LCD_Clear(LCD_COLOR_BLUE); 00115 LCD_SetBackColor(LCD_COLOR_BLACK); 00116 LCD_SetTextColor(LCD_COLOR_WHITE); 00117 LCD_DisplayStringLine(LCD_LINE_0, (uint8_t *)" Select CEC Command "); 00118 LCD_SetBackColor(LCD_COLOR_BLUE); 00119 LCD_SetTextColor(LCD_COLOR_WHITE); 00120 LCD_DisplayStringLine(LCD_LINE_2, (uint8_t *)"RIGHT : OSD Name "); 00121 LCD_DisplayStringLine(LCD_LINE_3, (uint8_t *)"LEFT : CEC Version "); 00122 LCD_DisplayStringLine(LCD_LINE_4, (uint8_t *)"UP : Volume Up "); 00123 LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"DOWN : Volume Down "); 00124 #endif /* DEVICE_3 */ 00125 00126 while(1) 00127 { 00128 /* If a frame has been received */ 00129 while(ReceivedFrame1 == 0) 00130 {} 00131 00132 if (ReceivedFrame1 == 1) 00133 { 00134 /* OK */ 00135 if (ReceiveBuffer[1] ==0x44) /* Test on the opcode value */ 00136 { 00137 /* Receive command is equal to Volume Up(Button Up) */ 00138 if (ReceiveBuffer[2] == 0x41) /* Test on the operand value */ 00139 { 00140 STM_EVAL_LEDOn(LED1); 00141 STM_EVAL_LEDOn(LED2); 00142 STM_EVAL_LEDOn(LED3); 00143 STM_EVAL_LEDOn(LED4); 00144 }/* Test on the operand value */ 00145 else if (ReceiveBuffer[2] == 0x42) /* Receive command is equal to Volume Down(Button Down) */ 00146 { 00147 STM_EVAL_LEDOff(LED1); 00148 STM_EVAL_LEDOff(LED2); 00149 STM_EVAL_LEDOff(LED3); 00150 STM_EVAL_LEDOff(LED4); 00151 } 00152 } 00153 else if (ReceiveBuffer[1] ==0x46) /* Test on the opcode value */ 00154 { 00155 STM_EVAL_LEDOn(LED1); 00156 STM_EVAL_LEDOn(LED2); 00157 STM_EVAL_LEDOff(LED3); 00158 STM_EVAL_LEDOff(LED4); 00159 } 00160 else if (ReceiveBuffer[1] ==0x9F) /* Test on the opcode value */ 00161 { 00162 STM_EVAL_LEDOff(LED1); 00163 STM_EVAL_LEDOff(LED2); 00164 STM_EVAL_LEDOn(LED3); 00165 STM_EVAL_LEDOn(LED4); 00166 } 00167 00168 LCD_SetBackColor(LCD_COLOR_GREEN); 00169 LCD_DisplayStringLine(LCD_LINE_7, (uint8_t *)"Receive : Succeeded "); 00170 } 00171 else 00172 { 00173 /* KO */ 00174 /* Turn on LED2 */ 00175 STM_EVAL_LEDOn(LED3); 00176 LCD_SetBackColor(LCD_COLOR_RED); 00177 LCD_DisplayStringLine(LCD_LINE_7, (uint8_t *)"Receive : Failed "); 00178 } 00179 ReceivedFrame1 = 0; 00180 00181 for (rcv_inc=0;rcv_inc<10;rcv_inc++) 00182 { 00183 ReceiveBuffer[rcv_inc]=0; 00184 } 00185 rcv_inc=0; 00186 } 00187 } 00188 /** 00189 * @brief Configures the CEC peripheral. 00190 * @param None 00191 * @retval None 00192 */ 00193 static void CEC_Config(void) 00194 { 00195 CEC_InitTypeDef CEC_InitStructure; 00196 NVIC_InitTypeDef NVIC_InitStructure; 00197 GPIO_InitTypeDef GPIO_InitStructure; 00198 00199 /* Enable CEC clocks */ 00200 RCC_APB1PeriphClockCmd(RCC_APB1Periph_CEC , ENABLE); 00201 00202 /* Enable CEC_LINE_GPIO clocks */ 00203 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); 00204 00205 /* Configure CEC_LINE_GPIO as Output open drain */ 00206 #ifdef USE_STM320518_EVAL 00207 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 00208 #else 00209 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; 00210 #endif /* USE_STM320518_EVAL */ 00211 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; 00212 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 00213 GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; 00214 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; 00215 GPIO_Init(GPIOB, &GPIO_InitStructure); 00216 00217 /* Enable the CEC global Interrupt (with higher priority) */ 00218 #ifdef USE_STM320518_EVAL 00219 NVIC_InitStructure.NVIC_IRQChannel = CEC_IRQn; 00220 #else 00221 NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn; 00222 #endif /* USE_STM320518_EVAL */ 00223 NVIC_InitStructure.NVIC_IRQChannelPriority = 0; 00224 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 00225 NVIC_Init(&NVIC_InitStructure); 00226 00227 /* Configure CEC */ 00228 CEC_InitStructure.CEC_SignalFreeTime = CEC_SignalFreeTime_Standard; 00229 CEC_InitStructure.CEC_RxTolerance = CEC_RxTolerance_Standard; 00230 CEC_InitStructure.CEC_StopReception = CEC_StopReception_Off; 00231 CEC_InitStructure.CEC_BitRisingError = CEC_BitRisingError_Off; 00232 CEC_InitStructure.CEC_LongBitPeriodError = CEC_LongBitPeriodError_Off; 00233 CEC_InitStructure.CEC_BRDNoGen = CEC_BRDNoGen_Off; 00234 CEC_InitStructure.CEC_SFTOption = CEC_SFTOption_Off; 00235 CEC_Init(&CEC_InitStructure); 00236 00237 /* Set the CEC initiator address */ 00238 CEC_OwnAddressConfig(MyLogicalAddress1); 00239 00240 #if defined (DEVICE_3) 00241 /* Enable the listen mode for the Spy device */ 00242 CEC_ListenModeCmd(ENABLE); 00243 #endif /* DEVICE_3 */ 00244 00245 /* Activate CEC interrupts associated to the set of TX flags */ 00246 CEC_ITConfig(CEC_IT_TXEND|CEC_IT_TXBR,ENABLE); 00247 00248 /* Activate CEC interrupts associated to the set of RX flags */ 00249 CEC_ITConfig(CEC_IT_RXEND|CEC_IT_RXBR,ENABLE); 00250 00251 /* Activate CEC interrupts associated to the set of TX error */ 00252 CEC_ITConfig(CEC_IT_TXACKE|CEC_IT_TXERR|CEC_IT_TXUDR|CEC_IT_ARBLST,ENABLE); 00253 00254 /* Activate CEC interrupts associated to the set of RX error */ 00255 CEC_ITConfig(CEC_IT_RXACKE|CEC_IT_LBPE|CEC_IT_SBPE|CEC_IT_BRE|CEC_IT_RXOVR,ENABLE); 00256 00257 /* Enable CEC */ 00258 CEC_Cmd(ENABLE); 00259 } 00260 00261 00262 #ifdef USE_FULL_ASSERT 00263 00264 /** 00265 * @brief Reports the name of the source file and the source line number 00266 * where the assert_param error has occurred. 00267 * @param file: pointer to the source file name 00268 * @param line: assert_param error line source number 00269 * @retval None 00270 */ 00271 void assert_failed(uint8_t* file, uint32_t line) 00272 { 00273 /* User can add his own implementation to report the file name and line number, 00274 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 00275 00276 /* Infinite loop */ 00277 while (1) 00278 { 00279 } 00280 } 00281 #endif 00282 00283 /** 00284 * @} 00285 */ 00286 00287 /** 00288 * @} 00289 */ 00290 00291 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/