64 lines
1.7 KiB
C
64 lines
1.7 KiB
C
#ifndef EVSE_LINK_FRAMING_H_
|
|
#define EVSE_LINK_FRAMING_H_
|
|
|
|
#include <stdint.h>
|
|
#include <stdbool.h>
|
|
#include "driver/uart.h"
|
|
|
|
// UART baud/buffer configuration
|
|
#define UART_BAUDRATE 9600
|
|
#define UART_RX_BUF_SIZE 256
|
|
|
|
// Select EVSE-Link physical layer at compile time.
|
|
// 0 = production RS485: UART2 GPIO17/16 + RTS/DE GPIO2
|
|
// 1 = emergency/internal UART TTL: UART1 GPIO21/22, no RS485/RTS
|
|
#ifndef EVSE_LINK_USE_UART_TTL
|
|
#define EVSE_LINK_USE_UART_TTL 0
|
|
#endif
|
|
|
|
#if EVSE_LINK_USE_UART_TTL
|
|
|
|
#define UART_PORT UART_NUM_1
|
|
#define TX_PIN 21
|
|
#define RX_PIN 22
|
|
#define RTS_PIN UART_PIN_NO_CHANGE
|
|
#define EVSE_LINK_UART_MODE UART_MODE_UART
|
|
#define EVSE_LINK_PHY_NAME "UART_TTL"
|
|
|
|
#else
|
|
|
|
#define UART_PORT UART_NUM_2
|
|
#define TX_PIN 17
|
|
#define RX_PIN 16
|
|
#define RTS_PIN 2
|
|
#define EVSE_LINK_UART_MODE UART_MODE_RS485_HALF_DUPLEX
|
|
#define EVSE_LINK_PHY_NAME "RS485"
|
|
|
|
#endif
|
|
// Frame delimiters
|
|
#define MAGIC_START 0x7E
|
|
#define MAGIC_END 0x7F
|
|
|
|
// Maximum payload (excluding sequence byte)
|
|
#define EVSE_LINK_MAX_PAYLOAD 254
|
|
|
|
// Callback type for when a full frame is received
|
|
typedef void (*evse_link_frame_cb_t)(uint8_t src, uint8_t dest,
|
|
const uint8_t *payload, uint8_t len);
|
|
|
|
// Initialize framing module (mutex, UART driver, etc.)
|
|
void evse_link_framing_init(void);
|
|
|
|
// Send a framed payload to `dest` with length `len`
|
|
// Includes source address in the header
|
|
bool evse_link_framing_send(uint8_t dest, uint8_t src,
|
|
const uint8_t *payload, uint8_t len);
|
|
|
|
// Feed a received byte into the framing parser
|
|
void evse_link_framing_recv_byte(uint8_t byte);
|
|
|
|
// Register a callback for complete frames
|
|
void evse_link_framing_register_cb(evse_link_frame_cb_t cb);
|
|
|
|
#endif // EVSE_LINK_FRAMING_H_
|