MLX90640_I2C_Driver.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 "luat_malloc.h"
  19. #include "MLX90640_I2C_Driver.h"
  20. extern uint8_t mlx90640_i2c_id;
  21. extern uint8_t mlx90640_i2c_speed;
  22. void MLX90640_I2CInit(void)
  23. {
  24. luat_i2c_setup(mlx90640_i2c_id, mlx90640_i2c_speed, NULL);
  25. }
  26. int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
  27. {
  28. int ret = 0;
  29. int cnt = 0;
  30. int i = 0;
  31. char* i2cData = NULL;
  32. uint16_t *p;
  33. p = data;
  34. char cmd[2] = {0,0};
  35. cmd[0] = startAddress >> 8;
  36. cmd[1] = startAddress & 0x00FF;
  37. i2cData = (char *)luat_heap_malloc(2*nMemAddressRead);
  38. ret = luat_i2c_transfer(mlx90640_i2c_id, slaveAddr, (uint8_t*)cmd, 2, (uint8_t*)i2cData, 2*nMemAddressRead);
  39. if (ret != 0){
  40. luat_heap_free(i2cData);
  41. return -1;
  42. }
  43. for(cnt=0; cnt < nMemAddressRead; cnt++)
  44. {
  45. i = cnt << 1;
  46. *p++ = (uint16_t)i2cData[i]*256 + (uint16_t)i2cData[i+1];
  47. }
  48. luat_heap_free(i2cData);
  49. return 0;
  50. }
  51. int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
  52. {
  53. int ret = 0;
  54. static uint16_t dataCheck;
  55. uint8_t cmd[4];
  56. cmd[0] = writeAddress >> 8;
  57. cmd[1] = writeAddress & 0x00FF;
  58. cmd[2] = data >> 8;
  59. cmd[3] = data & 0x00FF;
  60. ret = luat_i2c_send(mlx90640_i2c_id, slaveAddr, cmd, 4,0);
  61. if (ret != 0)return -1;
  62. ret = MLX90640_I2CRead(slaveAddr,writeAddress,1, &dataCheck);
  63. if (ret != 0)return -1;
  64. if ( dataCheck != data)
  65. {
  66. return -2;
  67. }
  68. return 0;
  69. }