diff --git a/components/msh/msh.c b/components/msh/msh.c index 4483162..f0f3453 100644 --- a/components/msh/msh.c +++ b/components/msh/msh.c @@ -54,8 +54,7 @@ MR_INLINE void msh_new_current_line(void) { msh.cursor = 0; msh.bytes = 0; - if (msh.echo == MR_ENABLE) - { + if (msh.echo == MR_ENABLE) { #ifndef MR_CFG_MSH_PROMPT #define MR_CFG_MSH_PROMPT "msh" #endif /* MR_CFG_MSH_PROMPT */ @@ -64,23 +63,26 @@ MR_INLINE void msh_new_current_line(void) #else #define MR_MSH_COLOR_CYAN(str) str #endif /* MR_USING_MSH_PRINTF_COLOR */ - mr_msh_printf(MR_MSH_COLOR_CYAN("%s ")MR_MSH_COLOR_YELLOW("%s> "), MR_CFG_MSH_PROMPT, msh.prompt); + mr_msh_printf(MR_MSH_COLOR_CYAN("%s ")MR_MSH_COLOR_YELLOW("%s> "), + MR_CFG_MSH_PROMPT, + msh.prompt); } } MR_INLINE void msh_refresh_line(void) { - if (msh.echo == MR_ENABLE) - { + if (msh.echo == MR_ENABLE) { mr_msh_printf(MSH_CLEAR_LINE); - mr_msh_printf(MR_MSH_COLOR_CYAN("%s ")MR_MSH_COLOR_YELLOW("%s> ")"%s", MR_CFG_MSH_PROMPT, msh.prompt, msh.buf); + mr_msh_printf(MR_MSH_COLOR_CYAN("%s ")MR_MSH_COLOR_YELLOW("%s> ")"%s", + MR_CFG_MSH_PROMPT, + msh.prompt, + msh.buf); } } MR_INLINE void msh_move_cursor_left(void) { - if (msh.cursor > 0) - { + if (msh.cursor > 0) { msh.cursor--; mr_msh_printf(MSH_CURSOR_FORWARD); } @@ -88,8 +90,7 @@ MR_INLINE void msh_move_cursor_left(void) MR_INLINE void msh_move_cursor_right(void) { - if (msh.cursor < msh.bytes) - { + if (msh.cursor < msh.bytes) { msh.cursor++; mr_msh_printf(MSH_CURSOR_BACKWARD); } @@ -97,17 +98,14 @@ MR_INLINE void msh_move_cursor_right(void) MR_INLINE void msh_delete_char(void) { - if (msh.cursor > 0) - { + if (msh.cursor > 0) { /* Move the cursor forward and delete the character */ mr_msh_printf(MSH_CURSOR_FORWARD MSH_DELETE_CHAR); /* Check if you need to remove characters from the middle */ - if (msh.cursor != msh.bytes) - { + if (msh.cursor != msh.bytes) { /* Readjust string */ - for (size_t i = msh.cursor; i < msh.bytes; i++) - { + for (size_t i = msh.cursor; i < msh.bytes; i++) { msh.buf[i - 1] = msh.buf[i]; } } @@ -119,22 +117,18 @@ MR_INLINE void msh_delete_char(void) MR_INLINE void msh_insert_char(char c) { - if ((msh.bytes + 1) == MR_CFG_MSH_BUFSZ) - { + if ((msh.bytes + 1) == MR_CFG_MSH_BUFSZ) { return; } /* Check whether the cursor is at the end of the buffer */ - if (msh.cursor != msh.bytes) - { - if (msh.echo == MR_ENABLE) - { + if (msh.cursor != msh.bytes) { + if (msh.echo == MR_ENABLE) { mr_msh_printf(MSH_INSERT_CHAR); } /* Readjust string */ - for (size_t i = msh.bytes; i > msh.cursor; i--) - { + for (size_t i = msh.bytes; i > msh.cursor; i--) { msh.buf[i] = msh.buf[i - 1]; } } @@ -144,8 +138,7 @@ MR_INLINE void msh_insert_char(char c) msh.buf[msh.bytes] = '\0'; /* Echo the character */ - if (msh.echo == MR_ENABLE) - { + if (msh.echo == MR_ENABLE) { mr_msh_printf("%c", c); } } @@ -155,14 +148,13 @@ MR_INLINE void msh_auto_complete(void) const struct mr_msh_cmd *msh_comp = MR_NULL; /* Find the command */ - for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); msh_cmd < &_mr_msh_cmd_end; msh_cmd++) - { + for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); + msh_cmd < &_mr_msh_cmd_end; + msh_cmd++) { /* When cursor is equal to 0, all commands are satisfied, so there is no auto-completion */ - if (strncmp(msh_cmd->name, msh.buf, msh.cursor) == 0) - { + if (strncmp(msh_cmd->name, msh.buf, msh.cursor) == 0) { /* Check whether multiple commands are met */ - if (msh_comp != MR_NULL) - { + if (msh_comp != MR_NULL) { return; } msh_comp = msh_cmd; @@ -170,10 +162,8 @@ MR_INLINE void msh_auto_complete(void) } /* Complete the command */ - if (msh_comp != MR_NULL) - { - for (size_t i = msh.cursor; i < strlen(msh_comp->name); i++) - { + if (msh_comp != MR_NULL) { + for (size_t i = msh.cursor; i < strlen(msh_comp->name); i++) { msh_insert_char(msh_comp->name[i]); } } @@ -181,11 +171,11 @@ MR_INLINE void msh_auto_complete(void) MR_INLINE void msh_history_push(void) { - uint16_t history_prev = (msh.history_index + (MR_CFG_MSH_HISTORY_LINES - 1)) % MR_CFG_MSH_HISTORY_LINES; + uint16_t history_prev = (msh.history_index + (MR_CFG_MSH_HISTORY_LINES - 1)) % + MR_CFG_MSH_HISTORY_LINES; /* Check whether this command is the same as the previous one */ - if ((msh.bytes != 0) && (strncmp(msh.history[history_prev], msh.buf, msh.bytes) != 0)) - { + if ((msh.bytes != 0) && (strncmp(msh.history[history_prev], msh.buf, msh.bytes) != 0)) { memcpy(msh.history[msh.history_index], msh.buf, (msh.bytes + 1)); msh.history_index = (msh.history_index + 1) % MR_CFG_MSH_HISTORY_LINES; msh.history_len = MR_BOUND(msh.history_len + 1, 0, MR_CFG_MSH_HISTORY_LINES); @@ -195,12 +185,12 @@ MR_INLINE void msh_history_push(void) MR_INLINE void msh_history_pop_prev(void) { - if (msh.history_pos < msh.history_len) - { + if (msh.history_pos < msh.history_len) { msh.history_pos++; /* If the previous index is equal to the current index, no previous record is available */ - uint16_t index = (msh.history_index + MR_CFG_MSH_HISTORY_LINES - msh.history_pos) % MR_CFG_MSH_HISTORY_LINES; + uint16_t index = (msh.history_index + MR_CFG_MSH_HISTORY_LINES - msh.history_pos) % + MR_CFG_MSH_HISTORY_LINES; /* Refresh the line with the previous command */ msh.cursor = msh.bytes = strlen(msh.history[index]); @@ -211,12 +201,11 @@ MR_INLINE void msh_history_pop_prev(void) MR_INLINE void msh_history_pop_next(void) { - if (msh.history_pos > 1) - { + if (msh.history_pos > 1) { msh.history_pos--; - uint16_t index = (msh.history_index + MR_CFG_MSH_HISTORY_LINES - msh.history_pos) % MR_CFG_MSH_HISTORY_LINES; - if (index >= msh.history_len) - { + uint16_t index = (msh.history_index + MR_CFG_MSH_HISTORY_LINES - msh.history_pos) % + MR_CFG_MSH_HISTORY_LINES; + if (index >= msh.history_len) { index = 0; } @@ -231,30 +220,27 @@ MR_INLINE void msh_parse_cmd(void) { mr_msh_printf("\r\n"); - if (msh.bytes == 0) - { + if (msh.bytes == 0) { msh_new_current_line(); return; } /* Find the command */ char *cmd = strchr(msh.buf, ' '); - if (cmd != MR_NULL) - { + if (cmd != MR_NULL) { *cmd = '\0'; } /* Execute the command */ - for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); msh_cmd < &_mr_msh_cmd_end; msh_cmd++) - { - if (strncmp(msh_cmd->name, msh.buf, strlen(msh_cmd->name)) != 0) - { + for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); + msh_cmd < &_mr_msh_cmd_end; + msh_cmd++) { + if (strncmp(msh_cmd->name, msh.buf, strlen(msh_cmd->name)) != 0) { continue; } /* Restore the command */ - if (cmd != MR_NULL) - { + if (cmd != MR_NULL) { *cmd = ' '; } @@ -265,11 +251,9 @@ MR_INLINE void msh_parse_cmd(void) #endif /* MR_CFG_MSH_ARGS_NUM */ char *argv[MR_CFG_MSH_ARGS_NUM] = {MR_NULL}; char *old_arg = msh.buf; - for (argc = 0; argc < MR_CFG_MSH_ARGS_NUM; argc++) - { + for (argc = 0; argc < MR_CFG_MSH_ARGS_NUM; argc++) { char *arg = strchr(old_arg, ' '); - if ((arg == MR_NULL) || (*(arg + 1) == '\0') || (*(arg + 1) == ' ')) - { + if ((arg == MR_NULL) || (*(arg + 1) == '\0') || (*(arg + 1) == ' ')) { break; } *arg = '\0'; @@ -350,59 +334,46 @@ static struct { const char *key; void (*fn)(void); -} msh_key_map[] = - { - {KEY_ENTER, msh_key_enter}, - {KEY_BACKSPACE, msh_key_backspace}, - {KEY_TABLE, msh_key_table}, - {KEY_LEFT, msh_key_left}, - {KEY_RIGHT, msh_key_right}, - {KEY_UP, msh_key_up}, - {KEY_DOWN, msh_key_down}, - {KEY_DELETE, msh_key_delete}, - }; +} msh_key_map[] = {{KEY_ENTER, msh_key_enter}, + {KEY_BACKSPACE, msh_key_backspace}, + {KEY_TABLE, msh_key_table}, + {KEY_LEFT, msh_key_left}, + {KEY_RIGHT, msh_key_right}, + {KEY_UP, msh_key_up}, + {KEY_DOWN, msh_key_down}, + {KEY_DELETE, msh_key_delete},}; MR_INLINE void msh_parse_key(char c) { int parse_flag = MR_DISABLE; /* key-bytes: (0) -> short character key, (!0) -> long character key */ - if (msh.key_bytes == 0) - { - if (MSH_IS_ESC_KEY(c)) - { + if (msh.key_bytes == 0) { + if (MSH_IS_ESC_KEY(c)) { msh.key_buf[msh.key_bytes++] = c; - } else - { + } else { msh.key_buf[msh.key_bytes++] = c; msh.key_buf[msh.key_bytes] = '\0'; parse_flag = MR_ENABLE; } - } else - { - if (MSH_IS_END_KEY(c)) - { + } else { + if (MSH_IS_END_KEY(c)) { msh.key_buf[msh.key_bytes++] = c; msh.key_buf[msh.key_bytes] = '\0'; parse_flag = MR_ENABLE; - } else - { + } else { msh.key_buf[msh.key_bytes++] = c; /* Check whether the key-buffer is full */ - if (msh.key_bytes >= sizeof(msh.key_buf) - 1) - { + if (msh.key_bytes >= sizeof(msh.key_buf) - 1) { msh.key_bytes = 0; } } } /* Parse the long character key */ - if (parse_flag == MR_ENABLE) - { - for (size_t i = 0; i < MR_ARRAY_NUM(msh_key_map); i++) - { - if (strncmp(msh.key_buf, msh_key_map[i].key, msh.key_bytes) == 0) - { + if (parse_flag == MR_ENABLE) { + for (size_t i = 0; i < MR_ARRAY_NUM(msh_key_map); i++) { + if (strncmp(msh.key_buf, msh_key_map[i].key, msh.key_bytes) == 0) { msh_key_map[i].fn(); break; } @@ -421,8 +392,7 @@ MR_INLINE void msh_parse_key(char c) MR_WEAK int mr_msh_input(char *c) { /* Try to open the serial port */ - if (msh.desc == -1) - { + if (msh.desc == -1) { #ifndef MR_CFG_MSH_DEV_NAME #define MR_CFG_MSH_DEV_NAME "serial1" #endif /* MR_CFG_MSH_DEV_NAME */ @@ -431,8 +401,7 @@ MR_WEAK int mr_msh_input(char *c) #else int ret = mr_dev_open(MR_CFG_MSH_DEV_NAME, MR_O_RDWR | MR_O_NONBLOCK); #endif /* MR_CFG_MSH_NONBLOCKING */ - if (ret < 0) - { + if (ret < 0) { return ret; } msh.desc = ret; @@ -453,15 +422,13 @@ MR_WEAK int mr_msh_input(char *c) MR_WEAK int mr_msh_printf_output(const char *buf, size_t size) { /* Try to open the serial port */ - if (msh.desc == -1) - { + if (msh.desc == -1) { #ifndef MR_CFG_MSH_NONBLOCKING int ret = mr_dev_open(MR_CFG_MSH_DEV_NAME, MR_O_RDWR); #else int ret = mr_dev_open(MR_CFG_MSH_DEV_NAME, MR_O_RDWR | MR_O_NONBLOCK); #endif /* MR_CFG_MSH_NONBLOCKING */ - if (ret < 0) - { + if (ret < 0) { return ret; } msh.desc = ret; @@ -512,14 +479,11 @@ void mr_msh_set_prompt(char *prompt) void mr_msh_handle(void) { char c; - while (mr_msh_input(&c) > 0) - { + while (mr_msh_input(&c) > 0) { /* Judgments are characters and keys */ - if (MSH_IS_PRINTABLE(c) && (msh.key_bytes == 0)) - { + if (MSH_IS_PRINTABLE(c) && (msh.key_bytes == 0)) { msh_insert_char(c); - } else - { + } else { msh_parse_key(c); } } @@ -551,8 +515,9 @@ MR_INIT_DEV_EXPORT(mr_msh_init); static void msh_cmd_help(int argc, void *argv) { - for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); msh_cmd < &_mr_msh_cmd_end; msh_cmd++) - { + for (const struct mr_msh_cmd *msh_cmd = ((&_mr_msh_cmd_start) + 1); + msh_cmd < &_mr_msh_cmd_end; + msh_cmd++) { mr_msh_printf("%-*s - %s\r\n", 16, msh_cmd->name, msh_cmd->help); } } @@ -574,17 +539,14 @@ static void msh_cmd_logo(int argc, void *argv) static void msh_cmd_echo(int argc, void *argv) { - if (argc == 1) - { + if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) { goto usage; } - if (strncmp(MR_MSH_GET_ARG(1), "on", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(1), "on", 2) == 0) { msh.echo = MR_ENABLE; return; - } else if (strncmp(MR_MSH_GET_ARG(1), "off", 3) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(1), "off", 3) == 0) { msh.echo = MR_DISABLE; return; } diff --git a/components/msh/msh_device.c b/components/msh/msh_device.c index 19fe855..8d1fac9 100644 --- a/components/msh/msh_device.c +++ b/components/msh/msh_device.c @@ -26,8 +26,7 @@ static int msh_update_path(int desc) /* Get the path and position */ int ret = msh_dev_get_path(desc, msh_path, sizeof(msh_path)); - if (ret < 0) - { + if (ret < 0) { return ret; } mr_dev_ioctl(desc, MR_IOC_GPOS, &position); @@ -48,27 +47,22 @@ static void msh_cmd_dselect(int argc, void *argv) { int desc, ret; - if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) - { + if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) { goto usage; } /* Parse <-g|desc (>=0)> */ - if (strncmp(MR_MSH_GET_ARG(1), "-g", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(1), "-g", 2) == 0) { mr_msh_printf("%d\r\n", MSH_GET_DESC()); - } else - { + } else { ret = sscanf(MR_MSH_GET_ARG(1), "%d", &desc); - if ((ret < 1) || (desc < 0)) - { + if ((ret < 1) || (desc < 0)) { goto usage; } /* Switch to the new descriptor */ ret = msh_update_path(desc); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dselect: %d: %s\r\n", desc, mr_strerror(ret)); } } @@ -83,50 +77,40 @@ static void msh_cmd_dopen(int argc, void *argv) const char *path; int flags, ret; - if (argc < 2) - { + if (argc < 2) { goto usage; } /* Parse <-g> */ - if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) { flags = MR_O_QUERY; /* Parse */ - } else if (strncmp(MR_MSH_GET_ARG(2), "rw", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(2), "rw", 2) == 0) { flags = MR_O_RDWR; - } else if (strncmp(MR_MSH_GET_ARG(2), "r", 1) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(2), "r", 1) == 0) { flags = MR_O_RDONLY; - } else if (strncmp(MR_MSH_GET_ARG(2), "w", 1) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(2), "w", 1) == 0) { flags = MR_O_WRONLY; - } else - { + } else { goto usage; } /* Parse [-n] */ - if ((MR_MSH_GET_ARG(3) != MR_NULL) && (strncmp(MR_MSH_GET_ARG(3), "-n", 2) == 0)) - { + if ((MR_MSH_GET_ARG(3) != MR_NULL) && (strncmp(MR_MSH_GET_ARG(3), "-n", 2) == 0)) { flags |= MR_O_NONBLOCK; } /* Open the new device */ path = MR_MSH_GET_ARG(1); ret = mr_dev_open(path, flags); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dopen: %s: %s\r\n", path, mr_strerror(ret)); return; } - if (flags != MR_O_QUERY) - { + if (flags != MR_O_QUERY) { msh_update_path(ret); - } else - { + } else { mr_msh_printf("%s %s\r\n", mr_strflags(ret), path); } return; @@ -139,36 +123,30 @@ static void msh_cmd_dclose(int argc, void *argv) { int desc, ret; - if ((MR_MSH_GET_ARG(1) != MR_NULL) && (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) - { + if ((MR_MSH_GET_ARG(1) != MR_NULL) && (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) { goto usage; } /* Parse [desc] */ - if (MR_MSH_GET_ARG(1) != MR_NULL) - { + if (MR_MSH_GET_ARG(1) != MR_NULL) { ret = sscanf(MR_MSH_GET_ARG(1), "%d", &desc); - if ((ret < 1) || (desc < 0)) - { + if ((ret < 1) || (desc < 0)) { goto usage; } - } else - { + } else { /* Use the current descriptor */ desc = MSH_GET_DESC(); } /* Close the device */ ret = mr_dev_close(desc); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dclose: %d: %s\r\n", desc, mr_strerror(ret)); return; } /* Switch to the root device */ - if (desc == MSH_GET_DESC()) - { + if (desc == MSH_GET_DESC()) { MSH_SET_DESC(-1); mr_msh_set_prompt("/"); } @@ -182,31 +160,25 @@ MR_INLINE void msh_dioctl_pos(int argc, void *argv) { int cmd = MR_IOC_SPOS, position, ret; - if ((argc < 2) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) - { + if ((argc < 2) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) { goto usage; } /* Parse <-g|position> */ - if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) { cmd = -cmd; - } else - { + } else { ret = sscanf(MR_MSH_GET_ARG(2), "%d", &position); - if (ret < 1) - { + if (ret < 1) { goto usage; } } /* Get/set the position */ mr_dev_ioctl(MSH_GET_DESC(), cmd, &position); - if (cmd < 0) - { + if (cmd < 0) { mr_msh_printf("%d\r\n", position); - } else - { + } else { /* Update the path */ msh_update_path(MSH_GET_DESC()); } @@ -220,47 +192,38 @@ MR_INLINE void msh_dioctl_bufsz(int argc, void *argv) { int cmd, bufsz, ret; - if ((argc < 3) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) - { + if ((argc < 3) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) { goto usage; } /* Parse <-r|-w> */ - if (strncmp(MR_MSH_GET_ARG(3), "-r", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(3), "-r", 2) == 0) { cmd = MR_IOC_SRBSZ; - } else if (strncmp(MR_MSH_GET_ARG(3), "-w", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(3), "-w", 2) == 0) { cmd = MR_IOC_SWBSZ; - } else - { + } else { goto usage; } /* Parse <-g|bufsz> */ - if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) { cmd = -cmd; - } else - { + } else { ret = sscanf(MR_MSH_GET_ARG(2), "%d", &bufsz); - if ((ret < 1) || (bufsz < 0)) - { + if ((ret < 1) || (bufsz < 0)) { goto usage; } } /* Set/get the buffer size */ ret = mr_dev_ioctl(MSH_GET_DESC(), cmd, &bufsz); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dioctl: bufsz: %s\r\n", mr_strerror(ret)); return; } /* If the command is <-g>, print the buffer size */ - if (cmd < 0) - { + if (cmd < 0) { mr_msh_printf("%d\r\n", bufsz); } return; @@ -274,43 +237,35 @@ MR_INLINE void msh_dioctl_datasz(int argc, void *argv) int cmd, datasz, ret; /* Check the arguments and print usage */ - if ((argc < 3) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) - { + if ((argc < 3) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) { goto usage; } /* Parse <-r|-w> */ - if (strncmp(MR_MSH_GET_ARG(3), "-r", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(3), "-r", 2) == 0) { cmd = MR_IOC_CRBD; - } else if (strncmp(MR_MSH_GET_ARG(3), "-w", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(3), "-w", 2) == 0) { cmd = MR_IOC_CWBD; - } else - { + } else { goto usage; } /* Parse <-g|-c> */ - if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) { cmd = -cmd; - } else if (strncmp(MR_MSH_GET_ARG(2), "-c", 2) != 0) - { + } else if (strncmp(MR_MSH_GET_ARG(2), "-c", 2) != 0) { goto usage; } /* Set/get the buffer size */ ret = mr_dev_ioctl(MSH_GET_DESC(), cmd, &datasz); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dioctl: datasz: %s\r\n", mr_strerror(ret)); return; } /* If the command is <-g>, print the buffer size */ - if (cmd < 0) - { + if (cmd < 0) { mr_msh_printf("%d\r\n", datasz); } return; @@ -326,24 +281,20 @@ MR_INLINE void msh_cmd_dioctl_cfg(int argc, void *argv) #endif /* MR_CFG_MSH_ARGS_NUM */ int cfg[MR_CFG_MSH_ARGS_NUM] = {0}, ret; - if ((argc < 2) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) - { + if ((argc < 2) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) { goto usage; } /* Parse <-g> */ memset(cfg, 0, sizeof(cfg)); - if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) { /* Get the config */ ret = mr_dev_ioctl(MSH_GET_DESC(), MR_IOC_GCFG, cfg); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dioctl: cfg: %s\r\n", mr_strerror(ret)); return; } - for (size_t i = 0; i < ret / sizeof(int); i++) - { + for (size_t i = 0; i < ret / sizeof(int); i++) { mr_msh_printf("%d ", cfg[i]); } mr_msh_printf("\r\n"); @@ -351,19 +302,16 @@ MR_INLINE void msh_cmd_dioctl_cfg(int argc, void *argv) } /* Parse */ - for (size_t i = 2; i <= argc; i++) - { + for (size_t i = 2; i <= argc; i++) { ret = sscanf(MR_MSH_GET_ARG(i), "%d", &cfg[i - 2]); - if (ret < 1) - { + if (ret < 1) { goto usage; } } /* Set the config */ ret = mr_dev_ioctl(MSH_GET_DESC(), MR_IOC_SCFG, cfg); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dioctl: cfg: %s\r\n", mr_strerror(ret)); return; } @@ -377,63 +325,50 @@ MR_INLINE void msh_dioctl_cmd(int argc, void *argv) { int args[MR_CFG_MSH_ARGS_NUM] = {0}, cmd, ret; - if ((argc < 2) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) - { + if ((argc < 2) || (strncmp(MR_MSH_GET_ARG(2), "-h", 2) == 0)) { goto usage; } /* Parse */ ret = sscanf(MR_MSH_GET_ARG(1), "%x", &cmd); - if (ret < 1) - { + if (ret < 1) { goto usage; } /* Parse <-g> */ - if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) - { - if (cmd > 0) - { + if (strncmp(MR_MSH_GET_ARG(2), "-g", 2) == 0) { + if (cmd > 0) { cmd = -cmd; } - } else - { - if (cmd < 0) - { + } else { + if (cmd < 0) { cmd = -cmd; } } /* Parse */ memset(args, 0, sizeof(args)); - if (cmd < 0) - { + if (cmd < 0) { /* Get the arguments */ ret = mr_dev_ioctl(MSH_GET_DESC(), cmd, args); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dioctl: %s: %s\r\n", MR_MSH_GET_ARG(1), mr_strerror(ret)); return; } - for (size_t i = 0; i < ret / sizeof(int); i++) - { + for (size_t i = 0; i < ret / sizeof(int); i++) { mr_msh_printf("%d ", args[i]); } mr_msh_printf("\r\n"); - } else - { + } else { /* Set the arguments */ - for (size_t i = 2; i <= argc; i++) - { + for (size_t i = 2; i <= argc; i++) { ret = sscanf(MR_MSH_GET_ARG(i), "%d", &args[i - 2]); - if (ret < 1) - { + if (ret < 1) { goto usage; } } ret = mr_dev_ioctl(MSH_GET_DESC(), cmd, args); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dioctl: %s: %s\r\n", MR_MSH_GET_ARG(1), mr_strerror(ret)); } } @@ -445,26 +380,20 @@ MR_INLINE void msh_dioctl_cmd(int argc, void *argv) static void msh_cmd_dioctl(int argc, void *argv) { - if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) - { + if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) { goto usage; } /* Parse */ - if (strncmp(MR_MSH_GET_ARG(1), "pos", 6) == 0) - { + if (strncmp(MR_MSH_GET_ARG(1), "pos", 6) == 0) { msh_dioctl_pos(argc, argv); - } else if (strncmp(MR_MSH_GET_ARG(1), "cfg", 3) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(1), "cfg", 3) == 0) { msh_cmd_dioctl_cfg(argc, argv); - } else if (strncmp(MR_MSH_GET_ARG(1), "bufsz", 5) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(1), "bufsz", 5) == 0) { msh_dioctl_bufsz(argc, argv); - } else if (strncmp(MR_MSH_GET_ARG(1), "datasz", 6) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(1), "datasz", 6) == 0) { msh_dioctl_datasz(argc, argv); - } else - { + } else { msh_dioctl_cmd(argc, argv); } return; @@ -480,19 +409,14 @@ static void msh_cmd_dioctl(int argc, void *argv) static void msh_printf_1(void *buf, size_t size, char format) { uint8_t *b = (uint8_t *)buf; - for (size_t i = 0; i < size; i++) - { - if (format == 'd') - { + for (size_t i = 0; i < size; i++) { + if (format == 'd') { mr_msh_printf("%d ", (int)b[i]); - } else if (format == 'u') - { + } else if (format == 'u') { mr_msh_printf("%u ", b[i]); - } else if (format == 'c') - { + } else if (format == 'c') { mr_msh_printf("%c ", b[i]); - } else - { + } else { mr_msh_printf("0x%02x ", b[i]); } } @@ -501,16 +425,12 @@ static void msh_printf_1(void *buf, size_t size, char format) static void msh_printf_2(void *buf, size_t size, char format) { uint16_t *b = (uint16_t *)buf; - for (size_t i = 0; i < (size / sizeof(*b)); i++) - { - if (format == 'd') - { + for (size_t i = 0; i < (size / sizeof(*b)); i++) { + if (format == 'd') { mr_msh_printf("%d ", (int)b[i]); - } else if (format == 'u') - { + } else if (format == 'u') { mr_msh_printf("%u ", b[i]); - } else - { + } else { mr_msh_printf("0x%04x ", b[i]); } } @@ -519,27 +439,19 @@ static void msh_printf_2(void *buf, size_t size, char format) static void msh_printf_4(void *buf, size_t size, char format) { uint32_t *b = (uint32_t *)buf; - for (size_t i = 0; i < (size / sizeof(*b)); i++) - { - if (format == 'd') - { + for (size_t i = 0; i < (size / sizeof(*b)); i++) { + if (format == 'd') { mr_msh_printf("%d ", b[i]); - } else if (format == 'u') - { + } else if (format == 'u') { mr_msh_printf("%u ", (int)b[i]); - } else - { + } else { mr_msh_printf("0x%08x ", b[i]); } } } static void (*msh_printf_fn[])(void *buf, size_t size, char format) = - { - msh_printf_1, - msh_printf_2, - msh_printf_4, - }; + {msh_printf_1, msh_printf_2, msh_printf_4,}; static void msh_cmd_dread(int argc, void *argv) { @@ -547,61 +459,48 @@ static void msh_cmd_dread(int argc, void *argv) int printf_index = 0, itemsz = 1, count, ret; char format = 'x'; - if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) - { + if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) { goto usage; } /* Parse */ ret = sscanf(MR_MSH_GET_ARG(1), "%d", &count); - if ((ret < 1) || (count < 0)) - { + if ((ret < 1) || (count < 0)) { goto usage; } /* Parse [-1|-2|-4] */ - if (MR_MSH_GET_ARG(2) != MR_NULL) - { + if (MR_MSH_GET_ARG(2) != MR_NULL) { int arg_index = 3; /* Parse item size */ - if (strncmp(MR_MSH_GET_ARG(2), "-1", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(2), "-1", 2) == 0) { printf_index = 0; itemsz = 1; - } else if (strncmp(MR_MSH_GET_ARG(2), "-2", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(2), "-2", 2) == 0) { printf_index = 1; itemsz = 2; - } else if (strncmp(MR_MSH_GET_ARG(2), "-4", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(2), "-4", 2) == 0) { printf_index = 2; itemsz = 4; - } else - { + } else { arg_index = 2; } /* Parse [-x|-d|-u|-c] */ - if ((arg_index == 2) || (MR_MSH_GET_ARG(3) != MR_NULL)) - { + if ((arg_index == 2) || (MR_MSH_GET_ARG(3) != MR_NULL)) { /* Parse format */ - if (strncmp(MR_MSH_GET_ARG(arg_index), "-x", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(arg_index), "-x", 2) == 0) { format = 'x'; - } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-d", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-d", 2) == 0) { format = 'd'; - } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-u", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-u", 2) == 0) { format = 'u'; - } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-c", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-c", 2) == 0) { printf_index = 0; itemsz = 1; format = 'c'; - } else - { + } else { goto usage; } } @@ -610,12 +509,10 @@ static void msh_cmd_dread(int argc, void *argv) /* Read data */ count = MR_BOUND(count * itemsz, 0, sizeof(buf)); ret = (int)mr_dev_read(MSH_GET_DESC(), buf, count); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dread: %s\r\n", mr_strerror(ret)); return; - } else if (ret == 0) - { + } else if (ret == 0) { mr_msh_printf("dread: no data\r\n"); return; } @@ -638,53 +535,41 @@ static void msh_cmd_dwrite(int argc, void *argv) int data_index = 1, itemsz = 1, count, ret; char format = 'x'; - if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) - { + if ((argc < 1) || (strncmp(MR_MSH_GET_ARG(1), "-h", 2) == 0)) { goto usage; } /* Parse [-1|-2|-4] */ - if (MR_MSH_GET_ARG(1) != MR_NULL) - { + if (MR_MSH_GET_ARG(1) != MR_NULL) { int arg_index = 2; /* Parse item size */ - if (strncmp(MR_MSH_GET_ARG(1), "-1", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(1), "-1", 2) == 0) { itemsz = 1; - } else if (strncmp(MR_MSH_GET_ARG(1), "-2", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(1), "-2", 2) == 0) { itemsz = 2; - } else if (strncmp(MR_MSH_GET_ARG(1), "-4", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(1), "-4", 2) == 0) { itemsz = 4; - } else - { + } else { arg_index = 1; } data_index = arg_index + 1; /* Parse [-x|-d|-u|-c] */ - if ((arg_index == 1) || (MR_MSH_GET_ARG(2) != MR_NULL)) - { + if ((arg_index == 1) || (MR_MSH_GET_ARG(2) != MR_NULL)) { data_index = arg_index + 1; /* Parse format */ - if (strncmp(MR_MSH_GET_ARG(arg_index), "-x", 2) == 0) - { + if (strncmp(MR_MSH_GET_ARG(arg_index), "-x", 2) == 0) { format = 'x'; - } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-d", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-d", 2) == 0) { format = 'd'; - } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-u", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-u", 2) == 0) { format = 'u'; - } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-c", 2) == 0) - { + } else if (strncmp(MR_MSH_GET_ARG(arg_index), "-c", 2) == 0) { itemsz = 1; format = 'c'; - } else - { + } else { data_index--; } } @@ -693,33 +578,27 @@ static void msh_cmd_dwrite(int argc, void *argv) /* Parse data and write */ count = MR_BOUND((argc - data_index + 1) * itemsz, 0, sizeof(buf)); for (size_t i = data_index; - i <= (argc < (sizeof(buf) / itemsz + data_index) ? argc : (sizeof(buf) / itemsz + data_index)); - i++) - { + i <= + (argc < (sizeof(buf) / itemsz + data_index) ? argc : (sizeof(buf) / itemsz + data_index)); + i++) { int arg; - if (format == 'd') - { + if (format == 'd') { ret = sscanf(MR_MSH_GET_ARG(i), "%d", &arg); - } else if (format == 'u') - { + } else if (format == 'u') { ret = sscanf(MR_MSH_GET_ARG(i), "%u", &arg); - } else if (format == 'c') - { + } else if (format == 'c') { ret = sscanf(MR_MSH_GET_ARG(i), "%c", (char *)&arg); - } else - { + } else { ret = sscanf(MR_MSH_GET_ARG(i), "%x", &arg); } memcpy(buf + (i - data_index) * itemsz, &arg, itemsz); - if (ret < 1) - { + if (ret < 1) { goto usage; } } ret = (int)mr_dev_write(MSH_GET_DESC(), buf, count); - if (ret < 0) - { + if (ret < 0) { mr_msh_printf("dwrite: %s\r\n", mr_strerror(ret)); } return; diff --git a/device/Kconfig b/device/Kconfig index 8281b69..d81d228 100644 --- a/device/Kconfig +++ b/device/Kconfig @@ -120,6 +120,12 @@ menu "Device configure" menu "Pin configure" depends on MR_USING_PIN + config MR_USING_PIN_AUTO_DISABLE + bool "Use auto disable pin" + default y + help + "Use this option to allow the pin to be automatically disabled when the device is turned off." + config MR_USING_PIN_CHECK bool "Use pin available check" default y @@ -171,6 +177,12 @@ menu "Device configure" default 0 help "This option sets the size of the TX (transmit) buffer used by the Serial device." + + config MR_USING_SERIAL_DMA + bool "Use Serial DMA" + default n + help + "Use this option allows for the use of Serial DMA." endmenu # SPI diff --git a/device/adc.c b/device/adc.c index 4c4ce73..e08a8df 100644 --- a/device/adc.c +++ b/device/adc.c @@ -10,41 +10,40 @@ #ifdef MR_USING_ADC -MR_INLINE int adc_channel_set_configure(struct mr_adc *adc, int channel, struct mr_adc_config config) +MR_INLINE int adc_channel_set_configure(struct mr_adc *adc, + int channel, + struct mr_adc_config config) { struct mr_adc_ops *ops = (struct mr_adc_ops *)adc->dev.drv->ops; - if (channel < 0 || channel >= 32) - { + if ((channel < 0) || (channel >= (sizeof(adc->channels) * 8))) { return MR_EINVAL; } int ret = ops->channel_configure(adc, channel, config.state); - if (ret < 0) - { + if (ret < 0) { return ret; } /* Enable or disable the channel */ - if (config.state == MR_ENABLE) - { - MR_BIT_SET(adc->channel, (1 << channel)); - } else - { - MR_BIT_CLR(adc->channel, (1 << channel)); + if (config.state == MR_ENABLE) { + MR_BIT_SET(adc->channels, (1 << channel)); + } else { + MR_BIT_CLR(adc->channels, (1 << channel)); } return MR_EOK; } -MR_INLINE int adc_channel_get_configure(struct mr_adc *adc, int channel, struct mr_adc_config *config) +MR_INLINE int adc_channel_get_configure(struct mr_adc *adc, + int channel, + struct mr_adc_config *config) { - if (channel < 0 || channel >= 32) - { + if ((channel < 0) || (channel >= (sizeof(adc->channels) * 8))) { return MR_EINVAL; } /* Get configure */ - config->state = MR_BIT_IS_SET(adc->channel, (1 << channel)); + config->state = MR_BIT_IS_SET(adc->channels, (1 << channel)); return MR_EOK; } @@ -63,12 +62,10 @@ static int mr_adc_close(struct mr_dev *dev) #ifdef MR_USING_ADC_AUTO_DISABLE /* Disable all channels */ - for (size_t i = 0; i < 32; i++) - { - if (MR_BIT_IS_SET(adc->channel, (1 << i)) == MR_ENABLE) - { + for (size_t i = 0; i < (sizeof(adc->channels) * 8); i++) { + if (MR_BIT_IS_SET(adc->channels, (1 << i)) == MR_ENABLE) { ops->channel_configure(adc, (int)i, MR_DISABLE); - MR_BIT_CLR(adc->channel, (1 << i)); + MR_BIT_CLR(adc->channels, (1 << i)); } } #endif /* MR_USING_ADC_AUTO_DISABLE */ @@ -85,17 +82,14 @@ static ssize_t mr_adc_read(struct mr_dev *dev, void *buf, size_t count) #ifdef MR_USING_ADC_CHANNEL_CHECK /* Check if the channel is enabled */ - if (MR_BIT_IS_SET(adc->channel, (1 << dev->position)) == MR_DISABLE) - { + if ((dev->position < 0) || (MR_BIT_IS_SET(adc->channels, (1 << dev->position)) == MR_DISABLE)) { return MR_EINVAL; } #endif /* MR_USING_ADC_CHANNEL_CHECK */ - for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf)) - { + for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf)) { int ret = ops->read(adc, dev->position, rd_buf); - if (ret < 0) - { + if (ret < 0) { return (rd_size == 0) ? ret : rd_size; } rd_buf++; @@ -107,39 +101,32 @@ static int mr_adc_ioctl(struct mr_dev *dev, int cmd, void *args) { struct mr_adc *adc = (struct mr_adc *)dev; - switch (cmd) - { - case MR_IOC_ADC_SET_CHANNEL_CONFIG: - { - if (args != MR_NULL) - { + switch (cmd) { + case MR_IOC_ADC_SET_CHANNEL_CONFIG: { + if (args != MR_NULL) { struct mr_adc_config config = *((struct mr_adc_config *)args); int ret = adc_channel_set_configure(adc, dev->position, config); - if (ret < 0) - { + if (ret < 0) { return ret; } return sizeof(config); } return MR_EINVAL; } - case MR_IOC_ADC_GET_CHANNEL_CONFIG: - { - if (args != MR_NULL) - { + case MR_IOC_ADC_GET_CHANNEL_CONFIG: { + if (args != MR_NULL) { struct mr_adc_config *config = (struct mr_adc_config *)args; int ret = adc_channel_get_configure(adc, dev->position, config); - if (ret < 0) - { + if (ret < 0) { return ret; } return sizeof(*config); } + return MR_EINVAL; } - default: - { + default: { return MR_ENOTSUP; } } @@ -156,15 +143,12 @@ static int mr_adc_ioctl(struct mr_dev *dev, int cmd, void *args) */ int mr_adc_register(struct mr_adc *adc, const char *path, struct mr_drv *drv) { - static struct mr_dev_ops ops = - { - mr_adc_open, - mr_adc_close, - mr_adc_read, - MR_NULL, - mr_adc_ioctl, - MR_NULL - }; + static struct mr_dev_ops ops = {mr_adc_open, + mr_adc_close, + mr_adc_read, + MR_NULL, + mr_adc_ioctl, + MR_NULL}; MR_ASSERT(adc != MR_NULL); MR_ASSERT(path != MR_NULL); @@ -172,7 +156,7 @@ int mr_adc_register(struct mr_adc *adc, const char *path, struct mr_drv *drv) MR_ASSERT(drv->ops != MR_NULL); /* Initialize the fields */ - adc->channel = 0; + adc->channels = 0; /* Register the adc */ return mr_dev_register(&adc->dev, path, MR_DEV_TYPE_ADC, MR_O_RDONLY, &ops, drv); diff --git a/device/dac.c b/device/dac.c index 0147dc2..68793b0 100644 --- a/device/dac.c +++ b/device/dac.c @@ -10,41 +10,40 @@ #ifdef MR_USING_DAC -MR_INLINE int dac_channel_set_configure(struct mr_dac *dac, int channel, struct mr_dac_config config) +MR_INLINE int dac_channel_set_configure(struct mr_dac *dac, + int channel, + struct mr_dac_config config) { struct mr_dac_ops *ops = (struct mr_dac_ops *)dac->dev.drv->ops; - if (channel < 0 || channel >= 32) - { + if ((channel < 0) || (channel >= (sizeof(dac->channels) * 8))) { return MR_EINVAL; } int ret = ops->channel_configure(dac, channel, config.state); - if (ret < 0) - { + if (ret < 0) { return ret; } /* Enable or disable the channel */ - if (config.state == MR_ENABLE) - { - MR_BIT_SET(dac->channel, (1 << channel)); - } else - { - MR_BIT_CLR(dac->channel, (1 << channel)); + if (config.state == MR_ENABLE) { + MR_BIT_SET(dac->channels, (1 << channel)); + } else { + MR_BIT_CLR(dac->channels, (1 << channel)); } return MR_EOK; } -MR_INLINE int dac_channel_get_configure(struct mr_dac *dac, int channel, struct mr_dac_config *config) +MR_INLINE int dac_channel_get_configure(struct mr_dac *dac, + int channel, + struct mr_dac_config *config) { - if (channel < 0 || channel >= 32) - { + if ((channel < 0) || (channel >= (sizeof(dac->channels) * 8))) { return MR_EINVAL; } /* Get configure */ - config->state = MR_BIT_IS_SET(dac->channel, (1 << channel)); + config->state = MR_BIT_IS_SET(dac->channels, (1 << channel)); return MR_EOK; } @@ -63,12 +62,10 @@ static int mr_dac_close(struct mr_dev *dev) #ifdef MR_USING_DAC_AUTO_DISABLE /* Disable all channels */ - for (size_t i = 0; i < 32; i++) - { - if (MR_BIT_IS_SET(dac->channel, (1 << i)) == MR_ENABLE) - { + for (size_t i = 0; i < (sizeof(dac->channels) * 8); i++) { + if (MR_BIT_IS_SET(dac->channels, (1 << i)) == MR_ENABLE) { ops->channel_configure(dac, (int)i, MR_DISABLE); - MR_BIT_CLR(dac->channel, (1 << i)); + MR_BIT_CLR(dac->channels, (1 << i)); } } #endif /* MR_USING_DAC_AUTO_DISABLE */ @@ -85,17 +82,14 @@ static ssize_t mr_dac_write(struct mr_dev *dev, const void *buf, size_t count) #ifdef MR_USING_DAC_CHANNEL_CHECK /* Check if the channel is enabled */ - if (MR_BIT_IS_SET(dac->channel, (1 << dev->position)) == MR_DISABLE) - { + if ((dev->position < 0) || (MR_BIT_IS_SET(dac->channels, (1 << dev->position)) == MR_DISABLE)) { return MR_EINVAL; } #endif /* MR_USING_DAC_CHANNEL_CHECK */ - for (wr_size = 0; wr_size < MR_ALIGN_DOWN(count, sizeof(*wr_buf)); wr_size += sizeof(*wr_buf)) - { + for (wr_size = 0; wr_size < MR_ALIGN_DOWN(count, sizeof(*wr_buf)); wr_size += sizeof(*wr_buf)) { int ret = ops->write(dac, dev->position, *wr_buf); - if (ret < 0) - { + if (ret < 0) { return (wr_size == 0) ? ret : wr_size; } wr_buf++; @@ -107,40 +101,32 @@ static int mr_dac_ioctl(struct mr_dev *dev, int cmd, void *args) { struct mr_dac *dac = (struct mr_dac *)dev; - switch (cmd) - { - case MR_IOC_DAC_SET_CHANNEL_CONFIG: - { - if (args != MR_NULL) - { + switch (cmd) { + case MR_IOC_DAC_SET_CHANNEL_CONFIG: { + if (args != MR_NULL) { struct mr_dac_config config = *((struct mr_dac_config *)args); int ret = dac_channel_set_configure(dac, dev->position, config); - if (ret < 0) - { + if (ret < 0) { return ret; } return sizeof(config); } return MR_EINVAL; } - case MR_IOC_DAC_GET_CHANNEL_CONFIG: - { - if (args != MR_NULL) - { + case MR_IOC_DAC_GET_CHANNEL_CONFIG: { + if (args != MR_NULL) { struct mr_dac_config *config = (struct mr_dac_config *)args; int ret = dac_channel_get_configure(dac, dev->position, config); - if (ret < 0) - { + if (ret < 0) { return ret; } return sizeof(*config); } return MR_EINVAL; } - default: - { + default: { return MR_ENOTSUP; } } @@ -157,15 +143,12 @@ static int mr_dac_ioctl(struct mr_dev *dev, int cmd, void *args) */ int mr_dac_register(struct mr_dac *dac, const char *path, struct mr_drv *drv) { - static struct mr_dev_ops ops = - { - mr_dac_open, - mr_dac_close, - MR_NULL, - mr_dac_write, - mr_dac_ioctl, - MR_NULL - }; + static struct mr_dev_ops ops = {mr_dac_open, + mr_dac_close, + MR_NULL, + mr_dac_write, + mr_dac_ioctl, + MR_NULL}; MR_ASSERT(dac != MR_NULL); MR_ASSERT(path != MR_NULL); @@ -173,7 +156,7 @@ int mr_dac_register(struct mr_dac *dac, const char *path, struct mr_drv *drv) MR_ASSERT(drv->ops != MR_NULL); /* Initialize the fields */ - dac->channel = 0; + dac->channels = 0; /* Register the dac */ return mr_dev_register(&dac->dev, path, MR_DEV_TYPE_DAC, MR_O_WRONLY, &ops, drv); diff --git a/device/fast_pin.c b/device/fast_pin.c index e6e6e5b..6c69cfa 100644 --- a/device/fast_pin.c +++ b/device/fast_pin.c @@ -29,8 +29,7 @@ MR_INLINE struct mr_dev **_fast_pin_dev_get(uint32_t magic) static struct mr_dev *dev = MR_NULL; /* If that doesn't stop you, feel free to use it */ - if (magic == MR_MAGIC_NUMBER) - { + if (magic == MR_MAGIC_NUMBER) { return &dev; } return MR_NULL; @@ -45,8 +44,7 @@ MR_INLINE struct mr_dev **_fast_pin_dev_get(uint32_t magic) */ void _mr_fast_pin_init(struct mr_dev *dev) { - if (_fast_pin_dev_get(dev->magic) != MR_NULL) - { + if (_fast_pin_dev_get(dev->magic) != MR_NULL) { *_fast_pin_dev_get(dev->magic) = dev; } } @@ -65,8 +63,7 @@ int _mr_fast_pin_mode(int number, int mode) { struct mr_dev *dev = *_fast_pin_dev_get(MR_MAGIC_NUMBER); - if (dev == MR_NULL) - { + if (dev == MR_NULL) { return MR_ENOTFOUND; } return ((struct mr_pin_ops *)dev->drv->ops)->configure((struct mr_pin *)dev, number, mode); diff --git a/device/pwm.c b/device/pwm.c index a1319df..8d194ec 100644 --- a/device/pwm.c +++ b/device/pwm.c @@ -10,44 +10,41 @@ #ifdef MR_USING_PWM -MR_INLINE int pwm_channel_set_configure(struct mr_pwm *pwm, int channel, struct mr_pwm_config config) +MR_INLINE int pwm_channel_set_configure(struct mr_pwm *pwm, + int channel, + struct mr_pwm_config config) { struct mr_pwm_ops *ops = (struct mr_pwm_ops *)pwm->dev.drv->ops; - if (channel < 0 || channel >= 32) - { + if ((channel < 0) || (channel >= (sizeof(pwm->channel) * 8))) { return MR_EINVAL; } int ret = ops->channel_configure(pwm, channel, config.state, config.polarity); - if (ret < 0) - { + if (ret < 0) { return ret; } /* Enable or disable the channel */ - if (config.state == MR_ENABLE) - { + if (config.state == MR_ENABLE) { MR_BIT_SET(pwm->channel, (1 << channel)); - if (config.polarity == MR_PWM_POLARITY_NORMAL) - { + if (config.polarity == MR_PWM_POLARITY_NORMAL) { MR_BIT_CLR(pwm->channel_polarity, (1 << channel)); - } else - { + } else { MR_BIT_SET(pwm->channel_polarity, (1 << channel)); } - } else - { + } else { MR_BIT_CLR(pwm->channel, (1 << channel)); MR_BIT_CLR(pwm->channel_polarity, (1 << channel)); } return MR_EOK; } -MR_INLINE int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct mr_pwm_config *config) +MR_INLINE int pwm_channel_get_configure(struct mr_pwm *pwm, + int channel, + struct mr_pwm_config *config) { - if (channel < 0 || channel >= 32) - { + if ((channel < 0) || (channel >= (sizeof(pwm->channel) * 8))) { return MR_EINVAL; } @@ -59,11 +56,11 @@ MR_INLINE int pwm_channel_get_configure(struct mr_pwm *pwm, int channel, struct MR_INLINE int pwm_calculate(struct mr_pwm *pwm, uint32_t freq) { - uint32_t clk = pwm->info->clk, psc_max = pwm->info->prescaler_max, per_max = pwm->info->period_max; + uint32_t clk = pwm->info->clk, psc_max = pwm->info->prescaler_max, per_max = pwm->info + ->period_max; uint32_t psc_best = 1, per_best = 1; - if ((clk == 0) || (freq == 0)) - { + if ((clk == 0) || (freq == 0)) { return MR_EINVAL; } @@ -71,23 +68,19 @@ MR_INLINE int pwm_calculate(struct mr_pwm *pwm, uint32_t freq) uint32_t product = clk / freq; /* If the product is within the maximum period, set it as the period */ - if (product <= per_max) - { + if (product <= per_max) { psc_best = 1; per_best = MR_BOUND(product, 1, per_max); - } else - { + } else { int error_min = INT32_MAX; /* Calculate the least error prescaler and period */ - for (uint32_t psc = MR_BOUND(product / per_max, 1, psc_max); psc < psc_max; psc++) - { + for (uint32_t psc = MR_BOUND(product / per_max, 1, psc_max); psc < psc_max; psc++) { uint32_t per = MR_BOUND(product / psc, 1, per_max); int error = (int)((clk / psc / per) - freq); /* Found a valid and optimal solution */ - if (error == 0) - { + if (error == 0) { psc_best = psc; per_best = per; break; @@ -97,8 +90,7 @@ MR_INLINE int pwm_calculate(struct mr_pwm *pwm, uint32_t freq) * smaller leads to smaller , * smaller means is lower than */ - } else if (error < error_min) - { + } else if (error < error_min) { error_min = error; psc_best = psc; per_best = per; @@ -127,10 +119,8 @@ static int mr_pwm_close(struct mr_dev *dev) #ifdef MR_USING_PWM_AUTO_DISABLE /* Disable all channels */ - for (size_t i = 0; i < 32; i++) - { - if (MR_BIT_IS_SET(pwm->channel, (1 << i)) == MR_ENABLE) - { + for (size_t i = 0; i < (sizeof(pwm->channel) * 8); i++) { + if (MR_BIT_IS_SET(pwm->channel, (1 << i)) == MR_ENABLE) { ops->channel_configure(pwm, (int)i, MR_DISABLE, MR_PWM_POLARITY_NORMAL); MR_BIT_CLR(pwm->channel, (1 << i)); } @@ -149,20 +139,17 @@ static ssize_t mr_pwm_read(struct mr_dev *dev, void *buf, size_t count) #ifdef MR_USING_PWM_CHANNEL_CHECK /* Check if the channel is enabled */ - if (MR_BIT_IS_SET(pwm->channel, (1 << dev->position)) == MR_DISABLE) - { + if ((dev->position < 0) || (MR_BIT_IS_SET(pwm->channel, (1 << dev->position)) == MR_DISABLE)) { return MR_EINVAL; } #endif /* MR_USING_PWM_CHANNEL_CHECK */ - for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf)) - { + for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf)) { uint32_t compare_value; /* Calculate the duty */ int ret = ops->read(pwm, dev->position, &compare_value); - if (ret < 0) - { + if (ret < 0) { return (rd_size == 0) ? ret : rd_size; } *rd_buf = (uint32_t)(((float)compare_value / (float)pwm->period) * 1000000.0f); @@ -180,21 +167,17 @@ static ssize_t mr_pwm_write(struct mr_dev *dev, const void *buf, size_t count) #ifdef MR_USING_PWM_CHANNEL_CHECK /* Check if the channel is enabled */ - if (MR_BIT_IS_SET(pwm->channel, (1 << dev->position)) == MR_DISABLE) - { + if ((dev->position < 0) || (MR_BIT_IS_SET(pwm->channel, (1 << dev->position)) == MR_DISABLE)) { return MR_EINVAL; } #endif /* MR_USING_PWM_CHANNEL_CHECK */ - for (wr_size = 0; wr_size < MR_ALIGN_DOWN(count, sizeof(*wr_buf)); wr_size += sizeof(*wr_buf)) - { + for (wr_size = 0; wr_size < MR_ALIGN_DOWN(count, sizeof(*wr_buf)); wr_size += sizeof(*wr_buf)) { /* Calculate the compare value */ - uint32_t compare_value = MR_BOUND((uint32_t)(((float)*wr_buf / 1000000.0f) * (float)(pwm->period)), - 0, - pwm->period); + uint32_t compare_value = MR_BOUND((uint32_t)(((float)*wr_buf / 1000000.0f) * + (float)(pwm->period)), 0, pwm->period); int ret = ops->write(pwm, dev->position, compare_value); - if (ret < 0) - { + if (ret < 0) { return (wr_size == 0) ? ret : wr_size; } wr_buf++; @@ -207,34 +190,27 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int cmd, void *args) struct mr_pwm *pwm = (struct mr_pwm *)dev; struct mr_pwm_ops *ops = (struct mr_pwm_ops *)dev->drv->ops; - switch (cmd) - { - case MR_IOC_PWM_SET_CHANNEL_CONFIG: - { - if (args != MR_NULL) - { + switch (cmd) { + case MR_IOC_PWM_SET_CHANNEL_CONFIG: { + if (args != MR_NULL) { struct mr_pwm_config config = *((struct mr_pwm_config *)args); int ret = pwm_channel_set_configure(pwm, dev->position, config); - if (ret < 0) - { + if (ret < 0) { return ret; } return sizeof(config); } return MR_EINVAL; } - case MR_IOC_PWM_SET_FREQ: - { - if (args != MR_NULL) - { + case MR_IOC_PWM_SET_FREQ: { + if (args != MR_NULL) { uint32_t freq = *((uint32_t *)args); uint32_t old_period = pwm->period; /* Calculate prescaler and period */ int ret = pwm_calculate(pwm, freq); - if (ret < 0) - { + if (ret < 0) { return ret; } @@ -242,21 +218,19 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int cmd, void *args) ops->start(pwm, pwm->prescaler, pwm->period); /* Refresh all channels compare value */ - for (size_t i = 0; i < 32; i++) - { - if (MR_BIT_IS_SET(pwm->channel, (1 << i)) == MR_ENABLE) - { + for (size_t i = 0; i < 32; i++) { + if (MR_BIT_IS_SET(pwm->channel, (1 << i)) == MR_ENABLE) { uint32_t compare_value; /* Get old duty */ ret = ops->read(pwm, (int)i, &compare_value); - if (ret < 0) - { + if (ret < 0) { continue; } /* Calculate new compare value */ - compare_value = (uint32_t)(((float)compare_value / (float)old_period) * (float)(pwm->period)); + compare_value = (uint32_t)(((float)compare_value / (float)old_period) * + (float)(pwm->period)); ops->write(pwm, (int)i, compare_value); } } @@ -264,25 +238,20 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int cmd, void *args) } return MR_EINVAL; } - case MR_IOC_PWM_GET_CHANNEL_CONFIG: - { - if (args != MR_NULL) - { + case MR_IOC_PWM_GET_CHANNEL_CONFIG: { + if (args != MR_NULL) { struct mr_pwm_config *config = ((struct mr_pwm_config *)args); int ret = pwm_channel_get_configure(pwm, dev->position, config); - if (ret < 0) - { + if (ret < 0) { return ret; } return sizeof(*config); } return MR_EINVAL; } - case MR_IOC_PWM_GET_FREQ: - { - if (args != MR_NULL) - { + case MR_IOC_PWM_GET_FREQ: { + if (args != MR_NULL) { uint32_t *freq = (uint32_t *)args; *freq = pwm->freq; @@ -290,8 +259,7 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int cmd, void *args) } return MR_EINVAL; } - default: - { + default: { return MR_ENOTSUP; } } @@ -307,17 +275,17 @@ static int mr_pwm_ioctl(struct mr_dev *dev, int cmd, void *args) * * @return 0 on success, otherwise an error code. */ -int mr_pwm_register(struct mr_pwm *pwm, const char *path, struct mr_drv *drv, struct mr_pwm_info *info) +int mr_pwm_register(struct mr_pwm *pwm, + const char *path, + struct mr_drv *drv, + struct mr_pwm_info *info) { - static struct mr_dev_ops ops = - { - mr_pwm_open, - mr_pwm_close, - mr_pwm_read, - mr_pwm_write, - mr_pwm_ioctl, - MR_NULL - }; + static struct mr_dev_ops ops = {mr_pwm_open, + mr_pwm_close, + mr_pwm_read, + mr_pwm_write, + mr_pwm_ioctl, + MR_NULL}; MR_ASSERT(pwm != MR_NULL); MR_ASSERT(path != MR_NULL); diff --git a/device/soft_i2c.c b/device/soft_i2c.c index c1c6950..13b4ce1 100644 --- a/device/soft_i2c.c +++ b/device/soft_i2c.c @@ -49,8 +49,7 @@ static int soft_i2c_bus_wait_ack(struct mr_i2c_bus *i2c_bus) soft_i2c_scl_set(soft_i2c_bus, SOFT_I2C_HIGH); mr_delay_us(soft_i2c_bus->delay); - if (soft_i2c_sda_get(soft_i2c_bus) == SOFT_I2C_LOW) - { + if (soft_i2c_sda_get(soft_i2c_bus) == SOFT_I2C_LOW) { ret = MR_EOK; } soft_i2c_scl_set(soft_i2c_bus, SOFT_I2C_LOW); @@ -63,11 +62,9 @@ static void soft_i2c_bus_send_ack(struct mr_i2c_bus *i2c_bus, int ack) struct mr_soft_i2c_bus *soft_i2c_bus = (struct mr_soft_i2c_bus *)i2c_bus; soft_i2c_scl_set(soft_i2c_bus, SOFT_I2C_LOW); - if (ack == MR_ENABLE) - { + if (ack == MR_ENABLE) { soft_i2c_bus_sda_set(soft_i2c_bus, SOFT_I2C_LOW); - } else - { + } else { soft_i2c_bus_sda_set(soft_i2c_bus, SOFT_I2C_HIGH); } @@ -78,17 +75,18 @@ static void soft_i2c_bus_send_ack(struct mr_i2c_bus *i2c_bus, int ack) soft_i2c_bus_sda_set(soft_i2c_bus, SOFT_I2C_HIGH); } -static int mr_soft_i2c_bus_configure(struct mr_i2c_bus *i2c_bus, struct mr_i2c_config *config, int addr, int addr_bits) +static int mr_soft_i2c_bus_configure(struct mr_i2c_bus *i2c_bus, + struct mr_i2c_config *config, + int addr, + int addr_bits) { struct mr_soft_i2c_bus *soft_i2c_bus = (struct mr_soft_i2c_bus *)i2c_bus; int state = (config->baud_rate != 0) ? MR_ENABLE : MR_DISABLE; int mode = (state == MR_ENABLE) ? MR_PIN_MODE_OUTPUT_OD : MR_PIN_MODE_NONE; - if (state == MR_ENABLE) - { + if (state == MR_ENABLE) { /* Soft I2C only support host mode */ - if (config->host_slave != MR_I2C_HOST) - { + if (config->host_slave != MR_I2C_HOST) { return MR_ENOTSUP; } @@ -98,13 +96,11 @@ static int mr_soft_i2c_bus_configure(struct mr_i2c_bus *i2c_bus, struct mr_i2c_c /* Configure SCL and SDA */ int ret = _mr_fast_pin_mode(soft_i2c_bus->scl_pin, mode); - if (ret < 0) - { + if (ret < 0) { return ret; } ret = _mr_fast_pin_mode(soft_i2c_bus->sda_pin, mode); - if (ret < 0) - { + if (ret < 0) { return ret; } return MR_EOK; @@ -127,18 +123,15 @@ static int mr_soft_i2c_bus_write(struct mr_i2c_bus *i2c_bus, uint8_t data); static int mr_soft_i2c_bus_send_addr(struct mr_i2c_bus *i2c_bus, int addr, int addr_bits) { - if (addr_bits == MR_I2C_ADDR_BITS_10) - { + if (addr_bits == MR_I2C_ADDR_BITS_10) { int ret = mr_soft_i2c_bus_write(i2c_bus, addr >> 8); - if (ret < 0) - { + if (ret < 0) { return ret; } } int ret = mr_soft_i2c_bus_write(i2c_bus, addr); - if (ret < 0) - { + if (ret < 0) { return ret; } return MR_EOK; @@ -166,15 +159,13 @@ static int mr_soft_i2c_bus_read(struct mr_i2c_bus *i2c_bus, uint8_t *data, int a mr_delay_us(soft_i2c_bus->delay); soft_i2c_bus_sda_set(soft_i2c_bus, SOFT_I2C_HIGH); - for (size_t bits = 0; bits < (sizeof(*data) * 8); bits++) - { + for (size_t bits = 0; bits < (sizeof(*data) * 8); bits++) { soft_i2c_scl_set(soft_i2c_bus, SOFT_I2C_LOW); mr_delay_us(soft_i2c_bus->delay); soft_i2c_scl_set(soft_i2c_bus, SOFT_I2C_HIGH); mr_delay_us(soft_i2c_bus->delay); *data <<= 1; - if (soft_i2c_sda_get(soft_i2c_bus) == SOFT_I2C_HIGH) - { + if (soft_i2c_sda_get(soft_i2c_bus) == SOFT_I2C_HIGH) { *data |= 0x01; } } @@ -189,8 +180,7 @@ static int mr_soft_i2c_bus_write(struct mr_i2c_bus *i2c_bus, uint8_t data) { struct mr_soft_i2c_bus *soft_i2c_bus = (struct mr_soft_i2c_bus *)i2c_bus; - for (size_t bits = 0; bits < 8; bits++) - { + for (size_t bits = 0; bits < 8; bits++) { soft_i2c_bus_sda_set(soft_i2c_bus, (data & 0x80) ? SOFT_I2C_HIGH : SOFT_I2C_LOW); data <<= 1; @@ -212,22 +202,18 @@ static int mr_soft_i2c_bus_write(struct mr_i2c_bus *i2c_bus, uint8_t data) * * @return 0 on success, otherwise an error code. */ -int mr_soft_i2c_bus_register(struct mr_soft_i2c_bus *soft_i2c_bus, const char *path, int scl_pin, int sda_pin) +int mr_soft_i2c_bus_register(struct mr_soft_i2c_bus *soft_i2c_bus, + const char *path, + int scl_pin, + int sda_pin) { - static struct mr_i2c_bus_ops ops = - { - mr_soft_i2c_bus_configure, - mr_soft_i2c_bus_start, - mr_soft_i2c_bus_send_addr, - mr_soft_i2c_bus_stop, - mr_soft_i2c_bus_read, - mr_soft_i2c_bus_write - }; - static struct mr_drv drv = - { - &ops, - MR_NULL - }; + static struct mr_i2c_bus_ops ops = {mr_soft_i2c_bus_configure, + mr_soft_i2c_bus_start, + mr_soft_i2c_bus_send_addr, + mr_soft_i2c_bus_stop, + mr_soft_i2c_bus_read, + mr_soft_i2c_bus_write}; + static struct mr_drv drv = {&ops, MR_NULL}; MR_ASSERT(soft_i2c_bus != MR_NULL); MR_ASSERT(path != MR_NULL); diff --git a/device/timer.c b/device/timer.c index 1f31ebe..1ce82e7 100644 --- a/device/timer.c +++ b/device/timer.c @@ -12,11 +12,11 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout) { - uint32_t clk = timer->info->clk, psc_max = timer->info->prescaler_max, per_max = timer->info->period_max; + uint32_t clk = timer->info->clk, psc_max = timer->info->prescaler_max, per_max = timer->info + ->period_max; uint32_t psc_best = 1, per_best = 1, reload_best = 1; - if ((clk == 0) || (timeout == 0)) - { + if ((clk == 0) || (timeout == 0)) { return MR_EINVAL; } @@ -24,28 +24,23 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout) uint32_t product = timeout; /* If the product is within the maximum period, set it as the period */ - if (product <= per_max) - { + if (product <= per_max) { psc_best = clk / 1000000; per_best = MR_BOUND(product, 1, per_max); - } else - { + } else { int error_min = INT32_MAX; /* Calculate the least error prescaler and period */ - for (uint32_t psc = MR_BOUND((product / per_max), 1, product); psc < UINT32_MAX; psc++) - { + for (uint32_t psc = MR_BOUND((product / per_max), 1, product); psc < UINT32_MAX; psc++) { uint32_t per = MR_BOUND(product / psc, 1, per_max); int error = (int)(timeout - (per * psc)); /* Found a valid and optimal solution */ - if (error <= 1) - { + if (error <= 1) { psc_best = psc; per_best = per; break; - } else if (error < error_min) - { + } else if (error < error_min) { error_min = error; psc_best = psc; per_best = per; @@ -57,19 +52,18 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout) error_min = INT32_MAX; /* Calculate the least error reload and prescaler */ - for (uint32_t reload = MR_BOUND(product / psc_max, 1, product); reload < product; reload++) - { + for (uint32_t reload = MR_BOUND(product / psc_max, 1, product); + reload < product; + reload++) { uint32_t psc = MR_BOUND(product / reload, 1, psc_max); int error = (int)product - (int)(reload * psc); /* Found a valid and optimal solution */ - if (error <= 1) - { + if (error <= 1) { reload_best = reload; psc_best = psc; break; - } else if (error < error_min) - { + } else if (error < error_min) { error_min = error; reload_best = reload; psc_best = psc; @@ -77,13 +71,11 @@ static int timer_calculate(struct mr_timer *timer, uint32_t timeout) } /* If period can take reload value, lower interrupts by loading reload to period */ - if (per_best <= (per_max / reload_best)) - { + if (per_best <= (per_max / reload_best)) { per_best *= reload_best; reload_best = 1; /* If the reload is less than the prescaler, swap them */ - } else if ((reload_best > per_best) && (reload_best < per_max)) - { + } else if ((reload_best > per_best) && (reload_best < per_max)) { MR_SWAP(reload_best, per_best); } } @@ -119,8 +111,7 @@ static ssize_t mr_timer_read(struct mr_dev *dev, void *buf, size_t count) uint32_t *rd_buf = (uint32_t *)buf; ssize_t rd_size; - for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf)) - { + for (rd_size = 0; rd_size < MR_ALIGN_DOWN(count, sizeof(*rd_buf)); rd_size += sizeof(*rd_buf)) { uint32_t cnt = ops->get_count(timer); *rd_buf = (timer->reload - timer->count) * timer->timeout + @@ -139,8 +130,7 @@ static ssize_t mr_timer_write(struct mr_dev *dev, const void *buf, size_t count) ssize_t wr_size; /* Only the last write is valid */ - for (wr_size = 0; wr_size < MR_ALIGN_DOWN(count, sizeof(*wr_buf)); wr_size += sizeof(*wr_buf)) - { + for (wr_size = 0; wr_size < MR_ALIGN_DOWN(count, sizeof(*wr_buf)); wr_size += sizeof(*wr_buf)) { timeout = *wr_buf; wr_buf++; } @@ -149,12 +139,10 @@ static ssize_t mr_timer_write(struct mr_dev *dev, const void *buf, size_t count) ops->stop(timer); timer->count = timer->reload; - if (timeout != 0) - { + if (timeout != 0) { /* Calculate prescaler and period */ int ret = timer_calculate(timer, timeout); - if (ret < 0) - { + if (ret < 0) { return ret; } @@ -168,12 +156,9 @@ static int mr_timer_ioctl(struct mr_dev *dev, int cmd, void *args) { struct mr_timer *timer = (struct mr_timer *)dev; - switch (cmd) - { - case MR_IOC_TIMER_SET_MODE: - { - if (args != MR_NULL) - { + switch (cmd) { + case MR_IOC_TIMER_SET_MODE: { + if (args != MR_NULL) { struct mr_timer_config config = *(struct mr_timer_config *)args; timer->config = config; @@ -181,10 +166,8 @@ static int mr_timer_ioctl(struct mr_dev *dev, int cmd, void *args) } return MR_EINVAL; } - case MR_IOC_TIMER_GET_MODE: - { - if (args != MR_NULL) - { + case MR_IOC_TIMER_GET_MODE: { + if (args != MR_NULL) { struct mr_timer_config *config = (struct mr_timer_config *)args; *config = timer->config; @@ -192,8 +175,7 @@ static int mr_timer_ioctl(struct mr_dev *dev, int cmd, void *args) } return MR_EINVAL; } - default: - { + default: { return MR_ENOTSUP; } } @@ -204,26 +186,21 @@ static ssize_t mr_timer_isr(struct mr_dev *dev, int event, void *args) struct mr_timer *timer = (struct mr_timer *)dev; struct mr_timer_ops *ops = (struct mr_timer_ops *)dev->drv->ops; - switch (event) - { - case MR_ISR_TIMER_TIMEOUT_INT: - { + switch (event) { + case MR_ISR_TIMER_TIMEOUT_INT: { timer->count--; - if (timer->count == 0) - { + if (timer->count == 0) { timer->count = timer->reload; - if (timer->config.mode == MR_TIMER_MODE_ONESHOT) - { + if (timer->config.mode == MR_TIMER_MODE_ONESHOT) { ops->stop(timer); } return MR_EOK; } return MR_EBUSY; } - default: - { + default: { return MR_ENOTSUP; } } @@ -239,17 +216,17 @@ static ssize_t mr_timer_isr(struct mr_dev *dev, int event, void *args) * * @return 0 on success, otherwise an error code. */ -int mr_timer_register(struct mr_timer *timer, const char *path, struct mr_drv *drv, struct mr_timer_info *info) +int mr_timer_register(struct mr_timer *timer, + const char *path, + struct mr_drv *drv, + struct mr_timer_info *info) { - static struct mr_dev_ops ops = - { - mr_timer_open, - mr_timer_close, - mr_timer_read, - mr_timer_write, - mr_timer_ioctl, - mr_timer_isr - }; + static struct mr_dev_ops ops = {mr_timer_open, + mr_timer_close, + mr_timer_read, + mr_timer_write, + mr_timer_ioctl, + mr_timer_isr}; struct mr_timer_config default_config = MR_TIMER_CONFIG_DEFAULT; MR_ASSERT(timer != MR_NULL); diff --git a/document/coding_style/coding_style.md b/document/coding_style/coding_style.md index 94cd088..fddaca7 100644 --- a/document/coding_style/coding_style.md +++ b/document/coding_style/coding_style.md @@ -18,13 +18,12 @@ ## 缩进与换行 1. 仅使用4个空格进行代码缩进,禁止使用tab键。 -2. 每行代码长度控制在120个字符内,超过换行,下一行缩进4个空格。 -3. 大括号`{`与`}`单独占一行。 +2. 每行代码长度控制在100个字符内,超过换行,下一行缩进4个空格。 +3. 大括号定义时各自占一行,`if`等使用时紧跟行尾。 例如: ```c - if (condition) - { + if (condition) { do_something(); } diff --git a/document/coding_style/coding_style_EN.md b/document/coding_style/coding_style_EN.md index e6b755a..b5fbd9c 100644 --- a/document/coding_style/coding_style_EN.md +++ b/document/coding_style/coding_style_EN.md @@ -18,8 +18,8 @@ ## Indentation and Line Breaks 1. Only use 4 spaces for code indentation, tabs are forbidden. -2. Keep each line of code within 120 characters, break lines and indent the next line by 4 spaces if exceeded. -3. Curly braces `{` and `}` should each be on their own line. +2. Keep each line of code within 100 characters, break lines and indent the next line by 4 spaces if exceeded. +3. Braces are defined on a single line, and `if` etc. are used immediately at the end of the line. For example: ````c diff --git a/document/device/serial/serial.md b/document/device/serial/serial.md index 13e249a..ad20599 100644 --- a/document/device/serial/serial.md +++ b/document/device/serial/serial.md @@ -200,9 +200,6 @@ mr_dev_ioctl(ds, MR_IOC_GWBDSZ, &size); /* 定义回调函数 */ void fn(int desc, void *args) { - /* 获取缓冲区数据大小 */ - ssize_t data_size = *(ssize_t *)args; - /* 处理中断 */ } void (*callback)(int desc, void *args); @@ -224,9 +221,6 @@ mr_dev_ioctl(ds, MR_IOC_SERIAL_GET_WR_CALL, &callback); /* 定义回调函数 */ void fn(int desc, void *args) { - /* 获取缓冲区数据大小 */ - ssize_t data_size = *(ssize_t *)args; - /* 处理中断 */ } void (*callback)(int desc, void *args); diff --git a/document/device/serial/serial_EN.md b/document/device/serial/serial_EN.md index 7766303..1372532 100644 --- a/document/device/serial/serial_EN.md +++ b/document/device/serial/serial_EN.md @@ -208,9 +208,6 @@ mr_dev_ioctl(ds, MR_IOC_GWBDSZ, &size); /* Define callback function */ void fn(int desc, void *args) { - /* Get buffer data size */ - ssize_t data_size = *(ssize_t *)args; - /* Handle interrupt */ } void (*callback)(int desc, void *args); @@ -234,9 +231,6 @@ Independent of SERIAL interface: /* Define callback function */ void fn(int desc, void *args) { - /* Get buffer data size */ - ssize_t data_size = *(ssize_t *)args; - /* Handle interrupt */ } void (*callback)(int desc, void *args); diff --git a/document/device/spi/spi.md b/document/device/spi/spi.md index e234340..cf5d122 100644 --- a/document/device/spi/spi.md +++ b/document/device/spi/spi.md @@ -240,9 +240,6 @@ mr_dev_ioctl(ds, MR_IOC_GRBDSZ, &size); /* 定义回调函数 */ void fn(int desc, void *args) { - /* 获取缓冲区数据大小 */ - ssize_t data_size = *(ssize_t *)args; - /* 处理中断 */ } void (*callback)(int, void *args); @@ -259,9 +256,6 @@ mr_dev_ioctl(ds, MR_IOC_SPI_GET_RD_CALL, &callback); /* 定义回调函数 */ void fn(int desc, void *args) { - /* 获取缓冲区数据大小 */ - ssize_t data_size = *(ssize_t *)args; - /* 处理中断 */ } void (*callback)(int, void *args); diff --git a/document/device/spi/spi_EN.md b/document/device/spi/spi_EN.md index 0a88335..1113a37 100644 --- a/document/device/spi/spi_EN.md +++ b/document/device/spi/spi_EN.md @@ -251,9 +251,6 @@ mr_dev_ioctl(ds, MR_IOC_GRBDSZ, &size); /* Define callback function */ void fn(int desc, void *args) { - /* Get buffer data size */ - ssize_t data_size = *(ssize_t *)args; - /* Handle interrupt */ } void (*callback)(int, void *args); @@ -271,9 +268,6 @@ Independent of SPI interface: /* Define callback function */ void fn(int desc, void *args) { - /* Get buffer data size */ - ssize_t data_size = *(ssize_t *)args; - /* Handle interrupt */ } void (*callback)(int, void *args); diff --git a/include/device/mr_adc.h b/include/device/mr_adc.h index 9fa2748..b765690 100644 --- a/include/device/mr_adc.h +++ b/include/device/mr_adc.h @@ -51,7 +51,7 @@ struct mr_adc { struct mr_dev dev; /**< Device */ - uint32_t channel; /**< Channel */ + uint32_t channels; /**< Channels */ }; /** @@ -66,6 +66,7 @@ struct mr_adc_ops int mr_adc_register(struct mr_adc *adc, const char *path, struct mr_drv *drv); /** @} */ + #endif /* MR_USING_ADC */ #ifdef __cplusplus diff --git a/include/device/mr_dac.h b/include/device/mr_dac.h index 1600524..bfc3ece 100644 --- a/include/device/mr_dac.h +++ b/include/device/mr_dac.h @@ -51,7 +51,7 @@ struct mr_dac { struct mr_dev dev; /**< Device */ - uint32_t channel; /**< Channel */ + uint32_t channels; /**< Channels */ }; /** @@ -66,6 +66,7 @@ struct mr_dac_ops int mr_dac_register(struct mr_dac *dac, const char *path, struct mr_drv *drv); /** @} */ + #endif /* MR_USING_DAC */ #ifdef __cplusplus diff --git a/include/device/mr_i2c.h b/include/device/mr_i2c.h index 8b078f2..86da57d 100644 --- a/include/device/mr_i2c.h +++ b/include/device/mr_i2c.h @@ -23,8 +23,8 @@ extern "C" { */ /** -* @brief I2C host/slave. -*/ + * @brief I2C host/slave. + */ #define MR_I2C_HOST (0) /**< I2C host */ #define MR_I2C_SLAVE (1) /**< I2C slave */ @@ -78,7 +78,7 @@ typedef uint8_t mr_i2c_data_t; /**< I2C rea /** * @brief I2C ISR events. */ -#define MR_ISR_I2C_RD_INT (MR_ISR_RD | (0x01)) /**< Read interrupt */ +#define MR_ISR_I2C_RD_INT (MR_ISR_RD | (0x01)) /**< Read interrupt event */ /** * @brief I2C bus structure. @@ -97,7 +97,10 @@ struct mr_i2c_bus */ struct mr_i2c_bus_ops { - int (*configure)(struct mr_i2c_bus *i2c_bus, struct mr_i2c_config *config, int addr, int addr_bits); + int (*configure)(struct mr_i2c_bus *i2c_bus, + struct mr_i2c_config *config, + int addr, + int addr_bits); void (*start)(struct mr_i2c_bus *i2c_bus); int (*send_addr)(struct mr_i2c_bus *i2c_bus, int addr, int addr_bits); void (*stop)(struct mr_i2c_bus *i2c_bus); @@ -128,6 +131,7 @@ struct mr_i2c_dev int mr_i2c_bus_register(struct mr_i2c_bus *i2c_bus, const char *path, struct mr_drv *drv); int mr_i2c_dev_register(struct mr_i2c_dev *i2c_dev, const char *path, int addr, int addr_bits); /** @} */ + #endif /* MR_USING_I2C */ #ifdef __cplusplus diff --git a/include/device/mr_pwm.h b/include/device/mr_pwm.h index 3b70715..da73283 100644 --- a/include/device/mr_pwm.h +++ b/include/device/mr_pwm.h @@ -91,8 +91,12 @@ struct mr_pwm_ops int (*write)(struct mr_pwm *pwm, int channel, uint32_t compare_value); }; -int mr_pwm_register(struct mr_pwm *pwm, const char *path, struct mr_drv *drv, struct mr_pwm_info *info); +int mr_pwm_register(struct mr_pwm *pwm, + const char *path, + struct mr_drv *drv, + struct mr_pwm_info *info); /** @} */ + #endif /* MR_USING_PWM */ #ifdef __cplusplus diff --git a/include/device/mr_serial.h b/include/device/mr_serial.h index 22a918f..6211a6e 100644 --- a/include/device/mr_serial.h +++ b/include/device/mr_serial.h @@ -102,6 +102,14 @@ struct mr_serial_config #define MR_IOC_SERIAL_GET_RD_CALL MR_IOC_GRCB /**< Get read callback command */ #define MR_IOC_SERIAL_GET_WR_CALL MR_IOC_GWCB /**< Get write callback command */ +#ifdef MR_USING_SERIAL_DMA +#define MR_IOC_SERIAL_SET_RD_DMA_BUFSZ (0x01) /**< Set read DMA buffer size command */ +#define MR_IOC_SERIAL_SET_WR_DMA_BUFSZ (0x02) /**< Set write DMA buffer size command */ + +#define MR_IOC_SERIAL_GET_RD_DMA_BUFSZ (-(0x01)) /**< Get read DMA buffer size command */ +#define MR_IOC_SERIAL_GET_WR_DMA_BUFSZ (-(0x02)) /**< Get write DMA buffer size command */ +#endif /* MR_USING_SERIAL_DMA */ + /** * @brief Serial data type. */ @@ -110,9 +118,10 @@ typedef uint8_t mr_serial_data_t; /**< Serial /** * @brief Serial ISR events. */ -#define MR_ISR_SERIAL_RD_INT (MR_ISR_RD | (0x01)) /**< Read interrupt */ -#define MR_ISR_SERIAL_WR_INT (MR_ISR_WR | (0x02)) /**< Write interrupt */ -#define MR_ISR_SERIAL_RD_IDLE (MR_ISR_RD | (0x03)) /**< Read idle */ +#define MR_ISR_SERIAL_RD_INT (MR_ISR_RD | (0x01)) /**< Read interrupt event */ +#define MR_ISR_SERIAL_WR_INT (MR_ISR_WR | (0x02)) /**< Write interrupt event */ +#define MR_ISR_SERIAL_RD_DMA (MR_ISR_RD | (0x03)) /**< Read DMA interrupt event */ +#define MR_ISR_SERIAL_WR_DMA (MR_ISR_WR | (0x04)) /**< Write DMA interrupt event */ /** * @brief Serial structure. @@ -126,6 +135,13 @@ struct mr_serial struct mr_ringbuf wr_fifo; /**< Write FIFO */ size_t rd_bufsz; /**< Read buffer size */ size_t wr_bufsz; /**< Write buffer size */ +#ifdef MR_USING_SERIAL_DMA + uint8_t *dma_rd_buf; /**< Read DMA buffer */ + uint8_t *dma_wr_buf; /**< Write DMA buffer */ + size_t dma_rd_bufsz; /**< Read DMA buffer size */ + size_t dma_wr_bufsz; /**< Write DMA buffer size */ +#endif /* MR_USING_SERIAL_DMA */ + int nonblock_state; /**< Nonblocking state */ }; /** @@ -138,10 +154,18 @@ struct mr_serial_ops int (*write)(struct mr_serial *serial, uint8_t data); void (*start_tx)(struct mr_serial *serial); void (*stop_tx)(struct mr_serial *serial); + +#ifdef MR_USING_SERIAL_DMA + void (*start_dma_tx)(struct mr_serial *serial, uint8_t *buf, size_t count); + void (*stop_dma_tx)(struct mr_serial *serial); + void (*start_dma_rx)(struct mr_serial *serial, uint8_t *buf, size_t count); + void (*stop_dma_rx)(struct mr_serial *serial); +#endif /* MR_USING_SERIAL_DMA */ }; int mr_serial_register(struct mr_serial *serial, const char *path, struct mr_drv *drv); /** @} */ + #endif /* MR_USING_SERIAL */ #ifdef __cplusplus diff --git a/include/device/mr_soft_i2c.h b/include/device/mr_soft_i2c.h index 6377a52..e63185b 100644 --- a/include/device/mr_soft_i2c.h +++ b/include/device/mr_soft_i2c.h @@ -36,8 +36,12 @@ struct mr_soft_i2c_bus int sda_pin; /**< SDA pin */ }; -int mr_soft_i2c_bus_register(struct mr_soft_i2c_bus *soft_i2c_bus, const char *path, int scl_pin, int sda_pin); +int mr_soft_i2c_bus_register(struct mr_soft_i2c_bus *soft_i2c_bus, + const char *path, + int scl_pin, + int sda_pin); /** @} */ + #endif /* defined(MR_USING_I2C) && defined(MR_USING_SOFT_I2C) */ #ifdef __cplusplus diff --git a/include/device/mr_spi.h b/include/device/mr_spi.h index 1d01a02..c8eacce 100644 --- a/include/device/mr_spi.h +++ b/include/device/mr_spi.h @@ -86,18 +86,18 @@ struct mr_spi_transfer /** * @brief SPI control command. */ -#define MR_IOC_SPI_SET_CONFIG MR_IOC_SCFG /**< Set configuration */ -#define MR_IOC_SPI_SET_REG MR_IOC_SPOS /**< Set register */ -#define MR_IOC_SPI_SET_RD_BUFSZ MR_IOC_SRBSZ /**< Set read buffer size */ -#define MR_IOC_SPI_CLR_RD_BUF MR_IOC_CRBD /**< Clear read buffer */ -#define MR_IOC_SPI_SET_RD_CALL MR_IOC_SRCB /**< Set read callback */ -#define MR_IOC_SPI_TRANSFER (0x01) /**< Transfer */ +#define MR_IOC_SPI_SET_CONFIG MR_IOC_SCFG /**< Set configuration command */ +#define MR_IOC_SPI_SET_REG MR_IOC_SPOS /**< Set register command */ +#define MR_IOC_SPI_SET_RD_BUFSZ MR_IOC_SRBSZ /**< Set read buffer size command */ +#define MR_IOC_SPI_CLR_RD_BUF MR_IOC_CRBD /**< Clear read buffer command */ +#define MR_IOC_SPI_SET_RD_CALL MR_IOC_SRCB /**< Set read callback command */ +#define MR_IOC_SPI_TRANSFER (0x01) /**< Transfer command */ -#define MR_IOC_SPI_GET_CONFIG MR_IOC_GCFG /**< Get configuration */ -#define MR_IOC_SPI_GET_REG MR_IOC_GPOS /**< Get register */ -#define MR_IOC_SPI_GET_RD_BUFSZ MR_IOC_GRBSZ /**< Get read buffer size */ -#define MR_IOC_SPI_GET_RD_DATASZ MR_IOC_GRBDSZ /**< Get read data size */ -#define MR_IOC_SPI_GET_RD_CALL MR_IOC_GRCB /**< Get read callback */ +#define MR_IOC_SPI_GET_CONFIG MR_IOC_GCFG /**< Get configuration command */ +#define MR_IOC_SPI_GET_REG MR_IOC_GPOS /**< Get register command */ +#define MR_IOC_SPI_GET_RD_BUFSZ MR_IOC_GRBSZ /**< Get read buffer size command */ +#define MR_IOC_SPI_GET_RD_DATASZ MR_IOC_GRBDSZ /**< Get read data size command */ +#define MR_IOC_SPI_GET_RD_CALL MR_IOC_GRCB /**< Get read callback command */ /** * @brief SPI data type. @@ -107,7 +107,7 @@ typedef uint8_t mr_spi_data_t; /**< SPI rea /** * @brief SPI ISR events. */ -#define MR_ISR_SPI_RD_INT (MR_ISR_RD | (0x01)) /**< Read interrupt */ +#define MR_ISR_SPI_RD_INT (MR_ISR_RD | (0x01)) /**< Read interrupt event */ /** * @brief SPI bus structure. @@ -156,6 +156,7 @@ struct mr_spi_dev int mr_spi_bus_register(struct mr_spi_bus *spi_bus, const char *path, struct mr_drv *drv); int mr_spi_dev_register(struct mr_spi_dev *spi_dev, const char *path, int cs_pin, int cs_active); /** @} */ + #endif /* MR_USING_SPI */ #ifdef __cplusplus diff --git a/include/device/mr_timer.h b/include/device/mr_timer.h index bccc6df..b2e4311 100644 --- a/include/device/mr_timer.h +++ b/include/device/mr_timer.h @@ -61,7 +61,7 @@ typedef uint32_t mr_timer_data_t; /**< Timer r /** * @brief Timer ISR events. */ -#define MR_ISR_TIMER_TIMEOUT_INT (MR_ISR_RD | (0x01)) /**< Timeout interrupt */ +#define MR_ISR_TIMER_TIMEOUT_INT (MR_ISR_RD | (0x01)) /**< Timeout interrupt event */ /** * @brief Timer information structure. @@ -101,7 +101,10 @@ struct mr_timer_ops uint32_t (*get_count)(struct mr_timer *timer); }; -int mr_timer_register(struct mr_timer *timer, const char *path, struct mr_drv *drv, struct mr_timer_info *info); +int mr_timer_register(struct mr_timer *timer, + const char *path, + struct mr_drv *drv, + struct mr_timer_info *info); /** @} */ #endif /* MR_USING_TIMER */ diff --git a/include/mr_def.h b/include/mr_def.h index c46ca44..69401e4 100644 --- a/include/mr_def.h +++ b/include/mr_def.h @@ -230,9 +230,9 @@ struct mr_avl #define MR_IOC_GWBDSZ (-(0x08 << 24)) /**< Get write buffer data size command */ /* [31:24] are for interrupt flags, [23:0] can define user flags */ -#define MR_ISR_RD (0x01 << 24) /**< Read interrupt */ -#define MR_ISR_WR (0x02 << 24) /**< Write interrupt */ -#define MR_ISR_MASK (0x7f << 24) /**< Interrupt mask */ +#define MR_ISR_RD (0x01 << 24) /**< Read interrupt event */ +#define MR_ISR_WR (0x02 << 24) /**< Write interrupt event */ +#define MR_ISR_MASK (0x7f << 24) /**< Interrupt event mask */ /** * @brief Driver structure. @@ -281,8 +281,9 @@ struct mr_dev_ops */ struct mr_dev_call { - void (*fn)(int desc, void *args); - struct mr_list list; + struct mr_list list; /**< List of callbacks */ + + void (*fn)(int desc, void *args); /**< Callback function */ }; /** diff --git a/include/mr_service.h b/include/mr_service.h index 5acebf4..f7c635a 100644 --- a/include/mr_service.h +++ b/include/mr_service.h @@ -351,8 +351,7 @@ MR_INLINE size_t mr_list_get_len(struct mr_list *list) struct mr_list *node = list; size_t len = 0; - while (node->next != list) - { + while (node->next != list) { node = node->next; len++; } diff --git a/source/memory.c b/source/memory.c index 93576aa..f7b35c7 100644 --- a/source/memory.c +++ b/source/memory.c @@ -20,11 +20,7 @@ static uint8_t heap_mem[MR_CFG_HEAP_SIZE] = {0}; /**< Heap me #define MR_HEAP_BLOCK_MIN_SIZE (sizeof(struct mr_heap_block) << 1) static struct mr_heap_block heap_start = /**< Heap start block */ - { - MR_NULL, - 0, - MR_HEAP_BLOCK_FREE - }; + {MR_NULL, 0, MR_HEAP_BLOCK_FREE}; /** * @brief This function initialize the heap. @@ -46,28 +42,25 @@ static void heap_insert_block(struct mr_heap_block *block) struct mr_heap_block *block_prev = &heap_start; /* Search for the previous block */ - while (((block_prev->next != MR_NULL) && (block_prev->next < block))) - { + while (((block_prev->next != MR_NULL) && (block_prev->next < block))) { block_prev = block_prev->next; } /* Insert the block */ - if (block_prev->next != MR_NULL) - { + if (block_prev->next != MR_NULL) { /* Merge with the previous block */ - if ((void *)(((uint8_t *)block_prev) + sizeof(struct mr_heap_block) + block_prev->size) == (void *)block) - { + if ((void *)(((uint8_t *)block_prev) + sizeof(struct mr_heap_block) + block_prev->size) == + (void *)block) { block_prev->size += block->size + sizeof(struct mr_heap_block); block = block_prev; } /* Merge with the next block */ - if ((void *)(((uint8_t *)block) + sizeof(struct mr_heap_block) + block->size) == (void *)block_prev->next) - { + if ((void *)(((uint8_t *)block) + sizeof(struct mr_heap_block) + block->size) == + (void *)block_prev->next) { block->size += block_prev->next->size + sizeof(struct mr_heap_block); block->next = block_prev->next->next; - if (block != block_prev) - { + if (block != block_prev) { block_prev->next = block; block = block_prev; } @@ -75,8 +68,7 @@ static void heap_insert_block(struct mr_heap_block *block) } /* Insert the block */ - if (block != block_prev) - { + if (block != block_prev) { block->next = block_prev->next; block_prev->next = block; } @@ -97,8 +89,7 @@ MR_WEAK void *mr_malloc(size_t size) mr_interrupt_disable(); /* Check size and residual memory */ - if ((size == 0) || (size > (UINT32_MAX >> 1) || (block == MR_NULL))) - { + if ((size == 0) || (size > (UINT32_MAX >> 1) || (block == MR_NULL))) { mr_interrupt_enable(); return MR_NULL; } @@ -107,10 +98,8 @@ MR_WEAK void *mr_malloc(size_t size) size = MR_ALIGN_UP(size, 4); /* Search for and take blocks that match the criteria */ - while (block->size < size) - { - if (block->next == MR_NULL) - { + while (block->size < size) { + if (block->next == MR_NULL) { mr_interrupt_enable(); return MR_NULL; } @@ -129,8 +118,7 @@ MR_WEAK void *mr_malloc(size_t size) block->allocated = MR_HEAP_BLOCK_ALLOCATED; /* Check if we need to allocate a new block */ - if (residual > MR_HEAP_BLOCK_MIN_SIZE) - { + if (residual > MR_HEAP_BLOCK_MIN_SIZE) { struct mr_heap_block *new_block = (struct mr_heap_block *)(((uint8_t *)memory) + size); /* Set the new block information */ @@ -153,15 +141,14 @@ MR_WEAK void *mr_malloc(size_t size) */ MR_WEAK void mr_free(void *memory) { - if (memory != MR_NULL) - { - struct mr_heap_block *block = (struct mr_heap_block *)((uint8_t *)memory - sizeof(struct mr_heap_block)); + if (memory != MR_NULL) { + struct mr_heap_block *block = (struct mr_heap_block *)((uint8_t *)memory - + sizeof(struct mr_heap_block)); mr_interrupt_disable(); /* Check the block */ - if (block->allocated == MR_HEAP_BLOCK_ALLOCATED && block->size != 0) - { + if (block->allocated == MR_HEAP_BLOCK_ALLOCATED && block->size != 0) { block->allocated = MR_HEAP_BLOCK_FREE; /* Insert the free block */ @@ -181,10 +168,10 @@ MR_WEAK void mr_free(void *memory) */ MR_WEAK size_t mr_malloc_usable_size(void *memory) { - if (memory != MR_NULL) - { + if (memory != MR_NULL) { /* Get the block information */ - struct mr_heap_block *block = (struct mr_heap_block *)((uint8_t *)memory - sizeof(struct mr_heap_block)); + struct mr_heap_block *block = (struct mr_heap_block *)((uint8_t *)memory - + sizeof(struct mr_heap_block)); return block->size; } return 0; @@ -203,8 +190,7 @@ MR_WEAK void *mr_calloc(size_t num, size_t size) size_t total = num * size; void *memory = mr_malloc(total); - if (memory != MR_NULL) - { + if (memory != MR_NULL) { memset(memory, 0, total); } return memory; @@ -223,8 +209,7 @@ MR_WEAK void *mr_realloc(void *memory, size_t size) size_t old_size = mr_malloc_usable_size(memory); void *new_memory = mr_malloc(size); - if (new_memory != MR_NULL) - { + if (new_memory != MR_NULL) { memcpy(new_memory, memory, old_size); mr_free(memory); } diff --git a/source/service.c b/source/service.c index 7481518..4ef5f29 100644 --- a/source/service.c +++ b/source/service.c @@ -26,8 +26,7 @@ MR_INIT_EXPORT(end, "5.end"); void mr_auto_init(void) { /* Auto-initialization */ - for (const mr_init_fn_t *fn = &_mr_auto_init_start; fn < &_mr_auto_init_end; fn++) - { + for (const mr_init_fn_t *fn = &_mr_auto_init_start; fn < &_mr_auto_init_end; fn++) { (*fn)(); } } @@ -63,8 +62,7 @@ MR_WEAK void mr_delay_us(uint32_t us) #else #define MR_DELAY_COUNT (1) #endif /* (MR_CFG_SYSCLK_FREQ > 1000000) */ - for (volatile uint32_t i = 0; i < us * MR_DELAY_COUNT; i++) - { + for (volatile uint32_t i = 0; i < us * MR_DELAY_COUNT; i++) { __asm__("nop"); } #undef MR_DELAY_COUNT @@ -78,8 +76,7 @@ MR_WEAK void mr_delay_us(uint32_t us) */ MR_WEAK void mr_delay_ms(uint32_t ms) { - for (volatile uint32_t i = 0; i < ms; i++) - { + for (volatile uint32_t i = 0; i < ms; i++) { mr_delay_us(1000); } } @@ -97,8 +94,7 @@ MR_WEAK int mr_printf_output(const char *buf, size_t size) static int desc = -1; /* Try to open the serial port */ - if (desc == -1) - { + if (desc == -1) { #ifndef MR_CFG_PRINTF_DEV_NAME #define MR_CFG_PRINTF_DEV_NAME "serial1" #endif /* MR_CFG_PRINTF_DEV_NAME */ @@ -107,8 +103,7 @@ MR_WEAK int mr_printf_output(const char *buf, size_t size) #else int ret = mr_dev_open(MR_CFG_PRINTF_DEV_NAME, MR_O_RDWR | MR_O_NONBLOCK); #endif /* MR_USING_PRINTF_NONBLOCKING */ - if (ret < 0) - { + if (ret < 0) { return ret; } desc = ret; @@ -152,8 +147,7 @@ int mr_printf(const char *fmt, ...) */ const char *mr_strerror(int err) { - switch (err) - { + switch (err) { case MR_EOK: return "ok"; case MR_ENOMEM: @@ -191,16 +185,13 @@ const char *mr_strflags(int flags) static char str[6] = {0}; strcpy(str, "-----"); - if (MR_BIT_IS_SET(flags, MR_O_RDONLY)) - { + if (MR_BIT_IS_SET(flags, MR_O_RDONLY)) { str[1] = 'r'; } - if (MR_BIT_IS_SET(flags, MR_O_WRONLY)) - { + if (MR_BIT_IS_SET(flags, MR_O_WRONLY)) { str[2] = 'w'; } - if (MR_BIT_IS_SET(flags, MR_O_NONBLOCK)) - { + if (MR_BIT_IS_SET(flags, MR_O_NONBLOCK)) { str[3] = 'n'; } return str; @@ -238,25 +229,21 @@ int mr_ringbuf_allocate(struct mr_ringbuf *ringbuf, size_t size) { MR_ASSERT(ringbuf != MR_NULL); - if (size == mr_ringbuf_get_bufsz(ringbuf)) - { + if (size == mr_ringbuf_get_bufsz(ringbuf)) { mr_ringbuf_reset(ringbuf); return MR_EOK; } - /* Free old buffer */ - if (ringbuf->size != 0) - { - mr_free(ringbuf->buffer); - mr_ringbuf_init(ringbuf, MR_NULL, 0); - } - /* Allocate new buffer */ void *pool = mr_malloc(size); - if (pool == MR_NULL && size != 0) - { + if ((pool == MR_NULL) && (size != 0)) { return MR_ENOMEM; } + + /* Free old buffer */ + if (ringbuf->size != 0) { + mr_free(ringbuf->buffer); + } mr_ringbuf_init(ringbuf, pool, size); return MR_EOK; } @@ -301,22 +288,17 @@ size_t mr_ringbuf_get_data_size(struct mr_ringbuf *ringbuf) MR_ASSERT(ringbuf != MR_NULL); /* Empty or full according to the mirror flag */ - if (ringbuf->read_index == ringbuf->write_index) - { - if (ringbuf->read_mirror == ringbuf->write_mirror) - { + if (ringbuf->read_index == ringbuf->write_index) { + if (ringbuf->read_mirror == ringbuf->write_mirror) { return 0; - } else - { + } else { return ringbuf->size; } } - if (ringbuf->write_index > ringbuf->read_index) - { + if (ringbuf->write_index > ringbuf->read_index) { return ringbuf->write_index - ringbuf->read_index; - } else - { + } else { return ringbuf->size - ringbuf->read_index + ringbuf->write_index; } } @@ -363,19 +345,16 @@ size_t mr_ringbuf_pop(struct mr_ringbuf *ringbuf, uint8_t *data) MR_ASSERT(data != MR_NULL); /* Get the buf size */ - if (mr_ringbuf_get_data_size(ringbuf) == 0) - { + if (mr_ringbuf_get_data_size(ringbuf) == 0) { return 0; } *data = ringbuf->buffer[ringbuf->read_index]; - if (ringbuf->read_index == ringbuf->size - 1) - { + if (ringbuf->read_index == ringbuf->size - 1) { ringbuf->read_mirror = ~ringbuf->read_mirror; ringbuf->read_index = 0; - } else - { + } else { ringbuf->read_index++; } return 1; @@ -399,20 +378,17 @@ size_t mr_ringbuf_read(struct mr_ringbuf *ringbuf, void *buffer, size_t size) /* Get the buf size */ size_t data_size = mr_ringbuf_get_data_size(ringbuf); - if (data_size == 0) - { + if (data_size == 0) { return 0; } /* Adjust the number of bytes to read if it exceeds the available buf */ - if (size > data_size) - { + if (size > data_size) { size = data_size; } /* Copy the buf from the ringbuf to the buffer */ - if ((ringbuf->size - ringbuf->read_index) > size) - { + if ((ringbuf->size - ringbuf->read_index) > size) { memcpy(read_buffer, &ringbuf->buffer[ringbuf->read_index], size); ringbuf->read_index += size; return size; @@ -441,19 +417,16 @@ size_t mr_ringbuf_push(struct mr_ringbuf *ringbuf, uint8_t data) MR_ASSERT(ringbuf != MR_NULL); /* Get the space size */ - if (mr_ringbuf_get_space_size(ringbuf) == 0) - { + if (mr_ringbuf_get_space_size(ringbuf) == 0) { return 0; } ringbuf->buffer[ringbuf->write_index] = data; - if (ringbuf->write_index == ringbuf->size - 1) - { + if (ringbuf->write_index == ringbuf->size - 1) { ringbuf->write_mirror = ~ringbuf->write_mirror; ringbuf->write_index = 0; - } else - { + } else { ringbuf->write_index++; } return 1; @@ -474,33 +447,27 @@ size_t mr_ringbuf_push_force(struct mr_ringbuf *ringbuf, uint8_t data) MR_ASSERT(ringbuf != MR_NULL); /* Get the buffer size */ - if (mr_ringbuf_get_bufsz(ringbuf) == 0) - { + if (mr_ringbuf_get_bufsz(ringbuf) == 0) { return 0; } /* Get the space size */ - if (mr_ringbuf_get_space_size(ringbuf) == 0) - { + if (mr_ringbuf_get_space_size(ringbuf) == 0) { state = 1; } ringbuf->buffer[ringbuf->write_index] = data; - if (ringbuf->write_index == ringbuf->size - 1) - { + if (ringbuf->write_index == ringbuf->size - 1) { ringbuf->write_mirror = ~ringbuf->write_mirror; ringbuf->write_index = 0; - if (state == 1) - { + if (state == 1) { ringbuf->read_mirror = ~ringbuf->read_mirror; ringbuf->read_index = ringbuf->write_index; } - } else - { + } else { ringbuf->write_index++; - if (state == 1) - { + if (state == 1) { ringbuf->read_index = ringbuf->write_index; } } @@ -525,27 +492,26 @@ size_t mr_ringbuf_write(struct mr_ringbuf *ringbuf, const void *buffer, size_t s /* Get the space size */ size_t space_size = mr_ringbuf_get_space_size(ringbuf); - if (space_size == 0) - { + if (space_size == 0) { return 0; } /* Adjust the number of bytes to write if it exceeds the available buf */ - if (size > space_size) - { + if (size > space_size) { size = space_size; } /* Copy the buf from the buffer to the ringbuf */ - if ((ringbuf->size - ringbuf->write_index) > size) - { + if ((ringbuf->size - ringbuf->write_index) > size) { memcpy(&ringbuf->buffer[ringbuf->write_index], write_buffer, size); ringbuf->write_index += size; return size; } - memcpy(&ringbuf->buffer[ringbuf->write_index], write_buffer, ringbuf->size - ringbuf->write_index); + memcpy(&ringbuf->buffer[ringbuf->write_index], + write_buffer, + ringbuf->size - ringbuf->write_index); memcpy(&ringbuf->buffer[0], &write_buffer[ringbuf->size - ringbuf->write_index], size - (ringbuf->size - ringbuf->write_index)); @@ -571,8 +537,7 @@ size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, si MR_ASSERT(ringbuf != MR_NULL); MR_ASSERT((buffer != MR_NULL) || (size == 0)); - if ((mr_ringbuf_get_bufsz(ringbuf) == 0) || (size == 0)) - { + if ((mr_ringbuf_get_bufsz(ringbuf) == 0) || (size == 0)) { return 0; } @@ -580,25 +545,24 @@ size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, si size_t space_size = mr_ringbuf_get_space_size(ringbuf); /* If the buf exceeds the buffer space_size, the front buf is discarded */ - if (size > ringbuf->size) - { + if (size > ringbuf->size) { write_buffer = &write_buffer[size - ringbuf->size]; size = ringbuf->size; } /* Copy the buf from the buffer to the ringbuf */ - if ((ringbuf->size - ringbuf->write_index) > size) - { + if ((ringbuf->size - ringbuf->write_index) > size) { memcpy(&ringbuf->buffer[ringbuf->write_index], write_buffer, size); ringbuf->write_index += size; - if (size > space_size) - { + if (size > space_size) { ringbuf->read_index = ringbuf->write_index; } return size; } - memcpy(&ringbuf->buffer[ringbuf->write_index], write_buffer, ringbuf->size - ringbuf->write_index); + memcpy(&ringbuf->buffer[ringbuf->write_index], + write_buffer, + ringbuf->size - ringbuf->write_index); memcpy(&ringbuf->buffer[0], &write_buffer[ringbuf->size - ringbuf->write_index], size - (ringbuf->size - ringbuf->write_index)); @@ -606,10 +570,8 @@ size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, si ringbuf->write_mirror = ~ringbuf->write_mirror; ringbuf->write_index = size - (ringbuf->size - ringbuf->write_index); - if (size > space_size) - { - if (ringbuf->write_index <= ringbuf->read_index) - { + if (size > space_size) { + if (ringbuf->write_index <= ringbuf->read_index) { ringbuf->read_mirror = ~ringbuf->read_mirror; } @@ -620,8 +582,7 @@ size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, si static int mr_avl_get_height(struct mr_avl *node) { - if (node == MR_NULL) - { + if (node == MR_NULL) { return -1; } return node->height; @@ -629,8 +590,7 @@ static int mr_avl_get_height(struct mr_avl *node) static int mr_avl_get_balance(struct mr_avl *node) { - if (node == MR_NULL) - { + if (node == MR_NULL) { return 0; } return (mr_avl_get_height(node->left_child) - mr_avl_get_height(node->right_child)); @@ -643,7 +603,8 @@ static void mr_avl_left_rotate(struct mr_avl **node) (*node)->right_child = right_child->left_child; right_child->left_child = (*node); - (*node)->height = MR_MAX(mr_avl_get_height((*node)->left_child), mr_avl_get_height((*node)->right_child)) + 1; + (*node)->height = MR_MAX(mr_avl_get_height((*node)->left_child), + mr_avl_get_height((*node)->right_child)) + 1; right_child->height = MR_MAX(mr_avl_get_height(right_child->left_child), mr_avl_get_height(right_child->right_child)) + 1; (*node) = right_child; @@ -656,7 +617,8 @@ static void mr_avl_right_rotate(struct mr_avl **node) (*node)->left_child = left_child->right_child; left_child->right_child = (*node); - (*node)->height = MR_MAX(mr_avl_get_height((*node)->left_child), mr_avl_get_height((*node)->right_child)) + 1; + (*node)->height = MR_MAX(mr_avl_get_height((*node)->left_child), + mr_avl_get_height((*node)->right_child)) + 1; left_child->height = MR_MAX(mr_avl_get_height(left_child->left_child), mr_avl_get_height(left_child->right_child)) + 1; (*node) = left_child; @@ -689,46 +651,39 @@ void mr_avl_insert(struct mr_avl **tree, struct mr_avl *node) MR_ASSERT(tree != MR_NULL); MR_ASSERT(node != MR_NULL); - if ((*tree) == MR_NULL) - { + if ((*tree) == MR_NULL) { (*tree) = node; } - if (node->value < (*tree)->value) - { + if (node->value < (*tree)->value) { mr_avl_insert(&(*tree)->left_child, node); - } else if (node->value > (*tree)->value) - { + } else if (node->value > (*tree)->value) { mr_avl_insert(&(*tree)->right_child, node); - } else - { + } else { return; } - (*tree)->height = MR_MAX(mr_avl_get_height((*tree)->left_child), mr_avl_get_height((*tree)->right_child)) + 1; + (*tree)->height = MR_MAX(mr_avl_get_height((*tree)->left_child), + mr_avl_get_height((*tree)->right_child)) + 1; int balance = mr_avl_get_balance((*tree)); - if (balance > 1 && node->value < (*tree)->left_child->value) - { + if (balance > 1 && node->value < (*tree)->left_child->value) { mr_avl_right_rotate(&(*tree)); return; } - if (balance < -1 && node->value > (*tree)->right_child->value) - { + if (balance < -1 && node->value > (*tree)->right_child->value) { mr_avl_left_rotate(&(*tree)); return; } - if (balance > 1 && node->value > (*tree)->left_child->value) - { + if (balance > 1 && node->value > (*tree)->left_child->value) { mr_avl_left_rotate(&(*tree)->left_child); mr_avl_right_rotate(&(*tree)); return; } - if (balance < -1 && node->value < (*tree)->right_child->value) - { + if (balance < -1 && node->value < (*tree)->right_child->value) { mr_avl_right_rotate(&(*tree)->right_child); mr_avl_left_rotate(&(*tree)); return; @@ -746,27 +701,21 @@ void mr_avl_remove(struct mr_avl **tree, struct mr_avl *node) MR_ASSERT(tree != MR_NULL); MR_ASSERT(node != MR_NULL); - if (*tree == MR_NULL) - { + if (*tree == MR_NULL) { return; } - if (node->value < (*tree)->value) - { + if (node->value < (*tree)->value) { mr_avl_remove(&(*tree)->left_child, node); - } else if (node->value > (*tree)->value) - { + } else if (node->value > (*tree)->value) { mr_avl_remove(&(*tree)->right_child, node); - } else - { - if ((*tree)->left_child == MR_NULL) - { + } else { + if ((*tree)->left_child == MR_NULL) { struct mr_avl *temp = (*tree)->right_child; (*tree)->right_child = MR_NULL; (*tree) = temp; return; - } else if ((*tree)->right_child == MR_NULL) - { + } else if ((*tree)->right_child == MR_NULL) { struct mr_avl *temp = (*tree)->left_child; (*tree)->left_child = MR_NULL; (*tree) = temp; @@ -779,28 +728,25 @@ void mr_avl_remove(struct mr_avl **tree, struct mr_avl *node) return; } - (*tree)->height = MR_MAX(mr_avl_get_height((*tree)->left_child), mr_avl_get_height((*tree)->right_child)) + 1; + (*tree)->height = MR_MAX(mr_avl_get_height((*tree)->left_child), + mr_avl_get_height((*tree)->right_child)) + 1; int balance = mr_avl_get_balance(*tree); - if (balance > 1 && mr_avl_get_balance((*tree)->left_child) >= 0) - { + if (balance > 1 && mr_avl_get_balance((*tree)->left_child) >= 0) { mr_avl_right_rotate(tree); } - if (balance > 1 && mr_avl_get_balance((*tree)->left_child) < 0) - { + if (balance > 1 && mr_avl_get_balance((*tree)->left_child) < 0) { mr_avl_left_rotate(&(*tree)->left_child); mr_avl_right_rotate(tree); } - if (balance < -1 && mr_avl_get_balance((*tree)->right_child) <= 0) - { + if (balance < -1 && mr_avl_get_balance((*tree)->right_child) <= 0) { mr_avl_left_rotate(tree); } - if (balance < -1 && mr_avl_get_balance((*tree)->right_child) > 0) - { + if (balance < -1 && mr_avl_get_balance((*tree)->right_child) > 0) { mr_avl_right_rotate(&(*tree)->right_child); mr_avl_left_rotate(tree); } @@ -816,21 +762,17 @@ void mr_avl_remove(struct mr_avl **tree, struct mr_avl *node) */ struct mr_avl *mr_avl_find(struct mr_avl *tree, uint32_t value) { - if (tree == MR_NULL) - { + if (tree == MR_NULL) { return tree; } - if (tree->value == value) - { + if (tree->value == value) { return tree; } - if (value < tree->value) - { + if (value < tree->value) { return mr_avl_find(tree->left_child, value); - } else if (value > tree->value) - { + } else if (value > tree->value) { return mr_avl_find(tree->right_child, value); } return MR_NULL; @@ -847,18 +789,15 @@ size_t mr_avl_get_length(struct mr_avl *tree) { size_t length = 1; - if (tree == MR_NULL) - { + if (tree == MR_NULL) { return 0; } - if (tree->left_child != MR_NULL) - { + if (tree->left_child != MR_NULL) { length += mr_avl_get_length(tree->left_child); } - if (tree->right_child != MR_NULL) - { + if (tree->right_child != MR_NULL) { length += mr_avl_get_length(tree->right_child); } return length;