Statistics
| Branch: | Tag: | Revision:

amiro-lld / source / AT24C01B / v1 / alld_AT24C01B_v1.c @ ef5bc242

History | View | Annotate | Download (6.638 KB)

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

5
This program is free software: you can redistribute it and/or modify
6 f0ca400f Thomas Schöpping
it under the terms of the GNU Lesser General Public License as published by
7 d6728c5b Thomas Schöpping
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 f0ca400f Thomas Schöpping
GNU Lesser General Public License for more details.
14 d6728c5b Thomas Schöpping

15 f0ca400f Thomas Schöpping
You should have received a copy of the GNU Lesser General Public License
16 d6728c5b Thomas Schöpping
along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19 5e2f673b Marc Rothmann
/**
20 1d5bcc82 Thomas Schöpping
 * @file    alld_AT24C01B_v1.c
21 5e2f673b Marc Rothmann
 * @brief   EEPROM function implementations.
22
 *
23
 * @addtogroup lld_eeprom
24
 * @{
25
 */
26
27 1d5bcc82 Thomas Schöpping
#include <alld_AT24C01B.h>
28 d6728c5b Thomas Schöpping
29 1d5bcc82 Thomas Schöpping
#if (defined(AMIROLLD_CFG_AT24C01B) && (AMIROLLD_CFG_AT24C01B == 1)) || defined(__DOXYGEN__)
30 d6728c5b Thomas Schöpping
31
#include <string.h>
32
33 ef078306 Thomas Schöpping
/******************************************************************************/
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 d6728c5b Thomas Schöpping
/**
58
 * @brief Acknowledgement polling to check if a write operation finished.
59 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
60 d6728c5b Thomas Schöpping
 * @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 1d5bcc82 Thomas Schöpping
apalExitStatus_t at24c01b_lld_poll_ack(const AT24C01BDriver* const at24c01b, const apalTime_t timeout)
65 d6728c5b Thomas Schöpping
{
66 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
67 d6728c5b Thomas Schöpping
68
  uint8_t tmp = 0;
69 1d5bcc82 Thomas Schöpping
  return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), &tmp, 1, timeout);
70 d6728c5b Thomas Schöpping
}
71
72
/**
73
 * @brief Read the data from the eeprom starting at the current address.
74 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
75
 * @param[out]  data      The data read from the AT24C01B.
76 d6728c5b Thomas Schöpping
 * @param[in]   num       The amount of bytes to read.
77
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
78
 *
79
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
80
 */
81 1d5bcc82 Thomas Schöpping
apalExitStatus_t at24c01b_lld_read_current_address(const AT24C01BDriver* const at24c01b, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
82 d6728c5b Thomas Schöpping
{
83 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
84 d6728c5b Thomas Schöpping
  apalDbgAssert(data != NULL);
85
86 1d5bcc82 Thomas Schöpping
  return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), data, num, timeout);
87 d6728c5b Thomas Schöpping
}
88
89
/**
90
 * @brief Read the data from the eeprom starting at a given address.
91 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
92 d6728c5b Thomas Schöpping
 * @param[in]   addr      The address to read.
93 1d5bcc82 Thomas Schöpping
 * @param[out]  data      The data read from the AT24C01B.
94 d6728c5b Thomas Schöpping
 * @param[in]   num       The amount of bytes to read.
95
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
96
 *
97
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
98
 */
99 1d5bcc82 Thomas Schöpping
apalExitStatus_t at24c01b_lld_read(const AT24C01BDriver* const at24c01b, const uint8_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
100 d6728c5b Thomas Schöpping
{
101 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
102 d6728c5b Thomas Schöpping
  apalDbgAssert(data != NULL);
103 1d5bcc82 Thomas Schöpping
  apalDbgAssert(addr + num <= AT24C01B_LLD_SIZE_BYTES);
104 d6728c5b Thomas Schöpping
105 1d5bcc82 Thomas Schöpping
  apalExitStatus_t status = apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), &addr, 1, NULL, 0, timeout);
106 d6728c5b Thomas Schöpping
  if (status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) {
107 1d5bcc82 Thomas Schöpping
    return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), data, num, timeout);
108 d6728c5b Thomas Schöpping
  } else {
109
    return status;
110
  }
111
}
112
113
/**
114
 * @brief Write a byte to the eeprom.
115 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
116 d6728c5b Thomas Schöpping
 * @param[in]   addr      The address to start writing.
117
 * @param[out]  data      The data to be written to the AT24C01BN.
118
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
119
 *
120
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
121
 */
122 1d5bcc82 Thomas Schöpping
apalExitStatus_t at24c01b_lld_write_byte(const AT24C01BDriver* const at24c01b, const uint8_t addr, const uint8_t data, const apalTime_t timeout)
123 d6728c5b Thomas Schöpping
{
124 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
125 d6728c5b Thomas Schöpping
126
  const uint8_t writedata[2] = {addr, data};
127 1d5bcc82 Thomas Schöpping
  return apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), writedata, 2, NULL, 0, timeout);
128 d6728c5b Thomas Schöpping
}
129
130
/**
131
 * @brief Write a page to the eeprom.
132 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
133 d6728c5b Thomas Schöpping
 * @param[in]   addr      The address to start writing.
134
 * @param[in]   data      The data to be written to the AT24C01BN.
135
 * @param[in]   num       The amount of bytes to write.
136
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
137
 *
138
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
139
 */
140 1d5bcc82 Thomas Schöpping
apalExitStatus_t at24c01b_lld_write_page(const AT24C01BDriver* const at24c01b, const uint8_t addr, const uint8_t* const data, const uint8_t num, const apalTime_t timeout)
141 d6728c5b Thomas Schöpping
{
142 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
143 d6728c5b Thomas Schöpping
  apalDbgAssert(data != NULL);
144
145
  uint8_t writedata[num+1];
146
  writedata[0] = addr;
147
  memcpy(&writedata[1], data, num);
148 1d5bcc82 Thomas Schöpping
  return apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), writedata, num+1, NULL, 0, timeout);
149 d6728c5b Thomas Schöpping
}
150
151 1d5bcc82 Thomas Schöpping
#endif /* defined(AMIROLLD_CFG_AT24C01B) && (AMIROLLD_CFG_AT24C01B == 1) */
152 5e2f673b Marc Rothmann
153
/** @} */