/** * \brief Example 1 main application routine */ int main(void) { board_init(); sysclk_init(); // Initialize the platform LED's. LED_Off(LED0_GPIO); LED_Off(LED1_GPIO); uint32_t checksum; uint8_t data[15]; // Randomly selected data data[0] = 0xAA; data[1] = 0xBB; data[2] = 0xCC; data[3] = 0xDD; data[4] = 0xEE; data[5] = 0xFF; // Calculate checksum for the data checksum = crc_io_checksum((void*)data, 6, CRC_32BIT); // Append complemented CHECKSUM registers value to the data, little endian crc32_append_value(checksum, data+6); // Calculate the new checksum checksum = crc_io_checksum((void*)data, 10, CRC_32BIT); // Check that the data has not been corrupted, and checksum is zero if (checksum == 0) { // Turn on LED0 LED_On(LED0_GPIO); } // Calculate CRC 16 checksum for the data checksum = crc_io_checksum((void*)data, 6, CRC_16BIT); // Append the checksum to the data, big endian crc16_append_value(checksum, data+6); // Calculate the new checksum checksum = crc_io_checksum((void*)data, 8, CRC_16BIT); // Check that the data has not been corrupted, and checksum is zero if (checksum == 0) { // Turn on LED1 LED_On(LED1_GPIO); } // End of application, loop forever while (true) { // Intentionally left empty } }
/** * \brief Compute 16-bit CRC for EEPROM address range using hardware CRC module. * * This function returns the 16-bit CRC of the specified EEPROM address range. * * \param origDataptr Address of EEPROM location to start CRC computation at. * \param numBytes Number of bytes of the data. * \param pchecksum Pointer to the checksum stored in EEPROM. * * \note No sanity checking of addresses is done. */ uint16_t CLASSB_CRC16_EEPROM_HW(eepromptr_t origDataptr, crcbytenum_t numBytes, eeprom_uint16ptr_t pchecksum) { uint32_t checksum; eeprom_uint8ptr_t dataptr = origDataptr; crc_set_initial_value(CRC16_INITIAL_REMAINDER); /* Ensure that EEPROM is memory mapped. */ CLASSB_EEMAP_BEGIN(); dataptr += MAPPED_EEPROM_START; checksum = crc_io_checksum((void*)dataptr, numBytes, CRC_16BIT); #if defined(__ICCAVR__) /* Disable memory mapping of EEPROM, if necessary. */ CLASSB_EEMAP_END(); /* Compare checksums and handle error if necessary. */ if (checksum != *pchecksum) { CLASSB_ERROR_HANDLER_CRC(); } #elif defined(__GNUC__) /* Compare checksums and handle error if necessary. */ if (checksum != (*(eeprom_uint16ptr_t)(MAPPED_EEPROM_START + (uintptr_t)pchecksum))) { CLASSB_ERROR_HANDLER_CRC(); } /* Disable memory mapping of EEPROM, if necessary. */ CLASSB_EEMAP_END(); #endif /* Return 16 bits */ return checksum & (uint32_t)0x0000FFFF; }
/** * \brief 16-bit checksum of data block test * * This test does a checksum of a data block with a known checksum, * and verifies that they are equal. It then appends the 16-bit CRC * to the end of the data, and does another checksum operation, which * should result in the zero flag being set. * * \param test Current test case */ static void run_16bit_io_test(const struct test_case *test) { uint8_t tmp_buffer[LENGTH(data_8bit) + sizeof(uint16_t)]; int i; uint16_t checksum; crc_io_checksum_byte_start(CRC_16BIT); for(i = 0;i < LENGTH(data_8bit); ++i) crc_io_checksum_byte_add(data_8bit[i]); checksum = crc_io_checksum_byte_stop(); test_assert_true(test, checksum == CRC_CHECKSUM_16BIT, "Checksum mismatch on IO CRC-16 test"); memcpy(tmp_buffer, data_8bit, LENGTH(data_8bit)); crc16_append_value(checksum, &tmp_buffer[LENGTH(tmp_buffer) - sizeof(uint16_t)]); checksum = crc_io_checksum(tmp_buffer, LENGTH(tmp_buffer), CRC_16BIT); test_assert_true(test, checksum == 0, "Checksum fail check failed on IO CRC-16 test"); }
/** * \brief main function */ int main(void) { struct dma_channel_config config; uint32_t checksum; pmic_init(); board_init(); sysclk_init(); sleepmgr_init(); // Randomly selected data source[0] = 0xAA; source[1] = 0xBB; source[2] = 0xCC; source[3] = 0xDD; source[4] = 0xEE; source[5] = 0xFF; // Calculate checksum for the data checksum = crc_io_checksum((void*)source, 6, CRC_16BIT); // Append the checksum to the data, big endian crc16_append_value(checksum, source+6); //Enable the CRC module for DMA crc_dma_checksum_start(DMA_CHANNEL, CRC_16BIT); // Enable DMA dma_enable(); // Set callback function for DMA completion dma_set_callback(DMA_CHANNEL, example_crc_dma_transfer_done); // Make sure config is all zeroed out so we don't get any stray bits memset(&config, 0, sizeof(config)); /** * This example will configure a DMA channel with the following * settings: * - Low interrupt priority * - 1 byte burst length * - DMA_BUFFER_SIZE bytes for each transfer * - Reload source and destination address at end of each transfer * - Increment source and destination address during transfer * - Source address is set to \ref source * - Destination address is set to \ref destination */ dma_channel_set_interrupt_level(&config, PMIC_LVL_LOW); dma_channel_set_burst_length(&config, DMA_CH_BURSTLEN_1BYTE_gc); dma_channel_set_transfer_count(&config, DMA_BUFFER_SIZE); dma_channel_set_src_reload_mode(&config, DMA_CH_SRCRELOAD_TRANSACTION_gc); dma_channel_set_dest_reload_mode(&config, DMA_CH_DESTRELOAD_TRANSACTION_gc); dma_channel_set_src_dir_mode(&config, DMA_CH_SRCDIR_INC_gc); dma_channel_set_dest_dir_mode(&config, DMA_CH_DESTDIR_INC_gc); dma_channel_set_source_address(&config, (uint16_t)(uintptr_t)source); dma_channel_set_destination_address(&config, (uint16_t)(uintptr_t)destination); dma_channel_write_config(DMA_CHANNEL, &config); // Use the configuration above by enabling the DMA channel in use. dma_channel_enable(DMA_CHANNEL); // Enable interrupts cpu_irq_enable(); // Trigger the DMA transfer dma_channel_trigger_block_transfer(DMA_CHANNEL); // Light the first LED to indicate that the DMA has started. gpio_set_pin_low(LED0_GPIO); while (true) { /* * Force a NOP instruction for an eventual placement of a debug * session breakpoint. */ asm("nop\n"); } }
void handle_spi_to_bbb(){ //Loop while we have data in the RX buffer to process while(circular_buffer_size(&rx_buff)){ rx_byte = circular_buffer_pop(&rx_buff); if(rx_byte == SPI_TX_START){ cmd_idx = CMD_DATA_SIZE; //Reset all the send variables/tmp storage cmd_finished = 0; send_idx = 0; send_crc_length = 0; send_crc = 0; send_crc_idx = 0; spi_transfer = 1; memset(&tx_buff, 0, sizeof(circular_buffer_t)); } //If we are receiving command, store it appropriately if(cmd_idx > 0){ cmd_data[CMD_DATA_SIZE-cmd_idx] = rx_byte; cmd_idx--; //Finished last storage of incoming data if(cmd_idx == 0){ //Check recieved_crc against calculated CRC received_crc = (cmd_data[CMD_DATA_SIZE-1]<<8) | cmd_data[CMD_DATA_SIZE-2]; calculated_crc = crc_io_checksum(cmd_data, CMD_DATA_SIZE-2, CRC_16BIT); //Send appropriate signal if passed/failed if(calculated_crc == received_crc){ SPIC.DATA = SPI_CRC_PASS; circular_buffer_push(&tx_buff,SPI_CRC_PASS); cmd_finished = 1; } else{ //SPIC.DATA = SPI_CRC_FAIL; circular_buffer_push(&tx_buff,SPI_CRC_FAIL); } } } else if(cmd_finished){ recv_cmd = cmd_data[1]; memcpy(send_data,sensor_data,SENSOR_DATA_SIZE); send_data[SENSOR_DATA_SIZE] = sensor_status; send_data[SENSOR_DATA_SIZE+1] = state; send_idx = SENSOR_DATA_SIZE+2; send_crc_length = send_idx; cmd_finished = 0; while(send_idx){ //SPIC.DATA = send_data[send_crc_length-send_idx]; circular_buffer_push(&tx_buff, send_data[send_crc_length-send_idx]); send_idx--; //Calculate CRC if(send_idx == 0){ send_crc = crc_io_checksum(send_data, send_crc_length, CRC_16BIT); circular_buffer_push(&tx_buff, send_crc); circular_buffer_push(&tx_buff, send_crc>> 8); } } spi_transfer = 0; } spi_isr = 0; }
/** * \brief Example 2 main application routine */ int main(void) { uint8_t flashbuf[8]; uint32_t checksum1; uint32_t checksum2; board_init(); sysclk_init(); gfx_mono_init(); // Calculate checksum for an address range in flash. checksum1 = crc_flash_checksum(CRC_FLASH_RANGE, FLASHADDR, 4); // Read out the buffer from flash nvm_flash_read_buffer(FLASHADDR, flashbuf, 4); // Calculate checksum for the buffer from flash checksum2 = crc_io_checksum(flashbuf, 4, CRC_32BIT); // Make sure the calculated checksums are equal and write to screen if (checksum1 == checksum2) { gfx_mono_draw_string("Address range CRC OK", 0, 0, &sysfont); } // Append the flash checksum and recalculate crc32_append_value(checksum1, flashbuf+4); checksum1 = crc_io_checksum(flashbuf, 8, CRC_32BIT); // Make sure the checksum is zero and write to screen if (checksum1 == 0) { gfx_mono_draw_string("IO CRC zero OK", 0, SYSFONT_HEIGHT + 1, &sysfont); } // Calculate checksum for the boot section checksum1 = crc_flash_checksum(CRC_BOOT, 0, 0); // Recalculate checksum for the boot section using flash range CRC checksum2 = crc_flash_checksum(CRC_FLASH_RANGE, BOOT_SECTION_START, BOOT_SECTION_END-BOOT_SECTION_START+1); // Make sure the checksums are equal and write to screen if (checksum1 == checksum2) { gfx_mono_draw_string("Boot section CRC OK", 0, (SYSFONT_HEIGHT + 1)*2, &sysfont); } // Calculate checksum for the application section checksum1 = crc_flash_checksum(CRC_APP, 0, 0); // Recalculate checksum for the application section using flash range CRC checksum2 = crc_flash_checksum(CRC_FLASH_RANGE, APP_SECTION_START, APP_SECTION_END-APP_SECTION_START+1); // Make sure the checksums are equal and write to screen if (checksum1 == checksum2) { gfx_mono_draw_string("App section CRC OK", 0, (SYSFONT_HEIGHT + 1)*3, &sysfont); } // End of application, loop forever while (true) { // Intentionally left empty } }