This is a very simple guide, reviewing the steps required to get Blinky working on an Mbed OS platform.

Dependencies:   RemoteIR

Revision:
139:78c21d98cff6
Parent:
138:e829be898713
--- a/main.cpp	Tue May 26 07:25:56 2020 +0000
+++ b/main.cpp	Sat May 30 08:17:30 2020 +0000
@@ -1,106 +1,180 @@
 #include "mbed.h"
-#define SERVER_IP "192.168.1.16"
-#define SERVER_PORT 50000
-Serial pc(USBTX, USBRX, 115200);
-WiFiInterface *wifi;
-TCPSocket socket;
-Thread sock_thread;
-char rx_buf_pc[100];
-int index = 0;
-volatile int flag ;
+#include <stdlib.h>
+#include <string.h>
+
+
+RawSerial pc(PA_2, PA_3, 115200);
 
-void rx_cb(void)   // ISR for receiving data from the PC keyboard {
+void USART2_init(void);
+void DMA1_init(void);
+void DMA1_Stream6_setup(unsigned int src, unsigned int dst, int len);
+
+int done = 1;
+
+void send_DMA1_STEAM6_CH4(char str[], int len)
 {
-    char ch;
-    ch = pc.getc();
-    pc.putc(ch);
-    rx_buf_pc[index++] = ch;
-    if (ch == 0x0D) { //LF
-        pc.putc(0x0A);
-        rx_buf_pc[--index] = '\0';
-        index = 0;
-        flag = 1;
-    }
-}
+
+    char message[80];
+    int i;
+
+    USART2_init();
+    DMA1_init();
 
-// rx_thread: a thread to receive data from the TCP server
-void rx_thread()
-{
-    char* buf = (char*)malloc(1024);
-    while (true) {
-        nsapi_size_or_error_t size = socket.recv(buf, 1024);
-        if (size <= 0) {
-            if (size == NSAPI_ERROR_WOULD_BLOCK) continue;
-// (-3001) no data for the case of Non-blocking mode
-            pc.printf("Error while receiving data from TCP socket (%d)\r\n", size);
-            return;
-        }
-        buf[size] = '\0'; // turn into valid C string
-        pc.printf("\r\nRX data: (%d) %s \r\n", size, buf);
-    }
+    /* prepare the message for transfer */
+    for (i = 0; i < len; i++)
+        message[i] = str[i];
+
+    /* send the message out by USART2 using DMA */
+    while (done == 0) {}    /* wait until DMA data transfer is done */
+    done = 0;               /* clear done flag */
+    DMA1_Stream6_setup((unsigned int)message, (unsigned int)&USART2->DR, len);
+
 }
 
-int main() {
-    SocketAddress sockAddr;
-    SocketAddress serverAddr(SERVER_IP, SERVER_PORT);
-    pc.printf("\r\n WiFi TCP Client example\r\n");
-    pc.attach(&rx_cb);
-    wifi = WiFiInterface::get_default_instance();
-    if (!wifi)
-    {
-        pc.printf("ERROR: No WiFiInterface found.\n");
-        while(1);
-    }
-    pc.printf("Connecting to %s...\r\n", MBED_CONF_APP_WIFI_SSID);
-    int ret = wifi->connect(MBED_CONF_APP_WIFI_SSID,
-    MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA2);
-    if (ret != 0)
-    {
-        pc.printf("Connection error!!\r\n");
-        while(1);
-    }
+
+int main (void)
+{
+    while (1) {
+        int size = 0;
+        char* buf = (char*)malloc(sizeof(char)*8);
+        pc.printf("Enter Fibonacci number n: \r\n");
+        while(true) {
+            if (pc.readable()) {
+                int ch = pc.getc();
+                pc.putc(ch);
+                if (ch == 0x0D) {
+                    while(!pc.writeable());
+                    pc.putc(0x0A);
+                    break;
+                }
+                buf[size] = ch;
+                size++;
+            }
+        }
+        int val = atoi(buf);
+
+        if(val < 0 || val > 40) {
+            pc.printf("ERROR: n(%d) must be satisfied with '0 <= n <= 40'\r\n",val);
+            continue;
+        }
+
+        size = val;
+        int *arr = (int*)malloc(sizeof(int)*size);
+        arr[0] = 0;
+        arr[1] = 1;
+
+        for(int i = 2; i < size; i++) {
 
-    pc.printf("Success!!\r\n");
-    pc.printf("RSSI: %d\r\n", wifi->get_rssi());
-    wifi->get_ip_address(&sockAddr)/;
-    pc.printf("IP: %s, ", sockAddr.get_ip_address());
-//wifi->get_netmask(&sockAddr);
-//pc.printf("Netmask: %s, ", sockAddr.get_ip_address()); pc.printf("Netmask: %s, ", wifi->get_netmask());
-    wifi->get_gateway(&sockAddr);
-    pc.printf("Gateway: %s\r\n", sockAddr.get_ip_address());
+            arr[i] = arr[i-1] + arr[i-2];
+        }
+        free(buf);
+
+        buf = (char*)malloc(sizeof(char)*256);
+
+        for(int i = 0; i < size; i ++) {
+
+            char temp[64];
+            sprintf(temp, "%d ", arr[i]);
+            strcat(buf, temp);
+
+            if(i%5 == 0) {
+                strcat(buf, "\r\n");
+            }
+        }
+        strcat(buf, "\r\n");
+
+        pc.printf("buf\r\n");
+
+        pc.printf("%s\r\n",buf);
+        pc.printf("\\buf\r\n");
+
+        char sub[8];
+        int s_size = 0;
+        for(int i = 0; i < strlen(buf); i++) {
+            
+            sub[s_size] = buf[i];
+            s_size++;
+            
+            if(s_size % 8 == 7) {
+                pc.printf("sub-buf\r\n");
+                pc.printf("%s\r\n",sub);
+                pc.printf("\\sub-buf\r\n");
+//                send_DMA1_STEAM6_CH4(sub,s_size);
+                strcpy(sub,"");
+                s_size = 0;
+            }
+        }
 
-// Open a TCP socket on the network interface, and create a TCP connection to a Server
-    socket.open(wifi);
-    int response = socket.connect(serverAddr);
-    if(0 != response)
-    {
-        pc.printf("Error connecting: %d\r\n", response);
-        socket.close();
-        wifi->disconnect();
-        while(1);
-    }
-    sock_thread.start(&rx_thread);
-    while (true)
-    {
-        flag = 0;
-        pc.printf("Enter characters to send to a server: ");
-        while (flag != 1) {
+        if(s_size > 0) {
+                pc.printf("sub-buf\r\n");
+                pc.printf("%s\r\n",sub);
+                pc.printf("\\sub-buf\r\n");
+//            send_DMA1_STEAM6_CH4(sub,s_size);
         }
-        if (!strcmp(rx_buf_pc, "q") || !strcmp(rx_buf_pc, "Q")) {
-            break;
-        } else {
-            int len = strlen(rx_buf_pc);
-            pc.printf("Sent: %s (%d)\r\n", rx_buf_pc, len);
-            rx_buf_pc[len++] = 0x0D;
-            rx_buf_pc[len++] = 0x0A;
-            socket.send((const char *)rx_buf_pc, len);
-            Thread::wait(500); // 0.5sec
-        }
+
+        free(sub);
+        free(buf);
+        free(arr);
     }
-    socket.close();
-    wifi->disconnect();
-    sock_thread.join();
-    pc.printf("RX sock_thread joined!!\r\n");
-    pc.printf("\nDone\r\n");
-    Thread::wait(osWaitForever);
+}
+void USART2_init (void)
+{
+    RCC->AHB1ENR |= 1;          /* enable GPIOA clock */
+    RCC->APB1ENR |= 0x20000;    /* enable USART2 clock */
+
+    /* Configure PA2 for USART2_TX */
+    GPIOA->AFR[0] &= ~0x0F00;
+    GPIOA->AFR[0] |=  0x0700;   /* alt7 for USART2 */
+    GPIOA->MODER  &= ~0x0030;
+    GPIOA->MODER  |=  0x0020;   /* enable alternate function for PA2 */
+
+    USART2->BRR = 434;       /* 9600 baud @ 16 MHz */
+    USART2->CR1 = 0x0008;       /* enable Tx, 8-bit data */
+    USART2->CR2 = 0x0000;       /* 1 stop bit */
+    USART2->CR3 = 0x0000;       /* no flow control */
+    USART2->CR1 |= 0x2000;      /* enable USART2 */
+
+    USART2->SR = ~0x40;         /* clear TC flag */
+    USART2->CR1 |= 0x0040;      /* enable transmit complete interrupt */
+
+    NVIC_EnableIRQ(USART2_IRQn);    /* USART2 interrupt enable at NVIC */
+}
+
+void DMA1_init(void)
+{
+    RCC->AHB1ENR |= 0x00200000;     /* DMA controller clock enable */
+    DMA1->HIFCR = 0x003F0000;       /* clear all interrupt flags of Stream 6 */
+    NVIC_EnableIRQ(DMA1_Stream6_IRQn);  /* DMA interrupt enable at NVIC */
+}
+
+void DMA1_Stream6_setup(unsigned int src, unsigned int dst, int len)
+{
+    DMA1_Stream6->CR &= ~1;         /* disable DMA1 Stream 6 */
+    while (DMA1_Stream6->CR & 1) {} /* wait until DMA1 Stream 6 is disabled */
+    DMA1->HIFCR = 0x003F0000;       /* clear all interrupt flags of Stream 6 */
+    DMA1_Stream6->PAR = dst;
+    DMA1_Stream6->M0AR = src;
+    DMA1_Stream6->NDTR = len;
+    DMA1_Stream6->CR = 0x08000000;  /* USART2_TX on DMA1 Stream6 Channel 4 */
+    DMA1_Stream6->CR |= 0x00000440; /* data size byte, mem incr, mem-to-peripheral */
+    DMA1_Stream6->CR |= 0x16;       /* enable interrupts DMA_IT_TC | DMA_IT_TE | DMA_IT_DME */
+    DMA1_Stream6->FCR  = 0;         /* direct mode, no FIFO */
+    DMA1_Stream6->CR |= 1;          /* enable DMA1 Stream 6 */
+
+    USART2->SR &= ~0x0040;          /* clear UART transmit complete interrupt flag */
+    USART2->CR3 |= 0x80;            /* enable USART2 transmitter DMA */
+}
+
+void DMA1_Stream6_IRQHandler(void)
+{
+    if (DMA1->HISR & 0x000C0000)    /* if an error occurred */
+        while(1) {}                 /* substitute this by error handling */
+    DMA1->HIFCR = 0x003F0000;       /* clear all interrupt flags of Stream 6 */
+    DMA1_Stream6->CR &= ~0x10;      /* disable DMA1 Stream 6 TCIE */
+}
+
+void USART2_IRQHandler(void)
+{
+    USART2->SR &= ~0x0040;          /* clear transmit complete interrupt flag */
+    done = 1;                       /* set the done flag */
 }
\ No newline at end of file