From 4a637504dd42e93243782a378260f2ac6c56b368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Mon, 17 Feb 2020 14:17:18 +0100 Subject: [PATCH] sensor_sdm120: minor, put spaces around operators --- lib/sensor_sdm120.c | 61 ++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 31 deletions(-) diff --git a/lib/sensor_sdm120.c b/lib/sensor_sdm120.c index 28f54d3..f5eb97c 100644 --- a/lib/sensor_sdm120.c +++ b/lib/sensor_sdm120.c @@ -110,11 +110,11 @@ static const uint16_t register_holding[] = { static uint16_t crc_modbus(uint8_t* buffer, uint8_t size) { uint16_t crc = 0xffff; // initial value (for ModBus) - for (uint8_t i=0; i>1)^0xA001; // // shift to the right (for the next bit) and XOR with (reverse) polynomial + for (uint8_t b = 0; b < 8; b++) { // go through every bit + if (crc & 0x0001) { // least significant bit is set (we are using the reverse way) + crc = (crc >> 1) ^ 0xA001; // // shift to the right (for the next bit) and XOR with (reverse) polynomial } else { crc >>= 1; // just shift right (for the next bit) } @@ -155,10 +155,10 @@ void sensor_sdm120_setup(uint32_t baudrate) rcc_periph_reset_pulse(RST_TIM(SENSOR_SDM120_TIMER)); // reset timer state timer_set_mode(TIM(SENSOR_SDM120_TIMER), TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP); // set timer mode, use undivided timer clock,edge alignment (simple count), and count up timer_one_shot_mode(TIM(SENSOR_SDM120_TIMER)); // stop counter after update event (we only need to count down once) - timer_set_prescaler(TIM(SENSOR_SDM120_TIMER), 66-1); // set the prescaler so this 16 bits timer allows to wait for 60 ms ( 1/(72E6/66/(2**16))=60.07ms ) - timeout_times[TIMEOUT_BEGIN] = (rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1))/baudrate/8/2.5; // wait at least 2.5 characters before sending data - timeout_times[TIMEOUT_END] = (rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1))/baudrate/8/2.5; // wait at least 2.5 characters after sending data - timeout_times[TIMEOUT_BETWEEN] = 0.06*(rcc_ahb_frequency/(TIM_PSC(TIM(SENSOR_SDM120_TIMER))+1)); // wait at least 60 ms before sending the next message + timer_set_prescaler(TIM(SENSOR_SDM120_TIMER), 66 - 1); // set the prescaler so this 16 bits timer allows to wait for 60 ms ( 1/(72E6/66/(2**16))=60.07ms ) + timeout_times[TIMEOUT_BEGIN] = (rcc_ahb_frequency / (TIM_PSC(TIM(SENSOR_SDM120_TIMER)) + 1)) / baudrate / 8 / 2.5; // wait at least 2.5 characters before sending data + timeout_times[TIMEOUT_END] = (rcc_ahb_frequency / (TIM_PSC(TIM(SENSOR_SDM120_TIMER)) + 1)) / baudrate / 8 / 2.5; // wait at least 2.5 characters after sending data + timeout_times[TIMEOUT_BETWEEN] = 0.06 * (rcc_ahb_frequency / (TIM_PSC(TIM(SENSOR_SDM120_TIMER)) + 1)); // wait at least 60 ms before sending the next message timer_clear_flag(TIM(SENSOR_SDM120_TIMER), TIM_SR_UIF); // clear flag timer_enable_irq(TIM(SENSOR_SDM120_TIMER), TIM_DIER_UIE); // enable update interrupt for timer nvic_enable_irq(NVIC_TIM_IRQ(SENSOR_SDM120_TIMER)); // catch interrupt in service routine @@ -178,13 +178,13 @@ void sensor_sdm120_setup(uint32_t baudrate) */ static bool sensor_sdm120_transmit_request(uint8_t meter_id, uint8_t function, uint16_t address, float value) { - if (meter_id==0) { // broadcast request are not supported + if (0 == meter_id) { // broadcast request are not supported return false; } - if (function!=0x03 && function!=0x04 && function!=0x10) { // function not supported + if (function != 0x03 && function != 0x04 && function != 0x10) { // function not supported return false; } - if (address%2) { // even register addresses are not supported by device + if (address % 2) { // even register addresses are not supported by device return false; } while (tx_used) { // transmission is ongoing @@ -195,13 +195,13 @@ static bool sensor_sdm120_transmit_request(uint8_t meter_id, uint8_t function, u uint8_t packet_size = 0; // ModBus message size (without error check) packet[0] = meter_id; // set slave device address packet[1] = function; // set function - packet[2] = address>>8; // set high register address + packet[2] = address >> 8; // set high register address packet[3] = address; // set low register address packet[4] = 0; // set high number of registers to read packet[5] = 2; // set low number of register to read (the measurement are encoded using 32 bits IEE745 float, and register hold 16 bits, thus we want to read 2 registers - if (function==0x03 || function==0x04) { // read register + if (function == 0x03 || function == 0x04) { // read register packet_size = 6; // set message size - } else if (function==0x10) { // write register + } else if (function == 0x10) { // write register packet[6] = 4; // byte count (writing two 16 bits registers) // store little endian encoded value in big endian encoded data uint8_t* data = (uint8_t*)&value; @@ -212,12 +212,12 @@ static bool sensor_sdm120_transmit_request(uint8_t meter_id, uint8_t function, u packet_size = 11; // set message size } uint16_t crc = crc_modbus(packet, packet_size); // compute error check - for (uint8_t i=0; i>8; // set high error check - tx_used = packet_size+2; // set request size + tx_buffer[0] = crc >> 8; // set high error check + tx_used = packet_size + 2; // set request size rx_used = 0; // reset reset buffer sensor_sdm120_measurement_received = false; // reset measurement flag while (TIM_CR1(TIM(SENSOR_SDM120_TIMER))&TIM_CR1_CEN) { // timer is already used @@ -235,7 +235,7 @@ static bool sensor_sdm120_transmit_request(uint8_t meter_id, uint8_t function, u bool sensor_sdm120_measurement_request(uint8_t meter_id, enum sensor_sdm120_measurement_type_t type) { - if (type>=SENSOR_SDM120_MEASUREMENT_MAX) { // invalid type + if (type >= SENSOR_SDM120_MEASUREMENT_MAX) { // invalid type return false; } return sensor_sdm120_transmit_request(meter_id, 0x04, register_input[type], 0); @@ -243,7 +243,7 @@ bool sensor_sdm120_measurement_request(uint8_t meter_id, enum sensor_sdm120_meas bool sensor_sdm120_configuration_request(uint8_t meter_id, enum sensor_sdm120_configuration_type_t type) { - if (type>=SENSOR_SDM120_CONFIGURATION_MAX) { // invalid type + if (type >= SENSOR_SDM120_CONFIGURATION_MAX) { // invalid type return false; } return sensor_sdm120_transmit_request(meter_id, 0x03, register_holding[type], 0); @@ -251,7 +251,7 @@ bool sensor_sdm120_configuration_request(uint8_t meter_id, enum sensor_sdm120_co bool sensor_sdm120_configuration_set(uint8_t meter_id, enum sensor_sdm120_configuration_type_t type, float value) { - if (type>=SENSOR_SDM120_CONFIGURATION_MAX) { // invalid type + if (type >= SENSOR_SDM120_CONFIGURATION_MAX) { // invalid type return false; } return sensor_sdm120_transmit_request(meter_id, 0x10, register_holding[type], value); @@ -265,19 +265,19 @@ float sensor_sdm120_measurement_decode(void) } else { sensor_sdm120_measurement_received = false; // reset flag } - if (rx_used<5) { // not a complete response (minimum is address, function, size/error, error check low, error check high) + if (rx_used < 5) { // not a complete response (minimum is address, function, size/error, error check low, error check high) return NAN; } // a complete message has been received if (crc_modbus(rx_buffer,rx_used)) { // checksum error, error check failed measurement = NAN; - } else if (rx_buffer[1]&0x80) { // error condition received + } else if (rx_buffer[1] & 0x80) { // error condition received measurement = INFINITY; // indicate we received and error } else { switch (rx_buffer[1]) { case 0x03: // read 4xxx holding register response received case 0x04: // read 3xxxx input register response received - if (rx_buffer[2]==0x04 && rx_used>=(4+5)) { // 2 registers received, corresponds to implemented request + if (rx_buffer[2] == 0x04 && rx_used >= (4 + 5)) { // 2 registers received, corresponds to implemented request // convert big endian received float value to little endian return value uint8_t* convert = (uint8_t*)&measurement; convert[0] = rx_buffer[6]; @@ -287,7 +287,7 @@ float sensor_sdm120_measurement_decode(void) } break; case 0x10: // write 4xxx holding register response received - measurement = (rx_buffer[4]<<8)+rx_buffer[5]; // number of registers written + measurement = (rx_buffer[4] << 8) + rx_buffer[5]; // number of registers written break; // not supported currently default: // unknown function response received measurement = INFINITY; @@ -323,13 +323,13 @@ void USART_ISR(SENSOR_SDM120_USART)(void) USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_RXNE; // clear flag, ignore received data } else if (rx_used=5 && (rx_buffer[1]&0x80)) { // error condition response received + } else if (rx_used >= 5 && (rx_buffer[1] & 0x80)) { // error condition response received sensor_sdm120_measurement_received = true; // notify used response has been received - } else if (rx_used>=5 && (uint8_t)(rx_used-5)>=rx_buffer[2] && (rx_buffer[1]==0x04 || rx_buffer[1]==0x03)) { // read input or holding register response received + } else if (rx_used >= 5 && (uint8_t)(rx_used - 5) >= rx_buffer[2] && (rx_buffer[1] == 0x04 || rx_buffer[1] == 0x03)) { // read input or holding register response received sensor_sdm120_measurement_received = true; // notify used response has been receive - } else if (rx_used>=8 && rx_buffer[1]==0x10) { // write holding register response received + } else if (rx_used >= 8 && rx_buffer[1] == 0x10) { // write holding register response received sensor_sdm120_measurement_received = true; // notify used response has been receive } } else { // buffer full and unknown response received @@ -352,7 +352,7 @@ void TIM_ISR(SENSOR_SDM120_TIMER)(void) case (TIMEOUT_BEGIN): // we can now send the data USART_SR(USART(SENSOR_SDM120_USART)) &= USART_SR_TXE; // clear interrupt flag usart_enable_tx_interrupt(USART(SENSOR_SDM120_USART)); // enable interrupt to send other bytes - usart_send(USART(SENSOR_SDM120_USART),tx_buffer[--tx_used]); // start transmission + usart_send(USART(SENSOR_SDM120_USART), tx_buffer[--tx_used]); // start transmission break; case (TIMEOUT_END): // we now have to wait before sending the next message gpio_clear(GPIO_PORT(SENSOR_SDM120_REDE_PIN), GPIO_PIN(SENSOR_SDM120_REDE_PIN)); // disable driver output (and enable receive output) @@ -367,4 +367,3 @@ void TIM_ISR(SENSOR_SDM120_TIMER)(void) } } } -