20145319 《信息安全系统设计基础》实验二 固件设计

20145319 《信息安全系统设计基础》实验二 固件设计

一 实验步骤

环境配置

  • 连接arm开发板
  • 建立超级终端
  • 建立共享文件夹
  • 修改pc,虚拟机以及arm端ip地址(使三者处在同一网段)
  • 安装arm编译器
  • ps:具体请参照实验一

代码调试

  • 代码如下:

      #include <stdio.h>
      #include <stdlib.h>
      #include <time.h>
      #include "pthread.h"
    
      #define BUFFER_SIZE 16
    
      /* Circular buffer of integers. */
      struct prodcons {
      	int buffer[BUFFER_SIZE];      /* the actual data */
      	pthread_mutex_t lock;         /* mutex ensuring exclusive access to buffer */
      	int readpos, writepos;        /* positions for reading and writing */
      	pthread_cond_t notempty;      /* signaled when buffer is not empty */
      	pthread_cond_t notfull;       /* signaled when buffer is not full */
      };
    
    
      void init(struct prodcons * b)
      {
        pthread_mutex_init(&b->lock, NULL);
        pthread_cond_init(&b->notempty, NULL);
        pthread_cond_init(&b->notfull, NULL);
        b->readpos = 0;
        b->writepos = 0;
      }
    
      void put(struct prodcons * b, int data)
      {
      	pthread_mutex_lock(&b->lock);
    
      	/* Wait until buffer is not full */
      	while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) {
      		printf("wait for not full\n");
      		pthread_cond_wait(&b->notfull, &b->lock);
      	}
    
        	b->buffer[b->writepos] = data;
        	b->writepos++;
        	if (b->writepos >= BUFFER_SIZE) b->writepos = 0;
    
        	pthread_cond_signal(&b->notempty);
    
      	pthread_mutex_unlock(&b->lock);
      }
    
      int get(struct prodcons * b)
      {
        	int data;
      	pthread_mutex_lock(&b->lock);
    
    
        	while (b->writepos == b->readpos) {
          	printf("wait for not empty\n");
      		pthread_cond_wait(&b->notempty, &b->lock);
      	}
    
        	data = b->buffer[b->readpos];
        	b->readpos++;
        	if (b->readpos >= BUFFER_SIZE) b->readpos = 0;
    
        	pthread_cond_signal(&b->notfull);
    
        	pthread_mutex_unlock(&b->lock);
        	return data;
      }
    
      #define OVER (-1)
      struct prodcons buffer;
    
      void * producer(void * data)
      {
        	int n;
      	for (n = 0; n < 1000; n++) {
      	printf(" put-->%d\n", n);
      	put(&buffer, n);
      }
        put(&buffer, OVER);
        printf("producer stopped!\n");
        return NULL;
      }
    
      void * consumer(void * data)
      {
       	int d;
       	while (1) {
       	d = get(&buffer);
       	if (d == OVER ) break;
       	printf("              %d-->get\n", d);
      }
       	printf("consumer stopped!\n");
       	return NULL;
      }
    
      int main(void)
      {
        	pthread_t th_a, th_b;
        	void * retval;
    
        	init(&buffer);
      	pthread_create(&th_a, NULL, producer, 0);
        	pthread_create(&th_b, NULL, consumer, 0);
        	pthread_join(th_a, &retval);
        	pthread_join(th_b, &retval);
    
        	return 0;
      }
    
  • 对于多线程相关的代码,编译时需要加-lpthread的库

  • 依次输入编译命令,最后在arm端运行结果如下

  • 按照实验参考书步骤调试term,运行可执行文件term时,会弹出no such file or directory

  • 经过查询之后才知道要在 Linux 下串口文件位于/dev 下,一般在老版本的内核中串口一为/dev/ttyS0 ,串口二为 /dev/ttyS1, 在我们的开发板中串口设备位于/dev/tts/下,因为开发板中没有ttyS0这个设备,所以我们要建立一个连接,输入ln –sf /dev/tts/0 /dev/ttyS0命令即可

二 合作伙伴

  • 本次实验与20145320周岐浩同学合作

posted on 2016-11-13 22:02  20145319钟轲  阅读(248)  评论(0编辑  收藏  举报