![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
SPKT
Dependencies: mbed SD_DISCO_F746NG BSP_DISCO_F746NG
main.cpp@4:12fc2f759b2e, 2019-06-04 (annotated)
- Committer:
- phungductung
- Date:
- Tue Jun 04 09:07:12 2019 +0000
- Revision:
- 4:12fc2f759b2e
- Parent:
- 3:33086de19b14
SPKT;
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 | Serial pc(USBTX, USBRX); |
adustm | 0:99e26e18b424 | 6 | |
adustm | 0:99e26e18b424 | 7 | #define BLOCK_START_ADDR 0 /* Block start address */ |
adustm | 0:99e26e18b424 | 8 | #define NUM_OF_BLOCKS 5 /* Total number of blocks */ |
adustm | 0:99e26e18b424 | 9 | #define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */ |
adustm | 0:99e26e18b424 | 10 | |
adustm | 0:99e26e18b424 | 11 | uint32_t aTxBuffer[BUFFER_WORDS_SIZE]; |
adustm | 0:99e26e18b424 | 12 | uint32_t aRxBuffer[BUFFER_WORDS_SIZE]; |
adustm | 0:99e26e18b424 | 13 | /* Private function prototypes -----------------------------------------------*/ |
adustm | 0:99e26e18b424 | 14 | void SD_main_test(void); |
adustm | 0:99e26e18b424 | 15 | void SD_Detection(void); |
adustm | 0:99e26e18b424 | 16 | |
adustm | 0:99e26e18b424 | 17 | static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset); |
adustm | 0:99e26e18b424 | 18 | static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength); |
adustm | 0:99e26e18b424 | 19 | |
adustm | 0:99e26e18b424 | 20 | int main() |
adustm | 0:99e26e18b424 | 21 | { |
adustm | 3:33086de19b14 | 22 | uint8_t SD_state = MSD_OK; |
phungductung | 4:12fc2f759b2e | 23 | pc.printf("\n\nuSD start:\n\r"); |
adustm | 0:99e26e18b424 | 24 | SD_state = sd.Init(); |
adustm | 3:33086de19b14 | 25 | if(SD_state != MSD_OK){ |
adustm | 0:99e26e18b424 | 26 | if(SD_state == MSD_ERROR_SD_NOT_PRESENT){ |
phungductung | 4:12fc2f759b2e | 27 | pc.printf("xin gan the SD truoc khi test\n\r"); |
adustm | 0:99e26e18b424 | 28 | } else { |
phungductung | 4:12fc2f759b2e | 29 | pc.printf("Khoi tao the SD: FAIL.\n\r"); |
adustm | 0:99e26e18b424 | 30 | } |
phungductung | 4:12fc2f759b2e | 31 | pc.printf("Kiem tra the SD bi huy bo.\n\r"); |
adustm | 0:99e26e18b424 | 32 | } else { |
phungductung | 4:12fc2f759b2e | 33 | pc.printf("Khoi tao the SD: OK.\n\r"); |
adustm | 3:33086de19b14 | 34 | SD_state = sd.Erase(BLOCK_START_ADDR, (BLOCK_START_ADDR + NUM_OF_BLOCKS - 1)); |
adustm | 3:33086de19b14 | 35 | |
adustm | 3:33086de19b14 | 36 | /* Wait until SD card is ready to use for new operation */ |
adustm | 3:33086de19b14 | 37 | while(sd.GetCardState() != SD_TRANSFER_OK){ |
adustm | 3:33086de19b14 | 38 | } |
adustm | 3:33086de19b14 | 39 | if (SD_state != MSD_OK){ |
phungductung | 4:12fc2f759b2e | 40 | pc.printf("Xoa the SD: FAILED.\n\r"); |
phungductung | 4:12fc2f759b2e | 41 | pc.printf("Kiem tra the SD bi huy bo.\n\r"); |
adustm | 0:99e26e18b424 | 42 | } else { |
phungductung | 4:12fc2f759b2e | 43 | pc.printf("Xoa the SD: OK.\n\r"); |
phungductung | 4:12fc2f759b2e | 44 | |
adustm | 0:99e26e18b424 | 45 | /* Fill the buffer to write */ |
adustm | 3:33086de19b14 | 46 | Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x2300); |
adustm | 0:99e26e18b424 | 47 | |
adustm | 3:33086de19b14 | 48 | SD_state = sd.WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 10000); |
adustm | 3:33086de19b14 | 49 | /* Wait until SD card is ready to use for new operation */ |
adustm | 3:33086de19b14 | 50 | while(sd.GetCardState() != SD_TRANSFER_OK){ |
adustm | 3:33086de19b14 | 51 | } |
adustm | 3:33086de19b14 | 52 | |
adustm | 3:33086de19b14 | 53 | if (SD_state != MSD_OK){ |
phungductung | 4:12fc2f759b2e | 54 | pc.printf("Ghi du lieu len the SD: FAILED.\n\r"); |
phungductung | 4:12fc2f759b2e | 55 | pc.printf("Kiem tra the SD bi huy bo.\n\r"); |
adustm | 0:99e26e18b424 | 56 | } else { |
phungductung | 4:12fc2f759b2e | 57 | pc.printf("Ghi du lieu len the SD: OK.\n\r"); |
adustm | 3:33086de19b14 | 58 | SD_state = sd.ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 10000); |
adustm | 3:33086de19b14 | 59 | /* Wait until SD card is ready to use for new operation */ |
adustm | 3:33086de19b14 | 60 | while(sd.GetCardState() != SD_TRANSFER_OK){ |
adustm | 3:33086de19b14 | 61 | } |
adustm | 3:33086de19b14 | 62 | |
adustm | 3:33086de19b14 | 63 | if (SD_state != MSD_OK){ |
phungductung | 4:12fc2f759b2e | 64 | pc.printf("Doc the SD: FAILED.\n\r"); |
phungductung | 4:12fc2f759b2e | 65 | pc.printf("Kiem tra the SD bi huy bo.\n\r"); |
adustm | 0:99e26e18b424 | 66 | } else { |
phungductung | 4:12fc2f759b2e | 67 | pc.printf("Doc the SD: OK.\n\r"); |
adustm | 0:99e26e18b424 | 68 | if(Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0){ |
phungductung | 4:12fc2f759b2e | 69 | pc.printf("So sanh the SD: FAILED.\n\r"); |
phungductung | 4:12fc2f759b2e | 70 | pc.printf("Kiem tra the SD bi huy bo.\n\r"); |
adustm | 0:99e26e18b424 | 71 | } else { |
phungductung | 4:12fc2f759b2e | 72 | pc.printf("Kiem tra the SD: OK.\n\r"); |
phungductung | 4:12fc2f759b2e | 73 | pc.printf("Co the go bo the SD.\n\r"); |
adustm | 0:99e26e18b424 | 74 | } |
adustm | 0:99e26e18b424 | 75 | } |
adustm | 0:99e26e18b424 | 76 | } |
adustm | 0:99e26e18b424 | 77 | } |
adustm | 0:99e26e18b424 | 78 | } |
adustm | 0:99e26e18b424 | 79 | |
adustm | 0:99e26e18b424 | 80 | while (1) { |
adustm | 0:99e26e18b424 | 81 | } |
adustm | 0:99e26e18b424 | 82 | } |
adustm | 0:99e26e18b424 | 83 | |
adustm | 0:99e26e18b424 | 84 | /** |
adustm | 0:99e26e18b424 | 85 | * @brief Fills buffer with user predefined data. |
adustm | 0:99e26e18b424 | 86 | * @param pBuffer: pointer on the buffer to fill |
adustm | 0:99e26e18b424 | 87 | * @param uwBufferLenght: size of the buffer to fill |
adustm | 0:99e26e18b424 | 88 | * @param uwOffset: first value to fill on the buffer |
adustm | 0:99e26e18b424 | 89 | * @retval None |
adustm | 0:99e26e18b424 | 90 | */ |
adustm | 0:99e26e18b424 | 91 | static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset) |
adustm | 0:99e26e18b424 | 92 | { |
adustm | 0:99e26e18b424 | 93 | uint32_t tmpIndex = 0; |
adustm | 0:99e26e18b424 | 94 | |
adustm | 0:99e26e18b424 | 95 | /* Put in global buffer different values */ |
adustm | 0:99e26e18b424 | 96 | for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++ ) |
adustm | 0:99e26e18b424 | 97 | { |
adustm | 0:99e26e18b424 | 98 | pBuffer[tmpIndex] = tmpIndex + uwOffset; |
adustm | 0:99e26e18b424 | 99 | } |
adustm | 0:99e26e18b424 | 100 | } |
adustm | 0:99e26e18b424 | 101 | |
adustm | 0:99e26e18b424 | 102 | /** |
adustm | 0:99e26e18b424 | 103 | * @brief Compares two buffers. |
adustm | 0:99e26e18b424 | 104 | * @param pBuffer1, pBuffer2: buffers to be compared. |
adustm | 0:99e26e18b424 | 105 | * @param BufferLength: buffer's length |
adustm | 0:99e26e18b424 | 106 | * @retval 1: pBuffer identical to pBuffer1 |
adustm | 0:99e26e18b424 | 107 | * 0: pBuffer differs from pBuffer1 |
adustm | 0:99e26e18b424 | 108 | */ |
adustm | 0:99e26e18b424 | 109 | static uint8_t Buffercmp(uint32_t* pBuffer1, uint32_t* pBuffer2, uint16_t BufferLength) |
adustm | 0:99e26e18b424 | 110 | { |
adustm | 0:99e26e18b424 | 111 | while (BufferLength--) |
adustm | 0:99e26e18b424 | 112 | { |
adustm | 0:99e26e18b424 | 113 | if (*pBuffer1 != *pBuffer2) |
adustm | 0:99e26e18b424 | 114 | { |
adustm | 0:99e26e18b424 | 115 | return 1; |
adustm | 0:99e26e18b424 | 116 | } |
adustm | 0:99e26e18b424 | 117 | |
adustm | 0:99e26e18b424 | 118 | pBuffer1++; |
adustm | 0:99e26e18b424 | 119 | pBuffer2++; |
adustm | 0:99e26e18b424 | 120 | } |
adustm | 0:99e26e18b424 | 121 | |
adustm | 0:99e26e18b424 | 122 | return 0; |
adustm | 0:99e26e18b424 | 123 | } |
adustm | 0:99e26e18b424 | 124 | |
phungductung | 4:12fc2f759b2e | 125 |