nplc  2.1.0.0
nplc.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 NPLC_H
29 #define NPLC_H
30 
31 #ifdef __cplusplus
32 extern "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_uart.h"
52 
73 #define NPLC_CMD_COMMAND_MODE "+++"
74 #define NPLC_CMD_DATA_MODE "ATO"
75 #define NPLC_CMD_S100 "ATS100"
76 #define NPLC_CMD_S110 "ATS110"
77 #define NPLC_CMD_S111 "ATS111"
78 #define NPLC_CMD_S112 "ATS112"
79 #define NPLC_CMD_S113 "ATS113"
80 #define NPLC_CMD_S114 "ATS114"
81 #define NPLC_CMD_S300 "ATS300"
82 #define NPLC_CMD_S301 "ATS301"
83 #define NPLC_CMD_S302 "ATS302"
84 #define NPLC_CMD_S305 "ATS305"
85 #define NPLC_CMD_S306 "ATS306"
86 #define NPLC_CMD_FWI "AT+FWI"
87 #define NPLC_CMD_X "ATX"
88 #define NPLC_CMD_Z "ATZ"
89 #define NPLC_CMD_F "AT&F"
90 #define NPLC_CMD_W "AT&W"
91 #define NPLC_CMD_H "AT&H"
92 #define NPLC_CMD_V "AT&V"
98 #define NPLC_MODE_COMMAND 0
99 #define NPLC_MODE_DATA 1
100 
106 #define NPLC_DRV_BUFFER_SIZE 200
107  // nplc_cmd
109 
124 #define NPLC_MAP_MIKROBUS( cfg, mikrobus ) \
125  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
126  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
127  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_AN ); \
128  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
129  cfg.md1 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
130  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT );
131  // nplc_map // nplc
134 
139 typedef struct
140 {
141  // Output pins
142  digital_out_t cts;
143  digital_out_t rst;
144  digital_out_t md1;
146  // Input pins
147  digital_in_t rts;
149  // Modules
150  uart_t uart;
152  // Buffers
153  char uart_rx_buffer[ NPLC_DRV_BUFFER_SIZE ];
154  char uart_tx_buffer[ NPLC_DRV_BUFFER_SIZE ];
156 } nplc_t;
157 
162 typedef struct
163 {
164  // Communication gpio pins
165  pin_name_t rx_pin;
166  pin_name_t tx_pin;
168  // Additional gpio pins
169  pin_name_t cts;
170  pin_name_t rst;
171  pin_name_t md1;
172  pin_name_t rts;
174  // Static variable
175  uint32_t baud_rate;
177  uart_data_bits_t data_bit;
178  uart_parity_t parity_bit;
179  uart_stop_bits_t stop_bit;
181 } nplc_cfg_t;
182 
187 typedef enum
188 {
189  NPLC_OK = 0,
190  NPLC_ERROR = -1
191 
193 
210 
224 err_t nplc_init ( nplc_t *ctx, nplc_cfg_t *cfg );
225 
238 err_t nplc_generic_write ( nplc_t *ctx, char *data_in, uint16_t len );
239 
252 err_t nplc_generic_read ( nplc_t *ctx, char *data_out, uint16_t len );
253 
263 void nplc_set_rst_pin ( nplc_t *ctx, uint8_t state );
264 
274 void nplc_set_md1_pin ( nplc_t *ctx, uint8_t state );
275 
285 void nplc_set_cts_pin ( nplc_t *ctx, uint8_t state );
286 
295 uint8_t nplc_get_rts_pin ( nplc_t *ctx );
296 
305 void nplc_hw_reset ( nplc_t *ctx );
306 
315 void nplc_sw_reset ( nplc_t *ctx );
316 
326 
336 
347 void nplc_set_mode ( nplc_t *ctx, uint8_t mode );
348 
358 void nplc_send_cmd ( nplc_t *ctx, char *cmd );
359 
371 void nplc_write_s_register ( nplc_t *ctx, char *s_reg, char *param_buf );
372 
383 void nplc_read_s_register ( nplc_t *ctx, char *s_reg );
384 
385 #ifdef __cplusplus
386 }
387 #endif
388 #endif // NPLC_H
389  // nplc
391 
392 // ------------------------------------------------------------------------ END
nplc_cfg_t::uart_blocking
bool uart_blocking
Definition: nplc.h:176
nplc_sw_reset
void nplc_sw_reset(nplc_t *ctx)
N-PLC sw reset function.
nplc_cfg_t
N-PLC Click configuration object.
Definition: nplc.h:163
nplc_hw_reset
void nplc_hw_reset(nplc_t *ctx)
N-PLC hw reset function.
nplc_return_value_t
nplc_return_value_t
N-PLC Click return value data.
Definition: nplc.h:188
nplc_factory_reset
void nplc_factory_reset(nplc_t *ctx)
N-PLC factory reset function.
nplc_set_cts_pin
void nplc_set_cts_pin(nplc_t *ctx, uint8_t state)
N-PLC set cts pin function.
nplc_set_md1_pin
void nplc_set_md1_pin(nplc_t *ctx, uint8_t state)
N-PLC set md1 pin function.
nplc_t::rts
digital_in_t rts
Definition: nplc.h:147
nplc_set_mode
void nplc_set_mode(nplc_t *ctx, uint8_t mode)
N-PLC set mode function.
nplc_cfg_t::rst
pin_name_t rst
Definition: nplc.h:170
nplc_cfg_t::md1
pin_name_t md1
Definition: nplc.h:171
nplc_t
N-PLC Click context object.
Definition: nplc.h:140
nplc_cfg_t::tx_pin
pin_name_t tx_pin
Definition: nplc.h:166
nplc_cfg_t::baud_rate
uint32_t baud_rate
Definition: nplc.h:175
NPLC_DRV_BUFFER_SIZE
#define NPLC_DRV_BUFFER_SIZE
N-PLC driver buffer size.
Definition: nplc.h:106
nplc_init
err_t nplc_init(nplc_t *ctx, nplc_cfg_t *cfg)
N-PLC initialization function.
nplc_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: nplc.h:177
nplc_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: nplc.h:178
nplc_send_cmd
void nplc_send_cmd(nplc_t *ctx, char *cmd)
N-PLC send command function.
nplc_generic_read
err_t nplc_generic_read(nplc_t *ctx, char *data_out, uint16_t len)
N-PLC data reading function.
nplc_t::cts
digital_out_t cts
Definition: nplc.h:142
NPLC_OK
@ NPLC_OK
Definition: nplc.h:189
nplc_read_s_register
void nplc_read_s_register(nplc_t *ctx, char *s_reg)
N-PLC read s register function.
nplc_generic_write
err_t nplc_generic_write(nplc_t *ctx, char *data_in, uint16_t len)
N-PLC data writing function.
nplc_t::rst
digital_out_t rst
Definition: nplc.h:143
nplc_get_rts_pin
uint8_t nplc_get_rts_pin(nplc_t *ctx)
N-PLC get rts pin function.
nplc_cfg_t::cts
pin_name_t cts
Definition: nplc.h:169
nplc_write_s_register
void nplc_write_s_register(nplc_t *ctx, char *s_reg, char *param_buf)
N-PLC write s register function.
nplc_firmware_version
void nplc_firmware_version(nplc_t *ctx)
N-PLC firmware version function.
nplc_cfg_setup
void nplc_cfg_setup(nplc_cfg_t *cfg)
N-PLC configuration object setup function.
nplc_cfg_t::rx_pin
pin_name_t rx_pin
Definition: nplc.h:165
NPLC_ERROR
@ NPLC_ERROR
Definition: nplc.h:190
nplc_t::md1
digital_out_t md1
Definition: nplc.h:144
nplc_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: nplc.h:179
nplc_set_rst_pin
void nplc_set_rst_pin(nplc_t *ctx, uint8_t state)
N-PLC set rst pin function.
nplc_t::uart
uart_t uart
Definition: nplc.h:150
nplc_cfg_t::rts
pin_name_t rts
Definition: nplc.h:172