template libraru
ADxxxx.cpp@2:1857aa1363ef, 2019-07-19 (annotated)
- Committer:
- MitchAD
- Date:
- Fri Jul 19 00:12:11 2019 +0000
- Revision:
- 2:1857aa1363ef
- Parent:
- 1:9524c69f480d
- Child:
- 3:ecb47ce6f212
To do: ; 1. Still make quasi calibrate function (maybe not); 2a. Go through and make sure it is readable and easy to understand; 2b. Add more hints about what certain things do?; 2c. Make it super usable!;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MitchAD | 1:9524c69f480d | 1 | /******************************/ |
MitchAD | 1:9524c69f480d | 2 | /* C FILE TEMPLATE */ |
MitchAD | 1:9524c69f480d | 3 | /******************************/ |
MitchAD | 1:9524c69f480d | 4 | #include "mbed.h" /*Neccessary Include Files*/ |
MitchAD | 1:9524c69f480d | 5 | #include "ADxxxx.h" |
MitchAD | 1:9524c69f480d | 6 | |
MitchAD | 1:9524c69f480d | 7 | /*Actual Declaration of all registers |
MitchAD | 1:9524c69f480d | 8 | based on the four fields declared in |
MitchAD | 1:9524c69f480d | 9 | the header file |
MitchAD | 1:9524c69f480d | 10 | {Address, Value, Size, Read or Write}*/ |
MitchAD | 2:1857aa1363ef | 11 | struct adxxxx_reg adxxxx_all_regs[ADXXXX_REG_NUM] = { |
MitchAD | 2:1857aa1363ef | 12 | {0x00, 0x00, 1, 1}, /*EX_ADXXXX_POWER_ON*/ |
MitchAD | 2:1857aa1363ef | 13 | {0x01, 0x00, 1, 1}, /*EX_ADXXXX_POWER_LEVEL*/ |
MitchAD | 2:1857aa1363ef | 14 | {0x02, 0x00, 1, 1}, /*EX_ADXXXX_CALIBRATION*/ |
MitchAD | 1:9524c69f480d | 15 | }; |
MitchAD | 1:9524c69f480d | 16 | |
MitchAD | 1:9524c69f480d | 17 | /*Function to malloc space for descriptor as well |
MitchAD | 1:9524c69f480d | 18 | as start the proper serial communication |
MitchAD | 1:9524c69f480d | 19 | **You should only be using either SPI or I2C at once** |
MitchAD | 1:9524c69f480d | 20 | Parameters: Pointer to a pointer to the device descriptor |
MitchAD | 1:9524c69f480d | 21 | Return Value: SUCCESS, FAILURE (You can make these more appropriate return |
MitchAD | 1:9524c69f480d | 22 | values if you would like, making it more |
MitchAD | 1:9524c69f480d | 23 | readable)*/ |
MitchAD | 2:1857aa1363ef | 24 | int adxxxx_setup(struct adxxxx_descriptor **device, struct adxxxx_init_params init_param) { |
MitchAD | 1:9524c69f480d | 25 | |
MitchAD | 1:9524c69f480d | 26 | int8_t return_val = 1; |
MitchAD | 1:9524c69f480d | 27 | struct adxxxx_descriptor * desc; |
MitchAD | 2:1857aa1363ef | 28 | extern struct adxxxx_reg adxxxx_all_regs[ADXXXX_REG_NUM]; |
MitchAD | 1:9524c69f480d | 29 | |
MitchAD | 1:9524c69f480d | 30 | desc = (struct adxxxx_descriptor *)malloc(sizeof(*desc)); |
MitchAD | 1:9524c69f480d | 31 | if (!desc) |
MitchAD | 1:9524c69f480d | 32 | return FAILURE; |
MitchAD | 1:9524c69f480d | 33 | |
MitchAD | 1:9524c69f480d | 34 | desc->all_regs = adxxxx_all_regs; |
MitchAD | 1:9524c69f480d | 35 | |
MitchAD | 1:9524c69f480d | 36 | /*Example of turning the device on and calibrating |
MitchAD | 1:9524c69f480d | 37 | it, but clearly this is device specific*/ |
MitchAD | 2:1857aa1363ef | 38 | desc->is_calibrated = ADXXXX_NOT_CALIBRATED; |
MitchAD | 2:1857aa1363ef | 39 | desc->power_level = ADXXXX_POW_IDLE; |
MitchAD | 1:9524c69f480d | 40 | |
MitchAD | 2:1857aa1363ef | 41 | /*Uncomment the protocol your device uses*/ |
MitchAD | 1:9524c69f480d | 42 | //return_val = spi_init(&desc->spi_desc, &init_params.spi_init); |
MitchAD | 1:9524c69f480d | 43 | //return_val = i2c_init(&desc->i2c_desc, &init_params.i2c_init); |
MitchAD | 1:9524c69f480d | 44 | if (return_val < 1) |
MitchAD | 1:9524c69f480d | 45 | return FAILURE; |
MitchAD | 1:9524c69f480d | 46 | |
MitchAD | 1:9524c69f480d | 47 | *device = desc; |
MitchAD | 1:9524c69f480d | 48 | return SUCCESS; |
MitchAD | 2:1857aa1363ef | 49 | } |
MitchAD | 2:1857aa1363ef | 50 | |
MitchAD | 2:1857aa1363ef | 51 | /*Function to sweep through all of the registers and set them |
MitchAD | 2:1857aa1363ef | 52 | to their initial values |
MitchAD | 2:1857aa1363ef | 53 | Parameters: Pointer to adxxxx's descriptor |
MitchAD | 2:1857aa1363ef | 54 | Return Value: SUCCESS, FAILURE*/ |
MitchAD | 2:1857aa1363ef | 55 | int adxxxx_init_regs(struct adxxxx_descriptor *device) { |
MitchAD | 2:1857aa1363ef | 56 | /*Sweep through all of the registers that you initialized |
MitchAD | 2:1857aa1363ef | 57 | in the struct above and write all the values to the device |
MitchAD | 2:1857aa1363ef | 58 | (Could return register that failed to be written to may be |
MitchAD | 2:1857aa1363ef | 59 | helpful for debugging)*/ |
MitchAD | 2:1857aa1363ef | 60 | return SUCCESS; |
MitchAD | 2:1857aa1363ef | 61 | } |
MitchAD | 2:1857aa1363ef | 62 | |
MitchAD | 2:1857aa1363ef | 63 | /*Function to make a single or multi read from the device, based off |
MitchAD | 2:1857aa1363ef | 64 | the registers size which you can find in the devices data sheet |
MitchAD | 2:1857aa1363ef | 65 | Parameters: Pointer to adxxxx's descriptor, register struct to read from |
MitchAD | 2:1857aa1363ef | 66 | Return Value: SUCCESS, FAILURE*/ |
MitchAD | 2:1857aa1363ef | 67 | int adxxx_read(struct adxxxx_descriptor *device, struct adxxxx_reg* reg) { |
MitchAD | 2:1857aa1363ef | 68 | /*Follow your devices specific write protocol (I2C, SPI) in conjunction |
MitchAD | 2:1857aa1363ef | 69 | with the platform specific drivers to effectively communicate with your |
MitchAD | 2:1857aa1363ef | 70 | device (A good first step is perhaps reading your devices ID register |
MitchAD | 2:1857aa1363ef | 71 | or something else that is easy to get to)*/ |
MitchAD | 2:1857aa1363ef | 72 | return SUCCESS; |
MitchAD | 2:1857aa1363ef | 73 | } |
MitchAD | 2:1857aa1363ef | 74 | |
MitchAD | 2:1857aa1363ef | 75 | /*Function to make a single or multi write to the device, based off |
MitchAD | 2:1857aa1363ef | 76 | the registers size which you can find in the devices data sheet |
MitchAD | 2:1857aa1363ef | 77 | Parameters: Pointer to adxxxx's descriptor, register struct to read from |
MitchAD | 2:1857aa1363ef | 78 | Return Value: SUCCESS, FAILURE*/ |
MitchAD | 2:1857aa1363ef | 79 | int adxxx_write(struct adxxxx_descriptor *device, struct adxxxx_reg* reg, uint32_t data) { |
MitchAD | 2:1857aa1363ef | 80 | /*Follow your devices specific write protocol (I2C, SPI) in conjunction |
MitchAD | 2:1857aa1363ef | 81 | with the platform specific drivers to effectively communicate with your |
MitchAD | 2:1857aa1363ef | 82 | device (Try and write to a read-enabled register and then immediately |
MitchAD | 2:1857aa1363ef | 83 | read back the value you wrote)*/ |
MitchAD | 2:1857aa1363ef | 84 | reg->value = data; |
MitchAD | 2:1857aa1363ef | 85 | return SUCCESS; |
MitchAD | 1:9524c69f480d | 86 | } |