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
#ifndef __I2C_H__
#define __I2C_H__

#include "nrf_drv_twi.h"

bool i2c_write( uint8_t device_address, uint8_t register_address, uint8_t *value, uint8_t number_of_bytes );
bool i2c_read( uint8_t device_address, uint8_t register_address, uint8_t *destination, uint8_t number_of_bytes );

#endif /* __I2C_H__ */

I2C.c
#include "I2C.h"

bool i2c_write(uint8_t device_address, uint8_t register_address, uint8_t *value, uint8_t number_of_bytes )
{  
 #define i2c_write_data_len 6
 uint8_t w2_data[i2c_write_data_len+1], i;
 
 const nrf_drv_twi_t twi = NRF_DRV_TWI_INSTANCE(1);
 nrf_drv_twi_init(&twi, NULL, NULL, NULL);
    
 w2_data[0] = register_address;
 for ( i = 0 ; i < number_of_bytes ; i++ ) {
  w2_data[i +1] = value[i];  
 }
 
 nrf_drv_twi_enable(&twi);
 nrf_drv_twi_tx(&twi, (device_address)>>1, w2_data, number_of_bytes+1, false);
 
 return true;
}

bool i2c_read(uint8_t device_address, uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes)
{  
 const nrf_drv_twi_t twi = NRF_DRV_TWI_INSTANCE(1);
 nrf_drv_twi_init(&twi, NULL, NULL, NULL);
 nrf_drv_twi_enable(&twi);
  
 nrf_drv_twi_tx(&twi, (device_address)>>1, &register_address, 1, true);
 nrf_drv_twi_rx(&twi, (device_address)>>1, destination, number_of_bytes);
   
 return true;  
}