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.
 
 

727 lines
22 KiB

/***********************************************************************************************************************
* DISCLAIMER
* This software is supplied by Renesas Electronics Corporation and is only intended for use with Renesas products.
* No other uses are authorized. This software is owned by Renesas Electronics Corporation and is protected under all
* applicable laws, including copyright laws.
* THIS SOFTWARE IS PROVIDED "AS IS" AND RENESAS MAKES NO WARRANTIES REGARDING THIS SOFTWARE, WHETHER EXPRESS, IMPLIED
* OR STATUTORY, INCLUDING BUT NOT LIMITED TO WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NON-INFRINGEMENT. ALL SUCH WARRANTIES ARE EXPRESSLY DISCLAIMED.TO THE MAXIMUM EXTENT PERMITTED NOT PROHIBITED BY
* LAW, NEITHER RENESAS ELECTRONICS CORPORATION NOR ANY OF ITS AFFILIATED COMPANIES SHALL BE LIABLE FOR ANY DIRECT,
* INDIRECT, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON RELATED TO THIS SOFTWARE, EVEN IF RENESAS OR
* ITS AFFILIATES HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
* Renesas reserves the right, without notice, to make changes to this software and to discontinue the availability
* of this software. By using this software, you agree to the additional terms and conditions found by accessing the
* following link:
* http://www.renesas.com/disclaimer
*
* Copyright (C) 2012, 2021 Renesas Electronics Corporation. All rights reserved.
***********************************************************************************************************************/
/***********************************************************************************************************************
* File Name : r_main.c
* Version : CodeGenerator for RL78/F14 V2.03.07.02 [08 Nov 2021]
* Device(s) : R5F10PPJ
* Tool-Chain : CCRL
* Description : This file implements main function.
* Creation Date: 2026-01-21
***********************************************************************************************************************/
/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include "r_cg_macrodriver.h"
#include "r_cg_cgc.h"
#include "r_cg_port.h"
#include "r_cg_serial.h"
#include "r_cg_wdt.h"
/* Start user code for include. Do not edit comment generated here */
#include "common.h"
#include "dipSwitch.h"
#include "gatectrl.h"
#include <string.h>
#include <ctype.h>
#include <stddef.h>
#include <r_cg_port.h>
#define CMD_MAX 529
#define RS485_BRIDGE_FIFO_SZ 256
volatile uint8_t uart_rx_done = 0;
volatile uint8_t uart_rx_index = 0;
volatile uint8_t uart_rx_buffer[UART_RX_BUF_SIZE] = {0};
volatile uint16_t uart_rx_length = 0;
volatile uint8_t rs485_rx_done = 0;
volatile uint8_t rs485_rx_index = 0;
volatile uint8_t rs485_rx_buffer[UART_RX_BUF_SIZE] = {0};
volatile uint16_t rs485_rx_length = 0;
volatile uint8_t g_rs485_bridge_active = 0;
volatile uint8_t g_rs485_bridge_done = 0;
// (1~32)
uint8_t g_fixed_addr = 1;
/* End user code. Do not edit comment generated here */
#include "r_cg_userdefine.h"
/***********************************************************************************************************************
Pragma directive
***********************************************************************************************************************/
/* Start user code for pragma. Do not edit comment generated here */
static volatile uint8_t s_rb_fifo[RS485_BRIDGE_FIFO_SZ];
static volatile uint8_t s_rb_head = 0;
static volatile uint8_t s_rb_tail = 0;
void RS485_Bridge_Push(uint8_t b)
{
uint8_t next = (uint8_t)(s_rb_head + 1);
if (next >= RS485_BRIDGE_FIFO_SZ) next = 0;
// FIFO full�̸� tail�� ���ܼ� ���ֽ� ������ �켱������ ����(���� ��å)
if (next == s_rb_tail) {
uint8_t t = (uint8_t)(s_rb_tail + 1);
if (t >= RS485_BRIDGE_FIFO_SZ) t = 0;
s_rb_tail = t;
}
s_rb_fifo[s_rb_head] = b;
s_rb_head = next;
}
static void RS485_Bridge_ResetFifo(void)
{
s_rb_head = 0;
s_rb_tail = 0;
}
static void RS485_Bridge_DrainToPC(void)
{
// UART1�� ���� ���̸�(�����̹� ī��Ʈ) �� ���� �������� ����
extern volatile uint16_t g_uart1_tx_count;
static char out[2];
while (s_rb_tail != s_rb_head) {
if (g_uart1_tx_count != 0U) break;
out[0] = (char)s_rb_fifo[s_rb_tail];
out[1] = '\0';
uart1_send_string(out); // PC(UART1)�� 1����Ʈ ���� ����
s_rb_tail++;
if (s_rb_tail >= RS485_BRIDGE_FIFO_SZ) s_rb_tail = 0;
}
}
static unsigned char hex2byte(char h, char l)
{
unsigned char hi, lo;
if (h >= 'a' && h <= 'f') h -= 32;
if (l >= 'a' && l <= 'f') l -= 32;
hi = (h >= 'A') ? (unsigned char)(h - 'A' + 10) : (unsigned char)(h - '0');
lo = (l >= 'A') ? (unsigned char)(l - 'A' + 10) : (unsigned char)(l - '0');
return (unsigned char)((hi << 4) | lo);
}
typedef enum {
PREFIX_NONE = 0,
PREFIX_CAL,
PREFIX_EOL
} PrefixMode;
static PrefixMode s_prefix_mode = PREFIX_NONE;
static int parse_x_prefix(const char *s, int len,
uint8_t *addr, uint8_t *ch,
char *mode, uint8_t *hash_on, uint8_t *anaout_on, uint8_t *check_on, int *payload_pos)
{
*hash_on = 0;
*anaout_on = 0;
*check_on = 1;
if (len < 8) return 0;
if (!(s[0] == 'x' || s[0] == 'X')) return 0;
if (!isdigit((unsigned char)s[1]) || !isdigit((unsigned char)s[2])) return -1;
if (s[3] == 'c' || s[3] == 'C') *mode = 'C';
else if (s[3] == 'e' || s[3] == 'E') *mode = 'E';
else return -1;
if (s[4] != '_') return -1;
if (!isdigit((unsigned char)s[5]) || !isdigit((unsigned char)s[6]) || !isdigit((unsigned char)s[7])) return -1;
*addr = (uint8_t)((s[1] - '0') * 10 + (s[2] - '0'));
*ch = (uint8_t)((s[5] - '0') * 100 + (s[6] - '0') * 10 + (s[7] - '0'));
// prefix�� �� ���̽�: x01e_001 / x01c_001
if (len == 8) {
*payload_pos = len;
return 1;
}
// payload �ִ� ���̽�: x01c_001:...
if (s[8] == ':') {
*payload_pos = 9;
return 1;
}
// 3) flags: x01c_001011 or x01c_001011:...
if (len >= 11 &&
(s[8] == '0' || s[8] == '1') &&
(s[9] == '0' || s[9] == '1') &&
(s[10] == '0' || s[10] == '1'))
{
*hash_on = (uint8_t)(s[8] - '0');
*anaout_on = (uint8_t)(s[9] - '0');
*check_on = (uint8_t)(s[10] - '0');
// flags only
if (len == 11) {
*payload_pos = 11;
return 1;
}
// flags + payload
if (len >= 12 && s[11] == ':') {
*payload_pos = 12;
return 1;
}
return -1;
}
return -1;
}
typedef enum {
CMD_SRC_PC = 0,
CMD_SRC_RS485 = 1
} CmdSource;
static void OUT_PRINT(CmdSource src, const char *s)
{
if (src == CMD_SRC_PC) PC_PRINT(s);
else RS485_PRINT(s);
}
// xNNv (��: x00v, x31v) �Ǻ�
static int parse_x_v_cmd(const char *s, int len, uint8_t *addr)
{
if (len != 4) return 0; // "x" + 2digit + "v" = 4
if (!(s[0] == 'x' || s[0] == 'X')) return 0;
if (!isdigit((unsigned char)s[1]) || !isdigit((unsigned char)s[2])) return 0;
if (!(s[3] == 'v' || s[3] == 'V')) return 0;
*addr = (uint8_t)((s[1] - '0') * 10 + (s[2] - '0'));
return 1;
}
static void send_v_response(CmdSource src, uint8_t addr)
{
char resp[8];
// "V00\r\n\0"
resp[0] = 'V';
resp[1] = (char)('0' + (addr / 10));
resp[2] = (char)('0' + (addr % 10));
resp[3] = '\r';
resp[4] = '\n';
resp[5] = '\0';
OUT_PRINT(src, resp);
}
// ����1�� addr(1~31)�� RS485�� ��ȸ�ϰ�, ������ ���� ��Ʈ�������� PC�� ����
static void scan_one_addr_rs485(uint8_t addr)
{
char cmdline[8];
unsigned long guard;
// "xNNv\r\n"
cmdline[0] = 'x';
cmdline[1] = (char)('0' + (addr / 10));
cmdline[2] = (char)('0' + (addr % 10));
cmdline[3] = 'v';
cmdline[4] = '\r';
cmdline[5] = '\n';
cmdline[6] = '\0';
// �긴�� �غ�
g_rs485_bridge_active = 1;
g_rs485_bridge_done = 0;
RS485_Bridge_ResetFifo();
R_UART0_Receive((uint8_t*)&rs485_rx_buffer[0], 1);
// ��ȸ Ŀ�ǵ� �۽�
RS485_PRINT(cmdline);
// ���� ��������(done)���� �巹��
guard = 0;
while (!g_rs485_bridge_done && guard++ < 300000UL) {
RS485_Bridge_DrainToPC();
}
// ���� FIFO ����
RS485_Bridge_DrainToPC();
// �긴�� ����
g_rs485_bridge_active = 0;
// ���� RS485 RX ������(���� ����ó���� ���� ���� �Ÿ� ����)
rs485_rx_done = 0;
rs485_rx_index = 0;
rs485_rx_length = 0;
R_UART0_Receive((uint8_t*)&rs485_rx_buffer[0], 1);
}
static int build_line_from_rx(const volatile uint8_t *rx_buf, int rx_len,
char *out, int out_sz)
{
int i, idx = 0;
if (rx_len < 0) rx_len = 0;
if (rx_len > out_sz - 1) rx_len = out_sz - 1;
for (i = 0; i < rx_len && idx < out_sz - 1; i++) {
char c = (char)rx_buf[i];
if (c == '\r') continue;
if (c == '\n') break;
out[idx++] = c;
}
out[idx] = '\0';
return idx;
}
typedef enum {
PROTOCOL_I2CT,
PROTOCOL_OWIT,
PROTOCOL_I2CW,
PROTOCOL_I2CR,
PROTOCOL_OWIW,
PROTOCOL_OWIR,
PROTOCOL_UNKNOWN
} ProtocolType;
static ProtocolType detect_protocol(char header1, char header2)
{
if (header1 == 'I' && header2 == 'W') return PROTOCOL_I2CW;
if (header1 == 'I' && header2 == 'R') return PROTOCOL_I2CR;
if (header1 == 'O' && header2 == 'W') return PROTOCOL_OWIW;
if (header1 == 'O' && header2 == 'R') return PROTOCOL_OWIR;
return PROTOCOL_UNKNOWN;
}
static void cmd_unknown(const unsigned char *d, unsigned int len)
{
HOST_PRINT("Unknown cmd\r\n");
delay(100000);
}
static void process_cmd(ProtocolType protocol, uint8_t id,
const unsigned char *data, unsigned int len)
{
switch (protocol) {
case PROTOCOL_I2CT: I2C_T_Command_Mode_receiveData(data, (uint8_t)len, id); break;
case PROTOCOL_OWIT: OWI_T_CommandMode(data, (uint8_t)len, id); break;
case PROTOCOL_I2CW: I2C_Command_Mode_receiveData(data, (uint8_t)len, id); break;
case PROTOCOL_I2CR: I2C_Command_Mode_Send((uint8_t)len, id); break;
case PROTOCOL_OWIW: OWI_CommandMode(data, (uint8_t)len, id); break;
case PROTOCOL_OWIR: OWI_ReadBytesAndPrint(len, id); break;
default: cmd_unknown(data, len); break;
}
}
static void process_cmd_by_prefix(PrefixMode pm,
ProtocolType protocol, uint8_t id,
const unsigned char *data, unsigned int len)
{
if (pm == PREFIX_EOL) {
// TODO: EOL ���� ó�� �ʿ� �� ���⿡
process_cmd(protocol, id, data, len);
} else {
// CAL �Ǵ� Prefix ����
process_cmd(protocol, id, data, len);
}
}
static void process_one_line(CmdSource src, const volatile uint8_t *rx_buf, uint16_t rx_len)
{
char line[UART_RX_BUF_SIZE];
uint8_t v_addr = 0;
int is_v = 0;
uint8_t a = 0;
int i;
int idx = 0;
int pos = 2;
ProtocolType proto;
uint8_t id;
unsigned int byte_len;
uint8_t cmd[CMD_MAX];
unsigned int k = 0;
char orig_line[UART_RX_BUF_SIZE];
int orig_len = 0;
s_prefix_mode = PREFIX_NONE;
// 1) RX ���� -> line ���� (CR/LF ����)
idx = build_line_from_rx(rx_buf, (int)rx_len, line, (int)sizeof(line));
if (idx <= 0) return;
// xNNv ó�� (x-prefix ó������ ����!)
{
is_v = parse_x_v_cmd(line, idx, &v_addr);
if (is_v) {
if (v_addr > 31) { OUT_PRINT(src, "Err:addr_range\r\n"); return; }
// ����1(ADDR0) + PC���� x00v => 00~31 ��ĵ
if (g_fixed_addr == 0 && src == CMD_SRC_PC && v_addr == 0) {
// �ڱ� �ڽ�(00)�� ���� ����
send_v_response(CMD_SRC_PC, 0);
// 01~31 ���� ��ȸ (�����ϴ� ���常 VNN�� PC�� �귯��)
for (a = 1; a <= 31; a++) {
scan_one_addr_rs485(a);
}
return;
}
// �� ��: �� �ּҿ� ��ġ�� ���� ����
if (v_addr == g_fixed_addr) {
send_v_response(src, g_fixed_addr);
}
return; // v Ŀ�ǵ� ó���� ���⼭ ����
}
}
// RS485 �߰��� ���� ����
orig_len = idx;
memcpy(orig_line, line, (size_t)orig_len);
orig_line[orig_len] = '\0';
// 2) x-prefix ó��(������)
{
uint8_t addr = 0;
uint8_t ch = 0;
char mode = 0;
int payload_pos = 0;
uint8_t hash_on = 0;
uint8_t anaout_on = 0;
uint8_t check_on = 1;
int r = parse_x_prefix(line, idx, &addr, &ch, &mode, &hash_on, &anaout_on, &check_on, &payload_pos);
if (r == -1) {
OUT_PRINT(src, "Err:X_prefix\r\n");
return;
}
if (r == 1) {
if (addr > 31) { OUT_PRINT(src, "Err:addr_range\r\n"); return; }
if (ch < 1 || ch > 20) { OUT_PRINT(src, "Err:ch_range\r\n"); return; }
// �� �ּҰ� �ƴϸ�:
// - ����1(ADDR0) + PC���� ������ ��: RS485�� �߰� + RS485������ PC�� ����
// - �� ��(�����̺��ų� RS485����): ������ ����
if (addr != g_fixed_addr) {
// ����1(ADDR0) + PC���� ������ �͸� RS485 �߰�
if (g_fixed_addr == 0 && src == CMD_SRC_PC) {
// 0) �긴�� �غ�
g_rs485_bridge_active = 1;
g_rs485_bridge_done = 0;
RS485_Bridge_ResetFifo();
// 1) RS485 RX ������(1����Ʈ�� ���� �ް�)
rs485_rx_done = 0;
rs485_rx_index = 0;
rs485_rx_length = 0;
R_UART0_Receive((uint8_t*)&rs485_rx_buffer[0], 1);
// 2) RS485�� Ŀ�ǵ� �۽�
RS485_PRINT(orig_line);
RS485_PRINT("\r\n");
// 3) �������� �������� ���ȡ� �巹�� �ϸ鼭 ����
{
unsigned long guard = 0;
while (!g_rs485_bridge_done && guard++ < 600000UL) {
RS485_Bridge_DrainToPC(); // ? ��Ʈ����
}
// ���� FIFO�� ������ �ѹ� �� �巹��
RS485_Bridge_DrainToPC();
// �긴�� ����
g_rs485_bridge_active = 0;
rs485_rx_done = 0;
rs485_rx_index = 0;
rs485_rx_length = 0;
R_UART0_Receive((uint8_t*)&rs485_rx_buffer[rs485_rx_index], 1);
if (!g_rs485_bridge_done) {
PC_PRINT("Err:rs485_timeout\r\n");
}
}
}
// �����̺�(�ٸ� ����)���� addr mismatch�� ������ ����
return;
}
// addr == g_fixed_addr: ���� ó��(ä�� ����)
if (mode == 'C') {
s_prefix_mode = PREFIX_CAL;
Cal_Init();
Gate_SetByNum(ch, hash_on, anaout_on, check_on);
GateCtrl_SelectChannel(ch);
} else {
s_prefix_mode = PREFIX_EOL;
Eol_Init();
Gate_SetByNum(ch, hash_on, anaout_on, check_on);
GateCtrl_SelectChannel(ch);
}
// prefix�� �� ���� / payload ó��
{
int rem = idx - payload_pos;
if (rem <= 0) {
if (mode == 'E') {
// EOL�� payload ��� OK
OUT_PRINT(src, "<ACK>XE\r\n");
return;
} else {
// CAL�� payload �ʼ�
OUT_PRINT(src, "Err:CAL_need_payload\r\n");
return;
}
}
// payload �ִ� ���̽��ε� EOL�̸� ����
if (mode == 'E') {
OUT_PRINT(src, "Err:EOL_no_payload\r\n");
return;
}
// CAL + payload�� ����: payload�� line ������ ����
for (i = 0; i < rem; i++) line[i] = line[payload_pos + i];
line[rem] = '\0';
idx = rem;
pos = 2;
}
}
// r==0 (prefix ����): �׳� �Ʒ� �Ϲ� Ŀ�ǵ� �Ľ����� ����
}
// 3) �ּ� ���� üũ
if (idx < 7) {
OUT_PRINT(src, "Err:short\r\n");
return;
}
// 4) �������� �Ǻ�
{
char h0 = (char)toupper((unsigned char)line[0]);
char h1 = (char)toupper((unsigned char)line[1]);
proto = detect_protocol(h0, h1);
}
if (proto == PROTOCOL_UNKNOWN) {
OUT_PRINT(src, "Err:ID\r\n");
return;
}
// 5) T ���� ó�� (OT/IT ��)
if (line[pos] == 't' || line[pos] == 'T') {
if (proto == PROTOCOL_OWIW) proto = PROTOCOL_OWIT;
else if (proto == PROTOCOL_I2CW) proto = PROTOCOL_I2CT;
pos++;
}
// 6) ������ ó��
if (line[pos] == '_' || line[pos] == ':') pos++;
// 7) ID (hex 2����)
if (pos + 1 >= idx) {
OUT_PRINT(src, "Err:id_short\r\n");
return;
}
id = hex2byte(line[pos], line[pos + 1]);
pos += 2;
// 8) ����(3�ڸ� 10��)
if (pos + 2 >= idx ||
!(line[pos] >= '0' && line[pos] <= '9') ||
!(line[pos+1] >= '0' && line[pos+1] <= '9') ||
!(line[pos+2] >= '0' && line[pos+2] <= '9')) {
OUT_PRINT(src, "Err:len_dec\r\n");
return;
}
byte_len = (unsigned int)(100*(line[pos]-'0') + 10*(line[pos+1]-'0') + (line[pos+2]-'0'));
pos += 3;
if (byte_len > CMD_MAX) {
OUT_PRINT(src, "Err:len_range\r\n");
return;
}
// 9) Write �迭: payload(hex) �Ľ�
if (proto == PROTOCOL_OWIT || proto == PROTOCOL_I2CT ||
proto == PROTOCOL_OWIW || proto == PROTOCOL_I2CW)
{
if (byte_len == 0) {
OUT_PRINT(src, "Err:payload0\r\n");
return;
}
if ((int)(pos + (int)byte_len*2) > idx) {
OUT_PRINT(src, "Err:len_mismatch\r\n");
return;
}
for (k = 0; k < byte_len; k++) {
cmd[k] = hex2byte(line[pos + 2*k], line[pos + 2*k + 1]);
}
pos += (int)byte_len * 2;
if (pos != idx) {
OUT_PRINT(src, "Err:len_trail\r\n");
return;
}
}
// 10) Read �迭: payload ������ ��
else if (proto == PROTOCOL_OWIR || proto == PROTOCOL_I2CR)
{
if (byte_len == 0) {
OUT_PRINT(src, "Err:read_len_nonzero\r\n");
return;
}
if (pos != idx) {
OUT_PRINT(src, "Err:read_no_payload\r\n");
return;
}
}
// 11) ���� ���� (prefix ���� �ݿ�)
process_cmd_by_prefix(s_prefix_mode, proto, id, cmd, byte_len);
}
void handle_uart_command_line(void)
{
while (1)
{
// �긴�� ���̸� RS485->PC�� ���� ��������
if (g_rs485_bridge_active) {
RS485_Bridge_DrainToPC();
}
// PC(UART1)
if (uart_rx_done)
{
uart_rx_done = 0;
process_one_line(CMD_SRC_PC, uart_rx_buffer, uart_rx_length);
uart_rx_index = 0;
uart_rx_length = 0;
R_UART1_Receive((uint8_t *)&uart_rx_buffer[uart_rx_index], 1);
}
// RS485(UART0)
if (rs485_rx_done)
{
rs485_rx_done = 0;
// �긴�� �߿� ��RS485 ���� Ŀ�ǵ� ó������ ���� ����(���� ��Ʈ���� ���̴ϱ�)
if (!g_rs485_bridge_active) {
process_one_line(CMD_SRC_RS485, rs485_rx_buffer, rs485_rx_length);
}
rs485_rx_index = 0;
rs485_rx_length = 0;
R_UART0_Receive((uint8_t *)&rs485_rx_buffer[rs485_rx_index], 1);
}
}
}
/* End user code. Do not edit comment generated here */
/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
/* Start user code for global. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */
void R_MAIN_UserInit(void);
/***********************************************************************************************************************
* Function Name: main
* Description : This function implements main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void main(void)
{
R_MAIN_UserInit();
/* Start user code. Do not edit comment generated here */
R_UART0_Create(); // UART0
R_UART1_Create(); // UART1
R_IICA0_Create(); // I2C
R_UART0_Start(); // RS485
R_UART1_Start(); // PC
R_UART1_Receive((uint8_t *)&uart_rx_buffer[uart_rx_index], 1);
R_UART0_Receive((uint8_t *)&rs485_rx_buffer[rs485_rx_index], 1);
handle_uart_command_line();
delay(100000);
while (1U)
{
;
}
/* End user code. Do not edit comment generated here */
}
/***********************************************************************************************************************
* Function Name: R_MAIN_UserInit
* Description : This function adds user code before implementing main function.
* Arguments : None
* Return Value : None
***********************************************************************************************************************/
void R_MAIN_UserInit(void)
{
/* Start user code. Do not edit comment generated here */
EI();
R_PORT_Create();
rs485_init();
DipSwitch_Init();
g_fixed_addr = DipSwitch_ReadAddr_0to31();
/* End user code. Do not edit comment generated here */
}
/* Start user code for adding. Do not edit comment generated here */
/* End user code. Do not edit comment generated here */