2009年12月21日 星期一

Vim color

  1. 下載 COLOR_SCHEME.vim
  2. mkdir ~/.vim/colors/
  3. mv COLOR_SCHEME.vim ~/.vim/colors/
  4. vim ~/.vimrc # 加入下述內容
    colorscheme COLOR_SCHEME

  5. 註1: .vimrc 要記得加 set t_Co=256 來支援 256 色的環境, 才不會覺得跟預覽圖不一樣.
  6. 註2: syntax on ...

Ubuntu samba 設定

【第一步】
叫出終端機,鍵入以下的指令:(以下數字後面的文字為說明)
1.安裝samba
sudo apt-get install samba

2.先停止服務,準備設定設定檔

sudo /etc/init.d/samba stop

3.設定 samba 設定檔

sudo gedit /etc/samba/smb.conf

4.設定分享資料夾,請在smb.conf的尾端加入以下的內容

[SHARE]
path = /home/你自己名稱/shared    ;分享路徑
browseable = yes            ;是否可瀏覽
read only = no              ;唯讀
create mask = 0644          ;檔案遮罩
directory mask = 0755       ;資料夾遮罩

5.啟動 samba 服務

sudo /etc/init.d/samba start

6.建立帳號密碼

sudo smbpasswd -a <帳號>

※若想直接分享,不需要設定每位使用者的帳號密碼,那麼在 smb.conf 中 security 設定更改成如下:

;security = share

【第二步】
  1. 在桌面上,滑鼠右鍵點選/home/你自己名稱/shared,選擇【共享選項】。
  2. 依照您的需求,自行決定。也可以都打勾!
  3. 再點選【新增分享】。

2009年12月1日 星期二

Timer tutorial include prescalers

timer tutorial (incl prescalers)

Delays@
Andrew Warren [fastfwd at ix.netcom.com] of Fast Forward Engineering San Diego, California says:
timer-0 has four components: The clock input (either from the TMR0 pin or from the internal instruction clock), the TMR0 prescaler, the TMR0 register, and the TMR0 interrupt flags (GIE, T0IE, and T0IF).

CLOCK INPUT:

You'll probably use the internal instruction clock as your TMR0 input; I'll assume that your PIC is running at 4 MHz. Since there's an internal divide-by-4 between the oscillator frequency and the instruction clock, this means that instruction clocks occur at a 1 MHz rate.

prescaler:

The TMR0 prescaler is set (via 4 bits in the OPTION register) to divide-by-1, -2, -4, -8, -16, -32, -64, -128, or -256. The TMR0 input clock (1 MHz in your case) is passed to the prescaler, whose divided-down output is then fed to the TMR0 register.
For example, if the TMR0 prescaler is set to divide-by-4 and the PIC is running at 4 MHz, the prescaler will send a 250 KHz clock to the TMR0 register.

TMR0 REGISTER:

The TMR0 register can be preloaded with any 8-bit value that you like.
Each clock pulse from the prescaler increments the contents of the TMR0 register. When the value in the TMR0 register rolls over from 0xFF to 0x00, the T0IF flag is set (the TMR0 register continues to be incremented on every pulse from the prescaler, though).

INTERRUPT FLAGS:

If the GIE and T0IE flags are set when the T0IF flag is set, an interrupt is generated (the GIE bit is automatically cleared (to temporarily prevent further interrupts while your interrupt routine is executing), and the PIC jumps to the "interrupt vector" at location 0x04. Your interrupt-service routine at that location should check the T0IF flag to determine the source of the interrupt, then must clear the T0IF flag to prevent the PIC from immediately jumping back to the interrupt routine when interrupts are re-enabled.
At this point in the interrupt routine, you can re-load the RTCC with any appropriate value.
When you're finished handling the interrupt, your code should execute a RETFIE instruction, which will automatically set the GIE bit to re-enable interrupts and return to your main program.
Luis F says:
If you want the delay in seconds:
Delay = (256 - InitTMR0 * prescaler)
            -----------------------------------------
                    Frequency / 4
Or if you want the value to put in TMR0 to get a determinate DELAY
InitTMR0 = 256 - ( DELAY * Frequency ) / ( 4* prescaler)

2009年11月28日 星期六

c language standard library function


Reference website: http://www.cplusplus.com/reference/clibrary/ 
Reference: "Embedded Linux, C-language programming practice" 
In fact, in reference to web pages, you can already find all the C language standard library functions. Here I only list what I learned: 
  C-set of the standard library functions are: 
     : the standard set of input and output functions. 
     : a set of function determining character type (whether upper case, numbers, spaces)

     : a set of function Operating string. 
     : a set of mathematical applications related functions. 
     : tool set, including the type conversion and some system functions. 
     : a set of additional assertion functions. 
     : variable parameters, tool kit. such as the printf function will use this package. 
     : a set of function supporting jump feature

     : a set of processing interrupts. 
     :a set of function dealing with the date and time
     : integer type maximum and minimum set.

     : maximum and minimum set of floating-point type.

1. Standard formal input and output class function 
    Header files are involved in stdio.h and stdarg.h 
   * scanf function: Format input string 
   * printf functions: formatting output strings 
   * putchar function: the output characters to the standard output 
   * getchar function: get character from standard input 
   * putc function: output characters to the file 
   * getc function: get characters from a file 
           while ((c = getchar ()! = EOF)) putchar (c) 
           c = getc (pFile) 
           getchar () is equivalent to getc (stdin) 
   * gets function: get the string 
   * puts the function: output the specified string 
   * ungetc function: the return of characters to write


2. Character processing and conversion functions 
   * sprintf function: formatted output string to a buffer 
           sprintf (s, "% d", 123) 
           sprintf (s, "% 08X", 456); turn 16 hex 
           sprintf (s, "% 10.3f", 3.1434) 
   * strcat and strncat functions: string concatenation 
          char * strncat (char * dest, const char * src, size_t n) 
   * strcpy and strncpy functions: string copying 
          char strncpy (char * dest, const char * src, size_t maxlen) 
   * strcmp and strncmp functions: String Comparison 
           int strcmp (const char * s1, const char * s2) 
           s1> s2 return 1 
           s1 = s2 return 0 
           s1
   * strlen function: Get the string length of the 
   * strchr and strrchr function: Character / String location 
           strchar: search a character position of first occurrence 
           strrchr: search a character position of last occurrence 
   * strstr function: string search 
   * strupr and strlwr function: to convert the letters 
   * strdup and strndup function: string copying 
           char * strdup (const char * s) 
           The string s to copy to the specified memory cell


3. Math class functions counting 
    div acos atan cos tan cosh exp frexp ldexp log modf pow sqrt ceil abs floor


4. Data structures and algorithms class functions 
     * bsearch function: binary search 
     * lfind function: linear search 
     * lsearch function: linear search 
     * qsort function: the use of ordered array of quick sort method 
     * rand function: generate random number


5. File I / O operation class correlation function 
     * fopen function: open file 
     * fclose function: close file 
     * fgetc function: read a character from a file 
            fp = fopen ( "exit", "r") 
            while ((c = fgetc (fp))! = EOF) 
     * fputc function: to a specified character is written to the file stream 
     * fgets function: read a string from a file 
     * fputs function: to a specified string into the document
     * rewind function: Reset the file position of the file stream to read and write at the beginning of 
     * ftell function: get the file stream to read position 
     * fseek function: get the file stream to read position 
     * fwrite function: to build the file to write to the file stream 
     * fread function: read data from the file stream 
     * fgetpos function: get the file location 
                The stream's current location is recorded in the * position for the subsequent fsetpos () call using the 
     * fsetpos function: Set File Location


6. Utility functions 
     * assert function: diagnostic procedures 
     * setjmp function: to save call

C language extensions library function


Reference: "Embedded Linux, C-language programming practice" 

1. File I / O operations functions 

   * open function: open file 
          int open (const char * pathname, int flags) 

   * close function: close file 

   * read function: read the file 
          ssize_t read (int_fd, void * buf, size_t count) 
            transfer bytes of count number into point buf
         

   * write function: write file 
          ssize_t write (int fd, const void * buf, size_t count) 
          write bytes of count number into file(fd) with beginning of point buf in the memory 

   * lseek Function: File Positioning 
          off_t lseek (int fildes, off_t offset, int whence); 
          retuen the current position of reading and writing 

   * flock function: lock file 
          int flock (int fd, int operation) 

   * mmap function and munmap functions: memory-mapped 
          int munmap (void * start, size_t length) 
         map the contents of a file into memory, to write or read this memory region is the direct access to to the content of the document. 

   * create a function: to create a new file 
          int create (const char * pathname, mode_t mode) 

   * fcntl function: to change the file attributes which has been opened 

          int fcntl (int fields, int cmd, int arg) 
          If success,return the implementation of the new descriptor, otherwise returns -1 

2. the function of related file permissions

   * access functions: to determine whether access to the file permissions 
          int access (const char * pathname, int mode) 
          if (0 == access (argv [1], W_OK)) R_OK X_OK F_OK 

   * chown function and fchown function: to change the file owner of the 
          int fchown (int fd, uid_t owner, gid_t group) 

   * chmod function and fchmod function: to change the permissions 
         int fchmod (int fildes, mode_t mode) 

   * unlink function: delete the file 

3. User group manipulation functions 

   * getegid function and setegid function: get / set the effective group ID 
         gid_t gete (gid_t egid) 

   * geteuid function and seteuid function: get / set the real user ID 

         uid_t getuid (void) 

  4. Signaling Function 

    * kill function: to send a signal to the specified process

         int kill (pid_t pid, int sig) 

    * rais function: signaling 

         int raise (int sig) 

    * alarm function: Set the timer 

         unsigned int alarm (unsigned int seconds) 

    * signal function: Signal Installation Function 
         sighandler_t signal (int signum, sighandler_t handler) 

5. The process of dealing with the function 

    * getpid function and getppid function: to obtain the process ID and the parent process ID 
         pid_t getpid (void) 
         pid_t getppid (void) 

    * fork function: the establishment of child process 
         pid_t fork (void) 
         The parent process has been the return value is the child process ID, while the child process returns 0 

    * sleep function: Let the process was suspended for a period of time 
         unsigned int sleep (unsigned int seconds) 

    * exec function: find the executable file 

    * _exit function: the end of the process of implementation of the

function pointer


Reference: "Embedded Linux, C-language programming practice" 

1. The concept of function pointers: 
   In the C language: the nature of the pointer is a memory address, function pointer is a pointer to a function of the code in the code segment address of the pointer. 
   For the function, its address and function name represents the same meaning 
   In the C language programming, data structures and algorithms are two basic elements, through a function pointer, the algorithm can be embedded into data structure. 

  

2. The usage of function pointers 

  
*the basic usage
    int add (int a, int b) 
    {....} 
    int sub (int a, int b) 
    {....} 
    int main ()
    ( 
      int (* pf) (int, int); 
      pf = add; 
      result = pf (100,200); 
      pf = sub; 
      result = pf (100,200); 
     )


   
* Type conversion: 
   int add (int a, int b) 
    {....} 
    int sub (int a, int b) 
    {....} 
    int main () 
    ( 
      void * pf; 
      pf = add; 
      result = (int (*) (int, int) pf) (100,200); 
      pf = sub; 
      result = (int (*) (int, int) pf) (100,200); 
     )


   
 * Function pointer type definition 
   typedef int (* fun_t) (int, int); 
   fun_t pf; 
   pf = add; 
   result = pf (100,200);


    
* Function pointer as a structure member 
struct source 
( 
   int a; 
   int b; 
   fun_t operation; / / equivalent to int * fun_t (int, int); 
); 
int main (int argc, char * argv []) 
( 
   struct source data; 
   int result; 
   data.a = 200; 
  
 data.b = 100; 
   data.operation = add; 
   result = data.operation (data.a, data.b); 
) 

   
* Function pointer being as a parameters of the function 
int calculate (int a, int b, fun_t operation) 
( 
   int result; 
   result = operation (a, b); 
   return result; 
) 
  int main (int argc, char * argv []) 
( 
   int a, b, result; 
   a = 200; 
   b = 100; 
   result = calculate (a, b, add); 
   ) 

   
* Function pointer as the function return value 
fun_t getoperation (char a) 
( 
   fun_t result; 
   switch (a) 
  ( 
   case "+"; 
       result = add; 
       break; 
   case "-"; 
       result = sub; 
       break; 
   ) 
    return result; 
) 
int main (int argc, char * argv []) 
( 
   int a, b, result; 
   char oper; 
  
 a = 200; 
   b = 100; 
   oper ="+"; 
   result = getoperation (oper) (a, b); 
)


 
  * Array of function pointers 
enum ( 
       oper_add = 0, 
       oper_sub 
) 
static const fun_t oper_table [oper_num] = 
( 
   add, 
   sub 
) 
int main (int argc, char * argv []) 
( 
   int a, b, result; 
   a = 200; 
   b = 100; 
   result = oper_table [oper_add] (a, b); 
)

Dynamic memory Heap and Stack


Reference: "Embedded Linux, C-language programming practice" 

1. The use of program memory area 

   Storage area is divided into: static and dynamic 

                 Static: read-only data area (RO DATA), has been initialized to read and write data area (RW DATA), an uninitialized read-write data area (BSS:
Block Started by Symbol) 


                 dynamic: heap Memory (heap) and stack memory (stack) 

   Heap memory is from low address to high address allocation, stack memory is from high address to low address allocation 

2.C program stack space usage 

   In the C language program, the stack space is managed by the compiler, can be reflected in the program stack space using the example of parameter passing, return values, and automatic variable is the use of space. 

3.C program heap space usage 

   In the C language program, the heap memory allocation and release is done by calling the library function, and their use need to include standard library files: 
    # include
 

  There are four functions to achieve the heap memory allocation and release: 
     void * malloc (size_t size) to allocate memory space 
     void free (void * ptr) free memory space 
     void * calloc (size_t nmemb, size_t size) to allocate memory space 
     void * realloc (void * ptr, size_t size) to re-allocate memory space 

4. Heap memory and stack memory usage comparison 
    The swap of the most classic examples: 
   void swap (int a, int b) 
   ( 
     int tmp; 
     tmp = b; 
     b = a; 
     a = tmp; 
     return; 
   ) 

    This swap can not be achieved, because, swap function is called when you create a new stack space, when the stack upon return, the stack area has been released. Therefore, we must use the pointer. 

   I seriously appreciate the two examples: 

typedef struct _S100 

( 

   char string [100]; 

) S100; 

  

void fun_para1 (S100 a) 

( 

printf ( "From para:% s \ n", a.string); 

strcpy (a.string ,"-----"); 

printf ( "fun_para1 change:% s \ n", a.string); 

return; 

) 

void test_fun_para1 (void) 

( 

S100 a; 

strcpy (a.string, "+++++") 

fun_para1 (a); 

printf ( "after fun_para1 return:% s \ n", a.string); 

return; 

) 

Running Results: 

From para :+++++ 

fun_para1 change: ----- 

after fun_para1 return :+++++ 

  

void fun_para3 (char a [100]) 

( 

strcpy (a ,"-----"); 

printf ( "fun_para1 change:% s \ n", a); 

retrun; 

) 

void test_fun_para3 (void) 

( 

char a [100]; 

strcpy (a ,"+++++"); 

fun_para3 (a); 

printf ( "after fun_para3 return:% s \ n", a); 

return; 

) 

Running Results: 

from para :+++++ 

fun_para1 change :----- 

after fun_para3 return :----- 

  

From the run results, such a conclusion can be drawn:

when structure being used as parameters in function, the entire structure has been pushed onto the stack memory, just like a variable. 
when array being used as a parameter in function, it will be handled as a pointer