-
Notifications
You must be signed in to change notification settings - Fork 38
/
drv_can.c
213 lines (185 loc) · 6.23 KB
/
drv_can.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
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
/****************************************************************************
**
** Copyright (C) ${COPYRIGHT_YEAR} MikroElektronika d.o.o.
** Contact: https://www.mikroe.com/contact
**
** This file is part of the mikroSDK package
**
** Commercial License Usage
**
** Licensees holding valid commercial NECTO compilers AI licenses may use this
** file in accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The MikroElektronika Company.
** For licensing terms and conditions see
** https://www.mikroe.com/legal/software-license-agreement.
** For further information use the contact form at
** https://www.mikroe.com/contact.
**
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used for
** non-commercial projects under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** 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,
** OF MERCHANTABILITY, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
** TO THE WARRANTIES 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.
**
****************************************************************************/
/*!
* @file drv_can.c
* @brief CAN driver implementation.
*/
#include "drv_can.h"
static can_t *_owner = NULL;
static err_t _acquire( can_t *obj, bool obj_open_state )
{
err_t status = ACQUIRE_SUCCESS;
if ( obj_open_state == true && _owner == obj )
{
return ACQUIRE_FAIL;
}
if ( _owner != obj )
{
status = hal_can_open( &obj->handle, obj_open_state );
if ( status != ACQUIRE_FAIL )
_owner = obj;
}
return status;
}
void can_configure_default( can_config_t *config )
{
if ( config )
{
config->tx_pin = (pin_name_t) 0xFFFFFFFF;
config->rx_pin = (pin_name_t) 0xFFFFFFFF;
config->mode = CAN_MODE_NORMAL;
config->frequency = 500000;
}
}
void can_filter_configure_default( can_filter_config_t *filter_config )
{
if ( filter_config )
{
filter_config->can_filter_id_high = 0x0000;
filter_config->can_filter_id_low = 0x0000;
filter_config->can_filter_mask_id_high = 0x0000;
filter_config->can_filter_mask_id_low = 0x0000;
filter_config->can_filter_mode = CAN_FILTER_MODE_IDMASK;
filter_config->can_filter_scale = CAN_FILTER_SCALE_16BIT;
filter_config->can_filter_enable = CAN_FILTER_DISABLE;
filter_config->can_filter_fifo = CAN_FILTER_FIFO0;
filter_config->can_filter_bank = 0;
}
}
err_t can_open( can_t *obj, can_config_t *config )
{
can_config_t *p_config = &obj->config;
memcpy( p_config, config, sizeof( can_config_t ) );
return _acquire( obj, true );
}
err_t can_init( can_t *obj ) {
if ( _acquire( obj, false ) != ACQUIRE_FAIL )
{
return hal_can_init( &obj->handle, (hal_can_config_t *) &obj->config, (hal_can_filter_config_t *) &obj->filter_config );
} else {
return CAN_ERROR;
}
}
err_t can_set_filter( can_t *obj, can_filter_config_t *filter_config ) {
can_filter_config_t *p_config = &obj->filter_config;
memcpy( p_config, filter_config, sizeof( can_filter_config_t ) );
if ( _acquire( obj, false ) != ACQUIRE_FAIL )
{
return hal_can_set_filter( &obj->handle, (hal_can_config_t *) &obj->config, (hal_can_filter_config_t *) &obj->filter_config );
} else {
return CAN_ERROR;
}
}
err_t can_set_frequency( can_t *obj, uint32_t frequency ) {
if ( _acquire( obj, false ) != ACQUIRE_FAIL )
{
uint32_t original_value = obj->config.frequency;
obj->config.frequency = frequency;
err_t result = hal_can_set_frequency( &obj->handle, &obj->config, &obj->filter_config );
if( HAL_CAN_ERROR == result ) {
obj->config.frequency = original_value;
}
return result;
} else {
return CAN_ERROR;
}
}
err_t can_get_frequency( can_t *obj ) {
if ( _acquire( obj, false ) != ACQUIRE_FAIL )
{
return hal_can_get_frequency( &obj->handle );
} else {
return CAN_ERROR;
}
}
err_t can_get_mode( can_t *obj ) {
if ( _acquire( obj, false ) != ACQUIRE_FAIL )
{
return hal_can_get_mode( &obj->handle );
} else {
return CAN_ERROR;
}
}
err_t can_set_mode( can_t *obj, can_mode_t mode ) {
if ( _acquire( obj, false ) != ACQUIRE_FAIL )
{
can_mode_t original_value = obj->config.mode;
obj->config.mode = mode;
err_t result = hal_can_set_mode( &obj->handle, &obj->config, &obj->filter_config );
if( HAL_CAN_ERROR == result ) {
obj->config.mode = original_value;
}
return result;
} else {
return CAN_ERROR;
}
}
err_t can_transmit( can_t *obj, can_transmit_message_struct *transmit_message ) {
if ( _acquire( obj, false ) != ACQUIRE_FAIL )
{
return hal_can_transmit( &obj->handle, (hal_can_transmit_message_struct *)transmit_message );
} else {
return CAN_ERROR;
}
}
void can_transmission_stop( can_t *obj ) {
if ( _acquire( obj, false ) != ACQUIRE_FAIL ) {
hal_can_transmission_stop( &obj->handle );
}
}
err_t can_receive( can_t *obj, can_receive_message_struct *receive_message ) {
if ( _acquire( obj, false ) != ACQUIRE_FAIL )
{
return hal_can_receive( &obj->handle, (hal_can_receive_message_struct *)receive_message );
} else {
return CAN_ERROR;
}
}
err_t can_close( can_t *obj )
{
if ( CAN_SUCCESS == hal_can_close( &obj->handle ) )
{
obj->handle = NULL;
_owner = NULL;
return CAN_SUCCESS;
} else {
return CAN_ERROR;
}
}
// ------------------------------------------------------------------------- END