顯示具有 C 標籤的文章。 顯示所有文章
顯示具有 C 標籤的文章。 顯示所有文章

2020年3月11日 星期三

How to get main() function return value in Linux terminal.

Sometimes we want to get the exit value of main() function, using the "echo $? " can get the most recently program exit value.

Example program:



Result:



Reference:
  1. UNIX Shell Programming 

2019年7月12日 星期五

C : strrchr() analyze and example get self executable directory function.

strrchr() function will return a pointer to the last occurrence.
Another example is modify from Advanced Linux Programming chapter 11.We can get a sub-sting without the occurrence with sub two pointer.





Ref:
   1. http://www.cplusplus.com/reference/cstring/strrchr/
   2. Advanced Linux Programming by Mark Mitchell, Jeffrey Oldham, and Alex Samuel www.newriders.com

2018年12月18日 星期二

Keil C: Warning: #1296-D: extended constant initialiser used"?


The root cause is miss use a pointer to a static variable like:

int x = 1;
int y = (int) &x;

Modify version

int x = 1;
int *y = &x;

printf("%p\n", &x);  // memory address of x
printf("%p\n", &y);   // memory address of y

printf("%p\n", y);     // y = memory address of x
printf("%d\n", *y);   // *y = memory content of x  = 1


Addresses are not arithmetic types in ANSI C spec, so if need to get the address of a static variable, use a pointer point to the variable's address.







Ref :
 1. http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.faqs/ka15182.html
 2.https://openhome.cc/Gossip/CGossip/Pointer.html

2018年7月30日 星期一

Unsigned and signed shift:logical shift and arithmetic shift

In C, we usually use left shift and right shift operation << and >> to operate multiple / divide to variable with power of 2,ex: 4<<2  = 16

But when we use the signed variable and the shift operation we must consider the singed bit.See the below example code.

Since the C language support the arithmetic left shift and logic right shift,  we must consider the signed bit to  avoid some bug in code.



Ref:
     1.Logical Vs. Arithmetic Shift
     2.C语言中的逻辑右移和算术左移

2018年4月2日 星期一

CC2640:Change RF front end mode and bias

1. In ../main.c define your own BLE configuration template.
2.The option and default configuration is define in ../ble_user_config.h
 

The internal bias and external bias is determine by the RX_TX pin, which didn't implement in 7x7 package.In our application we use the CC2592 as PA and use 5x5 package for smaller footprint, and the configuration is differential mode internal bias.


Ref:
      1.CC26xx HW Training : RF Front End options and Antennas
      2.CC2640 在 Beacon 应用中的实现方法
      3.CC26x0 SimpleLink™Bluetooth®low energy Software Stack 2.2.xDeveloper's Guide

2018年1月17日 星期三

CC2640:Use System_printf in project.

1.I use IAR as IDE to develop CC2640 BLE project and XDS100V3 as debugger.First make sure           enable the Semihosted and Via semihosting in General Options.
 
2.Add below code in app_ble.cfg file.
 

3.Add include header file and test.



4.In debug mode enable View->Terminal I/O and we can see the Hello World! in terminal.




















Ref:
     1.System_printf and printf() on CC2650 sensorTag
     2.System_printf supported format specifications

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年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

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...