int main(void) { int nacfd = -1; unsigned int menu; unsigned int addr; uint16_t value; unsigned char c; if((nacfd = nac_open("/dev/nac0")) < 0) { printf("open pcie card failed\n"); return 0; } while(1){ printf("1: write \n"); printf("2: read \n"); printf("3: exit \n"); scanf("%d",&menu); switch (menu) { case 1: printf("Input reg address\n"); scanf("%x",&addr); printf("input value:\n"); scanf("%x",&value); framer_write(nacfd,addr,value); break; case 2: printf("Input reg address\n"); scanf("%x",&addr); value = framer_read(nacfd,addr); printf("reg 0x%x value is 0x%x\n",addr,value); break; case 3: goto end; default : break; } menu=0; } end: close(nacfd); return 0; }
int main(void) { uint16 value, count; uint8 len; char * request; char * response; char * payload; char * msg = "welcome to node..."; TiTimerAdapter * timeradapter; TiTimerManager * vtm; TiTimer * mac_timer; TiCc2420Adapter * cc; TiFrameRxTxInterface * rxtx; TiNioAcceptor * nac; TiAloha * mac; TiAdcAdapter * adc; TiLumSensor * lum; TiDataTreeNetwork * dtp; TiFrame * rxbuf; TiFrame * txbuf; target_init(); led_open(); led_on( LED_ALL ); hal_delay( 500 ); led_off( LED_ALL ); rtl_init( (void *)dbio_open(38400), (TiFunDebugIoPutChar)dbio_putchar, (TiFunDebugIoGetChar)dbio_getchar, hal_assert_report ); dbc_write( msg, strlen(msg) ); timeradapter = timer_construct( (void *)(&m_timeradapter), sizeof(m_timeradapter) ); vtm = vtm_construct( (void*)&m_vtm, sizeof(m_vtm) ); cc = cc2420_construct( (char *)(&m_cc), sizeof(TiCc2420Adapter) ); nac = nac_construct( &m_nacmem[0], NAC_SIZE ); mac = aloha_construct( (char *)(&m_aloha), sizeof(TiAloha) ); dtp = dtp_construct( (char *)(&m_dtp), sizeof(TiDataTreeNetwork) ); adc = adc_construct( (void *)&m_adc, sizeof(TiAdcAdapter) ); lum = lum_construct( (void *)&m_lum, sizeof(TiLumSensor) ); txbuf = frame_open( (char*)(&m_txbuf), FRAME_HOPESIZE(MAX_IEEE802FRAME154_SIZE), 3, 20, 0 ); rxbuf = frame_open( (char*)(&m_rxbuf), FRAME_HOPESIZE(MAX_IEEE802FRAME154_SIZE), 3, 20, 0 ); // timeradapter is used by the vtm(virtual timer manager). vtm require to enable the // period interrupt modal of vtm //timeradapter = timer_open( timeradapter, 0, NULL, NULL, 0x01 ); vtm = vtm_open( vtm, timeradapter, CONFIG_VTM_RESOLUTION ); cc = cc2420_open(cc, 0, NULL, NULL, 0x00 ); rxtx = cc2420_interface( cc, &m_rxtx ); mac_timer = vtm_apply( vtm ); mac_timer = vti_open( mac_timer, NULL, mac_timer); hal_assert( rxtx != NULL ); nac = nac_open( nac, rxtx, CONFIG_NIOACCEPTOR_RXQUE_CAPACITY, CONFIG_NIOACCEPTOR_TXQUE_CAPACITY); hal_assert( nac != NULL ); mac = aloha_open( mac, rxtx,nac, CONFIG_NODE_CHANNEL, CONFIG_NODE_PANID, CONFIG_NODE_ADDRESS,mac_timer, NULL, NULL,0x01); adc = adc_open( adc, 0, NULL, NULL, 0 ); lum = lum_open( lum, 0, adc ); dtp = dtp_open( dtp, mac, CONFIG_NODE_ADDRESS, NULL, NULL, 0x00 ); //todo cc2420_setchannel( cc, CONFIG_NODE_CHANNEL ); cc2420_setrxmode( cc ); // enable RX mode cc2420_setpanid( cc, CONFIG_NODE_PANID ); // network identifier, seems no use in sniffer mode cc2420_setshortaddress( cc, CONFIG_NODE_ADDRESS ); // in network address, seems no use in sniffer mode cc2420_enable_autoack( cc ); //todo cc2420_settxpower( cc, CC2420_POWER_1);//cc2420_settxpower( cc, CC2420_POWER_2); cc2420_enable_autoack( cc ); // ledtune = ledtune_construct( (void*)(&m_ledtune), sizeof(m_ledtune), vti ); // ledtune = ledtune_open( ledtune ); /* assert: all the above open() functions return non NULL values */ hal_assert((timeradapter != NULL) && (cc != NULL) && (mac != NULL) && (adc != NULL) && (lum != NULL) && (rxbuf != NULL) && (txbuf != NULL) && (dtp != NULL)); hal_enable_interrupts(); dtp->state = DTP_STATE_IDLE;//todo for testing 临时用这一句必须删掉 //todo for testing //dtp->root = 0x01;//todo for testing //dtp->parent = 0x03;//todo for testing /* while ( 1)//todo for testing { response = frame_startptr( txbuf ); value = lum_value( lum ); payload = DTP_PAYLOAD_PTR(response); payload[0] = 0x13; payload[1] = 0x14; if (dtp_send_response(dtp, txbuf, 0x01) > 0) { led_toggle( LED_RED);//todo for testing } dtp_evolve( dtp, NULL ); hal_delay( 2000);//todo for testing } */ while(1) { /* Only the following two kinds of frames will be put into "rxbuf" by dtp_recv() * - broadcast frames. the destination address of these frames are 0xFFFF. * - destination is the current node. */ //dbo_putchar(0x33); len = dtp_recv( dtp, rxbuf, 0x00 ); if (len > 0) { //ieee802frame154_dump( rxbuf); request = frame_startptr( rxbuf ); switch (DTP_CMDTYPE(request)) { /* if the frame is DTP_DATA_REQUEST, then the node will measure the data and * encapsulate the data into the txbuf, which is a TiOpenFrame and sent it back. */ case DTP_DATA_REQUEST: //payload = DTP_PAYLOAD_PTR( frame_startptr(txbuf) ); //ledtune_write( ledtune, MAKE_WORD(payload[1], payload[0]) ); // response frame = PHY Length 1B // + Frame Control 2B // + Sequence No 1B // + Destination Pan & Address 4B // + Source Pan & Address 4B // + DTP Section 15B //opf_cast( txbuf, 50, OPF_DEF_FRAMECONTROL_DATA_ACK ); response = frame_startptr( txbuf ); value = lum_value( lum ); DTP_SET_MAX_HOPCOUNT( response,0x03);//todo for testing payload = DTP_PAYLOAD_PTR(response); //payload[0] = 0x17;//todo 第三个节点数据 //payload[1] = 0x18;//todo 第三个节点数据 //payload[1] = 0x13; //payload[2] = 0x14; payload[1] = 0x15;//todo 另一个节点 payload[2] = 0x16;//todo 另一个节点 /* call dtp_send_response() to send the data in txbuf out. * * modified by zhangwei on 20091230 * - Bug fix. In the past, there's no delay between two adjacent * dtp_send_response() calls. This policy is too ambitious and this * node will occupy the whole time so that the other nodes will lost * chances to send, or encounter much higher frame collision probabilities. * so I add a little time delay here. * Attention the delay time here shouldn't be too large because * we don't want the hal_delay() to occupy all the CPU time. If this * occurs, it may lead to unnecessary frame lossing. */ // try some times until the RESPONSE is successfully sent for (count=0; count<10; count++) { //hal_delay( 500); if (dtp_send_response(dtp, txbuf, 0x03) > 0) { led_toggle( LED_RED);//todo for testing break; } //hal_delay( 50 ); } break; default: //hal_assert(false); break; } } nac_evolve( nac,NULL);//todo for tesitng aloha_evolve( mac,NULL);//todo for testing dtp_evolve( dtp, NULL ); hal_delay( 50 ); } }
void recvnode(void) { TiCc2420Adapter * cc; TiFrameRxTxInterface * rxtx; TiNioAcceptor * nac; TiAloha * mac; TiTimerAdapter *timer; TiTimerManager * vtm; TiTimer * mac_timer; TiFrame * rxbuf; char * msg = "welcome to aloha recv test..."; int len=0; target_init(); // flash the led to indicate the software is successfully running now. // led_open(); led_on( LED_ALL ); hal_delay( 500 ); led_off( LED_ALL ); //led_on( LED_RED ); // initialize the runtime library for debugging input/output and assertion // hal_assert_report is defined in module "hal_assert" // //dbo_open( 38400 ); rtl_init( (void *)dbio_open(38400), (TiFunDebugIoPutChar)dbio_putchar, (TiFunDebugIoGetChar)dbio_getchar, hal_assert_report ); dbc_putchar( 0xF0 ); dbc_mem( msg, strlen(msg) ); cc = cc2420_construct( (void *)(&m_cc), sizeof(TiCc2420Adapter) ); nac = nac_construct( &m_nacmem[0], NAC_SIZE ); mac = aloha_construct( (char *)(&m_aloha), sizeof(TiAloha) ); timer= timer_construct(( char *)(&m_timer),sizeof(TiTimerAdapter)); vtm = vtm_construct( (void*)&m_vtm, sizeof(m_vtm) ); timer = timer_open( timer, 2, NULL, NULL, 0x00 ); vtm = vtm_open( vtm, timer, VTM_RESOLUTION ); mac_timer = vtm_apply( vtm ); vti_open( mac_timer, NULL, NULL ); #ifdef CONFIG_TSET_LISTENER // cc = cc2420_open( cc, 0, _aloha_listener, NULL, 0x00 ); cc = cc2420_open( cc, 0, aloha_evolve, mac, 0x00 ); rxtx = cc2420_interface( cc, &m_rxtx ); mac = aloha_open( mac, rxtx, CONFIG_ALOHA_CHANNEL, CONFIG_ALOHA_PANID, CONFIG_ALOHA_LOCAL_ADDRESS, mac_timer, _aloha_listener, NULL,0x00 ); #endif #ifndef CONFIG_TSET_LISTENER cc = cc2420_open( cc, 0, NULL, NULL, 0x00 ); rxtx = cc2420_interface( cc, &m_rxtx ); nac_open( nac, rxtx, CONFIG_NIOACCEPTOR_RXQUE_CAPACITY, CONFIG_NIOACCEPTOR_TXQUE_CAPACITY); mac = aloha_open( mac, rxtx, nac,CONFIG_ALOHA_CHANNEL, CONFIG_ALOHA_PANID, CONFIG_ALOHA_LOCAL_ADDRESS, mac_timer, NULL, NULL,0x00 ); #endif cc2420_setchannel( cc, CONFIG_ALOHA_CHANNEL ); cc2420_setrxmode( cc ); // enable RX mode cc2420_setpanid( cc, CONFIG_ALOHA_PANID ); // network identifier, seems no use in sniffer mode cc2420_setshortaddress( cc, CONFIG_ALOHA_LOCAL_ADDRESS ); // in network address, seems no use in sniffer mode cc2420_enable_autoack( cc ); cc2420_settxpower( cc, CC2420_POWER_1);//cc2420_settxpower( cc, CC2420_POWER_2);CC2420_POWER_1 #ifdef CONFIG_TEST_ADDRESSRECOGNITION cc2420_enable_addrdecode( cc ); #else cc2420_disable_addrdecode( cc ); #endif #ifdef CONFIG_TEST_ACK cc2420_enable_autoack( cc ); #endif rxbuf = frame_open( (char*)(&m_rxbufmem), FRAME_HOPESIZE(MAX_IEEE802FRAME154_SIZE), 3, 20, 0 ); dbc_putchar(0x11); #ifdef CONFIG_TEST_ACK //fcf = OPF_DEF_FRAMECONTROL_DATA_ACK; #else //fcf = OPF_DEF_FRAMECONTROL_DATA_NOACK; #endif hal_enable_interrupts(); /* Wait for listener action. The listener function will be called by the TiCc2420Adapter * object when a frame arrives */ #ifdef CONFIG_TEST_LISTENER while (1) {} #endif /* Query the TiCc2420Adapter object if there's no listener */ #ifndef CONFIG_TEST_LISTENER while(1) { char * ptr;//todo for testing frame_reset( rxbuf, 3, 20, 0 ); len = aloha_recv( mac, rxbuf, 0x00 ); if (len > 0) { frame_moveouter( rxbuf ); _output_frame( rxbuf, NULL ); frame_moveinner( rxbuf ); /* dbc_write( frame_startptr( rxbuf),frame_length( rxbuf));//todo for testing ptr = frame_startptr( rxbuf);//todo for testing dbc_putchar( 0xff);//todo for testing dbc_uint8( ptr[ (frame_length( rxbuf)-2)]);//todo for testing dbc_putchar( 0xff);//todo for testing frame_moveinner( rxbuf );//todo for testing*/ //led_off( LED_RED ); /* warning: You shouldn't wait too long in the while loop, or else * you may encounter frame loss. However, the program should still * work properly even the delay time is an arbitrary value. No error * are allowed in this case. */ //hal_delay( 500 ); led_toggle( LED_RED ); } aloha_evolve(mac, NULL ); } #endif frame_close( rxbuf ); aloha_close( mac ); cc2420_close( cc ); }