uint8_t Init_Twi(int index) { if(index > (sizeof(twi_instances)/sizeof(twi_instances[0]))) { return 0; } if(twi_Init[index] == 1) { return 1; // allready initialized. } freertos_peripheral_options_t async_driver_options = { NULL, 0, (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY + 1), TWI_I2C_MASTER, (USE_TX_ACCESS_MUTEX | USE_RX_ACCESS_MUTEX | WAIT_TX_COMPLETE | WAIT_RX_COMPLETE), }; /*Initialize twi bus.*/ freertos_twi_if twi = freertos_twi_master_init( twi_instances[index], &async_driver_options ); if ( twi == NULL ) { return 0; } freeRTOS_twi[index] = twi; twi_set_speed( twi_instances[index], 400000, sysclk_get_cpu_hz() ); /*High speed TWI setting*/ twi_Init[index] = 1; return 1; }
/** * \brief Initialize TWI master mode. * * \param p_twi Pointer to a TWI instance. * \param p_opt Options for initializing the TWI module (see \ref twi_options_t). * * \return TWI_SUCCESS if initialization is complete, error code otherwise. */ uint32_t twi_master_init(Twi *p_twi, const twi_options_t *p_opt) { uint32_t status = TWI_SUCCESS; /* Disable TWI interrupts */ p_twi->TWI_IDR = ~0UL; /* Dummy read in status register */ p_twi->TWI_SR; /* Reset TWI peripheral */ twi_reset(p_twi); twi_enable_master_mode(p_twi); /* Select the speed */ if (twi_set_speed(p_twi, p_opt->speed, p_opt->master_clk) == FAIL) { /* The desired speed setting is rejected */ status = TWI_INVALID_ARGUMENT; } if (p_opt->smbus == 1) { p_twi->TWI_CR = TWI_CR_QUICK; } return status; }
int twi_master_init(volatile avr32_twi_t *twi, const twi_options_t *opt) { irqflags_t flags = sysreg_read(AVR32_SR); int status = TWI_SUCCESS; // Set pointer to TWIM instance for IT twi_inst = twi; // Disable TWI interrupts cpu_irq_disable(); twi->idr = ~0UL; twi->sr; // Reset TWI twi->cr = AVR32_TWI_CR_SWRST_MASK; cpu_irq_restore(flags); // Dummy read in SR twi->sr; // register Register twim_master_interrupt_handler interrupt // on level CONF_TWI_IRQ_LEVEL flags = cpu_irq_save(); irq_register_handler(&twi_master_interrupt_handler, CONF_TWI_IRQ_LINE, CONF_TWI_IRQ_LEVEL); cpu_irq_restore(flags); // Select the speed twi_set_speed(twi, opt->speed, opt->pba_hz); // Probe the component //status = twi_probe(twi, opt->chip); return status; }
static uint8_t twi_init(void){ freertos_peripheral_options_t driver_options = { //Receiver Buffer NULL, //Size Receiver Buffer 0, //Priority Interrupt (configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY), TWI_I2C_MASTER, ( USE_TX_ACCESS_MUTEX | USE_RX_ACCESS_MUTEX | WAIT_TX_COMPLETE | WAIT_RX_COMPLETE) }; freertos_twi = freertos_twi_master_init(TWI0, &driver_options); configASSERT(freertos_twi); uint8_t twi_flag = twi_set_speed(TWI0, TWI_SPEED, sysclk_get_cpu_hz()); return twi_flag; }
int twi_master_init(volatile avr32_twim_t *twi, const twi_options_t *opt) { bool global_interrupt_enabled = Is_global_interrupt_enabled(); int status = TWI_SUCCESS; // Disable TWI interrupts if (global_interrupt_enabled) Disable_global_interrupt(); twi->idr = ~0UL; // Reset TWI twi->cr = AVR32_TWIM_CR_SWRST_MASK; if (global_interrupt_enabled) Enable_global_interrupt(); // Clear SR twi->scr = ~0UL; // Disable all interrupts Disable_global_interrupt(); // Register TWI handler on level 2 BSP_INTC_IntReg( &twi_master_interrupt_handler, AVR32_TWIM0_IRQ, AVR32_INTC_INT1); // Enable all interrupts Enable_global_interrupt(); twi->cr = AVR32_TWIM_CR_MEN_MASK; if (opt->smbus) { twi->cr = AVR32_TWIM_CR_SMEN_MASK; twi->smbtr = (unsigned long) -1; } // Select the speed if (twi_set_speed(twi, opt->speed, opt->pba_hz) == TWI_INVALID_CLOCK_DIV) return TWI_INVALID_CLOCK_DIV; // Probe the component //status = twi_probe(twi, opt->chip); return status; }
int twi_master_init(volatile avr32_twi_t *twi, const twi_options_t *opt) { bool global_interrupt_enabled = Is_global_interrupt_enabled(); int status = TWI_SUCCESS; // Set pointer to TWIM instance for IT twi_inst = twi; // Disable TWI interrupts if (global_interrupt_enabled) Disable_global_interrupt(); twi->idr = ~0UL; twi->sr; // Reset TWI twi->cr = AVR32_TWI_CR_SWRST_MASK; if (global_interrupt_enabled) Enable_global_interrupt(); // Dummy read in SR twi->sr; // Disable all interrupts Disable_global_interrupt(); // Register TWI handler on level 2 INTC_register_interrupt( &twi_master_interrupt_handler, AVR32_TWI_IRQ, AVR32_INTC_INT1); // Enable all interrupts Enable_global_interrupt(); // Select the speed twi_set_speed(twi, opt->speed, opt->pba_hz); // Probe the component //status = twi_probe(twi, opt->chip); return status; }