Other Parts Discussed in Thread: CC2540
Hi,
I am new user, i am trying to use PAN1326B for my project. but I dont know how to begin. Can you help me?
My project plan: I use ATTiny1634 to connect (UART) with PAN1326B (CC2560). i want to communicate with another device by bluetooth.
I am using atmel studio to programm for it, and I use reference from https://github.com/RedBearLab/BLE_HCI/tree/master/arduino.
Can I use HCI command from this library (CC2540) for my project (CC2560) ?
I am waiting for some components, so I cant test my circuit now.
this my code:
Thank you so much
hng
sorry for my bad english
/* * main.c * * Created: 6/26/2017 11:28:29 AM * BLE_HCI central */ #define F_CPU 8000000 #include <avr/io.h> #include <stdio.h> #include <avr/interrupt.h> #include <util/delay.h> #include "CPble_UART.h" //#include "CPble_HCI_driver.h" //#include "CPble_PCM_driver.h" #include "CPble_MSPIM.h" #include <stdlib.h> #include <string.h> #include "CPble_typedef.h" char found_address[6]; char ble_event_process() { //uint8_t type, event_code, data_len, status1; //uint16_t event; //uint8_t buf[64]; char type, event_code, data_len, status1; char event; char buf[64]; UART0_Init(); // UART0_Receive(); receive a char, type = UART0_Receive(); _delay_ms(35); event_code = UART0_Receive(); data_len = UART0_Receive(); UART1_Init(); SerialPrint1("-----------------------------\r\n"); SerialPrint1("-Type :"); SerialPrint1(type); SerialPrint1("\n"); SerialPrint1("-EventCode :"); SerialPrint1(event_code); SerialPrint1("\n"); SerialPrint1("-Data Length :"); SerialPrint1(data_len); SerialPrint1("\n"); for (int i = 0; i < data_len; i++) { buf[i] = UART0_Receive();} event = BUILD_UINT16(buf[0], buf[1]); status1 = buf[2]; SerialPrint1("-Event :"); SerialPrint1(event); SerialPrint1("\n"); SerialPrint1("-Status :"); SerialPrint1(status1); SerialPrint1("\n"); switch (event) { case 0x060D: // GAP_DeviceInformation { SerialPrint1("GAP_DeviceInformation:\n"); int8_t rssi = buf[11]; SerialPrint1("RSSI :"); // RSSI Receiev Signal Strength Indication SerialPrint1(rssi); SerialPrint1("\n"); SerialPrint1("MAC Address:"); SerialPrint1(buf[10]); SerialPrint1(buf[9]);SerialPrint1(buf[8]);SerialPrint1(buf[7]);SerialPrint1(buf[6]);SerialPrint1(buf[5]); SerialPrint1("\n"); } break; case 0x0601: // GAP_DeviceDiscoveryDone { SerialPrint1("GAP_DeviceDiscoveryDone\n"); uint8_t num_devs = buf[3]; SerialPrint1("NumDevs :"); // RSSI Receiev Signal Strength Indication SerialPrint1(num_devs); SerialPrint1("\n"); if (num_devs > 0) memcpy(found_address, &buf[6], 6); // store 1 device address only in this demo } break; case 0x051B: // ATT_HandleValueNotification { SerialPrint1("ATT_HandleValueNotification\r\n"); uint8_t data[21] = {0}; uint8_t len = data_len - 8; if (len > 20) len = 20; memcpy(data, &buf[8], len); SerialPrint1(" -------------> Received from cpble peripheral:"); SerialPrint1(data);SerialPrint1("\n"); } break; default: SerialPrint1(" -> No connecting\n"); } } int main() { SerialPrint1("Step1: cpble_central_init\n"); cpble_central_init(); SerialPrint1("Step2: ble_event_process\n"); ble_event_process(); int intval; UART1_Init(); while(1){ char str = UART1_Receive(); switch(str) { case 'D': case 'd': SerialPrint1(" -> Start discovery...\r\n"); cpble_central_start_discovery(); break; case 'E': case 'e': SerialPrint1(" -> Connecting to cpble peripheral...\r\n"); cpble_central_connect(found_address); break; case 'N': case 'n': SerialPrint1(" -> Enable notification to receive data...\r\n"); cpble_central_enable_notification(); break; case '1': SerialPrint1(" -> Send \"Hello World!\" to the cpble peripheral...\r\n"); cpble_central_write_bytes((uint8_t *)"Hello World!\r\n", 14); break; case '2': SerialPrint1(" -> Send \"I love BLE!\" to the cpble peripheral...\r\n"); cpble_central_write_bytes((uint8_t *)"I love BLE!\r\n", 13); break; default: SerialPrint1(" -> Invalid command.\r\n"); } } } /* int main() { //int byte, val, val1; int intval; char str[30]; intval = 50; //itoa(intval, str, 10); //convI8toStr(str, intval); adc_init(); UART0_Init(); UART1_Init(); while(1){ /*print adc value ****A int adc_value = read_adc(0x03);// 0x03 pin PA6 itoa(adc_value, str, 10); // receive data from port0 //Serialgets0(str); // send back data from port1 SerialPrint1("the ADC value: "); SerialPrint1(str);SerialPrint1("\n"); //str[0] = UART0_Receive(); //byte=atoi(str); // return integer values //UART0_Init(); //UART0_Sendstr(str);//SerialPrint1("\n"); } } */ /* ready and replay int main() { //int byte, val, val1; uint8_t intval; char str[30]; char buf; UART1_Init(); while(1){ //*print adc value ****A Serialgets1(str); //intval = atoi(str); // return integer values //str[0] = UART1_Receive(); //intval=atoi(str); //UART0_Init(); SerialPrint1(str); } } */ /* int main(void) { UART1_Init(); SerialPrint1("Start:\n"); SerialPrint1("Wait for data:\n"); unsigned char receivedata; //int val; while(1) { // doesnt work receivedata = UART1_Receive(); SerialPrint1(receivedata); //SerialPrint0(""Start:\n"); //SerialPrint1("Hello the world\n"); _delay_ms(500); // Send back //SerialPrint0("echo:\n"); //_delay_ms(200); //SerialPrint0(receivedata); //UART0_Transmit(receivedata); // _delay_ms(200); } } */ /* int main() { int byte, val, val1; char str[20]; UART0_Init(); while(1){ Serialgets(str); byte=atoi(str); val = atoi(str); // sprintf(str,"data retrived from mem= 0x%x\n", val ); //sprintf(str) SerialPrint0("String value ="); SerialPrint0(str); SerialPrint0("\n"); SerialPrint0("Int value ="); SerialPrint0(val1); SerialPrint0("\n"); strcpy(str, "tutorialspoint.com"); val = atoi(str); val1 = sprintf(str,"data retrived from mem= 0x%x\n", val ); SerialPrint0("String value ="); SerialPrint0(str); SerialPrint0("\n"); SerialPrint0("Int value ="); SerialPrint0(val1); SerialPrint0("\n"); //return(0); //} } */
/* * CPble_hci.c * * Created: 6/30/2017 1:19:10 PM * */ //#include <AltSoftSerial.h> #include "CPble_typedef.h" #include "CPble_hci.h" #include <string.h> #include <stdio.h> #include <stdint.h> uint8_t GAP_DeviceInit( uint8_t taskID, uint8_t profileRole, uint8_t maxScanResponses, uint8_t *pIRK, uint8_t *pSRK, uint32_t *pSignCounter ) { uint8_t buf[42]; uint8_t len = 0; buf[len++] = 0x01; // -Type : 0x01 (Command) buf[len++] = 0x00; // -Opcode : 0xFE00 (GAP_DeviceInit) buf[len++] = 0xFE; buf[len++] = 0x26; // -Data Length 0x26bytes (38bytes) = 1 + 1 + 16 + 16 + 4 buf[len++] = profileRole; // Profile Role buf[len++] = maxScanResponses; // MaxScanRsps memcpy(&buf[len], pIRK, 16); // IRK // copy 16bytes from pIRK to buf[] len += 16; memcpy(&buf[len], pSRK, 16); // SRK // copy 16bytes from pSRK to buf[] len += 16; memcpy(&buf[len], pSignCounter, 4); // SignCounter // copy 4bytes from pSignCounter to buf[] len += 4; UART0_Init(); SerialPrintln0(buf, len); //AltSoftSerial.write(buf, len); return 1; } uint8_t GAP_DeviceDiscoveryRequest( gapDevDiscReq_t *pParams ) { uint8_t buf[20]; uint8_t len = 0; buf[len++] = 0x01; // -Type : 0x01 (Command) buf[len++] = 0x04; // -Opcode : 0xFE04 (GAP_DeviceDiscoveryRequest) buf[len++] = 0xFE; buf[len++] = 0x03; // -Data Length 0x03bytes (3bytes) = 1 + 1 + 1 buf[len++] = pParams->mode; // Mode buf[len++] = pParams->activeScan; // ActiveScan buf[len++] = pParams->whiteList; // WhiteList // hci_tl_write(buf, len); // hci_tl_wait_for_response(); UART0_Init(); SerialPrintln0(buf, len); //AltSoftSerial.write(buf, len); return 1; } uint8_t GAP_EstablishLinkReq( gapEstLinkReq_t *pParams ) { uint8_t buf[20]; uint8_t len = 0; buf[len++] = 0x01; // memcpy(&buf[len], "\x09\xFE", 2); len += 2; buf[len++] = 0x03 + B_ADDR_LEN; buf[len++] = pParams->highDutyCycle; buf[len++] = pParams->whiteList; buf[len++] = pParams->addrTypePeer; memcpy(&buf[len], pParams->peerAddr, B_ADDR_LEN); len+=B_ADDR_LEN; UART0_Init(); SerialPrintln0(buf, len); return 1; } uint8_t GATT_WriteCharValue( uint16_t connHandle, attWriteReq_t *pReq, uint8_t taskId ) { uint8_t buf[20]; uint8_t len = 0; buf[len++] = 0x01; memcpy(&buf[len], "\x92\xFD", 2); len += 2; buf[len++] = 0x04 + pReq->len; memcpy(&buf[len], &connHandle, 2); len += 2; memcpy(&buf[len], &pReq->handle, 2); len += 2; memcpy(&buf[len], pReq->value, pReq->len); len += pReq->len; UART0_Init(); SerialPrintln0(buf, len); return 1; } uint8_t GATT_WriteNoRsp( uint16_t connHandle, attWriteReq_t *pReq, uint8_t taskId ) { uint8_t buf[20]; uint8_t len = 0; buf[len++] = 0x01; memcpy(&buf[len], "\xB6\xFD", 2); len += 2; buf[len++] = 0x04 + pReq->len; memcpy(&buf[len], &connHandle, 2); len += 2; memcpy(&buf[len], &pReq->handle, 2); len += 2; memcpy(&buf[len], pReq->value, pReq->len); len += pReq->len; UART0_Init(); SerialPrintln0(buf, len); //AltSoftSerial.write(buf, len); return 1; } /*********************************** * Master device *************************************/ /* void biscuit_central_init() { GAPCentralRole_StartDevice(); } static uint8_t gapCentralRoleTaskId = 0; static uint8_t gapCentralRoleIRK[KEYLEN] = {0}; static uint8_t gapCentralRoleSRK[KEYLEN] = {0}; static uint32_t gapCentralRoleSignCounter = 1; static uint8_t gapCentralRoleBdAddr[B_ADDR_LEN]; static uint8_t gapCentralRoleMaxScanRes = 5; */ static uint8_t gapCentralRoleTaskId = 0; static uint8_t gapCentralRoleIRK[KEYLEN] = {0}; static uint8_t gapCentralRoleSRK[KEYLEN] = {0}; static uint32_t gapCentralRoleSignCounter = 1; static uint8_t gapCentralRoleBdAddr[B_ADDR_LEN]; static uint8_t gapCentralRoleMaxScanRes = 5; uint8_t GAPCentralRole_StartDevice() { // gapCentralRoleTaskId = 0, // GAP_PROFILE_CENTRAL = 0x08 // gapCentralRoleMaxScanRes = 5 // gapCentralRoleIRK = gapCentralRoleIRK[KEYLEN] = {0}; // KEYLEN= 16 // gapCentralRoleSRK = {0}; // gapCentralRoleSignCounter = 1 return GAP_DeviceInit( gapCentralRoleTaskId, GAP_PROFILE_CENTRAL, gapCentralRoleMaxScanRes, gapCentralRoleIRK, gapCentralRoleSRK, &gapCentralRoleSignCounter ); } uint8_t GAPCentralRole_EstablishLink( uint8_t highDutyCycle, uint8_t whiteList, uint8_t addrTypePeer, uint8_t *peerAddr ) { gapEstLinkReq_t params; params.taskID = gapCentralRoleTaskId; params.highDutyCycle = highDutyCycle; params.whiteList = whiteList; params.addrTypePeer = addrTypePeer; memcpy( params.peerAddr, peerAddr, B_ADDR_LEN ); return GAP_EstablishLinkReq( ¶ms ); } uint8_t GAPCentralRole_StartDiscovery( uint8_t mode, uint8_t activeScan, uint8_t whiteList ) { gapDevDiscReq_t params; params.taskID = gapCentralRoleTaskId; params.mode = mode; params.activeScan = activeScan; params.whiteList = whiteList; return GAP_DeviceDiscoveryRequest( ¶ms ); }