小编sve*_*ven的帖子

spidev无法控制芯片选择信号

我在嵌入式linux设备上使用内核3.12.rc4(olimex imx233 micro).我的目标是使用/dev/spidev能够与另一个spi设备通信.

我编辑arch/arm/boot/dts/imx23-olinuxino.dts为:

ssp1: ssp@80034000 {
  #address-cells = <1>;
  #size-cells = <0>;
  compatible = "fsl,imx23-spi";
  pinctrl-names = "default";
  pinctrl-0 = <&spi2_pins_a>;
  clock-frequency = <1000000>;
  status = "okay";

  spidev: spidev@0 {
    compatible = "spidev";
    spi-max-frequency = <1000000>;
    reg = <1>;
  };
};
Run Code Online (Sandbox Code Playgroud)

arch/arm/boot/dts/imx23.dtsi: 有这个配置

spi2_pins_a: spi2@0 {
  reg = <0>;
  fsl,pinmux-ids = <
    0x0182 /* MX23_PAD_GPMI_WRN__SSP2_SCK */
    0x0142 /* MX23_PAD_GPMI_RDY1__SSP2_CMD */
    0x0002 /* MX23_PAD_GPMI_D00__SSP2_DATA0 */
    0x0032 /* MX23_PAD_GPMI_D03__SSP2_DATA3 */
  >;
  fsl,drive-strength = <1>;
  fsl,voltage = <1>; …
Run Code Online (Sandbox Code Playgroud)

linux spi linux-kernel

9
推荐指数
1
解决办法
7855
查看次数

linux命令由popen在C代码上执行

我有下面的代码,我在这里引用线程来使用该popen函数

int main(int argc,char *argv[]){    
    FILE* file = popen("ntpdate", "r");
    char buffer[100];
    fscanf(file, "%100s", buffer);
    pclose(file);
    printf("buffer is :%s\n", buffer);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

它输出:

21 Apr 03:03:03 ntpdate[4393]: no server can be used, exiting
buffer is:
Run Code Online (Sandbox Code Playgroud)

为什么printf不输出任何东西?如果我ls用作命令,则printf输出ls输出.我做错了ntpdate什么?

如果我执行下面的代码(参考网页)

#define COMMAND_LEN 8
#define DATA_SIZE 512

int main(int argc,char *argv[]){


    FILE *pf;
       char command[COMMAND_LEN];
       char data[DATA_SIZE];

       // Execute a process listing
       sprintf(command, "ntpdate");

       // Setup our pipe for reading and …
Run Code Online (Sandbox Code Playgroud)

c linux popen

8
推荐指数
1
解决办法
8万
查看次数

inet_aton转换010.000.000.001错了?

我有inet_aton转换网络地址的问题.下面的代码可以很好地转换地址10.0.0.1

char *x1;
struct sockaddr_in si_other;
inet_aton("10.0.0.1", &si_other.sin_addr);
printf("si_other.sin_addr =%lu\n",si_other.sin_addr);
x1 = inet_ntoa(si_other.sin_addr);
printf("x1=%s\n",x1);
Run Code Online (Sandbox Code Playgroud)

它输出:

si_other.sin_addr =16777226
x1=10.0.0.01
Run Code Online (Sandbox Code Playgroud)

到目前为止没问题.但是,该函数在010.000.000.001传递时会起作用

char *x2;
struct sockaddr_in si_other2;
inet_aton("010.000.000.001", &si_other2.sin_addr);
printf("si_other2.sin_addr =%lu\n",si_other2.sin_addr);
x2 = inet_ntoa(si_other2.sin_addr);
printf("x2=%s\n",x2);
Run Code Online (Sandbox Code Playgroud)

输出:

si_other.sin_addr2 =16777224
x2=8.0.0.01
Run Code Online (Sandbox Code Playgroud)

当该功能正常工作192.168.0.1192.168.000.001传递.

任何人都可以解释我的问题是什么以及如何解决问题?(注意:我需要传递010.000.000.001我的代码中的IP地址)

c sockets inet-aton

7
推荐指数
1
解决办法
1537
查看次数

inet_aton IPv4地址的规范化

inet_aton假设规范化互联网地址的点版本?为什么我会得到下面示例的不同输出值?

int main(){
    char USER_IP[16] = "192.168.002.025";
    char USER_IP2[16] = "192.168.2.25";
    struct sockaddr_in addr;
    struct sockaddr_in addr2;

    inet_aton(USER_IP2, &addr.sin_addr);
    inet_aton(USER_IP, &addr2.sin_addr);

    printf("addr.sin_addr:%lu\n", addr.sin_addr);
    printf("addr2.sin_addr:%lu\n", addr2.sin_addr);


    return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:

addr.sin_addr:419604672
addr2.sin_addr:352495808
Run Code Online (Sandbox Code Playgroud)

c linux radix inet-aton

7
推荐指数
1
解决办法
262
查看次数

C编程错误处理

我希望我的代码能够处理错误情况,例如函数返回失败.例如,pthread_create通常我使用下面的函数:

int thread_check1;
 pthread_t function;
 thread_check1 = pthread_create( &function, NULL, function_function,  NULL);
if(thread_check1 != 0){
   fprintf(stderr, "pthread_create error\n");
   exit(1);
}
Run Code Online (Sandbox Code Playgroud)

考虑到错误情况,调用相同的函数直到它返回0(对于这个特定的函数)是否正确,如下所示?

thread_check1 = pthread_create( &function, NULL, function_function,  NULL);
while(thread_check1 != 0){
    thread_check1 = pthread_create( &function, NULL, function_function,  NULL);
}
Run Code Online (Sandbox Code Playgroud)

我可以将相同的逻辑应用于返回值的其他C函数吗?否则,您如何在不退出程序的情况下建议处理错误情况(函数返回)?

c error-handling pthreads

5
推荐指数
1
解决办法
1426
查看次数

为什么ioctl会返回"糟糕的地址"

我使用下面的代码从嵌入式电路板的SPI端口输出数据(olimex imx233-micro - 它不是特定于电路板的问题).当我运行代码ioctl返回" 坏地址 ".我正在修改http://twilight.ponies.cz/spi-test.c上的代码,工作正常.谁能告诉我我做错了什么?

root@ubuntu:/home# gcc test.c -o test
test.c:20: warning: conflicting types for ‘msg_send’
test.c:16: note: previous implicit declaration of ‘msg_send’ was here
root@ubuntu:/home# ./test
errno:Bad address - cannot send SPI message
root@ubuntu:/home# uname -a
Linux ubuntu 3.7.1 #2 Sun Mar 17 03:49:39 CET 2013 armv5tejl GNU/Linux
Run Code Online (Sandbox Code Playgroud)

码:

//test.c
#include <stdint.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <getopt.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/spi/spidev.h>
#include <errno.h>

static uint16_t …
Run Code Online (Sandbox Code Playgroud)

c linux posix ioctl

5
推荐指数
1
解决办法
1万
查看次数

如果多个pthread使用相同的函数会发生什么

我想知道如果两个线程同时调用同一个函数会发生什么,并且该函数是一个通过套接字发送文本的UDP客户端.

考虑到下面的代码,我一直在运行它,但我还没有任何错误.我想知道它是否应该崩溃,因为线程同时使用相同的源(函数,变量,IP,端口),它们如何共享源?我可以想象下面的代码是多线程的错误用法,你能解释一下如何使用线程,以便线程只使用该函数而不使用其他线程吗?换句话说,它怎么可能是线程安全的?

作为Linux上的C代码示例:

void *thread1_fcn();
void *thread2_fcn();
void msg_send(char *message);

int main(void){
    pthread_t thread1, thread2;
    pthread_create( &thread1, NULL, thread1_fcn,  NULL);
    pthread_create( &thread2, NULL, thread2_fcn,  NULL);
    while(1){}
    return 0;
}

void *thread1_fcn(){
    while(1){
        msg_send("hello");
        usleep(500);
    }
    pthread_exit(NULL);
}

void *thread2_fcn(){
    while(1){
        msg_send("world");
        usleep(500);
    }
    pthread_exit(NULL);
}

void msg_send(char message[]){
       struct sockaddr_in si_other;
       int s=0;
       char SRV_IP[16] = "192.168.000.002";

        s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
        memset((char *) &si_other, 0, sizeof(si_other));
        si_other.sin_family = AF_INET;
        si_other.sin_port = htons(12346);
        si_other.sin_addr.s_addr = htonl(INADDR_ANY);
        inet_aton(SRV_IP, &si_other.sin_addr);
        sendto(s, message, …
Run Code Online (Sandbox Code Playgroud)

c linux multithreading pthreads thread-safety

5
推荐指数
2
解决办法
2351
查看次数

snprintf - 格式不是字符串文字且没有格式参数警告

format not a string literal and no format arguments当我在 Linux 上编译它时,我收到警告。snprintf显示const char*第三个参数。const char *INTERFACE = "wlan0"定义然后将其传递给函数有什么问题 ?

#include <stdio.h>
#include <net/if.h>
#include <string.h>

int main(int argc,char *argv[]){
    const char *INTERFACE        = "wlan0";
    struct ifreq ifr;

    memset(&ifr, 0, sizeof(ifr));
    snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), INTERFACE);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c linux printf gcc posix

4
推荐指数
1
解决办法
9888
查看次数

在Qt-Creator SDK上以root身份运行代码

我使用Qt-Creator 2.5.2 SDK在Linux上开发C代码.当从SDK界面运行代码时,如何以root身份运行代码(例如,ctrl + R)?

c linux qt qt-creator

4
推荐指数
1
解决办法
5262
查看次数

设置UDP套接字的recv fcn超时

我发送一个UDP数据包sendto,然后收到答案.recv如果recv没有收到回复,程序就不会继续.但是,udp数据包可能会丢失,或者由于某种原因,数据包可能无法传送,因此程序卡recv在线.我想知道recv如果nopacket到达时如何设置超时,例如一分钟,然后跳过该行并继续执行代码?

我没有粘贴完整的代码,因为它是一个通用udp代码,我的问题只与唯一相关recv.最后要说明的是,开发环境是linux.

unsigned long  buf[maxlen];
struct protoent *proto;     //
struct sockaddr_in server_addr;
int s;  // socket
memset( &server_addr, 0, sizeof( server_addr ));
server_addr.sin_family=AF_INET;
server_addr.sin_addr.s_addr = inet_addr(hostname);
sendto(s,msg,sizeof(msg),0,(struct sockaddr *)&server_addr,sizeof(server_addr));

recv(s,buf,sizeof(buf),0);
Run Code Online (Sandbox Code Playgroud)

c sockets linux posix udp

3
推荐指数
2
解决办法
1万
查看次数