mirror of
https://gitea.ecohim.ru:3000/RS485_Relay/RS485_Relay2_fw.git
synced 2025-08-06 16:07:17 +03:00
initial commit
This commit is contained in:
159
Core/Src/board_logic.c
Normal file
159
Core/Src/board_logic.c
Normal file
@@ -0,0 +1,159 @@
|
||||
/*
|
||||
* board_logic.c
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#include "main.h"
|
||||
#include "modbus_logic.h"
|
||||
#include "board_logic.h"
|
||||
|
||||
#define BOARD_DESC_LEN (23)
|
||||
|
||||
static const char board_description[BOARD_DESC_LEN] = "RS485 Relay Module v1";
|
||||
extern uint16_t tranfer_errors_count;
|
||||
extern uint16_t last_rx_time;
|
||||
|
||||
#define REL_MAIN_ON HAL_GPIO_WritePin(RL_MAIN_GPIO_Port, RL_MAIN_Pin, GPIO_PIN_SET)
|
||||
#define REL_MAIN_OFF HAL_GPIO_WritePin(RL_MAIN_GPIO_Port, RL_MAIN_Pin, GPIO_PIN_RESET)
|
||||
#define REL_AUX_ON HAL_GPIO_WritePin(RL_AUX_GPIO_Port, RL_AUX_Pin, GPIO_PIN_SET)
|
||||
#define REL_AUX_OFF HAL_GPIO_WritePin(RL_AUX_GPIO_Port, RL_AUX_Pin, GPIO_PIN_RESET)
|
||||
#define LED_MAIN_ON HAL_GPIO_WritePin(LED_MAIN_GPIO_Port, LED_MAIN_Pin, GPIO_PIN_SET)
|
||||
#define LED_MAIN_OFF HAL_GPIO_WritePin(LED_MAIN_GPIO_Port, LED_MAIN_Pin, GPIO_PIN_RESET)
|
||||
#define LED_AUX_ON HAL_GPIO_WritePin(LED_AUX_GPIO_Port, LED_AUX_Pin, GPIO_PIN_SET)
|
||||
#define LED_AUX_OFF HAL_GPIO_WritePin(LED_AUX_GPIO_Port, LED_AUX_Pin, GPIO_PIN_RESET)
|
||||
#define LED_ACT_ON HAL_GPIO_WritePin(LED_ACT_GPIO_Port, LED_ACT_Pin, GPIO_PIN_SET)
|
||||
#define LED_ACT_OFF HAL_GPIO_WritePin(LED_ACT_GPIO_Port, LED_ACT_Pin, GPIO_PIN_RESET)
|
||||
|
||||
uint16_t relays=0;
|
||||
uint16_t motor_pwm=0;
|
||||
uint16_t led_time_act=0;
|
||||
|
||||
void update_service_indication(void);
|
||||
|
||||
void loop_iterate()
|
||||
{
|
||||
|
||||
if(relays&REL_MAIN_BIT)
|
||||
{
|
||||
REL_MAIN_ON;
|
||||
LED_MAIN_ON;
|
||||
}
|
||||
else
|
||||
{
|
||||
REL_MAIN_OFF;
|
||||
LED_MAIN_OFF;
|
||||
}
|
||||
if(relays&REL_AUX_BIT)
|
||||
{
|
||||
REL_AUX_ON;
|
||||
LED_AUX_ON;
|
||||
}
|
||||
else
|
||||
{
|
||||
REL_AUX_OFF;
|
||||
LED_AUX_OFF;
|
||||
}
|
||||
/*
|
||||
if(motor_pwm>MOTOR_MAX)motor_pwm=MOTOR_MAX;
|
||||
if(motor_pwm<MOTOR_MIN)motor_pwm=MOTOR_MIN;
|
||||
if(motor_pwm==0)
|
||||
//TCCR1A&=~(1<<COM1B1);
|
||||
else
|
||||
{
|
||||
//MOTOR_PWM = motor_pwm;
|
||||
//TCCR1A|=(1<<COM1B1);
|
||||
}
|
||||
*/
|
||||
modbus_loop_iterate();
|
||||
HAL_Delay(1);
|
||||
}
|
||||
|
||||
// Timer1 overflow interrupt service routine
|
||||
void timer1_ovf_isr(void) //1 ms
|
||||
{
|
||||
static uint32_t count_1sec=0;
|
||||
if(last_rx_time<0xFFFF)last_rx_time++;
|
||||
|
||||
update_service_indication();
|
||||
|
||||
if(++count_1sec==1000)
|
||||
{
|
||||
count_1sec = 0;
|
||||
//led_time_g += 10;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void update_service_indication(void)
|
||||
{
|
||||
if(led_time_act)
|
||||
{
|
||||
LED_ACT_ON;
|
||||
led_time_act--;
|
||||
}
|
||||
else
|
||||
LED_ACT_OFF;
|
||||
/*
|
||||
if(led_time_g)
|
||||
{
|
||||
LED_G_ON;
|
||||
led_time_g--;
|
||||
}
|
||||
else
|
||||
LED_G_OFF;
|
||||
*/
|
||||
}
|
||||
|
||||
|
||||
uint8_t read_register(uint16_t address, uint16_t* value)
|
||||
{
|
||||
if (address == 0x0001)
|
||||
*value = MODBUS_PROTOCOL_VERSION;
|
||||
else if (address == 0x0002)
|
||||
*value = MODBUS_FIRMWARE_VERSION;
|
||||
else if (address == 0x0003)
|
||||
*value = MODBUS_BOARD_TYPE;
|
||||
else if (address == 0x0004)
|
||||
*value = tranfer_errors_count;
|
||||
else if (address == 0x0005)
|
||||
*value = 0; // supported data rate: unknown
|
||||
else if (address >= 0x0010 && address <= 0x0099)
|
||||
{
|
||||
int index = address - 0x0010;
|
||||
if (index < BOARD_DESC_LEN)
|
||||
*value = board_description[index];
|
||||
else
|
||||
*value = 0;
|
||||
}
|
||||
else if (address == 0x2001) //Read relays state
|
||||
{
|
||||
*value = relays;
|
||||
}
|
||||
else if (address == 0x2002) //Read motor pwm
|
||||
{
|
||||
*value = motor_pwm;
|
||||
}
|
||||
else
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint8_t write_register(uint16_t address, uint16_t value)
|
||||
{
|
||||
uint8_t ret;
|
||||
ret=0;
|
||||
#ifdef UART_DEBUG
|
||||
printf("Write A=0x%X D=0x%X\n\r",address,value);
|
||||
#endif
|
||||
switch (address)
|
||||
{
|
||||
case 0x2001: relays = value; break;
|
||||
case 0x2002: motor_pwm = value; break;
|
||||
default: ret = 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
Reference in New Issue
Block a user