O_STM32HAL库二次封装


O_嵌入式专题目录


  • 基于 STM32Cube_FW_F1_V1.8.0 HAL库,具体库函数说明详见索引;索引因实时性问题与源码可能会有些许差距,源码为最新版本。

(Tips:Win系统反编译chm hh -decompile D:\htmlFloder D:\DEC.chm

INCLUDE_ATY.h

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
/**
* @mainpage ATYSTMLIB_HAL
* @section Project
* - ATYSTMLIB_HAL
*
* @section Author
* - ATY
*
* @section Copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @section Descriptions
* - Familiar functions for STM32F1 series
**********************************************************************************
*/

#ifndef __INCLUDE_ATY_H
#define __INCLUDE_ATY_H


#include "stdio.h"
//#include "GPIO_ATY.h"
//#include "TIMER_ATY.h"
//#include "UART_ATY.h"
//#include "I2C_ATY.h"
//#include "RTC_ATY.h"
//#include "PID_ATY.h"
//#include "CRC_ATY.h"
//#include "FILTER_ATY.h"
//#include "L_74HC165_ATY.h"
//#include "L_TPIC6B595_ATY.h"


#endif /* __INCLUDE_ATY_H */

/******************************** End Of File *********************************/

CRC_ATY.h

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
175
176
177
178
179
180
181
182
183
184
185
186
/**
* @file CRC_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @par Update
* - 1_01_200318 > ATY
* -# Preliminary version, first Release
* - 1_03_200318 > ATY
* -# Add Conditional Compilation
* -# Add user port
* - 1_05_200722 > ATY
* -# Change parameters
**********************************************************************************
*/


#ifndef __CRC_ATY_H
#define __CRC_ATY_H

#include "stm32f1xx_hal.h"


/******************************* For user *************************************/
/******************************************************************************/


/**
* @brief Invert buf with uint8_t size
* @param genBuf - Generate buf
* @param srcBuf - Input source buf
*/
void InvertUint8(uint8_t *genBuf, uint8_t *srcBuf)
{
uint8_t i;
uint8_t tempBuf=0;

for(i=0;i<8;i++)
{
if(srcBuf[0]&(1<<i))
tempBuf|=1<<(7-i);
}
genBuf[0]=tempBuf;
}
/**
* @brief Invert buf with uint16_t size
* @param genBuf - Generate buf
* @param srcBuf - Input source buf
*/
void InvertUint16(uint16_t *genBuf, uint16_t *srcBuf)
{
uint8_t i;
uint16_t tempBuf=0;

for(i=0;i<16;i++)
{
if(srcBuf[0]&(1<<i))
tempBuf|=1<<(15-i);
}
genBuf[0]=tempBuf;
}
/**
* @brief Generate CRC of Modbus 16
* @param buf - Generate buf
* @param size - Input source buf
* @return Value of CRC generate
* @warning Buf size should not cross the boundary, otherwise it will get stuck
*/
uint16_t GenCrc16Modbus(uint8_t *buf, uint8_t size)
{
uint16_t crcXorResult=0xFFFF;
uint16_t crcXorMultinomial=0x8005;
uint8_t tempBuf=0;

while(size--)
{
tempBuf=*(buf++);
InvertUint8(&tempBuf, &tempBuf);
crcXorResult^=(tempBuf<<8);

for(int i=0;i<8;i++)
{
if(crcXorResult&0x8000)
crcXorResult=(crcXorResult<<1)^crcXorMultinomial;
else
crcXorResult=crcXorResult<<1;
}
}
InvertUint16(&crcXorResult, &crcXorResult);
return (crcXorResult);
}
/**
* @brief Generate CRC of Modbus 16 with high bit or low bit
* @param buf - Generate buf
* @param size - Input source buf
* @param highLow - Choose output high bit or low bit, 1 is high, 0 is low
* @return Value of CRC generate in high bit or low bit
* @warning Buf size should not cross the boundary, otherwise it will get stuck
*/
uint8_t GenCrc16ModbusHL(uint8_t *buf, uint8_t size, uint8_t highLow)
{
uint16_t crcXorResult=0xFFFF;
uint16_t crcXorMultinomial=0x8005;
uint8_t tempBuf=0;

while(size--)
{
tempBuf=*(buf++);
InvertUint8(&tempBuf, &tempBuf);
crcXorResult^=(tempBuf<<8);

for(int i=0;i<8;i++)
{
if(crcXorResult&0x8000)
crcXorResult=(crcXorResult<<1)^crcXorMultinomial;
else
crcXorResult=crcXorResult<<1;
}
}
InvertUint16(&crcXorResult, &crcXorResult);
if(highLow)
return ((uint8_t)(crcXorResult & 0x00FF));
return ((uint8_t)((crcXorResult >> 8) & 0xFF));
}
/**
* @brief Check CRC of Modbus 16
* @param buf - Generate buf
* @param size - Input source buf
* @return Check CRC success or fault
* @warning Buf size should not cross the boundary, otherwise it will get stuck
* @todo "bufData" can creat with unkonw size
*/
uint8_t CheckCrc16Modbus(uint8_t *buf, uint8_t size)
{
uint8_t bufData[20];
for(int i=0;i<size-2;i++)
bufData[i]=buf[i];

uint16_t crcO=GenCrc16Modbus(bufData, size-2);
uint8_t CRC_L=(uint8_t)(crcO&0x00FF);
uint8_t CRC_H=(uint8_t)((crcO>>8)&0xFF);

if((buf[size-2]==CRC_L) && (buf[size-1]==CRC_H))
return 1;
return 0;
}

/**
* @brief Method of application
*/
void UseOfCrc16Modbus(void)
{
volatile uint8_t checkResult=0;
uint8_t tempA[12]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0xB3,0xC3};
uint8_t tempB[12]={0x0A,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01}; //B0E7
uint8_t tempC[12]={0x0A,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x01}; //B0E7

checkResult=CheckCrc16Modbus(tempA, 12);
tempB[10]=(uint8_t)(GenCrc16Modbus(tempB, 10) & 0x00FF);
tempB[11]=(uint8_t)((GenCrc16Modbus(tempB, 10) >> 8) & 0x00FF);
tempC[10]=GenCrc16ModbusHL(tempC, 10, 1);
tempC[11]=GenCrc16ModbusHL(tempC, 10, 0);
}


#endif /* __CRC_ATY_H */

/******************************** End Of File *********************************/

FILTER_ATY.h

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
/**
* @file FILTER_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @par Update
* - 1_01_200318 > ATY
* -# Preliminary version, first Release
* - 1_02_200318 > ATY
* -# Add Conditional Compilation
* -# Add user port
**********************************************************************************
*/


#ifndef __FILTER_ATY_H
#define __FILTER_ATY_H

#include "stm32f1xx_hal.h"


/******************************* For user *************************************/
#define CHECK_TIMES 0x06
#define CHECK_STATE 0x14
/******************************************************************************/


volatile uint8_t filterState8[CHECK_STATE][CHECK_TIMES]={0};
/**
* @brief 8 byte size filter
* @param name - Which array bit to save
* @param state - The newest state of input
* @param checkTimes - Times to check
* @return The newest state when it be stable
* @warning The real filter time depends on the total run time of the loop which
* contains this program multiply the number of filter times
*/
uint8_t FilterA_8(uint8_t name, uint8_t state, uint8_t checkTimes)
{
uint8_t FilterA_8_i=0;
for(FilterA_8_i=0; FilterA_8_i < (checkTimes-1); FilterA_8_i++)
filterState8[name][FilterA_8_i+1] = filterState8[name][(FilterA_8_i)];
filterState8[name][0]=state;
for(FilterA_8_i=0; FilterA_8_i < (checkTimes-1); FilterA_8_i++)
{
if(filterState8[name][FilterA_8_i] != filterState8[name][(FilterA_8_i+1)])
return 0xFF;
}
return filterState8[name][0];
}


volatile uint32_t filterState32[CHECK_STATE][CHECK_TIMES]={0};
/**
* @brief 32 byte size filter
* @param name - Which array bit to save
* @param state - The newest state of input
* @param checkTimes - Times to check
* @return The newest state when it be stable
* @warning The real filter time depends on the total run time of the loop which
* contains this program multiply the number of filter times
*/
uint32_t FilterA_32(uint32_t name, uint32_t state, uint32_t checkTimes)
{
uint32_t FilterA_32_i=0;
filterState32[name][CHECK_TIMES-1]=state;
for(FilterA_32_i=0; FilterA_32_i < (checkTimes-1); FilterA_32_i++)
filterState32[name][FilterA_32_i] = filterState32[name][(FilterA_32_i+1)];
for(FilterA_32_i=0; FilterA_32_i < (checkTimes-1); FilterA_32_i++)
{
if(filterState32[name][FilterA_32_i] != filterState32[name][(FilterA_32_i+1)])
return 0xFF;
}
return filterState32[name][0];
}


#endif /* __DEBOUNCE_ATY_H */

/******************************** End Of File *********************************/

GPIO_ATY.h

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/**
* @file GPIO_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @attention
* - If use KeyDetect function, the whole while delay should be as small
* as possible
*
* @par Update
* - 1_01_200321 > ATY
* -# Preliminary version, first Release
* - 1_02_200401 > ATY
* -# Add KeyTest()
* - 1_03_200402 > ATY
* -# Add KeyScan() reserved function
* -# Refactor all function
* -# Add KeyDetectOnce()
* -# Add KeyDetectTwiceBlock()
* -# Add KeyDetectTwiceUnblock()
**********************************************************************************
*/


#ifndef __GPIO_ATY_H
#define __GPIO_ATY_H

#include "stm32f1xx_hal.h"


/******************************* For user *************************************/
/** @brief You can define GPIO read&write function with your MCU type */

#define HeartbeatLedPort GPIOE
#define HeartbeatLedPin GPIO_PIN_5
#define KeyPort GPIOE
#define KeyPin GPIO_PIN_4
/******************************************************************************/


#define NONE_PRESS 0
#define PRESS_ONCE 1
#define PRESS_TWICE 2
#define PRESS_LONG 3


uint8_t pressType=NONE_PRESS;
#define LONG_PRESS_TIME 60
uint8_t longPressCount=0;
/**
* @brief Detect whether the key is pressed and how to press
* @param GPIOx - GPIO_Port
* @param GPIO_Pin - GPIO_Pin
*/
void KeyDetectOnce(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
if(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin)==GPIO_PIN_RESET)
{
HAL_Delay(20);
if(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin)==GPIO_PIN_RESET)
{
if(longPressCount>LONG_PRESS_TIME)
{
pressType=PRESS_LONG;
}
else
{
longPressCount++;
}
}
}
if(HAL_GPIO_ReadPin(GPIOx, GPIO_Pin)==GPIO_PIN_SET)
{
if(longPressCount>0 && longPressCount<=LONG_PRESS_TIME)
{
pressType=PRESS_ONCE;
}
longPressCount=0;
}
}

#define INTERVAL_TIME 100
uint16_t intervalCount=0;
/**
* @brief Detect press key twice in block way
* @param GPIOx - GPIO_Port
* @param GPIO_Pin - GPIO_Pin
*/
void KeyDetectTwiceBlock(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
KeyDetectOnce(GPIOx, GPIO_Pin);
if(pressType==PRESS_ONCE)
{
intervalCount=0;
pressType=NONE_PRESS;
while(++intervalCount)
{
KeyDetectOnce(GPIOx, GPIO_Pin);
HAL_Delay(1);
if(pressType==PRESS_ONCE)
{
pressType=PRESS_TWICE;
break;
}
else
{
if(intervalCount>INTERVAL_TIME)
{
pressType=PRESS_ONCE;
break;
}
}
}
}
}

uint8_t pressTimes=0;
/**
* @brief Detect press key twice in block way
* @param GPIOx - GPIO_Port
* @param GPIO_Pin - GPIO_Pin
*/
void KeyDetectTwiceUnblock(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
KeyDetectOnce(GPIOx, GPIO_Pin);
if(pressType==PRESS_ONCE)
{
intervalCount=0;
if(pressTimes==0)
{
pressType=NONE_PRESS;
pressTimes++;
}
else
{
pressType=PRESS_TWICE;
pressTimes=0;
intervalCount=0;
}
}
intervalCount++;
HAL_Delay(1);
if(intervalCount>INTERVAL_TIME && pressTimes!=0)
{
pressType=PRESS_ONCE;
pressTimes=0;
intervalCount=0;
}
}
/**
* @brief Scan key matrix value
* @warning If return value is 32bit, theoretical support 16*16 key matrix
*/
uint32_t KeyScan(void)
{

return 0;
}
/**
* @brief 8 byte size filter
* @param keyValue - Key value from KeyScan() return
*/
void KeyDetectCombine(uint32_t keyValue)
{

}
/**
* @brief Test key input
* @param testMode - Test key input
*/
void KeyTest(uint8_t testMode)
{
switch(testMode)
{
case 1:
{
KeyDetectOnce(KeyPort, KeyPin);
if(pressType==PRESS_ONCE)
{
pressType=NONE_PRESS;
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
}
else if(pressType==PRESS_LONG)
{
pressType=NONE_PRESS;
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(20);
}
break;
}
case 2:
{
KeyDetectTwiceBlock(KeyPort, KeyPin);
if(pressType==PRESS_ONCE)
{
pressType=NONE_PRESS;
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
}
else if(pressType==PRESS_LONG)
{
pressType=NONE_PRESS;
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(20);
}
else if(pressType==PRESS_TWICE)
{
pressType=NONE_PRESS;
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(200);
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(200);
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(200);
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(200);

}
break;
}
case 3:
{
KeyDetectTwiceUnblock(KeyPort, KeyPin);
if(pressType==PRESS_ONCE)
{
pressType=NONE_PRESS;
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
}
else if(pressType==PRESS_LONG)
{
pressType=NONE_PRESS;
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(20);
}
else if(pressType==PRESS_TWICE)
{
pressType=NONE_PRESS;
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(200);
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(200);
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(200);
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
HAL_Delay(200);

}
break;
}
case 4:
{
KeyScan();
break;
}
}
}


#endif /* __GPIO_ATY_H */

/******************************** End Of File *********************************/

I2C_ATY.h

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
/**
* @file I2C_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @par Update
* - 1_01_200320 > ATY
* -# Preliminary version, first Release
**********************************************************************************
*/


#ifndef __I2C_ATY_H
#define __I2C_ATY_H

#include "stm32f1xx_hal.h"


/******************************* For user *************************************/
#define HI2C_N hi2c1
/******************************************************************************/


extern I2C_HandleTypeDef HI2C_N;
/**
* @brief Read I2C with HAL lib function
* @param addr - Start address
* @param pData - Data
* @param size - Size to write or read
*/
void I2C_Read(uint16_t addr, uint8_t *pData, uint16_t size)
{
HAL_I2C_Mem_Read(&HI2C_N, 0xA0, addr, I2C_MEMADD_SIZE_8BIT, pData, size, 100);
}
/**
* @brief Write I2C with HAL lib function
* @param addr - Start address
* @param pData - Data
* @param size - Size to write or read
*/
void I2C_Write(uint16_t addr, uint8_t *pData, uint16_t size)
{
uint16_t firstWrite=0,i=0;

HAL_I2C_Mem_Write(&HI2C_N, 0xA1, addr, I2C_MEMADD_SIZE_8BIT, pData, size, 100);
HAL_Delay(5);

firstWrite=8-(addr%8); //How long left of this page
if((addr/8) != ((addr+size)/8))
{
for(i=0; i<(((addr+size)/8)-(addr/8)); i++)
{
// HAL_IWDG_Refresh(&hiwdg);
HAL_I2C_Mem_Write(&HI2C_N, 0xA1, addr+firstWrite+(i*8), I2C_MEMADD_SIZE_8BIT,
pData+firstWrite+(i*8), size-firstWrite-(i*8), 100);
HAL_Delay(5);
}
}
}

#define WRITE_LENGTH 5
/**
* @brief A template to use I2C function for AT24C02
*/
void I2C_Template(void)
{
uint8_t tmpWriteData[WRITE_LENGTH]={0};
uint8_t tmpAddr=0,tmpReadData=0;
uint16_t i=0;

I2C_Read(0, &tmpAddr, 1);
if(tmpAddr>=(256-WRITE_LENGTH))
{
tmpAddr=0;
}
tmpAddr+=WRITE_LENGTH;
I2C_Write(0, &tmpAddr, 1);

tmpWriteData[0]+=tmpAddr;
for(i=0;i<(WRITE_LENGTH-1);i++)
{
// HAL_IWDG_Refresh(&hiwdg);
tmpWriteData[i+1]=tmpWriteData[i]+1;
}
I2C_Write(tmpAddr, tmpWriteData, WRITE_LENGTH);

for(i=0;i<(tmpAddr+WRITE_LENGTH);i++)
{
// HAL_IWDG_Refresh(&hiwdg);
I2C_Read(i, &tmpReadData, 1);
printf("%d ", tmpReadData);
}
printf("\n\n");

// I2C_Read(tmpAddr, tmpBuf+2, WRITE_LENGTH); //tmpBuf can be a pointer like a aarry
}


/**
* @brief Eliminate HAL_I2C bug
* @warning Put "__HAL_RCC_I2C1_CLK_ENABLE();" before GPIO init
*/
//void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
//{
// GPIO_InitTypeDef GPIO_InitStruct = {0};
// if(hi2c->Instance==I2C1)
// {
// /* USER CODE BEGIN I2C1_MspInit 0 */
// __HAL_RCC_I2C1_CLK_ENABLE();
// /* USER CODE END I2C1_MspInit 0 */
//
// __HAL_RCC_GPIOB_CLK_ENABLE();
// /**I2C1 GPIO Configuration
// PB6 ------> I2C1_SCL
// PB7 ------> I2C1_SDA
// */
// GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
// GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
// GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
// HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//
// /* Peripheral clock enable */
// //__HAL_RCC_I2C1_CLK_ENABLE();
// /* USER CODE BEGIN I2C1_MspInit 1 */
//
// /* USER CODE END I2C1_MspInit 1 */
// }
//
//}


#endif /* __I2C_ATY_H */

/******************************** End Of File *********************************/

L_74HC165_ATY.h

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
/**
* @file L_74HC165_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of L_74HC165 for all codes
*
* @par Update
* - 1_01_200525 > ATY
* -# Preliminary version, first Release
* - 1_02_200525 > ATY
* -# Add Conditional Compilation
* -# Add user port
**********************************************************************************
*/


#ifndef __L_74HC165_ATY_H
#define __L_74HC165_ATY_H

#include "stm32f1xx_hal.h"

/******************************* For user *************************************/
#define CLK_W_H HAL_GPIO_WritePin(L_74HC165_CLK_GPIO_Port, L_74HC165_CLK_Pin, GPIO_PIN_SET)
#define CLK_W_L HAL_GPIO_WritePin(L_74HC165_CLK_GPIO_Port, L_74HC165_CLK_Pin, GPIO_PIN_RESET)
#define SH_W_H HAL_GPIO_WritePin(L_74HC165_SH_GPIO_Port, L_74HC165_SH_Pin, GPIO_PIN_SET)
#define SH_W_L HAL_GPIO_WritePin(L_74HC165_SH_GPIO_Port, L_74HC165_SH_Pin, GPIO_PIN_RESET)
#defien DATA_R_H (HAL_GPIO_ReadPin(L_74HC165_DATA_GPIO_Port, L_74HC165_DATA_Pin) == GPIO_PIN_SET)
#define DATA_R_L (HAL_GPIO_ReadPin(L_74HC165_DATA_GPIO_Port, L_74HC165_DATA_Pin) == GPIO_PIN_RESET)
/******************************************************************************/


/**
* @brief Get value of L_74HC165
* @param size - Size to read
* @return Value of L_74HC165
* @warning L_74HC165 reading data unstable at high temperature(about 80 celsius degree),
* so it's better to read at the begining only
*/
uint32_t L_74HC165_Get(uint32_t size)
{
uint32_t scanValue=0,i_L_74HC165_Get=0;

CLK_W_H;
SH_W_L; //Start read
SH_W_H; //Stop read

for(i_L_74HC165_Get=0; i_L_74HC165_Get<size; i_L_74HC165_Get++)
{
scanValue = scanValue << 1;
if(DATA_R_H)
scanValue |= 0x01;
CLK_W_L;
__nop();
CLK_W_H;
}

CLK_W_L;

return scanValue;
}

#endif /* __L_74HC165_ATY_H */

/******************************** End Of File *********************************/

PID_ATY.h

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
/**
* @file PID_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @par Update
* - 1_01_200318 > ATY
* -# Preliminary version, first Release
* - 1_02_200318 > ATY
* -# Add Conditional Compilation
* -# Add user port
**********************************************************************************
*/


#ifndef __PID_ATY_H
#define __PID_ATY_H

#include "stm32f1xx_hal.h"
#include "string.h"


/******************************* For user *************************************/
/******************************************************************************/


double pidSetNum=0,pidCurNum=0;

typedef struct PID{
double SetPoint; //Set point desired value
double Proportion; //Proportional const
double Integral; //Integral const
double Derivative; //Derivative const
double LastError; //Error[-1]
double PrevError; //Error[-2]
double SumError; //Sums of errors
}PID;

/**
* @brief Calculate rOut
* @param pidStruct - pidStruct
* @param nextPoint - nextPoint
* @return pidStruct
*/
double PidCalc(PID *pidStruct, double nextPoint)
{
double dError,Error;
Error=pidStruct->SetPoint-nextPoint; //Deviation
pidStruct->SumError+=Error; //Integral
dError=pidStruct->LastError-pidStruct->PrevError; //Current differential
pidStruct->PrevError=pidStruct->LastError;
pidStruct->LastError=Error;
return (pidStruct->Proportion *Error //Proportional
+pidStruct->Integral *pidStruct->SumError //Integral item
+pidStruct->Derivative *dError ); //Differential item
}

/**
* @brief Init pid
* @param pidStruct - pidStruct
*/
void PidInit (PID *pidStruct)
{
memset(pidStruct, 0, sizeof(PID));
}

/**
* @brief Calculate pidCurNum
* @param rDelta - rDelta
* @return
*/
void Actuator(double rDelta)
{
pidCurNum+=rDelta;
}

/**
* @brief Pid control
*/
void PidControl(void)
{
PID sPID[2]; //PID Control Struct
double rOut[2]; //PID Response (Output)
double rIn[2]; //PID Feedback (Input)
uint8_t i=0;

PidInit(&sPID[0]); //Initialize Struct
sPID[0].Proportion=0.5; //Set PID Coefficients
sPID[0].Integral=0.5;
sPID[0].Derivative=0;
sPID[0].SetPoint=pidSetNum; //Set PID Setpoint (Goal)

while(1) //Better torite in timer interrupt call back function
{ //Mock Up of PID processing
if(i>50)
{
i=0;
pidSetNum+=10;
if(pidSetNum>100)
pidSetNum=0;
}
i++;

sPID[0].SetPoint=pidSetNum;

rIn[0]=pidCurNum; //Reads the input variable function (Read Input )
rOut[0]=PidCalc(&sPID[0], rIn[0]); //PID calculation function (Perform PID Interation)
Actuator(rOut[0]); //Output variable control function (Effect Needed Changes)

// printf("In: %f\nOut: %f\nPidCur: %f\n\n", rIn[0], rOut[0], pidCurNum);
printf("%f\n", pidCurNum);

HAL_Delay(100);
}
}


#endif /* __PID_ATY_H */

/******************************** End Of File *********************************/

RTC_ATY.h

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/**
* @file RTC_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @par Update
* - 1_01_200317 > ATY
* -# Preliminary version, first Release
* - 1_02_200318 > ATY
* -# Add RTC_Set and RTC_GetShowHex()
* - 1_03_200319 > ATY
* -# Add RTC_UartSet()
* -# Add RTC_FormatToUnix32()
* -# Add RTC_Unix32ToFormat()
**********************************************************************************
*/


#ifndef __RTC_ATY_H
#define __RTC_ATY_H

#include "stm32f1xx_hal.h"
#include "time.h"


/******************************* For user *************************************/
#define RTC_N hrtc
// #define SET_IN_UART
/******************************************************************************/


extern RTC_HandleTypeDef RTC_N;
RTC_DateTypeDef rtcDateStructure;
RTC_TimeTypeDef rtcTimeStructure;

/**
* @brief Init rtc date and time
*/
void RTC_Init(void)
{
rtcDateStructure.Year=20;
rtcDateStructure.Month=3;
rtcDateStructure.Date=16;
rtcTimeStructure.Hours=17;
rtcTimeStructure.Minutes=12;
rtcTimeStructure.Seconds=00;
HAL_RTC_SetDate(&RTC_N, &rtcDateStructure, RTC_FORMAT_BIN);
HAL_RTC_SetTime(&RTC_N, &rtcTimeStructure, RTC_FORMAT_BIN);
}

/**
* @brief Set rtc date and time
* @param year - Year
* @param month - Month
* @param date - Date
* @param hours - Hours
* @param minutes - Minutes
* @param seconds - Seconds
*/
void RTC_Set(uint8_t year, uint8_t month, uint8_t date,
uint8_t hours, uint8_t minutes, uint8_t seconds)
{
rtcDateStructure.Year=year;
rtcDateStructure.Month=month;
rtcDateStructure.Date=date;
rtcTimeStructure.Hours=hours;
rtcTimeStructure.Minutes=minutes;
rtcTimeStructure.Seconds=seconds;
HAL_RTC_SetDate(&RTC_N, &rtcDateStructure, RTC_FORMAT_BIN);
HAL_RTC_SetTime(&RTC_N, &rtcTimeStructure, RTC_FORMAT_BIN);
}

/**
* @brief Determine if it's a leap year
* @param year - Year
* @return Is leap year or not
* @brief
* Month 01 02 03 04 05 06 07 08 09 10 11 12
* IsLeapYear 31 29 31 30 31 30 31 31 30 31 30 31
* NotLeapYear 31 28 31 30 31 30 31 31 30 31 30 31
*/
uint8_t IsLeapYear(uint16_t year)
{
if(year%4==0)
{
if(year%100==0)
{
/*- It has to be divisible by 400 if the year end with 00 */
if(year%400==0)
{
return 1;
}
else
{
return 0;
}
}
else
{
return 1;
}
}
else
{
return 0;
}
}


/**
* @brief Get rtc date time and convert to unix
* @param show - Chose to print or not
* @return Time data in unix32
* @warning Must get time before get date
*/
uint32_t RTC_FormatToUnix32(uint8_t show)
{
uint32_t unixTime;
struct tm stmT;

HAL_RTC_GetTime(&hrtc, &rtcTimeStructure, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&hrtc, &rtcDateStructure, RTC_FORMAT_BIN);

stmT.tm_year=rtcDateStructure.Year+100;
stmT.tm_mon=rtcDateStructure.Month-1;
stmT.tm_mday=rtcDateStructure.Date;
stmT.tm_hour=rtcTimeStructure.Hours-8;
stmT.tm_min=rtcTimeStructure.Minutes;
stmT.tm_sec=rtcTimeStructure.Seconds;

unixTime=mktime(&stmT);
if(show)
{
printf("UTC:%d\n",unixTime);
}
return unixTime;
}

/**
* @brief Detect whether the key is pressed and how to press
* @param unixTime - Time data in unix32
* @param show - Chose to print or not
*/
void RTC_Unix32ToFormat(uint32_t unixTime, uint8_t show)
{
struct tm *stmU;

stmU=localtime(&unixTime);

rtcDateStructure.Year=stmU->tm_year-100;
rtcDateStructure.Month=stmU->tm_mon+1;
rtcDateStructure.Date=stmU->tm_mday;
rtcTimeStructure.Hours=stmU->tm_hour+8;
rtcTimeStructure.Minutes=stmU->tm_min;
rtcTimeStructure.Seconds=stmU->tm_sec;

HAL_RTC_SetDate(&hrtc, &rtcDateStructure, RTC_FORMAT_BIN);
HAL_RTC_SetTime(&hrtc, &rtcTimeStructure, RTC_FORMAT_BIN);

if(show)
{
printf("%02d/%02d/%02d\n", 2000+rtcDateStructure.Year, rtcDateStructure.Month, rtcDateStructure.Date);
printf("%02d:%02d:%02d\n\n", rtcTimeStructure.Hours, rtcTimeStructure.Minutes, rtcTimeStructure.Seconds);
}
}

uint8_t dateTimeShow[6]={0};
/**
* @brief Get rtc date time and print to uart
* @param show - Chose to print or not
* @warning Must get time before get date
*/
void RTC_GetShow(uint8_t show)
{
HAL_RTC_GetTime(&RTC_N, &rtcTimeStructure, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RTC_N, &rtcDateStructure, RTC_FORMAT_BIN);

if(show)
{
dateTimeShow[0]=RTC_FormatToUnix32(0)>>24;
dateTimeShow[1]=RTC_FormatToUnix32(0)>>16;
dateTimeShow[2]=RTC_FormatToUnix32(0)>>8;
dateTimeShow[3]=RTC_FormatToUnix32(0);
dateTimeShow[4]=0x00;
dateTimeShow[5]=0x00;
// dateTimeShow[0]=rtcDateStructure.Year;
// dateTimeShow[1]=rtcDateStructure.Month;
// dateTimeShow[2]=rtcDateStructure.Date;
// dateTimeShow[3]=rtcTimeStructure.Hours;
// dateTimeShow[4]=rtcTimeStructure.Minutes;
// dateTimeShow[5]=rtcTimeStructure.Seconds;
// /* Display date Format : yy-mm-dd */
printf("\n%02d-%02d-%02d\n",2000+rtcDateStructure.Year,
rtcDateStructure.Month,
rtcDateStructure.Date);
// /* Display time Format : hh:mm:ss */
printf("%02d:%02d:%02d\n",rtcTimeStructure.Hours,
rtcTimeStructure.Minutes,
rtcTimeStructure.Seconds);
}
}

/**
* @brief Get rtc date time and print to uart in hexadecimal
* @param show - Chose to print or not
* @warning Must get time before get date
*/
void RTC_GetShowHex(uint8_t show)
{
uint32_t dateHex=0,timeHex=0;

HAL_RTC_GetTime(&RTC_N, &rtcTimeStructure, RTC_FORMAT_BIN);
HAL_RTC_GetDate(&RTC_N, &rtcDateStructure, RTC_FORMAT_BIN);

timeHex=(rtcTimeStructure.Hours<<16)+(rtcTimeStructure.Minutes<<8)+rtcTimeStructure.Seconds;
dateHex=(rtcDateStructure.Year<<16)+(rtcDateStructure.Month<<8)+rtcDateStructure.Date;

if(show)
{
printf("\n%08x\n",dateHex);
printf("%08x\n",timeHex);
}
}

void RTC_GetShowUnix32(uint8_t show)
{

}

#ifdef SET_IN_UART
/**
* @brief Use uart set RTC time
* @brief Temp: 200318083000 - 2020/03/18 08:30:00
*/
void RTC_SetInUart(void)
{
if(uart1GetCount >0)
{
rtcDateStructure.Year=((uart1GetStr[0]-48)*10)+(uart1GetStr[1]-48);
rtcDateStructure.Month=((uart1GetStr[2]-48)*10)+(uart1GetStr[3]-48);
rtcDateStructure.Date=((uart1GetStr[4]-48)*10)+(uart1GetStr[5]-48);
rtcTimeStructure.Hours=((uart1GetStr[6]-48)*10)+(uart1GetStr[7]-48);
rtcTimeStructure.Minutes=((uart1GetStr[8]-48)*10)+(uart1GetStr[9]-48);
rtcTimeStructure.Seconds=((uart1GetStr[10]-48)*10)+(uart1GetStr[11]-48);
HAL_RTC_SetTime(&RTC_N, &rtcTimeStructure, RTC_FORMAT_BIN);
HAL_RTC_SetDate(&RTC_N, &rtcDateStructure, RTC_FORMAT_BIN);
memset(uart1GetStr, 0, sizeof(uart1GetStr));
uart1GetCount=0;
}
}
#endif /* SET_IN_UART */


#endif /* __RTC_ATY_H */

/******************************** End Of File *********************************/

TIMER_ATY.h

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* @file TIMER_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @par Update
* - 1_01_200317 > ATY
* -# Preliminary version, first Release
* - 1_02_200318 > ATY
* -# Add Conditional Compilation
* -# Add user port
* - 1_03_200319 > ATY
* -# Add RTC delay_s()
* - 1_04_200401 > ATY
* -# Add delay_s() for other types
* -# Add delayTest()
* -# Fix Timer type function bug
* - 1_05_200423 > ATY
* -# Add TEST_ATY()
**********************************************************************************
*/


#ifndef __TIMER_ATY_H
#define __TIMER_ATY_H

#include "stm32f1xx_hal.h"


/******************************* For user *************************************/
//#define SYS_ATY
//#define HAL_ATY
#define TIM_ATY
#define HTIM_N htim2
//#define RTC_ATY
//#define HRTC_N hrtc

//#define TEST_ATY
/******************************************************************************/


/****************************** Use SysTick ***********************************/
#ifdef SYS_ATY
#define F0_BASE_NUM_SYS 6
#define F1_BASE_NUM_SYS 9
#define BASE_NUM_SYS F1_BASE_NUM_SYS
/**
* @brief Delay n us with SysTick
* @param us - us
* @brief SysTick is a 24-bit timer, max count number is 16777215;
* F0: 48MHz/8 -> 6 -> 6 numbers per us -> 2796ms max;
* F1: 72MHz/8 -> 9 -> 9 numbers per us -> 1864ms max;
*/
void delay_us(uint32_t us)
{
uint32_t temp;
SysTick->LOAD=BASE_NUM_SYS*us;
SysTick->VAL=0X00;
SysTick->CTRL=0X01;
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&(!(temp&(1<<16))));

SysTick->CTRL=0x00;
SysTick->VAL=0X00;
}
/**
* @brief Delay n ms with SysTick
* @param ms - ms
*/
void delay_ms(uint16_t ms)
{
uint32_t temp;
SysTick->LOAD=BASE_NUM_SYS*1000*ms;
SysTick->VAL=0X00;
SysTick->CTRL=0X01;
do
{
temp=SysTick->CTRL;
}while((temp&0x01)&&(!(temp&(1<<16))));

SysTick->CTRL=0x00;
SysTick->VAL=0X00;
}
/**
* @brief Delay n s with SysTick
* @param s - s
*/
void delay_s(uint32_t s)
{
for(;s>0;s--)
{
delay_ms(1000);
}
}
#endif /* SYS_ATY */


/**************************** Change Hal_Delay() ******************************/
#ifdef HAL_ATY
/**
* @brief Delay n us in changing RCC clock
* @param us - us
*/
void delay_us(uint32_t us)
{
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000000);
HAL_Delay(us);
}
/**
* @brief Delay n ms in changing RCC clock
* @param ms - ms
* @return
*/
void delay_ms(uint16_t ms)
{
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_Delay(ms);
}
/**
* @brief Delay n s in changing RCC clock
* @param s - s
*/
void delay_s(uint32_t s)
{
for(;s>0;s--)
{
delay_ms(1000);
}
}

#endif /* HAL_ATY */


/********************************* Use Timer **********************************/
#ifdef TIM_ATY
extern TIM_HandleTypeDef HTIM_N;
/**
* @brief Delay n us with TimerN
* @param us - us
* @brief Commonly used in RTOS microsecond delay;
* F1 -> 72Mhz -> Prescaler=71 -> 1us per count,
* Prescaler=719 -> 10us per count,
* Prescaler=7199 -> 100us per count,
* so Period init number need to be larger than max us,
* and Prescaler init must be 71 if use this type delay function
* @warning Be easily disturbed by other interrupt;
*/
void delay_us(uint16_t us)
{
uint16_t timerCounter=0xffff-us-5;
HAL_TIM_Base_Start(&HTIM_N);
__HAL_TIM_SET_COUNTER(&HTIM_N,timerCounter);
while(timerCounter<0xffff-5)
{
timerCounter=__HAL_TIM_GET_COUNTER(&HTIM_N);
}
HAL_TIM_Base_Stop(&HTIM_N);
}
/**
* @brief Delay n ms with TimerN
* @param ms - ms
*/
void delay_ms(uint32_t ms)
{
for(;ms>0;ms--)
{
delay_us(1000);
}
}
/**
* @brief Delay n s with TimerN
* @param s - s
*/
void delay_s(uint32_t s)
{
for(;s>0;s--)
{
delay_ms(1000);
}
}
#endif /* TIM_ATY */


/********************************* Use RTC ************************************/
#ifdef RTC_ATY
extern RTC_HandleTypeDef HRTC_N;
/**
* @brief Get counter number of rtc
* @param hrtc - hrtc
*/
uint32_t RTC_ReadTimeCounter(RTC_HandleTypeDef *hrtc)
{
uint16_t highFirst=0U,low=0U,highSecond=0U;
uint32_t timeCounter=0U;
highFirst=READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);
low=READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT);
highSecond=READ_REG(hrtc->Instance->CNTH & RTC_CNTH_RTC_CNT);

if(highFirst!=highSecond)
{
/*- In this case the counter roll over during reading of CNTL and CNTH registers,
read again CNTL register then return the counter value */
timeCounter=(((uint32_t)highSecond<<16U) |
READ_REG(hrtc->Instance->CNTL & RTC_CNTL_RTC_CNT));
}
else
{
/*- No counter roll over during reading of CNTL and CNTH registers,
counter value is equal to first value of CNTL and CNTH */
timeCounter=(((uint32_t)highFirst<<16U) | low);
}

return timeCounter;
}

/**
* @brief Delay n us
*/
void delay_us(void){}
/**
* @brief Delay n ms
*/
void delay_ms(void){}

/**
* @FunctionName: delay_s
* @Descriptions: Delay n seconds with RTC timeCounter
* @Parameters: seconds
* @ReturnedValue: none
* @Instruction:
*/
void delay_s(uint32_t s)
{
s+=RTC_ReadTimeCounter(&HRTC_N);
while(s>RTC_ReadTimeCounter(&HRTC_N)){}
}
#endif /* RTC_ATY */

#ifdef TEST_ATY
/**
* @brief A simple test function for delay_us/ms/s
* @brief Heartbeat LED need to be defined at GPIO_ATY.h
*/
void delayTest(void)
{
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
delay_ms(1000);
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
for(i_ATY=0;i_ATY<1000;i_ATY++)
delay_us(1000);
HAL_GPIO_TogglePin(HeartbeatLedPort, HeartbeatLedPin);
delay_s(5);

}
#endif /* TEST_ATY */

#endif /* __TIMER_ATY_H */

/******************************** End Of File *********************************/

L_TPIC6B595_ATY.h

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
/**
* @file L_TPIC6B595_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of L_TPIC6B595 for all codes
*
* @par Update
* - 1_01_200825 > ATY
* -# Preliminary version, first Release
**********************************************************************************
*/


#ifndef __L_TPIC6B595_ATY_H
#define __L_TPIC6B595_ATY_H

#include "stm32f1xx_hal.h"

/******************************* For user *************************************/
#define SERIN_W_H HAL_GPIO_WritePin(L_TPIC6B595_SERIN_GPIO_Port, L_TPIC6B595_SERIN_Pin, GPIO_PIN_SET)
#define SERIN_W_L HAL_GPIO_WritePin(L_TPIC6B595_SERIN_GPIO_Port, L_TPIC6B595_SERIN_Pin, GPIO_PIN_RESET)
#define RSCK_W_H HAL_GPIO_WritePin(L_TPIC6B595_RSCK_GPIO_Port, L_TPIC6B595_RSCK_Pin, GPIO_PIN_SET)
#define RSCK_W_L HAL_GPIO_WritePin(L_TPIC6B595_RSCK_GPIO_Port, L_TPIC6B595_RSCK_Pin, GPIO_PIN_RESET)
#defien SCK_W_H HAL_GPIO_WritePin(L_TPIC6B595_SCK_GPIO_Port, L_TPIC6B595_SCK_Pin, GPIO_PIN_SET)
#define SCK_W_L HAL_GPIO_WritePin(L_TPIC6B595_SCK_GPIO_Port, L_TPIC6B595_SCK_Pin, GPIO_PIN_RESET)
/******************************************************************************/


/**
* @brief Set value of L_TPIC6B595
* @param data - Data to write
* @param size - Size of data
*/
void L_TPIC6B595_Set(uint32_t data, uint32_t size)
{
uint32_t i_L_TPIC6B595_Set=0;

SCK_W_L;
RSCK_W_L;

for(i_L_TPIC6B595_Set=0; i_L_TPIC6B595_Set<size; i_L_TPIC6B595_Set++)
{
if(data&0X01)
SERIN_W_H;
else
SERIN_W_L;
__nop();__nop();
SCK_W_L;
__nop();__nop();
SCK_W_H;
__nop();__nop();
data>>=1;
}

RSCK_W_L;
__nop();__nop();
RSCK_W_H;
__nop();__nop();
RSCK_W_L;
}

#endif /* __L_TPIC6B595_ATY_H */

/******************************** End Of File *********************************/

UART_ATY.h

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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
/**
* @file UART_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @attention
* - "UartInit_ATY();" need to be added at main()
*
* @par Update
* - 1_01_200317 > ATY
* -# Preliminary version, first Release
* - 1_02_200318 > ATY
* -# Add Conditional Compilation
* -# Add user port
* - 1_03_200401 > ATY
* -# Fix UartControlTemplate()
* -# Change UartSend() to use string and size
* -# Test uart 1-5 in normal IT
* -# Change DMA all function
* -# Test uart 1-4 in DMA IT
* -# (Might be the last version)
* - 1_04_200423 > ATY
* -# Add TEST_ATY()
* - 1_05_200429 > ATY
* -# Change to uart1-5 independent function
**********************************************************************************
*/


#ifndef __UART_ATY_H
#define __UART_ATY_H

#include "stm32f1xx_hal.h"
#include "stdio.h" //For "FILE"
#include "string.h" //For memset etc.


/******************************* For user *************************************/
// #define TEST_IT_ATY
// #define TEST_DMA_ATY

//#define HUART_N huart2 //For printf();

#define UART1_NIT_ATY
// #define UART1_DMA_ATY
#define UART1_GET_SIZE 4
#define UART1_SEND_SIZE 8

#define UART2_NIT_ATY
// #define UART2_DMA_ATY
#define UART2_GET_SIZE 11
#define UART2_SEND_SIZE 8

#define UART3_NIT_ATY
// #define UART3_DMA_ATY
#define UART3_GET_SIZE 13
#define UART3_SEND_SIZE 255

// #define UART4_NIT_ATY
// #define UART4_DMA_ATY
// #define UART4_GET_SIZE 8
// #define UART4_SEND_SIZE 8

// #define UART5_NIT_ATY
// #define UART5_DMA_ATY
// #define UART5_GET_SIZE 8
// #define UART5_SEND_SIZE 8
/******************************************************************************/


#ifdef UART1_NIT_ATY
uint8_t uart1GetByte=0;
uint8_t uart1GetCount=0;
uint8_t uart1GetStr[UART1_GET_SIZE];
uint8_t uart1SendStr[UART1_SEND_SIZE];
#endif /* UART1_NIT_ATY */

#ifdef UART2_NIT_ATY
uint8_t uart2GetByte=0;
uint8_t uart2GetCount=0;
uint8_t uart2GetStr[UART2_GET_SIZE];
uint8_t uart2SendStr[UART2_SEND_SIZE];
#endif /* UART2_NIT_ATY */

#ifdef UART3_NIT_ATY
uint8_t uart3GetByte=0;
uint8_t uart3GetCount=0;
uint8_t uart3GetStr[UART3_GET_SIZE];
uint8_t uart3SendStr[UART3_SEND_SIZE];
#endif /* UART3_NIT_ATY */

#ifdef UART4_NIT_ATY
uint8_t uart4GetByte=0;
uint8_t uart4GetCount=0;
uint8_t uart4GetStr[UART4_GET_SIZE];
uint8_t uart4SendStr[UART4_SEND_SIZE];
#endif /* UART4_NIT_ATY */

#ifdef UART5_NIT_ATY
uint8_t uart5GetByte=0;
uint8_t uart5GetCount=0;
uint8_t uart5GetStr[UART5_GET_SIZE];
uint8_t uart5SendStr[UART5_SEND_SIZE];
#endif /* UART5_NIT_ATY */


#ifdef UART1_DMA_ATY
uint8_t uart1GetByte=0;
uint8_t uart1GetCount=0;
uint8_t uart1GetStr[UART1_GET_SIZE];
uint8_t uart1SendStr[UART1_SEND_SIZE];
uint16_t uart1DmaSendFlag=0;
volatile uint8_t uart1RecvEndFlag=0;
static __IO ITStatus uart1Ready=RESET;
extern DMA_HandleTypeDef hdma_usart1_rx;
#endif /* UART1_DMA_ATY */


#ifdef UART2_DMA_ATY
uint8_t uart2GetByte=0;
uint8_t uart2GetCount=0;
uint8_t uart2GetStr[UART2_GET_SIZE];
uint8_t uart2SendStr[UART2_SEND_SIZE];
uint16_t uart2DmaSendFlag=0;
volatile uint8_t uart2RecvEndFlag=0;
static __IO ITStatus uart2Ready=RESET;
extern DMA_HandleTypeDef hdma_usart2_rx;
#endif /* UART2_DMA_ATY */


#ifdef UART3_DMA_ATY
uint8_t uart3GetByte=0;
uint8_t uart3GetCount=0;
uint8_t uart3GetStr[UART3_GET_SIZE];
uint8_t uart3SendStr[UART3_SEND_SIZE];
uint16_t uart3DmaSendFlag=0;
volatile uint8_t uart3RecvEndFlag=0;
static __IO ITStatus uart3Ready=RESET;
extern DMA_HandleTypeDef hdma_usart3_rx;
#endif /* UART3_DMA_ATY */


#ifdef UART4_DMA_ATY
uint8_t uart4GetByte=0;
uint8_t uart4GetCount=0;
uint8_t uart4GetStr[UART4_GET_SIZE];
uint8_t uart4SendStr[UART4_SEND_SIZE];
uint16_t uart4DmaSendFlag=0;
volatile uint8_t uart4RecvEndFlag=0;
static __IO ITStatus uart4Ready=RESET;
extern DMA_HandleTypeDef hdma_usart4_rx;
#endif /* UART4_DMA_ATY */


#ifdef UART5_DMA_ATY
uint8_t uart5GetByte=0;
uint8_t uart5GetCount=0;
uint8_t uart5GetStr[UART5_GET_SIZE];
uint8_t uart5SendStr[UART5_SEND_SIZE];
uint16_t uart5DmaSendFlag=0;
volatile uint8_t uart5RecvEndFlag=0;
static __IO ITStatus uart5Ready=RESET;
extern DMA_HandleTypeDef hdma_usart5_rx;
#endif /* UART5_DMA_ATY */


/**
* @brief Rewrite "printf();"
*/
//extern UART_HandleTypeDef HUART_N;
//int fputc(int ch, FILE *f)
//{
// HAL_UART_Transmit(&HUART_N, (uint8_t*)&ch, 1, 1000);
// return ch;
//}


/**
* @brief Init uart interrupt
*/
void UartInit_ATY(void)
{
#ifdef UART1_NIT_ATY
HAL_UART_Receive_IT(&huart1, &uart1GetByte, 1);
#endif /* UART1_NIT_ATY */

#ifdef UART2_NIT_ATY
HAL_UART_Receive_IT(&huart2, &uart2GetByte, 1);
#endif /* UART2_NIT_ATY */

#ifdef UART3_NIT_ATY
HAL_UART_Receive_IT(&huart3, &uart3GetByte, 1);
#endif /* UART3_NIT_ATY */

#ifdef UART4_NIT_ATY
HAL_UART_Receive_IT(&huart4, &uart4GetByte, 1);
#endif /* UART4_NIT_ATY */

#ifdef UART5_NIT_ATY
HAL_UART_Receive_IT(&huart5, &uart5GetByte, 1);
#endif /* UART5_NIT_ATY */


#ifdef UART1_DMA_ATY
__HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
HAL_UART_Receive_DMA(&huart1, uart1GetStr, UART1_GET_SIZE);
#endif /* UART1_DMA_ATY */

#ifdef UART2_DMA_ATY
__HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
HAL_UART_Receive_DMA(&huart2, uart2GetStr, UART2_GET_SIZE);
#endif /* UART2_DMA_ATY */

#ifdef UART3_DMA_ATY
__HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);
HAL_UART_Receive_DMA(&huart3, uart3GetStr, UART3_GET_SIZE);
#endif /* UART3_DMA_ATY */

#ifdef UART4_DMA_ATY
__HAL_UART_ENABLE_IT(&huart4, UART_IT_IDLE);
HAL_UART_Receive_DMA(&huart4, uart4GetStr, UART4_GET_SIZE);
#endif /* UART4_DMA_ATY */

#ifdef UART5_DMA_ATY
__HAL_UART_ENABLE_IT(&huart5, UART_IT_IDLE);
HAL_UART_Receive_DMA(&huart5, uart5GetStr, UART5_GET_SIZE);
#endif /* UART5_DMA_ATY */
}

/******************************* Normal IT Type *******************************/
#ifdef UART1_NIT_ATY
/**
* @brief Send data from uart
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart1Send(uint8_t *pData, uint16_t Size)
{
HAL_UART_Transmit_IT(&huart1, (uint8_t*)pData, Size);
}
/**
* @brief Receive data from uart with IT
*/
void Uart1Recv(void)
{
if(uart1GetCount<UART1_GET_SIZE)
{
uart1GetStr[uart1GetCount++]=uart1GetByte;
}
else
{
uart1GetCount=0;
}
while(HAL_UART_Receive_IT(&huart1, &uart1GetByte, 1)==HAL_OK);
}
#endif /* UART1_NIT_ATY */


#ifdef UART2_NIT_ATY
/**
* @brief Send data from uart
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart2Send(uint8_t *pData, uint16_t Size)
{
HAL_UART_Transmit_IT(&huart2, (uint8_t*)pData, Size);
}
/**
* @brief Receive data from uart with IT
*/
void Uart2Recv(void)
{
if(uart2GetCount<UART2_GET_SIZE)
{
uart2GetStr[uart2GetCount++]=uart2GetByte;
}
else
{
uart2GetCount=0;
}
while(HAL_UART_Receive_IT(&huart2, &uart2GetByte, 1)==HAL_OK);
}
#endif /* UART2_NIT_ATY */


#ifdef UART3_NIT_ATY
/**
* @brief Send data from uart
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart3Send(uint8_t *pData, uint16_t Size)
{
HAL_UART_Transmit_IT(&huart3, (uint8_t*)pData, Size);
}
/**
* @brief Receive data from uart with IT
*/
void Uart3Recv(void)
{
if(uart3GetCount<UART3_GET_SIZE)
{
uart3GetStr[uart3GetCount++]=uart3GetByte;
}
else
{
uart3GetCount=0;
}
while(HAL_UART_Receive_IT(&huart3, &uart3GetByte, 1)==HAL_OK);
}
#endif /* UART3_NIT_ATY */


#ifdef UART4_NIT_ATY
/**
* @brief Send data from uart
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart4Send(uint8_t *pData, uint16_t Size)
{
HAL_UART_Transmit_IT(&huart4, (uint8_t*)pData, Size);
}
/**
* @brief Receive data from uart with IT
*/
void Uart4Recv(void)
{
if(uart4GetCount<UART4_GET_SIZE)
{
uart4GetStr[uart4GetCount++]=uart4GetByte;
}
else
{
uart4GetCount=0;
}
while(HAL_UART_Receive_IT(&huart4, &uart4GetByte, 1)==HAL_OK);
}
#endif /* UART4_NIT_ATY */


#ifdef UART5_NIT_ATY
/**
* @brief Send data from uart
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart5Send(uint8_t *pData, uint16_t Size)
{
HAL_UART_Transmit_IT(&huart5, (uint8_t*)pData, Size);
}
/**
* @brief Receive data from uart with IT
*/
void Uart5Recv(void)
{
if(uart5GetCount<UART5_GET_SIZE)
{
uart5GetStr[uart5GetCount++]=uart5GetByte;
}
else
{
uart5GetCount=0;
}
while(HAL_UART_Receive_IT(&huart5, &uart5GetByte, 1)==HAL_OK);
}
#endif /* UART5_NIT_ATY */


#if defined(UART1_NIT_ATY) \
|| defined(UART2_NIT_ATY) \
|| defined(UART3_NIT_ATY) \
|| defined(UART4_NIT_ATY) \
|| defined(UART5_NIT_ATY)
/**
* @brief Rewrite uart rx interrupt call-back function
* @param huart - huart
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
#ifdef UART1_NIT_ATY
if(huart==&huart1)
{
Uart1Recv();
}
#endif /* UART1_NIT_ATY */

#ifdef UART2_NIT_ATY
if(huart==&huart2)
{
Uart2Recv();
}
#endif /* UART2_NIT_ATY */

#ifdef UART3_NIT_ATY
if(huart==&huart3)
{
Uart3Recv();
}
#endif /* UART3_NIT_ATY */

#ifdef UART4_NIT_ATY
if(huart==&huart4)
{
Uart4Recv();
}
#endif /* UART4_NIT_ATY */

#ifdef UART5_NIT_ATY
if(huart==&huart5)
{
Uart5Recv();
}
#endif /* UART5_NIT_ATY */
}
#endif /* UART1-5_NIT_ATY */


#ifdef TEST_NIT_ATY
/**
* @brief A template function of using uart raceive
* @brief 50ms is the min interval time when tested at 9600 baud rate
*/
void UartControlTemplate(void)
{
HAL_UART_Receive_IT(&HUART_N, &uartByte, 1);
if(uartBufCount>0)
{
if(memcmp(uartBuf, "Hello STM32F1!", 14)==0)
{
printf("Getted! - printf\n");
UartSend("Getted! - send\n", 15);
uartBufCount=0;
memset(uartBuf, 0, sizeof(uartBuf));
}
}
}
#endif /* TEST_NIT_ATY */


/******************************** DMA IT Type *********************************/
#if defined(UART1_DMA_ATY) \
|| defined(UART2_DMA_ATY) \
|| defined(UART3_DMA_ATY) \
|| defined(UART4_DMA_ATY) \
|| defined(UART5_DMA_ATY)
/**
* @brief Uart tx DMA interrupt call back
* @param huart - huart
*/
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
#ifdef UART1_DMA_ATY
if(huart==&huart1)
uart1Ready=SET;
#endif /* UART1_DMA_ATY */

#ifdef UART2_DMA_ATY
if(huart==&huart2)
uart2Ready=SET;
#endif /* UART2_DMA_ATY */

#ifdef UART3_DMA_ATY
if(huart==&huart3)
uart3Ready=SET;
#endif /* UART3_DMA_ATY */

#ifdef UART4_DMA_ATY
if(huart==&huart4)
uart4Ready=SET;
#endif /* UART4_DMA_ATY */

#ifdef UART5_DMA_ATY
if(huart==&huart5)
uart5Ready=SET;
#endif /* UART5_DMA_ATY */
}
/**
* @brief Uart tx DMA interrupt call back
* @param huart - huart
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
#ifdef UART1_DMA_ATY
if(huart==&huart1)
uart1Ready=SET;
#endif /* UART1_DMA_ATY */

#ifdef UART2_DMA_ATY
if(huart==&huart2)
uart2Ready=SET;
#endif /* UART2_DMA_ATY */

#ifdef UART3_DMA_ATY
if(huart==&huart3)
uart3Ready=SET;
#endif /* UART3_DMA_ATY */

#ifdef UART4_DMA_ATY
if(huart==&huart4)
uart4Ready=SET;
#endif /* UART4_DMA_ATY */

#ifdef UART5_DMA_ATY
if(huart==&huart5)
uart5Ready=SET;
#endif /* UART5_DMA_ATY */
}
#endif /* UART1-5_DMA_ATY */


#ifdef UART1_DMA_ATY
/**
* @brief Send data with DMA
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart1SendDMA(uint8_t *pData, uint16_t Size)
{
if(HAL_UART_Transmit_DMA(&huart1, pData, Size)!=HAL_OK)
{
Error_Handler();
}
while(uart1Ready!=SET){}
uart1Ready=RESET;
uart1DmaSendFlag=0;
}
/**
* @brief Uart interrupt function, use to get data
*/
void USART1_IRQHandler(void) //\Src\stm32f1xx_it.c
{
uint32_t tmpFlag=0;
uint32_t temp;
tmpFlag=__HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE);
if((tmpFlag!=RESET))
{
__HAL_UART_CLEAR_IDLEFLAG(&huart1);
temp=huart1.Instance->SR;
temp=huart1.Instance->DR;
HAL_UART_DMAStop(&huart1);
temp=hdma_usart1_rx.Instance->CNDTR;
uart1GetCount=UART1_GET_SIZE-temp;
uart1RecvEndFlag=1;
}
HAL_UART_IRQHandler(&huart1);
HAL_UART_Receive_DMA(&huart1, uart1GetStr, UART1_GET_SIZE);
}
#endif /* UART1_DMA_ATY */


#ifdef UART2_DMA_ATY
/**
* @brief Send data with DMA
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart2SendDMA(uint8_t *pData, uint16_t Size)
{
if(HAL_UART_Transmit_DMA(&huart2, pData, Size)!=HAL_OK)
{
Error_Handler();
}
while(uart2Ready!=SET){}
uart2Ready=RESET;
uart2DmaSendFlag=0;
}
/**
* @brief Uart interrupt function, use to get data
*/
void USART2_IRQHandler(void) //\Src\stm32f1xx_it.c
{
uint32_t tmpFlag=0;
uint32_t temp;
tmpFlag=__HAL_UART_GET_FLAG(&huart2, UART_FLAG_IDLE);
if((tmpFlag!=RESET))
{
__HAL_UART_CLEAR_IDLEFLAG(&huart2);
temp=huart2.Instance->SR;
temp=huart2.Instance->DR;
HAL_UART_DMAStop(&huart2);
temp=hdma_usart2_rx.Instance->CNDTR;
uart2GetCount=UART2_GET_SIZE-temp;
uart2RecvEndFlag=1;
}
HAL_UART_IRQHandler(&huart2);
HAL_UART_Receive_DMA(&huart2, uart2GetStr, UART2_GET_SIZE);
}
#endif /* UART2_DMA_ATY */


#ifdef UART3_DMA_ATY
/**
* @brief Send data with DMA
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart3SendDMA(uint8_t *pData, uint16_t Size)
{
if(HAL_UART_Transmit_DMA(&huart3, pData, Size)!=HAL_OK)
{
Error_Handler();
}
while(uart3Ready!=SET){}
uart3Ready=RESET;
uart3DmaSendFlag=0;
}
/**
* @brief Uart interrupt function, use to get data
*/
void USART3_IRQHandler(void) //\Src\stm32f1xx_it.c
{
uint32_t tmpFlag=0;
uint32_t temp;
tmpFlag=__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE);
if((tmpFlag!=RESET))
{
__HAL_UART_CLEAR_IDLEFLAG(&huart3);
temp=huart3.Instance->SR;
temp=huart3.Instance->DR;
HAL_UART_DMAStop(&huart3);
temp=hdma_usart3_rx.Instance->CNDTR;
uart3GetCount=UART3_GET_SIZE-temp;
uart3RecvEndFlag=1;
}
HAL_UART_IRQHandler(&huart3);
HAL_UART_Receive_DMA(&huart3, uart3GetStr, UART3_GET_SIZE);
}
#endif /* UART3_DMA_ATY */


#ifdef UART4_DMA_ATY
/**
* @brief Send data with DMA
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart4SendDMA(uint8_t *pData, uint16_t Size)
{
if(HAL_UART_Transmit_DMA(&huart4, pData, Size)!=HAL_OK)
{
Error_Handler();
}
while(uart4Ready!=SET){}
uart4Ready=RESET;
uart4DmaSendFlag=0;
}
/**
* @brief Uart interrupt function, use to get data
*/
void USART4_IRQHandler(void) //\Src\stm32f1xx_it.c
{
uint32_t tmpFlag=0;
uint32_t temp;
tmpFlag=__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE);
if((tmpFlag!=RESET))
{
__HAL_UART_CLEAR_IDLEFLAG(&huart4);
temp=huart4.Instance->SR;
temp=huart4.Instance->DR;
HAL_UART_DMAStop(&huart4);
temp=hdma_usart4_rx.Instance->CNDTR;
uart4GetCount=UART4_GET_SIZE-temp;
uart4RecvEndFlag=1;
}
HAL_UART_IRQHandler(&huart4);
HAL_UART_Receive_DMA(&huart4, uart4GetStr, UART4_GET_SIZE);
}
#endif /* UART4_DMA_ATY */


#ifdef UART5_DMA_ATY
/**
* @brief Send data with DMA
* @param pData - Data to send
* @param Size - Size of data
*/
void Uart5SendDMA(uint8_t *pData, uint16_t Size)
{
if(HAL_UART_Transmit_DMA(&huart5, pData, Size)!=HAL_OK)
{
Error_Handler();
}
while(uart5Ready!=SET){}
uart5Ready=RESET;
uart5DmaSendFlag=0;
}
/**
* @brief Uart interrupt function, use to get data
*/
void USART5_IRQHandler(void) //\Src\stm32f1xx_it.c
{
uint32_t tmpFlag=0;
uint32_t temp;
tmpFlag=__HAL_UART_GET_FLAG(&huart5, UART_FLAG_IDLE);
if((tmpFlag!=RESET))
{
__HAL_UART_CLEAR_IDLEFLAG(&huart5);
temp=huart5.Instance->SR;
temp=huart5.Instance->DR;
HAL_UART_DMAStop(&huart5);
temp=hdma_usart5_rx.Instance->CNDTR;
uart5GetCount=UART5_GET_SIZE-temp;
uart5RecvEndFlag=1;
}
HAL_UART_IRQHandler(&huart5);
HAL_UART_Receive_DMA(&huart5, uart5GetStr, UART5_GET_SIZE);
}
#endif /* UART5_DMA_ATY */


#ifdef TEST_DMA_ATY
/**
* @brief A template function of using uart raceive with DMA
* @brief 50ms is the min interval time when tested at 9600 baud rate;
* shorter time is unstable, but better than narmal IT type
*/
void UartControlTemplate(void)
{
if(uart1RecvEndFlag)
{
if(memcmp(uart1GetStr, "Hello STM32F1!", 14)==0)
{
printf("Getted! - printf\n");
Uart1SendDMA("Getted! - send\n", 15);
memset(uart1GetStr, 0, sizeof(uart1GetStr));
uart1GetCount=0;
}
uart1RecvEndFlag=0;
}
}
#endif /* TEST_DMA_ATY */


#endif /* __UART_ATY_H */

/******************************** End Of File *********************************/

UARTIO_ATY.h - 整理中

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
/**
* @file UARTIO_ATY.h
* @par Project
* - ATYSTMLIB_HAL
*
* @author
* - ATY
*
* @copyright
* - Copyright © 2020 MZ-ATY
* - This code follows:
* - MZ-ATY Various Contents Joint Statement -
* <a href="https://mengze.top/MZ-ATY_VCJS">
* https://mengze.top/MZ-ATY_VCJS</a>
* - CC 4.0 BY-NC-SA -
* <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
* https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
* - Your use will be deemed to have accepted the terms of this statement.
* - 您的使用行为将视为已接受本声明各项条款。
*
* @par Descriptions
* - Familiar functions of CRC for all codes
*
* @par Update
* - 1_01_20722 > ATY
* -# Preliminary version, first Release
**********************************************************************************
*/

#ifndef __UART_ATY_H
#define __UART_ATY_H

#include "stm32f1xx_hal.h"
#include "stdio.h" //For "FILE"
#include "string.h" //For memset etc.


/******************************* For user *************************************/

/******************************************************************************/

#endif /* __UART_ATY_H */

/******************************** End Of File *********************************/