vl6180x_i2c.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*******************************************************************************
  2. Copyright ?2014, STMicroelectronics International N.V.
  3. All rights reserved.
  4. Redistribution and use in source and binary forms, with or without
  5. modification, are permitted provided that the following conditions are met:
  6. * Redistributions of source code must retain the above copyright
  7. notice, this list of conditions and the following disclaimer.
  8. * Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. * Neither the name of STMicroelectronics nor the
  12. names of its contributors may be used to endorse or promote products
  13. derived from this software without specific prior written permission.
  14. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND
  17. NON-INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS ARE DISCLAIMED.
  18. IN NO EVENT SHALL STMICROELECTRONICS INTERNATIONAL N.V. BE LIABLE FOR ANY
  19. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. ********************************************************************************/
  26. /*
  27. * $Date: 2015-01-08 05:30:24 -0800 (Thu, 08 Jan 2015) $
  28. * $Revision: 2039 $
  29. */
  30. /**
  31. * @file vl6180x_i2c.c
  32. *
  33. * Copyright (C) 2014 ST MicroElectronics
  34. *
  35. * provide variable word size byte/Word/dword VL6180x register access via i2c
  36. *
  37. */
  38. #include "vl6180x_i2c.h"
  39. #if 1
  40. #ifndef I2C_BUFFER_CONFIG
  41. #error "I2C_BUFFER_CONFIG not defined"
  42. /* TODO you must define value for I2C_BUFFER_CONFIG in configuration or platform h */
  43. #endif
  44. extern int s4VL6180_WriteRegByte(u16 addr, u8 data);
  45. extern int s4VL6180_ReadRegByte(u16 addr, u8 *data);
  46. extern int s4VL6180_WriteRegWord(u16 addr, u16 data);
  47. extern int s4VL6180_ReadRegWord(u16 addr, u16 *data);
  48. extern int s4VL6180_WriteRegDWord(u16 addr, u32 data);
  49. extern int s4VL6180_ReadRegDWord(u16 addr, u32 *data);
  50. #if I2C_BUFFER_CONFIG == 0
  51. /* GLOBAL config buffer */
  52. uint8_t i2c_global_buffer[VL6180x_MAX_I2C_XFER_SIZE];
  53. #define DECL_I2C_BUFFER
  54. #define VL6180x_GetI2cBuffer(dev, n_byte) i2c_global_buffer
  55. #elif I2C_BUFFER_CONFIG == 1
  56. /* ON STACK */
  57. #define DECL_I2C_BUFFER uint8_t LocBuffer[VL6180x_MAX_I2C_XFER_SIZE];
  58. #define VL6180x_GetI2cBuffer(dev, n_byte) LocBuffer
  59. #elif I2C_BUFFER_CONFIG == 2
  60. /* user define buffer type declare DECL_I2C_BUFFER as access via VL6180x_GetI2cBuffer */
  61. #define DECL_I2C_BUFFER
  62. #else
  63. #error "invalid I2C_BUFFER_CONFIG "
  64. #endif
  65. int VL6180x_WrByte(VL6180xDev_t dev, uint16_t index, uint8_t data)
  66. {
  67. int status;
  68. status = s4VL6180_WriteRegByte(index, data);
  69. return status;
  70. }
  71. int VL6180x_WrWord(VL6180xDev_t dev, uint16_t index, uint16_t data)
  72. {
  73. int status;
  74. status = s4VL6180_WriteRegWord(index, data);
  75. return status;
  76. }
  77. int VL6180x_WrDWord(VL6180xDev_t dev, uint16_t index, uint32_t data)
  78. {
  79. int status;
  80. status = s4VL6180_WriteRegDWord(index, data);
  81. return status;
  82. }
  83. int VL6180x_RdByte(VL6180xDev_t dev, uint16_t index, uint8_t *data)
  84. {
  85. int status;
  86. status = s4VL6180_ReadRegByte(index, data);
  87. return status;
  88. }
  89. int VL6180x_RdWord(VL6180xDev_t dev, uint16_t index, uint16_t *data)
  90. {
  91. int status;
  92. status = s4VL6180_ReadRegWord(index, data);
  93. return status;
  94. }
  95. int VL6180x_RdDWord(VL6180xDev_t dev, uint16_t index, uint32_t *data)
  96. {
  97. int status;
  98. status = s4VL6180_ReadRegDWord(index, data);
  99. return status;
  100. }
  101. int VL6180x_UpdateByte(VL6180xDev_t dev, uint16_t index, uint8_t AndData, uint8_t OrData)
  102. {
  103. int data;
  104. int status;
  105. VL6180x_RdByte(dev, index, (uint8_t *)&data );
  106. data=( data&AndData)|OrData;
  107. status = VL6180x_WrByte(dev, index, data);
  108. return status;
  109. }
  110. #endif