Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (5.105 KB)

1
/*
2
AMiRo-LLD is a compilation of low-level hardware drivers for the Autonomous Mini Robot (AMiRo) platform.
3
Copyright (C) 2016..2018  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
#include <alld_at24c01bn-sh-b.h>
20

    
21
#if defined(AMIROLLD_CFG_USE_AT24C01BN) || defined(__DOXYGEN__)
22

    
23
#include <string.h>
24

    
25
/**
26
 * @brief Acknowledgement polling to check if a write operation finished.
27
 * @param[in]   at24c01bn The eeprom driver to use.
28
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
29
 *
30
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
31
 */
32
inline apalExitStatus_t
33
at24c01bn_lld_poll_ack(const AT24C01BNDriver* const at24c01bn, const apalTime_t timeout)
34
{
35
  apalDbgAssert(at24c01bn != NULL);
36

    
37
  uint8_t tmp = 0;
38
  return apalI2CMasterReceive(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), &tmp, 1, timeout);
39
}
40

    
41
/**
42
 * @brief Read the data from the eeprom starting at the current address.
43
 * @param[in]   at24c01bn The eeprom driver to use.
44
 * @param[out]  data      The data read from the AT24C01BN.
45
 * @param[in]   num       The amount of bytes to read.
46
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
47
 *
48
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
49
 */
50
inline apalExitStatus_t
51
at24c01bn_lld_read_current_address(const AT24C01BNDriver* const at24c01bn, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
52
{
53
  apalDbgAssert(at24c01bn != NULL);
54
  apalDbgAssert(data != NULL);
55

    
56
  return apalI2CMasterReceive(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), data, num, timeout);
57
}
58

    
59
/**
60
 * @brief Read the data from the eeprom starting at a given address.
61
 * @param[in]   at24c01bn The eeprom driver to use.
62
 * @param[in]   addr      The address to read.
63
 * @param[out]  data      The data read from the AT24C01BN.
64
 * @param[in]   num       The amount of bytes to read.
65
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
66
 *
67
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
68
 */
69
inline apalExitStatus_t
70
at24c01bn_lld_read(const AT24C01BNDriver* const at24c01bn, const uint8_t addr, uint8_t* const data, const uint8_t num, const apalTime_t timeout)
71
{
72
  apalDbgAssert(at24c01bn != NULL);
73
  apalDbgAssert(data != NULL);
74
  apalDbgAssert(addr + num <= AT24C01BN_LLD_SIZE_BYTES);
75

    
76
  apalExitStatus_t status = apalI2CMasterTransmit(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), &addr, 1, NULL, 0, timeout);
77
  if (status == APAL_STATUS_OK || status == APAL_STATUS_WARNING) {
78
    return apalI2CMasterReceive(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), data, num, timeout);
79
  } else {
80
    return status;
81
  }
82
}
83

    
84
/**
85
 * @brief Write a byte to the eeprom.
86
 * @param[in]   at24c01bn The eeprom driver to use.
87
 * @param[in]   addr      The address to start writing.
88
 * @param[out]  data      The data to be written to the AT24C01BN.
89
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
90
 *
91
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
92
 */
93
inline apalExitStatus_t
94
at24c01bn_lld_write_byte(const AT24C01BNDriver* const at24c01bn, const uint8_t addr, const uint8_t data, const apalTime_t timeout)
95
{
96
  apalDbgAssert(at24c01bn != NULL);
97

    
98
  const uint8_t writedata[2] = {addr, data};
99
  return apalI2CMasterTransmit(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), writedata, 2, NULL, 0, timeout);
100
}
101

    
102
/**
103
 * @brief Write a page to the eeprom.
104
 * @param[in]   at24c01bn The eeprom driver to use.
105
 * @param[in]   addr      The address to start writing.
106
 * @param[in]   data      The data to be written to the AT24C01BN.
107
 * @param[in]   num       The amount of bytes to write.
108
 * @param[in]   timeout   Timeout for the function to return (in microseconds)
109
 *
110
 * @return  The return status indicates whether the function call was succesfull or a timeout occurred.
111
 */
112
inline apalExitStatus_t
113
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)
114
{
115
  apalDbgAssert(at24c01bn != NULL);
116
  apalDbgAssert(data != NULL);
117

    
118
  uint8_t writedata[num+1];
119
  writedata[0] = addr;
120
  memcpy(&writedata[1], data, num);
121
  return apalI2CMasterTransmit(at24c01bn->i2cd, (AT24C01BN_LLD_I2C_ADDR_FIXED | at24c01bn->addr), writedata, num+1, NULL, 0, timeout);
122
}
123

    
124
#endif /* defined(AMIROLLD_CFG_USE_AT24C01BN) */