Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. /**\mainpage
  2.  * MB85RC64TA Comms - v1.0a (24.09.19)
  3.  *
  4.  * Testing direct register access over I2C
  5.  *
  6.  * ANTALIFE
  7.  */
  8.  
  9. #include <stdio.h>
  10. #include "boards.h"
  11. #include "app_util_platform.h"
  12. #include "app_error.h"
  13. #include "nrf_drv_twi.h"
  14. #include "nrf_delay.h"
  15.  
  16. #define MB85RC64TA_ADDR     0x50
  17.  
  18. /****************************** I2C initialisation definition START ******************************/
  19. #define TWI_INSTANCE_ID     0 //If you want to change to 1 then you need to define TWI1_ENABLED 1" in "sdk_config.h"
  20.  
  21. /* TWI instance define */
  22. static const nrf_drv_twi_t i2c_instance = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
  23.  
  24. /*
  25.  * Initialize all hardware/software components that are needed for the I2C comms
  26.  *
  27.  * @returns 0 on success, otherwise an error code is given on failure
  28.  */
  29. int16_t i2c_init()
  30. {
  31.     ret_code_t err_code;
  32.  
  33.     const nrf_drv_twi_config_t i2c_instance_config =
  34.     {
  35.         .scl                = I2C_SCL_PIN,
  36.         .sda                = I2C_SDA_PIN,
  37.         .frequency          = NRF_TWI_FREQ_100K,
  38.         .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
  39.         .clear_bus_init     = false
  40.     };
  41.  
  42.     /* TWI instance initiate */
  43.     err_code = nrf_drv_twi_init(&i2c_instance, &i2c_instance_config, NULL, NULL);
  44.     if (err_code)
  45.     {
  46.         printf("Error %d: Initialization of I2C connection failed!\n", err_code);
  47.     }
  48.  
  49.     /* TWI instance enable */
  50.     nrf_drv_twi_enable(&i2c_instance);
  51.     return err_code;
  52. }
  53. /****************************** I2C initialisation definition END ******************************/
  54.  
  55.  
  56.  
  57. /**
  58.  * ADD NOTES HERE
  59.  */
  60. int main(void)
  61. {
  62.     /* Initialize I2C */
  63.     i2c_init();
  64.  
  65.     ret_code_t err_code;
  66.  
  67.     //Useful tools:
  68.     // https://www.rapidtables.com/convert/number/binary-to-hex.html
  69.     // https://www.rapidtables.com/convert/number/hex-to-decimal.html
  70.     uint8_t REG_ADDR_MSB  = 0x15; //0001 0101
  71.     uint8_t REG_ADDR_LSB  = 0x10; //0001 0000
  72.     uint8_t REG_DATA      = 0x30; //0011 0000
  73.     uint32_t GLOBAL_DELAY = 1000;
  74.  
  75.  
  76.     ////////////////////// WRITING VALUE TO REGISTER
  77.     uint8_t VAL_TX_1[3] = {REG_ADDR_MSB, REG_ADDR_LSB, REG_DATA};
  78.  
  79.     err_code = nrf_drv_twi_tx(&i2c_instance, MB85RC64TA_ADDR, VAL_TX_1, sizeof(VAL_TX_1), true);
  80.     if(err_code == NRF_SUCCESS)
  81.     {
  82.         printf("Device address, register address, & register value sent\n");
  83.     }
  84.     ////////////////////// WRITING VALUE TO REGISTER
  85.    
  86.  
  87.     nrf_delay_ms(GLOBAL_DELAY);
  88.  
  89.  
  90.     ////////////////////// READING VALUE FROM REGISTER
  91.     uint8_t VAL_TX_2[2] = {REG_ADDR_MSB, REG_ADDR_LSB};
  92.  
  93.     err_code = nrf_drv_twi_tx(&i2c_instance, MB85RC64TA_ADDR, VAL_TX_2, sizeof(VAL_TX_2), true);
  94.     if(err_code == NRF_SUCCESS)
  95.     {
  96.         printf("Device address & register address sent\n");
  97.     }
  98.  
  99.  
  100.     nrf_delay_ms(GLOBAL_DELAY);
  101.  
  102.  
  103.     uint8_t REG_VAL;
  104.  
  105.     err_code = nrf_drv_twi_rx(&i2c_instance, MB85RC64TA_ADDR, &REG_VAL, sizeof(REG_VAL));
  106.     if (err_code == NRF_SUCCESS)
  107.     {
  108.         printf("The Register read = 0x%x\n", REG_VAL);
  109.     }
  110.     ////////////////////// READING VALUE FROM REGISTER
  111.  
  112.     /* ADD NOTES HERE */
  113.     while (1)
  114.     {
  115.         //DO STUFF HERE
  116.         //nrf_gpio_pin_write(6, 0);
  117.     }
  118. }
  119.  
  120. /**
  121.  * v1.0a  Initial try of register access via I2C
  122.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement