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
--------------------------------------------------------------------------------------------------------------------
  1.  
  2. #include "Button.h"
  3.  
  4. uint16_t DebounceKeyCnt = 0;
  5. uint8_t KeyValue = 0;
  6. uint8_t KeyState = 0;
  7.  
  8. void Button_Init(void)
  9. {
  10. nrf_gpio_pin_dir_set(Button_GPIO_Pin,NRF_GPIO_PIN_DIR_INPUT); //configurate pin direction to input
  11. nrf_gpio_cfg_input(Button_GPIO_Pin, NRF_GPIO_PIN_PULLUP); //pull-up
  12. }
  13.  
  14. uint8_t KeyScan(void)
  15. {
  16. KeyValue = nrf_gpio_pin_read(Button_GPIO_Pin);
  17. #ifdef high_active
  18. KeyValue = KeyValue; //active high
  19. #endif
  20. #ifdef low_active
  21. KeyValue = !(KeyValue); //active low
  22. #endif
  23. if(KeyValue >= 1)
  24. {
  25. DebounceKeyCnt++;
  26. if(DebounceKeyCnt >= 5)
  27. {
  28. DebounceKeyCnt = 0;
  29. KeyState = 1;
  30. }
  31. }
  32. else
  33. {
  34. DebounceKeyCnt = 0;
  35. KeyState = 0;
  36. }
  37. return(KeyState);
  38. }
  39.  


3. Button.h
---------------------------------------------------------------------------------------------------------------------
  1.  
  2.  
  3. #include "nrf_drv_gpiote.h"
  4.  
  5. #define Button_GPIO_Pin 25
  6.  
  7. #if 0
  8. #define high_active
  9. #else
  10. #define low_active
  11. #endif
  12.  
  13. void Button_Init(void);
  14. uint8_t KeyScan(void);
  15.  


nRF51x22 Simple Timer Code

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


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

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


Timer Handler:
-------------------------------------------------------------------------------------------------------------------------
  1.  
  2. void TIMER2_IRQHandler(void)
  3. {
  4. if((NRF_TIMER2->EVENTS_COMPARE[2] == 1) && (NRF_TIMER2->INTENSET & TIMER_INTENSET_COMPARE2_Msk))
  5. {
  6. NRF_TIMER2->EVENTS_COMPARE[2] = 0;
  7. //nrf_gpio_pin_toggle(20); //debug gpio
  8. NRF_TIMER2->TASKS_CLEAR = 1;
  9. }
  10. }
  11.  
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:
--------------------------------------------------------------------------------------------------------------------------
  1. #include "i2c.h"
  2.  
  3. void I2C_Master_Init(void)
  4. {
  5. twi_master_init();
  6. }
  7.  
  8. int I2C_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToWrite)
  9. {
  10. #define i2c_write_data_len 6
  11. uint8_t w2_data[i2c_write_data_len+1], i;
  12.  
  13. w2_data[0] = RegAddr;
  14. for ( i = 0 ; i < NumByteToWrite ; i++ ) {
  15. w2_data[i +1] = pBuffer[i];
  16. }
  17.  
  18. if(twi_master_transfer(DeviceAddr,w2_data,NumByteToWrite+1,TWI_ISSUE_STOP) == false)
  19. return -1;
  20.  
  21. return 0;
  22. }
  23.  
  24. int I2C_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead)
  25. {
  26. if(twi_master_transfer(DeviceAddr, &RegAddr, 1, TWI_DONT_ISSUE_STOP) == false)
  27. return -1;
  28.  
  29. if(twi_master_transfer(DeviceAddr|TWI_READ_BIT, pBuffer, NumByteToRead, TWI_ISSUE_STOP) == false)
  30. return -1;
  31. return 0;
  32. }
  33.  

i2c.h:
--------------------------------------------------------------------------------------------------------------------------
  1. #include "twi_master.h"
  2.  
  3. void I2C_Master_Init(void);
  4. int I2C_Write(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToWrite);
  5. int I2C_Read(uint8_t DeviceAddr, uint8_t RegAddr, uint8_t* pBuffer, uint16_t NumByteToRead);
  6.  
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.