summaryrefslogtreecommitdiff
path: root/bsp/at32/Libraries/rt_drivers/drv_sram.c
blob: d77b900728d10f15d3db15ccfaa87cadb95b6c6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-12-04      Leo      first version
 */

#include <board.h>
#include <rtthread.h>
#include "drv_sram.h"

#ifdef BSP_USING_SRAM

#define DRV_DEBUG
#define LOG_TAG             "drv.sram"
#include <drv_log.h>

uint16_t RT_TxBuffer[RT_BUFFER_SIZE];
uint16_t RT_RxBuffer[RT_BUFFER_SIZE];
uint32_t WriteReadStatus = 0, Index = 0;


#ifdef RT_USING_MEMHEAP_AS_HEAP
static struct rt_memheap system_heap;
#endif



static int rt_hw_sram_Init(void)
{
    int result = RT_EOK;
    XMC_Bank1_Type *XMC;
    XMC_NORSRAMInitType  XMC_NORSRAMInitStructure;
    XMC_NORSRAMTimingInitType  p;

    /* Init XMC pin */
    at32_msp_xmc_init(XMC);

    /*-- FSMC Configuration ------------------------------------------------------*/
    p.XMC_AdrOpTime         = 0x04;
    p.XMC_AdrHoldTime       = 0x04;
    p.XMC_DataOpTime        = 0x0a;
    p.XMC_IntervalBetweenOP = 0x0;
    p.XMC_CLKPsc            = 0x0;
    p.XMC_DataStableTime    = 0x0;
    p.XMC_Mode              = XMC_Mode_A;

    XMC_NORSRAMInitStructure.XMC_Bank                   = XMC_Bank1_NORSRAM3;
    XMC_NORSRAMInitStructure.XMC_DataAdrMux             = XMC_DataAdrMux_Disable;
    XMC_NORSRAMInitStructure.XMC_Dev                    = XMC_Dev_SRAM;
    XMC_NORSRAMInitStructure.XMC_BusType                = XMC_BusType_16b;
    XMC_NORSRAMInitStructure.XMC_EnableBurstMode        = XMC_BurstMode_Disable;
    XMC_NORSRAMInitStructure.XMC_EnableAsynWait         = XMC_AsynWait_Disable;
    XMC_NORSRAMInitStructure.XMC_WaitSignalLv           = XMC_WaitSignalLv_Low;
    XMC_NORSRAMInitStructure.XMC_EnableBurstModeSplit   = XMC_BurstModeSplit_Disable;
    XMC_NORSRAMInitStructure.XMC_WaitSignalConfig       = XMC_WaitSignalConfig_BeforeWaitState;
    XMC_NORSRAMInitStructure.XMC_EnableWrite            = XMC_WriteOperation_Enable;
    XMC_NORSRAMInitStructure.XMC_EnableWaitSignal       = XMC_WaitSignal_Disable;
    XMC_NORSRAMInitStructure.XMC_EnableWriteTiming      = XMC_WriteTiming_Disable;
    XMC_NORSRAMInitStructure.XMC_WriteBurstSyn          = XMC_WriteBurstSyn_Disable;
    XMC_NORSRAMInitStructure.XMC_RWTimingStruct         = &p;
    XMC_NORSRAMInitStructure.XMC_WTimingStruct          = &p;

    XMC_NORSRAMInit(&XMC_NORSRAMInitStructure);

    /*!< Enable FSMC Bank1_SRAM Bank */
    XMC_NORSRAMCmd(XMC_Bank1_NORSRAM3, ENABLE);

#ifdef RT_USING_MEMHEAP_AS_HEAP
        /* If RT_USING_MEMHEAP_AS_HEAP is enabled, SRAM is initialized to the heap */
        rt_memheap_init(&system_heap, "sram", (void *)EXT_SRAM_BEGIN, SRAM_LENGTH);
#endif

    return result;
}
INIT_BOARD_EXPORT(rt_hw_sram_Init);

#ifdef DRV_DEBUG
#ifdef FINSH_USING_MSH
/**
  * @brief  Writes a Half-word buffer to the FSMC SRAM memory.
  * @param  pBuffer : pointer to buffer.
  * @param  WriteAddr : SRAM memory internal address from which the data will be
  *         written.
  * @param  NumHalfwordToWrite : number of half-words to write.
  * @retval None
  */
static void SRAM_WriteBuffer(uint16_t* pBuffer, uint32_t WriteAddr, uint32_t NumHalfwordToWrite)
{
  for(; NumHalfwordToWrite != 0; NumHalfwordToWrite--) /*!< while there is data to write */
  {
    /*!< Transfer data to the memory */
    *(uint16_t *) (EXT_SRAM_BEGIN + WriteAddr) = *pBuffer++;

    /*!< Increment the address*/
    WriteAddr += 2;
  }
}

/**
  * @brief  Reads a block of data from the FSMC SRAM memory.
  * @param  pBuffer : pointer to the buffer that receives the data read from the
  *         SRAM memory.
  * @param  ReadAddr : SRAM memory internal address to read from.
  * @param  NumHalfwordToRead : number of half-words to read.
  * @retval None
  */
static void SRAM_ReadBuffer(uint16_t* pBuffer, uint32_t ReadAddr, uint32_t NumHalfwordToRead)
{
  for(; NumHalfwordToRead != 0; NumHalfwordToRead--) /*!< while there is data to read */
  {
    /*!< Read a half-word from the memory */
    *pBuffer++ = *(__IO uint16_t*) (EXT_SRAM_BEGIN + ReadAddr);

    /*!< Increment the address*/
    ReadAddr += 2;
  }
}

/**
  * @brief  Fill the global buffer
  * @param  pBuffer: pointer on the Buffer to fill
  * @param  BufferSize: size of the buffer to fill
  * @param  Offset: first value to fill on the Buffer
  */
static void Fill_Buffer(uint16_t *pBuffer, uint16_t BufferLenght, uint32_t Offset)
{
    uint16_t IndexTmp = 0;

    /* Put in global buffer same values */
    for (IndexTmp = 0; IndexTmp < BufferLenght; IndexTmp++ )
    {
        pBuffer[IndexTmp] = IndexTmp + Offset;
    }
}

int sram_test(void)
{
    /* Write data to XMC SRAM memory */
    /* Fill the buffer to send */
    Fill_Buffer(RT_TxBuffer, RT_BUFFER_SIZE, 0x3212);
    SRAM_WriteBuffer(RT_TxBuffer, RT_WRITE_READ_ADDR, RT_BUFFER_SIZE);


    /* Read data from XMC SRAM memory */
    SRAM_ReadBuffer(RT_RxBuffer, RT_WRITE_READ_ADDR, RT_BUFFER_SIZE);

    /* Read back SRAM memory and check content correctness */
    for (Index = 0x00; (Index < RT_BUFFER_SIZE) && (WriteReadStatus == 0); Index++)
    {
        if (RT_RxBuffer[Index] != RT_TxBuffer[Index])
        {
            WriteReadStatus = Index + 1;
        }
    }

    if(WriteReadStatus == 0)
    {
        LOG_D("SRAM test success!");
    }
    else
    {
        LOG_E("SRAM test failed!");
    }

    return RT_EOK;
}
MSH_CMD_EXPORT(sram_test, sram test)
#endif /* FINSH_USING_MSH */
#endif /* DRV_DEBUG */
#endif /* BSP_USING_SRAM */