|
|
|
|
#include "owi.h"
|
|
|
|
|
#include "r_cg_wdt.h"
|
|
|
|
|
#include "delay.h"
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
/* 내부 상태 */
|
|
|
|
|
static uint32_t bit_period_us = OWI_BIT_PERIOD_US;
|
|
|
|
|
static uint8_t g_owi_timeout_latched = 0;
|
|
|
|
|
|
|
|
|
|
static uint16_t g_owi_last_timeout_byte_index = 0xFFFFu;
|
|
|
|
|
static uint8_t g_owi_last_timeout_bit_index = 0xFFu;
|
|
|
|
|
|
|
|
|
|
static uint16_t g_owi_current_read_byte_index = 0u;
|
|
|
|
|
static uint8_t g_owi_current_read_bit_index = 0u;
|
|
|
|
|
|
|
|
|
|
static void OWI_KickWdt(void)
|
|
|
|
|
{
|
|
|
|
|
R_WDT_Restart();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =========================================================
|
|
|
|
|
* GPIO helpers (P70 / HW Open-Drain)
|
|
|
|
|
* - LOW : 출력 모드 + 0
|
|
|
|
|
* - HIGH : 입력(Hi-Z)로 release
|
|
|
|
|
* ========================================================= */
|
|
|
|
|
void GPIO_Clear(void)
|
|
|
|
|
{
|
|
|
|
|
OWI_PORT_POM |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_P &= (uint8_t)~OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_PM &= (uint8_t)~OWI_PIN_MASK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GPIO_Input(void)
|
|
|
|
|
{
|
|
|
|
|
OWI_PORT_POM |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_P |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_PM |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int GPIO_Read(void)
|
|
|
|
|
{
|
|
|
|
|
return (OWI_PORT_P & (uint8_t)OWI_PIN_MASK) ? 1 : 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void OWI_Release(void)
|
|
|
|
|
{
|
|
|
|
|
OWI_PORT_POM |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_P |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_PM |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void OWI_DriveLow(void)
|
|
|
|
|
{
|
|
|
|
|
OWI_PORT_POM |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_P &= (uint8_t)~OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_PM &= (uint8_t)~OWI_PIN_MASK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =========================================================
|
|
|
|
|
* Diagnostics
|
|
|
|
|
* ========================================================= */
|
|
|
|
|
uint8_t OWI_HasTimeout(void)
|
|
|
|
|
{
|
|
|
|
|
return g_owi_timeout_latched;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OWI_ClearTimeout(void)
|
|
|
|
|
{
|
|
|
|
|
g_owi_timeout_latched = 0;
|
|
|
|
|
g_owi_last_timeout_byte_index = 0xFFFFu;
|
|
|
|
|
g_owi_last_timeout_bit_index = 0xFFu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint16_t OWI_GetLastTimeoutByteIndex(void)
|
|
|
|
|
{
|
|
|
|
|
return g_owi_last_timeout_byte_index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t OWI_GetLastTimeoutBitIndex(void)
|
|
|
|
|
{
|
|
|
|
|
return g_owi_last_timeout_bit_index;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =========================================================
|
|
|
|
|
* OWI Init
|
|
|
|
|
* ========================================================= */
|
|
|
|
|
void OWI_Init(uint32_t bit_time_us)
|
|
|
|
|
{
|
|
|
|
|
bit_period_us = bit_time_us;
|
|
|
|
|
(void)bit_period_us;
|
|
|
|
|
|
|
|
|
|
OWI_PORT_POM |= (uint8_t)OWI_PIN_MASK;
|
|
|
|
|
OWI_PORT_PU &= (uint8_t)~OWI_PIN_MASK;
|
|
|
|
|
|
|
|
|
|
OWI_ClearTimeout();
|
|
|
|
|
GPIO_Input();
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =========================================================
|
|
|
|
|
* Start/Stop/Secure
|
|
|
|
|
* ========================================================= */
|
|
|
|
|
void OWI_Start(void)
|
|
|
|
|
{
|
|
|
|
|
GPIO_Clear();
|
|
|
|
|
delay_us(TSTART_HOLD);
|
|
|
|
|
|
|
|
|
|
GPIO_Input();
|
|
|
|
|
delay_us(TBIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OWI_Stop(void)
|
|
|
|
|
{
|
|
|
|
|
GPIO_Input();
|
|
|
|
|
delay_us(TSTOP_LOW);
|
|
|
|
|
delay_us(TIDLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void OWI_StopRead(void)
|
|
|
|
|
{
|
|
|
|
|
OWI_DriveLow();
|
|
|
|
|
delay_us(TSTOP_LOW);
|
|
|
|
|
OWI_Release();
|
|
|
|
|
delay_us(TIDLE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint8_t OWI_WaitForFallingEdge(uint16_t byte_index, uint8_t bit_index)
|
|
|
|
|
{
|
|
|
|
|
int timeout = (int)(bit_period_us * 4u);
|
|
|
|
|
|
|
|
|
|
while (GPIO_Read() && timeout-- > 0) {
|
|
|
|
|
delay_us(1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timeout <= 0) {
|
|
|
|
|
if (!g_owi_timeout_latched) {
|
|
|
|
|
g_owi_timeout_latched = 1;
|
|
|
|
|
g_owi_last_timeout_byte_index = byte_index;
|
|
|
|
|
g_owi_last_timeout_bit_index = bit_index;
|
|
|
|
|
}
|
|
|
|
|
return 0u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 1u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint8_t OWI_ReadByte_StreamSynced(uint16_t byte_index)
|
|
|
|
|
{
|
|
|
|
|
uint8_t data = 0;
|
|
|
|
|
int b;
|
|
|
|
|
|
|
|
|
|
for (b = 7; b >= 0; b--) {
|
|
|
|
|
uint8_t bit_index = (uint8_t)(7 - b);
|
|
|
|
|
uint8_t bit;
|
|
|
|
|
|
|
|
|
|
g_owi_current_read_byte_index = byte_index;
|
|
|
|
|
g_owi_current_read_bit_index = bit_index;
|
|
|
|
|
|
|
|
|
|
/* 슬롯 시작 후 중앙 샘플 */
|
|
|
|
|
delay_us(bit_period_us / 2u);
|
|
|
|
|
bit = GPIO_Read() ? 1u : 0u;
|
|
|
|
|
data |= (uint8_t)(bit << b);
|
|
|
|
|
|
|
|
|
|
/* 남은 반 슬롯 대기 */
|
|
|
|
|
delay_us(bit_period_us / 2u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OWI_SecureStop(void)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
GPIO_Clear();
|
|
|
|
|
delay_us(SECURE_HIGH);
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < (int)SECURE_TOGGLE_COUNT; i++) {
|
|
|
|
|
GPIO_Input();
|
|
|
|
|
delay_us(SECURE_TOGGLE_HIGH);
|
|
|
|
|
|
|
|
|
|
GPIO_Clear();
|
|
|
|
|
delay_us(SECURE_TOGGLE_LOW);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GPIO_Input();
|
|
|
|
|
delay_us(SECURE_HIGH);
|
|
|
|
|
|
|
|
|
|
GPIO_Clear();
|
|
|
|
|
delay_us(TSTART_HOLD);
|
|
|
|
|
GPIO_Input();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =========================================================
|
|
|
|
|
* Bit/Byte IO
|
|
|
|
|
* ========================================================= */
|
|
|
|
|
void OWI_WriteBit(int bit)
|
|
|
|
|
{
|
|
|
|
|
uint32_t t_low = bit ? (uint32_t)TLOW_1 : (uint32_t)TLOW_0;
|
|
|
|
|
uint32_t t_high = (uint32_t)TBIT - t_low;
|
|
|
|
|
|
|
|
|
|
/* HIGH 구간 */
|
|
|
|
|
OWI_Release();
|
|
|
|
|
delay_us(t_high);
|
|
|
|
|
|
|
|
|
|
/* LOW 구간 */
|
|
|
|
|
OWI_DriveLow();
|
|
|
|
|
delay_us(t_low);
|
|
|
|
|
|
|
|
|
|
/* 다음 슬롯 시작 전 반드시 release */
|
|
|
|
|
OWI_Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OWI_WriteByte(uint8_t data)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
for (i = 7; i >= 0; i--) {
|
|
|
|
|
OWI_WriteBit((data >> i) & 0x01u);
|
|
|
|
|
}
|
|
|
|
|
GPIO_Input();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t OWI_ReadBit(void)
|
|
|
|
|
{
|
|
|
|
|
int timeout;
|
|
|
|
|
int low_width = 0;
|
|
|
|
|
|
|
|
|
|
/* 1) 라인이 HIGH가 될 때까지 정리 */
|
|
|
|
|
timeout = (int)(bit_period_us * 2u);
|
|
|
|
|
while (!GPIO_Read() && timeout-- > 0) {
|
|
|
|
|
delay_us(1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timeout <= 0) {
|
|
|
|
|
if (!g_owi_timeout_latched) {
|
|
|
|
|
g_owi_timeout_latched = 1;
|
|
|
|
|
g_owi_last_timeout_byte_index = g_owi_current_read_byte_index;
|
|
|
|
|
g_owi_last_timeout_bit_index = g_owi_current_read_bit_index;
|
|
|
|
|
}
|
|
|
|
|
return 0u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 2) 다음 LOW 시작 대기 */
|
|
|
|
|
timeout = (int)(bit_period_us * 2u);
|
|
|
|
|
while (GPIO_Read() && timeout-- > 0) {
|
|
|
|
|
delay_us(1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (timeout <= 0) {
|
|
|
|
|
if (!g_owi_timeout_latched) {
|
|
|
|
|
g_owi_timeout_latched = 1;
|
|
|
|
|
g_owi_last_timeout_byte_index = g_owi_current_read_byte_index;
|
|
|
|
|
g_owi_last_timeout_bit_index = g_owi_current_read_bit_index;
|
|
|
|
|
}
|
|
|
|
|
return 0u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 3) LOW가 유지되는 시간을 직접 측정 */
|
|
|
|
|
while (!GPIO_Read() && low_width < (int)(bit_period_us * 2u)) {
|
|
|
|
|
delay_us(1u);
|
|
|
|
|
low_width++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 4) LOW 폭 기준으로 판정
|
|
|
|
|
- 짧으면 1
|
|
|
|
|
- 길면 0
|
|
|
|
|
threshold ~= TBIT/2
|
|
|
|
|
*/
|
|
|
|
|
return (low_width < (int)(bit_period_us / 2u)) ? 1u : 0u;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t OWI_ReadByte(void)
|
|
|
|
|
{
|
|
|
|
|
uint8_t data = 0;
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
for (i = 7; i >= 0; i--) {
|
|
|
|
|
g_owi_current_read_bit_index = (uint8_t)(7 - i);
|
|
|
|
|
data |= (uint8_t)(OWI_ReadBit() << i);
|
|
|
|
|
if (OWI_HasTimeout()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* =========================================================
|
|
|
|
|
* Raw helpers
|
|
|
|
|
* ========================================================= */
|
|
|
|
|
static void owi_result_init(owi_io_result_t *r)
|
|
|
|
|
{
|
|
|
|
|
if (!r) return;
|
|
|
|
|
memset(r, 0, sizeof(*r));
|
|
|
|
|
r->ok = 1;
|
|
|
|
|
r->timeout_byte_index = 0xFFFFu;
|
|
|
|
|
r->timeout_bit_index = 0xFFu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void owi_result_latch_timeout(owi_io_result_t *r)
|
|
|
|
|
{
|
|
|
|
|
if (!r) return;
|
|
|
|
|
|
|
|
|
|
if (OWI_HasTimeout()) {
|
|
|
|
|
r->ok = 0;
|
|
|
|
|
r->timeout = 1;
|
|
|
|
|
r->timeout_byte_index = OWI_GetLastTimeoutByteIndex();
|
|
|
|
|
r->timeout_bit_index = OWI_GetLastTimeoutBitIndex();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OWI_T_CommandModeRaw(const uint8_t *tx_data, uint8_t tx_len, uint8_t id, owi_io_result_t *r)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
owi_result_init(r);
|
|
|
|
|
|
|
|
|
|
//delay_us(200);
|
|
|
|
|
OWI_Init(OWI_BIT_PERIOD_US);
|
|
|
|
|
OWI_ClearTimeout();
|
|
|
|
|
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
OWI_SecureStop();
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
OWI_WriteByte((uint8_t)(id << 1));
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < (int)tx_len; i++) {
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
OWI_WriteByte(tx_data[i]);
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OWI_Stop();
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
owi_result_latch_timeout(r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OWI_CommandModeRaw(const uint8_t *tx_data, uint8_t tx_len, uint8_t id, owi_io_result_t *r)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
owi_result_init(r);
|
|
|
|
|
OWI_Init(OWI_BIT_PERIOD_US);
|
|
|
|
|
OWI_ClearTimeout();
|
|
|
|
|
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
OWI_SecureStop();
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
OWI_WriteByte((uint8_t)(id << 1));
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < (int)tx_len; i++) {
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
OWI_WriteByte(tx_data[i]);
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OWI_Stop();
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
owi_result_latch_timeout(r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void OWI_ReadBytesRaw(int length, uint8_t id, owi_io_result_t *r)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
int max_len;
|
|
|
|
|
uint8_t is_long_read;
|
|
|
|
|
|
|
|
|
|
if (!r) return;
|
|
|
|
|
|
|
|
|
|
owi_result_init(r);
|
|
|
|
|
OWI_Init(OWI_BIT_PERIOD_US);
|
|
|
|
|
|
|
|
|
|
if (length <= 0) {
|
|
|
|
|
r->ok = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
max_len = (int)OWI_IO_MAX_BYTES;
|
|
|
|
|
if (length > max_len) {
|
|
|
|
|
length = max_len;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r->read_len = (uint16_t)length;
|
|
|
|
|
OWI_ClearTimeout();
|
|
|
|
|
|
|
|
|
|
is_long_read = (length >= 119) ? 1u : 0u;
|
|
|
|
|
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
OWI_SecureStop();
|
|
|
|
|
OWI_WriteByte((uint8_t)((id << 1) | 1u));
|
|
|
|
|
|
|
|
|
|
/* 기존 잘 되던 값 유지 */
|
|
|
|
|
delay_us(100u);
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
|
|
|
|
|
/* 첫 falling edge를 한 번 잡는다 */
|
|
|
|
|
if (!OWI_WaitForFallingEdge(0u, 0u)) {
|
|
|
|
|
owi_result_latch_timeout(r);
|
|
|
|
|
OWI_StopRead();
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < length; i++) {
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
|
|
|
|
|
/* long read만 주기적 재동기화 */
|
|
|
|
|
if (is_long_read && i == (length - 50)) {
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
if (!OWI_WaitForFallingEdge((uint16_t)i, 0u)) {
|
|
|
|
|
owi_result_latch_timeout(r);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r->data[i] = OWI_ReadByte_StreamSynced((uint16_t)i);
|
|
|
|
|
|
|
|
|
|
if (OWI_HasTimeout()) {
|
|
|
|
|
owi_result_latch_timeout(r);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
OWI_StopRead();
|
|
|
|
|
OWI_KickWdt();
|
|
|
|
|
}
|