2017年12月24日 星期日

CC2640: Add Watchdog driver to project.

1.Add ble_sdk_2_02_01_18\src\components\services\src\watchdog
   Watchdog.h, WatchdogCC26XX.c, WatchdogCC26XX.h to your project, and add
   $SRC_COMMON$/services/src/watchdog to your project include path.
2.In CC2650DK_5XD.h(Select what platform you use) add below code.

3.In CC2650DK_5XD.c(Select what platform you use) add below code.

4.Add below code to your application.
 






Ref:
      1.RTOS/CC2640: How to set CC2640 watchdog with TIRTOS 2.13
      2.WatchdogCC26XX.h File Reference
      3.CC2640 看门狗
      4.http://blog.csdn.net/zzfenglin/article/details/52253536

2017年11月9日 星期四

BLE Mesh Overview(2)

BLE Mesh Overview (2)

Flooding:
     Each packet will broadcast to every node in the network until it reaches the target node(s).
Complemented by three types of addressing. “Unicast” (used during initial node set-up), “group”, and
virtual” (primarily used by  device manufacturers to ‘label’their products).

     Of these, group addressing is the most applicableto everyday operation. A groupaddress is a multicast address which represents one or more elements in the network. The SIG has defined four “Fixed Group Addresses” named “All-proxies”, “All-friends”, “All-relays”, and “All-nodes”
to specifically address the types of nodes defined above.

      In addition to these group addresses, the installer will likely allocate dynamic group addresses
during configuration to reflect the physical mesh layout. For example, dynamic group addresses could be allocated to the lights in each room of a building.

     The specification includes several mechanisms to mitigate advertising and scanning channel congestion. The first is a “Time-To- Live” (TTL) counter that defines how many times a specific packet can be relayed – after a defined number of steps, the packet will not be relayed further.
The second congestion prevention mechanism is a message cache of transmitted packets. A packet can only arrive in the cache once it has circulated completely around the mesh – at which point it can safely be assumed that further transmission is unnecessary. An “optional relaying” feature also limits mesh congestion. By switching off relaying, nodes can receive packets but not
pass them on. Optional relaying adds complexity—because the designer must trade-off mesh flexibility against bandwidth consumption—but in doing so improves overall mesh performance.

Security :
    Provisioning is the process by which otherwise isolated devices become nodes on the mesh network and can be implemented via an app supported on a smartphone or tablet. The process follows five steps: Beaconing (indicating availability to join the mesh); Invitation (from the provisioning device); Public Key Exchange;Authentication; and Session Key Exchange (securing the
subsequent distribution of data to complete provisioning). The provisioning process implements a high degree of security. Thereafter, packets are end-to-end encrypted with AES-128, with privacy enhanced by additional AES-128 encryption between each relay (in part to hide the node addresses). In addition, procedures are included to periodically refresh the encryption keys, and to detect and repel “replay attacks”.


Ref:
      1. ULP Wireless Quarter 10-11
      2. nRF5 SDK for Mesh
      3. Introducing Bluetooth Mesh Networking
      4.How to build a wireless sensor-driven lighting control system based on Bluetooth mesh
         networking - Silvair
      5.2017 Introducing Bluetooth Mesh
      6.Le IoT 想想物聯網 - mesh network topology of BLE 介紹 (1) - Star & Mesh Topology 簡介

BLE 5 Overview

Bluetooth 5 it is highly likely they belong to one or more of these three major advancements:

  • 2Mbs for higher throughput and/or energy saving
  • 500/125kbs modes for considerably increased range (x4)
  • Advertising extensions for increased broadcast capacity (x8)

Advertising extensions :
       Allow add more payload on advertising data packet. by use the secondary
  advertisement channel(previous data channel).enables chaining of advertising packets to create
  greater advertising data payloads of up to 255 bytes.

      Periodic advertisements comprises packets sent at a fixed interval to synchronized devices via
  “connectionless” broadcasting.By using periodic advertisements,scanning devices can more
  consistently “follow” the advertiser and monitor its updates more frequently.

      Connectionless broadcasting is further boosted because Bluetooth 5 features an eight times
  higher broadcast capacity compared to previous versions of the technology.


Ref:
      1. ULP Wireless Quarter 20
      2. Taking a deeper dive into Bluetooth 5
   

     
 

BLE Mesh Overview(1)

    SIG announce the BLE mesh at 2017 July. Not only for BLE 5 but also compatible with BLE4.0 (or later). BLE mesh is implement by add new layers on the BLE physical layer so it need to make appropriate modification on the old BLE 4.0 firmware to get the mesh work.

    Bluetooth mesh follows Bluetooth technology’s architecture whereby GATT
“Profiles”.To avoid GATT Profiles being mistaken for the mesh equivalent, mesh profiles
are called “Models”

The Bluetooth mesh specification details four types
Relay Nodes :
       Relay Nodes re-transmit received packets and are the mechanism by which packets rapidly propagate across the entire network.The downside of relaying is increased current consumption because it relies on the nodes being continuously ‘awake’.

Low Power Nodes & Friend Nodes:
        Low Power Nodes work in tandem with “Friend Nodes”.Friend Nodes are typically
mains powered so can remain permanently awake; the devices cache any messages destined
for the LPN. The LPNs switch to receive’ mode according to a predetermined schedule, accept
cached messages, operate as instructed, and then rapidly return to a power-saving ‘sleep’ state.

Proxy Nodes:
       Proxy Nodes allow devices that run the Bluetooth low energy stack but don’t support Bluetooth
mesh (for example, the current generation of smartphones) to connect to a Bluetooth mesh network. Interaction is achieved via both the node and device’s Generic Attribute Profile (GATT) interface.






Ref:
      1. ULP Wireless Quarter 10-11
      2. nRF5 SDK for Mesh
      3. Bluetooth mesh networking SIG

2017年10月2日 星期一

NANO100: Use CMSIS DSP library in NANO100 series microcontroler

1.Build the arm_cortexM0l_math.lib
   The source project is located on
   After build you will find the arm_cortexM0l_math.lib in
2.Use the arm_cortexM0l_math.lib copy the Template project. And add ARM_MATH_CM0 in 
   Preprocessor Symbols

3.Add the #include "arm_math.h"  and  arm_cortexM0l_math.lib in Template project.

4.Now you can use the CMSIS DSP library to optimized the math function!







2017年9月13日 星期三

Some little trick in C.


1.Exchange two variable without extra memory space.
    Since A xor A = 0 , so A xor A xor B = B thus we can change this two variable by use xor op.
2.String compose.
   By use #define fun(a,b) a##b  we can easily to compose a in a string.
3.Use union to transform float to 4-char raw data
   union define two different data type in same memory space(Ref 3)
4.Counting the number of args
 
    If we call NUM_VA_ARGS(1, 2, 3) will turn out to 

    NUM_VA_ARGS_IMPL( 1, 2,  3, 63, 62, 61,  \
             60, 59, 58, 57, 56, 55, 54, 53, 52, 51,                         \
             50, 49, 48, 47, 46, 45, 44, 43, 42, 41,                         \
             40, 39, 38, 37, 36, 35, 34, 33, 32, 31,                         \
             30, 29, 28, 27, 26, 25, 24, 23, 22, 21,                         \
             20, 19, 18, 17, 16, 15, 14, 13, 12, 11,                         \
             10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0)
     The blue part is the ... variable and the red part is the N so that we can get the count of args.
5.Define how many bits use to represent the variable in struct.
   Consider we have a struct represent 32 bits register and have 0~7 bits for data transfer, 8~31 bits
   for reserve.We can declare a struct like
So that if we give the data value above 255 than compiler will warning us the value is over the range of this variable can represent.

6.Compare unsigned value to constant value use minus replace compare

7.Define constant in struct and integer trick.

Ref:
      1.Linux C编程一站式学习
      2.C語言:浮點數通信傳輸
      3.Union型別 (討論在C&C++中的差異)
      4.nRF52 SDK/nrf_log.h
      5.C - Bit Fields
      6.https://stackoverflow.com/questions/514118/how-does-duffs-device-work


   

2017年9月5日 星期二

A fast method to round a double to a 32-bit int explained note

Quote:

Between 2^52=4,503,599,627,370,496 and ^253=9,007,199,254,740,992 the representable numbers are exactly the integers
This follows from the fact that the mantissa is 52 bits wide.

The other interesting fact about adding 2^51+2^52 is that it affects the mantissa only in the two highest bits - which are discarded anyway, since we are taking only its lowest 32 bits.



IEEE 754 Double Floating Point Format.svg








So we add 2^51+2^52 and get the lowest 32 bits via a cast than we can get the integer from double.

#define double2int(i, d) \
    {double t = ((d) + 6755399441055744.0); i = *((int *)(&t));}

Ref:
      1. A fast method to round a double to a 32-bit int explained
      2. Double-precision floating-point format

2017年8月27日 星期日

SVE:new set of vector instructions for ARMV8

SVE introduce new set of register
• 32 Z registers, z0, z1, . . . , z31;

Data register multiple of 128 bits up to 2048 bits, support 8-bit bytes, 16-bit halfwords, 32-bit words or 64-bit doublewords.

• 16 P registers, p0, p1, . . . , p15;

Predicate registers hold one bit for each byte available.

• 1 FFR register.

Special predicate register that differs from regular predicate registers by way of being used
implicitly by some dedicated instructions, called first faulting loads.


SVE is more flexible and efficient than the neon instruction . Target on the multi vector computation such as: machine learning and the graphic .



Ref:
     1.A sneak peek into SVE and VLA programming
     2.ARMv8-A SVE technology Hot Chips v12
     3.The ARM Scalable Vector Extension
     4.SIMD Programmingwith Larrabee
     5.Using SVE intrinsics directly in your C code

2017年7月16日 星期日

Competitive Programmer’s Handbook Note.

1.g++ compiler provide __int128_t type 128 bits interger

2.Because the precision errors it's better use subtraction and compare with a small number like(1e-9) than use == to indicate two floating point numbers is equal.

3.page 16 book list

4.Computers and Intractability: A Guide to the Theory of NP-Completeness

5.Kadane’s algorithm.

6.Chapter 7 DP

2017年6月11日 星期日

NANO100:Using data flash without the external 12M HZ crystal.

The data flash start address is set by register so it need a reset after update the address, but in default it will use  the external 12M HZ crystal.It's need to modify the CFOSC .



Modify the \SampleCode\StdDriver\FMC_RW set_data_flash_base() function



Ref:
     Nano100 Series Technical Reference Manual

NANO100:Enable printf use Nu-Link to send massage

1.Add #define DEBUG_ENABLE_SEMIHOST in Library\StdDriver\src\retarget.c .
 

2.Start Keil C IDE debug session and enable the View\Serial Windows\UART #1.


3. Now you can see the printf massage in Keil C IDE.

Ref:
   2. Semihosting

2017年6月7日 星期三

Octave: Modulation Sine Wave use PWM generate look up table for MCU DAC.

Input the target sample frequency ,sample bits and sample frequency.Generate the duty of PWM(percentage).
Code:
Avoid the negative part cause normally MCU didn't have negative voltage output ability.







Ref:
    1.http://bugworkshop.blogspot.tw/2011/04/blog-post_12.html

2017年5月26日 星期五

NANO100: MCU Sine wave generator - using MCU DAC(Look up table with adjustable amplitude)

1.Generate sample point use octave.

2.Create look up table.use const to store look up table in ROM for less latency.

3.Scale to real data function (for 12 bits DAC the max value is 4096)

2017年5月11日 星期四

nrf52 : Add CMSIS_DSP package in Keil 5

1.Open Manage Run-Time Environment and check the DSP














2.Include arm_math.h
  and  path in your project:Keil_v5\ARM\PACK\ARM\CMSIS\5.0.1\CMSIS\Include

3.Add symbol ARM_MATH_CM4 since nrf52832 use Cortex M4F






4.Simple matrix multiplication code

Ref:
    1. CMSIS DSP Software Library







2017年4月19日 星期三

Use ADC to measure battery.

In my project I use a 8 bits resolution ADC to measure battery . 3.0 V as 100% and 2.2 V as 0%.
When input is 3.0 V the ADC value is 255 and 2.2 V for 180. Because it's unnecessary to be very accurate so I use simple equation y = ax +b to calculate the percentage of the battery.

100 = 255 a + b ........ (1)
0     = 180 a + b ........ (2)

Use 1 and 2 we can calculate a = 4/3 and b = -240.











Ref:
      Float divide and integer divide in C.

nrf52: Change pin 0.09 and pin 0.10 as normal GPIO

1.Open project option -> C/C++ and add CONFIG_NFCT_PINS_AS_GPIOS to preprocessor symbols.


2,Now you can use pin 0.09 and pin 0.10 as normal GPIO



Ref:

2017年4月15日 星期六

nrf52: Use PWM to control DC motor rotate speed.

DC motor rotate speed is directly proportional  to supply voltage and current. But it is very difficult to use software to control supply voltage and current . So use on/off time to control the DC motor is a better solution.

In my project I use TI DRV8837  to driver. DRV8837  has H-bridge structure to control motor forward/reverse rotate. So just simply add pwm input to DRV8837 then we can control the DC motor rotate speed.

1.Connect three gpio to DRV8837 nSleep , IN1 , IN2 .


2.Config PWM module and connect to IN1,IN2.   

3.Now we can control DC motor rotate speed from 0 to 100% and rotate forward/reverse.



NOTICE!: It very danger when motor change rotate direction because it will shortly to turn both side H-bridge ON.It will need a short dead zone to prevent this situation.Luckily DRV8837 has build- in protection function when both IN1 IN2 are high the output will be low to stop the motor.

Ref:
     1. DRV883x datasheet 
     2. H-bridge
        

2017年4月12日 星期三

nrf52:Use DC/DC regulator to save power consumption

nrf52832 has two power mode.LDO mode and Switching power mode.To use Switching power mode will need to add Inductor  between the DEC4 pin and DCC pin, and use software to change mode.




in softdevice :




It can save 5~6 uA around 17% of current on ble_template project








Ref:
       1. LDO vs Switching power
       2. nRF52 Online Power Profiler
       

nrf52: merge softdevice and application hex.

Add nrfTool mergehex to enviroment path .

Type mergehex -m softdevice.hex application.hex -o your.hex

2017年4月5日 星期三

Use Astyle in Keil uVision

1.Download Astyle
   http://astyle.sourceforge.net/
2. Open Keil -> Tool -> Customize Tool Menu config as below

My argument. Can customize as you want.
a.Astyle Current File :
--style=allman --indent-switches -n --indent=tab --indent-labels --indent-preprocessor -p -U !E
b. Astyle All File        :
 --style=allman --indent-switches -n --indent=tab --indent-labels --indent-preprocessor -p -U "$E*.c" "$E*.h"


2017年2月21日 星期二

Linux UIO with ZYC706 U-boot config

Xilinx remove the compatible = "generic-uio" configuration in latest Kernel distribution ,if you want to use the uio function, you need  to add environment variable in uEnv.txt.



bootargs = "console=ttyPS0,115200 earlyprintk uio_pdrv_genirq.of_id=generic-uio";






ref:
1.https://forums.xilinx.com/t5/Embedded-Linux/generic-UIO-broken/td-p/564259/page/2
2.https://github.com/Xilinx/linux-xlnx/commit/7ebd62dbc727ef343b07c01c852a15fc4d9cc9e5

Linux driver: How to enable dynamic debug at booting time for built-in driver.

 Dynamic debug is useful for debug driver, and can be enable by: 1. Mount debug fs #>mount -t debugfs none /sys/kernel/debug 2. Enable dy...