Statistics
| Branch: | Tag: | Revision:

amiro-lld / drivers / AT24C01B / v1 / alld_AT24C01B.c @ 86e60b45

History | View | Annotate | Download (6.47 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 9466e34d Thomas Schöpping
 * @file    alld_AT24C01B.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
#include <string.h>
29
30 ef078306 Thomas Schöpping
/******************************************************************************/
31
/* LOCAL DEFINITIONS                                                          */
32
/******************************************************************************/
33
34
/******************************************************************************/
35
/* EXPORTED VARIABLES                                                         */
36
/******************************************************************************/
37
38
/******************************************************************************/
39
/* LOCAL TYPES                                                                */
40
/******************************************************************************/
41
42
/******************************************************************************/
43
/* LOCAL VARIABLES                                                            */
44
/******************************************************************************/
45
46
/******************************************************************************/
47
/* LOCAL FUNCTIONS                                                            */
48
/******************************************************************************/
49
50
/******************************************************************************/
51
/* EXPORTED FUNCTIONS                                                         */
52
/******************************************************************************/
53
54 d6728c5b Thomas Schöpping
/**
55
 * @brief Acknowledgement polling to check if a write operation finished.
56 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
57 d6728c5b Thomas Schöpping
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
58
 *
59
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
60
 */
61 1d5bcc82 Thomas Schöpping
apalExitStatus_t at24c01b_lld_poll_ack(const AT24C01BDriver* const at24c01b, const apalTime_t timeout)
62 d6728c5b Thomas Schöpping
{
63 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
64 d6728c5b Thomas Schöpping
65
  uint8_t tmp = 0;
66 1d5bcc82 Thomas Schöpping
  return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), &tmp, 1, timeout);
67 d6728c5b Thomas Schöpping
}
68
69
/**
70
 * @brief Read the data from the eeprom starting at the current address.
71 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
72
 * @param[out]  data      The data read from the AT24C01B.
73 d6728c5b Thomas Schöpping
 * @param[in]   num       The amount of bytes to read.
74
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
75
 *
76
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
77
 */
78 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)
79 d6728c5b Thomas Schöpping
{
80 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
81 d6728c5b Thomas Schöpping
  apalDbgAssert(data != NULL);
82
83 1d5bcc82 Thomas Schöpping
  return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), data, num, timeout);
84 d6728c5b Thomas Schöpping
}
85
86
/**
87
 * @brief Read the data from the eeprom starting at a given address.
88 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
89 d6728c5b Thomas Schöpping
 * @param[in]   addr      The address to read.
90 1d5bcc82 Thomas Schöpping
 * @param[out]  data      The data read from the AT24C01B.
91 d6728c5b Thomas Schöpping
 * @param[in]   num       The amount of bytes to read.
92
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
93
 *
94
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
95
 */
96 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)
97 d6728c5b Thomas Schöpping
{
98 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
99 d6728c5b Thomas Schöpping
  apalDbgAssert(data != NULL);
100 1d5bcc82 Thomas Schöpping
  apalDbgAssert(addr + num <= AT24C01B_LLD_SIZE_BYTES);
101 d6728c5b Thomas Schöpping
102 1d5bcc82 Thomas Schöpping
  apalExitStatus_t status = apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), &addr, 1, NULL, 0, timeout);
103 d6728c5b Thomas Schöpping
  if (status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) {
104 1d5bcc82 Thomas Schöpping
    return apalI2CMasterReceive(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), data, num, timeout);
105 d6728c5b Thomas Schöpping
  } else {
106
    return status;
107
  }
108
}
109
110
/**
111
 * @brief Write a byte to the eeprom.
112 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
113 d6728c5b Thomas Schöpping
 * @param[in]   addr      The address to start writing.
114
 * @param[out]  data      The data to be written to the AT24C01BN.
115
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
116
 *
117
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
118
 */
119 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)
120 d6728c5b Thomas Schöpping
{
121 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
122 d6728c5b Thomas Schöpping
123
  const uint8_t writedata[2] = {addr, data};
124 1d5bcc82 Thomas Schöpping
  return apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), writedata, 2, NULL, 0, timeout);
125 d6728c5b Thomas Schöpping
}
126
127
/**
128
 * @brief Write a page to the eeprom.
129 1d5bcc82 Thomas Schöpping
 * @param[in]   at24c01b  The eeprom driver to use.
130 d6728c5b Thomas Schöpping
 * @param[in]   addr      The address to start writing.
131
 * @param[in]   data      The data to be written to the AT24C01BN.
132
 * @param[in]   num       The amount of bytes to write.
133
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
134
 *
135
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
136
 */
137 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)
138 d6728c5b Thomas Schöpping
{
139 1d5bcc82 Thomas Schöpping
  apalDbgAssert(at24c01b != NULL);
140 d6728c5b Thomas Schöpping
  apalDbgAssert(data != NULL);
141
142
  uint8_t writedata[num+1];
143
  writedata[0] = addr;
144
  memcpy(&writedata[1], data, num);
145 1d5bcc82 Thomas Schöpping
  return apalI2CMasterTransmit(at24c01b->i2cd, (AT24C01B_LLD_I2C_ADDR_FIXED | at24c01b->addr), writedata, num+1, NULL, 0, timeout);
146 d6728c5b Thomas Schöpping
}
147
148 5e2f673b Marc Rothmann
/** @} */