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 "drv_digital_out.h"
39#include "drv_digital_in.h"
40#include "drv_uart.h"
41
42
43// -------------------------------------------------------------- PUBLIC MACROS
53#define CANFD_MAP_MIKROBUS( cfg, mikrobus ) \
54 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
55 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
56 cfg.stb = MIKROBUS( mikrobus, MIKROBUS_RST ); \
57 cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS ); \
58 cfg.wke = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
59 cfg.err = MIKROBUS( mikrobus, MIKROBUS_INT )
66#define CANFD_RETVAL uint8_t
67
68#define CANFD_OK 0x00
69#define CANFD_INIT_ERROR 0xFF
76#define CANFD_PIN_STATE_ENABLE 0x01
77#define CANFD_PIN_STATE_DISABLE 0x00
78#define CANFD_NULL 0x00
85#define CANFD_OPERATING_MODE_NORMAL 0
86#define CANFD_OPERATING_MODE_RECEIVE 1
87#define CANFD_OPERATING_MODE_STAND_BY 2
88#define CANFD_OPERATING_MODE_SLEEP 3
95#define DRV_RX_BUFFER_SIZE 500 // End group macro
99// --------------------------------------------------------------- PUBLIC TYPES
107typedef struct
108{
109 // Output pins
110
111 digital_out_t stb;
112 digital_out_t en;
113
114 // Input pins
115
116 digital_in_t wke;
117 digital_in_t err;
118
119 // Modules
120
121 uart_t uart;
122
123 char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
124 char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
125
126} canfd_t;
127
131typedef struct
132{
133 // Communication gpio pins
134
135 pin_name_t rx_pin;
136 pin_name_t tx_pin;
137
138 // Additional gpio pins
139
140 pin_name_t stb;
141 pin_name_t en;
142 pin_name_t wke;
143 pin_name_t err;
144
145 // static variable
146
147 uint32_t baud_rate; // Clock speed.
149 uart_data_bits_t data_bit; // Data bits.
150 uart_parity_t parity_bit; // Parity bit.
151 uart_stop_bits_t stop_bit; // Stop bits.
152
154
158typedef uint8_t canfd_error_t;
159 // End types group
161// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
162
168#ifdef __cplusplus
169extern "C"{
170#endif
171
181
190
197void canfd_set_wke_pin ( canfd_t *ctx, uint8_t state );
198
204void canfd_set_stb_pin ( canfd_t *ctx, uint8_t state );
205
212uint8_t canfd_get_err_pin ( canfd_t *ctx );
213
220void canfd_set_stand_by_stb_pin ( canfd_t *ctx, uint8_t stb_state );
221
228void canfd_set_mode_en_pin ( canfd_t *ctx, uint8_t en_state );
229
236void canfd_generic_write ( canfd_t *ctx, char *data_buf, uint16_t len );
237
245int32_t canfd_generic_read ( canfd_t *ctx, char *data_buf, uint16_t max_len );
246
253void canfd_set_operating_mode ( canfd_t *ctx, uint8_t op_mode );
254
255#ifdef __cplusplus
256}
257#endif
258#endif // _CANFD_H_
259 // End public_function group
262
263// ------------------------------------------------------------------------- END
#define DRV_RX_BUFFER_SIZE
Definition: canfd.h:95
#define CANFD_RETVAL
Definition: canfd.h:66
void canfd_cfg_setup(canfd_cfg_t *cfg)
Config Object Initialization function.
void canfd_set_stand_by_stb_pin(canfd_t *ctx, uint8_t stb_state)
Set STB ( RST ) pin state.
void canfd_generic_write(canfd_t *ctx, char *data_buf, uint16_t len)
Generic write function.
void canfd_set_stb_pin(canfd_t *ctx, uint8_t state)
Set RST ( reset ) pin state.
uint8_t canfd_get_err_pin(canfd_t *ctx)
Set ERR ( INT ) pin state.
CANFD_RETVAL canfd_init(canfd_t *ctx, canfd_cfg_t *cfg)
Initialization function.
void canfd_set_operating_mode(canfd_t *ctx, uint8_t op_mode)
Operation mode.
void canfd_set_wke_pin(canfd_t *ctx, uint8_t state)
Set WKE ( wake-up ) pin state.
void canfd_set_mode_en_pin(canfd_t *ctx, uint8_t en_state)
Set EN ( CS ) pin state.
int32_t canfd_generic_read(canfd_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.
uint8_t canfd_error_t
Error type.
Definition: canfd.h:158
Click configuration structure definition.
Definition: canfd.h:132
uint32_t baud_rate
Definition: canfd.h:147
bool uart_blocking
Definition: canfd.h:148
pin_name_t err
Definition: canfd.h:143
uart_data_bits_t data_bit
Definition: canfd.h:149
pin_name_t wke
Definition: canfd.h:142
pin_name_t tx_pin
Definition: canfd.h:136
pin_name_t rx_pin
Definition: canfd.h:135
uart_stop_bits_t stop_bit
Definition: canfd.h:151
pin_name_t en
Definition: canfd.h:141
uart_parity_t parity_bit
Definition: canfd.h:150
pin_name_t stb
Definition: canfd.h:140
Click ctx object definition.
Definition: canfd.h:108
digital_out_t stb
Definition: canfd.h:111
digital_in_t wke
Definition: canfd.h:116
uart_t uart
Definition: canfd.h:121
digital_out_t en
Definition: canfd.h:112
digital_in_t err
Definition: canfd.h:117