📄 ser0cli.c
字号:
/***************************************************************************
* This code and information is provided "as is" without warranty of any *
* kind, either expressed or implied, including but not limited to the *
* implied warranties of merchantability and/or fitness for a particular *
* purpose. *
* *
* Copyright (C) 2005 TDK SemiConductor, Corporation. All Rights Reserved. *
* *
***************************************************************************/
//**************************************************************************
// DESCRIPTION: 71M652x POWER METER - hardware abstraction layer
// for SERIAL Ports. The idea here is to hide the hardware just
// enough that several identical files can include this file
// and become adapted to a serial port.
//
// AUTHOR: RGV
//
// HISTORY: See end of file
//**************************************************************************
// File: SER0CLI.C
//
//**************************************************************************
// Hardware debug access layer for UART 0.
//
#define PORT 0 // To produce sercli1.c, copy the file, and change this
#include "options.h"
#if SERIAL0_CLI // and this to be for port 1
#include "batmodes.h"
#include "cli.h"
#include "irq.h"
#include "library.h"
#include "main.h"
#include "stm.h"
#if PORT == 0
#include "ser0.h"
#include "ser0cli.h"
#elif PORT == 1
#include "ser1.h"
#include "ser1cli.h"
#else
#error unhandled port selection
#endif
#define sbf 64 // size of the buffer- must be a power of two
#define mbuf (sbf - 1)
#if PORT == 0
#define xon_xoff_s xon_xoff_0
#define bit_rate_s bit_rate_0
#define BAUD_S BAUD_0
#define SerialS_CTx(_buffer_, _len_) Serial0_CTx(_buffer_, _len_)
#define SerialS_RxLen() Serial0_RxLen()
#define SerialS_CRx(_buffer_, _cch_) Serial0_CRx(_buffer_, _cch_)
#define SerialS_RxFlowOff() Serial0_RxFlowOff()
#define SerialS_RxFlowOn() Serial0_RxFlowOn()
#elif PORT == 1
#define xon_xoff_s xon_xoff_1
#define bit_rate_s bit_rate_1
#define BAUD_S BAUD_1
#define SerialS_CTx(_buffer_, _len_) Serial1_CTx(_buffer_, _len_)
#define SerialS_RxLen() Serial1_RxLen()
#define SerialS_CRx(_buffer_, _cch_) Serial1_CRx(_buffer_, _cch_)
#define SerialS_RxFlowOff() Serial1_RxFlowOff()
#define SerialS_RxFlowOn() Serial1_RxFlowOn()
#else
#error unhandled port selection
#endif
/*** Public functions declared within this module ***/
// See "ser0cli.h".
/*** Public variables declared within this module ***/
#if PORT == 0
bool xon_xoff_0; // Flow control active.
uint8_t xdata bit_rate_0 = BAUD_0;
#elif PORT == 1
bool xon_xoff_1; // Flow control active.
uint8_t xdata bit_rate_1 = BAUD_1;
#else
#error unhandled port selection
#endif
/*** Private functions declared within this module ***/
static void rxs_flow_off (void) small reentrant;
static void rxs_flow_on (void) small reentrant;
static void free_timer (void) small reentrant;
/*** Private variables declared within this module ***/
static uint8_t xdata achTx[sbf];
static uint8_t xdata achRx[sbf];
static volatile uint8_t ichInTx;
static volatile uint8_t ichOutTx;
static volatile uint8_t cchTx;
static volatile uint8_t ichInRx;
static volatile uint8_t ichOutRx;
static volatile uint8_t cchRx;
static volatile uint8_t data rxs; // Last byte received from SerialS.
static volatile bool rxs_flow; // If true, active RX.
static volatile uint8_t data txs; // Next byte to send to SerialS.
static volatile bool txs_flow; // If true, active TX.
static volatile enum SERIAL_RC data txs_status;
static volatile uint16x_t *free_cnt = NULL; // time till freeing serial port
// Serial definitions.
//===================================================================//
#if PORT == 0
bool cli0_init (enum SERIAL_SPD speed, bool xon_xoff)
#elif PORT == 1
bool cli1_init (enum SERIAL_SPD speed, bool xon_xoff)
#else
#error unhandled port selection
#endif
{
cchTx = ichOutTx = ichInTx = 0;
cchRx = ichOutRx = ichInRx = 0;
txs_flow = ON;
rxs_flow = ON;
xon_xoff_s = xon_xoff;
if (xon_xoff_s)
{
txs = XON; // Let Host know RX flow is on.
txs_status = S_PENDING;
}
else
{
txs = '\0';
txs_status = S_EMPTY;
}
bit_rate_s = speed;
ser_initialize (speed); // also sets TI = TRUE (Readies TX).
ser_enable_rcv_rdy();
return TRUE;
}
// Clock Generator Circuit API
//
#if PORT == 0
bool MPU_Clk_Select0 (enum eMPU_DIV speed)
#elif PORT == 1
bool MPU_Clk_Select1 (enum eMPU_DIV speed)
#else
#error unhandled port selection
#endif
{
bool Ok;
Ok = (bit_rate_s + speed) < (sizeof(bit_rate_tbl)/2);
if (Ok)
{
uint8_16_t t;
#if BROWNOUT_BATMODE
if ( batmode_is_brownout () )
{
t.i = BPS_BROWNOUT_300; // UART clock is 7/8 of 32768Hz
}
else
{
#endif
t.i = bit_rate_tbl[ bit_rate_s + speed ];
#if BROWNOUT_BATMODE
}
#endif
#if PORT == 0
S0RELH = t.c[HI];
S0RELL = t.c[LO];
#elif PORT == 1
S1RELH = t.c[HI];
S1RELL = t.c[LO];
#else
#error unhandled port selection
#endif
CONFIG0 = (CONFIG0 & ~MPU_DIV) | speed;
// mpu_speed = speed;
}
return (Ok);
}
#if PORT == 0
void Serial0_Tx (uint8x_t *buffer, uint16_t len)
#elif PORT == 1
void Serial1_Tx (uint8x_t *buffer, uint16_t len)
#else
#error unhandled port selection
#endif
{
uint16_t cch;
do
{
cch = SerialS_CTx(buffer, len);
buffer += cch;
len -= cch;
main_background ();
} while (len > 0);
}
#if EXTRAS
#pragma save
#pragma NOAREGS
#if PORT == 0
uint16_t Serial0_TxLen (void)
#elif PORT == 1
uint16_t Serial1_TxLen (void)
#else
#error unhandled port selection
#endif
{
uint16_t len;
irq_disable (); // critical section- find the buffer size in use
len = cchTx;
irq_enable (); // critical section- find the buffer size in use
return len;
}
#pragma restore
#endif
#if PORT == 0
uint16_t Serial0_CTx (uint8x_t *buffer, uint16_t txlen)
#elif PORT == 1
uint16_t Serial1_CTx (uint8x_t *buffer, uint16_t txlen)
#else
#error unhandled port selection
#endif
{
uint16_t len;
if (txlen > 0)
{
irq_disable (); // critical section- find the buffer size in use
len = cchTx; //
irq_enable (); // critical section- find the buffer size in use
txlen = len = min (txlen, (sbf - len)); // figure the size to be used.
while (len--) // copy the data in.
{
ichInTx &= mbuf;
achTx[ ichInTx++ ] = *buffer++;
}
}
if (txlen > 0)
{
irq_disable (); // critical section- update the length in use
cchTx += txlen;
if (S_EMPTY == txs_status)
{
ser_xmit_on();
txs_status = S_PENDING;
stm_stop (free_cnt);
free_cnt = NULL;
ser_enable_xmit_rdy();
}
irq_enable (); // critical section- update the length in use
}
return (txlen);
}
#if EXTRAS
#if PORT == 0
void Serial0_Rx (uint8x_t *buffer, uint16_t len)
#elif PORT == 1
void Serial1_Rx (uint8x_t *buffer, uint16_t len)
#else
#error unhandled port selection
#endif
{
int cch;
while (len > 0)
{
while ((cch = SerialS_RxLen()) == 0)
main_background ();
cch = min (cch, len);
SerialS_CRx (buffer, cch);
buffer += cch;
len -= cch;
}
}
#endif
#if EXTRAS
#pragma save
#pragma NOAREGS
#if PORT == 0
uint16_t Serial0_RxLen (void)
#elif PORT == 1
uint16_t Serial1_RxLen (void)
#else
#error unhandled port selection
#endif
{
uint16_t len;
irq_disable (); // critical section- find the buffer size in use
len = cchRx;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -