static int accel_init(void) { i2c_init(i2c); i2c_master_enable(i2c, 0); return 0; }
/*---------------------------------------------------------------------------*/ void i2c_master_interface_init(void) { if (!initialized) { struct i2c_master_config config_i2c_master; /* Toggle SCL for some time, this solves I2C periperhal problem */ struct port_config pin_conf; port_get_config_defaults(&pin_conf); pin_conf.direction = PORT_PIN_DIR_OUTPUT; port_pin_set_config(PIN_PA13, &pin_conf); for (int i = 0; i < 1000; i++) { port_pin_toggle_output_level(PIN_PA13); clock_wait(CLOCK_SECOND / 1000); } /* Initialize config structure and software module. */ i2c_master_get_config_defaults(&config_i2c_master); config_i2c_master.baud_rate = 400; config_i2c_master.pinmux_pad0 = SENSORS_I2C_SERCOM_PINMUX_PAD0; config_i2c_master.pinmux_pad1 = SENSORS_I2C_SERCOM_PINMUX_PAD1; /* Initialize and enable device with config. */ i2c_master_init(&i2c_master_instance, SENSORS_I2C_MODULE, &config_i2c_master); i2c_master_enable(&i2c_master_instance); initialized = true; } }
void TwoWire::setClock(uint32_t frequencyHz) { if (frequencyHz<=40000) { switch(frequencyHz) { case 1000000: dev_flags &= ~(I2C_FAST_MODE); dev_flags |= I2C_FAST_MODE_PLUS;// set FAST_MODE bit break; case 400000: dev_flags &= ~(I2C_FAST_MODE_PLUS); dev_flags |= I2C_FAST_MODE;// set FULL_SPEED bit break; case 100000: default: dev_flags &= ~(I2C_FAST_MODE | I2C_FAST_MODE_PLUS);// clear FAST_MODE and FULL_SPEED bits break; } if (sel_hard->regs->CR1 & I2C_CR1_PE){ i2c_disable(sel_hard); i2c_master_enable(sel_hard, dev_flags); } } else { uint32 hz = (frequencyHz > 1200000) ? 1200000 : frequencyHz; i2c_overclock(sel_hard, hz); } }
//! [initialize_i2c] void configure_EEPROM(void) { pinMode(WP, OUTPUT); PinSet(WP); /* Initialize config structure and software module. */ //! [init_conf] struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); //! [init_conf] /* Change buffer timeout to something longer. */ //! [conf_change] config_i2c_master.buffer_timeout = 10000; config_i2c_master.generator_source=GCLK_GENERATOR_0; config_i2c_master.pinmux_pad0=PINMUX_PA12C_SERCOM2_PAD0; config_i2c_master.pinmux_pad1=PINMUX_PA13C_SERCOM2_PAD1; //! [conf_change] /* Initialize and enable device with config. */ //! [init_module] i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master); /*BAUD=GCLK0freq(48Mhz)*Thigh /i.e SCLfreq=GCLK/(2*Baud)*/ REG_SERCOM2_I2CM_BAUD=200; //force baud rate after above setting gives around 100Khz //! [enable_module] i2c_master_enable(&i2c_master_instance); //! [enable_module] }
uint8 HardWire::process() { int8 res = i2c_master_xfer(sel_hard, &itc_msg, 1, 0); if (res != 0) { i2c_disable(sel_hard); i2c_master_enable(sel_hard, (I2C_BUS_RESET | dev_flags)); } return 0; }
/*---------------------------------------------------------------------------*/ void bme280_arch_i2c_read_mem(uint8_t addr, uint8_t reg, uint8_t *buf, uint8_t bytes) { i2c_master_enable(); if(i2c_single_send(addr, reg) == I2C_MASTER_ERR_NONE) { while(i2c_master_busy()); i2c_burst_receive(addr, buf, bytes); } }
void i2c_module_init() { msgs.data = buf; i2c_master_enable(I2C1, I2C_FAST_MODE | I2C_SLAVE_GENERAL_CALL | I2C_SLAVE_USE_RX_BUFFER); i2c_slave_attach_recv_handler(I2C1, pmsg, funcrx); i2c_slave_attach_transmit_handler(I2C1, pmsg, functx); i2c_slave_set_own_address(I2C1, 0x10); //banks_read_byte(POS_PID_BANK, REG_TWI_ADDRESS)); //i2c_slave_set_own_address2(I2C1, 0x0020); }
/*---------------------------------------------------------------------------*/ void bme280_arch_i2c_write_mem(uint8_t addr, uint8_t reg, uint8_t value) { uint8_t buf[2]; buf[0] = reg; buf[1] = value; i2c_master_enable(); i2c_burst_send(addr, buf, 2); }
void i2c_init(void) { i2c_mutex = thinkos_mutex_alloc(); // tracef("I2C mutex=%d\n", i2c_mutex); thinkos_mutex_lock(i2c_mutex); i2c_master_init(100000); i2c_master_enable(); thinkos_mutex_unlock(i2c_mutex); }
/** * \brief Initialize EDBG I2C communication for SAM0 * */ bool adp_interface_init(void) { enum status_code return_value; system_init(); struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); config_i2c_master.buffer_timeout = 10000; return_value = i2c_master_init(&i2c_master_instance, EDBG_TWI, &config_i2c_master); i2c_master_enable(&i2c_master_instance); return return_value; }
/** * \internal * \brief Test for I2C master initialization. * * This test initializes the i2c master module and checks whether the * initialization is successful or not. * * \param test Current test case. */ static void run_i2c_init_test(const struct test_case *test) { enum status_code status; struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); config_i2c_master.buffer_timeout = 10000; status = i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master); /* Check for successful initialization */ test_assert_true(test, status == STATUS_OK, "I2C master initialization failed"); i2c_master_enable(&i2c_master_instance); }
void configure_i2c_master(void) { /* initialize config structure and software module */ struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); /* change buffer timeout to something longer */ config_i2c_master.buffer_timeout = 10000; /* initialize and enable device with config. */ i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master); i2c_master_enable(&i2c_master_instance); }
void TwoWire::begin(void) { rxBufferIndex = 0; rxBufferLength = 0; txBufferIndex = 0; txBufferLength = 0; i2c_master_enable(dev,I2C_BUS_RESET); //twi_init(); }
/* * @fn nm_bus_init * @brief Initialize the bus wrapper * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure */ sint8 nm_bus_init(void *pvinit) { sint8 result = M2M_SUCCESS; #ifdef CONF_WINC_USE_I2C /* Initialize config structure and software module. */ struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); /* Change buffer timeout to something longer. */ config_i2c_master.buffer_timeout = 1000; /* Initialize and enable device with config. */ i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master); i2c_master_enable(&i2c_master_instance); #elif defined CONF_WINC_USE_SPI /* Structure for SPI configuration. */ struct spi_config config; struct spi_slave_inst_config slave_config; /* Select SPI slave CS pin. */ /* This step will set the CS high */ spi_slave_inst_get_config_defaults(&slave_config); slave_config.ss_pin = CONF_WINC_SPI_CS_PIN; spi_attach_slave(&slave_inst, &slave_config); /* Configure the SPI master. */ spi_get_config_defaults(&config); config.mux_setting = CONF_WINC_SPI_SERCOM_MUX; config.pinmux_pad0 = CONF_WINC_SPI_PINMUX_PAD0; config.pinmux_pad1 = CONF_WINC_SPI_PINMUX_PAD1; config.pinmux_pad2 = CONF_WINC_SPI_PINMUX_PAD2; config.pinmux_pad3 = CONF_WINC_SPI_PINMUX_PAD3; config.master_slave_select_enable = false; config.mode_specific.master.baudrate = CONF_WINC_SPI_CLOCK; if (spi_init(&master, CONF_WINC_SPI_MODULE, &config) != STATUS_OK) { return M2M_ERR_BUS_FAIL; } /* Enable the SPI master. */ spi_enable(&master); nm_bsp_reset(); nm_bsp_sleep(1); #endif return result; }
/** * Configure I2C master. */ static void configure_i2c_master(void) { struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); config_i2c_master.buffer_timeout = 10000; config_i2c_master.pinmux_pad0 = BOOT_I2C_PAD0; config_i2c_master.pinmux_pad1 = BOOT_I2C_PAD1; i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master); i2c_master_enable(&i2c_master_instance); }
/** * \brief Function for configuring I2C master module * * This function will configure the I2C master module with * the SERCOM module to be used and pinmux settings */ static void configure_i2c_master(void) { /* Create and initialize config structure */ struct i2c_master_config config_i2c; i2c_master_get_config_defaults(&config_i2c); /* Change pins */ config_i2c.pinmux_pad0 = EDBG_I2C_SERCOM_PINMUX_PAD0; config_i2c.pinmux_pad1 = EDBG_I2C_SERCOM_PINMUX_PAD1; /* Initialize and enable device with config */ i2c_master_init(&i2c_master_instance, EDBG_I2C_MODULE, &config_i2c); i2c_master_enable(&i2c_master_instance); }
/*---------------------------------------------------------------------------*/ static uint16_t sht25_read_reg(uint8_t reg, uint8_t *buf, uint8_t num) { if((buf == NULL) || (num <= 0)) { return SHT25_ERROR; } i2c_master_enable(); if(i2c_single_send(SHT25_ADDR, reg) == I2C_MASTER_ERR_NONE) { if(i2c_burst_receive(SHT25_ADDR, buf, num) == I2C_MASTER_ERR_NONE) { return SHT25_SUCCESS; } } return SHT25_ERROR; }
void configure_i2c_master(void){ /* Initialize config structure and software module. */ struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); config_i2c_master.buffer_timeout = 10000; config_i2c_master.pinmux_pad0 = PINMUX_PA16C_SERCOM1_PAD0; config_i2c_master.pinmux_pad1 = PINMUX_PA17C_SERCOM1_PAD1; /* Initialize and enable device with config. */ i2c_master_init(&i2c_master_instance, SERCOM1, &config_i2c_master); //i2c_master_reset(&i2c_master_instance); i2c_master_enable(&i2c_master_instance); }
uint8_t TwoWire::process() { int8 res = i2c_master_xfer(sel_hard, itc_msg, itc_msg_count, 0); if (res == I2C_ERROR_PROTOCOL) { if (sel_hard->error_flags & I2C_SR1_AF) { /* NACK */ res = (sel_hard->error_flags & I2C_SR1_ADDR ? ENACKADDR : ENACKTRNS); } else if (sel_hard->error_flags & I2C_SR1_OVR) { /* Over/Underrun */ res = EDATA; } else { /* Bus or Arbitration error */ res = EOTHER; } i2c_disable(sel_hard); i2c_master_enable(sel_hard, (I2C_BUS_RESET | dev_flags)); } return res; }
static void eeprom_test_setup(void) { int i; i2c_master_enable(I2C1, I2C_BUS_RESET); buffer_r = (uint8 *) zalloc(BUFFER_SIZE); os_assert(buffer_r != NULL); buffer_w = (uint8 *) zalloc(BUFFER_SIZE + 1); os_assert(buffer_w != NULL); buffer_w[0] = 0x0; for (i = 1; i < BUFFER_SIZE + 1; i++) buffer_w[i] = i - 1; os_log(LOG_DEBUG, "buffers are ready: r: 0x%x, w: 0x%x\n", buffer_r, buffer_w); }
/*---------------------------------------------------------------------------*/ void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed) { /* Enable I2C clock in different modes */ REG(SYS_CTRL_RCGCI2C) |= 1; /* Run mode */ /* Reset I2C peripheral */ REG(SYS_CTRL_SRI2C) |= 1; /* Reset position */ /* Delay for a little bit */ clock_delay_usec(50); REG(SYS_CTRL_SRI2C) &= ~1; /* Normal position */ /* Set pins in input */ GPIO_SET_INPUT(GPIO_PORT_TO_BASE(port_sda), GPIO_PIN_MASK(pin_sda)); GPIO_SET_INPUT(GPIO_PORT_TO_BASE(port_scl), GPIO_PIN_MASK(pin_scl)); /* Set peripheral control for the pins */ GPIO_PERIPHERAL_CONTROL(GPIO_PORT_TO_BASE(port_sda), GPIO_PIN_MASK(pin_sda)); GPIO_PERIPHERAL_CONTROL(GPIO_PORT_TO_BASE(port_scl), GPIO_PIN_MASK(pin_scl)); /* Set the pad to no drive type */ ioc_set_over(port_sda, pin_sda, IOC_OVERRIDE_DIS); ioc_set_over(port_scl, pin_scl, IOC_OVERRIDE_DIS); /* Set pins as peripheral inputs */ REG(IOC_I2CMSSDA) = ioc_input_sel(port_sda, pin_sda); REG(IOC_I2CMSSCL) = ioc_input_sel(port_scl, pin_scl); /* Set pins as peripheral outputs */ ioc_set_sel(port_sda, pin_sda, IOC_PXX_SEL_I2C_CMSSDA); ioc_set_sel(port_scl, pin_scl, IOC_PXX_SEL_I2C_CMSSCL); /* Enable the I2C master module */ i2c_master_enable(); /* t the master clock frequency */ i2c_set_frequency(bus_speed); }
//! [initialize_i2c] void configure_i2c_master(void) { /* Initialize config structure and software module. */ //! [init_conf] struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); //! [init_conf] /* Change buffer timeout to something longer. */ //! [conf_change] config_i2c_master.buffer_timeout = 10000; //! [conf_change] /* Initialize and enable device with config. */ //! [init_module] i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master); //! [init_module] //! [enable_module] i2c_master_enable(&i2c_master_instance); //! [enable_module] }
void EEP_Reset() { i2c_master_disable(&i2c_master_instance); pinMux(SCL, Disablemux); pinMux(SDA, Disablemux); pinMode(SDA,INPUT); for (uint8_t i=0;i<9; i++) { PinSet(SCL); delay_us(10); if(PinRead(SDA)) { break; } PinClr(SCL); } pinMux(SCL, CSERCOMmux); pinMode(SDA,OUTPUT); pinMux(SDA, CSERCOMmux); i2c_master_enable(&i2c_master_instance); I2C_STATUS|=(1<<4); }
int main(void) { int16 acc[3]; int16 gyro[3]; int16 mag[3]; int16 temperature = 0; int32 pressure = 0; int32 centimeters = 0; i2c_master_enable(I2C1, I2C_FAST_MODE); delay(200); initAcc(); delay(200); initGyro(); delay(200); zeroCalibrateGyroscope(128,5); compassInit(false); compassCalibrate(1); compassSetMode(0); bmp085Calibration(); while(1) { getAccelerometerData(acc); //Read acceleration SerialUSB.print("Accel: "); SerialUSB.print(acc[0]); SerialUSB.print(" "); SerialUSB.print(acc[1]); SerialUSB.print(" "); SerialUSB.print(acc[2]); getGyroscopeData(gyro); //Read acceleration SerialUSB.print(" Gyro: "); SerialUSB.print(gyro[0]); SerialUSB.print(" "); SerialUSB.print(gyro[1]); SerialUSB.print(" "); SerialUSB.print(gyro[2]); getMagnetometerData(mag); //Read acceleration SerialUSB.print(" Mag: "); SerialUSB.print(mag[0]); SerialUSB.print(" "); SerialUSB.print(mag[1]); SerialUSB.print(" "); SerialUSB.print(mag[2]); temperature = bmp085GetTemperature(bmp085ReadUT()); pressure = bmp085GetPressure(bmp085ReadUP()); centimeters = bmp085GetAltitude(); SerialUSB.print(" Temp: "); SerialUSB.print(temperature, DEC); SerialUSB.print(" *0.1 deg C "); SerialUSB.print("Pressure: "); SerialUSB.print(pressure, DEC); SerialUSB.print(" Pa "); SerialUSB.print("Altitude: "); SerialUSB.print(centimeters, DEC); SerialUSB.print(" cm "); SerialUSB.println(" "); delay(100); } return 0; }
/* * @fn nm_bus_init * @brief Initialize the bus wrapper * @return M2M_SUCCESS in case of success and M2M_ERR_BUS_FAIL in case of failure */ sint8 nm_bus_init(void *pvinit) { sint8 result = M2M_SUCCESS; #ifdef CONF_WINC_USE_I2C /* Initialize config structure and software module. */ struct i2c_master_config config_i2c_master; i2c_master_get_config_defaults(&config_i2c_master); /* Change buffer timeout to something longer. */ config_i2c_master.buffer_timeout = 1000; /* Initialize and enable device with config. */ i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master); i2c_master_enable(&i2c_master_instance); #elif defined CONF_WINC_USE_SPI // hspi1.Instance = SPI1; // hspi1.Init.Mode = SPI_MODE_MASTER; // hspi1.Init.Direction = SPI_DIRECTION_2LINES; // hspi1.Init.DataSize = SPI_DATASIZE_8BIT; // hspi1.Init.CLKPolarity = SPI_POLARITY_LOW; // hspi1.Init.CLKPhase = SPI_PHASE_1EDGE; // hspi1.Init.NSS = SPI_NSS_SOFT; // hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4; // hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; // hspi1.Init.TIMode = SPI_TIMODE_DISABLED; // hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED; // hspi1.Init.CRCPolynomial = 10; /* Structure for SPI configuration. */ // struct spi_config config; // struct spi_slave_inst_config slave_config; // /* Select SPI slave CS pin. */ // /* This step will set the CS high */ // spi_slave_inst_get_config_defaults(&slave_config); // slave_config.ss_pin = CONF_WINC_SPI_CS_PIN; // spi_attach_slave(&slave_inst, &slave_config); // /* Configure the SPI master. */ // spi_get_config_defaults(&config); // config.mux_setting = CONF_WINC_SPI_SERCOM_MUX; // config.pinmux_pad0 = CONF_WINC_SPI_PINMUX_PAD0; // config.pinmux_pad1 = CONF_WINC_SPI_PINMUX_PAD1; // config.pinmux_pad2 = CONF_WINC_SPI_PINMUX_PAD2; // config.pinmux_pad3 = CONF_WINC_SPI_PINMUX_PAD3; // config.master_slave_select_enable = false; // config.mode_specific.master.baudrate = CONF_WINC_SPI_CLOCK; // if (spi_init(&master, CONF_WINC_SPI_MODULE, &config) != STATUS_OK) { // return M2M_ERR_BUS_FAIL; // } // /* Enable the SPI master. */ // spi_enable(&master); nm_bsp_reset(); nm_bsp_sleep(1); #endif return result; }
void HardWire::begin(uint8 self_addr) { i2c_master_enable(sel_hard, dev_flags); }
/** * @brief Initialize an I2C device as slave (and master) * @param dev Device to enable * @param flags Bitwise or of the following I2C options: * I2C_FAST_MODE: 400 khz operation, * I2C_DUTY_16_9: 16/9 Tlow/Thigh duty cycle (only applicable for * fast mode), * I2C_BUS_RESET: Reset the bus and clock out any hung slaves on * initialization, * I2C_10BIT_ADDRESSING: Enable 10-bit addressing, * I2C_REMAP: (deprecated, STM32F1 only) Remap I2C1 to SCL/PB8 * SDA/PB9. * I2C_SLAVE_DUAL_ADDRESS: Slave can respond on 2 i2C addresses * I2C_SLAVE_GENERAL_CALL: SLA+W broadcast to all general call * listeners on bus. Addr 0x00 * I2C_SLAVE_USE_RX_BUFFER: Use a buffer to receive the incoming * data. Callback at end of recv * I2C_SLAVE_USE_TX_BUFFER: Use a buffer to transmit data. * Callback will be called before tx */ void i2c_slave_enable(i2c_dev *dev, uint32 flags) { i2c_disable(dev); i2c_master_enable(dev, dev->config_flags | flags); }
void TwoWire::begin(uint8_t self_addr) { if (!_init) { i2c_master_enable(sel_hard, dev_flags); _init = true; } }
/** * \internal * \brief Test for I2C master transfer. * * First test transfer function with stop. * write to slave, read from slave and then compare the data. * the slave send out the data it received, * so master write and read data should be the same. * * Then test transfer function without stop. * write to slave, then use i2c_master_send_stop to complete writing, * read from slave, compare the data. * finally, use function with stop to complete the transfer. * * \param test Current test case. */ static void run_i2c_master_transfer_test(const struct test_case *test) { uint32_t timeout_cycles = 1000; uint32_t i; bool status = true; uint8_t read_buffer[DATA_LENGTH] = {0}; struct i2c_master_packet packet = { .address = SLAVE_ADDRESS, .data_length = DATA_LENGTH, .data = write_buffer, .ten_bit_address = false, .high_speed = false, .hs_master_code = 0x0, }; /* with stop function: master transfer test */ /* wait the master write to complete */ do { timeout_cycles--; if (i2c_master_write_packet_wait(&i2c_master_instance, &packet) == STATUS_OK) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "i2c master write failed"); /* wait the master read to complete */ packet.data = read_buffer; timeout_cycles = 1000; do { timeout_cycles--; if (i2c_master_read_packet_wait(&i2c_master_instance, &packet) == STATUS_OK) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "i2c master read failed"); /* Compare the sent and the received */ for (i = 0; i < DATA_LENGTH; i++) { if (read_buffer[i] != write_buffer[i]) { status = false; break; } } test_assert_true(test, status == true, "i2c master transfer comparsion failed"); /* with stop function master transfer test end */ /* without stop function: master transfer test*/ /* wait the master write to finish */ packet.data = write_buffer; timeout_cycles = 1000; do { timeout_cycles--; if (i2c_master_write_packet_wait_no_stop(&i2c_master_instance, &packet) == STATUS_OK) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "i2c master write without stop failed"); /* use i2c_master_send_stop to complete master writing */ i2c_master_send_stop(&i2c_master_instance); /* wait the master read to finish */ packet.data = read_buffer; timeout_cycles = 1000; do { timeout_cycles--; if (i2c_master_read_packet_wait_no_stop(&i2c_master_instance, &packet) == STATUS_OK) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "i2c master read without stop failed"); /* Compare the sent and the received */ for (i = 0; i < DATA_LENGTH; i++) { if (read_buffer[i] != write_buffer[i]) { status = false; break; } } test_assert_true(test, status == true, "i2c master transfer without stop comparsion failed"); /* use i2c_master_write_packet_wait to complete the transfer */ packet.data = write_buffer; do { timeout_cycles--; if (i2c_master_write_packet_wait(&i2c_master_instance, &packet) == STATUS_OK) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "i2c master write with repeated start failed"); /* without stop function: master transfer test end*/ } /** * \internal * \brief Test full speed mode master transfer. * * test function with stop in full speed mode. * \param test Current test case. */ static void run_i2c_full_speed_test(const struct test_case *test) { enum status_code status; struct i2c_master_config config_i2c_master; /* init i2c master in full speed mode*/ i2c_master_get_config_defaults(&config_i2c_master); config_i2c_master.buffer_timeout = 10000; config_i2c_master.baud_rate = I2C_MASTER_BAUD_RATE_400KHZ; i2c_master_disable(&i2c_master_instance); status = i2c_master_init(&i2c_master_instance, SERCOM2, &config_i2c_master); /* Check for successful initialization */ test_assert_true(test, status == STATUS_OK, "I2C master fast-mode initialization failed"); i2c_master_enable(&i2c_master_instance); uint32_t timeout_cycles = 1000; uint32_t i; bool status1 = true; struct i2c_master_packet packet = { .address = SLAVE_ADDRESS, .data_length = DATA_LENGTH, .data = write_buffer, .ten_bit_address = false, .high_speed = false, .hs_master_code = 0x0, }; uint8_t read_buffer[DATA_LENGTH] = {0}; /* wait master write complete */ do { timeout_cycles--; if (i2c_master_write_packet_wait(&i2c_master_instance, &packet) == STATUS_OK) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "i2c master write failed"); /* wait master read complete */ packet.data = read_buffer; timeout_cycles = 1000; do { timeout_cycles--; if (i2c_master_read_packet_wait(&i2c_master_instance, &packet) == STATUS_OK) { break; } } while (timeout_cycles > 0); test_assert_true(test, timeout_cycles > 0, "i2c master read failed"); /* Compare the sent and the received */ for (i = 0; i < DATA_LENGTH; i++) { if (read_buffer[i] != write_buffer[i]) { status1 = false; break; } } test_assert_true(test, status1 == true, "i2c master transfer comparsion failed"); } /** * \brief Run I2C master unit tests * * Initializes the system and serial output, then sets up the * I2C master unit test suite and runs it. */ int main(void) { system_init(); cdc_uart_init(); /* Define Test Cases */ DEFINE_TEST_CASE(i2c_init_test, NULL, run_i2c_init_test, NULL, "Testing I2C Initialization"); DEFINE_TEST_CASE(i2c_master_transfer_test, NULL, run_i2c_master_transfer_test, NULL, "Testing I2C master data transfer"); DEFINE_TEST_CASE(i2c_full_speed_test, NULL, run_i2c_full_speed_test, NULL, "Testing I2C change speed transfer"); /* Put test case addresses in an array */ DEFINE_TEST_ARRAY(i2c_tests) = { &i2c_init_test, &i2c_master_transfer_test, &i2c_full_speed_test, }; /* Define the test suite */ DEFINE_TEST_SUITE(i2c_test_suite, i2c_tests, "SAM I2C driver test suite"); /* Run all tests in the suite*/ test_suite_run(&i2c_test_suite); while (true) { /* Intentionally left empty */ } }
void cap_init(void) { // 63 2 4 1 63 2 4 1 0 8 4 cap_set_mhd_r(63); cap_set_nhd_r(2); cap_set_ncl_r(4); cap_set_fdl_r(1); cap_set_mhd_f(63); cap_set_nhd_f(2); cap_set_ncl_r(4); cap_set_fdl_f(1); cap_set_dbr(0); cap_set_touch_threshold(8); cap_set_release_threshold(4); /** gpio_set_mode(PIN_MAP[BOARD_I2C_SDA].gpio_device, PIN_MAP[BOARD_I2C_SDA].gpio_bit, GPIO_OUTPUT_PP); gpio_set_mode(PIN_MAP[BOARD_I2C_SCL].gpio_device, PIN_MAP[BOARD_I2C_SCL].gpio_bit, GPIO_OUTPUT_PP); gpio_write_bit(PIN_MAP[BOARD_I2C_SDA].gpio_device, PIN_MAP[BOARD_I2C_SDA].gpio_bit, 1); gpio_write_bit(PIN_MAP[BOARD_I2C_SCL].gpio_device, PIN_MAP[BOARD_I2C_SCL].gpio_bit, 1); delay_us(1000); gpio_set_mode(PIN_MAP[BOARD_I2C_SDA].gpio_device, PIN_MAP[BOARD_I2C_SDA].gpio_bit, GPIO_INPUT_PD); // Can also be floating, but PD is safer if components misplaced. gpio_set_mode(PIN_MAP[BOARD_I2C_SCL].gpio_device, PIN_MAP[BOARD_I2C_SCL].gpio_bit, GPIO_INPUT_PD); */ i2c = CAPTOUCH_I2C; i2c_init(i2c); i2c_master_enable(i2c, I2C_BUS_RESET); mpr121Write(0x80, 0x63); // soft reset delay_us(1000); mpr121Write(ELE_CFG, 0x00); // disable electrodes for config delay_us(100); // Section A and B - R (rise) F (fall) T (touch) mpr121Write(MHD_R, cap_mhd_r); // (1 to 63) mpr121Write(NHD_R, cap_nhd_r); // (1 to 63) mpr121Write(NCL_R, cap_ncl_r); // (0 to 255) mpr121Write(FDL_R, cap_fdl_r); // (0 to 255) mpr121Write(MHD_F, cap_mhd_f); // (1 to 63) largest value to pass through filer mpr121Write(NHD_F, cap_nhd_f); // (1 to 63) maximum change allowed mpr121Write(NCL_F, cap_ncl_f); // (0 to 255) number of samples required to determine non-noise mpr121Write(FDL_F, cap_fdl_f); // (0 to 255) rate of filter operation, larger = slower. // Section D // Set the Filter Configuration // Set ESI2 // was 0x01, 0x25 mpr121Write(AFE_CONF, 0x01); //AFE_CONF 0x5C mpr121Write(FIL_CFG, 0x04); //FIL_CFG 0x5D // Section F mpr121Write(ATO_CFG0, 0x0B); // ATO_CFG0 0x7B // limits // was0xFF,0x00,0x0E mpr121Write(ATO_CFGU, 0x9C); // ATO_CFGU 0x7D mpr121Write(ATO_CFGL, 0x65); // ATO_CFGL 0x7E mpr121Write(ATO_CFGT, 0x8C); // ATO_CFGT 0x7F // enable debouncing mpr121Write(DBR, cap_dbr); // set debouncing, in this case 7 for both touch and release. // Section C // This group sets touch and release thresholds for each electrode mpr121Write(ELE0_T, cap_touch_threshold); mpr121Write(ELE0_R, cap_release_threshold); mpr121Write(ELE1_T, cap_touch_threshold); mpr121Write(ELE1_R, cap_release_threshold); mpr121Write(ELE2_T, cap_touch_threshold); mpr121Write(ELE2_R, cap_release_threshold); mpr121Write(ELE3_T, cap_touch_threshold); mpr121Write(ELE3_R, cap_release_threshold); mpr121Write(ELE4_T, cap_touch_threshold); mpr121Write(ELE4_R, cap_release_threshold); mpr121Write(ELE5_T, cap_touch_threshold); mpr121Write(ELE5_R, cap_release_threshold); mpr121Write(ELE6_T, cap_touch_threshold); mpr121Write(ELE6_R, cap_release_threshold); mpr121Write(ELE7_T, cap_touch_threshold); mpr121Write(ELE7_R, cap_release_threshold); mpr121Write(ELE8_T, cap_touch_threshold); mpr121Write(ELE8_R, cap_release_threshold); mpr121Write(ELE9_T, cap_touch_threshold); mpr121Write(ELE9_R, cap_release_threshold); mpr121Write(ELE10_T, cap_touch_threshold); mpr121Write(ELE10_R, cap_release_threshold); mpr121Write(ELE11_T, cap_touch_threshold); mpr121Write(ELE11_R, cap_release_threshold); delay_us(100); // Section E // Electrode Configuration // Enable 6 Electrodes and set to run mode // Set ELE_CFG to 0x00 to return to standby mode mpr121Write(ELE_CFG, 0x0C); // Enables all 12 Electrodes delay_us(100); // This can also be FLOATING, but PU is safer if components misplaced. gpio_set_mode(PIN_MAP[CAPTOUCH_GPIO].gpio_device, PIN_MAP[CAPTOUCH_GPIO].gpio_bit, GPIO_INPUT_PU); exti_attach_interrupt((afio_exti_num) (PIN_MAP[CAPTOUCH_GPIO].gpio_bit), gpio_exti_port(PIN_MAP[CAPTOUCH_GPIO].gpio_device), cap_change, EXTI_FALLING); // Clears the first interrupt uint32_t ts = realtime_get_unixtime(); for (int n = 0; n < 16; n++) press_time[n] = ts; for (int n = 0; n < 16; n++) release_time[n] = ts; press_time_any = ts; release_time_any = ts; // Set the cap_enabled flag to remember the captouch is on cap_enabled = 1; return; }