-
Notifications
You must be signed in to change notification settings - Fork 3
/
MCP4725.h
149 lines (120 loc) · 4.81 KB
/
MCP4725.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*!\file MCP4725.h
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief MCP4725 Driver
** \details MCP4725: 12-Bit Digital-to-Analog Converter with EEPROM Memory
**/
/****************************************************************/
#ifndef __MCP4725_H__
#define __MCP4725_H__
#ifdef __cplusplus
extern "C" {
#endif
#include "sarmfsw.h"
#include "I2C_component.h"
#include "I2C_peripheral.h"
#if defined(HAL_I2C_MODULE_ENABLED)
/****************************************************************/
#ifndef I2C_MCP4725_NB
//! \note Define I2C_MCP4725_NB to enable multiple peripherals of this type
#define I2C_MCP4725_NB 1 //!< Number of MCP4725 peripherals
#endif
// *****************************************************************************
// Section: Constants
// *****************************************************************************
// For Adafruit MCP4725A1 the address is 0x62 (default) or 0x63 (ADDR pin tied to VDD)
// For MCP4725A0 the address is 0x60 or 0x61 // Microchip EvalBoard
// For MCP4725A2 the address is 0x64 or 0x65
#define MCP4725A0_ADDR 0x60 //!< MCP4725 address
#define MCP4725A1_ADDR 0x62 //!< MCP4725 with A1 address
#define MCP4725A2_ADDR 0x64 //!< MCP4725 with A2 address
#ifndef MCP4725_BASE_ADDR
//! \note Define MCP4725_BASE_ADDR to change default device base address
#define MCP4725_BASE_ADDR MCP4725A1_ADDR //!< MCP4725 Base address
#endif
// *****************************************************************************
// Section: Types
// *****************************************************************************
/*!\enum MCP4725_cmd
** \brief Register map enum of MCP4725
**/
typedef enum PACK__ MCP4725_cmd {
MCP4725__FAST_MODE = 0, //!< This command is used to change the DAC register. EEPROM is not affected
MCP4725__WRITE_DAC = 2, //!< Load configuration bits and data code to the DAC Register
MCP4725__WRITE_DAC_EEP, //!< Load configuration bits and data code to the DAC Register and also write the EEPROM
MCP4725__RESERVED_1, //!< Reserved for future use
MCP4725__RESERVED_2, //!< Reserved for future use
MCP4725__RESERVED_3, //!< Reserved for future use
MCP4725__RESERVED_4, //!< Reserved for future use
} MCP4725_cmd;
/*!\enum MCP4725_pd
** \brief Power down enum of MCP4725
**/
typedef enum PACK__ MCP4725_pd {
MCP4725__PWR_NORMAL = 0, //!< Normal mode
MCP4725__PWR_DOWN_1K, //!< 1K Ohm resistor to ground
MCP4725__PWR_DOWN_100K, //!< 100K Ohm resistor to ground
MCP4725__PWR_DOWN_500K //!< 500K Ohm resistor to ground
} MCP4725_pd;
/*!\union uMCP4725_REG__CMD
** \brief Union for CMD of MCP4725
**/
typedef union uMCP4725_REG__CMD {
uint8_t Byte;
struct PACK__ {
uint8_t :1;
MCP4725_pd PD :2; //!< Power down mode
uint8_t :2;
MCP4725_cmd C :3; //!< Write command type
} Bits;
} uMCP4725_REG__CMD;
// *****************************************************************************
// Section: Interface Routines
// *****************************************************************************
/******************/
/*** Slave init ***/
/******************/
/*!\brief Initialization of the MCP4725 peripheral
** \param[in] idx - MCP4725 index
** \param[in] hi2c - pointer to MCP4725 I2C instance
** \param[in] devAddress - MCP4725 device address
** \return FctERR - error code
**/
FctERR NONNULL__ MCP4725_Init(const uint8_t idx, I2C_HandleTypeDef * const hi2c, const uint16_t devAddress);
/*!\brief Initialization for MCP4725 peripheral
** \warning In case multiple devices (defined by I2C_MCP4725_NB > 1), you shall use MCP4725_Init instead
** \return FctERR - error code
**/
FctERR MCP4725_Init_Single(void);
/************************/
/*** Low level access ***/
/************************/
/*!\brief I2C general call for MCP4725
** \param[in] hi2c - pointer to general call I2C instance
** \param[in] cmd - Command to send
** \return FctERR - error code
**/
FctERR NONNULL__ MCP4725_General_Call(I2C_HandleTypeDef * const hi2c, const uint8_t cmd);
/*!\brief I2C Write function for MCP4725
** \param[in,out] pSlave - Pointer to I2C slave instance
** \param[in] data - pointer to write from
** \param[in] nb - Number of bytes to write
** \return FctERR - error code
**/
FctERR NONNULL__ MCP4725_Write(I2C_slave_t * const pSlave, const uint8_t * data, const uint16_t nb);
/*!\brief I2C Read function for MCP4725
** \param[in,out] pSlave - Pointer to I2C slave instance
** \param[in,out] data - pointer to read to
** \param[in] nb - Number of bytes to read
** \return FctERR - error code
**/
FctERR NONNULL__ MCP4725_Read(I2C_slave_t * const pSlave, uint8_t * data, const uint16_t nb);
/****************************************************************/
#include "MCP4725_proc.h" // Include procedures
#include "MCP4725_ex.h" // Include extensions
#endif
#ifdef __cplusplus
}
#endif
#endif /* __MCP4725_H__ */
/****************************************************************/