template libraru
ADxxxx.cpp@1:9524c69f480d, 2019-07-18 (annotated)
- Committer:
- MitchAD
- Date:
- Thu Jul 18 00:12:31 2019 +0000
- Revision:
- 1:9524c69f480d
- Parent:
- 0:ba4696a23d38
- Child:
- 2:1857aa1363ef
To Do: ; 1. Write two print functions for device and registers; 2. Make example of Sub Menu for Power Level; 3. Fix bug that deals with adxxxx_setup(); 4. Go through and make sure it is easy to follow everything is explicit
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 | #define ADXXXX_POWER_FULL 3 |
MitchAD | 1:9524c69f480d | 8 | #define ADXXXX_POWER_MED 2 |
MitchAD | 1:9524c69f480d | 9 | #define ADXXXX_POWER_LOW 1 |
MitchAD | 1:9524c69f480d | 10 | |
MitchAD | 1:9524c69f480d | 11 | #define ADXXXX_CALIBRATED 1 |
MitchAD | 1:9524c69f480d | 12 | #define ADXXXX_NOT_CALIBRATED 0 |
MitchAD | 1:9524c69f480d | 13 | /*Actual Declaration of all registers |
MitchAD | 1:9524c69f480d | 14 | based on the four fields declared in |
MitchAD | 1:9524c69f480d | 15 | the header file |
MitchAD | 1:9524c69f480d | 16 | {Address, Value, Size, Read or Write}*/ |
MitchAD | 1:9524c69f480d | 17 | struct adxxxx_reg adxxxx_all_regs[ADXXXX_REG_NUMB] = { |
MitchAD | 1:9524c69f480d | 18 | {0x00, 0x00, 1, 1}, /* EX_ADXXXX_READ_WRITE_REG */ |
MitchAD | 1:9524c69f480d | 19 | {0x01, 0x00, 1, 2}, /* EX_ADXXXX_READ_REG */ |
MitchAD | 1:9524c69f480d | 20 | {0x02, 0x00, 1, 3}, /* EX_ADXXXX_WRITE_REG */ |
MitchAD | 1:9524c69f480d | 21 | }; |
MitchAD | 1:9524c69f480d | 22 | |
MitchAD | 1:9524c69f480d | 23 | /*Function to malloc space for descriptor as well |
MitchAD | 1:9524c69f480d | 24 | as start the proper serial communication |
MitchAD | 1:9524c69f480d | 25 | **You should only be using either SPI or I2C at once** |
MitchAD | 1:9524c69f480d | 26 | Parameters: Pointer to a pointer to the device descriptor |
MitchAD | 1:9524c69f480d | 27 | Return Value: SUCCESS, FAILURE (You can make these more appropriate return |
MitchAD | 1:9524c69f480d | 28 | values if you would like, making it more |
MitchAD | 1:9524c69f480d | 29 | readable)*/ |
MitchAD | 1:9524c69f480d | 30 | static int adxxxx_setup(struct adxxxx_descriptor **device, struct adxxxx_init_params init_param) { |
MitchAD | 1:9524c69f480d | 31 | |
MitchAD | 1:9524c69f480d | 32 | int8_t return_val = 1; |
MitchAD | 1:9524c69f480d | 33 | struct adxxxx_descriptor * desc; |
MitchAD | 1:9524c69f480d | 34 | extern struct adxxxx_reg adxxxx_all_regs[ADXXXX_REG_NUMB]; |
MitchAD | 1:9524c69f480d | 35 | |
MitchAD | 1:9524c69f480d | 36 | desc = (struct adxxxx_descriptor *)malloc(sizeof(*desc)); |
MitchAD | 1:9524c69f480d | 37 | if (!desc) |
MitchAD | 1:9524c69f480d | 38 | return FAILURE; |
MitchAD | 1:9524c69f480d | 39 | |
MitchAD | 1:9524c69f480d | 40 | desc->all_regs = adxxxx_all_regs; |
MitchAD | 1:9524c69f480d | 41 | |
MitchAD | 1:9524c69f480d | 42 | /*Example of turning the device on and calibrating |
MitchAD | 1:9524c69f480d | 43 | it, but clearly this is device specific*/ |
MitchAD | 1:9524c69f480d | 44 | desc->is_calibrated = 1; |
MitchAD | 1:9524c69f480d | 45 | desc->power_level = 1; |
MitchAD | 1:9524c69f480d | 46 | |
MitchAD | 1:9524c69f480d | 47 | //return_val = spi_init(&desc->spi_desc, &init_params.spi_init); |
MitchAD | 1:9524c69f480d | 48 | //return_val = i2c_init(&desc->i2c_desc, &init_params.i2c_init); |
MitchAD | 1:9524c69f480d | 49 | if (return_val < 1) |
MitchAD | 1:9524c69f480d | 50 | return FAILURE; |
MitchAD | 1:9524c69f480d | 51 | |
MitchAD | 1:9524c69f480d | 52 | *device = desc; |
MitchAD | 1:9524c69f480d | 53 | return SUCCESS; |
MitchAD | 1:9524c69f480d | 54 | } |