I am implementing an SPI driver for an accelerometer. The SPI part is done but I can't read values from userspace.
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
", 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
", size_requested);
return (size_requested = 0);
}
else
{
printk(KERN_INFO "MOB: Failed to send %d characters to the user
", error_count);
return -EFAULT;
}
}
The node has been created when I installed the driver. But when I tried cat
or tried to read by python it returns an empty string.
dmesg
says it has been sent to userspace successfully:
[ 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
What am I doing wrong?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…