我正在爲加速度計實現SPI驅動程序。 SPI部分已完成,但無法讀取用戶空間的值。無法從linux字符設備讀取
static char charDevMessage[CD_BUFFER_SIZE] = { 0 };
...
static ssize_t char_dev_read(struct file *filep, char *buffer, size_t len, loff_t *position)
{
int error_count = 0;
struct xyz_values xyz;
size_t size_requested;
xyz = adxl345_get_xyz();
memset(charDevMessage, 0, CD_BUFFER_SIZE);
sprintf(charDevMessage, "%d,%d,%d", xyz.x, xyz.y, xyz.z);
printk(KERN_INFO "MOB: %s, requested size: %d\n", charDevMessage, len);
if (len >= CD_BUFFER_SIZE)
{
size_requested = CD_BUFFER_SIZE;
}
else
{
size_requested = len;
}
error_count = copy_to_user(buffer, charDevMessage, size_requested);
if (error_count == 0)
{
printk(KERN_INFO "MOB: Sent %d characters to the user\n", size_requested);
return (size_requested = 0);
}
else
{
printk(KERN_INFO "MOB: Failed to send %d characters to the user\n", error_count);
return -EFAULT;
}
}
節點在安裝驅動程序時已創建。但是,當我嘗試cat
或試圖通過python讀取它返回一個空字符串。
dmesg
說,它已發送成功用戶空間:
[ 3094.495972] MOB: SPI Character device has been opened 1 time(s) [ 3094.506075] MOB: -349,-512,511 , requested size:49 [ 3094.514487] MOB: Sent 256 characters to the user [ 3094.522646] MOB: Character device successfully closed [ 3120.658568] MOB: SPI Character device has been opened 2 time(s) [ 3120.668609] MOB: 0,0,0 , requested size:48 [ 3120.676392] MOB: Sent 256 characters to the user [ 3120.684740] MOB: Character device successfully closed
我在做什麼錯?
'return(size_requested = 0);'返回0,返回0表示文件結束。 – immibis
那麼它會關閉內核端的文件? – obayhan
您的第一個printk格式錯誤。 –