【问题标题】:gcc Warning Passing argument 2 of 'alt_16550_fifo_write' makes pointer from integer without a castgcc 警告传递 'alt_16550_fifo_write' 的参数 2 使指针从整数而不进行强制转换
【发布时间】:2015-05-30 05:38:27
【问题描述】:

我目前正在从事一个涉及 Cyclone V ARM Cortex A9 处理器和外部设备的项目。我在 C 编程方面相对较新。我正在使用处理器的 UART 接口在 C 中的 API 的帮助下从外部发送和接收数据。当我编译我的代码时,我收到警告说我正在传递从整数而不在函数中进行强制转换的参数@ 987654321@。有人可以帮忙吗?

下面是我的代码

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>     // string function definitions
#include <unistd.h>     // UNIX standard function definitions
#include <fcntl.h>      // File control definitions
#include <errno.h>      // Error number definitions
#include <termios.h>    // POSIX terminal control definitions
#include <stdbool.h>
#include <sys/types.h>
#include "ergo.h"
#include "alt_clock_manager.h"
#include "hwlib.h"
#include "alt_clock_group.h"
#include "alt_hwlibs_ver.h"
#include "alt_16550_uart.h"
#include "uart.h"
#include "socal/alt_clkmgr.h"
#include "socal/alt_rstmgr.h"
#include "socal/alt_uart.h"
#include "socal/hps.h"
#include "socal/socal.h"



/* commands to control the ergo bike */
#define ERGO_CMD_GET_ADDRESS   0x11
#define ERGO_CMD_RUN_DATA      0x40
#define ERGO_CMD_SET_WATT     0x51
#define UART_MAX_DATA       20
#define enable_init          TRUE


/*Global Variables*/
ergo_run_data_t ergo_run_data;
u_int8_t ergo_adr_int;

/* External Functions that are called in the main function*/
static ALT_STATUS_CODE alt_16550_reset_helper(ALT_16550_HANDLE_t * handle, bool enable_init);
static inline uint32_t alt_read_word_helper(const void * addr);
static ALT_STATUS_CODE alt_16550_write_divisor_helper(ALT_16550_HANDLE_t * handle,uint32_t divisor);
ALT_STATUS_CODE alt_clk_clock_enable(ALT_CLK_t ALT_CLK_L4_SP);
ALT_STATUS_CODE alt_clk_is_enabled(ALT_CLK_t ALT_CLK_L4_SP);
ALT_STATUS_CODE alt_clk_freq_get(ALT_CLK_t ALT_CLK_L4_SP,alt_freq_t* freq);
ALT_STATUS_CODE alt_16550_fifo_write(ALT_16550_HANDLE_t * handle,const char * buffer,size_t count);
void ergo_get_address(ALT_16550_HANDLE_t * handle);
void ergo_get_run_data(void);
void ergo_set_watt(u_int8_t ergo_adr_int, u_int8_t watt);
void ergo_reset(ALT_16550_HANDLE_t * handle,u_int8_t ergo_adr_int);
void ergo_break(void);


/*function to enable the SOCFPGA UART Clock*/
ALT_STATUS_CODE alt_clk_clock_enable(ALT_CLK_t ALT_CLK_L4_SP)
{


    if (alt_clk_clock_enable(ALT_CLK_L4_SP) != ALT_E_ERROR)
    {

      return ALT_E_SUCCESS;  // The operation was successfull
    }
    else
    {
      return ALT_E_ERROR;  // The operation was not successfull
    }

}

/*Function to check whether the SOCFPGA Clock is enabled*/
ALT_STATUS_CODE alt_clk_is_enabled(ALT_CLK_t ALT_CLK_L4_SP)
{
        ALT_16550_HANDLE_t * handle;
    handle->clock_freq = 0;

   if (alt_clk_is_enabled(ALT_CLK_L4_SP) != ALT_E_TRUE)
    {
      return ALT_E_BAD_CLK;
    }
  else
   {
    ALT_STATUS_CODE status;
    status = alt_clk_freq_get(ALT_CLK_L4_SP, &handle->clock_freq);
   }
}

//function to get the clock frequency
ALT_STATUS_CODE alt_clk_freq_get(ALT_CLK_t ALT_CLK_L4_SP,alt_freq_t* freq)
{

     ALT_16550_HANDLE_t * handle;
     handle->clock_freq = 0;

     ALT_STATUS_CODE status;
     status = alt_clk_freq_get(ALT_CLK_L4_SP, &handle->clock_freq);

     if (status != ALT_E_SUCCESS)
            {
                return status;
            }

}

struct uart_data_t
{

      size_t tx_count;                  /*amount of data to send*/

      char tx_buffer[UART_MAX_DATA];    /*data to send*/

      size_t rx_count;                  /*amount of data to send*/

      char rx_buffer[UART_MAX_DATA];    /*data received*/


}uart_data_t;

/*==========================UART functions======================*/

/*-----------------------------  uart_init() -------------------------*/
/**
 * Übergabeparameter:   -
 * Return:              -
 * Funktion:            Initialisiert UART-Schnittstelle
*---------------------------------------------------------------------*/


int main()
{
     /* Open File Descriptor */
    int USB = open( "/dev/ttyUSB0", O_RDWR|O_NOCTTY|O_NONBLOCK);


     //Error Handling
    if ( USB < 0 )
    {
    printf("Error beim oeffnen");
    }



        // Configure Port
    struct termios tty;
    struct termios tty_old;
    memset (&tty, 0, sizeof tty);

    // Error Handling
    if ( tcgetattr ( USB, &tty ) != 0 )
        {
       printf("error beim tcgetattr");
    }

    // Save old tty parameters
    tty_old = tty;



    ALT_16550_HANDLE_t * handle;
    handle->data;
    handle->fcr;
    handle->clock_freq;
    handle->location; //ALT_UART0_ADDR
    handle->device;

        //ALT_16550_DEVICE_SOCFPGA_UART0 = 0; //This option selects UART0 in the SoC FPGA



        ALT_16550_DEVICE_t device;
        ALT_STATUS_CODE status;
        alt_freq_t clock_freq;
        void *location;
        const void *  addr;
        bool enable_init;
        uint32_t baudrate = ALT_16550_BAUDRATE_9600;

        uint32_t divisor;  //((handle->clock_freq + (8 * baudrate)) / (16 * baudrate));

        printf("Program start \n");

         // Enable the UART Clock
         alt_clk_clock_enable(ALT_CLK_L4_SP);


    // Helper function to reset and Initialise the UART (UART 0)
     alt_16550_reset_helper(handle, enable_init);

    // Helper function to carryout the actual register read.
     alt_read_word_helper(addr);

    //Helper function to write the divisor in Hardware
    alt_16550_write_divisor_helper(handle,divisor);

    //Enable the UART (UART 0)
    alt_16550_enable(handle);

    //Enable the FIFO
    alt_16550_fifo_enable(handle);

    //Get the Ergometer address
    ergo_get_address(handle);

    return 0;
}


/*---------------------------  ergo_get_adr() ------------------------*/
/**
 * Übergabeparameter:   -
 * Return:              -
 * Funktion:            Holen der Ergometer-Adreesse (1 Byte)
*---------------------------------------------------------------------*/

void ergo_get_address(ALT_16550_HANDLE_t * handle)
{
        struct uart_data_t data;

    /* build up data frame for address request */
    data.tx_count = 1;       // amount of data to send
    data.tx_buffer[0] = ERGO_CMD_GET_ADDRESS;
    data.rx_count = 2;        /*amount of data to receive*/

    /* get address from ergo bike */
        alt_16550_fifo_write(handle, *ERGO_CMD_GET_ADDRESS,1);
        alt_16550_fifo_read(handle,data.rx_buffer,2);

    /* save ergo address if the bike responded */
    if(data.rx_buffer[0] == ERGO_CMD_GET_ADDRESS)
    {
        ergo_adr_int = data.rx_buffer[1];
                printf("%d\n",data.rx_buffer[1]);
    }

    /* wait for 50ms */
    ergo_break();

  return;
}

/*----------------------------  ergo_reset() -------------------------*/
/**
 * Übergabeparameter:   u_int8_t ergo_adr_int
 * Return:              -
 * Funktion:            Setzt Ergometer zurück
*---------------------------------------------------------------------*/

void ergo_reset(ALT_16550_HANDLE_t * handle,u_int8_t ergo_adr_int)
{
  alt_16550_fifo_write(handle,0x12,1);
  alt_16550_fifo_write(handle,ergo_adr_int,1);
 ergo_break();
return;
}

/*----------------------------  ergo_break() -------------------------*/
/**
 * Übergabeparameter:   -
 * Return:              -
 * Funktion:            Wait for about 50 ms
*---------------------------------------------------------------------*/
void ergo_break(void)
{
    u_int16_t d1;
    u_int8_t d2;

    //wait for ~50 ms
    for(d1=0; d1 < 65535; d1++)



    {
        for(d2=0; d2 < 64; d2++)
                {
                }
    }

    return;
}

【问题讨论】:

  • 窄。问题。下来。

标签: c linux pointers int compiler-warnings


【解决方案1】:

在您的代码中,您的函数原型是

ALT_STATUS_CODE alt_16550_fifo_write
                 (ALT_16550_HANDLE_t * handle,const char * buffer,size_t count);

第二个参数是const char * buffer

调用此函数时,您正在使用

void ergo_reset(ALT_16550_HANDLE_t * handle,u_int8_t ergo_adr_int)
{
  alt_16550_fifo_write(handle,0x12,1);
  alt_16550_fifo_write(handle,ergo_adr_int,1);
 ergo_break();
return;
}

这里,0x12ergo_adr_int 不是 const char * 类型。

  • 0x12 是一个 integer 常量(准确地说是十六进制常量)#
  • ergo_adr_int 属于 u_int8_t 类型。

因此出现不匹配和警告。

解决

您需要传递一个const char * 变量作为函数的第二个参数。


# :: 根据C11 标准文档,第 6.4.4.1 章,整数常量

十六进制常量:

十六进制前缀十六进制数字

十六进制常量十六进制数字

在哪里

十六进制前缀:其中之一

0x 0X

十六进制数:其中之一

0 1 2 3 4 5 6 7 8 9

a b c d e f

A B C D E F

【讨论】:

  • 非常感谢您的贡献。我已经能够解决这个问题,也从中学到了很多东西。
  • @user3628617 不客气。 :-) 顺便说一句,您可以考虑 accepting 一个对您有帮助的答案。
【解决方案2】:

alt_16550_fifo_write() 的原型是:

ALT_STATUS_CODE alt_16550_fifo_write(ALT_16550_HANDLE_t * handle,
                                     const char * buffer, size_t count);

您在两个地方使用它:

alt_16550_fifo_write(handle, *ERGO_CMD_GET_ADDRESS,1);
...
alt_16550_fifo_write(handle,0x12,1);

第二个参数错误。应该是const char * buffer,比如某个字符串。

【讨论】:

  • Jurkovic 非常感谢您的贡献。我已经能够解决这个问题,也从中学到了很多东西。
【解决方案3】:

这是你alt_16550_fifo_write函数的声明

ALT_STATUS_CODE alt_16550_fifo_write(ALT_16550_HANDLE_t * handle,const char * buffer,size_t count);

第二个参数是指针类型,但在你的程序中你总是传递整数值:

alt_16550_fifo_write(handle,0x12,1);
alt_16550_fifo_write(handle,ergo_adr_int,1);

您需要传递一个指向char 的指针。例如:

alt_16550_fifo_write(handle, &(char) {0x12}, 1);

char x = 0x12;
alt_16550_fifo_write(handle, &x, 1);  

【讨论】:

  • 非常感谢您的贡献。我已经能够解决这个问题,也从中学到了很多东西。
猜你喜欢
  • 1970-01-01
  • 2019-09-17
  • 1970-01-01
  • 2012-11-03
  • 1970-01-01
  • 1970-01-01
  • 2016-03-05
  • 1970-01-01
  • 2019-02-18
相关资源
最近更新 更多