/* _osx_evolve() * Everytime this function is called, one event is processed. */ void _osx_evolve( void * osxptr, TiEvent * e ) { bool pop = false; TiOSX * osx = (TiOSX *)osxptr; if (e == NULL) { e = osx_queue_front( osx->eventqueue ); pop = true; } if (e != NULL) { if (e->handler == NULL) { hal_assert(e->id != 0); dispa_send( osx->dispatcher, e ); } else{ e->handler( e->objectto, e ); } } if (pop) osx_queue_popfront(osx->eventqueue); }
TiBlockDeviceInterface * uart_get_blockinterface( TiUartAdapter * uart, TiBlockDeviceInterface * intf ) { hal_assert( intf != NULL ); memset( intf, 0x00, sizeof(TiBlockDeviceInterface) ); intf->provider = uart; intf->read = (TiFunBlockDeviceWrite)uart_read; intf->write = (TiFunBlockDeviceWrite)uart_write; intf->evolve = NULL; intf->switchtomode = NULL; return intf; }
/* * assume: the timer object is initialized successfully. */ TiAloha * aloha_open( TiAloha * mac, TiFrameTxRxInterface * rxtx, uint8 chn, uint16 panid, uint16 address, TiTimer * timer, TiFunEventHandler listener, void * lisowner, uint8 option ) { void * provider; // assert( the rxtx driver has already been opened ); // assert( mac->timer is already opened but not start yet ); rtl_assert((rxtx != NULL) && (timer != NULL)); mac->state = ALOHA_STATE_IDLE; mac->rxtx = rxtx; mac->timer = timer; mac->listener = listener; mac->lisowner = lisowner; mac->retry = 0; mac->backoff = CONFIG_ALOHA_MIN_BACKOFF; mac->panto = panid; mac->shortaddrto = FRAME154_BROADCAST_ADDRESS; mac->panfrom = panid; mac->shortaddrfrom = address; mac->seqid = 0; mac->sendoption = 0x00; mac->sendfailed = 0; mac->option = option; mac->txbuf = frame_open( &(mac->txbuf_memory[0]), FRAME_HOPESIZE(CONFIG_ALOHA_MAX_FRAME_SIZE), 0, 0, 0 ); hal_assert( mac->timer != NULL ); // initialize the frame transceiver component // attention enable ACK support for aloha protocol. the current implementation depends // on low level transceiver component to provide ACK mechanism. provider = rxtx->provider; rxtx->setchannel( provider, chn ); rxtx->setpanid( provider, panid ); rxtx->setshortaddress( provider, address ); rxtx->enable_addrdecode( provider ); rxtx->enable_autoack( provider ); ieee802frame154_open( &(mac->desc) ); // initialize the random number generator with a random seed. you can replace // the seed with other values. // // @attention: generally, the random generator can be initialized when the whole // application started. if so, you needn't call rand_open() to initialize it // again here. rand_open( 0x3212 ); return mac; }
/****************************************************************************** * @TODO 20061013 * if the uart adapter is driven by interrupt, then you should enable the interrupt * in configure function. however, whether the ISR really works or not still depends * on the global interrupt flag. * * @assume: the global interrupt should be disabled before calling this function. * @todo stop bits input is actually no use now. *****************************************************************************/ TiUartAdapter * uart_open( TiUartAdapter * uart, uint8 id, uint16 baudrate, uint8 databits, uint8 stopbits, uint8 option ) { //USART_InitTypeDef USART_InitStructure; /* assume: * - the global interrupt is disabled when calling this function * - you have already call HAL_SET_PIN_DIRECTIONS. the pin should initialized correctly. or else it doesn't work. */ uart->id = id; uart->baudrate = baudrate; uart->databits = databits; uart->stopbits = stopbits; uart->option = option; #ifdef CONFIG_UART_INTERRUPT_DRIVEN uart->txlen = 0; uart->txidx = 0; uart->rxlen = 0; uart->listener = NULL; uart->lisowner = NULL; #endif switch (id) { case 0: IO_PER_LOC_USART0_AT_PORT0_PIN2345(); SET_MAIN_CLOCK_SOURCE(CRYSTAL); UART_SETUP(0, 38400, HIGH_STOP); //UART_SETUP(0, 57600, HIGH_STOP); UTX0IF = 1; break; case 1: break; case 2: break; default: // not support now. hal_assert(false); uart = NULL; break; } return uart; }
void _osx_execute( TiOSX * osx ) { /* register default handler for dispatching later */ //dispa_attach( osx->dispatcher, EVENT_SLEEP, _osx_target_sleep ); //dispa_attach( osx->dispatcher, EVENT_WAKEUP, _osx_target_wakeup ); //dispa_attach( osx->dispatcher, EVENT_REBOOT, _osx_target_handler ); //dispa_attach( sche->dispatcher, EVENT_SHUTDOWN, _osx_target_shutdown ); //hal_setlistener( (TiFunEventHandler)_osx_post, osx ); hal_assert( g_osx != NULL ); hal_enable_interrupts(); while (1) { _osx_evolve( osx, NULL ); } _osx_close( g_osx ); }
TiFloodNetwork * flood_open( TiFloodNetwork * net, TiAloha * mac, TiFunEventHandler listener, void * lisowner, uint16 pan, uint16 localaddress ) { net->state = FLOOD_STATE_IDLE; net->mac = mac; net->panto = pan; net->panfrom = pan; net->localaddress = localaddress; net->distance = ~0; net->seqid = 0; net->listener = listener; net->lisowner = lisowner; net->txque = frame_open( (char * )( &net->txque_mem), FLOOD_FRAMEOBJECT_SIZE, 3, 20, 0); net->rxque = frame_open( (char *)( &net->rxque_mem), FLOOD_FRAMEOBJECT_SIZE, 3, 20, 0); net->rxbuf = frame_open( (char *)( &net->rxbuf_mem), FLOOD_FRAMEOBJECT_SIZE, 3, 20, 0); net->cache = flood_cache_open( (char *)( &net->cache_mem), FLOOD_CACHE_HOPESIZE ); hal_assert( net->cache != NULL ); return net; }
/** * open TiI2cAdapter for putchar/getchar */ TiI2cAdapter * i2c_open( TiI2cAdapter * i2c, uint8 id, uint8 rate ) { i2c->id = id; // i2c->rate = rate; switch (i2c->id) { case 0: TWBR = rate; // 设置比特率,根据SCL频率公式设定,主机模式下不小于10// TWDR = 0xFF; // Default content = SDA released// TWCR = 0x04; // TWEN置位,使能TWI接口;Disable Interupt and No Signal requests// break; case 1: // not supported now hal_assert( false ); break; default: i2c = NULL; break; } return i2c; }
void cpu_cli( void ) { hal_assert(false); }
void cpu_sti( void ) { hal_assert(false); }
void cpu_reboot( void ) { hal_assert(false); }
void cpu_sleep( void ) { hal_assert(false); }
/** * Construct a TiTimerAdapter object in the memory. This function should always be * success. */ TiTimerAdapter * timer_construct( char * buf, uint8 size ) { hal_assert( sizeof(TiTimerAdapter) <= size ); memset( buf, 0x00, size ); return (TiTimerAdapter *)buf; }
/* Construct a empty TiLightSensor object in the memory */ TiLightSensor * light_construct( char * buf, uint16 size ) { hal_assert( sizeof(TiLightSensor) <= size ); memset( buf, 0x00, size ); return (TiLightSensor *)buf; }
TiFloodNetwork * flood_construct( void * mem, uint16 size ) { hal_assert( sizeof(TiFloodNetwork) <= size ); memset( mem, 0x00, size ); return (TiFloodNetwork *)mem; }
TiUartAdapter * uart_construct( char * buf, uint16 size ) { hal_assert( sizeof(TiUartAdapter) <= size ); memset( buf, 0x00, size ); return (TiUartAdapter *)buf; }
void _osx_futurepost( TiOSX * osx, TiEvent * e, uint16 future ) { hal_assert(false); //osx_queue_pushback( osx->futqueue, e, future ); }
void _osx_rtpost( TiOSX * osx, TiEvent * e, uint16 deadline ) { hal_assert(false); //osx_queue_pushback( osx->rtqueue, e, deadline ); }
TiRtc * rtc_construct( char * buf, uint8 size ) { hal_assert( sizeof(TiRtc) <= size ); memset( buf, 0x00, size ); return (TiRtc *)buf; }
TiNanoSync * nanosync_construct( char * buf,uintx size) { memset(buf,0x00,size); hal_assert( sizeof(TiNanoSync)<=size); return ( TiNanoSync *)buf; }
TiAloha * aloha_construct( char * buf, uint16 size ) { hal_assert( sizeof(TiAloha) <= size ); memset( buf, 0x00, size ); return (TiAloha *)buf; }
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 CC2520_ACTIVATE(void) { //int i; // enable the SPI module which is used for communication between MCU and cc2520. RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // configure Port B Pin 14 // Port B Pin 14 is used for SPI's MISO (IPD means Input Pull Down). GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; GPIO_Init(GPIOB,&GPIO_InitStructure); // Port B Pin 1 is used for cc2520 RST // Port B Pin 5 is used for VREG_EN // Port B Pin 12 is used for NSS // GPIO_Mode_Out_PP here means Push Pull(推挽输出) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_5 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); // reset the cc2520 nRST GPIO_ResetBits(GPIOB, GPIO_Pin_1); // set VREG_EN which will enable the cc2520's internal voltage regulator GPIO_SetBits( GPIOB,GPIO_Pin_5); // wait for the regulator to be stabe. // @todo //for ( i=0;i<13500;i++); // hal_delayus(?) hal_delayms(2); // set the cc2520 nRST GPIO_SetBits(GPIOB,GPIO_Pin_1); hal_delayus(1); // reset the cc2520 CSn GPIO_ResetBits( GPIOB,GPIO_Pin_12); // @todo: shall we need to wait a little while after CS and then RST for stable? // @todo repalce with hal_delayus(?) hal_delayms(2); // todo //for ( i=0;i<13500;i++);//wait for the output of SO to be 1//todo for testing hal_assert(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14));//todo该语句报错,可能是因为SO引脚的 输出模式改变的原 // set the cc2520 CSn GPIO_SetBits(GPIOB, GPIO_Pin_12); hal_delayus(1); // configure Port B Pin 13 for SCK // configure Port B Pin 15 for SPI's MOSI GPIO_InitStructure.GPIO_Pin = SPI_pin_MOSI | SPI_pin_SCK; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init( GPIO_SPI, &GPIO_InitStructure); // configure Port B Pin 14 for MISO GPIO_InitStructure.GPIO_Pin = SPI_pin_MISO; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIO_SPI, &GPIO_InitStructure); // configure Port B Pin 12 for NSS GPIO_InitStructure.GPIO_Pin = SPI_pin_SS; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIO_SPI,&GPIO_InitStructure); }