[En-Nut-Discussion] MASK and SPACE support for 9bit frame

power ip powerip at gmail.com
Sun Sep 23 15:41:31 CEST 2007


i changed usartavr.c,add MASK and SPACE support for 9bit frame
   MASK - Transmit as address frame bit9=1
   SPACE - Transmit as data frame bit9=0
   //set SPACE
   u_long i1=3;
   _ioctl(_fileno(stream), UART_SETPARITY,&i1);
   //set MASK
   u_long i1=4;
   _ioctl(_fileno(stream), UART_SETPARITY,&i1);
   static void AvrUsartTxEmpty(void *arg) {
   ....
           if (tx_aframe==1) {
             //mask,bit9=1
               sbi(UCSRnB, UCSZ2);
               sbi(UCSRnB, TXB8);
           }  else
           if (tx_aframe==2) {
             //space bit9=0
               sbi(UCSRnB, UCSZ2);

- Ignored:
               cbi(UCSRnB, TXB8);
           };
    ...
   }


   static int AvrUsartSetParity(u_char mode)
   {
   #ifdef __AVR_ENHANCED__
       AvrUsartDisable();
       switch (mode) {
       case 0:

           cbi(UCSRnC, UPM0);
           cbi(UCSRnC, UPM1);
           //don't send bit 9
           tx_aframe=0;
           break;
       case 1:
           sbi(UCSRnC, UPM0);
           sbi(UCSRnC, UPM1);
           tx_aframe=0;
           break;
       case 2:
           cbi(UCSRnC, UPM0);
           sbi(UCSRnC, UPM1);
           tx_aframe=0;
           break;
       case 3:
           //space
           //Clear Parity
           cbi(UCSRnC, UPM0);
           cbi(UCSRnC, UPM1);
           sbi(UCSRnB, UCSZ2);
           //send bit 9=0 is space
           tx_aframe=2;
           break;
       case 4:
           //mask
           //Clear Parity
           cbi(UCSRnC, UPM0);
           cbi(UCSRnC, UPM1);
           sbi(UCSRnB, UCSZ2);
           //send bit 9=1 is mask
           tx_aframe=1;
           break;
       }
       AvrUsartEnable();
   #endif

       /*
        * Verify the result.
        */
       if (AvrUsartGetParity() != mode) {
           return -1;
       }
       return 0;
   }


   static u_char AvrUsartGetParity(void)
   {
   #ifdef __AVR_ENHANCED__

       if (tx_aframe==1)
          {//mask
           return 4;
          } else
       if (tx_aframe==1)
          {//space
           return 3;
          };

       if (bit_is_set(UCSRnC, UPM1)) {
           if (bit_is_set(UCSRnC, UPM0)) {
               return 1;
           } else {
               return 2;
           }
       }
   #endif
       return 0;
   }



More information about the En-Nut-Discussion mailing list