/
drv_uart.h
187 lines (164 loc) · 5.18 KB
/
drv_uart.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
/*!
@file drv_uart.h
@brief <brief description here>
@t.odo -
-----------------------------------------------------------------------
MIT License
Copyright (c) 2019 Ioannis Deligiannis
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
/******************************************************************************
* Preprocessor Definitions & Macros
******************************************************************************/
#ifndef DRIVERS_INC_DRV_UART_H_
#define DRIVERS_INC_DRV_UART_H_
/******************************************************************************
* Includes
******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#if __has_include("../drv_uart_config.h")
#include "../drv_uart_config.h"
#define DRV_UART_CONFIG_PRESENT
#else
#warning "Missing: `drv_uart_config.h` file in the parent folder. The library is disabled"
#endif
#ifdef DRV_UART_CONFIG_PRESENT
#if __has_include("usart.h")
#include "usart.h"
#define DRV_UART_HAL_H_PRESENT
#else
#error "Missing: `usart.h` file. Cannot use this library"
#endif
#endif
#ifdef DRV_UART_HAL_H_PRESENT
#if defined(DRV_UART_TIMER) && defined(DRV_UART_TIMER_HANDLER)
#if __has_include("tim.h")
#include "tim.h"
#define DRV_UART_ENABLED
#else
#undef DRV_UART_ENABLED
#error "Missing: `tim.h` file. Cannot use this library as raw with timouts"
#endif
#else
#define DRV_UART_ENABLED
#endif
#endif
#ifdef DRV_UART_ENABLED
#if __has_include("FreeRTOS.h")
#include "FreeRTOS.h"
#endif
#if __has_include("lib_crypto.h")
#include "lib_crypto.h"
#endif
/******************************************************************************
* Enumerations, structures & Variables
******************************************************************************/
#ifndef ENUM_I_STATUS
#define ENUM_I_STATUS
typedef enum
{
I_OK = 0x00,
I_INVALID = 0x01,
I_EXISTS = 0x02,
I_NOTEXISTS = 0x03,
I_FAILED = 0x04,
I_EXPIRED = 0x05,
I_UNKNOWN = 0x06,
I_INPROGRESS = 0x07,
I_IDLE = 0x08,
I_FULL = 0x09,
I_EMPTY = 0x0A,
I_YES = 0x0B,
I_NO = 0x0C,
I_SKIP = 0x0D,
I_LOCKED = 0x0E,
I_INACTIVE = 0x0F,
I_ACTIVE = 0x10,
I_READY = 0x11,
I_WAIT = 0x12,
I_OVERFLOW = 0x13,
I_CONTINUE = 0x14,
I_STOPPED = 0x15,
I_WARNING = 0x16,
I_SLEEP = 0x17,
I_DEEPSLEEP = 0x18,
I_STANDBY = 0x19,
I_GRANTED = 0x1A,
I_DENIED = 0x1B,
I_DEBUG_01 = 0xE0,
I_DEBUG_02 = 0xE1,
I_DEBUG_03 = 0xE2,
I_DEBUG_04 = 0xE3,
I_DEBUG_05 = 0xE4,
I_DEBUG_06 = 0xE5,
I_DEBUG_07 = 0xE6,
I_DEBUG_08 = 0xE7,
I_DEBUG_09 = 0xE8,
I_DEBUG_10 = 0xE9,
I_DEBUG_11 = 0xEA,
I_DEBUG_12 = 0xEB,
I_DEBUG_13 = 0xEC,
I_DEBUG_14 = 0xED,
I_DEBUG_15 = 0xEE,
I_DEBUG_16 = 0xEF,
I_MEMALIGNED = 0xFC,
I_MEMUNALIGNED = 0xFD,
I_NOTIMPLEMENTED = 0xFE,
I_ERROR = 0xFF
}i_status;
#endif
struct uart_callback
{
void (*callback)(UART_HandleTypeDef *huart, uint8_t *buffer, uint32_t size);
struct uart_callback *next;
};
typedef struct uart_callback uart_callback_t;
typedef struct
{
void (*mx_init)();
UART_HandleTypeDef *huart;
uart_callback_t * callbacks;
GPIO_TypeDef * tx_port;
uint16_t tx_pin;
uint8_t parse_as_protocol;
uint8_t* in_buffer;
uint32_t in_buffer_sz;
uint32_t in_buffer_tsz;
#ifdef DRV_UART_TIMER
uint16_t send_custom_low;
uint8_t raw_timout;
uint8_t max_raw_timout;
#endif
}uart_t;
/******************************************************************************
* Declaration | Public Functions
******************************************************************************/
i_status uart_initialize(uart_t* uart);
i_status uart_send(uart_t* uart, uint8_t *buffer, uint32_t size);
i_status uart_send_message(uart_t* uart, uint8_t *buffer, uint32_t size);
#ifdef DRV_UART_TIMER
i_status uart_send_lowpulse(uart_t* uart, uint32_t microseconds);
void uart_tim_complete_cb(TIM_HandleTypeDef *htim);
#endif
i_status uart_callback_add(uart_t* huart, void(*cb)(UART_HandleTypeDef *huart, uint8_t *buffer, uint32_t size));
/******************************************************************************
* EOF - NO CODE AFTER THIS LINE
******************************************************************************/
#endif
#endif