test f746

Dependencies:   BSP_DISCO_F746NG

Committer:
xalamander66
Date:
Thu Feb 25 14:29:39 2021 +0000
Revision:
0:e6941fb40a7a
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xalamander66 0:e6941fb40a7a 1 #include "mbed.h"
xalamander66 0:e6941fb40a7a 2 #include "stm32746g_discovery_sd.h"
xalamander66 0:e6941fb40a7a 3
xalamander66 0:e6941fb40a7a 4 #define BLOCK_START_ADDR 0 /* Block start address */
xalamander66 0:e6941fb40a7a 5 #define NUM_OF_BLOCKS 5 /* Total number of blocks */
xalamander66 0:e6941fb40a7a 6 #define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes */
xalamander66 0:e6941fb40a7a 7
xalamander66 0:e6941fb40a7a 8 uint32_t aTxBuffer[BUFFER_WORDS_SIZE];
xalamander66 0:e6941fb40a7a 9 uint32_t aRxBuffer[BUFFER_WORDS_SIZE];
xalamander66 0:e6941fb40a7a 10
xalamander66 0:e6941fb40a7a 11 /* Private function prototypes -----------------------------------------------*/
xalamander66 0:e6941fb40a7a 12 void SD_main_test(void);
xalamander66 0:e6941fb40a7a 13 void SD_Detection(void);
xalamander66 0:e6941fb40a7a 14
xalamander66 0:e6941fb40a7a 15 static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLenght, uint32_t uwOffset);
xalamander66 0:e6941fb40a7a 16 static uint8_t Buffercmp(uint32_t *pBuffer1, uint32_t *pBuffer2, uint16_t BufferLength);
xalamander66 0:e6941fb40a7a 17
xalamander66 0:e6941fb40a7a 18 int main()
xalamander66 0:e6941fb40a7a 19 {
xalamander66 0:e6941fb40a7a 20 uint8_t SD_state = MSD_OK;
xalamander66 0:e6941fb40a7a 21 static uint8_t prev_status = SD_PRESENT;
xalamander66 0:e6941fb40a7a 22
xalamander66 0:e6941fb40a7a 23 printf("\n\nSD example start:\n");
xalamander66 0:e6941fb40a7a 24
xalamander66 0:e6941fb40a7a 25 SD_state = BSP_SD_Init();
xalamander66 0:e6941fb40a7a 26 if (SD_state != MSD_OK) {
xalamander66 0:e6941fb40a7a 27 if (SD_state == MSD_ERROR_SD_NOT_PRESENT) {
xalamander66 0:e6941fb40a7a 28 printf("SD shall be inserted before running test\n");
xalamander66 0:e6941fb40a7a 29 } else {
xalamander66 0:e6941fb40a7a 30 printf("SD Initialization : FAIL.\n");
xalamander66 0:e6941fb40a7a 31 }
xalamander66 0:e6941fb40a7a 32 printf("SD Test Aborted.\n");
xalamander66 0:e6941fb40a7a 33 } else {
xalamander66 0:e6941fb40a7a 34 printf("SD Initialization : OK.\n");
xalamander66 0:e6941fb40a7a 35
xalamander66 0:e6941fb40a7a 36 SD_state = BSP_SD_Erase(BLOCK_START_ADDR, (BLOCK_START_ADDR + NUM_OF_BLOCKS - 1));
xalamander66 0:e6941fb40a7a 37
xalamander66 0:e6941fb40a7a 38 /* Wait until SD card is ready to use for new operation */
xalamander66 0:e6941fb40a7a 39 while (BSP_SD_GetCardState() != SD_TRANSFER_OK) {
xalamander66 0:e6941fb40a7a 40 }
xalamander66 0:e6941fb40a7a 41 if (SD_state != MSD_OK) {
xalamander66 0:e6941fb40a7a 42 printf("SD ERASE : FAILED.\n");
xalamander66 0:e6941fb40a7a 43 printf("SD Test Aborted.\n");
xalamander66 0:e6941fb40a7a 44 } else {
xalamander66 0:e6941fb40a7a 45 printf("SD ERASE : OK.\n");
xalamander66 0:e6941fb40a7a 46
xalamander66 0:e6941fb40a7a 47 /* Fill the buffer to write */
xalamander66 0:e6941fb40a7a 48 Fill_Buffer(aTxBuffer, BUFFER_WORDS_SIZE, 0x2300);
xalamander66 0:e6941fb40a7a 49
xalamander66 0:e6941fb40a7a 50 SD_state = BSP_SD_WriteBlocks(aTxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 10000);
xalamander66 0:e6941fb40a7a 51 /* Wait until SD card is ready to use for new operation */
xalamander66 0:e6941fb40a7a 52 while (BSP_SD_GetCardState() != SD_TRANSFER_OK) {
xalamander66 0:e6941fb40a7a 53 }
xalamander66 0:e6941fb40a7a 54
xalamander66 0:e6941fb40a7a 55 if (SD_state != MSD_OK) {
xalamander66 0:e6941fb40a7a 56 printf("SD WRITE : FAILED.\n");
xalamander66 0:e6941fb40a7a 57 printf("SD Test Aborted.\n");
xalamander66 0:e6941fb40a7a 58 } else {
xalamander66 0:e6941fb40a7a 59 printf("SD WRITE : OK.\n");
xalamander66 0:e6941fb40a7a 60
xalamander66 0:e6941fb40a7a 61 SD_state = BSP_SD_ReadBlocks(aRxBuffer, BLOCK_START_ADDR, NUM_OF_BLOCKS, 10000);
xalamander66 0:e6941fb40a7a 62 /* Wait until SD card is ready to use for new operation */
xalamander66 0:e6941fb40a7a 63 while (BSP_SD_GetCardState() != SD_TRANSFER_OK) {
xalamander66 0:e6941fb40a7a 64 }
xalamander66 0:e6941fb40a7a 65
xalamander66 0:e6941fb40a7a 66 if (SD_state != MSD_OK) {
xalamander66 0:e6941fb40a7a 67 printf("SD READ : FAILED.\n");
xalamander66 0:e6941fb40a7a 68 printf("SD Test Aborted.\n");
xalamander66 0:e6941fb40a7a 69 } else {
xalamander66 0:e6941fb40a7a 70 printf("SD READ : OK.\n");
xalamander66 0:e6941fb40a7a 71 if (Buffercmp(aTxBuffer, aRxBuffer, BUFFER_WORDS_SIZE) > 0) {
xalamander66 0:e6941fb40a7a 72 printf("SD COMPARE : FAILED.\n");
xalamander66 0:e6941fb40a7a 73 printf("SD Test Aborted.\n");
xalamander66 0:e6941fb40a7a 74 } else {
xalamander66 0:e6941fb40a7a 75 printf("SD Test : OK.\n");
xalamander66 0:e6941fb40a7a 76 printf("SD can be removed.\n");
xalamander66 0:e6941fb40a7a 77 }
xalamander66 0:e6941fb40a7a 78 }
xalamander66 0:e6941fb40a7a 79 }
xalamander66 0:e6941fb40a7a 80 }
xalamander66 0:e6941fb40a7a 81 }
xalamander66 0:e6941fb40a7a 82
xalamander66 0:e6941fb40a7a 83 while (1) {
xalamander66 0:e6941fb40a7a 84 if (BSP_SD_IsDetected() != SD_PRESENT) {
xalamander66 0:e6941fb40a7a 85 if (prev_status != SD_NOT_PRESENT) {
xalamander66 0:e6941fb40a7a 86 BSP_SD_Init();
xalamander66 0:e6941fb40a7a 87 prev_status = SD_NOT_PRESENT;
xalamander66 0:e6941fb40a7a 88 printf("SD removed\n");
xalamander66 0:e6941fb40a7a 89 }
xalamander66 0:e6941fb40a7a 90 } else if (prev_status != SD_PRESENT) {
xalamander66 0:e6941fb40a7a 91 printf("SD detected\n");
xalamander66 0:e6941fb40a7a 92 prev_status = SD_PRESENT;
xalamander66 0:e6941fb40a7a 93 }
xalamander66 0:e6941fb40a7a 94 }
xalamander66 0:e6941fb40a7a 95 }
xalamander66 0:e6941fb40a7a 96
xalamander66 0:e6941fb40a7a 97 /**
xalamander66 0:e6941fb40a7a 98 * @brief Fills buffer with user predefined data.
xalamander66 0:e6941fb40a7a 99 * @param pBuffer: pointer on the buffer to fill
xalamander66 0:e6941fb40a7a 100 * @param uwBufferLenght: size of the buffer to fill
xalamander66 0:e6941fb40a7a 101 * @param uwOffset: first value to fill on the buffer
xalamander66 0:e6941fb40a7a 102 * @retval None
xalamander66 0:e6941fb40a7a 103 */
xalamander66 0:e6941fb40a7a 104 static void Fill_Buffer(uint32_t *pBuffer, uint32_t uwBufferLength, uint32_t uwOffset)
xalamander66 0:e6941fb40a7a 105 {
xalamander66 0:e6941fb40a7a 106 uint32_t tmpIndex = 0;
xalamander66 0:e6941fb40a7a 107
xalamander66 0:e6941fb40a7a 108 /* Put in global buffer different values */
xalamander66 0:e6941fb40a7a 109 for (tmpIndex = 0; tmpIndex < uwBufferLength; tmpIndex++) {
xalamander66 0:e6941fb40a7a 110 pBuffer[tmpIndex] = tmpIndex + uwOffset;
xalamander66 0:e6941fb40a7a 111 }
xalamander66 0:e6941fb40a7a 112 }
xalamander66 0:e6941fb40a7a 113
xalamander66 0:e6941fb40a7a 114 /**
xalamander66 0:e6941fb40a7a 115 * @brief Compares two buffers.
xalamander66 0:e6941fb40a7a 116 * @param pBuffer1, pBuffer2: buffers to be compared.
xalamander66 0:e6941fb40a7a 117 * @param BufferLength: buffer's length
xalamander66 0:e6941fb40a7a 118 * @retval 1: pBuffer identical to pBuffer1
xalamander66 0:e6941fb40a7a 119 * 0: pBuffer differs from pBuffer1
xalamander66 0:e6941fb40a7a 120 */
xalamander66 0:e6941fb40a7a 121 static uint8_t Buffercmp(uint32_t *pBuffer1, uint32_t *pBuffer2, uint16_t BufferLength)
xalamander66 0:e6941fb40a7a 122 {
xalamander66 0:e6941fb40a7a 123 while (BufferLength--) {
xalamander66 0:e6941fb40a7a 124 if (*pBuffer1 != *pBuffer2) {
xalamander66 0:e6941fb40a7a 125 return 1;
xalamander66 0:e6941fb40a7a 126 }
xalamander66 0:e6941fb40a7a 127
xalamander66 0:e6941fb40a7a 128 pBuffer1++;
xalamander66 0:e6941fb40a7a 129 pBuffer2++;
xalamander66 0:e6941fb40a7a 130 }
xalamander66 0:e6941fb40a7a 131
xalamander66 0:e6941fb40a7a 132 return 0;
xalamander66 0:e6941fb40a7a 133 }
xalamander66 0:e6941fb40a7a 134