Em 22/7/2010 12:51, Matt Rhys-Roberts escreveu: >> Why don't you pass the port as an argument to the functions? >> Instead of duplicating a lot of code, just duplicate and select between >> the code that access the hardware? >> >> Or you could do as in desktop systems, OpenSPI takes the port number and >> returns a handle which must be passed to the other functions or -1 if it >> fails? >> >> This way you could share the SPI ports between threads (if you are using >> a RTOS). I do it this way in my designs, for SPI, USART, etc. >> >> >> Isaac >> = > Hi Isaac, > > Thanks for the reply... Sorry to sound stupid, but you've lost me = > altogether with "pass the port as an argument to the functions", and = > "duplicate and select between the code that accesses the hardware" = > confuses me. Can you suggest an example please? Example (pseudo-code, not compiled): static char portisopen[2] =3D { 0 , 0 }; signed char OpenSPI( char port, unsigned char sync_mode, unsigned char bus_mode, unsigned char smp_phase ) { if( port < 1 || port > sizeof portisopen / sizeof portisopen[0] ) return -1; if( portisopen[port-1] !=3D 0 ) return -1; portisopen[port-1] =3D 1; if( port =3D=3D 1 ) { // Initialize the hardware of SPI port 1 here. } else { // Initialize the hardware of SPI port 2 here. } = return 0; } void CloseSPI( char port ) { if( port < 1 || port > sizeof portisopen / sizeof portisopen[0] ) return -1; if( portisopen[port-1] =3D=3D 0 ) return; if( port =3D=3D 1 ) { // Stop the hardware of SPI port 1 here. } else { // Stop the hardware of SPI port 2 here. } portisopen[port-1] =3D 0; } unsigned char TransceiveSPI( char port, unsigned char data_out ) { unsigned char data_in; if( port < 1 || port > sizeof portisopen / sizeof portisopen[0] ) return -1; if( portisopen[port-1] =3D=3D 0 ) return 0; if( port =3D=3D 1 ) { // Send data_out and receive_data_in to/from SPI port 1. } else { // Send data_out and receive_data_in to/from SPI port 2. } return data_in; } > Referring to http://en.wikipedia.org/wiki/Function_prototype , I = > understand that prototyping functions before using them helps to make = > the compiler's job easier and prevents confusing errors. > > I'm not multithreading or using a RTOS here, just trying to configure = > things politely in C so that I can make use of C's more elegant = > operators when it comes to data manipulation and processing. > > NB. I've been using PIC assembly for years, so I'm not familiar with = > some of the higher-minded C concepts yet! > > Best regards, > Matt. I understand, your questions were more about of C language techniques than implementation... Welcome to the C language world! Best regards, Isaac __________________________________________________ Fale com seus amigos de gra=E7a com o novo Yahoo! Messenger = http://br.messenger.yahoo.com/ = -- = http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist