WIP: Add new features for board version 2

This commit is contained in:
Anton Mukhin
2024-01-17 15:52:55 +03:00
parent c882ede6bd
commit 108f165ca2
10 changed files with 374 additions and 68 deletions

View File

@@ -12,8 +12,10 @@
#define BOARD_DESC_LEN (16)
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim3;
static const char board_description[BOARD_DESC_LEN] = "RS485_Relay V1R1";
extern TIM_HandleTypeDef htim14;
static const char board_description[BOARD_DESC_LEN] = "RS485_Relay V2R1";
extern uint16_t tranfer_errors_count;
extern uint16_t last_rx_time;
@@ -28,11 +30,59 @@ extern uint16_t last_rx_time;
#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);
uint16_t relays = 0;
uint16_t status = 0;
uint16_t motor1_pwm = 0;
uint16_t motor2_pwm = 0;
uint16_t lights_pwm = 0;
uint16_t led_time_act = 0;
void estop_reset(void) {
HAL_GPIO_WritePin(RL_EN_GPIO_Port, RL_EN_Pin, GPIO_PIN_RESET);
HAL_Delay(1);
HAL_GPIO_WritePin(RL_EN_GPIO_Port, RL_EN_Pin, GPIO_PIN_SET);
}
void board_init(void) {
// Activate (normalize) E-STOP trigger
estop_reset();
// 1ms timer start
HAL_TIM_Base_Start_IT(&htim1);
}
void set_pwm(uint8_t unit, uint16_t duty) {
uint32_t channel;
TIM_HandleTypeDef* tim;
switch (unit) {
case 1: // motor 1
tim = &htim3;
channel = TIM_CHANNEL_1;
break;
case 2: // motor 2
tim = &htim3;
channel = TIM_CHANNEL_2;
break;
case 3: // lights
tim = &htim14;
channel = TIM_CHANNEL_1;
break;
default:
return;
}
if (duty > MOTOR_MAX) duty = MOTOR_MAX;
if (duty < MOTOR_MIN) duty = MOTOR_MIN;
if (duty == 0)
HAL_TIM_PWM_Stop(tim, channel);
else {
HAL_TIM_PWM_Start(tim, channel);
__HAL_TIM_SetCompare(tim, channel, duty);
HAL_Delay(5);
}
}
void loop_iterate()
{
@@ -82,22 +132,13 @@ void timer1_ovf_isr(void) //1 ms
void update_service_indication(void)
{
if(led_time_act)
{
if (led_time_act) {
LED_ACT_ON;
led_time_act--;
}
else
} else
LED_ACT_OFF;
/*
if(led_time_g)
{
LED_G_ON;
led_time_g--;
}
else
LED_G_OFF;
*/
}
@@ -125,9 +166,22 @@ uint8_t read_register(uint16_t address, uint16_t* value)
{
*value = relays;
}
else if (address == 0x2002) //Read motor pwm
else if (address == 0x2002) //Read motor 1 pwm
{
*value = motor_pwm;
*value = motor1_pwm;
}
else if (address == 0x2003) //Read motor 2 pwm
{
*value = motor2_pwm;
}
else if (address == 0x2004) //Read Lights pwm
{
*value = lights_pwm;
}
else if (address == 0x2004) //Read status
{
*value = status;
status &= 0b011; // Reset light button press event status
}
else
return 1;
@@ -137,7 +191,7 @@ uint8_t read_register(uint16_t address, uint16_t* value)
uint8_t write_register(uint16_t address, uint16_t value)
{
uint8_t ret;
ret=0;
ret = 0;
#ifdef UART_DEBUG
printf("Write A=0x%X D=0x%X\n\r",address,value);
#endif
@@ -146,15 +200,21 @@ uint8_t write_register(uint16_t address, uint16_t value)
relays = value;
break;
case 0x2002:
motor_pwm = value;
if(motor_pwm>MOTOR_MAX)motor_pwm=MOTOR_MAX;
if(motor_pwm<MOTOR_MIN)motor_pwm=MOTOR_MIN;
if(motor_pwm==0)
HAL_TIM_PWM_Stop(&htim3, TIM_CHANNEL_1);
else {
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
__HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, motor_pwm);
HAL_Delay(5);
motor1_pwm = value;
set_pwm(1, motor1_pwm);
break;
case 0x2003:
motor2_pwm = value;
set_pwm(2, motor2_pwm);
break;
case 0x2004:
lights_pwm = value;
set_pwm(3, lights_pwm);
break;
case 0x2020:
if (value == 1) {
estop_reset();
status &= 0b101;
}
break;
default:
@@ -163,3 +223,17 @@ uint8_t write_register(uint16_t address, uint16_t value)
return ret;
}
// Pin external interrupts handler
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if(GPIO_Pin == ESTOP_Pin) {
status = status | 0b010;
}
if(GPIO_Pin == LIGHTS_SW_Pin) {
status |= 0b100; // Set "lights switch pressed" status bit
}
if(GPIO_Pin == WATER_Pin) {
// Set or reset "water" status bit
status |= HAL_GPIO_ReadPin(WATER_GPIO_Port, WATER_Pin);
}
}