main.s
来自「mega128平台下」· S 代码 · 共 1,005 行 · 第 1/2 页
S
1,005 行
.module main.c
.area data(ram, con, rel)
_unreceive_timer::
.blkb 2
.area idata
.byte 0,0
.area data(ram, con, rel)
_receive_flag::
.blkb 2
.area idata
.byte 0,0
.area data(ram, con, rel)
.area text(rom, con, rel)
.even
_port_init::
; #include <iom128v.h>
; #include <macros.h>
; #include "stdio.h"
; #include "../api/schedule.h"
; #include "../api/cc1100.h"
; #include "../api/led.h"
; #include "../api/timer.h"
; #include "../api/comm.h"
;
; #define LEN_SEND_MSG 12
; #define SYNC_INTERVAL 500 //实际间隔为50ms 因为发送过程有30ms的延时
; #define CLUSTER_NUMBER 2
; #define NODE_NUMBER 2
;
; COMM_MSG_CMD SYNC_MSG, RADIO_ACK_MSG;
; unsigned char SyncSeqNo;
; unsigned char ask_seqno;
; unsigned char unreceive_timer[CLUSTER_NUMBER]= {0,0};
; unsigned char receive_flag[CLUSTER_NUMBER]= {0,0};
; unsigned int crc;
; static unsigned char Data[28]; //server-sink串口数据
;
;
; typedef struct NodeData{
; unsigned char ID;
; unsigned char temp;
; unsigned char vol;
; }NodeData;
; NodeData *Node[CLUSTER_NUMBER][NODE_NUMBER];
;
;
; //unsigned char len_COMM_MSG = (sizeof(COMM_MSG))-1;
;
; /**************************************************
; * port init
; *
; * cc1100
; * IN G1 o
; * GD0O D1 i
; * CSN C2 o
; * SCLK G0 o
; * OUT C1 i
; *
; * led
; * led0 C4 o
; * led1 C5 o
; * led2 C6 o
; ************************************************** */
; void port_init(void)
; {
; PORTA = 0x00;
clr R2
out 0x1b,R2
; DDRA = 0x00;
out 0x1a,R2
; PORTB = 0x00;
out 0x18,R2
; DDRB = 0x00;
out 0x17,R2
; PORTC = 0x00; //m103 output only
out 0x15,R2
; DDRC = 0x74;
ldi R24,116
out 0x14,R24
; PORTD = 0x00;
out 0x12,R2
; DDRD = 0x00;
out 0x11,R2
; PORTE = 0x00;
out 0x3,R2
; DDRE = 0x00;
out 0x2,R2
; PORTF = 0x00;
sts 98,R2
; DDRF = 0x00;
sts 97,R2
; PORTG = 0x00;
sts 101,R2
; DDRG = 0x03;
ldi R24,3
sts 100,R24
L4:
.dbline 0 ; func end
ret
.even
_uart0_init::
; }
;
;
; /**************************************************
; //UART0 initialize
; // desired baud rate: 57600
; // actual: baud rate:57600 (0.0%)
; // char size: 8 bit
; // parity: Disabled
; **************************************************/
; void uart0_init(void)
; {
; UCSR0B = 0x00; //disable while setting baud rate
clr R2
out 0xa,R2
; UCSR0A = 0x00;
out 0xb,R2
; UCSR0C = 0x06;
ldi R24,6
sts 149,R24
; UBRR0L = 0x07; //set baud rate lo
ldi R24,7
out 0x9,R24
; UBRR0H = 0x00; //set baud rate hi
sts 144,R2
; UCSR0B = 0xD8;
ldi R24,216
out 0xa,R24
L5:
.dbline 0 ; func end
ret
.even
_init_devices::
; }
;
; //call this routine to initialize all peripherals
; void init_devices(void)
; {
; //stop errant interrupts until set up
; CLI(); //disable all interrupts
cli
; XDIV = 0x00; //xtal divider
clr R2
out 0x3c,R2
; XMCRA = 0x00; //external memory
sts 109,R2
; port_init(); //端口初始化
xcall _port_init
; led_init(); //所有灯灭
xcall _led_init
; T0_init();
xcall _T0_init
; T2_init();
xcall _T2_init
; T1_init();
xcall _T1_init
; T3_init();
xcall _T3_init
; CC1100_init(); //RF初始化
xcall _CC1100_init
; uart0_init(); //串口初始化
xcall _uart0_init
;
; MCUCR = 0x00;
clr R2
out 0x35,R2
; EICRA = 0x00; //extended ext ints
sts 106,R2
; EICRB = 0x00; //extended ext ints
out 0x3a,R2
; EIMSK = 0x00;
out 0x39,R2
; TIMSK = 0x00; //timer interrupt sources
out 0x37,R2
; ETIMSK = 0x00; //extended timer interrupt sources
sts 125,R2
; SEI(); //re-enable interrupts
sei
L6:
.dbline 0 ; func end
ret
; i -> R20
; b -> R18
; intcrc -> R16,R17
.even
_xcrc_byte::
xcall push_gset1
; //all peripherals are now initialized
; }
;
;
; /******************************************************
; * crc校验
; *
; *******************************************************/
; unsigned short int xcrc_byte(unsigned short intcrc, unsigned char b)
; {
; unsigned char i;
;
; crc = crc ^ b << 8;
mov R2,R18
clr R3
mov R3,R2
clr R2
lds R4,_crc
lds R5,_crc+1
eor R4,R2
eor R5,R3
sts _crc+1,R5
sts _crc,R4
; i = 8;
ldi R20,8
L8:
; do{
; if (crc & 0x8000)
lds R2,_crc
lds R3,_crc+1
sbrs R3,7
rjmp L11
; crc = crc << 1 ^ 0x1021;
ldi R24,4129
ldi R25,16
lsl R2
rol R3
eor R2,R24
eor R3,R25
sts _crc+1,R3
sts _crc,R2
xjmp L12
L11:
; else
; crc = crc << 1;
lds R2,_crc
lds R3,_crc+1
lsl R2
rol R3
sts _crc+1,R3
sts _crc,R2
L12:
L9:
; }while (--i);
mov R24,R20
subi R24,1
mov R20,R24
tst R24
brne L8
;
; return crc;
lds R16,_crc
lds R17,_crc+1
L7:
xcall pop_gset1
.dbline 0 ; func end
ret
; crc -> R20,R21
; count -> R22,R23
; index -> R10,R11
; packet -> R12,R13
.even
_xcrc_calc::
xcall push_gset4
movw R10,R18
movw R12,R16
ldd R22,y+8
ldd R23,y+9
; }
;
; unsigned int xcrc_calc(char *packet, int index, int count)
; {
; int crc = 0;
clr R20
clr R21
xjmp L15
L14:
movw R2,R10
movw R24,R2
adiw R24,1
movw R10,R24
movw R30,R2
add R30,R12
adc R31,R13
ldd R18,z+0
movw R16,R20
xcall _xcrc_byte
movw R20,R16
subi R22,1
sbci R23,0
L15:
;
; while (count > 0)
clr R2
clr R3
cp R2,R22
cpc R3,R23
brlt L14
; {
; crc = xcrc_byte(crc, packet[index++]);
; count--;
; }
; return crc;
movw R16,R20
L13:
xcall pop_gset4
.dbline 0 ; func end
ret
.even
_T0_timeout::
; }
;
; /*
; * T0计时中断
; */
;
; void T0_timeout()
; {
;
; SyncSeqNo++;
lds R24,_SyncSeqNo
subi R24,255 ; addi 1
sts _SyncSeqNo,R24
; SYNC_MSG.data[0] = SyncSeqNo;
mov R2,R24
sts _SYNC_MSG+6,R2
; CC1100_send(((char *)&SYNC_MSG),LEN_SEND_MSG+1);
ldi R18,13
ldi R16,<_SYNC_MSG
ldi R17,>_SYNC_MSG
xcall _CC1100_send
; start_T0(0,SYNC_INTERVAL);
ldi R18,500
ldi R19,1
clr R16
xcall _start_T0
;
; if (SyncSeqNo>=2)
lds R24,_SyncSeqNo
cpi R24,2
brlo L19
; stop_T0();
xcall _stop_T0
L19:
L17:
.dbline 0 ; func end
ret
; j -> <dead>
; i -> R16
.even
_T1_timeout::
;
; }
;
; void T1_timeout()
; {
; unsigned char i, j;
; for(i=1; i<=CLUSTER_NUMBER; i++)
ldi R16,1
xjmp L25
L22:
; {
; if(receive_flag[i]==1)
ldi R24,<_receive_flag
ldi R25,>_receive_flag
mov R30,R16
clr R31
add R30,R24
adc R31,R25
ldd R24,z+0
cpi R24,1
brne L26
; {
; unreceive_timer[i] = 0;
ldi R24,<_unreceive_timer
ldi R25,>_unreceive_timer
mov R30,R16
clr R31
add R30,R24
adc R31,R25
clr R2
std z+0,R2
; receive_flag[i] =0;
ldi R24,<_receive_flag
ldi R25,>_receive_flag
mov R30,R16
clr R31
add R30,R24
adc R31,R25
std z+0,R2
; }
xjmp L27
L26:
; else
; unreceive_timer[i]++;
ldi R24,<_unreceive_timer
ldi R25,>_unreceive_timer
mov R2,R16
clr R3
add R2,R24
adc R3,R25
movw R30,R2
ldd R24,z+0
subi R24,255 ; addi 1
std z+0,R24
L27:
;
; if(unreceive_timer[i]>=3)
ldi R24,<_unreceive_timer
ldi R25,>_unreceive_timer
mov R30,R16
clr R31
add R30,R24
adc R31,R25
ldd R24,z+0
cpi R24,3
brlo L28
; {
; /* for(j=0; j<NODE_NUMBER; j++)
; {
; Node[i-1][j]->temp = 0;
; Node[i-1][j]->vol = 0;
; }
; */
; //发送一个故障报告
; }
L28:
L23:
inc R16
L25:
ldi R24,2
cp R24,R16
brlo X0
xjmp L22
X0:
L21:
.dbline 0 ; func end
ret
.even
_T2_timeout::
; }
; }
;
; void T2_timeout()
; {
; led0Toggle();
xcall _led0Toggle
L30:
.dbline 0 ; func end
ret
.even
_T3_timeout::
; }
; void T3_timeout()
; {
; led1Toggle();
xcall _led1Toggle
L31:
.dbline 0 ; func end
ret
.even
_CC1100_sendDone::
; }
;
; void CC1100_sendDone()
; {
; //led0Off();
; return;
L32:
.dbline 0 ; func end
ret
.even
_radio_ack_task::
; }
; /*
; * ACK sink
; */
; void radio_ack_task()
; {
; RADIO_ACK_MSG.len = LEN_SEND_MSG;
ldi R24,12
sts _RADIO_ACK_MSG,R24
; RADIO_ACK_MSG.ID = 0x00;
clr R2
sts _RADIO_ACK_MSG+1,R2
; RADIO_ACK_MSG.GID = 0x00;
sts _RADIO_ACK_MSG+2,R2
; RADIO_ACK_MSG.type = CLUSTER_ACK;
ldi R24,3
sts _RADIO_ACK_MSG+5,R24
; // RADIO_ACK_MSG->crc = xcrc_calc(RADIO_ACK_MSG, 2,17);
; CC1100_send((((char *)&RADIO_ACK_MSG)),LEN_SEND_MSG+1);
ldi R18,13
ldi R16,<_RADIO_ACK_MSG
ldi R17,>_RADIO_ACK_MSG
xcall _CC1100_send
L33:
.dbline 0 ; func end
ret
; i -> <dead>
; msg -> R22,R23
; target -> R22,R23
; cluster -> R10
; receive_msg -> R20,R21
.even
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?