SAM4S-EK_FreeRTOS+FAT-SL  1.0
An example project to test the functionality of FreeRTOS+FAT-SL using SD card as data storage medium
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
SAM4S-EK C Ex.c
Go to the documentation of this file.
1 
8 #include <stdarg.h>
9 
10 #include "sam4.h"
11 #include "printf_stdarg.h"
12 #include "HSMCI.h"
13 #include "sd_mmc_protocol.h"
14 
15 #include "FreeRTOS.h"
16 #include "task.h"
17 #include "fat_sl.h"
18 #include "api_mdriver.h"
19 
22 #define fsSD_BUFFER_SIZE 200
23 
26 #define fsMAX_FILE_NAME_LEN 13
27 
30 #define fsPUTC_FILE_SIZE 100
31 
34 static char cSDBuffer[ fsSD_BUFFER_SIZE ];
35 
37 static const char *pcRoot = "/", *pcDirectory1 = "SUB1", *pcDirectory2 = "SUB2", *pcFullPath = "/SUB1/SUB2";
38 
44 void vCreateAndVerifySampleFiles( void );
45 
51 static void prvCreateDemoFilesUsing_f_write( void );
52 
58 static void prvVerifyDemoFileUsing_f_read( void );
59 
64 static void prvCreateDemoFileUsing_f_putc( void );
65 
71 static void prvVerifyDemoFileUsing_f_getc( void );
72 
76 void vApplicationMallocFailedHook ( void );
77 void vApplicationIdleHook ( void );
78 void vApplicationStackOverflowHook ( xTaskHandle pxTask, signed char *pcTaskName );
79 void vApplicationTickHook ( void );
80 
87 void SD_Test ( void* pvParameters );
88 void Impulse ( void* pvParameters );
89 
95 void Config_UART0 ( void )
96 {
98  PIOA->PIO_ABCDSR[0] = ~PIO_ABCDSR_P9;
99  PIOA->PIO_ABCDSR[1] = ~PIO_ABCDSR_P9;
100 
102  PIOA->PIO_PDR = PIO_PDR_P9;
103 
105  PIOA->PIO_ABCDSR[0] = ~PIO_ABCDSR_P10;
106  PIOA->PIO_ABCDSR[1] = ~PIO_ABCDSR_P10;
107 
109  PIOA->PIO_PDR = PIO_PDR_P10;
110 
112  PMC->PMC_PCER0 = PMC_PCER0_PID8;
113 
115  UART0->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS;
116 
119  UART0->UART_BRGR = 65;
120 
122  UART0->UART_MR = UART_MR_PAR_NO;
123 
125  UART0->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;
126 
128  UART0->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
129 
131  UART0->UART_MR = UART_MR_CHMODE_NORMAL;
132 }
133 
140 void WaitSomeTime ( void )
141 {
142  volatile uint32_t i = 0;
143 
144  while (i < 1000000)
145  {
146  i++;
147  }
148 }
149 
155 int main(void)
156 {
158  SystemInit();
160  Config_UART0();
162  hsmci_init();
165 
167  xTaskCreate( SD_Test, (const char*)"Task 1", 500, NULL, 2, NULL );
168  xTaskCreate( Impulse, (const char*)"Task 2", 500, NULL, 1, NULL );
169 
172 
173  for (;;);
174  return 0;
175 }
176 
182 void Impulse (void* pvParameters)
183 {
185  REG_PIOA_OER = 0x100000;
187  REG_PIOA_OWER = 0x100000;
189  portTickType xLastWakeTime;
191  xLastWakeTime = xTaskGetTickCount();
192  for( ;; )
193  {
195  REG_PIOA_ODSR ^= 0x100000;
197  vTaskDelayUntil( &xLastWakeTime, 500 );
198  //vTaskDelay(1000);
199  }
200 }
201 
207 void SD_Test (void* pvParameters)
208 {
209  REG_PIOA_OER = 0x80000;
210  REG_PIOA_OWER = 0x80000;
211 
212  char cFileName[ fsMAX_FILE_NAME_LEN ];
213  F_FILE *pxFile;
214  char *pcString = "This is a test string generated to calculate and evaluate the time required to write data on a SD card using the HSMCI interface of SAM4S";
216  sprintf( cFileName, "TestFile.txt");
217  portTickType xLastWakeTime;
218  xLastWakeTime = xTaskGetTickCount();
219  for( ;; )
220  {
221  REG_PIOA_ODSR ^= 0x80000;
224  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
226  pxFile = f_open( cFileName, "w" );
227  if( pxFile != NULL )
228  {
230  if( f_write( pcString, 1, 137, pxFile ) == 137)
231  {
233  //printf("Content written to file\n\r");
234  }
235  //printf("File created and saved\n\r");
237  f_close( pxFile );
238  }
240  vTaskDelayUntil( &xLastWakeTime, 1000 );
241  }
242 }
243 
251 {
252  unsigned char ucStatus;
253  printf("Please insert an SD card in slot\n\r");
254  while((REG_PIOA_PDSR & PIN_CDMCI) != 0);
255  printf("Card inserted\n\r");
256  WaitSomeTime();
258  ucStatus = f_initvolume( sd_init );
259 
261  if( ucStatus == F_ERR_NOTFORMATTED )
262  {
264  ucStatus = f_format( F_FAT32_MEDIA );
265  }
266 
267  if( ucStatus == F_NO_ERROR )
268  {
270  //prvCreateDemoFilesUsing_f_write();
271 
273  //prvVerifyDemoFileUsing_f_read();
274 
276  //prvCreateDemoFileUsing_f_putc();
277 
279  //prvVerifyDemoFileUsing_f_getc();
280  printf("SD card initialized\n\r");
281  }
282 }
283 
290 static void prvCreateDemoFilesUsing_f_write( void )
291 {
292 portBASE_TYPE xFileNumber, xWriteNumber;
293 char cFileName[ fsMAX_FILE_NAME_LEN ];
294 const portBASE_TYPE xMaxFiles = 5;
295 long lItemsWritten;
296 F_FILE *pxFile;
300  for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
301  {
303  sprintf( cFileName, "TestF%03d.txt", ( int ) xFileNumber );
304 
306  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
307 
309  pxFile = f_open( cFileName, "w" );
310  configASSERT( pxFile );
311 
314  //memset( cSDBuffer, ( int ) ( '0' + xFileNumber ), fsSD_BUFFER_SIZE );
315 
319  for( xWriteNumber = 0; xWriteNumber < xFileNumber; xWriteNumber++ )
320  {
321  lItemsWritten = f_write( cSDBuffer, fsSD_BUFFER_SIZE, 1, pxFile );
322  configASSERT( lItemsWritten == 1 );
323  }
324 
326  f_close( pxFile );
327  }
328 }
329 
335 static void prvVerifyDemoFileUsing_f_read( void )
336 {
337 portBASE_TYPE xFileNumber, xReadNumber;
338 char cFileName[ fsMAX_FILE_NAME_LEN ];
339 const portBASE_TYPE xMaxFiles = 5;
340 long lItemsRead, lChar;
341 F_FILE *pxFile;
342 
344  for( xFileNumber = 1; xFileNumber <= xMaxFiles; xFileNumber++ )
345  {
347  sprintf( cFileName, "root%03d.txt", ( int ) xFileNumber );
348 
351  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
352 
354  pxFile = f_open( cFileName, "r" );
355  configASSERT( pxFile );
356 
359  for( xReadNumber = 0; xReadNumber < xFileNumber; xReadNumber++ )
360  {
362  memset( cSDBuffer, 0x00, fsSD_BUFFER_SIZE );
363 
364  lItemsRead = f_read( cSDBuffer, fsSD_BUFFER_SIZE, 1, pxFile );
365  configASSERT( lItemsRead == 1 );
366 
370  for( lChar = 0; lChar < fsSD_BUFFER_SIZE; lChar++ )
371  {
372  configASSERT( cSDBuffer[ lChar ] == ( '0' + ( char ) xFileNumber ) );
373  }
374  }
375 
377  f_close( pxFile );
378  }
379 }
380 
387 static void prvCreateDemoFileUsing_f_putc( void )
388 {
389 unsigned char ucReturn;
390 int iByte, iReturned;
391 F_FILE *pxFile;
392 char cFileName[ fsMAX_FILE_NAME_LEN ];
393 
395  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
396 
398  ucReturn = f_mkdir( pcDirectory1 );
399  configASSERT( ucReturn == F_NO_ERROR );
400 
402  ucReturn = f_chdir( pcDirectory1 );
403  configASSERT( ucReturn == F_NO_ERROR );
404 
406  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
407 
409  ucReturn = f_mkdir( pcDirectory2 );
410  configASSERT( ucReturn == F_NO_ERROR );
411 
413  ucReturn = f_chdir( pcDirectory2 );
414  configASSERT( ucReturn == F_NO_ERROR );
415 
417  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
418  configASSERT( strcmp( cSDBuffer, pcFullPath ) == 0 );
419 
421  sprintf( cFileName, "%s.txt", pcDirectory2 );
422 
424  pxFile = f_open( cFileName, "w" );
425 
427  for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
428  {
429  iReturned = f_putc( ( ( int ) '0' + iByte ), pxFile );
430  configASSERT( iReturned == ( ( int ) '0' + iByte ) );
431  }
432 
434  f_close( pxFile );
435 
437  ucReturn = f_chdir( "../.." );
438  configASSERT( ucReturn == F_NO_ERROR );
439 
441  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
442  configASSERT( strcmp( cSDBuffer, pcRoot ) == 0 );
443 }
444 
450 static void prvVerifyDemoFileUsing_f_getc( void )
451 {
452 unsigned char ucReturn;
453 int iByte, iReturned;
454 F_FILE *pxFile;
455 char cFileName[ fsMAX_FILE_NAME_LEN ];
456 
458  ucReturn = f_chdir( pcFullPath );
459  configASSERT( ucReturn == F_NO_ERROR );
460 
462  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
463  configASSERT( strcmp( cSDBuffer, pcFullPath ) == 0 );
464 
466  sprintf( cFileName, "%s.txt", pcDirectory2 );
467 
469  pxFile = f_open( cFileName, "r" );
470 
472  for( iByte = 0; iByte < fsPUTC_FILE_SIZE; iByte++ )
473  {
474  iReturned = f_getc( pxFile );
475  configASSERT( iReturned == ( ( int ) '0' + iByte ) );
476  }
477 
479  f_close( pxFile );
480 
482  ucReturn = f_chdir( "../.." );
483  configASSERT( ucReturn == F_NO_ERROR );
484 
486  f_getcwd( cSDBuffer, fsSD_BUFFER_SIZE );
487 }
491 {
504  for (;;) {
505  }
506 }
510 {
521 }
524 void vApplicationStackOverflowHook(xTaskHandle pxTask, signed char *pcTaskName)
525 {
526  (void) pcTaskName;
527  (void) pxTask;
528 
534  for (;;) {
535  }
536 }
540 {
547 }
548 
#define f_getcwd(buffer, maxlen)
Definition: fat_sl.h:389
void vApplicationTickHook(void)
#define fsPUTC_FILE_SIZE
The number of bytes written to the file that uses f_putc() and f_getc().
Definition: SAM4S-EK C Ex.c:30
int sprintf(char *out, const char *format,...)
F_DRIVER * sd_init(unsigned long driver_param)
Definition: SDdrv_f.c:613
SD/MMC protocol definitions.
#define f_initvolume
Definition: fat_sl.h:267
#define taskEXIT_CRITICAL()
Definition: task.h:216
#define configASSERT(x)
int printf(const char *format,...)
void SystemInit(void)
Setup the microcontroller system.
Definition: system_sam4s.c:57
void SD_Test(void *pvParameters)
FreeRTOS tasks These are the method declarations for the tasks created and executed by FreeRTOS sched...
#define portTickType
Definition: FreeRTOS.h:728
void vApplicationStackOverflowHook(xTaskHandle pxTask, signed char *pcTaskName)
void vApplicationIdleHook(void)
void Impulse(void *pvParameters)
A function which creates an impulse This is a task used to generate a short impulse on an external LE...
uint32_t hsmci_init(void)
Initializes the low level driver.
Definition: HSMCI.c:172
void vTaskStartScheduler(void) PRIVILEGED_FUNCTION
Definition: tasks.c:1454
#define f_putc(ch, filehandle)
Definition: fat_sl.h:423
TickType_t xTaskGetTickCount(void) PRIVILEGED_FUNCTION
Definition: tasks.c:1674
#define xTaskHandle
Definition: FreeRTOS.h:729
#define fsSD_BUFFER_SIZE
The number of bytes read/written to the example files at a time.
Definition: SAM4S-EK C Ex.c:22
void vTaskDelayUntil(TickType_t *const pxPreviousWakeTime, const TickType_t xTimeIncrement) PRIVILEGED_FUNCTION
#define f_write(buf, size, _size_t, filehandle)
Definition: fat_sl.h:414
void Config_UART0(void)
A function which initializes UART This is a method used to configure the UART0 interface of SAM4S...
Definition: SAM4S-EK C Ex.c:95
void vApplicationMallocFailedHook(void)
FreeRTOS hook (or callback) functions that are defined in this file.
#define f_read(buf, size, _size_t, filehandle)
Definition: fat_sl.h:440
#define f_open(filename, mode)
Definition: fat_sl.h:439
void vCreateAndVerifySampleFiles(void)
A function which uses FreeRTOS+FAT-SL APIs Creates and verifies different files on the volume...
#define taskENTER_CRITICAL()
Definition: task.h:202
#define portBASE_TYPE
Definition: portmacro.h:91
#define f_getc(filehandle)
Definition: fat_sl.h:421
#define f_format(fattype)
Definition: fat_sl.h:387
int main(void)
Application entry point.
void WaitSomeTime(void)
A function which creates a delay This is a method used to generate a delay by executing a for loop fo...
#define f_mkdir(dirname)
Definition: fat_sl.h:398
#define taskDISABLE_INTERRUPTS()
Definition: task.h:226
Definition: fat_sl.h:107
#define xTaskCreate(pvTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask)
Definition: task.h:330
#define PIN_CDMCI
Definition: HSMCI.h:21
#define f_chdir(dirname)
Definition: fat_sl.h:396
#define f_close(filehandle)
Definition: fat_sl.h:438
#define fsMAX_FILE_NAME_LEN
8.3 format, plus null terminator.
Definition: SAM4S-EK C Ex.c:26