Advertisement
tilz0R

Untitled

Jun 5th, 2015
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.87 KB | None | 0 0
  1. /**
  2.  *  Keil project for FatFS for SD cards with benchmark for READ SPEED
  3.  *
  4.  *  Before you start, select your target, on the right of the "Load" button
  5.  *
  6.  *  @author     Tilen Majerle
  7.  *  @email      tilen@majerle.eu
  8.  *  @website    http://stm32f4-discovery.com
  9.  *  @ide        Keil uVision 5
  10.  *  @packs      STM32F4xx Keil packs version 2.2.0 or greater required
  11.  *  @stdperiph  STM32F4xx Standard peripheral drivers version 1.4.0 or greater required
  12.  */
  13. /* Include core modules */
  14. #include "stm32f4xx.h"
  15. /* Include my libraries here */
  16. #include "defines.h"
  17. #include "tm_stm32f4_delay.h"
  18. #include "tm_stm32f4_disco.h"
  19. #include "tm_stm32f4_fatfs.h"
  20. #include "tm_stm32f4_usart.h"
  21. #include <stdio.h>
  22. #include <string.h>
  23.  
  24. /* Fatfs object */
  25. FATFS FatFs;
  26. /* File object */
  27. FIL fil;
  28.  
  29. /* Create buffer of 20 512bytes long sector */
  30. uint8_t SD_Buffer[512 * 20];
  31.  
  32. int main(void) {
  33.     /* Free and total space */
  34.     uint32_t write_ok = 0, cnt = 0;
  35.     FRESULT fres;
  36.    
  37.     /* Initialize system */
  38.     SystemInit();
  39.    
  40.     /* Initialize delays */
  41.     TM_DELAY_Init();
  42.    
  43.     /* Initialize LEDs */
  44.     TM_DISCO_LedInit();
  45.    
  46.     /* Initialize USART for debug, USART6, TX: PC6, 921600 bauds */
  47.     TM_USART_Init(USART6, TM_USART_PinsPack_1, 921600);
  48.    
  49.     /* Print debug */
  50.     TM_USART_Puts(USART6, "SDCARD benchmark test for reading and writing\n");
  51.    
  52.     while (1) {
  53.  
  54.         /* Lets first write something to SDCARD */
  55.         if (!write_ok) {
  56.             /* Mount drive */
  57.             if (f_mount(&FatFs, "SD:", 1) == FR_OK) {
  58.                 /* Mounted OK, turn on RED LED */
  59.                 TM_DISCO_LedOn(LED_RED);
  60.                
  61.                 /* Try to open file */
  62.                 if ((fres = f_open(&fil, "SD:benc.txt", FA_CREATE_ALWAYS | FA_READ | FA_WRITE)) == FR_OK) {
  63.                    
  64.                     /* Reset time */
  65.                     TM_DELAY_SetTime(0);
  66.                    
  67.                     /* Write block of data */
  68.                     f_write(&fil, SD_Buffer, sizeof(SD_Buffer), &cnt);
  69.                    
  70.                     /* We are done here */
  71.                     printf("Writing is done. Written %d bytes in %d ms = %u B/s\n", cnt, TM_DELAY_Time(), (cnt * 1000) / TM_DELAY_Time());
  72.                    
  73.                     /* Save everything for sure */
  74.                     f_sync(&fil);
  75.                    
  76.                     /* Move pointer to the beginning of file */
  77.                     f_lseek(&fil, 0);
  78.                    
  79.                     /* Reset time */
  80.                     TM_DELAY_SetTime(0);
  81.                    
  82.                     /* Read everything from file */
  83.                     f_read(&fil, SD_Buffer, sizeof(SD_Buffer), &cnt);
  84.                    
  85.                     /* We are done here */
  86.                     printf("Reading is done. Read %d bytes in %d ms = %u B/s\n", cnt, TM_DELAY_Time(), (cnt * 1000) / TM_DELAY_Time());
  87.                    
  88.                     /* Close file, don't forget this! */
  89.                     f_close(&fil);
  90.                    
  91.                     write_ok = 1;
  92.                 } else {
  93.                     printf("Could not open file for write; FRES = %d\n", fres);
  94.                 }
  95.                
  96.                 /* Unmount drive, don't forget this! */
  97.                 f_mount(0, "SD:", 1);
  98.             } else {
  99.                 printf("Could not mount SD CARD\n");
  100.             }
  101.         }      
  102.     }
  103. }
  104.  
  105. /* Printf handler */
  106. int fputc(int ch, FILE* fil) {
  107.     /* Send to USART */
  108.     TM_USART_Putc(USART6, ch);
  109.    
  110.     /* Return character back */
  111.     return ch;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement