b.liu | ced8dd0 | 2024-06-28 13:28:29 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <unistd.h> |
| 5 | #include <limits.h> |
| 6 | #include <stdarg.h> |
| 7 | #include <unistd.h> |
| 8 | #include <time.h> |
| 9 | #include <fcntl.h> |
| 10 | #include <include/log.h> |
| 11 | |
| 12 | #include "jacana_download.h" |
| 13 | #include "jacana_mem.h" |
| 14 | #include "jacana_log.h" |
| 15 | #include "jacana_usleep.h" |
| 16 | |
| 17 | #undef LOG_TAG |
| 18 | #define LOG_TAG "ABoot" |
| 19 | |
| 20 | #ifdef PATH_MAX |
| 21 | #undef PATH_MAX |
| 22 | #define PATH_MAX 127 |
| 23 | #else |
| 24 | #define PATH_MAX 127 |
| 25 | #endif |
| 26 | /*---------------------------------------------------------------------------*/ |
| 27 | char jacana_firmware_file_name[PATH_MAX]; |
| 28 | char jacana_pvt_file_name[PATH_MAX]; |
| 29 | #define ABOOT_PM_NAME "aboot.pm" |
| 30 | #define ABOOT_CPU_FREQ "aboot 1248000" |
| 31 | #define ABOOT_CPU "aboot" |
| 32 | |
| 33 | char gpioCtrlPath[128] = "/sys/devices/platform/asr-gps/ctrl"; |
| 34 | |
| 35 | |
| 36 | static void dubheWrite(char* str) |
| 37 | { |
| 38 | int fd; |
| 39 | fd = open((const char*)gpioCtrlPath, O_WRONLY); |
| 40 | |
| 41 | if (fd >= 0) { |
| 42 | write(fd, str, strlen(str)); |
| 43 | close(fd); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | static int gpsHalReset() |
| 50 | { |
| 51 | dubheWrite("reset 0"); |
| 52 | jacana_delay_msleep(100); |
| 53 | dubheWrite("reset 1"); |
| 54 | jacana_delay_msleep(100); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int gpsHalFWDone() |
| 59 | { |
| 60 | dubheWrite("on"); |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int gpsHalFWFail() |
| 65 | { |
| 66 | dubheWrite("off"); |
| 67 | return 0; |
| 68 | } |
| 69 | /*---------------------------------------------------------------------------*/ |
| 70 | void * |
| 71 | jacana_mem_alloc(size_t size) |
| 72 | { |
| 73 | void *ptr; |
| 74 | ptr = malloc(size); |
| 75 | return ptr; |
| 76 | } |
| 77 | /*---------------------------------------------------------------------------*/ |
| 78 | void |
| 79 | jacana_mem_free(void *ptr) |
| 80 | { |
| 81 | free(ptr); |
| 82 | } |
| 83 | /*---------------------------------------------------------------------------*/ |
| 84 | extern int slogFd; |
| 85 | int |
| 86 | jacana_log_printf(const char *format, ...) |
| 87 | { |
| 88 | int write_length = 0; |
| 89 | va_list args; |
| 90 | char buf[1024]; |
| 91 | va_start(args, format); |
| 92 | write_length = vsnprintf(buf, 1024, format, args); |
| 93 | va_end(args); |
| 94 | |
| 95 | __android_log_printf(LOG_ID_RADIO, LOG_ERR_LEVEL, "%s", buf); |
| 96 | |
| 97 | return write_length; |
| 98 | } |
| 99 | |
| 100 | /*---------------------------------------------------------------------------*/ |
| 101 | int |
| 102 | jacana_delay_usleep(unsigned long useconds) |
| 103 | { |
| 104 | |
| 105 | (void)useconds; |
| 106 | jacana_log_printf("jacana_delay_usleep: %d\n", useconds); |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | |
| 112 | /*---------------------------------------------------------------------------*/ |
| 113 | int |
| 114 | jacana_delay_msleep(unsigned long mseconds) |
| 115 | { |
| 116 | struct timespec wait; |
| 117 | wait.tv_sec = mseconds / 1000; |
| 118 | wait.tv_nsec = (mseconds % 1000) * 1000 * 1000; |
| 119 | nanosleep(&wait, NULL); |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /*---------------------------------------------------------------------------*/ |
| 125 | static int |
| 126 | gnss_module_lock( void ) |
| 127 | { |
| 128 | FILE *flk; |
| 129 | int ret = -1; |
| 130 | |
| 131 | flk = fopen("/sys/power/wake_lock", "w"); |
| 132 | if(flk) { |
| 133 | fprintf(flk, ABOOT_PM_NAME); |
| 134 | ret = fclose(flk); |
| 135 | } |
| 136 | |
| 137 | flk = fopen("/sys/power/cpu_freq_min_pm_qos", "w"); |
| 138 | if(flk) { |
| 139 | fprintf(flk, ABOOT_CPU_FREQ); |
| 140 | ret = fclose(flk); |
| 141 | } |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | /*---------------------------------------------------------------------------*/ |
| 146 | static int |
| 147 | gnss_module_unlock( void ) |
| 148 | { |
| 149 | FILE *flk; |
| 150 | int ret = -1; |
| 151 | |
| 152 | flk = fopen("/sys/power/wake_unlock", "w"); |
| 153 | if(flk) { |
| 154 | fprintf(flk, ABOOT_PM_NAME); |
| 155 | ret = fclose(flk); |
| 156 | } |
| 157 | |
| 158 | flk = fopen("/sys/power/cpu_freq_min_pm_unqos", "w"); |
| 159 | if(flk) { |
| 160 | fprintf(flk, ABOOT_CPU); |
| 161 | ret = fclose(flk); |
| 162 | } |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | /*---------------------------------------------------------------------------*/ |
| 167 | static void |
| 168 | usage(const char *prog) |
| 169 | { |
| 170 | fprintf(stderr, "usage: %s [options]\n", prog); |
| 171 | fprintf(stderr, "example: jacana_download -B 115200 -s ttyUSB1 -F firmware.bin -P pvt.bin\n"); |
| 172 | fprintf(stderr, "Options are:\n"); |
| 173 | fprintf(stderr, " -B baudrate 9600,19200,38400,57600,115200,921600 (default 115200)\n"); |
| 174 | fprintf(stderr, " -s siodev Serial device (e.x. /dev/ttyUSB0)\n"); |
| 175 | fprintf(stderr, " -F firmware Firmware file name\n"); |
| 176 | fprintf(stderr, " -P pvt PVT file name\n"); |
| 177 | } |
| 178 | /*---------------------------------------------------------------------------*/ |
| 179 | int |
| 180 | main(int argc, char *argv[]) |
| 181 | { |
| 182 | const char *prog; |
| 183 | char serial_dev[32]; |
| 184 | int baudrate = 115200; |
| 185 | int c; |
| 186 | int ret = 0; |
| 187 | // int i = 0; |
| 188 | |
| 189 | /*LOGCAT showing with ACAT Tool */ |
| 190 | set_service_log_tag(LOG_TAG); |
| 191 | |
| 192 | prog = argv[0]; |
| 193 | while((c = getopt(argc, argv, "B:s:F:P:h?")) != -1) { |
| 194 | switch(c) { |
| 195 | case 'B': |
| 196 | baudrate = atoi(optarg); |
| 197 | break; |
| 198 | |
| 199 | case 's': |
| 200 | if(strncmp("/dev/", optarg, 5) == 0) { |
| 201 | strcpy(serial_dev, optarg); |
| 202 | } else { |
| 203 | sprintf(serial_dev, "/dev/%s", optarg); |
| 204 | } |
| 205 | break; |
| 206 | |
| 207 | case 'F': |
| 208 | strcpy(jacana_firmware_file_name, optarg); |
| 209 | break; |
| 210 | |
| 211 | case 'P': |
| 212 | strcpy(jacana_pvt_file_name, optarg); |
| 213 | break; |
| 214 | |
| 215 | case '?': |
| 216 | case 'h': |
| 217 | default: |
| 218 | usage(prog); |
| 219 | exit(1); |
| 220 | break; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if(!strlen(serial_dev) || !strlen(jacana_firmware_file_name) |
| 225 | || !strlen(jacana_pvt_file_name)) { |
| 226 | usage(prog); |
| 227 | exit(1); |
| 228 | } |
| 229 | |
| 230 | //for( i=0; i<3; i++) { |
| 231 | /* Reset Jacana Module..*/ |
| 232 | gpsHalReset(); |
| 233 | |
| 234 | /* Disable PM. */ |
| 235 | gnss_module_lock(); |
| 236 | ret = jacana_aboot_tiny_download(serial_dev, baudrate); |
| 237 | gnss_module_unlock(); |
| 238 | |
| 239 | // if (!ret) |
| 240 | // break; |
| 241 | // } |
| 242 | |
| 243 | /* Mark Done. */ |
| 244 | if (!ret) |
| 245 | gpsHalFWDone(); |
| 246 | else |
| 247 | gpsHalFWFail(); |
| 248 | |
| 249 | return ret; |
| 250 | } |