canbus  2.0.0.0
canbus.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef CANBUS_H
29 #define CANBUS_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_uart.h"
48 
70 #define DRV_BUFFER_SIZE 200
71  // canbus_set
73 
88 #define CANBUS_MAP_MIKROBUS( cfg, mikrobus ) \
89  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
90  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
91  cfg.stb = MIKROBUS( mikrobus, MIKROBUS_AN )
92  // canbus_map // canbus
95 
100 typedef struct
101 {
102  // Output pins
103 
104  digital_out_t stb;
106  // Modules
107 
108  uart_t uart;
110  // Buffers
111 
112  char uart_rx_buffer[ DRV_BUFFER_SIZE ];
113  char uart_tx_buffer[ DRV_BUFFER_SIZE ];
115 } canbus_t;
116 
121 typedef struct
122 {
123  // Communication gpio pins
124 
125  pin_name_t rx_pin;
126  pin_name_t tx_pin;
128  // Additional gpio pins
129 
130  pin_name_t stb;
134  // Static variable
135 
136  uint32_t baud_rate;
138  uart_data_bits_t data_bit;
139  uart_parity_t parity_bit;
140  uart_stop_bits_t stop_bit;
142 } canbus_cfg_t;
143 
148 typedef enum
149 {
151  CANBUS_ERROR = -1
152 
154 
171 
186 err_t canbus_init ( canbus_t *ctx, canbus_cfg_t *cfg );
187 
202 
216 err_t canbus_generic_write ( canbus_t *ctx, char *data_buf, uint16_t len );
217 
231 err_t canbus_generic_read ( canbus_t *ctx, char *data_buf, uint16_t max_len );
232 
247 
262 
275 err_t canbus_send_data ( canbus_t *ctx, char *tx_data );
276 
277 #ifdef __cplusplus
278 }
279 #endif
280 #endif // CANBUS_H
281  // canbus
283 
284 // ------------------------------------------------------------------------ END
canbus_t
CAN Bus Click context object.
Definition: canbus.h:101
canbus_default_cfg
err_t canbus_default_cfg(canbus_t *ctx)
CAN Bus default configuration function.
canbus_cfg_t::tx_pin
pin_name_t tx_pin
Definition: canbus.h:126
canbus_t::uart
uart_t uart
Definition: canbus.h:108
canbus_init
err_t canbus_init(canbus_t *ctx, canbus_cfg_t *cfg)
CAN Bus initialization function.
canbus_set_low_current_standby_mode
err_t canbus_set_low_current_standby_mode(canbus_t *ctx)
CAN Bus low current standby mode function.
CANBUS_OK
@ CANBUS_OK
Definition: canbus.h:150
canbus_cfg_t::rx_pin
pin_name_t rx_pin
Definition: canbus.h:125
canbus_generic_read
err_t canbus_generic_read(canbus_t *ctx, char *data_buf, uint16_t max_len)
CAN Bus data reading function.
canbus_set_high_speed_mode
err_t canbus_set_high_speed_mode(canbus_t *ctx)
CAN Bus high speed mode function.
canbus_cfg_t::uart_blocking
bool uart_blocking
Definition: canbus.h:137
canbus_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: canbus.h:139
canbus_cfg_t
CAN Bus Click configuration object.
Definition: canbus.h:122
canbus_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: canbus.h:140
CANBUS_ERROR
@ CANBUS_ERROR
Definition: canbus.h:151
canbus_cfg_setup
void canbus_cfg_setup(canbus_cfg_t *cfg)
CAN Bus configuration object setup function.
canbus_t::stb
digital_out_t stb
Definition: canbus.h:104
canbus_cfg_t::baud_rate
uint32_t baud_rate
Definition: canbus.h:136
canbus_return_value_t
canbus_return_value_t
CAN Bus Click return value data.
Definition: canbus.h:149
canbus_generic_write
err_t canbus_generic_write(canbus_t *ctx, char *data_buf, uint16_t len)
CAN Bus data writing function.
canbus_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: canbus.h:138
DRV_BUFFER_SIZE
#define DRV_BUFFER_SIZE
CAN Bus driver buffer size.
Definition: canbus.h:70
canbus_send_data
err_t canbus_send_data(canbus_t *ctx, char *tx_data)
CAN Bus send data function.
canbus_cfg_t::stb
pin_name_t stb
Definition: canbus.h:130