void block_on_To_msgqueues() { if (!in_main()) { return; } #ifdef __USE18F2680 LATBbits.LATB3 = 1; #endif MQ_Main_Willing_to_block = 1; while (1) { if (check_msg(&ToMainHigh_MQ)) { MQ_Main_Willing_to_block = 0; #ifdef __USE18F2680 LATBbits.LATB3 = 0; #endif return; } if (check_msg(&ToMainLow_MQ)) { MQ_Main_Willing_to_block = 0; #ifdef __USE18F2680 LATBbits.LATB3 = 0; #endif return; } Delay1KTCYx(10); #ifdef __USE18F2680 LATBbits.LATB3 = !LATBbits.LATB3; #endif } }
// only called from "main" void block_on_To_msgqueues() { if (!in_main()) { return; } //LATBbits.LATB3 = 1; MQ_Main_Willing_to_block = 1; while (1) { if (check_msg(&ToMainHigh_MQ)) { MQ_Main_Willing_to_block = 0; //LATBbits.LATB3 = 0; return; } if (check_msg(&ToMainLow_MQ)) { MQ_Main_Willing_to_block = 0; //LATBbits.LATB3 = 0; return; } if (check_msg(&adReadQueue)) { MQ_Main_Willing_to_block = 0; //LATBbits.LATB3 = 0; return; } Delay1KTCYx(10); //LATBbits.LATB3 = !LATBbits.LATB3; } }
//addr is the actual address. It will be shifted here unsigned char i2c_master_send(unsigned char addr, unsigned char length, unsigned char *msg) { if(ic_ptr->status != I2C_IDLE){ //copy addr and msg into a single array char tempbuf[MAX_I2C_SENSOR_DATA_LEN +1]; tempbuf[0] = addr; int i = 1; for(i; i < length+1; i++){ tempbuf[i] = msg[i-1]; } if(in_main()){ FromMainHigh_sendmsg(length+1, MSGT_MASTER_SEND_BUSY, tempbuf); } else{ FromI2CInt_sendmsg(length+1, MSGT_MASTER_SEND_BUSY, tempbuf); } return 0; } ic_ptr->status = I2C_STARTED; ic_ptr->read_after_write = 1; ic_ptr->txnrx = 1; ic_ptr->addr = addr; char buf_addr = (addr << 1) & 0xFE; // explicitely make sure that the lsb is 0 and et the addr in top 7 bits ic_ptr->outbuffer[0] = buf_addr; int i =1; for(i; i < length + 1; i++){ ic_ptr->outbuffer[i] = msg[i-1]; } ic_ptr->outbuflen = length + 1; //char length + addr byte ic_ptr->outbufind = 0; //start at 0th pos. addr will be written in after S int happens SSPCON2bits.SEN = 1; //send start signal return 1; }
signed char FromMainHigh_sendmsg(unsigned char length, unsigned char msgtype, void *data) { #ifdef DEBUG if (!in_main()) { return (MSG_NOT_IN_MAIN); } #endif return (send_msg(&FromMainHigh_MQ, length, msgtype, data)); }
signed char ToMainHigh_recvmsg(unsigned char maxlength, unsigned char *msgtype, void *data) { #ifdef DEBUG if (!in_main()) { return (MSG_NOT_IN_MAIN); } #endif return (recv_msg(&ToMainHigh_MQ, maxlength, msgtype, data)); }
signed char ADQueue_sendmsg(unsigned char length,unsigned char msgtype,void *data) { #ifdef DEBUG if (! in_main()) { return(MSG_NOT_IN_MAIN); } #endif return(send_msg(&adReadQueue,length,msgtype,data)); }
signed char SensorData_recvmsg(unsigned char maxlength, unsigned char *msgtype, void *data) { #ifdef DEBUG if (!in_main()) { return (MSG_NOT_IN_MAIN); } #endif //return (recv_msg(&SensorData_MQ, maxlength, msgtype, data)); return(recv_msg(&SensorData_MQ, maxlength, msgtype, data)); }
signed char SensorData_recvmsg(unsigned char maxlength, unsigned char id, void *data) { #ifdef DEBUG if (!in_main()) { return (MSG_NOT_IN_MAIN); } #endif unsigned char slot; //unsigned char i; //unsigned char retlength; //unsigned char *msg = (unsigned char *) data; msg *qmsg; size_t tlength; sensor_queue *qptr = &SensorData_SQ; qmsg = &(qptr->queue[id]); // check to see if anything is available //slot = qptr->cur_read_ind; //qmsg = &(qptr->queue[slot]); if (qmsg->full == 1) { // not enough room in the buffer provided if (qmsg->length > maxlength) { return (MSGBUFFER_TOOSMALL); } // now actually copy the message tlength = qmsg->length; memcpy(data, qmsg->data, tlength); /* for (i=0;i<qmsg->length;i++) { ((unsigned char *) data)[i] = qptr->queue[slot].data[i]; } */ //qptr->cur_read_ind = (qptr->cur_read_ind + 1) % MSGQUEUELEN; //retlength = qptr->queue[slot].length; //(*msgtype) = qmsg->msgtype; // this must be done after the message is completely extracted qmsg->full = 0; return (tlength); } else { return (MSGQUEUE_EMPTY); } //return (recv_msg(&SensorData_MQ, maxlength, msgtype, data)); //return(recv_msg(&SensorData_MQ, maxlength, msgtype, data)); }
unsigned char i2c_master_recv(unsigned char addr) { if(ic_ptr->status != I2C_IDLE){ if(in_main()){ FromMainHigh_sendmsg(1, MSGT_MASTER_RECV_BUSY, &addr); } else{ FromI2CInt_sendmsg(1, MSGT_MASTER_RECV_BUSY, &addr); } return 0; } ic_ptr->txnrx = 0; ic_ptr->addr = addr; char buf_addr = (addr << 1) | 0x1; // set addr in top 7 bits and set lsb ic_ptr->outbuffer[0] = buf_addr; ic_ptr->outbuflen = 1; //just enough room for addr ic_ptr->outbufind = 0; //set for addr ic_ptr->buflen = HEADER_MEMBERS; //reset the buffer, we'll at LEAST read 5 bytes ic_ptr->bufind = 0; ic_ptr->status = I2C_STARTED; SSPCON2bits.SEN = 1; return 1; }
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 }