void mpipe_setspeed(mpipe_speed speed) { ot_u8* baud_data; /// You will need to change this baud rate matrix if /// you change the bit clock from SMCLK @ 2.496 MHz /// Order is [ BR0, BR1, MCTL, ... ] /// BR1:0 = Floor(BitCLK/BaudRate) /// MCTL = Round(((BitCLK/BaudRate) - Floor(BitCLK/BaudRate)) * 8) << 1 static const ot_u8 baud_matrix[] = { 0x04, 0x01, 0x00, 0, \ 0x56, 0x00, 0x0A, 0, \ 0x2B, 0x00, 0x06, 0, \ 0x15, 0x00, 0x0A, 0 }; # if (MCU_FEATURE(MPIPEDMA) == ENABLED) MPIPE_DMAEN(OFF); # else # error "Mpipe requires a DMA in this implementation" # endif UART_CLOSE(); // Parity off, LSB-first, 8N1, async uart // Use SMCLK and keep UART reset MPIPE_UART->CTL0 = 0; MPIPE_UART->CTL1 = 0x81; //MPIPE_UART->IE = 0; //MPIPE_UART->IFG = 0; baud_data = (ot_u8*)&baud_matrix[speed << 2]; MPIPE_UART->BR0 = *baud_data++; MPIPE_UART->BR1 = *baud_data++; MPIPE_UART->MCTL = *baud_data; }
/*主函数体*/ int main(int argc, char **argv) { int fd; //文件描述符 int err; //返回调用函数的状态 int len; int i; char rcv_buf[200]; char send_buf[30]="**This's a GPS Locator!**\n"; int listen_fd,accept_fd; struct sockaddr_in client_addr; int n; // int nbytes; if(argc != 3){ printf("Usage: %s /dev/ttyS* s-Send/r-Receive\n",argv[0]); return FALSE; } fd = UART_OPEN(fd,argv[1]); //打开串口,返回文件描述符 do{ err = UART_DEFAULT(fd); }while(FALSE == err || FALSE == fd); printf("Port is READY!\n"); if((listen_fd=socket(AF_INET,SOCK_STREAM,0))<0) //创建socket { printf("Socket Error:%s\n\a",strerror(errno)); return FALSE; } bzero(&client_addr,sizeof(struct sockaddr_in)); client_addr.sin_family=AF_INET; client_addr.sin_port=htons(MY_PORT); client_addr.sin_addr.s_addr=htonl(INADDR_ANY); n=1; setsockopt(listen_fd,SOL_SOCKET,SO_REUSEADDR,&n,sizeof(int)); //如果服务器终止后,服务器可以第二次快速启动而不用等待一段时间 if(bind(listen_fd,(struct sockaddr *)&client_addr,sizeof(client_addr))<0) //Bind { printf("Bind Error:%s\n\a",strerror(errno)); return FALSE; } listen(listen_fd,MAX_CONN_NUM); if(0 == strcmp(argv[2],"s")) { for(i = 0;i < 50;i++) { len = UART_SEND(fd,send_buf,30); if(len > 0) printf("Count.%d :Send data successful!\n",i); else printf("Count.%d :Send data failed!\n",i); sleep(1); } } else if(0 == strcmp(argv[2],"r")) { while (1) //循环读取数据 { accept_fd=accept(listen_fd,NULL,NULL); if((accept_fd<0)&&(errno==EINTR)) continue; else if(accept_fd<0) { printf("Accept Error:%s\n\a",strerror(errno)); continue; } if((n=fork())==0) { /* 子进程处理客户端的连接 */ unsigned int i; for(i=0;i<200;i++){ len = UART_RECV(fd, rcv_buf,MAX_REC_BUFF); if(len > 0) write(accept_fd,rcv_buf,len); else printf("(Time limit)No data now!\n"); } close(accept_fd); exit(0); } else if(n<0) printf("Fork Error:%s\n\a",strerror(errno)); } } else { fprintf(stderr,"Unsupported char:'s' or 'r'\n"); return (FALSE); } UART_CLOSE(fd); close(listen_fd); return 0; }
void mpipe_isr() { /// MPipe is state-based. Depending on the MPipe implementation and the HW /// implementation of the DMA+UART, state transitions may happen differently. /// <LI> In typical RX, there is a header detection event that sets-up a second /// RX process for downloading the rest of the packet. When the DMA is /// done, the process completes. </LI> /// <LI> For TX, there is a wait-state needed while the HW UART finishes /// sending the DMA buffered data (two bytes). </LI> /// <LI> If MPipe does not have HW acks, then software can be used to manage /// Acks. In this case, a complete TX process also requires RX'ing an /// Ack, and a complete RX process requires TX'ing an Ack. </LI> switch (mpipe.state) { case MPIPE_Idle: //note, case doesn't break! # if ((OT_FEATURE(MPIPE_CALLBACKS) == ENABLED) && !defined(EXTF_mpipe_sig_rxdetect)) mpipe.sig_rxdetect(0); # elif defined(EXTF_mpipe_sig_rxdetect) mpipe_sig_rxdetect(0); # endif case MPIPE_RxHeader: ///@note DMA doesn't seem to need intermediate disabling here mpipe.state = MPIPE_RxPayload; mpipe_alp.inq->length = mpipe_alp.inq->front[2] + 10; mpipe_alp.inq->back = (mpipe_alp.inq->front+6) + mpipe_alp.inq->front[2]; MPIPE_DMA_RXCONFIG( (mpipe_alp.inq->front+6), \ mpipe_alp.inq->front[2]+4, \ ON); return; case MPIPE_RxPayload: # if (MPIPE_USE_ACKS) // ACKs must be used when Broadcast mode is off if (mpipe.priority != MPIPE_Broadcast) { // 1. On ACKs, txndef() requires caller to choose state // 2. Copy RX'ed sequence number into local sequence number // 3. Copy NACK/ACK status to 6th byte in NDEF header { ot_u8* scratch; mpipe.state = MPIPE_TxAck_Done; //MPIPE_TxAck_Wait; scratch = \ mpipe_alp.inq->front[mpipe_alp.inq->length-MPIPE_FOOTERBYTES]; mpipe.sequence.ubyte[UPPER] = *scratch++; mpipe.sequence.ubyte[LOWER] = *scratch; } sub_txack_header(); if (platform_crc_block(mpipe_alp.inq->front, mpipe_alp.inq->length)) { mpipe.outq->front[5] = 0x7F; } mpipe_txndef(NULL, False, MPIPE_Ack); return; } # endif break; //goto mpipe_isr_RXDONE; //case MPIPE_TxAck_Wait: //MPIPE_UART->IE = UCTXIE; //return; case MPIPE_TxAck_Done: // TX'ed an ACK //MPIPE_UART->IE = 0; # if (MPIPE_USE_ACKS) if (mpipe_alp.outq->front[5] != 0) { // TX'ed a NACK mpipe_rxndef(NULL, False, mpipe.priority); mpipe.state = MPIPE_RxHeader; return; } # endif break; //goto mpipe_isr_RXDONE; //case MPIPE_Tx_Wait: //MPIPE_UART->IE = UCTXIE; //return; case MPIPE_Tx_Done: //MPIPE_UART->IE = 0; # if (MPIPE_USE_ACKS) if (mpipe.priority != MPIPE_Broadcast) { mpipe_rxndef(NULL, False, MPIPE_Ack); mpipe.state = MPIPE_RxAck; return; } goto mpipe_isr_TXDONE; // Broadcast, so no ACK # endif case MPIPE_RxAck: # if (MPIPE_USE_ACKS) if (platform_crc_block(mpipe_alp.inq->front, 10) != 0) { //RX'ed NACK mpipe_txndef(NULL, False, mpipe.priority); break; } # endif goto mpipe_isr_TXDONE; //RX'ed ACK default: mpipe_kill(); return; } // This is a stack-less RX-Done subroutine mpipe_isr_RXDONE: UART_CLOSE(); mpipe.state = MPIPE_Idle; # if (MPIPE_USE_ACKS) mpipe.priority = MPIPE_Low; # endif # if ((OT_FEATURE(MPIPE_CALLBACKS) == ENABLED) && !defined(EXTF_mpipe_sig_rxdone)) mpipe.sig_rxdone(0); # elif defined(EXTF_mpipe_sig_rxdone) mpipe_sig_rxdone(0); # else otapi_ndef_proc(NULL); # endif return; // This is a stack-less TX-Done subroutine mpipe_isr_TXDONE: UART_CLOSE(); mpipe.sequence.ushort++; //increment sequence on TX Done mpipe.state = MPIPE_Idle; //if (mpipe_alp.outq->getcursor == mpipe_alp.outq->putcursor) { q_empty(mpipe_alp.outq); mpipe_alp.outq->back -= 10; //} # if ((OT_FEATURE(MPIPE_CALLBACKS) == ENABLED) && !defined(EXTF_mpipe_sig_txdone)) mpipe.sig_txdone(0); # elif defined(EXTF_mpipe_sig_txdone) mpipe_sig_txdone(0); # else otapi_ndef_idle(NULL); # endif }