spiextend 2.0.0.0
spiextend.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 SPIEXTEND_H
29#define SPIEXTEND_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_spi_master.h"
52#include "spi_specifics.h"
53
74#define SPIEXTEND_REG_CONFIG 0x00
75#define SPIEXTEND_REG_STATUS 0x01
76#define SPIEXTEND_REG_EVENT 0x02
77#define SPIEXTEND_REG_INT_EN 0x03
78#define SPIEXTEND_REG_FAULT 0x04
79#define SPIEXTEND_REG_WORD_LENGTH 0x05
80#define SPIEXTEND_REG_SCRATCH 0x06
81
86#define SPIEXTEND_ACCEL14_REG_WHO_AM_I 0x0F
87#define SPIEXTEND_ACCEL14_REG_CTRL1_XL 0x10
88#define SPIEXTEND_ACCEL14_REG_CTRL3_C 0x12
89#define SPIEXTEND_ACCEL14_REG_OUT_TEMP_L 0x20
90#define SPIEXTEND_ACCEL14_REG_OUT_TEMP_H 0x21
91#define SPIEXTEND_ACCEL14_REG_OUTX_L_A 0x28
92#define SPIEXTEND_ACCEL14_REG_OUTX_H_A 0x29
93#define SPIEXTEND_ACCEL14_REG_OUTY_L_A 0x2A
94#define SPIEXTEND_ACCEL14_REG_OUTY_H_A 0x2B
95#define SPIEXTEND_ACCEL14_REG_OUTZ_L_A 0x2C
96#define SPIEXTEND_ACCEL14_REG_OUTZ_H_A 0x2D
97 // spiextend_reg
98
113#define SPIEXTEND_STATUS_ACTIVE 0x00
114#define SPIEXTEND_STATUS_INACTIVE 0x01
115
120#define SPIEXTEND_ACCEL14_CTRL1_XL_POWER_UP 0xA0
121#define SPIEXTEND_ACCEL14_CTRL1_XL_HIGH_RES_FS 0x00
122#define SPIEXTEND_ACCEL14_CTRL1_XL_GSEL_4G 0x08
123#define SPIEXTEND_ACCEL14_CTRL3_C_BOOT_NORMAL 0x00
124#define SPIEXTEND_ACCEL14_CTRL3_C_BDU_READ_UPDATE 0x40
125#define SPIEXTEND_ACCEL14_CTRL3_C_INT_ACTIVE_HIGH 0x00
126#define SPIEXTEND_ACCEL14_CTRL3_C_PP_OD_PUSH_PULL 0x00
127#define SPIEXTEND_ACCEL14_CTRL3_C_SIM_SPI_4_WIRE 0x00
128#define SPIEXTEND_ACCEL14_CTRL3_C_IF_INC_ENABLE 0x04
129#define SPIEXTEND_ACCEL14_CTRL3_C_SW_RESET_DIS 0x00
130
135#define SPIEXTEND_ACCEL14_SPI_WRITE 0x00
136#define SPIEXTEND_ACCEL14_SPI_READ 0x80
137
142#define SPIEXTEND_CMD_CLEAR_EVENT 0x00
143
148#define SPIEXTEND_SLAVE_SELECT_SS1 0x01
149#define SPIEXTEND_SLAVE_SELECT_SS2 0x02
150#define SPIEXTEND_SLAVE_SELECT_SS3 0x03
151
156#define SPIEXTEND_PIN_STATE_LOW 0x00
157#define SPIEXTEND_PIN_STATE_HIGH 0x01
158
163#define SPIEXTEND_SPI_WRITE 0x00
164#define SPIEXTEND_SPI_READ 0x01
165
174#define SPIEXTEND_SET_DATA_SAMPLE_EDGE SET_SPI_DATA_SAMPLE_EDGE
175#define SPIEXTEND_SET_DATA_SAMPLE_MIDDLE SET_SPI_DATA_SAMPLE_MIDDLE
176
177 // spiextend_set
178
193#define SPIEXTEND_MAP_MIKROBUS( cfg, mikrobus ) \
194 cfg.miso = MIKROBUS( mikrobus, MIKROBUS_MISO ); \
195 cfg.mosi = MIKROBUS( mikrobus, MIKROBUS_MOSI ); \
196 cfg.sck = MIKROBUS( mikrobus, MIKROBUS_SCK ); \
197 cfg.cs = MIKROBUS( mikrobus, MIKROBUS_CS ); \
198 cfg.ss3 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
199 cfg.ss2 = MIKROBUS( mikrobus, MIKROBUS_RST ); \
200 cfg.ssc = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
201 cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
202
203 // spiextend_map
204 // spiextend
205
210typedef struct
211{
212 // Output pins
213 digital_out_t ss3;
214 digital_out_t ss2;
215 digital_out_t ssc;
217 // Input pins
218 digital_in_t int_pin;
220 // Modules
221 spi_master_t spi;
223 pin_name_t chip_select;
226
231typedef struct
232{
233 // Communication gpio pins
234 pin_name_t miso;
235 pin_name_t mosi;
236 pin_name_t sck;
237 pin_name_t cs;
239 // Additional gpio pins
240 pin_name_t ss3;
241 pin_name_t ss2;
242 pin_name_t ssc;
243 pin_name_t int_pin;
245 // static variable
246 uint32_t spi_speed;
247 spi_master_mode_t spi_mode;
248 spi_master_chip_select_polarity_t cs_polarity;
251
256typedef struct
257{
258 uint8_t rem_ss3_pol;
259 uint8_t rem_ss3_pha;
260 uint8_t rem_ss2_pol;
261 uint8_t rem_ss2_pha;
262 uint8_t rem_ss1_pol;
263 uint8_t rem_ss1_pha;
264}
266
271typedef struct
272{
273 uint8_t nlink;
274 uint8_t nint;
275 uint8_t rmt_nint;
276 uint8_t speed_idx;
277}
279
284typedef struct
285{
286 uint8_t e_link_good;
287 uint8_t e_link_lost;
288 uint8_t e_fault;
289}
291
296typedef struct
297{
299 uint8_t link_fault;
303}
305
316
333
349
364
378err_t spiextend_generic_write ( spiextend_t *ctx, uint8_t reg, uint8_t data_in );
379
393err_t spiextend_generic_read ( spiextend_t *ctx, uint8_t reg, uint8_t *data_out );
394
405
416
427
439
449
460
471
481
491void spiextend_set_word_length ( spiextend_t *ctx, uint8_t word_length );
492
502
512void spiextend_set_scratch ( spiextend_t *ctx, uint8_t scratch );
513
523void spiextend_set_ss1 ( spiextend_t *ctx, uint8_t en_cs );
524
534void spiextend_set_ss2 ( spiextend_t *ctx, uint8_t en_rst );
535
545void spiextend_set_ss3 ( spiextend_t *ctx, uint8_t en_an );
546
557
568void spiextend_rmt_spi_write ( spiextend_t *ctx, uint8_t *p_tx_data, uint16_t n_bytes );
569
580void spiextend_rmt_spi_read ( spiextend_t *ctx, uint8_t *p_rx_data, uint16_t n_bytes );
581
593void spiextend_rmt_write ( spiextend_t *ctx, uint8_t reg, uint8_t tx_data, uint8_t sel_slave );
594
605uint8_t spiextend_rmt_read ( spiextend_t *ctx, uint8_t reg, uint8_t sel_slave );
606
619void spiextend_rmt_multi_write ( spiextend_t *ctx, uint8_t reg, uint8_t *p_tx_data, uint16_t n_bytes, uint8_t sel_slave );
620
633void spiextend_rmt_multi_read ( spiextend_t *ctx, uint8_t reg, uint8_t *p_rx_data, uint8_t n_bytes, uint8_t sel_slave );
634
635#ifdef __cplusplus
636}
637#endif
638#endif // SPIEXTEND_H
639
640 // spiextend
641
642// ------------------------------------------------------------------------ END
uint8_t spiextend_check_int(spiextend_t *ctx)
SPI Extend check interrupt status function.
void spiextend_rmt_write(spiextend_t *ctx, uint8_t reg, uint8_t tx_data, uint8_t sel_slave)
SPI Extend generic write data in Remote Mode function.
err_t spiextend_default_cfg(spiextend_t *ctx)
SPI Extend default configuration function.
void spiextend_cfg_setup(spiextend_cfg_t *cfg)
SPI Extend configuration object setup function.
void spiextend_rmt_spi_read(spiextend_t *ctx, uint8_t *p_rx_data, uint16_t n_bytes)
SPI Extend generic SPI read data in Remote Mode function.
void spiextend_get_event(spiextend_t *ctx, spiextend_event_data_t *event_data)
SPI Extend get the event function.
void spiextend_set_ss2(spiextend_t *ctx, uint8_t en_rst)
SPI Extend set SS2 pin state function.
void spiextend_set_ss3(spiextend_t *ctx, uint8_t en_an)
SPI Extend set SS3 pin state function.
err_t spiextend_generic_write(spiextend_t *ctx, uint8_t reg, uint8_t data_in)
SPI Extend data writing function.
uint8_t spiextend_get_word_length(spiextend_t *ctx)
SPI Extend get the word length function.
void spiextend_enable_int(spiextend_t *ctx, spiextend_event_data_t event_data)
SPI Extend enable int function.
void spiextend_set_scratch(spiextend_t *ctx, uint8_t scratch)
SPI Extend set scratch function.
void spiextend_rmt_multi_write(spiextend_t *ctx, uint8_t reg, uint8_t *p_tx_data, uint16_t n_bytes, uint8_t sel_slave)
SPI Extend generic multi write data in Remote Mode function.
uint8_t spiextend_rmt_read(spiextend_t *ctx, uint8_t reg, uint8_t sel_slave)
SPI Extend generic read data in Remote Mode function.
void spiextend_rmt_multi_read(spiextend_t *ctx, uint8_t reg, uint8_t *p_rx_data, uint8_t n_bytes, uint8_t sel_slave)
SPI Extend generic multi read data in Remote Mode function.
void spiextend_clear_event(spiextend_t *ctx)
SPI Extend clear the event function.
void spiextend_set_ss1(spiextend_t *ctx, uint8_t en_cs)
SPI Extend set SS1 pin state function.
void spiextend_get_status(spiextend_t *ctx, spiextend_status_data_t *status_data)
SPI Extend get the status function.
err_t spiextend_init(spiextend_t *ctx, spiextend_cfg_t *cfg)
SPI Extend initialization function.
void spiextend_set_config(spiextend_t *ctx, spiextend_config_data_t config_data)
SPI Extend set the configuration function.
void spiextend_get_config(spiextend_t *ctx, spiextend_config_data_t *config_data)
SPI Extend get the configuration function.
void spiextend_get_fault(spiextend_t *ctx, spiextend_fault_data_t *fault_data)
SPI Extend get the fault state function.
uint8_t spiextend_get_scratch(spiextend_t *ctx)
SPI Extend get scratch function.
err_t spiextend_generic_read(spiextend_t *ctx, uint8_t reg, uint8_t *data_out)
SPI Extend data reading function.
void spiextend_set_word_length(spiextend_t *ctx, uint8_t word_length)
SPI Extend set the word length function.
void spiextend_rmt_spi_write(spiextend_t *ctx, uint8_t *p_tx_data, uint16_t n_bytes)
SPI Extend generic SPI write data in Remote Mode function.
This file contains SPI specific macros, functions, etc.
spiextend_return_value_t
SPI Extend Click return value data.
Definition spiextend.h:311
@ SPIEXTEND_OK
Definition spiextend.h:312
@ SPIEXTEND_ERROR
Definition spiextend.h:313
SPI Extend Click configuration object.
Definition spiextend.h:232
pin_name_t ss3
Definition spiextend.h:240
spi_master_chip_select_polarity_t cs_polarity
Definition spiextend.h:248
pin_name_t sck
Definition spiextend.h:236
spi_master_mode_t spi_mode
Definition spiextend.h:247
pin_name_t mosi
Definition spiextend.h:235
uint32_t spi_speed
Definition spiextend.h:246
pin_name_t int_pin
Definition spiextend.h:243
pin_name_t ss2
Definition spiextend.h:241
pin_name_t miso
Definition spiextend.h:234
pin_name_t ssc
Definition spiextend.h:242
pin_name_t cs
Definition spiextend.h:237
SPI Extend Click configuration data.
Definition spiextend.h:257
uint8_t rem_ss2_pha
Definition spiextend.h:261
uint8_t rem_ss2_pol
Definition spiextend.h:260
uint8_t rem_ss3_pol
Definition spiextend.h:258
uint8_t rem_ss3_pha
Definition spiextend.h:259
uint8_t rem_ss1_pha
Definition spiextend.h:263
uint8_t rem_ss1_pol
Definition spiextend.h:262
SPI Extend Click event data.
Definition spiextend.h:285
uint8_t e_link_good
Definition spiextend.h:286
uint8_t e_fault
Definition spiextend.h:288
uint8_t e_link_lost
Definition spiextend.h:287
SPI Extend Click Fault data.
Definition spiextend.h:297
uint8_t rmt_spi_fault
Definition spiextend.h:300
uint8_t link_fault
Definition spiextend.h:299
uint8_t tx_buf_overflow
Definition spiextend.h:301
uint8_t spi_write_fault
Definition spiextend.h:298
uint8_t tx_buf_underflow
Definition spiextend.h:302
SPI Extend Click status data.
Definition spiextend.h:272
uint8_t speed_idx
Definition spiextend.h:276
uint8_t nlink
Definition spiextend.h:273
uint8_t nint
Definition spiextend.h:274
uint8_t rmt_nint
Definition spiextend.h:275
SPI Extend Click context object.
Definition spiextend.h:211
spi_master_t spi
Definition spiextend.h:221
digital_in_t int_pin
Definition spiextend.h:218
digital_out_t ssc
Definition spiextend.h:215
digital_out_t ss3
Definition spiextend.h:213
pin_name_t chip_select
Definition spiextend.h:223
digital_out_t ss2
Definition spiextend.h:214