發表文章

目前顯示的是有「C sample code」標籤的文章

Socket example

圖片
reference: http://kezeodsnx.pixnet.net/blog/post/27462696-socket-programming-%E7%AD%86%E8%A8%98 http://www.cs.dartmouth.edu/~campbell/cs50/socketprogramming.html

C語言測試 想成為嵌入式程序員應知道的0x10個基本問題

============================================== C語言測試 想成為嵌入式程序員應知道的0x10個基本問題 原文出處 A 'C' Test: The 0x10 Best Questions for Would-be Embedded Programmers: http://www.embedded.com/2000/0005/0005feat2.htm 以下是網路上找到中文版,作者不詳。 ============================================== C語言測試是徵選嵌入式系統程式員過程中必須而且有效的方法。這些年我既參加也組織 了許多這種測試,在這過程中我意識到這些測試能為面試者和被面試者提供許多有用訊息 ,此外,撇開面試的壓力不談,這種測試也是相當有趣的。 從被面試者的角度來講,你能瞭解許多關於出題者或監考者的情況。這個測試只是出題者 出某個字符的ASCII值。這些問題著重考察你的系統調用(invoke)和記憶體分發策略方面 的能力嗎?這反映出出題者也許花時間在微處理機上而不在嵌入式系統上。 如果上述任何問題的答案是「是」的話,那麼我知道我得認真考慮我是否應該去做這份工 作。 從面試者的角度來講,一個測試也許能從多方面揭示應試者的素質。最基本的,你能瞭解 應試者C語言的水準。不管怎麼樣,看一下這人如何回答他不會的問題也是滿有趣。應試 者是以好的直覺做出明智的選擇,還是只是瞎蒙呢?當應試者在某個問題上卡住時是找藉 口呢,還是表現出對問題的真正的好奇心,把這看成學習的機會呢?我發現這些訊息與他 們的測試成績一樣有用。 有了這些想法,我決定出一些真正針對嵌入式系統的考題,希望這些令人頭痛的考題能給 正在謀職的人一點幫住。這些問題都是我這些年實際碰到的。其中有些題很難,但它們應 該都能給你一點啟發。 這個測試適用於不同水準的應試者,大多數初級水準的應試者的成績會很差,經驗豐富的 程式員應該有很好的成績。為了讓你能自己決定某些問題的偏好,每個問題沒有分發分數 ,如果選擇這些考題為你所用,請自行按你的意思分發分數。 ============================================== 預處理器 (Preprocessor) 1.用預處理指令#define 聲...

Linux GPIO user mode 使用方法

以下是 GPIO 的使用方法, interrupt 還沒試過... /* Copyright (c) 2011, RidgeRun     * All rights reserved.     *      * Redistribution and use in source and binary forms, with or without     * modification, are permitted provided that the following conditions are met:     * 1. Redistributions of source code must retain the above copyright     *  notice, this list of conditions and the following disclaimer.     * 2. Redistributions in binary form must reproduce the above copyright     *  notice, this list of conditions and the following disclaimer in the     *  documentation and/or other materials provided with the distribution.     * 3. All advertising materials mentioning features or use of this software     *  must display the following acknowledgement:     *  This product includes software developed by the RidgeRun.     *...

Virtual Mouse Linux Kernel Driver

在 Fred's blog 看到了 virtual mouse driver, 先紀錄下來, 有空在研究. 試過, 可以用. /*  * A Virtual Mouse Driver to send fake events from userspace.  *  * Written by Fred Chien <fred@ullab.org>  *  */ #include <linux/fs.h> #include <asm/uaccess.h> #include <linux/pci.h> #include <linux/input.h> #include <linux/platform_device.h> struct input_dev *virmouse_input_dev; static struct platform_device *virmouse_dev; /* Device structure */ /* Sysfs method to input simulated coordinates */ static ssize_t write_virmouse(struct device *dev,                               struct device_attribute *attr,                               const char *buffer, size_t count) {         int x, y, key;         /* parsing input data */       ...

用 c 讀取 i2c 的 register

C example 底下是個範例, 詳細資料參考 http://www.mjmwired.net/kernel/Documentation/i2c/dev-interface  會更清楚. write.c #include <stdio.h> #include <fcntl.h> #include <linux/i2c-dev.h> #include <errno.h> #define I2C_ADDR 0x20 int main (void) {     int value;     int fd;     fd = open("/dev/i2c-0", O_RDWR);     if (fd < 0) {         printf("Error opening file: %s\n", strerror(errno));         return 1;     }     if (ioctl(fd, I2C_SLAVE, I2C_ADDR) < 0) {         printf("ioctl error: %s\n", strerror(errno));         return 1;     }     for (value=0; value<=255; value++) {         if (write(fd, &value, 1) != 1) {             printf("Error writing file: %s\n", strerror(errno));        ...

linux 下 用I2C tools 來讀取 i2c 上的 register

前陣子再測試 TI DM8168 時, 發現 i2c bus 好像有標準的 tool 可以測試,  找了一下, 發現了一套 i2c-tools, 請到 lm-sensors 網站下載. compile 後應該有四個執行檔 i2cdetect – 用來列舉I2C bus和上面所有的裝置 i2cdump – 顯示裝置上所有register的值 i2cget – 讀取裝置上某個register的值 i2cset – 寫入裝置上某個register example: #i2cdetect -l i2c-1 i2c       OMAP I2C adapter                 I2C adapter i2c-2 i2c       OMAP I2C adapter                 I2C adapter #i2cdetect -y -r 1      0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f 00:          -- -- -- -- -- -- -- -- -- -- -- -- --  10: -- -- -- -- -- -- -- -- UU -- -- -- -- -- -- --  20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --  50: -- -- ...

SSDP UDP broadcast ...

#include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/select.h> #include <sys/time.h> #include <sys/socket.h> #include <netdb.h> #include <stdio.h> #define RESPONSE_BUFFER_LEN 1024 #define SSDP_MULTICAST      "239.255.255.250" #define SSDP_PORT           1900 int main (int argc, const char * argv[]) { int sock; size_t ret; unsigned int socklen; struct sockaddr_in sockname; struct sockaddr clientsock; struct hostent *hostname; char data[] =  "M-SEARCH * HTTP/1.1\r\n" "Host: 239.255.255.250:1900\r\n" "Man: \"ssdp:discover\"\r\n" "ST:upnp:rootdevice\r\n" "MX:3\r\n" "\r\n"; char buffer[RESPONSE_BUFFER_LEN]; unsigned int len = RESPONSE_BUFFER_LEN; fd_set fds; struct timeval timeout; hostname = gethostbyname(SSDP_MULTICAST); hostname->h_addrtype = AF_INET; if((sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) { p...