Skip to content

Commit

Permalink
Updated to portable types
Browse files Browse the repository at this point in the history
  • Loading branch information
jcomas committed Jan 18, 2022
1 parent 92b4414 commit b250fbd
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 25 deletions.
8 changes: 6 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
; PlatformIO Project Configuration File
;
;

[platformio]
src_dir = ./
Expand Down Expand Up @@ -30,7 +30,7 @@ board = nodemcuv2
build_flags =
${env.build_flags}
-DNODEMCUV2

[env:pico]
platform = raspberrypi
board = pico
Expand All @@ -47,3 +47,7 @@ board = nrf52840_dk_adafruit
[env:black_stm32f407ve]
platform = ststm32
board = black_f407ve

[env:pro16MHzatmega328]
platform = atmelavr
board = pro16MHzatmega328
14 changes: 8 additions & 6 deletions src/modbus_crc.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include <stdint.h>

/* ModBus CRC routine extracted from https://modbus.org/docs/Modbus_over_serial_line_V1_02.pdf */

/* Table of CRC values for high–order byte */
static unsigned char auchCRCHi[] = {
static const uint8_t auchCRCHi[] = {
0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81,
0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0,
0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01,
Expand All @@ -23,7 +25,7 @@ static unsigned char auchCRCHi[] = {
} ;

/* Table of CRC values for low–order byte */
static char auchCRCLo[] = {
static const uint8_t auchCRCLo[] = {
0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4,
0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09,
0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD,
Expand All @@ -45,14 +47,14 @@ static char auchCRCLo[] = {
};

/* The function returns the CRC as a unsigned short type */
unsigned short modbus_CRC16 (unsigned char *puchMsg, unsigned short usDataLen ) {
uint16_t modbus_CRC16 (uint8_t *puchMsg, uint16_t usDataLen ) {
/*
puchMsg -> message to calculate CRC upon
usDataLen -> quantity of bytes in message
*/
unsigned char uchCRCHi = 0xFF ; /* high byte of CRC initialized */
unsigned char uchCRCLo = 0xFF ; /* low byte of CRC initialized */
unsigned uIndex ; /* will index into CRC lookup table */
uint8_t uchCRCHi = 0xFF ; /* high byte of CRC initialized */
uint8_t uchCRCLo = 0xFF ; /* low byte of CRC initialized */
uint16_t uIndex ; /* will index into CRC lookup table */

while (usDataLen--) /* pass through message buffer */
{
Expand Down
2 changes: 1 addition & 1 deletion src/modbus_crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
/* The function returns the CRC as a unsigned short type
puchMsg -> message to calculate CRC upon
usDataLen -> quantity of bytes in message */
unsigned short modbus_CRC16 (unsigned char *puchMsg, unsigned short usDataLen );
uint16_t modbus_CRC16 (uint8_t *puchMsg, uint16_t usDataLen );
#endif
21 changes: 11 additions & 10 deletions src/s8_uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ bool S8_UART::clear_acknowledgement() {
LOG_DEBUG_INFO("Successful clearing acknowledgement flags");

} else {
LOG_DEBUG_ERROR("Error clearing acknowledgement flags!");
LOG_DEBUG_ERROR("Error clearing acknowledgement flags!");
}

return result;
Expand All @@ -205,13 +205,13 @@ bool S8_UART::clear_acknowledgement() {
/* Start a manual calibration (go to outdoors, wait 5 minutes o more and then you issue this command) */
bool S8_UART::manual_calibration() {
bool result = clear_acknowledgement();

if (result) {
send_special_command(S8_CO2_BACKGROUND_CALIBRATION);

if (result) {
LOG_DEBUG_INFO("Manual calibration in background has started");

} else {
LOG_DEBUG_ERROR("Error starting manual calibration!");
}
Expand Down Expand Up @@ -376,7 +376,7 @@ int32_t S8_UART::get_sensor_type_ID() {
if (valid_response_len(MODBUS_FUNC_READ_INPUT_REGISTERS, nb, 7)) {

// Save sensor type ID (high)
sensorType = ((buf_msg[4] << 16) & 0x00FF0000);
sensorType = (((int32_t)buf_msg[4] << 16) & 0x00FF0000);

// Ask sensor type ID (low)
send_cmd(MODBUS_FUNC_READ_INPUT_REGISTERS, MODBUS_IR27, 0x0001);
Expand Down Expand Up @@ -419,7 +419,7 @@ int32_t S8_UART::get_sensor_ID() {
if (valid_response_len(MODBUS_FUNC_READ_INPUT_REGISTERS, nb, 7)) {

// Save sensor ID (high)
sensorID = ((buf_msg[3] << 24) & 0xFF000000) | ((buf_msg[4] << 16) & 0x00FF0000);
sensorID = (((int32_t)buf_msg[3] << 24) & 0xFF000000) | (((int32_t)buf_msg[4] << 16) & 0x00FF0000);

// Ask sensor ID (low)
send_cmd(MODBUS_FUNC_READ_INPUT_REGISTERS, MODBUS_IR31, 0x0001);
Expand Down Expand Up @@ -477,6 +477,7 @@ bool S8_UART::valid_response_len(uint8_t func, uint8_t nb, uint8_t len) {

if (nb == len) {
result = valid_response(func, nb);

} else {
LOG_DEBUG_ERROR("Unexpected length!");
}
Expand Down Expand Up @@ -538,18 +539,18 @@ void S8_UART::send_cmd( uint8_t func, uint16_t reg, uint16_t value) {
/* Send bytes to sensor */
void S8_UART::serial_write_bytes(uint8_t size) {

LOG_DEBUG_VERBOSE_PACKET("Bytes to send: ", (char *)buf_msg, size);
LOG_DEBUG_VERBOSE_PACKET("Bytes to send: ", (uint8_t *)buf_msg, size);

mySerial->write(buf_msg, size);
mySerial->flush();
}


/* Read answer of sensor */
uint8_t S8_UART::serial_read_bytes(uint8_t max_bytes, unsigned long timeout_ms) {
uint8_t S8_UART::serial_read_bytes(uint8_t max_bytes, uint32_t timeout_ms) {

unsigned long start_t = millis();
unsigned long end_t = start_t;
uint32_t start_t = millis();
uint32_t end_t = start_t;
bool readed = false;

uint8_t nb = 0;
Expand All @@ -565,7 +566,7 @@ uint8_t S8_UART::serial_read_bytes(uint8_t max_bytes, unsigned long timeout_ms)

if (readed) {
if (nb > 0) {
LOG_DEBUG_VERBOSE_PACKET("Bytes received: ", (char *)buf_msg, nb);
LOG_DEBUG_VERBOSE_PACKET("Bytes received: ", (uint8_t *)buf_msg, nb);

} else {
LOG_DEBUG_ERROR("Unexpected reading serial port!");
Expand Down
11 changes: 5 additions & 6 deletions src/s8_uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@


struct S8_sensor {
char firm_version[S8_LEN_FIRMVER + 1];
char firm_version[S8_LEN_FIRMVER + 1];
int16_t co2;
int16_t abc_period;
int16_t ack;
Expand Down Expand Up @@ -147,8 +147,8 @@
bool set_ABC_period(int16_t period); // Set ABC period (4 - 4800 hours, 0 to disable)

/* Manual calibration */
bool manual_calibration(); // Start a manual calibration (it clears acknowledgement flags and it calls to
// send_special_command with background calibration command)
bool manual_calibration(); // Start a manual calibration (it clears acknowledgement flags and it calls to
// send_special_command with background calibration command)
// (go to outdoors, wait 5 minutes o more and then you call this command)

/* Bits information */
Expand All @@ -163,16 +163,15 @@


private:
Stream* mySerial; // Communication serial with the sensor
Stream* mySerial; // Serial communication with the sensor
uint8_t buf_msg[S8_LEN_BUF_MSG]; // Buffer for communication messages with the sensor

void serial_write_bytes(uint8_t size); // Send bytes to sensor
uint8_t serial_read_bytes(uint8_t max_bytes, unsigned long timeout_seconds); // Read received bytes from sensor
uint8_t serial_read_bytes(uint8_t max_bytes, uint32_t timeout_seconds); // Read received bytes from sensor
bool valid_response(uint8_t func, uint8_t nb); // Check if response is valid according to sent command
bool valid_response_len(uint8_t func, uint8_t nb, uint8_t len); // Check if response is valid according to sent command and checking expected total length
void send_cmd(uint8_t func, uint16_t reg, uint16_t value); // Send command


};

#endif

0 comments on commit b250fbd

Please sign in to comment.