static void readFrom(uint8_t address, uint8_t num) { // address to read from aTxBuffer[0] = address; /*##-2- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ while(HAL_I2C_Master_Transmit_DMA(&I2cHandle, I2C_ADDRESS << 1, (uint8_t*)aTxBuffer, 1)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it�s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } // request 6 bytes from device /*##-4- Put I2C peripheral in reception process ############################*/ while(HAL_I2C_Master_Receive_DMA(&I2cHandle, I2C_ADDRESS << 1, (uint8_t *)aRxBuffer, 6) != HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /*##-5- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it�s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } }
/** * @brief Configures I2C interface * @retval HAL status */ static HAL_StatusTypeDef I2C_EXPBD_Init(void) { HAL_StatusTypeDef ret_val = HAL_OK; if(HAL_I2C_GetState(&I2C_EXPBD_Handle) == HAL_I2C_STATE_RESET) { /* I2C_EXPBD peripheral configuration */ #if ((defined (USE_STM32F4XX_NUCLEO)) || (defined (USE_STM32L1XX_NUCLEO))) I2C_EXPBD_Handle.Init.ClockSpeed = NUCLEO_I2C_EXPBD_SPEED; I2C_EXPBD_Handle.Init.DutyCycle = I2C_DUTYCYCLE_2; #endif #if (defined (USE_STM32L0XX_NUCLEO)) I2C_EXPBD_Handle.Init.Timing = NUCLEO_I2C_EXPBD_TIMING_400KHZ; /* 400KHz */ #endif I2C_EXPBD_Handle.Init.OwnAddress1 = 0x33; I2C_EXPBD_Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2C_EXPBD_Handle.Instance = NUCLEO_I2C_EXPBD; /* Init the I2C */ I2C_EXPBD_MspInit(); ret_val = HAL_I2C_Init(&I2C_EXPBD_Handle); } return ret_val; }
/** * @brief Configures I2C interface * @param None * @retval HAL status */ static HAL_StatusTypeDef I2C_SHIELDS_Init(void) { HAL_StatusTypeDef ret_val = HAL_OK; if(HAL_I2C_GetState(&I2C_SHIELDS_Handle) == HAL_I2C_STATE_RESET) { /* I2C_SHIELDS peripheral configuration */ // I2C_SHIELDS_Handle.Init.ClockSpeed = NUCLEO_I2C_SHIELDS_SPEED; // I2C_SHIELDS_Handle.Init.DutyCycle = I2C_DUTYCYCLE_2; #ifdef STM32F401xE I2C_SHIELDS_Handle.Init.ClockSpeed = NUCLEO_I2C_SHIELDS_SPEED; I2C_SHIELDS_Handle.Init.DutyCycle = I2C_DUTYCYCLE_2; #endif #ifdef STM32L053xx I2C_SHIELDS_Handle.Init.Timing = 0x0070D8FF; /*Refer AN4235-Application note Document*/ #endif I2C_SHIELDS_Handle.Init.OwnAddress1 = 0x33; I2C_SHIELDS_Handle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2C_SHIELDS_Handle.Instance = NUCLEO_I2C_SHIELDS; /* Init the I2C */ I2C_SHIELDS_MspInit(); ret_val = HAL_I2C_Init(&I2C_SHIELDS_Handle); } return ret_val; }
int initMPU(void){ int initOkay = -1; HAL_I2C_StateTypeDef state; uint8_t tempByte = 13; uint8_t buffer[10] = {0,0,0,0,0,0,0,0,0,0}; hnd.Instance = I2C1; hnd.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; hnd.Init.ClockSpeed = 400000; hnd.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; hnd.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; hnd.Init.DutyCycle = I2C_DUTYCYCLE_2; hnd.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; hnd.Init.OwnAddress1 = 0x00; HAL_I2C_Init(&hnd); __HAL_I2C_ENABLE(&hnd); state = HAL_I2C_GetState(&hnd); if(state == HAL_I2C_STATE_READY){ initOkay = 0; } buffer[0]=MPU6050_RA_PWR_MGMT_1; buffer[1]=0x80; printf("READ: %u",SCCB_Read(MPU6050_RA_WHO_AM_I)); printf("error: %u",HAL_I2C_GetError(&hnd)); return initOkay; }
void AP_ReadBuffer(uint8_t RegAddr, uint8_t *aRxBuffer, uint8_t RXBUFFERSIZE) { /* -> Lets ask for register's address */ AP_WriteBuffer(&RegAddr, 1); /* -> Put I2C peripheral in reception process */ while(HAL_I2C_Master_Receive(&AP_I2C_HANDLE, AP_I2C_ADDR, aRxBuffer, (uint16_t)RXBUFFERSIZE, (uint32_t)1000) != HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. * When Acknowledge failure occurs (Slave don't acknowledge it's address) * Master restarts communication */ if (HAL_I2C_GetError(&AP_I2C_HANDLE) != HAL_I2C_ERROR_AF) { //DEBUG(3, "In I2C::WriteBuffer -> error"); //Error_Handler(4); PUTZ_ASSERT(false); } } /* -> Wait for the end of the transfer */ /* Before starting a new communication transfer, you need to check the current * state of the peripheral; if it’s busy you need to wait for the end of current * transfer before starting a new one. * For simplicity reasons, this example is just waiting till the end of the * transfer, but application may perform other tasks while transfer operation * is ongoing. **/ while (HAL_I2C_GetState(&AP_I2C_HANDLE) != HAL_I2C_STATE_READY) { } }
void AP_WriteBuffer(uint8_t *aTxBuffer, uint8_t TXBUFFERSIZE) { /* -> Start the transmission process */ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ while(HAL_I2C_Master_Transmit(&AP_I2C_HANDLE, AP_I2C_ADDR, (uint8_t*)aTxBuffer, (uint16_t)TXBUFFERSIZE, (uint32_t)1000)!= HAL_OK) { /* * Error_Handler() function is called when Timeout error occurs. * When Acknowledge failure occurs (Slave don't acknowledge it's address) * Master restarts communication */ if (HAL_I2C_GetError(&AP_I2C_HANDLE) != HAL_I2C_ERROR_AF) { //DEBUG(3, "In I2C::WriteBuffer -> error"); //Error_Handler(3); } } /* -> Wait for the end of the transfer */ /* Before starting a new communication transfer, you need to check the current * state of the peripheral; if it’s busy you need to wait for the end of current * transfer before starting a new one. * For simplicity reasons, this example is just waiting till the end of the * transfer, but application may perform other tasks while transfer operation * is ongoing. */ while (HAL_I2C_GetState(&AP_I2C_HANDLE) != HAL_I2C_STATE_READY) { } }
/** * @brief I2C1 Bus Deinitialization * @retval None */ static void I2C1_DeInit(void) { if(HAL_I2C_GetState(&heval_I2c1) != HAL_I2C_STATE_RESET) { /* DeInit the I2C */ HAL_I2C_DeInit(&heval_I2c1); I2C1_MspDeInit(&heval_I2c1); } }
//get DATA static char getData(void) { // I2C uint8_t aRxBuffer[1]; HAL_I2C_Master_Receive_DMA(&hi2c1, (uint16_t)65, (uint8_t *)aRxBuffer, 1); while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { } return aRxBuffer[0]; }
STATIC HAL_StatusTypeDef i2c_wait_dma_finished(I2C_HandleTypeDef *i2c, uint32_t timeout) { // Note: we can't use WFI to idle in this loop because the DMA completion // interrupt may occur before the WFI. Hence we miss it and have to wait // until the next sys-tick (up to 1ms). uint32_t start = HAL_GetTick(); while (HAL_I2C_GetState(i2c) != HAL_I2C_STATE_READY) { if (HAL_GetTick() - start >= timeout) { return HAL_TIMEOUT; } } return HAL_OK; }
void i2c (){ I2C_tx_buffer[0]=(uint8_t)MODE; HAL_I2C_Master_Transmit(&hi2c1, (uint16_t) I2C_ADDRESS, &I2C_tx_buffer[0], 1,100); //HAL_Delay(10); HAL_GPIO_TogglePin(Kimenet_GPIO_Port,Kimenet_Pin); //HAL_I2C_Master_Transmit_IT(&hi2c1, (uint16_t) I2C_ADDRESS, &I2C_tx_buffer[0], 1); while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY); //waiting for the end of current transfer before starting a new one HAL_I2C_Master_Receive(&hi2c1, (uint16_t) I2C_ADDRESS, &I2C_rx_buffer[0], 1,100); while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY); //waiting for the end of current transfer for(int i=0;i<10;i++){ HAL_GPIO_TogglePin(Kimenet_GPIO_Port,Kimenet_Pin); HAL_Delay (500); } }
/** * @brief Configures I2C interface. * @param None * @retval None */ static void I2Cx_Init(void) { if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET) { /* DISCOVERY_I2Cx peripheral configuration */ I2cHandle.Init.ClockSpeed = 10000; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; I2cHandle.Init.OwnAddress1 = 0x33; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Instance = DISCOVERY_I2Cx; /* Init the I2C */ I2Cx_MspInit(); HAL_I2C_Init(&I2cHandle); } }
/** * @brief Discovery I2Cx Bus initialization * @param None * @retval None */ void I2Cbar_Init(void) { if(HAL_I2C_GetState(&I2CbarHandle) == HAL_I2C_STATE_RESET) { I2CbarHandle.Instance = DISCOVERY_I2Cbar; I2CbarHandle.Init.OwnAddress1 = BAROMETER_I2C_ADDRESS; I2CbarHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2CbarHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2CbarHandle.Init.OwnAddress2 = 0; I2CbarHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2CbarHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; /* Init the I2C */ I2Cbar_MspInit(); HAL_I2C_Init(&I2CbarHandle); } }
/** * @brief Eval I2Cx Bus initialization * @param None * @retval None */ static void I2Cx_Init(void) { if(HAL_I2C_GetState(&heval_I2c) == HAL_I2C_STATE_RESET) { heval_I2c.Instance = EVAL_I2Cx; heval_I2c.Init.Timing = EVAL_I2Cx_TIMING; heval_I2c.Init.OwnAddress1 = 0; heval_I2c.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; heval_I2c.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; heval_I2c.Init.OwnAddress2 = 0; heval_I2c.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; heval_I2c.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; /* Init the I2C */ I2Cx_MspInit(&heval_I2c); HAL_I2C_Init(&heval_I2c); } }
/** * @brief I2C Bus initialization * @param None * @retval None */ void I2C1_Init(void) { if(HAL_I2C_GetState(&heval_I2c1) == HAL_I2C_STATE_RESET) { heval_I2c1.Instance = BSP_I2C1; heval_I2c1.Init.Timing = I2C1_TIMING; heval_I2c1.Init.OwnAddress1 = 0; heval_I2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; heval_I2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; heval_I2c1.Init.OwnAddress2 = 0; heval_I2c1.Init.OwnAddress2Masks = I2C_OA2_NOMASK; heval_I2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; heval_I2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; /* Init the I2C */ I2C1_MspInit(&heval_I2c1); HAL_I2C_Init(&heval_I2c1); } }
/** * @brief I2Cx Bus initialization. */ static void I2Cx_Init(void) { if(HAL_I2C_GetState(&I2cHandle) == HAL_I2C_STATE_RESET) { I2cHandle.Instance = DISCOVERY_I2Cx; I2cHandle.Init.ClockSpeed = BSP_I2C_SPEED; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_2; I2cHandle.Init.OwnAddress1 = 0; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2cHandle.Init.OwnAddress2 = 0; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; /* Init the I2C */ I2Cx_MspInit(&I2cHandle); HAL_I2C_Init(&I2cHandle); } }
/** * @brief Main program * @param None * @retval None */ int main(void) { #ifdef MASTER_BOARD GPIO_InitTypeDef GPIO_InitStruct; #endif /* STM32F3xx HAL library initialization: - Configure the Flash prefetch - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 64 MHz */ SystemClock_Config(); /* Configure LED3 and LED3*/ BSP_LED_Init(LED3); /*##-1- Configure the I2C peripheral ######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.Timing = I2C_TIMING; I2cHandle.Init.OwnAddress1 = I2C_ADDRESS; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; I2cHandle.Init.OwnAddress2 = 0xFF; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if(HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Enable the Analog I2C Filter */ HAL_I2CEx_ConfigAnalogFilter(&I2cHandle,I2C_ANALOGFILTER_ENABLE); #ifdef MASTER_BOARD /* Configure PA.12 (Arduino D2) button */ GPIO_InitStruct.Pin = GPIO_PIN_12; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; /* Enable GPIOA clock */ __HAL_RCC_GPIOA_CLK_ENABLE(); HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Wait Until PA.12 (Arduino D2) is connected to GND */ while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_12) != GPIO_PIN_RESET) { } /* Wait Until PA.12 (Arduino D2) is de-connected from GND */ while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_12) != GPIO_PIN_SET) { } /* The board sends the message and expects to receive it back */ /*##-2- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } HAL_Delay(1000); /* Wait Until PA.12 (Arduino D2) is connected to GND */ while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_12) != GPIO_PIN_RESET) { } /* Wait Until PA.12 (Arduino D2) is de-connected from GND */ while(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_12) != GPIO_PIN_SET) { } /*##-4- Put I2C peripheral in reception process ###########################*/ while(HAL_I2C_Master_Receive_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } #else /*##-2- Enable I2C peripheral in wake up from stop mode ###################*/ HAL_I2CEx_EnableWakeUp(&I2cHandle); /*##-3- Put I2C peripheral in reception process ###########################*/ if(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } /* enter stop mode */ /* Configure the WakeUp clock source */ /* Enable Power Control clock */ __HAL_RCC_PWR_CLK_ENABLE(); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* ... STOP mode ... */ /*##-4- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-5- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ if(HAL_I2C_Slave_Transmit_IT(&I2cHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /* enter stop mode */ HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* ... STOP mode ... */ /* Disable Power Control clock */ __HAL_RCC_PWR_CLK_DISABLE(); #endif /* MASTER_BOARD */ /*##-6- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-7- Compare the sent and received buffers ##############################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE)) { /* Processing Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F3xx HAL library initialization: - Configure the Flash prefetch - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 64 MHz */ SystemClock_Config(); /* Configure LED1 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED3); /*##-1- Configure the I2C peripheral ######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.Timing = I2C_TIMING; I2cHandle.Init.OwnAddress1 = I2C_ADDRESS; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_10BIT; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; I2cHandle.Init.OwnAddress2 = 0xFF; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if(HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Enable the Analog I2C Filter */ HAL_I2CEx_ConfigAnalogFilter(&I2cHandle,I2C_ANALOGFILTER_ENABLE); #ifdef MASTER_BOARD /* Configure User push-button button */ BSP_PB_Init(BUTTON_USER,BUTTON_MODE_GPIO); /* Wait for User push-button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET) { } /* Delay to avoid that possible signal rebound is taken as button release */ HAL_Delay(50); /* Wait for User push-button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_RESET) { } /* The board sends the message and expects to receive it back */ /*##-2- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Wait for User push-button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET) { } /* Delay to avoid that possible signal rebound is taken as button release */ HAL_Delay(50); /* Wait for User push-button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_RESET) { } /*##-4- Put I2C peripheral in reception process ###########################*/ while(HAL_I2C_Master_Receive_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } #else /* The board receives the message and sends it back */ /*##-2- Put I2C peripheral in reception process ###########################*/ if(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ if(HAL_I2C_Slave_Transmit_IT(&I2cHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } #endif /* MASTER_BOARD */ /*##-5- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-6- Compare the sent and received buffers ##############################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE)) { /* Processing Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F2xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure LED1 and LED2 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); /* Configure the system clock to 120 MHz */ SystemClock_Config(); /*##-1- Configure the I2C peripheral #######################################*/ I2CxHandle.Instance = I2Cx; I2CxHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2CxHandle.Init.ClockSpeed = 400000; I2CxHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2CxHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9; I2CxHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2CxHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; I2CxHandle.Init.OwnAddress1 = I2C_ADDRESS; I2CxHandle.Init.OwnAddress2 = 0; if(HAL_I2C_Init(&I2CxHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } #ifdef MASTER_BOARD /* Configure User Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Wait for User Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 1) { } /* Wait for User Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 0) { } while(1) { /* Initialize number of data variables */ hTxNumData = TXBUFFERSIZE; hRxNumData = RXBUFFERSIZE; /* Update bTransferRequest to send buffer write request for Slave */ bTransferRequest = MASTER_REQ_WRITE; /*##-2- Master sends write request for slave #############################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-3- Master sends number of data to be written ########################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&hTxNumData, 2)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-4- Master sends aTxBuffer to slave ##################################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* Update bTransferRequest to send buffer read request for Slave */ bTransferRequest = MASTER_REQ_READ; /*##-5- Master sends read request for slave ##############################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-6- Master sends number of data to be read ###########################*/ while(HAL_I2C_Master_Transmit_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&hRxNumData, 2)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-7- Master receives aRxBuffer from slave #############################*/ while(HAL_I2C_Master_Receive_IT(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* Check correctness of received buffer ##################################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,hRxNumData)) { /* Processing Error */ Error_Handler(); } /* Flush Rx buffers */ Flush_Buffer((uint8_t*)aRxBuffer,RXBUFFERSIZE); /* Toggle LED1 */ BSP_LED_Toggle(LED1); /* This delay permit the user to see LED1 toggling */ HAL_Delay(25); } #else while(1) { /* Initialize number of data variables */ hTxNumData = 0; hRxNumData = 0; /*##-2- Slave receive request from master ################################*/ while(HAL_I2C_Slave_Receive_IT(&I2CxHandle, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* If master request write operation #####################################*/ if (bTransferRequest == MASTER_REQ_WRITE) { /*##-3- Slave receive number of data to be read ########################*/ while(HAL_I2C_Slave_Receive_IT(&I2CxHandle, (uint8_t*)&hRxNumData, 2)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-4- Slave receives aRxBuffer from master ###########################*/ while(HAL_I2C_Slave_Receive_IT(&I2CxHandle, (uint8_t*)aRxBuffer, hRxNumData)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* Check correctness of received buffer ################################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,hRxNumData)) { /* Processing Error */ Error_Handler(); } /* Flush Rx buffers */ Flush_Buffer((uint8_t*)aRxBuffer,RXBUFFERSIZE); /* Toggle LED1 */ BSP_LED_Toggle(LED1); } /* If master request write operation #####################################*/ else { /*##-3- Slave receive number of data to be written #####################*/ while(HAL_I2C_Slave_Receive_IT(&I2CxHandle, (uint8_t*)&hTxNumData, 2)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-4- Slave transmit aTxBuffer to master #############################*/ while(HAL_I2C_Slave_Transmit_IT(&I2CxHandle, (uint8_t*)aTxBuffer, RXBUFFERSIZE)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } } } #endif /* MASTER_BOARD */ }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F0xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 48 MHz */ SystemClock_Config(); /* Configure LED5, LED4 and LED3 */ BSP_LED_Init(LED5); BSP_LED_Init(LED4); BSP_LED_Init(LED3); /*##-1- Configure the I2C peripheral #######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.Timing = I2Cx_TIMING; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2cHandle.Init.OwnAddress1 = 0; I2cHandle.Init.OwnAddress2 = 0; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; if (HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* The board sends the message to EEPROM then reads it back */ /*##-2- Start writing process ##############################################*/ /* Initialize Remaining Bytes Value to TX Buffer Size */ Remaining_Bytes = TXBUFFERSIZE; /* Initialize Memory address to 0 since EEPROM write will start from address 0 */ Memory_Address = 0; /* Since page size is 4 bytes, the write procedure will be done in a loop */ while (Remaining_Bytes > 0) { /* Write 4 bytes */ if (HAL_I2C_Mem_Write_DMA(&I2cHandle, (uint16_t)I2C_ADDRESS, Memory_Address, I2C_MEMADD_SIZE_16BIT, (uint8_t *)(aTxBuffer + Memory_Address), 4) != HAL_OK) { /* Writing process Error */ Error_Handler(); } /* Wait for the end of the transfer */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Check if the EEPROM is ready for a new operation */ while (HAL_I2C_IsDeviceReady(&I2cHandle, I2C_ADDRESS, 20, 300) == HAL_TIMEOUT); /* Wait for the end of the transfer */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Update Remaining bytes and Memory Address values */ Remaining_Bytes -= 4; Memory_Address += 4; } /*##-3- Start reading process ##############################################*/ if (HAL_I2C_Mem_Read_DMA(&I2cHandle, (uint16_t)I2C_ADDRESS, 0, I2C_MEMADD_SIZE_16BIT, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Reading process Error */ Error_Handler(); } /* Wait for the end of the transfer */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Compare the sent and received buffers ##############################*/ if (Buffercmp((uint8_t *)aTxBuffer, (uint8_t *)aRxBuffer, RXBUFFERSIZE)) { /* Processing Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F3xx HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 72 MHz */ SystemClock_Config(); /* Configure LED7, LED3 and LED9*/ BSP_LED_Init(LED7); BSP_LED_Init(LED3); BSP_LED_Init(LED9); /*##-1- Configure the I2C peripheral ######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.Timing = I2C_TIMING; I2cHandle.Init.OwnAddress1 = I2C_ADDRESS; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2cHandle.Init.OwnAddress2 = 0xFF; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; if(HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Enable the Analog I2C Filter */ HAL_I2CEx_AnalogFilter_Config(&I2cHandle,I2C_ANALOGFILTER_ENABLED); #ifdef MASTER_BOARD /* Configure USER Button*/ BSP_PB_Init(BUTTON_USER, BUTTON_MODE_GPIO); /* Wait for USER Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_RESET) { } /* Wait for USER Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET) { } /* The board sends the message and expects to receive it back */ /*##-2- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Wait for USER Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_RESET) { } /* Wait for USER Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_USER) != GPIO_PIN_SET) { } /*##-4- Put I2C peripheral in reception process ###########################*/ while(HAL_I2C_Master_Receive_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } #else /*##-2- Enable I2C peripheral in wake up from stop mode ###################*/ HAL_I2CEx_EnableWakeUp(&I2cHandle); /*##-3- Put I2C peripheral in reception process ###########################*/ if(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /* enter stop mode */ /* Turn LED9 on */ BSP_LED_On(LED9); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* ... STOP mode ... */ /* Wake Up from Stop mode */ /* Turn LED9 off */ BSP_LED_Off(LED9); /*##-4- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-5- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ if(HAL_I2C_Slave_Transmit_IT(&I2cHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } /* enter stop mode */ /* Turn LED9 on */ BSP_LED_On(LED9); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); /* ... STOP mode ... */ /* Wake Up from Stop mode */ /* Turn LED9 off */ BSP_LED_Off(LED9); #endif /* MASTER_BOARD */ /*##-6- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-7- Compare the sent and received buffers ##############################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE)) { /* Processing Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32L0xx HAL library initialization: - Configure the Flash prefetch, Flash preread and Buffer caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Low Level Initialization */ HAL_Init(); /* Configure LED3 and LED4 */ BSP_LED_Init(LED3); BSP_LED_Init(LED4); /* Configure the system clock to 32 Mhz */ SystemClock_Config(); /*##-1- Configure the I2C peripheral #######################################*/ I2CxHandle.Instance = I2Cx; I2CxHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_10BIT; I2CxHandle.Init.Timing = I2C_TIMING_400KHZ; I2CxHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLED; I2CxHandle.Init.OwnAddress2Masks = I2C_OA2_NOMASK; I2CxHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLED; I2CxHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLED; I2CxHandle.Init.OwnAddress1 = I2C_ADDRESS; I2CxHandle.Init.OwnAddress2 = 0xFE; if(HAL_I2C_Init(&I2CxHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } #ifdef MASTER_BOARD /* Configure User Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Wait for User Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 1) { } /* Wait for User Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 0) { } /* The board sends the message and expects to receive it back */ /*##-2- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ while(HAL_I2C_Master_Transmit_DMA(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /* Wait for User Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 1) { } /* Wait for User Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 0) { } /*##-4- Put I2C peripheral in reception process ###########################*/ while(HAL_I2C_Master_Receive_DMA(&I2CxHandle, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Error_Handler() function is called when Timout error occurs. When Acknowledge failure ocucurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2CxHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } #else /* The board receives the message and sends it back */ /*##-2- Put I2C peripheral in reception process ###########################*/ if(HAL_I2C_Slave_Receive_DMA(&I2CxHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-4- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ if(HAL_I2C_Slave_Transmit_DMA(&I2CxHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } #endif /* MASTER_BOARD */ /*##-5- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2CxHandle) != HAL_I2C_STATE_READY) { } /*##-6- Compare the sent and received buffers ##############################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE)) { /* Processing Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { sAPP_PIXARM_READ_DATA* data; sAPP_PIXARM_READ_REQ* req; uint8_t iter = 1; HAL_StatusTypeDef stat; /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure LED4, LED5 and LED6 */ BSP_LED_Init(LED4); BSP_LED_Init(LED5); BSP_LED_Init(LED6); BSP_LED_Init(LED3); /* Configure the system clock to 168 MHz */ SystemClock_Config(); /*##-1- Configure the I2C peripheral ######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.ClockSpeed = 400000; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; I2cHandle.Init.OwnAddress1 = I2C_ADDRESS; I2cHandle.Init.OwnAddress2 = 0x0; if(HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } Test_Log_Init(); /* Configure USER Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Wait for USER Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 1) { } /* Wait for USER Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 0) { } BSP_LED_On(LED3); BSP_LED_Off(LED6); BSP_LED_Off(LED4); /* The board sends the message and expects to receive it back */ Test_Log("Starting I2C Handshake.\r\n"); /*##-2- Start the transmission process #####################################*/ /* Timeout is set to 10S */ while(HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)I2C_ADDRESS, aTxBuffer, TXBUFFERSIZE, 10000)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /*##-3- Put I2C peripheral in reception process ############################*/ /* Timeout is set to 10S */ while((stat = HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)I2C_ADDRESS, aRxBuffer, RXBUFFERSIZE, 10000)) != HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Turn LED6 on: Transfer in reception process is correct */ BSP_LED_On(LED6); /*##-4- Compare the sent and received buffers ##############################*/ //if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE)) //{ // /* Processing Error */ // Error_Handler(); //} Test_Log("Finished I2C Handshaking.\r\n"); req = (sAPP_PIXARM_READ_REQ *) bTxBuffer; data = (sAPP_PIXARM_READ_DATA *) aRxBuffer; /* Infinite loop */ while (1) { /*##-2- Start the transmission process #####################################*/ /* Timeout is set to 10S */ Test_Log("Iteration %d\r\n", iter); while((stat = HAL_I2C_Master_Transmit(&I2cHandle, (uint16_t)I2C_ADDRESS, bTxBuffer, TXBUFFERSIZE, 10000)) != HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Test_Log("Error during Transmission. Transmission Error no. %d\r\n", stat); Test_Log("Error During Transmission.\r\n"); Error_Handler(); } //Test_Log("Transmit Failed. Error no. %d. Trying again.\r\n", stat); } while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } req->rotation_absolute = 0; /*##-3- Put I2C peripheral in reception process ############################*/ /* Timeout is set to 10S */ while((stat = HAL_I2C_Master_Receive(&I2cHandle, (uint16_t)I2C_ADDRESS, aRxBuffer, RXBUFFERSIZE, 10000)) != HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Test_Log("Error during Reception. Receive Error no. %d\r\n", stat); Test_Log("Error during Reception. I2C Error no. %d\r\n", HAL_I2C_GetError(&I2cHandle)); Error_Handler(); } //Test_Log("Receive Failed. Error no. %d. Trying again.\r\n", stat); } while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } // Validate the data if (data->cmd != 0x04) { Test_Log("CMD incorrect on packet. Received %x.\r\n", data->cmd); } if (data->x_intensity != 4) { Test_Log("X incorrect on packet. Received %d.\r\n", data->x_intensity); } if (data->y_intensity != 4) { Test_Log("Y incorrect on packet. Received %d.\r\n", data->y_intensity); } if (data->z_intensity != 4) { Test_Log("Z incorrect on packet. Received %d.\r\n", data->z_intensity); } if (data->rotation_absolute != (int16_t) 36000) { Test_Log("Rotation in packet was %d.\r\n", data->rotation_absolute); Test_Log("Replying with same rotation.\r\n"); req->rotation_absolute = data->rotation_absolute; } if (data->flag != 0xFF) { Test_Log("Flag incorrect on packet. Received %x.\r\n", data->flag); } HAL_Delay(100); iter++; } }
/** * @brief EPS I2C Task * @param argument: I2C's Handle * @retval None */ static void I2C_2_Slave_Mode(void *argument) { I2C_HandleTypeDef* I2CHandle; uint8_t priTransferDataBuffer[50]; uint8_t* default_string = "012345678996543210"; I2CHandle = (I2C_HandleTypeDef*) argument; prvNewPrintString("EPS I2C Task\n",13); for(;;) { //prvNewPrintString("Hello",5); /*##-2- Slave receive request from master ################################*/ while(HAL_I2C_Slave_Receive_IT(I2CHandle, priTransferDataBuffer, TRANSFERBUFFERSIZE)!= HAL_OK) vTaskDelay(1); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it? busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(I2CHandle) != HAL_I2C_STATE_READY) { vTaskDelay(1); //prvNewPrintString("Chuck",5); } if(priI2C_NewCommand_2==1) { priI2C_NewCommand_2 =0; //get command number and command arguments from master device I2C_transfer_slave_2.priCommand = priTransferDataBuffer[0]; I2C_transfer_slave_2.priRequestPtr = priTransferDataBuffer+1; switch(I2C_transfer_slave_2.priCommand) { case 0x08: I2C_transfer_slave_2.priErrorcode = 0; //reply data I2C_transfer_slave_2.priSizeofReply = 2+43; priTransferDataBuffer[0] = I2C_transfer_slave_2.priCommand; priTransferDataBuffer[1] = I2C_transfer_slave_2.priErrorcode; memcpy( (void *)(priTransferDataBuffer+2), (void *)&hk_test, 43); break; case 0x09: output_test.output_byte = *(I2C_transfer_slave_2.priRequestPtr); //print result //HAL_UART_Transmit(&UartHandle, &output_test, 1 , 1000); /* Flush Tx/Rx buffers */ Flush_Buffer(priTransferDataBuffer,TRANSFERBUFFERSIZE); break; default: priTransferDataBuffer[0] = 0xff; priTransferDataBuffer[1] = 0xEE; memcpy( (void *)(priTransferDataBuffer+2), (void *)default_string, 18); } } } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F103xG HAL library initialization: - Configure the Flash prefetch - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization */ HAL_Init(); /* Configure the system clock to 72 MHz */ SystemClock_Config(); /* Configure LED1 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED3); /*##-1- Configure the I2C peripheral ######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.ClockSpeed = I2C_SPEEDCLOCK; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE; I2cHandle.Init.OwnAddress1 = I2C_ADDRESS; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; I2cHandle.Init.OwnAddress2 = 0xFF; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if(HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } #ifdef MASTER_BOARD /* Configure Key push-button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Wait for Key push-button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != GPIO_PIN_RESET) { } /* Wait for Key push-button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != GPIO_PIN_SET) { } while(1) { /* Initialize number of data variables */ hTxNumData = TXBUFFERSIZE; hRxNumData = RXBUFFERSIZE; /* Update bTransferRequest to send buffer write request for Slave */ bTransferRequest = MASTER_REQ_WRITE; /*##-2- Master sends write request for slave #############################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-3- Master sends number of data to be written ########################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&hTxNumData, 2)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Master sends aTxBuffer to slave ##################################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Update bTransferRequest to send buffer read request for Slave */ bTransferRequest = MASTER_REQ_READ; /*##-5- Master sends read request for slave ##############################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-6- Master sends number of data to be read ###########################*/ while(HAL_I2C_Master_Transmit_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)&hRxNumData, 2)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-7- Master receives aRxBuffer from slave #############################*/ while(HAL_I2C_Master_Receive_IT(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aRxBuffer, RXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge its address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Check correctness of received buffer ##################################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,hRxNumData)) { /* Processing Error */ Error_Handler(); } /* Flush Rx buffers */ Flush_Buffer((uint8_t*)aRxBuffer,RXBUFFERSIZE); /* Toggle LED1 */ BSP_LED_Toggle(LED1); /* This delay permits to see LED1 toggling */ HAL_Delay(25); } #else while(1) { /* Initialize number of data variables */ hTxNumData = 0; hRxNumData = 0; /*##-2- Slave receive request from master ################################*/ while(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { } /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* If master request write operation #####################################*/ if (bTransferRequest == MASTER_REQ_WRITE) { /*##-3- Slave receive number of data to be read ########################*/ while(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t*)&hRxNumData, 2)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Slave receives aRxBuffer from master ###########################*/ while(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t*)aRxBuffer, hRxNumData)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Check correctness of received buffer ################################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,hRxNumData)) { /* Processing Error */ Error_Handler(); } /* Flush Rx buffers */ Flush_Buffer((uint8_t*)aRxBuffer,RXBUFFERSIZE); /* Toggle LED1 */ BSP_LED_Toggle(LED1); } /* If master request write operation #####################################*/ else { /*##-3- Slave receive number of data to be written #####################*/ while(HAL_I2C_Slave_Receive_IT(&I2cHandle, (uint8_t*)&hTxNumData, 2)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Slave transmit aTxBuffer to master #############################*/ while(HAL_I2C_Slave_Transmit_IT(&I2cHandle, (uint8_t*)aTxBuffer, RXBUFFERSIZE)!= HAL_OK); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } } } #endif /* MASTER_BOARD */ }
/** * @brief INMS I2C Task * @param argument: I2C's Handle * @retval None */ static void I2C_2_Slave_Mode(void *argument) { I2C_HandleTypeDef* I2CHandle; uint8_t priTransferDataBuffer[TRANSFERBUFFERSIZE]; uint8_t* default_string = "012345678996543210"; I2CHandle = (I2C_HandleTypeDef*) argument; prvNewPrintString("ADCS I2C Task\n",13); for(;;) { //prvNewPrintString("Hello",5); /*##-2- Slave receive request from master ################################*/ while(HAL_I2C_Slave_Receive_IT(I2CHandle, priTransferDataBuffer, TRANSFERBUFFERSIZE)!= HAL_OK) vTaskDelay(1); /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it? busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(I2CHandle) != HAL_I2C_STATE_READY) { vTaskDelay(1); } if(priI2C_NewCommand_2==1) { priI2C_NewCommand_2 =0; //get command number and command arguments from master device I2C_transfer_slave_2.priCommand = priTransferDataBuffer[0]; I2C_transfer_slave_2.priRequestPtr = priTransferDataBuffer+1; /* the format of register output to master device. It can call interface format of subsystm */ switch(I2C_transfer_slave_2.priCommand) { /* Channel 1 INMS 5V current*/ case 0xb0: I2C_transfer_slave_2.priSizeofReply = 4; memset(priTransferDataBuffer,I2C_transfer_slave_2.priCommand,TRANSFERBUFFERSIZE); priTransferDataBuffer[0] = (ifb_test.IFB_5V_Current & 0xff00)>>8; priTransferDataBuffer[1] = ifb_test.IFB_5V_Current & 0xff; priTransferDataBuffer[2] = 0xff; break; /* Channel 0 INMS 3_3V current*/ case 0x90: I2C_transfer_slave_2.priSizeofReply = 4; memset(priTransferDataBuffer,I2C_transfer_slave_2.priCommand,TRANSFERBUFFERSIZE); priTransferDataBuffer[0] = (ifb_test.IFB_3_3V_Current & 0xff00)>>8; priTransferDataBuffer[1] = ifb_test.IFB_3_3V_Current & 0xff; priTransferDataBuffer[2] = 0xff; break; /* Channel 2 INMS INMS temperature*/ case 0xd0: I2C_transfer_slave_2.priSizeofReply = 4; memset(priTransferDataBuffer,I2C_transfer_slave_2.priCommand,TRANSFERBUFFERSIZE); priTransferDataBuffer[0] = (ifb_test.IFB_INMS_Temp & 0xff00)>>8; priTransferDataBuffer[1] = ifb_test.IFB_INMS_Temp & 0xff; priTransferDataBuffer[2] = 0xff; break; /* Channel 3 INMS INMS Interface Board temperature*/ case 0xf0: I2C_transfer_slave_2.priSizeofReply = 4; memset(priTransferDataBuffer,I2C_transfer_slave_2.priCommand,TRANSFERBUFFERSIZE); priTransferDataBuffer[0] = (ifb_test.IFB_IFB_Temp & 0xff00)>>8; priTransferDataBuffer[1] = ifb_test.IFB_IFB_Temp & 0xff; priTransferDataBuffer[2] = 0xff; break; default: /* send a default string to remind user*/ memcpy( (void *)(priTransferDataBuffer), (void *)default_string, 18); } } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Systick timer is configured by default as source of time base, but user can eventually implement his proper time base source (a general purpose timer for example or other time source), keeping in mind that Time base duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and handled in milliseconds basis. - Set NVIC Group Priority to 4 - Low Level Initialization: global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure the system clock to 180 MHz */ SystemClock_Config(); /* Configure LED1, LED2 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); /*##-1- Configure the I2C peripheral #######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.ClockSpeed = 400000; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; I2cHandle.Init.OwnAddress1 = 0x00; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT; I2cHandle.Init.OwnAddress2 = 0x00; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; if (HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* The board sends the message to EEPROM then reads it back */ /*##-2- Start writing process ##############################################*/ /* Initialize Remaining Bytes Value to TX Buffer Size */ Remaining_Bytes = TXBUFFERSIZE; /* Initialize Memory address to 0 since EEPROM write will start from address 0 */ Memory_Address = 0; /* Since page size is 4 bytes, the write procedure will be done in a loop */ while (Remaining_Bytes > 0) { /* Write EEPROM_PAGESIZE */ if(HAL_I2C_Mem_Write_DMA(&I2cHandle , (uint16_t)EEPROM_ADDRESS, Memory_Address, I2C_MEMADD_SIZE_16BIT, (uint8_t*)(aTxBuffer + Memory_Address), EEPROM_PAGESIZE)!= HAL_OK) { /* Writing process Error */ Error_Handler(); } /* Wait for the end of the transfer */ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Check if the EEPROM is ready for a new operation */ while (HAL_I2C_IsDeviceReady(&I2cHandle, EEPROM_ADDRESS, EEPROM_MAX_TRIALS, I2Cx_TIMEOUT_MAX) == HAL_TIMEOUT); /* Wait for the end of the transfer */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Update Remaining bytes and Memory Address values */ Remaining_Bytes -= EEPROM_PAGESIZE; Memory_Address += EEPROM_PAGESIZE; } /*##-3- Start reading process ##############################################*/ if (HAL_I2C_Mem_Read_DMA(&I2cHandle, (uint16_t)EEPROM_ADDRESS, 0, I2C_MEMADD_SIZE_16BIT, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Reading process Error */ Error_Handler(); } /* Wait for the end of the transfer */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Compare the sent and received buffers ##############################*/ if (Buffercmp((uint8_t *)aTxBuffer, (uint8_t *)aRxBuffer, RXBUFFERSIZE)) { /* Processing Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F4xx HAL library initialization: - Configure the Flash prefetch, instruction and Data caches - Configure the Systick to generate an interrupt each 1 msec - Set NVIC Group Priority to 4 - Global MSP (MCU Support Package) initialization */ HAL_Init(); /* Configure LED4, LED5 and LED6 */ BSP_LED_Init(LED4); BSP_LED_Init(LED5); BSP_LED_Init(LED6); /* Configure the system clock to 84 MHz */ SystemClock_Config(); /*##-1- Configure the I2C peripheral ######################################*/ I2cHandle.Instance = I2Cx; I2cHandle.Init.AddressingMode = I2C_ADDRESSINGMODE_10BIT; I2cHandle.Init.ClockSpeed = 400000; I2cHandle.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE; I2cHandle.Init.DutyCycle = I2C_DUTYCYCLE_16_9; I2cHandle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; I2cHandle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; I2cHandle.Init.OwnAddress1 = I2C_ADDRESS; I2cHandle.Init.OwnAddress2 = 0xFE; if(HAL_I2C_Init(&I2cHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } #ifdef MASTER_BOARD /* Configure User Button */ BSP_PB_Init(BUTTON_KEY, BUTTON_MODE_GPIO); /* Wait for User Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 1) { } /* Wait for User Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 0) { } /* The board sends the message and expects to receive it back */ /*##-2- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ while(HAL_I2C_Master_Transmit_DMA(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /* Wait for User Button press before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 1) { } /* Wait for User Button release before starting the Communication */ while (BSP_PB_GetState(BUTTON_KEY) != 0) { } /*##-4- Put I2C peripheral in reception process ############################*/ while(HAL_I2C_Master_Receive_DMA(&I2cHandle, (uint16_t)I2C_ADDRESS, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Error_Handler() function is called when Timeout error occurs. When Acknowledge failure occurs (Slave don't acknowledge it's address) Master restarts communication */ if (HAL_I2C_GetError(&I2cHandle) != HAL_I2C_ERROR_AF) { Error_Handler(); } } #else /* The board receives the message and sends it back */ /*##-2- Put I2C peripheral in reception process ############################*/ if(HAL_I2C_Slave_Receive_DMA(&I2cHandle, (uint8_t *)aRxBuffer, RXBUFFERSIZE) != HAL_OK) { /* Transfer error in reception process */ Error_Handler(); } /*##-3- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-4- Start the transmission process #####################################*/ /* While the I2C in reception process, user can transmit data through "aTxBuffer" buffer */ if(HAL_I2C_Slave_Transmit_DMA(&I2cHandle, (uint8_t*)aTxBuffer, TXBUFFERSIZE)!= HAL_OK) { /* Transfer error in transmission process */ Error_Handler(); } #endif /* MASTER_BOARD */ /*##-5- Wait for the end of the transfer ###################################*/ /* Before starting a new communication transfer, you need to check the current state of the peripheral; if it’s busy you need to wait for the end of current transfer before starting a new one. For simplicity reasons, this example is just waiting till the end of the transfer, but application may perform other tasks while transfer operation is ongoing. */ while (HAL_I2C_GetState(&I2cHandle) != HAL_I2C_STATE_READY) { } /*##-6- Compare the sent and received buffers ##############################*/ if(Buffercmp((uint8_t*)aTxBuffer,(uint8_t*)aRxBuffer,RXBUFFERSIZE)) { /* Processing Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); /* USER CODE BEGIN 2 */ /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* Initialize number of data variables */ hTxNumData = 0; hRxNumData = 0; /*##-2- Slave receive request from master ################################*/ while(HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t*)&bTransferRequest, 1)!= HAL_OK) { } while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { } /* If master request write operation #####################################*/ if (bTransferRequest == MASTER_REQ_WRITE) { /*##-3- Slave receive number of data to be read ########################*/ while(HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t*)&hRxNumData, 2)!= HAL_OK); while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { } /*##-4- Slave receives aRxBuffer from master ###########################*/ while(HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t*)aRxBuffer, hRxNumData)!= HAL_OK); while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { } } else { /*##-3- Slave receive number of data to be written #####################*/ while(HAL_I2C_Slave_Receive_IT(&hi2c1, (uint8_t*)&hTxNumData, 2)!= HAL_OK); while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { } /*##-4- Slave transmit aTxBuffer to master #############################*/ while(HAL_I2C_Slave_Transmit_IT(&hi2c1, (uint8_t*)aTxBuffer, RXBUFFERSIZE)!= HAL_OK); while (HAL_I2C_GetState(&hi2c1) != HAL_I2C_STATE_READY) { } } /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }