sci_send.c
00001
00002
00003
00004
00005
00006
00007 #include "sh7045lib.h"
00008 #include "ringBuffer.h"
00009
00010
00011 int putstr(int port, const unsigned char *data) {
00012 const unsigned char *p = data;
00013
00014 while (*p != '\0') {
00015 ++p;
00016 }
00017 return sci_write(port, data, p-data);
00018 }
00019
00020
00021 static void reverse(unsigned char *buf) {
00022 unsigned char *first, *last;
00023 unsigned char ch;
00024
00025 last = buf;
00026 while (*last != '\0') {
00027 last++;
00028 }
00029 last--;
00030
00031 for (first = buf; first < last; first++, last--) {
00032 ch = *first, *first = *last, *last = ch;
00033 }
00034 }
00035
00036
00037 static int iabs(int num) {
00038 if (num >= 0) {
00039 return num;
00040 } else {
00041 return -num;
00042 }
00043 }
00044
00045
00046 static int utoa(unsigned char *buf, unsigned long num, int base) {
00047 unsigned char *p = buf;
00048 unsigned int val;
00049
00050 do {
00051 val = iabs(num % base);
00052 *p++ = (val > 9) ? (val + 'a' - 10) : (val + '0') ;
00053 num /= base;
00054 } while (num != 0);
00055 *p = '\0';
00056 reverse(buf);
00057
00058 return p - buf;
00059 }
00060
00061
00062 int puthex(int port, unsigned long num, int hexwidth) {
00063 char buffer[16] = { '0', '0', '0', '0', '0', '0', '0' };
00064 int n;
00065
00066 n = utoa((unsigned char *)&buffer[7], num, 16);
00067 return sci_write(port, (const unsigned char *)&buffer[7 + n - hexwidth],
00068 ((n > hexwidth) ? n : hexwidth));
00069 }
00070
00071
00072 int putnum(int port, int num) {
00073 unsigned char buffer[12];
00074 int filled = 0;
00075 int unum = num;
00076 int n;
00077
00078 if (num < 0) {
00079 buffer[filled++] = '-';
00080 unum = -num;
00081 }
00082 n = utoa(&buffer[filled], unum, 10) + filled;
00083 return sci_write(port, buffer, n);
00084 }
00085