2016年5月31日 星期二

[Life] NuMicro Family - NuEdu-SDK-M451

1. 最近公司找了一家新的Solution方案公司, 不知道跟ST的比較起來差多少, 有空再來研究一下.


2016年5月12日 星期四

nRF52832 I2C Master Driver

1. 最近才知道原來Nordic有新版的i2c driver, 簡單地做個備份, 才知道以後要怎樣用.

2. 需先include nrf_drv_twi.c & nrf_drv_twi.h, 路徑在\components\drivers_nrf\twi_master\.

3. 需記得打開Define: TWIM_IN_USE.


4. 本範例使用TWI1, 所以記得需至nrf_drv_config.h打開相關的設定及設定I2C GPIO和I2C Frequency.


5. 接下來就是I2C.h & I2C.c的簡易程式碼.

I2C.h
  1. #ifndef __I2C_H__
  2. #define __I2C_H__
  3.  
  4. #include "nrf_drv_twi.h"
  5.  
  6. bool i2c_write( uint8_t device_address, uint8_t register_address, uint8_t *value, uint8_t number_of_bytes );
  7. bool i2c_read( uint8_t device_address, uint8_t register_address, uint8_t *destination, uint8_t number_of_bytes );
  8.  
  9. #endif /* __I2C_H__ */
  10.  
I2C.c
  1. #include "I2C.h"
  2.  
  3. bool i2c_write(uint8_t device_address, uint8_t register_address, uint8_t *value, uint8_t number_of_bytes )
  4. {
  5. #define i2c_write_data_len 6
  6. uint8_t w2_data[i2c_write_data_len+1], i;
  7. const nrf_drv_twi_t twi = NRF_DRV_TWI_INSTANCE(1);
  8. nrf_drv_twi_init(&twi, NULL, NULL, NULL);
  9. w2_data[0] = register_address;
  10. for ( i = 0 ; i < number_of_bytes ; i++ ) {
  11. w2_data[i +1] = value[i];
  12. }
  13. nrf_drv_twi_enable(&twi);
  14. nrf_drv_twi_tx(&twi, (device_address)>>1, w2_data, number_of_bytes+1, false);
  15. return true;
  16. }
  17.  
  18. bool i2c_read(uint8_t device_address, uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes)
  19. {
  20. const nrf_drv_twi_t twi = NRF_DRV_TWI_INSTANCE(1);
  21. nrf_drv_twi_init(&twi, NULL, NULL, NULL);
  22. nrf_drv_twi_enable(&twi);
  23. nrf_drv_twi_tx(&twi, (device_address)>>1, &register_address, 1, true);
  24. nrf_drv_twi_rx(&twi, (device_address)>>1, destination, number_of_bytes);
  25. return true;
  26. }
  27.