gps_cc1165w.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2022 Actions Technology Co., Ltd
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <soc.h>
  7. #include <board.h>
  8. #include <device.h>
  9. #include <drivers/uart.h>
  10. #include <logging/log.h>
  11. #include <gps/gps.h>
  12. #include <drivers/gpio.h>
  13. LOG_MODULE_REGISTER(gps_1165, LOG_LEVEL_INF);
  14. #define CONFIG_GPS_DEV_NAME "gps"
  15. #define GPS_UERT_DEV_NAME CONFIG_UART_2_NAME
  16. #define UART_FIFO_MAX 16
  17. #define GPS_DATA_BUFFER_SIZE 100
  18. struct cc1165w_data {
  19. const struct device *uart_gpio_dev;
  20. uint8_t gps_data[2][GPS_DATA_BUFFER_SIZE];
  21. uint8_t gpa_data_cur;
  22. uint8_t gpa_data_prev;
  23. gps_notify_t notify;
  24. struct k_work gps_work;
  25. };
  26. static struct cc1165w_data cc1165w_data;
  27. static void uart_fifo_callback(const struct device *dev, void *user_data)
  28. {
  29. uint8_t rx_buff[UART_FIFO_MAX];
  30. int read_size;
  31. static uint8_t cnt;
  32. uart_irq_update(dev);
  33. if(uart_irq_rx_ready(dev))
  34. {
  35. read_size = uart_fifo_read(dev,rx_buff,UART_FIFO_MAX);
  36. uart_fifo_fill(dev,rx_buff,read_size);
  37. for(int i = 0; i < read_size; i++)
  38. {
  39. switch (rx_buff[i])
  40. {
  41. case '$':
  42. if(cc1165w_data.gpa_data_cur)
  43. cc1165w_data.gpa_data_cur = 0;
  44. else
  45. cc1165w_data.gpa_data_cur = 1;
  46. memset(cc1165w_data.gps_data[cc1165w_data.gpa_data_cur], 0, sizeof(cc1165w_data.gps_data[cc1165w_data.gpa_data_cur]));
  47. cnt = 0;
  48. cc1165w_data.gps_data[cc1165w_data.gpa_data_cur][cnt] = rx_buff[i];
  49. break;
  50. case '\n':
  51. cnt ++;
  52. cc1165w_data.gps_data[cc1165w_data.gpa_data_cur][cnt] = rx_buff[i];
  53. cc1165w_data.gpa_data_prev = cc1165w_data.gpa_data_cur;
  54. k_work_submit(&cc1165w_data.gps_work);
  55. break;
  56. default:
  57. cnt ++;
  58. cc1165w_data.gps_data[cc1165w_data.gpa_data_cur][cnt] = rx_buff[i];
  59. /* protection data cannot exceed boundary */
  60. if(cnt >= GPS_DATA_BUFFER_SIZE)
  61. cnt --;
  62. break;
  63. }
  64. }
  65. }
  66. }
  67. static void cc1165w_enable(const struct device *dev)
  68. {
  69. struct cc1165w_data *data = (struct cc1165w_data *)dev->data;
  70. printk("cc1165w_enable %s\n",data->uart_gpio_dev->name);
  71. gps_wake_up_pin_ctl(true);
  72. uart_irq_rx_enable(data->uart_gpio_dev);
  73. }
  74. static void cc1165w_disable(const struct device *dev)
  75. {
  76. printk("cc1165 disable \n");
  77. struct cc1165w_data *data = (struct cc1165w_data *)dev->data;
  78. gps_wake_up_pin_ctl(false);
  79. uart_irq_rx_disable(data->uart_gpio_dev);
  80. }
  81. static void cc1165w_register_notify(const struct device *dev, gps_notify_t notify)
  82. {
  83. struct cc1165w_data *data = (struct cc1165w_data *)dev->data;
  84. data->notify = notify;
  85. }
  86. static void cc1165w_unregister_notify(const struct device *dev, gps_notify_t notify)
  87. {
  88. struct cc1165w_data *data = (struct cc1165w_data *)dev->data;
  89. data->notify = NULL;
  90. }
  91. static const struct gps_dev_driver_api cc1165w_api = {
  92. .enable = cc1165w_enable,
  93. .disable = cc1165w_disable,
  94. .inquiry = NULL,
  95. .register_notify = cc1165w_register_notify,
  96. .unregister_notify = cc1165w_unregister_notify,
  97. };
  98. //发送消息给到gps service
  99. static void _gps_work_handler(struct k_work *work)
  100. {
  101. struct gps_value val = {0};
  102. // LOG_INF("%s",cc1165w_data.gps_data[cc1165w_data.gpa_data_prev]);
  103. if (cc1165w_data.notify)
  104. {
  105. val.gps_nmea_data = cc1165w_data.gps_data[cc1165w_data.gpa_data_prev];
  106. cc1165w_data.notify(NULL, &val);
  107. }
  108. }
  109. static int cc1165w_init(const struct device *dev)
  110. {
  111. struct cc1165w_data *data = (struct cc1165w_data *)dev->data;
  112. LOG_INF("cc1165w_init");
  113. gps_power_pin_ctl(true);
  114. gps_power095_pin_ctl(true);
  115. // gps_reset_pin_ctl();
  116. data->uart_gpio_dev = device_get_binding(GPS_UERT_DEV_NAME);
  117. if (data->uart_gpio_dev == NULL) {
  118. LOG_ERR("Couldn't find gps uart");
  119. return -ENODEV;
  120. }
  121. k_work_init(&data->gps_work, _gps_work_handler);
  122. printk("cc1165w_init*\n");
  123. uart_irq_callback_set(data->uart_gpio_dev, uart_fifo_callback);
  124. data->gpa_data_cur = 0;
  125. return 0;
  126. }
  127. DEVICE_DEFINE(cc1165w, CONFIG_GPS_DEV_NAME, &cc1165w_init,
  128. NULL, &cc1165w_data, NULL, POST_KERNEL,
  129. 60, &cc1165w_api);