/** \brief method to change the bus speed of I2C * \param[in] interface on which to change bus speed * \param[in] baud rate (typically 100000 or 400000) */ void change_i2c_speed( ATCAIface iface, uint32_t speed ) { ATCAIfaceCfg *cfg = atgetifacecfg(iface); int bus = cfg->atcai2c.bus; switch(bus) { case 0: //pmc_enable_periph_clk(ID_TWI0); //flexcom_enable(FLEXCOM0); //flexcom_set_opmode(FLEXCOM0, FLEXCOM_TWI); flexcom_enable(FLEX_Channel0); flexcom_set_opmode(FLEX_Channel0, FLEXCOM_TWI); opt_twi_master.master_clk = sysclk_get_cpu_hz(); opt_twi_master.speed = speed; opt_twi_master.smbus = 0; //twi_master_init(TWI0, &opt_twi_master); twi_master_init(TWI_Channel0, &opt_twi_master); break; case 1: //pmc_enable_periph_clk(ID_TWI1); flexcom_enable(FLEX_Channel1); flexcom_set_opmode(FLEX_Channel1, FLEXCOM_TWI); opt_twi_master.master_clk = sysclk_get_cpu_hz(); opt_twi_master.speed = speed; opt_twi_master.smbus = 0; twi_master_init(TWI_Channel1, &opt_twi_master); break; } }
/** Main initialisation. */ static void main_init (void) { #ifndef HOST /* Disable watchdog (enabled in bootloader). */ MCUSR &= ~(1 << WDRF); wdt_disable (); #endif /* Serial port */ uart0_init (); /* Enable interrupts */ sei (); /* Main timer */ timer_init (); timer_wait (); /* TWI communications */ asserv_init (); mimot_init (); twi_master_init (); /* IO modules. */ pwm_init (); contact_init (); codebar_init (); usdist_init (); /* AI modules. */ clamp_init (); logistic_init (); path_init (); /* Initialization done. */ proto_send0 ('z'); }
bool fb_sleep(uint8_t faceNum, bool sleepEnabled) { uint8_t twiBuf[2]; bool success; if (faceNum > 6) { return false; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_SLEEP; if (sleepEnabled) { twiBuf[1] = 0x01; } else { twiBuf[1] = 0x00; } success = twi_master_transfer((faceNum << 1), twiBuf, 2, true); /* After sending a sleep command, we do not attempt to read a response * because doing so will wake-up the daughterboard processor. */ twi_master_deinit(); return success; }
bool fb_getRxEnable(uint8_t faceNum, bool *rxEnabled) { uint8_t twiBuf[2]; bool success = true; if (faceNum > 6) { return -1; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_RX_ENABLE; success &= twi_master_transfer((faceNum << 1), twiBuf, 1, true); success &= twi_master_transfer((faceNum << 1) | TWI_READ_BIT, twiBuf, 1, true); twi_master_deinit(); if (twiBuf[0] & 0x01) { *rxEnabled = true; } else { *rxEnabled = false; } return success; }
bool fb_getIRTxLEDs(uint8_t faceNum, bool *led1, bool *led2, bool *led3, bool *led4) { uint8_t twiBuf[2]; bool success = true; if ((faceNum < 1) || (faceNum > 6)) { return -1; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_TX_LED_SELECT; success &= twi_master_transfer((faceNum << 1), twiBuf, 1, true); success &= twi_master_transfer((faceNum << 1) | TWI_READ_BIT, twiBuf, 1, true); twi_master_deinit(); if (success) { *led1 = (twiBuf[0] & 0x01) ? true : false; *led2 = (twiBuf[0] & 0x02) ? true : false; *led3 = (twiBuf[0] & 0x04) ? true : false; *led4 = (twiBuf[0] & 0x08) ? true : false; } return success; }
int16_t fb_getAmbientLight(uint8_t faceNum) { uint8_t twiBuf[2]; bool success = true; int16_t ambientLight; if ((faceNum < 1) || (faceNum > 6)) { return -1; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_AMBIENT_LIGHT; success &= twi_master_transfer((faceNum << 1), twiBuf, 1, true); success &= twi_master_transfer((faceNum << 1) | TWI_READ_BIT, twiBuf, 2, true); twi_master_deinit(); /* The 10-bit result is returned left-shifted so that it is possible to * read just one byte and still get most of the resolution (even though * we still read both bytes). */ ambientLight = twiBuf[0] << 2; ambientLight |= twiBuf[1] >> 6; if (!success) { ambientLight = -1; } return ambientLight; }
bool fb_getTopLEDs(uint8_t faceNum, bool *redOn, bool *greenOn, bool *blueOn) { uint8_t twiBuf[2]; bool success = true; if ((faceNum < 1) || (faceNum > 6)) { return false; } twi_master_init(); twiBuf[0] = FB_REGISTER_ADDR_LEDS_TOP; success &= twi_master_transfer((faceNum << 1), twiBuf, 1, true); success &= twi_master_transfer((faceNum << 1) | TWI_READ_BIT, twiBuf, 1, true); twi_master_deinit(); if (success) { *redOn = (twiBuf[0] & 0x01) ? true : false; *greenOn = (twiBuf[0] & 0x02) ? true : false; *blueOn = (twiBuf[0] & 0x04) ? true : false; } return success; }
/* ============================================== Function: initialize_all(void) Initialize oscillator, radio, bluetooth, twi and vibration ============================================== */ static void initialize_all() { char buf[30]; // Start 16 MHz crystal oscillator. NRF_CLOCK->EVENTS_HFCLKSTARTED = 0; NRF_CLOCK->TASKS_HFCLKSTART = 1; // oscillator while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) { // busy wait until the oscilator is up and running } simple_uart_config(0, 23, 0, 22, 0); simple_uart_putstring("INIT\n"); // initiliaze radio radio_configure(); simple_uart_putstring("Configured radio\n"); // initialize bluetooth start_ble(MUG_LIST); simple_uart_putstring("BLUETOOTH STARTED\n"); // initialize twi twi_master_init(); simple_uart_putstring("TWI master init\n"); init_vibration(); simple_uart_putstring("Vibration init\n"); }
bool nrf6350_lcd_init(void) { if (!twi_master_init()) { return false; } // Sometimes the first command doesn't get through, so we'll try // sending non-important "wake up" command first and don't care if it fails. (void)nrf6350_lcd_wake_up(); if (!nrf6350_lcd_set_instruction(0x38)) // Function set. return false; if (!nrf6350_lcd_set_instruction(0x39)) // Choose two-line mode. return false; if (!nrf6350_lcd_set_instruction(0x14)) // Internal OSC frequency. return false; if (!nrf6350_lcd_set_contrast(LCD_CONTRAST_HIGH)) // Contrast set (low byte). return false; if (!nrf6350_lcd_set_instruction(0x5F)) // Power/ICON control/. return false; if (!nrf6350_lcd_set_instruction(0x6A)) // Follower control. return false; nrf_delay_us(200000); // Need to wait 200ms here according to datasheet. if (!nrf6350_lcd_on()) // Display ON. return false; if (!nrf6350_lcd_clear()) // Clear display. return false; return nrf6350_lcd_set_instruction(0x06); // Entry mode set. }
static bool twi_master_write(uint8_t *data, uint8_t data_length, bool issue_stop_condition) { uint32_t timeout = MAX_TIMEOUT_LOOPS; /* max loops to wait for EVENTS_TXDSENT event*/ if (data_length == 0) { /* Return false for requesting data of size 0 */ return false; } NRF_TWI1->TXD = *data++; NRF_TWI1->TASKS_STARTTX = 1; /** @snippet [TWI HW master write] */ while (true) { while(NRF_TWI1->EVENTS_TXDSENT == 0 && NRF_TWI1->EVENTS_ERROR == 0 && (--timeout)) { // Do nothing. } if (timeout == 0 || NRF_TWI1->EVENTS_ERROR != 0) { // Recover the peripheral as indicated by PAN 56: "TWI: TWI module lock-up." found at // Product Anomaly Notification document found at // https://www.nordicsemi.com/eng/Products/Bluetooth-R-low-energy/nRF51822/#Downloads NRF_TWI1->EVENTS_ERROR = 0; NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; NRF_TWI1->POWER = 0; nrf_delay_us(5); NRF_TWI1->POWER = 1; NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos; (void)twi_master_init(); return false; } NRF_TWI1->EVENTS_TXDSENT = 0; if (--data_length == 0) { break; } NRF_TWI1->TXD = *data++; } /** @snippet [TWI HW master write] */ if (issue_stop_condition) { NRF_TWI1->EVENTS_STOPPED = 0; NRF_TWI1->TASKS_STOP = 1; /* Wait until stop sequence is sent */ while(NRF_TWI1->EVENTS_STOPPED == 0) { // Do nothing. } NRF_TWI1->EVENTS_STOPPED = 0; } return true; }
int main() { DDRB = 0x20; PORTC = 1 << 4 | 1 << 5; stdout = &mystdout; stdin = &mystdin; uart_init(); twi_master_init(); puts("Master Receive!"); while (twi_mr_start(0x08) != TWST_OK) { printf("Failed: %x\n", TW_STATUS); twi_stop(); puts("No ACK (Enter to continue)"); getchar(); } while (1) { char c = twi_read(); printf("Char: %c, Status: %x\n", c, TW_STATUS); if (TW_STATUS != TW_MR_DATA_ACK) break; PINB = 0x20; } puts("Disconnected"); return 0; }
int main(void) { uint32_t err_code; // Initialize. timers_init(); err_code = bsp_init(BSP_INIT_LED, APP_TIMER_TICKS(100, APP_TIMER_PRESCALER), NULL); APP_ERROR_CHECK(err_code); uart_init(); ble_stack_init(); twi_master_init(); err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING); nrf_delay_ms(200); bsp_indication_set(BSP_INDICATE_IDLE); nrf_delay_ms(200); err_code = bsp_indication_set(BSP_INDICATE_ADVERTISING); update_sensor(0); start_advertising(); timers_start(); // Enter main loop. for (;; ) { power_manage(); } }
static void init(void){ my_twi_config.twi_ppi_ch = 1; my_twi_config.twi = NRF_TWI1; twi_init_config_t init_config; init_config.frequency = TWI_FREQ_400KHZ; init_config.twi_pinselect_scl = light_TWI_SCL; init_config.twi_pinselect_sda = light_TWI_SDA; init_config.twi_interrupt_no = SPI1_TWI1_IRQn; if(!twi_master_init(&init_config,&my_twi_config)) { APP_ERROR_CHECK(666); } iss_struct.p_update_samp_freq = update_measurement_samp_freq; NRF_GPIO->PIN_CNF[TSL2561_BASE_PIN] = (GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos) | (GPIO_PIN_CNF_DRIVE_S0S1 << GPIO_PIN_CNF_DRIVE_Pos) | (GPIO_PIN_CNF_PULL_Disabled << GPIO_PIN_CNF_PULL_Pos) | (GPIO_PIN_CNF_INPUT_Disconnect << GPIO_PIN_CNF_INPUT_Pos) | (GPIO_PIN_CNF_DIR_Output << GPIO_PIN_CNF_DIR_Pos); iss_struct.coord = TSL2561_COORDINATE; iss_struct.type = BLE_UUID_ITU_SENSOR_TYPE_LIGHT; iss_struct.make = BLE_UUID_ITU_SENSOR_MAKE_TSL2561; iss_struct.IEEE_exponent = 0; iss_update_config(&iss_struct); }
void main(void) { twi_master_init(); mpu6500_init(); mpu6500_stop(); disable_i2c(); simble_init("Motion"); motion_ctx.sampling_period = DEFAULT_SAMPLING_PERIOD; //Set the timer parameters and initialize it. struct rtc_ctx rtc_ctx = { .rtc_x[NOTIF_TIMER_ID] = { .type = PERIODIC, .period = motion_ctx.sampling_period, .enabled = false, .cb = notif_timer_cb, } }; batt_serv_init(&rtc_ctx); rtc_init(&rtc_ctx); ind_init(); motion_init(&motion_ctx); simble_adv_start(); simble_process_event_loop(); }
/** * \brief initialize an I2C interface using given config * * \param[in] hal - opaque ptr to HAL data * \param[in] cfg - interface configuration * * \return ATCA_STATUS */ ATCA_STATUS hal_i2c_init(void *hal, ATCAIfaceCfg *cfg) { // set to default i2c bus if (cfg->atcai2c.bus > MAX_I2C_BUSES - 1) cfg->atcai2c.bus = 0; int bus = cfg->atcai2c.bus; // 0-based logical bus number ATCAHAL_t *phal = (ATCAHAL_t*)hal; if (i2c_bus_ref_ct == 0) // power up state, no i2c buses will have been used for (int i = 0; i < MAX_I2C_BUSES; i++) i2c_hal_data[i] = NULL; i2c_bus_ref_ct++; // total across buses if (bus >= 0 && bus < MAX_I2C_BUSES) { // if this is the first time this bus and interface has been created, do the physical work of enabling it if (i2c_hal_data[bus] == NULL) { i2c_hal_data[bus] = malloc(sizeof(ATCAI2CMaster_t)); i2c_hal_data[bus]->ref_ct = 1; // buses are shared, this is the first instance switch (bus) { case 0: i2c_hal_data[bus]->twi_id = ID_TWI0; i2c_hal_data[bus]->twi_master_instance = TWI0; break; case 1: i2c_hal_data[bus]->twi_id = ID_TWI1; i2c_hal_data[bus]->twi_master_instance = TWI1; // configure TWI1 pins gpio_configure_pin(PIO_PB4_IDX, (PIO_PERIPH_A | PIO_PULLUP)); gpio_configure_pin(PIO_PB5_IDX, (PIO_PERIPH_A | PIO_PULLUP)); // disable JTAG MATRIX->CCFG_SYSIO |= (1 << 4) | (1 << 5); break; } pmc_enable_periph_clk(i2c_hal_data[bus]->twi_id); opt_twi_master.master_clk = sysclk_get_cpu_hz(); opt_twi_master.speed = cfg->atcai2c.baud; opt_twi_master.smbus = 0; twi_master_init(i2c_hal_data[bus]->twi_master_instance, &opt_twi_master); // store this for use during the release phase i2c_hal_data[bus]->bus_index = bus; }else { // otherwise, another interface already initialized the bus, so this interface will share it and any different // cfg parameters will be ignored...first one to initialize this sets the configuration i2c_hal_data[bus]->ref_ct++; } phal->hal_data = i2c_hal_data[bus]; return ATCA_SUCCESS; } return ATCA_COMM_FAIL; }
u8 init_master() { // options settings opt.pba_hz = FPBA_HZ; opt.speed = TWI_SPEED; opt.chip = addr; // initialize TWI driver with options return twi_master_init(&AVR32_TWI, &opt); }
void i2c400_init(void) { m_options.speed_reg = TWI_BAUD(sysclk_get_cpu_hz(),m_options.speed); sysclk_enable_peripheral_clock(&TWI_I2C400); twi_master_init(&TWI_I2C400,&m_options); }
/** * \brief Configures a TWI peripheral to operate in master mode, at the given * frequency (in Hz). The duty cycle of the TWI clock is set to 50%. * \param pTwi Pointer to an Twi instance. * \param twck Desired TWI clock frequency. * \param mck Master clock frequency. */ static void TWI_ConfigureMaster(Twi* pTwi, uint32_t dwTwCk, uint32_t dwMCk) { twi_options_t opt; opt.speed = dwTwCk; opt.master_clk = dwMCk; opt.chip = opt.smbus = 0; twi_master_init(pTwi, &opt); }
// Supporting function implementation Accelerometer::Accelerometer() { // Initialize variables uint8_t value; // Configure VDDIO, SDA and SCL pins ioport_set_pin_dir(ACCELEROMETER_VDDIO_PIN, IOPORT_DIR_OUTPUT); ioport_set_pin_level(ACCELEROMETER_VDDIO_PIN, IOPORT_PIN_LEVEL_HIGH); ioport_set_pin_mode(ACCELEROMETER_SDA_PIN, IOPORT_MODE_WIREDANDPULL); ioport_set_pin_mode(ACCELEROMETER_SCL_PIN, IOPORT_MODE_WIREDANDPULL); // Configure interface twi_options_t options; options.speed = BUS_SPEED; options.chip = MASTER_ADDRESS; options.speed_reg = TWI_BAUD(sysclk_get_cpu_hz(), BUS_SPEED); // Initialize interface sysclk_enable_peripheral_clock(&TWI_MASTER); twi_master_init(&TWI_MASTER, &options); twi_master_enable(&TWI_MASTER); // Create packet twi_package_t packet; packet.addr[0] = WHO_AM_I; packet.addr_length = 1; packet.chip = ACCELEROMETER_ADDRESS; packet.buffer = &value; packet.length = 1; packet.no_wait = false; // Check if transmitting or receiving failed if(twi_master_read(&TWI_MASTER, &packet) != TWI_SUCCESS || value != DEVICE_ID) // Clear is working isWorking = false; // Otherwise else { // Reset the accelerometer writeValue(CTRL_REG2, CTRL_REG2_RST); // Wait enough time for accelerometer to initialize delay_ms(1); // Initialize settings initializeSettings(); // Calibrate //calibrate(); // Set is working isWorking = true; } }
static void init_twi(uint32_t fpba_hz) { #if(DEFAULT_DACS == AUDIO_MIXER_DAC_ABDAC) const gpio_map_t TPA6130_TWI_GPIO_MAP = { {TPA6130_TWI_SCL_PIN, TPA6130_TWI_SCL_FUNCTION}, {TPA6130_TWI_SDA_PIN, TPA6130_TWI_SDA_FUNCTION} }; twi_options_t TPA6130_TWI_OPTIONS = { .speed = TPA6130_TWI_MASTER_SPEED, .chip = TPA6130_TWI_ADDRESS }; TPA6130_TWI_OPTIONS.pba_hz = fpba_hz; // Assign I/Os to TWI. gpio_enable_module(TPA6130_TWI_GPIO_MAP, sizeof(TPA6130_TWI_GPIO_MAP) / sizeof(TPA6130_TWI_GPIO_MAP[0])); // Initialize as master. twi_master_init(TPA6130_TWI, &TPA6130_TWI_OPTIONS); #elif(DEFAULT_DACS == AUDIO_MIXER_DAC_AIC23B) static const gpio_map_t AIC23B_TWI_GPIO_MAP = { {AIC23B_TWI_SCL_PIN, AIC23B_TWI_SCL_FUNCTION}, {AIC23B_TWI_SDA_PIN, AIC23B_TWI_SDA_FUNCTION} }; static twi_options_t AIC23B_TWI_OPTIONS = { .speed = AIC23B_TWI_MASTER_SPEED, .chip = AIC23B_TWI_ADDRESS }; AIC23B_TWI_OPTIONS.pba_hz = fpba_hz; gpio_enable_module(AIC23B_TWI_GPIO_MAP, sizeof(AIC23B_TWI_GPIO_MAP) / sizeof(AIC23B_TWI_GPIO_MAP[0])); twi_master_init(AIC23B_TWI, &AIC23B_TWI_OPTIONS); #endif }
void rgb_lcd_init() { if (!twi_master_init()) { APP_ERROR_CHECK(1); } rgb_lcd_begin(); }
void dev_init(void) { gpio_init(); twi_master_init(); timers_init(); //app_button_init()やble_conn_params_init()よりも前に呼ぶこと! //呼ばなかったら、NRF_ERROR_INVALID_STATE(8)が発生する。 //scheduler_init(); ble_stack_init(); }
void i2c_reset(i2c_t *obj) { obj->i2c->EVENTS_ERROR = 0; obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; obj->i2c->POWER = 0; for (int i = 0; i<100; i++) { } obj->i2c->POWER = 1; twi_master_init(obj, obj->sda, obj->scl, obj->freq); }
void CLumenCapsense::init() { uint8_t data[10]; twi_master_init(); lumen_capsense_ambient_setpoint = 200; mpr121_soft_reset(); nrf_delay_us(100); mpr121_config_prox_touch_0(); nrf_delay_us(100000); mpr121_read_register(MPR121_OOR_0_7, data); }
/** * @brief Main function of the Terminal Target application * @ingroup App_API */ int main(void) { /* Initialize all layers */ if (nwk_init() != NWK_SUCCESS) { /* something went wrong during initialization */ pal_alert(); } /* disable pull-ups */ MCUCR |= (1u << PUD); #ifdef FLASH_NVRAM pal_ps_set(EE_IEEE_ADDR,IEEE_ADDRESS_BYTES, &tal_pib_IeeeAddress); #endif /* Initialize LEDs. */ pal_led_init(); pal_led(LED_START, LED_ON); /* indicating application is started */ pal_led(LED_NWK_SETUP, LED_OFF); /* indicating network is started */ pal_led(LED_DATA, LED_OFF); /* indicating data reception */ /* * The stack is initialized above, hence the global interrupts are enabled * here. */ pal_global_irq_enable(); /** * @brief TWI and QT600 interface initialization */ int i; twi_master_init(); RESET_QT600_PIN_INIT(); RESET_QT600_ON(); for (i = 0; i < 100 ; i++) asm("nop"); /* Endless while loop */ while (1) { app_task(); /* Application task */ if(rf4ce_new_msg == 1) { twi_send_message(); TX_index = 0; rf4ce_new_msg = 0; } nwk_task(); /* RF4CE network layer task */ } }
bool db_getVersion(char *verStr, uint8_t verStrSize) { uint8_t twiBuf[34]; uint32_t time_ms; char *strPtr; twi_master_init(); twiBuf[0] = DB_VERSION_CMD; if (!twi_master_transfer((DB_TWI_ADDR << 1), twiBuf, 1, true)) { twi_master_deinit(); return false; } delay_ms(DB_POLL_FIRST_INTERVAL_MS); for (time_ms = 0; time_ms < DB_VERSION_TIMEOUT_MS; time_ms += DB_POLL_INTERVAL_MS) { if (!twi_master_transfer((DB_TWI_ADDR << 1) | TWI_READ_BIT, twiBuf, sizeof(twiBuf), true)) { /* If the daughterboard fails to respond to the I2C master read, * something is wrong, so we return failure. */ twi_master_deinit(); return false; } /* If the daughterboard responds and echoes the command code in the the * first byte, and the second byte is 0x01, the version command was * successful. */ if ((twiBuf[0] == DB_VERSION_CMD) && (twiBuf[1] == 0x01)) { /* The following bytes contain a human-readable version string. */ strPtr = (char *)&twiBuf[2]; /* If the version string will fit into the buffer provided by the * caller, we copy it. Otherwise, we copy whatever will fit and * add a null-terminator. */ if (strlen(strPtr) < verStrSize) { strcpy(verStr, strPtr); } else { strncpy(verStr, strPtr, verStrSize - 1); verStr[verStrSize-1] = '\0'; } twi_master_deinit(); return true; } /* If the daughterboard responded but has not yet processed the command * we delay and then try to read from the daughterboard again. */ delay_ms(DB_POLL_INTERVAL_MS); } twi_master_deinit(); return false; }
bool db_setLEDs(bool redOn, bool greenOn, bool blueOn) { uint8_t twiBuf[2]; uint32_t time_ms; twi_master_init(); twiBuf[0] = DB_LED_CMD; twiBuf[1] = 0x00; if (redOn) { twiBuf[1] |= 0x01; } if (greenOn) { twiBuf[1] |= 0x02; } if (blueOn) { twiBuf[1] |= 0x04; } if (!twi_master_transfer((DB_TWI_ADDR << 1), twiBuf, 2, true)) { twi_master_deinit(); return false; } delay_ms(DB_POLL_FIRST_INTERVAL_MS); for (time_ms = 0; time_ms < DB_LED_TIMEOUT_MS; time_ms += DB_POLL_INTERVAL_MS) { if (!twi_master_transfer((DB_TWI_ADDR << 1) | TWI_READ_BIT, twiBuf, sizeof(twiBuf), true)) { /* If the daughterboard fails to respond to the I2C master read, * something is wrong, so we return failure. */ twi_master_deinit(); return false; } /* If the daughterboard responds and echoes the command code in the the * first byte, and the second byte is 0x01, the temperature command was * successful. */ if ((twiBuf[0] == DB_LED_CMD) && (twiBuf[1] == 0x01)) { twi_master_deinit(); return true; } /* If the daughterboard responded but has not yet processed the command * we delay and then try to read from the daughterboard again. */ delay_ms(DB_POLL_INTERVAL_MS); } twi_master_deinit(); return false; }
void i2c_init(i2c_t *obj, PinName sda, PinName scl) { // Initialize variable to avoid compiler warnings NRF_TWI_Type *i2c = (NRF_TWI_Type *)I2C_0; if (i2c0_spi0_peripheral.usage == I2C_SPI_PERIPHERAL_FOR_I2C && i2c0_spi0_peripheral.sda_mosi == (uint8_t)sda && i2c0_spi0_peripheral.scl_miso == (uint8_t)scl) { // The I2C with the same pins is already initialized i2c = (NRF_TWI_Type *)I2C_0; obj->peripheral = 0x1; } else if (i2c1_spi1_peripheral.usage == I2C_SPI_PERIPHERAL_FOR_I2C && i2c1_spi1_peripheral.sda_mosi == (uint8_t)sda && i2c1_spi1_peripheral.scl_miso == (uint8_t)scl) { // The I2C with the same pins is already initialized i2c = (NRF_TWI_Type *)I2C_1; obj->peripheral = 0x2; } else if (i2c0_spi0_peripheral.usage == 0) { i2c0_spi0_peripheral.usage = I2C_SPI_PERIPHERAL_FOR_I2C; i2c0_spi0_peripheral.sda_mosi = (uint8_t)sda; i2c0_spi0_peripheral.scl_miso = (uint8_t)scl; i2c = (NRF_TWI_Type *)I2C_0; obj->peripheral = 0x1; } else if (i2c1_spi1_peripheral.usage == 0) { i2c1_spi1_peripheral.usage = I2C_SPI_PERIPHERAL_FOR_I2C; i2c1_spi1_peripheral.sda_mosi = (uint8_t)sda; i2c1_spi1_peripheral.scl_miso = (uint8_t)scl; i2c = (NRF_TWI_Type *)I2C_1; obj->peripheral = 0x2; } else { // No available peripheral error("No available I2C"); } obj->i2c = i2c; obj->scl = scl; obj->sda = sda; obj->i2c->EVENTS_ERROR = 0; obj->i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; obj->i2c->POWER = 0; for (int i = 0; i<100; i++) { } obj->i2c->POWER = 1; twi_master_init(obj, sda, scl, 100000); }
/** * \brief Initialize PIO capture and the OV7740 image sensor. * * \return 0 on success, 1 otherwise. */ static uint32_t capture_init(void) { twi_options_t opt; /* Init Vsync handler */ init_vsync_interrupts(); /* Init PIO capture */ pio_capture_init(OV_DATA_BUS_PIO, OV_DATA_BUS_ID); /* Turn on OV7740 image sensor using power pin */ ov_power(true, OV_POWER_PIO, OV_POWER_MASK); /* Init PCK0 at 24 Mhz */ PMC->PMC_PCK[0] = (PMC_PCK_PRES_CLK_4 | PMC_PCK_CSS_PLLA_CLK); PMC->PMC_SCER = PMC_SCER_PCK0; while (!(PMC->PMC_SCSR & PMC_SCSR_PCK0)) { } /* Enable TWI peripheral */ pmc_enable_periph_clk(ID_BOARD_TWI); /* Init TWI peripheral */ opt.master_clk = sysclk_get_cpu_hz(); opt.speed = TWI_CLK; twi_master_init(BOARD_TWI, &opt); /* Configure TWI interrupts */ NVIC_DisableIRQ(BOARD_TWI_IRQn); NVIC_ClearPendingIRQ(BOARD_TWI_IRQn); NVIC_SetPriority(BOARD_TWI_IRQn, 0); NVIC_EnableIRQ(BOARD_TWI_IRQn); /* OV7740 initialization*/ delay_ms(1000); if (ov_init(BOARD_TWI) == 0) { /* OV7740 configuration */ ov_configure(BOARD_TWI, TEST_PATTERN); /* Wait 3 seconds to let the image sensor to adapt to the * environment */ delay_ms(3000); return 0; } else { return 1; } }
/** * \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) { /* Enable the peripheral clock for TWI */ pmc_enable_periph_clk(EDBG_I2C_MODULE_ID); /* Configure the options of TWI driver */ opt.master_clk = sysclk_get_cpu_hz(); opt.speed = TWI_CLK; if (twi_master_init(EDBG_I2C_MODULE, &opt) != TWI_SUCCESS) { puts("-E-\tTWI master initialization failed.\r"); while (1) { /* Capture error */ } } }