canfd2  2.0.0.0
canfd2.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  */
32 // ----------------------------------------------------------------------------
33 
34 #ifndef CANFD2_H
35 #define CANFD2_H
36 
41 #ifdef PREINIT_SUPPORTED
42 #include "preinit.h"
43 #endif
44 
45 #ifdef MikroCCoreVersion
46  #if MikroCCoreVersion >= 1
47  #include "delays.h"
48  #endif
49 #endif
50 
51 #include "drv_digital_out.h"
52 #include "drv_digital_in.h"
53 #include "drv_uart.h"
54 #include "drv_spi_master.h"
55 #include "spi_specifics.h"
56 
57 // -------------------------------------------------------------- PUBLIC MACROS
67 #define CANFD2_MAP_MIKROBUS( cfg, mikrobus ) \
68  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
69  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
70  cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
71  cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
72  cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
73  cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS )
74 
80 #define CANFD2_OK 0
81 #define CANFD2_ERROR -1
82 
88 #define CANFD2_DRV_TX_BUFFER_SIZE 100
89 #define CANFD2_DRV_RX_BUFFER_SIZE 300
90 
96 #define CANFD2_REG_MODE_CTRL 0x01
97 #define CANFD2_REG_HW_CTRL 0x02
98 #define CANFD2_REG_TXD_TO_CTRL 0x03
99 #define CANFD2_REG_SUPPLY_CTRL 0x04
100 
106 #define CANFD2_REG_SWK_CTRL_1 0x05
107 #define CANFD2_REG_SWK_CTRL_2 0x06
108 #define CANFD2_REG_SWK_ID3_CTRL 0x07
109 #define CANFD2_REG_SWK_ID2_CTRL 0x08
110 #define CANFD2_REG_SWK_ID1_CTRL 0x09
111 #define CANFD2_REG_SWK_ID0_CTRL 0x0A
112 #define CANFD2_REG_SWK_MASK_ID3_CTRL 0x0B
113 #define CANFD2_REG_SWK_MASK_ID2_CTRL 0x0C
114 #define CANFD2_REG_SWK_MASK_ID1_CTRL 0x0D
115 #define CANFD2_REG_SWK_MASK_ID0_CTRL 0x0E
116 #define CANFD2_REG_SWK_DLC_CTRL 0x0F
117 #define CANFD2_REG_SWK_DATA7_CTRL 0x10
118 #define CANFD2_REG_SWK_DATA6_CTRL 0x11
119 #define CANFD2_REG_SWK_DATA5_CTRL 0x12
120 #define CANFD2_REG_SWK_DATA4_CTRL 0x13
121 #define CANFD2_REG_SWK_DATA3_CTRL 0x14
122 #define CANFD2_REG_SWK_DATA2_CTRL 0x15
123 #define CANFD2_REG_SWK_DATA1_CTRL 0x16
124 #define CANFD2_REG_SWK_DATA0_CTRL 0x17
125 
131 #define CANFD2_REG_TRANS_STAT 0x18
132 #define CANFD2_REG_TRANS_UV_STAT 0x19
133 #define CANFD2_REG_ERR_STAT 0x1A
134 #define CANFD2_REG_WAKE_STAT 0x1B
135 
141 #define CANFD2_REG_SWK_STAT 0x1C
142 #define CANFD2_REG_SWK_ECNT_STAT 0x1D
143 
149 #define CANFD2_CTRL_MODE_SLEEP 0x01
150 #define CANFD2_CTRL_MODE_STANDBY 0x02
151 #define CANFD2_CTRL_MODE_REC_ONLY 0x04
152 #define CANFD2_CTRL_MODE_NORMAL_OP 0x08
153 
159 #define CANFD2_HW_CTRL_VBAT_CON 0x01
160 #define CANFD2_HW_CTRL_WAKE_TOG 0x02
161 #define CANFD2_HW_CTRL_LWU_POS 0x20
162 #define CANFD2_HW_CTRL_LWU_NEG 0x40
163 #define CANFD2_HW_CTRL_STTS_EN 0x80
164 
170 #define CANFD2_TXD_TO_1_4 0x01
171 #define CANFD2_TXD_TO_2_5 0x02
172 #define CANFD2_TXD_TO_5_10 0x03
173 #define CANFD2_TXD_TO_DISABLE 0x04
174 
180 #define CANFD2_SUP_CTRL_VCC_UV_T_100 0x01
181 #define CANFD2_SUP_CTRL_VCC_UV_T_200 0x02
182 #define CANFD2_SUP_CTRL_VCC_UV_T_300 0x03
183 #define CANFD2_SUP_CTRL_VCC_UV_T_400 0x04
184 #define CANFD2_SUP_CTRL_VCC_UV_T_500 0x05
185 #define CANFD2_SUP_CTRL_VCC_UV_T_600 0x06
186 #define CANFD2_SUP_CTRL_VCC_UV_T_700 0x07
187 #define CANFD2_SUP_CTRL_VCC_UV_T_800 0x08
188 #define CANFD2_SUP_CTRL_VCC_UV_T_900 0x09
189 #define CANFD2_SUP_CTRL_VCC_UV_T_1000 0x0A
190 #define CANFD2_SUP_CTRL_VCC_UV_T_1100 0x0B
191 #define CANFD2_SUP_CTRL_VCC_UV_T_1200 0x0C
192 #define CANFD2_SUP_CTRL_VCC_UV_T_1300 0x0D
193 #define CANFD2_SUP_CTRL_VCC_UV_T_1400 0x0E
194 #define CANFD2_SUP_CTRL_VCC_UV_T_1500 0x0F
195 
196 #define CANFD2_SUP_CTRL_VIO_UV_T_100 0x10
197 #define CANFD2_SUP_CTRL_VIO_UV_T_200 0x20
198 #define CANFD2_SUP_CTRL_VIO_UV_T_300 0x30
199 #define CANFD2_SUP_CTRL_VIO_UV_T_400 0x40
200 #define CANFD2_SUP_CTRL_VIO_UV_T_500 0x50
201 #define CANFD2_SUP_CTRL_VIO_UV_T_600 0x60
202 #define CANFD2_SUP_CTRL_VIO_UV_T_700 0x70
203 #define CANFD2_SUP_CTRL_VIO_UV_T_800 0x80
204 #define CANFD2_SUP_CTRL_VIO_UV_T_900 0x90
205 #define CANFD2_SUP_CTRL_VIO_UV_T_1000 0xA0
206 #define CANFD2_SUP_CTRL_VIO_UV_T_1100 0xB0
207 #define CANFD2_SUP_CTRL_VIO_UV_T_1200 0xC0
208 #define CANFD2_SUP_CTRL_VIO_UV_T_1300 0xD0
209 #define CANFD2_SUP_CTRL_VIO_UV_T_1400 0xE0
210 #define CANFD2_SUP_CTRL_VIO_UV_T_1500 0xF0
211 
217 #define CANFD2_SWK_CTRL_1_CFG_VAL 0x01
218 
224 #define CANFD2_SWK_CTRL_2_BR_125K 0x01
225 #define CANFD2_SWK_CTRL_2_BR_250K 0x02
226 #define CANFD2_SWK_CTRL_2_BR_500K 0x03
227 #define CANFD2_SWK_CTRL_2_BR_1M 0x04
228 #define CANFD2_SWK_CTRL_2_BR_RATIO_4 0x00
229 #define CANFD2_SWK_CTRL_2_BR_RATIO_10 0x10
230 #define CANFD2_SWK_CTRL_2_SWK_EN 0x80
231 
237 #define CANFD2_SWK_ID3_CTRL_IDE 0x20
238 
244 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_0 0x00
245 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_1 0x01
246 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_2 0x02
247 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_3 0x03
248 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_4 0x04
249 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_5 0x05
250 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_6 0x06
251 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_7 0x07
252 #define CANFD2_SWK_DLC_CTRL_DATA_BYTES_8 0x08
253 
259 #define CANFD2_TRANS_STAT_TSD 0x02
260 #define CANFD2_TRANS_STAT_TXD_TO 0x04
261 #define CANFD2_TRANS_STAT_POR 0x80
262 
268 #define CANFD2_TRANS_UV_STAT_VIO_STUV 0x01
269 #define CANFD2_TRANS_UV_STAT_VIO_LTUV 0x02
270 #define CANFD2_TRANS_UV_STAT_VCC_STUV 0x10
271 #define CANFD2_TRANS_UV_STAT_VCC_LTUV 0x20
272 #define CANFD2_TRANS_UV_STAT_VBAT_UV 0x80
273 
279 #define CANFD2_ERR_STAT_CMD_ERR 0x01
280 #define CANFD2_ERR_STAT_COM_ERR 0x02
281 
287 #define CANFD2_WAKE_STAT_WUF 0x01
288 #define CANFD2_WAKE_STAT_WUP 0x02
289 #define CANFD2_WAKE_STAT_LWU 0x04
290 #define CANFD2_WAKE_STAT_LWU_DIR 0x08
291 
297 #define CANFD2_SWK_STAT_SWK_ACT 0x01
298 #define CANFD2_SWK_STAT_CANSIL 0x02
299 #define CANFD2_SWK_STAT_CANTO 0x04
300 #define CANFD2_SWK_STAT_SYNC 0x08
301 #define CANFD2_SWK_STAT_SYSERR 0x10
302 
308 #define CANFD2_OP_MODE_SLEEP 0x01
309 #define CANFD2_OP_MODE_STANDBY 0x02
310 #define CANFD2_OP_MODE_RECEIVE_ONLY 0x03
311 #define CANFD2_OP_MODE_NORMAL 0x04
312 #define CANFD2_OP_MODE_UNKNOWN 0x05
313 
314 #define DUMMY 0
315 #define CANFD2_READ_CMD_MASK 0x7F
316 #define CANFD2_WRITE_CMD 0x80
317 
327 #define CANFD2_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
328 #define CANFD2_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
329 
330 /* \} */ // End group macro
331 // --------------------------------------------------------------- PUBLIC TYPES
339 typedef struct
340 {
341  // Output pins
342 
343  digital_out_t cs;
344 
345  // Modules
346 
347  uart_t uart;
348  spi_master_t spi;
349  pin_name_t chip_select;
350 
351  uint8_t uart_rx_buffer[ CANFD2_DRV_RX_BUFFER_SIZE ];
352  uint8_t uart_tx_buffer[ CANFD2_DRV_TX_BUFFER_SIZE ];
353 
354 } canfd2_t;
355 
359 typedef struct
360 {
361  // Communication gpio pins
362 
363  pin_name_t rx_pin;
364  pin_name_t tx_pin;
365  pin_name_t miso;
366  pin_name_t mosi;
367  pin_name_t sck;
368  pin_name_t cs;
369 
370  // static variable
371 
372  uint32_t baud_rate; // Clock speed.
374  uart_data_bits_t data_bit; // Data bits.
375  uart_parity_t parity_bit; // Parity bit.
376  uart_stop_bits_t stop_bit; // Stop bits.
377 
378  uint32_t spi_speed;
379  spi_master_mode_t spi_mode;
380  spi_master_chip_select_polarity_t cs_polarity;
381 
382 } canfd2_cfg_t;
383 
387 typedef uint8_t canfd2_error_t;
388  // End types group
390 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
391 
397 #ifdef __cplusplus
398 extern "C"{
399 #endif
400 
410 
419 err_t canfd2_init ( canfd2_t *ctx, canfd2_cfg_t *cfg );
420 
428 void canfd2_generic_write ( canfd2_t *ctx, uint8_t *data_buf, uint16_t len );
429 
439 int32_t canfd2_generic_read ( canfd2_t *ctx, uint8_t *data_buf, uint16_t max_len );
440 
452 void canfd2_generic_transfer ( canfd2_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len );
453 
465 uint8_t canfd2_read_data ( canfd2_t *ctx, uint8_t reg_addr );
466 
481 void canfd2_write_data ( canfd2_t *ctx, uint8_t reg_addr, uint8_t write_data );
482 
497 void canfd2_set_mode ( canfd2_t *ctx, uint8_t op_mode );
498 
514 uint8_t canfd2_get_mode ( canfd2_t *ctx );
515 
516 #ifdef __cplusplus
517 }
518 #endif
519 #endif // _CANFD2_H_
520  // End public_function group
523 
524 // ------------------------------------------------------------------------- END
canfd2_t
Click ctx object definition.
Definition: canfd2.h:340
canfd2_cfg_setup
void canfd2_cfg_setup(canfd2_cfg_t *cfg)
Config Object Initialization function.
canfd2_cfg_t::spi_mode
spi_master_mode_t spi_mode
Definition: canfd2.h:379
canfd2_cfg_t::cs
pin_name_t cs
Definition: canfd2.h:368
canfd2_t::chip_select
pin_name_t chip_select
Definition: canfd2.h:349
spi_specifics.h
This file contains SPI specific macros, functions, etc.
canfd2_cfg_t::tx_pin
pin_name_t tx_pin
Definition: canfd2.h:364
canfd2_cfg_t::baud_rate
uint32_t baud_rate
Definition: canfd2.h:372
canfd2_generic_write
void canfd2_generic_write(canfd2_t *ctx, uint8_t *data_buf, uint16_t len)
Generic write function.
canfd2_cfg_t::sck
pin_name_t sck
Definition: canfd2.h:367
canfd2_cfg_t::mosi
pin_name_t mosi
Definition: canfd2.h:366
canfd2_write_data
void canfd2_write_data(canfd2_t *ctx, uint8_t reg_addr, uint8_t write_data)
Generic write the byte of data function.
canfd2_error_t
uint8_t canfd2_error_t
Error type.
Definition: canfd2.h:387
canfd2_cfg_t::spi_speed
uint32_t spi_speed
Definition: canfd2.h:378
CANFD2_DRV_RX_BUFFER_SIZE
#define CANFD2_DRV_RX_BUFFER_SIZE
Definition: canfd2.h:89
canfd2_cfg_t
Click configuration structure definition.
Definition: canfd2.h:360
canfd2_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: canfd2.h:375
canfd2_t::cs
digital_out_t cs
Definition: canfd2.h:343
canfd2_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: canfd2.h:376
canfd2_cfg_t::rx_pin
pin_name_t rx_pin
Definition: canfd2.h:363
canfd2_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: canfd2.h:374
canfd2_generic_read
int32_t canfd2_generic_read(canfd2_t *ctx, uint8_t *data_buf, uint16_t max_len)
Generic read function.
canfd2_cfg_t::uart_blocking
bool uart_blocking
Definition: canfd2.h:373
canfd2_cfg_t::cs_polarity
spi_master_chip_select_polarity_t cs_polarity
Definition: canfd2.h:380
canfd2_t::spi
spi_master_t spi
Definition: canfd2.h:348
CANFD2_DRV_TX_BUFFER_SIZE
#define CANFD2_DRV_TX_BUFFER_SIZE
Definition: canfd2.h:88
canfd2_init
err_t canfd2_init(canfd2_t *ctx, canfd2_cfg_t *cfg)
Initialization function.
canfd2_cfg_t::miso
pin_name_t miso
Definition: canfd2.h:365
canfd2_set_mode
void canfd2_set_mode(canfd2_t *ctx, uint8_t op_mode)
Set operating mode function.
canfd2_read_data
uint8_t canfd2_read_data(canfd2_t *ctx, uint8_t reg_addr)
Generic read the byte of data function.
canfd2_t::uart
uart_t uart
Definition: canfd2.h:347
canfd2_get_mode
uint8_t canfd2_get_mode(canfd2_t *ctx)
Get operating mode function.
canfd2_generic_transfer
void canfd2_generic_transfer(canfd2_t *ctx, uint8_t *wr_buf, uint16_t wr_len, uint8_t *rd_buf, uint16_t rd_len)
Generic transfer function.