ch_serial.c 15.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
#include "ch_serial.h"
#include "bsp.h"
#if defined(CH_DEBUG)
#include <stdio.h>
//#define CH_TRACE	printf
#else
//#define CH_TRACE(...)
#endif

 uint8_t decode_succ;
#define CHSYNC1         (0x5A)        /* CHAOHE message sync code 1 */
#define CHSYNC2         (0xA5)        /* CHAOHE message sync code 2 */
#define CH_HDR_SIZE     (0x06)        /* CHAOHE protocol header size */

/* common type conversion */
#define U1(p) (*((uint8_t *)(p)))
#define I1(p) (*((int8_t  *)(p)))
#define I2(p) (*((int16_t  *)(p)))
static uint16_t U2(uint8_t *p) {uint16_t u; memcpy(&u,p,2); return u;}
static uint32_t U4(uint8_t *p) {uint32_t u; memcpy(&u,p,4); return u;}
static float    R4(uint8_t *p) {float    r; memcpy(&r,p,4); return r;}

#define ARRAY_SIZE(x)	(sizeof(x) / sizeof((x)[0]))

static raw_t raw = {0};                                         /* IMU stram read/control struct */
static uint8_t decode_succ;                               /* 0: no new frame arrived, 1: new frame arrived */

typedef struct
{
    uint8_t code;
    char    name[8];
}item_code_name_t;

const item_code_name_t item_code_name[] = 
{
    {0x90, "id"},
    {0xA0, "acc"},
    {0xB0, "gyr"},
    {0xC0, "mag"},
    {0xD0, "eul"},
    {0xD1, "quat"},
    {0xF0, "pressure"},
    {0x91, "IMUSOL"},   /* collection of acc,gyr,mag,eul etc. to replace A0,B0,C0,D0... see user manual*/
    {0x60, "GWSOL"},    /* HI221 node imu data collection  see user manual */
};

static const char *code2name(uint8_t code)
{
    const char *p = NULL;
    int i;
    for(i=0; i<ARRAY_SIZE(item_code_name); i++)
    {
        if(code == item_code_name[i].code)
        {
            p = item_code_name[i].name;
        }
    }
    return p;
}

static void dump_imu_data(raw_t *raw)
{
    int i;
    if(raw->item_code[0] != KItemGWSOL) /* HI226 HI229 CH100 CH110 */  
    {
//        Uart_Printf(COM1,"%-16s%d\r\n",       "id:",  raw->imu[0].id);
//        Uart_Printf(COM1,"%-16s%.3f %.3f %.3f\r\n",       "acc(G):",        raw->imu[0].acc[0], raw->imu[0].acc[1],  raw->imu[0].acc[2]);
//        Uart_Printf(COM1,"%-16s%.3f %.3f %.3f\r\n",       "gyr(deg/s):",    raw->imu[0].gyr[0], raw->imu[0].gyr[1],  raw->imu[0].gyr[2]);
//        Uart_Printf(COM1,"%-16s%.3f %.3f %.3f\r\n",       "mag(uT):",       raw->imu[0].mag[0], raw->imu[0].mag[1],  raw->imu[0].mag[2]);
//        Uart_Printf(COM1,"%-16s%.3f %.3f %.3f\r\n",       "eul(deg):",      raw->imu[0].eul[0], raw->imu[0].eul[1],  raw->imu[0].eul[2]);
//        Uart_Printf(COM1,"%-16s%.3f %.3f %.3f %.3f\r\n",  "quat:",          raw->imu[0].quat[0], raw->imu[0].quat[1],  raw->imu[0].quat[2], raw->imu[0].quat[3]);
//        Uart_Printf(COM1,"%-16s%.3f\r\n",       "presure(pa):",  raw->imu[0].pressure);
//        Uart_Printf(COM1,"%-16s%d\r\n",       "timestamp(ms):",  raw->imu[0].timestamp);
//        Uart_Printf(COM1,"item: ");
//        for(i=0; i<raw->nitem_code; i++)
//        {
//            Uart_Printf(COM1,"0x%02X(%s)", raw->item_code[i], code2name(raw->item_code[i]));
//        }
//        Uart_Printf(COM1,"\r\n");
    }
    else /* HI222(HI221GW) */
    {
//        putchar(10);
//        Uart_Printf(COM1,"gateway: %s%d, %s%d\r\n",       "gwid:",      raw->gwid, "node cnt:", raw->nimu);
        for(i=0; i<raw->nimu; i++)
        {
//            putchar(10);
//            Uart_Printf(COM1,"%-16s%d\r\n",       "id:",  raw->imu[i].id);
//            Uart_Printf(COM1,"%-16s%.3f %.3f %.3f\r\n",       "acc(G):",        raw->imu[i].acc[0], raw->imu[i].acc[1],  raw->imu[i].acc[2]);
//            Uart_Printf(COM1,"%-16s%.3f %.3f %.3f\r\n",       "gyr(deg/s):",    raw->imu[i].gyr[0], raw->imu[i].gyr[1],  raw->imu[i].gyr[2]);
//            Uart_Printf(COM1,"%-16s%.3f %.3f %.3f\r\n",       "mag(uT):",       raw->imu[i].mag[0], raw->imu[i].mag[1],  raw->imu[i].mag[2]);
//            Uart_Printf(COM1,"%-16s%.3f %.3f %.3f\r\n",       "eul(deg):",      raw->imu[i].eul[0], raw->imu[i].eul[1],  raw->imu[i].eul[2]);
//            Uart_Printf(COM1,"%-16s%.3f %.3f %.3f %.3f\r\n",  "quat:",          raw->imu[i].quat[0], raw->imu[i].quat[1],  raw->imu[i].quat[2], raw->imu[i].quat[3]);
//            Uart_Printf(COM1,"%-16s%.3f\r\n",       "presure(pa):",  raw->imu[i].pressure);
//            Uart_Printf(COM1,"%-16s%d\r\n",       "timestamp(ms):",  raw->imu[i].timestamp);
        }
    }
}


static void crc16_update(uint16_t *currect_crc, const uint8_t *src, uint32_t len)
{
    uint32_t crc = *currect_crc;
    uint32_t j;
    for (j=0; j < len; ++j)
    {
        uint32_t i;
        uint32_t byte = src[j];
        crc ^= byte << 8;
        for (i = 0; i < 8; ++i)
        {
            uint32_t temp = crc << 1;
            if (crc & 0x8000)
            {
                temp ^= 0x1021;
            }
            crc = temp;
        }
    } 
    *currect_crc = crc;
}

/* dump hi229 or hi226 data packet*/
void ch_dump_imu_data(raw_t *raw)
{
    int i;
    if(raw->item_code[0] != KItemGWSOL)
    {
//        CH_TRACE("%-16s%d\r\n",       "id:",  raw->imu[0].id);
//        CH_TRACE("%-16s%.3f %.3f %.3f\r\n",       "acc(G):",        raw->imu[0].acc[0], raw->imu[0].acc[1],  raw->imu[0].acc[2]);
//        CH_TRACE("%-16s%.3f %.3f %.3f\r\n",       "gyr(deg/s):",    raw->imu[0].gyr[0], raw->imu[0].gyr[1],  raw->imu[0].gyr[2]);
//        CH_TRACE("%-16s%.3f %.3f %.3f\r\n",       "mag(uT):",       raw->imu[0].mag[0], raw->imu[0].mag[1],  raw->imu[0].mag[2]);
//        CH_TRACE("%-16s%.3f %.3f %.3f\r\n",       "eul(deg):",      raw->imu[0].eul[0], raw->imu[0].eul[1],  raw->imu[0].eul[2]);
//        CH_TRACE("%-16s%.3f %.3f %.3f %.3f\r\n",  "quat:",          raw->imu[0].quat[0], raw->imu[0].quat[1],  raw->imu[0].quat[2], raw->imu[0].quat[3]);
//        CH_TRACE("%-16s%.3f\r\n",       "presure(pa):",  raw->imu[0].pressure);
//        CH_TRACE("%-16s%d\r\n",       "timestamp(ms):",  raw->imu[0].timestamp);
//        
//        CH_TRACE("item: ");
//        for(i=0; i<raw->nitem_code; i++)
//        {
//            CH_TRACE("0x%02X ", raw->item_code[i]);
//        }
//        CH_TRACE("\r\n");
    }
    else
    {
//        CH_TRACE("gateway: %s%d, %s%d\r\n",       "gwid:",      raw->gwid, "node cnt:", raw->nimu);
//        for(i=0; i<raw->nimu; i++)
//        {
//            CH_TRACE("%-16s%d\r\n",       "id:",  raw->imu[i].id);
//            CH_TRACE("%-16s%.3f %.3f %.3f\r\n",       "acc(G):",        raw->imu[i].acc[0], raw->imu[i].acc[1],  raw->imu[i].acc[2]);
//            CH_TRACE("%-16s%.3f %.3f %.3f\r\n",       "gyr(deg/s):",    raw->imu[i].gyr[0], raw->imu[i].gyr[1],  raw->imu[i].gyr[2]);
//            CH_TRACE("%-16s%.3f %.3f %.3f\r\n",       "mag(uT):",       raw->imu[i].mag[0], raw->imu[i].mag[1],  raw->imu[i].mag[2]);
//            CH_TRACE("%-16s%.3f %.3f %.3f\r\n",       "eul(deg):",      raw->imu[i].eul[0], raw->imu[i].eul[1],  raw->imu[i].eul[2]);
//            CH_TRACE("%-16s%.3f %.3f %.3f %.3f\r\n",  "quat:",          raw->imu[i].quat[0], raw->imu[i].quat[1],  raw->imu[i].quat[2], raw->imu[i].quat[3]);
//            CH_TRACE("%-16s%.3f\r\n",       "presure(pa):",  raw->imu[i].pressure);
//            CH_TRACE("%-16s%d\r\n",       "timestamp(ms):",  raw->imu[i].timestamp);
//        }
    }
}



/* parse the payload of a frame and feed into data section */
static int parse_data(raw_t *raw)
{
    int ofs = 0, i = 0;
    uint8_t *p = &raw->buf[CH_HDR_SIZE];
    memset(raw->item_code, 0, sizeof(raw->item_code));
    raw->nitem_code = 0;

	while(ofs < raw->len)
	{
		switch(p[ofs])
		{
            case kItemID:
                raw->nimu = 1;
                raw->item_code[raw->nitem_code++] = kItemID;
                raw->imu[0].id = U1(p+ofs+1);
                ofs += 2;
                break;
            case kItemAccRaw:
                raw->nimu = 1;
                raw->item_code[raw->nitem_code++] = kItemAccRaw;
                raw->imu[0].acc[0] = (float)I2(p+ofs+1) / 1000;
                raw->imu[0].acc[1] = (float)I2(p+ofs+3) / 1000;
                raw->imu[0].acc[2] = (float)I2(p+ofs+5) / 1000;
                ofs += 7;
                break;
            case kItemGyrRaw:
                raw->nimu = 1;
                raw->item_code[raw->nitem_code++] = kItemGyrRaw;
                raw->imu[0].gyr[0] = (float)I2(p+ofs+1) / 10;
                raw->imu[0].gyr[1] = (float)I2(p+ofs+3) / 10;
                raw->imu[0].gyr[2] = (float)I2(p+ofs+5) / 10;
                ofs += 7;
                break;
            case kItemMagRaw:
                raw->nimu = 1;
                raw->item_code[raw->nitem_code++] = kItemMagRaw;
                raw->imu[0].mag[0] = (float)I2(p+ofs+1) / 10;
                raw->imu[0].mag[1] = (float)I2(p+ofs+3) / 10;
                raw->imu[0].mag[2] = (float)I2(p+ofs+5) / 10;
                ofs += 7;
                break;
            case kItemRotationEul:
                raw->item_code[raw->nitem_code++] = kItemRotationEul;
                raw->imu[0].eul[0] = (float)I2(p+ofs+1) / 100;
                raw->imu[0].eul[1] = (float)I2(p+ofs+3) / 100;
                raw->imu[0].eul[2] = (float)I2(p+ofs+5) / 10;
                ofs += 7;
                break;
            case kItemRotationQuat:
                raw->nimu = 1;
                raw->item_code[raw->nitem_code++] = kItemRotationQuat;
                raw->imu[0].quat[0] = R4(p+ofs+1);
                raw->imu[0].quat[1] = R4(p+ofs+5);
                raw->imu[0].quat[2] = R4(p+ofs+9);
                raw->imu[0].quat[3] = R4(p+ofs+13);
                ofs += 17;
                break;
            case kItemPressure:
                raw->nimu = 1;
                raw->item_code[raw->nitem_code++] = kItemPressure;
                raw->imu[0].pressure = R4(p+ofs+1);
                ofs += 5;
                break;

            case KItemIMUSOL:
                raw->nimu = 1;
                raw->item_code[raw->nitem_code++] = KItemIMUSOL;
                raw->imu[0].id = U1(p+ofs+1);
                raw->imu[0].pressure = R4(p+ofs+4);
                raw->imu[0].timestamp = U4(p+ofs+8);
                raw->imu[0].acc[0] = R4(p+ofs+12);
                raw->imu[0].acc[1] = R4(p+ofs+16);
                raw->imu[0].acc[2] = R4(p+ofs+20);
                raw->imu[0].gyr[0] = R4(p+ofs+24);
                raw->imu[0].gyr[1] = R4(p+ofs+28);
                raw->imu[0].gyr[2] = R4(p+ofs+32);
                raw->imu[0].mag[0] = R4(p+ofs+36);
                raw->imu[0].mag[1] = R4(p+ofs+40);
                raw->imu[0].mag[2] = R4(p+ofs+44);
                raw->imu[0].eul[0] = R4(p+ofs+48);
                raw->imu[0].eul[1] = R4(p+ofs+52);
                raw->imu[0].eul[2] = R4(p+ofs+56);
                raw->imu[0].quat[0] = R4(p+ofs+60);
                raw->imu[0].quat[1] = R4(p+ofs+64);
                raw->imu[0].quat[2] = R4(p+ofs+68);
                raw->imu[0].quat[3] = R4(p+ofs+72);
                ofs += 76;
                break;
				
            case KItemGWSOL:
                raw->item_code[raw->nitem_code++] = KItemGWSOL;
                raw->gwid = U1(p+ofs+1);
                raw->nimu = U1(p+ofs+2);
                ofs += 8;
                for (i=0; i<raw->nimu; i++)
                {
                    raw->imu[i].id = U1(p+ofs+1);
                    raw->imu[i].pressure = R4(p+ofs+4);
                    raw->imu[i].timestamp = U4(p+ofs+8);
                    raw->imu[i].acc[0] = R4(p+ofs+12);
                    raw->imu[i].acc[1] = R4(p+ofs+16);
                    raw->imu[i].acc[2] = R4(p+ofs+20);
                    raw->imu[i].gyr[0] = R4(p+ofs+24);
                    raw->imu[i].gyr[1] = R4(p+ofs+28);
                    raw->imu[i].gyr[2] = R4(p+ofs+32);
                    raw->imu[i].mag[0] = R4(p+ofs+36);
                    raw->imu[i].mag[1] = R4(p+ofs+40);
                    raw->imu[i].mag[2] = R4(p+ofs+44);
                    raw->imu[i].eul[0] = R4(p+ofs+48);
                    raw->imu[i].eul[1] = R4(p+ofs+52);
                    raw->imu[i].eul[2] = R4(p+ofs+56);
                    raw->imu[i].quat[0] = R4(p+ofs+60);
                    raw->imu[i].quat[1] = R4(p+ofs+64);
                    raw->imu[i].quat[2] = R4(p+ofs+68);
                    raw->imu[i].quat[3] = R4(p+ofs+72);
                    ofs += 76;
                }
                break;
            default:
				ofs++;
                break;
		}
    }
    
    return 1;
}

static int decode_ch(raw_t *raw)
{
    uint16_t crc = 0;   

    /* checksum */
    crc16_update(&crc, raw->buf, 4);
    crc16_update(&crc, raw->buf+6, raw->len);
    if (crc != U2(raw->buf+4))
    {
//        CH_TRACE("ch checksum error: frame:0x%X calcuate:0x%X, len:%d\n", U2(raw->buf+4), crc, raw->len);
        return -1;
    }
    
    return parse_data(raw);
}

/* sync code */
static int sync_ch(uint8_t *buf, uint8_t data)
{
    buf[0] = buf[1];
    buf[1] = data;
    return buf[0] == CHSYNC1 && buf[1] == CHSYNC2;
}

int ch_serial_input(raw_t *raw, uint8_t data)
{
    /* synchronize frame */
    if (raw->nbyte == 0)
    {
        if (!sync_ch(raw->buf, data)) return 0;
        raw->nbyte = 2;
        return 0;
    }

    raw->buf[raw->nbyte++] = data;
    
    if (raw->nbyte == CH_HDR_SIZE)
    {
        if ((raw->len = U2(raw->buf+2)) > (MAXRAWLEN - CH_HDR_SIZE))
        {
//            CH_TRACE("ch length error: len=%d\n",raw->len);
            raw->nbyte = 0;
            return -1;
        }
    }
    
    if (raw->nbyte < (raw->len + CH_HDR_SIZE)) 
    {
        return 0;
    }
    
    raw->nbyte = 0;
    
    return decode_ch(raw);
}
extern UartStruct Uart2Stu;

void USART2_IRQHandler(void)
{
    CPU_SR_ALLOC();
    unsigned int i;    
    unsigned char ch;
		u8 flag = 0;
    CPU_CRITICAL_ENTER();
    OSIntEnter();                                          
    CPU_CRITICAL_EXIT();
    /*接收中断*/
    if (USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
    { 
			flag = 1;
    	/* Read one byte from the receive data register */
        ch = USART_ReceiveData(USART2);
    	if(((Uart2Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart2Stu.RxBuf_Out)
    	{

    	}
    	else
    	{
            Uart2Stu.RxBuf[(Uart2Stu.RxBuf_In)] = ch;
    	    Uart2Stu.RxBuf_In = (Uart2Stu.RxBuf_In + 1) & UART_BUF_LEN;
    	}    	
    }	    	    
    /* 发送缓冲区空中断 */
    if (USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
    {
        if(Uart2Stu.TxBuf_In != Uart2Stu.TxBuf_Out)
        {
            i = Uart2Stu.TxBuf_Out;
            Uart2Stu.TxBuf_Out = (Uart2Stu.TxBuf_Out + 1) & UART_BUF_LEN;
            ch = Uart2Stu.TxBuf[i];
            USART_SendData(USART2,ch);
        }
        else
    	{
    	    /* 禁止发送缓冲区空中断,使能发送完毕中断 */
    	    USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
    	    USART_ITConfig(USART2, USART_IT_TC, ENABLE);
    	}
    }
    /* 1个字节发送完毕的中断 */
    else if (USART_GetITStatus(USART2, USART_IT_TC) != RESET)
    {
        if(Uart2Stu.TxBuf_In != Uart2Stu.TxBuf_Out)
        {
            i = Uart2Stu.TxBuf_Out;
            Uart2Stu.TxBuf_Out = (Uart2Stu.TxBuf_Out + 1) & UART_BUF_LEN;
            ch = Uart2Stu.TxBuf[i];
            USART_SendData(USART2,ch);
        }
        else
    	{
    	    /* 禁止发送缓冲区空中断,使能发送完毕中断 */
    	    USART_ITConfig(USART2, USART_IT_TC, DISABLE);
    	}
    }
		else if(flag == 0)//接收
    {
    	/* Read one byte from the receive data register */
        ch = USART_ReceiveData(USART2);
    	if(((Uart2Stu.RxBuf_In + 1) & UART_BUF_LEN) == Uart2Stu.RxBuf_Out)
    	{

    	}
    	else
    	{
            Uart2Stu.RxBuf[(Uart2Stu.RxBuf_In)] = ch;
    	    Uart2Stu.RxBuf_In = (Uart2Stu.RxBuf_In + 1) & UART_BUF_LEN;
    	}    	
    }
	decode_succ = ch_serial_input(&raw, ch);
	OSIntExit(); 
}
void DecodeIMUdata()
{
	if(decode_succ)
	{
			decode_succ = 0;
			dump_imu_data(&raw);
	}
	IMU.palstance = raw.imu[0].gyr[2]*0.017453;
	IMU.angle = raw.imu[0].eul[2]/57.3;
	if(Camera.UpdataFlag == 1)//有码
	{		
			IMU.angleCompensation = Camera.angle - IMU.angle;
	}
	IMU.angle += IMU.angleCompensation;
}