Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / alld_at24c01bn-sh-b.c @ ef078306

History | View | Annotate | Download (6.67 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2019  Thomas Schöpping et al.
4

5
This program is free software: you can redistribute it and/or modify
6
it under the terms of the GNU Lesser General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9

10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
GNU Lesser General Public License for more details.
14

15
You should have received a copy of the GNU Lesser General Public License
16
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18

    
19
/**
20
 * @file    alld_at24c01bn-sh-b.c
21
 * @brief   EEPROM function implementations.
22
 *
23
 * @addtogroup lld_eeprom
24
 * @{
25
 */
26

    
27
#include <alld_at24c01bn-sh-b.h>
28

    
29
#if defined(AMIROLLD_CFG_USE_AT24C01BN) || defined(__DOXYGEN__)
30

    
31
#include <string.h>
32

    
33
/******************************************************************************/
34
/* LOCAL DEFINITIONS                                                          */
35
/******************************************************************************/
36

    
37
/******************************************************************************/
38
/* EXPORTED VARIABLES                                                         */
39
/******************************************************************************/
40

    
41
/******************************************************************************/
42
/* LOCAL TYPES                                                                */
43
/******************************************************************************/
44

    
45
/******************************************************************************/
46
/* LOCAL VARIABLES                                                            */
47
/******************************************************************************/
48

    
49
/******************************************************************************/
50
/* LOCAL FUNCTIONS                                                            */
51
/******************************************************************************/
52

    
53
/******************************************************************************/
54
/* EXPORTED FUNCTIONS                                                         */
55
/******************************************************************************/
56

    
57
/**
58
 * @brief Acknowledgement polling to check if a write operation finished.
59
 * @param[in]   at24c01bn The eeprom driver to use.
60
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
61
 *
62
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
63
 */
64
inline apalExitStatus_t
65
at24c01bn_lld_poll_ack(const AT24C01BNDriver* const at24c01bn, const apalTime_t timeout)
66
{
67
  apalDbgAssert(at24c01bn != NULL);
68

    
69
  uint8_t tmp = 0;
70
  return apalI2CMasterReceive(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), &tmp, 1, timeout);
71
}
72

    
73
/**
74
 * @brief Read the data from the eeprom starting at the current address.
75
 * @param[in]   at24c01bn The eeprom driver to use.
76
 * @param[out]  data      The data read from the AT24C01BN.
77
 * @param[in]   num       The amount of bytes to read.
78
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
79
 *
80
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
81
 */
82
inline apalExitStatus_t
83
at24c01bn_lld_read_current_address(const AT24C01BNDriver* const at24c01bn, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
84
{
85
  apalDbgAssert(at24c01bn != NULL);
86
  apalDbgAssert(data != NULL);
87

    
88
  return apalI2CMasterReceive(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), data, num, timeout);
89
}
90

    
91
/**
92
 * @brief Read the data from the eeprom starting at a given address.
93
 * @param[in]   at24c01bn The eeprom driver to use.
94
 * @param[in]   addr      The address to read.
95
 * @param[out]  data      The data read from the AT24C01BN.
96
 * @param[in]   num       The amount of bytes to read.
97
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
98
 *
99
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
100
 */
101
inline apalExitStatus_t
102
at24c01bn_lld_read(const AT24C01BNDriver* const at24c01bn, const uint8_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
103
{
104
  apalDbgAssert(at24c01bn != NULL);
105
  apalDbgAssert(data != NULL);
106
  apalDbgAssert(addr + num <= AT24C01BN_LLD_SIZE_BYTES);
107

    
108
  apalExitStatus_t status = apalI2CMasterTransmit(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), &addr, 1, NULL, 0, timeout);
109
  if (status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) {
110
    return apalI2CMasterReceive(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), data, num, timeout);
111
  } else {
112
    return status;
113
  }
114
}
115

    
116
/**
117
 * @brief Write a byte to the eeprom.
118
 * @param[in]   at24c01bn The eeprom driver to use.
119
 * @param[in]   addr      The address to start writing.
120
 * @param[out]  data      The data to be written to the AT24C01BN.
121
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
122
 *
123
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
124
 */
125
inline apalExitStatus_t
126
at24c01bn_lld_write_byte(const AT24C01BNDriver* const at24c01bn, const uint8_t addr, const uint8_t data, const apalTime_t timeout)
127
{
128
  apalDbgAssert(at24c01bn != NULL);
129

    
130
  const uint8_t writedata[2] = {addr, data};
131
  return apalI2CMasterTransmit(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), writedata, 2, NULL, 0, timeout);
132
}
133

    
134
/**
135
 * @brief Write a page to the eeprom.
136
 * @param[in]   at24c01bn The eeprom driver to use.
137
 * @param[in]   addr      The address to start writing.
138
 * @param[in]   data      The data to be written to the AT24C01BN.
139
 * @param[in]   num       The amount of bytes to write.
140
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
141
 *
142
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
143
 */
144
inline apalExitStatus_t
145
at24c01bn_lld_write_page(const AT24C01BNDriver* const at24c01bn, const uint8_t addr, const uint8_t* const data, const uint8_t num, const apalTime_t timeout)
146
{
147
  apalDbgAssert(at24c01bn != NULL);
148
  apalDbgAssert(data != NULL);
149

    
150
  uint8_t writedata[num+1];
151
  writedata[0] = addr;
152
  memcpy(&writedata[1], data, num);
153
  return apalI2CMasterTransmit(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), writedata, num+1, NULL, 0, timeout);
154
}
155

    
156
#endif /* defined(AMIROLLD_CFG_USE_AT24C01BN) */
157

    
158
/** @} */