int main(void) { clock_setup(); usart_clock_setup(); gpio_setup(); usart_setup(); printf("\r\nstarting i2c scan.\r\n"); int i, j; for (i = 1; i < 0x80; i++) { i2c_init(); msleep(50); for (j = 0; j < 0x100; j++) { int data; // = 0; data = i2c_read(I2C3, i, j); if (data > -1) { if (data) { printf("device on address 0x%02X : reg = 0x%02X with data == 0x%02X\r\n", i, j, data); } } else { printf("errerr!!\r\n"); break; } //i2c_send_stop(I2C3); } i2c_deinit(); msleep(50); } printf("scan ended!\r\n"); return 0; }
int fc8100_i2c_deinit(HANDLE hDevice) { int res = BBM_NOK; res = i2c_deinit(hDevice); return res; }
//---------------------------------------------------------------------------------- // Close Humidity Sensor driver int CloseHMSdrv(void) //---------------------------------------------------------------------------------- { ets_timer_disarm(&test_timer); int ret = i2c_deinit(); hms_errflg = -1; // драйвер не инициализирован hms_init_flg = 0; return ret; }
STATIC void i2c_reset_after_error(I2C_HandleTypeDef *i2c) { // wait for bus-busy flag to be cleared, with a timeout for (int timeout = 50; timeout > 0; --timeout) { if (!__HAL_I2C_GET_FLAG(i2c, I2C_FLAG_BUSY)) { // stop bit was generated and bus is back to normal return; } mp_hal_delay_ms(1); } // bus was/is busy, need to reset the peripheral to get it to work again i2c_deinit(i2c); i2c_init(i2c); }
void i2c_init_freq(const pyb_i2c_obj_t *self, mp_int_t freq) { I2C_InitTypeDef *init = &self->i2c->Init; init->AddressingMode = I2C_ADDRESSINGMODE_7BIT; init->DualAddressMode = I2C_DUALADDRESS_DISABLED; init->GeneralCallMode = I2C_GENERALCALL_DISABLED; init->NoStretchMode = I2C_NOSTRETCH_DISABLE; init->OwnAddress1 = PYB_I2C_MASTER_ADDRESS; init->OwnAddress2 = 0; // unused if (freq != -1) { i2c_set_baudrate(init, MIN(freq, MICROPY_HW_I2C_BAUDRATE_MAX)); } *self->use_dma = false; // init the I2C bus i2c_deinit(self->i2c); i2c_init(self->i2c); }
int main(void) { board_init(); board_console_init(BOARD_DEBUG_BAUD); SysTick_Config(BOARD_SYSTICK_100MS); while (true) { i2c_init(&i2c_config); for (uint8_t address = 0x01; address <= 0x7f; address++) { PRINTF("Scanning for device 0x%02x: \e[K", address); status_t status = i2c_ping(address); switch (status) { case kStatus_Success: PRINTF("FOUND\r\n"); break; case kStatus_I2C_Nak: PRINTF("NOT FOUND\r"); break; case kStatus_I2C_ArbitrationLost: PRINTF("ARBITRATION LOST\r\n"); break; case kStatus_I2C_Busy: PRINTF("BUSY\r\n"); break; case kStatus_I2C_Idle: PRINTF("IDLE\r\n"); break; case kStatus_I2C_Timeout: PRINTF("TIMEOUT\r\n"); break; default: PRINTF("ERROR %0d\r", status); break; } delay(100); } i2c_deinit(); } }
/** * @brief Reset an I2C bus. * * Reset is accomplished by clocking out pulses until any hung slaves * release SDA and SCL, then generating a START condition, then a STOP * condition. * * @param dev I2C device */ void i2c_bus_reset(i2c_dev *dev) { //unsigned clock_count; //unsigned stretch_count; i2c_deinit(dev); /* Release both lines */ i2c_master_release_bus(dev); /* * Make sure the bus is free by clocking it until any slaves release the * bus. */ while (!gpio_read_bit(dev->gpio_port, dev->sda_pin)) { /* Wait for any clock stretching to finish */ while (!gpio_read_bit(dev->gpio_port, dev->scl_pin)) ; delay_us(10); /* Pull low */ gpio_write_bit(dev->gpio_port, dev->scl_pin, 0); delay_us(10); /* Release high again */ gpio_write_bit(dev->gpio_port, dev->scl_pin, 1); delay_us(10); } /* Generate start then stop condition */ gpio_write_bit(dev->gpio_port, dev->sda_pin, 0); delay_us(10); gpio_write_bit(dev->gpio_port, dev->scl_pin, 0); delay_us(10); gpio_write_bit(dev->gpio_port, dev->scl_pin, 1); delay_us(10); gpio_write_bit(dev->gpio_port, dev->sda_pin, 1); i2c_init(dev, 0, I2C_400KHz_SPEED); }
int main(int argc, char **argv) { int i, address, cmdbuf; int num_bytes = 1; unsigned int ret; char *conv_err; i2c_getopt(argc, argv); if (argc < 3) { fputs("Usage: vusb-i2cget <address> <num_bytes> [register ...] ", stderr); return 1; } address = strtol(argv[1], &conv_err, 0); if (conv_err && *conv_err) { fprintf(stderr, "address: Conversion error at '%s'\n", conv_err); return 2; } num_bytes = strtol(argv[2], &conv_err, 0); if (conv_err && *conv_err) { fprintf(stderr, "num_bytes: Conversion error at '%s'\n", conv_err); return 2; } i2c_init(); if (argc >= 3) { i2c_hw_start(); if (!i2c_hw_tx_byte((address << 1) | 0)) { fprintf(stderr, "Received NAK from slave 0x%02x, aborting\n", address); i2c_hw_stop(); i2c_deinit(); return 3; } } for (i = 3; i < argc; i++) { cmdbuf = strtol(argv[i], &conv_err, 0); if (conv_err && *conv_err) { fprintf(stderr, "read command: Conversion error at '%s'\n", conv_err); i2c_hw_stop(); i2c_deinit(); return 2; } if (!i2c_hw_tx_byte(cmdbuf)) { fprintf(stderr, "Received NAK after byte %d (0x%02x)\n", i-1, cmdbuf); i2c_hw_stop(); i2c_deinit(); return 4; } } i2c_hw_start(); if (!i2c_hw_tx_byte((address << 1) | 1)) { fprintf(stderr, "Received NAK after reSTART from slave 0x%02x, aborting\n", address); i2c_hw_stop(); i2c_deinit(); return 3; } for (i = 1; i <= num_bytes; i++) { printf("%i ", i2c_hw_rx_byte((i < num_bytes) * 1)); } putc('\n', stdout); i2c_hw_stop(); i2c_deinit(); return 0; }
/// \method deinit() /// Turn off the I2C bus. STATIC mp_obj_t pyb_i2c_deinit(mp_obj_t self_in) { pyb_i2c_obj_t *self = self_in; i2c_deinit(self->i2c); return mp_const_none; }