aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/libopenstm32/usart.h54
-rw-r--r--lib/usart.c12
2 files changed, 39 insertions, 27 deletions
diff --git a/include/libopenstm32/usart.h b/include/libopenstm32/usart.h
index 7271ba7..3f93d1b 100644
--- a/include/libopenstm32/usart.h
+++ b/include/libopenstm32/usart.h
@@ -125,7 +125,7 @@
/* --- USART_DR values ----------------------------------------------------- */
/* USART_DR[8:0]: DR[8:0]: Data value */
-#define USART_DR_MASK 0xFF
+#define USART_DR_MASK 0x1FF
/* --- USART_BRR values ---------------------------------------------------- */
@@ -178,26 +178,18 @@
/* SBK: Send break */
#define USART_CR1_SBK (1 << 0)
-/* CR1_PCE / CR1_PS combined values */
-#define USART_PARITY_NONE 0x00
-#define USART_PARITY_ODD 0x02
-#define USART_PARITY_EVEN 0x03
-
-/* CR1_TE/CR1_RE combined values */
-#define USART_MODE_RX 0x01
-#define USART_MODE_TX 0x02
-#define USART_MODE_TX_RX 0x03
-
/* --- USART_CR2 values ---------------------------------------------------- */
/* LINEN: LIN mode enable */
#define USART_CR2_LINEN (1 << 14)
/* STOP[13:12]: STOP bits */
-#define USART_STOPBITS_1 0x00 /* 1 stop bit */
-#define USART_STOPBITS_0_5 0x01 /* 0.5 stop bits */
-#define USART_STOPBITS_2 0x02 /* 2 stop bits */
-#define USART_STOPBITS_1_5 0x03 /* 1.5 stop bits */
+#define USART_CR2_STOPBITS_1 (0x00 << 12) /* 1 stop bit */
+#define USART_CR2_STOPBITS_0_5 (0x01 << 12) /* 0.5 stop bits */
+#define USART_CR2_STOPBITS_2 (0x02 << 12) /* 2 stop bits */
+#define USART_CR2_STOPBITS_1_5 (0x03 << 12) /* 1.5 stop bits */
+#define USART_CR2_STOPBITS_MASK (0x03 << 12)
+#define USART_CR2_STOPBITS_SHIFT 12
/* CLKEN: Clock enable */
#define USART_CR2_CLKEN (1 << 11)
@@ -262,12 +254,6 @@
/* EIE: Error interrupt enable */
#define USART_CR3_EIE (1 << 0)
-/* CR3_CTSE/CR3_RTSE combined values */
-#define USART_FLOWCONTROL_NONE 0x00
-#define USART_FLOWCONTROL_RTS 0x01
-#define USART_FLOWCONTROL_CTS 0x02
-#define USART_FLOWCONTROL_RTS_CTS 0x03
-
/* --- USART_GTPR values --------------------------------------------------- */
/* GT[7:0]: Guard time value */
@@ -280,6 +266,32 @@
/* TODO */ /* Note to Uwe: what needs to be done here? */
+/* --- Convenience defines ------------------------------------------------- */
+
+/* CR1_PCE / CR1_PS combined values */
+#define USART_PARITY_NONE 0x00
+#define USART_PARITY_ODD USART_CR1_PS
+#define USART_PARITY_EVEN (USART_CR1_PS | USART_CR1_PCE)
+#define USART_PARITY_MASK (USART_CR1_PS | USART_CR1_PCE)
+
+/* CR1_TE/CR1_RE combined values */
+#define USART_MODE_RX USART_CR1_RE
+#define USART_MODE_TX USART_CR1_TE
+#define USART_MODE_TX_RX (USART_CR1_RE | USART_CR1_TE)
+#define USART_MODE_MASK (USART_CR1_RE | USART_CR1_TE)
+
+#define USART_STOPBITS_1 USART_CR2_STOPBITS_1 /* 1 stop bit */
+#define USART_STOPBITS_0_5 USART_CR2_STOPBITS_0_5 /* 0.5 stop bits */
+#define USART_STOPBITS_2 USART_CR2_STOPBITS_2 /* 2 stop bits */
+#define USART_STOPBITS_1_5 USART_CR2_STOPBITS_1_5 /* 1.5 stop bits */
+
+/* CR3_CTSE/CR3_RTSE combined values */
+#define USART_FLOWCONTROL_NONE 0x00
+#define USART_FLOWCONTROL_RTS USART_CR3_RTSE
+#define USART_FLOWCONTROL_CTS USART_CR3_CTSE
+#define USART_FLOWCONTROL_RTS_CTS (USART_CR3_RTSE | USART_CR3_CTSE)
+#define USART_FLOWCONTROL_MASK (USART_CR3_RTSE | USART_CR3_CTSE)
+
/* --- Function prototypes ------------------------------------------------- */
void usart_set_baudrate(u32 usart, u32 baud);
diff --git a/lib/usart.c b/lib/usart.c
index 09feb6e..6f3905f 100644
--- a/lib/usart.c
+++ b/lib/usart.c
@@ -40,7 +40,7 @@ void usart_set_stopbits(u32 usart, u32 stopbits)
u32 reg32;
reg32 = USART_CR2(usart);
- reg32 = (reg32 & ~((1 << 13) | (1 << 12))) | (stopbits << 12);
+ reg32 = (reg32 & ~USART_CR2_STOPBITS_MASK) | stopbits;
USART_CR2(usart) = reg32;
}
@@ -49,7 +49,7 @@ void usart_set_parity(u32 usart, u32 parity)
u32 reg32;
reg32 = USART_CR1(usart);
- reg32 = (reg32 & ~((1 << 10) | (1 << 9))) | (parity << 9);
+ reg32 = (reg32 & ~USART_PARITY_MASK) | parity;
USART_CR1(usart) = reg32;
}
@@ -58,7 +58,7 @@ void usart_set_mode(u32 usart, u32 mode)
u32 reg32;
reg32 = USART_CR1(usart);
- reg32 = (reg32 & ~((1 << 3) | (1 << 2))) | (mode << 2);
+ reg32 = (reg32 & ~USART_MODE_MASK) | mode;
USART_CR1(usart) = reg32;
}
@@ -67,7 +67,7 @@ void usart_set_flow_control(u32 usart, u32 flowcontrol)
u32 reg32;
reg32 = USART_CR3(usart);
- reg32 = (reg32 & ~((1 << 9) | (1 << 8))) | (flowcontrol << 2);
+ reg32 = (reg32 & ~USART_FLOWCONTROL_MASK) | flowcontrol;
USART_CR3(usart) = reg32;
}
@@ -84,13 +84,13 @@ void usart_disable(u32 usart)
void usart_send(u32 usart, u16 data)
{
/* Send data. */
- USART_DR(usart) = (data & 0x1ff);
+ USART_DR(usart) = (data & USART_DR_MASK);
}
u16 usart_recv(u32 usart)
{
/* Receive data. */
- return USART_DR(usart) & 0x1ff;
+ return USART_DR(usart) & USART_DR_MASK;
}
void usart_wait_send_ready(u32 usart)