Hi every body.
I have trying without success to attach an event handler function to the TWI.
I am reading a temperature and humidity sensor using I2C. In polling mode everything is working perfectly but when I try to attach a callback function, that function is never called.
Any idea about what I am doing wrong?
Here is the code I am implementing:
// ########## M.I. BSP ########## #include "bsp_handler.h" // ########## M.I. Lib ########## #include "board.h" #include "uart_handler.h" #include "nrfx_twim.h" // i2c parameters for SHT35 #define SHT35_I2C_ADDRESS 0x44 #define CMD_SOFT_RST0 0xA2 // soft reset command LSB #define CMD_SOFT_RST1 0x30 // soft reset command MSB #define HIGH_REP_WITH_STRCH0 0x06 // request command LSB #define HIGH_REP_WITH_STRCH1 0x2C // request command MSB #define NRF52833_XXAA static const char messageBuffer[] = "*** DEMO EHAL-I2C Driver ***\r\n" "*** Website: https://monitoreointeligente.com/ ***\r\n"; // setup UART uart_instance SerialExt(UART0_PORT_AVAILABLE); uint8_t StringDebug[SERIAL_BUFFER_SIZE] = {}; uint8_t rx_buffer[SERIAL_BUFFER_SIZE] = {}; uint8_t tx_buffer[SERIAL_BUFFER_SIZE] = {}; // setup I2C nrfx_twim_t i2c_instance = NRFX_TWIM_INSTANCE(0); uint8_t i2c_rx[6] = {0}; uint8_t i2c_tx[2] = {CMD_SOFT_RST1, CMD_SOFT_RST0}; void i2cEventHandler(nrfx_twim_evt_t const *p_event, void *p_context) { int n; switch(p_event->type) { case NRFX_TWIM_EVT_BUS_ERROR: n = sprintf((char*)StringDebug, "Error\r\n"); SerialExt.write(StringDebug, false, n); break; case NRFX_TWIM_EVT_DATA_NACK: n = sprintf((char*)StringDebug, "Data Nack\r\n"); SerialExt.write(StringDebug, false, n); break; case NRFX_TWIM_EVT_DONE: n = sprintf((char*)StringDebug, "Done!\r\n"); SerialExt.write(StringDebug, false, n); break; default: break; } } int main(void) { halSetup(); int n; float temp; float humidity; nrfx_twim_xfer_desc_t tx_config; nrfx_twim_xfer_desc_t rx_config; // setup UART SerialExt.setup(rx_buffer, SERIAL_BUFFER_SIZE, tx_buffer, SERIAL_BUFFER_SIZE, '\n'); SerialExt.write((uint8_t*)messageBuffer, false, sizeof(messageBuffer)); // setup I2C nrfx_twim_config_t i2c0_config = NRFX_TWIM_DEFAULT_CONFIG(17, 16); i2c0_config.interrupt_priority = 2; //if(nrfx_twim_init(&i2c_instance, &i2c0_config, NULL, NULL) == NRFX_SUCCESS) if(nrfx_twim_init(&i2c_instance, &i2c0_config, i2cEventHandler, NULL) == NRFX_SUCCESS) { n = sprintf((char*)StringDebug, "I2C init OK!\r\n"); SerialExt.write(StringDebug, false, n); } nrfx_twim_enable(&i2c_instance); tx_config = NRFX_TWIM_XFER_DESC_TX(SHT35_I2C_ADDRESS, i2c_tx, sizeof(i2c_tx)); rx_config = NRFX_TWIM_XFER_DESC_RX(SHT35_I2C_ADDRESS, i2c_rx, sizeof(i2c_rx)); if(nrfx_twim_xfer(&i2c_instance, &tx_config, NRFX_TWIM_FLAG_NO_XFER_EVT_HANDLER) == NRFX_SUCCESS) { n = sprintf((char*)StringDebug, "Soft Reset command sent!\r\n"); SerialExt.write(StringDebug, false, n); } while (true) { // request command i2c_tx[0] = HIGH_REP_WITH_STRCH1; i2c_tx[1] = HIGH_REP_WITH_STRCH0; /* tx_config = NRFX_TWIM_XFER_DESC_TX(SHT35_I2C_ADDRESS, i2c_tx, sizeof(i2c_tx)); if(nrfx_twim_xfer(&i2c_instance, &tx_config, NRFX_TWIM_FLAG_REPEATED_XFER) == NRFX_SUCCESS) { n = sprintf((char*)StringDebug, "Temp/Humidity command sent!\r\n"); SerialExt.write(StringDebug, false, n); } rx_config = NRFX_TWIM_XFER_DESC_RX(SHT35_I2C_ADDRESS, i2c_rx, sizeof(i2c_rx)); if(nrfx_twim_xfer(&i2c_instance, &rx_config, NRFX_TWIM_FLAG_REPEATED_XFER) == NRFX_SUCCESS) { // mapping values temp = (175.0 / 65535.0) * (((int16_t)i2c_rx[0] << 8 ) | (int16_t)i2c_rx[1]) - 45; humidity = (100.0 / 65535.0) * (((int16_t)i2c_rx[3] << 8) | (int16_t)i2c_rx[4]); n = sprintf((char*)StringDebug, "Data received temp: %.2f humidity %.2f\r\n", temp, humidity); SerialExt.write(StringDebug, false, n); } */ tx_config = NRFX_TWIM_XFER_DESC_TXRX(SHT35_I2C_ADDRESS, i2c_tx, sizeof(i2c_tx), i2c_rx, sizeof(i2c_rx)); if(nrfx_twim_xfer(&i2c_instance, &tx_config, NRFX_TWIM_FLAG_REPEATED_XFER) == NRFX_SUCCESS) { // mapping values temp = (175.0 / 65535.0) * (((int16_t)i2c_rx[0] << 8 ) | (int16_t)i2c_rx[1]) - 45; humidity = (100.0 / 65535.0) * (((int16_t)i2c_rx[3] << 8) | (int16_t)i2c_rx[4]); n = sprintf((char*)StringDebug, "Data received temp: %.2f humidity %.2f\r\n", temp, humidity); SerialExt.write(StringDebug, false, n); } memset(i2c_rx, 0, sizeof(i2c_rx)); k_msleep(1000); } }
Here is the prj.conf
# Enable the UART driver CONFIG_UART_ASYNC_API=y CONFIG_NRFX_UARTE0=y CONFIG_UART_0_ASYNC=y CONFIG_SERIAL=y CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_UART_0_INTERRUPT_DRIVEN=n CONFIG_UART_USE_RUNTIME_CONFIGURE=y CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y # Enable I2C CONFIG_I2C=y CONFIG_I2C_NRFX=y CONFIG_NRFX_TWIM=y CONFIG_NRFX_TWIM0=y CONFIG_NRFX_TWIM1=n CONFIG_NRFX_PRS=y CONFIG_NRFX_PRS_BOX_0=y # Heap memory pool and Stack memory size 256/1024/4096/16384 CONFIG_HEAP_MEM_POOL_SIZE=1024 #CONFIG_MAIN_STACK_SIZE=16384 # Enable gpio CONFIG_GPIO=y # Enable timer CONFIG_NRFX_TIMER=y CONFIG_NRFX_TIMER0=y CONFIG_NRFX_TIMER1=y CONFIG_NRFX_TIMER2=y CONFIG_NRFX_TIMER3=y CONFIG_NRFX_TIMER4=y # Heap memory pool 256/1024/4096/16384 CONFIG_HEAP_MEM_POOL_SIZE=1024 # C++ config CONFIG_NEWLIB_LIBC=y CONFIG_CPLUSPLUS=y #CONFIG_CPP_STATIC_INIT_GNU=y