b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 1 | /* |
| 2 | * gnss_asr5311.c |
| 3 | * |
| 4 | * ASR 5311 gnss support source. |
| 5 | * |
| 6 | */ |
| 7 | /****************************************************************************** |
| 8 | |
| 9 | EDIT HISTORY FOR FILE |
| 10 | |
| 11 | WHEN WHO WHAT,WHERE,WHY |
| 12 | -------- -------- ------------------------------------------------------- |
| 13 | 2024/6/19 LiuBin Initial version |
| 14 | |
| 15 | ******************************************************************************/ |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 21 | #include <pthread.h> |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 22 | |
| 23 | #include "mbtk_log.h" |
| 24 | #include "mbtk_type.h" |
| 25 | #include "mbtk_gpio.h" |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 26 | #include "mbtk_utils.h" |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 27 | #include "gnss_utils.h" |
| 28 | #include "gnss_asr5311.h" |
| 29 | |
| 30 | #define UART_BITRATE_NMEA_DEF_FW 115200 // Default bitrate. |
| 31 | #define GNSS_SET_TIMEOUT 3000 // 3s |
| 32 | |
b.liu | b8c3f62 | 2024-07-01 16:53:13 +0800 | [diff] [blame] | 33 | static char config_msg_pm5[] = "PMTEST,5"; |
| 34 | static char config_msg_pm4[] = "PMTEST,4"; |
| 35 | static char config_msg_boot[] = "START,1"; |
| 36 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 37 | static pthread_cond_t read_cond; |
| 38 | static pthread_mutex_t read_mutex; |
| 39 | static bool setting_waitting = FALSE; |
| 40 | static bool setting_busy = FALSE; |
| 41 | static void *gnss_set_rsp_ptr = NULL; |
| 42 | static gnss_err_enum gnss_set_result = GNSS_ERR_OK; |
| 43 | |
| 44 | int gnss_write(int fd, const void *data, int data_len); |
| 45 | |
b.liu | b8c3f62 | 2024-07-01 16:53:13 +0800 | [diff] [blame] | 46 | static int gnss_send_cmd(int fd, const char *cmd, int cmd_len) |
| 47 | { |
| 48 | unsigned char check_sum = 0; |
| 49 | unsigned int index; |
| 50 | int len = 0; |
| 51 | char cmd_buff[64] = {0}; |
| 52 | |
| 53 | for(index = 0; index < strlen( (char *)cmd); index++) |
| 54 | check_sum ^= cmd[index]; |
| 55 | |
| 56 | sprintf((char *)cmd_buff, "$%s*%02x\r\n", cmd, check_sum); |
| 57 | len = strlen((char *)cmd_buff)+1; |
| 58 | return gnss_write(fd, cmd_buff, len); |
| 59 | } |
| 60 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 61 | static void gnss_set_timer_cb(int signo) |
| 62 | { |
| 63 | if(setting_busy) { |
| 64 | pthread_mutex_lock(&read_mutex); |
| 65 | pthread_cond_signal(&read_cond); |
| 66 | pthread_mutex_unlock(&read_mutex); |
| 67 | gnss_set_result = GNSS_ERR_TIMEOUT; |
| 68 | } |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | static void gnss_hal_gpioctrl(char* str) |
| 73 | { |
| 74 | int fd; |
| 75 | fd = open("/sys/devices/platform/asr-gps/ctrl", O_WRONLY); |
| 76 | |
| 77 | if (fd >= 0) { |
| 78 | write(fd, str, strlen(str)); |
| 79 | close(fd); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | static int gnss_module_wait_fw_download_done() |
| 84 | { |
| 85 | int fd; |
| 86 | char buf[64]; |
| 87 | int len; |
| 88 | int ret = -1; |
| 89 | |
| 90 | fd = open("/sys/devices/platform/asr-gps/status", O_RDONLY); |
| 91 | if (fd >= 0) { |
| 92 | sleep(1); |
| 93 | len = read(fd, buf, 64); |
| 94 | LOGD("FW DONE %s, %d", buf, len); |
| 95 | if(!memcmp(buf, "status: on", 10)) { |
| 96 | /* FW Downloading is OK. */ |
| 97 | ret = 0; |
| 98 | } |
| 99 | |
| 100 | close(fd); |
| 101 | } |
| 102 | |
b.liu | b8c3f62 | 2024-07-01 16:53:13 +0800 | [diff] [blame] | 103 | if(!ret) |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 104 | gnss_hal_gpioctrl("off"); |
| 105 | |
| 106 | return ret; |
| 107 | } |
| 108 | |
| 109 | |
| 110 | int gnss_5311_dev_open() |
| 111 | { |
| 112 | return 0; |
| 113 | } |
| 114 | |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 115 | int gnss_5311_dev_close(int fd) |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 116 | { |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 117 | gnss_send_cmd(fd, config_msg_pm5, strlen(config_msg_pm5)); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int gnss_5311_open(const char *dev) |
| 122 | { |
| 123 | pthread_mutex_init(&read_mutex, NULL); |
| 124 | pthread_cond_init(&read_cond, NULL); |
| 125 | return gnss_port_open(dev, O_RDWR | O_NONBLOCK | O_NOCTTY, UART_BITRATE_NMEA_DEF_FW, TRUE); |
| 126 | } |
| 127 | |
| 128 | int gnss_5311_close(int fd) |
| 129 | { |
| 130 | pthread_mutex_destroy(&read_mutex); |
| 131 | pthread_cond_destroy(&read_cond); |
b.liu | b8c3f62 | 2024-07-01 16:53:13 +0800 | [diff] [blame] | 132 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 133 | return gnss_port_close(fd); |
| 134 | } |
| 135 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 136 | int gnss_5311_fw_dl(int fd, const char *fw_name, const char *dev) |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 137 | { |
| 138 | char cmd[256] = {0}; |
| 139 | if(memcmp(dev, "/dev/", 5) == 0) { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 140 | snprintf(cmd, sizeof(cmd), "nice -n -10 aboot-tiny -B 2000000 -s %s -F /etc/jacana_fw.bin -P /etc/jacana_pvt.bin", dev + 5); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 141 | } else { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 142 | snprintf(cmd, sizeof(cmd), "nice -n -10 aboot-tiny -B 2000000 -s %s -F /etc/jacana_fw.bin -P /etc/jacana_pvt.bin", dev); |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 143 | } |
| 144 | system(cmd); |
| 145 | |
| 146 | return gnss_module_wait_fw_download_done(); |
| 147 | } |
| 148 | |
| 149 | void gnss_5311_set_cb(const void *data, int data_len) |
| 150 | { |
| 151 | const char *buff = (const char*)data; |
| 152 | if(setting_busy) { // Has setting cmd process. |
| 153 | |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | gnss_err_enum gnss_5311_set(int fd, const char *cmd, void *cmd_rsp, int cmd_rsp_len) |
| 158 | { |
| 159 | if(setting_busy) { |
| 160 | return GNSS_ERR_SET_BUSY; |
| 161 | } else { |
| 162 | bool should_wait_rsp = TRUE; |
| 163 | setting_busy = TRUE; |
| 164 | gnss_set_rsp_ptr = cmd_rsp; |
| 165 | gnss_set_result = GNSS_ERR_OK; |
| 166 | |
| 167 | mbtk_timer_set(gnss_set_timer_cb, GNSS_SET_TIMEOUT); |
| 168 | |
| 169 | if(memcmp(cmd, "$RESET", 6) == 0) { // $RESET,<mode> |
| 170 | gnss_reset_type_enum mode = (gnss_reset_type_enum)atoi(cmd + 7); |
| 171 | if(mode == GNSS_RESET_TYPE_HOT) { |
| 172 | |
| 173 | } else if(mode == GNSS_RESET_TYPE_WARM) { |
| 174 | |
| 175 | } else if(mode == GNSS_RESET_TYPE_COLD) { |
| 176 | |
| 177 | } else { |
| 178 | gnss_set_result = GNSS_ERR_ARG; |
| 179 | goto set_fail; |
| 180 | } |
| 181 | if(gnss_set_result != GNSS_ERR_OK) { |
| 182 | goto set_fail; |
| 183 | } |
| 184 | should_wait_rsp = FALSE; |
| 185 | } else if(memcmp(cmd, "$SYSCFG", 7) == 0) { // $SYSCFG,<mode> |
| 186 | uint32 mode = 0; |
| 187 | mode = (uint32)atoi(cmd + 8); |
| 188 | uint32 new_mode = 0; |
| 189 | if(((GNSS_SET_SYSCFG_GPS | GNSS_SET_SYSCFG_BDS | GNSS_SET_SYSCFG_GLO | GNSS_SET_SYSCFG_GAL) & mode) != mode) { |
| 190 | gnss_set_result = GNSS_ERR_ARG; |
| 191 | goto set_fail; |
| 192 | } |
| 193 | |
| 194 | if(mode & GNSS_SET_SYSCFG_GPS) { // GPS |
| 195 | new_mode |= 0x00000001; |
| 196 | } |
| 197 | if(mode & GNSS_SET_SYSCFG_BDS) { // BDS |
| 198 | new_mode |= 0x00000002; |
| 199 | } |
| 200 | if(mode & GNSS_SET_SYSCFG_GLO) { // GLO |
| 201 | new_mode |= 0x00000004; |
| 202 | } |
| 203 | if(mode & GNSS_SET_SYSCFG_GAL) { // GAL |
| 204 | new_mode |= 0x00000010; |
| 205 | } |
| 206 | |
| 207 | |
| 208 | if(gnss_set_result != GNSS_ERR_OK) { |
| 209 | goto set_fail; |
| 210 | } |
| 211 | should_wait_rsp = TRUE; |
| 212 | } else if(memcmp(cmd, "$MSGCFG", 7) == 0) { // $MSGCFG,<mode>,<rate> |
| 213 | uint32 mode; |
| 214 | int rate; |
| 215 | if(2 == sscanf(cmd, "$MSGCFG,%d,%d", &mode, &rate)) { |
| 216 | int time = rate / 1000; // s |
| 217 | if(time < 0) { |
| 218 | gnss_set_result = GNSS_ERR_ARG; |
| 219 | goto set_fail; |
| 220 | } |
| 221 | |
| 222 | if(((GNSS_SET_MSGCFG_RMC | GNSS_SET_MSGCFG_VTG | GNSS_SET_MSGCFG_GGA | GNSS_SET_MSGCFG_GSA |
| 223 | | GNSS_SET_MSGCFG_GRS | GNSS_SET_MSGCFG_GSV | GNSS_SET_MSGCFG_GLL | GNSS_SET_MSGCFG_ZDA |
| 224 | | GNSS_SET_MSGCFG_GST | GNSS_SET_MSGCFG_TXT) & mode) != mode) { |
| 225 | gnss_set_result = GNSS_ERR_ARG; |
| 226 | goto set_fail; |
| 227 | } |
| 228 | |
| 229 | if(mode & GNSS_SET_MSGCFG_RMC) { |
| 230 | |
| 231 | } |
| 232 | |
| 233 | if(mode & GNSS_SET_MSGCFG_VTG) { |
| 234 | |
| 235 | } |
| 236 | |
| 237 | if(mode & GNSS_SET_MSGCFG_GGA) { |
| 238 | |
| 239 | } |
| 240 | |
| 241 | if(mode & GNSS_SET_MSGCFG_GSA) { |
| 242 | |
| 243 | } |
| 244 | |
| 245 | if(mode & GNSS_SET_MSGCFG_GRS) { |
| 246 | |
| 247 | } |
| 248 | |
| 249 | if(mode & GNSS_SET_MSGCFG_GSV) { |
| 250 | |
| 251 | } |
| 252 | |
| 253 | if(mode & GNSS_SET_MSGCFG_GLL) { |
| 254 | |
| 255 | } |
| 256 | |
| 257 | if(mode & GNSS_SET_MSGCFG_ZDA) { |
| 258 | |
| 259 | } |
| 260 | |
| 261 | if(mode & GNSS_SET_MSGCFG_GST) { |
| 262 | |
| 263 | } |
| 264 | |
| 265 | if(mode & GNSS_SET_MSGCFG_TXT) { |
| 266 | |
| 267 | } |
| 268 | } else { |
| 269 | gnss_set_result = GNSS_ERR_ARG; |
| 270 | goto set_fail; |
| 271 | } |
| 272 | |
| 273 | should_wait_rsp = TRUE; |
| 274 | } else if(memcmp(cmd, "$MINEL", 6) == 0) { // $MINEL,<elev> |
| 275 | #if 0 |
| 276 | float elev = 0.0f; |
| 277 | elev = (float)atof(cmd + 7); |
| 278 | // LOGD("ELEV : %f", elev); |
| 279 | // log_hex("ELEV", &elev, sizeof(double)); |
| 280 | if(elev < -90.0f || elev > 90.0f) { |
| 281 | gnss_set_result = GNSS_ERR_ARG; |
| 282 | goto set_fail; |
| 283 | } |
| 284 | float elevs[2]; |
| 285 | elevs[0] = elev; |
| 286 | elevs[1] = elev; |
| 287 | gnss_set_result = gnss_8122_minel(fd, elevs); |
| 288 | if(gnss_set_result != GNSS_ERR_OK) { |
| 289 | goto set_fail; |
| 290 | } |
| 291 | should_wait_rsp = FALSE; |
| 292 | #else |
| 293 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 294 | goto set_fail; |
| 295 | #endif |
| 296 | } else if(memcmp(cmd, "$NMEACFG", 8) == 0) { // $NMEACFG,<ver> |
| 297 | #if 0 |
| 298 | gnss_memaver_type_enum version = (gnss_memaver_type_enum)atoi(cmd + 9); |
| 299 | if(version == GNSS_MEMAVER_TYPE_3_0) { |
| 300 | gnss_set_result = gnss_8122_nmeaver(fd, 1); |
| 301 | } else if(version == GNSS_MEMAVER_TYPE_4_0) { |
| 302 | gnss_set_result = gnss_8122_nmeaver(fd, 2); |
| 303 | } else if(version == GNSS_MEMAVER_TYPE_4_1) { |
| 304 | gnss_set_result = gnss_8122_nmeaver(fd, 3); |
| 305 | } else { |
| 306 | gnss_set_result = GNSS_ERR_ARG; |
| 307 | goto set_fail; |
| 308 | } |
| 309 | if(gnss_set_result != GNSS_ERR_OK) { |
| 310 | goto set_fail; |
| 311 | } |
| 312 | should_wait_rsp = FALSE; |
| 313 | #else |
| 314 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 315 | goto set_fail; |
| 316 | #endif |
| 317 | } |
| 318 | else |
| 319 | { |
| 320 | LOGW("Unknown cmd:%s", cmd); |
| 321 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 322 | goto set_fail; |
| 323 | } |
| 324 | |
| 325 | set_success: |
| 326 | if(should_wait_rsp) { |
| 327 | setting_waitting = TRUE; |
| 328 | pthread_mutex_lock(&read_mutex); |
| 329 | pthread_cond_wait(&read_cond, &read_mutex); |
| 330 | pthread_mutex_unlock(&read_mutex); |
| 331 | } else { |
| 332 | mbtk_timer_clear(); |
| 333 | } |
| 334 | |
| 335 | setting_busy = FALSE; |
| 336 | return gnss_set_result; |
| 337 | set_fail: |
| 338 | setting_busy = FALSE; |
| 339 | mbtk_timer_clear(); |
| 340 | return gnss_set_result; |
| 341 | } |
| 342 | } |
| 343 | |