void msp430_uart_write(uint8_t *data, uint8_t length) { uint8_t i = (_txBuf.head + 1) % UART_BUFFER_SIZE; uint8_t len; // If the output buffer is full, there's nothing for it other than to // wait for the interrupt handler to empty it a bit // ???: return 0 here instead? while (i == _txBuf.tail); _txBuf.buf[_txBuf.head] = *data; _txBuf.head = i; // Enable USCI_A1 TX interrupt USCI_A_UART_enableInterrupt(USCI_A1_BASE, USCI_A_UART_TRANSMIT_INTERRUPT); // Enable interrupt for(len = 0; len <length - 1; len++) { data++; i = (_txBuf.head + 1) % UART_BUFFER_SIZE; while (i == _txBuf.tail); { // Enable USCI_A1 TX interrupt USCI_A_UART_enableInterrupt(USCI_A1_BASE, USCI_A_UART_TRANSMIT_INTERRUPT); // Enable interrupt } _txBuf.buf[_txBuf.head] = *data; _txBuf.head = i; } }
t_uint8 _Device_Uart_Module_1_Enable(t_uint32 baud_rate){ //setUSCI_X TXD GPIO_setAsPeripheralModuleFunctionInputPin( USART_Module_1_TX_PORT, USART_Module_1_TX_PIN ); //setUSCI_X RXD GPIO_setAsPeripheralModuleFunctionInputPin( USART_Module_1_RX_PORT, USART_Module_1_RX_PIN ); if ( STATUS_FAIL == USCI_A_UART_init(UART_Module_1_USCI_A_BASEADDRESS, USCI_A_UART_CLOCKSOURCE_SMCLK, UCS_getSMCLK(UCS_BASE), baud_rate, USCI_A_UART_NO_PARITY, USCI_A_UART_LSB_FIRST, USCI_A_UART_ONE_STOP_BIT, USCI_A_UART_MODE, USCI_A_UART_LOW_FREQUENCY_BAUDRATE_GENERATION )){ return Func_Failure; } //Enable UART module for operation USCI_A_UART_enable(UART_Module_1_USCI_A_BASEADDRESS); //Enable Receive Interrupt USCI_A_UART_clearInterruptFlag(UART_Module_1_USCI_A_BASEADDRESS, USCI_A_UART_RECEIVE_INTERRUPT); USCI_A_UART_enableInterrupt(UART_Module_1_USCI_A_BASEADDRESS, USCI_A_UART_RECEIVE_INTERRUPT); //Enter LPM3, interrupts enabled //__bis_SR_register(LPM3_bits + GIE); __no_operation(); Interrupt_UART_ReceiveData_ptr_fuc = Empty_UART_fun; SendingWhileTimeOutCount = 0; return Func_Success; }
static inline void _uart0_peripheral_init(void){ USCI_A_UART_initParam param = {0}; param.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK; param.clockPrescalar = _UART0_DIV_INT; param.firstModReg = 0; param.secondModReg = (uint8_t)_UART0_BRS_VAL; param.parity = USCI_A_UART_NO_PARITY; param.msborLsbFirst = USCI_A_UART_LSB_FIRST; param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT; param.uartMode = USCI_A_UART_MODE; param.overSampling = USCI_A_UART_LOW_FREQUENCY_BAUDRATE_GENERATION; USCI_A_UART_init(uC_UART0_BASE, ¶m); USCI_A_UART_enable(uC_UART0_BASE); USCI_A_UART_clearInterrupt(uC_UART0_BASE, USCI_A_UART_RECEIVE_INTERRUPT); USCI_A_UART_enableInterrupt(uC_UART0_BASE, USCI_A_UART_RECEIVE_INTERRUPT); }
void msp430_uart_init(unsigned long baud) { unsigned long divider; unsigned long _smclk; _smclk = msp430_get_smclk_freq(); /**Configure UART pins * Set P4.4 and P4.5 as Secondary Module Function Input. * Select Port 4 * Set Pin 4, 5 to input Secondary Module Function, (UCA1TXD/UCA1SIMO, UCA1RXD/UCA1SOMI). **/ GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P4, GPIO_PIN4 + GPIO_PIN5 ); divider=(_smclk<<3)/baud; //fixed-point number: q3 // Configure UART USCI_A_UART_initParam param = {0}; param.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK; param.clockPrescalar = divider>>3; param.firstModReg = 0; param.secondModReg = (divider & 0x07); param.parity = USCI_A_UART_NO_PARITY; param.msborLsbFirst = USCI_A_UART_LSB_FIRST; param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT; param.uartMode = USCI_A_UART_MODE; param.overSampling = USCI_A_UART_LOW_FREQUENCY_BAUDRATE_GENERATION; USCI_A_UART_init(USCI_A1_BASE, ¶m); USCI_A_UART_enable(USCI_A1_BASE); USCI_A_UART_clearInterrupt(USCI_A1_BASE, USCI_A_UART_RECEIVE_INTERRUPT_FLAG); // Enable USCI_A1 RX interrupt USCI_A_UART_enableInterrupt(USCI_A1_BASE, USCI_A_UART_RECEIVE_INTERRUPT); // Enable interrupt }
/*P5.6 P5.7 串口屏*/ int UartScreenInit(void){ GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P5, GPIO_PIN6 + GPIO_PIN7 ); //Initialize UART module in auto baudrate detection multiprocessor mode //Baudrate = 9600, clock freq = 1.048MHz //UCBRx = 6, UCBRFx = 13, UCBRSx = 0, UCOS16 = 1 USCI_A_UART_initParam param = {0}; param.selectClockSource = USCI_A_UART_CLOCKSOURCE_SMCLK; param.clockPrescalar = 6; param.firstModReg = 13; param.secondModReg = 0; param.parity = USCI_A_UART_NO_PARITY; param.msborLsbFirst = USCI_A_UART_LSB_FIRST; param.numberofStopBits = USCI_A_UART_ONE_STOP_BIT; param.uartMode = USCI_A_UART_AUTOMATIC_BAUDRATE_DETECTION_MODE; param.overSampling = USCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION; if(STATUS_FAIL == USCI_A_UART_init(USCI_A1_BASE, ¶m)) { return 0; } //Enable UART module for operation USCI_A_UART_enable(USCI_A1_BASE); //Put UART to sleep/dormant mode toreceive break/sync first // USCI_A_UART_setDormant(USCI_A1_BASE); USCI_A_UART_transmitBreak(USCI_A1_BASE); //Enable Receive Interrupt USCI_A_UART_clearInterrupt(USCI_A1_BASE, USCI_A_UART_RECEIVE_INTERRUPT); USCI_A_UART_enableInterrupt(USCI_A1_BASE, USCI_A_UART_RECEIVE_INTERRUPT); return 1; }