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 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_uart.h"
48 
69 #define NPLC_CMD_COMMAND_MODE "+++"
70 #define NPLC_CMD_DATA_MODE "ATO"
71 #define NPLC_CMD_S100 "ATS100"
72 #define NPLC_CMD_S110 "ATS110"
73 #define NPLC_CMD_S111 "ATS111"
74 #define NPLC_CMD_S112 "ATS112"
75 #define NPLC_CMD_S113 "ATS113"
76 #define NPLC_CMD_S114 "ATS114"
77 #define NPLC_CMD_S300 "ATS300"
78 #define NPLC_CMD_S301 "ATS301"
79 #define NPLC_CMD_S302 "ATS302"
80 #define NPLC_CMD_S305 "ATS305"
81 #define NPLC_CMD_S306 "ATS306"
82 #define NPLC_CMD_FWI "AT+FWI"
83 #define NPLC_CMD_X "ATX"
84 #define NPLC_CMD_Z "ATZ"
85 #define NPLC_CMD_F "AT&F"
86 #define NPLC_CMD_W "AT&W"
87 #define NPLC_CMD_H "AT&H"
88 #define NPLC_CMD_V "AT&V"
94 #define NPLC_MODE_COMMAND 0
95 #define NPLC_MODE_DATA 1
96 
102 #define NPLC_DRV_BUFFER_SIZE 200
103  // nplc_cmd
105 
120 #define NPLC_MAP_MIKROBUS( cfg, mikrobus ) \
121  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
122  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
123  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_AN ); \
124  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
125  cfg.md1 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
126  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT );
127  // nplc_map // nplc
130 
135 typedef struct
136 {
137  // Output pins
138  digital_out_t cts;
139  digital_out_t rst;
140  digital_out_t md1;
142  // Input pins
143  digital_in_t rts;
145  // Modules
146  uart_t uart;
148  // Buffers
149  char uart_rx_buffer[ NPLC_DRV_BUFFER_SIZE ];
150  char uart_tx_buffer[ NPLC_DRV_BUFFER_SIZE ];
152 } nplc_t;
153 
158 typedef struct
159 {
160  // Communication gpio pins
161  pin_name_t rx_pin;
162  pin_name_t tx_pin;
164  // Additional gpio pins
165  pin_name_t cts;
166  pin_name_t rst;
167  pin_name_t md1;
168  pin_name_t rts;
170  // Static variable
171  uint32_t baud_rate;
173  uart_data_bits_t data_bit;
174  uart_parity_t parity_bit;
175  uart_stop_bits_t stop_bit;
177 } nplc_cfg_t;
178 
183 typedef enum
184 {
185  NPLC_OK = 0,
186  NPLC_ERROR = -1
187 
189 
206 
220 err_t nplc_init ( nplc_t *ctx, nplc_cfg_t *cfg );
221 
234 err_t nplc_generic_write ( nplc_t *ctx, char *data_in, uint16_t len );
235 
248 err_t nplc_generic_read ( nplc_t *ctx, char *data_out, uint16_t len );
249 
259 void nplc_set_rst_pin ( nplc_t *ctx, uint8_t state );
260 
270 void nplc_set_md1_pin ( nplc_t *ctx, uint8_t state );
271 
281 void nplc_set_cts_pin ( nplc_t *ctx, uint8_t state );
282 
291 uint8_t nplc_get_rts_pin ( nplc_t *ctx );
292 
301 void nplc_hw_reset ( nplc_t *ctx );
302 
311 void nplc_sw_reset ( nplc_t *ctx );
312 
322 
332 
343 void nplc_set_mode ( nplc_t *ctx, uint8_t mode );
344 
354 void nplc_send_cmd ( nplc_t *ctx, char *cmd );
355 
367 void nplc_write_s_register ( nplc_t *ctx, char *s_reg, char *param_buf );
368 
379 void nplc_read_s_register ( nplc_t *ctx, char *s_reg );
380 
381 #ifdef __cplusplus
382 }
383 #endif
384 #endif // NPLC_H
385  // nplc
387 
388 // ------------------------------------------------------------------------ END
nplc_cfg_t::uart_blocking
bool uart_blocking
Definition: nplc.h:172
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:159
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:184
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:143
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:166
nplc_cfg_t::md1
pin_name_t md1
Definition: nplc.h:167
nplc_t
N-PLC Click context object.
Definition: nplc.h:136
nplc_cfg_t::tx_pin
pin_name_t tx_pin
Definition: nplc.h:162
nplc_cfg_t::baud_rate
uint32_t baud_rate
Definition: nplc.h:171
NPLC_DRV_BUFFER_SIZE
#define NPLC_DRV_BUFFER_SIZE
N-PLC driver buffer size.
Definition: nplc.h:102
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:173
nplc_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: nplc.h:174
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:138
NPLC_OK
@ NPLC_OK
Definition: nplc.h:185
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:139
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:165
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:161
NPLC_ERROR
@ NPLC_ERROR
Definition: nplc.h:186
nplc_t::md1
digital_out_t md1
Definition: nplc.h:140
nplc_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: nplc.h:175
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:146
nplc_cfg_t::rts
pin_name_t rts
Definition: nplc.h:168