template libraru

ADxxxx.cpp

Committer:
MitchAD
Date:
2019-07-19
Revision:
2:1857aa1363ef
Parent:
1:9524c69f480d
Child:
3:ecb47ce6f212

File content as of revision 2:1857aa1363ef:

/******************************/
/*      C FILE TEMPLATE       */
/******************************/
#include "mbed.h" /*Neccessary Include Files*/
#include "ADxxxx.h"

/*Actual Declaration of all registers 
  based on the four fields declared in 
  the header file
  {Address, Value, Size, Read or Write}*/
struct adxxxx_reg adxxxx_all_regs[ADXXXX_REG_NUM] = {
    {0x00, 0x00,   1, 1}, /*EX_ADXXXX_POWER_ON*/
    {0x01, 0x00,   1, 1}, /*EX_ADXXXX_POWER_LEVEL*/    
    {0x02, 0x00,   1, 1}, /*EX_ADXXXX_CALIBRATION*/        
};

/*Function to malloc space for descriptor as well
  as start the proper serial communication
  **You should only be using either SPI or I2C at once**
  Parameters: Pointer to a pointer to the device descriptor
  Return Value: SUCCESS, FAILURE (You can make these more appropriate return 
                                  values if you would like, making it more
                                  readable)*/
int adxxxx_setup(struct adxxxx_descriptor **device, struct adxxxx_init_params init_param) {
    
    int8_t return_val = 1;
    struct adxxxx_descriptor * desc;
    extern struct adxxxx_reg adxxxx_all_regs[ADXXXX_REG_NUM];
    
    desc = (struct adxxxx_descriptor *)malloc(sizeof(*desc));
    if (!desc) 
        return FAILURE;
    
    desc->all_regs = adxxxx_all_regs;
    
    /*Example of turning the device on and calibrating
      it, but clearly this is device specific*/
    desc->is_calibrated = ADXXXX_NOT_CALIBRATED;
    desc->power_level = ADXXXX_POW_IDLE;
    
    /*Uncomment the protocol your device uses*/
    //return_val = spi_init(&desc->spi_desc, &init_params.spi_init);
    //return_val = i2c_init(&desc->i2c_desc, &init_params.i2c_init);
    if (return_val < 1) 
        return FAILURE; 
    
    *device = desc;
    return SUCCESS;     
}

/*Function to sweep through all of the registers and set them
  to their initial values
  Parameters: Pointer to adxxxx's descriptor
  Return Value: SUCCESS, FAILURE*/
int adxxxx_init_regs(struct adxxxx_descriptor *device) {
    /*Sweep through all of the registers that you initialized
      in the struct above and write all the values to the device
      (Could return register that failed to be written to may be
      helpful for debugging)*/
    return SUCCESS;    
}

/*Function to make a single or multi read from the device, based off 
  the registers size which you can find in the devices data sheet
  Parameters: Pointer to adxxxx's descriptor, register struct to read from
  Return Value: SUCCESS, FAILURE*/
int adxxx_read(struct adxxxx_descriptor *device, struct adxxxx_reg* reg) {
    /*Follow your devices specific write protocol (I2C, SPI) in conjunction 
      with the platform specific drivers to effectively communicate with your
      device (A good first step is perhaps reading your devices ID register
      or something else that is easy to get to)*/
    return SUCCESS;        
}

/*Function to make a single or multi write to the device, based off 
  the registers size which you can find in the devices data sheet
  Parameters: Pointer to adxxxx's descriptor, register struct to read from
  Return Value: SUCCESS, FAILURE*/
int adxxx_write(struct adxxxx_descriptor *device, struct adxxxx_reg* reg, uint32_t data) {
    /*Follow your devices specific write protocol (I2C, SPI) in conjunction 
      with the platform specific drivers to effectively communicate with your
      device (Try and write to a read-enabled register and then immediately
      read back the value you wrote)*/
    reg->value = data;
    return SUCCESS;    
}