Browse Source

mailbox: add makefile support be built by buildroot

fix the building warning

Signed-off-by: Andy Hu <andy.hu@starfivetech.com>
Andy Hu 1 year ago
parent
commit
71d5759d1e
2 changed files with 73 additions and 36 deletions
  1. 36 0
      mailbox/Makefile
  2. 37 36
      mailbox/read_mbox.c

+ 36 - 0
mailbox/Makefile

@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: GPL-2.0
+#
+# Copyright (C) 2022 StarFive Technology Co., Ltd.
+#
+CROSS_BIN_PATH := $(CURDIR)/../../work/buildroot_initramfs/host/bin
+target := riscv64-buildroot-linux-gnu
+
+CC  = $(target)-gcc
+CXX = $(target)-g++
+AR  = $(target)-ar
+
+# if cross gcc file exist, then use it, else use the buildroot to build
+ifeq ($(CROSS_BIN_PATH)/$(target)-gcc, $(wildcard $(CROSS_BIN_PATH)/$(target)-gcc))
+CC  = $(CROSS_BIN_PATH)/$(target)-gcc
+CXX = $(CROSS_BIN_PATH)/$(target)-g++
+AR  = $(CROSS_BIN_PATH)/$(target)-ar
+endif
+
+CFLAGS  += -g -I. $(INCLUDES)
+LDFLAGS = -lpthread -Wl,--fatal-warning
+
+SOURCES_READ_TEST=read_mbox.c
+OBJECTS_READ_TEST=$(patsubst %.c,%.o,$(SOURCES_READ_TEST))
+
+.PHONY: all clean
+
+all: read_test
+
+read_test: $(OBJECTS_READ_TEST)
+	$(CC) -o $@ $(LDFLAGS) $^
+
+clean:
+	rm -rf read_test *.o *.dep
+
+%.o: %.c
+	$(CC) $(CFLAGS) -Wall -Werror  -c $< -o $@ -MD -MF $(@:.o=.dep)

+ 37 - 36
mailbox/read_mbox.c

@@ -12,17 +12,18 @@ int active = 1;
 
 void mailbox_irq_handler(int val)
 {
-	printf("mailbox msg recv %d\n",val);
-	msg_done = 1;
+    printf("mailbox msg recv %d\n",val);
+    msg_done = 1;
 }
 
 void init_signal(int fd)
 {
-	int flags,ret;
-	
-	signal(SIGIO, mailbox_irq_handler);
+    int flags = 0, ret = 0;
+
+    signal(SIGIO, mailbox_irq_handler);
 
     ret = fcntl(fd, F_SETOWN, getpid());
+    printf("mailbox fcntl F_SETOWN ret: %d\n",ret);
 
     flags = fcntl(fd, F_GETFL);
 
@@ -31,46 +32,46 @@ void init_signal(int fd)
 
 void * mailbox_user_read(void *arg)
 {
-	char buf[8];
-	int fp = open("/sys/kernel/debug/soc:mailbox_client@0/message",O_RDWR);
-	
-	while(active)
-	{
-		read(fp,buf,sizeof(char));
-		printf("user recv:%c\n",buf[0]);
-	}
-	close(fp);
+    char buf[8] = {0};
+    int fp = open("/sys/kernel/debug/soc:mailbox_client@0/message", O_RDWR);
+
+    while(active) {
+        read(fp, buf, sizeof(char));
+        printf("user recv:%c\n", buf[0]);
+    }
+    close(fp);
+    return NULL;
 }
 
 int main(int argv,char **argc)
 {
-    int fd,i=0,ret;
-    char buf_tx[8],buf_rx[8];
+    int fd = 0, i = 0;
+    char buf_tx[8];
 
-    fd = open("/sys/kernel/debug/soc:mailbox_client@0/message",O_RDWR);
-    if(fd < 0)
+    fd = open("/sys/kernel/debug/soc:mailbox_client@0/message", O_RDWR);
+    if (fd < 0) {
         printf("open device error\n");
+        return 1;
+    }
 
-	pthread_t thread_read;
+    pthread_t thread_read;
+    if (pthread_create(&thread_read, NULL, mailbox_user_read, NULL) != 0) {
+        printf("pthread_create error\n");
+        close(fd);
+        return 0;
+    }
 
-	if(pthread_create(&thread_read,NULL,mailbox_user_read,NULL) != 0)
-	{
-		printf("pthread_create error\n");
-		close(fd);
-		return 0;
-	}
-	
-	buf_tx[0] = 'a';
-	do{
-		write(fd,buf_tx,sizeof(char));	
-		printf("user send:%c\n",buf_tx[0]);
-		sleep(1);
+    buf_tx[0] = 'a';
+    do {
+        write(fd, buf_tx, sizeof(char));
+        printf("user send:%c\n", buf_tx[0]);
+        sleep(1);
 
-		buf_tx[0]++;
-		i++;
-	}while(i<2);
-	active  = 0;
+        buf_tx[0]++;
+        i++;
+    } while (i < 2);
+    active = 0;
     close(fd);
 
-    return 1;
+    return 0;
 }