2009年12月21日 星期一

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

2009年11月25日 星期三

Intel Canmore嵌入式平台可以基於多種方式啟動

原文地址:http://samzhen.blogspot.com/2008/11/intel-ce3110canmore.html
Intel Canmore嵌入式平台可以基於多種方式啟動。


先介紹背景知識。
Intel CE3110 啟動時,首先去執行Non flash上的一個稱為CEFDK的程序。CEFDK做最初的初始化動作。然後將執行權交給redboot. redboot則將kernel和rootfs準備好並將執行權交給redboot.


CEFDK, redboot, kernel, rootfs放在不同的位置上,所以Canmore可以有多種啟動方式。


方式1:
CEFDK, redboot,kernel,rootfs全部放入Non flash.
這個方式有個前提, 因為Non flash非常小。 所以 kernel,rootfs必須非常基礎,非常小的。其實做不了什麼事。基本很少使用這種方式。


方式2:
CEFDK,redboot放在Non flash. Kernel放在tftp根目錄。rootfs放在nfs目錄。
這種方式常用來在開發階段使用。因為kernel,rootfs可以很容易的修改。


方式3:
CEFDK,redboot放在Non flash中,Kernel, rootfs放在SATA harddisk中。
這種方式是STB最終產品模式。


現在具體介紹不同方式的做法:


CEFDK在Canmore出廠時,就已經燒入到Non Flash中。所以需要做的就是把redboot, kernel,rootfs放到相應的位置。


方式2:
CEFDK,redboot放在Non flash. Kernel放在tftp根目錄。rootfs放在nfs目錄。
1.首先燒入redboot.
重新啟動Canmore時,按住空格鍵,則進入CEFDK界面:
shell>
shell> ymodem 0x200000 0
在Transfer->send File 中選擇ymodem. 並把redboot_flashboot.bin選中。
傳輸完成後,只是將image放到內存.還需要燒到Non flash中去。
shell>expflash burnFlash 0x200000 0x100000 0x20000
註:redboot_flashboot.bin表示是:這個版本的redboot是放在Non flash中的。
重新啟動Canmore,按ctrl+c.則進入redboot界面:
RedBoot>


2.將kernel放到tftpboot中,將rootfs放到NFS目錄中。
將kernel文件,bzImage放到server的/tftpboot中。(tftp要安裝好)
將rootfs目錄--fsroot放到NFS允許使用的目錄中。(nfs服務要安裝好 /etc/exports要允許Canmore IP地址使用這個目錄)


3.在redboot中,設置kernel,rootfs的放置地點。
RedBoot> fconfig


>> load -v -r -m tftp -h 172.16.1.61 -b 0x200000 bzImage
>> exec -b 0x200000 -l 0x300000 -c "console=ttyS0,115200 root=/dev/nfs rw nfsroot=172.16.1.61:/home/sam/Intel/Canmore/fsroot_canmore_1073_new ip=dhcp mem=exactmap memmap=640k@0 memmap=95M@1M"
>>
Boot script timeout (1000ms resolution): 2Use BOOTP for network configuration: trueDefault server IP address: 172.16.1.70Console baud rate: 115200GDB connection port: 9000Force console for special debug messages: falseNetwork debug at boot time: falseUpdate RedBoot non-volatile configuration - continue (y/n)? y


之後重新啟動,則redboot從172.16.1.61的tftpboot中拿kernel.
把172.16.1.61:/home/sam/Intel/Canmore/fsroot_canmore_1073_new 當作rootfs.






方式3: CEFDK,redboot放在Non flash中,Kernel, rootfs放在SATA harddisk中。
3.1.準備SATA harddisk.
3.1.1分區:
將SATA硬盤連接到開發機Linux上 ,而不是Canmore Linux上。
#fdisk -l


注意:/dev/sdb是4G固態硬盤。/dev/sda是Sam插入的40G SATA硬盤。
Sam想把SATA硬盤分2個區。第一個區用來放置redboot.第二個分區用來放kernel+rootfs.
注意:
#fdisk /dev/sda


Command (m for help): n
Command action e extended p primary partition (1-4)p
Partition number (1-4): 1
First cylinder (1-1022, default 1): Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-1022, default 1022): 1


Command (m for help): n
Command action e extended p primary partition (1-4)p
Partition number (1-4): 2
First cylinder (2-1022, default 2): Using default value 2
Last cylinder or +size or +sizeM or +sizeK (2-1022, default 1022): +1024M


Command (m for help): a
Partition number (1-4): 1
Command (m for help): w
The partition table has been altered!


n:新建partition.
p:邏輯分區
1,2:分區號。
First cylinder :第一個扇區號
Last cylinder or +size or +sizeM or +sizeK :最後一個扇區號.或者用+xxxM.


a: 制定boot flag.
w:執行以上動作。


3.1.2格式化/dev/sda2
#mkfs.ext3 /dev/sda2




3.2:放置redboot.bin到第一個分區。
首先使用tftpboot+nfs方式啟動。
# dd conv=sync if=redboot.bin of=/dev/sda1 [注1]
224+1 records in
225+0 records out


3.3:放置kernel+rootfs到第二個分區。
# mkdir disk
# mount /dev/sda2 /disk/
#cp bzImage /disk
#cd fsroot
# cp * -rf /disk/


3.4:設置redboot:(Sam認為應該是Non Flash中的redboot)
先查看當前Redboot設置。
Redboot> fconfig -l


寫Redboot:
Redboot> fconfig
>> load -v -r -m disk -b 0x200000 hda2:bzImage
>> exec -b 0x200000 -l 0x300000 -c "console=ttyS0,115200 root=/dev/sda2 rw mem=exactmap memmap=640K@0 memmap=95M@1M"
>>


重新啟動。OK




注1:
Linux命令dd:
dd: convert and copy a file.轉換和copy 文件。
conv= : convert the file as per the comma separated symbol list. 將文件轉化為指定的格式
其中:conv = sync:把每個輸入塊填充到ibs個字節,不足部分用空(NUL)字符補齊。
if=:read from FILE instead of stdin。輸入
of=:write to FILE instead of stdout。輸出
# dd conv=sync if=redboot.bin of=/dev/sda1
所以,這句話:將redboot.bin放入/dev/sda1