提交 | 用户 | age
|
483170
|
1 |
/** |
Q |
2 |
****************************************************************************** |
|
3 |
* @file : functions.c |
|
4 |
* @brief : funcstions program body |
|
5 |
****************************************************************************** |
|
6 |
*/ |
|
7 |
//#include "globaldef.h" |
|
8 |
#include "functions.h" |
|
9 |
#include "string.h" |
|
10 |
#include "stm32f0xx_hal.h" |
|
11 |
#if (BOARD_TYPE == 14) |
5dd1b7
|
12 |
#include "fpx.h" |
483170
|
13 |
#endif |
Q |
14 |
extern __IO uint32_t uwTick; |
|
15 |
//#include "Myprotocol.h" |
|
16 |
int TickFreq=10000; |
|
17 |
int CoreClkMHz; //=SystemCoreClock/1000000; |
|
18 |
int TickPriodClk; //=SystemCoreClock/TickFreq; |
|
19 |
unsigned int TickPrioduS; // |
|
20 |
volatile unsigned int nCurTick=0; |
|
21 |
volatile unsigned int CurTickuS=0; |
|
22 |
//volatile unsigned int ThisRunTime=0; //开机时间 |
|
23 |
//volatile unsigned int TotalRunTime=0; //总开机时间 |
|
24 |
//volatile unsigned int PwrCount=0; //开机次数 |
|
25 |
unsigned short ClkuS; //每个Clk的nS数, |
5dd1b7
|
26 |
|
Q |
27 |
unsigned char SPI1RecvBuf[64]; |
|
28 |
unsigned char SPI1SendBuf[64]; |
|
29 |
|
|
30 |
volatile unsigned char bSPI1Recving=0; |
|
31 |
volatile unsigned char bSPI1RecvDone =0; |
|
32 |
|
|
33 |
volatile unsigned char nSPI1RecvPos =0; |
|
34 |
volatile unsigned char nSPI1RecvLenInBuf=0; |
|
35 |
|
|
36 |
volatile unsigned char nSPI1ToSendLen=0; |
|
37 |
volatile unsigned char nSPI1SentLen=0; |
|
38 |
|
|
39 |
volatile unsigned char bSPI1Sending=0; |
|
40 |
volatile unsigned char bSPI1SendDone=0; |
|
41 |
|
483170
|
42 |
|
Q |
43 |
int InituS(int TickFreq1) |
|
44 |
{ |
|
45 |
TickPrioduS=1000000/TickFreq1; //每个SysTick的微秒数 |
|
46 |
CoreClkMHz=HAL_RCC_GetHCLKFreq()/1000000; //=SystemCoreClock/1000000;每uS的时钟数 |
|
47 |
TickPriodClk=SystemCoreClock/TickFreq1; //每个SysTick的时钟数 |
|
48 |
ClkuS=(1000000LL*65536)/SystemCoreClock; |
|
49 |
CurTickuS=TickPrioduS+100u; |
|
50 |
return 0; |
|
51 |
} |
|
52 |
|
|
53 |
inline unsigned int GetuS(void) |
|
54 |
{ |
|
55 |
// unsigned short Clk1=SysTick->VAL; |
|
56 |
|
|
57 |
int CurTickuS1=CurTickuS; |
|
58 |
int Val1=SysTick->VAL; |
|
59 |
if (CurTickuS1 != CurTickuS ) |
|
60 |
{ |
|
61 |
CurTickuS1=CurTickuS; |
|
62 |
Val1=SysTick->VAL; |
|
63 |
} |
|
64 |
uint32_t us1=CurTickuS1-(((Val1)*1365)>>16); |
|
65 |
return us1; |
|
66 |
} |
|
67 |
|
|
68 |
inline unsigned int GetTick(void) |
|
69 |
{ |
|
70 |
// unsigned short Clk1=SysTick->VAL; |
|
71 |
return nCurTick; |
|
72 |
} |
|
73 |
|
|
74 |
void logData(unsigned char d) |
|
75 |
{ |
842bb6
|
76 |
KMem.WDB[128+KMem.WDT[7]] = d; |
Q |
77 |
KMem.WDT[7]++; if (KMem.WDT[7]>=100) {KMem.WDT[7]=81;} |
483170
|
78 |
} |
Q |
79 |
|
842bb6
|
80 |
/* |
483170
|
81 |
const unsigned short crc16_table[256] = { |
Q |
82 |
0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf, |
|
83 |
0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7, |
|
84 |
0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e, |
|
85 |
0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876, |
|
86 |
0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd, |
|
87 |
0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5, |
|
88 |
0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c, |
|
89 |
0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974, |
|
90 |
0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb, |
|
91 |
0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3, |
|
92 |
0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a, |
|
93 |
0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72, |
|
94 |
0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9, |
|
95 |
0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1, |
|
96 |
0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738, |
|
97 |
0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70, |
|
98 |
0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7, |
|
99 |
0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff, |
|
100 |
0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036, |
|
101 |
0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e, |
|
102 |
0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5, |
|
103 |
0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd, |
|
104 |
0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134, |
|
105 |
0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c, |
|
106 |
0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3, |
|
107 |
0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb, |
|
108 |
0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232, |
|
109 |
0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a, |
|
110 |
0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1, |
|
111 |
0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9, |
|
112 |
0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330, |
|
113 |
0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78 |
|
114 |
}; |
|
115 |
|
|
116 |
unsigned short crc_check(const unsigned char * data, unsigned int length) |
|
117 |
{ |
|
118 |
unsigned short crc_reg = 0xFFFF; |
|
119 |
while (length--) |
|
120 |
{ |
|
121 |
crc_reg = (crc_reg >> 8) ^ crc16_table[(crc_reg ^ *data++) & 0xff]; |
|
122 |
} |
|
123 |
return (~crc_reg) & 0xFFFF; |
|
124 |
} |
|
125 |
|
|
126 |
const uint16_t polynom = 0xA001; |
|
127 |
|
|
128 |
unsigned short crc16bitbybit(const unsigned char *ptr, unsigned int len) |
|
129 |
{ |
|
130 |
uint8_t i; |
|
131 |
uint16_t crc = 0xffff; |
|
132 |
|
|
133 |
if (len == 0) { |
|
134 |
len = 1; |
|
135 |
} |
|
136 |
while (len--) { |
|
137 |
crc ^= *ptr; |
|
138 |
for (i = 0; i<8; i++) |
|
139 |
{ |
|
140 |
if (crc & 1) { |
|
141 |
crc >>= 1; |
|
142 |
crc ^= polynom; |
|
143 |
} |
|
144 |
else { |
|
145 |
crc >>= 1; |
|
146 |
} |
|
147 |
} |
|
148 |
ptr++; |
|
149 |
} |
|
150 |
return(crc); |
|
151 |
} |
842bb6
|
152 |
*/ |
483170
|
153 |
/* Table of CRC values for high-order byte */ |
Q |
154 |
const uint8_t crctablehi[] = { |
|
155 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, |
|
156 |
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, |
|
157 |
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, |
|
158 |
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, |
|
159 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, |
|
160 |
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, |
|
161 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, |
|
162 |
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, |
|
163 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, |
|
164 |
0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, |
|
165 |
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, |
|
166 |
0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
|
167 |
0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, |
|
168 |
0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, |
|
169 |
0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, |
|
170 |
0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, |
|
171 |
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, |
|
172 |
0x40 |
|
173 |
}; |
|
174 |
/* Table of CRC values for low-order byte */ |
|
175 |
const uint8_t crctablelo[] = { |
|
176 |
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4, |
|
177 |
0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, |
|
178 |
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, |
|
179 |
0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, |
|
180 |
0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, |
|
181 |
0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, |
|
182 |
0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, |
|
183 |
0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, |
|
184 |
0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, |
|
185 |
0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, |
|
186 |
0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, |
|
187 |
0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, |
|
188 |
0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, |
|
189 |
0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, |
|
190 |
0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, |
|
191 |
0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, |
|
192 |
0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, |
|
193 |
0x40 |
|
194 |
}; |
7d8ba5
|
195 |
/* |
483170
|
196 |
uint16_t crc16table(const uint8_t *ptr, uint16_t len) |
Q |
197 |
{ |
|
198 |
uint8_t crchi = 0xff; |
|
199 |
uint8_t crclo = 0xff; |
|
200 |
uint16_t index; |
|
201 |
while (len--) |
|
202 |
{ |
|
203 |
index = crclo ^ *ptr++; |
|
204 |
crclo = crchi ^ crctablehi[index]; |
|
205 |
crchi = crctablelo[index]; |
|
206 |
} |
|
207 |
return (crchi << 8 | crclo); |
|
208 |
} |
7d8ba5
|
209 |
*/ |
Q |
210 |
const uint16_t crctalbeabs[] = { |
|
211 |
0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, |
|
212 |
0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400 |
|
213 |
}; |
|
214 |
|
|
215 |
uint16_t crc16tablefast(const uint8_t *ptr, uint16_t len) |
|
216 |
{ |
|
217 |
uint16_t crc = 0xffff; |
|
218 |
uint16_t i; |
|
219 |
uint8_t ch; |
|
220 |
|
|
221 |
for (i = 0; i < len; i++) { |
|
222 |
ch = *ptr++; |
|
223 |
crc = crctalbeabs[(ch ^ crc) & 15] ^ (crc >> 4); |
|
224 |
crc = crctalbeabs[((ch >> 4) ^ crc) & 15] ^ (crc >> 4); |
|
225 |
} |
|
226 |
|
|
227 |
return crc; |
|
228 |
} |
|
229 |
|
842bb6
|
230 |
/* |
483170
|
231 |
void modbuscrc16test() |
Q |
232 |
{ |
|
233 |
printf("\n"); |
|
234 |
printf(" Modbus CRC16 tester\n"); |
|
235 |
printf("-----------------------------------------------------------------------\n"); |
|
236 |
uint8_t crc16_data[] = { 0x01, 0x04, 0x04, 0x43, 0x6b, 0x58, 0x0e }; // expected crc value 0xD825. |
|
237 |
printf(" modbus crc16table test, expected value : 0xd825, calculate value : 0x%x\n", crc16table(crc16_data, sizeof(crc16_data))); |
|
238 |
// printf(" modbus crc16tablefast test, expected value : 0xd825, calculate value : 0x%x\n", crc16tablefast(crc16_data, sizeof(crc16_data))); |
|
239 |
printf(" modbus crc16bitbybit test, expected value : 0xd825, calculate value : 0x%x\n", crc16bitbybit(crc16_data, sizeof(crc16_data))); |
|
240 |
} |
842bb6
|
241 |
*/ |
483170
|
242 |
int InitUartstat(stUartStat * pUartstat,void * pBufRx, int nSizeRx, void * pBufTx, int nSizeTx) |
Q |
243 |
{ |
|
244 |
memset(pUartstat,sizeof(stUartStat),0); |
842bb6
|
245 |
if (pBufRx) initQueue(&pUartstat->QRx,pBufRx,nSizeRx); |
Q |
246 |
if (pBufTx) initQueue(&pUartstat->QTx,pBufTx,nSizeTx); |
483170
|
247 |
return 0; |
Q |
248 |
} |
|
249 |
|
|
250 |
int Uart1SendDMA(void * pData, int nSize) |
|
251 |
{ |
|
252 |
LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_2); |
|
253 |
LL_DMA_ConfigAddresses(DMA1,LL_DMA_CHANNEL_2, (uint32_t)pData, |
|
254 |
(uint32_t)&USART1->TDR, LL_DMA_DIRECTION_MEMORY_TO_PERIPH); |
|
255 |
LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_2,nSize); |
|
256 |
LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_2); |
|
257 |
Uart1Stat.DMASendLen=nSize; |
|
258 |
Uart1Stat.bSending=1; |
|
259 |
LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_2); |
|
260 |
LL_USART_EnableDMAReq_TX(USART1); |
|
261 |
return nSize; |
|
262 |
} |
|
263 |
|
|
264 |
int Uart1TriggerSendDMA() |
|
265 |
{ |
|
266 |
if (!Uart1Stat.bSending&&!IsEmpty(&Uart1Stat.QTx)) |
|
267 |
{ |
|
268 |
int len1=GetContinueData(&Uart1Stat.QTx); |
|
269 |
Uart1SendDMA(GetReadBuffer(&Uart1Stat.QTx),len1); |
|
270 |
} |
|
271 |
return 0; |
|
272 |
} |
|
273 |
|
|
274 |
void Uart2SetDE(void) |
|
275 |
{ |
|
276 |
LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_1); |
|
277 |
} |
|
278 |
void Uart2UnsetDE(void) |
|
279 |
{ |
|
280 |
LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_1); |
|
281 |
} |
|
282 |
void DelayUs(int nUs) |
|
283 |
{ |
|
284 |
for (int i=0;i<nUs;i++) |
|
285 |
for (volatile int j=0;j<24;j++) |
|
286 |
{ |
|
287 |
__nop(); |
|
288 |
} |
|
289 |
} |
005755
|
290 |
void Delay100nS(int n100nS) |
Q |
291 |
{ |
|
292 |
for (int i=0;i<n100nS;i++) |
|
293 |
for (volatile int j=0;j<1;j++) |
|
294 |
{ |
|
295 |
__nop(); |
|
296 |
} |
|
297 |
} |
483170
|
298 |
|
Q |
299 |
int Uart2RecvDMA(void * pBuf, int nSize) |
|
300 |
{ |
|
301 |
LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_5); |
|
302 |
LL_DMA_ConfigAddresses(DMA1,LL_DMA_CHANNEL_5, (uint32_t)&USART2->RDR, |
|
303 |
(uint32_t)pBuf, LL_DMA_DIRECTION_PERIPH_TO_MEMORY); |
|
304 |
LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_5,nSize); |
|
305 |
LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_5); |
|
306 |
Uart2Stat.DMARecvLen=nSize; |
|
307 |
Uart2Stat.bRecving=1; |
|
308 |
LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_5); |
|
309 |
LL_USART_EnableDMAReq_RX(USART2); |
|
310 |
return 0; |
|
311 |
} |
|
312 |
int Uart2SendDMA(void * pData, int nSize) |
|
313 |
{ |
|
314 |
#if (USART2_USE_HARDWARE_DE == 1) |
|
315 |
|
|
316 |
#else |
|
317 |
Uart2SetDE(); |
|
318 |
DelayUs(USART2_DE_ASSERTION_TIME); |
|
319 |
#endif |
|
320 |
LL_DMA_DisableChannel(DMA1,LL_DMA_CHANNEL_4); |
|
321 |
LL_DMA_ConfigAddresses(DMA1,LL_DMA_CHANNEL_4, (uint32_t)pData, |
|
322 |
(uint32_t)&USART2->TDR, LL_DMA_DIRECTION_MEMORY_TO_PERIPH); |
|
323 |
LL_DMA_SetDataLength(DMA1,LL_DMA_CHANNEL_4,nSize); |
|
324 |
LL_DMA_EnableChannel(DMA1,LL_DMA_CHANNEL_4); |
|
325 |
Uart2Stat.DMASendLen=nSize; |
|
326 |
Uart2Stat.bSending=1; |
|
327 |
LL_DMA_EnableIT_TC(DMA1,LL_DMA_CHANNEL_4); |
|
328 |
LL_USART_EnableDMAReq_TX(USART2); |
|
329 |
return nSize; |
|
330 |
} |
|
331 |
int Uart2TriggerSendDMA() |
|
332 |
{ |
|
333 |
if (!Uart2Stat.bSending&&!IsEmpty(&Uart2Stat.QTx)) |
|
334 |
{ |
|
335 |
int len1=GetContinueData(&Uart2Stat.QTx); |
|
336 |
Uart2SendDMA(GetReadBuffer(&Uart2Stat.QTx),len1); |
|
337 |
} |
|
338 |
return 0; |
|
339 |
} |
|
340 |
|
|
341 |
void TriggerPendSV() |
|
342 |
{ |
|
343 |
SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos; |
|
344 |
} |
|
345 |
|
|
346 |
void SPI1_IRQ_CallBack() |
|
347 |
{ |
|
348 |
uint8_t value; |
|
349 |
// LL_GPIO_TogglePin(GPIOB,LL_GPIO_PIN_7); |
|
350 |
if (LL_SPI_IsActiveFlag_RXNE(SPI1)) |
|
351 |
{ |
|
352 |
value = LL_SPI_ReceiveData8( SPI1); |
|
353 |
#if (BOARD_TYPE == 14) |
|
354 |
KMem.WDD[62]++; |
|
355 |
KMem.WDT[122]++; |
|
356 |
if (!bSPI1Sending) |
|
357 |
{ |
|
358 |
logData(value); |
|
359 |
} |
|
360 |
if (!bSPI1Sending && (1 || bSPI1Recving)) |
|
361 |
{ |
|
362 |
SPI1RecvBuf[nSPI1RecvPos]=value; |
|
363 |
nSPI1RecvPos++; |
|
364 |
|
|
365 |
if (value==0x0d) |
|
366 |
{ |
|
367 |
logData(nSPI1RecvPos); |
|
368 |
|
|
369 |
nSPI1RecvLenInBuf=nSPI1RecvPos; |
|
370 |
bSPI1RecvDone=1; |
|
371 |
nSPI1RecvPos=0; |
|
372 |
bSPI1Recving=0; |
|
373 |
|
|
374 |
|
|
375 |
TriggerPendSV(); |
|
376 |
} |
|
377 |
} |
|
378 |
if (bSPI1Sending) |
|
379 |
{ |
|
380 |
nSPI1SentLen++; |
|
381 |
KMem.SDD[63]++; |
|
382 |
|
|
383 |
if (nSPI1SentLen >= nSPI1ToSendLen) { |
|
384 |
SetACKPin_1(); |
|
385 |
bSPI1Sending=0; |
|
386 |
bSPI1SendDone=1; |
|
387 |
bSPI1Recving=1; |
|
388 |
nSPI1RecvPos=0; |
5dd1b7
|
389 |
SetFPxDEPin_0(); |
483170
|
390 |
} |
Q |
391 |
else { |
|
392 |
value = SPI1SendBuf[nSPI1SentLen]; |
|
393 |
LL_SPI_TransmitData8(SPI1,value); |
|
394 |
logData(value); |
|
395 |
} |
|
396 |
} |
842bb6
|
397 |
#else |
Q |
398 |
UNUSED(value); |
|
399 |
#endif |
483170
|
400 |
} |
Q |
401 |
} |
|
402 |
|
|
403 |
void Uart1SendDone() |
|
404 |
{ |
|
405 |
Uart1Stat.TcCount++; |
|
406 |
return; |
|
407 |
} |
|
408 |
|
|
409 |
void Uart1RecvDone() |
|
410 |
{ |
|
411 |
Uart1Stat.IdelCount++; |
|
412 |
// NVIC_SetPendingIRQ(PendSV_IRQn); |
|
413 |
// SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos; |
|
414 |
|
7eb19e
|
415 |
if (Uart1RxBuf1DataLen >0) |
483170
|
416 |
{ |
Q |
417 |
Uart1Stat.bPacketRecved=1; |
|
418 |
// SCB->ICSR=SCB_ICSR_PENDSVSET_Msk; //1<<SCB_ICSR_PENDSVSET_Pos; |
|
419 |
// KLParsePacket(Uart1RecvBuf1,Uart1RecvBuf1DataLen); |
|
420 |
// Uart1RecvBuf1DataLen=0; |
7d8ba5
|
421 |
TriggerPendSV(); |
483170
|
422 |
} |
Q |
423 |
} |
|
424 |
|
|
425 |
void Uart2SendDone() |
|
426 |
{ |
|
427 |
Uart2Stat.TcCount++; |
|
428 |
#if (USART2_USE_HARDWARE_DE == 1) |
|
429 |
#else |
|
430 |
Uart2UnsetDE(); |
|
431 |
#endif |
842bb6
|
432 |
Uart2Stat.bSendDone = 1; |
Q |
433 |
TriggerPendSV(); |
483170
|
434 |
} |
Q |
435 |
void Uart2RecvDone() |
|
436 |
{ |
7eb19e
|
437 |
Uart2RxBuf1DataLen=sizeof(Uart2RxBuf1) - LL_DMA_GetDataLength(DMA1,LL_DMA_CHANNEL_5); |
483170
|
438 |
Uart2Stat.bPacketRecved=1; |
Q |
439 |
Uart2Stat.IdelCount++; |
7eb19e
|
440 |
if (Uart2RxBuf1DataLen>0) |
483170
|
441 |
TriggerPendSV(); |
Q |
442 |
// ParsePacket((pKBPacket)Uart2RecvBuf1,Uart2RecvBuf1DataLen); |
|
443 |
} |
|
444 |
|
|
445 |
int PutStr(char * str1, int len1) |
|
446 |
{ |
|
447 |
// Uart1SendDMA(str1,len1); |
|
448 |
PushIn(&Uart1Stat.QTx,str1,len1); |
|
449 |
// LL_USART_EnableIT_TXE(USART1); |
|
450 |
// LL_USART_EnableIT_TC(USART1); |
|
451 |
Uart1TriggerSendDMA(); |
|
452 |
return len1; |
|
453 |
} |
|
454 |
int PutStr1(char * str1, int len1) |
|
455 |
{ |
|
456 |
// Uart1SendDMA(str1,len1); |
|
457 |
PushIn(&Uart1Stat.QTx,str1,len1); |
|
458 |
// LL_USART_EnableIT_TXE(USART1); |
|
459 |
// LL_USART_EnableIT_TC(USART1); |
|
460 |
Uart1TriggerSendDMA(); |
5dd1b7
|
461 |
Uart1Stat.SentPacket++; |
483170
|
462 |
return len1; |
Q |
463 |
} |
|
464 |
int PutStr2(char * str1, int len1) |
|
465 |
{ |
|
466 |
Uart2SendDMA(str1,len1); |
|
467 |
// PushIn(&Uart2Stat.QTx,str1,len1); |
|
468 |
// LL_USART_EnableIT_TXE(USART1); |
|
469 |
// LL_USART_EnableIT_TC(USART1); |
|
470 |
// Uart2TriggerSendDMA(); |
5dd1b7
|
471 |
Uart2Stat.SentPacket++; |
483170
|
472 |
return len1; |
Q |
473 |
} |
|
474 |
|
|
475 |
int SendPacket(int nChn, void * pBuf,int len1) |
|
476 |
{ |
|
477 |
if (nChn==1) { |
7eb19e
|
478 |
Uart1SendDMA(pBuf, len1); |
Q |
479 |
// PutStr1((char *)pBuf,len1); |
483170
|
480 |
// PushIn(&Uart1Stat.QTx,p1,len1); |
Q |
481 |
// Uart1TriggerSendDMA(); |
5dd1b7
|
482 |
|
483170
|
483 |
}else if (nChn==2){ |
Q |
484 |
PutStr2((char *)pBuf,len1); |
|
485 |
// PushIn(&Uart2Stat.QTx,p1,len1); |
|
486 |
// Uart2TriggerSendDMA(); |
5dd1b7
|
487 |
|
483170
|
488 |
} |
Q |
489 |
return len1; |
|
490 |
} |
|
491 |
/* |
|
492 |
int SendPacket1(void * pBuf,int len1) |
|
493 |
{ |
|
494 |
PutStr1((char *)pBuf,len1); |
|
495 |
// PushIn(&Uart1Stat.QTx,p1,len1); |
|
496 |
// Uart1TriggerSendDMA(); |
|
497 |
Uart1Stat.SentPacket++; |
|
498 |
return len1; |
|
499 |
} |
|
500 |
int SendPacket2(pKBPacket p1,int len1) |
|
501 |
{ |
|
502 |
PutStr2((char *)p1,len1); |
|
503 |
// PushIn(&Uart2Stat.QTx,p1,len1); |
|
504 |
// Uart2TriggerSendDMA(); |
|
505 |
Uart2Stat.SentPacket++; |
|
506 |
return len1; |
|
507 |
} |
|
508 |
*/ |
|
509 |
void ToggleRunLed() { LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_13);} |
|
510 |
void ToggleErrLed() { LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_14);} |
|
511 |
void ToggleErr2Led() { LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);} |
|
512 |
|
|
513 |
#if (BOARD_TYPE == 14) |
|
514 |
void ToggleOutStat() { LL_GPIO_TogglePin(GPIOC,LL_GPIO_PIN_15);} |
|
515 |
|
|
516 |
void SetOutStat(uchar bOn) |
|
517 |
{ |
|
518 |
if (bOn) {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_15);} |
|
519 |
else {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_15);} |
|
520 |
} |
|
521 |
#else |
9aed5d
|
522 |
void ToggleOutStat() { } //LL_GPIO_TogglePin(GPIOA,LL_GPIO_PIN_11);} |
483170
|
523 |
|
Q |
524 |
void SetOutStat(uchar bOn) |
|
525 |
{ |
9aed5d
|
526 |
// if (bOn) {LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_11);} |
Q |
527 |
// else {LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_11);} |
483170
|
528 |
} |
Q |
529 |
#endif |
|
530 |
|
|
531 |
void SetRunLed(uchar bOn) |
|
532 |
{ |
|
533 |
if (bOn) {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_13);} |
|
534 |
else {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_13);} |
|
535 |
} |
|
536 |
void SetErrLed(uchar bOn) |
|
537 |
{ |
|
538 |
if (bOn) {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_14);} |
|
539 |
else {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_14);} |
|
540 |
} |
|
541 |
void SetErr2Led(uchar bOn) |
|
542 |
{ |
|
543 |
if (bOn) {LL_GPIO_ResetOutputPin(GPIOC,LL_GPIO_PIN_15);} |
|
544 |
else {LL_GPIO_SetOutputPin(GPIOC,LL_GPIO_PIN_15);} |
|
545 |
} |
|
546 |
/* |
|
547 |
void SetLeds(uchar bRun, uchar bErr) |
|
548 |
{ |
|
549 |
SetRunLed(bRun); SetErrLed (bErr); |
|
550 |
} |
|
551 |
*/ |
|
552 |
#define set165SL_0() LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_4) |
|
553 |
#define set165SL_1() LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_4) |
|
554 |
#define set165CLK_0() LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_5) |
|
555 |
#define set165CLK_1() LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_5) |
|
556 |
#define Get165SER() LL_GPIO_IsInputPinSet(GPIOA,LL_GPIO_PIN_6) |
|
557 |
#define GetCfg165SER() LL_GPIO_IsInputPinSet(GPIOA,LL_GPIO_PIN_7) |
|
558 |
|
|
559 |
unsigned int Input165_8() |
|
560 |
{ |
|
561 |
int x1=0; |
|
562 |
__disable_irq(); |
|
563 |
set165SL_0(); set165SL_1(); |
|
564 |
unsigned int mask1=0x0080; |
|
565 |
volatile uint32_t * p1=&GPIOA->IDR; |
|
566 |
for (uint32_t i=8;i;i--) |
|
567 |
{ |
|
568 |
//if (Get165SER()) {} |
|
569 |
//else x1|=mask1; |
|
570 |
if (*p1&LL_GPIO_PIN_6) {} |
|
571 |
else x1|=mask1; |
|
572 |
//x1<<=1; |
|
573 |
//x1|=!Get165SER(); |
|
574 |
set165CLK_0(); |
|
575 |
mask1>>=1; |
|
576 |
set165CLK_1(); |
|
577 |
} |
|
578 |
__enable_irq(); |
|
579 |
return x1; |
|
580 |
} |
|
581 |
|
|
582 |
unsigned int Input165(int nBit) |
|
583 |
{ |
|
584 |
int x1=0; |
|
585 |
__disable_irq(); |
|
586 |
set165SL_0(); set165SL_1(); |
|
587 |
unsigned int mask1=1<<(nBit-1); |
|
588 |
volatile uint32_t * p1=&GPIOA->IDR; |
|
589 |
for (uint32_t i=nBit;i;i--) |
|
590 |
{ |
|
591 |
//if (Get165SER()) {} |
|
592 |
//else x1|=mask1; |
|
593 |
if (*p1&LL_GPIO_PIN_6) {} |
|
594 |
else x1|=mask1; |
|
595 |
//x1<<=1; |
|
596 |
//x1|=!Get165SER(); |
|
597 |
set165CLK_0(); |
|
598 |
mask1>>=1; |
|
599 |
set165CLK_1(); |
|
600 |
} |
|
601 |
__enable_irq(); |
|
602 |
return x1; |
|
603 |
} |
|
604 |
unsigned int Input165_R(int nBit) |
|
605 |
{ |
|
606 |
__disable_irq(); |
|
607 |
set165SL_0(); set165SL_1(); |
|
608 |
int nBytes = nBit /8; |
|
609 |
volatile uint32_t * p1=&GPIOA->IDR; |
|
610 |
union stdata |
|
611 |
{ |
|
612 |
int intvalue; |
|
613 |
unsigned char Bytes[4]; |
|
614 |
}rdata; |
|
615 |
rdata.intvalue=0; |
|
616 |
for (int i=0;i<nBytes;i++) |
|
617 |
{ |
|
618 |
int x1=0; |
|
619 |
unsigned int mask1=1<<(7); |
|
620 |
for (uint32_t i=8;i;i--) |
|
621 |
{ |
|
622 |
//if (Get165SER()) {} |
|
623 |
//else x1|=mask1; |
|
624 |
if (*p1&LL_GPIO_PIN_6) {} |
|
625 |
else x1|=mask1; |
|
626 |
//x1<<=1; |
|
627 |
//x1|=!Get165SER(); |
|
628 |
set165CLK_0(); |
|
629 |
mask1>>=1; |
|
630 |
set165CLK_1(); |
|
631 |
} |
|
632 |
rdata.Bytes[i]=x1; |
|
633 |
} |
|
634 |
__enable_irq(); |
|
635 |
return rdata.intvalue; |
|
636 |
} |
|
637 |
|
|
638 |
unsigned int Input165Cfg(int nBit) |
|
639 |
{ |
|
640 |
int x1=0; |
|
641 |
__disable_irq(); |
|
642 |
set165SL_0(); set165SL_1(); |
|
643 |
unsigned int mask1=1<<(nBit-1); |
|
644 |
volatile uint32_t * p1=&GPIOA->IDR; |
|
645 |
for (uint32_t i=nBit;i;i--) |
|
646 |
{ |
|
647 |
//if (Get165SER()) {} |
|
648 |
//else x1|=mask1; |
|
649 |
if (*p1&LL_GPIO_PIN_7) {} |
|
650 |
else x1|=mask1; |
|
651 |
//x1<<=1; |
|
652 |
//x1|=!Get165SER(); |
|
653 |
set165CLK_0(); |
|
654 |
mask1>>=1; |
|
655 |
set165CLK_1(); |
|
656 |
} |
|
657 |
__enable_irq(); |
|
658 |
return x1; |
|
659 |
} |
|
660 |
|
|
661 |
unsigned int GetInput() |
|
662 |
{ |
|
663 |
int BoradType=GetBoardType(); |
|
664 |
switch (BoradType) |
|
665 |
{ |
|
666 |
|
|
667 |
case 1: |
|
668 |
return Input165(8); |
|
669 |
case 2: |
|
670 |
return Input165(8); |
|
671 |
case 3: |
|
672 |
return Input165(16); |
|
673 |
case 4: |
|
674 |
return Input165(8); |
|
675 |
case 5: |
|
676 |
case 7: |
|
677 |
return Input165(16); |
|
678 |
case 6: |
|
679 |
case 8: |
|
680 |
return Input165(8); |
|
681 |
case 9: |
|
682 |
return Input165_R(16); |
|
683 |
case 10: |
|
684 |
return Input165_R(8); |
|
685 |
case 11: |
|
686 |
return Input165_R(8); |
|
687 |
case 13: |
|
688 |
return Input165_R(16); |
|
689 |
case 14: |
5dd1b7
|
690 |
return 0; //FPx |
483170
|
691 |
case 15: |
Q |
692 |
return Input165_R(16); |
|
693 |
case 16: |
|
694 |
return Input165_R(8); |
|
695 |
|
|
696 |
default: |
|
697 |
break; |
|
698 |
} |
|
699 |
return 0; |
|
700 |
|
|
701 |
} |
|
702 |
int ReadConfig_0() |
|
703 |
{ |
|
704 |
return 0; |
|
705 |
} |
|
706 |
|
|
707 |
int ReadConfig_1() |
|
708 |
{ |
|
709 |
// uchar x = LL_GPIO_ReadInputPort(GPIOB); |
|
710 |
// uchar x1 = LL_GPIO_IsInputPinSet(GPIOB,LL_GPIO_PIN_10); |
|
711 |
// uchar x2=(x&0x7) | (x1 << 3 ) ; |
|
712 |
uchar x2=0; |
|
713 |
x2=(GPIOB->IDR&0x7) | ((GPIOB->IDR &0x400)>>7); |
|
714 |
x2= (~x2)&0xf; |
|
715 |
return x2; |
|
716 |
} |
|
717 |
|
|
718 |
int ReadConfig_2() |
|
719 |
{ |
|
720 |
uchar x2=0; |
|
721 |
x2=(GPIOB->IDR&0x7) | ((GPIOB->IDR &0x400)>>7); |
|
722 |
x2= (~x2)&0xf; |
|
723 |
return x2; |
|
724 |
} |
|
725 |
|
|
726 |
int ReadConfig_3() |
|
727 |
{ |
|
728 |
uchar x2=0; |
|
729 |
x2 = Input165(24)&0xff; |
|
730 |
return x2; |
|
731 |
|
|
732 |
} |
|
733 |
int ReadConfig_4() |
|
734 |
{ |
|
735 |
|
|
736 |
uchar x2=0; |
|
737 |
x2 = Input165(16)&0xff; |
|
738 |
return x2; |
|
739 |
} |
|
740 |
|
|
741 |
int ReadConfig_5() |
|
742 |
{ |
|
743 |
uchar x2=0; |
|
744 |
x2 = Input165Cfg(8)&0xff; |
|
745 |
return x2; |
|
746 |
} |
|
747 |
|
|
748 |
int ReadConfig_11() |
|
749 |
{ |
|
750 |
uchar x2=0; |
|
751 |
x2=((GPIOB->IDR&0x2)<<2); |
|
752 |
x2= ((~x2)&0x8)|1; |
|
753 |
return x2; |
|
754 |
} |
|
755 |
|
|
756 |
int ReadJumperSW() |
|
757 |
{ |
|
758 |
int BoradType=GetBoardType(); |
|
759 |
switch (BoradType) |
|
760 |
{ |
|
761 |
case 0: |
|
762 |
return ReadConfig_0(); |
|
763 |
case 1: |
|
764 |
return ReadConfig_1(); //old board 4 in 4 o |
|
765 |
case 2: |
|
766 |
return ReadConfig_2(); //old board 8 in 8 o |
|
767 |
case 3: |
|
768 |
return ReadConfig_3(); //Master 16 in16 o |
|
769 |
case 4: |
|
770 |
return ReadConfig_4(); //Slave 8 in 8 o |
|
771 |
case 5: |
|
772 |
case 7: |
|
773 |
return ReadConfig_5(); // New Master 16 in16 o |
|
774 |
case 6: |
|
775 |
case 8: |
|
776 |
return ReadConfig_5(); //New Slave 8 in 8 o |
|
777 |
case 9: |
|
778 |
case 10: |
|
779 |
return ReadConfig_5(); //New Master Slave 8 in 8 o |
|
780 |
case 11: |
|
781 |
return ReadConfig_11(); //Mini Board |
|
782 |
case 13: |
|
783 |
return ReadConfig_5(); |
|
784 |
case 14: |
5dd1b7
|
785 |
return (~(LL_GPIO_ReadInputPort(GPIOA)>>4))&0x0f; //FPx |
483170
|
786 |
case 15: |
Q |
787 |
case 16: |
|
788 |
return ReadConfig_5(); //Wireless Master Slave 8 in 8 o |
|
789 |
default: |
|
790 |
|
|
791 |
return 0; |
|
792 |
} |
|
793 |
} |
|
794 |
|
|
795 |
#define SRCLK2_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_13) |
|
796 |
#define SRCLK2_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_13) |
|
797 |
#define STRCLK2_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_12) |
|
798 |
#define STRCLK2_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_12) |
|
799 |
#define SER2_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_15) |
|
800 |
#define SER2_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_15) |
|
801 |
|
|
802 |
void Enable595(uchar bEnable) |
|
803 |
{ |
|
804 |
if (bEnable) {LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_8);} |
|
805 |
else {LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_8);} |
|
806 |
} |
|
807 |
|
|
808 |
void Output595_8(unsigned int cc) |
|
809 |
{ |
|
810 |
//unsigned char i; |
|
811 |
;// 74HC595输出程序,输出8位 |
|
812 |
// cc=~0x3f; |
|
813 |
__disable_irq(); |
|
814 |
STRCLK2_1(); |
|
815 |
unsigned int mask1=0x0080; |
|
816 |
//volatile uint32_t * p1 = &GPIOB->BRR; |
|
817 |
//volatile uint32_t * p2 = &GPIOB->BSRR; |
|
818 |
for (;mask1;) |
|
819 |
{ |
|
820 |
SRCLK2_0(); |
|
821 |
//*p1=LL_GPIO_PIN_13; |
|
822 |
if (cc&mask1) {SER2_1();} |
|
823 |
else {SER2_0();} |
|
824 |
mask1>>=1; |
|
825 |
SRCLK2_1(); |
|
826 |
// __nop(); |
|
827 |
//*p2=LL_GPIO_PIN_13; |
|
828 |
} |
|
829 |
STRCLK2_0(); |
|
830 |
STRCLK2_1(); |
|
831 |
__enable_irq(); |
|
832 |
} |
|
833 |
|
|
834 |
void Output595_16(unsigned int cc) |
|
835 |
{ |
|
836 |
//unsigned char i; |
|
837 |
;// 74HC595输出程序,输出16位 |
|
838 |
// cc=~0x3f; |
|
839 |
__disable_irq(); |
|
840 |
STRCLK2_1(); |
|
841 |
unsigned int mask1=0x8000; |
|
842 |
//volatile uint32_t * p1 = &GPIOB->BRR; |
|
843 |
//volatile uint32_t * p2 = &GPIOB->BSRR; |
|
844 |
for (;mask1;) |
|
845 |
{ |
|
846 |
SRCLK2_0(); |
|
847 |
//*p1=LL_GPIO_PIN_13; |
|
848 |
if (cc&mask1) {SER2_1();} |
|
849 |
else {SER2_0();} |
|
850 |
mask1>>=1; |
|
851 |
SRCLK2_1(); |
|
852 |
// __nop(); |
|
853 |
//*p2=LL_GPIO_PIN_13; |
|
854 |
} |
|
855 |
STRCLK2_0(); |
|
856 |
STRCLK2_1(); |
|
857 |
__enable_irq(); |
|
858 |
} |
|
859 |
|
|
860 |
/* |
|
861 |
#define STRCLK12_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_7) |
|
862 |
#define STRCLK12_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_7) |
|
863 |
|
|
864 |
void PutOutputSPI1(unsigned int Y) |
|
865 |
{ |
|
866 |
__disable_irq(); |
|
867 |
STRCLK12_1(); |
|
868 |
LL_SPI_TransmitData8(SPI1,Y>>8); |
|
869 |
int i=0; |
|
870 |
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) |
|
871 |
{ |
|
872 |
} |
|
873 |
KMem.SDD[28]=i; |
|
874 |
i=0; |
|
875 |
while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) |
|
876 |
{ |
|
877 |
i++; |
|
878 |
} |
|
879 |
LL_SPI_TransmitData8(SPI1,Y); |
|
880 |
while (LL_SPI_IsActiveFlag_TXE(SPI1) == RESET) |
|
881 |
{ |
|
882 |
} |
|
883 |
KMem.SDD[28]=i; |
|
884 |
i=0; |
|
885 |
while (LL_SPI_IsActiveFlag_BSY(SPI1) == SET) |
|
886 |
{ |
|
887 |
i++; |
|
888 |
} |
|
889 |
KMem.SDD[30]=i; |
|
890 |
STRCLK12_0(); |
|
891 |
__nop(); |
|
892 |
STRCLK12_1(); |
|
893 |
__enable_irq(); |
|
894 |
} |
|
895 |
*/ |
|
896 |
|
|
897 |
void PutOutputSPI2(unsigned int Y) |
|
898 |
{ |
|
899 |
__disable_irq(); |
|
900 |
STRCLK2_1(); |
|
901 |
LL_SPI_TransmitData8(SPI2,Y>>8); |
9aed5d
|
902 |
|
Q |
903 |
while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET) { } |
|
904 |
|
|
905 |
while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET) { } |
483170
|
906 |
LL_SPI_TransmitData8(SPI2,Y); |
9aed5d
|
907 |
while (LL_SPI_IsActiveFlag_TXE(SPI2) == RESET) { } |
Q |
908 |
|
|
909 |
while (LL_SPI_IsActiveFlag_BSY(SPI2) == SET) { } |
|
910 |
|
483170
|
911 |
STRCLK2_0(); |
Q |
912 |
STRCLK2_1(); |
|
913 |
__enable_irq(); |
|
914 |
} |
|
915 |
|
|
916 |
void PutOutput(unsigned int Y) |
|
917 |
{ |
|
918 |
#if (BOARD_TYPE == 14) |
|
919 |
return ; |
|
920 |
#else |
|
921 |
PutOutputSPI2(Y); |
|
922 |
//Output595_16(Y); |
|
923 |
#endif |
|
924 |
} |
|
925 |
|
|
926 |
#if (BOARD_TYPE == 9 || BOARD_TYPE == 10 || BOARD_TYPE == 15 || BOARD_TYPE == 16) |
|
927 |
//#pragma message("9,10") |
|
928 |
// V4.2 管脚排列向右移动了一位。 |
|
929 |
#define SRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1) |
|
930 |
#define SRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_1) |
|
931 |
#define STRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_2) |
|
932 |
#define STRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_2) |
|
933 |
#define OE1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_10) |
|
934 |
#define OE1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_10) |
|
935 |
#define SER1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_11) |
|
936 |
#define SER1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_11) |
|
937 |
#else //按照原来的管脚排列 |
|
938 |
#define SRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_0) |
|
939 |
#define SRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_0) |
|
940 |
#define STRCLK1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_1) |
|
941 |
#define STRCLK1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_1) |
|
942 |
#define OE1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_2) |
|
943 |
#define OE1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_2) |
|
944 |
#define SER1_0() LL_GPIO_ResetOutputPin(GPIOB,LL_GPIO_PIN_10) |
|
945 |
#define SER1_1() LL_GPIO_SetOutputPin(GPIOB,LL_GPIO_PIN_10) |
|
946 |
#endif |
|
947 |
|
|
948 |
|
|
949 |
void EnableDisIn(uchar bEnable) |
|
950 |
{ |
|
951 |
if (bEnable) {OE1_0();} |
|
952 |
else {OE1_1();} |
|
953 |
} |
|
954 |
|
|
955 |
void displayInput(unsigned int cc) |
|
956 |
{ |
|
957 |
//unsigned char i; |
|
958 |
;// 74HC595输出程序,输出8位 |
|
959 |
// cc=~0x3f; |
|
960 |
__disable_irq(); |
|
961 |
STRCLK1_1(); |
|
962 |
unsigned int mask1=0x8000; |
|
963 |
//volatile uint32_t * p1 = &GPIOB->BRR; |
|
964 |
//volatile uint32_t * p2 = &GPIOB->BSRR;````` |
|
965 |
for (;mask1;) |
|
966 |
{ |
|
967 |
SRCLK1_0(); |
|
968 |
//*p1=LL_GPIO_PIN_13; |
|
969 |
if (cc&mask1) {SER1_1();} |
|
970 |
else {SER1_0();} |
|
971 |
mask1>>=1; |
|
972 |
SRCLK1_1(); |
|
973 |
// __nop(); |
|
974 |
//*p2=LL_GPIO_PIN_13; |
|
975 |
} |
|
976 |
STRCLK1_0(); |
|
977 |
STRCLK1_1(); |
|
978 |
__enable_irq(); |
|
979 |
} |
005755
|
980 |
|
Q |
981 |
|
|
982 |
#define W25X_WriteEnable 0x06 |
|
983 |
#define W25X_WriteDisable 0x04 |
|
984 |
#define W25X_ReadStatusReg 0x05 |
|
985 |
#define W25X_ReadStatusReg2 0x35 |
|
986 |
#define W25X_WriteStatusReg 0x01 |
|
987 |
#define W25X_ReadData 0x03 |
|
988 |
#define W25X_FastReadData 0x0B |
|
989 |
#define W25X_FastReadDual 0x3B |
|
990 |
#define W25X_PageProgram 0x02 |
|
991 |
#define W25X_BlockErase 0xD8 |
|
992 |
#define W25X_SectorErase 0x20 |
|
993 |
#define W25X_ChipErase 0xC7 |
|
994 |
#define W25X_PowerDown 0xB9 |
|
995 |
#define W25X_ReleasePowerDown 0xAB |
|
996 |
#define W25X_DeviceID 0xAB |
|
997 |
#define W25X_ManufactDeviceID 0x90 |
|
998 |
#define W25X_JedecDeviceID 0x9F |
|
999 |
#define W25X_ReadUniqueID 0x4B |
|
1000 |
|
|
1001 |
|
|
1002 |
//W25X??/Q?????? |
|
1003 |
//W25Q80 ID 0XEF13 |
|
1004 |
//W25Q16 ID 0XEF14 |
|
1005 |
//W25Q32 ID 0XEF15 |
|
1006 |
//W25Q32 ID 0XEF16 |
9aed5d
|
1007 |
|
Q |
1008 |
#define W25Q80 0XEF13 |
|
1009 |
#define W25Q16 0XEF14 |
|
1010 |
#define W25Q32 0XEF15 |
|
1011 |
#define W25Q64 0XEF16 |
|
1012 |
#define W25Q128 0xEF17 |
005755
|
1013 |
//????? |
9aed5d
|
1014 |
#define SPI1_FLASH_CS PAout[15] //??FLASH,???????PB12? |
Q |
1015 |
#define SPI1_CS_EN LL_GPIO_ResetOutputPin(GPIOA,LL_GPIO_PIN_15) |
|
1016 |
#define SPI1_CS_NA LL_GPIO_SetOutputPin(GPIOA,LL_GPIO_PIN_15) |
005755
|
1017 |
|
Q |
1018 |
|
|
1019 |
|
9aed5d
|
1020 |
uint8_t SPI_Tranceive8(SPI_TypeDef * SPIx, uint8_t Y) |
005755
|
1021 |
{ |
Q |
1022 |
int i=0; |
9aed5d
|
1023 |
while (LL_SPI_IsActiveFlag_TXE(SPIx) == RESET){ DelayUs(1);i++; if (i>3200) break; } |
Q |
1024 |
LL_SPI_TransmitData8(SPIx,Y); |
005755
|
1025 |
i=0; |
9aed5d
|
1026 |
while (LL_SPI_IsActiveFlag_BSY(SPIx) == SET) { DelayUs(1);i++; if (i>3200) break;} |
005755
|
1027 |
i=0; |
9aed5d
|
1028 |
while (LL_SPI_IsActiveFlag_RXNE(SPIx) == RESET) { DelayUs(1);i++; if (i>3200) break;} |
Q |
1029 |
Y = LL_SPI_ReceiveData8(SPIx); |
005755
|
1030 |
return Y; |
Q |
1031 |
} |
|
1032 |
|
9aed5d
|
1033 |
uint8_t SPI_Transmit(SPI_TypeDef * SPIx, uint8_t * tData, uint8_t nLen, uint8_t timeout) |
005755
|
1034 |
{ |
9aed5d
|
1035 |
int nToSend=nLen; |
Q |
1036 |
while(nLen > 0) |
|
1037 |
{ |
|
1038 |
if (LL_SPI_IsActiveFlag_TXE(SPI1) && nToSend>0) { |
|
1039 |
LL_SPI_TransmitData8(SPI1,*tData++); nToSend--; |
|
1040 |
} |
|
1041 |
if (LL_SPI_IsActiveFlag_RXNE(SPI1)) { |
|
1042 |
LL_SPI_ReceiveData8(SPI1); |
|
1043 |
nLen--; |
|
1044 |
} |
|
1045 |
} |
|
1046 |
|
|
1047 |
return 0; |
005755
|
1048 |
for (int i=0;i<nLen;i++) { |
9aed5d
|
1049 |
SPI_Tranceive8(SPIx, tData[i]); |
005755
|
1050 |
} |
Q |
1051 |
return 0; |
|
1052 |
} |
|
1053 |
|
9aed5d
|
1054 |
uint8_t SPI_TransmitReceive(SPI_TypeDef * SPIx, uint8_t * tData, uint8_t * rData, uint8_t nLen, uint8_t timeout) |
005755
|
1055 |
{ |
Q |
1056 |
for (int i=0;i<nLen;i++) { |
9aed5d
|
1057 |
// rData[i] = SPI_Tranceive8(SPIx, tData[i]); |
Q |
1058 |
__IO uint32_t * pSR = &(SPI1->SR); |
|
1059 |
int j=0; |
|
1060 |
while ((* pSR & SPI_SR_TXE) == RESET){ } |
|
1061 |
LL_SPI_TransmitData8(SPIx,tData[i]); |
|
1062 |
j=0; |
|
1063 |
// while (LL_SPI_IsActiveFlag_BSY(SPIx) == SET) { DelayUs(1);j++; if (j>3200) break;} |
|
1064 |
// i=0; |
|
1065 |
while (LL_SPI_IsActiveFlag_RXNE(SPIx) == RESET) { } |
|
1066 |
rData[i] = LL_SPI_ReceiveData8(SPIx); |
005755
|
1067 |
} |
Q |
1068 |
return 0; |
|
1069 |
} |
|
1070 |
|
9aed5d
|
1071 |
uint8_t SPI1_Tranceive(uint8_t * tData, uint8_t * rData, uint8_t nLen) |
Q |
1072 |
{ |
|
1073 |
register int nToSend=nLen; |
|
1074 |
// /* |
|
1075 |
__IO uint32_t * pSR = &(SPI1->SR); |
|
1076 |
__IO uint8_t * pDR = (__IO uint8_t *)&(SPI1->DR); |
|
1077 |
__disable_irq(); |
|
1078 |
while(nLen >0) |
|
1079 |
{ |
|
1080 |
if (*pSR & SPI_SR_TXE && nToSend>0) { |
|
1081 |
LL_SPI_TransmitData8(SPI1,*tData++); nToSend--; |
|
1082 |
} |
|
1083 |
if (*pSR & SPI_SR_RXNE) { |
|
1084 |
*rData++ = * pDR; |
|
1085 |
nLen--; // if (nLen == 0) break; |
|
1086 |
} |
|
1087 |
} |
|
1088 |
|
|
1089 |
// */ |
|
1090 |
/* |
|
1091 |
while(nLen > 0) |
|
1092 |
{ |
|
1093 |
if (LL_SPI_IsActiveFlag_TXE(SPI1) && nToSend>0) { |
|
1094 |
LL_SPI_TransmitData8(SPI1,*tData++); nToSend--; |
|
1095 |
} |
|
1096 |
if (LL_SPI_IsActiveFlag_RXNE(SPI1)) { |
|
1097 |
*rData++ = LL_SPI_ReceiveData8(SPI1); |
|
1098 |
nLen--; // if (nLen == 0) break; |
|
1099 |
} |
|
1100 |
} |
|
1101 |
// */ |
|
1102 |
__enable_irq(); |
|
1103 |
return nLen; |
|
1104 |
} |
005755
|
1105 |
|
9aed5d
|
1106 |
|
Q |
1107 |
uint8_t SPI1_Flash_ReadSR(void) |
005755
|
1108 |
{ |
Q |
1109 |
uint8_t Data1[2]= {W25X_ReadStatusReg,0x00}; |
|
1110 |
uint8_t Rxdata[2]; |
|
1111 |
uint8_t byte=0; |
9aed5d
|
1112 |
SPI1_CS_EN; |
Q |
1113 |
SPI1_Tranceive(Data1,Rxdata,2); |
|
1114 |
SPI1_CS_NA; |
005755
|
1115 |
byte=Rxdata[1]; |
Q |
1116 |
return byte; |
|
1117 |
} |
|
1118 |
void SPI_Flash_Wait_Busy(void) |
|
1119 |
{ |
9aed5d
|
1120 |
while((SPI1_Flash_ReadSR()&0x01)==0x01); // ??BUSY??? |
005755
|
1121 |
} |
Q |
1122 |
|
|
1123 |
void SPI_FLASH_Write_SR(uint8_t sr) |
|
1124 |
{ |
|
1125 |
uint8_t Data1[2]= {W25X_ReadStatusReg,0x00}; |
|
1126 |
Data1[1]=sr; |
9aed5d
|
1127 |
SPI1_CS_EN; |
Q |
1128 |
SPI_Transmit(SPI1, Data1, 2,100); |
|
1129 |
SPI1_CS_NA; |
005755
|
1130 |
} |
Q |
1131 |
|
|
1132 |
uint16_t SPI_Flash_ReadID(void) |
|
1133 |
{ |
|
1134 |
uint16_t Temp = 0; |
9aed5d
|
1135 |
uint8_t Data1[6] = {W25X_ManufactDeviceID,0x00,0x00,0x00,0x00,0x00}; |
Q |
1136 |
uint8_t Data2[6]= {0x00,0x00}; |
|
1137 |
// uint8_t Rxdata[2]; |
|
1138 |
SPI1_CS_EN; |
|
1139 |
//SPI_Transmit(SPI1, Data1,4,100); |
|
1140 |
// SPI_TransmitReceive(SPI1,Data1,Data2,6,100); |
|
1141 |
SPI1_Tranceive(Data1,Data2,6); |
|
1142 |
SPI1_CS_NA; |
|
1143 |
|
|
1144 |
Temp=(Data2[4]<<8)|Data2[5]; |
005755
|
1145 |
return Temp; |
Q |
1146 |
} |
|
1147 |
uint64_t SPI_Flash_ReadUID(uint8_t * Uid) |
|
1148 |
{ |
|
1149 |
union |
|
1150 |
{ |
|
1151 |
uint64_t Temp; |
|
1152 |
uint32_t temp2[2]; |
|
1153 |
uint8_t Rxdata[8]; |
|
1154 |
}uids; |
|
1155 |
|
|
1156 |
uint8_t Data1[5] = {W25X_ReadUniqueID,0x00,0x00,0x00,0x00}; |
|
1157 |
uint8_t Data2[8]= {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; |
9aed5d
|
1158 |
SPI1_CS_EN; |
Q |
1159 |
SPI_Transmit(SPI1, Data1,5,100); |
|
1160 |
SPI_TransmitReceive(SPI1, Data2,uids.Rxdata,8,100); |
|
1161 |
SPI1_CS_NA; |
005755
|
1162 |
memcpy(Uid,uids.Rxdata,8); |
Q |
1163 |
uint32_t t1 = __rev(uids.temp2[0]); |
|
1164 |
uids.temp2[0]= __rev(uids.temp2[1]); |
|
1165 |
uids.temp2[1]=t1; |
|
1166 |
return uids.Temp; |
|
1167 |
} |
|
1168 |
|
|
1169 |
void SPI_FLASH_Write_Enable(void) |
|
1170 |
{ |
|
1171 |
uint8_t Txdata[2]={W25X_WriteEnable}; |
9aed5d
|
1172 |
SPI1_CS_EN; |
Q |
1173 |
SPI1_Tranceive(Txdata,Txdata,1); |
|
1174 |
SPI1_CS_NA; |
005755
|
1175 |
} |
Q |
1176 |
|
|
1177 |
void SPI_FLASH_Write_Disable(void) |
|
1178 |
{ |
|
1179 |
uint8_t Txdata[2]={W25X_WriteDisable}; |
9aed5d
|
1180 |
SPI1_CS_EN; |
Q |
1181 |
SPI_Transmit(SPI1, Txdata,1,100); |
|
1182 |
SPI1_CS_NA; |
005755
|
1183 |
} |
Q |
1184 |
void W25QXX_Erase_Sector(uint32_t Dst_Addr) |
|
1185 |
{ |
|
1186 |
//??falsh????,??? |
|
1187 |
|
|
1188 |
Dst_Addr*=4096; |
|
1189 |
uint8_t Data1[4] = {W25X_SectorErase,0x00,0x00,0x00}; |
|
1190 |
Data1[1]=Dst_Addr>>16; |
|
1191 |
Data1[2]=Dst_Addr>>8; |
|
1192 |
Data1[3]=Dst_Addr; |
|
1193 |
SPI_FLASH_Write_Enable(); //SET WEL ,??? |
|
1194 |
SPI_Flash_Wait_Busy(); |
|
1195 |
|
9aed5d
|
1196 |
SPI1_CS_EN; |
Q |
1197 |
SPI1_Tranceive(Data1,Data1,4); |
|
1198 |
SPI1_CS_NA; |
005755
|
1199 |
SPI_Flash_Wait_Busy(); //?????? |
Q |
1200 |
} |
|
1201 |
void W25QXX_Read(uint8_t* pBuffer,uint32_t ReadAddr,uint16_t NumByteToRead) |
|
1202 |
{ |
|
1203 |
uint16_t i; |
|
1204 |
uint8_t Data1[4] = {W25X_ReadData,0x00,0x00,0x00}; |
9aed5d
|
1205 |
// uint8_t Data2[16]= {0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; |
005755
|
1206 |
|
Q |
1207 |
Data1[1]=ReadAddr>>16; |
|
1208 |
Data1[2]=ReadAddr>>8; |
|
1209 |
Data1[3]=ReadAddr; |
|
1210 |
|
9aed5d
|
1211 |
SPI1_CS_EN; |
Q |
1212 |
SPI_Transmit(SPI1, Data1,4,100); |
005755
|
1213 |
// HAL_SPI_TransmitReceive(&hspi1,pBuffer,pBuffer,NumByteToRead,10); |
Q |
1214 |
// HAL_SPI_TransmitReceive_DMA(&hspi1,pBuffer,pBuffer,NumByteToRead); |
9aed5d
|
1215 |
SPI1_Tranceive(pBuffer,pBuffer,NumByteToRead); |
005755
|
1216 |
/* |
Q |
1217 |
for(i=0;i<NumByteToRead/16;i++) |
|
1218 |
{ |
|
1219 |
HAL_SPI_TransmitReceive(&hspi1,Data2,pBuffer+i*16,16,100); |
|
1220 |
} |
|
1221 |
if (NumByteToRead%16) |
|
1222 |
{ |
|
1223 |
HAL_SPI_TransmitReceive(&hspi1,Data2,pBuffer+i*16,NumByteToRead%16,100); |
|
1224 |
} |
|
1225 |
*/ |
9aed5d
|
1226 |
SPI1_CS_NA; |
005755
|
1227 |
} |
Q |
1228 |
void W25QXX_Write_Page(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t NumByteToWrite) |
|
1229 |
{ |
|
1230 |
uint8_t Data1[4] = {W25X_PageProgram,0x00,0x00,0x00}; |
|
1231 |
|
|
1232 |
Data1[1]=WriteAddr>>16; |
|
1233 |
Data1[2]=WriteAddr>>8; |
|
1234 |
Data1[3]=WriteAddr; |
|
1235 |
|
|
1236 |
SPI_FLASH_Write_Enable(); //SET WEL ,??? |
|
1237 |
SPI_Flash_Wait_Busy(); |
9aed5d
|
1238 |
SPI1_CS_EN; |
Q |
1239 |
SPI_Transmit(SPI1, Data1,4,100); |
|
1240 |
SPI_Transmit(SPI1, pBuffer,NumByteToWrite,100); |
|
1241 |
SPI1_CS_NA; |
005755
|
1242 |
SPI_Flash_Wait_Busy(); //?????? |
Q |
1243 |
} |
|
1244 |
/* |
|
1245 |
int flash_func(int argc, char * argv[]) |
|
1246 |
{ |
|
1247 |
char str1[128]; |
|
1248 |
int len1; |
|
1249 |
uint8_t databuf[4096]; |
|
1250 |
if (argc<=1) |
|
1251 |
{ |
|
1252 |
len1=sprintf(str1," id uid sr en dis read speed er write write2\r\n");fputstr(str1,len1,0); |
|
1253 |
}else if (argc>=2) |
|
1254 |
{ |
|
1255 |
if (strcmp(argv[1],"id")==0) |
|
1256 |
{ |
|
1257 |
uint16_t flashid; |
|
1258 |
flashid=SPI_Flash_ReadID(); |
|
1259 |
len1=sprintf(str1,"Readid %4X \r\n",flashid); |
|
1260 |
fputstr(str1,len1,0); |
|
1261 |
|
|
1262 |
} else if (strcmp(argv[1],"sr")==0) |
|
1263 |
{ |
|
1264 |
uint8_t flashsr; |
|
1265 |
flashsr=SPI_Flash_ReadSR(); |
|
1266 |
len1=sprintf(str1,"Readsr %2X \r\n",flashsr); |
|
1267 |
fputstr(str1,len1,0); |
|
1268 |
} else if (strcmp(argv[1],"uid")==0) |
|
1269 |
{ |
|
1270 |
uint8_t Uid[8]; |
|
1271 |
uint64_t uid; |
|
1272 |
uid=SPI_Flash_ReadUID(Uid); |
|
1273 |
len1=sprintf(str1,"Uid %016llX %02X%02X%02X%02X%02X%02X%02X%02X \r\n",uid,Uid[0],Uid[1],Uid[2],Uid[3],Uid[4],Uid[5],Uid[6],Uid[7]); |
|
1274 |
fputstr(str1,len1,0); |
|
1275 |
} else if (strcmp(argv[1],"en")==0) |
|
1276 |
{ |
|
1277 |
SPI_FLASH_Write_Enable(); |
|
1278 |
len1=sprintf(str1,"Write Enable \r\n"); |
|
1279 |
fputstr(str1,len1,0); |
|
1280 |
} else if (strcmp(argv[1],"dis")==0) |
|
1281 |
{ |
|
1282 |
SPI_FLASH_Write_Disable(); |
|
1283 |
len1=sprintf(str1,"Write Disable \r\n"); |
|
1284 |
fputstr(str1,len1,0); |
|
1285 |
}else if (strcmp(argv[1],"read")==0) |
|
1286 |
{ |
|
1287 |
len1=0; |
|
1288 |
uint32_t addr=0; |
|
1289 |
if (argc>=3) addr=atoi(argv[2]); |
|
1290 |
uint16_t datalens=256; |
|
1291 |
len1=sprintf(str1,"Read Flash %08X for %d bytes \r\n",addr,datalens); |
|
1292 |
fputstr(str1,len1,0); |
|
1293 |
W25QXX_Read(databuf,addr,datalens); |
|
1294 |
for (int i=0;i<16;i++) |
|
1295 |
{ |
|
1296 |
len1=sprintf(str1,"%2X: ",i); |
|
1297 |
for (int j=0;j<16;j++) |
|
1298 |
{ |
|
1299 |
len1+=sprintf(str1+len1," %2X",databuf[i*16+j]); |
|
1300 |
} |
|
1301 |
len1+=sprintf(str1+len1,"\r\n"); |
|
1302 |
fputstr(str1,len1,0); |
|
1303 |
} |
|
1304 |
}else if (strcmp(argv[1],"speed")==0) |
|
1305 |
{ |
|
1306 |
len1=0; |
|
1307 |
uint32_t addr=0; |
|
1308 |
if (argc>=3) addr=atoi(argv[2]); |
|
1309 |
uint16_t datalens=4096; |
|
1310 |
int times=1000; |
|
1311 |
len1=sprintf(str1,"Read Flash speed test for %d bytes %d times\r\n",datalens,times); |
|
1312 |
fputstr(str1,len1,0); |
|
1313 |
int time1=HAL_GetTick(); |
|
1314 |
for (int i=0;i<times;i++) |
|
1315 |
{ |
|
1316 |
addr=i*4096; |
|
1317 |
W25QXX_Read(databuf,addr,datalens); |
|
1318 |
} |
|
1319 |
int time2=HAL_GetTick(); |
|
1320 |
int deltime=time2-time1; |
|
1321 |
if (deltime < 1) {deltime =1;} |
|
1322 |
len1=sprintf(str1,"%d mS %dK/s \r\n",deltime,datalens*times/deltime); |
|
1323 |
fputstr(str1,len1,0); |
|
1324 |
|
|
1325 |
}else if (strcmp(argv[1],"er")==0) |
|
1326 |
{ |
|
1327 |
uint32_t addr=0; |
|
1328 |
if (argc>=3) addr=atoi(argv[2]); |
|
1329 |
W25QXX_Erase_Sector(addr); |
|
1330 |
len1=sprintf(str1," erase %d\r\n",addr); |
|
1331 |
fputstr(str1,len1,0); |
|
1332 |
} else if (strcmp(argv[1],"write")==0) |
|
1333 |
{ |
|
1334 |
uint32_t addr=0; |
|
1335 |
if (argc>=3) addr=atoi(argv[2]); |
|
1336 |
uint16_t datalens=256; |
|
1337 |
uint8_t value; |
|
1338 |
if (argc>=4) |
|
1339 |
{ |
|
1340 |
value=atoi(argv[3]); |
|
1341 |
for(int i=0;i<256;i++) {databuf[i]=value;} |
|
1342 |
}else for(int i=0;i<256;i++) {databuf[i]=i;} |
|
1343 |
W25QXX_Write_Page(databuf,addr,datalens); |
|
1344 |
len1=sprintf(str1," write %06X for %d bytes\r\n",addr,datalens); |
|
1345 |
fputstr(str1,len1,0); |
|
1346 |
} else if (strcmp(argv[1],"write2")==0) |
|
1347 |
{ |
|
1348 |
uint32_t addr=0; |
|
1349 |
if (argc>=3) addr=atoi(argv[2]); |
|
1350 |
uint16_t datalens=256; |
|
1351 |
for(int i=0;i<256;i++) {databuf[i]=0;} |
|
1352 |
W25QXX_Write_Page(databuf,addr,datalens); |
|
1353 |
len1=sprintf(str1," write %06X for %d bytes\r\n",addr,datalens); |
|
1354 |
fputstr(str1,len1,0); } |
|
1355 |
else |
|
1356 |
{ |
|
1357 |
len1=sprintf(str1,"unknown %s \r\n",argv[1]); |
|
1358 |
fputstr(str1,len1,0); |
|
1359 |
} |
|
1360 |
|
|
1361 |
} |
|
1362 |
return 0; |
|
1363 |
} |
|
1364 |
*/ |
|
1365 |
|