00001
00009 #include "comm_config.h"
00010
00016 static uint8 au8_inPacket[IN_PACKET_SIZE];
00017
00023 static uint8 au8_outPacket[OUT_PACKET_SIZE];
00024
00025 union {
00026 uint8 u8_commError;
00027 struct {
00028 unsigned int errH1:
00029 1;
00030 unsigned int errDupH1:
00031 1;
00032 unsigned int errH2:
00033 1;
00034 unsigned int errT1:
00035 1;
00036 unsigned int errT2:
00037 1;
00038 unsigned int badState:
00039 1;
00040 unsigned int unused:
00041 2;
00042 } commErrorBits;
00043 } commError;
00044
00045 static uint8 u8_inIndex = IN_PACKET_HEADER_SIZE;
00046 static uint8 u8_outIndex = OUT_PACKET_HEADER_SIZE;
00047
00048 #define COMM_GET_IN_DATA_GENERIC(datatype, func_name) \
00049 datatype func_name(void) { \
00050 datatype temp; \
00051 ASSERT((u8_inIndex + sizeof(datatype)) <= \
00052 (IN_PACKET_SIZE - IN_PACKET_HEADER_SIZE)); \
00053 memcpy(&temp, (au8_inPacket + u8_inIndex), sizeof(datatype)); \
00054 u8_inIndex += sizeof(datatype); \
00055 return temp; \
00056 }
00057
00058 #define COMM_PUT_OUT_DATA_GENERIC(datatype, func_name) \
00059 void func_name(datatype temp) { \
00060 ASSERT((u8_outIndex + sizeof(datatype)) <= \
00061 (OUT_PACKET_SIZE - OUT_PACKET_HEADER_SIZE)); \
00062 memcpy((au8_outPacket + u8_outIndex), &temp, sizeof(datatype)); \
00063 u8_outIndex += sizeof(datatype); \
00064 }
00065
00066 COMM_GET_IN_DATA_GENERIC(uint8, CommGetInDataUInt8)
00067 COMM_GET_IN_DATA_GENERIC(int8, CommGetInDataInt8)
00068 COMM_GET_IN_DATA_GENERIC(uint16, CommGetInDataUInt16)
00069 COMM_GET_IN_DATA_GENERIC(int16, CommGetInDataInt16)
00070 COMM_GET_IN_DATA_GENERIC(uint32, CommGetInDataUInt32)
00071 COMM_GET_IN_DATA_GENERIC(int32, CommGetInDataInt32)
00072
00073 COMM_PUT_OUT_DATA_GENERIC(uint8, CommPutOutDataUInt8)
00074 COMM_PUT_OUT_DATA_GENERIC(int8, CommPutOutDataInt8)
00075 COMM_PUT_OUT_DATA_GENERIC(uint16, CommPutOutDataUInt16)
00076 COMM_PUT_OUT_DATA_GENERIC(int16, CommPutOutDataInt16)
00077 COMM_PUT_OUT_DATA_GENERIC(uint32, CommPutOutDataUInt32)
00078 COMM_PUT_OUT_DATA_GENERIC(int32, CommPutOutDataInt32)
00079
00080
00081 uint8 CommGetInDataUInt8Error(void) {
00082 return commError.u8_commError;
00083 }
00084
00085
00086
00087
00088
00089
00090 uint8 CommGetInDataIsValid(void) {
00091 if (commError.u8_commError < 8)
00092 return 1;
00093 else
00094 return 0;
00095 }
00096
00097
00098 void CommGetInDataClearError(void) {
00099 commError.u8_commError = 0;
00100 }
00101
00102
00103 ESOS_USER_TASK(comm) {
00104 static uint8 u8_Char;
00105 static uint8 u8_state = 0;
00106
00107 ESOS_TASK_BEGIN();
00108 while (TRUE) {
00109
00110 while (TRUE) {
00111 ESOS_TASK_WAIT_ON_AVAILABLE_IN_COMM();
00112 if (u8_state < 2)
00113 ESOS_TASK_WAIT_ON_GET_UINT8(u8_Char);
00114 else
00115 ESOS_TASK_WAIT_ON_GET_U8BUFFER(au8_inPacket + 2, sizeof(au8_inPacket) - 2);
00116 ESOS_TASK_SIGNAL_AVAILABLE_IN_COMM();
00117 if (u8_state == 0) {
00118 if (u8_Char == IN_PACKET_HEADER_1) {
00119 u8_state++;
00120 au8_inPacket[0] = u8_Char;
00121 } else {
00122 commError.commErrorBits.errH1 = 1;
00123 }
00124 } else if (u8_state == 1) {
00125 if (u8_Char == IN_PACKET_HEADER_2) {
00126 u8_state++;
00127 au8_inPacket[1] = u8_Char;
00128 } else if (u8_Char != IN_PACKET_HEADER_1) {
00129 commError.commErrorBits.errH2 = 1;
00130 u8_state = 0;
00131 } else {
00132 commError.commErrorBits.errDupH1 = 1;
00133 }
00134 } else if (u8_state == 2) {
00135
00136 if (au8_inPacket[IN_PACKET_SIZE - 2] != IN_PACKET_TRAILER_1) {
00137 commError.commErrorBits.errT1 = 1;
00138 }
00139 if (au8_inPacket[IN_PACKET_SIZE - 1] != IN_PACKET_TRAILER_2) {
00140 commError.commErrorBits.errT2 = 1;
00141 }
00142 u8_state = 0;
00143 break;
00144 } else {
00145 commError.commErrorBits.badState = 1;
00146 u8_state = 0;
00147 }
00148 }
00149 u8_inIndex = 2;
00150 u8_outIndex = 2;
00151 divvy_packet();
00152 CommGetInDataClearError();
00153
00154
00155 ESOS_TASK_WAIT_ON_AVAILABLE_OUT_COMM();
00156 ESOS_TASK_WAIT_ON_SEND_U8BUFFER(au8_outPacket, sizeof(au8_outPacket));
00157 ESOS_TASK_SIGNAL_AVAILABLE_OUT_COMM();
00158 }
00159 ESOS_TASK_END();
00160 }
00161
00162
00163 void initialize_comm(void) {
00164 au8_outPacket[0] = OUT_PACKET_HEADER_1;
00165 au8_outPacket[1] = OUT_PACKET_HEADER_2;
00166 au8_outPacket[OUT_PACKET_SIZE - 2] = OUT_PACKET_TRAILER_1;
00167 au8_outPacket[OUT_PACKET_SIZE - 1] = OUT_PACKET_TRAILER_2;
00168 CommGetInDataClearError();
00169
00170 esos_RegisterTask(comm);
00171 }