⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bdm.c

📁 freecale单片机基于bdm接口的一些源代码
💻 C
📖 第 1 页 / 共 5 页
字号:
    LDX   ,X                /* load X with value on the PTA port (including BDM_IN) */
		BIT   ,X                /* wait 9 cycles, iterate the loop while waiting (7 cycles) */
		LSLX                    /* shift BDM_IN into C */
		LSLX                    /* LSLX needs to be after the BIT as it chages C */  
    DEC   j		              
    BNE   loop
    ROL   i                 /* shift C into i (from the bottom) */
    LDA   i                 /* load the result into A */
  }
}
/* 1.227 - 1.772 MHz */
unsigned char bdm_rx10(void) {
  asm {
    LDA   #BDM_DIR1_MASK+RESET_OUT_MASK	/* contents of A will be driven to PTA in order to switch the driver off */
    MOV   #8,j              /* store number of iterations into j */
    CLRH                    /* prepare H */
  loop:
    CLRX                    /* clear X to point to PTA, CLRX has been moved to the beginning of the algorithm here to prevent its execution for the last bit */
    STX   ,X                /* drive BDM low */
    BRN   0                 /* wait 3 cycles */
    STA   ,X                /* switch BDM to high impedance */
    BIT   ,X                /* wait 15 cycles */
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BRN   0
    LDX   ,X                /* load X with value on the PTA port (including BDM_IN) */
		LSLX                    /* shift BDM_IN into C */
		LSLX
    ROL   i                 /* shift C into i (from the bottom) */
    BIT   ,X                /* wait 13 cycles, iterate the loop while waiting (7 cycles), ROL takes 4 cycles */
    DEC   j		              
    BNE   loop
    LDA   i                 /* load the result into A */
  }
}
/* 1 - 1.444 MHz */
unsigned char bdm_rx11(void) {
  asm {
    LDA   #BDM_DIR1_MASK+RESET_OUT_MASK	/* contents of A will be driven to PTA in order to switch the driver off */
    MOV   #8,j              /* store number of iterations into j */
    CLRH                    /* prepare H */
  loop:
    CLRX                    /* clear X to point to PTA, CLRX has been moved to the beginning of the algorithm here to prevent its execution for the last bit */
    STX   ,X                /* drive BDM low */
    BIT   ,X                /* wait 4 cycles */
    BIT   ,X                
    STA   ,X                /* switch BDM to high impedance */
    BIT   ,X                /* wait 19 cycles */
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BRN   0
    LDX   ,X                /* load X with value on the PTA port (including BDM_IN) */
		LSLX                    /* shift BDM_IN into C */
		LSLX
    ROL   i                 /* shift C into i (from the bottom) */
    BIT   ,X                /* wait 16 cycles, iterate the loop while waiting (7 cycles), ROL takes 4 cycles */
    BRN   0
    DEC   j		              
    BNE   loop
    LDA   i                 /* load the result into A */
  }
}
/* 0.818 - 1.181 MHz */
unsigned char bdm_rx12(void) {
  asm {
    LDA   #BDM_DIR1_MASK+RESET_OUT_MASK	/* contents of A will be driven to PTA in order to switch the driver off */
    MOV   #8,j              /* store number of iterations into j */
    CLRH                    /* prepare H */
  loop:
    CLRX                    /* clear X to point to PTA, CLRX has been moved to the beginning of the algorithm here to prevent its execution for the last bit */
    STX   ,X                /* drive BDM low */
    BIT   ,X                /* wait 6 cycles */
    BIT   ,X                
    BIT   ,X                
    STA   ,X                /* switch BDM to high impedance */
    MOV   #2,k              /* wait 23 cycles (= 4 + 2*9 + 1) */
  wait1:
    BIT   ,X
    DEC   k
    BNE   wait1
    NOP
    LDX   ,X                /* load X with value on the PTA port (including BDM_IN) */
		LSLX                    /* shift BDM_IN into C */
		LSLX
    ROL   i                 /* shift C into i (from the bottom) */
    BIT   ,X                /* wait 21 cycles, iterate the loop while waiting (7 cycles), ROL takes 4 cycles */
    BIT   ,X
    BIT   ,X
    BIT   ,X
    BIT   ,X
    DEC   j		              
    BNE   loop
    LDA   i                 /* load the result into A */
  }
}
/* 0.658 - 0.951 MHz */
unsigned char bdm_rx13(void) {
  asm {
    LDA   #BDM_DIR1_MASK+RESET_OUT_MASK	/* contents of A will be driven to PTA in order to switch the driver off */
    MOV   #8,j              /* store number of iterations into j */
    CLRH                    /* prepare H */
  loop:
    CLRX                    /* clear X to point to PTA, CLRX has been moved to the beginning of the algorithm here to prevent its execution for the last bit */
    STX   ,X                /* drive BDM low */
    BIT   ,X                /* wait 8 cycles */
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    STA   ,X                /* switch BDM to high impedance */
    MOV   #3,k              /* wait 29 cycles (= 4 + 3*8 + 1) */
  wait1:
    NOP
    DEC   k
    BNE   wait1
    NOP
    LDX   ,X                /* load X with value on the PTA port (including BDM_IN) */
		LSLX                    /* shift BDM_IN into C */
		LSLX
    ROL   i                 /* shift C into i (from the bottom) */
    BIT   ,X                /* wait 27 cycles, iterate the loop while waiting (7 cycles), ROL takes 4 cycles */
    BIT   ,X
    BIT   ,X
    BIT   ,X
    BIT   ,X
    BIT   ,X
    BIT   ,X
    BIT   ,X
    DEC   j		              
    BNE   loop
    LDA   i                 /* load the result into A */
  }
}
/* 0.529 - 0.764 MHz */
unsigned char bdm_rx14(void) {
  asm {
    LDA   #BDM_DIR1_MASK+RESET_OUT_MASK	/* contents of A will be driven to PTA in order to switch the driver off */
    MOV   #8,j              /* store number of iterations into j */
    CLRH                    /* prepare H */
  loop:
    CLRX                    /* clear X to point to PTA, CLRX has been moved to the beginning of the algorithm here to prevent its execution for the last bit */
    STX   ,X                /* drive BDM low */
    BIT   ,X                /* wait 10 cycles */
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    STA   ,X                /* switch BDM to high impedance */
    MOV   #4,k              /* wait 37 cycles (= 4 + 4*8 + 1) */
  wait1:
    NOP
    DEC   k
    BNE   wait1
    NOP
    LDX   ,X                /* load X with value on the PTA port (including BDM_IN) */
		LSLX                    /* shift BDM_IN into C */
		LSLX
    ROL   i                 /* shift C into i (from the bottom) */
    MOV   #2,k              /* wait 35 cycles, iterate the loop while waiting (7 cycles), ROL takes 4 cycles */
  wait2:		  							/* 35 = 4 + 2*10 + 4 + 7 */
    BRN   0
    DEC   k
    BNE   wait2
    DEC   j		              
    BNE   loop
    LDA   i                 /* load the result into A */
  }
}
/* 0.421 - 0.609 MHz */
unsigned char bdm_rx15(void) {
  asm {
    LDA   #BDM_DIR1_MASK+RESET_OUT_MASK	/* contents of A will be driven to PTA in order to switch the driver off */
    MOV   #8,j              /* store number of iterations into j */
    CLRH                    /* prepare H */
  loop:
    CLRX                    /* clear X to point to PTA, CLRX has been moved to the beginning of the algorithm here to prevent its execution for the last bit */
    STX   ,X                /* drive BDM low */
    BIT   ,X                /* wait 13 cycles */
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BIT   ,X                
    BRN   0
    STA   ,X                /* switch BDM to high impedance */
    MOV   #6,k              /* wait 46 cycles (= 4 + 6*7) */ /* wait 47 cycles (= 4 + 6*7 + 1) */
  wait1:
    DEC   k
    BNE   wait1
/*  NOP  */
    LDX   ,X                /* load X with value on the PTA port (including BDM_IN) */
		LSLX                    /* shift BDM_IN into C */
		LSLX
    ROL   i                 /* shift C into i (from the bottom) */
    MOV   #3,k              /* wait 45 cycles, iterate the loop while waiting (7 cycles), ROL takes 4 cycles */
  wait2:		  							/* 45 = 4 + 3*10 + 4 + 7 */
    BRN   0
    DEC   k
    BNE   wait2
    DEC   j		              
    BNE   loop
    LDA   i                 /* load the result into A */
  }
}

/* decodes values recorded by RX functions */
/* expects LSB data in X and remaining 7 bytes on stack */
/* it is expected that caller will JUMP into this routine */
void rx_stack_decode(void) {
  asm {
    MOV   #8,i
  decode:  
    ROLX                    /* get the interesting bit into C (it is bit 6) */
    ROLX
    RORA                    /* and rotate it into A from the top */
    PULX                    /* get the next value from stack */
    DEC   i
    BNE   decode
    PSHX                    /* that was one pop too many, so push the value back */
  }				
}

/* transmit 8 bits of data, MSB first */
/* expects DIR2 active and DIR1 inactive (call bdm_tx_prepare) */ 
/* target frequency 6.6 - 8.4 MHz (this is BDM frequency (=crystal/2)) */
void bdm_tx1(unsigned char data) {
  asm {
    CLRX					/* HX points to PTA */
    CLRH
    MOV   #0,BDM_DIR2_PORT    /* start driving the BDM */
    /* bit 7 (MSB) */
    SEC           /* set C */
    CLR   ,X      /* bring BDM low */
    STA   ,X      /* assign value from MSB of A to BDM */
    RORA          /* set MSB of A */
    STA   ,X      /* bring BDM high */
    ROLA          /* advance to next bit */
    ROLA          
    /* bit 6 */
    SEC           /* set C */
    CLR   ,X      /* bring BDM low */
    STA   ,X      /* assign value from MSB of A to BDM */
    RORA          /* set MSB of A */
    STA   ,X      /* bring BDM high */
    ROLA          /* advance to next bit */
    ROLA          
    /* bit 5 */
    SEC           /* set C */
    CLR   ,X      /* bring BDM low */
    STA   ,X      /* assign value from MSB of A to BDM */
    RORA          /* set MSB of A */
    STA   ,X      /* bring BDM high */
    ROLA          /* advance to next bit */
    ROLA          
    /* bit 4 */
    SEC           /* set C */
    CLR   ,X      /* bring BDM low */
    STA   ,X      /* assign value from MSB of A to BDM */
    RORA          /* set MSB of A */
    STA   ,X      /* bring BDM high */
    ROLA          /* advance to next bit */
    ROLA          
    /* bit 3 */
    SEC           /* set C */
    CLR   ,X      /* bring BDM low */
    STA   ,X      /* assign value from MSB of A to BDM */
    RORA          /* set MSB of A */
    STA   ,X      /* bring BDM high */
    ROLA          /* advance to next bit */
    ROLA          
    /* bit 2 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -