/*
 * @Date: 2025-06-19 12:29:23
 * @Author: mypx
 * @LastEditors: mypx mypx_coder@163.com
 * @LastEditTime: 2025-08-25 17:06:01
 * @FilePath: board_pm.h
 * @Description: 
 * Copyright (c) 2025 by mypx, All Rights Reserved.
 */
/*** 
 * @Author: mypx
 * @Email: mypx_coder@163.com
 * @Date: 2025-06-19 12:29:23
 * @LastEditors: mypx mypx_coder@163.com
 * @Description: 
 */
#ifndef __PM_BOARD_H__
#define __PM_BOARD_H__
#include "adc.h"
#include "main.h"
#include "rtthread.h"
#include "tim.h"
#include "user_config.h"
#include <stdbool.h>
#include <stdint.h>

extern volatile uint64_t timestamp_tim_overflow_count;

typedef enum
{
    TEC_COOLING = 0,
    TEC_HEATING = 1
} TEC_Direction_TypeDef;

int pm_board_init(void);

void pm_system_led_toggle(void);

void pm_system_led_on(void);
void pm_system_led_off(void);

void pm_error_led_on(void);
void pm_error_led_off(void);
void pm_error_led_toggle(void);

int pm_i2c_write_bytes(uint16_t dev_addr, uint16_t reg, uint8_t *data, uint16_t size);
int pm_i2c_read_bytes(uint16_t dev_addr, uint16_t reg, uint8_t *data, uint16_t size);

void pm_ad7793_spi_transfer(uint8_t *tx_buf, uint8_t *rx_buf, uint16_t len);

void pm_uart_print_send(const uint8_t *data, uint16_t len);

void pm_lcd_cmd_send(const uint8_t *data, uint16_t len);

#define MB_HMI_RX_QUEUE_SIZE 128 // Modbus HMI 接收队列大小
extern struct rt_messagequeue mb_hmi_mq;

int pm_mb_hmi_init(void);

int32_t pm_hmi_uart_read(uint8_t *buf, uint16_t count, int32_t byte_timeout_ms, void *arg);

int32_t pm_hmi_uart_write(const uint8_t *buf, uint16_t count, int32_t byte_timeout_ms, void *arg);

#if (ENABLE_SV630P == 1)
#define SERVO_RX_QUEUE_SIZE 128 // Modbus RTU 接收队列大小
extern struct rt_messagequeue servo_mq;
int                           pm_servo_init(void);

int32_t pm_servo_uart_read(uint8_t *buf, uint16_t count, int32_t timeout, void *arg);

int32_t pm_servo_uart_write(const uint8_t *buf, uint16_t count, int32_t timeout, void *arg);
#endif // ENABLE_SV630P

#if (ENABLE_TEC == 1)
void pm_tec_init(void);
void pm_tec_start(void);
void pm_tec_stop(void);
void pm_tec_set_duty(float pid_output);

void pm_fan_start(void);

void pm_fan_stop(void);
#endif

void pm_timestamp_tim_init(void);

void pm_timestamp_start(void);

void pm_timestamp_stop(void);

uint64_t pm_get_timestamp_us(void);

void pm_adc_init(void);

void pm_sampling_adc_start(uint16_t *buffer, uint32_t length);

void pm_sampling_adc_stop(void);

uint32_t pm_adc_get_value(void);

void pm_sampling_tim_init(void);

void pm_sampling_tim_start(void);

void pm_sampling_tim_stop(void);

float pm_sampling_tim_get_freq(void);

#endif // __BOARD_H__
