int HexrecSign(int a) { int i,crc; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE); i=FlashErase(_SIGN_PAGE); CRC_ResetDR(); crc=CRC_CalcBlockCRC((uint32_t *)a,0x8000); i |= FlashProgram32((int)_FW_CRC,crc); // vpisi !!! i |= FlashProgram32((int)_FW_SIZE,0x8000); i |= FlashProgram32((int)_FW_START,a); CRC_ResetDR(); crc=CRC_CalcBlockCRC((uint32_t *)_FW_SIZE,3); i |= FlashProgram32((int)_SIGN_CRC,crc); return i; }
int main(void){ SystemInit(); usart1_init(); // enable clock for CRC unit RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); unsigned int data = 0x12345678; unsigned int result = CRC_CalcCRC(data); printf("single word crc calculation:\r\n"); printf(" data = 0x%08x\r\n", (unsigned int)data); printf(" result = 0x%08x\r\n", (unsigned int)result); CRC_ResetDR(); int len_datablock = sizeof(data_block); result = CRC_CalcBlockCRC(data_block, len_datablock); printf("block crc calculation (%d bytes):\r\n", len_datablock); printf(" data block can be found in source.\r\n"); printf(" result = 0x%08x\r\n", result); for(;;) { } return 0; }
bool CC2500_PacketCheck() { u8 i; u32 *pu32 = NULL; RSSI = CC2500_ReadStatus(CCxxx0_RSSI); LQI = CC2500_ReadStatus(CCxxx0_LQI); CC2500_ReceivePacket(gPacketbuf); ReceiveTime = Timer2Counter; CC2500_SetRxd(); pu32 = (u32*) &gPacketbuf[1]; CRC_ResetDR(); if (CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1) == pu32[CRC_LENGTH - 1]) { if (NodeTimestamp[gPacketbuf[SRC_NUM]] != pu32[CRC_LENGTH - 2]) { NodeTimestamp[gPacketbuf[SRC_NUM]] = pu32[CRC_LENGTH - 2]; return TRUE; } else { return FALSE; } } else { return FALSE; } }
/* * 函数名:main * 描述 :主函数 * 输入 :无 * 输出 :无 */ int main(void) { uint8_t i = 0; /* USART1 config 115200 8-N-1 */ USART1_Config(); /* 使能CRC时钟 */ CRC_Config(); printf("\r\n 这是一个 CRC(循环冗余校验)实验 \r\n"); /* Compute the CRC of "DataBuffer" */ for(i=0; i<BUFFER_SIZE; i++ ) { CRCValue = CRC_CalcBlockCRC((uint32_t *)DataBuffer, BUFFER_SIZE); printf("\r\n32-bit CRC 校验码为:0X%X\r\n", CRCValue); } printf("\r\nCRC(循环冗余校验)测试成功\r\n"); for(;;) { } }
uint16_t set_CRC(uint8_t*pBuf, uint16_t lenght) { uint16_t u32_len, CRCValue, u8_len; if((lenght%4) != 0) { u32_len = (lenght/4)+1; u8_len = u32_len*4; } else { u32_len = lenght/4; u8_len = lenght; } if(u8_len > (RX_BUFSIZE-2)) return 0; CRC_ResetDR(); CRCValue = CRC_CalcBlockCRC((uint32_t *)pBuf, u32_len); memcpy(pBuf+u8_len, &CRCValue, 2); u8_len += 2; return u8_len; }
/* Private function ----------------------------------------------------------*/ int main(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Set the Vector Table base adress at 0x8004000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x4000); /* Enable clock for GPIOB port */ RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE); /* LED0 -> PB0 LED1 -> PB1 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB, &GPIO_InitStructure); USART1_Init(); USART1_Print("\r\n"); USART1_Print("*************************************************************\r\n"); USART1_Print("* *\r\n"); USART1_Print("* Thank you for using MatchboxARM Development Board ! ^_^ *\r\n"); USART1_Print("* *\r\n"); USART1_Print("*************************************************************\r\n"); /* Enable CRC clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); /* Compute the CRC of "DataBuffer" */ CRCValue = CRC_CalcBlockCRC((uint32_t *)DataBuffer, BUFFER_SIZE); /* Display the computed CRC variable */ USART1_Print("The CRC of the buffer is = "); USART1_Print_Int(CRCValue); USART1_Print("\n\r"); /* Infinite loop, just toggle leds */ while(1) { /* LED0-ON LED1-OFF */ GPIO_SetBits(GPIOB , GPIO_Pin_0); GPIO_ResetBits(GPIOB , GPIO_Pin_1); Delay(0xfffff); Delay(0xfffff); Delay(0xfffff); Delay(0xfffff); Delay(0x5ffff); /* LED0-OFF LED1-ON */ GPIO_ResetBits(GPIOB , GPIO_Pin_0); GPIO_SetBits(GPIOB , GPIO_Pin_1); Delay(0xfffff); Delay(0xfffff); Delay(0xfffff); Delay(0xfffff); Delay(0x5ffff); } }
int crcSIGN(void) { int i=-1,crc; #if defined (STM32F10X_HD) RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); #elif defined (STM32F2XX) RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE); #endif if(_Words32Received) { i=EraseFLASH(_SIGN_PAGE); CRC_ResetDR(); crc=CRC_CalcBlockCRC((uint32_t *)_FLASH_TOP,_Words32Received); i |= FlashProgram32((int)_FW_CRC,crc); // vpisi !!! i |= FlashProgram32((int)_FW_SIZE,_Words32Received); CRC_ResetDR(); crc=CRC_CalcBlockCRC(_FW_SIZE,2); i |= FlashProgram32((int)_SIGN_CRC,crc); } return i; }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* Setup the microcontroller system. Initialize the Embedded Flash Interface, initialize the PLL and update the SystemFrequency variable. */ SystemInit(); /* Enable CRC clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); /* Compute the CRC of "DataBuffer" */ CRCValue = CRC_CalcBlockCRC((uint32_t *)DataBuffer, BUFFER_SIZE); while (1) { } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file */ /* Enable CRC clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); /* Compute the CRC of "DataBuffer" */ CRCValue = CRC_CalcBlockCRC((uint32_t *)DataBuffer, BUFFER_SIZE); while (1) { } }
bool check_CRC(uint8_t* pBuf, uint16_t lenght) { uint16_t u32_len, CRCValue, crc; if(lenght > RX_BUFSIZE) return false; u32_len = (lenght-2)/4; //转换为u32的长度 memcpy(&CRCValue, pBuf+lenght-2, 2); CRC_ResetDR(); crc = CRC_CalcBlockCRC((uint32_t *)pBuf, u32_len); if(CRCValue != crc) return false; return true; }
int main(int argc, char** argv) { unsigned long argLen = strlen(argv[1]); unsigned long remainder = argLen % 8; unsigned long numWords = argLen / 8; if (remainder != 0){ numWords++; } uint32_t inputArray[numWords]; int index, start = 0, length = 8; char intString[9]; intString[8] = '\0'; for (index = 0; index < numWords; index++) { memcpy(&intString, argv[1] + start, length); sscanf(intString, "%x", inputArray+index); memset(intString, 0, 8); start += 8; } uint32_t res = CRC_CalcBlockCRC(inputArray, numWords); printf("%02u\n", res); }
static uint32_t s_hal_crc32_hw_compute(hal_crc_info_t *info, const void *data, uint32_t size) { uint8_t tailsize = 0; if(0xffffffff == info->initialvalue) { CRC_ResetDR(); } info->initialvalue = CRC_CalcBlockCRC_networkorder((uint32_t*)data, size/4); tailsize = size%4; if(0 != (tailsize)) { uint8_t tail[4]; memset(tail, 0, 4); memcpy(tail, ((uint8_t*)data) + size-tailsize, tailsize); info->initialvalue = CRC_CalcBlockCRC((uint32_t*)tail, 1); } return(info->initialvalue); }
/** * @brief Main program. * @param None * @retval None */ void CRC_32BitsCRCMessage(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f30x.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f30x.c file */ /* Initialize LEDs available on STM32303C-EVAL board */ STM_EVAL_LEDInit(LED1); STM_EVAL_LEDInit(LED3); /* Configure the CRC peripheral to use the polynomial X32 + X26 + X23 + X22 + X16 + X12 + X11 + X10 +X8 + X7 + X5 + X4 + X2+ X +1 */ CRC_Config(0x4C11DB7); /* Compute the CRC value of the 8-bit buffer: CRCBuffer */ ComputedCRC = CRC_CalcBlockCRC(CRCBuffer, BUFFER_SIZE); /* Check if the computed CRC matches the expected one */ if (ComputedCRC != ExpectedCRC) { /* Turn on LD3 */ STM_EVAL_LEDOn(LED3); } else { /* Turn on LD1 */ STM_EVAL_LEDOn(LED1); } /* Infinite loop */ while(1) { } }
unsigned int HardGenCrc32(unsigned int* puData, unsigned int uSize) { CRC_ResetDR(); return CRC_CalcBlockCRC((unsigned int *)puData, (unsigned int)uSize); }
void DMA1_Stream0_IRQHandler(void) { static uint16_t msg_id[2] = {0,0}; static int wrr = 0; static int msg_index = 0; static uint16_t data_temp[2] = {0,0}; static uint32_t mode = DLE_STX; uint32_t *pdata = (uint32_t*)&data_temp[0]; uint16_t *padpcm_msg; uint32_t* pin; uint32_t* pout; uint32_t crc; int i,j,id,adr; DMA_ClearFlag(DMA1_Stream0, DMA_IT_TC | DMA_IT_TE); DMA_ClearITPendingBit(DMA1_Stream0, DMA_IT_TCIF0 | DMA_IT_TEIF0); id = DMA_GetCurrentMemoryTarget(DMA1_Stream0); for(i = 0; i < SPI_RX_DMA; i++) { data_temp[1] = data_temp[0]; data_temp[0] = spi_dma_buffer[id][i]; if(mode == DLE_STX) { if(*pdata == DLE_STX) { mode = DLE_ETX; padpcm_msg = (uint16_t*)&adpcm_msg[msg_index]; wrr = 0; } } else if(mode == DLE_ETX) { if(*pdata == DLE_ETX) { CRC_ResetDR(); crc = CRC_CalcBlockCRC(((uint32_t*)&adpcm_msg[msg_index])+1,msg_32bit_size-1); if(crc == adpcm_msg[msg_index].crc) { if(adpcm_msg[msg_index].msg_id == F415_CHECK_SPI_CONNECT+100) { t_info.f415_spi1_error = 0; t_info.f415_mode = F415_STOP_MODE; } else if(adpcm_msg[msg_index].msg_id == F415_AUDIO_STREAM) { t_info.f415_mode = F415_AUDIO_STREAM_MODE; for(i = 0; i < MAX_CHANNEL; i++) { id = adpcm_ctrl[i].id; adr = adpcm_ctrl[i].adr++; pin = (uint32_t*)&padpcm[id][i]->adpcm_data[adr][0]; pout = (uint32_t*)&adpcm_msg[msg_index].adpcm_block[i].adpcm_data[0]; for(j = 0; j < 28; j++) *(pin++) = *(pout++); if(adr == 0) { padpcm[id][i]->prevsample = adpcm_msg[msg_index].adpcm_block[i].prevsample; padpcm[id][i]->previndex = adpcm_msg[msg_index].adpcm_block[i].previndex; padpcm[id][i]->channel_id = i; padpcm[id][i]->time = GetTime(); adpcm_ctrl[i].crc = crc32_t(-1,padpcm[id][i],116,116); } else if(adr == (ADPCM_MAX_BLOCK-1)) { padpcm[id][i]->crc = crc32_t(adpcm_ctrl[i].crc,&padpcm[id][i]->adpcm_data[adr][0],120,120) ^ 0xffffffff; adpcm_ctrl[i].id ^= 1; adpcm_ctrl[i].adr = 0; adpcm_ctrl[i].done = 1; } else { adpcm_ctrl[i].crc = crc32_t(adpcm_ctrl[i].crc,&padpcm[id][i]->adpcm_data[adr][0],112,112); } } if(adr == (ADPCM_MAX_BLOCK-1)) { for(i = 0; i < MAX_CHANNEL; i++) adpcm_ready |= adpcm_ctrl[i].done; } } } msg_id[0] = adpcm_msg[msg_index].msg_counter; if(msg_id[0] != (msg_id[1]+1)) { //sys_info.L151_stream_error++; t_info.crc_binar_error++; } msg_id[1] = msg_id[0]; *pdata = 0; mode = DLE_STX; msg_index ^= 1; } else if(*pdata == DLE_DLE) { data_temp[0] = 0; } else { if(wrr++ < msg_16bit_size) *(padpcm_msg++) = data_temp[0]; } } } }
// crc hw is a shared resource so we need to lock around it sys_enter_critical_section(); // reset if (reset) CRC_ResetDR(); // mangle length so it is 4 byte aligned as hard requires this if (len & 0x03) len = (len >> 2) + 1; else len = (len >> 2); // crc buf r = CRC_CalcBlockCRC((uint32_t *)buf, len); // return result sys_leave_critical_section(); return r; } bool crc_init_hard(struct crc_h *h) { // if the cm config matches the hardware we are good to go !! if (!cm_t_compare(&h->cm, &stm32f4_crc_h.cm)) return false; // crc hw is a shared resource so we need to lock around it sys_enter_critical_section();
int main(void) { *SCB_DEMCR = *SCB_DEMCR | 0x01000000; *DWT_CYCCNT = 0; // reset the counter *DWT_CONTROL = *DWT_CONTROL | 1 ; // enable the counter /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); // Activate I2C1 clock. RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; /* GPIO configuration ------------------------------------------------------*/ //GPIO_Configuration(); /* TIM1 configuration ------------------------------------------------------*/ InitTIM1(); //InitTIM2(); LED_Init1(); /* Accelerometer Configuration ----------------------------------------------*/ //InitACC(); /* DMA1 channel1 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&SampleBuff1[0]; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = SampleBuffSize; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel1, DMA_IT_TC | DMA_IT_HT, ENABLE); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; //TIMER1 COMANDA ADC1!!!!!!!! ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = NUM_ADC; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channels configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_28Cycles5); // Canale ECG1 ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_28Cycles5); // Canale ECG2 ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_28Cycles5); // Canale Temperatura /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 external trigger */ ADC_ExternalTrigConvCmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); TN100_InitTypeDef TN100_InitStructure; uint8_t senderID[6]; uint8_t receiverID[6]; uint8_t packetType; uint8_t rxmsg[5]; uint16_t rxmsglen; uint32_t rangingtimer = 0; uint16_t Buffer_Tx[NUM_SAMP+8]; uint16_t *pBuffer_Tx; int8_t Buff_Comp[116]; //Buffer compresso uint8_t *pBuff_Comp, num_loc, y, q, diz[49], CRC_val; int16_t Buff_app[NUM_SAMP]; uint16_t Buffer3_in[NUM_SAMP_ECG]; uint16_t Buffer3_out[NUM_SAMP_ECG]; uint16_t sample, max1, max2, imax1, imax2, Buff[58]; uint16_t m = 0; uint16_t n = 0; uint16_t Temp; int num_pacchetto=0; int i, step; int j=1; int k; int RR_Fill = 0; // float iir_coef[] = {GAIN1, -1.994468725910304, 0.99463933704617047, -1.9983537046882773, 1, -1.9916039050017902, 0.99164560916623756, -1.9886914599162702, 1, -1.9979953458398652, 0.99827402341158644, -1.9990466012362298, 1}; // float iir_coef[] = {GAIN1, -1.9985564208306903, 0.99862589632961685, -1.9995236407524317, 1, -1.996067915503039, 0.99609980456504865, -1.9988120476320312, 1, -0.99746047624731959, 0, 1, 0}; // LPF 5 Hz // float iir_coef[] = {GAIN1, -1.9988611276805377, 0.9988619329900803, -1.9996627164949214, 1, -1.9995421854793711, 0.99954495837831336, -1.9999409520041804, 1}; // LPF 1 Hz // float iir_coef[] = {GAIN1, -1.9977219408097229, 0.99772516021851665, -1.9986512052812304, 1, -1.9990790359606454, 0.9990901250259242, -1.9997638181447535, 1}; // LPF 2 Hz float iir_coef[] = {GAIN1, -1.9995869592235613, 0.99958765723850362, -1.9998923257509362, 1, -0.99958496672120145, 0, 1, 0}; /* Inizializzazione del Timer */ Timer_Init(); /* Inizializzazione dell'interfaccia seriale */ usart_init(230400); // Init usart with 230400 Baud /* Unlock the Flash Program Erase controller */ FLASH_Unlock(); /*Inizializzazione del LED */ /*Configurazione del TN100 */ TN100_InitStructure.srcId[0] = 0x02; // Source address TN100_InitStructure.srcId[1] = 0x00; TN100_InitStructure.srcId[2] = 0x00; TN100_InitStructure.srcId[3] = 0x00; TN100_InitStructure.srcId[4] = 0x00; TN100_InitStructure.srcId[5] = 0x00; TN100_InitStructure.destId[0] = 0x04; // Destination address TN100_InitStructure.destId[1] = 0x00; TN100_InitStructure.destId[2] = 0x00; TN100_InitStructure.destId[3] = 0x00; TN100_InitStructure.destId[4] = 0x00; TN100_InitStructure.destId[5] = 0x00; TN100_InitStructure.syncword[0] = 0xAB; TN100_InitStructure.syncword[1] = 0x2C; TN100_InitStructure.syncword[2] = 0xD5; TN100_InitStructure.syncword[3] = 0x92; TN100_InitStructure.syncword[4] = 0x94; TN100_InitStructure.syncword[5] = 0xCA; TN100_InitStructure.syncword[6] = 0x69; TN100_InitStructure.syncword[7] = 0xAB; TN100_InitStructure.txpacketType = TN100_TxData; TN100_InitStructure.rxpacketType = TN100_RxData; /*set channel (center frequency)*/ TN100_InitStructure.chNo = E1_2412MHZ; // -> Originale per Ranging: TN100_InitStructure.mode = TN100_80MHz_1MS_1us; //TN100_InitStructure.mode = TN100_80MHz_500kS_2us; TN100_InitStructure.mode = TN100_80MHz_250kS_4us; //TN100_InitStructure.mode = TN100_22MHz_1MS_1us; //TN100_InitStructure.mode = TN100_22MHz_500kS_2us; //TN100_InitStructure.mode = TN100_22MHz_250kS_4us; // La potenza in trasmissione va da 1.79 dBm (scrivere nel registro 63 ovvero 0x3F in esadecimale) // a -36.20 dBm (ovvero 0 nel registro 0x00) (vedi datasheet TN100 pag. 96) TN100_InitStructure.txpwr = 0x28; // 40 ovvero -7.31 dBm TN100_InitStructure.txArq = 0x03; TN100_InitStructure.txArqMode = 0x01; TN100_InitStructure.rxArqMode = TN100_RxArqModeCrc2; TN100_InitStructure.addrMatching = ON; #ifdef PA_EN #warning PA is on TN100_PA_Init(TN100_PA_SMD_ANT); // Abilito l'antenna SMD sulla scheda DiZic #endif /* Initialize delay pointer (needed for the TN100_lib)*/ Ptr_Delay_ms = Delay_ms; /* Initialize get time pointer (needed for the TN100_lib)*/ Ptr_GetTime_ms = GetSysTick; /* Initialize the TN100 module */ if(TN100_Init(TN100_INIT_FULL, &TN100_InitStructure) != 1) { myprintf("initialization failed!\n"); myprintf("stop application!\n"); while(1); } /* set source address */ TN100_SetStationAddr(&TN100_InitStructure.srcId[0],&TN100_InitStructure.srcId[0]); rxmsg[0] = 0; rangingtimer = GetSysTick(); //valori massimi e minimi di accelerometro e magnetometro int16_t ax_max=1024; int16_t ay_max=1024; int16_t az_max=1024; int16_t ax_min=-1024; int16_t ay_min=-1024; int16_t az_min=-1024; int16_t mx_max = 101; int16_t mx_min = -104; int16_t my_max = 80; int16_t my_min = -147; int16_t mz_max = 79; int16_t mz_min = -103; /*libreria ufficiale: deinizializza tutto ciò che riguarda la i2c1 tutti i registri*/ I2C_DeInit(I2C1); LSM303DLHC_I2C_InitialConfig(I2C1); /*abilita pb6 e pb7 descritti nella documentazione*/ GPIOB->CRL=0xFF444444; //per i2c1 su pb6 e pb7 /*configurazione LSM303DLHC.c dell'accelerometro e magnetometro*/ LSM303DLHC_I2C_Accelerometer_Config(I2C1); LSM303DLHC_I2C_Magnetometer_Config(I2C1); delay(0x0FFF); //ECGInit(); NVIC_Configuration(); // initialize the filter // firFixedInit(); //TN100_Callibration(); GPIO_SetBits(GPIOC, GPIO_Pin_15); // led2 on // initialize the filter buffer xv[0] = xv[1] = xv[2] = xv[3] = xv[4] = 0x3F; yv[0] = yv[1] = yv[2] = yv[3] = yv[4] = 0x3F; m = max1 = max2 = imax1 = imax2 = 0; while(1){ // TN100_Callibration(); // rxmsglen = TN100_is_Msg_Received(&rxmsg[0], senderID, receiverID, &packetType); /*faccio le letture x y z dell'accellerometro e magnetometro*/ buffer[0]=LSM303DLHC_I2C_Accelerometer_ReadDataAXL(I2C1); buffer[1]=LSM303DLHC_I2C_Accelerometer_ReadDataAXH(I2C1); // Prendo gli 8bit (MSB e LSB), li unisco per ricreare i 16bit del accelerometro // ci tolgo 2^16 cosi da ottenere un numero basso ad accelerometro fermo e 65536 // in movimento (originariamente è all'incontro) // mems[0] = 65536 -( (uint16_t) ((buffer[0] << 8) | buffer[1]) ); mems[0] = buffer[0]; mems[1] = buffer[1]; buffer[2]=LSM303DLHC_I2C_Accelerometer_ReadDataAYL(I2C1); buffer[3]=LSM303DLHC_I2C_Accelerometer_ReadDataAYH(I2C1); // mems[1] = 65536 -( (uint16_t) ((buffer[2] << 8) | buffer[3] )); mems[2] = buffer[2]; mems[3] = buffer[3]; buffer[4]=LSM303DLHC_I2C_Accelerometer_ReadDataAZL(I2C1); buffer[5]=LSM303DLHC_I2C_Accelerometer_ReadDataAZH(I2C1); // mems[2] = 65536 -( (uint16_t) ((buffer[4] << 8) | buffer[5] )); mems[4] = buffer[4]; mems[5] = buffer[5]; buffer[6]=LSM303DLHC_I2C_Magnetometer_ReadDataMXL(I2C1); buffer[7]=LSM303DLHC_I2C_Magnetometer_ReadDataMXH(I2C1); // mems[3] = 65536 -( (uint16_t) ((buffer[6] << 8) | buffer[7] )); buffer[8]=LSM303DLHC_I2C_Magnetometer_ReadDataMYL(I2C1); buffer[9]=LSM303DLHC_I2C_Magnetometer_ReadDataMYH(I2C1); // mems[4] = 65536 -( (uint16_t) ((buffer[8] << 8) | buffer[9] )); buffer[10]=LSM303DLHC_I2C_Magnetometer_ReadDataMZL(I2C1); buffer[11]=LSM303DLHC_I2C_Magnetometer_ReadDataMZH(I2C1); // mems[5] = 65536 -( (uint16_t) ((buffer[10] << 8) | buffer[11]) ); // Aggiusto i dati dell'accelerometro secondo la notazione 12-bit left-justified big endian double ax = ((int16_t)(( (buffer[0]<<8) | buffer[1] )))/16; double ay = ((int16_t)(( (buffer[2]<<8) | buffer[3] )))/16; double az = ((int16_t)(( (buffer[4]<<8) | buffer[5] )))/16; // Aggiusto i dati del Magnetometro secondo la notazione 12-bit right-justified little endian double mx = ((int16_t)(( (buffer[7]<<8) | buffer[6] ))); double my = ((int16_t)(( (buffer[9]<<8) | buffer[8] ))); double mz = ((int16_t)(( (buffer[11]<<8) | buffer[10] ))); // normalizzo i dati utilizzando i massimi ed i minimi, i risultati vanno da -1 a +1 double ax_n = ((ax - ax_min) / (ax_max - ax_min)) * 2 - 1; double ay_n = ((ay - ay_min) / (ay_max - ay_min)) * 2 - 1; double az_n = ((az - az_min) / (az_max - az_min)) * 2 - 1; double mx_n = ((mx - mx_min) / (mx_max - mx_min)) * 2 - 1; double my_n = ((my - my_min) / (my_max - my_min)) * 2 - 1; double mz_n = ((mz - mz_min) / (mz_max - mz_min)) * 2 - 1; // calcolo pitch e roll del piano orizzontale double pitch = asin(-ax_n); double roll = asin(ay_n / cos(pitch)); //formule per calcolare l'angolo in base all'inclinazione double xh = mx_n * cos(pitch) + mz_n * sin(pitch); double yh = mx_n * sin(roll) * sin(pitch) + my_n * cos(roll) - mz_n * sin(roll) * cos(pitch); //angolo sfruttando l'accelerometro double heading = (180 * atan2(yh, xh)/M_PI); //angolo considerando pitch e roll uguali a zero double headingZero = (180 * atan2(my_n, mx_n) / M_PI); //per avere valori da 0 a 360 e non da -180 a +180 if (yh < 0) heading += 360; //per avere valori da 0 a 360 e non da -180 a +180 if (headingZero < 0) headingZero += 360; //inserisce i valori aggiustati utilizzando le notazioni precedenti nel buffer da inviare *(int16_t*)&(buffer[0]) = (int16_t)ax; *(int16_t*)&(buffer[2]) = (int16_t)ay; *(int16_t*)&(buffer[4]) = (int16_t)az; *(int16_t*)&(buffer[6]) = (int16_t)mx; *(int16_t*)&(buffer[8]) = (int16_t)my; *(int16_t*)&(buffer[10])= (int16_t)mz; //inserisco nel buffer i valori dell'angolo e dell'angolo con piano orizzontale nullo *(int16_t*)&(buffer[12]) = (int16_t)heading; *(int16_t*)&(buffer[14]) = (int16_t)headingZero; pBuffer_Tx = &Buffer_Tx[0]; pBuff_Comp = &Buff_Comp[0]; if (Buffer_Ready==1) { step=0; if(pSampleBuff2==&SampleBuff2[0]) k=0; else k=DATA_BUFF_SIZE; if(j<=25) { Buffer_Tx[j] = SampleBuff2[k+step]; p3_in = &Buffer3_in[0]; p3_out = &Buffer3_out[0]; for(i=0; i<DATA_BUFF_SIZE; i++) { if((i%3) == 2) Buffer3_in[i/3]=SampleBuff2[i+k]; // Fill Temperature Buffer } Filter3(p3_in, NUM_SAMP_ECG, p3_out); Temp=Buffer3_out[0]; // Decimation Buffer_Tx[j+25]=SampleBuff2[k+1+step]; j++; step=step+3; } else if(j>25) { num_loc=2; Buff_app[0]= Buffer_Tx[1]; for(i=1; i<NUM_SAMP; i=i+1){ Buff_app[i]= Buffer_Tx[i+1]-Buffer_Tx[i]; } for(i=1; i<NUM_SAMP; i=i+1){ if(Buff_app[i]>127){diz[i-1]=1; num_loc=num_loc+2;} else if(Buff_app[i]<-128){diz[i-1]=1; num_loc=num_loc+2;} else {diz[i-1]=0; num_loc=num_loc+1;} } y=3; Buff_Comp[1]=Buff_app[0]; Buff_Comp[2]=(0xFFFF & Buff_app[0])>>8; for(i=0; i<49; i=i+1){ if(diz[i]==0){Buff_Comp[y]=Buff_app[i+1]; y++;} else{Buff_Comp[y]=Buff_app[i+1]; Buff_Comp[y+1]=(0xFFFF & Buff_app[i+1])>>8; y=y+2;} } Buff_Comp[num_loc+1]=Temp; // Accelerometro X a 16bit con 8bit (LSB) in mems[0] e 8bit (MSB) in mems[1] Buff_Comp[num_loc+2]=mems[0]; Buff_Comp[num_loc+3]=mems[1]; // Accelerometro Y Buff_Comp[num_loc+4]=mems[2]; Buff_Comp[num_loc+5]=mems[3]; // Accelerometro Z Buff_Comp[num_loc+6]=mems[4]; Buff_Comp[num_loc+7]=mems[5]; Buff_Comp[0]=0x00FF & num_pacchetto; q=0; for(i=0; i<41; i=i+8){ Buff_Comp[num_loc+8+q]=((diz[i]&1)<<7)|((diz[i+1]&1)<<6)|((diz[i+2]&1)<<5)|((diz[i+3]&1)<<4)|((diz[i+4]&1)<<3)|((diz[i+5]&1)<<2)|((diz[i+6]&1)<<1)|(diz[i+7]&1); q++; } Buff_Comp[num_loc+14]=diz[48]; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,ENABLE); CRC->CR = 0x00000001; CRC_val=CRC_CalcBlockCRC((uint32_t *)pBuff_Comp, ((num_loc+15)/4)); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC,DISABLE); Buff_Comp[num_loc+15]=CRC_val; GPIO_SetBits(GPIOC, GPIO_Pin_14); // led1 on // Attendo che il TN100 si svegli. Per svegliarlo cambio lo stato al pin DII0_3 (D03) while(TN100_getWUState() != 1) { TN100_DIIO_3_STROBE(); } Delay_ms(2); // Lo reinizializzo ed invio il dato TN100_Init(TN100_INIT_FULL, &TN100_InitStructure); TN100_Send_Data(&TN100_InitStructure.destId[0], (uint8_t *)pBuff_Comp, (num_loc+16)); // Ad ogni ciclo ED UNA SOLA VOLTA per ciclo devo calibrarlo // TN100_Callibration(); // rxmsglen = TN100_is_Msg_Received(&rxmsg[0], senderID, receiverID, &packetType); // Mando il Tn100 in sleep TN100_Sleep(TN100_WAKEUPDIIO, TN100_PD_FULL, TN100_DIIO_3, NULL); GPIO_ResetBits(GPIOC, GPIO_Pin_14); // led1 off // STOPWATCH_STOP; // STOPWATCH_START; j=1; num_pacchetto++; Buffer_Ready = 0; } }
/* This function runs before the OS start. */ void NetworkConnecting() { int i, p, test, test1; int sendcounter = 0; u32 *pu32 = NULL; test1 = Timer2Counter; test = Timer2Counter; sPacketbuf[DES_NUM] = SELFADDRESS; sPacketbuf[SRC_NUM] = SELFADDRESS; CC2500_SeleChannel(0); CC2500_SetRecvAddr(SELFADDRESS); CC2500_SetRxd(); CC2500_SendPacket(sPacketbuf,PACKET_LEN); CC2500_SetRxd(); sPacketbuf[DES_NUM]=0x00; NbrList[0]=NbrCount; // while(RESET==RNG_GetFlagStatus(RNG_FLAG_DRDY)); // Delay_100us(RNG_GetRandomNumber() & 0x000000f); pu32 = (u32*) &sPacketbuf[1]; while (0 == NodeFlag[SELFADDRESS]) { if (ISIDLE) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_MESSAGE; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); p = Bernoulli(1.0/(MAXNUM-NbrCount+1)); //Delay_100us(1); if (ISIDLE) { if (0 == DelayFlag) { if (p/*Bernoulli(1.0/(MAXNUM-NbrCount+1))*/) { CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); DelayFlag=1; } } else { Preemptive_Delay_100us(25, &DelayFlag); DelayFlag=0; } } } } test1 = Timer2Counter - test1; while(MAXNUM > NbrCount) { if (0 != DiscoveryHelp) { if (ISIDLE) { Delay_100us(1); if (ISIDLE) { if (Bernoulli(1.0/(MAXNUM-NbrCount+1))) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_MESSAGE; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); } } } DiscoveryHelp = 0; } } test = Timer2Counter - test; while(RESET==RNG_GetFlagStatus(RNG_FLAG_DRDY)); Delay_100us(RNG_GetRandomNumber() & 0x000000f); pu32[CRC_LENGTH - 2] = Timer2Counter;//Add the timestamp. sendcounter = 3; while (0 != sendcounter) { if (ISIDLE) { Delay_100us(1); if (ISIDLE) { NbrList[0] = NbrCount; sPacketbuf[PRC_NUM] = DISCOVERY_ACCOMPLISHED; arm_copy_q7(NbrList, sPacketbuf+LEN_NUM, NbrList[0]+1); CRC_ResetDR(); pu32[CRC_LENGTH - 1] = CRC_CalcBlockCRC(pu32, CRC_LENGTH - 1); CC2500_SendPacket(sPacketbuf, PACKET_LEN); CC2500_SetRxd(); sendcounter--; } } Delay_100us(10); } printf("NeighbourList:\r\n"); for (i=0;i<MAXNUM;i++) { printf("%#x\r\n",NbrList[1+i]); } printf("t1 = %d\r\n", test1); printf("t = %d\r\n", test); }