sensor_sdm120: minor, update GPIO definitions
This commit is contained in:
parent
4c6629d441
commit
011d12330c
|
@ -12,10 +12,10 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to query measurements from eastron SDM120-ModBus electricity meter (code)
|
||||
* @file sensor_sdm120.c
|
||||
/** library to query measurements from eastron SDM120-ModBus electricity meter
|
||||
* @file
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @date 2016-2020
|
||||
* @note peripherals used: USART @ref sensor_sdm120_usart , GPIO @ref sensor_sdm120_gpio , timer @ref sensor_sdm120_timer
|
||||
*/
|
||||
/* standard libraries */
|
||||
|
@ -44,8 +44,7 @@
|
|||
* @note driver output is enabled on high while receiver output is enabled on low, thus one pin can be used to control both
|
||||
* @{
|
||||
*/
|
||||
#define SENSOR_SDM120_REDE_PORT B /**< GPIO port for RS-485 receiver and driver output enable signal */
|
||||
#define SENSOR_SDM120_REDE_PIN 12 /**< GPIO pin for RS-485 receiver and driver output enable signal */
|
||||
#define SENSOR_SDM120_REDE_PIN PB12 /**< GPIO pin for RS-485 receiver and driver output enable signal */
|
||||
/** @} */
|
||||
|
||||
/** @defgroup sensor_sdm120_timer timer peripheral to enforce waiting time between messages
|
||||
|
@ -128,11 +127,11 @@ void sensor_sdm120_setup(uint32_t baudrate)
|
|||
{
|
||||
// enable USART I/O peripheral
|
||||
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (USART)
|
||||
rcc_periph_clock_enable(USART_PORT_RCC(SENSOR_SDM120_USART)); // enable clock for USART port peripheral
|
||||
rcc_periph_clock_enable(USART_RCC(SENSOR_SDM120_USART)); // enable clock for USART peripheral
|
||||
gpio_set_mode(USART_PORT(SENSOR_SDM120_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_PIN_TX(SENSOR_SDM120_USART)); // setup GPIO pin USART transmit
|
||||
gpio_set_mode(USART_PORT(SENSOR_SDM120_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_PIN_RX(SENSOR_SDM120_USART)); // setup GPIO pin USART receive
|
||||
gpio_clear(USART_PORT(SENSOR_SDM120_USART), USART_PIN_RX(SENSOR_SDM120_USART)); // pull down to avoid noise when not connected (it will be set low by RS485 chip when RO is enabled)
|
||||
rcc_periph_clock_enable(RCC_USART_PORT(SENSOR_SDM120_USART)); // enable clock for USART port peripheral
|
||||
rcc_periph_clock_enable(RCC_USART(SENSOR_SDM120_USART)); // enable clock for USART peripheral
|
||||
gpio_set_mode(USART_TX_PORT(SENSOR_SDM120_USART), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_TX_PIN(SENSOR_SDM120_USART)); // setup GPIO pin USART transmit
|
||||
gpio_set_mode(USART_RX_PORT(SENSOR_SDM120_USART), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_RX_PIN(SENSOR_SDM120_USART)); // setup GPIO pin USART receive
|
||||
gpio_clear(USART_RX_PORT(SENSOR_SDM120_USART), USART_RX_PIN(SENSOR_SDM120_USART)); // pull down to avoid noise when not connected (it will be set low by RS485 chip when RO is enabled)
|
||||
|
||||
// setup USART parameters for electricity meter
|
||||
usart_set_baudrate(USART(SENSOR_SDM120_USART), baudrate); // get baud rate by scrolling through the measurements on the electricity meter's screen (default 2400)
|
||||
|
@ -147,9 +146,9 @@ void sensor_sdm120_setup(uint32_t baudrate)
|
|||
usart_enable(USART(SENSOR_SDM120_USART)); // enable USART
|
||||
|
||||
// setup GPIO
|
||||
rcc_periph_clock_enable(RCC_GPIO(SENSOR_SDM120_REDE_PORT)); // enable clock for GPIO peripheral
|
||||
gpio_set_mode(GPIO(SENSOR_SDM120_REDE_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(SENSOR_SDM120_REDE_PIN)); // setup GPIO pin for receiver and driver output enable pin
|
||||
gpio_clear(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // disable driver output and enable receive output
|
||||
rcc_periph_clock_enable(GPIO_RCC(SENSOR_SDM120_REDE_PIN)); // enable clock for GPIO peripheral
|
||||
gpio_set_mode(GPIO_PORT(SENSOR_SDM120_REDE_PIN), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO_PIN(SENSOR_SDM120_REDE_PIN)); // setup GPIO pin for receiver and driver output enable pin
|
||||
gpio_clear(GPIO_PORT(SENSOR_SDM120_REDE_PIN),GPIO_PIN(SENSOR_SDM120_REDE_PIN)); // disable driver output and enable receive output
|
||||
|
||||
// setup timer to wait for minimal time before next transmission
|
||||
rcc_periph_clock_enable(RCC_TIM(SENSOR_SDM120_TIMER)); // enable clock for timer block
|
||||
|
@ -224,7 +223,7 @@ static bool sensor_sdm120_transmit_request(uint8_t meter_id, uint8_t function, u
|
|||
while (TIM_CR1(TIM(SENSOR_SDM120_TIMER))&TIM_CR1_CEN) { // timer is already used
|
||||
__WFI(); // wait until something happens (timer is available again)
|
||||
}
|
||||
gpio_set(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // enable driver output and disable receive output
|
||||
gpio_set(GPIO_PORT(SENSOR_SDM120_REDE_PIN), GPIO_PIN(SENSOR_SDM120_REDE_PIN)); // enable driver output and disable receive output
|
||||
// start timeout
|
||||
timeout = TIMEOUT_BEGIN; // select time before sending message
|
||||
timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
|
||||
|
@ -320,7 +319,7 @@ void USART_ISR(SENSOR_SDM120_USART)(void)
|
|||
timer_enable_counter(TIM(SENSOR_SDM120_TIMER)); // wait
|
||||
}
|
||||
if (usart_get_interrupt_source(USART(SENSOR_SDM120_USART), USART_SR_RXNE)) { // data has been received
|
||||
if (gpio_get(GPIO(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN))) { // not in receiver mode
|
||||
if (gpio_get(GPIO_PORT(SENSOR_SDM120_REDE_PIN),GPIO_PIN(SENSOR_SDM120_REDE_PIN))) { // not in receiver mode
|
||||
USART_SR(USART(SENSOR_SDM120_USART)) &= ~USART_SR_RXNE; // clear flag, ignore received data
|
||||
} else if (rx_used<LENGTH(rx_buffer)) { // receiving response
|
||||
rx_buffer[rx_used++] = usart_recv(USART(SENSOR_SDM120_USART)); // put received byte in buffer (clears flag)
|
||||
|
@ -356,7 +355,7 @@ void TIM_ISR(SENSOR_SDM120_TIMER)(void)
|
|||
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(SENSOR_SDM120_REDE_PORT),GPIO(SENSOR_SDM120_REDE_PIN)); // disable driver output (and enable receive output)
|
||||
gpio_clear(GPIO_PORT(SENSOR_SDM120_REDE_PIN), GPIO_PIN(SENSOR_SDM120_REDE_PIN)); // disable driver output (and enable receive output)
|
||||
timeout = TIMEOUT_BETWEEN; // select time between sending message
|
||||
timer_set_period(TIM(SENSOR_SDM120_TIMER), timeout_times[timeout]); // set corresponding timeout
|
||||
timer_set_counter(TIM(SENSOR_SDM120_TIMER), 0); // reset timer counter to get preset waiting time
|
||||
|
|
|
@ -12,10 +12,10 @@
|
|||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
/** library to query measurements from eastron SDM120-ModBus electricity meter (API)
|
||||
* @file sensor_sdm120.h
|
||||
/** library to query measurements from eastron SDM120-ModBus electricity meter
|
||||
* @file
|
||||
* @author King Kévin <kingkevin@cuvoodoo.info>
|
||||
* @date 2016
|
||||
* @date 2016-2020
|
||||
* @note peripherals used: USART @ref sensor_sdm120_usart , GPIO @ref sensor_sdm120_gpio , timer @ref sensor_sdm120_timer
|
||||
*/
|
||||
#pragma once
|
||||
|
|
Loading…
Reference in New Issue