void unit_aversive(void) { int16_t a, x, y; aversive_goto_xy_abs(&aversive_device, 100, 100); wait_done(&aversive_device); aversive_get_pos(&aversive_device, &a, &x, &y); lcd_string(3, 0, uint16_to_string((uint16_t)x)); lcd_string(3, 30, uint16_to_string((uint16_t)y)); }
static void do_addr(const char* line, uint8_t len) { uint16_t tmp; skip_ws(&line, &len); if (len == 0) { /* get address value */ write_ok(); uart_write((const uint8_t*)uint16_to_string(mem_addr), 4); write_eol(); } else { /* set address value */ if (string_to_uint16(line, &tmp)) goto on_error; mem_addr = tmp; write_ok(); } return ; on_error: write_ko(); return ; }
/********************************************************************* * @fn controlBLECentralEventCB * * @brief Central event callback function. * * @param pEvent - pointer to event structure * * @return none */ static void controlBLECentralEventCB( gapCentralRoleEvent_t *pEvent ) { switch ( pEvent->gap.opcode ) { case GAP_DEVICE_INIT_DONE_EVENT: { //LCD_WRITE_STRING( "BLE Central", HAL_LCD_LINE_1 ); // LCD_WRITE_STRING( bdAddr2Str( pEvent->initDone.devAddr ), HAL_LCD_LINE_2 ); } /***wkxboot***/ app_write_string("hello wkxboot&ble4.0!\r\n"); app_write_string("central role init done. own addr is:"); app_write_string(bdAddr2Str(pEvent->initDone.devAddr)); wkxboot_start_discover(); break; case GAP_DEVICE_INFO_EVENT: { app_write_string("\r\nfind device addr is:"); app_write_string(bdAddr2Str( pEvent->deviceInfo.addr )); // if filtering device discovery results based on service UUID if ( DEFAULT_DEV_DISC_BY_SVC_UUID == TRUE ) { if ( simpleBLEFindSvcUuid( PARAM_BLE_CAR_IN_PROFILE_SERV_UUID, pEvent->deviceInfo.pEvtData, pEvent->deviceInfo.dataLen ) ) { simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType ); } } /****wkxboot***/ // simpleBLEAddDeviceInfo( pEvent->deviceInfo.addr, pEvent->deviceInfo.addrType ); } break; case GAP_DEVICE_DISCOVERY_EVENT: { // discovery complete simpleBLEScanning = FALSE; // if not filtering device discovery results based on service UUID if ( DEFAULT_DEV_DISC_BY_SVC_UUID == FALSE ) { // Copy results simpleBLEScanRes = pEvent->discCmpl.numDevs; osal_memcpy( simpleBLEDevList, pEvent->discCmpl.pDevList, (sizeof( gapDevRec_t ) * pEvent->discCmpl.numDevs) ); } //LCD_WRITE_STRING_VALUE( "Devices Found", simpleBLEScanRes, // 10, HAL_LCD_LINE_1 ); if ( simpleBLEScanRes > 0 ) { LCD_WRITE_STRING( "<- To Select", HAL_LCD_LINE_2 ); } // initialize scan index to last device simpleBLEScanIdx = simpleBLEScanRes; /****wkxboot***/ app_write_string("scan completed!!\r\n"); app_write_string("scan num is:"); app_write_string(uint8_to_string(simpleBLEScanRes)); if(simpleBLEScanIdx--) { wkxboot_start_connect(); } else { wkxboot_start_discover(); } } break; case GAP_LINK_ESTABLISHED_EVENT: { if ( pEvent->gap.hdr.status == SUCCESS ) { simpleBLEState = BLE_STATE_CONNECTED; simpleBLEConnHandle = pEvent->linkCmpl.connectionHandle; simpleBLEProcedureInProgress = TRUE; // If service discovery not performed initiate service discovery if ( simpleBLECharHdl == 0 ) { osal_start_timerEx( simpleBLETaskId, START_DISCOVERY_EVT, DEFAULT_SVC_DISCOVERY_DELAY ); } //LCD_WRITE_STRING( "Connected", HAL_LCD_LINE_1 ); // LCD_WRITE_STRING( bdAddr2Str( pEvent->linkCmpl.devAddr ), HAL_LCD_LINE_2 ); app_write_string("connected peeraddr is:"); app_write_string(bdAddr2Str(pEvent->linkCmpl.devAddr)); app_write_string("connected connhandle is:"); app_write_string(uint16_to_string(simpleBLEConnHandle)); app_write_string("start to read RSSI!\r\n"); GAPCentralRole_StartRssi( simpleBLEConnHandle, DEFAULT_RSSI_PERIOD ); app_write_string("start to find services.....\r\n"); } else { simpleBLEState = BLE_STATE_IDLE; simpleBLEConnHandle = GAP_CONNHANDLE_INIT; simpleBLERssi = FALSE; simpleBLEDiscState = BLE_DISC_STATE_IDLE; //LCD_WRITE_STRING( "Connect Failed", HAL_LCD_LINE_1 ); //LCD_WRITE_STRING_VALUE( "Reason:", pEvent->gap.hdr.status, 10, HAL_LCD_LINE_2 ); app_write_string("connect failed!reason is:"); app_write_string(uint8_to_string(pEvent->gap.hdr.status)); } } break; case GAP_LINK_TERMINATED_EVENT: { simpleBLEState = BLE_STATE_IDLE; simpleBLEConnHandle = GAP_CONNHANDLE_INIT; simpleBLERssi = FALSE; simpleBLEDiscState = BLE_DISC_STATE_IDLE; simpleBLECharHdl = 0; simpleBLEProcedureInProgress = FALSE; // LCD_WRITE_STRING( "Disconnected", HAL_LCD_LINE_1 ); // LCD_WRITE_STRING_VALUE( "Reason:", pEvent->linkTerminate.reason, // 10, HAL_LCD_LINE_2 ); app_write_string("disconnected !reason is:"); app_write_string(uint8_to_string(pEvent->gap.hdr.status)); app_write_string("scan again!!\r\n"); wkxboot_start_discover(); } break; case GAP_LINK_PARAM_UPDATE_EVENT: { // LCD_WRITE_STRING( "Param Update", HAL_LCD_LINE_1 ); app_write_string("link param update!\r\n"); } break; default: break; } }
/********************************************************************* * @fn simpleBLEGATTDiscoveryEvent * * @brief Process GATT discovery event * * @return none */ static void simpleBLEGATTDiscoveryEvent( gattMsgEvent_t *pMsg ) { attReadByTypeReq_t req; if ( simpleBLEDiscState == BLE_DISC_STATE_SVC ) { // Service found, store handles if ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->msg.findByTypeValueRsp.numInfo > 0 ) { simpleBLESvcStartHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].handle; simpleBLESvcEndHdl = pMsg->msg.findByTypeValueRsp.handlesInfo[0].grpEndHandle; app_write_string("find service start is:"); app_write_string(uint16_to_string(simpleBLESvcStartHdl)); app_write_string("find service end is:"); app_write_string(uint16_to_string(simpleBLESvcEndHdl)); } // If procedure complete if ( ( pMsg->method == ATT_FIND_BY_TYPE_VALUE_RSP && pMsg->hdr.status == bleProcedureComplete ) || ( pMsg->method == ATT_ERROR_RSP ) ) { if ( simpleBLESvcStartHdl != 0 ) { // Discover characteristic simpleBLEDiscState = BLE_DISC_STATE_CHAR; req.startHandle = simpleBLESvcStartHdl; req.endHandle = simpleBLESvcEndHdl; req.type.len = ATT_BT_UUID_SIZE; req.type.uuid[0] = LO_UINT16(PARAM_BLE_CAR_IN_CHAR_UUID); req.type.uuid[1] = HI_UINT16(PARAM_BLE_CAR_IN_CHAR_UUID); GATT_ReadUsingCharUUID( simpleBLEConnHandle, &req, simpleBLETaskId ); app_write_string("start to read charac.\r\n"); } /****wkxboot***/ else { app_write_string("ATT_ERROR_RSP.\r\n"); } } } else if ( simpleBLEDiscState == BLE_DISC_STATE_CHAR ) { // Characteristic found, store handle if ( pMsg->method == ATT_READ_BY_TYPE_RSP && pMsg->msg.readByTypeRsp.numPairs > 0 ) { simpleBLECharHdl = BUILD_UINT16( pMsg->msg.readByTypeRsp.dataList[0], pMsg->msg.readByTypeRsp.dataList[1] ); app_write_string("Simple Svc Found.char handle is:\r\n"); app_write_string(uint16_to_string(simpleBLECharHdl)); /***wkxboot***/ //LCD_WRITE_STRING( "Simple Svc Found", HAL_LCD_LINE_1 ); simpleBLEProcedureInProgress = FALSE; } /***wkxboot***/ simpleBLEDiscState = BLE_DISC_STATE_IDLE; /***wkxboot***/ } }
int main(void) { uint16_t counter; uint8_t x; #define CONFIG_CHECK 0 #if CONFIG_CHECK uint16_t error; #endif /* setup spi first */ spi_setup_master(); spi_set_sck_freq(SPI_SCK_FREQ_FOSC2); uart_setup(); /* setup timer1, normal mode, interrupt on match 0xffff */ OCR1A = 0xffff; TCCR1A = 0; TCCR1B = 0; TCCR1C = 0; TIMSK1 = 1 << 1; /* enable interrupts */ sei(); nrf24l01p_setup(); /* sparkfun usb serial board configuration */ /* NOTE: nrf24l01p_enable_crc8(); for nrf24l01p board */ /* nrf24l01p_enable_crc16(); */ nrf24l01p_disable_crc(); /* auto ack disabled */ /* auto retransmit disabled */ /* 4 bytes payload */ /* 1mbps, 0dbm */ /* nrf24l01p_set_rate(NRF24L01P_RATE_1MBPS); */ nrf24l01p_set_rate(NRF24L01P_RATE_2MBPS); /* nrf24l01p_set_rate(NRF24L01P_RATE_250KBPS); */ /* channel 2 */ nrf24l01p_set_chan(2); /* 5 bytes addr width */ /* nrf24l01p_set_addr_width(NRF24L01P_ADDR_WIDTH_5); */ nrf24l01p_set_addr_width(NRF24L01P_ADDR_WIDTH_3); /* rx address */ nrf24l01p_cmd_buf[0] = 0xe7; nrf24l01p_cmd_buf[1] = 0xe7; nrf24l01p_cmd_buf[2] = 0xe7; nrf24l01p_cmd_buf[3] = 0xe7; nrf24l01p_cmd_buf[4] = 0xe7; nrf24l01p_write_reg40(NRF24L01P_REG_RX_ADDR_P0); /* tx address */ nrf24l01p_cmd_buf[0] = 0xe7; nrf24l01p_cmd_buf[1] = 0xe7; nrf24l01p_cmd_buf[2] = 0xe7; nrf24l01p_cmd_buf[3] = 0xe7; nrf24l01p_cmd_buf[4] = 0xe7; nrf24l01p_write_reg40(NRF24L01P_REG_TX_ADDR); /* enable tx no ack command */ nrf24l01p_enable_tx_noack(); nrf24l01p_powerdown_to_standby(); uart_write((uint8_t*)"rx side\r\n", 9); redo_receive: #if CONFIG_CHECK error = 0; #endif counter = 0; is_timer1_irq = 0; uart_write((uint8_t*)"press space\r\n", 13); uart_read_uint8(&x); uart_write((uint8_t*)"starting\r\n", 10); nrf24l01p_standby_to_rx(); /* wait for the starting packet */ if (nrf24l01p_is_rx_full()) nrf24l01p_flush_rx(); while (nrf24l01p_is_rx_irq() == 0) ; nrf24l01p_read_rx(); /* clock source disabled, safe to access 16 bits counter */ TCNT1 = 0; /* prescaler set to 256 */ /* interrupt every 16000000 / (65535 * 256) = 0.954 s */ TCCR1B = 1 << 2; while (1) { if (is_timer1_irq == 1) { break ; } else if (nrf24l01p_is_rx_irq()) { #if CONFIG_CHECK uint8_t i; for (i = 0; i < NRF24L01P_PAYLOAD_WIDTH; ++i) nrf24l01p_cmd_buf[i] = 0x00; #endif nrf24l01p_read_rx(); #if CONFIG_CHECK for (i = 0; i < NRF24L01P_PAYLOAD_WIDTH; ++i) { if (nrf24l01p_cmd_buf[i] != (0x2a + i)) { ++error; break ; } } #endif ++counter; } } /* print counter */ uart_write((uint8_t*)"counter: ", 9); uart_write((uint8_t*)uint16_to_string(counter), 4); uart_write((uint8_t*)"\r\n", 2); #if CONFIG_CHECK /* print error */ uart_write((uint8_t*)"error: ", 7); uart_write((uint8_t*)uint16_to_string(error), 4); uart_write((uint8_t*)"\r\n", 2); #endif /* still in rx mode */ nrf24l01p_set_standby(); goto redo_receive; return 0; }
int main(void) { uint16_t counter; uint8_t x; /* setup spi first */ spi_setup_master(); spi_set_sck_freq(SPI_SCK_FREQ_FOSC2); uart_setup(); /* setup timer1, normal mode, interrupt on match 0x8000 */ OCR1A = 0x8000; TCCR1A = 0; TCCR1B = 0; TCCR1C = 0; TIMSK1 = 1 << 1; /* enable interrupts */ sei(); nrf24l01p_setup(); /* sparkfun usb serial board configuration */ /* NOTE: nrf24l01p_enable_crc8(); for nrf24l01p board */ /* nrf24l01p_enable_crc16(); */ nrf24l01p_disable_crc(); /* auto ack disabled */ /* auto retransmit disabled */ /* 4 bytes payload */ /* 1mbps, 0dbm */ /* nrf24l01p_set_rate(NRF24L01P_RATE_1MBPS); */ nrf24l01p_set_rate(NRF24L01P_RATE_2MBPS); /* nrf24l01p_set_rate(NRF24L01P_RATE_250KBPS); */ /* channel 2 */ nrf24l01p_set_chan(2); /* 5 bytes addr width */ /* nrf24l01p_set_addr_width(NRF24L01P_ADDR_WIDTH_5); */ nrf24l01p_set_addr_width(NRF24L01P_ADDR_WIDTH_3); /* rx address */ nrf24l01p_cmd_buf[0] = 0xe7; nrf24l01p_cmd_buf[1] = 0xe7; nrf24l01p_cmd_buf[2] = 0xe7; nrf24l01p_cmd_buf[3] = 0xe7; nrf24l01p_cmd_buf[4] = 0xe7; nrf24l01p_write_reg40(NRF24L01P_REG_RX_ADDR_P0); /* tx address */ nrf24l01p_cmd_buf[0] = 0xe7; nrf24l01p_cmd_buf[1] = 0xe7; nrf24l01p_cmd_buf[2] = 0xe7; nrf24l01p_cmd_buf[3] = 0xe7; nrf24l01p_cmd_buf[4] = 0xe7; nrf24l01p_write_reg40(NRF24L01P_REG_TX_ADDR); /* enable tx no ack command */ nrf24l01p_enable_tx_noack(); nrf24l01p_powerdown_to_standby(); uart_write((uint8_t*)"tx side\r\n", 9); redo_transmit: counter = 0; is_timer1_irq = 0; uart_write((uint8_t*)"press space\r\n", 13); uart_read_uint8(&x); uart_write((uint8_t*)"starting\r\n", 10); nrf24l01p_standby_to_tx(); if (nrf24l01p_is_tx_empty() == 0) nrf24l01p_flush_tx(); /* clock source disabled, safe to access 16 bits counter */ TCNT1 = 0; /* prescaler set to 1024 */ /* interrupt every 2.01s (match on 0x8000) */ TCCR1B = 5 << 0; while (1) { #define CONFIG_CHECK 0 #if CONFIG_CHECK uint8_t i; for (i = 0; i < NRF24L01P_PAYLOAD_WIDTH; ++i) nrf24l01p_cmd_buf[i] = 0x2a + i; #endif nrf24l01p_write_tx_noack(); while (nrf24l01p_is_tx_irq() == 0) ; ++counter; if (is_timer1_irq == 1) break ; } /* print counter */ uart_write((uint8_t*)"counter: ", 9); uart_write((uint8_t*)uint16_to_string(counter), 4); uart_write((uint8_t*)"\r\n", 2); goto redo_transmit; return 0; }
static void write_uint16(uint16_t x) { uart_write(uint16_to_string(x), 4); write_eol(); }