Contiki-NG
Loading...
Searching...
No Matches
uart0.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026, RISE Research Institutes of Sweden AB
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30/**
31 * \file
32 * eUSCI_A0 UART driver for MSP430FR5969
33 *
34 * Uses eUSCI_A0 module with P2.0 (TXD) and P2.1 (RXD) for
35 * backchannel UART communication.
36 */
37
38#include "contiki.h"
39#include "dev/uart0.h"
40#include "sys/energest.h"
41#include "isr_compat.h"
42#include <msp430.h>
43
44static int (*uart0_input_handler)(unsigned char c);
45
46/*---------------------------------------------------------------------------*/
47int
48uart0_active(void)
49{
50 return (UCA0STATW & UCBUSY) != 0;
51}
52/*---------------------------------------------------------------------------*/
53void
54uart0_set_input(int (*input)(unsigned char c))
55{
56 uart0_input_handler = input;
57}
58/*---------------------------------------------------------------------------*/
59void
60uart0_writeb(unsigned char c)
61{
62 /* Wait for previous transmission to complete */
63 while(!(UCA0IFG & UCTXIFG));
64
65 UCA0TXBUF = c;
66}
67/*---------------------------------------------------------------------------*/
68void
69uart0_init(unsigned long ubr)
70{
71 /* The baud rate is fixed at 115200 with an 8 MHz SMCLK; ubr is
72 * accepted for API compatibility with other MSP430 UART drivers but
73 * is not used to derive the divider values. */
74 (void)ubr;
75
76 /* Put eUSCI in reset */
77 UCA0CTLW0 = UCSWRST;
78
79 /* Configure eUSCI_A0: 8N1, SMCLK source */
80 UCA0CTLW0 |= UCSSEL__SMCLK;
81
82 /* Set baud rate at 115200 with oversampling (8 MHz SMCLK):
83 * 8000000 / 115200 = 69.44 → UCBRx = 4, UCBRF = 5, UCBRS = 0x55. */
84 UCA0BRW = 4;
85 UCA0MCTLW = (5 << 4) | (0x55 << 8) | UCOS16;
86
87 /* Configure pins: P2.0 = TXD (output), P2.1 = RXD (input).
88 * msp430_cpu_init() previously drove every port pin as an output
89 * low, so the RX direction must be reset explicitly before the
90 * eUSCI peripheral function is selected. */
91 P2DIR |= BIT0;
92 P2DIR &= ~BIT1;
93 P2SEL0 &= ~(BIT0 | BIT1);
94 P2SEL1 |= BIT0 | BIT1;
95
96 /* Release from reset */
97 UCA0CTLW0 &= ~UCSWRST;
98
99 /* Enable RX interrupt */
100 UCA0IE |= UCRXIE;
101}
102/*---------------------------------------------------------------------------*/
103ISR(USCI_A0, uart0_rx_interrupt)
104{
105 uint8_t c;
106
107 ENERGEST_ON(ENERGEST_TYPE_IRQ);
108
109 switch(UCA0IV) {
110 case USCI_UART_UCRXIFG:
111 c = UCA0RXBUF;
112 if(uart0_input_handler != NULL) {
113 if(uart0_input_handler(c)) {
114 LPM4_EXIT;
115 }
116 }
117 break;
118 }
119
120 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
121}
122/*---------------------------------------------------------------------------*/
Header file for the energy estimation mechanism.
void uart0_init(void)
Initializes the UART driver.
Definition uart0-arch.c:83