c - 写入 I/O 地址

标签 c

我正在使用VersaLogic Osprey Board我正在使用 GCC 编译器 6.2.0 运行 Lubuntu 16.04 Xenial。用 C 编写,无法编译 C++。

我正在尝试读取看门狗定时器的 I/O 地址,以便稍后启用它。根据data sheet I/O地址为1CA8(WDT_CTL是该地址的标识符)

到目前为止我的代码如下:

#define WDT_CTL 0x1CA8

int main(void)
{

char *p_CTL = (char*) WDT_CTL;

printf("Attempting to print contents %c\n"),*p_CTL); 


}

代码可以编译,但是当我到达重要行时出现段错误(核心转储),没有其他错误或警告。据我所知,段错误是“您无法访问该内存位置”错误,但我正在尝试从数据表中找到的地址进行读取。

我做了一些研究,根据数据表,我的地址是一个 I/O 地址,这似乎与普通地址不同。当我查看自定义变量 (char a) 的地址时,该地址的长度为 12 位,而不是为看门狗定时器提供的 4 位。

我错过了什么吗?我无法再直接写入 I/O 地址,这是 Lubuntu 的限制吗?我需要使用某些特定命令来执行此操作吗?我需要启用某些功能或更改设置吗?

请注意,该线程与 similar title 的线程不同。因为这是在 Windows 7 中完成的,而不是 Lubuntu,并且该修复在这里不起作用。

最佳答案

这是我通常访问看门狗硬件的方式:

这已在多台不同的计算机上运行。

#include "watchdog.h"
#include <fcntl.h>      /* open() */
#include <stdio.h>      /* printf() */
#include <unistd.h>     /* write() */
#include <string.h>     /* strerror() */
#include <errno.h>      /* errno */

#define APP_VERSION     "1.1.0"

/* Look for the device in two places */
#define WD_KICK_DEV_NAME_1       "/dev/cs5535/gpio/5"
#define WD_KICK_DEV_NAME_2       "/dev/gpio/5"

#define WD_ENABLE_DEV_NAME_1     "/dev/cs5535/gpio/27"
#define WD_ENABLE_DEV_NAME_2     "/dev/gpio/27"

#define TEXT_WDOG_KICK           "Ot0101"
#define TEXT_WDOG_ENABLE         "1TO"
#define TEXT_WDOG_DISABLE        "0TO"

static INT32 wd_kick_fd;
static INT32 wd_enable_fd;


/**
 * Return the version
 */
const char *wd_get_version(void)
{
   return(APP_VERSION);
}


/**
 * Open GPIO-5 and GPIO-27
 */
INT32 wd_init(void)
{
   if (((wd_kick_fd = open(WD_KICK_DEV_NAME_2, O_WRONLY)) < 0) &&
       ((wd_kick_fd = open(WD_KICK_DEV_NAME_1, O_WRONLY)) < 0))
   {
      printf("Failed to open '%s' or '%s': %s (%d)\n",
             WD_KICK_DEV_NAME_1, WD_KICK_DEV_NAME_2, strerror(errno), errno);
      return(FAILURE);
   }

   if (((wd_enable_fd = open(WD_ENABLE_DEV_NAME_2, O_WRONLY)) < 0) &&
       ((wd_enable_fd = open(WD_ENABLE_DEV_NAME_1, O_WRONLY)) < 0))
   {
      printf("Failed to open '%s' or '%s': %s (%d)\n",
             WD_ENABLE_DEV_NAME_1, WD_ENABLE_DEV_NAME_2, strerror(errno), errno);
      return(FAILURE);
   }
   return(SUCCESS);
}


/**
 * Disabled via GPIO-27
 */
void wd_disable(void)
{
   (void)write(wd_enable_fd, TEXT_WDOG_DISABLE, strlen(TEXT_WDOG_DISABLE));
}


/**
 * Enabled via GPIO-27
 */
void wd_enable(void)
{
   (void)write(wd_enable_fd, TEXT_WDOG_ENABLE, strlen(TEXT_WDOG_ENABLE));
}

/**
 * Kick the watchdog by toggling GPIO 5
 */
void wd_kick(void)
{
   (void)write(wd_kick_fd, TEXT_WDOG_KICK, strlen(TEXT_WDOG_KICK));
}

并且watchdog.h文件包含:

#ifndef WATCHDOG_H_INCLUDED
#define WATCHDOG_H_INCLUDED


/**
 * Grab the version of the kicker stuff
 */
const char *wd_get_version(void);


/**
 * Do any initialization needed for the watchdog.
 *
 * @return SUCCESS or FAILURE
 */
INT32 wd_init(void);


/**
 * Disables the watchdog.
 * If watchdog cannot be disabled, then calls wd_kick()
 */
void wd_disable(void);


/**
 * Enables the watchdog.
 * If watchdog cannot be disabled, do nothing.
 */
void wd_enable(void);


/**
 * Kicks the watchdog
 */
void wd_kick(void);


#endif /* WATCHDOG_H_INCLUDED */

关于c - 写入 I/O 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47077232/

相关文章:

编译器错误: undefined reference to `print'

c - 将文件中的数据分解为结构体

c++ - C 字符串与带有类成员函数的字符串类语法

编译器错误 - 返回一个字符数组

c - 使用主入口点将 HINSTANCE 传递给 WNDCLASS

sql - 在c中生成动态sql

c - 我如何声明一个 int 类型输入变量,例如。在c中输入x(0<x<1000)?

C 字符串 malloc(输出)

c - 如何在 C 中使用 fork 写入和读取文件

c - 当我声明一个字符串时保留多少内存?