views:

197

answers:

4

I always seem to encounter this dilemma when writing low level code for MCU's. I never know where to declare pin definitions so as to make the code as reusable as possible.

In this case Im writing a driver to interface an 8051 to a MCP4922 12bit serial DAC. Im unsure how/where I should declare the pin definitions for The CS(chip select) and LDAC(data latch) for the DAC. At the moment there declared in the header file for the driver.

Iv done a lot of research trying to figure out the best approach but havent really found anything.

Im basically want to know what the best practices... if there are some books worth reading or online information, examples etc, any recommendations would be welcome.

Just a snippet of the driver so you get the idea

/**
    @brief  This function is used to write a 16bit data word  to DAC B -12 data bit plus 4 configuration bits
    @param  dac_data A 12bit word 
    @param  ip_buf_unbuf_select Input Buffered/unbuffered  select bit. Buffered = 1; Unbuffered = 0
    @param  gain_select Output Gain Selection bit. 1 = 1x (VOUT = VREF * D/4096).  0 =2x (VOUT = 2 * VREF * D/4096)
*/
void MCP4922_DAC_B_TX_word(unsigned short int dac_data, bit ip_buf_unbuf_select, bit gain_select)
{                                             

    unsigned char low_byte=0, high_byte=0;
    CS = 0;                                               /**Select the chip*/

    high_byte |= ((0x01 << 7) | (0x01 << 4));            /**Set bit to select DAC A and Set SHDN bit high for DAC A active operation*/
    if(ip_buf_unbuf_select) high_byte |= (0x01 << 6);
    if(gain_select)         high_byte |= (0x01 << 5);

    high_byte |= ((dac_data >> 8) & 0x0F);
    low_byte |= dac_data;
    SPI_master_byte(high_byte);
    SPI_master_byte(low_byte);

    CS = 1;                                               
    LDAC = 0;                                             /**Latch the Data*/
    LDAC = 1;                                         
}
+3  A: 

This is what I did in a similar case, this example is for writing an I²C driver:

// Structure holding information about an I²C bus
struct IIC_BUS
{
    int pin_index_sclk;
    int pin_index_sdat;
};

// Initialize I²C bus structure with pin indices
void iic_init_bus( struct IIC_BUS* iic, int idx_sclk, int idx_sdat );

// Write data to an I²C bus, toggling the bits
void iic_write( struct IIC_BUS* iic, uint8_t iicAddress, uint8_t* data, uint8_t length );

All pin indices are declared in an application-dependent header file to allow quick overview, e.g.:

// ...
#define MY_IIC_BUS_SCLK_PIN 12
#define MY_IIC_BUS_SCLK_PIN 13
#define OTHER_PIN 14
// ...

In this example, the I²C bus implementation is completely portable. It only depends on an API that can write to the chip's pins by index.

Edit:

This driver is used like this:

// main.c
#include "iic.h"
#include "pin-declarations.h"

main()
{
    struct IIC_BUS mybus;
    iic_init_bus( &mybus, MY_IIC_BUS_SCLK_PIN, MY_IIC_BUS_SDAT_PIN );

    // ...

    iic_write( &mybus, 0x42, some_data_buffer, buffer_length );
}
Timbo
This is the kind of thing I was looking for thanks. I will summarize my understanding to ensure I am understanding... if thats ok. 1) A structure to hold PIN info is defined in the driver header file 2) An instance of the structure is declared in your main app 3) pining info defined in another header is assigned to the struture 4) structure is passed to driver routines as neccesary. Is this right?
volting
I added a usage example for further clarification, but you got the point. I do not claim that this is the absolute best way to write a device driver for an embedded system, but in my application is seemed suitable.
Timbo
Thanks got it now, I see the assignment gets done in the iic_init_bus() -makes its nice and tidy..Even if its not the "absolute best" its certainly better then what I had.. I really need to find a good book on good-practices for embedded programming to save me asking these type questions...Thanks again
volting
I just found out that it isn't possible to pass pin definitions on my platform keil+8051 so Im back to square one, I think the best way (in this case)is to probably to put the pin definitions in separate configuration header, unless anybody has any better ideas.
volting
+1  A: 

In one shop I worked at, the pin definitions were put into a processor specific header file. At another shop, I broke the header files into themes associated with modules in the processor, such as DAC, DMA and USB. A master include file for the processor included all of these themed header files. We could model different varieties of the same processor by include different module header files in the processor file.

You could create an implementation header file. This file would define I/O pins in terms of the processor header file. This gives you one layer of abstraction between your application and the hardware. The idea is to loosely couple the application from hardware as much as possible.

Thomas Matthews
Thanks ...some worthy tips. I was leaning implementation header file type solution, it seems to be the only sane solution for my current platform. Thanks
volting
+1  A: 

If only the driver needs to know about the CS pin, then the declaration should not appear in the header, but within the driver module itself. Code re-use is best served by hiding data at the most restrictive scope possible.

In the event that an external module needs to control CS, add an access function to the device driver module so that you have single point control. This is useful if during debugging you need to know where and when an I/O pin is being asserted; you only have one point to apply instrumentation or breakpoints.

Clifford
Yes only the driver needs to know about the CS pin but I want to implement some sort of API that would allow me to use a different MCU port pin in different projects or for porting to other platforms without messing with the driver module ( which obviously I would have to change the CS address..)
volting
It hardly matters whether you change the header or the .c, it is all source code and will need to be recompiled in any case.
Clifford
In fact all more the reason to hide the hardware specifics, the header file will then remain identical for all implementations. On another platform, the implementation is likely to require changes in any case; if you follow your suggestion, *both* files will require changes rather than just one. Header file should ideally define interfaces not internals.
Clifford
A: 

The answer with the run-time configuration will work for a decent CPU like ARM, PowerPC...but the author is running a 8051 here. #define is probably the best way to go. Here's how I would break it down:

blah.h:

#define CSN_LOW()   CS = 0
#define CSN_HI()    CS = 1
#define LATCH_STROBE() \
 do { LDAC = 0; LDAC = 1; } while (0)

blah.c:
#include <blah.h>
void blah_update( U8 high, U8 low ) 
{
   CSN_LOW();
   SPI_master_byte(high);
   SPI_master_byte(low);
   CSN_HI();
   LATCH_STROBE();
} 

If you need to change the pin definition, or moved to a different CPU, it should be obvious where you need to update. And it's also helps when you have to adjust the timing on the bus (ie. insert a delay here and there) as you don't need to change all over the place. Hope it helps.

fseto