From 4f7419c7152916fa91d83a7eaaace27114b0cd7d Mon Sep 17 00:00:00 2001 From: eddyem Date: Tue, 23 Sep 2014 12:17:35 +0400 Subject: [PATCH] modified client.c --- client-term/client.c | 197 +++++++++++++++++----------------- with_opencm3/AD7794.c | 4 + with_opencm3/hardware_ini.h | 9 ++ with_opencm3/ircontroller.bin | Bin 11500 -> 11644 bytes with_opencm3/main.c | 46 ++++---- with_opencm3/main.h | 1 + with_opencm3/onewire.c | 196 +++++++++++++++++++++++++++++++++ with_opencm3/onewire.h | 114 ++++++++++++++++++++ with_opencm3/uart.c | 63 ++++++----- with_opencm3/uart.h | 11 +- 10 files changed, 495 insertions(+), 146 deletions(-) create mode 100644 with_opencm3/onewire.c create mode 100644 with_opencm3/onewire.h diff --git a/client-term/client.c b/client-term/client.c index 3cc4bc7..5fff221 100644 --- a/client-term/client.c +++ b/client-term/client.c @@ -31,7 +31,8 @@ #include // int types #include // gettimeofday -int zmon_period = 30; // monitor temperature each 30s +#define BUFLEN 1024 + double t0; // start time FILE *fout = NULL; // file for messages duplicating @@ -70,6 +71,11 @@ void quit(int ex_stat){ * Open & setup TTY, terminal */ void tty_init(){ + // terminal without echo + tcgetattr(STDIN_FILENO, &oldt); + newt = oldt; + newt.c_lflag &= ~(ICANON | ECHO); + if(tcsetattr(STDIN_FILENO, TCSANOW, &newt) < 0) quit(-2); printf("\nOpen port...\n"); if ((comfd = open(comdev,O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0){ fprintf(stderr,"Can't use port %s\n",comdev); @@ -84,107 +90,86 @@ void tty_init(){ tty.c_cc[VMIN] = 0; // non-canonical mode tty.c_cc[VTIME] = 5; if(ioctl(comfd,TCSETA,&tty) < 0) exit(-1); // set new mode - // terminal without echo - tcgetattr(STDIN_FILENO, &oldt); - newt = oldt; - newt.c_lflag &= ~(ICANON | ECHO); - if(tcsetattr(STDIN_FILENO, TCSANOW, &newt) < 0) quit(-2); printf(" OK\n"); -} - -/** - * Read character from console without echo - * @return char readed - */ -int read_console(){ - int rb; - struct timeval tv; - int retval; - fd_set rfds; tcsetattr(STDIN_FILENO, TCSANOW, &newt); - FD_ZERO(&rfds); - FD_SET(STDIN_FILENO, &rfds); - tv.tv_sec = 0; tv.tv_usec = 10000; - retval = select(1, &rfds, NULL, NULL, &tv); - if(!retval) rb = 0; - else { - if(FD_ISSET(STDIN_FILENO, &rfds)) rb = getchar(); - else rb = 0; - } - tcsetattr(STDIN_FILENO, TCSANOW, &oldt); - return rb; } /** * getchar() without echo * wait until at least one character pressed * @return character readed - */ -int mygetchar(){ // аналог getchar() без необходимости жать Enter + * +int mygetchar(){ int ret; do ret = read_console(); while(ret == 0); return ret; -} +}*/ /** - * Read data from TTY - * @param buff (o) - buffer for data read - * @param length - buffer len - * @return amount of readed bytes + * read both tty & console + * @param buff (o) - buffer for messages readed from tty + * @param length (io) - buff's length (return readed len or 0) + * @param rb (o) - byte readed from console or -1 + * @return 1 if something was readed here or there */ -size_t read_tty(uint8_t *buff, size_t length){ - ssize_t L = 0; - fd_set rfds; +int read_tty_and_console(char *buff, size_t *length, int *rb){ + ssize_t L; struct timeval tv; - int retval; + int sel, retval = 0; + fd_set rfds; FD_ZERO(&rfds); + FD_SET(STDIN_FILENO, &rfds); FD_SET(comfd, &rfds); - tv.tv_sec = 0; tv.tv_usec = 500000; // wait for 500ms - retval = select(comfd + 1, &rfds, NULL, NULL, &tv); - if (!retval) return 0; - if(FD_ISSET(comfd, &rfds)){ - if((L = read(comfd, buff, length)) < 1) return 0; + tv.tv_sec = 0; tv.tv_usec = 10000; + sel = select(comfd + 1, &rfds, NULL, NULL, &tv); + if(sel > 0){ + if(FD_ISSET(STDIN_FILENO, &rfds)){ + *rb = getchar(); + retval = 1; + }else{ + *rb = -1; + } + if(FD_ISSET(comfd, &rfds)){ + if((L = read(comfd, buff, *length)) < 1){ // disconnect or other troubles + fprintf(stderr, "USB error or disconnected!\n"); + quit(1); + }else{ + if(L == 0){ // USB disconnected + fprintf(stderr, "USB disconnected!\n"); + quit(1); + } + *length = (size_t) L; + retval = 1; + } + }else{ + *length = 0; + } } - return (size_t)L; + return retval; } void help(){ printf("Use this commands:\n" "h\tShow this help\n" "q\tQuit\n" - "t\tMonitor temperature on ZakWire\n" - "v\tNon-verbose\n" - "z\tGet ZakWire data\n" - "V\tVerbose\n" ); } -#define dup_pr(...) do{printf(__VA_ARGS__); if(fout) fprintf(fout, __VA_ARGS__);}while(0) - -int zflag = 0; // monitor zakwire void con_sig(int rb){ - uint8_t cmd; + char cmd; if(rb < 1) return; if(rb == 'q') quit(0); // q == exit - switch(rb){ + cmd = (char) rb; + write(comfd, &cmd, 1); + /*switch(rb){ case 'h': help(); break; - case 's': - dup_pr("Stop ZakWire\n"); - zflag = 0; - break; - case 't': - dup_pr("ZakWire thermal monitoring\n"); - zflag = 1; - write(comfd, "v", 1); - t0 = dtime() - zmon_period; - break; default: cmd = (uint8_t) rb; write(comfd, &cmd, 1); - } + }*/ } /** @@ -193,11 +178,7 @@ void con_sig(int rb){ * @param len - length of data in buffer (could be 2 or 4) * @return */ -uint32_t get_int(uint8_t *buff, size_t len){ -/* int i; - printf("read %zd bytes: ", len); - for(i = 0; i < len; i++) printf("0x%x ", buff[i]); - printf("\n");*/ +uint32_t get_int(char *buff, size_t len){ if(len != 2 && len != 4){ fprintf(stdout, "Bad data length!\n"); return 0xffffffff; @@ -209,9 +190,42 @@ uint32_t get_int(uint8_t *buff, size_t len){ return data; } +/** + * Copy line by line buffer buff to file removing cmd starting from newline + * @param buffer - data to put into file + * @param cmd - symbol to remove from line startint (if found, change *cmd to (-1) + * or NULL, (-1) if no command to remove + */ +void copy_buf_to_file(char *buffer, int *cmd){ + char *buff, *line, *ptr; + if(!cmd || *cmd < 0){ + fprintf(fout, "%s", buffer); + return; + } + buff = strdup(buffer), ptr = buff; + do{ + if(!*ptr) break; + if(ptr[0] == (char)*cmd){ + *cmd = -1; + ptr++; + if(ptr[0] == '\n') ptr++; + if(!*ptr) break; + } + line = ptr; + ptr = strchr(buff, '\n'); + if(ptr){ + *ptr++ = 0; + //fprintf(fout, "%s\n", line); + }//else + //fprintf(fout, "%s", line); // no newline found in buffer + fprintf(fout, "%s\n", line); + }while(ptr); + free(buff); +} + int main(int argc, char *argv[]){ - int rb; - uint8_t buff[128]; + int rb, oldcmd = -1; + char buff[BUFLEN+1]; size_t L; if(argc == 2){ fout = fopen(argv[1], "a"); @@ -229,34 +243,19 @@ int main(int argc, char *argv[]){ setbuf(stdout, NULL); t0 = dtime(); while(1){ - rb = read_console(); - if(rb > 0) con_sig(rb); - L = read_tty(buff, 127); - if(L){ - buff[L] = 0; - printf("TTY: %s\n", buff); - if(fout) fprintf(fout, "%zd\t%s\n", time(NULL), buff); - } - if(zflag && dtime() - t0 > zmon_period){ // thermal monitoring - t0 += zmon_period; - char NUM[] = {'6', '5', '3'}; - char obuf[] = {'N', 0, 'z'}; - int i; - double temper[3]; - for(i = 0; i < 3; i++){ - obuf[1] = NUM[i]; - if(3 != write(comfd, &obuf, 3)){ - perror("Can't write"); - quit(-1); - } - L = read_tty(buff, 127); - //for(j = 0; j < 10; j++) // 10 tries to read data from USB - // if((L = read_tty(buff, 127)) < 1) continue; - uint32_t ans = get_int(buff, L); - if(ans == 0xffffffff) temper[i] = -274.; - else temper[i] = ((double)ans)/2047.*70. - 10.; + L = BUFLEN; + if(read_tty_and_console(buff, &L, &rb)){ + if(rb > 0){ + con_sig(rb); + oldcmd = rb; + } + if(L){ + buff[L] = 0; + printf("%s", buff); + if(fout){ + copy_buf_to_file(buff, &oldcmd); + } } - dup_pr("%zd\t%.2f\t%.2f\t%.2f\n", time(NULL), temper[0], temper[1], temper[2]); } } } diff --git a/with_opencm3/AD7794.c b/with_opencm3/AD7794.c index 2e092a2..cbc0445 100644 --- a/with_opencm3/AD7794.c +++ b/with_opencm3/AD7794.c @@ -213,21 +213,25 @@ uint32_t read_AD7794(uint8_t channel){ uint8_t dr; switch (N){ case 0: // start: set channel +//P("A0 ", uart1_send); if(!AD7794_set_channel(channel)){ return 0; // 0 in return means error } break; case 1: // put ADC to a single conversion mode +//P("A1 ", uart1_send); sendWord(MODE_REGISTER, SINGLE_MODE | U16(0x0f)); // the lowest speed; check_errR(); break; case 2: // wait for data reading & check errors +//P("A2", uart1_send); dr = check_data_ready(); check_errR(); if(!dr) return AD7794_NOTRDY; break; default: // last step -> return readed data +//P("\r\n", uart1_send); N = 0; return read_ADC_data(); } diff --git a/with_opencm3/hardware_ini.h b/with_opencm3/hardware_ini.h index f76bf73..57c1381 100644 --- a/with_opencm3/hardware_ini.h +++ b/with_opencm3/hardware_ini.h @@ -33,4 +33,13 @@ void SysTick_init(); void ADC_init(); void ADC_calibrate_and_start(); +/* + * One Wire interface + */ +// In case of using USART2 for 1-wire port, make corresponding change +// and redefine pins in OW_Init +//#define OW_USART_X USART2 +#define OW_USART_X USART3 + + #endif // __HARDWARE_INI_H__ diff --git a/with_opencm3/ircontroller.bin b/with_opencm3/ircontroller.bin index 6d348979beb8a9099c6c7877c2106b2b60bf7473..577916fafaaf1635078c2199607686c9df0cecae 100755 GIT binary patch delta 2737 zcmb7G4Qx}_6+ZX*#d-co;@|=Z&>N+9x+=V0@kGM z6@3Y78%T8sa1j490!V8RJa7ZftwMwmGYHU4D7PW1OhBQN_?H7vN7CLK8DAG8>|%ii z1sU9t@%Oe6c0}izzKhQh_MC1~B23>|A9bW5!=K|oNJ*dq`${4o7;EtgYXodtwED#d8lIpVH2;~E44ixxj?v&*U8F~K zvlke{Bk#t&rwfh9cR$t)LZF>7FRl8{B&5VF4YuRzh2fj#7X*^h_F9&H>BoANf{)Xl zmj$;U+vznOJfP9wVcFz%p#4MLjO((Ta0}|ady9mM zS!+l{kA+2`73Ub{ejRh~sGjP-G1M*CbG-MYMIhB997%I8kv(cf`2GL*O()M2HdEIm zTWfpLu=dg@9kQ$^Y^*?44=v%G*eN*o4a?+bfxx@G{-$02$cF+8X<8_n%7WgVtYB+U2P#Uimd-c%u6- z!99z#uiN*n94dt{)K5!(9M6<|K@-y4iMl$8p;`PcQZF011IDK+4R$kX`ZSuprFR26 zqBT_=pXo1=6`!47yCioV=8M^%JjwfFh4y~chJ>V?2-rtV6S3|S0rQAiF^7)@>={=O zXskyJO#y4~kK+3bkAQy;$~O?5$%>$n%Nka2$OIdV2E_@w6_}~W&+>~&-fRTh&5M2n{XU-b(0h$0zN-z^JVt(? zXiyfHXqXtmOy+EPaD zH-;Qvr3?_#?uxDmUpkLhLev$N5(^>>O!ka3RvYt$ol%a(xRorckHym7z7BP7Dl-1< za#HJykaA^PF_Aq*ZxmV|8LiFkZuE=-HFEvM?l~r(H@fOtv(gYhJjC}@;z5=cX{ugv z#Sl?jmV5RV>rjuSXDWWEk00^c6*~(4(`Lp#nP7W zj=I`-RXCu_UcuwR^Z>_->8ly74B*vnM{s8N6+MYkM7s&uaiElvW~E-mqeUwe7rrT$ z$#e}~*ha4BUC2o2zy$5Z8ScamgVMnsQIUf#pK3EVDA*$)dfS|PDk8uNhq`r6+n;>9b&^dgy#9cM;!YO1sn( zJyOS#JEL}Fv6P!-ymoWyv&gSDhCKZq`M!eqhi&Rp%z5MdRhfp(U)X21+-?M^rwOFX z!_U=!)ofwdz@5q7xJ|oEXOs7|3VEqzcn4bx$8a z-GVO`5zsM$IX<>X>uP&d`&(PY1aMMoU({mx2Bx?haTqOUyiEq_E?PX&Hk2N&3xF}e z{Ny~0<_76F$~u%A5c&By=K<+a#7Bs;&Hqb~$G{wcb4aF7(Tqu~MuZWBr#a(b8UF>U CMrB3- delta 2620 zcmb7GeQXrR6@Rn0KJT4ve8w1z&xYAs+Z>z&XP+I8kHFb3=ku-2nSk1ci1r$avQ-5! zL{Sd6>N$xJLiYUctGlO=&}2|4_cJgP+KoyMFz;56?@Q! zi!#lY9(NGft7c?|VqmQp7{MT1X)c?>ggsZ#Jg6N`ORo4__X~wa!gkNgz`Wd2kjUr( z!dBhUA0w=KzJF611ATbThma;RYAl>ahFGo1!}waE+*2r>g>omBl3eTp*2L>&>uo2q+L5+yGsX0^_HNI5qb=zQQLGPS#Y9_@9w~OWOx0rjOxBL8J)b5g zn>E)F!cOR??499ImQXQi4~b~ekp)_Gn&IBJdq65$Fqcm|QMX|Z@m%Uh{aJ)#+}cBA zMJo#b{${{3Gz#n!Q}>s+n-7`x9vY=1wr;}CW~gRf7fw8O=xZcd8 z4c2~%vQ(bil{e17Bo8!+{x5p;H%s@ednO-3SKrGAu>4Nm7qlSFe$@Rrbj?Dyk$Txo z9WbwHG}y_gJj3unko%hlaZN2 zeHL7OBI`}qLUwhj#%VND74J}t{|8xcPhTL4C+f%8`91RwjC}!ap@_MCM1+|>Pqe;wmA1XMatf%KI-KoX zX7RbBt(RPCU5_)C9pPSzaVa~prxPAc!H}J*Gj?iZagU-snphaFh*gC?5%9GwsK@m$Q#b5{%C^Y zLpR#(2h`PDotkgNr*&?zT==U2gJ$Y>-lsk49Ge}8mwvRo@MHn|bw*K(W(S^fBD-el zY+g!IB<@s)cyYN34RaJY)s9=`2bO9!=dljql2iLh$9os6RdKdpuN&1t)8ALMK<%PUFPt0{wDEzNJOUojErAI zjF-&4gPc9iVon%)dC@+r?U7oL-bOq$R_1)%YKvm(`p0`>CHgLT3GwR3&YhBq797e1Q+ zAP^XAS%hZ>$b4%PR_^FG(b|S5i3n)-Hg=rx=pT7rwE@3%(`6v(`)k 999){ // write out time in seconds + if(Timer - Old_timer > 999){ // one-second cycle Old_timer += 1000; gpio_toggle(GPIOC, GPIO12); // toggle LED - if(!ad7794_on) AD7794_init(); // try to init ADC + if(!ad7794_on) AD7794_init(); // try to init ADC if it doesn't work //print_int(Timer/1000, usb_send); }else if(Timer < Old_timer){ // Timer overflow Old_timer = 0; diff --git a/with_opencm3/main.h b/with_opencm3/main.h index 9a6e350..9f48d0e 100644 --- a/with_opencm3/main.h +++ b/with_opencm3/main.h @@ -39,6 +39,7 @@ #include "user_proto.h" #include "AD7794.h" +#include "onewire.h" #define _U_ __attribute__((__unused__)) #define U8(x) ((uint8_t) x) diff --git a/with_opencm3/onewire.c b/with_opencm3/onewire.c new file mode 100644 index 0000000..da5ee4c --- /dev/null +++ b/with_opencm3/onewire.c @@ -0,0 +1,196 @@ +/* + * onewire.c - functions to work with 1-wire devices + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ +#include "onewire.h" + +#define OW_0 0x00 +#define OW_1 0xff +#define OW_R 0xff +#define OW_RST 0xf0 + + +// In/Out buffer +uint8_t ow_buf[8]; +/** + * this function sends bits of ow_byte (LSB first) to 1-wire line + * @param ow_byte - byte to convert + * @param Nbits - number of bits to send + */ +void OW_SendBits(uint8_t ow_byte, uint8_t Nbits){ + uint8_t i, byte; + if(Nbits == 0) return; + if(Nbits > 8) Nbits = 8; + for(i = 0; i < Nbits; i++){ + if(ow_byte & 0x01){ + byte = OW_1; + }else{ + byte = OW_0; + } + fill_uart_buff(OW_USART_X, byte); // send next "bit" + ow_byte = ow_byte >> 1; + } +} + +/* + * Inverce conversion - read data (not more than 8 b + */ +uint8_t OW_ReadByte(){ + UART_buff *curbuff = get_uart_buffer(OW_USART_X); + uint8_t ow_byte = 0, i, L, *buf; + if(!curbuff || !(L = curbuff->end)) return 0; // no data? + if(L > 8) L = 8; // forget all other data + buf = curbuff->buf; + for(i = 0; i < L; i++, buf++){ + ow_byte = ow_byte >> 1; // prepare for next bit filling + if(*buf == OW_1){ + ow_byte |= 0x80; // MSB = 1 + } + } + return ow_byte >> (8 - L); // shift to the end: L could be != 8 ??? +} + + +/* + * Configure peripherial ports (USART2) for 1-wire + */ +void OW_Init(){ + struct usb_cdc_line_coding owlc = { + .dwDTERate = 115200, + .bCharFormat = USB_CDC_1_STOP_BITS, + .bParityType = USB_CDC_NO_PARITY, + .bDataBits = 8, + }; + UART_init(OW_USART_X); + UART_setspeed(OW_USART_X, &owlc); +} + +/* + * 1-wire reset + * Reset procedure: USART settings are 9600,8,n,1, + * send 0xf0 then check what we get + * if not 0xf0 line is busy. + * Other operations work with next USART settings: 115200,8,n,1 + * + * return 1 in case of 1-wire devices present; otherwise return 0 + */ +uint8_t OW_Reset() { + uint8_t ow_presence; + UART_buff *curbuff; + // change speed to 9600 + usart_set_baudrate(OW_USART_X, 9600); + //USART_ClearFlag(OW_USART_X, USART_FLAG_TC); + fill_uart_buff(OW_USART_X, OW_RST); // send 1 byte data + // wait for end of transmission + while(!(USART_SR(OW_USART_X) & USART_SR_TC)); + curbuff = get_uart_buffer(OW_USART_X); + if(!curbuff || !(curbuff->end)) return 0; // error reading + curbuff->end = 0; // zero counter + ow_presence = curbuff->buf[0]; + // change speed back + usart_set_baudrate(OW_USART_X, 115200); + // if there is any device on bus, it will pull it, so we'll get not 0xf0 + if(ow_presence != OW_RST){ + return 1; + } + // we get 0xf0 -> there's nothing on the bus + return 0; +} + +/* + * Procedure of 1-wire communications + * variables: + * sendReset - send RESET before transmission + * command - bytes sent to the bus (if we want to read, send OW_READ_SLOT) + * cLen - command buffer length (how many bytes to send) + * data - pointer for reading buffer (if reading needed) + * readStart - first byte to read (starts from 0) or OW_NO_READ (not read) + * + * return 1 if succeed, 0 if failure + */ +uint8_t OW_Send(uint8_t sendReset, uint8_t *command, uint8_t cLen, + uint8_t *data, uint8_t dLen, uint8_t readStart) { + // if reset needed - send RESET and check bus + if(sendReset){ + if(OW_Reset() == 0){ + return 0; + } + } + while(cLen > 0){ + OW_SendBits(*command, 8); + command++; + cLen--; + // wait for EOT + while(!(USART_SR(OW_USART_X) & USART_SR_TC)); + // put data from bus into user buffer + if(readStart == 0 && dLen > 0){ + *data = OW_ReadByte(); + data++; + dLen--; + }else{ + if(readStart != OW_NO_READ){ + readStart--; + } + } + } + return 1; +} + +/* + * scan 1-wire bus + * num - max number of devices + * buf - array for devices' ID's (8*num bytes) + * return amount of founded devices + */ +uint8_t OW_Scan(uint8_t *buf, uint8_t num) { + unsigned long path,next,pos; + uint8_t bit,chk; + uint8_t cnt_bit, cnt_byte, cnt_num; + path=0; + cnt_num=0; + do{ + //(issue the 'ROM search' command) + if( 0 == OW_WriteCmd(OW_SEARCH_ROM) ) return 0; + next=0; // next path to follow + pos=1; // path bit pointer + for(cnt_byte = 0; cnt_byte != 8; cnt_byte++){ + buf[cnt_num*8 + cnt_byte] = 0; + for(cnt_bit = 0; cnt_bit != 8; cnt_bit++){ + //(read two bits, 'bit' and 'chk', from the 1-wire bus) + OW_SendBits(OW_R, 2); + bit = OW_ReadByte(); + chk = bit & 0x02; // bit 1 + bit = bit & 0x01; // bit 0 + //bit = (ow_buf[0] == OW_1); chk = (ow_buf[1] == OW_1); + if(bit && chk) return 0; // error + if(!bit && !chk){ // collision, both are zero + if (pos & path) bit = 1; // if we've been here before + else next = (path&(pos-1)) | pos; // else, new branch for next + pos <<= 1; + } + //(save this bit as part of the current ROM value) + if (bit) buf[cnt_num*8 + cnt_byte]|=(1<0 degrC ==> T=byte0; 1 - T<0 degrC ==> T=byte0-0xff+1) + * 2 - T_H + * 3 - T_L + * 4 - 0xff (reserved) + * 5 - 0xff (reserved) + * 6 - COUNT_REMAIN (0x0c) + * 7 - COUNT PER DEGR (0x10) + * 8 - CRC + */ + + +#endif // __ONEWIRE_H__ diff --git a/with_opencm3/uart.c b/with_opencm3/uart.c index 1ab7a89..4fbf8a8 100644 --- a/with_opencm3/uart.c +++ b/with_opencm3/uart.c @@ -24,15 +24,9 @@ #include "cdcacm.h" // Buffers for Tx -typedef struct { - uint8_t buf[UART_TX_DATA_SIZE]; - uint8_t start; // index from where to start reading - uint8_t end; // index from where to start writing -} UART_buff; static UART_buff TX_buffer[3]; // Tx buffers for all three ports static UART_buff RX_buffer[3]; // Rx buffers for all three ports -void fill_uart_buff(uint32_t UART, uint8_t byte); void fill_uart_RXbuff(uint32_t UART, uint8_t byte); /** @@ -242,32 +236,49 @@ void uart3_send(uint8_t byte){ fill_uart_buff(USART3, byte); } +UART_buff *get_uart_buffer(uint32_t UART){ + switch(UART){ + case USART1: + return &RX_buffer[0]; + break; + case USART2: + return &RX_buffer[1]; + break; + case USART3: + return &RX_buffer[2]; + break; + default: // error - return + return NULL; + } + return NULL; +} + /** * Check buffers for non-empty & run parsing function + * @param UART - device to check */ -void check_and_parce_UART(){ - int i; +void check_and_parce_UART(uint32_t UART){ sendfun sf; - UART_buff *curbuff; + UART_buff *curbuff = get_uart_buffer(UART); uint8_t datalen; // length of data in buffer - here we use param "end" - for(i = 0; i < 3; i++){ - curbuff = &RX_buffer[i]; - datalen = curbuff->end; - if(!datalen) continue; // buffer is empty - // buffer isn't empty: process data in it - switch (i){ - case 0: - sf = uart1_send; - break; - case 1: - sf = uart2_send; - break; - default: - sf = uart3_send; - } - parce_incoming_buf((char*)curbuff->buf, datalen, sf); // process data - curbuff->end = 0; // and zero counter + if(!curbuff) return; + switch(UART){ + case USART1: + sf = uart1_send; + break; + case USART2: + sf = uart2_send; + break; + case USART3: + sf = uart3_send; + break; + default: // error - return + return; } + datalen = curbuff->end; + if(!datalen) return; // buffer is empty + parce_incoming_buf((char*)curbuff->buf, datalen, sf); // process data + curbuff->end = 0; // and zero counter } /** diff --git a/with_opencm3/uart.h b/with_opencm3/uart.h index 75abdca..66ea2e9 100644 --- a/with_opencm3/uart.h +++ b/with_opencm3/uart.h @@ -26,14 +26,23 @@ // Size of buffers #define UART_TX_DATA_SIZE 64 +typedef struct { + uint8_t buf[UART_TX_DATA_SIZE]; + uint8_t start; // index from where to start reading + uint8_t end; // index from where to start writing +} UART_buff; + void UART_init(uint32_t UART); void UART_setspeed(uint32_t UART, struct usb_cdc_line_coding *linecoding); +void fill_uart_buff(uint32_t UART, uint8_t byte); void uart1_send(uint8_t byte); void uart2_send(uint8_t byte); void uart3_send(uint8_t byte); -void check_and_parce_UART(); +void check_and_parce_UART(uint32_t UART); + +UART_buff *get_uart_buffer(uint32_t UART); #endif // __UART_H__