void mBusInit(void) { RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); RCC_I2CCLKConfig(RCC_I2C2CLK_SYSCLK); CPAL_I2C_StructInit(&mBusStruct); mBusStruct.CPAL_Dev = CPAL_I2C1; mBusStruct.CPAL_Direction = CPAL_DIRECTION_TXRX; mBusStruct.CPAL_Mode = CPAL_MODE_MASTER; mBusStruct.CPAL_ProgModel = CPAL_PROGMODEL_DMA; mBusStruct.pCPAL_TransferTx = &mBusTx; mBusStruct.pCPAL_TransferRx = &mBusRx; mBusStruct.wCPAL_Options = CPAL_OPT_I2C_AUTOMATIC_END; mBusStruct.pCPAL_I2C_Struct->I2C_Timing = I2C_Timing_FastMode; mBusStruct.pCPAL_I2C_Struct->I2C_AnalogFilter = I2C_AnalogFilter_Enable; mBusStruct.pCPAL_I2C_Struct->I2C_DigitalFilter = 0x00; mBusStruct.pCPAL_I2C_Struct->I2C_Mode = I2C_Mode_I2C; mBusStruct.pCPAL_I2C_Struct->I2C_OwnAddress1 = 0x00; mBusStruct.pCPAL_I2C_Struct->I2C_Ack = I2C_Ack_Enable; mBusStruct.pCPAL_I2C_Struct->I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; CPAL_I2C_Init(&mBusStruct); }
/** * @brief User callback that manages the Timeout error * @param pDevInitStruct * @retval None. */ uint32_t CPAL_TIMEOUT_UserCallback(CPAL_InitTypeDef* pDevInitStruct) { /* Update CPAL states */ pDevInitStruct->CPAL_State = CPAL_STATE_READY; pDevInitStruct->wCPAL_DevError = CPAL_I2C_ERR_NONE ; pDevInitStruct->wCPAL_Timeout = CPAL_I2C_TIMEOUT_DEFAULT; /* DeInitialize CPAL device */ CPAL_I2C_DeInit(pDevInitStruct); /* Initialize CPAL device with the selected parameters */ CPAL_I2C_Init(pDevInitStruct); /* Switch the LCD write color */ Switch_ErrorColor(); LCD_DisplayStringLine(Line9, (uint8_t*)" Timeout Recovered "); ActionState = ACTION_NONE; return CPAL_PASS; }
/** * @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_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* Configure Clocks */ RCC_Config(); /* Initialize LEDs, Key Button and LCD available on STM320518-EVAL board *****************************************************/ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED2); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); /* Initialize the LCD */ STM320518_LCD_Init(); /* Display message on 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); /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Set the LCD Text Color */ LCD_SetTextColor(Blue); /* Configure the Push buttons in Polling mode */ STM_EVAL_PBInit(BUTTON_KEY, Mode_GPIO); /* if STM32 device is set as Master */ #ifdef I2C_MASTER /* Configure and enable the systick timer to generate an interrupt each 1 ms */ SysTick_Config((SystemCoreClock / 1000)); /* Deinitialize I2Cx Device */ CPAL_I2C_DeInit(&MASTERSTRUCTURE); /* Initialize CPAL I2C structure parameters values */ CPAL_I2C_StructInit(&MASTERSTRUCTURE); #ifdef CPAL_I2C_DMA_PROGMODEL MASTERSTRUCTURE.wCPAL_Options = CPAL_OPT_NO_MEM_ADDR | CPAL_OPT_DMATX_TCIT; MASTERSTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_DMA; #elif defined (CPAL_I2C_IT_PROGMODEL) MASTERSTRUCTURE.wCPAL_Options = CPAL_OPT_NO_MEM_ADDR; MASTERSTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT; #else #error "Please select one of the programming model (in main.h)" #endif /* Set I2C Speed */ MASTERSTRUCTURE.pCPAL_I2C_Struct->I2C_Timing = MASTER_I2C_TIMING; /* Select Master Mode */ MASTERSTRUCTURE.CPAL_Mode = CPAL_MODE_MASTER; /* Initialize I2Cx Device*/ CPAL_I2C_Init(&MASTERSTRUCTURE); /* Infinite loop */ while(1) { /* Initialize Transfer parameters */ MASTERSTRUCTURE.pCPAL_TransferTx = &sTxStructure; sTxStructure.wNumData = BufferSize; sTxStructure.pbBuffer = (uint8_t*)BufferTX; sTxStructure.wAddr1 = OWNADDRESS; /* Update LCD Display */ LCD_SetBackColor(White); LCD_SetTextColor(Blue); LCD_DisplayStringLine(Line8, MEASSAGE_EMPTY); LCD_DisplayStringLine(Line5, MESSAGE4); LCD_DisplayStringLine(Line6, MESSAGE5); /* wait until Key button is pushed */ while(STM_EVAL_PBGetState(BUTTON_KEY)); /* Update LCD Display */ LCD_DisplayStringLine(Line5, MEASSAGE_EMPTY); LCD_DisplayStringLine(Line6, MEASSAGE_EMPTY); /* Write operation */ CPAL_I2C_Write(&MASTERSTRUCTURE); /* Wait until communication finishes */ while ((MASTERSTRUCTURE.CPAL_State != CPAL_STATE_READY) && (MASTERSTRUCTURE.CPAL_State != CPAL_STATE_ERROR)); if (TransferStatus == PASSED) { /* Update LCD Display */ LCD_SetBackColor(Red); LCD_SetTextColor(White); LCD_DisplayStringLine(Line8, MESSAGE6); } else { TransferStatus = PASSED; } Delay(1000); } #endif /* I2C_MASTER */ /* if STM32 device is set as Slave */ #ifdef I2C_SLAVE /* GPIOA Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); /* Output System Clock on MCO pin (PA.08) */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_MCOConfig(RCC_MCOSource_SYSCLK); /* Deinitialize I2Cx Device */ CPAL_I2C_DeInit(&SLAVESTRUCTURE); /* Initialize CPAL I2C structure parameters values */ CPAL_I2C_StructInit(&SLAVESTRUCTURE); #ifdef CPAL_I2C_DMA_PROGMODEL SLAVESTRUCTURE.wCPAL_Options = CPAL_OPT_I2C_NACK_ADD | CPAL_OPT_I2C_WAKEUP_STOP | CPAL_OPT_DMARX_TCIT; SLAVESTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_DMA; #elif defined (CPAL_I2C_IT_PROGMODEL) SLAVESTRUCTURE.wCPAL_Options = CPAL_OPT_I2C_NACK_ADD | CPAL_OPT_I2C_WAKEUP_STOP; SLAVESTRUCTURE.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT; #else #error "Please select one of the programming model (in main.h)" #endif /* Configure Own address 1 */ SLAVESTRUCTURE.pCPAL_I2C_Struct->I2C_OwnAddress1 = OWNADDRESS; /* Set I2C Speed */ SLAVESTRUCTURE.pCPAL_I2C_Struct->I2C_Timing = SLAVE_I2C_TIMING; /* Select Slave Mode */ SLAVESTRUCTURE.CPAL_Mode = CPAL_MODE_SLAVE; /* Initialize I2Cx Device*/ CPAL_I2C_Init(&SLAVESTRUCTURE); /* Infinite loop */ while(1) { /* Reset BufferRX value */ Reset_bBuffer(BufferRX, (uint16_t)BufferSize); /* Initialize Transfer parameters */ SLAVESTRUCTURE.pCPAL_TransferRx = &sRxStructure; sRxStructure.wNumData = BufferSize; sRxStructure.pbBuffer = (uint8_t*)BufferRX; /* Update LCD Display */ LCD_SetBackColor(White); LCD_SetTextColor(Blue); LCD_DisplayStringLine(Line8, MEASSAGE_EMPTY); LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY); LCD_DisplayStringLine(Line5, MESSAGE7); Delay(1000); /* Update LCD Display */ LCD_DisplayStringLine(Line5, MEASSAGE_EMPTY); LCD_DisplayStringLine(Line6, MESSAGE8); /* Read operation */ CPAL_I2C_Read(&SLAVESTRUCTURE); /* Enter Stop Mode and wait for interrupt to wake up */ PWR_EnterSTOPMode(PWR_Regulator_LowPower, PWR_STOPEntry_WFI); /* Wait until communication finishes */ while ((SLAVESTRUCTURE.CPAL_State != CPAL_STATE_READY) && (SLAVESTRUCTURE.CPAL_State != CPAL_STATE_ERROR)); /* Configure SystemClock*/ RestoreConfiguration(); /* Configure and enable the systick timer to generate an interrupt each 1 ms */ SysTick_Config((SystemCoreClock / 1000)); /* Update LCD Display */ LCD_DisplayStringLine(Line6, MEASSAGE_EMPTY); LCD_SetBackColor(Red); LCD_SetTextColor(White); LCD_DisplayStringLine(Line8, MESSAGE9); /* If are received correctly */ if (Compare_bBuffer((uint8_t*)BufferTX, BufferRX, BufferSize) == PASSED ) { /* Update LCD Display */ LCD_DisplayStringLine(Line9, MESSAGE6); } else { /* Update LCD Display */ LCD_DisplayStringLine(Line9, MESSAGE10); } Delay(1500); } #endif /* I2C_SLAVE */ }
/** * @brief Initialize peripherals used by the I2C EEPROM driver. * @param sEEInitStruct : Pointer to sEE Device structure * @retval None */ void sEE_Init(sEE_InitTypeDef * sEEInitStruct) { /* Initialize CPAL peripheral */ CPAL_I2C_Init(sEEInitStruct->sEE_CPALStructure); }
/** * @brief Initializes the LM75_I2C. * @param None * @retval None */ static void LM75_Init(void) { /* Initialize CPAL peripheral */ CPAL_I2C_Init(&LM75_DevStructure); }
/** * @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_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* Initialize LEDs and LCD available on STM320518-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 */ STM320518_LCD_Init(); /* Display message on 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(Line3, MESSAGE3); /* Set the LCD Back Color */ LCD_SetBackColor(White); /* Set the LCD Text Color */ LCD_SetTextColor(Blue); /* Configure the Push buttons in interrupt mode *****************************/ STM_EVAL_PBInit(BUTTON_KEY, Mode_EXTI); STM_EVAL_PBInit(BUTTON_TAMPER, Mode_EXTI); /* 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 = 0; /* Not needed */ 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 */ /* Set SYSCLK as I2C clock source */ RCC_I2CCLKConfig(RCC_I2C1CLK_SYSCLK); /* 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_DMATX_TCIT | CPAL_OPT_DMARX_TCIT; I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_DMA; #elif defined (CPAL_I2C_IT_PROGMODEL) I2C_DevStructure.wCPAL_Options = CPAL_OPT_NO_MEM_ADDR; I2C_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_INTERRUPT; #else #error "Please select one of the programming model (in stm32f0xx_i2c_cpal_conf.h)" #endif I2C_DevStructure.pCPAL_I2C_Struct->I2C_Timing = I2C_TIMING; 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 ((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \ (I2C_DevStructure.CPAL_State == CPAL_STATE_BUSY_RX) ||\ (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) { /* Initialize local Transmission structures */ sTxStructure.wNumData = BufferSize; /* Maximum Number of data to be received */ sTxStructure.wAddr1 = OWN_ADDRESS; /* The own board address */ sTxStructure.wAddr2 = 0; /* Not needed */ switch (ActionState) { case BUTTON_KEY: sTxStructure.pbBuffer = (uint8_t*)tSignal1; Divider = 1; break; case BUTTON_TAMPER: sTxStructure.pbBuffer = (uint8_t*)tSignal2; Divider = 2; break; case ACTION_PERIODIC: sTxStructure.pbBuffer = (uint8_t*)tStateSignal; break; default: sTxStructure.pbBuffer = (uint8_t*)tSignal1; 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; TransmitMode = STATE_ON; /* Configure a Timer to generate periodic interrupt: used to send state signal */ TIM17_Config(PeriodicValue/Divider); /* Start writing data in master mode */ if (CPAL_I2C_Write(&I2C_DevStructure) == CPAL_PASS) { } } } /* Read Operations -------------------------------------------------------*/ if (((I2C_DevStructure.CPAL_State == CPAL_STATE_READY) || \ (I2C_DevStructure.CPAL_State == CPAL_STATE_DISABLED)) && \ (TransmitMode == STATE_OFF)) { /* Initialize local Reception structures */ sRxStructure.wNumData = BufferSize; /* Maximum Number of data to be received */ sRxStructure.pbBuffer = tRxBuffer; /* Common Rx buffer for all received data */ /* Reconfigure device for slave receiver mode */ I2C_DevStructure.CPAL_Mode = CPAL_MODE_SLAVE; I2C_DevStructure.CPAL_State = CPAL_STATE_READY; /* Start waiting for data to be received in slave mode */ if (CPAL_I2C_Read(&I2C_DevStructure) == CPAL_PASS) { LCD_DisplayStringLine(Line9, MEASSAGE_EMPTY); } } } }
static void MPU9150_Init(void) { /* Initialize CPAL peripheral */ CPAL_I2C_Init(&MPU9150_DevStructure); }
int main() { GPIO_InitTypeDef GPIO_InitStructure; EXTI_InitTypeDef EXTI_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; LED_Init(); LED_R_ON(); CPAL_I2C_StructInit(&I2C1_DevStructure); I2C1_DevStructure.CPAL_Dev = CPAL_I2C1; I2C1_DevStructure.pCPAL_I2C_Struct->I2C_ClockSpeed = 400000; I2C1_DevStructure.CPAL_ProgModel = CPAL_PROGMODEL_DMA; I2C1_DevStructure.wCPAL_Options = 0; CPAL_I2C_Init(&I2C1_DevStructure); // Interrupt input(PB5) configuration RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource5); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); EXTI_InitStructure.EXTI_Line = EXTI_Line5; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x0F; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); LLIO_Init(115200); printf("\r\nmyPressure I2C example\r\n"); // myPressure의 WHO_AM_I 레지스터 값을 읽어 출력한다. // read 결과값은 등록된 user callback을 통해 받아온다. buff.pbBuffer = i2c_buff; buff.wAddr1 = PRESSURE_SLAVE_ADDRESS << 1; buff.wAddr2 = PRESSURE_REG_WHO_AM_I; buff.wNumData = 1; I2C1_DevStructure.pCPAL_TransferRx = &buff; printf("\r\n\r\nread : %d\r\n", CPAL_I2C_Read(&I2C1_DevStructure)); while(1) { __WFI(); // 인터럽트가 발생할 때 까지 이곳에서 대기 } }
/** * @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); } } } }