全ソースコード
c328(scam)をテストするために作成したファームウエアの
全ソースコードです。
GCCのH8プログラム開発環境GDLを利用してコンパイル、リンク
できます。
GDLは、ベストテクノロジーのホームページからダウンロード可能です。
#define SYS_CLOCK_24_576
#include "3048.h"
typedef unsigned char UBYTE ;
typedef unsigned short UWORD ;
typedef unsigned long ULONG ;
typedef signed char SBYTE ;
typedef signed short SWORD ;
#define NO 0
#define YES 1
/*----------------*/
/* user variables */
/*----------------*/
#ifdef SYS_CLOCK_24_576
#define ITU0_AREG 24576
#endif
typedef union {
struct {
unsigned char B7:1;
unsigned char B6:1;
unsigned char B5:1;
unsigned char B4:1;
unsigned char B3:1;
unsigned char B2:1;
unsigned char B1:1;
unsigned char B0:1;
} BIT ;
unsigned char DR ;
} FLAGSP ;
typedef struct {
UBYTE param[5] ;
} PARAMP ;
typedef struct {
UWORD cap ;
UWORD wp ;
UWORD rp ;
UBYTE dat[256] ;
} RINGP ;
FLAGSP x_flags ;
#define S1FLAG x_flags.BIT.B0
#define TTRG x_flags.BIT.B1
#define TFLAG x_flags.BIT.B2
#define SFLAG x_flags.BIT.B3
#define LMOTOR x_flags.BIT.B4
#define RMOTOR x_flags.BIT.B5
#define DFLAG x_flags.BIT.B7
PARAMP cmdp;
RINGP sring ;
UWORD wcnt ;
#define P4DDR P4.DDR
#define P4DR P4.DR.BYTE
#define P7DR P7.DR.BYTE
#define P9DDR P9.DDR
#define P9DR P9.DR.BYTE
#define PADDR PA.DDR
#define PADR PA.DR.BYTE
#define PBDDR PB.DDR
#define PBDR PB.DR.BYTE
#define ALT PB.DR.BIT.B1
#define AHT PB.DR.BIT.B3
#define SRAM_OE P4.DR.BIT.B7
#define SRAM_WE P4.DR.BIT.B6
#define TRG_LOWER 0
#define TRG_UPPER 2
#define LON 0
#define LOFF 1
#define MASKFFFF 0xffff
#define MASKFF 0xff
#define MASKCF 0xcf
#define MASK0F 0x0f
#define OFF 0
#define ON OFF+1
#define IDLE 0
#define LAST 20480
#define RES_NONE 0
#define RES_SYNC RES_NONE+1
#define RES_ACK RES_NONE+2
#define RES_NAK RES_NONE+3
#define RES_DATA RES_NONE+4
UBYTE sindex ;
UBYTE sbuf[8];
UBYTE rbuf[12];
UBYTE pre_ah ;
UBYTE sysparam ;
/*
2^0 uncompressed 1 / compressed 0
2^1 gray scale 1 8bits / 0 2bits(binary)
2^2 size 1 160 x 120 / 0 80 x 60
2^3 preview 1 preview / 0 snapshot
*/
UBYTE cmd ;
UWORD sram_address ;
ULONG sram_end ;
UBYTE pwm_cnt ;
UBYTE left_duty ;
UBYTE right_duty ;
UWORD dcount ;
/*------------------------*/
/* task function protoype */
/*------------------------*/
void sci1_handling(void);
UBYTE confirm_sync(void);
UBYTE set_params(UBYTE x);
UBYTE set_snapshot(void);
UBYTE get_picture(UBYTE onoff);
/*-----------------------*/
/* system call prototype */
/*-----------------------*/
void update_address(UWORD x);
/*--------------------------------*/
/* Insert user functions protoype */
/*--------------------------------*/
void user_initialize(void);
void init_timer0();
void init_SCI0(UWORD x);
void init_SCI1(UWORD x);
void rs_put_txd0(UBYTE x);
void rs_put_txd1(UBYTE x);
void rs_puts_txd1(UBYTE *x);
void crlf(void);
UBYTE judge_response(UBYTE x);
void put_sram(UWORD adr,UBYTE dat);
UBYTE get_sram(UWORD adr);
void atrg(UBYTE x);
void send_command(void);
void clear_command(void);
UBYTE get_hex(UBYTE x);
void send_sync(void);
void send_ack(UBYTE x);
void send_snapshot(UBYTE onoff);
void send_getpicture(UBYTE onoff);
UBYTE delay_ms(UWORD x);
UWORD get_capacity(void);
void put_ring(UBYTE x);
UBYTE get_ring(void);
void init_ring(void);
/*------*/
/* main */
/*------*/
int main(void)
{
/* disable interrupt */
DI ;
/* initialize */
user_initialize();
/* enable interrupt */
EI ;
/* clear sram */
for ( sram_address = 0 ; sram_address < LAST ; sram_address++ ) {
put_sram( sram_address , 0 );
}
/* loop */
while ( ON ) {
sci1_handling();
}
return 0 ;
}
/*----------------*/
/* task functions */
/*----------------*/
void sci1_handling(void)
{
UBYTE s1cmd ;
UWORD i ;
UBYTE xdat[4] ;
UBYTE tmp ;
/* judge */
if ( S1FLAG == OFF ) return ;
/* clear flag */
S1FLAG = OFF ;
/* command intepreter */
s1cmd = *(sbuf+0) ;
/* get button state */
if ( s1cmd == 'B' ) {
*(xdat+0) = PB.DR.BIT.B5 + '0' ;
*(xdat+1) = PB.DR.BIT.B4 + '0' ;
rs_put_txd1( *(xdat+0) ) ;
rs_put_txd1( *(xdat+1) ) ;
crlf();
}
/* put motor state */
if ( s1cmd == 'M' ) {
left_duty = ( *(sbuf+1) - '0' ) * 10 + ( *(sbuf+2) - '0' ) ;
right_duty = ( *(sbuf+3) - '0' ) * 10 + ( *(sbuf+4) - '0' ) ;
crlf();
}
/* set image infomations */
if ( s1cmd == 'P' ) {
/* ? uncompressed */
sysparam &= ~1 ; /* compressed */
if ( *(sbuf+1) & 1 ) { sysparam |= 1 ; }
/* ? gray scale */
sysparam &= ~2 ; /* 2 bits(binary) */
if ( *(sbuf+2) & 1 ) { sysparam |= 2 ; }
/* ? size */
sysparam &= ~4 ; /* 80 x 60 */
if ( *(sbuf+3) & 1 ) { sysparam |= 4 ; }
/* ? preview or snapshot */
sysparam &= ~8 ; /* snapshot */
if ( *(sbuf+4) & 1 ) { sysparam |= 8 ; }
crlf(); rs_put_txd1( 'p' );
}
/* confirm synchronous */
if ( s1cmd == 'C' ) {
SFLAG = confirm_sync() ;
crlf(); rs_put_txd1( 'c' );
}
/* get image */
if ( s1cmd == 'G' ) {
get_picture(sysparam);
crlf(); rs_put_txd1( 'g' );
}
/* transfer image (LOAD) */
if ( s1cmd == 'L' ) {
*(xdat+3) = ' ';
for ( sram_address = 0 ; sram_address < (UWORD)sram_end ; sram_address++ ) {
tmp = get_sram(sram_address);
*(xdat+0) = *(xdat+1) = *(xdat+2) = ' ' ;
*(xdat+0) = tmp / 100 + '0' ; tmp %= 100 ;
*(xdat+1) = tmp / 10 + '0' ;
if ( tmp < 10 ) {
*(xdat+2) = tmp % 10 + '0' ;
} else {
tmp /= 10 ;
*(xdat+1) = tmp % 10 + '0' ;
if ( tmp > 9 ) {
*(xdat+0) = tmp / 10 + '0' ;
}
}
rs_put_txd1( *(xdat+0) ) ;
rs_put_txd1( *(xdat+1) ) ;
rs_put_txd1( *(xdat+2) ) ;
rs_put_txd1( *(xdat+3) ) ;
if ( (sram_address % 16) == 15 ) crlf();
}
}
/* get status */
if ( s1cmd == 'S' ) {
/* ? uncompressed */
if ( sysparam & 1 ) { rs_puts_txd1((UBYTE *)"uncompressed") ; }
else { rs_puts_txd1((UBYTE *)" compressed") ; }
/* ? gray scale */
if ( sysparam & 2 ) { rs_puts_txd1((UBYTE *)"8 bits gray scale") ; }
else { rs_puts_txd1((UBYTE *)"2 bits gray scale") ; }
/* ? size */
if ( sysparam & 4 ) { rs_puts_txd1((UBYTE *)"160 x 120") ; }
else { rs_puts_txd1((UBYTE *)" 80 x 60") ; }
/* ? preview or snapshot */
if ( sysparam & 8 ) { rs_puts_txd1((UBYTE *)"preview") ; }
else { rs_puts_txd1((UBYTE *)"snapshot") ; }
/* confirm synchronous */
if ( SFLAG == ON ) { rs_puts_txd1((UBYTE *)"SYNC OK") ; }
else { rs_puts_txd1((UBYTE *)"SYNC NG") ; }
}
/* debug */
if ( s1cmd == 'D' ) {
DFLAG = OFF ;
if ( *(sbuf+1) == '1' ) { DFLAG = ON ; }
crlf();
}
/* help */
if ( s1cmd == '?' ) {
rs_puts_txd1((UBYTE *)"? : help");
rs_puts_txd1((UBYTE *)"P : set parameters");
rs_puts_txd1((UBYTE *)" 1??? : uncompressed");
rs_puts_txd1((UBYTE *)" 0??? : compressed");
rs_puts_txd1((UBYTE *)" ?1?? : 8 bits gray scale");
rs_puts_txd1((UBYTE *)" ?0?? : 2 btis gray scale");
rs_puts_txd1((UBYTE *)" ??1? : 160 x 120");
rs_puts_txd1((UBYTE *)" ??0? : 80 x 60");
rs_puts_txd1((UBYTE *)" ???1 : preview");
rs_puts_txd1((UBYTE *)" ???0 : snapshot");
rs_puts_txd1((UBYTE *)"G : get image");
rs_puts_txd1((UBYTE *)"L : load image (processor => host)");
rs_puts_txd1((UBYTE *)"S : status");
rs_puts_txd1((UBYTE *)"D : debug");
rs_puts_txd1((UBYTE *)" D0 : debug off");
rs_puts_txd1((UBYTE *)" D1 : debug on");
}
}
/* initial sync */
UBYTE confirm_sync(void)
{
UBYTE i,tmp,doflag ;
/* default */
doflag = OFF ;
/* looping */
for ( i = 0 ; i < 60 ; i++ ) {
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" SYNC") ; }
/* send command */
send_sync();
/* wait */
delay_ms( 20 ) ;
/* judge */
if ( get_capacity() >= 12 ) {
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" Get Ack and Sync") ; }
doflag = ON ;
}
if ( doflag ) break ;
}
/* judge */
if ( doflag ) {
/* copy */
for ( i = 0 ; i < 12 ; i++ ) {
*(rbuf+i) = tmp = get_ring() ;
if ( DFLAG ) {
rs_put_txd1( get_hex( (tmp >> 4) & MASK0F ) );
rs_put_txd1( get_hex( tmp & MASK0F ) );
rs_put_txd1( ' ' );
}
}
i = 0 ;
if ( judge_response(0) == RES_ACK && *(rbuf+2) == 0x0D ) {
i++ ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" CONFIRM ACK") ; }
}
/* send ACK */
if ( judge_response(6) == RES_SYNC && *(rbuf+8) == 0x00 ) {
i++ ;
send_ack( 0x0D ) ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" SEND ACK") ; }
}
}
return doflag ;
}
UBYTE set_params(UBYTE x)
{
UBYTE i,doflag ;
/* clear */
clear_command();
/* send initial parameters */
cmdp.param[0] = 0x01 ;
/* color type
0x01 2-bit Gray Scale
0x02 4-bit Gray Scale
0x03 8-bit Gray Scale
0x05 12-bit Color
0x06 16-bit Color
0x07 JPEG
*/
cmdp.param[2] = 0x01 ;
if ( x & 2 ) { cmdp.param[2] = 0x03 ; }
/* preview resolution
0x01 80 x 60
0x03 160 x 120
snap shot resolution
0x01 80 x 64
0x03 160 x 128
0x05 320 x 240
0x07 640 x 480
*/
cmdp.param[3] = 0x01 ;
if ( x & 4 ) { cmdp.param[3] = 0x03 ; }
/* JPEG resolution
0x01 80 x 64
0x03 160 x 128
0x05 320 x 240
0x07 640 x 480
*/
cmdp.param[4] = 0x03 ;
/* send command */
send_command();
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" SET PARAMETERS") ; }
/* wait */
while ( get_capacity() < 6 ) ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" exit PARAMETERS WAIT") ; }
/* copy */
for ( i = 0 ; i < 6 ; i++ ) { *(rbuf+i) = get_ring() ; }
/* judge */
doflag = ON ;
if ( judge_response(0) == RES_ACK && *(rbuf+2) == 1 ) {
doflag = OFF ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" confirm ACK") ; }
}
return doflag ;
}
UBYTE set_snapshot(void)
{
UBYTE i,doflag ;
/* send command */
send_snapshot( sysparam );
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" SET SNAPSHOT") ; }
/* wait */
while ( get_capacity() < 6 ) ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" exit SNAPSHOT WAIT") ; }
/* copy */
for ( i = 0 ; i < 6 ; i++ ) { *(rbuf+i) = get_ring() ; }
/* judege */
doflag = ON ;
if ( judge_response(0) == RES_ACK && *(rbuf+2) == 5 ) {
doflag = OFF ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" confirm ACK") ; }
}
return doflag ;
}
#define SUCCESS 0
#define ERR_PARAMS 1
#define ERR_SNAPSHOT 2
UBYTE get_picture(UBYTE onoff)
{
UWORD i,k,length ;
UBYTE result,doflag ;
/* send parameters */
result = set_params( onoff ) ;
if ( result ) return ERR_PARAMS ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)"SEND PARAMETERS") ; }
/* ? snapshot */
if ( !(onoff & 8) ) {
result = set_snapshot() ;
if ( result ) return ERR_SNAPSHOT ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)"SEND SNAPSHOT") ; }
}
/* send get picture command */
k = 1 ;
if ( sysparam & 8 ) { k = 2 ; } /* preview */
send_getpicture( k ) ;
if ( DFLAG ) { rs_puts_txd1((UBYTE *)"SEND GETPICTURE") ; }
/* wait */
while ( get_capacity() < 12 ) ;
/* copy */
for ( i = 0 ; i < 12 ; i++ ) { *(rbuf+i) = get_ring() ; }
/* judge */
if ( judge_response(0) == RES_ACK && *(rbuf+2) == 4 ) {
k = SUCCESS ;
if ( DFLAG == ON && k == SUCCESS ) { rs_puts_txd1((UBYTE *)" SUCCESS") ; }
}
/* if ( judge_response(6) == RES_DATA && (*(rbuf+8) & 3) ) { */
if ( judge_response(6) == RES_DATA && *(rbuf+8) == 1 ) {
if ( DFLAG ) {
rs_puts_txd1((UBYTE *)" SIZE") ;
i = *(rbuf+9) ;
k = (i >> 4) & MASK0F ; rs_put_txd1( get_hex((UBYTE)k) );
k = i & MASK0F ; rs_put_txd1( get_hex((UBYTE)k) );
i = *(rbuf+10) ;
k = (i >> 4) & MASK0F ; rs_put_txd1( get_hex((UBYTE)k) );
k = i & MASK0F ; rs_put_txd1( get_hex((UBYTE)k) );
i = *(rbuf+11) ;
k = (i >> 4) & MASK0F ; rs_put_txd1( get_hex((UBYTE)k) );
k = i & MASK0F ; rs_put_txd1( get_hex((UBYTE)k) );
}
sram_end = *(rbuf+11) ;
sram_end = sram_end * 256 + *(rbuf+10) ;
sram_end = sram_end * 256 + *(rbuf+9) ;
if ( sram_end > 32767 ) {
rs_puts_txd1((UBYTE *)" OUT OF MEMORY") ;
return 1 ;
}
}
/* store data */
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" STORE IMAGE") ; }
sram_address = 0 ;
while ( sram_address < sram_end ) {
/* check data */
while ( get_capacity() == 0 ) ;
/* store */
k = get_capacity();
for ( i = 0 ; i < k ; i++ ) {
put_sram( sram_address , get_ring() );
sram_address++ ;
}
}
/* send ACK */
send_ack( 0x0A );
if ( DFLAG ) { rs_puts_txd1((UBYTE *)" send ACK") ; }
return 0 ;
}
/*-----------------------*/
/* Insert user functions */
/*-----------------------*/
void user_initialize(void)
{
/* PORT 4 */
P4DR = MASKFF ; /* disable all */
P4DDR = MASKFF ; /* all outputs */
/* PORT 9 */
PADR = 0 ;
PADDR = MASKFF ; /* all outputs */
/* PORT 9 */
P9DR = 0 ;
P9DDR = 0x03 ;
/* P90,P91 outputs others are inputs */
/* PORT B */
PBDR = 0xca ;
PBDDR = MASKCF ; /* PB5,PB4 inputs , others are outputs */
/* initialize timers */
init_timer0();
/* initialize SCI */
init_SCI0( br14400 );
init_SCI1( br57600 );
/* */
x_flags.DR = 0 ;
pre_ah = 0 ;
/* clear SCI buffer */
*(sbuf+0) = 0 ; sindex = 0 ;
/* system parameters */
/*
2^0 uncompressed 1 / compressed 0
2^1 gray scale 1 8bits / 0 2bits(binary)
2^2 size 1 160 x 128 / 0 80 x 64
2^3 preview 1 / snapshot 0
*/
sysparam = 0x03 ; /* sysparam = 0x07 ; */
init_ring();
pwm_cnt = left_duty = right_duty = 0 ;
dcount = 0 ;
}
void init_timer0(void)
{
/* stop timer */
ITU.TSTR.BIT.STR0 = OFF ;
/* TOER : Timer Output Enable Register
7 **** -> 0
6 **** -> 0
5 EXB4 -> 0
4 EXA4 -> 0
3 EB3 -> 0
2 EB4 -> 0
1 EA4 -> 0
0 EA3 -> 0
*/
ITU.TOER.BYTE = 0 ;
/* TIOR : Timer I/O Control Register
7 **** -> 0
6 IOB2 -> 0 GRB is not output compare match register
5 IOB1 -> 0
4 IOB0 -> 0
3 **** -> 0
2 IOA2 -> 0 GRA is not output compare match register
1 IOA1 -> 0
0 IOA0 -> 0
*/
ITU0.TIOR.BYTE = 0 ;
/* TCR : Timer Control Register
7 **** -> 0
6 CCLR1 -> 0 clear TCNT if GRA = TCNT
5 CCLR0 -> 1
4 CKEG1 -> 0 rising edge
3 CKEG0 -> 0
2 TPSC2 -> 0 φ利用
1 TPSC1 -> 0
0 TPSC0 -> 0
*/
ITU0.TCR.BYTE = 0x20 ;
/* TIER : Timer Interrupt Enable Register
7 **** -> 0
6 *** -> 0
5 *** -> 0
4 *** -> 0
3 *** -> 0
2 OVIE -> 0
1 IMIEB -> 0
0 IMIEA -> 1 select compare match interrupt
*/
ITU0.TIER.BIT.IMIEA = ON ;
/* reference */
ITU0.GRA = ITU0_AREG ;
ITU0.GRB = MASKFFFF ;
/* counter */
ITU0.TCNT = 0 ;
/* start timer */
ITU.TSTR.BIT.STR0 = ON ;
}
void init_SCI0(UWORD x)
{
volatile UWORD i;
SCI0.SCR.BYTE = 0 ;
SCI0.SMR.BYTE = 0 ;
/* data transfer speed */
SCI0.BRR = x ;
/* wait 1 frame */
for (i = 0; i < 3000 ; i++) ;
/* enable Transmmit and Receive with interrupt */
SCI0.SCR.BYTE = 0x70 ;
}
void init_SCI1(UWORD x)
{
volatile UWORD i;
SCI1.SCR.BYTE = 0 ;
SCI1.SMR.BYTE = 0 ;
/* data transfer speed */
SCI1.BRR = x ;
/* wait 1 frame */
for (i = 0; i < 3000 ; i++) ;
/* enable Transmmit and Receive with interrupt */
SCI1.SCR.BYTE = 0x70 ;
}
/*-----------------*/
/* SCI0 1文字出力 */
/*-----------------*/
void rs_put_txd0(UBYTE x)
{
/* wait data transfer */
while ( SCI0.SSR.BIT.TDRE == OFF ) ;
/* put */
SCI0.TDR = x ;
SCI0.SSR.BIT.TDRE = OFF ;
}
/*-----------------*/
/* SCI1 1文字出力 */
/*-----------------*/
void rs_put_txd1(UBYTE x)
{
/* wait data transfer */
while ( SCI1.SSR.BIT.TDRE == OFF ) ;
/* put */
SCI1.TDR = x ;
SCI1.SSR.BIT.TDRE = OFF ;
}
/*-----------------*/
/* SCI1 文字列出力 */
/*-----------------*/
void rs_puts_txd1(UBYTE *x)
{
while ( *x ) {
rs_put_txd1( *x );
x++ ;
}
crlf();
}
/*-----------*/
/* SCI1 改行 */
/*-----------*/
void crlf(void)
{
rs_put_txd1('\r');
rs_put_txd1('\n');
}
/*+++++++++++++++++++++++++++++++++++++*/
/* ITU0 interrupt with compare match A */
/* 1ms interval */
/*+++++++++++++++++++++++++++++++++++++*/
void int_imia0(void)
{
UBYTE dummy ;
/* clear flag */
dummy = ITU0.TSR.BIT.IMFA ;
ITU0.TSR.BIT.IMFA = OFF ;
/* judge */
if ( TTRG ) {
/* decrement counter */
wcnt-- ;
if ( wcnt == 0 ) {
TTRG = OFF ;
TFLAG = ON ;
}
}
/* pwm handling */
pwm_cnt++ ;
if ( pwm_cnt == 100 ) { pwm_cnt = 0 ; }
LMOTOR = RMOTOR = OFF ;
if ( pwm_cnt < left_duty ) { LMOTOR = ON ; }
if ( pwm_cnt < right_duty ) { RMOTOR = ON ; }
PB.DR.BIT.B2 = LMOTOR ;
PB.DR.BIT.B0 = RMOTOR ;
}
/*-----------------*/
/* Framing Error 0 */
/*-----------------*/
void int_eri0(void)
{
/* Framing Error */
SCI0.SSR.BYTE &= ~0x30 ;
}
/*---------------------*/
/* receive interrupt 0 */
/*---------------------*/
void int_rxi0(void)
{
UBYTE ch0,dummy ;
/* clear flag */
dummy = SCI0.SSR.BYTE ;
SCI0.SSR.BIT.RDRF = OFF ;
/* get a character */
ch0 = SCI0.RDR ;
/* store */
put_ring( ch0 ) ;
}
/*-----------------*/
/* Framing Error 1 */
/*-----------------*/
void int_eri1(void)
{
/* Framing Error */
SCI1.SSR.BYTE &= ~0x30 ;
}
/*---------------------*/
/* receive interrupt 1 */
/*---------------------*/
void int_rxi1(void)
{
volatile UBYTE ch,dummy ;
/* clear flag */
dummy = SCI1.SSR.BYTE ;
SCI1.SSR.BIT.RDRF = OFF ;
/* get a character */
ch = SCI1.RDR ;
/* store */
*(sbuf+sindex) = ch ;
sindex++ ;
/* check */
if ( ch == '\r' ) {
*(sbuf+sindex) = 0 ;
sindex = 0 ;
S1FLAG = ON ;
}
}
UBYTE judge_response(UBYTE x)
{
UBYTE result ;
/* set default */
result = RES_NONE ;
/* check command */
if ( *(rbuf+x) == 0xAA ) {
if ( *(rbuf+x+1) == 0x0A ) { result = RES_DATA ; }
if ( *(rbuf+x+1) == 0x0D ) { result = RES_SYNC ; }
if ( *(rbuf+x+1) == 0x0E ) { result = RES_ACK ; }
if ( *(rbuf+x+1) == 0x0F ) { result = RES_NAK ; }
}
return result ;
}
/*----------------*/
/* SRAMデータ設定 */
/*----------------*/
void put_sram(UWORD adr,UBYTE dat)
{
UBYTE result ;
/* send address */
update_address(adr);
/* send data */
PADR = dat ;
/* wait */
/* enable WE */
SRAM_OE = LOFF ;
SRAM_WE = LON ;
/* wait */
result = 0 ;
/* disable WE */
SRAM_WE = LOFF ;
}
/*----------------*/
/* SRAMデータ取得 */
/*----------------*/
UBYTE get_sram(UWORD adr)
{
UBYTE result ;
/* send address */
update_address(adr);
/* change input */
PADDR = 0x00 ;
/* enable OE */
SRAM_WE = LOFF ;
SRAM_OE = LON ;
/* wait */
/* get 1 byte */
result = PADR ;
/* disable OE */
SRAM_OE = LOFF ;
/* change output */
PADDR = 0xff ;
return result ;
}
/*------------------*/
/* SRAMアドレス更新 */
/*------------------*/
void update_address(UWORD x)
{
UBYTE ah ;
UBYTE al ;
/* send upper byte */
ah = x / 256 ;
if ( ah != pre_ah ) {
PADR = ah ;
atrg(TRG_UPPER | ON ) ;
pre_ah = ah ;
atrg(TRG_UPPER | OFF) ;
}
/* send lowrebyte */
al = x % 256 ;
PADR = al ;
atrg(TRG_LOWER | ON ) ;
al = ah ;
atrg(TRG_LOWER | OFF) ;
}
/*--------------*/
/* データラッチ */
/*--------------*/
void atrg(UBYTE x)
{
if ( x == 0 ) { ALT = OFF ; }
if ( x == 1 ) { ALT = ON ; }
if ( x == 2 ) { AHT = OFF ; }
if ( x == 3 ) { AHT = ON ; }
}
void send_command(void)
{
/* 0xAA */
rs_put_txd0( 0xAA ) ;
/* command */
rs_put_txd0( cmdp.param[0] );
/* parameters */
rs_put_txd0( cmdp.param[1] );
rs_put_txd0( cmdp.param[2] );
rs_put_txd0( cmdp.param[3] );
rs_put_txd0( cmdp.param[4] );
}
void clear_command(void)
{
cmdp.param[0] = cmdp.param[1] = cmdp.param[2] = cmdp.param[3] = 0 ;
cmdp.param[4] = 0 ;
}
UBYTE get_hex(UBYTE x)
{
UBYTE result ;
/* default */
result = 0 ;
/* convert */
if ( x < 10 ) { result = x + '0' ; }
if ( 9 < x && x < 16 ) { result = x - 10 + 'A' ; }
return result ;
}
void send_sync(void)
{
clear_command();
/* command */
cmdp.param[0] = 0x0D ;
/* send */
send_command() ;
}
void send_ack(UBYTE x)
{
clear_command();
/* command */
cmdp.param[0] = 0x0E ; cmdp.param[1] = x ;
/* send */
send_command() ;
}
void send_snapshot(UBYTE onoff)
{
clear_command();
/* command */
cmdp.param[0] = 0x05 ;
cmdp.param[1] = 0x00 ; /* compressed */
if ( onoff & 1 ) { cmdp.param[1] = 0x01 ; } /* uncompressed */
/* send */
send_command() ;
}
void send_getpicture(UBYTE onoff)
{
clear_command();
/* command */
cmdp.param[0] = 0x04 ;
cmdp.param[1] = onoff ;
/* send */
send_command() ;
}
UBYTE delay_ms(UWORD x)
{
/* clear flag */
TFLAG = OFF ;
/* set counter */
wcnt = x ;
/* set flag */
TTRG = ON ;
/* wait */
while ( TTRG == ON ) ;
/* */
return TFLAG ;
}
UWORD get_capacity(void)
{
return sring.cap ;
}
void put_ring(UBYTE x)
{
/* store data */
sring.dat[sring.wp] = x ;
/* update pointer */
sring.wp += 1 ;
if ( sring.wp == 256 ) { sring.wp = 0 ; }
/* capacity increment */
sring.cap += 1 ;
}
UBYTE get_ring(void)
{
UBYTE result ;
/* load data */
result = sring.dat[sring.rp] ;
/* update pointer */
sring.rp += 1 ;
if ( sring.rp == 256 ) { sring.rp = 0 ; }
/* capacity decrement */
sring.cap -= 1 ;
return result ;
}
void init_ring(void)
{
sring.cap = sring.wp = sring.rp = 0 ;
}
目次
前
次