diff --git a/comm/src/comm.c b/comm/src/comm.c index 35e5481ecaddae3eea5040a4ff5517e1d2867bf3..37533ff7bd7b2a05577266d99b8e81143b5c83ca 100644 --- a/comm/src/comm.c +++ b/comm/src/comm.c @@ -82,7 +82,7 @@ void comm_init(TComm *dev,unsigned char use_dma,TTime *time) dev->dma_receive_cb=dummy_dma_receive_cb; } -comm_error send(TComm *dev,unsigned char *data,unsigned short int length) +comm_error comm_send(TComm *dev,unsigned char *data,unsigned short int length) { comm_error error; @@ -122,7 +122,9 @@ comm_error comm_receive(TComm *dev,unsigned char *data,unsigned short int *lengt } } // get the received data - if(!dev->use_dma) + if(dev->use_dma) + *length=buffer_get_num_data(&dev->rx_buffer); + else comm_get_received_data(dev,data,length); // get any possible error error=comm_get_error(dev); @@ -214,7 +216,6 @@ unsigned char comm_do_irq_receive(TComm *dev,unsigned char byte) void comm_cancel_irq_receive(TComm *dev) { - buffer_flush(&dev->rx_buffer); dev->irq_receiving=0x00; dev->rx_length=0; dev->cancel_receive_irq(); @@ -273,7 +274,7 @@ void comm_do_dma_receive(TComm *dev) dev->dma_receive_cb(dev->data); } -void comm_cancel_receive_dma(TComm *dev) +void comm_cancel_dma_receive(TComm *dev) { dev->dma_receiving=0x00; dev->cancel_receive_dma(); diff --git a/comm/test/Makefile b/comm/test/Makefile new file mode 100755 index 0000000000000000000000000000000000000000..a40098ba7f708534abcd321cd0330f3398c62389 --- /dev/null +++ b/comm/test/Makefile @@ -0,0 +1,40 @@ +# setup +# modified by zerom for WinARM 8/2010 + +PROJECT_NAME=comm_test +TARGET_FILES=$(wildcard *.c) +TARGET_FILES+=$(wildcard ../src/*.c) +TARGET_FILES+=$(wildcard ../../utils/src/*.c) +BUILD_PATH=build +BIN_PATH=bin + +INCLUDE_DIRS = -I../include -I../../utils/include + +CC = gcc + +UTILS_OBJS_TMP = $(notdir $(TARGET_FILES:.c=.o)) +UTILS_OBJS = $(patsubst %,$(BUILD_PATH)/%,$(UTILS_OBJS_TMP)) + +OUT_FILE=$(BIN_PATH)/$(PROJECT_NAME) + +all: $(OUT_FILE) + +make_dirs: + mkdir -p $(BUILD_PATH) + mkdir -p $(BIN_PATH) + +$(BUILD_PATH)/%.o: %.c + $(CC) -c -g $(INCLUDE_DIRS) -o $@ $< + +$(BUILD_PATH)/%.o: ../src/%.c + $(CC) -c -g $(INCLUDE_DIRS) -o $@ $< + +$(BUILD_PATH)/%.o: ../../utils/src/%.c + $(CC) -c -g $(INCLUDE_DIRS) -o $@ $< + +$(OUT_FILE): make_dirs $(UTILS_OBJS) + $(CC) -g $(UTILS_OBJS) -lpthread --output $@ + +clean: + -rm -rf $(BUILD_PATH) + -rm -rf $(BIN_PATH) diff --git a/comm/test/comm_test.c b/comm/test/comm_test.c new file mode 100644 index 0000000000000000000000000000000000000000..471ea6a0b471b7750db62fc25b948b7a2182acca --- /dev/null +++ b/comm/test/comm_test.c @@ -0,0 +1,224 @@ +#include "comm.h" +#include "stm32_time.h" +#include <stdio.h> +#include <stdlib.h> +#include <pthread.h> +#include <time.h> + +/* communication thread */ +pthread_t irq_thread; +pthread_t dma_thread; +unsigned char sending=0x00; +unsigned char receiving=0x00; +TComm comm_dev; + +typedef struct +{ + unsigned char *data; + unsigned short int length; +}TDMAData; + +TDMAData DMA_data; + +typedef struct +{ + unsigned char int_priority; +}TUSART_IRQ_Priorities; + +long get_time(void) +{ + struct timespec time_temp; + + clock_gettime(CLOCK_REALTIME, &time_temp ); + + return time_temp.tv_sec*1000000+time_temp.tv_nsec/1000; +} + +/* communication threads */ +void *irq_thread_function(void *param) +{ + unsigned char end=0x00; + unsigned char data; + + while(!end) + { + usleep(100000); + if(sending==0x01) + { + if(comm_do_irq_send(&comm_dev,&data)) + printf("IRQ: byte sent: %d\n",(int)data); + else + { + sending=0x00; + end=0x01; + } + } + if(receiving==0x01) + { + data=rand()%256; + printf("IRQ: byte received: %d\n",(int)data); + // call the reception function + if(!comm_do_irq_receive(&comm_dev,data)) + { + receiving=0x00; + end=0x01; + } + } + } + + pthread_exit(NULL); +} + +void *dma_thread_function(void *param) +{ + unsigned char end=0x00; + unsigned char data; + unsigned short int num=0; + TDMAData *dma_data=(TDMAData *)param; + + while(!end) + { + usleep(100000); + if(sending==0x01) + { + printf("DMA: byte sent: %d\n",dma_data->data[num]); + num++; + if(num==dma_data->length) + { + sending=0x00; + end=0x01; + comm_do_dma_send(&comm_dev); + } + } + if(receiving==0x01) + { + data=rand()%256; + printf("DMA: byte received: %d\n",(int)data); + dma_data->data[num]=data; + num++; + if(num==dma_data->length) + { + receiving=0x00; + end=0x01; + comm_do_dma_receive(&comm_dev); + } + } + } + + pthread_exit(NULL); +} + +/* IRQ functions */ +unsigned char usart2_send_irq(unsigned char first_byte) +{ + /* initialize the thread */ + printf("IRQ: byte sent: %d\n",(int)first_byte); + sending=0x01; + pthread_create(&irq_thread, NULL, irq_thread_function,NULL); +} + +unsigned char usart2_receive_irq(void) +{ + /* initialize the thread */ + receiving=0x01; + pthread_create(&irq_thread, NULL, irq_thread_function,NULL); +} + +unsigned char usart2_cancel_receive_irq(void) +{ + printf("IRQ: Cancelling data reception\n"); + receiving=0x00; + pthread_cancel(irq_thread); +} + +/* DMA functions */ +unsigned char usart2_send_dma(unsigned char *data,unsigned short int length) +{ + /* initialize the thread */ + DMA_data.data=data; + DMA_data.length=length; + sending=0x01; + pthread_create(&dma_thread, NULL, dma_thread_function,(void *)&DMA_data); +} + +unsigned char usart2_receive_dma(unsigned char *data,unsigned short int length) +{ + /* initialize the thread */ + DMA_data.data=data; + DMA_data.length=length; + receiving=0x01; + pthread_create(&dma_thread, NULL, dma_thread_function,(void *)&DMA_data); +} + +unsigned char usart2_cancel_receive_dma(void) +{ + printf("DMA: Cancelling data reception\n"); + receiving=0x00; + pthread_cancel(dma_thread); +} + +/* communication device functions */ +void usart2_init(TComm *comm_dev,TUSART_IRQ_Priorities *priorities) +{ + /* Initialize the comm structure */ + comm_dev->send_irq=usart2_send_irq; + comm_dev->receive_irq=usart2_receive_irq; + comm_dev->cancel_receive_irq=usart2_cancel_receive_irq; + if(comm_dev->use_dma) + { + comm_dev->send_dma=usart2_send_dma; + comm_dev->receive_dma=usart2_receive_dma; + comm_dev->cancel_receive_dma=usart2_cancel_receive_dma; + } + + sending=0x00; + receiving=0x00; +} + +int main(void) +{ + int i=0; + TTime timeout; + unsigned short int length; + unsigned char data_out[128],data_in[128]; + TUSART_IRQ_Priorities priorities; + + srand(time(NULL)); + + // initialize the time structure + time_init(&timeout,1,get_time); + // initialize the comm device without DMA + comm_init(&comm_dev,0x00,&timeout); + // usart 2 initialization + usart2_init(&comm_dev,&priorities); + + for(i=0;i<128;i++) + data_out[i]=i; + comm_send(&comm_dev,data_out,10); + length=10; + comm_receive(&comm_dev,data_in,&length,20000000); + printf("Data actually read: %d\n",length); + for(i=0;i<length;i++) + printf("byte received: %d\n",data_in[i]); + length=10; + comm_receive(&comm_dev,data_in,&length,500000); + printf("Data actually read: %d\n",length); + for(i=0;i<length;i++) + printf("byte received: %d\n",data_in[i]); + + // initialize the comm device with DMA + comm_init(&comm_dev,0x01,&timeout); + // usart 2 initialization + usart2_init(&comm_dev,&priorities); + comm_send(&comm_dev,&data_out[10],10); + length=10; + comm_receive(&comm_dev,data_in,&length,20000000); + printf("Data actually read: %d\n",length); + for(i=0;i<length;i++) + printf("byte received: %d\n",data_in[i]); + length=10; + comm_receive(&comm_dev,data_in,&length,500000); + printf("Data actually read: %d\n",length); + for(i=0;i<length;i++) + printf("byte received: %d\n",data_in[i]); +}