/**************************************************************************** * DESCRIPTION: Checks for data on the receive UART, and handles errors * RETURN: none * ALGORITHM: none * NOTES: none *****************************************************************************/ bool RS485_Check_UART_Data( volatile struct mstp_port_struct_t * mstp_port) { /* check for data */ if (!FIFO_Empty(&FIFO_Rx)) { mstp_port->DataRegister = FIFO_Get(&FIFO_Rx); mstp_port->DataAvailable = TRUE; } return (!FIFO_Empty(&FIFO_Rx)); }
/**************************************************************************** * DESCRIPTION: Gets the data from the front of the list, and removes it * RETURN: the data, or zero if nothing in the list * ALGORITHM: none * NOTES: Use Empty function to see if there is data to retrieve *****************************************************************************/ u16 FIFO_GetAr(fifo_buffer * b, u8 * data) { u16 len = 0; u16 cpsr; u16 delta = 0; if (!FIFO_Empty(b)) { cpsr = __disable_irq(); if(b->head > b->tail) { delta = (b->head - b->tail); memcpy((char *)data, (char *)&(b->buffer[b->tail]), delta); len = delta; } else { delta = (b->buffer_len - b->tail); memcpy((char *)data, (char *)&(b->buffer[b->tail]), delta); len = delta; delta = (b->head - 0); memcpy((char *)&data[len], (char *)&(b->buffer[0]), delta); len += delta; } b->tail = b->head; if(b->tail == b->buffer_len) b->tail = 0; if(!cpsr) __enable_irq(); } return len; }
void FIFO_Destroy(FIFO_QUEUE *queue) { if (!FIFO_Empty(queue)) { CmiError("Tried to FIFO_Destroy a non-empty queue.\n"); exit(1); } free(queue->block); free(queue); }
bool rs485_byte_available( uint8_t * data_register) { bool data_available = false; /* return value */ if (!FIFO_Empty(&Receive_Buffer)) { led_on(LED_1); *data_register = FIFO_Get(&Receive_Buffer); data_available = true; led_off_delay(LED_1, 10); } return data_available; }
void sendStringUsingFIFO(char* string){ while(*string){ if(interruptTurnedOn == FALSE){ turnOnInterrupts(); } if(FIFO_Put(&fifo,*string++) == FIFO_ERROR){ FIFO_Empty(&fifo); DBGU_sendString((char*)"Buffer Overflow Error\r\n"); break; } } }
/**************************************************************************** * DESCRIPTION: Return true if data is available * RETURN: true if data is available, with the data in the parameter set * ALGORITHM: none * NOTES: none *****************************************************************************/ bool RS485_DataAvailable(uint8_t * data_register,uint8_t port) { bool DataAvailable = false; if(port == 0) { if (!FIFO_Empty(&Receive_Buffer0)) { if (data_register) { *data_register = FIFO_Get(&Receive_Buffer0); } // timer_elapsed_start(&Silence_Timer); DataAvailable = true; } } return DataAvailable; }
/**************************************************************************** * DESCRIPTION: Gets the data from the front of the list, and removes it * RETURN: the data, or zero if nothing in the list * ALGORITHM: none * NOTES: Use Empty function to see if there is data to retrieve *****************************************************************************/ u8 FIFO_Get(fifo_buffer * b) { u8 data_byte = 0; u16 cpsr; if (!FIFO_Empty(b)) { data_byte = b->buffer[b->tail % b->buffer_len]; cpsr = __disable_irq(); b->tail++; if(b->tail == b->buffer_len) b->tail = 0; if(!cpsr) __enable_irq(); } return data_byte; }
/************************************************************************* * DESCRIPTION: Return true if a byte is available * RETURN: true if a byte is available, with the byte in the parameter * NOTES: none **************************************************************************/ bool rs485_byte_available( uint8_t * data_register) { bool data_available = false; /* return value */ if (!FIFO_Empty(&Receive_Buffer)) { if (data_register) { *data_register = FIFO_Get(&Receive_Buffer); } timer_elapsed_start(&Silence_Timer); data_available = true; led_rx_on_interval(10); } return data_available; }
/* ************************************************************************* DESCRIPTION: Transmits a byte using the UART out the RS485 port RETURN: none ALGORITHM: none NOTES: none *************************************************************************** */ void RS485_Interrupt_Tx( void) { if (!FIFO_Empty(&FIFO_Tx)) { TXREG2 = FIFO_Get(&FIFO_Tx); } else { /* wait for the USART to be empty */ while (!TXSTA2bits.TRMT); /* disable this interrupt */ PIE3bits.TX2IE = 0; /* enable the receiver */ RS485_TX_ENABLE = 0; RS485_RX_DISABLE = 0; /* enable the this interrupt */ PIE3bits.RC2IE = 1; RCSTA2bits.CREN = 1; } }
/**************************************************************************** * DESCRIPTION: Transmits a frame using the UART * RETURN: none * ALGORITHM: none * NOTES: none *****************************************************************************/ void RS485_Send_Frame( volatile struct mstp_port_struct_t *mstp_port, /* port specific data */ uint8_t * buffer, /* frame to send (up to 501 bytes of data) */ uint16_t nbytes) { /* number of bytes of data (up to 501) */ uint16_t i = 0; /* loop counter */ uint8_t turnaround_time; if (!buffer) return; while (!FIFO_Empty(&FIFO_Tx)) { /* buffer is not empty. Wait for ISR to transmit. */ }; /* wait 40 bit times since reception */ if (RS485_Baud_Rate == 9600) turnaround_time = 4; else if (RS485_Baud_Rate == 19200) turnaround_time = 2; else turnaround_time = 1; while (mstp_port->SilenceTimer < turnaround_time) { /* The line has not been silent long enough, so wait. */ }; if (FIFO_Add(&FIFO_Tx, buffer, nbytes)) { /* disable the receiver */ PIE3bits.RC2IE = 0; RCSTA2bits.CREN = 0; /* enable the transceiver */ RS485_TX_ENABLE = 1; RS485_RX_DISABLE = 1; /* enable the transmitter */ TXSTA2bits.TXEN = 1; PIE3bits.TX2IE = 1; /* reset the silence timer per MSTP spec, sort of */ mstp_port->SilenceTimer = 0; } return; }