-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMCP4725.c
81 lines (54 loc) · 2.66 KB
/
MCP4725.c
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
/*!\file MCP4725.c
** \author SMFSW
** \copyright MIT (c) 2017-2024, SMFSW
** \brief MCP4725 Driver
** \details MCP4725: 12-Bit Digital-to-Analog Converter with EEPROM Memory
**/
/****************************************************************/
#include "MCP4725.h"
#if defined(HAL_I2C_MODULE_ENABLED)
#if defined(I2C_MCP4725)
/****************************************************************/
static const I2C_slave_t MCP4725_defaults = { { pNull, 0, I2C_slave_timeout, I2C_MEMADD_SIZE_8BIT, I2C_HS }, 0, HAL_OK, true, false };
static I2C_slave_t MCP4725_hal[I2C_MCP4725_NB]; //!< MCP4725 Slave structure
/****************************************************************/
FctERR NONNULL__ MCP4725_Init(const uint8_t idx, I2C_HandleTypeDef * const hi2c, const uint16_t devAddress)
{
FctERR err;
assert_param(IS_I2C_PERIPHERAL(MCP4725, idx));
I2C_PERIPHERAL_SET_DEFAULTS(MCP4725, idx);
err = I2C_slave_init(&MCP4725_hal[idx], hi2c, devAddress, MCP4725_hal[idx].cfg.timeout);
if (!err) { err = MCP4725_Init_Sequence(&MCP4725[idx]); }
if (err) { I2C_set_enable(&MCP4725_hal[idx], false); }
return err;
}
FctERR MCP4725_Init_Single(void) {
return MCP4725_Init(0, I2C_MCP4725, MCP4725_BASE_ADDR); }
/****************************************************************/
FctERR NONNULL__ MCP4725_General_Call(I2C_HandleTypeDef * const hi2c, const uint8_t cmd)
{
if ((cmd != MCP4725__RESET) && (cmd != MCP4725__WAKEUP)) { return ERROR_CMD; } // Unknown command
return HALERRtoFCTERR(HAL_I2C_Master_Transmit((I2C_HandleTypeDef *) hi2c, I2C_ADDR_General_Call, (uint8_t *) &cmd, 1, I2C_slave_timeout));
}
FctERR NONNULL__ MCP4725_Write(I2C_slave_t * const pSlave, const uint8_t * data, const uint16_t nb)
{
if (!I2C_is_enabled(pSlave)) { return ERROR_DISABLED; } // Peripheral disabled
if (nb > 3) { return ERROR_RANGE; } // More bytes than registers
I2C_set_busy(pSlave, true);
pSlave->status = HAL_I2C_Master_Transmit(pSlave->cfg.bus_inst, pSlave->cfg.addr, (uint8_t *) data, nb, pSlave->cfg.timeout);
I2C_set_busy(pSlave, false);
return HALERRtoFCTERR(pSlave->status);
}
FctERR NONNULL__ MCP4725_Read(I2C_slave_t * const pSlave, uint8_t * data, const uint16_t nb)
{
if (!I2C_is_enabled(pSlave)) { return ERROR_DISABLED; } // Peripheral disabled
if (nb > 3) { return ERROR_RANGE; } // More bytes than registers
I2C_set_busy(pSlave, true);
pSlave->status = HAL_I2C_Master_Receive(pSlave->cfg.bus_inst, pSlave->cfg.addr, data, nb, pSlave->cfg.timeout);
I2C_set_busy(pSlave, false);
return HALERRtoFCTERR(pSlave->status);
}
/****************************************************************/
#endif
#endif
/****************************************************************/