1 /* 2 * ard_utils.h 3 * 4 * Created on: Jul 4, 2010 5 * Author: mlf by Metodo2 srl 6 */ 7 8 #ifndef ARD_UTILS_H_ 9 #define ARD_UTILS_H_ 10 11 #include "gpio.h" 12 #include "debug.h" 13 #include "ARDUINO/arduino.h" 14 #define INIT_SIGNAL_FOR_SPI() gpio_disable_pin_pull_up(ARDUINO_HANDSHAKE_PIN); 15 #define BUSY_FOR_SPI() gpio_set_gpio_pin(ARDUINO_HANDSHAKE_PIN) 16 #define AVAIL_FOR_SPI() gpio_clr_gpio_pin(ARDUINO_HANDSHAKE_PIN) 17 18 #define LED0_UP() gpio_set_gpio_pin(LED0_GPIO) 19 #define LED0_DN() gpio_clr_gpio_pin(LED0_GPIO) 20 #define LED0_TL() gpio_tgl_gpio_pin(LED0_GPIO) 21 #define LED1_UP() gpio_set_gpio_pin(LED1_GPIO) 22 #define LED1_DN() gpio_clr_gpio_pin(LED1_GPIO) 23 #define LED1_TL() gpio_tgl_gpio_pin(LED1_GPIO) 24 #define LED2_UP() gpio_set_gpio_pin(LED2_GPIO) 25 #define LED2_DN() gpio_clr_gpio_pin(LED2_GPIO) 26 #define LED2_TL() gpio_tgl_gpio_pin(LED2_GPIO) 27 28 #ifdef _DEBUG_ 29 #define SIGN0_UP LED0_UP 30 #define SIGN0_DN LED0_DN 31 #define SIGN0_TL LED0_TL 32 #define SIGN1_UP LED1_UP 33 #define SIGN1_DN LED1_DN 34 #define SIGN1_TL LED1_TL 35 #define SIGN2_UP LED2_UP 36 #define SIGN2_DN LED2_DN 37 #define SIGN2_TL LED2_TL 38 39 #define DEB_PIN_UP(X) gpio_set_gpio_pin(DEB##X##_PIN_GPIO) 40 #define DEB_PIN_DN(X) gpio_clr_gpio_pin(DEB##X##_PIN_GPIO) 41 #define DEB_PIN_ENA(X) gpio_enable_gpio_pin(DEB##X##_PIN_GPIO) 42 #define DEB_PIN_TOGGLE(X) gpio_tgl_gpio_pin(DEB##X##_PIN_GPIO) 43 #define DEB_PIN_TRIGGER(X) DEB_PIN_DN(X); DEB_PIN_UP(X); 44 45 46 #else 47 #define SIGN0_UP() 48 #define SIGN0_DN() 49 #define SIGN0_TL() 50 #define SIGN1_UP() 51 #define SIGN1_DN() 52 #define SIGN1_TL() 53 #define SIGN2_UP() 54 #define SIGN2_DN() 55 #define SIGN2_TL() 56 57 #define DEB_PIN_UP(X) 58 #define DEB_PIN_DN(X) 59 #define DEB_PIN_ENA(X) 60 #define DEB_PIN_TOGGLE(X) 61 #define DEB_PIN_TRIGGER(X) 62 63 //#define TOGGLE_SIG0 64 #endif 65 66 #define DELAY_450NS asm volatile("nop") 67 #define DELAY_1uS DELAY_450NS; DELAY_450NS; 68 #define TOGGLE_SIG0() SIGN0_UP(); DELAY_450NS;SIGN0_DN(); 69 70 71 #define LINK_LED_OFF LED0_UP 72 #define ERROR_LED_OFF LED1_UP 73 #define DATA_LED_OFF LED2_UP 74 75 #define LINK_LED_ON LED0_DN 76 #define ERROR_LED_ON LED1_DN 77 #define DATA_LED_ON LED2_DN 78 79 #define LINK_LED_BL LED0_TL 80 #define ERROR_LED_BL LED1_TL 81 #define DATA_LED_BL LED2_TL 82 83 84 #define CREATE_HEADER_REPLY(REPLY, RECV, NUM_PARAMS)\ 85 REPLY[0] = RECV[0]; \ 86 REPLY[1] = RECV[1] | REPLY_FLAG; \ 87 REPLY[2] = NUM_PARAMS; 88 89 #define CREATE_HEADER_REPLY_WAIT(REPLY, RECV, NUM_PARAMS)\ 90 REPLY[0] = RECV[0]; \ 91 REPLY[1] = RECV[1] | WAIT_FLAG; \ 92 REPLY[2] = NUM_PARAMS; 93 94 95 #define END_HEADER_REPLY(REPLY, TOT_LEN, COUNT)\ 96 REPLY[TOT_LEN] = END_CMD; \ 97 REPLY[TOT_LEN+1] = 0; \ 98 COUNT=TOT_LEN+1; 99 100 #define RETURN_ERR_REPLY(RECV,REPLY,COUNT) \ 101 {uint8_t err = 0; return ack_reply_cb(RECV,REPLY,&err,COUNT);} 102 103 #define CHECK_ARD_NETIF(RECV,REPLY,COUNT) \ 104 if (ard_netif == NULL) \ 105 { uint8_t err = 0; return ack_reply_cb(RECV,REPLY,&err,COUNT); } 106 107 #define PUT_LONG_IN_BYTE_HO(LONG, BYTE, IDX) { \ 108 uint32_t _long = LONG; \ 109 BYTE[IDX] = 4; \ 110 BYTE[IDX+1] = (uint8_t)(_long & 0xff); \ 111 BYTE[IDX+2] = (uint8_t)((_long & 0xff00)>>8); \ 112 BYTE[IDX+3] = (uint8_t)((_long & 0xff0000)>>16); \ 113 BYTE[IDX+4] = (uint8_t)((_long & 0xff000000)>>24); \ 114 } 115 116 #define PUT_LONG_IN_BYTE_NO(LONG, BYTE, IDX) { \ 117 uint32_t _long = LONG; \ 118 BYTE[IDX] = 4; \ 119 BYTE[IDX+4] = (uint8_t)(_long & 0xff); \ 120 BYTE[IDX+3] = (uint8_t)((_long & 0xff00)>>8); \ 121 BYTE[IDX+2] = (uint8_t)((_long & 0xff0000)>>16); \ 122 BYTE[IDX+1] = (uint8_t)((_long & 0xff000000)>>24); \ 123 } 124 125 126 #define PUT_DATA_INT(INT, BYTE, IDX) { \ 127 uint16_t _int = INT; \ 128 BYTE[IDX] = 2; \ 129 BYTE[IDX+1] = (uint8_t)((_int & 0xff00)>>8); \ 130 BYTE[IDX+2] = (uint8_t)(_int & 0xff); \ 131 } 132 133 #define PUT_DATA_INT_NO(INT, BYTE, IDX) { \ 134 uint16_t _int = INT; \ 135 BYTE[IDX] = 2; \ 136 BYTE[IDX+2] = (uint8_t)((_int & 0xff00)>>8); \ 137 BYTE[IDX+1] = (uint8_t)(_int & 0xff); \ 138 } 139 140 #define PUT_DATA_BYTE(DATA, BYTE, IDX) { \ 141 BYTE[IDX] = 1; \ 142 BYTE[IDX+1] = (uint8_t)DATA; \ 143 } 144 145 #define PUT_BUFDATA_BYTE(BUF, BUFLEN, BYTE, IDX) { \ 146 BYTE[IDX] = (uint8_t)(BUFLEN & 0xff); \ 147 uint16_t i = 0; \ 148 for (; i<BUFLEN; ++i) \ 149 BYTE[IDX+1+i]=BUF[i]; \ 150 } 151 152 #define PUT_BUFDATA_INT(BUF, BUFLEN, BYTE, IDX) { \ 153 BYTE[IDX] = (uint8_t)((BUFLEN & 0xff00)>>8); \ 154 BYTE[IDX+1] = (uint8_t)(BUFLEN & 0xff); \ 155 uint16_t i = 0; \ 156 for (; i<BUFLEN; ++i) \ 157 BYTE[IDX+2+i]=BUF[i]; \ 158 } 159 160 161 #define PUT_BUFDATA_BYTE_REV(BUF, BUFLEN, BYTE, IDX) { \ 162 BYTE[IDX] = (uint8_t)(BUFLEN & 0xff); \ 163 uint16_t i = 0; \ 164 for (; i<BUFLEN; ++i) \ 165 BYTE[IDX+1+i]=BUF[BUFLEN-i-1]; \ 166 } 167 168 #define GET_DATA_LONG(INT32, BUF) \ 169 uint32_t INT32 = ((*(BUF))<<24) + ((*(BUF+1))<<16) + ((*(BUF+2))<<8) + (*(BUF+3)); 170 171 #define GET_DATA_INT(INT16, BUF) \ 172 uint16_t INT16 = ((*(BUF))<<8) + (*(BUF+1)); 173 174 #define GET_DATA_BYTE(BYTE, BUF) \ 175 uint8_t BYTE = (*(BUF)); 176 177 #define CHECK_PARAM_LEN(PARAM, LEN) ((PARAM!=NULL)&&(PARAM->paramLen == LEN)) 178 179 #define NEXT_PARAM(PARAM) \ 180 do { \ 181 if (PARAM!=NULL){ \ 182 PARAM=(tParam*)((uint8_t*)PARAM+PARAM->paramLen+1); \ 183 GET_PARAM_BYTE(PARAM, end) \ 184 if (end == END_CMD) WARN("End of cmd params", PARAM); \ 185 } \ 186 }while(0); 187 188 #define GET_PARAM_LONG(PARAM, LONG) \ 189 uint32_t LONG = 0; \ 190 if CHECK_PARAM_LEN(PARAM, 4) { \ 191 tLongParam* s = (tLongParam*)PARAM; \ 192 LONG = s->param; \ 193 } 194 195 #define GET_PARAM_INT(PARAM, INT) \ 196 uint16_t INT = 0; \ 197 if CHECK_PARAM_LEN(PARAM, 2) { \ 198 tIntParam* s = (tIntParam*)PARAM; \ 199 INT = s->param; \ 200 } 201 202 #define GET_PARAM_BYTE(PARAM, BYTE) \ 203 uint8_t BYTE = 0; \ 204 if CHECK_PARAM_LEN(PARAM, 1) { \ 205 tByteParam* s = (tByteParam*)PARAM; \ 206 BYTE = s->param; \ 207 } 208 209 #define GET_PARAM_NEXT(TYPE, PARAM, DATA) \ 210 GET_PARAM_##TYPE(PARAM, DATA) \ 211 NEXT_PARAM(PARAM) 212 213 #ifdef _SPI_STATS_ 214 #define STATSPI_TIMEOUT_ERROR() \ 215 statSpi.timeoutIntErr++; \ 216 statSpi.rxErr++; \ 217 statSpi.lastError = SPI_TIMEOUT_ERROR; \ 218 statSpi.status = spi_getStatus(ARD_SPI); 219 220 #define STATSPI_DISALIGN_ERROR() \ 221 statSpi.frameDisalign++; \ 222 statSpi.rxErr++; \ 223 statSpi.lastError = SPI_ALIGN_ERROR; \ 224 statSpi.status = spi_getStatus(ARD_SPI); 225 226 #define STATSPI_OVERRIDE_ERROR() \ 227 statSpi.overrideFrame++; \ 228 statSpi.rxErr++; \ 229 statSpi.lastError = SPI_OVERRIDE_ERROR; \ 230 statSpi.status = spi_getStatus(ARD_SPI); 231 232 #define STATSPI_TX_TIMEOUT_ERROR() \ 233 statSpi.timeoutErr++; \ 234 statSpi.txErr++; \ 235 statSpi.lastError = SPI_ERROR_TIMEOUT; \ 236 statSpi.status = spi_getStatus(ARD_SPI); 237 #else 238 #define STATSPI_TIMEOUT_ERROR() 239 #define STATSPI_TX_TIMEOUT_ERROR() 240 #define STATSPI_DISALIGN_ERROR() 241 #define STATSPI_OVERRIDE_ERROR() 242 #endif 243 244 #define DUMP_TCP_STATE(TTCP) do {\ 245 int i = getCurrClientConnId(); \ 246 INFO_TCP("%d] ttcp:%p tpcb:%p state:%d lpcb:%p state:%d left:%d sent:%d\n", \ 247 i, TTCP, TTCP->tpcb[i], (TTCP->tpcb[i])?TTCP->tpcb[i]->state:0, \ 248 TTCP->lpcb, (TTCP->lpcb)?TTCP->lpcb->state:0, \ 249 (TTCP->tpcb[i])?TTCP->left[i]:0, (TTCP->tpcb[i])?TTCP->buff_sent[i]:0); \ 250 } while(0); 251 252 #define Mode2Str(_Mode) ((_Mode==0)?"TRANSMIT":"RECEIVE") 253 #define ProtMode2Str(_protMode) ((_protMode==0)?"TCP":"UDP") 254 255 typedef struct sData 256 { 257 uint8_t* data; 258 uint16_t len; 259 uint16_t idx; 260 void* pcb; 261 }tData; 262 263 struct pbuf; 264 265 void init_pBuf(); 266 267 uint8_t* insert_pBuf(struct pbuf* q, uint8_t sock, void* _pcb); 268 269 uint8_t* insertBuf(uint8_t sock, uint8_t* buf, uint16_t len); 270 271 uint8_t* mergeBuf(uint8_t sock, uint8_t** buf, uint16_t* _len); 272 273 uint16_t clearBuf(uint8_t sock); 274 275 tData* get_pBuf(uint8_t sock); 276 277 void freetData(void * buf, uint8_t sock); 278 279 void freetDataIdx(uint8_t idxBuf, uint8_t sock); 280 281 bool isBufAvail(); 282 283 bool getTcpData(uint8_t sock, void** payload, uint16_t* len); 284 285 bool getTcpDataByte(uint8_t sock, uint8_t* payload, uint8_t peek); 286 287 uint16_t getAvailTcpDataByte(uint8_t sock); 288 289 bool isAvailTcpDataByte(uint8_t sock); 290 291 uint8_t freeTcpData(uint8_t sock); 292 293 void freeAllTcpData(uint8_t sock); 294 295 #endif /* ARD_UTILS_H_ */ 296