23 static void (*usart1_isr_rx)(uint8_t c);
24 static void (*usart1_isr_tx)(void);
27 static uint8_t usart1_set_baud(uint8_t baud);
28 static uint8_t usart1_set_stop(uint8_t stop);
29 static uint8_t usart1_set_parity(uint8_t parity);
30 static uint8_t usart1_set_data(uint8_t data);
31 static uint8_t usart1_setup(
void);
32 static void usart1_default_rx_isr(uint8_t byte);
33 static void usart1_default_tx_isr(
void);
34 static void usart1_put(uint8_t c);
35 static uint8_t usart1_get(
void);
42 static void (*usart2_isr_rx)(uint8_t c);
43 static void (*usart2_isr_tx)(void);
46 static uint8_t usart2_set_baud(uint8_t baud);
47 static uint8_t usart2_set_stop(uint8_t stop);
48 static uint8_t usart2_set_parity(uint8_t parity);
49 static uint8_t usart2_set_data(uint8_t data);
50 static uint8_t usart2_setup(
void);
51 static void usart2_default_rx_isr(uint8_t byte);
52 static void usart2_default_tx_isr(
void);
53 static void usart2_put(uint8_t c);
54 static uint8_t usart2_get(
void);
61 static void (*usart3_isr_rx)(uint8_t c);
62 static void (*usart3_isr_tx)(void);
65 static uint8_t usart3_set_baud(uint8_t baud);
66 static uint8_t usart3_set_stop(uint8_t stop);
67 static uint8_t usart3_set_parity(uint8_t parity);
68 static uint8_t usart3_set_data(uint8_t data);
69 static uint8_t usart3_setup(
void);
70 static void usart3_default_rx_isr(uint8_t byte);
71 static void usart3_default_tx_isr(
void);
72 static void usart3_put(uint8_t c);
73 static uint8_t usart3_get(
void);
135 rcc_periph_clock_enable(RCC_USART1);
144 USART_CR1(USART1) |= USART_CR1_RE;
147 USART_CR1(USART1) |= USART_CR1_RXNEIE;
152 USART_CR1(USART1) |= USART_CR1_TCIE;
155 USART_CR1(USART1) |= USART_CR1_TE;
158 USART_CR1(USART1) |= USART_CR1_UE;
161 nvic_enable_irq(NVIC_USART1_IRQ);
175 rcc_periph_clock_enable(RCC_USART2);
184 USART_CR1(USART2) |= USART_CR1_RE;
187 USART_CR1(USART2) |= USART_CR1_RXNEIE;
192 USART_CR1(USART2) |= USART_CR1_TCIE;
195 USART_CR1(USART2) |= USART_CR1_TE;
198 USART_CR1(USART2) |= USART_CR1_UE;
201 nvic_enable_irq(NVIC_USART2_IRQ);
215 rcc_periph_clock_enable(RCC_USART3);
224 USART_CR1(USART3) |= USART_CR1_RE;
227 USART_CR1(USART3) |= USART_CR1_RXNEIE;
232 USART_CR1(USART3) |= USART_CR1_TCIE;
235 USART_CR1(USART3) |= USART_CR1_TE;
238 USART_CR1(USART3) |= USART_CR1_UE;
241 nvic_enable_irq(NVIC_USART3_IRQ);
258 usart1_set_baud(baud);
263 usart2_set_baud(baud);
268 usart3_set_baud(baud);
292 usart_set_baudrate(USART1,2400);
297 usart_set_baudrate(USART1,4800);
302 usart_set_baudrate(USART1,9600);
307 usart_set_baudrate(USART1,19200);
312 usart_set_baudrate(USART1,38400);
316 usart_set_baudrate(USART1,57600);
321 usart_set_baudrate(USART1,115200);
344 usart_set_baudrate(USART2,2400);
349 usart_set_baudrate(USART2,4800);
354 usart_set_baudrate(USART2,9600);
359 usart_set_baudrate(USART2,19200);
364 usart_set_baudrate(USART2,38400);
368 usart_set_baudrate(USART2,57600);
373 usart_set_baudrate(USART2,115200);
396 usart_set_baudrate(USART3,2400);
401 usart_set_baudrate(USART3,4800);
406 usart_set_baudrate(USART3,9600);
411 usart_set_baudrate(USART3,19200);
416 usart_set_baudrate(USART3,38400);
420 usart_set_baudrate(USART3,57600);
425 usart_set_baudrate(USART3,115200);
451 usart1_set_stop(stop);
456 usart2_set_stop(stop);
461 usart3_set_stop(stop);
485 usart_set_stopbits(USART1,USART_STOPBITS_1);
490 usart_set_stopbits(USART1,USART_STOPBITS_2);
513 usart_set_stopbits(USART2,USART_STOPBITS_1);
518 usart_set_stopbits(USART2,USART_STOPBITS_2);
541 usart_set_stopbits(USART3,USART_STOPBITS_1);
546 usart_set_stopbits(USART3,USART_STOPBITS_2);
572 usart1_set_data(data);
577 usart2_set_data(data);
582 usart3_set_data(data);
605 usart_set_databits(USART1,8);
610 usart_set_databits(USART1,9);
634 usart_set_databits(USART2,8);
639 usart_set_databits(USART2,9);
663 usart_set_databits(USART3,8);
668 usart_set_databits(USART3,9);
695 usart1_set_parity(parity);
700 usart2_set_parity(parity);
705 usart3_set_parity(parity);
729 usart_set_databits(USART1,USART_PARITY_EVEN);
734 usart_set_databits(USART1,USART_PARITY_ODD);
739 usart_set_databits(USART1,USART_PARITY_NONE);
763 usart_set_databits(USART2,USART_PARITY_EVEN);
768 usart_set_databits(USART2,USART_PARITY_ODD);
773 usart_set_databits(USART2,USART_PARITY_NONE);
797 usart_set_databits(USART3,USART_PARITY_EVEN);
802 usart_set_databits(USART3,USART_PARITY_ODD);
807 usart_set_databits(USART3,USART_PARITY_NONE);
926 void usart1_put(uint8_t byte){
929 while(!(USART_SR(USART1) & USART_SR_TXE));
931 USART_DR(USART1) = byte;
935 void usart2_put(uint8_t byte){
938 while(!(USART_SR(USART2) & USART_SR_TXE));
940 USART_DR(USART2) = byte;
944 void usart3_put(uint8_t byte){
947 while(!(USART_SR(USART3) & USART_SR_TXE));
949 USART_DR(USART3) = byte;
953 uint8_t usart1_get(
void){
956 while(!(USART_SR(USART1) & USART_SR_RXNE));
958 return USART_DR(USART1);
962 uint8_t usart2_get(
void){
965 while(!(USART_SR(USART2) & USART_SR_RXNE));
967 return USART_DR(USART2);
971 uint8_t usart3_get(
void){
974 while(!(USART_SR(USART3) & USART_SR_RXNE));
976 return USART_DR(USART3);
991 usart1_isr_rx = new_handler;
996 usart2_isr_rx = new_handler;
1001 usart3_isr_rx = new_handler;
1023 usart1_isr_tx = new_handler;
1028 usart2_isr_tx = new_handler;
1033 usart3_isr_tx = new_handler;
1059 USART_CR1(USART1) &= ~USART_CR1_RXNEIE;
1064 USART_CR1(USART1) &= ~USART_CR1_TCIE;
1065 USART_CR1(USART1) &= ~USART_CR1_TXEIE;
1073 nvic_disable_irq(NVIC_USART1_IRQ);
1082 USART_CR1(USART2) &= ~USART_CR1_RXNEIE;
1087 USART_CR1(USART2) &= ~USART_CR1_TCIE;
1088 USART_CR1(USART2) &= ~USART_CR1_TXEIE;
1096 nvic_disable_irq(NVIC_USART2_IRQ);
1105 USART_CR1(USART3) &= ~USART_CR1_RXNEIE;
1110 USART_CR1(USART3) &= ~USART_CR1_TCIE;
1111 USART_CR1(USART3) &= ~USART_CR1_TXEIE;
1119 nvic_disable_irq(NVIC_USART3_IRQ);
1147 USART_DR(USART1) = 0;
1150 USART_CR1(USART1) |= USART_CR1_RXNEIE;
1153 nvic_enable_irq(NVIC_USART1_IRQ);
1158 USART_DR(USART1) = 0;
1161 USART_CR1(USART1) |= USART_CR1_TCIE;
1164 nvic_enable_irq(NVIC_USART1_IRQ);
1177 USART_DR(USART2) = 0;
1180 USART_CR1(USART2) |= USART_CR1_RXNEIE;
1183 nvic_enable_irq(NVIC_USART2_IRQ);
1188 USART_DR(USART2) = 0;
1191 USART_CR1(USART2) |= USART_CR1_TCIE;
1194 nvic_enable_irq(NVIC_USART2_IRQ);
1207 USART_DR(USART3) = 0;
1210 USART_CR1(USART3) |= USART_CR1_RXNEIE;
1213 nvic_enable_irq(NVIC_USART3_IRQ);
1218 USART_DR(USART3) = 0;
1221 USART_CR1(USART3) |= USART_CR1_TCIE;
1224 nvic_enable_irq(NVIC_USART3_IRQ);
1250 void usart1_isr (
void)
1253 if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART1) & USART_SR_RXNE) != 0)) {
1255 usart1_isr_rx(USART_DR(USART1));
1259 if (((USART_CR1(USART1) & USART_CR1_TCIE) != 0) && ((USART_SR(USART1) & USART_SR_TC) != 0)) {
1261 USART_SR(USART1) &= ~USART_SR_TC;
1266 void usart2_isr (
void)
1269 if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART2) & USART_SR_RXNE) != 0)) {
1271 usart2_isr_rx(USART_DR(USART2));
1275 if (((USART_CR1(USART2) & USART_CR1_TCIE) != 0) && ((USART_SR(USART2) & USART_SR_TC) != 0)) {
1277 USART_SR(USART2) &= ~USART_SR_TC;
1282 void usart3_isr (
void)
1285 if (((USART_CR1(USART3) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART3) & USART_SR_RXNE) != 0)) {
1287 usart3_isr_rx(USART_DR(USART3));
1291 if (((USART_CR1(USART3) & USART_CR1_TCIE) != 0) && ((USART_SR(USART3) & USART_SR_TC) != 0)) {
1293 USART_SR(USART3) &= ~USART_SR_TC;
1302 void usart1_default_rx_isr(uint8_t c){
1309 void usart1_default_tx_isr(
void){
1315 void usart2_default_rx_isr(uint8_t c){
1322 void usart2_default_tx_isr(
void){
1328 void usart3_default_rx_isr(uint8_t c){
1335 void usart3_default_tx_isr(
void){
mcu_error USART_setStop(usart_periph peripheral, usart_stop stop)
Set the number of stop bits for the port.
mcu_error USART_setTxISR(usart_periph peripheral, void(*new_handler)(void))
Sets the target function called when transmit ISR is triggered.
Header file for stm32f103cb USART.
#define USART_DEFAULT_DATA
The default data frame used by setup function.
usart_isr
ISR values for the port.
mcu_error USART_setData(usart_periph peripheral, usart_data data)
Set the data frame size for the port.
#define USART_DEFAULT_PARITY
The default parity used by setup function.
Even parity for the port.
v_fp_u8 USART_add_put(usart_periph peripheral)
Return the address of the peripheral put function.
void MCU_printError(mcu_error errorNum)
Print a given error number as a character stream.
#define USART_DEFAULT_BAUD
The default baud rate used by setup function.
mcu_error USART_disableISR(usart_periph peripheral, usart_isr isr_target)
Disables the specified ISR function.
uint8_t MCU_debugIsEnabled(void)
Checks if debug is enabled.
usart_data
Data frame lengths for the port.
#define USART_DEFAULT_STOP
The default stop bits used by setup function.
usart_periph
Peripheral ports available on the MCU.
Two stop bits for the port.
usart_stop
Stop bit options available for the port.
Error USART: Baud rate not available.
Setup the port for USART receive.
mcu_error USART_enableISR(usart_periph peripheral, usart_isr isr_target)
Enables the specified ISR function.
Error USART: Port doesn't exist.
Setup the port for USART transmit.
void(* v_fp_u8)(uint8_t)
Function pointer typedef for void function with uint8_t parameter.
Error USART: Parity option not available.
mcu_error USART_setParity(usart_periph peripheral, usart_parity parity)
Set the parity for the port.
Eight bit data frame for the port.
usart_baud
Baud rate options for the port.
uint8_t USART_get(usart_periph peripheral)
Get a byte on the given peripheral (blocking).
mcu_error pinSetup(gpio_mode mode, gpio_port port, gpio_pin pin)
Setup a GPIO pin for a given function.
mcu_error USART_setup(usart_periph peripheral)
Initialize USART port.
Error USART: Interrupt option doesn't exist.
mcu_error USART_put(usart_periph peripheral, uint8_t byte)
Send a byte on the given peripheral.
usart_parity
Parity option available for the port.
Error USART: Stop bits don't exist.
One stop bit for the port.
mcu_error USART_setRxISR(usart_periph peripheral, void(*new_handler)(uint8_t received))
Sets the target function called when recieve ISR is triggered.
Error USART: Data frame not available.
mcu_error USART_setBaud(usart_periph peripheral, usart_baud baud)
Set the baud rate for the port.
Nine bit data frame for the port.
mcu_error
Error enumerators for the Debug peripheral.