Página 1 de 1

Passar pointer de struct para funcao

MensagemEnviado: 30 Set 2010 07:31
por albertorcneto
Estou tendo problemas para compilar o seguinte codigo:

beagle_spi.h
Código: Selecionar todos
#ifndef BEAGLE_SPI_H_GUARD
#define BEAGLE_SPI_H_GUARD

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/types.h>

/* File name for memory access */
#ifndef FILE_PINCONF
#define FILE_PINCONF      "/dev/mem"
#endif
#define FILE_MCSPI      "/dev/mem"

/* Base adresses */
#ifndef BASE_PINCONF
#define BASE_PINCONF      0x48000000
#endif
#define BASE_MCSPI1      0x48098000     // page 192 , page 2849 of OMAP3530 processor's manual
#define BASE_MCSPI2      0x4809A000
#define BASE_MCSPI3      0x480B8000
#define BASE_MCSPI4      0x480BA000

/* Other definitions */
#define SPISIZE   0x10000

/* SPI Register structure : bit field accessible (for future compatibility) */
//struct SPI_t {
//   union {               // Example on how to access bit fields in a 32-bit word
//      ulong all;
//      struct {
//         unsigned minor:4;
//         unsigned major:4;
//      };
//   } revision;               // offset: 0x00
//   ulong dump_0[3];            // jump: 0x04, 0x08, 0x0C
//   union {
//      ulong all;
//      struct {
//         unsigned autoidle:1;      // bit  [0]
//         unsigned softreset:1;   // bit  [1]
//         unsigned enawakeup:1;   // bit  [2]
//         unsigned sidlemode:2;   // bits [3:4]
//         unsigned dump_0:3;      // bits [5:7]
//         unsigned clockactivity:2;   // bits [8:9]
//      };
//   } sysconfig;               // offset: 0x10
//   union {
//      ulong all;
//      struct {
//         unsigned
//      };
//   } sysstatus;               // offset: 0x14
//   union {
//      ulong all;
//      struct {
//         unsigned
//      };
//   } irqstatus;      // offset: 0x18
//   ulong irqenable;      // offset: 0x1C
//   ulong wakeupenable;      // offset: 0x20
//   ulong syst;         // offset: 0x24
//   ulong modulctrl;      // offset: 0x28
//   struct {         // channel structure (size 0x14)
//      ulong conf;      // offset: 0x2C + (0x14 * x)
//      ulong stat;      // offset: 0x30 + (0x14 * x)
//      ulong ctrl;      // offset: 0x34 + (0x14 * x)
//      ulong tx;      // offset: 0x38 + (0x14 * x)
//      ulong rx;      // offset: 0x3C + (0x14 * x)
//   } channel[4];
//   ulong dump_1[5];      // jump: from 0x40 to 0x78
//   ulong xferlevel;      // offset: 0x7C
//};

/* SPI Register structure : 32-bit (word) field accessible */
struct SPI_t {
   ulong REVISION;      // offset: 0x00
   ulong dummy_0[3];      // jump: 0x04, 0x08, 0x0C
   ulong SYSCONFIG;      // offset: 0x10
   ulong SYSSTATUS;      // offset: 0x14
   ulong IRQSTATUS;      // offset: 0x18
   ulong IRQENABLE;      // offset: 0x1C
   ulong WAKEUPENABLE;      // offset: 0x20
   ulong SYST;         // offset: 0x24
   ulong MODULCTRL;      // offset: 0x28
   struct {         // channel structure (size 0x14)
      ulong CONF;      // offset: 0x2C + (0x14 * x)
      ulong STAT;      // offset: 0x30 + (0x14 * x)
      ulong CTRL;      // offset: 0x34 + (0x14 * x)
      ulong TX;      // offset: 0x38 + (0x14 * x)
      ulong RX;      // offset: 0x3C + (0x14 * x)
   } CH[4];
   ulong dummy_1[5];      // jump: from 0x40 to 0x78
   ulong XFERLEVEL;      // offset: 0x7C
};

/* SPI Register Vector : vector accessible (for backwards compatibility) */
//typedef ulong[SIZESPI] SPI_t;

/* Function prototypes */
SPI_t    *   spiopen (const ulong baseaddress);
ulong       spigetc (SPI_t *spipointer, uint channel);

#endif


beagle_spi.c
Código: Selecionar todos
#ifndef BEAGLE_SPI_C_GUARD
#define BEAGLE_SPI_C_GUARD


#include "beagle_spi.h"

SPI_t * spiopen (const ulong baseaddress) {
   int fd;
   fd = open(FILE_PINCONF, O_RDWR | O_SYNC);

   if (fd < 0) {                  
      printf("Could not open memory. Error: %d.\n", errno);
      return(NULL);
   }

   /* Pad configuration: clocks must be previously enabled before accesing McSPI registers */
   volatile ulong *pinconf;
   pinconf = (ulong*) mmap(NULL, 0x10000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, BASE_PINCONF);
   if (pinconf == MAP_FAILED) {
      printf("Pinconf Mapping failed. Error: %d.\n", errno);
      close(fd);
      return(NULL);
   }
   ulong mask;
   switch (baseaddress) {
      case BASE_MCSPI3:
         mask = 0x00100000;
         pinconf[0x2158/4] = (pinconf[0x2158/4] | 0x00000119);   // pin configured as mcspi3_clk    Page 853
         pinconf[0x2158/4] = (pinconf[0x2158/4] | 0x01090000);   // pin configured as mcspi3_simo   Page 853
         pinconf[0x215C/4] = (pinconf[0x215C/4] | 0x00000109);   // pin configured as mcspi3_somi   Page 853
         pinconf[0x2160/4] = (pinconf[0x2160/4] | 0x01190000);   // pin configured as mcspi3_cs0    Page 853
         break;
      case BASE_MCSPI4:
         mask = 0x00200000;
         /* Configure Expansion header pins as SPI output. */
         pinconf[0x218C/4] = (pinconf[0x218C/4] | 0x00000119);   // pin configured as mcspi4_clk    Page 853
         pinconf[0x2190/4] = (pinconf[0x2190/4] | 0x00000119);   // pin configured as mcspi4_simo   Page 853
         pinconf[0x2190/4] = (pinconf[0x2190/4] | 0x01190000);   // pin configured as mcspi4_somi   Page 853
         pinconf[0x2194/4] = (pinconf[0x2194/4] | 0x01190000);   // pin configured as mcspi4_cs0    Page 853
         break;
   }
   pinconf[0x4A00/4] = pinconf[0x4A00/4] | mask;         // McSPI functional clock is enabled
   pinconf[0x4A10/4] = pinconf[0x4A10/4] | mask;         // McSPI  interface clock is enabled
   pinconf[0x4A20/4] = pinconf[0x4A20/4] & (mask ^ 0xFFFFFFFF);   // McSPI can be accessed      

   fclose(fd);

   fd = open(FILE_MCSPI, O_RDWR | O_SYNC);

   if (fd < 0) {                  
      printf("Could not open memory. Error: %d.\n", errno);
      return(NULL);
   }

   SPI_t *mcspi;
   mcspi = (SPI_t*) mmap(NULL, sizeof(SPI_t), PROT_READ | PROT_WRITE, MAP_SHARED, fd, baseaddress);
   if (mcspi == MAP_FAILED) {
      printf("McSPI Mapping failed. Error: %d.\n", errno);
      close(fd);
      return(NULL);
   }

   return(&mcspi);
}

ulong spigetc (SPI_t *spipointer, uint channel) {
   spipointer->CH[channel].TX = 0x00000000;         // Transmit dummy data (MUST ALWAYS BE 0x00000000 to avoid cross-talk)
   while (!(spipointer->CH[channel].STAT | 0xFFFFFFFE));   // Wait for set bit in reception end flag
   return (spipointer->CH[cahnnel].RX);
}

#endif


O compilador aponta erro nas duas linhas do arquivo beagle_spi.h:

Código: Selecionar todos
SPI_t    *   spiopen (const ulong baseaddress);
ulong       spigetc (SPI_t *spipointer, uint channel);


e nessas duas linhas do arquivo beagle_spi.c:

Código: Selecionar todos
SPI_t  * spiopen (const ulong baseaddress) {
ulong spigetc (SPI_t *spipointer, uint channel) {


O que eu to fazendo de errado? Nao to conseguindo perceber nada. Alguem poderia me dar uma ajuda?

MensagemEnviado: 30 Set 2010 07:48
por tcpipchip
Passa o erro para turma...

MensagemEnviado: 30 Set 2010 08:35
por albertorcneto
O erro:

Código: Selecionar todos
ebv@ebvbeagle:~/projeto$ gcc -O3 -funroll-loops -Wall -c -o beagle_spi.o beagle_spi.c
In file included from beagle_spi.c:30:
beagle_spi.h:134: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before â*â token
beagle_spi.h:135: error: expected â)â before â*â token
beagle_spi.c:32: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before â*â token
beagle_spi.c:91: error: expected â)â before â*â token
ebv@ebvbeagle:~/projeto$

MensagemEnviado: 30 Set 2010 08:51
por helton
você está uitilizando uma estrutura, não criou um tipo de dado (typedef struct { ...}SPI_t;)

Código: Selecionar todos
struct SPI_t  * spiopen (const ulong baseaddress);
ulong       spigetc (struct SPI_t *spipointer, uint channel);

MensagemEnviado: 30 Set 2010 09:06
por albertorcneto
Valeu helton!

MensagemEnviado: 30 Set 2010 09:13
por albertorcneto
Como ficou:

Código: Selecionar todos
/* SPI Register structure : 32-bit (word) field accessible */
struct {
    ulong REVISION;        // offset: 0x00
    ulong dummy_0[3];        // jump: 0x04, 0x08, 0x0C
    ulong SYSCONFIG;        // offset: 0x10
    ulong SYSSTATUS;        // offset: 0x14
    ulong IRQSTATUS;        // offset: 0x18
    ulong IRQENABLE;        // offset: 0x1C
    ulong WAKEUPENABLE;        // offset: 0x20
    ulong SYST;            // offset: 0x24
    ulong MODULCTRL;        // offset: 0x28
    struct {            // channel structure (size 0x14)
        ulong CONF;        // offset: 0x2C + (0x14 * x)
        ulong STAT;        // offset: 0x30 + (0x14 * x)
        ulong CTRL;        // offset: 0x34 + (0x14 * x)
        ulong TX;        // offset: 0x38 + (0x14 * x)
        ulong RX;        // offset: 0x3C + (0x14 * x)
    } CH[4];
    ulong dummy_1[5];        // jump: from 0x40 to 0x78
    ulong XFERLEVEL;        // offset: 0x7C
} SPI_struct;

typedef struct SPI_struct SPI_t;


Agora estou tendo problemas nas linhas da funcao spigetc. Voce poderia me dizer o que estou fazendo errado? Eh a primeira vez que to fazendo esse tipo de abordagem (ponteiro para estrutura), entao ainda estou apanhando.

MensagemEnviado: 30 Set 2010 10:11
por albertorcneto
Resolvido!

Código: Selecionar todos
/* SPI Register structure : 32-bit (word) field accessible */
typedef struct {
    ulong REVISION;        // offset: 0x00
    ulong dummy_0[3];        // jump: 0x04, 0x08, 0x0C
    ulong SYSCONFIG;        // offset: 0x10
    ulong SYSSTATUS;        // offset: 0x14
    ulong IRQSTATUS;        // offset: 0x18
    ulong IRQENABLE;        // offset: 0x1C
    ulong WAKEUPENABLE;        // offset: 0x20
    ulong SYST;            // offset: 0x24
    ulong MODULCTRL;        // offset: 0x28
    struct {            // channel structure (size 0x14)
        ulong CONF;        // offset: 0x2C + (0x14 * x)
        ulong STAT;        // offset: 0x30 + (0x14 * x)
        ulong CTRL;        // offset: 0x34 + (0x14 * x)
        ulong TX;        // offset: 0x38 + (0x14 * x)
        ulong RX;        // offset: 0x3C + (0x14 * x)
    } CH[4];
    ulong dummy_1[5];        // jump: from 0x40 to 0x78
    ulong XFERLEVEL;        // offset: 0x7C
} SPI_t;