You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

231 lines
5.5 KiB

3 months ago
#include "uart.h"
#include "delay.h"
#include "r_cg_adc.h"
#include "r_cg_port.h"
2 months ago
#include <string.h>
3 months ago
#define RS485_EN_PORT P4
#define RS485_EN_PM PM4
#define RS485_EN_MASK (0x20U) // P4.5
volatile uint8_t g_uart0_tx_done = 1;
2 months ago
volatile uint8_t g_uart1_tx_done = 1;
extern volatile uint16_t g_uart1_tx_count;
2 months ago
extern volatile uint8_t rs485_rx_done;
extern volatile uint8_t rs485_rx_index;
extern volatile uint16_t rs485_rx_length;
extern volatile uint8_t rs485_rx_buffer[];
extern volatile uint8_t g_rs485_bridge_active;
extern volatile uint8_t g_rs485_bridge_done;
volatile uint8_t g_rs485_need_recover = 0;
void rs485_recover(void)
{
DI(); // ISR 경쟁 줄이기 (권장)
rs485_set_tx(0); // RX 모드 고정
// UART0 재시작
R_UART0_Stop();
delay_us(200);
R_UART0_Create();
delay_us(200);
R_UART0_Start();
delay_us(200);
// 에러 플래그/카운터 정리
g_uart0_err_fef = 0;
g_uart0_err_ovf = 0;
g_uart0_err_pef = 0;
// RX 다시 arm
rs485_rx_done = 0;
rs485_rx_index = 0;
rs485_rx_length = 0;
R_UART0_Receive((uint8_t*)&rs485_rx_buffer[0], 1);
g_rs485_need_recover = 0; // recover 완료 처리 (여기서 내려도 OK)
EI();
}
2 months ago
/* UART1 송신용 내부 버퍼(스택 포인터 안전) */
2 months ago
static uint8_t s_uart1_txbuf[1024]; // 너 출력 단위가 "72 " / "7272 " 정도라 64면 충분
2 months ago
// (더 길게 보내면 키워도 됨)
static void UART1_WaitTxIdle(void)
{
unsigned long guard = 0;
while (g_uart1_tx_count != 0U) {
if (guard++ > 3000000UL) break; // 무한루프 방지
}
}
3 months ago
void rs485_set_tx(uint8_t on)
{
if (on) RS485_EN_PORT |= RS485_EN_MASK; // EN=1 (TX)
else RS485_EN_PORT &= (uint8_t)~RS485_EN_MASK; // EN=0 (RX)
}
void rs485_init(void)
{
RS485_EN_PM &= (uint8_t)~RS485_EN_MASK; // 출력
rs485_set_tx(0); // 기본 RX 모드
}
void UART0_WaitTxDone_Us(uint32_t timeout_us)
{
while (!g_uart0_tx_done && timeout_us >= 10U) {
delay_us(10);
timeout_us -= 10U;
}
}
void RS485_Send(const uint8_t* data, uint16_t len)
{
2 months ago
if (!data || len == 0) return;
2 months ago
/* 송신 시작을 명확히 */
3 months ago
g_uart0_tx_done = 0;
rs485_set_tx(1);
2 months ago
3 months ago
R_UART0_Send((uint8_t*)data, len);
}
void RS485_SendString(const char* s)
{
uint16_t len = 0;
while (s[len] != '\0') len++;
RS485_Send((const uint8_t*)s, len);
}
2 months ago
static void UART1_WaitTxDone_Us(uint32_t timeout_us)
{
while (!g_uart1_tx_done && timeout_us >= 10U) {
delay_us(10);
timeout_us -= 10U;
}
}
3 months ago
/**
* : uart_send_string
* : null UART0로
*
* :
* - str : (C , '\0' )
*
* : (void)
*
* :
* 1)
* - '\0' len
*
* 2) UART
* - R_UART0_Send()
* - (uint8_t *)
*
* :
* - null ('\0')
* - UART0
*/
// UART0(RS485)
void uart_send_string(const char *str)
{
uint16_t len = 0;
while (str[len] != '\0') len++;
2 months ago
RS485_Send((const uint8_t*)str, len);
3 months ago
}
// UART1(PC)
void uart1_send_string(const char *str)
{
uint16_t len = 0;
2 months ago
if (str == 0) return;
3 months ago
while (str[len] != '\0') len++;
2 months ago
/* ✅ 이전 전송 끝날 때까지 대기 */
UART1_WaitTxIdle();
2 months ago
g_uart1_tx_done = 0;
2 months ago
/* ✅ 스택 포인터/버퍼 수명 문제 방지: 내부 버퍼로 복사 */
if (len >= (uint16_t)sizeof(s_uart1_txbuf))
len = (uint16_t)(sizeof(s_uart1_txbuf) - 1);
memcpy(s_uart1_txbuf, str, len);
/* ✅ 비동기 송신 시작 */
R_UART1_Send(s_uart1_txbuf, len);
/* ❌ 여기서 또 기다리지 마(속도 저하). 다음 호출이 알아서 대기함 */
}
3 months ago
/**
* : uart_send_hex
* : 8 (uint8_t) 16 UART0로
*
* :
* - val : 8
*
* : (void)
*
* :
* 1) / 4
* - high = val >> 4, 4
* - low = val & 0x0F, 4
*
* 2) 16
* - 0~9 '0'~'9'
* - 10~15 'A'~'F'
* - hex[0] = 4
* - hex[1] = 4
*
* 3) UART
* - R_UART0_Send() 2
*
* 4)
* - delay(10000)
*
* :
* - 1 2 16
* - : val = 0xAF "AF"
*/
void uart_send_hex(uint8_t val)
{
uint8_t hex[2];
uint8_t high = (val >> 4) & 0x0F;
uint8_t low = val & 0x0F;
hex[0] = (high < 10) ? ('0' + high) : ('A' + (high - 10));
hex[1] = (low < 10) ? ('0' + low) : ('A' + (low - 10));
2 months ago
RS485_Send(hex, 2);
3 months ago
3 months ago
}
void uart1_send_hex(uint8_t val)
{
2 months ago
static uint8_t hex[2];
3 months ago
uint8_t high = (val >> 4) & 0x0F;
uint8_t low = val & 0x0F;
hex[0] = (high < 10) ? ('0' + high) : ('A' + (high - 10));
hex[1] = (low < 10) ? ('0' + low ) : ('A' + (low - 10));
2 months ago
UART1_WaitTxIdle();
2 months ago
g_uart1_tx_done = 0;
3 months ago
R_UART1_Send(hex, 2);
}