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 | |
| 115 | int gnss_5311_dev_close() |
| 116 | { |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | int gnss_5311_open(const char *dev) |
| 121 | { |
| 122 | pthread_mutex_init(&read_mutex, NULL); |
| 123 | pthread_cond_init(&read_cond, NULL); |
| 124 | return gnss_port_open(dev, O_RDWR | O_NONBLOCK | O_NOCTTY, UART_BITRATE_NMEA_DEF_FW, TRUE); |
| 125 | } |
| 126 | |
| 127 | int gnss_5311_close(int fd) |
| 128 | { |
| 129 | pthread_mutex_destroy(&read_mutex); |
| 130 | pthread_cond_destroy(&read_cond); |
b.liu | b8c3f62 | 2024-07-01 16:53:13 +0800 | [diff] [blame^] | 131 | |
| 132 | gnss_send_cmd(fd, config_msg_pm5, strlen(config_msg_pm5)); |
| 133 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 134 | return gnss_port_close(fd); |
| 135 | } |
| 136 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 137 | 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] | 138 | { |
| 139 | char cmd[256] = {0}; |
| 140 | if(memcmp(dev, "/dev/", 5) == 0) { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 141 | 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] | 142 | } else { |
b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 143 | 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] | 144 | } |
| 145 | system(cmd); |
| 146 | |
| 147 | return gnss_module_wait_fw_download_done(); |
| 148 | } |
| 149 | |
| 150 | void gnss_5311_set_cb(const void *data, int data_len) |
| 151 | { |
| 152 | const char *buff = (const char*)data; |
| 153 | if(setting_busy) { // Has setting cmd process. |
| 154 | |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | gnss_err_enum gnss_5311_set(int fd, const char *cmd, void *cmd_rsp, int cmd_rsp_len) |
| 159 | { |
| 160 | if(setting_busy) { |
| 161 | return GNSS_ERR_SET_BUSY; |
| 162 | } else { |
| 163 | bool should_wait_rsp = TRUE; |
| 164 | setting_busy = TRUE; |
| 165 | gnss_set_rsp_ptr = cmd_rsp; |
| 166 | gnss_set_result = GNSS_ERR_OK; |
| 167 | |
| 168 | mbtk_timer_set(gnss_set_timer_cb, GNSS_SET_TIMEOUT); |
| 169 | |
| 170 | if(memcmp(cmd, "$RESET", 6) == 0) { // $RESET,<mode> |
| 171 | gnss_reset_type_enum mode = (gnss_reset_type_enum)atoi(cmd + 7); |
| 172 | if(mode == GNSS_RESET_TYPE_HOT) { |
| 173 | |
| 174 | } else if(mode == GNSS_RESET_TYPE_WARM) { |
| 175 | |
| 176 | } else if(mode == GNSS_RESET_TYPE_COLD) { |
| 177 | |
| 178 | } else { |
| 179 | gnss_set_result = GNSS_ERR_ARG; |
| 180 | goto set_fail; |
| 181 | } |
| 182 | if(gnss_set_result != GNSS_ERR_OK) { |
| 183 | goto set_fail; |
| 184 | } |
| 185 | should_wait_rsp = FALSE; |
| 186 | } else if(memcmp(cmd, "$SYSCFG", 7) == 0) { // $SYSCFG,<mode> |
| 187 | uint32 mode = 0; |
| 188 | mode = (uint32)atoi(cmd + 8); |
| 189 | uint32 new_mode = 0; |
| 190 | if(((GNSS_SET_SYSCFG_GPS | GNSS_SET_SYSCFG_BDS | GNSS_SET_SYSCFG_GLO | GNSS_SET_SYSCFG_GAL) & mode) != mode) { |
| 191 | gnss_set_result = GNSS_ERR_ARG; |
| 192 | goto set_fail; |
| 193 | } |
| 194 | |
| 195 | if(mode & GNSS_SET_SYSCFG_GPS) { // GPS |
| 196 | new_mode |= 0x00000001; |
| 197 | } |
| 198 | if(mode & GNSS_SET_SYSCFG_BDS) { // BDS |
| 199 | new_mode |= 0x00000002; |
| 200 | } |
| 201 | if(mode & GNSS_SET_SYSCFG_GLO) { // GLO |
| 202 | new_mode |= 0x00000004; |
| 203 | } |
| 204 | if(mode & GNSS_SET_SYSCFG_GAL) { // GAL |
| 205 | new_mode |= 0x00000010; |
| 206 | } |
| 207 | |
| 208 | |
| 209 | if(gnss_set_result != GNSS_ERR_OK) { |
| 210 | goto set_fail; |
| 211 | } |
| 212 | should_wait_rsp = TRUE; |
| 213 | } else if(memcmp(cmd, "$MSGCFG", 7) == 0) { // $MSGCFG,<mode>,<rate> |
| 214 | uint32 mode; |
| 215 | int rate; |
| 216 | if(2 == sscanf(cmd, "$MSGCFG,%d,%d", &mode, &rate)) { |
| 217 | int time = rate / 1000; // s |
| 218 | if(time < 0) { |
| 219 | gnss_set_result = GNSS_ERR_ARG; |
| 220 | goto set_fail; |
| 221 | } |
| 222 | |
| 223 | if(((GNSS_SET_MSGCFG_RMC | GNSS_SET_MSGCFG_VTG | GNSS_SET_MSGCFG_GGA | GNSS_SET_MSGCFG_GSA |
| 224 | | GNSS_SET_MSGCFG_GRS | GNSS_SET_MSGCFG_GSV | GNSS_SET_MSGCFG_GLL | GNSS_SET_MSGCFG_ZDA |
| 225 | | GNSS_SET_MSGCFG_GST | GNSS_SET_MSGCFG_TXT) & mode) != mode) { |
| 226 | gnss_set_result = GNSS_ERR_ARG; |
| 227 | goto set_fail; |
| 228 | } |
| 229 | |
| 230 | if(mode & GNSS_SET_MSGCFG_RMC) { |
| 231 | |
| 232 | } |
| 233 | |
| 234 | if(mode & GNSS_SET_MSGCFG_VTG) { |
| 235 | |
| 236 | } |
| 237 | |
| 238 | if(mode & GNSS_SET_MSGCFG_GGA) { |
| 239 | |
| 240 | } |
| 241 | |
| 242 | if(mode & GNSS_SET_MSGCFG_GSA) { |
| 243 | |
| 244 | } |
| 245 | |
| 246 | if(mode & GNSS_SET_MSGCFG_GRS) { |
| 247 | |
| 248 | } |
| 249 | |
| 250 | if(mode & GNSS_SET_MSGCFG_GSV) { |
| 251 | |
| 252 | } |
| 253 | |
| 254 | if(mode & GNSS_SET_MSGCFG_GLL) { |
| 255 | |
| 256 | } |
| 257 | |
| 258 | if(mode & GNSS_SET_MSGCFG_ZDA) { |
| 259 | |
| 260 | } |
| 261 | |
| 262 | if(mode & GNSS_SET_MSGCFG_GST) { |
| 263 | |
| 264 | } |
| 265 | |
| 266 | if(mode & GNSS_SET_MSGCFG_TXT) { |
| 267 | |
| 268 | } |
| 269 | } else { |
| 270 | gnss_set_result = GNSS_ERR_ARG; |
| 271 | goto set_fail; |
| 272 | } |
| 273 | |
| 274 | should_wait_rsp = TRUE; |
| 275 | } else if(memcmp(cmd, "$MINEL", 6) == 0) { // $MINEL,<elev> |
| 276 | #if 0 |
| 277 | float elev = 0.0f; |
| 278 | elev = (float)atof(cmd + 7); |
| 279 | // LOGD("ELEV : %f", elev); |
| 280 | // log_hex("ELEV", &elev, sizeof(double)); |
| 281 | if(elev < -90.0f || elev > 90.0f) { |
| 282 | gnss_set_result = GNSS_ERR_ARG; |
| 283 | goto set_fail; |
| 284 | } |
| 285 | float elevs[2]; |
| 286 | elevs[0] = elev; |
| 287 | elevs[1] = elev; |
| 288 | gnss_set_result = gnss_8122_minel(fd, elevs); |
| 289 | if(gnss_set_result != GNSS_ERR_OK) { |
| 290 | goto set_fail; |
| 291 | } |
| 292 | should_wait_rsp = FALSE; |
| 293 | #else |
| 294 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 295 | goto set_fail; |
| 296 | #endif |
| 297 | } else if(memcmp(cmd, "$NMEACFG", 8) == 0) { // $NMEACFG,<ver> |
| 298 | #if 0 |
| 299 | gnss_memaver_type_enum version = (gnss_memaver_type_enum)atoi(cmd + 9); |
| 300 | if(version == GNSS_MEMAVER_TYPE_3_0) { |
| 301 | gnss_set_result = gnss_8122_nmeaver(fd, 1); |
| 302 | } else if(version == GNSS_MEMAVER_TYPE_4_0) { |
| 303 | gnss_set_result = gnss_8122_nmeaver(fd, 2); |
| 304 | } else if(version == GNSS_MEMAVER_TYPE_4_1) { |
| 305 | gnss_set_result = gnss_8122_nmeaver(fd, 3); |
| 306 | } else { |
| 307 | gnss_set_result = GNSS_ERR_ARG; |
| 308 | goto set_fail; |
| 309 | } |
| 310 | if(gnss_set_result != GNSS_ERR_OK) { |
| 311 | goto set_fail; |
| 312 | } |
| 313 | should_wait_rsp = FALSE; |
| 314 | #else |
| 315 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 316 | goto set_fail; |
| 317 | #endif |
| 318 | } |
| 319 | else |
| 320 | { |
| 321 | LOGW("Unknown cmd:%s", cmd); |
| 322 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 323 | goto set_fail; |
| 324 | } |
| 325 | |
| 326 | set_success: |
| 327 | if(should_wait_rsp) { |
| 328 | setting_waitting = TRUE; |
| 329 | pthread_mutex_lock(&read_mutex); |
| 330 | pthread_cond_wait(&read_cond, &read_mutex); |
| 331 | pthread_mutex_unlock(&read_mutex); |
| 332 | } else { |
| 333 | mbtk_timer_clear(); |
| 334 | } |
| 335 | |
| 336 | setting_busy = FALSE; |
| 337 | return gnss_set_result; |
| 338 | set_fail: |
| 339 | setting_busy = FALSE; |
| 340 | mbtk_timer_clear(); |
| 341 | return gnss_set_result; |
| 342 | } |
| 343 | } |
| 344 | |