STM32F0xx Standard Peripherals Firmware Library: main.c Source File

STM32F0xx Standard Peripherals Library

STM32F0xx_StdPeriph_Examples/CEC/CEC_DataExchange/main.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    CEC/CEC_DataExchange/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>&copy; 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_DataExchange
00036   * @{
00037   */
00038 
00039 /* Private typedef -----------------------------------------------------------*/
00040 /* Private define ------------------------------------------------------------*/
00041 /* Private macro -------------------------------------------------------------*/
00042 /* Private variables ---------------------------------------------------------*/
00043 uint8_t MyLogicalAddress1 = 0x0, MyFollowerAddress = 0x0;
00044 uint8_t HeaderBlockValueToSend = 0;
00045 __IO uint8_t TransferStatus = 0;
00046 extern uint8_t ReceiveBuffer[10];
00047 extern __IO uint8_t ReceivedFrame1;
00048 extern uint8_t rcv_inc;
00049 
00050 /* Private function prototypes -----------------------------------------------*/
00051 static void CEC_Config(void);
00052 
00053 /* Private functions ---------------------------------------------------------*/
00054 
00055 /**
00056   * @brief  Main program.
00057   * @param  None
00058   * @retval None
00059   */
00060 int main(void)
00061 {
00062   /*!< At this stage the microcontroller clock setting is already configured, 
00063   this is done through SystemInit() function which is called from startup
00064   file (startup_stm32f0xx.s) before to branch to application main.
00065   To reconfigure the default setting of SystemInit() function, refer to
00066   system_stm32f0xx.c file
00067   */
00068 #ifdef LCD_DISPLAY
00069   /* Initialize the LCD */
00070 #ifdef USE_STM320518_EVAL
00071     STM320518_LCD_Init();
00072 #else
00073     STM32072B_LCD_Init();
00074 #endif /* USE_STM320518_EVAL */
00075 #endif /* LCD_DISPLAY */
00076 
00077   /* Initialize LEDs and JoyStick Button 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   /* Configure the Joystick button and its associated EXTI Line */
00085   STM_EVAL_PBInit(BUTTON_RIGHT, BUTTON_MODE_EXTI);
00086   STM_EVAL_PBInit(BUTTON_LEFT, BUTTON_MODE_EXTI);
00087   STM_EVAL_PBInit(Button_UP, BUTTON_MODE_EXTI);
00088   STM_EVAL_PBInit(Button_DOWN, BUTTON_MODE_EXTI);
00089 
00090 #if defined (DEVICE_1)
00091   MyLogicalAddress1 = DEVICE_ADDRESS_1;
00092   MyFollowerAddress = DEVICE_ADDRESS_2;
00093 #elif defined (DEVICE_2)
00094   MyLogicalAddress1 = DEVICE_ADDRESS_2;
00095   MyFollowerAddress = DEVICE_ADDRESS_1;
00096 #endif /* DEVICE_1 */
00097 
00098   /* CEC configuration */
00099   CEC_Config();
00100 
00101   /* Build the Header block to send */
00102   HeaderBlockValueToSend = (((MyLogicalAddress1 & 0xF) << 4)|(MyFollowerAddress & 0xF));
00103 
00104 #ifdef LCD_DISPLAY
00105   /* Clear the LCD */
00106   LCD_Clear(LCD_COLOR_BLUE);
00107   LCD_SetBackColor(LCD_COLOR_BLACK);
00108   LCD_SetTextColor(LCD_COLOR_WHITE);
00109   LCD_DisplayStringLine(LCD_LINE_0, (uint8_t *)" Select CEC Command ");
00110   LCD_SetBackColor(LCD_COLOR_BLUE);
00111   LCD_SetTextColor(LCD_COLOR_WHITE);
00112   LCD_DisplayStringLine(LCD_LINE_2, (uint8_t *)"RIGHT : OSD Name     ");
00113   LCD_DisplayStringLine(LCD_LINE_3, (uint8_t *)"LEFT  : CEC Version  ");
00114   LCD_DisplayStringLine(LCD_LINE_4, (uint8_t *)"UP    : Volume Up    ");
00115   LCD_DisplayStringLine(LCD_LINE_5, (uint8_t *)"DOWN  : Volume Down  ");
00116 #endif /* LCD_DISPLAY */
00117 
00118   while(1)
00119   {
00120     /* If a frame has been received */
00121     while(ReceivedFrame1 == 0)
00122     {}
00123 
00124     if (ReceivedFrame1 == 1)
00125     {
00126       /* OK */
00127       if (ReceiveBuffer[1] ==0x44) /* Test on the opcode value */
00128       {
00129         /* Receive command is equal to Volume Up(Button Up) */
00130         if (ReceiveBuffer[2] == 0x41) /* Test on the operand value */
00131         {
00132           STM_EVAL_LEDOn(LED1);
00133           STM_EVAL_LEDOn(LED2);
00134           STM_EVAL_LEDOn(LED3);
00135           STM_EVAL_LEDOn(LED4);
00136         }/* Test on the operand value */
00137         else if (ReceiveBuffer[2] == 0x42) /* Receive command is equal to Volume Down(Button Down) */
00138         {
00139           STM_EVAL_LEDOff(LED1);
00140           STM_EVAL_LEDOff(LED2);
00141           STM_EVAL_LEDOff(LED3);
00142           STM_EVAL_LEDOff(LED4);
00143         }
00144       }
00145       else if (ReceiveBuffer[1] ==0x46) /* Test on the opcode value */
00146       {
00147         STM_EVAL_LEDOn(LED1);
00148         STM_EVAL_LEDOn(LED2);
00149         STM_EVAL_LEDOff(LED3);
00150         STM_EVAL_LEDOff(LED4);
00151       }
00152       else if (ReceiveBuffer[1] ==0x9F) /* Test on the opcode value */
00153       {
00154         STM_EVAL_LEDOff(LED1);
00155         STM_EVAL_LEDOff(LED2);
00156         STM_EVAL_LEDOn(LED3);
00157         STM_EVAL_LEDOn(LED4);
00158       }
00159 
00160 #ifdef LCD_DISPLAY
00161       LCD_SetBackColor(LCD_COLOR_GREEN);
00162       LCD_DisplayStringLine(LCD_LINE_7, (uint8_t *)"Receive : Succeeded ");
00163 #endif /* LCD_DISPLAY */
00164     }
00165     else
00166     {
00167       /* KO */
00168       /* Turn on LED2 */
00169       STM_EVAL_LEDOn(LED3);
00170 #ifdef LCD_DISPLAY
00171       LCD_SetBackColor(LCD_COLOR_RED);
00172       LCD_DisplayStringLine(LCD_LINE_7, (uint8_t *)"Receive : Failed   ");
00173 #endif /* LCD_DISPLAY */
00174     }
00175     ReceivedFrame1 = 0;
00176 
00177     for (rcv_inc=0;rcv_inc<10;rcv_inc++)
00178     {
00179       ReceiveBuffer[rcv_inc]=0;
00180     }
00181     rcv_inc=0;
00182   }
00183 }
00184 /**
00185   * @brief  Configures the CEC peripheral.
00186   * @param  None
00187   * @retval None
00188   */
00189 static void CEC_Config(void)
00190 {
00191   CEC_InitTypeDef CEC_InitStructure;
00192   NVIC_InitTypeDef NVIC_InitStructure;
00193   GPIO_InitTypeDef GPIO_InitStructure;
00194 
00195   /* Enable CEC clocks */
00196   RCC_APB1PeriphClockCmd(RCC_APB1Periph_CEC , ENABLE);
00197   
00198   /* Enable CEC_LINE_GPIO clocks */
00199   RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
00200 
00201   /* Configure CEC_LINE_GPIO as Output open drain */
00202 #ifdef USE_STM320518_EVAL
00203   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
00204 #else
00205   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
00206 #endif /* USE_STM320518_EVAL */  
00207   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
00208   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
00209   GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
00210   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
00211   GPIO_Init(GPIOB, &GPIO_InitStructure);
00212 
00213   /* Enable the CEC global Interrupt (with higher priority) */
00214 #ifdef USE_STM320518_EVAL
00215   NVIC_InitStructure.NVIC_IRQChannel = CEC_IRQn;
00216 #else
00217   NVIC_InitStructure.NVIC_IRQChannel = CEC_CAN_IRQn;
00218 #endif /* USE_STM320518_EVAL */
00219   NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
00220   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
00221   NVIC_Init(&NVIC_InitStructure);
00222 
00223   /* Configure CEC */
00224   CEC_InitStructure.CEC_SignalFreeTime = CEC_SignalFreeTime_Standard;
00225   CEC_InitStructure.CEC_RxTolerance = CEC_RxTolerance_Standard;
00226   CEC_InitStructure.CEC_StopReception = CEC_StopReception_Off;
00227   CEC_InitStructure.CEC_BitRisingError = CEC_BitRisingError_Off;
00228   CEC_InitStructure.CEC_LongBitPeriodError = CEC_LongBitPeriodError_Off;
00229   CEC_InitStructure.CEC_BRDNoGen = CEC_BRDNoGen_Off;
00230   CEC_InitStructure.CEC_SFTOption = CEC_SFTOption_Off;
00231   CEC_Init(&CEC_InitStructure);
00232 
00233   /* Set the CEC initiator address */
00234   CEC_OwnAddressConfig(MyLogicalAddress1);
00235 
00236   /* Activate CEC interrupts associated to the set of TX flags */
00237   CEC_ITConfig(CEC_IT_TXEND|CEC_IT_TXBR,ENABLE);
00238 
00239   /* Activate CEC interrupts associated to the set of RX flags */
00240   CEC_ITConfig(CEC_IT_RXEND|CEC_IT_RXBR,ENABLE);
00241 
00242   /* Activate CEC interrupts associated to the set of TX error */
00243   CEC_ITConfig(CEC_IT_TXACKE|CEC_IT_TXERR|CEC_IT_TXUDR|CEC_IT_ARBLST,ENABLE);
00244 
00245   /* Activate CEC interrupts associated to the set of RX error */
00246   CEC_ITConfig(CEC_IT_RXACKE|CEC_IT_LBPE|CEC_IT_SBPE|CEC_IT_BRE|CEC_IT_RXOVR,ENABLE);
00247 
00248   /* Enable CEC */
00249   CEC_Cmd(ENABLE);
00250 }
00251 
00252 
00253 #ifdef  USE_FULL_ASSERT
00254 
00255 /**
00256   * @brief  Reports the name of the source file and the source line number
00257   *         where the assert_param error has occurred.
00258   * @param  file: pointer to the source file name
00259   * @param  line: assert_param error line source number
00260   * @retval None
00261   */
00262 void assert_failed(uint8_t* file, uint32_t line)
00263 { 
00264   /* User can add his own implementation to report the file name and line number,
00265      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
00266 
00267   /* Infinite loop */
00268   while (1)
00269   {
00270   }
00271 }
00272 #endif
00273 
00274 /**
00275   * @}
00276   */
00277 
00278 /**
00279   * @}
00280   */
00281 
00282 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM32L1xx Standard Peripherals Library: Footer

 

 

 For complete documentation on STM32 Microcontrollers visit www.st.com/STM32