| 1 | #include "ucos_dev.h" |
|---|
| 2 | #include "bos.h" |
|---|
| 3 | #include "ucos_ii.h" |
|---|
| 4 | #include "serial.h" |
|---|
| 5 | #include "bkni.h" |
|---|
| 6 | #include "bcmmemmgr.h" |
|---|
| 7 | #include "ministd.h" |
|---|
| 8 | #include <sys/errno.h> |
|---|
| 9 | #include <sys/time.h> |
|---|
| 10 | #include <sys/times.h> |
|---|
| 11 | #include <unistd.h> |
|---|
| 12 | #include <string.h> |
|---|
| 13 | #include <stdlib.h> |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | int ucos_fork_r(struct _reent *r); |
|---|
| 17 | int ucos_kill_r(struct _reent *r, int pid, int sig); |
|---|
| 18 | int ucos_execve_r(struct _reent *r, const char *, char *const *, char *const *); |
|---|
| 19 | int ucos_wait_r(struct _reent *r, int *); |
|---|
| 20 | void ucos_exit(int ret); |
|---|
| 21 | int ucos_gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp); |
|---|
| 22 | _CLOCK_T_ ucos_times_r(struct _reent *, struct tms *); |
|---|
| 23 | int ucos_getpid_r(struct _reent *r); |
|---|
| 24 | int ucos_creat_r(struct _reent *r, const char *path, int mode); |
|---|
| 25 | void* ucos_sbrk_r(struct _reent *r, ptrdiff_t ptrdiff); |
|---|
| 26 | |
|---|
| 27 | int ucos_com_open_r( struct _reent *r, const char *path, int flags, int mode ); |
|---|
| 28 | int ucos_com_close_r( struct _reent *r, int fd ); |
|---|
| 29 | long ucos_com_write_r( struct _reent *r, int fd, const char *ptr, int len ); |
|---|
| 30 | long ucos_com_read_r( struct _reent *r, int fd, char *ptr, int len ); |
|---|
| 31 | off_t ucos_com_lseek_r(struct _reent *r, int fd, off_t offset, int whence); |
|---|
| 32 | int ucos_com_fstat_r(struct _reent *r, int fd, struct stat *buf); |
|---|
| 33 | |
|---|
| 34 | static sys_table_t g_sys_table = |
|---|
| 35 | { |
|---|
| 36 | ucos_fork_r, |
|---|
| 37 | ucos_kill_r, |
|---|
| 38 | ucos_execve_r, |
|---|
| 39 | ucos_wait_r, |
|---|
| 40 | ucos_exit, |
|---|
| 41 | ucos_gettimeofday_r, |
|---|
| 42 | ucos_times_r, |
|---|
| 43 | ucos_getpid_r, |
|---|
| 44 | ucos_creat_r, |
|---|
| 45 | ucos_sbrk_r, |
|---|
| 46 | }; |
|---|
| 47 | |
|---|
| 48 | sys_table_t *g_p_sys_table = &g_sys_table; |
|---|
| 49 | |
|---|
| 50 | static io_device_table_t g_com0_sys_table = |
|---|
| 51 | { |
|---|
| 52 | "stdin", |
|---|
| 53 | NULL, |
|---|
| 54 | ucos_com_open_r, |
|---|
| 55 | ucos_com_close_r, |
|---|
| 56 | ucos_com_write_r, |
|---|
| 57 | ucos_com_read_r, |
|---|
| 58 | ucos_com_lseek_r, |
|---|
| 59 | ucos_com_fstat_r, |
|---|
| 60 | }; |
|---|
| 61 | static io_device_table_t g_com1_sys_table = |
|---|
| 62 | { |
|---|
| 63 | "stdout", |
|---|
| 64 | NULL, |
|---|
| 65 | ucos_com_open_r, |
|---|
| 66 | ucos_com_close_r, |
|---|
| 67 | ucos_com_write_r, |
|---|
| 68 | ucos_com_read_r, |
|---|
| 69 | ucos_com_lseek_r, |
|---|
| 70 | ucos_com_fstat_r, |
|---|
| 71 | }; |
|---|
| 72 | static io_device_table_t g_com2_sys_table = |
|---|
| 73 | { |
|---|
| 74 | "stderr", |
|---|
| 75 | NULL, |
|---|
| 76 | ucos_com_open_r, |
|---|
| 77 | ucos_com_close_r, |
|---|
| 78 | ucos_com_write_r, |
|---|
| 79 | ucos_com_read_r, |
|---|
| 80 | ucos_com_lseek_r, |
|---|
| 81 | ucos_com_fstat_r, |
|---|
| 82 | }; |
|---|
| 83 | |
|---|
| 84 | io_device_table_t *g_device_tables[] = |
|---|
| 85 | { |
|---|
| 86 | &g_com0_sys_table, |
|---|
| 87 | &g_com1_sys_table, |
|---|
| 88 | &g_com2_sys_table, |
|---|
| 89 | NULL |
|---|
| 90 | }; |
|---|
| 91 | |
|---|
| 92 | |
|---|
| 93 | |
|---|
| 94 | extern io_device_table_t *g_device_tables[]; |
|---|
| 95 | |
|---|
| 96 | int ucos_fork_r(struct _reent *r) |
|---|
| 97 | { |
|---|
| 98 | __errno_r(r) = ENOTSUP; |
|---|
| 99 | return -1; |
|---|
| 100 | } |
|---|
| 101 | int ucos_kill_r(struct _reent *r, int pid, int sig) |
|---|
| 102 | { |
|---|
| 103 | __errno_r(r) = ENOTSUP; |
|---|
| 104 | return -1; |
|---|
| 105 | } |
|---|
| 106 | int ucos_execve_r(struct _reent *r, const char *path, char *const *argv, char *const *envp) |
|---|
| 107 | { |
|---|
| 108 | __errno_r(r) = ENOTSUP; |
|---|
| 109 | return -1; |
|---|
| 110 | } |
|---|
| 111 | int ucos_wait_r(struct _reent *r, int *status) |
|---|
| 112 | { |
|---|
| 113 | __errno_r(r) = ENOTSUP; |
|---|
| 114 | return -1; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | void ucos_exit(int ret) |
|---|
| 118 | { |
|---|
| 119 | __asm__("sdbbp"); |
|---|
| 120 | } |
|---|
| 121 | #define do_div(n,base) ({ \ |
|---|
| 122 | int __res; \ |
|---|
| 123 | __res = ((unsigned long) (n)) % (unsigned) (base); \ |
|---|
| 124 | (n) = ((unsigned long) (n)) / (unsigned) (base); \ |
|---|
| 125 | __res; }) |
|---|
| 126 | |
|---|
| 127 | #define do_div64_32(res, high, low, base) ({ \ |
|---|
| 128 | unsigned long __quot, __mod; \ |
|---|
| 129 | unsigned long __cf, __tmp, __tmp2, __i; \ |
|---|
| 130 | \ |
|---|
| 131 | __asm__(".set push\n\t" \ |
|---|
| 132 | ".set noat\n\t" \ |
|---|
| 133 | ".set noreorder\n\t" \ |
|---|
| 134 | "move %2, $0\n\t" \ |
|---|
| 135 | "move %3, $0\n\t" \ |
|---|
| 136 | "b 1f\n\t" \ |
|---|
| 137 | " li %4, 0x21\n" \ |
|---|
| 138 | "0:\n\t" \ |
|---|
| 139 | "sll $1, %0, 0x1\n\t" \ |
|---|
| 140 | "srl %3, %0, 0x1f\n\t" \ |
|---|
| 141 | "or %0, $1, %5\n\t" \ |
|---|
| 142 | "sll %1, %1, 0x1\n\t" \ |
|---|
| 143 | "sll %2, %2, 0x1\n" \ |
|---|
| 144 | "1:\n\t" \ |
|---|
| 145 | "bnez %3, 2f\n\t" \ |
|---|
| 146 | "sltu %5, %0, %z6\n\t" \ |
|---|
| 147 | "bnez %5, 3f\n\t" \ |
|---|
| 148 | "2:\n\t" \ |
|---|
| 149 | " addiu %4,%4,-1\n\t" \ |
|---|
| 150 | "subu %0, %0, %z6\n\t" \ |
|---|
| 151 | "addiu %2, %2, 1\n" \ |
|---|
| 152 | "3:\n\t" \ |
|---|
| 153 | "bnez %4, 0b\n\t" \ |
|---|
| 154 | " srl %5, %1, 0x1f\n\t" \ |
|---|
| 155 | ".set pop" \ |
|---|
| 156 | : "=&r" (__mod), "=&r" (__tmp), "=&r" (__quot), "=&r" (__cf), \ |
|---|
| 157 | "=&r" (__i), "=&r" (__tmp2) \ |
|---|
| 158 | : "Jr" (base), "0" (high), "1" (low)); \ |
|---|
| 159 | \ |
|---|
| 160 | (res) = __quot; \ |
|---|
| 161 | __mod; }) |
|---|
| 162 | |
|---|
| 163 | static unsigned |
|---|
| 164 | b_div64_32(unsigned *res, unsigned high, unsigned low, unsigned base) |
|---|
| 165 | { |
|---|
| 166 | unsigned res_; |
|---|
| 167 | unsigned rem; |
|---|
| 168 | rem = do_div64_32(res_, high, low, base); |
|---|
| 169 | *res = res_; |
|---|
| 170 | return rem; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | int ucos_gettimeofday_r(struct _reent *r, struct timeval *tp, void *tzp) |
|---|
| 174 | { |
|---|
| 175 | #if 1 |
|---|
| 176 | b_clock clock; |
|---|
| 177 | long long usec_ticks; |
|---|
| 178 | |
|---|
| 179 | /* get running clock */ |
|---|
| 180 | bos_getclock(&clock); |
|---|
| 181 | if(clock.clock_freq>0) { |
|---|
| 182 | usec_ticks = b_div64_32((unsigned *)&tp->tv_sec, clock.clock_hi, clock.clock_low, clock.clock_freq); |
|---|
| 183 | usec_ticks *= (1000000/64); /* scale usec ticks */ |
|---|
| 184 | b_div64_32((unsigned *)&tp->tv_usec, usec_ticks>>32, usec_ticks, (clock.clock_freq/64)); |
|---|
| 185 | } else { |
|---|
| 186 | tp->tv_usec = 0; |
|---|
| 187 | tp->tv_sec = 0; |
|---|
| 188 | } |
|---|
| 189 | #endif |
|---|
| 190 | return 0; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | extern struct b_task_stats *bos_taskinfo(int priority); |
|---|
| 194 | |
|---|
| 195 | _CLOCK_T_ ucos_times_r(struct _reent *r, struct tms *tms) |
|---|
| 196 | { |
|---|
| 197 | b_cpu_load load; |
|---|
| 198 | struct b_task_stats * p_stats = bos_taskinfo(ucos_getpid_r(r)); |
|---|
| 199 | bos_getload(&load); |
|---|
| 200 | if (!p_stats || !tms) |
|---|
| 201 | { |
|---|
| 202 | __errno_r(r) = ENODEV; |
|---|
| 203 | return -1; |
|---|
| 204 | } |
|---|
| 205 | memset(tms,0,sizeof(*tms)); |
|---|
| 206 | tms->tms_utime = p_stats->clock_cnt; /* user time */ |
|---|
| 207 | tms->tms_stime = load.duration; |
|---|
| 208 | tms->tms_cutime = load.idle; |
|---|
| 209 | tms->tms_cstime = load.isr; |
|---|
| 210 | return bos_getticks(); |
|---|
| 211 | } |
|---|
| 212 | |
|---|
| 213 | int ucos_getpid_r(struct _reent *r) |
|---|
| 214 | { |
|---|
| 215 | OS_TCB tcb; |
|---|
| 216 | |
|---|
| 217 | int id; |
|---|
| 218 | |
|---|
| 219 | /* use our priority as a task id */ |
|---|
| 220 | OSTaskQuery( OS_PRIO_SELF, &tcb ); |
|---|
| 221 | id = tcb.OSTCBPrio; |
|---|
| 222 | |
|---|
| 223 | return id; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | int ucos_creat_r(struct _reent *r, const char *path, int mode) |
|---|
| 227 | { |
|---|
| 228 | __errno_r(r) = ENOTSUP; |
|---|
| 229 | return -1; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | |
|---|
| 233 | void* ucos_sbrk_r(struct _reent *r, ptrdiff_t ptrdiff) |
|---|
| 234 | { |
|---|
| 235 | abort(); |
|---|
| 236 | return (void*) NULL; |
|---|
| 237 | } |
|---|
| 238 | |
|---|
| 239 | int ucos_com_open_r( struct _reent *r, const char *path, int flags, int mode ) |
|---|
| 240 | { |
|---|
| 241 | return 0; |
|---|
| 242 | } |
|---|
| 243 | int ucos_com_close_r( struct _reent *r, int fd ) |
|---|
| 244 | { |
|---|
| 245 | return 0; |
|---|
| 246 | } |
|---|
| 247 | extern int BKNI_Print_Write(const char *ptr, int len); |
|---|
| 248 | |
|---|
| 249 | long ucos_com_write_r( struct _reent *r, int fd, const char *ptr, int len ) |
|---|
| 250 | { |
|---|
| 251 | |
|---|
| 252 | return BKNI_Print_Write(ptr,len); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | long ucos_com_read_r( struct _reent *r, int fd, char *ptr, int len ) |
|---|
| 256 | { |
|---|
| 257 | int ch; |
|---|
| 258 | char *orig_ptr = ptr; |
|---|
| 259 | |
|---|
| 260 | do |
|---|
| 261 | { |
|---|
| 262 | ch = serial_getc(CONSOLE_UART,false); |
|---|
| 263 | if ((ch != -1) && (ch != 0)) |
|---|
| 264 | { |
|---|
| 265 | /* Don't print backspace if we have reached beginning of string input */ |
|---|
| 266 | if (ch == '\b') |
|---|
| 267 | { |
|---|
| 268 | if (ptr > orig_ptr) |
|---|
| 269 | { |
|---|
| 270 | serial_putc(CONSOLE_UART,'\b'); |
|---|
| 271 | serial_putc(CONSOLE_UART,' '); |
|---|
| 272 | serial_putc(CONSOLE_UART,ch); |
|---|
| 273 | ptr--; |
|---|
| 274 | } |
|---|
| 275 | continue; |
|---|
| 276 | } |
|---|
| 277 | serial_putc(CONSOLE_UART,ch); |
|---|
| 278 | if ((ch != '\n') && (ch != '\r')) |
|---|
| 279 | { |
|---|
| 280 | *ptr++ = ch; |
|---|
| 281 | } |
|---|
| 282 | }else{ |
|---|
| 283 | bos_sleep(20); /* let othg_p_sys_tableer task run */ |
|---|
| 284 | } |
|---|
| 285 | if ((unsigned int)ptr >= (unsigned int)(&orig_ptr[len - 1])) |
|---|
| 286 | { |
|---|
| 287 | break; |
|---|
| 288 | } |
|---|
| 289 | } while ((ch != '\n') && (ch != '\r')); |
|---|
| 290 | *ptr = 0; |
|---|
| 291 | return (long)orig_ptr; |
|---|
| 292 | } |
|---|
| 293 | off_t ucos_com_lseek_r(struct _reent *r, int fd, off_t offset, int whence) |
|---|
| 294 | { |
|---|
| 295 | return 0; |
|---|
| 296 | } |
|---|
| 297 | int ucos_com_fstat_r(struct _reent *r, int fd, struct stat *buf) |
|---|
| 298 | { |
|---|
| 299 | return 0; |
|---|
| 300 | } |
|---|
| 301 | _PTR _malloc_r (struct _reent *r, size_t size) |
|---|
| 302 | { |
|---|
| 303 | return malloc(size); |
|---|
| 304 | } |
|---|
| 305 | _VOID _free_r (struct _reent *r, _PTR ptr) |
|---|
| 306 | { |
|---|
| 307 | free(ptr); |
|---|
| 308 | } |
|---|
| 309 | _PTR _realloc_r (struct _reent *r,_PTR ptr, size_t size) |
|---|
| 310 | { |
|---|
| 311 | _PTR tmpPtr = NULL; |
|---|
| 312 | if (size == 0) |
|---|
| 313 | { |
|---|
| 314 | if (ptr) |
|---|
| 315 | { |
|---|
| 316 | free(ptr); |
|---|
| 317 | return NULL; |
|---|
| 318 | } |
|---|
| 319 | return ptr; |
|---|
| 320 | |
|---|
| 321 | } |
|---|
| 322 | |
|---|
| 323 | tmpPtr = malloc(size); |
|---|
| 324 | if (!tmpPtr) |
|---|
| 325 | { |
|---|
| 326 | return NULL; |
|---|
| 327 | } |
|---|
| 328 | |
|---|
| 329 | if (!ptr) |
|---|
| 330 | return tmpPtr; |
|---|
| 331 | |
|---|
| 332 | memcpy(tmpPtr,ptr,size); |
|---|
| 333 | return tmpPtr; |
|---|
| 334 | } |
|---|
| 335 | |
|---|
| 336 | _PTR _calloc_r (struct _reent *r,size_t nmbr, size_t size) |
|---|
| 337 | { |
|---|
| 338 | return malloc(nmbr * size); |
|---|
| 339 | } |
|---|
| 340 | _PTR _memalign_r (struct _reent *r, size_t align, size_t size) |
|---|
| 341 | { |
|---|
| 342 | return malloc_align(size,align); |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | #ifdef CONFIG_GP |
|---|
| 346 | void GP_putchar(char ch) |
|---|
| 347 | { |
|---|
| 348 | putchar(ch); |
|---|
| 349 | } |
|---|
| 350 | |
|---|
| 351 | int GP_getchar(void) |
|---|
| 352 | { |
|---|
| 353 | return getchar(); |
|---|
| 354 | } |
|---|
| 355 | #endif |
|---|
| 356 | |
|---|
| 357 | struct _reent rtbl[OS_MAX_TASKS]; |
|---|
| 358 | |
|---|