void i2c_init(i2c_t *obj, PinName sda, PinName scl) { // Determine the I2C to use I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C clock if (obj->i2c == I2C_1) { __I2C1_CLK_ENABLE(); } if (obj->i2c == I2C_2) { __I2C2_CLK_ENABLE(); } if (obj->i2c == I2C_3) { __I2C3_CLK_ENABLE(); } // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); // Reset to clear pending flags if any i2c_reset(obj); // I2C configuration i2c_frequency(obj, 100000); // 100 kHz per default }
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) { GPIO_InitTypeDef GPIO_InitStruct; if(hi2c->Instance==I2C3) { /* USER CODE BEGIN I2C3_MspInit 0 */ /* USER CODE END I2C3_MspInit 0 */ /**I2C3 GPIO Configuration PH8 ------> I2C3_SDA PH7 ------> I2C3_SCL */ GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_7; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_I2C3; HAL_GPIO_Init(GPIOH, &GPIO_InitStruct); /* Peripheral clock enable */ __I2C3_CLK_ENABLE(); /* USER CODE BEGIN I2C3_MspInit 1 */ /* USER CODE END I2C3_MspInit 1 */ } }
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) { GPIO_InitTypeDef GPIO_InitStruct; if(hi2c->Instance==I2C3) { /* Peripheral clock enable */ __I2C3_CLK_ENABLE(); /**I2C3 GPIO Configuration PC9 ------> I2C3_SDA PA8 ------> I2C3_SCL */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF4_I2C3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; GPIO_InitStruct.Alternate = GPIO_AF4_I2C3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); } }
void i2c_init(i2c_t *obj, PinName sda, PinName scl) { static int i2c1_inited = 0; static int i2c2_inited = 0; #if defined(I2C3_BASE) static int i2c3_inited = 0; #endif // Determine the I2C to use I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C1 clock and pinout if not done if ((obj->i2c == I2C_1) && !i2c1_inited) { i2c1_inited = 1; __HAL_RCC_I2C1_CONFIG(RCC_I2C1CLKSOURCE_SYSCLK); __I2C1_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } // Enable I2C2 clock and pinout if not done if ((obj->i2c == I2C_2) && !i2c2_inited) { i2c2_inited = 1; __I2C2_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } #if defined(I2C3_BASE) // Enable I2C3 clock and pinout if not done if ((obj->i2c == I2C_3) && !i2c3_inited) { i2c3_inited = 1; __I2C3_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } #endif // Reset to clear pending flags if any i2c_reset(obj); // I2C configuration i2c_frequency(obj, 100000); // 100 kHz per default }
void i2c_init(i2c_t *obj, PinName sda, PinName scl) { // Determine the I2C to use I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA); I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL); obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl); MBED_ASSERT(obj->i2c != (I2CName)NC); // Enable I2C1 clock and pinout if not done if ((obj->i2c == I2C_1) && !i2c1_inited) { i2c1_inited = 1; __I2C1_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } // Enable I2C2 clock and pinout if not done if ((obj->i2c == I2C_2) && !i2c2_inited) { i2c2_inited = 1; __I2C2_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } // Enable I2C3 clock and pinout if not done if ((obj->i2c == I2C_3) && !i2c3_inited) { i2c3_inited = 1; __I2C3_CLK_ENABLE(); // Configure I2C pins pinmap_pinout(sda, PinMap_I2C_SDA); pinmap_pinout(scl, PinMap_I2C_SCL); pin_mode(sda, OpenDrain); pin_mode(scl, OpenDrain); } // Reset to clear pending flags if any i2c_reset(obj); // I2C configuration i2c_frequency(obj, 100000); // 100 kHz per default // I2C master by default obj->slave = 0; }
void HAL_I2C_MspInit( I2C_HandleTypeDef *hi2c ) { GPIO_InitTypeDef gio; if( hi2c->Instance == I2C3 ) { __I2C3_CLK_ENABLE(); __GPIOA_CLK_ENABLE(); __GPIOC_CLK_ENABLE(); // A8 --> I2C3_SCL // C9 --> I2C3_SDA gio.Pin = GPIO_PIN_8; gio.Mode = GPIO_MODE_AF_OD; gio.Pull = GPIO_NOPULL; gio.Speed = GPIO_SPEED_MAX; gio.Alternate = GPIO_AF4_I2C3; HAL_GPIO_Init( GPIOA, &gio ); gio.Pin = GPIO_PIN_9; HAL_GPIO_Init( GPIOC, &gio ); // tmp: not now // HAL_NVIC_SetPriority( I2C3_EV_IRQn, 15, 0 ); // TODO: from FreeRTOS // HAL_NVIC_EnableIRQ( I2C3_EV_IRQn ); } }
void i2c_init(I2C_HandleTypeDef *i2c) { int i2c_unit; const pin_obj_t *scl_pin; const pin_obj_t *sda_pin; if (0) { #if defined(MICROPY_HW_I2C1_SCL) } else if (i2c == &I2CHandle1) { i2c_unit = 1; scl_pin = &MICROPY_HW_I2C1_SCL; sda_pin = &MICROPY_HW_I2C1_SDA; __I2C1_CLK_ENABLE(); #endif #if defined(MICROPY_HW_I2C2_SCL) } else if (i2c == &I2CHandle2) { i2c_unit = 2; scl_pin = &MICROPY_HW_I2C2_SCL; sda_pin = &MICROPY_HW_I2C2_SDA; __I2C2_CLK_ENABLE(); #endif #if defined(MICROPY_HW_I2C3_SCL) } else if (i2c == &I2CHandle3) { i2c_unit = 3; scl_pin = &MICROPY_HW_I2C3_SCL; sda_pin = &MICROPY_HW_I2C3_SDA; __I2C3_CLK_ENABLE(); #endif #if defined(MICROPY_HW_I2C4_SCL) } else if (i2c == &I2CHandle4) { i2c_unit = 4; scl_pin = &MICROPY_HW_I2C4_SCL; sda_pin = &MICROPY_HW_I2C4_SDA; __I2C3_CLK_ENABLE(); #endif } else { // I2C does not exist for this board (shouldn't get here, should be checked by caller) return; } // init the GPIO lines uint32_t mode = MP_HAL_PIN_MODE_ALT_OPEN_DRAIN; uint32_t pull = MP_HAL_PIN_PULL_NONE; // have external pull-up resistors on both lines mp_hal_pin_config_alt(scl_pin, mode, pull, AF_FN_I2C, i2c_unit); mp_hal_pin_config_alt(sda_pin, mode, pull, AF_FN_I2C, i2c_unit); // init the I2C device if (HAL_I2C_Init(i2c) != HAL_OK) { // init error // TODO should raise an exception, but this function is not necessarily going to be // called via Python, so may not be properly wrapped in an NLR handler printf("OSError: HAL_I2C_Init failed\n"); return; } // invalidate the DMA channels so they are initialised on first use const pyb_i2c_obj_t *self = &pyb_i2c_obj[i2c_unit - 1]; dma_invalidate_channel(self->tx_dma_descr); dma_invalidate_channel(self->rx_dma_descr); if (0) { #if defined(MICROPY_HW_I2C1_SCL) } else if (i2c->Instance == I2C1) { HAL_NVIC_EnableIRQ(I2C1_EV_IRQn); HAL_NVIC_EnableIRQ(I2C1_ER_IRQn); #endif #if defined(MICROPY_HW_I2C2_SCL) } else if (i2c->Instance == I2C2) { HAL_NVIC_EnableIRQ(I2C2_EV_IRQn); HAL_NVIC_EnableIRQ(I2C2_ER_IRQn); #endif #if defined(MICROPY_HW_I2C3_SCL) } else if (i2c->Instance == I2C3) { HAL_NVIC_EnableIRQ(I2C3_EV_IRQn); HAL_NVIC_EnableIRQ(I2C3_ER_IRQn); #endif #if defined(MICROPY_HW_I2C4_SCL) } else if (i2c->Instance == I2C4) { HAL_NVIC_EnableIRQ(I2C4_EV_IRQn); HAL_NVIC_EnableIRQ(I2C4_ER_IRQn); #endif } }
void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c) { GPIO_InitTypeDef GPIO_InitStruct; if(hi2c->Instance==I2C1) { /* USER CODE BEGIN I2C1_MspInit 0 */ /* USER CODE END I2C1_MspInit 0 */ /**I2C1 GPIO Configuration PB8 ------> I2C1_SCL PB9 ------> I2C1_SDA */ GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_I2C1; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); /* Peripheral clock enable */ __I2C1_CLK_ENABLE(); /* Peripheral DMA init*/ hdma_i2c1_rx.Instance = DMA1_Stream5; hdma_i2c1_rx.Init.Channel = DMA_CHANNEL_1; hdma_i2c1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_i2c1_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_i2c1_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_i2c1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_i2c1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_i2c1_rx.Init.Mode = DMA_NORMAL; hdma_i2c1_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH; hdma_i2c1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_i2c1_rx); __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c1_rx); hdma_i2c1_tx.Instance = DMA1_Stream6; hdma_i2c1_tx.Init.Channel = DMA_CHANNEL_1; hdma_i2c1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_i2c1_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_i2c1_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_i2c1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_i2c1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_i2c1_tx.Init.Mode = DMA_NORMAL; hdma_i2c1_tx.Init.Priority = DMA_PRIORITY_LOW; hdma_i2c1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_i2c1_tx); __HAL_LINKDMA(hi2c,hdmatx,hdma_i2c1_tx); /* USER CODE BEGIN I2C1_MspInit 1 */ /* USER CODE END I2C1_MspInit 1 */ } else if(hi2c->Instance==I2C3) { /* USER CODE BEGIN I2C3_MspInit 0 */ /* USER CODE END I2C3_MspInit 0 */ /**I2C3 GPIO Configuration PC9 ------> I2C3_SDA PA8 ------> I2C3_SCL */ GPIO_InitStruct.Pin = GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_I2C3; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); GPIO_InitStruct.Pin = GPIO_PIN_8; GPIO_InitStruct.Mode = GPIO_MODE_AF_OD; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; GPIO_InitStruct.Alternate = GPIO_AF4_I2C3; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* Peripheral clock enable */ __I2C3_CLK_ENABLE(); /* Peripheral DMA init*/ hdma_i2c3_tx.Instance = DMA1_Stream4; hdma_i2c3_tx.Init.Channel = DMA_CHANNEL_3; hdma_i2c3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; hdma_i2c3_tx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_i2c3_tx.Init.MemInc = DMA_MINC_ENABLE; hdma_i2c3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_i2c3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_i2c3_tx.Init.Mode = DMA_NORMAL; hdma_i2c3_tx.Init.Priority = DMA_PRIORITY_MEDIUM; hdma_i2c3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_i2c3_tx); __HAL_LINKDMA(hi2c,hdmatx,hdma_i2c3_tx); hdma_i2c3_rx.Instance = DMA1_Stream2; hdma_i2c3_rx.Init.Channel = DMA_CHANNEL_3; hdma_i2c3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; hdma_i2c3_rx.Init.PeriphInc = DMA_PINC_DISABLE; hdma_i2c3_rx.Init.MemInc = DMA_MINC_ENABLE; hdma_i2c3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma_i2c3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma_i2c3_rx.Init.Mode = DMA_NORMAL; hdma_i2c3_rx.Init.Priority = DMA_PRIORITY_MEDIUM; hdma_i2c3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; HAL_DMA_Init(&hdma_i2c3_rx); __HAL_LINKDMA(hi2c,hdmarx,hdma_i2c3_rx); /* USER CODE BEGIN I2C3_MspInit 1 */ /* USER CODE END I2C3_MspInit 1 */ } }