#include #include #include #include #include #include "../llbruby.h" serial_t *Serial; serial_t *Serial1; /*1:Serial1*/ serial_t *Serial2; /*2:Serial3*/ serial_t *Serial3; /*3:Serial2*/ serial_t *Serial4; /*4:Serial6*/ serial_t *Serial5; /*5:Serial7*/ i2c_t Wire1; i2c_t Wire2; i2c_t Wire3; i2c_t Wire4; i2c_t Wire5; char ExeFilename[64]; void arduino_init() { } void randomSeed(long seed) { srand(seed); } long arduino_random(long min, long max) { return (long)(((double)rand() / (double)RAND_MAX) / ((double)max - (double)min)) + min; } struct pin_node pin_list; static struct pin_node *find_pin(PinType type, PinName pin) { struct pin_node *ret = &pin_list; while (ret != NULL) { if ((ret->type == type) && (ret->pin == pin)) { break; } ret = ret->next; } return ret; } static struct pin_node *new_pin(PinType type, PinName pin) { struct pin_node *ret = calloc(1, sizeof(struct pin_node)); ret->next = pin_list.next; ret->type = type; ret->pin = pin; pin_list.next = ret; return ret; } struct pin_node *get_pin(PinType type, PinName pinno) { struct pin_node *pin; if (pinno == -1) return NULL; pin = find_pin(type, pinno); if (pin != NULL) return pin; pin = new_pin(type, pinno); if (pin == NULL) return NULL; switch (type) { case pintype_gpio: gpio_init(&pin->gpio, pinno); break; case pintype_dac: #if DEVICE_ANALOGOUT analogout_init(&pin->dac, pinno); #endif break; case pintype_pwmout: pwmout_init(&pin->pwmout, pinno); pin->period = 491; break; case pintype_analogin: analogin_init(&pin->analogin, pinno); break; } return pin; } bool portToPins(SCI_PORT port, PinName *txpin, PinName *rxpin) { switch (port) { case SCI_USB0: *txpin = 201; *rxpin = 202; break; case SCI_SCI0P2x: *txpin = 1; *rxpin = 2; break; case SCI_SCI2B: *txpin = 1; *rxpin = 2; break; case SCI_SCI6B: *txpin = 1; *rxpin = 2; break; case SCI_SCI2A: *txpin = 1; *rxpin = 2; break; default: *txpin = 0; *rxpin = 0; return false; } return true; } PinType get_pin_type(PinName pin) { struct pin_node *ret = &pin_list; while (ret != NULL) { if (ret->pin == pin) { return ret->type; } } return pintype_none; } void analogReference(int mode) { } void setPinModeDac(int pin) { } void tone(int pin, int frequency, int duration) { } void noTone(int pin) { } int serial_write(serial_t *serial, const unsigned char *buf, int len) { const char *c = (const char *)buf; for (int i = 0; i < len; i++) { serial_putc(serial, c[i]); } return len; } int serial_print(serial_t *serial, const char *str) { int i; for (i = 0; ; i++) { char c = str[i]; if (c == '\0') break; serial_putc(serial, c); } return i; } int serial_println(serial_t *serial, const char *str) { int i; for (i = 0; ; i++) { char c = str[i]; if (c == '\0') break; serial_putc(serial, c); } i++; serial_putc(serial, '\r'); i++; serial_putc(serial, '\n'); return i; } uint8_t EEPROM_write(int addr, uint8_t data) { return 0; } uint8_t EEPROM_read(int addr) { return 0xFF; } int EEP_fopen(FILEEEP *fp, const char *str, char mode) { int ret; int fm; switch (mode){ case 0: fm = O_RDONLY; break; case 1: fm = O_RDWR | O_CREAT; break; case 2: fm = O_RDWR; break; default: return -1; } ret = open(str, fm); if (ret < 0) { fp->fd = 0; return -1; } fp->fd = ret; return 0; } int EEP_fwrite(FILEEEP *fp, byte *data, int *len) { int ret; ret = write(fp->fd, data, *len); if (ret < 0) { *len = 0; return -1; } *len = ret; return 0; } int EEP_fread(FILEEEP *fp) { int ret; char c; ret = read(fp->fd, &c, 1); if (ret < 0) return -1; return c; } void EEP_fclose(FILEEEP *fp) { close(fp->fd); } int EEP_fseek(FILEEEP *fp, int pos, int mode) { int ret; switch (mode){ case EEP_SEEKTOP: ret = lseek(fp->fd, pos, SEEK_SET); break; case EEP_SEEKCUR: ret = lseek(fp->fd, pos, SEEK_CUR); break; case EEP_SEEKEND: ret = lseek(fp->fd, pos, SEEK_END); break; default: return -1; break; } if (ret < 0) return false; return true; } int EEP_ffilesize(const char *str) { int ret; struct stat fno; ret = stat(str, &fno); if (ret < 0) return 0; return fno.st_size; } bool EEP_fEof(FILEEEP *fp) { int ret, pos; struct stat fno; ret = fstat(fp->fd, &fno); if (ret < 0) return false; pos = lseek(fp->fd, 0, SEEK_CUR); return pos == fno.st_size; } bool EEP_fexist(const char *path) { int ret; struct stat fno; ret = stat(path, &fno); return (ret == 0); } bool EEP_fcopy(const char *src, const char *dst) { int fsrc, fdst; unsigned char buffer[512]; int res = true; int br, bw; fsrc = open(src, O_RDONLY); if (fsrc < 0) return false; fdst = open(dst, O_RDWR | O_CREAT); if (fdst < 0){ close(fsrc); return false; } for (;;) { br = read(fsrc, buffer, sizeof(buffer)); if (br < 0) { res = false; break; } if (br == 0) break; bw = write(fdst, buffer, br); if ((bw < 0) || bw < br) { res = false; break; } } close(fsrc); close(fdst); return res; } bool EEP_fdelete(const char *path) { int res; res = unlink(path); return (res == 0); } bool SD_open(File *file, const char *path, int mode) { int ret; if (!SD_begin()) return false; ret = open(path, mode); if (ret < 0) { file->fd = 0; return false; } file->fd = ret; return true; } bool SD_rmdir(const char *path) { int ret; if (!SD_begin()) return false; ret = rmdir(path); return ret == 0; } bool SD_rename(const char *path1, const char *path2) { int ret; if (!SD_begin()) return false; ret = rename(path1, path2); return ret == 0; } bool SD_remove(const char *path) { int ret; if (!SD_begin()) return false; ret = unlink(path); return ret == 0; } bool SD_exists(const char *path) { int ret; struct stat fno; if (!SD_begin()) return false; ret = stat(path, &fno); return ret == 0; } bool SD_mkdir(const char *path) { int ret; if (!SD_begin()) return false; ret = mkdir(path, 0777); return ret == 0; } int file_write(File *fp, unsigned char *str, int len) { int ret; ret = write(fp->fd, str, len); if (ret < 0) return -1; return ret; } void file_close(File *fp) { close(fp->fd); } int file_size(File *fp) { int ret; struct stat fno; ret = fstat(fp->fd, &fno); if (ret < 0) return -1; return fno.st_size; } int file_position(File *fp) { int ret; struct stat fno; ret = lseek(fp->fd, 0, SEEK_CUR); if (ret < 0) return -1; return ret; } void file_flush(File *fp) { fsync(fp->fd); } bool file_seek(File *fp, int pos) { int ret; ret = lseek(fp->fd, pos, SEEK_SET); return ret == 0; } int file_read(File *fp) { int ret; char c; ret = read(fp->fd, &c, 1); if (ret < 0) return -1; return c; } void system_reboot(int mode) { exit(mode); } int fileloader(const char *progVer, const char *binVer) { return 0; }