ph 2.0.0.0
ph.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 PH_H
29#define PH_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_uart.h"
52
73#define PH_CMD_CNG_BAUD_RATE "Baud,"
74#define PH_CMD_CHK_BAUD_RATE "Baud,?"
75#define PH_CMD_SET_CONT_READ "C,"
76#define PH_CMD_CHK_CONT_READ "C,?"
77#define PH_CMD_CALIB_PERF "Cal"
78#define PH_CMD_CALIB_MID ",mid,"
79#define PH_CMD_CALIB_LOW ",low,"
80#define PH_CMD_CALIB_HIGH ",high,"
81#define PH_CMD_CHK_CALIB "Cal,?"
82#define PH_CMD_CALIB_EXP "Export"
83#define PH_CMD_CHK_CALIB_EXP "Export,?"
84#define PH_CMD_CALIB_IMP "Import,"
85#define PH_CMD_EN_FTY_RST "Factory"
86#define PH_CMD_DEV_FIND "Find"
87#define PH_CMD_DEV_INFO "i"
88#define PH_CMD_CNG_I2C "I2C,"
89#define PH_CMD_EN_LED "L,"
90#define PH_CMD_CHK_LED "L,?"
91#define PH_CMD_SET_DEV_NAME "Name,"
92#define PH_CMD_GET_DEV_NAME "Name,?"
93#define PH_CMD_PROTOCOL_LOCK "Plock,"
94#define PH_CMD_PROTOCOL_CKH "Plock,?"
95#define PH_CMD_SET_SNGL_READ "r"
96#define PH_CMD_ENTER_SLEEP "Sleep"
97#define PH_CMD_RET_SLOPE "Slope,?"
98#define PH_CMD_READ_STATUS "Status"
99#define PH_CMD_TEMP_COMP "T,"
100#define PH_CMD_CHK_TEMP_COMP "T,?"
101#define PH_CMD_RSP_CODES "*OK"
102#define PH_CMD_EN_RSP_CODES "*OK,1"
103#define PH_CMD_DIS_RSP_CODES "*OK,0"
104#define PH_CMD_CHK_RSP_CODES "*OK,?"
105#define PH_CMD_END_COMMAND "\r"
106
111#define PH_RSP_CODE_ERROR "*ER"
112#define PH_RSP_CODE_OV_VOLT "*OV"
113#define PH_RSP_CODE_UN_VOLT "*UV"
114#define PH_RSP_CODE_RESET "*RS"
115#define PH_RSP_CODE_READY "*RE"
116#define PH_RSP_CODE_SLEEP_EN "*SL"
117#define PH_RSP_CODE_WAKE_UP "*WA"
118#define PH_RSP_CODE_ALL_GOOD "*OK"
119// Restart codes list
120
125#define PH_RST_CODE_POW_OFF "P"
126#define PH_RST_CODE_SW_RST "S"
127#define PH_RST_CODE_BRWN_OUT "B"
128#define PH_RST_CODE_WTD "W"
129#define PH_RST_CODE_UNKNOWN "U"
130
135#define PH_BAUD_RATE_300 "300"
136#define PH_BAUD_RATE_1200 "1200"
137#define PH_BAUD_RATE_2400 "2400"
138#define PH_BAUD_RATE_9600 "9600"
139#define PH_BAUD_RATE_19200 "19200"
140#define PH_BAUD_RATE_38400 "38400"
141#define PH_BAUD_RATE_57600 "57600"
142#define PH_BAUD_RATE_115200 "115200"
143
149#define DRV_BUFFER_SIZE 200
150
151 // ph_cmd
152
167#define PH_MAP_MIKROBUS( cfg, mikrobus ) \
168 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
169 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX )
170 // ph_map
171 // ph
172
177typedef struct
178{
179 // Modules
180 uart_t uart;
182 // Buffers
183 char uart_rx_buffer[ DRV_BUFFER_SIZE ];
184 char uart_tx_buffer[ DRV_BUFFER_SIZE ];
186} ph_t;
187
192typedef struct
193{
194 // Communication gpio pins
195 pin_name_t rx_pin;
196 pin_name_t tx_pin;
198 // Static variable
199 uint32_t baud_rate;
201 uart_data_bits_t data_bit;
202 uart_parity_t parity_bit;
203 uart_stop_bits_t stop_bit;
205} ph_cfg_t;
206
211typedef enum
212{
213 PH_OK = 0,
214 PH_ERROR = -1
215
217
233void ph_cfg_setup ( ph_cfg_t *cfg );
234
248err_t ph_init ( ph_t *ctx, ph_cfg_t *cfg );
249
263err_t ph_default_cfg ( ph_t *ctx );
264
277err_t ph_generic_write ( ph_t *ctx, uint8_t *data_buf, uint16_t len );
278
291err_t ph_generic_read ( ph_t *ctx, uint8_t *data_buf, uint16_t max_len );
292
301void ph_uart_write ( ph_t *ctx, uint8_t input );
302
312void ph_uart_read_until ( ph_t *ctx, char *p_rx_data, char *delim );
313
322void ph_send_cmd ( ph_t *ctx, char *p_cmd );
323
333void ph_send_cmd_resp ( ph_t *ctx, char *p_cmd, char *p_resp );
334
346void ph_send_cmd_w_num ( ph_t *ctx, char *p_cmd, char *flt_val, char *p_resp );
347
357void ph_get_cmd_resp ( ph_t *ctx, char *p_cmd, char *p_resp );
358
368void ph_switch_led ( ph_t *ctx, uint8_t state, char *p_resp );
369
378void ph_chk_led_state ( ph_t *ctx, char *p_resp );
379
390void ph_find_dev ( ph_t *ctx, char *p_resp );
391
403void ph_cont_read ( ph_t *ctx, uint8_t time, char *p_resp );
404
413void ph_chk_cont_read ( ph_t *ctx, char *p_resp );
414
429void ph_perf_calib ( ph_t *ctx, char *point, float flt_val, char *p_resp );
430
444void ph_chk_calib ( ph_t *ctx, char *p_resp );
445
454void ph_chk_slope ( ph_t *ctx, char *p_resp );
455
466void ph_get_calib_exp_bytes_num ( ph_t *ctx, char *p_resp );
467
479void ph_import_calib ( ph_t *ctx, uint8_t *in_calib, char *p_resp );
480
489void ph_factory_rst ( ph_t *ctx, char *p_resp );
490
503void ph_set_temp_comp ( ph_t *ctx, float flt_val, char *p_resp );
504
515void ph_get_temp_comp ( ph_t *ctx, char *p_resp );
516
528void ph_set_name ( ph_t *ctx, char *p_dev_name, char *p_resp );
529
538void ph_show_name ( ph_t *ctx, char *p_dev_name );
539
548void ph_get_dev_info ( ph_t *ctx, char *p_resp );
549
558void ph_check_status ( ph_t *ctx, char *p_resp );
559
567void ph_sleep ( ph_t *ctx );
568
580void ph_chng_baud_rate ( ph_t *ctx, char *baud_rate, char *p_resp );
581
592void ph_chk_baud_rate ( ph_t *ctx, char *p_resp );
593
603void ph_protocol_lock ( ph_t *ctx, uint8_t state, char *p_resp );
604
613void ph_check_protocol_lock ( ph_t *ctx, char *p_resp );
614
626void ph_chng_i2c_adr ( ph_t *ctx, uint32_t i2c_adr, char *p_resp );
627
636void ph_single_read ( ph_t *ctx, char *p_resp );
637
646uint8_t ph_response ( ph_t *ctx, char *p_resp );
647
654void ph_clr_log_buf ( char *p_str );
655
656#ifdef __cplusplus
657}
658#endif
659#endif // PH_H
660
661 // ph
662
663// ------------------------------------------------------------------------ END
#define DRV_BUFFER_SIZE
pH driver buffer size.
Definition ph.h:149
void ph_cfg_setup(ph_cfg_t *cfg)
pH configuration object setup function.
err_t ph_generic_write(ph_t *ctx, uint8_t *data_buf, uint16_t len)
pH data writing function.
void ph_cont_read(ph_t *ctx, uint8_t time, char *p_resp)
Set Continous read function.
void ph_uart_read_until(ph_t *ctx, char *p_rx_data, char *delim)
UART read until delimiter function.
void ph_uart_write(ph_t *ctx, uint8_t input)
UART Write Single Byte function.
void ph_get_temp_comp(ph_t *ctx, char *p_resp)
Get Temperature Compensation function.
void ph_get_cmd_resp(ph_t *ctx, char *p_cmd, char *p_resp)
Send get response function.
void ph_perf_calib(ph_t *ctx, char *point, float flt_val, char *p_resp)
Perform Calibration function.
err_t ph_default_cfg(ph_t *ctx)
pH default configuration function.
void ph_get_dev_info(ph_t *ctx, char *p_resp)
Get Device Information function.
void ph_import_calib(ph_t *ctx, uint8_t *in_calib, char *p_resp)
Import calibration data function.
void ph_chng_i2c_adr(ph_t *ctx, uint32_t i2c_adr, char *p_resp)
Change Device's I2C slave address function.
err_t ph_generic_read(ph_t *ctx, uint8_t *data_buf, uint16_t max_len)
pH data reading function.
void ph_factory_rst(ph_t *ctx, char *p_resp)
Perform Factory Reset function.
err_t ph_init(ph_t *ctx, ph_cfg_t *cfg)
pH initialization function.
void ph_send_cmd(ph_t *ctx, char *p_cmd)
Send command function.
void ph_single_read(ph_t *ctx, char *p_resp)
Perform single read function.
void ph_set_name(ph_t *ctx, char *p_dev_name, char *p_resp)
Set name function.
void ph_switch_led(ph_t *ctx, uint8_t state, char *p_resp)
Toggle LED function.
void ph_chk_cont_read(ph_t *ctx, char *p_resp)
Check Continous read settings function.
void ph_chng_baud_rate(ph_t *ctx, char *baud_rate, char *p_resp)
Change Baud Rate function.
uint8_t ph_response(ph_t *ctx, char *p_resp)
pH Response handler function
void ph_chk_baud_rate(ph_t *ctx, char *p_resp)
Check Baud Rate applied function.
void ph_chk_led_state(ph_t *ctx, char *p_resp)
Check LED state function.
void ph_set_temp_comp(ph_t *ctx, float flt_val, char *p_resp)
Set Temperature Compensation function.
void ph_send_cmd_w_num(ph_t *ctx, char *p_cmd, char *flt_val, char *p_resp)
Send command with number and get response function.
void ph_send_cmd_resp(ph_t *ctx, char *p_cmd, char *p_resp)
Send command and get response function.
void ph_sleep(ph_t *ctx)
Enter Sleep mode function.
void ph_check_status(ph_t *ctx, char *p_resp)
Check Device Status function.
void ph_get_calib_exp_bytes_num(ph_t *ctx, char *p_resp)
Export calibration data function.
void ph_chk_calib(ph_t *ctx, char *p_resp)
Check if Calibration was performed function.
void ph_protocol_lock(ph_t *ctx, uint8_t state, char *p_resp)
Protocol Lock enable/disable function.
void ph_clr_log_buf(char *p_str)
Clear log buffer function.
void ph_check_protocol_lock(ph_t *ctx, char *p_resp)
Check Protocol Lock function.
void ph_chk_slope(ph_t *ctx, char *p_resp)
Check slope function.
void ph_show_name(ph_t *ctx, char *p_dev_name)
Show Device Name function.
void ph_find_dev(ph_t *ctx, char *p_resp)
Find device function.
ph_return_value_t
pH Click return value data.
Definition ph.h:212
@ PH_ERROR
Definition ph.h:214
@ PH_OK
Definition ph.h:213
pH Click configuration object.
Definition ph.h:193
uint32_t baud_rate
Definition ph.h:199
bool uart_blocking
Definition ph.h:200
uart_data_bits_t data_bit
Definition ph.h:201
pin_name_t tx_pin
Definition ph.h:196
pin_name_t rx_pin
Definition ph.h:195
uart_stop_bits_t stop_bit
Definition ph.h:203
uart_parity_t parity_bit
Definition ph.h:202
pH Click context object.
Definition ph.h:178
uart_t uart
Definition ph.h:180