/******************************************************************************* * @file ir_i2c.c * @brief ******************************************************************************/ /* board driver */ #include #include #include #include #include "nrf.h" #include "app_error.h" #include "boards.h" #include "nrfx_gpiote.h" #include "nrfx_twi.h" #include "nrf_drv_twi.h" #include "nrf_delay.h" #include "ir_i2c.h" #include "debug_print.h" /* I2C number and slave address for DS39305272 */ #define LED_1_I2C_ADDR 0x50 #define AD5272_MAX_SERIAL_WRITE 16 int16_t read_from_DS3930 = 0; uint16_t data_160_to_write = 0; static volatile bool m_xfer_done = false; /* TWI instance. */ //const nrfx_twi_t m_twi_ir = NRFX_TWI_INSTANCE(IR_I2C_INSTANCE); const nrf_drv_twi_t m_twi_ir = NRF_DRV_TWI_INSTANCE(IR_I2C_INSTANCE); //void twi_handler(nrfx_twi_evt_t const * p_event, void * p_context) //{ // m_xfer_done = true; //} //void ir_irq_init(void){ // ret_code_t err_code; // /* Initialize int pin */ // if (!nrfx_gpiote_is_init()) // { // err_code = nrfx_gpiote_init(); // APP_ERROR_CHECK(err_code); // } // nrfx_gpiote_in_config_t in_config = NRFX_GPIOTE_CONFIG_IN_SENSE_LOTOHI(true); // in_config.pull = NRF_GPIO_PIN_PULLDOWN; // err_code = nrfx_gpiote_in_init(ADA2200_SYNCO_PIN, &in_config, NULL); // APP_ERROR_CHECK(err_code); // nrfx_gpiote_in_event_enable(ADA2200_SYNCO_PIN, true); //} //void ir_irq_uninit(void){ // nrfx_gpiote_in_event_disable(ADA2200_SYNCO_PIN); // nrfx_gpiote_in_uninit(ADA2200_SYNCO_PIN); //} void ir_i2c_uninit(void){ nrf_drv_twi_disable(&m_twi_ir); nrf_drv_twi_uninit(&m_twi_ir); // } void ir_i2c_init(void){ ret_code_t err_code; const nrf_drv_twi_config_t twi_ir_config = { .scl = IR_I2C_SCL_PIN, .sda = IR_I2C_SDA_PIN, .frequency = NRF_DRV_TWI_FREQ_100K, .interrupt_priority = APP_IRQ_PRIORITY_HIGH, .clear_bus_init = false }; err_code = nrf_drv_twi_init(&m_twi_ir, &twi_ir_config, NULL, NULL); APP_ERROR_CHECK(err_code); if (err_code != NRF_SUCCESS) { DBG_PRINTF("TWI ir_irq_init Error."); } nrf_drv_twi_enable(&m_twi_ir); // ir_irq_init(); } uint8_t ir_command_read(uint8_t device_id, uint8_t address, uint8_t *data) { uint8_t read_data = 0; char adata[8]; ret_code_t err_code; //address = 1|(address<<1); address = (address & 0xFF); err_code = nrf_drv_twi_tx(&m_twi_ir, device_id, &address, 1, true); if (err_code != NRF_SUCCESS) { // Handle error // return; } err_code = nrf_drv_twi_rx(&m_twi_ir, device_id, data, 8); if (err_code != NRF_SUCCESS) { // Handle error DBG_PRINTF("TWI read Error."); return 0; } read_data = data[0]; memcpy(adata,data,8); // DBG_PRINTF("ir Data %x %x %x %x. \r\n", device_id, address, data[0], data[1]); //DBG_PRINTF("ir Data read %s . \r\n", adata); return read_data; } void ir_command_write(uint8_t device_id, uint8_t address, uint8_t data) { //uint16_t data_to_write = 0; uint8_t buffer[7]={0x00,0x00,0x00,0x00,0x00,0x00,0x00}; address = (address & 0xFF); //buffer[0] = 0x00; buffer[0] = (address); buffer[1] =(data & 0xFF); // buffer[2] = data1+1; // buffer[3] = data1+2; // buffer[4] = data1+3; // buffer[5] = data1+4; // buffer[6] = data1+5; //memcpy(&buffer[1], data, length ); ret_code_t err_code; //err_code = nrf_drv_twi_tx(&m_twi_ir, device_id, 0x00, 1, false); err_code = nrf_drv_twi_tx(&m_twi_ir, device_id, buffer, 2, false); DBG_PRINTF("ir Write Data %x %x %x %x. \r\n", device_id, buffer[0], buffer[1], buffer[2]); //err_code = nrf_drv_twi_tx(&m_twi_ir, device_id, buffer, 6, false); if (err_code != NRF_SUCCESS) { DBG_PRINTF("TWI Error."); } }