This file gives an example of using the SD/MMC card driver. If a SD or a MMC card is detected, a message is sent to user after that it copies data from SD/MMC to RAM using the PDCA.
Definition in file sd_mmc_spi_example.c.
#include <avr32/io.h>
#include "compiler.h"
#include "board.h"
#include "power_clocks_lib.h"
#include "gpio.h"
#include "spi.h"
#include "conf_sd_mmc_spi.h"
#include "sd_mmc_spi.h"
#include "usart.h"
#include "print_funcs.h"
#include "pdca.h"
#include "intc.h"
#include "dummy.h"
Go to the source code of this file.
Defines | |
#define | AVR32_PDCA_CHANNEL_SPI_RX 0 |
#define | AVR32_PDCA_CHANNEL_SPI_TX 1 |
#define | AVR32_PDCA_CHANNEL_USED_RX AVR32_PDCA_PID_SPI_RX |
#define | AVR32_PDCA_CHANNEL_USED_TX AVR32_PDCA_PID_SPI_TX |
#define | BUFFERSIZE 64 |
Number of bytes in the receive buffer when operating in slave mode. | |
#define | PBA_HZ FOSC0 |
PBA clock frequency (Hz). | |
Functions | |
void | local_pdca_init (void) |
Initialize PDCA (Peripheral DMA Controller A) resources for the SPI transfer and start a dummy transfer. | |
int | main (void) |
Main function. Execution starts here. | |
static void | pdca_int_handler (void) |
static void | sd_mmc_resources_init (void) |
Initializes SD/MMC resources: GPIO, SPI and SD/MMC. | |
void | wait () |
Variables | |
const char | dummy_data [] |
volatile Bool | end_of_transfer |
volatile avr32_pdca_channel_t * | pdca_channelrx |
volatile avr32_pdca_channel_t * | pdca_channeltx |
volatile char | ram_buffer [1000] |
#define AVR32_PDCA_CHANNEL_SPI_RX 0 |
Definition at line 138 of file sd_mmc_spi_example.c.
Referenced by local_pdca_init(), main(), and pdca_int_handler().
#define AVR32_PDCA_CHANNEL_SPI_TX 1 |
Definition at line 139 of file sd_mmc_spi_example.c.
Referenced by local_pdca_init(), main(), and pdca_int_handler().
#define AVR32_PDCA_CHANNEL_USED_RX AVR32_PDCA_PID_SPI_RX |
#define AVR32_PDCA_CHANNEL_USED_TX AVR32_PDCA_PID_SPI_TX |
#define BUFFERSIZE 64 |
Number of bytes in the receive buffer when operating in slave mode.
Definition at line 121 of file sd_mmc_spi_example.c.
#define PBA_HZ FOSC0 |
PBA clock frequency (Hz).
Definition at line 118 of file sd_mmc_spi_example.c.
Referenced by main(), and sd_mmc_resources_init().
void local_pdca_init | ( | void | ) |
Initialize PDCA (Peripheral DMA Controller A) resources for the SPI transfer and start a dummy transfer.
Enable pdca transfer interrupt when completed
Definition at line 235 of file sd_mmc_spi_example.c.
References AVR32_PDCA_CHANNEL_SPI_RX, AVR32_PDCA_CHANNEL_SPI_TX, AVR32_PDCA_CHANNEL_USED_RX, AVR32_PDCA_CHANNEL_USED_TX, dummy_data, pdca_int_handler(), and ram_buffer.
Referenced by main().
00236 { 00237 // this PDCA channel is used for data reception from the SPI 00238 pdca_channel_options_t pdca_options_SPI_RX ={ // pdca channel options 00239 00240 .addr = ram_buffer, 00241 // memory address. We take here the address of the string dummy_data. This string is located in the file dummy.h 00242 00243 .size = 512, // transfer counter: here the size of the string 00244 .r_addr = NULL, // next memory address after 1st transfer complete 00245 .r_size = 0, // next transfer counter not used here 00246 .pid = AVR32_PDCA_CHANNEL_USED_RX, // select peripheral ID - data are on reception from SPI1 RX line 00247 .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits 00248 }; 00249 00250 // this channel is used to activate the clock of the SPI by sending a dummy variables 00251 pdca_channel_options_t pdca_options_SPI_TX ={ // pdca channel options 00252 00253 .addr = (void *)&dummy_data, // memory address. 00254 // We take here the address of the string dummy_data. 00255 // This string is located in the file dummy.h 00256 .size = 512, // transfer counter: here the size of the string 00257 .r_addr = NULL, // next memory address after 1st transfer complete 00258 .r_size = 0, // next transfer counter not used here 00259 .pid = AVR32_PDCA_CHANNEL_USED_TX, // select peripheral ID - data are on reception from SPI1 RX line 00260 .transfer_size = PDCA_TRANSFER_SIZE_BYTE // select size of the transfer: 8,16,32 bits 00261 }; 00262 00263 // Init PDCA transmission channel 00264 pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_TX, &pdca_options_SPI_TX); 00265 00266 // Init PDCA Reception channel 00267 pdca_init_channel(AVR32_PDCA_CHANNEL_SPI_RX, &pdca_options_SPI_RX); 00268 00270 INTC_register_interrupt(&pdca_int_handler, AVR32_PDCA_IRQ_0, AVR32_INTC_INT1); // pdca_channel_spi1_RX = 0 00271 00272 }
int main | ( | void | ) |
Main function. Execution starts here.
Definition at line 277 of file sd_mmc_spi_example.c.
References AVR32_PDCA_CHANNEL_SPI_RX, AVR32_PDCA_CHANNEL_SPI_TX, capacity, dummy_data, end_of_transfer, local_pdca_init(), PBA_HZ, pdca_channelrx, pdca_channeltx, ram_buffer, sd_mmc_resources_init(), sd_mmc_spi_get_capacity(), sd_mmc_spi_mem_check(), and sd_mmc_spi_read_open_PDCA().
00278 { 00279 int i, j; 00280 00281 00282 // Switch the main clock to the external oscillator 0 00283 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP); 00284 00285 // Initialize debug RS232 with PBA clock 00286 init_dbg_rs232(PBA_HZ); 00287 00288 //start test 00289 print_dbg("\nInit SD/MMC Driver"); 00290 print_dbg("\nInsert SD/MMC..."); 00291 00292 // Initialize Interrupt Controller 00293 INTC_init_interrupts(); 00294 00295 // Initialize SD/MMC driver resources: GPIO, SPI and SD/MMC. 00296 sd_mmc_resources_init(); 00297 00298 // Wait for a card to be inserted 00299 while ( sd_mmc_spi_mem_check() != OK ); 00300 print_dbg("\nCard detected!"); 00301 00302 // Read Card capacity 00303 sd_mmc_spi_get_capacity(); 00304 print_dbg("Capacity = "); 00305 print_dbg_ulong(capacity >> 20); 00306 print_dbg(" MBytes"); 00307 00308 // Enable all interrupts. 00309 Enable_global_interrupt(); 00310 00311 // Initialize PDCA controller before starting a transfer 00312 local_pdca_init(); 00313 00314 // Read the first sectors number 1, 2, 3 of the card 00315 for(j = 1; j <= 3; j++) 00316 { 00317 // Configure the PDCA channel: the adddress of memory ram_buffer to receive the data at sector address j 00318 pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_RX, 00319 &ram_buffer, 00320 512); 00321 00322 pdca_load_channel( AVR32_PDCA_CHANNEL_SPI_TX, 00323 (void *)&dummy_data, 00324 512); //send dummy to activate the clock 00325 00326 end_of_transfer = FALSE; 00327 // open sector number j 00328 if(sd_mmc_spi_read_open_PDCA (j) == OK) 00329 { 00330 print_dbg("\nFirst 512 Bytes of Transfer number "); 00331 print_dbg_ulong(j); 00332 print_dbg(" :\n"); 00333 00334 spi_write(SD_MMC_SPI,0xFF); // Write a first dummy data to synchronise transfer 00335 pdca_enable_interrupt_transfer_complete(AVR32_PDCA_CHANNEL_SPI_RX); 00336 pdca_channelrx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_RX); // get the correct PDCA channel pointer 00337 pdca_channeltx =(volatile avr32_pdca_channel_t*) pdca_get_handler(AVR32_PDCA_CHANNEL_SPI_TX); // get the correct PDCA channel pointer 00338 pdca_channelrx->cr = AVR32_PDCA_TEN_MASK; // Enable RX PDCA transfer first 00339 pdca_channeltx->cr = AVR32_PDCA_TEN_MASK; // and TX PDCA transfer 00340 00341 while(!end_of_transfer); 00342 00343 // Display the first 2O bytes of the ram_buffer content 00344 for( i = 0; i < 20; i++) 00345 { 00346 print_dbg_char_hex( (U8)(*(ram_buffer + i))); 00347 } 00348 } 00349 else 00350 { 00351 print_dbg("\n! Unable to open memory \n"); 00352 } 00353 } 00354 print_dbg("\nEnd of the example.\n"); 00355 00356 while (1); 00357 }
static void pdca_int_handler | ( | void | ) | [static] |
Definition at line 168 of file sd_mmc_spi_example.c.
References AVR32_PDCA_CHANNEL_SPI_RX, AVR32_PDCA_CHANNEL_SPI_TX, end_of_transfer, sd_mmc_spi_read_close_PDCA(), and wait().
Referenced by local_pdca_init().
00169 { 00170 // Disable all interrupts. 00171 Disable_global_interrupt(); 00172 00173 // Disable interrupt channel. 00174 pdca_disable_interrupt_transfer_complete(AVR32_PDCA_CHANNEL_SPI_RX); 00175 00176 sd_mmc_spi_read_close_PDCA();//unselects the SD/MMC memory. 00177 wait(); 00178 // Disable unnecessary channel 00179 pdca_disable(AVR32_PDCA_CHANNEL_SPI_TX); 00180 pdca_disable(AVR32_PDCA_CHANNEL_SPI_RX); 00181 00182 // Enable all interrupts. 00183 Enable_global_interrupt(); 00184 00185 end_of_transfer = TRUE; 00186 }
static void sd_mmc_resources_init | ( | void | ) | [static] |
Initializes SD/MMC resources: GPIO, SPI and SD/MMC.
Definition at line 191 of file sd_mmc_spi_example.c.
References PBA_HZ, SD_MMC_SPI_BITS, sd_mmc_spi_init(), and SD_MMC_SPI_MASTER_SPEED.
Referenced by main().
00192 { 00193 // GPIO pins used for SD/MMC interface 00194 static const gpio_map_t SD_MMC_SPI_GPIO_MAP = 00195 { 00196 {SD_MMC_SPI_SCK_PIN, SD_MMC_SPI_SCK_FUNCTION }, // SPI Clock. 00197 {SD_MMC_SPI_MISO_PIN, SD_MMC_SPI_MISO_FUNCTION}, // MISO. 00198 {SD_MMC_SPI_MOSI_PIN, SD_MMC_SPI_MOSI_FUNCTION}, // MOSI. 00199 {SD_MMC_SPI_NPCS_PIN, SD_MMC_SPI_NPCS_FUNCTION} // Chip Select NPCS. 00200 }; 00201 00202 // SPI options. 00203 spi_options_t spiOptions = 00204 { 00205 .reg = SD_MMC_SPI_NPCS, 00206 .baudrate = SD_MMC_SPI_MASTER_SPEED, // Defined in conf_sd_mmc_spi.h. 00207 .bits = SD_MMC_SPI_BITS, // Defined in conf_sd_mmc_spi.h. 00208 .spck_delay = 0, 00209 .trans_delay = 0, 00210 .stay_act = 1, 00211 .spi_mode = 0, 00212 .modfdis = 1 00213 }; 00214 00215 // Assign I/Os to SPI. 00216 gpio_enable_module(SD_MMC_SPI_GPIO_MAP, 00217 sizeof(SD_MMC_SPI_GPIO_MAP) / sizeof(SD_MMC_SPI_GPIO_MAP[0])); 00218 00219 // Initialize as master. 00220 spi_initMaster(SD_MMC_SPI, &spiOptions); 00221 00222 // Set SPI selection mode: variable_ps, pcs_decode, delay. 00223 spi_selectionMode(SD_MMC_SPI, 0, 0, 0); 00224 00225 // Enable SPI module. 00226 spi_enable(SD_MMC_SPI); 00227 00228 // Initialize SD/MMC driver with SPI clock (PBA). 00229 sd_mmc_spi_init(spiOptions, PBA_HZ); 00230 }
void wait | ( | ) |
const char dummy_data[] |
Initial value:
# 113 "/cygdrive/d/TEMP/1.7.0-AT32UC3/COMPONENTS/MEMORY/SD_MMC/SD_MMC_SPI/EXAMPLE/sd_mmc_spi_example.c" 2
Definition at line 112 of file sd_mmc_spi_example.c.
Referenced by local_pdca_init(), and main().
volatile Bool end_of_transfer |
volatile avr32_pdca_channel_t* pdca_channelrx |
volatile avr32_pdca_channel_t* pdca_channeltx |
volatile char ram_buffer[1000] |