void main() { RCC_ClocksTypeDef RCC_Clocks; RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); LED_Config(); LWIP_Config(); UDP_Config(); CAN_Config(); TIM7_Config(PERIOD); while(1) { if(ETH_CheckFrameReceived()) { LwIP_Pkt_Handle(); } LwIP_Periodic_Handle(LocalTime); if(rew_status != 0) ReSendUdpData(); } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32xxx_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32xxx.c file */ /* Initialize LEDs, Key Button, LCD and COM port(USART) available on STM3210X-EVAL board ******************************************************/ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Initialize TIM6 */ TIM6_Config(); /* Initialize the LCD */ #ifdef USE_STM322xG_EVAL STM322xG_LCD_Init(); #elif defined USE_STM324xG_EVAL STM324xG_LCD_Init(); #elif defined USE_STM3210C_EVAL STM3210C_LCD_Init(); #elif defined USE_STM32100E_EVAL STM32100E_LCD_Init(); #elif defined USE_STM32L152_EVAL STM32L152_LCD_Init(); #elif defined USE_STM32L152D_EVAL STM32L152D_LCD_Init(); #endif /* Display message on STM3210X-EVAL LCD *************************************/ /* Clear the LCD */ LCD_Clear(White); /* Set the LCD Back Color */ LCD_SetBackColor(Blue); /* Set the LCD Text Color */ LCD_SetTextColor(Yellow); LCD_DisplayStringLine(Line0, MESSAGE1); LCD_DisplayStringLine(Line1, MESSAGE2); LCD_DisplayStringLine(Line5, MESSAGE3); /* Configure the Push buttons in interrupt mode *****************************/ #if defined (USE_STM32100E_EVAL) || defined(USE_STM3210C_EVAL) || defined(USE_STM322xG_EVAL ) || defined (USE_STM324xG_EVAL) STM_EVAL_PBInit(BUTTON_KEY, Mode_EXTI); STM_EVAL_PBInit(BUTTON_TAMPER, Mode_EXTI); #elif defined (USE_STM32L152_EVAL) || defined (USE_STM32L152D_EVAL) STM_EVAL_PBInit(BUTTON_LEFT, Mode_EXTI); STM_EVAL_PBInit(BUTTON_RIGHT, Mode_EXTI); #endif /* USE_STM32100E_EVAL || USE_STM3210C_EVAL || USE_STM322xG_EVAL || USE_STM324xG_EVAL */ /* Start CPAL communication configuration ***********************************/ /* Initialize local Reception structures */ sRxStructure.wNumData = BufferSize; /* Maximum Number of data to be received */ sRxStructure.pbBuffer = tRxBuffer; /* Common Rx buffer for all received data */ sRxStructure.wAddr1 = OWN_ADDRESS; /* The own board address */ sRxStructure.wAddr2 = 0; /* Not needed */ /* Initialize local Transmission structures */ sTxStructure.wNumData = BufferSize; /* Maximum Number of data to be received */ sTxStructure.pbBuffer = (uint8_t*)tStateSignal; /* Common Rx buffer for all received data */ sTxStructure.wAddr1 = OWN_ADDRESS; /* The own board address */ sTxStructure.wAddr2 = 0; /* Not needed */ /* Configure the device structure */ CPAL_I2C_StructInit(&I2C_DevStructure); /* Set all fields to default values */ I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE; #ifdef CPAL_I2C_DMA_PROGMODEL I2C_DevStructure.wCPAL_Options = CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_I2C_NACK_ADD; I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_DMA; #elif defined (CPAL_I2C_IT_PROGMODEL) I2C_DevStructure.wCPAL_Options = CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_I2C_NACK_ADD; I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT; #else #error "Please select one of the programming model (in main.h)" #endif I2C_DevStructure.pCPAL_I2C_Struct->I2C_ClockSpeed = I2C_SPEED; I2C_DevStructure.pCPAL_I2C_Struct->I2C_OwnAddress1 = OWN_ADDRESS; I2C_DevStructure.pCPAL_TransferRx = &sRxStructure; I2C_DevStructure.pCPAL_TransferTx = &sTxStructure; /* Initialize CPAL device with the selected parameters */ CPAL_I2C_Init(&I2C_DevStructure); /* Infinite loop */ while (1) { /* Write operations ------------------------------------------------------*/ /* Check if any action has been triggered by push buttons */ if ((ActionState != ACTION_PENDING) && (ActionState != ACTION_NONE)) { /* Check if the current CPAL device state allows write operation */ if (((DeviceMode == SLAVE) && (LastMode == SLAVE))||\ (((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) ||\ (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) && (DeviceMode == MASTER))) { if (LastMode == SLAVE) { /* Set the LCD Back Color */ LCD_SetBackColor(Red); /* Set the LCD Text Color */ LCD_SetTextColor(White); LCD_DisplayStringLine(Line3, (uint8_t*)" MASTER MODE ACTIVE "); /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Set the LCD Text Color */ LCD_SetTextColor(Blue); /* Disable CPAL_OPT_I2C_NACK_ADD option when switch to Master mode */ I2C_DevStructure.wCPAL_Options &= (~CPAL_OPT_I2C_NACK_ADD); } LastMode = MASTER; /* Initialize local Reception structures */ sRxStructure.wNumData = BufferSize; /* Initialize local Transmission structures */ sTxStructure.wNumData = BufferSize; switch (ActionState) { case BUTTON_KEY: TransmitMode = STATE_ON; sTxStructure.pbBuffer = (uint8_t*)tSignal; Divider = 2; break; case BUTTON_TAMPER: TransmitMode = STATE_OFF; sRxStructure.pbBuffer = tRxBuffer; Divider = 4; break; case ACTION_PERIODIC: if(TransmitMode == STATE_ON) { sTxStructure.pbBuffer = (uint8_t*)tStateSignal; } break; default: break; } /* Configure the device mode to master */ I2C_DevStructure.CPAL_Mode = CPAL_MODE_MASTER; /* Force the CPAL state to ready (in case a read operation has been initiated) */ I2C_DevStructure.CPAL_State = CPAL_STATE_READY; /* Prevent other actions to be performed while the current is not finished */ ActionState = ACTION_PENDING; DeviceMode = MASTER; /* Configure a Timer to generate periodic interrupt: used to send state signal */ TIM7_Config(PeriodicValue/Divider); if(TransmitMode == STATE_ON) { /* Start writing data in master mode */ if (CPAL_I2C_Write(&I2C_DevStructure) == CPAL_PASS) { } } else { /* Start reading data in master mode */ if (CPAL_I2C_Read(&I2C_DevStructure) == CPAL_PASS) { } } } } /* Read Operations -------------------------------------------------------*/ if (((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \ (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) && \ (DeviceMode == SLAVE)) { /* Reconfigure device for slave receiver mode */ I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE; I2C_DevStructure.CPAL_State = CPAL_STATE_READY; if (LastMode == SLAVE) { /* Set the LCD Back Color */ LCD_SetBackColor(Red); /* Set the LCD Text Color */ LCD_SetTextColor(White); LCD_DisplayStringLine(Line3, (uint8_t*)" SLAVE MODE ACTIVE "); /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Set the LCD Text Color */ LCD_SetTextColor(Blue); } /* Start waiting for data to be received in slave mode */ if (CPAL_I2C_Listen(&I2C_DevStructure) == CPAL_PASS) { LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY); } } } }