-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperipheral.c
138 lines (122 loc) · 3.37 KB
/
peripheral.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "peripheral.h"
////////// LEDS /////////////////////
// LED0
const GPIO_Pin_TypeDef LED_pin[LEDS_CNT] = { GPIO_PIN_5 };
GPIO_TypeDef* const LED_port[LEDS_CNT] = { GPIOB };
uint8_t g_LEDS_mode[LEDS_CNT];
void Leds_init(void)
{
uint8_t i;
for(i=0;i<LEDS_CNT;i++)
{
GPIO_Init(LED_port[i], LED_pin[i], GPIO_MODE_OUT_PP_HIGH_SLOW);
Led_setmode(i, LED_OFF);
}
}
void Led_setmode(uint8_t num, uint8_t mode)
{
if (num >= LEDS_CNT) return;
g_LEDS_mode[num] = mode;
if (mode == LED_ON) {
GPIO_WriteLow(LED_port[num], LED_pin[num]);
} else
if (mode == LED_OFF) {
GPIO_WriteHigh(LED_port[num], LED_pin[num]);
}
}
/// UART ///////////////////////////////////////////
uint32_t g_UART_timeout;
uint8_t g_UART_RX_buf[256];
volatile uint8_t g_UART_RX_buf_WR_ptr;
uint8_t g_UART_RX_buf_RD_ptr;
void UART_init(void)
{
UART1_DeInit();
UART1_Init((uint32_t)UART_SPEED, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO,
UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
GPIOD->CR1 |= (uint8_t)GPIO_PIN_6; // UART RX Pull up
g_UART_RX_buf_WR_ptr = 0;
g_UART_RX_buf_RD_ptr = 0;
g_UART_timeout = 10000; // 10 sec
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);
UART1_Cmd(ENABLE);
}
uint8_t UART_send(uint8_t *p_buf, uint16_t size)
{
uint16_t i;
if (!p_buf) return -1;
if (!size) return -2;
for(i=0;i<size;i++)
{
while (UART1_GetFlagStatus(UART1_FLAG_TXE) == RESET); // Loop until the end of transmission
UART1_SendData8(p_buf[i]);
}
return 0;
}
void UART_RX_INTR(void)
{
g_UART_RX_buf[g_UART_RX_buf_WR_ptr] = UART1_ReceiveData8();
g_UART_RX_buf_WR_ptr++;
}
void UART_Flush(void)
{
g_UART_RX_buf_RD_ptr = g_UART_RX_buf_WR_ptr;
}
bool UART_available(void)
{
return ((g_UART_RX_buf_RD_ptr == g_UART_RX_buf_WR_ptr)?FALSE:TRUE);
}
uint8_t UART_readStringUntil(uint8_t *p_buf, uint8_t buf_max_size, uint8_t c)
{
uint8_t size = 0;
uint32_t tickstart = HAL_GetTick_ms();
while(size < buf_max_size) {
if (g_UART_RX_buf_RD_ptr != g_UART_RX_buf_WR_ptr) {
p_buf[size] = g_UART_RX_buf[g_UART_RX_buf_RD_ptr];
g_UART_RX_buf_RD_ptr++;
if (p_buf[size] == c) return size;
size++;
} else {
if ((HAL_GetTick_ms() - tickstart) > g_UART_timeout) return 0;
}
}
return size;
}
uint32_t UART_strtoul(uint8_t *p_buf, uint8_t buf_size)
{
uint32_t ret = 0;
uint8_t size = 0;
if (buf_size > 10) buf_size = 10;
while(size < buf_size) {
ret = ret * 10;
ret += (p_buf[size] - '0');
size++;
}
return ret;
}
uint8_t UART_ultostr(uint8_t *p_buf, uint8_t max_buf_size, uint32_t value)
{
uint8_t size = 0, tmp;
while(size < max_buf_size) {
p_buf[max_buf_size-1-size] = (value % 10) + '0';
value = value / 10;
size++;
if (value == 0) break;
}
if (size < max_buf_size) {
tmp = size;
while(tmp) {
p_buf[size-tmp] = p_buf[max_buf_size-tmp];
tmp--;
}
p_buf[size] = 0;
}
return size;
}
void UART_bytetohex(uint8_t *p_buf, uint8_t value)
{
p_buf[0] = (value >> 4) + '0';
if (p_buf[0] > '9') p_buf[0] += ('A'-1-'9');
p_buf[1] = (value & 0xF) + '0';
if (p_buf[1] > '9') p_buf[1] += ('A'-1-'9');
}