canfd  2.0.0.0
canfd.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef CANFD_H
36 #define CANFD_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_uart.h"
51 
52 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define CANFD_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
65  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
66  cfg.stb = MIKROBUS( mikrobus, MIKROBUS_RST ); \
67  cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
68  cfg.wke = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
69  cfg.err = MIKROBUS( mikrobus, MIKROBUS_INT )
70 
76 #define CANFD_RETVAL uint8_t
77 
78 #define CANFD_OK 0x00
79 #define CANFD_INIT_ERROR 0xFF
80 
86 #define CANFD_PIN_STATE_ENABLE 0x01
87 #define CANFD_PIN_STATE_DISABLE 0x00
88 #define CANFD_NULL 0x00
89 
95 #define CANFD_OPERATING_MODE_NORMAL 0
96 #define CANFD_OPERATING_MODE_RECEIVE 1
97 #define CANFD_OPERATING_MODE_STAND_BY 2
98 #define CANFD_OPERATING_MODE_SLEEP 3
99 
105 #define DRV_RX_BUFFER_SIZE 500
106  // End group macro
109 // --------------------------------------------------------------- PUBLIC TYPES
117 typedef struct
118 {
119  // Output pins
120 
121  digital_out_t stb;
122  digital_out_t en;
123 
124  // Input pins
125 
126  digital_in_t wke;
127  digital_in_t err;
128 
129  // Modules
130 
131  uart_t uart;
132 
133  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
134  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
135 
136 } canfd_t;
137 
141 typedef struct
142 {
143  // Communication gpio pins
144 
145  pin_name_t rx_pin;
146  pin_name_t tx_pin;
147 
148  // Additional gpio pins
149 
150  pin_name_t stb;
151  pin_name_t en;
152  pin_name_t wke;
153  pin_name_t err;
154 
155  // static variable
156 
157  uint32_t baud_rate; // Clock speed.
159  uart_data_bits_t data_bit; // Data bits.
160  uart_parity_t parity_bit; // Parity bit.
161  uart_stop_bits_t stop_bit; // Stop bits.
162 
163 } canfd_cfg_t;
164 
168 typedef uint8_t canfd_error_t;
169  // End types group
171 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
172 
178 #ifdef __cplusplus
179 extern "C"{
180 #endif
181 
191 
200 
207 void canfd_set_wke_pin ( canfd_t *ctx, uint8_t state );
208 
214 void canfd_set_stb_pin ( canfd_t *ctx, uint8_t state );
215 
222 uint8_t canfd_get_err_pin ( canfd_t *ctx );
223 
230 void canfd_set_stand_by_stb_pin ( canfd_t *ctx, uint8_t stb_state );
231 
238 void canfd_set_mode_en_pin ( canfd_t *ctx, uint8_t en_state );
239 
246 void canfd_generic_write ( canfd_t *ctx, char *data_buf, uint16_t len );
247 
255 int32_t canfd_generic_read ( canfd_t *ctx, char *data_buf, uint16_t max_len );
256 
263 void canfd_set_operating_mode ( canfd_t *ctx, uint8_t op_mode );
264 
265 #ifdef __cplusplus
266 }
267 #endif
268 #endif // _CANFD_H_
269  // End public_function group
272 
273 // ------------------------------------------------------------------------- END
canfd_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: canfd.h:160
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: canfd.h:105
canfd_cfg_t::err
pin_name_t err
Definition: canfd.h:153
canfd_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: canfd.h:159
canfd_init
CANFD_RETVAL canfd_init(canfd_t *ctx, canfd_cfg_t *cfg)
Initialization function.
canfd_cfg_setup
void canfd_cfg_setup(canfd_cfg_t *cfg)
Config Object Initialization function.
canfd_set_mode_en_pin
void canfd_set_mode_en_pin(canfd_t *ctx, uint8_t en_state)
Set EN ( CS ) pin state.
CANFD_RETVAL
#define CANFD_RETVAL
Definition: canfd.h:76
canfd_t::stb
digital_out_t stb
Definition: canfd.h:121
canfd_set_stand_by_stb_pin
void canfd_set_stand_by_stb_pin(canfd_t *ctx, uint8_t stb_state)
Set STB ( RST ) pin state.
canfd_set_stb_pin
void canfd_set_stb_pin(canfd_t *ctx, uint8_t state)
Set RST ( reset ) pin state.
canfd_cfg_t::en
pin_name_t en
Definition: canfd.h:151
canfd_set_wke_pin
void canfd_set_wke_pin(canfd_t *ctx, uint8_t state)
Set WKE ( wake-up ) pin state.
canfd_set_operating_mode
void canfd_set_operating_mode(canfd_t *ctx, uint8_t op_mode)
Operation mode.
canfd_t::en
digital_out_t en
Definition: canfd.h:122
canfd_cfg_t::wke
pin_name_t wke
Definition: canfd.h:152
canfd_cfg_t::rx_pin
pin_name_t rx_pin
Definition: canfd.h:145
canfd_cfg_t
Click configuration structure definition.
Definition: canfd.h:142
canfd_error_t
uint8_t canfd_error_t
Error type.
Definition: canfd.h:168
canfd_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: canfd.h:161
canfd_get_err_pin
uint8_t canfd_get_err_pin(canfd_t *ctx)
Set ERR ( INT ) pin state.
canfd_generic_read
int32_t canfd_generic_read(canfd_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.
canfd_t::wke
digital_in_t wke
Definition: canfd.h:126
canfd_generic_write
void canfd_generic_write(canfd_t *ctx, char *data_buf, uint16_t len)
Generic write function.
canfd_t
Click ctx object definition.
Definition: canfd.h:118
canfd_cfg_t::baud_rate
uint32_t baud_rate
Definition: canfd.h:157
canfd_cfg_t::tx_pin
pin_name_t tx_pin
Definition: canfd.h:146
canfd_t::uart
uart_t uart
Definition: canfd.h:131
canfd_cfg_t::stb
pin_name_t stb
Definition: canfd.h:150
canfd_cfg_t::uart_blocking
bool uart_blocking
Definition: canfd.h:158
canfd_t::err
digital_in_t err
Definition: canfd.h:127