2016年3月4日 星期五

2016年3月2日 星期三

STM32F103 I2C Master Driver

話不說多, 直接上code.

I2C_Master.h

#include "stm32f10x.h"

void I2C_Master_Init(void);
void I2C_Master_DeInit(void);
int I2C_Master_Read(uint8_t deviceAddr, uint8_t readAddr, uint8_t* pBuffer, uint16_t numByteToRead);
int I2C_Master_Write(uint8_t deviceAddress, uint8_t WriteAddr, uint8_t* pBuffer, uint16_t numByteToWrite);
int I2C_TIMEOUT_UserCallback(void);


I2C_Master.c

#include "I2C_Master.h"

#define I2C_MEMS                I2C1
#define I2C_MEMS_CLK            RCC_APB1Periph_I2C1
#define I2C_MEMS_GPIO           GPIOB
#define I2C_MEMS_GPIO_CLK       RCC_APB2Periph_GPIOB
#define I2C_MEMS_SCL            GPIO_Pin_6
#define I2C_MEMS_SDA            GPIO_Pin_7

#define I2C_Speed               400000
#define I2C_SLAVE_ADDRESS7      0xA0
#define I2C_TIMEOUT             3000

/* I2C STOP mask */
#define CR1_STOP_Set            ((uint16_t)0x0200)
#define CR1_STOP_Reset          ((uint16_t)0xFDFF)

/* I2C ACK mask */
#define CR1_ACK_Set             ((uint16_t)0x0400)
#define CR1_ACK_Reset           ((uint16_t)0xFBFF)

/* I2C POS mask */
#define CR1_POS_Set             ((uint16_t)0x0800)
#define CR1_POS_Reset           ((uint16_t)0xF7FF)

#define NULL ((void *)0)


void I2C_Master_Init(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure; 
  I2C_InitTypeDef  I2C_InitStructure; 
  
  /* I2C Periph clock enable */
  RCC_APB1PeriphClockCmd(I2C_MEMS_CLK, ENABLE);   
  
  /* GPIO Periph clock enable */
  RCC_APB2PeriphClockCmd(I2C_MEMS_GPIO_CLK, ENABLE);    
  
  /* GPIO configuration */
  GPIO_InitStructure.GPIO_Pin =  I2C_MEMS_SCL | I2C_MEMS_SDA; 
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
  GPIO_Init(I2C_MEMS_GPIO, &GPIO_InitStructure);  
  
   /* I2C configuration */
  I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
  I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_ADDRESS7;
  I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  I2C_InitStructure.I2C_ClockSpeed = I2C_Speed;
  
  /* I2C Peripheral Enable */
  I2C_Cmd(I2C_MEMS, ENABLE);
  /* Apply I2C configuration after enabling it */
  I2C_Init(I2C_MEMS, &I2C_InitStructure);
}

void I2C_Master_DeInit(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;  
    
  /* UnConfigure I2C */
  I2C_DeInit(I2C_MEMS);
  I2C_Cmd(I2C_MEMS, DISABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, DISABLE);
           
  /* UnConfigure I2C_MEMS pins: SCL and SDA */
  GPIO_InitStructure.GPIO_Pin = I2C_MEMS_SCL | I2C_MEMS_SDA; 
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(I2C_MEMS_GPIO, &GPIO_InitStructure);  
  
}


int I2C_Master_Read(uint8_t deviceAddr, uint8_t readAddr, uint8_t* pBuffer, uint16_t numByteToRead) {
  
  __IO uint32_t temp = 0;
  volatile int I2C_TimeOut = 0;
  
  // /* While the bus is busy * /
  I2C_TimeOut = I2C_TIMEOUT;
  while(I2C_GetFlagStatus(I2C_MEMS, I2C_FLAG_BUSY))
  {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
  }
  
  
  // * Send START condition * /
  I2C_GenerateSTART(I2C_MEMS, ENABLE);

  // / * Test on EV5 and clear it * /
  I2C_TimeOut = I2C_TIMEOUT;
  while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_MODE_SELECT))
  {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
  }
  
  // / * Send EEPROM address for write  * /
  I2C_Send7bitAddress(I2C_MEMS, deviceAddr, I2C_Direction_Transmitter);

  // / * Test on EV6 and clear it * /
  I2C_TimeOut = I2C_TIMEOUT;
  while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
  }
  
  // / * Send the EEPROM's internal address to read from: Only one byte address  * /
  I2C_SendData(I2C_MEMS, readAddr);  

  /// * Test on EV8 and clear it * /
  I2C_TimeOut = I2C_TIMEOUT;
  while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
  }  
    
  /// * Send STRAT condition a second time * /  
  I2C_GenerateSTART(I2C_MEMS, ENABLE);

  /// * Test on EV5 and clear it * /
  I2C_TimeOut = I2C_TIMEOUT;
  while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_MODE_SELECT))
  {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
  }  
  
    // * Send EEPROM address for read * /
  I2C_Send7bitAddress(I2C_MEMS, deviceAddr, I2C_Direction_Receiver);  

  if (numByteToRead == 1)  {
    /* Wait until ADDR is set */
    I2C_TimeOut = I2C_TIMEOUT;
    while ((I2C_MEMS->SR1&0x0002) != 0x0002)
    {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
    }    
   /* Clear ACK bit */
    I2C_MEMS->CR1 &= CR1_ACK_Reset;
    /* Disable all active IRQs around ADDR clearing and STOP programming because the EV6_3
    software sequence must complete before the current byte end of transfer */
    __disable_irq();
    /* Clear ADDR flag */
    temp = I2C_MEMS->SR2;
    /* Program the STOP */
    I2C_GenerateSTOP(I2C_MEMS, ENABLE);
    /* Re-enable IRQs */
    __enable_irq();
    /* Wait until a data is received in DR register (RXNE = 1) EV7 */
    I2C_TimeOut = I2C_TIMEOUT;
    while ((I2C_MEMS->SR1 & 0x00040) != 0x000040)
    {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
    }    
    /* Read the data */
      *pBuffer = I2C_MEMS->DR;
  
  }
  else if (numByteToRead == 2) {
                  
    /* Set POS bit */
    I2C_MEMS->CR1 |= CR1_POS_Set;
    /* Wait until ADDR is set: EV6 */
    I2C_TimeOut = I2C_TIMEOUT;
    while ((I2C_MEMS->SR1&0x0002) != 0x0002)
    {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
    }      
    /* EV6_1: The acknowledge disable should be done just after EV6,
    that is after ADDR is cleared, so disable all active IRQs around ADDR clearing and 
    ACK clearing */
    __disable_irq();
    /* Clear ADDR by reading SR2 register  */
    temp = I2C_MEMS->SR2;
    /* Clear ACK */
    I2C_MEMS->CR1 &= CR1_ACK_Reset;
    /*Re-enable IRQs */
    __enable_irq();
    /* Wait until BTF is set */
    I2C_TimeOut = I2C_TIMEOUT;
    while ((I2C_MEMS->SR1 & 0x00004) != 0x000004)
    {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
    }        
    /* Disable IRQs around STOP programming and data reading */
    __disable_irq();
    /* Program the STOP */
    I2C_GenerateSTOP(I2C_MEMS, ENABLE);
    /* Read first data */
    *pBuffer = I2C_MEMS->DR;
    /* Re-enable IRQs */
    __enable_irq();
    /**/
    pBuffer++;
    /* Read second data */
    *pBuffer = I2C_MEMS->DR;
    /* Clear POS bit */
    I2C_MEMS->CR1  &= CR1_POS_Reset;
  }
  

  else { //numByteToRead > 2 
    // * Test on EV6 and clear it * /
    I2C_TimeOut = I2C_TIMEOUT;
    while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED))
    {
 if (I2C_TimeOut-- <= 0){
  return(I2C_TIMEOUT_UserCallback());
 }
    }         
    // * While there is data to be read * /
    while(numByteToRead)   {
      /* Receive bytes from first byte until byte N-3 */
      if (numByteToRead != 3) {
        /* Poll on BTF to receive data because in polling mode we can not guarantee the
        EV7 software sequence is managed before the current byte transfer completes */
        I2C_TimeOut = I2C_TIMEOUT;
        while ((I2C_MEMS->SR1 & 0x00004) != 0x000004)
        {
            if (I2C_TimeOut-- <= 0){
                    return(I2C_TIMEOUT_UserCallback());
            }
        }           
        /* Read data */
        *pBuffer = I2C_MEMS->DR;
         pBuffer++;
        /* Decrement the read bytes counter */
        numByteToRead--;
      }
      
      /* it remains to read three data: data N-2, data N-1, Data N */
      if (numByteToRead == 3) {
        /* Wait until BTF is set: Data N-2 in DR and data N -1 in shift register */
        I2C_TimeOut = I2C_TIMEOUT;
        while ((I2C_MEMS->SR1 & 0x00004) != 0x000004)
        {
            if (I2C_TimeOut-- <= 0){
                    return(I2C_TIMEOUT_UserCallback());
            }
        }            
        /* Clear ACK */
        I2C_MEMS->CR1 &= CR1_ACK_Reset;
    
        /* Disable IRQs around data reading and STOP programming */
        __disable_irq();
        /* Read Data N-2 */
        *pBuffer = I2C_MEMS->DR;
        /* Increment */
        pBuffer++;
        /* Program the STOP */
        I2C_MEMS->CR1 |= CR1_STOP_Set;
        /* Read DataN-1 */
        *pBuffer = I2C_MEMS->DR;
        /* Re-enable IRQs */
        __enable_irq();
        /* Increment */
        pBuffer++;
        /* Wait until RXNE is set (DR contains the last data) */
        I2C_TimeOut = I2C_TIMEOUT;
        while ((I2C_MEMS->SR1 & 0x00040) != 0x000040)
        {
            if (I2C_TimeOut-- <= 0){
                    return(I2C_TIMEOUT_UserCallback());
            }
        }           
        /* Read DataN */
        *pBuffer = I2C_MEMS->DR;
        /* Reset the number of bytes to be read by master */
        numByteToRead = 0;
      }
    }
  }
 
  /* Make sure that the STOP bit is cleared by Hardware before CR1 write access */
  I2C_TimeOut = I2C_TIMEOUT;
  while ((I2C_MEMS->CR1&0x200) == 0x200)
  {
            if (I2C_TimeOut-- <= 0){
                    return(I2C_TIMEOUT_UserCallback());
            }
  }             
  
  // * Enable Acknowledgement to be ready for another reception * /
  I2C_AcknowledgeConfig(I2C_MEMS, ENABLE);
  
  return 0;
    
}

int I2C_Master_Write(uint8_t deviceAddress, uint8_t WriteAddr, uint8_t* pBuffer, uint16_t numByteToWrite) {    
  
  volatile int I2C_TimeOut = 0;  
  
  /* Send STRAT condition */
  I2C_GenerateSTART(I2C_MEMS, ENABLE);

  /* Test on EV5 and clear it */
  I2C_TimeOut = I2C_TIMEOUT;
  while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_MODE_SELECT))
  {
    if (I2C_TimeOut-- <= 0){
            return(I2C_TIMEOUT_UserCallback());
    }
  }       

  /* Send EEPROM address for write */
  I2C_Send7bitAddress(I2C_MEMS, deviceAddress, I2C_Direction_Transmitter);
  
  /* Test on EV6 and clear it */
  I2C_TimeOut = I2C_TIMEOUT;
  while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
  {
    if (I2C_TimeOut-- <= 0){
            return(I2C_TIMEOUT_UserCallback());
    }
  }        
 
  /* Send the EEPROM's internal address to write to : only one byte Address */
  I2C_SendData(I2C_MEMS, WriteAddr); 
  
  /* Test on EV8 and clear it */
  I2C_TimeOut = I2C_TIMEOUT;
  while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
  {
    if (I2C_TimeOut-- <= 0){
            return(I2C_TIMEOUT_UserCallback());
    }
  }        
 
  while(numByteToWrite > 0) {
    /* Send the byte to be written */
    I2C_SendData(I2C_MEMS, *pBuffer); 
   
    /* Test on EV8 and clear it */
    I2C_TimeOut = I2C_TIMEOUT;
    while(!I2C_CheckEvent(I2C_MEMS, I2C_EVENT_MASTER_BYTE_TRANSMITTED))
    {
      if (I2C_TimeOut-- <= 0){
            return(I2C_TIMEOUT_UserCallback());
      }
    }     
  
    pBuffer++;
    numByteToWrite--;
  }
  
  /* Send STOP condition */
  I2C_GenerateSTOP(I2C_MEMS, ENABLE);
  
  return 0;
}

int I2C_TIMEOUT_UserCallback(void)
{
  /* User can add his own implementation to manage TimeOut Communication failure */
  /* Block communication and all processes */
  I2C_Master_DeInit();
  for(int i=0; i<3000; i++){__asm("nop");}
  I2C_Master_Init();
  for(int i=0; i<3000; i++){__asm("nop");}
  return -1;
}


2016年3月1日 星期二

[Unboxing]] eMotion: ST MEMS adapters motherboard

1. 之前經常會拿到st提供的sensor board來進行相關的測試或是移植, 有時候在沒有ap或是pc端驗
   證軟體的時候, 就比較難以知道sensor是否正常動作, 因此st mems team有提供了專屬的開發板
   和pc tool來協助驗證.

2. mems motherboard如下圖所示, 編號是STEVAL-MKI109V2, 而搭配測試的sensor board編號
   是STEVAL-MKI108V2.

[將SWD拉出來]


3. 接下來將sensor board與mems motherboard相接, 基本上要測試的硬體已經準備好了.


4. 接著到ST官方網頁去下載STEVAL-MKI109V2的相關資料.

    http://www.st.com/web/en/catalog/tools/PF252688



5. 解壓縮後, 可以看到底下的相關檔案, 最重要的就是FIRMWARE和Unico的執行檔.



6. 我們首先先使用IAR EWARM先開啟FIRMWARE, 並將Build好的檔案燒錄至mems
    motherboard, 這邊有一個需要特別注意的地方就是需要先將project的Workspace從
    EMOTION_DFU切換到EMOTION_FLASH, 否則一斷電後, 程式就會消失, 需要每次都
    使用ICE將程式燒錄進去.


7. 燒錄完成後, 接著就是安裝Setup_Unico_4.2.0.0.exe程式了, 完成後, pc上會產生Unico tool.


8. 接著就是透過usb cable將mems motherboard與pc相連接, 並開啟Unico tool, 若程式正常執行且
    sensor board也沒有接反的話, 則會有藍色LED亮起.


9. 開啟Unico tool後, 第一個會先要你選擇搭配的sensor board編號, 例如我們使用的為STEVAL-
    MKI108V2, 從清單列表上可以找到此顆編號, 看起來是一顆包含acc/gyro/mag sensor的9 axis
    sensor module, 最後按下Select Device.



10. 接著開始設定Unico tool, 首先先設定STM32 Virtual com-port的編號, 並按下Connect.




11. 連線成功後, 就可以開始對sensor module進行相關的設定, 如果還不熟的話, 也可以先選擇
     Easy Configuration來快速設定, 且連線成功後, mems motherboard上會多亮起3個LED.




12. 也可以直接對各個暫存器進行數值修改.


13. 當設定完成後, 接著按下Start按鈕, 便可以開始觀察數值上的變化.


14. 可以選擇各種不同的觀察資料方式, 例如可以選擇Plot等方式.


15. 這樣比看暫存器輸出的數值要方便地多了.



2016年2月23日 星期二

Jlink ARM OB

   最近上淘寶買了一個很便宜的Jlink ARM OB, 剛好中間遇到過年, 所以等了快三週才收到,
簡單測試了一下真的是蠻方便使用的, 唯一美中不足的大概就是沒有把RESET Pin拉出來吧.


2016年2月19日 星期五

STM32F373/383 I2C Master Driver

1. 底下是stm32f373/stm32f383 I2C Master test code的簡易測試範例, 主要是利用I2C去
    對L3GD20做I2C R/W的測試.


2. Main Loop:

  • Include I2C.h.
  • Init I2C.
  • Enable L3GD20 Normal Mode by I2C.(I2C Write)
  • Get L3GD20 Chip ID.(I2C Read)


3. 測試波形如下:

  • I2C Write:

  • I2C Read:


4. I2C Driver Download Link:


2016年2月18日 星期四

bin2h tool

1. 有時候在執行一些bin file的操作時, 會很希望把bin file轉成hex buffer來操作, 剛好最近看到有
    一個Tool就可以做到這件事情, 先趕快把它記錄下來.

2. 先到官方網站上去下載最新版Tool.
    http://www.deadnode.org/sw/bin2h/

3. 接著將下載下來的bin2h.exe和希望轉換成hex buffer的bin file放在同一個目錄底下.
    ex: 希望將SysTick_Example.bin轉換成hex buffer, 並存在 systick.h檔案裡面.



4. 開啟DOS Command模式, 到存放bin2h.exe和bin file檔案的路徑底下, 輸入以下command:

    bin2h.exe -c -z <bin file name> output file name

    ex:  bin2h.exe -c -z <SysTick_Example.bin> systick.h



5. 轉換成功後, .h檔案會自己產生, 此時可以先用ultraedit檢查一下是否有轉換成功, 使用ultraedit
    開啟SysTick_Example.bin後, 可以看到右下方有顯示bin file的大小, 以本範例來看是1560bytes.


6. 於是我們也利用ultraedit來打開systick.h看看, 打開後就會看到如下所示的hex buffer,
    可以直接複製到project裡面使用, 而buffer size則在最下面.


7. 可以看到buffer size = 1560, 與我們從ultraedit上面觀察到的長度相同.



8. bin2h.exe and test files download link:

    bin2h.exe

    SysTick_Example.bin

    systick.h

Street Fighter V 開箱





2016年2月15日 星期一

巧虎藍光光碟機微開箱

    家裡之前都是用PS3來播巧虎的, 只是沒想到居然遇到了死亡黃燈, 於是只好再買一台PS4來繼續播放巧虎....T_T.
















2016年2月3日 星期三

nRF51x22 Simple KeyScan

1. Test Schematic: (KEY1 = GPIO25)












2. Button.c
--------------------------------------------------------------------------------------------------------------------

#include "Button.h"

uint16_t  DebounceKeyCnt = 0;
uint8_t   KeyValue = 0; 
uint8_t   KeyState = 0;

void Button_Init(void)
{
 nrf_gpio_pin_dir_set(Button_GPIO_Pin,NRF_GPIO_PIN_DIR_INPUT);   //configurate pin direction to input
 nrf_gpio_cfg_input(Button_GPIO_Pin, NRF_GPIO_PIN_PULLUP); //pull-up
}

uint8_t KeyScan(void)
{
 KeyValue = nrf_gpio_pin_read(Button_GPIO_Pin);
 
 #ifdef high_active
  KeyValue = KeyValue;    //active high
 #endif
 
 #ifdef low_active
  KeyValue = !(KeyValue); //active low
 #endif
  
 if(KeyValue >= 1)
 {
  DebounceKeyCnt++;
 
                if(DebounceKeyCnt >= 5)
                {
   DebounceKeyCnt = 0;  
   KeyState = 1;
                }    
 }
 else
 {
  DebounceKeyCnt = 0;
  KeyState = 0;   
 } 
        
 return(KeyState);
}



3. Button.h
---------------------------------------------------------------------------------------------------------------------


#include "nrf_drv_gpiote.h"

#define Button_GPIO_Pin 25

#if 0
 #define high_active
#else
 #define low_active
#endif

void Button_Init(void);
uint8_t KeyScan(void);



nRF51x22 Simple Timer Code

1. 定義一個固定時間觸發的Timer.


2. 只需要Timer Init 和 Timer Handler這兩個function就可以用了.

Timer Init:
-------------------------------------------------------------------------------------------------------------------------
void Timer_Init(int32_t deltatime)  
{
    
    NRF_TIMER2->BITMODE     = TIMER_BITMODE_BITMODE_16Bit; //Set counter to 16 bit resolution
    NRF_TIMER2->MODE        = TIMER_MODE_MODE_Timer;
    NRF_TIMER2->PRESCALER   = 9;               //16MHz/(2^9) = 16MHz/512 = 31250Hz = 32usec
    NRF_TIMER2->CC[2]       = (deltatime/32);            //250 = 8msec , 500 = 16msec
    NRF_TIMER2->INTENSET    = TIMER_INTENSET_COMPARE2_Enabled << TIMER_INTENSET_COMPARE2_Pos;
    NRF_TIMER2->SHORTS      = (TIMER_SHORTS_COMPARE1_CLEAR_Enabled << TIMER_SHORTS_COMPARE1_CLEAR_Pos);
                
    NVIC_ClearPendingIRQ(TIMER2_IRQn);
    NVIC_SetPriority(TIMER2_IRQn,3);
    NVIC_EnableIRQ(TIMER2_IRQn);  
    NRF_TIMER2->TASKS_START = 1; 
}



Timer Handler:
-------------------------------------------------------------------------------------------------------------------------

void TIMER2_IRQHandler(void)  
{
    if((NRF_TIMER2->EVENTS_COMPARE[2] == 1) && (NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE2_Msk))
    {
 NRF_TIMER2->EVENTS_COMPARE[2] = 0;
   
 //nrf_gpio_pin_toggle(20);      //debug gpio
       
 NRF_TIMER2->TASKS_CLEAR = 1; 
    }
}

3. 假設需要定義一個固定每10msec會觸發的Timer, 可以在Init的時候帶入這樣的參數.     Timer_Init(10000);

2016年2月2日 星期二

nRF51x22 I2C Master Driver

1. 最近開始接觸nRF51x22系列的東西, 真的是有點小複雜, 因此就把最近弄好的一些東西先簡
    易備份在Blogger上, 以方便之後有需要的時候容易能馬上使用.

2. 使用I2C Master Driver之前, 必須要先include以下的c & h file, 才能正常使用, 檔名及路徑如
    下:

  • 測試的版本皆是在nRF51_SDK_9.0.0_2e23562.zip這個sdk版本下進行測試的.
  • twi_hw_master.c:
    \components\drivers_nrf\twi_master\incubated\
  • twi_master.h:
    \components\drivers_nrf\twi_master\incubated\
  • twi_master_config.h:
    \components\drivers_nrf\twi_master\incubated\config\
3. 接著就是使用底下的i2c.c & i2c.h 這兩個檔案就可以使用了.

i2c.c:
--------------------------------------------------------------------------------------------------------------------------
#include "i2c.h"

void I2C_Master_Init(void)
{
 twi_master_init();
}

int I2C_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToWrite)
{
   #define i2c_write_data_len 6
   uint8_t w2_data[i2c_write_data_len+1], i;

   w2_data[0] = RegAddr;
   for ( i = 0 ; i < NumByteToWrite ; i++ ) {
 w2_data[i +1] = pBuffer[i];  
   }

   if(twi_master_transfer(DeviceAddr,w2_data,NumByteToWrite+1,TWI_ISSUE_STOP) == false)
        return -1;

   return 0;
}

int I2C_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
{
 
   if(twi_master_transfer(DeviceAddr, &RegAddr, 1, TWI_DONT_ISSUE_STOP) == false)
 return -1;

   if(twi_master_transfer(DeviceAddr|TWI_READ_BIT, pBuffer, NumByteToRead, TWI_ISSUE_STOP) == false)
 return -1;
  
   return 0; 
}


i2c.h:
--------------------------------------------------------------------------------------------------------------------------
#include "twi_master.h"

void I2C_Master_Init(void);
int I2C_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToWrite);
int I2C_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead);

4. 調整i2c speed的地方在twi_master_init()裡面:

TWI_FREQUENCY_FREQUENCY_K400 = 400KHz
TWI_FREQUENCY_FREQUENCY_K250 = 250KHz
TWI_FREQUENCY_FREQUENCY_K100 = 100KHz



5. 調整i2c gpio pin的地方在twi_master_config.h:

    SCL = 1U = GPIO1.

    SDA = 3U = GPIO3.