aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Rahm <joshuarahm@gmail.com>2018-01-23 23:13:16 -0700
committerJosh Rahm <joshuarahm@gmail.com>2018-01-23 23:13:16 -0700
commitacd8afd83da625d36ef39bc01717f29f3b689952 (patch)
tree9ca98e5b0bd66e10e30fd074d55834140637dcaa
parentaf0244fb51ccb608440128d0f3303ccce77c14c2 (diff)
downloadstm32l4-acd8afd83da625d36ef39bc01717f29f3b689952.tar.gz
stm32l4-acd8afd83da625d36ef39bc01717f29f3b689952.tar.bz2
stm32l4-acd8afd83da625d36ef39bc01717f29f3b689952.zip
simple uart enabled.
-rw-r--r--README.md5
-rw-r--r--usart/include/common.h12
-rw-r--r--usart/include/usart.h39
-rw-r--r--usart/src/main.c12
-rw-r--r--usart/src/usart.c34
5 files changed, 82 insertions, 20 deletions
diff --git a/README.md b/README.md
index 5d97395..8630ab2 100644
--- a/README.md
+++ b/README.md
@@ -23,3 +23,8 @@ Requirements
* st-flash -- This can be found [here](https://github.com/texane/stlink.git)
* perl
* Cross compiler for arm -- Mine is installed with prefix `arm-unknown-eabi-`.
+
+Resources
+---------
+
+Datasheet may be found [here](http://www.st.com/content/ccc/resource/technical/document/reference_manual/group0/b0/ac/3e/8f/6d/21/47/af/DM00151940/files/DM00151940.pdf/jcr:content/translations/en.DM00151940.pdf)
diff --git a/usart/include/common.h b/usart/include/common.h
index d88d82d..9d5c7cd 100644
--- a/usart/include/common.h
+++ b/usart/include/common.h
@@ -1,6 +1,8 @@
#ifndef COMMON__H
#define COMMON__H
+#include <stdint.h>
+
/* Define __IO to be volatile if it's not already. */
#ifndef __IO
#define __IO volatile
@@ -15,4 +17,14 @@
#define PACKED __attribute__((packed))
#define BIT(n) (1 << (n))
+#define RESERVED_CONCAT_IMPL(x, y) x ## y
+#define RESERVED_MACRO_CONCAT(x, y) RESERVED_CONCAT_IMPL(x, y)
+#define RESERVED(n) \
+ bits_t RESERVED_MACRO_CONCAT(_r, __COUNTER__) :n
+
+#define RESERVE(type) \
+ __IO type RESERVED_MACRO_CONCAT(_r, __COUNTER__)
+
+typedef uint32_t bits_t;
+
#endif /* COMMON_H */
diff --git a/usart/include/usart.h b/usart/include/usart.h
index ebc5458..257aab6 100644
--- a/usart/include/usart.h
+++ b/usart/include/usart.h
@@ -18,7 +18,33 @@ typedef enum {
typedef struct {
/* USART configuration registers 0x04 - 0x0c. */
- uint32_t c_r1;
+ union {
+ uint32_t c_r1;
+ struct {
+ bits_t ue:1; /* UART enable */
+ bits_t uesm:1; /* UART enabled in stop mode. */
+ bits_t re:1; /* reciever enabled. */
+ bits_t te:1; /* transmitter enabled. */
+ bits_t idleie:1; /* Idle interrupt enabled. */
+ bits_t rxneie:1; /* RXNEIE RXNE interrupt enable. */
+ bits_t tcie:1;
+ bits_t txeie:1;
+ bits_t peie:1;
+ bits_t ps:1;
+ bits_t pce:1;
+ bits_t wake:1;
+ bits_t m0:1;
+ bits_t mme:1;
+ bits_t cmie:1;
+ bits_t over8:1;
+ bits_t dedt:4;
+ bits_t deat:4;
+ bits_t rtoie:1;
+ bits_t eobie:1;
+ bits_t m1:1;
+ bits_t reserved:3;
+ } PACKED c1_bf;
+ };
uint32_t c_r2;
uint32_t c_r3;
@@ -73,10 +99,9 @@ typedef enum {
} usart_parity_t;
typedef enum {
- USART_ENABLE_TX_RX = 0x0d, /* 0b1101 */
- USART_ENABLE_TX = 0x09, /* 0b1001 */
- USART_ENABLE_RX = 0x05, /* 0b0101 */
- USART_ENABLE_DISABLED = 0x00, /* 0b0000 */
+ USART_ENABLE_TX = 0x02,
+ USART_ENABLE_RX = 0x01,
+ USART_ENABLE_DISABLED = 0x00,
} usart_enable_t;
void usart_set_parity(__IO usart_t* usart, usart_parity_t parity);
@@ -97,6 +122,10 @@ void set_usart2_clock_src(__IO rcc_t* rcc, usart_clk_src_t usart_clk_src);
void set_usart2_clock_enabled(__IO rcc_t* rcc, bool enable);
+void usart_transmit_bytes(
+ __IO usart_t* usart, const uint8_t* bytes, uint32_t n);
+
+void usart_transmit_str(__IO usart_t* usart, const char* str);
#endif /* H__USART_ */
diff --git a/usart/src/main.c b/usart/src/main.c
index 5e33876..5af52ed 100644
--- a/usart/src/main.c
+++ b/usart/src/main.c
@@ -1,3 +1,4 @@
+
#include "clock.h"
#include "delay.h"
#include "gpio.h"
@@ -34,7 +35,7 @@ int enable_usart2(uint32_t baud_rate)
USART2.c_r3 = 0;
usart_set_divisor(&USART2, 16000000 / baud_rate);
- usart_set_enabled(&USART2, USART_ENABLE_TX_RX);
+ usart_set_enabled(&USART2, USART_ENABLE_TX | USART_ENABLE_RX);
}
int enable_usart1(uint32_t baud_rate)
@@ -82,12 +83,11 @@ int main()
gpio_output_pin_t pin1 = set_gpio_pin_output(port_b, PIN_1);
/* Enable a higher clock frequency. */
- // set_system_clock_MHz(80);
+ set_system_clock_MHz(80);
- enable_usart2(9600);
+ enable_usart2(115200);
pin_on(pin3);
- while (true) {
- usart_transmit_byte(&USART2, 0x32);
- }
+ usart_transmit_str(&USART2, "Hello, World\n");
+ for(;;);
}
diff --git a/usart/src/usart.c b/usart/src/usart.c
index 23dbd95..eddfbe7 100644
--- a/usart/src/usart.c
+++ b/usart/src/usart.c
@@ -40,16 +40,15 @@ void usart_set_parity(__IO usart_t* usart, usart_parity_t parity)
void usart_set_enabled(__IO usart_t* usart, usart_enable_t enabled)
{
uint32_t c_r1 = usart->c_r1;
- /* Clear relevant bits. */
- c_r1 &= ~USART_ENABLE_TX_RX;
- /* Set TX/RX enabled, but not actually USART enabled. */
- c_r1 |= (enabled & ~1);
-
- /* Set usart enabled bit separately. */
- c_r1 |= enabled & 1;
-
- usart->c_r1 = c_r1;
+ if (!enabled) {
+ usart->c1_bf.ue = 0;
+ } else {
+ /* Set the rx enabled. */
+ usart->c1_bf.re = !!(enabled & USART_ENABLE_RX);
+ usart->c1_bf.te = !!(enabled & USART_ENABLE_TX);
+ usart->c1_bf.ue = 1;
+ }
}
void usart_transmit_byte(__IO usart_t* usart, uint8_t byte)
@@ -62,3 +61,20 @@ void usart_transmit_byte(__IO usart_t* usart, uint8_t byte)
while (!(usart->is_r & BIT(7)))
;
}
+
+void usart_transmit_bytes(__IO usart_t* usart, const uint8_t* bytes, uint32_t n)
+{
+ while (n --) {
+ usart_transmit_byte(usart, *(bytes ++));
+ }
+}
+
+void usart_transmit_str(__IO usart_t* usart, const char* str)
+{
+ while (*str) {
+ if (*str == '\n') {
+ usart_transmit_byte(usart, '\r');
+ }
+ usart_transmit_byte(usart, *(str ++));
+ }
+}