Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**\mainpage
- * MB85RC64TA Comms - v1.0a (24.09.19)
- *
- * Testing direct register access over I2C
- *
- * ANTALIFE
- */
- #include <stdio.h>
- #include "boards.h"
- #include "app_util_platform.h"
- #include "app_error.h"
- #include "nrf_drv_twi.h"
- #include "nrf_delay.h"
- #define MB85RC64TA_ADDR 0x50
- /****************************** I2C initialisation definition START ******************************/
- #define TWI_INSTANCE_ID 0 //If you want to change to 1 then you need to define TWI1_ENABLED 1" in "sdk_config.h"
- /* TWI instance define */
- static const nrf_drv_twi_t i2c_instance = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
- /*
- * Initialize all hardware/software components that are needed for the I2C comms
- *
- * @returns 0 on success, otherwise an error code is given on failure
- */
- int16_t i2c_init()
- {
- ret_code_t err_code;
- const nrf_drv_twi_config_t i2c_instance_config =
- {
- .scl = I2C_SCL_PIN,
- .sda = I2C_SDA_PIN,
- .frequency = NRF_TWI_FREQ_100K,
- .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
- .clear_bus_init = false
- };
- /* TWI instance initiate */
- err_code = nrf_drv_twi_init(&i2c_instance, &i2c_instance_config, NULL, NULL);
- if (err_code)
- {
- printf("Error %d: Initialization of I2C connection failed!\n", err_code);
- }
- /* TWI instance enable */
- nrf_drv_twi_enable(&i2c_instance);
- return err_code;
- }
- /****************************** I2C initialisation definition END ******************************/
- /**
- * ADD NOTES HERE
- */
- int main(void)
- {
- /* Initialize I2C */
- i2c_init();
- ret_code_t err_code;
- //Useful tools:
- // https://www.rapidtables.com/convert/number/binary-to-hex.html
- // https://www.rapidtables.com/convert/number/hex-to-decimal.html
- uint8_t REG_ADDR_MSB = 0x15; //0001 0101
- uint8_t REG_ADDR_LSB = 0x10; //0001 0000
- uint8_t REG_DATA = 0x30; //0011 0000
- uint32_t GLOBAL_DELAY = 1000;
- ////////////////////// WRITING VALUE TO REGISTER
- uint8_t VAL_TX_1[3] = {REG_ADDR_MSB, REG_ADDR_LSB, REG_DATA};
- err_code = nrf_drv_twi_tx(&i2c_instance, MB85RC64TA_ADDR, VAL_TX_1, sizeof(VAL_TX_1), true);
- if(err_code == NRF_SUCCESS)
- {
- printf("Device address, register address, & register value sent\n");
- }
- ////////////////////// WRITING VALUE TO REGISTER
- nrf_delay_ms(GLOBAL_DELAY);
- ////////////////////// READING VALUE FROM REGISTER
- uint8_t VAL_TX_2[2] = {REG_ADDR_MSB, REG_ADDR_LSB};
- err_code = nrf_drv_twi_tx(&i2c_instance, MB85RC64TA_ADDR, VAL_TX_2, sizeof(VAL_TX_2), true);
- if(err_code == NRF_SUCCESS)
- {
- printf("Device address & register address sent\n");
- }
- nrf_delay_ms(GLOBAL_DELAY);
- uint8_t REG_VAL;
- err_code = nrf_drv_twi_rx(&i2c_instance, MB85RC64TA_ADDR, ®_VAL, sizeof(REG_VAL));
- if (err_code == NRF_SUCCESS)
- {
- printf("The Register read = 0x%x\n", REG_VAL);
- }
- ////////////////////// READING VALUE FROM REGISTER
- /* ADD NOTES HERE */
- while (1)
- {
- //DO STUFF HERE
- //nrf_gpio_pin_write(6, 0);
- }
- }
- /**
- * v1.0a Initial try of register access via I2C
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement