MLX90640_I2C_Driver.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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 <stdio.h>
  18. #include "i2c.h"
  19. #include "MLX90640_I2C_Driver.h"
  20. void MLX90640_I2CInit()
  21. {
  22. MX_I2C1_Init();
  23. }
  24. int MLX90640_I2CRead(uint8_t slaveAddr, uint16_t startAddress, uint16_t nMemAddressRead, uint16_t *data)
  25. {
  26. uint8_t* bp = (uint8_t*) data;
  27. int ack = 0;
  28. int cnt = 0;
  29. ack = HAL_I2C_Mem_Read(&hi2c1, (slaveAddr<<1), startAddress, I2C_MEMADD_SIZE_16BIT, bp, nMemAddressRead*2, 500);
  30. if (ack != HAL_OK)
  31. {
  32. return -1;
  33. }
  34. for(cnt=0; cnt < nMemAddressRead*2; cnt+=2) {
  35. uint8_t tmpbytelsb = bp[cnt+1];
  36. bp[cnt+1] = bp[cnt];
  37. bp[cnt] = tmpbytelsb;
  38. }
  39. return 0;
  40. }
  41. int MLX90640_I2CWrite(uint8_t slaveAddr, uint16_t writeAddress, uint16_t data)
  42. {
  43. uint8_t sa;
  44. int ack = 0;
  45. uint8_t cmd[2];
  46. static uint16_t dataCheck;
  47. sa = (slaveAddr << 1);
  48. cmd[0] = data >> 8;
  49. cmd[1] = data & 0x00FF;
  50. ack = HAL_I2C_Mem_Write(&hi2c1, sa, writeAddress, I2C_MEMADD_SIZE_16BIT, cmd, sizeof(cmd), 500);
  51. if (ack != HAL_OK)
  52. {
  53. return -1;
  54. }
  55. MLX90640_I2CRead(slaveAddr,writeAddress,1, &dataCheck);
  56. if ( dataCheck != data)
  57. {
  58. return -2;
  59. }
  60. return 0;
  61. }