void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else if (DataRdyUSART()) { uc_ptr->buffer[uc_ptr->buflen] = ReadUSART(); #endif uc_ptr->buflen++; // check if a message should be sent if (uc_ptr->buflen == MAXUARTBUF) { ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer); uc_ptr->buflen = 0; } } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->buflen = 0; }
void uart_recv_int_handler() { unsigned char echo[3]; unsigned char readin[23]; if (DataRdy1USART()) { readin[uc_ptr->msg_count] = Read1USART(); // check if a message should be sent // if (readin[5] == 0xFF) { if(readin[uc_ptr->msg_count] == 0xFF) { // readin[0] = 0x1; // ToMainLow_sendmsg(uc_ptr->msg_count, MSGT_UART_DATA, (void *) readin); // ToMainLow_sendmsg(6, MSGT_UART_DATA, test); SensorData_sendmsg(23, MSGT_I2C_RQST, readin); //SensorData_sendmsg(22, MSGT_I2C_RQST, test); // echo[0] = 0x33; // echo[1] = readin[0]; // echo[2] = 0xFF; // uart_send(3, echo); uc_ptr->msg_count = 0; } else { uc_ptr->msg_count++; } } if (USART1_Status.OVERRUN_ERROR == 1) { // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } }
void uart_recv_int_handler() { #ifdef __USE18F46J50 if (DataRdy1USART()) { // UART state machine uart_recv_state(Read1USART()); } if (USART1_Status.OVERRUN_ERROR == 1) { // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } #else if (DataRdyUSART()) { // UART state machine uart_recv_state(ReadUSART()); } if (USART_Status.OVERRUN_ERROR == 1) { // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } #endif }
void rx_routine (void) { //Alustetaan muuttuja //charracterille unsigned char character; //Luetaan merkki UART- //vaylasta character = Read1USART(); //Jos merkki oli g, sytytetaan //ledi ja kaiutetaan. //Jos merkki oli h, sammutetaan //ledi ja kaiutetaan. //Muissa tapauksissa vain //kaiutetaan if (character == 'g') { LED_PWR = 1; printf("%c",character); } else if (character == 'h') { LED_PWR = 0; printf("%c",character); } else { printf("%c",character); } //Nollataan keskeytysbitti PIR1bits.RCIF = 0; }
/************************************************************************ DoApplicationTasks This is the main application code that executes upon each power on and wake up. It handles displaying a menu of choices while updating the current date/time display on the serial port. If the user presses the RB0/INT0 button, the application will exit and Deep Sleep mode will be entered by the main() function above. Parameters: None Return: None Side Effects: None ************************************************************************/ void DoApplicationTasks(void) { unsigned char exit = 0; PrintMenu(); LATDbits.LATD7 = 1; // turn on LED PIE1bits.RC1IE = 1; // enable USART receive interrupt (for Sleep wakeup) RB0ReleasedWait(); // Enter IDLE mode on Sleep() instruction. Idle mode is used here so // that the Timer 0 and UART can continue operating while the // processor is powered down. OSCCONbits.IDLEN = 1; while (exit == 0) { // wait for user to make a selection EnableINT0(); EnableRTCCAlarmEverySecond(); RCSTA1bits.CREN = 0; // reset UART continuous receive to avoid overrun state RCSTA1bits.CREN = 1; Sleep(); while (DataRdy1USART()) { switch (Read1USART()) { case '1': RTCCFGbits.RTCEN ^= 1; PrintMenu(); break; case '2': SetDate(); PrintMenu(); break; case '3': SetTime(); PrintMenu(); break; case '4': exit = 1; break; } } if (RTCCFGbits.RTCEN) { PrintRTCC(); } if (INTCONbits.INT0IF) { printf("\r\nRB0/INT0 power down requested."); exit = 1; } } DisableRTCCAlarm(); printf("\r\n"); LATDbits.LATD7 = 0; // turn off LED }
char UART1_Read() { while(!UART1_Data_Ready()); //Waits for Recpetion to complete return Read1USART(); //Returns the 8 bit data /* while(!PIE1bits.RC1IE); //Waits for Recpetion to complete return RCREG1; //Returns the 8 bit data */ }
/************************************************************************ ReadBCD Reads a two digit number from the serial port and stores it in Binary Coded Decimal (BCD) formatted byte return value. Parameters: None Return: Two digit number entered from UART, stored in BCD form. Side Effects: None ************************************************************************/ unsigned char ReadBCD(void) { unsigned char byte = 0; unsigned char bcd; while (byte < '0' || byte > '9') { while (DataRdy1USART() == 0); byte = Read1USART(); } Write1USART(byte); byte -= '0'; bcd = byte << 4; while (byte < '0' || byte > '9') { while (DataRdy1USART() == 0); byte = Read1USART(); } Write1USART(byte); byte -= '0'; bcd |= byte; return bcd; }
void Uart1_ReceiveHandler(void) { volatile UINT8 data; // Get the character received from the UART #if(defined __18F8722_H) ||(defined __18F46K22_H) data = Read1USART(); #else data = ReadUSART(); #endif uart[0].rxBuff[uart[0].rxBuffIndex++] = data; if( uart[0].rxBuffIndex >= RX_PACKET_SIZE) { uart[0].rxBuffIndex = 0; } uart[0].rxDataCount++; // Clear the interrupt flag PIR1bits.RC1IF = 0; }
void uart_rx_int_handler() { unsigned char uart_msg_buf[1]; if (DataRdy1USART()) { uart_msg_buf[0] = Read1USART(); //ToMainLow_sendmsg(1, MSGT_UART_DATA, (void *) uart_msg_buf); } if (RCSTA1bits.OERR == 1) { RCSTA1bits.CREN = 0; RCSTA1bits.CREN = 1; // LATB = 0xFF; } }
void UartReceiveHandler(void) { volatile UINT8 data; // Get the character received from the UART data = Read1USART(); uart.rxBuff[uart.rxBuffIndex++] = data; if( uart.rxBuffIndex >= RX_PACKET_SIZE) { uart.rxBuffIndex = 0; } uart.rxDataCount++; // Clear the interrupt flag PIR1bits.RCIF = 0; }
void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else if (DataRdyUSART()) { uc_ptr->buffer[uc_ptr->buflen] = ReadUSART(); #endif #endif uc_ptr->buflen++; // check if a message should be sent if (uc_ptr->buflen == MAXUARTBUF) { ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer); uc_ptr->buflen = 0; } ToMainLow_sendmsg(uc_ptr->buflen, MSGT_UART_DATA, (void *) uc_ptr->buffer); } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void uart_trans_int_handler() { unsigned char msgbuffer[MSGLEN+1]; unsigned char msgtype; signed char length; unsigned char start; length = SensorData_recvmsg(start, MSGLEN, &msgtype, (void *) msgbuffer); //unsigned char msg[6]; //msg[0] = 0x0; // i = i << 1; if (msgtest[0] == 0xFF) { msgtest[0] = 0x0; } msgtest[0]++; msgtest[1] = 0xA; msgtest[2] = 0x00; msgtest[3] = msgtest[0]; msgtest[4] = 0xFF; msgtest[5] = 0x0; // if (length == 0) { // PIE1bits.TX1IE = 0x0; // } TXREG1 = msgtest[index]; //i++; index++; //FromMainHigh_recvmsg() if (index == 6) { index = 0; msgtest[0] = msgtest[0] - 0x5; PIE1bits.TX1IE = 0x0; } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->buflen = 0; } void uart_send(int length, unsigned char *msg_buffer) { int i = 0; for (i; i < length; i++) { //TXREG1 = msg_buffer[i]; uc_ptr->buffer[i] = msg_buffer[i]; } PIE1bits.TX1IE = 0x0; }
void uart_recv_wifly_debug_handler(){ #ifdef __USE18F46J50 if (DataRdy1USART()) { unsigned char last = Read1USART(); #else if (DataRdyUSART()) { unsigned char last = ReadUSART(); #endif static unsigned char cur = 0; if (last == endmsg[cur]) { cur++; if(cur >= sizeof endmsg - 1) { //-1 for null terminated wifly_setup = 1; } } else{ cur = 0; } } } void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { unsigned char recv = Read1USART(); #else if (DataRdyUSART()) { unsigned char recv = ReadUSART(); #endif #endif int pos = uc_ptr->buflen++; uc_ptr->buffer[pos] = recv; //We recieved the last byte of data //Check the 5th byte recieved for payload length if(pos == PAYLOADLEN_POS){ payload_length = recv; } // Get checksum byte if(pos == CHECKSUM_POS){ checksum_recv_value = recv; } // Count any other byte other than checksum else{ checksum_calc_value += recv; } // check if a message should be sent if (pos == payload_length+HEADER_MEMBERS-1){ pos++; if(checksum_calc_value == checksum_recv_value){ FromUARTInt_sendmsg(pos, MSGT_UART_DATA, (void *) uc_ptr->buffer); } else{ //Invalid Checksum FromUARTInt_sendmsg(pos, MSGT_UART_RECV_FAILED, (void *) uc_ptr->buffer); } //Clean up for next packet uc_ptr->buflen = 0; payload_length = 0; checksum_recv_value = 0; checksum_calc_value = 0; #ifdef __USE18F46J50 //Read1USART(); // clears buffer and returns value to nothing #else //ReadUSART(); #endif } // portion of the bytes were received or there was a corrupt byte or there was // an overflow transmitted to the buffer else if (pos >= MAXUARTBUF) { FromUARTInt_sendmsg(pos, MSGT_OVERRUN, (void *) uc_ptr->buffer); uc_ptr->buflen = 0; payload_length = 0; checksum_recv_value = 0; checksum_calc_value = 0; } } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; FromUARTInt_sendmsg(0, MSGT_OVERRUN, (void *) 0); } #ifdef __USE18F46J50 if (USART1_Status.FRAME_ERROR) { #else if (USART_Status.FRAME_ERROR) { #endif init_uart_recv(uc_ptr); } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->status = UART_IDLE; uc_ptr->buflen = 0; payload_length = 0; checksum_recv_value = 0; checksum_calc_value = 0; } void uart_send_array(char* data, char length) { #if !defined(SENSOR_PIC) && !defined(MOTOR_PIC) if(!wifly_setup) return; //just return #endif if(uc_ptr->status != UART_IDLE){ if(in_main()){ debugNum(2); if(FromMainHigh_sendmsg(length, MSGT_UART_TX_BUSY, data) == MSGQUEUE_FULL) debugNum(4); } else{ FromUARTInt_sendmsg(length, MSGT_UART_TX_BUSY, data); } return; } uc_ptr->status = UART_TX; //TODO: Create logic to prevent you from overriding the current buffer if //it has not yet been sent. uint8_t i; for(i = 0; i<length; i++) { uc_ptr->outBuff[i] = *(data + i); } uc_ptr->outLength = length; uc_ptr->outIndex = 1; #ifdef __USE18F46J50 Write1USART(uc_ptr->outBuff[0]); #else WriteUSART(uc_ptr->outBuff[0]); #endif PIR1bits.TXIF = 0; PIE1bits.TXIE = 1; } //Used to send multiple char. Will get called when uart_send_array is called //Brian says DONT DELETE! I will find you. void uart_send_int_handler() { if(uc_ptr->outLength > uc_ptr->outIndex){ uart_send(uc_ptr->outBuff[uc_ptr->outIndex++]); } else if(uc_ptr->outLength == uc_ptr->outIndex){ uc_ptr->outIndex++; //still need to increment } else //uc_ptr->outLength < uc_ptr->outIndex { // done sending message PIE1bits.TXIE = 0; PIR1bits.TXIF = 0; uc_ptr->status = UART_IDLE; } } void uart_send(char data){ //TODO possibly create logic to (without using a while) prevent writing if the buffer is not //clear #ifdef __USE18F46J50 Write1USART(data); #else WriteUSART(data); #endif }
void UART_read(char* commandPtr) { commandPtr[0] = Read1USART(); }
void uart_recv_int_handler() { // Write a byte (one character) to the usart transmit buffer. // If 9-bit mode is enabled, the 9th bit is written from the field TX_NINE, // found in a union of type USART #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { char data = Read1USART(); #else if (DataRdyUSART()) { char data = ReadUSART(); #endif #endif switch (msgtype_uart) { case MSGTYPE: uc_ptr->Rx_buffer[0] = data; uc_ptr->Rx_buflen++; msgtype_uart = LENGTH; break; case LENGTH: if (uc_ptr->Rx_buflen == 1) { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data; uc_ptr->Rx_buflen++; msgtype_uart = MESSAGE; } else { uc_ptr->Rx_buflen = 0; msgtype_uart = MSGTYPE; } break; case MESSAGE: if (uc_ptr->Rx_buflen > uc_ptr->Rx_buffer[1]) { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data; uc_ptr->Rx_buflen++; msgtype_uart = CHECKSUM; } else { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data; uc_ptr->Rx_buflen++; msgtype_uart = MESSAGE; } break; case CHECKSUM: if (uc_ptr->Rx_buflen > uc_ptr->Rx_buffer[1]) { uc_ptr->Rx_buffer[uc_ptr->Rx_buflen] = data; uc_ptr->Rx_buflen++; unsigned char checkSum = 0; unsigned char bufLength = uc_ptr->Rx_buffer[1]; // Check for correct checksum. int i = 0; for (; i < uc_ptr->Rx_buffer[1]; i++) { checkSum ^= uc_ptr->Rx_buffer[i + 2]; } if (uc_ptr->Rx_buflen != uc_ptr->Rx_buffer[1] + 3) { // Message has been scrambled. uc_ptr->Rx_buffer[0] == COMMAND_NACK; } if (checkSum != uc_ptr->Rx_buffer[uc_ptr->Rx_buflen - 1] || uc_ptr->Rx_buffer[0] == COMMAND_NACK) { // Send previous message again. ToMainHigh_sendmsg(uc_ptr->Tx_buflen, MSGT_UART_SEND, (void *) uc_ptr->Tx_buffer); } else if (uc_ptr->Rx_buffer[0] != COMMAND_ACK) { // Send sensor or motor encoder data to the ARM. ToMainHigh_sendmsg(uc_ptr->Rx_buflen, MSGT_UART_RCV, (void *) uc_ptr->Rx_buffer); // Return an ACK to the Master. ToMainLow_sendmsg(CMD_ACKLEN, MSGT_UART_SEND, (void *) uc_ptr->cmd_ack_buf); } else if (uc_ptr->Rx_buffer[0] == COMMAND_ACK) { // Allow ARM to send a new motor command. motorCommandSent = 1; } } msgtype_uart = MSGTYPE; uc_ptr->Rx_buflen = 0; break; default: break; } } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainLow_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void uart_send_int_handler() { if (uc_ptr->Tx_buflen == uc_ptr->msg_length) { PIE1bits.TX1IE = 0; // Clear TXIE to end write. uc_ptr->Tx_buflen = 0; } else { Write1USART(uc_ptr->Tx_buffer[uc_ptr->Tx_buflen++]); } } void init_uart_recv(uart_comm * uc) { uc_ptr = uc; uc_ptr->Tx_buflen = 0; uc_ptr->Rx_buflen = 0; uc_ptr->msg_length = 0; // Intialize CMD ACK response. uc_ptr->cmd_ack_buf[0] = 0x10; uc_ptr->cmd_ack_buf[1] = 0x01; uc_ptr->cmd_ack_buf[2] = 0x02; uc_ptr->cmd_ack_buf[3] = 0x02; } void uart_retrieve_buffer(int length, unsigned char* msgbuffer) { int i = 0; uc_ptr->Tx_buflen = 0; uc_ptr->msg_length = length; for (; i < length; i++) { uc_ptr->Tx_buffer[i] = msgbuffer[i]; } // Set UART TXF interrupt flag PIE1bits.TX1IE = 0x1; }
void uart_recv_int_handler() { #ifdef __USE18F26J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else #ifdef __USE18F46J50 if (DataRdy1USART()) { uc_ptr->buffer[uc_ptr->buflen] = Read1USART(); #else if (DataRdyUSART()) { buffer_temp[buf_len] = ReadUSART(); #endif #endif uc_ptr->buflen++; buf_len++; parseUART(); // check if a message should be sent } #ifdef __USE18F26J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else #ifdef __USE18F46J50 if (USART1_Status.OVERRUN_ERROR == 1) { #else if (USART_Status.OVERRUN_ERROR == 1) { #endif #endif // we've overrun the USART and must reset // send an error message for this RCSTAbits.CREN = 0; RCSTAbits.CREN = 1; ToMainHigh_sendmsg(0, MSGT_OVERRUN, (void *) 0); } } void init_uart_recv(uart_comm *uc) { uc_ptr = uc; uc_ptr->buflen = 0; buf_len = 0; command_length = 0; command_count = 0; State = GET_MSGID; } void parseUART() { unsigned char x = uc_ptr->buflen; // uart_write(1, &x); switch(State) { case(GET_MSGID): { command_length = buffer_temp[buf_len -1] & 0xF; command_length = command_length; if(command_length != 0) { State = GET_COMMAND; } else { State = CHECKSUM; } break; } case(GET_COMMAND): { if(command_count+1 < command_length) { command_count++; } else { State = CHECKSUM; } break; } case(CHECKSUM): { ToMainLow_sendmsg(buf_len ,MSGT_PARSE, &buffer_temp[0]); uc_ptr->buflen = 0; buf_len = 0; State = GET_MSGID; command_count = 0; command_length = 0; break; } } } void uart_write(unsigned char length, unsigned char *msg){ unsigned char i = 0; for(i = 0; i < length; i++){ #ifdef __USE18F26J50 while(Busy1USART()); Write1USART(msg[i]); #else #ifdef __USE18F46J50 while(Busy1USART()); Write1USART(msg[i]); #else while(BusyUSART()); WriteUSART(msg[i]); #endif #endif } }