2009-03-03 07:22 AM
CRC Calculation Unit
2011-05-17 04:04 AM
I am working with the stm32f103rb, to calculate CRC using the Calculation Unit.
I used the example of the firmware guide: u32 DataCRC = 0; CRC_ResetDR(); CRC_CalcCRC(0x32F103); DataCRC = CRC_GetCRC(); And after debuggin' this the value of DataCRC is 0x00, doesn't matter what is in the input parameter in the CalcCRC function.2011-05-17 04:04 AM
Quote:
And after debuggin' this the value of DataCRC is 0x00, doesn't matter what is in the input parameter in the CalcCRC function. Hi, you've most likely forgotten to enable the peripheral? Yuck! This code formatting is horribleCode:
//This function is set up so that if it is called from the main loop (or a low priority interrupt) that // it can recover if it is interrupted by a higher priority task. uint32_t calculate_CRC32( uint32_t *data, uint32_t length ) { uint8_t copy_idr; uint32_t copy_length; uint32_t *copy_data_ptr; uint32_t crc_result; do { copy_idr = CRC->IDR; copy_length = length; copy_data_ptr = data; CRC->CR = 0x01; //Reset the CRC while (copy_length--) { CRC->DR = *copy_data_ptr; copy_data_ptr++; } crc_result = CRC->DR; } while ( copy_idr != CRC->IDR); CRC->IDR = CRC->IDR+1; return crc_result; } //---------------------------------------------------------------------------------------------------------------- bool init_CRC32( void ) { static bool done_once = false; //------------------------- if ( done_once ) return true; //------------------------- if ( CodeModules_CheckRunning( REQUIRED_CODE_MODULES ) ) { done_once = true; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); CodeModules_Started( MODULE_CRC32 ); } return done_once; }2011-05-17 04:04 AM
Quote:
On 02-03-2009 at 21:48, Anonymous wrote:Quote:
And after debuggin' this the value of DataCRC is 0x00, doesn't matter what is in the input parameter in the CalcCRC function. Hi, you've most likely forgotten to enable the peripheral? Yuck! This code formatting is horribleCode:
<BR>//This function is set up so that if it is called from the main loop (or a low priority interrupt) that <BR>// it can recover if it is interrupted by a higher priority task. <BR>uint32_t calculate_CRC32( uint32_t *data, uint32_t length ) <BR>{ <BR> uint8_t copy_idr; <BR> uint32_t copy_length; <BR> uint32_t *copy_data_ptr; <BR> uint32_t crc_result; <BR> <BR> do <BR> { <BR> copy_idr = CRC->IDR; <BR> copy_length = length; <BR> copy_data_ptr = data; <BR> <BR> CRC->CR = 0x01; //Reset the CRC <BR> <BR> while (copy_length--) <BR> { <BR> CRC->DR = *copy_data_ptr; <BR> copy_data_ptr++; <BR> } <BR> <BR> crc_result = CRC->DR; <BR> <BR> } while ( copy_idr != CRC->IDR); <BR> <BR> CRC->IDR = CRC->IDR+1; <BR> <BR> return crc_result; <BR>} <BR> <BR>//---------- <BR> <BR>bool init_CRC32( void ) <BR>{ <BR> static bool done_once = false; <BR> //-------- <BR> if ( done_once ) <BR> return true; <BR> //-------- <BR> if ( CodeModules_CheckRunning( REQUIRED_CODE_MODULES ) ) <BR> { <BR> done_once = true; <BR> RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE); <BR> CodeModules_Started( MODULE_CRC32 ); <BR> } <BR> return done_once; <BR>} <BR>
It Was missing to enable the pheriferal clock. Why you don`t use the ST library?, there`s all functions done to calculate the CRC. [ This message was edited by: vinicius_acvasconcelos on 03-03-2009 18:20 ]2011-05-17 04:04 AM
Quote:
It Was missing to enable the pheriferal clock. Why you don`t use the ST library?, there`s all functions done to calculate the CRC. The issue with the library functions is that they don't allow reentrant calls... so if you need to use the CRC32 peripheral from multiple locations it needs to handle being interrupted. i.e. main loop calls CRC32 function, interrupted by low priority interrupt requiring a call to the CRC32 function, which is also interrupted again by a higher priority interrupt... We have four different comms busses and so need to be able to handle this sort of scenario. I could have used their functions but it would have added a fair bit of overhead for such a simple routine