void jshI2CRead(IOEventFlags device, unsigned char address, int nBytes, unsigned char *data, bool sendStop) { const nrf_drv_twi_t *twi = jshGetTWI(device); if (!twi) return; uint32_t err_code = nrf_drv_twi_rx(twi, address, data, nBytes); if (err_code != NRF_SUCCESS) jsExceptionHere(JSET_INTERNALERROR, "I2C Read Error %d\n", err_code); }
/** Set up I2C, if pins are -1 they will be guessed */ void jshI2CSetup(IOEventFlags device, JshI2CInfo *inf) { NRF_TWI_Type *twi = jshGetTWI(device); // jshPinSetState // nrf_twi_frequency_set(twi, inf->bitrate); // nrf_twi_pins_set(twi, pinInfo[inf->pinSCL].pin, pinInfo[inf->pinSDA].pin); // nrf_twi_enable }
/** Set up I2C, if pins are -1 they will be guessed */ void jshI2CSetup(IOEventFlags device, JshI2CInfo *inf) { if (!jshIsPinValid(inf->pinSCL) || !jshIsPinValid(inf->pinSDA)) { jsError("SDA and SCL pins must be valid, got %d and %d\n", inf->pinSDA, inf->pinSCL); return; } const nrf_drv_twi_t *twi = jshGetTWI(device); if (!twi) return; // http://infocenter.nordicsemi.com/index.jsp?topic=%2Fcom.nordic.infocenter.sdk51.v9.0.0%2Fhardware_driver_twi.html&cp=4_1_0_2_10 nrf_drv_twi_config_t p_twi_config; p_twi_config.scl = (uint32_t)pinInfo[inf->pinSCL].pin; p_twi_config.sda = (uint32_t)pinInfo[inf->pinSDA].pin; p_twi_config.frequency = (inf->bitrate<175000) ? NRF_TWI_FREQ_100K : ((inf->bitrate<325000) ? NRF_TWI_FREQ_250K : NRF_TWI_FREQ_400K); p_twi_config.interrupt_priority = APP_IRQ_PRIORITY_LOW; if (twi1Initialised) nrf_drv_twi_uninit(twi); twi1Initialised = true; uint32_t err_code = nrf_drv_twi_init(twi, &p_twi_config, NULL, NULL); if (err_code != NRF_SUCCESS) jsExceptionHere(JSET_INTERNALERROR, "I2C Initialisation Error %d\n", err_code); else nrf_drv_twi_enable(twi); }
/** Addresses are 7 bit - that is, between 0 and 0x7F. sendStop is whether to send a stop bit or not */ void jshI2CWrite(IOEventFlags device, unsigned char address, int nBytes, const unsigned char *data, bool sendStop) { NRF_TWI_Type *twi = jshGetTWI(device); // nrf_twi_address_set(twi, address); }