Debian Codegraph @

examples/emcu-firmware/uart-main.c-src

raw
1 /* Connect USB2COM to RXD and TXD pins */
2 /* 9600 baud */
3
4 /* Includes ------------------------------------------------------------------*/
5 #include "gw1ns4c.h"
6 #include <stdio.h>
7 /*----------------------------------------------------------------------------*/
8
9 void go(void);
10
11 /* Declarations*/
12 void initializeTimer();
13 void delayMillis(uint32_t ms);
14 void initializeUART();
15 int readstr(char *ptr, int len);
16
17 char buf[100];
18 int main(void)
19 {
20 SystemInit(); //Configures CPU for the defined system clock
21 initializeTimer();
22 initializeUART();
23
24 uint32_t counter = 0;
25 printf("************* Apicula on Tangnano4k with Cortex-M3 ************\n");
26 delayMillis(1000);
27 go();
28 while(1);
29 }
30
31 int readstr(char *ptr, int len) {
32 int DataIdx;
33
34 for (DataIdx = 0; DataIdx < len; DataIdx++) {
35 *ptr++ = UART_ReceiveChar(UART0);
36 if (*(ptr - 1) == '\r') {
37 --ptr;
38 --DataIdx;
39 break;
40 }
41 }
42
43 *ptr = '\0';
44 return DataIdx;
45 }
46
47 //Initializes UART0
48 void initializeUART()
49 {
50 UART_InitTypeDef uartInitStruct;
51 //Enable transmission
52 uartInitStruct.UART_Mode.UARTMode_Tx = ENABLE;
53 //Disable reception
54 uartInitStruct.UART_Mode.UARTMode_Rx = ENABLE;
55 //9600 baud rate typical of Arduinos
56 uartInitStruct.UART_BaudRate = 9600;
57 //Initialize UART0 using the struct configs
58 UART_Init(UART0, &uartInitStruct);
59 }
60
61 void initializeTimer() {
62 TIMER_InitTypeDef timerInitStruct;
63
64 timerInitStruct.Reload = 0;
65
66 //Disable interrupt requests from timer for now
67 timerInitStruct.TIMER_Int = DISABLE;
68
69 //Disable timer enabling/clocking from external pins (GPIO)
70 timerInitStruct.TIMER_Exti = TIMER_DISABLE;
71
72 TIMER_Init(TIMER0, &timerInitStruct);
73 TIMER_StopTimer(TIMER0);
74 }
75
76 #define CYCLES_PER_MILLISEC (SystemCoreClock / 1000)
77 void delayMillis(uint32_t ms) {
78 TIMER_StopTimer(TIMER0);
79 TIMER_SetValue(TIMER0, 0); //Reset timer just in case it was modified elsewhere
80 TIMER_EnableIRQ(TIMER0);
81
82 uint32_t reloadVal = CYCLES_PER_MILLISEC * ms;
83 //Timer interrupt will trigger when it reaches the reload value
84 TIMER_SetReload(TIMER0, reloadVal);
85
86 TIMER_StartTimer(TIMER0);
87 //Block execution until timer wastes the calculated amount of cycles
88 while (TIMER_GetIRQStatus(TIMER0) != SET);
89
90 TIMER_StopTimer(TIMER0);
91 TIMER_ClearIRQ(TIMER0);
92 TIMER_SetValue(TIMER0, 0);
93 }
94
95
96