/** Reconfigures the USART to match the current serial port settings issued by the host as closely as possible. */ void ReconfigureUSART(void) { uint8_t ConfigMask = 0; /* Determine parity - non odd/even parity mode defaults to no parity */ if (LineCoding.ParityType == Parity_Odd) ConfigMask = ((1 << UPM11) | (1 << UPM10)); else if (LineCoding.ParityType == Parity_Even) ConfigMask = (1 << UPM11); /* Determine stop bits - 1.5 stop bits is set as 1 stop bit due to hardware limitations */ if (LineCoding.CharFormat == TwoStopBits) ConfigMask |= (1 << USBS1); /* Determine data size - 5, 6, 7, or 8 bits are supported */ if (LineCoding.DataBits == 6) ConfigMask |= (1 << UCSZ10); else if (LineCoding.DataBits == 7) ConfigMask |= (1 << UCSZ11); else if (LineCoding.DataBits == 8) ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10)); /* Enable double speed, gives better error percentages at 8MHz */ UCSR1A = (1 << U2X1); /* Enable transmit and receive modules and interrupts */ UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); /* Set the USART mode to the mask generated by the Line Coding options */ UCSR1C = ConfigMask; /* Set the USART baud rate register to the desired baud rate value */ UBRR1 = SERIAL_2X_UBBRVAL((uint16_t)LineCoding.BaudRateBPS); }
/** Configures the board hardware and chip peripherals for the demo's functionality. */ void SetupHardware(void) { /* Disable watchdog if enabled by bootloader/fuses */ MCUSR &= ~(1 << WDRF); wdt_disable(); /* Hardware Initialization */ Serial_Init(115200, false); LEDs_Init(); USB_Init(); /* Start the flush timer so that overflows occur rapidly to push received bytes to the USB interface */ TCCR0B = (1 << CS02); /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */ UCSR1B = 0; UCSR1A = 0; UCSR1C = 0; UBRR1 = SERIAL_2X_UBBRVAL(115200); UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10)); UCSR1A = (1 << U2X1); UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); }
void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { uint8_t ConfigMask = 0; switch (CDCInterfaceInfo->State.LineEncoding.ParityType) { case CDC_PARITY_Odd: { ConfigMask = ((1 << UPM11) | (1 << UPM10)); break; } case CDC_PARITY_Even: { ConfigMask = (1 << UPM11); break; } } if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits) { ConfigMask |= (1 << USBS1); } switch (CDCInterfaceInfo->State.LineEncoding.DataBits) { case 6: { ConfigMask |= (1 << UCSZ10); break; } case 7: { ConfigMask |= (1 << UCSZ11); break; } case 8: { ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10)); break; } } PORTD |= (1 << 3); /* Keep the TX line held high (idle) while the USART is reconfigured */ /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */ UCSR1B = 0; UCSR1A = 0; UCSR1C = 0; UBRR1 = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); /* Set the new baud rate before configuring the USART */ /* Reconfigure the USART in double speed mode for a wider baud rate range at the expense of accuracy */ UCSR1C = ConfigMask; UCSR1A = (1 << U2X1); UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); PORTD &= ~(1 << 3); /* Release the TX line after the USART has been reconfigured */ }
/** Event handler for the CDC Class driver Line Encoding Changed event. * * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced */ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { uint8_t ConfigMask = 0; switch (CDCInterfaceInfo->State.LineEncoding.ParityType) { case CDC_PARITY_Odd: ConfigMask = ((1 << UPM11) | (1 << UPM10)); break; case CDC_PARITY_Even: ConfigMask = (1 << UPM11); break; } if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits) ConfigMask |= (1 << USBS1); switch (CDCInterfaceInfo->State.LineEncoding.DataBits) { case 6: ConfigMask |= (1 << UCSZ10); break; case 7: ConfigMask |= (1 << UCSZ11); break; case 8: ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10)); break; } /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */ UCSR1B = 0; UCSR1A = 0; UCSR1C = 0; /* Set the new baud rate before configuring the USART */ UBRR1 = SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); /* Reconfigure the USART in double speed mode for a wider baud rate range at the expense of accuracy */ UCSR1C = ConfigMask; UCSR1A = (1 << U2X1); UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); // If the baudrate is at this special (and unlikely) value, it means that we want to trigger the bootloader if (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 1200){ needs_bootload = true; } }
/************************************************************************* Function: uart_init() Purpose: initialize UART and set baudrate Input: baudrate using macro UART_BAUD_SELECT() Returns: none **************************************************************************/ void uart_init(uint32_t baudrate, bool doublespeed) { UART_TxHead = 0; UART_TxTail = 0; UART_RxHead = 0; UART_RxTail = 0; UBRR1 = (doublespeed ? SERIAL_2X_UBBRVAL(baudrate) : SERIAL_UBBRVAL(baudrate)); UCSR1C = ((1 << UCSZ11) | (1 << UCSZ10)); UCSR1A = (doublespeed ? (1 << U2X1) : 0); UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); DDRD |= (1 << 3); PORTD |= (1 << 2); }/* uart_init */
/** change serial configuration between atmega and jennic */ void Serial_Config(uint32_t Baudrate, uint8_t DataBits, uint8_t StopBits, uint8_t Parity) { uint8_t ConfigMask = 0; switch (Parity) { case CDC_PARITY_Odd: ConfigMask = ((1 << UPM11) | (1 << UPM10)); break; case CDC_PARITY_Even: ConfigMask = (1 << UPM11); break; } if (StopBits == CDC_LINEENCODING_TwoStopBits) ConfigMask |= (1 << USBS1); switch (DataBits) { case 6: ConfigMask |= (1 << UCSZ10); break; case 7: ConfigMask |= (1 << UCSZ11); break; case 8: ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10)); break; } /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */ UCSR1B = 0; UCSR1A = 0; UCSR1C = 0; /* Set the new baud rate before configuring the USART */ UBRR1 = SERIAL_2X_UBBRVAL(Baudrate); UCSR1A = (1 << U2X1); UCSR1C = ConfigMask; UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); /* enable RTS/CTS flow control XXX: conflict with CC3000 */ //UCSR1D = (1<<RTSEN)|(1<<CTSEN); }
/** Event handler for the CDC Class driver Line Encoding Changed event. * * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced */ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { uint8_t ConfigMask = 0; switch (CDCInterfaceInfo->State.LineEncoding.ParityType) { case CDC_PARITY_Odd: ConfigMask = ((1 << UPM11) | (1 << UPM10)); break; case CDC_PARITY_Even: ConfigMask = (1 << UPM11); break; } if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits) ConfigMask |= (1 << USBS1); switch (CDCInterfaceInfo->State.LineEncoding.DataBits) { case 6: ConfigMask |= (1 << UCSZ10); break; case 7: ConfigMask |= (1 << UCSZ11); break; case 8: ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10)); break; } /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */ UCSR1B = 0; UCSR1A = 0; UCSR1C = 0; /* Special case 57600 baud for compatibility with the ATmega328 bootloader. */ UBRR1 = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) ? SERIAL_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS) : SERIAL_2X_UBBRVAL(CDCInterfaceInfo->State.LineEncoding.BaudRateBPS); UCSR1C = ConfigMask; UCSR1A = (CDCInterfaceInfo->State.LineEncoding.BaudRateBPS == 57600) ? 0 : (1 << U2X1); UCSR1B = ((1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1)); }
/** Event handler for the CDC Class driver Line Encoding Changed event. * * \param[in] CDCInterfaceInfo Pointer to the CDC class interface configuration structure being referenced */ void EVENT_CDC_Device_LineEncodingChanged(USB_ClassInfo_CDC_Device_t* const CDCInterfaceInfo) { uint8_t ConfigMask = 0; switch (CDCInterfaceInfo->State.LineEncoding.ParityType) { case CDC_PARITY_Odd: ConfigMask = ((1 << UPM11) | (1 << UPM10)); break; case CDC_PARITY_Even: ConfigMask = (1 << UPM11); break; } if (CDCInterfaceInfo->State.LineEncoding.CharFormat == CDC_LINEENCODING_TwoStopBits) ConfigMask |= (1 << USBS1); switch (CDCInterfaceInfo->State.LineEncoding.DataBits) { case 6: ConfigMask |= (1 << UCSZ10); break; case 7: ConfigMask |= (1 << UCSZ11); break; case 8: ConfigMask |= ((1 << UCSZ11) | (1 << UCSZ10)); break; } /* Must turn off USART before reconfiguring it, otherwise incorrect operation may occur */ UCSR1B = 0; UCSR1A = 0; UCSR1C = 0; long bps = CDCInterfaceInfo->State.LineEncoding.BaudRateBPS; Selected1200BPS = (bps == 1200); UBRR1 = SERIAL_2X_UBBRVAL(bps); UCSR1C = ConfigMask; UCSR1A = (1 << U2X1); UCSR1B = (1 << RXCIE1) | (1 << TXEN1) | (1 << RXEN1); }