-
Notifications
You must be signed in to change notification settings - Fork 39
/
main.c
149 lines (119 loc) · 3.89 KB
/
main.c
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
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "drv_digital_out.h"
#include "drv_dma.h"
#include "board.h"
#include "delays.h"
// -------------------------------------------------------------------- MACROS
// #define TEST_AUTOMATIC_ALLOCATION
#ifndef PB0
#define TEST_PIN_NAME HAL_PIN_NC
#else
#define TEST_PIN_NAME PB0
#endif
#define BUFFER_SIZE 32
#ifndef TEST_AUTOMATIC_ALLOCATION
#define TEST_DMA_MODULE 1
#define TEST_DMA_STREAM 1
#define TEST_DMA_CHANNEL 3
#endif
// ----------------------------------------------------------------- VARIABLES
static dma_config_t dma_cfg;
static dma_t dma;
static dma_channel_t channels;
static digital_out_t pin;
int check_result = 0xFF;
static const uint32_t buffer_src_flash[ BUFFER_SIZE ]= {
0x01020304,0x05060708,0x090A0B0C,0x0D0E0F10,
0x11121314,0x15161718,0x191A1B1C,0x1D1E1F20,
0x21222324,0x25262728,0x292A2B2C,0x2D2E2F30,
0x31323334,0x35363738,0x393A3B3C,0x3D3E3F40,
0x41424344,0x45464748,0x494A4B4C,0x4D4E4F50,
0x51525354,0x55565758,0x595A5B5C,0x5D5E5F60,
0x61626364,0x65666768,0x696A6B6C,0x6D6E6F70,
0x71727374,0x75767778,0x797A7B7C,0x7D7E7F80
};
#ifdef PIC32xx
static uint32_t __attribute__((coherent)) buffer_dst_ram[ BUFFER_SIZE ];
#else
static uint32_t buffer_dst_ram[ BUFFER_SIZE ];
#endif
// ----------------------------------------------------------------- USER CODE
void test_fail();
int main( void ) {
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
digital_out_init( &pin, TEST_PIN_NAME );
digital_out_low( &pin );
Delay_1sec();
Delay_1sec();
// Set default configuration values first.
dma_configure_default( &dma_cfg );
// Now set data alignment to WORD(32-bit) because
// arrays are word size.
dma_cfg.data_align_source = DMA_DATA_ALIGN_BYTES_4;
dma_cfg.data_align_destination = DMA_DATA_ALIGN_BYTES_4;
// Then set memory addresses to be incremented
// for both memory and peripheral.
dma_cfg.src_inc = true;
dma_cfg.dst_inc = true;
#ifndef TEST_AUTOMATIC_ALLOCATION
// NOTE - on stm32f2/f4 and f7 MCUs only DMA2 can
// work in M2M mode.
dma_get_channels( &channels );
if ( !channels[ TEST_DMA_MODULE ][ TEST_DMA_STREAM ][ TEST_DMA_CHANNEL ] ) {
dma_cfg.module = TEST_DMA_MODULE;
dma_cfg.stream = TEST_DMA_STREAM;
dma_cfg.channel = TEST_DMA_CHANNEL;
} else {
test_fail();
}
#endif
// Test closing and opening effect on registers.
if ( DMA_SUCCESS != dma_open( &dma, &dma_cfg ) ) {
test_fail();
}
#ifndef TEST_AUTOMATIC_ALLOCATION
dma_get_channels( &channels );
if ( !channels[ TEST_DMA_MODULE ][ TEST_DMA_STREAM ][ TEST_DMA_CHANNEL ] ) {
test_fail();
}
#endif
if ( DMA_SUCCESS != dma_set_transfer_config(
&dma,
(uint32_t)&buffer_src_flash,
(uint32_t)&buffer_dst_ram,
sizeof( buffer_src_flash ),
DMA_SOURCE_MEMORY_REGION_PFM
) ) {
test_fail();
}
digital_out_high( &pin );
// Start transmission.
if ( DMA_SUCCESS != dma_transfer_start( &dma ) ) {
test_fail();
}
check_result = memcmp( buffer_src_flash, buffer_dst_ram, sizeof( buffer_src_flash ) );
while( 1 ) {
if ( !check_result ) {
digital_out_toggle( &pin );
Delay_100ms();
}
}
return 0;
}
void test_fail() {
while( 1 ) {
digital_out_toggle( &pin );
Delay_1sec();
}
}
// ----------------------------------------------------------------------- END