GlobLib
HAL and API libraries for MCUs and hardware.
stm32f103cb_usart.c
Go to the documentation of this file.
1 /*!**************************************************************************
2  @file stm32f103cb_usart.c
3  @brief Source file for stm32f103cb USART
4  @author Stuart Ianna
5  @version 0.1
6  @date May 2018
7  @copyright GNU GPLv3
8  @warning None
9  @bug The USART.get() function is not compatible when using ISRs. This is becuase the data register needs to be read to reset the flags. This cannot be efficiently done in both ISR and get function. This is not a large problem as the get function is blocking, and isn't really used with an ISR.
10 
11  @details
12  @par Compilers
13  - arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
14 ******************************************************************************/
15 
16 #include "stm32f103cb_usart.h"
17 
18 /****************************************************************************/
19 // USART1
20 /****************************************************************************/
21 
22 //ISR function pointers
23 static void (*usart1_isr_rx)(uint8_t c);
24 static void (*usart1_isr_tx)(void);
25 
26 //USART1 routines
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);
36 
37 /****************************************************************************/
38 // USART2
39 /****************************************************************************/
40 
41 //ISR function pointers
42 static void (*usart2_isr_rx)(uint8_t c);
43 static void (*usart2_isr_tx)(void);
44 
45 //USART1 routines
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);
55 
56 /****************************************************************************/
57 // USART3
58 /****************************************************************************/
59 
60 //ISR function pointers
61 static void (*usart3_isr_rx)(uint8_t c);
62 static void (*usart3_isr_tx)(void);
63 
64 //USART1 routines
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);
74 
75 /****************************************************************************/
76 // SETUP FUNCTIONS
77 /****************************************************************************/
78 
80 
81  uint8_t error = E_USART_NOERROR;
82 
83  switch(peripheral){
84 
85  case USART_1:
86 
87  //Direct the ISR routines
88  USART_setTxISR (peripheral, usart1_default_tx_isr);
89  USART_setRxISR (peripheral, usart1_default_rx_isr);
90 
91  //Setup the port
92  usart1_setup();
93  break;
94 
95  case USART_2:
96 
97  //Direct the ISR routines
98  USART_setTxISR (peripheral, usart2_default_tx_isr);
99  USART_setRxISR (peripheral, usart2_default_rx_isr);
100 
101  //Setup the port
102  usart2_setup();
103  break;
104 
105  case USART_3:
106 
107  //Direct the ISR routines
108  USART_setTxISR (peripheral, usart3_default_tx_isr);
109  USART_setRxISR (peripheral, usart3_default_rx_isr);
110 
111  //Setup the port
112  usart3_setup();
113  break;
114  default:
115  error = E_USART_NOPORT;
116 
117  if(MCU_debugIsEnabled()){
119  }
120  break;
121  }
122 
123  return error;
124 }
125 
126 mcu_error usart1_setup(void){
127 
128  uint8_t error = E_USART_NOERROR;
129 
130  //Setup the GPIO pins
133 
134  //Enable USART CLOCK
135  rcc_periph_clock_enable(RCC_USART1);
136 
137  //Set default settings
138  usart1_set_parity(USART_DEFAULT_PARITY);
139  usart1_set_data(USART_DEFAULT_DATA);
140  usart1_set_stop(USART_DEFAULT_STOP);
141  usart1_set_baud(USART_DEFAULT_BAUD);
142 
143  //Enable receive
144  USART_CR1(USART1) |= USART_CR1_RE;
145 
146  //Enable receive interrupts
147  USART_CR1(USART1) |= USART_CR1_RXNEIE;
148 
149  USART_DR(USART1)= 0;
150 
151  //Enable transmit interrupts
152  USART_CR1(USART1) |= USART_CR1_TCIE;
153 
154  //Enable transmit
155  USART_CR1(USART1) |= USART_CR1_TE;
156 
157  //Enable USART1
158  USART_CR1(USART1) |= USART_CR1_UE;
159 
160  //Enable interrupts
161  nvic_enable_irq(NVIC_USART1_IRQ);
162 
163  return error;
164 }
165 
166 mcu_error usart2_setup(void){
167 
168  uint8_t error = E_USART_NOERROR;
169 
170  //Setup the GPIO pins
173 
174  //Enable USART CLOCK
175  rcc_periph_clock_enable(RCC_USART2);
176 
177  //Set default settings
178  usart2_set_parity(USART_DEFAULT_PARITY);
179  usart2_set_data(USART_DEFAULT_DATA);
180  usart2_set_stop(USART_DEFAULT_STOP);
181  usart2_set_baud(USART_DEFAULT_BAUD);
182 
183  //Enable receive
184  USART_CR1(USART2) |= USART_CR1_RE;
185 
186  //Enable receive interrupts
187  USART_CR1(USART2) |= USART_CR1_RXNEIE;
188 
189  USART_DR(USART2)= 0;
190 
191  //Enable transmit interrupts
192  USART_CR1(USART2) |= USART_CR1_TCIE;
193 
194  //Enable transmit
195  USART_CR1(USART2) |= USART_CR1_TE;
196 
197  //Enable USART1
198  USART_CR1(USART2) |= USART_CR1_UE;
199 
200  //Enable interrupts
201  nvic_enable_irq(NVIC_USART2_IRQ);
202 
203  return error;
204 }
205 
206 mcu_error usart3_setup(void){
207 
208  uint8_t error = E_USART_NOERROR;
209 
210  //Setup the GPIO pins
213 
214  //Enable USART CLOCK
215  rcc_periph_clock_enable(RCC_USART3);
216 
217  //Set default settings
218  usart3_set_parity(USART_DEFAULT_PARITY);
219  usart3_set_data(USART_DEFAULT_DATA);
220  usart3_set_stop(USART_DEFAULT_STOP);
221  usart3_set_baud(USART_DEFAULT_BAUD);
222 
223  //Enable receive
224  USART_CR1(USART3) |= USART_CR1_RE;
225 
226  //Enable receive interrupts
227  USART_CR1(USART3) |= USART_CR1_RXNEIE;
228 
229  USART_DR(USART3)= 0;
230 
231  //Enable transmit interrupts
232  USART_CR1(USART3) |= USART_CR1_TCIE;
233 
234  //Enable transmit
235  USART_CR1(USART3) |= USART_CR1_TE;
236 
237  //Enable USART1
238  USART_CR1(USART3) |= USART_CR1_UE;
239 
240  //Enable interrupts
241  nvic_enable_irq(NVIC_USART3_IRQ);
242 
243  return error;
244 }
245 
246 /****************************************************************************/
247 // BAUD FUNCTIONS
248 /****************************************************************************/
249 
251 
252  uint8_t error = E_USART_NOERROR;
253 
254  switch(peripheral){
255 
256  case USART_1:
257 
258  usart1_set_baud(baud);
259  break;
260 
261  case USART_2:
262 
263  usart2_set_baud(baud);
264  break;
265 
266  case USART_3:
267 
268  usart3_set_baud(baud);
269  break;
270 
271  default:
272  error = E_USART_NOPORT;
273 
274  if(MCU_debugIsEnabled()){
276  }
277  break;
278  }
279 
280  return error;
281 
282 }
283 
284 mcu_error usart1_set_baud(usart_baud baud){
285 
286  uint8_t error = E_USART_NOERROR;
287 
288  switch(baud){
289 
290  case USART_BAUD_2400:
291 
292  usart_set_baudrate(USART1,2400);
293  break;
294 
295  case USART_BAUD_4800:
296 
297  usart_set_baudrate(USART1,4800);
298  break;
299 
300  case USART_BAUD_9600:
301 
302  usart_set_baudrate(USART1,9600);
303  break;
304 
305  case USART_BAUD_19200:
306 
307  usart_set_baudrate(USART1,19200);
308  break;
309 
310  case USART_BAUD_38400:
311 
312  usart_set_baudrate(USART1,38400);
313  break;
314 
315  case USART_BAUD_57600:
316  usart_set_baudrate(USART1,57600);
317  break;
318 
319  case USART_BAUD_115200:
320 
321  usart_set_baudrate(USART1,115200);
322  break;
323  default:
324  error = E_USART_NOBAUD;
325 
326  if(MCU_debugIsEnabled()){
328  }
329  break;
330 
331  }
332 
333  return error;
334 }
335 
336 mcu_error usart2_set_baud(usart_baud baud){
337 
338  uint8_t error = E_USART_NOERROR;
339 
340  switch(baud){
341 
342  case USART_BAUD_2400:
343 
344  usart_set_baudrate(USART2,2400);
345  break;
346 
347  case USART_BAUD_4800:
348 
349  usart_set_baudrate(USART2,4800);
350  break;
351 
352  case USART_BAUD_9600:
353 
354  usart_set_baudrate(USART2,9600);
355  break;
356 
357  case USART_BAUD_19200:
358 
359  usart_set_baudrate(USART2,19200);
360  break;
361 
362  case USART_BAUD_38400:
363 
364  usart_set_baudrate(USART2,38400);
365  break;
366 
367  case USART_BAUD_57600:
368  usart_set_baudrate(USART2,57600);
369  break;
370 
371  case USART_BAUD_115200:
372 
373  usart_set_baudrate(USART2,115200);
374  break;
375  default:
376  error = E_USART_NOBAUD;
377 
378  if(MCU_debugIsEnabled()){
380  }
381  break;
382 
383  }
384 
385  return error;
386 }
387 
388 mcu_error usart3_set_baud(usart_baud baud){
389 
390  uint8_t error = E_USART_NOERROR;
391 
392  switch(baud){
393 
394  case USART_BAUD_2400:
395 
396  usart_set_baudrate(USART3,2400);
397  break;
398 
399  case USART_BAUD_4800:
400 
401  usart_set_baudrate(USART3,4800);
402  break;
403 
404  case USART_BAUD_9600:
405 
406  usart_set_baudrate(USART3,9600);
407  break;
408 
409  case USART_BAUD_19200:
410 
411  usart_set_baudrate(USART3,19200);
412  break;
413 
414  case USART_BAUD_38400:
415 
416  usart_set_baudrate(USART3,38400);
417  break;
418 
419  case USART_BAUD_57600:
420  usart_set_baudrate(USART3,57600);
421  break;
422 
423  case USART_BAUD_115200:
424 
425  usart_set_baudrate(USART3,115200);
426  break;
427  default:
428  error = E_USART_NOBAUD;
429 
430  if(MCU_debugIsEnabled()){
432  }
433  break;
434 
435  }
436 
437  return error;
438 }
439 /****************************************************************************/
440 // STOP FUNCTIONS
441 /****************************************************************************/
442 
444 
445  uint8_t error = E_USART_NOERROR;
446 
447  switch(peripheral){
448 
449  case USART_1:
450 
451  usart1_set_stop(stop);
452  break;
453 
454  case USART_2:
455 
456  usart2_set_stop(stop);
457  break;
458 
459  case USART_3:
460 
461  usart3_set_stop(stop);
462  break;
463 
464  default:
465  error = E_USART_NOPORT;
466 
467  if(MCU_debugIsEnabled()){
469  }
470  break;
471  }
472 
473  return error;
474 
475 }
476 
477 mcu_error usart1_set_stop(usart_stop stop){
478 
479  uint8_t error = E_USART_NOERROR;
480 
481  switch(stop){
482 
483  case USART_STOP_ONE:
484 
485  usart_set_stopbits(USART1,USART_STOPBITS_1);
486  break;
487 
488  case USART_STOP_TWO:
489 
490  usart_set_stopbits(USART1,USART_STOPBITS_2);
491  break;
492 
493  default:
494  error = E_USART_NOSTOP;
495 
496  if(MCU_debugIsEnabled()){
498  }
499  break;
500  }
501 
502  return error;
503 }
504 
505 mcu_error usart2_set_stop(usart_stop stop){
506 
507  uint8_t error = E_USART_NOERROR;
508 
509  switch(stop){
510 
511  case USART_STOP_ONE:
512 
513  usart_set_stopbits(USART2,USART_STOPBITS_1);
514  break;
515 
516  case USART_STOP_TWO:
517 
518  usart_set_stopbits(USART2,USART_STOPBITS_2);
519  break;
520 
521  default:
522  error = E_USART_NOSTOP;
523 
524  if(MCU_debugIsEnabled()){
526  }
527  break;
528  }
529 
530  return error;
531 }
532 
533 mcu_error usart3_set_stop(usart_stop stop){
534 
535  uint8_t error = E_USART_NOERROR;
536 
537  switch(stop){
538 
539  case USART_STOP_ONE:
540 
541  usart_set_stopbits(USART3,USART_STOPBITS_1);
542  break;
543 
544  case USART_STOP_TWO:
545 
546  usart_set_stopbits(USART3,USART_STOPBITS_2);
547  break;
548 
549  default:
550  error = E_USART_NOSTOP;
551 
552  if(MCU_debugIsEnabled()){
554  }
555  break;
556  }
557 
558  return error;
559 }
560 
561 /****************************************************************************/
562 // DATA FUNCTIONS
563 /****************************************************************************/
564 
566 
567  uint8_t error = E_USART_NOERROR;
568 
569  switch(peripheral){
570  case USART_1:
571 
572  usart1_set_data(data);
573  break;
574 
575  case USART_2:
576 
577  usart2_set_data(data);
578  break;
579 
580  case USART_3:
581 
582  usart3_set_data(data);
583  break;
584 
585  default:
586  error = E_USART_NOPORT;
587 
588  if(MCU_debugIsEnabled()){
590  }
591  break;
592  }
593 
594  return error;
595 }
596 
597 mcu_error usart1_set_data(usart_data data){
598 
599  uint8_t error = E_USART_NOERROR;
600 
601  switch(data){
602 
603  case USART_DATA_EIGHT:
604 
605  usart_set_databits(USART1,8);
606  break;
607 
608  case USART_DATA_NINE:
609 
610  usart_set_databits(USART1,9);
611  break;
612 
613  default:
614  error = E_USART_NODATA;
615 
616  if(MCU_debugIsEnabled()){
618  }
619  break;
620 
621  }
622 
623  return error;
624 }
625 
626 mcu_error usart2_set_data(usart_data data){
627 
628  uint8_t error = E_USART_NOERROR;
629 
630  switch(data){
631 
632  case USART_DATA_EIGHT:
633 
634  usart_set_databits(USART2,8);
635  break;
636 
637  case USART_DATA_NINE:
638 
639  usart_set_databits(USART2,9);
640  break;
641 
642  default:
643  error = E_USART_NODATA;
644 
645  if(MCU_debugIsEnabled()){
647  }
648  break;
649 
650  }
651 
652  return error;
653 }
654 
655 mcu_error usart3_set_data(usart_data data){
656 
657  uint8_t error = E_USART_NOERROR;
658 
659  switch(data){
660 
661  case USART_DATA_EIGHT:
662 
663  usart_set_databits(USART3,8);
664  break;
665 
666  case USART_DATA_NINE:
667 
668  usart_set_databits(USART3,9);
669  break;
670 
671  default:
672  error = E_USART_NODATA;
673 
674  if(MCU_debugIsEnabled()){
676  }
677  break;
678 
679  }
680 
681  return error;
682 }
683 /****************************************************************************/
684 // PARITY FUNCTIONS
685 /****************************************************************************/
686 
688 
689  uint8_t error = E_USART_NOERROR;
690 
691  switch(peripheral){
692 
693  case USART_1:
694 
695  usart1_set_parity(parity);
696  break;
697 
698  case USART_2:
699 
700  usart2_set_parity(parity);
701  break;
702 
703  case USART_3:
704 
705  usart3_set_parity(parity);
706  break;
707 
708  default:
709  error = E_USART_NOPORT;
710 
711  if(MCU_debugIsEnabled()){
713  }
714  break;
715  }
716 
717  return error;
718 }
719 
720 mcu_error usart1_set_parity(usart_parity parity){
721 
722  uint8_t error = E_USART_NOERROR;
723 
724 
725  switch(parity){
726 
727  case USART_EVEN:
728 
729  usart_set_databits(USART1,USART_PARITY_EVEN);
730  break;
731 
732  case USART_ODD:
733 
734  usart_set_databits(USART1,USART_PARITY_ODD);
735  break;
736 
737  case USART_NONE:
738 
739  usart_set_databits(USART1,USART_PARITY_NONE);
740  break;
741 
742  default:
743  error = E_USART_NOPARITY;
744 
745  if(MCU_debugIsEnabled()){
747  }
748  break;
749  }
750 
751  return error;
752 }
753 
754 mcu_error usart2_set_parity(usart_parity parity){
755 
756  uint8_t error = E_USART_NOERROR;
757 
758 
759  switch(parity){
760 
761  case USART_EVEN:
762 
763  usart_set_databits(USART2,USART_PARITY_EVEN);
764  break;
765 
766  case USART_ODD:
767 
768  usart_set_databits(USART2,USART_PARITY_ODD);
769  break;
770 
771  case USART_NONE:
772 
773  usart_set_databits(USART2,USART_PARITY_NONE);
774  break;
775 
776  default:
777  error = E_USART_NOPARITY;
778 
779  if(MCU_debugIsEnabled()){
781  }
782  break;
783  }
784 
785  return error;
786 }
787 
788 mcu_error usart3_set_parity(usart_parity parity){
789 
790  uint8_t error = E_USART_NOERROR;
791 
792 
793  switch(parity){
794 
795  case USART_EVEN:
796 
797  usart_set_databits(USART3,USART_PARITY_EVEN);
798  break;
799 
800  case USART_ODD:
801 
802  usart_set_databits(USART3,USART_PARITY_ODD);
803  break;
804 
805  case USART_NONE:
806 
807  usart_set_databits(USART3,USART_PARITY_NONE);
808  break;
809 
810  default:
811  error = E_USART_NOPARITY;
812 
813  if(MCU_debugIsEnabled()){
815  }
816  break;
817  }
818 
819  return error;
820 }
821 
822 /****************************************************************************/
823 // INPUT / OUTPUT FUNCTIONS
824 /****************************************************************************/
825 
826 mcu_error USART_put(usart_periph peripheral, uint8_t byte){
827 
828  uint8_t error = E_USART_NOERROR;
829 
830  switch(peripheral){
831 
832  case USART_1:
833 
834  usart1_put(byte);
835  break;
836 
837  case USART_2:
838 
839  usart2_put(byte);
840  break;
841 
842  case USART_3:
843 
844  usart3_put(byte);
845  break;
846 
847  default:
848  error = E_USART_NOPORT;
849 
850  if(MCU_debugIsEnabled()){
852  }
853  break;
854  }
855 
856  return error;
857 
858 }
859 
861 
862 
863  switch(peripheral){
864 
865  case USART_1:
866 
867  return &usart1_put;
868  break;
869 
870  case USART_2:
871 
872  return &usart2_put;
873  break;
874 
875  case USART_3:
876 
877  return &usart3_put;
878  break;
879 
880  default:
881 
882  if(MCU_debugIsEnabled()){
884  }
885  return NULL;
886  break;
887  }
888 
889 }
890 
891 uint8_t USART_get(usart_periph peripheral){
892 
893  uint8_t rec;
894 
895  switch(peripheral){
896 
897  case USART_1:
898 
899  rec = usart1_get();
900  break;
901 
902  case USART_2:
903 
904  rec = usart2_get();
905  break;
906 
907  case USART_3:
908 
909  rec = usart3_get();
910  break;
911 
912  default:
913 
914  if(MCU_debugIsEnabled()){
916  }
917 
918  return E_USART_NOPORT;
919  break;
920  }
921 
922  return rec;
923 
924 }
925 
926 void usart1_put(uint8_t byte){
927 
928  //Wait for a the buffer to be empty to stop lost transmission bytes
929  while(!(USART_SR(USART1) & USART_SR_TXE));
930 
931  USART_DR(USART1) = byte;
932 
933 }
934 
935 void usart2_put(uint8_t byte){
936 
937  //Wait for a the buffer to be empty to stop lost transmission bytes
938  while(!(USART_SR(USART2) & USART_SR_TXE));
939 
940  USART_DR(USART2) = byte;
941 
942 }
943 
944 void usart3_put(uint8_t byte){
945 
946  //Wait for a the buffer to be empty to stop lost transmission bytes
947  while(!(USART_SR(USART3) & USART_SR_TXE));
948 
949  USART_DR(USART3) = byte;
950 
951 }
952 
953 uint8_t usart1_get(void){
954 
955  //Wait for a character to be available to read
956  while(!(USART_SR(USART1) & USART_SR_RXNE));
957 
958  return USART_DR(USART1);
959 
960 }
961 
962 uint8_t usart2_get(void){
963 
964  //Wait for a character to be available to read
965  while(!(USART_SR(USART2) & USART_SR_RXNE));
966 
967  return USART_DR(USART2);
968 
969 }
970 
971 uint8_t usart3_get(void){
972 
973  //Wait for a character to be available to read
974  while(!(USART_SR(USART3) & USART_SR_RXNE));
975 
976  return USART_DR(USART3);
977 
978 }
979 /****************************************************************************/
980 // TX/RX ISR REDIRECT FUNCTIONS
981 /****************************************************************************/
982 
983 mcu_error USART_setRxISR (usart_periph peripheral, void (*new_handler)(uint8_t received)){
984 
985  uint8_t error = E_USART_NOERROR;
986 
987  switch(peripheral){
988 
989  case USART_1:
990 
991  usart1_isr_rx = new_handler;
992  break;
993 
994  case USART_2:
995 
996  usart2_isr_rx = new_handler;
997  break;
998 
999  case USART_3:
1000 
1001  usart3_isr_rx = new_handler;
1002  break;
1003 
1004  default:
1005  error = E_USART_NOPORT;
1006 
1007  if(MCU_debugIsEnabled()){
1009  }
1010  break;
1011  }
1012  return error;
1013 }
1014 
1015 mcu_error USART_setTxISR (usart_periph peripheral, void (*new_handler)(void)){
1016 
1017  uint8_t error = E_USART_NOERROR;
1018 
1019  switch(peripheral){
1020 
1021  case USART_1:
1022 
1023  usart1_isr_tx = new_handler;
1024  break;
1025 
1026  case USART_2:
1027 
1028  usart2_isr_tx = new_handler;
1029  break;
1030 
1031  case USART_3:
1032 
1033  usart3_isr_tx = new_handler;
1034  break;
1035 
1036  default:
1037  error = E_USART_NOPORT;
1038 
1039  if(MCU_debugIsEnabled()){
1041  }
1042  break;
1043  }
1044  return error;
1045 }
1046 
1048 
1049  uint8_t error = E_USART_NOERROR;
1050 
1051  switch(peripheral){
1052 
1053  case USART_1:
1054 
1055 
1056  if(USART_RX == isr_target){
1057 
1058  //Disable receive interrupts
1059  USART_CR1(USART1) &= ~USART_CR1_RXNEIE;
1060  }
1061  else if(USART_TX == isr_target){
1062 
1063  //Disable transmit interrupts
1064  USART_CR1(USART1) &= ~USART_CR1_TCIE;
1065  USART_CR1(USART1) &= ~USART_CR1_TXEIE;
1066 
1067  }
1068  else{
1069  error = E_USART_NOINT;
1070  }
1071 
1072  //Disable interrupts
1073  nvic_disable_irq(NVIC_USART1_IRQ);
1074  break;
1075 
1076  case USART_2:
1077 
1078 
1079  if(USART_RX == isr_target){
1080 
1081  //Disable receive interrupts
1082  USART_CR1(USART2) &= ~USART_CR1_RXNEIE;
1083  }
1084  else if(USART_TX == isr_target){
1085 
1086  //Disable transmit interrupts
1087  USART_CR1(USART2) &= ~USART_CR1_TCIE;
1088  USART_CR1(USART2) &= ~USART_CR1_TXEIE;
1089 
1090  }
1091  else{
1092  error = E_USART_NOINT;
1093  }
1094 
1095  //Disable interrupts
1096  nvic_disable_irq(NVIC_USART2_IRQ);
1097  break;
1098 
1099  case USART_3:
1100 
1101 
1102  if(USART_RX == isr_target){
1103 
1104  //Disable receive interrupts
1105  USART_CR1(USART3) &= ~USART_CR1_RXNEIE;
1106  }
1107  else if(USART_TX == isr_target){
1108 
1109  //Disable transmit interrupts
1110  USART_CR1(USART3) &= ~USART_CR1_TCIE;
1111  USART_CR1(USART3) &= ~USART_CR1_TXEIE;
1112 
1113  }
1114  else{
1115  error = E_USART_NOINT;
1116  }
1117 
1118  //Disable interrupts
1119  nvic_disable_irq(NVIC_USART3_IRQ);
1120  break;
1121 
1122  default:
1123  error = E_USART_NOPORT;
1124 
1125  if(MCU_debugIsEnabled()){
1127  }
1128  break;
1129  }
1130 
1131  return error;
1132 
1133 
1134 }
1135 
1137 
1138  uint8_t error = E_USART_NOERROR;
1139 
1140  switch(peripheral){
1141 
1142  case USART_1:
1143 
1144  if(USART_RX == isr_target){
1145 
1146  //Clear the data register to stop inital interrupts
1147  USART_DR(USART1) = 0;
1148 
1149  //Enable receive interrupts
1150  USART_CR1(USART1) |= USART_CR1_RXNEIE;
1151 
1152  //Enable interrupts
1153  nvic_enable_irq(NVIC_USART1_IRQ);
1154  }
1155  else if(USART_TX == isr_target){
1156 
1157  //Clear the data register to stop inital interrupts
1158  USART_DR(USART1) = 0;
1159 
1160  //Enable transmit interrupts
1161  USART_CR1(USART1) |= USART_CR1_TCIE;
1162 
1163  //Enable interrupts
1164  nvic_enable_irq(NVIC_USART1_IRQ);
1165 
1166  }
1167  else{
1168  error = E_USART_NOINT;
1169  }
1170  break;
1171 
1172  case USART_2:
1173 
1174  if(USART_RX == isr_target){
1175 
1176  //Clear the data register to stop inital interrupts
1177  USART_DR(USART2) = 0;
1178 
1179  //Enable receive interrupts
1180  USART_CR1(USART2) |= USART_CR1_RXNEIE;
1181 
1182  //Enable interrupts
1183  nvic_enable_irq(NVIC_USART2_IRQ);
1184  }
1185  else if(USART_TX == isr_target){
1186 
1187  //Clear the data register to stop inital interrupts
1188  USART_DR(USART2) = 0;
1189 
1190  //Enable transmit interrupts
1191  USART_CR1(USART2) |= USART_CR1_TCIE;
1192 
1193  //Enable interrupts
1194  nvic_enable_irq(NVIC_USART2_IRQ);
1195 
1196  }
1197  else{
1198  error = E_USART_NOINT;
1199  }
1200  break;
1201 
1202  case USART_3:
1203 
1204  if(USART_RX == isr_target){
1205 
1206  //Clear the data register to stop inital interrupts
1207  USART_DR(USART3) = 0;
1208 
1209  //Enable receive interrupts
1210  USART_CR1(USART3) |= USART_CR1_RXNEIE;
1211 
1212  //Enable interrupts
1213  nvic_enable_irq(NVIC_USART3_IRQ);
1214  }
1215  else if(USART_TX == isr_target){
1216 
1217  //Clear the data register to stop inital interrupts
1218  USART_DR(USART3) = 0;
1219 
1220  //Enable transmit interrupts
1221  USART_CR1(USART3) |= USART_CR1_TCIE;
1222 
1223  //Enable interrupts
1224  nvic_enable_irq(NVIC_USART3_IRQ);
1225 
1226  }
1227  else{
1228  error = E_USART_NOINT;
1229  }
1230  break;
1231 
1232 
1233 
1234  default:
1235  error = E_USART_NOPORT;
1236 
1237  if(MCU_debugIsEnabled()){
1239  }
1240  break;
1241  }
1242  return error;
1243 
1244 }
1245 
1246 /****************************************************************************/
1247 // ISRs
1248 /****************************************************************************/
1249 
1250 void usart1_isr (void)
1251 {
1252  //Check if called because of RXNE.
1253  if (((USART_CR1(USART1) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART1) & USART_SR_RXNE) != 0)) {
1254 
1255  usart1_isr_rx(USART_DR(USART1));
1256  }
1257 
1258  // Check if called because of TXE.
1259  if (((USART_CR1(USART1) & USART_CR1_TCIE) != 0) && ((USART_SR(USART1) & USART_SR_TC) != 0)) {
1260 
1261  USART_SR(USART1) &= ~USART_SR_TC;
1262  usart1_isr_tx();
1263  }
1264 }
1265 
1266 void usart2_isr (void)
1267 {
1268  //Check if called because of RXNE.
1269  if (((USART_CR1(USART2) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART2) & USART_SR_RXNE) != 0)) {
1270 
1271  usart2_isr_rx(USART_DR(USART2));
1272  }
1273 
1274  // Check if called because of TXE.
1275  if (((USART_CR1(USART2) & USART_CR1_TCIE) != 0) && ((USART_SR(USART2) & USART_SR_TC) != 0)) {
1276 
1277  USART_SR(USART2) &= ~USART_SR_TC;
1278  usart2_isr_tx();
1279  }
1280 }
1281 
1282 void usart3_isr (void)
1283 {
1284  //Check if called because of RXNE.
1285  if (((USART_CR1(USART3) & USART_CR1_RXNEIE) != 0) && ((USART_SR(USART3) & USART_SR_RXNE) != 0)) {
1286 
1287  usart3_isr_rx(USART_DR(USART3));
1288  }
1289 
1290  // Check if called because of TXE.
1291  if (((USART_CR1(USART3) & USART_CR1_TCIE) != 0) && ((USART_SR(USART3) & USART_SR_TC) != 0)) {
1292 
1293  USART_SR(USART3) &= ~USART_SR_TC;
1294  usart3_isr_tx();
1295  }
1296 }
1297 
1298 /****************************************************************************/
1299 // DEFAULT ISR
1300 /****************************************************************************/
1301 
1302 void usart1_default_rx_isr(uint8_t c){
1303 
1304  //This exists to catch un-implemented ISR calls
1305  c++;
1306 
1307 }
1308 
1309 void usart1_default_tx_isr(void){
1310 
1311  //This exists to catch un-implemented ISR calls
1312 
1313 }
1314 
1315 void usart2_default_rx_isr(uint8_t c){
1316 
1317  //This exists to catch un-implemented ISR calls
1318  c++;
1319 
1320 }
1321 
1322 void usart2_default_tx_isr(void){
1323 
1324  //This exists to catch un-implemented ISR calls
1325 
1326 }
1327 
1328 void usart3_default_rx_isr(uint8_t c){
1329 
1330  //This exists to catch un-implemented ISR calls
1331  c++;
1332 
1333 }
1334 
1335 void usart3_default_tx_isr(void){
1336 
1337  //This exists to catch un-implemented ISR calls
1338 
1339 }
1340 
Pin 3 of the port.
mcu_error USART_setStop(usart_periph peripheral, usart_stop stop)
Set the number of stop bits for the port.
Pin 9 of the port.
mcu_error USART_setTxISR(usart_periph peripheral, void(*new_handler)(void))
Sets the target function called when transmit ISR is triggered.
4800bps buad rate
Recieve ISR.
Header file for stm32f103cb USART.
#define USART_DEFAULT_DATA
The default data frame used by setup function.
usart_isr
ISR values for the port.
115200bps baud rate
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.
Transmit ISR.
#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.
Pin 2 of the port.
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.
19200bps baud rate
mcu_error USART_setParity(usart_periph peripheral, usart_parity parity)
Set the parity for the port.
No parity for the port.
38400bps baud rate
Eight bit data frame for the port.
First peripheral port.
Odd parity for the port.
usart_baud
Baud rate options for the port.
Port B of the MCU.
2400bps baud rate
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.
Pin 11 of the port.
mcu_error USART_put(usart_periph peripheral, uint8_t byte)
Send a byte on the given peripheral.
Third peripheral port.
57600bps baud rate
Pin 10 of the port.
usart_parity
Parity option available for the port.
Second peripheral 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.
Error USART: No Error.
Nine bit data frame for the port.
Port A of the MCU.
9600bps baud rate
mcu_error
Error enumerators for the Debug peripheral.