/** * \brief Initialize the twi master module * * \param twim Base address of the TWIM (i.e. &AVR32_TWIM) * \param *opt Options for initializing the twim module * (see \ref twim_options_t) * \retval STATUS_OK Transaction is successful * \retval ERR_INVALID_ARG Invalid arg resulting in wrong CWGR Exponential * \retval ERR_IO_ERROR NACK is received or Bus Arbitration lost */ status_code_t twim_master_init (volatile avr32_twim_t *twim, const twim_options_t *opt) { bool global_interrupt_enabled = cpu_irq_is_enabled (); // Initialize bus transfer status transfer_status = TWI_SUCCESS; // Disable TWI interrupts if (global_interrupt_enabled) { cpu_irq_disable (); } twim->idr = ~0UL; // Enable master transfer twim->cr = AVR32_TWIM_CR_MEN_MASK; // Reset TWI twim->cr = AVR32_TWIM_CR_SWRST_MASK; if (global_interrupt_enabled) { cpu_irq_enable (); } // Clear SR twim->scr = ~0UL; // register Register twim_master_interrupt_handler interrupt on level CONF_TWIM_IRQ_LEVEL irqflags_t flags = cpu_irq_save(); irq_register_handler(twim_master_interrupt_handler, CONF_TWIM_IRQ_LINE, CONF_TWIM_IRQ_LEVEL); cpu_irq_restore(flags); /* if (opt->smbus) { // Enable SMBUS Transfer twim->cr = AVR32_TWIM_CR_SMEN_MASK; twim->smbtr = (uint32_t) -1; } */ // Select the speed if (twim_set_speed (twim, opt->speed, opt->pba_hz) == ERR_INVALID_ARG) { return ERR_INVALID_ARG; } // Probe the component twim_probe (twim, opt->chip); //Check for nack and abitration if (transfer_status == TWI_RECEIVE_NACK || transfer_status == TWI_ARBITRATION_LOST) { return ERR_IO_ERROR; } return STATUS_OK; }
void lsm330dlc_init(void) { if(twim_probe(&AVR32_TWIM0, LSM330_ACC_SLAVE_ADDRESS) == STATUS_OK) { print_util_dbg_print("LSM330 sensor found (0x18) \r\n"); } else { print_util_dbg_print("LSM330 sensor not responding (0x18) \r\n"); return; } lsm330dlc_acc_init(); lsm330dlc_gyro_init(); lsm330dlc_get_acc_config(); lsm330dlc_get_gyro_config(); }
void hmc5883l_init_slow() { if(twim_probe(&AVR32_TWIM0, HMC5883_SLAVE_ADDRESS) == STATUS_OK) { print_util_dbg_print("HMC5883 compass sensor found (0x1E) \r\n"); } else { print_util_dbg_print("HMC5883 compass sensor not responding (0x1E) \r\n"); return; } static uint8_t compass_default_configuration[4] = { CONF_REG_A, (HMC_SAMPLE_AVG) << 5 | (HMC_RATE) << 2 | HMC_MODE, (HMC_RANGE) << 5, (HMC_MODE) }; twim_write(&AVR32_TWIM0, (uint8_t*)&compass_default_configuration, 4, HMC5883_SLAVE_ADDRESS, false); }