/*! \brief Prints the variables stored in NVRAM. * * \param nvram_data Pointer to the NVRAM data structure to print. */ static void print_nvram_variables(nvram_data_t *nvram_data) { print_dbg("var8:\t0x"); print_dbg_char_hex(nvram_data->var8); print_dbg("\r\nvar16:\t0x"); print_dbg_short_hex(nvram_data->var16); print_dbg("\r\nvar8_3:\t0x"); print_dbg_char_hex(nvram_data->var8_3[0]); print_dbg_char_hex(nvram_data->var8_3[1]); print_dbg_char_hex(nvram_data->var8_3[2]); print_dbg("\r\nvar32:\t0x"); print_dbg_hex(nvram_data->var32); print_dbg("\r\n"); }
// build and send a midi serial packet void op_midi_out_note_send_packet( op_midi_out_note_t* mout ) { u8 pack[3]; if(mout->vel == 0) { // note on/off pack[0] = 0x80; } else { pack[0] = 0x90; } pack[0] |= (u8)(mout->chan & 0x0f); pack[1] = (u8)(mout->num); pack[2] = (u8)(mout->vel); print_dbg("\r\n midi_out_note_send_packet; data: "); print_dbg_char_hex(pack[0]); print_dbg(" "); print_dbg_char_hex(pack[1]); print_dbg(" "); print_dbg_char_hex(pack[2]); print_dbg(" "); midi_write(pack, 3); }
/*! \brief Initializes SD/MMC resources: GPIO, SPI and SD/MMC. */ static void sd_mmc_resources_init(void) { // GPIO pins used for SD/MMC interface static const gpio_map_t SD_MMC_SPI_GPIO_MAP = { {SD_MMC_SPI_SCK_PIN, SD_MMC_SPI_SCK_FUNCTION }, // SPI Clock. {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION}, // MISO. {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION}, // MOSI. {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION} // Chip Select NPCS. }; // SPI options. spi_options_t spiOptions = { .reg = SD_MMC_SPI_NPCS, .baudrate = SD_MMC_SPI_MASTER_SPEED, // Defined in conf_sd_mmc_spi.h. .bits = SD_MMC_SPI_BITS, // Defined in conf_sd_mmc_spi.h. .spck_delay = 0, .trans_delay = 0, .stay_act = 1, .spi_mode = 0, .modfdis = 1 }; // Assign I/Os to SPI. gpio_enable_module(SD_MMC_SPI_GPIO_MAP, sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0])); // Initialize as master. spi_initMaster(SD_MMC_SPI, &spiOptions); // Set SPI selection mode: variable_ps, pcs_decode, delay. spi_selectionMode(SD_MMC_SPI, 0, 0, 0); // Enable SPI module. spi_enable(SD_MMC_SPI); // Initialize SD/MMC driver with SPI clock (PBA). sd_mmc_spi_init(spiOptions, PBA_HZ); } /*! \brief Initialize PDCA (Peripheral DMA Controller A) resources for the SPI transfer and start a dummy transfer */ void local_pdca_init(void) { // this PDCA channel is used for data reception from the SPI pdca_channel_options_t pdca_options_SPI_RX ={ // pdca channel options .addr = ram_buffer, // memory address. We take here the address of the string dummy_data. This string is located in the file dummy.h .size = 512, // transfer counter: here the size of the string .r_addr = NULL, // next memory address after 1st transfer complete .r_size = 0, // next transfer counter not used here .pid = AVR32_PDCA_CHANNEL_USED_RX, // select peripheral ID - data are on reception from SPI1 RX line .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits }; // this channel is used to activate the clock of the SPI by sending a dummy variables pdca_channel_options_t pdca_options_SPI_TX ={ // pdca channel options .addr = (void *)&dummy_data, // memory address. // We take here the address of the string dummy_data. // This string is located in the file dummy.h .size = 512, // transfer counter: here the size of the string .r_addr = NULL, // next memory address after 1st transfer complete .r_size = 0, // next transfer counter not used here .pid = AVR32_PDCA_CHANNEL_USED_TX, // select peripheral ID - data are on reception from SPI1 RX line .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits }; // Init PDCA transmission channel pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_TX, &pdca_options_SPI_TX); // Init PDCA Reception channel pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_RX, &pdca_options_SPI_RX); //! \brief Enable pdca transfer interrupt when completed INTC_register_interrupt(&pdca_int_handler, AVR32_PDCA_IRQ_0, AVR32_INTC_INT1); // pdca_channel_spi1_RX = 0 } /*! \brief Main function. Execution starts here. */ int main(void) { int i, j; // Switch the main clock to the external oscillator 0 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); // Initialize debug RS232 with PBA clock init_dbg_rs232(PBA_HZ); //start test print_dbg("\r\nInit SD/MMC Driver"); print_dbg("\r\nInsert SD/MMC..."); // Initialize Interrupt Controller INTC_init_interrupts(); // Initialize SD/MMC driver resources: GPIO, SPI and SD/MMC. sd_mmc_resources_init(); // Wait for a card to be inserted while (!sd_mmc_spi_mem_check()); print_dbg("\r\nCard detected!"); // Read Card capacity sd_mmc_spi_get_capacity(); print_dbg("Capacity = "); print_dbg_ulong(capacity >> 20); print_dbg(" MBytes"); // Enable all interrupts. Enable_global_interrupt(); // Initialize PDCA controller before starting a transfer local_pdca_init(); // Read the first sectors number 1, 2, 3 of the card for(j = 1; j <= 3; j++) { // Configure the PDCA channel: the address of memory ram_buffer to receive the data at sector address j pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_RX, &ram_buffer, 512); pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_TX, (void *)&dummy_data, 512); //send dummy to activate the clock end_of_transfer = false; // open sector number j if(sd_mmc_spi_read_open_PDCA (j)) { print_dbg("\r\nFirst 512 Bytes of Transfer number "); print_dbg_ulong(j); print_dbg(" :\r\n"); spi_write(SD_MMC_SPI,0xFF); // Write a first dummy data to synchronize transfer pdca_enable_interrupt_transfer_complete(AVR32_PDCA_CHANNEL_SPI_RX); pdca_channelrx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_RX); // get the correct PDCA channel pointer pdca_channeltx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_TX); // get the correct PDCA channel pointer pdca_channelrx->cr = AVR32_PDCA_TEN_MASK; // Enable RX PDCA transfer first pdca_channeltx->cr = AVR32_PDCA_TEN_MASK; // and TX PDCA transfer while(!end_of_transfer); // Display the first 2O bytes of the ram_buffer content for( i = 0; i < 20; i++) { print_dbg_char_hex( (U8)(*(ram_buffer + i))); } } else { print_dbg("\r\n! Unable to open memory \r\n"); } } print_dbg("\r\nEnd of the example.\r\n"); while (1); }
/*! \brief Main function. Execution starts here. */ int main(void) { U32 n_sector = 0; U32 card_size; // Unit is in sector. U32 bench_start_sector; U16 i = 0; U16 j = 0; t_cpu_time timer; Ctrl_status status; // Set CPU and PBA clock if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) ) return 42; // Initialize HMatrix init_hmatrix(); // Initialize debug RS232 with PBA clock init_dbg_rs232(pm_freq_param.pba_f); // Start test print_dbg("\r\nInitialize SD/MMC driver"); // Initialize SD/MMC driver resources: GPIO, SDIO and SD/MMC. sd_mmc_mci_resources_init(); // Wait for a card to be inserted #if (BOARD == EVK1104) #if EXAMPLE_SD_SLOT == SD_SLOT_8BITS print_dbg("\r\nInsert a MMC/SD card into the SD/MMC 8bits slot..."); #elif EXAMPLE_SD_SLOT == SD_SLOT_4BITS print_dbg("\r\nInsert a MMC/SD card into the SD/MMC 4bits slot..."); #else # error SD_SLOT not supported #endif while (!sd_mmc_mci_mem_check(EXAMPLE_SD_SLOT)); #else # error Board not supported #endif print_dbg("Card detected!\r\n"); // Read Card capacity sd_mmc_mci_read_capacity(EXAMPLE_SD_SLOT, &card_size); print_dbg("\r\nCapacity = "); print_dbg_ulong(card_size*512); print_dbg(" Bytes\r\n"); // Read the first sector number 0 of the card status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } // Display the ram_buffer content print_dbg("\r\nFirst sector of the card:\r\n"); for (i=0;i<(512);i++) { print_dbg_char_hex(buffer_in[i]); j++; if (j%32==0) print_dbg("\r\n"), j=0; else if (j%4==0) print_dbg(" "); } // Write some patterns in the first sector number 0 of the card print_dbg("Testing write.\r\n"); if( !test_sd_mmc_write(0) ) return -1; if( !test_sd_mmc_write(1) ) return -1; if( !test_sd_mmc_write(2) ) return -1; if( !test_sd_mmc_write(3) ) return -1; // Bench single-block read operations without DMA // print_dbg("Benching single-block read (without DMA). Please wait..."); n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector+=1; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench single-block read operations with DMA // print_dbg("Benching single-block read (with DMA). Please wait..."); n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_dma_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector+=1; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench multi-block read operations without DMA // print_dbg("Benching multi-block read (without DMA). Please wait..."); n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_multiple_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector+=ALLOCATED_SECTORS; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench multi-block read operations with DMA // print_dbg("Benching multi-block read (with DMA). Please wait..."); n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_dma_multiple_mem_2_ram(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector+=ALLOCATED_SECTORS; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench single-block write operations without DMA // print_dbg("Benching single-block write (without DMA). Please wait..."); status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); // Backup the first sector number 0 of the card if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return -1; } n_sector+=1; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench single-block write operations with DMA // print_dbg("Benching single-block write (with DMA). Please wait..."); status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); // Backup the first sector number 0 of the card if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_dma_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return -1; } n_sector+=1; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench multi-block write operations without DMA // print_dbg("Benching multi-block write (without DMA). Please wait..."); status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); // Backup the first sector number 0 of the card if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_multiple_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return -1; } n_sector+=ALLOCATED_SECTORS; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); // Bench multi-block write operations with DMA // print_dbg("Benching multi-block write (with DMA). Please wait..."); status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); // Backup the first sector number 0 of the card if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not read device.\r\n"); return -1; } n_sector = bench_start_sector = (card_size<BENCH_START_SECTOR) ? 0 : BENCH_START_SECTOR; cpu_set_timeout( cpu_ms_2_cy(BENCH_TIME_MS, pm_freq_param.cpu_f), &timer); while( !cpu_is_timeout(&timer) ) { status = sd_mmc_mci_dma_multiple_ram_2_mem(EXAMPLE_SD_SLOT, n_sector, buffer_in, ALLOCATED_SECTORS); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return -1; } n_sector+=ALLOCATED_SECTORS; } display_perf_bps((((U64)n_sector-bench_start_sector)*512)/(BENCH_TIME_MS/1000)); return 0; }
static bool test_sd_mmc_write(U8 pattern_id) { U16 i = 0; U16 j = 0; bool b_error; Ctrl_status status; print_dbg("Testing pattern "); print_dbg_char_hex( pattern_id ); // Backup sector 0. status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_saved); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR 001: can not read device.\r\n"); return false; } // Initialize pattern. for( i=0 ; i<SD_MMC_SECTOR_SIZE ; i++ ) { switch( pattern_id ) { case 0: buffer_in[i] = i; break; case 1: buffer_in[i] = 0xAA; break; case 2: buffer_in[i] = 0x55; break; default: buffer_in[i] = (256-(U8)i); break; } } // Write the pattern into the sector. status = sd_mmc_mci_ram_2_mem(EXAMPLE_SD_SLOT, 0, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return false; } // Clear buffer before comparing. for( i=0 ; i<SD_MMC_SECTOR_SIZE ; i++ ) buffer_in[i] = 0; // Read back the sector. status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR 002: can not read device.\r\n"); return false; } // Check the sector content. b_error = false; for( i=0 ; i<SD_MMC_SECTOR_SIZE ; i++ ) { switch( pattern_id ) { case 0: if( buffer_in[i]!=(i%256) ) b_error = true; break; case 1: if( buffer_in[i]!=0xAA ) b_error = true; break; case 2: if( buffer_in[i]!=0x55 ) b_error = true; break; default: if( buffer_in[i]!=((256-(U8)i)%256) ) b_error = true; break; } if( b_error ) { print_dbg("\r\nERROR: pattern comparison failed.\r\n"); for( i=0 ; i<SD_MMC_SECTOR_SIZE ; i++ ) { print_dbg_char_hex(buffer_in[i]); j++; if (j%32==0) print_dbg("\r\n"), j=0; else if (j%4==0) print_dbg(" "); } return false; } } // Restore back the old sector content. status = sd_mmc_mci_ram_2_mem(EXAMPLE_SD_SLOT, 0, buffer_saved); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR: can not write device.\r\n"); return false; } // Check that the sector restore is successful. status = sd_mmc_mci_mem_2_ram(EXAMPLE_SD_SLOT, 0, buffer_in); if( status!=CTRL_GOOD ) { print_dbg("\r\nERROR 003: can not read device.\r\n"); return false; } for (i=0;i<(512);i++) { if( buffer_in[i]!=buffer_saved[i] ) { print_dbg("\r\nERROR: old sector is not correctly restored.\r\n"); return false; } } print_dbg(" [PASS].\r\n"); return true; }
void slave_rx(u8 val) { print_dbg("\r\n i2c slave receive: "); print_dbg_char_hex(val); }