Revision 6ef38c23

View differences:

include/P9221R/v1/alld_P9221R_v1.h
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_P9221R_v1.h
21
 * @brief   Power Monitor macros and structures.
22
 *
23
 * @addtogroup lld_power
24
 * @{
25
 */
26

  
27
#ifndef AMIROLLD_P9221R_V1_H
28
#define AMIROLLD_P9221R_V1_H
29

  
30
#include <amiro-lld.h>
31

  
32
/******************************************************************************/
33
/* CONSTANTS                                                                  */
34
/******************************************************************************/
35

  
36
/******************************************************************************/
37
/* SETTINGS                                                                   */
38
/******************************************************************************/
39

  
40
/******************************************************************************/
41
/* CHECKS                                                                     */
42
/******************************************************************************/
43

  
44
/******************************************************************************/
45
/* DATA STRUCTURES AND TYPES                                                  */
46
/******************************************************************************/
47
/**
48
 * @brief Registers.
49
 */
50
typedef enum {
51
  // Writeable Registers
52
  P9221R_LLD_REGISTER_BATTERY_STATUS_SENT       = 0x3A,
53
  P9221R_LLD_REGISTER_END_POWER_TRANSFER        = 0x3B,
54
  // Read-Only Registers
55
  P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_LSB        = 0x3C,
56
  P9221R_LLD_REGISTER_OUTPUT_VOLTAGE_MSB        = 0x3D,
57
  P9221R_LLD_REGISTER_IOUT_CURRENT_LSB          = 0x44,
58
  P9221R_LLD_REGISTER_IOUT_CURRENT_MSB          = 0x45,
59
  P9221R_LLD_REGISTER_OPERATING_FREQUENCY_MSB   = 0x48,
60
  P9221R_LLD_REGISTER_OPERATING_FREQUENCY_LSB   = 0x49,
61
  P9221R_LLD_REGISTER_ALIGNMENT_X               = 0x4B,
62
  P9221R_LLD_REGISTER_ALIGNMENT_Y               = 0x4C,
63
} p9221r_lld_register_t;
64

  
65
/**
66
 * @brief I2C address masks.
67
 */
68
enum {
69
  P9221R_LLD_I2C_ADDR_FIXED   = 0x0061u,
70
};
71

  
72
typedef struct {
73
  apalI2CDriver_t* i2cd;
74
  apalI2Caddr_t addr;
75
  uint16_t current_lsb_uA;
76
  P9221R_lld_cfg_t *config;
77
} P9221RDriver;
78

  
79

  
80
/******************************************************************************/
81
/* MACROS                                                                     */
82
/******************************************************************************/
83

  
84
/******************************************************************************/
85
/* EXTERN DECLARATIONS                                                        */
86
/******************************************************************************/
87

  
88
#ifdef __cplusplus
89
extern "C" {
90
#endif
91
  apalExitStatus_t p9221r_lld_read_register(const P9221RDriver* const p9221r, const p9221r_lld_register_t addr, uint16_t* const data, const uint8_t num, const apalTime_t timeout);
92
  apalExitStatus_t p9221r_lld_write_register(const P9221RDriver* const p9221r, const p9221r_lld_register_t addr, const uint16_t* const data, const uint8_t num, const apalTime_t timeout);
93
#ifdef __cplusplus
94
}
95
#endif
96

  
97
/******************************************************************************/
98
/* INLINE FUNCTIONS                                                           */
99
/******************************************************************************/
100

  
source/P9221R/v1/alld_P9221R_v1.c
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_P9221R_v1.c
21
 *
22
 * @brief   Power Monitor function implementations
23
 *
24
 * @addtogroup lld_power
25
 * @{
26
 */
27

  
28
#include <alld_P9221R_v1.h>
29

  
30

  
31
/******************************************************************************/
32
/* LOCAL DEFINITIONS                                                          */
33
/******************************************************************************/
34

  
35
/******************************************************************************/
36
/* EXPORTED VARIABLES                                                         */
37
/******************************************************************************/
38

  
39
/******************************************************************************/
40
/* LOCAL TYPES                                                                */
41
/******************************************************************************/
42

  
43
/******************************************************************************/
44
/* LOCAL VARIABLES                                                            */
45
/******************************************************************************/
46

  
47
/******************************************************************************/
48
/* LOCAL FUNCTIONS                                                            */
49
/******************************************************************************/
50

  
51
/******************************************************************************/
52
/* EXPORTED FUNCTIONS                                                         */
53
/******************************************************************************/
54

  
55

  
56
/**
57
 * @brief Read the value of one or more of the registers.
58
 * @param[in]   i2cd        i2c driver
59
 * @param[in]   PRd         p9221r driver
60
 * @param[in]   addr        register address
61
 * @param[out]  data        register content
62
 * @param[in]   num         number of subsequent registers to read
63
 * @param[in]   timeout     timeout
64
 * @return                  An indicator whether the call was successfull
65
 */
66
apalExitStatus_t p9221r_lld_read_register(const P9221RDriver* const p9221r, const p9221r_lld_register_t addr, uint16_t* const data, const uint8_t num, const apalTime_t timeout)
67
{
68
  apalDbgAssert(p9221r != NULL);
69
  apalDbgAssert(p9221r->i2cd != NULL);
70
  apalDbgAssert(data != NULL);
71

  
72
  uint8_t buffer[num*2];
73
  apalExitStatus_t status = apalI2CMasterTransmit(p9221r->i2cd, (P9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), (uint8_t*)&addr, 1, buffer, 2*num, timeout);
74
  for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) {
75
    data[dataIdx] = (buffer[2*dataIdx] << 8) | buffer[2*dataIdx+1];
76
  }
77
  return status;
78
}
79

  
80
/**
81
 * @brief Write the value of one or more of the registers.
82
 * @param[in]   i2cd        i2c driver
83
 * @param[in]   PRd         p9221r driver
84
 * @param[in]   addr        register address
85
 * @param[in]   data        data to write
86
 * @param[in]   num         number of subsequent registers to read
87
 * @param[in]   timeout     timeout
88
 * @return                  An indicator whether the call was successfull
89
 */
90

  
91
apalExitStatus_t p9221r_lld_write_register(const R9221RDriver* const p9221r, const p9221r_lld_register_t addr, const uint16_t* const data, const uint8_t num, const apalTime_t timeout)
92
{
93
  apalDbgAssert(p9221r != NULL);
94
  apalDbgAssert(p9221r->i2cd != NULL);
95
  apalDbgAssert(data != NULL);
96

  
97
  uint8_t buffer[1+2*num];
98
  buffer[0] = addr;
99
  for (uint8_t dataIdx = 0; dataIdx < num; dataIdx++) {
100
    buffer[dataIdx*2+1] = data[dataIdx] >> 8;
101
    buffer[dataIdx*2+2] = data[dataIdx] & (0x00FFu);
102
  }
103
  return apalI2CMasterTransmit(p9221r->i2cd, (R9221R_LLD_I2C_ADDR_FIXED | p9221r->addr), buffer, 1+2*num, NULL, 0, timeout);
104
}
105

  
106

  
107

  
108

  
109

  
110

  
111

  
112

  
113

  
114

  
115

  
116

  
117

  
118

  
119

  
120

  
121

  
122

  
123

  
124

  
125

  
126

  
127

  
128

  
129

  
130

  
131

  
132

  
133

  
134

  
135

  

Also available in: Unified diff