void matrix_init(void) { debug_enable = true; debug_matrix = true; debug_mouse = true; // initialize row and col unselect_rows(); init_cols(); #ifdef USE_I2C i2c_master_init(); #else serial_master_init(); #endif /* int x = USB_IsInitialized; */ /* if (x) { */ /* DDRD |= 1<<5; */ /* PORTD &= ~(1<<5); */ /* } else { */ // use the pro micro TX led as an indicator // pull D5 low to turn on //DDRD |= 1<<5; //PORTD |= (1<<5); /* } */ // initialize matrix state: all keys off for (uint8_t i=0; i < MATRIX_ROWS; i++) { matrix[i] = 0; matrix_debouncing[i] = 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; } }
//! [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] }
static void keyboard_master_setup(void) { #ifdef USE_I2C i2c_master_init(); #else serial_master_init(); #endif }
/****************************************************************************** * FunctionName : i2c_master_gpio_init * Description : config SDA and SCL gpio to open-drain output mode, * mux and gpio num defined in i2c_master.h * Parameters : uint8 sda and scl pin numbers * Returns : bool, true if init okay *******************************************************************************/ bool i2c_master_gpio_init(uint8 sda, uint8 scl) { if((sda > GPIO_PIN_NUM) || (pin_func[sda] == GPIO_PIN_FUNC_INVALID)){ return false; } if((scl > GPIO_PIN_NUM) || (pin_func[scl] == GPIO_PIN_FUNC_INVALID)){ return false; } pinSDA = sda; pinSCL = scl; ETS_GPIO_INTR_DISABLE() ; // ETS_INTR_LOCK(); PIN_FUNC_SELECT(pin_mux[sda], pin_func[sda]); PIN_FUNC_SELECT(pin_mux[scl], pin_func[scl]); GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(sda)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(sda))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain; GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << sda)); GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(scl)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(scl))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain; GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << scl)); i2c_master_setDC(1, 1); ETS_GPIO_INTR_ENABLE() ; // ETS_INTR_UNLOCK(); i2c_master_init(); return true; }
/****************************************************************************** * FunctionName : i2c_master_gpio_init * Description : config SDA and SCL gpio to open-drain output mode, * mux and gpio num defined in i2c_master.h * Parameters : NONE * Returns : NONE *******************************************************************************/ void ICACHE_FLASH_ATTR i2c_master_gpio_init(uint8 scl, uint8 sda, uint32 bitrate) { pinSCL = scl; pinSDA = sda; #if 0 ETS_GPIO_INTR_DISABLE(); // ETS_INTR_LOCK(); // Assume the pin mux is set before calling this function //PIN_FUNC_SELECT(I2C_MASTER_SDA_MUX, I2C_MASTER_SDA_FUNC); //PIN_FUNC_SELECT(I2C_MASTER_SCL_MUX, I2C_MASTER_SCL_FUNC); GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(sda)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(sda))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain; GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << sda)); GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(scl)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(scl))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain; GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << scl)); I2C_MASTER_SDA_SCL(1, 1); ETS_GPIO_INTR_ENABLE() ; // ETS_INTR_UNLOCK(); #endif I2C_MASTER_SDA_SCL(1, 1); i2c_master_init(); }
void bus_init(void) { #if ENABLE_BUS_I2C i2c_master_gpio_init(); i2c_master_init(); #endif #if ENABLE_BUS_ONEWIRE onewire_init(); #endif }
static void keyboard_master_setup(void) { #ifdef USE_I2C i2c_master_init(); #ifdef SSD1306OLED matrix_master_OLED_init(); #endif #else serial_master_init(); #endif }
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); }
int main (void) { //Do setup here serial_init(9600, 8, 0, 1); i2c_master_init(400); //Enable accelerometer in 2g measurement mode message[0] = ADDRESS << 1 | I2C_WRITE; message[1] = 0x16; message[2] = _BV(6) | _BV(2) | _BV(0); //6 == Disable external ready pin, 2 == 2g, 0 == measurement mode i2c_start_transceiver_with_data(message, 3); //Turn on 125Hz Filtering message[0] = ADDRESS << 1 | I2C_WRITE; message[1] = 0x18; message[2] = _BV(7); i2c_start_transceiver_with_data(message, 3); serial_write_s("milli-G Values (1000 == 1.000g)\n\r"); message[0] = ADDRESS << 1 | I2C_WRITE; message[1] = 0x10; message[2] = 0x00; message[3] = 0x00; message[4] = 0x00; message[5] = 0x00; message[6] = 0x00; message[7] = 0x00; i2c_start_transceiver_with_data(message, 8); show_reading(); //Calibrate device to 0g on X / Y axis and 1g on Z get_average_reading(data, 0xF0); message[0] = ADDRESS << 1 | I2C_WRITE; message[1] = 0x10; message[2] = data[0] * -2; message[3] = 0x00; message[4] = data[1] * -2; message[5] = 0x00; message[6] = (64 - data[2]) * 2; message[7] = 0x00; i2c_start_transceiver_with_data(message, 8); //Main program loop while (1) { show_reading(); } }
void matrix_init_user(void) { #ifdef USE_I2C i2c_master_init(); #ifdef SSD1306OLED // calls code for the SSD1306 OLED _delay_ms(400); TWI_Init(TWI_BIT_PRESCALE_1, TWI_BITLENGTH_FROM_FREQ(1, 800000)); iota_gfx_init(); // turns on the display #endif #endif #ifdef AUDIO_ENABLE startup_user(); #endif }
/* * @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; }
static void keyboard_master_setup(void) { #if defined(USE_I2C) || defined(EH) i2c_master_init(); #ifdef SSD1306OLED matrix_master_OLED_init (); #endif #else serial_master_init(); #endif // For master the Backlight info needs to be sent on startup // Otherwise the salve won't start with the proper info until an update BACKLIT_DIRTY = true; }
/** * 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); }
bool iota_gfx_init(bool rotate) { bool success = false; i2c_master_init(); send_cmd1(DisplayOff); send_cmd2(SetDisplayClockDiv, 0x80); send_cmd2(SetMultiPlex, DisplayHeight - 1); send_cmd2(SetDisplayOffset, 0); send_cmd1(SetStartLine | 0x0); send_cmd2(SetChargePump, 0x14 /* Enable */); send_cmd2(SetMemoryMode, 0 /* horizontal addressing */); if(rotate){ // the following Flip the display orientation 180 degrees send_cmd1(SegRemap); send_cmd1(ComScanInc); }else{ // Flips the display orientation 0 degrees send_cmd1(SegRemap | 0x1); send_cmd1(ComScanDec); } send_cmd2(SetComPins, 0x2); send_cmd2(SetContrast, 0x8f); send_cmd2(SetPreCharge, 0xf1); send_cmd2(SetVComDetect, 0x40); send_cmd1(DisplayAllOnResume); send_cmd1(NormalDisplay); send_cmd1(DeActivateScroll); send_cmd1(DisplayOn); send_cmd2(SetContrast, 0); // Dim clear_display(); success = true; iota_gfx_flush(); #if DEBUG_TO_SCREEN print_set_sendchar(capture_sendchar); #endif done: return success; }
/** * \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); }
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); }
/****************************************************************************** * FunctionName : i2c_master_gpio_init * Description : config SDA and SCL gpio to open-drain output mode, * mux and gpio num defined in i2c_master.h * Parameters : NONE * Returns : NONE *******************************************************************************/ void ICACHE_FLASH_ATTR i2c_master_gpio_init(void) { ETS_GPIO_INTR_DISABLE() ; // ETS_INTR_LOCK(); PIN_FUNC_SELECT(I2C_MASTER_SDA_MUX, I2C_MASTER_SDA_FUNC); PIN_FUNC_SELECT(I2C_MASTER_SCL_MUX, I2C_MASTER_SCL_FUNC); GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SDA_GPIO))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain; GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SDA_GPIO)); GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SCL_GPIO)), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(I2C_MASTER_SCL_GPIO))) | GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE)); //open drain; GPIO_REG_WRITE(GPIO_ENABLE_ADDRESS, GPIO_REG_READ(GPIO_ENABLE_ADDRESS) | (1 << I2C_MASTER_SCL_GPIO)); I2C_MASTER_SDA_HIGH_SCL_HIGH(); ETS_GPIO_INTR_ENABLE() ; // ETS_INTR_UNLOCK(); i2c_master_init(); }
/** * @brief Configures and activates the I2C peripheral. * * @param[in] i2cp pointer to the @p I2CDriver object * * @notapi */ void i2c_lld_start(I2CDriver *i2cp) { if (i2cp->state == I2C_STOP) { /* Enables the peripheral.*/ #if SAM3X_I2C_USE_I2C1 if (&I2CD1 == i2cp) { palSetPadMode(IOPORTA, 18, PAD_TO_A); palSetPadMode(IOPORTA, 17, PAD_TO_A); } #endif #if SAM3X_I2C_USE_I2C2 if (&I2CD2 == i2cp) { palSetPadMode(IOPORTB, 13, PAD_TO_A); palSetPadMode(IOPORTB, 12, PAD_TO_A); } #endif i2c_master_init(i2cp); } /* Configures the peripheral.*/ }
//! [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] }
int main (void){ //Do setup here serial_init(9600, 8, 0, 1); i2c_master_init(100); //Enable memory in normal mode message[0] = ADDRESS << 1 | I2C_WRITE; message[1] = 0x01; message[2] = 0x01; message[3] = 0x42; message[4] = 0x41; message[5] = 0x40; i2c_start_transceiver_with_data(message, 6); _delay_ms(100); //Main program loop while (1){ message[0] = ADDRESS << 1 | I2C_WRITE; message[1] = 0x01; //Reset register pointer message[2] = 0x01; i2c_start_transceiver_with_data(message, 3); message[0] = ADDRESS << 1 | I2C_READ; i2c_start_transceiver_with_data(message, 4); i2c_get_data_from_transceiver(message, 4); serial_write_s("value="); serial_write_s(itoa(message[1], temp, 16)); serial_write_s(", "); serial_write_s(itoa(message[2], temp, 16)); serial_write_s(", "); serial_write_s(itoa(message[3], temp, 16)); serial_write_s("\n\r"); _delay_ms(500); } }
void main(void) { stop_watchdog(); set_clk_8MHz(); interrupts_enable(); lcd_init(); uart_init(); uart_tx_string("Register Value:"); unsigned char i2creceive[8]; i2c_master_init(); unsigned char data[1] = {0x6F}; lcd_goto(1,0); lcd_print("R: "); lcd_print(int2HEXcharArray(*data)); lcd_goto(2,0); lcd_print("V: "); i2c_writeByte(1,data,0x50); __delay_cycles(300); // delay some time to get a better view on the signals on the scope unsigned char rx_byte = i2c_receiveSingleByte(0x50); __delay_cycles(300); uart_tx_string(int2HEXcharArray(rx_byte)); lcd_print(int2HEXcharArray(rx_byte)); __delay_cycles(300); uart_tx_string("\n"); for (;;) { } }
ch_bool InitializeCH7033(char* param) { ch_uint32 i, val_t; ch_uint32 hinc_reg, hinca_reg, hincb_reg; ch_uint32 vinc_reg, vinca_reg, vincb_reg; ch_uint32 hdinc_reg, hdinca_reg, hdincb_reg; ch_uint32 REGTABLE_LEN = 0; ch_uint32 *CH7033_RegTable = NULL; printf("CH7033 Init ...\n"); if(strcmp(param, "in_1024x768_out_hdmi_1280x720") == 0) { CH7033_RegTable = (ch_uint32*)CH7033_in_1024x768_out_HDMI_1280x720_RegTable; REGTABLE_LEN = (sizeof(CH7033_in_1024x768_out_HDMI_1280x720_RegTable))/(2*sizeof(ch_uint32)); } else if(strcmp(param, "in_1024x768_out_vga_1024x768") == 0) { CH7033_RegTable = (ch_uint32*)CH7033_in_1024x768_out_VGA_1024x768_RegTable; REGTABLE_LEN = (sizeof(CH7033_in_1024x768_out_VGA_1024x768_RegTable))/(2*sizeof(ch_uint32)); } else if(strcmp(param, "in_1024x768_out_vga_1280x1024") == 0) { CH7033_RegTable = (ch_uint32*)CH7033_in_1024x768_out_VGA_1280x1024_RegTable; REGTABLE_LEN = (sizeof(CH7033_in_1024x768_out_VGA_1280x1024_RegTable))/(2*sizeof(ch_uint32)); } else if(strcmp(param, "in_1024x600_out_hdmi_1280x720") == 0) { CH7033_RegTable = (ch_uint32*)CH7033_in_1024x600_out_hdmi_1280x720_RegTable; REGTABLE_LEN = (sizeof(CH7033_in_1024x600_out_hdmi_1280x720_RegTable))/(2*sizeof(ch_uint32)); } else { printf("Mode not supported by CH7033\n"); return ch_false; } printf("Mode = %s\n", param); i2c_master_init(0x76); //or 76 //1. write register table: for(i=0; i<REGTABLE_LEN; ++i) { //I2CWrite(CH7033_RegTable[i][0], CH7033_RegTable[i][1]); I2CWrite(CH7033_RegTable[2*i], CH7033_RegTable[2*i+1]); // printf("CH7033 Reg[0x%x] = 0x%x\n",CH7033_RegTable[2*i],I2CRead(CH7033_RegTable[2*i])); } //2. Calculate online parameters: I2CWrite(0x03, 0x00); i = I2CRead(0x25); I2CWrite(0x03, 0x04); //HINCA: val_t = I2CRead(0x2A); hinca_reg = (val_t << 3) | (I2CRead(0x2B) & 0x07); //HINCB: val_t = I2CRead(0x2C); hincb_reg = (val_t << 3) | (I2CRead(0x2D) & 0x07); //VINCA: val_t = I2CRead(0x2E); vinca_reg = (val_t << 3) | (I2CRead(0x2F) & 0x07); //VINCB: val_t = I2CRead(0x30); vincb_reg = (val_t << 3) | (I2CRead(0x31) & 0x07); //HDINCA: val_t = I2CRead(0x32); hdinca_reg = (val_t << 3) | (I2CRead(0x33) & 0x07); //HDINCB: val_t = I2CRead(0x34); hdincb_reg = (val_t << 3) | (I2CRead(0x35) & 0x07); //no calculate hdinc if down sample disaled if(i & (1 << 6)) { if(hdincb_reg == 0) { return ch_false; } //hdinc_reg = (ch_uint32)(((ch_uint64)hdinca_reg) * (1 << 20) / hdincb_reg); hdinc_reg = (ch_uint32)lldiv(((ch_uint64)hdinca_reg) * (1 << 20), hdincb_reg); I2CWrite(0x3C, (hdinc_reg >> 16) & 0xFF); I2CWrite(0x3D, (hdinc_reg >> 8) & 0xFF); I2CWrite(0x3E, (hdinc_reg >> 0) & 0xFF); } if(hincb_reg == 0 || vincb_reg == 0) { return ch_false; } if(hinca_reg > hincb_reg) { return ch_false; } //hinc_reg = (ch_uint32)((ch_uint64)hinca_reg * (1 << 20) / hincb_reg); hinc_reg = (ch_uint32)lldiv(((ch_uint64)hinca_reg) * (1 << 20), hincb_reg); //vinc_reg = (ch_uint32)((ch_uint64)vinca_reg * (1 << 20) / vincb_reg); vinc_reg = (ch_uint32)lldiv(((ch_uint64)vinca_reg) * (1 << 20), vincb_reg); I2CWrite(0x36, (hinc_reg >> 16) & 0xFF); I2CWrite(0x37, (hinc_reg >> 8) & 0xFF); I2CWrite(0x38, (hinc_reg >> 0) & 0xFF); I2CWrite(0x39, (vinc_reg >> 16) & 0xFF); I2CWrite(0x3A, (vinc_reg >> 8) & 0xFF); I2CWrite(0x3B, (vinc_reg >> 0) & 0xFF); //3. Start to running: I2CWrite(0x03, 0x00); val_t = I2CRead(0x0A); I2CWrite(0x0A, val_t | 0x80); I2CWrite(0x0A, val_t & 0x7F); val_t = I2CRead(0x0A); I2CWrite(0x0A, val_t & 0xEF); I2CWrite(0x0A, val_t | 0x10); I2CWrite(0x0A, val_t & 0xEF); printf("done\n"); return ch_true; }
void i2c_shared_master_init(REFERENCE_PARAM(struct r_i2c, i2cPorts)) { swlock_acquire(&i2c_swlock); i2c_master_init(i2cPorts); swlock_release(&i2c_swlock); }
/** * \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 */ } }
/* * @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; }
int main () { pio_config_set (LED_GREEN_PIO, PIO_OUTPUT_HIGH); pio_config_set (LED_ORANGE_PIO, PIO_OUTPUT_HIGH); pio_config_set (LED_RED_PIO, PIO_OUTPUT_HIGH); pio_config_set (LED_BLUE_PIO, PIO_OUTPUT_HIGH); /* Initialise i2c */ i2c_motor = i2c_master_init (&i2c_bus_cfg, &i2c_motor_cfg); i2c_camera = i2c_master_init (&i2c_bus_cfg, &i2c_camera_cfg); /* Initialise bluetooth */ bluetooth_t btconn = bluetooth_init(115200); kernel_init (); kernel_taskRegister (bluetooth_comms_task, BLUETOOTH_COMMS_TASK, btconn, 10); kernel_taskRegister (action_commands_task, ACTION_COMMANDS_TASK, btconn, 100); kernel_start (); return 0; }