-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.c
More file actions
118 lines (107 loc) · 3.91 KB
/
Copy pathexample.c
File metadata and controls
118 lines (107 loc) · 3.91 KB
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
/*******************************************************************************
* Include files
******************************************************************************/
#include "magrid.h"
#include "ring_buffer.h"
/*******************************************************************************
* Local type definitions ('typedef')
******************************************************************************/
/*******************************************************************************
* Local pre-processor symbols/macros ('#define')
******************************************************************************/
/*******************************************************************************
* Global variable definitions (declared in header file with 'extern')
******************************************************************************/
static uint8_t magrid_tx[1024] = {0};
static uint8_t magrid_rx[2048] = {0};
static ring_buffer_t magrid_ring_buff_tx = {0};
static ring_buffer_t magrid_ring_buff_rx = {0};
static bool magrid_tx_compl_f = true;
/*******************************************************************************
* Local function prototypes ('static')
******************************************************************************/
/*******************************************************************************
* Local variable definitions ('static')
******************************************************************************/
/*******************************************************************************
* Function implementation - public ('extern') and private ('static')
******************************************************************************/
/*******************************************************************************
** \brief None
** \param None
** \retval None
******************************************************************************/
static void magrid_start_tx(void)
{
if(!magrid_tx_compl_f) return;
if(!ring_buffer_is_empty(&magrid_ring_buff_tx))
{
magrid_tx_compl_f = false;
USART_ConfigInt(USARTy, USART_INT_TXDE, ENABLE);
USART_ConfigInt(USARTy, USART_INT_TXC, ENABLE);
}
}
uint16_t magrid_send_tx(uint8_t *data, uint16_t len)
{
uint16_t written = ring_buffer_put_multiple(&magrid_ring_buff_tx, data, len);
if(written > 0) magrid_start_tx();
return written;
}
uint8_t magrid_read_rx(uint8_t *buffer, uint16_t len)
{
uint16_t read_count = 0;
for (uint16_t i = 0; i < len; i++)
{
if(!ring_buffer_get(&magrid_ring_buff_rx, &buffer[i])) break;
read_count++;
}
return read_count;
}
/*******************************************************************************
** \brief None
** \param None
** \retval None
******************************************************************************/
void magrid_rx_irq_handler(void)
{
uint8_t received_byte = (uint8_t)USART_ReceiveData(USARTy);
ring_buffer_put(&magrid_ring_buff_rx, received_byte);
}
void magrid_tx_irq_handler(void)
{
uint8_t data = 0;
if(ring_buffer_get(&magrid_ring_buff_tx, &data)
{
USART_SendData(USARTy, (uint16_t)data);
}
else
{
USART_ConfigInt(USARTy, USART_INT_TXDE, DISABLE);
USART_ConfigInt(USARTy, USART_INT_TXC, DISABLE);
magrid_tx_compl_f = true;
}
}
void magrid_tx_cmpl_irq_handler(void)
{
if(ring_buffer_is_empty(&magrid_ring_buff_tx))
{
magrid_start_tx();
}
else
{
magrid_tx_compl_f = true;
USART_ConfigInt(USARTy, USART_INT_TXDE, DISABLE);
USART_ConfigInt(USARTy, USART_INT_TXC, DISABLE);
}
}
/*******************************************************************************
** \brief None
** \param None
** \retval None
******************************************************************************/
void magrid_init(void)
{
ring_buffer_init(&magrid_ring_buff_tx, magrid_tx, sizeof(magrid_tx));
ring_buffer_init(&magrid_ring_buff_rx, magrid_rx, sizeof(magrid_rx));
//............
}