static ERL_NIF_TERM context_fini(ErlNifEnv* env, Context* ctx, size_t dsize, ChunkHandler handler) { ERL_NIF_TERM result; ctx->bitlen += ctx->count*8; pad(0, 0, ctx); handler(ctx, ctx->bytes); if (ctx->count > ctx->size) { handler(ctx, ctx->bytes + ctx->size); } #ifndef WORDS_BIGENDIAN { int i; if (ctx->size == PADDED_SIZE_2XX_BYTES) { uint32_t* hash = (uint32_t*)ctx->digest.data; for (i = 0; i < ctx->digest.size/sizeof(*hash); ++i) { hash[i] = BYTESWAP32(hash[i]); } } else { uint64_t* hash = (uint64_t*)ctx->digest.data; for (i = 0; i < ctx->digest.size/sizeof(*hash); ++i) { hash[i] = BYTESWAP64(hash[i]); } } } #endif if (ctx->digest.size != dsize) { enif_realloc_binary(&ctx->digest, dsize); } result = enif_make_binary(env, &ctx->digest); ctx->digest.size = 0; return result; }
/**************************************************************************//*! * * @name msc_read_capacity_command * * @brief command provides a means for the host computer to request information * regarding the capacity of the installed medium of the device. * * @param controller_ID: To identify the controller * @param cbw_ptr : pointer to Command Block Wrapper sent by host * @param csw_residue_ptr: pointer to dCSWDataResidue of Command Status Wrapper * @param csw_status_ptr : pointer to bCSWStatus of Command Status Wrapper * * @return error * *****************************************************************************/ uint_8 msc_read_capacity_command ( uint_8 controller_ID, PTR_CBW cbw_ptr, uint_32* csw_residue_ptr, uint_8* csw_status_ptr ) { READ_CAPACITY_DATA_STRUCT read_capacity; uint_8 error = USBERR_TX_FAILED; #if (!defined LITTLE_ENDIAN) read_capacity.last_logical_block_address = g_msc_scsi.device_info.total_lba_device_supports -1; read_capacity.block_size = (uint_32)g_msc_scsi.device_info.length_of_each_lba_of_device; #else read_capacity.last_logical_block_address = BYTESWAP32(g_msc_scsi.device_info.total_lba_device_supports -1); read_capacity.block_size = BYTESWAP32((uint_32)g_msc_scsi.device_info.length_of_each_lba_of_device); #endif /* initialize sense code values */ g_msc_scsi.request_sense.sense_key = NO_SENSE; g_msc_scsi.request_sense.add_sense_code = NO_SENSE; g_msc_scsi.request_sense.add_sense_code_qual = NO_SENSE; g_msc_scsi.thirteen_case_struct.controller_ID = controller_ID; g_msc_scsi.thirteen_case_struct.host_expected_data_len = cbw_ptr->data_length; g_msc_scsi.thirteen_case_struct.host_expected_direction = (uint_8)(cbw_ptr->flag >> USB_CBW_DIRECTION_SHIFT); g_msc_scsi.thirteen_case_struct.device_expected_data_len = READ_CAPACITY_DATA_LENGTH; g_msc_scsi.thirteen_case_struct.device_expected_direction = USB_SEND; g_msc_scsi.thirteen_case_struct.csw_status_ptr = csw_status_ptr; g_msc_scsi.thirteen_case_struct.csw_residue_ptr = csw_residue_ptr; g_msc_scsi.thirteen_case_struct.buffer_ptr = (uint_8_ptr)&read_capacity; g_msc_scsi.thirteen_case_struct.lba_txrx_select = FALSE; error = msc_thirteen_cases_check(&(g_msc_scsi.thirteen_case_struct)); return error; }
static void sha2xx_chunk(Context* ctx, unsigned char* chunk) { uint32_t* hash = (uint32_t*)ctx->digest.data; uint32_t a, b, c, d, e, f, g, h; uint32_t words[64]; int i; #ifndef WORDS_BIGENDIAN { uint32_t* from = (uint32_t*)chunk; for (i = 0; i < 16; ++i) { words[i] = BYTESWAP32(from[i]); } } #else memcpy(words, chunk, 16*sizeof(*words)); #endif for (i = 16; i < sizeof(words)/sizeof(*words); ++i) { uint32_t w15 = words[i-15], w2 = words[i-2]; uint32_t s0 = SM_SIGMA256_0(w15), s1 = SM_SIGMA256_1(w2); uint32_t w7 = words[i-7], w16 = words[i-16]; words[i] = s1 + w7 + s0 + w16; } a = hash[0]; b = hash[1]; c = hash[2]; d = hash[3]; e = hash[4]; f = hash[5]; g = hash[6]; h = hash[7]; for (i = 0; i < sizeof(words)/sizeof(*words); ++i) { uint32_t t1, t2; t1 = h + BIG_SIGMA256_1(e) + CH(e,f,g) + K256[i] + words[i]; t2 = BIG_SIGMA256_0(a) + MAJ(a,b,c); h = g; g = f; f = e; e = d + t1; d = c; c = b; b = a; a = t1 + t2; } hash[0] += a; hash[1] += b; hash[2] += c; hash[3] += d; hash[4] += e; hash[5] += f; hash[6] += g; hash[7] += h; }
/****************************************************************************** * @name Shell_play * * @brief Servers the play command * * @param None * * @return None * ****************************************************************************** * This function is used to play an audio wav file *****************************************************************************/ int32_t Shell_play(int32_t argc, char *argv[]) { bool print_usage, shorthelp = FALSE; uint8_t bSamFreqType_index; print_usage = Shell_check_help_request (argc, argv, &shorthelp); if (!print_usage) { WAVE_FILE_HEADER header; if (argc > 1) { /* stop the current file playing */ if(AUDIO_PLAYING == audio_state) { audio_state = AUDIO_IDLE; hwtimer_stop(&audio_timer); } /* check the device is connected */ if ((USB_DEVICE_INUSE != audio_stream.DEV_STATE)||(device_direction != USB_AUDIO_DEVICE_DIRECTION_IN)) { printf(" Error: Audio Speaker is not connected\n"); return (SHELL_EXIT_ERROR); } if (FillWaveHeader(argv[1], &header) != 0) { printf(" Error: Unable to open file: %s\n", argv[1]); return (SHELL_EXIT_ERROR); } if (strcmp(header.CHUNK_DESCRIPTOR.Format, "WAVE")) { printf(" Error: File is not WAVE file.\n"); return (SHELL_EXIT_ERROR); } if (strcmp(header.FMT_SUBCHUNK.Subchunk1ID, "fmt ")) { printf(" Error: File does not contain format subchunk.\n"); } if (BYTESWAP16(header.FMT_SUBCHUNK.AudioFormat) != 1) { printf(" Error: File is compressed (not PCM).\n"); return (SHELL_EXIT_ERROR); } if (strcmp(header.DATA_SUBCHUNK.Subchunk2ID, "data")) { printf(" Error: File does not contain data subchunk.\n"); return (SHELL_EXIT_ERROR); } file_ptr = fopen(argv[1], "r"); if (file_ptr == NULL) { printf(" Unable to open file: %s\n", argv[1]); return (SHELL_EXIT_ERROR); } file_open_count ++; printf("Audio file properties:\n"); printf(" - Sample rate : %d Hz\n", BYTESWAP32(header.FMT_SUBCHUNK.SampleRate)); printf(" - Sample size : %d bits\n", BYTESWAP16(header.FMT_SUBCHUNK.BitsPerSample)); printf(" - Number of channels : %d channels\n", BYTESWAP16(header.FMT_SUBCHUNK.NumChannels)); /* Compare the sample rate */ for (bSamFreqType_index =0; bSamFreqType_index < frm_type_desc->bSamFreqType; bSamFreqType_index++) { if (((frm_type_desc->tSamFreq[3*bSamFreqType_index + 2] << 16) | (frm_type_desc->tSamFreq[3*bSamFreqType_index + 1] << 8) | (frm_type_desc->tSamFreq[3*bSamFreqType_index] << 0)) == BYTESWAP32(header.FMT_SUBCHUNK.SampleRate)) { packet_size = USB_Audio_Get_Packet_Size(frm_type_desc,bSamFreqType_index); break; } } if (bSamFreqType_index == frm_type_desc->bSamFreqType) { printf(" The audio device doesn't support that audio sample rate \n"); return (SHELL_EXIT_ERROR); } /* Compare the bits sample number */ if (frm_type_desc->bBitResolution != BYTESWAP16(header.FMT_SUBCHUNK.BitsPerSample)) { printf(" The audio device doesn't support that audio bit sample number \n"); return (SHELL_EXIT_ERROR); } /* Compare the channel number */ if (frm_type_desc->bNrChannels != BYTESWAP16(header.FMT_SUBCHUNK.NumChannels)) { printf(" The audio device doesn't support that audio channel number \n"); return (SHELL_EXIT_ERROR); } fseek(file_ptr, WAVE_HEADER_SIZE, IO_SEEK_SET); audio_state = AUDIO_PLAYING; printf(" Playing...\n"); hwtimer_start(&audio_timer); } else { if (AUDIO_PLAYING == audio_state) { printf(" The file is playing...\n"); } else if (AUDIO_PAUSE == audio_state) { audio_state = AUDIO_PLAYING; hwtimer_start(&audio_timer); printf(" Playing...\n"); } else if (AUDIO_IDLE == audio_state) { printf(" Not enough parameters.\n"); } } } else { if (shorthelp) { printf("%s <filename>\n", argv[0]); } else { printf("Usage: %s <filename>\n", argv[0]); printf(" filename = wav file to play\n"); } } return(SHELL_EXIT_SUCCESS); }
uint_8 SD_Init(void) { #ifdef MCU_MKL25Z4 SPI_Init(); /* SPI Initialization */ SD_CLKDelay(10); /* Send 80 clocks */ SPI_clr_SS() ; gu8SD_Argument.lword = 0; SPI_set_SS(); SD_CLKDelay(8); /* IDLE Command */ SPI_set_SS(); if(SD_SendCommand(SD_CMD0|0x40,SD_IDLE)) { SPI_clr_SS(); return(FALSE); /* Command IDLE fail */ } SPI_clr_SS(); (void)SPI_Receive_byte(); /* Dummy SPI cycle */ /* Initialize SD Command */ SPI_set_SS(); while(SD_SendCommand(SD_CMD1|0x40,SD_OK)) { Watchdog_Reset(); } SPI_clr_SS() ; (void)SPI_Receive_byte(); /* Dummy SPI cycle */ /* Block Length */ gu8SD_Argument.lword = BYTESWAP32(SD_BLOCK_SIZE); SPI_set_SS(); if(SD_SendCommand(SD_CMD16|0x40,SD_OK)) { SPI_clr_SS() ; return(FALSE); /* Command IDLE fail */ } SPI_clr_SS() ; SPI_High_rate(); SPI_Send_byte(0x00); SPI_Send_byte(0x00); return(TRUE); #else /*Body*/ SPI_Init(); /* SPI Initialization*/ SD_CLKDelay(10); /* Send 80 clocks*/ gu8SD_Argument.lword = 0; SD_CLKDelay(8); /* IDLE Command */ if(SD_SendCommand(SD_CMD0|0x40,SD_IDLE)) { return(FALSE); /* Command IDLE fail*/ }/*Endif*/ (void)SPI_Receive_byte(); /* Dummy SPI cycle*/ /* Initialize SD Command */ while(SD_SendCommand(SD_CMD1|0x40,SD_OK)) { Watchdog_Reset(); }/*EndWhile*/ (void)SPI_Receive_byte(); /*Dummy SPI cycle */ /* Block Length */ gu8SD_Argument.lword = BYTESWAP32(SD_BLOCK_SIZE); if(SD_SendCommand(SD_CMD16|0x40,SD_OK)) { return(FALSE); /* Command IDLE fail */ }/*Endif*/ SPI_High_rate(); SPI_Send_byte(0x00); SPI_Send_byte(0x00); return(TRUE); #endif }/*End Body*/
void SD_Read_Block(PTR_LBA_APP_STRUCT lba_data_ptr) { #ifdef MCU_MKL25Z4 uint_8 u8Temp=0; uint_32 u32Counter; SPI_set_SS(); gu8SD_Argument.lword = BYTESWAP32(lba_data_ptr->offset); if(SD_SendCommand(SD_CMD17|0x40,SD_OK)) { SPI_clr_SS() ; return; /* Command IDLE fail */ } while(u8Temp!=0xFE) { u8Temp = SPI_Receive_byte(); } for(u32Counter=0;u32Counter<lba_data_ptr->size;u32Counter++) { *(lba_data_ptr->buff_ptr + u32Counter) = SPI_Receive_byte(); } (void)SPI_Receive_byte(); /* Dummy SPI cycle */ (void)SPI_Receive_byte(); /* Dummy SPI cycle */ SPI_clr_SS() ; (void)SPI_Receive_byte(); /* Dummy SPI cycle */ return; #else uint_8 u8Temp=0; uint_32 u32Counter; gu8SD_Argument.lword = BYTESWAP32(lba_data_ptr->offset); if(SD_SendCommand(SD_CMD17|0x40,SD_OK)) { return; /* Command IDLE fail*/ } while(u8Temp!=0xFE) { u8Temp = SPI_Receive_byte(); }/*EndWhile*/ for(u32Counter=0;u32Counter<lba_data_ptr->size;u32Counter++) { *(lba_data_ptr->buff_ptr + u32Counter) = SPI_Receive_byte(); } (void)SPI_Receive_byte(); /* Dummy SPI cycle */ (void)SPI_Receive_byte(); /* Dummy SPI cycle */ (void)SPI_Receive_byte(); /* Dummy SPI cycle */ return; #endif }/*EndBody*/
void SD_Write_Block(PTR_LBA_APP_STRUCT lba_data_ptr) { #ifdef MCU_MKL25Z4 uint_32 u32Counter; SPI_set_SS(); gu8SD_Argument.lword = BYTESWAP32(lba_data_ptr->offset); if(SD_SendCommand(SD_CMD24|0x40,SD_OK)) { SPI_clr_SS() ; return; /* Command IDLE fail */ } SPI_Send_byte(0xFE); for(u32Counter=0;u32Counter<lba_data_ptr->size;u32Counter++) { SPI_Send_byte(*(lba_data_ptr->buff_ptr + u32Counter)); } SPI_Send_byte(0xFF); /* checksum Bytes not needed */ SPI_Send_byte(0xFF); if((SPI_Receive_byte() & 0x0F) != 0x05) { SPI_clr_SS() ; return; /* Command fail */ } while(SPI_Receive_byte()==0x00) { Watchdog_Reset(); /* Dummy SPI cycle */ } SPI_clr_SS() ; return; #else /* Body */ uint_32 u32Counter, time_out; gu8SD_Argument.lword = BYTESWAP32(lba_data_ptr->offset); if(SD_SendCommand(SD_CMD24|0x40,SD_OK)) { return; /* Command IDLE fail */ } /* EndIf */ SPI_Send_byte(0xFE); for(u32Counter=0;u32Counter<lba_data_ptr->size;u32Counter++) { SPI_Send_byte(*(lba_data_ptr->buff_ptr + u32Counter)); } /* EndFor */ SPI_Send_byte(0xFF); /* checksum Bytes not needed */ SPI_Send_byte(0xFF); for (time_out=0;time_out<1000;time_out++) { if((SPI_Receive_byte() & 0x1F) < 16) { break; } /* EndIf */ }/* EndFor */ if (time_out>=1000) return; while(SPI_Receive_byte()==0x00) { Watchdog_Reset(); } /* EndWhile */ return; #endif }
/*FUNCTION*---------------------------------------------------------------- * * Function Name : FlashApplication * Returned Value : 0 if successful, other value if error * Comments : parse and flash an array to flash memory * *END*--------------------------------------------------------------------*/ uint_8 FlashApplication ( /* [IN] the array to parse */ uint_8* arr, /* [IN] data length of the array */ uint_32 length ) { /* Body */ uint_8 result; uint_32 write_addr; /* address to write to flash */ static uint_32 bytes_written; /* number of bytes was written to flash */ #ifdef _MC9S08_H uint_16 header; #else uint_32 header; #endif #ifdef _MC9S08_H header = (uint_16)*(uint_16*)(arr); #else header = get_uint32(arr,0); #endif /* Get image type */ if(filetype == UNKNOWN) { bytes_written = 0; SizeOfFirmware = 0; /* first four bytes is SP */ if( (MIN_RAM1_ADDRESS <=header ) && (header<= MAX_RAM1_ADDRESS)) { #ifndef NO_PRINTF_SUPPORT printf("\nRaw binary file found\n\r"); printf("\nFLASHING....\n\rPlease do not remove your device\n\r"); #endif filetype = RAW_BINARY; } /* first four bytes is user application address */ if( (MIN_FLASH1_ADDRESS <=header ) && (header<= MAX_FLASH1_ADDRESS)) { #ifdef _MC9S08_H /* HCS08 don't have code warrior binary file */ filetype = RAW_BINARY; #else #ifndef NO_PRINTF_SUPPORT printf("\n\rCodeWarrior binary file found\n\r"); printf("\n\rFLASHING....\n\rPlease do not remove your device\n\r"); #endif filetype = CODE_WARRIOR_BINARY; #endif } /* first four bytes is S-Record header */ #ifdef LITTLE_ENDIAN header = BYTESWAP32(header); #endif #ifdef _MC9S08_H /*FSL: restrection for 8 bit architecture only*/ if(header ==(uint_32)(S19_RECORD_HEADER)) #else if((header&0xFFFF0000) ==(uint_32)(S19_RECORD_HEADER)) #endif { #ifndef NO_PRINTF_SUPPORT printf("\n\rS-Record file found\n\r"); printf("\n\rFLASHING....\n\rPlease do not remove your device\n\r"); #endif filetype = S19_RECORD; } if(filetype == UNKNOWN) { #if (!(defined _MC9S08_H)) #ifndef NO_PRINTF_SUPPORT printf("\n\r\t\tERROR......\n\rUnknown file type"); #endif #endif return (uint_8)-1;/*FSL: error*/ } /* EndIf */ } /* EndIf */ /* Flash image */ switch (filetype) { case RAW_BINARY: /* Raw binary file found*/ /* the address to write to flash */ write_addr =(uint_32) IMAGE_ADDR + bytes_written; /* if flash error , break the loop */ DisableInterrupts; (void)erase_sectors((uint_32*)(write_addr),length); result = Flash_ByteProgram((uint_32*)(write_addr),(uint_32*)arr,length); EnableInterrupts; bytes_written += length; SizeOfFirmware = bytes_written; break; case CODE_WARRIOR_BINARY: /* CodeWarrior binary file found */ result = FlashArrayCW(arr,length,line); /* DES parse and flash array */ break; case S19_RECORD: /* S19 file found */ result = FlashArrayS19(arr,length,line); /* DES parse and flash array */ break; } /* EndSwitch */ /* DES Should add programming verification code ...minimal code to see if "#Flash Programming Signature" present from linker script */ #ifndef NO_PRINTF_SUPPORT printf("&"); /* flashing indicator */ #endif return result; } /* EndBody */
/**************************************************************************//*! * * @name msc_read_format_capacity_command * * @brief allows the host to request a list of the possible capacities that * can be formatted on the currently installed medium * * @param controller_ID: To identify the controller * @param cbw_ptr : pointer to Command Block Wrapper sent by host * @param csw_residue_ptr: pointer to dCSWDataResidue of Command Status Wrapper * @param csw_status_ptr : pointer to bCSWStatus of Command Status Wrapper * * @return error * *****************************************************************************/ uint_8 msc_read_format_capacity_command ( uint_8 controller_ID, PTR_CBW cbw_ptr, uint_32* csw_residue_ptr, uint_8* csw_status_ptr ) { uint_8 i; /* general variable for counting in loop */ uint_8 error = USBERR_TX_FAILED; /* size of data to be sent in data phase for this command*/ uint_32 response_size; uint_16 allocation_length = (uint_16)((cbw_ptr->command_block[7] << 8) | cbw_ptr->command_block[8]); uint_8_ptr response_data_ptr;/* pointer to data to be sent in data phase for this command*/ uint_8 num_formattable_cap_desc = (uint_8)(g_msc_scsi.formatted_disk ? (IMPLEMENTING_DISK_DRIVE?0x02:0x03):0x00); /* gives the number of Formattable Capacity Descriptor to be sent by device in response to read format capacities command */ /* 0x03 is number of formattable capacity desc. for HD while for DD its 2 if formatted drive is there */ CAPACITY_LIST_HEADER_STRUCT capacity_list_header = {0x00,0x00,0x00,0x00}; /* Initializing all fields */ CURR_MAX_CAPACITY_DESC_STRUCT curr_max_cap_header; FORMATTABLE_CAP_DESC formattable_cap_descriptor; uint_32 desc_code = (uint_32)(g_msc_scsi.formatted_disk?FORMATTED_MEDIA: UNFORMATTED_MEDIA); #if (!defined LITTLE_ENDIAN) formattable_cap_descriptor.num_blocks = g_msc_scsi.device_info.total_lba_device_supports; formattable_cap_descriptor.block_len = g_msc_scsi.device_info.length_of_each_lba_of_device; #else formattable_cap_descriptor.num_blocks = BYTESWAP32(g_msc_scsi.device_info.total_lba_device_supports); formattable_cap_descriptor.block_len = BYTESWAP32(g_msc_scsi.device_info.length_of_each_lba_of_device); #endif /* initialize sense code values */ g_msc_scsi.request_sense.sense_key = NO_SENSE; g_msc_scsi.request_sense.add_sense_code = NO_SENSE; g_msc_scsi.request_sense.add_sense_code_qual = NO_SENSE; capacity_list_header.capacity_list_len = (uint_8)(num_formattable_cap_desc * 8); #if (!defined LITTLE_ENDIAN) curr_max_cap_header.num_blocks = g_msc_scsi.device_info.total_lba_device_supports; curr_max_cap_header.desc_code_and_block_len = (desc_code << 24)| g_msc_scsi.device_info.length_of_each_lba_of_device; #else curr_max_cap_header.num_blocks = BYTESWAP32(g_msc_scsi.device_info.total_lba_device_supports); curr_max_cap_header.desc_code_and_block_len = BYTESWAP32((desc_code << 24)| g_msc_scsi.device_info.length_of_each_lba_of_device); #endif response_size = sizeof(capacity_list_header) + sizeof(curr_max_cap_header)+ sizeof(formattable_cap_descriptor) * num_formattable_cap_desc; if(response_size > allocation_length) { /* comparing the length of data available with allocation length value sent in CBW which indicates the length of buffer host has reserved for data phase of this command */ response_size = allocation_length; } /* reserving memory for response data */ response_data_ptr = (uint_8_ptr)response_data_array; USB_memcopy(&capacity_list_header, response_data_ptr, sizeof(capacity_list_header)); USB_memcopy(&curr_max_cap_header, response_data_ptr + sizeof(capacity_list_header),sizeof(curr_max_cap_header)); if(g_msc_scsi.formatted_disk) { for(i = 0; i < num_formattable_cap_desc; i++) { USB_memcopy(&formattable_cap_descriptor, response_data_ptr + sizeof(capacity_list_header) + sizeof(curr_max_cap_header)+ sizeof(formattable_cap_descriptor) * i, sizeof(formattable_cap_descriptor)); } } g_msc_scsi.thirteen_case_struct.controller_ID = controller_ID; g_msc_scsi.thirteen_case_struct.host_expected_data_len = cbw_ptr->data_length; g_msc_scsi.thirteen_case_struct.host_expected_direction = (uint_8)(cbw_ptr->flag >> USB_CBW_DIRECTION_SHIFT); g_msc_scsi.thirteen_case_struct.device_expected_data_len = response_size; g_msc_scsi.thirteen_case_struct.device_expected_direction = USB_SEND; g_msc_scsi.thirteen_case_struct.csw_status_ptr = csw_status_ptr; g_msc_scsi.thirteen_case_struct.csw_residue_ptr = csw_residue_ptr; g_msc_scsi.thirteen_case_struct.buffer_ptr = response_data_ptr; g_msc_scsi.thirteen_case_struct.lba_txrx_select = FALSE; error = msc_thirteen_cases_check(&(g_msc_scsi.thirteen_case_struct)); return error; }