MLX90640_I2C_Driver.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /**
  2. * @copyright (C) 2017 Melexis N.V.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. #include "luat_i2c.h"
  18. #include "MLX90640_I2C_Driver.h"
  19. extern uint8_t mlx90640_i2c_id;
  20. extern uint8_t mlx90640_i2c_speed;
  21. void MLX90640_I2CInit()
  22. {
  23. luat_i2c_setup(mlx90640_i2c_id, mlx90640_i2c_speed, NULL);
  24. }
  25. int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
  26. {
  27. int ret = 0;
  28. int cnt = 0;
  29. int i = 0;
  30. char i2cData[1664] = {0}; // TODO 改成malloc
  31. uint16_t *p;
  32. p = data;
  33. char cmd[2] = {0,0};
  34. cmd[0] = startAddress >> 8;
  35. cmd[1] = startAddress & 0x00FF;
  36. ret = luat_i2c_send(mlx90640_i2c_id, slaveAddr, cmd, 2);
  37. if (ret != 0)return -1;
  38. ret = luat_i2c_recv(mlx90640_i2c_id, slaveAddr, i2cData, 2*nMemAddressRead);
  39. if (ret != 0)return -1;
  40. for(cnt=0; cnt < nMemAddressRead; cnt++)
  41. {
  42. i = cnt << 1;
  43. *p++ = (uint16_t)i2cData[i]*256 + (uint16_t)i2cData[i+1];
  44. }
  45. return 0;
  46. }
  47. int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
  48. {
  49. int ret = 0;
  50. static uint16_t dataCheck;
  51. uint8_t cmd[4];
  52. cmd[0] = writeAddress >> 8;
  53. cmd[1] = writeAddress & 0x00FF;
  54. cmd[2] = data >> 8;
  55. cmd[3] = data & 0x00FF;
  56. ret = luat_i2c_send(mlx90640_i2c_id, slaveAddr, cmd, 4);
  57. if (ret != 0)return -1;
  58. ret = MLX90640_I2CRead(slaveAddr,writeAddress,1, &dataCheck);
  59. if (ret != 0)return -1;
  60. if ( dataCheck != data)
  61. {
  62. return -2;
  63. }
  64. return 0;
  65. }