demo
Dependencies: mbed SD_DISCO_F746NG BSP_DISCO_F746NG
main.cpp@2:522456118ea2, 2016-12-12 (annotated)
- Committer:
- adustm
- Date:
- Mon Dec 12 13:28:59 2016 +0000
- Revision:
- 2:522456118ea2
- Parent:
- 0:99e26e18b424
- Child:
- 3:33086de19b14
Remove dupplicated HAL_Delay function + change mbed-dev lib to mbed lib
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adustm | 0:99e26e18b424 | 1 | #include "mbed.h" |
adustm | 0:99e26e18b424 | 2 | #include "SD_DISCO_F746NG.h" |
adustm | 0:99e26e18b424 | 3 | |
adustm | 0:99e26e18b424 | 4 | SD_DISCO_F746NG sd; |
adustm | 0:99e26e18b424 | 5 | |
adustm | 0:99e26e18b424 | 6 | DigitalOut led_green(LED1); |
adustm | 0:99e26e18b424 | 7 | DigitalOut led_red(LED2); |
adustm | 0:99e26e18b424 | 8 | |
adustm | 0:99e26e18b424 | 9 | Serial pc(USBTX, USBRX); |
adustm | 0:99e26e18b424 | 10 | |
adustm | 0:99e26e18b424 | 11 | #define BLOCK_START_ADDR 0 /* Block start address */ |
adustm | 0:99e26e18b424 | 12 | #define BLOCKSIZE 512 /* Block Size in Bytes */ |
adustm | 0:99e26e18b424 | 13 | #define NUM_OF_BLOCKS 5 /* Total number of blocks */ |
adustm | 0:99e26e18b424 | 14 | #define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */ |
adustm | 0:99e26e18b424 | 15 | |
adustm | 0:99e26e18b424 | 16 | uint32_t aTxBuffer[BUFFER_WORDS_SIZE]; |
adustm | 0:99e26e18b424 | 17 | uint32_t aRxBuffer[BUFFER_WORDS_SIZE]; |
adustm | 0:99e26e18b424 | 18 | /* Private function prototypes -----------------------------------------------*/ |
adustm | 0:99e26e18b424 | 19 | void SD_main_test(void); |
adustm | 0:99e26e18b424 | 20 | void SD_Detection(void); |
adustm | 0:99e26e18b424 | 21 | |
adustm | 0:99e26e18b424 | 22 | static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset); |
adustm | 0:99e26e18b424 | 23 | static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength); |
adustm | 0:99e26e18b424 | 24 | |
adustm | 0:99e26e18b424 | 25 | int main() |
adustm | 0:99e26e18b424 | 26 | { |
adustm | 0:99e26e18b424 | 27 | uint8_t SD_state = SD_OK; |
adustm | 0:99e26e18b424 | 28 | pc.printf("\n\nuSD example start:\n"); |
adustm | 0:99e26e18b424 | 29 | led_red = 0; |
adustm | 0:99e26e18b424 | 30 | |
adustm | 0:99e26e18b424 | 31 | SD_state = sd.Init(); |
adustm | 0:99e26e18b424 | 32 | if(SD_state != SD_OK){ |
adustm | 0:99e26e18b424 | 33 | if(SD_state == MSD_ERROR_SD_NOT_PRESENT){ |
adustm | 0:99e26e18b424 | 34 | pc.printf("SD shall be inserted before running test\n"); |
adustm | 0:99e26e18b424 | 35 | } else { |
adustm | 0:99e26e18b424 | 36 | pc.printf("SD Initialization : FAIL.\n"); |
adustm | 0:99e26e18b424 | 37 | } |
adustm | 0:99e26e18b424 | 38 | pc.printf("SD Test Aborted.\n"); |
adustm | 0:99e26e18b424 | 39 | } else { |
adustm | 0:99e26e18b424 | 40 | pc.printf("SD Initialization : OK.\n"); |
adustm | 0:99e26e18b424 | 41 | |
adustm | 0:99e26e18b424 | 42 | if(sd.Erase(BLOCK_START_ADDR, (BLOCKSIZE * NUM_OF_BLOCKS)) != SD_OK){ |
adustm | 0:99e26e18b424 | 43 | pc.printf("SD ERASE : FAILED.\n"); |
adustm | 0:99e26e18b424 | 44 | pc.printf("SD Test Aborted.\n"); |
adustm | 0:99e26e18b424 | 45 | } else { |
adustm | 0:99e26e18b424 | 46 | pc.printf("SD ERASE : OK.\n"); |
adustm | 0:99e26e18b424 | 47 | |
adustm | 0:99e26e18b424 | 48 | /* Fill the buffer to write */ |
adustm | 0:99e26e18b424 | 49 | Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x22FF); |
adustm | 0:99e26e18b424 | 50 | |
adustm | 0:99e26e18b424 | 51 | if(sd.WriteBlocks(aTxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS) != SD_OK){ |
adustm | 0:99e26e18b424 | 52 | pc.printf("SD WRITE : FAILED.\n"); |
adustm | 0:99e26e18b424 | 53 | pc.printf("SD Test Aborted.\n"); |
adustm | 0:99e26e18b424 | 54 | } else { |
adustm | 0:99e26e18b424 | 55 | pc.printf("SD WRITE : OK.\n"); |
adustm | 0:99e26e18b424 | 56 | |
adustm | 0:99e26e18b424 | 57 | if(sd.ReadBlocks(aRxBuffer, BLOCK_START_ADDR, BLOCKSIZE, NUM_OF_BLOCKS)!= SD_OK){ |
adustm | 0:99e26e18b424 | 58 | pc.printf("SD READ : FAILED.\n"); |
adustm | 0:99e26e18b424 | 59 | pc.printf("SD Test Aborted.\n"); |
adustm | 0:99e26e18b424 | 60 | } else { |
adustm | 0:99e26e18b424 | 61 | pc.printf("SD READ : OK.\n"); |
adustm | 0:99e26e18b424 | 62 | if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0){ |
adustm | 0:99e26e18b424 | 63 | pc.printf("SD COMPARE : FAILED.\n"); |
adustm | 0:99e26e18b424 | 64 | pc.printf("SD Test Aborted.\n"); |
adustm | 0:99e26e18b424 | 65 | } else { |
adustm | 0:99e26e18b424 | 66 | pc.printf("SD Test : OK.\n"); |
adustm | 0:99e26e18b424 | 67 | pc.printf("SD can be removed.\n"); |
adustm | 0:99e26e18b424 | 68 | } |
adustm | 0:99e26e18b424 | 69 | } |
adustm | 0:99e26e18b424 | 70 | } |
adustm | 0:99e26e18b424 | 71 | } |
adustm | 0:99e26e18b424 | 72 | } |
adustm | 0:99e26e18b424 | 73 | |
adustm | 0:99e26e18b424 | 74 | while (1) { |
adustm | 0:99e26e18b424 | 75 | } |
adustm | 0:99e26e18b424 | 76 | } |
adustm | 0:99e26e18b424 | 77 | |
adustm | 0:99e26e18b424 | 78 | /** |
adustm | 0:99e26e18b424 | 79 | * @brief Fills buffer with user predefined data. |
adustm | 0:99e26e18b424 | 80 | * @param pBuffer: pointer on the buffer to fill |
adustm | 0:99e26e18b424 | 81 | * @param uwBufferLenght: size of the buffer to fill |
adustm | 0:99e26e18b424 | 82 | * @param uwOffset: first value to fill on the buffer |
adustm | 0:99e26e18b424 | 83 | * @retval None |
adustm | 0:99e26e18b424 | 84 | */ |
adustm | 0:99e26e18b424 | 85 | static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset) |
adustm | 0:99e26e18b424 | 86 | { |
adustm | 0:99e26e18b424 | 87 | uint32_t tmpIndex = 0; |
adustm | 0:99e26e18b424 | 88 | |
adustm | 0:99e26e18b424 | 89 | /* Put in global buffer different values */ |
adustm | 0:99e26e18b424 | 90 | for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ ) |
adustm | 0:99e26e18b424 | 91 | { |
adustm | 0:99e26e18b424 | 92 | pBuffer[tmpIndex] = tmpIndex + uwOffset; |
adustm | 0:99e26e18b424 | 93 | } |
adustm | 0:99e26e18b424 | 94 | } |
adustm | 0:99e26e18b424 | 95 | |
adustm | 0:99e26e18b424 | 96 | /** |
adustm | 0:99e26e18b424 | 97 | * @brief Compares two buffers. |
adustm | 0:99e26e18b424 | 98 | * @param pBuffer1, pBuffer2: buffers to be compared. |
adustm | 0:99e26e18b424 | 99 | * @param BufferLength: buffer's length |
adustm | 0:99e26e18b424 | 100 | * @retval 1: pBuffer identical to pBuffer1 |
adustm | 0:99e26e18b424 | 101 | * 0: pBuffer differs from pBuffer1 |
adustm | 0:99e26e18b424 | 102 | */ |
adustm | 0:99e26e18b424 | 103 | static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength) |
adustm | 0:99e26e18b424 | 104 | { |
adustm | 0:99e26e18b424 | 105 | while (BufferLength--) |
adustm | 0:99e26e18b424 | 106 | { |
adustm | 0:99e26e18b424 | 107 | if (*pBuffer1 != *pBuffer2) |
adustm | 0:99e26e18b424 | 108 | { |
adustm | 0:99e26e18b424 | 109 | return 1; |
adustm | 0:99e26e18b424 | 110 | } |
adustm | 0:99e26e18b424 | 111 | |
adustm | 0:99e26e18b424 | 112 | pBuffer1++; |
adustm | 0:99e26e18b424 | 113 | pBuffer2++; |
adustm | 0:99e26e18b424 | 114 | } |
adustm | 0:99e26e18b424 | 115 | |
adustm | 0:99e26e18b424 | 116 | return 0; |
adustm | 0:99e26e18b424 | 117 | } |
adustm | 0:99e26e18b424 | 118 |