# CAN IoT Card driver

**URL:** https://mangoh.discourse.group/t/can-iot-card-driver/997
**Category:** mangOH Red
**Created:** [December 16, 2017, 12:44pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997 "2017-12-16T12:44:54Z")
**Posts on this page:** 20
**Page:** 2

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [January 23, 2018, 10:52am UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/21 "2018-01-23T10:52:55Z")

</div>

Progress update :

echo 13 \> /sys/class/gpio/export  
echo out \> /sys/class/gpio/gpio13/direction  
echo 1 \> /sys/class/gpio/gpio13/value

pin SPI1\_CLK / SPI1\_MISO / SPI1\_MOSI follow the state on GPIO13 on WP76…

SPI bus seems don’t work. mcp2515 is not probed.

Asyal any idea ?

---

<div class="post-metadata">

### Author: ![asyal](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/asyal/32/78_2.png) [@asyal](https://mangoh.discourse.group/u/asyal)
#### Post date: [January 24, 2018, 4:41pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/22 "2018-01-24T16:41:20Z")

</div>

@Francis.duhaut I’ll build this on Friday and get back to you with the right steps.

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [January 24, 2018, 4:54pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/23 "2018-01-24T16:54:03Z")

</div>

ok thanks 🙂

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [January 26, 2018, 11:28pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/24 "2018-01-26T23:28:00Z")

</div>

@asyal any progress on the CAN for the WP76 ?

---

<div class="post-metadata">

### Author: ![asyal](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/asyal/32/78_2.png) [@asyal](https://mangoh.discourse.group/u/asyal)
#### Post date: [January 26, 2018, 11:43pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/25 "2018-01-26T23:43:56Z")

</div>

I ran into the same issues you did and will work on resolving it next week.

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [January 27, 2018, 8:12am UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/26 "2018-01-27T08:12:30Z")

</div>

Thank’s a lot for your help.

---

<div class="post-metadata">

### Author: ![raxsix](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@raxsix](https://mangoh.discourse.group/u/raxsix)
#### Post date: [January 28, 2018, 6:04pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/27 "2018-01-28T18:04:51Z")

</div>

1. Reflash new image with CAN driver module support to the target board  
FDT command: fdt yocto\_wp85.cwe

When I run it I get: **command not found: fdt**  
What do I need?

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [January 29, 2018, 5:18am UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/28 "2018-01-29T05:18:40Z")

</div>

Use :  
cfglegato  
fwupdate download yocto\_wp85.cwe 192.168.2.2

---

<div class="post-metadata">

### Author: ![raxsix](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@raxsix](https://mangoh.discourse.group/u/raxsix)
#### Post date: [January 29, 2018, 7:57pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/29 "2018-01-29T19:57:23Z")

</div>

Thanks, Francis, I got one step closer but I am a bit confused about next one. When should I remove the card and how? I tried different ways but  
**echo 2 \> /sys/class/gpio/export**  
gives me  
**sh: write error: Device or resource busy**

---

<div class="post-metadata">

### Author: ![raxsix](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@raxsix](https://mangoh.discourse.group/u/raxsix)
#### Post date: [March 8, 2018, 4:59pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/30 "2018-03-08T16:59:44Z")

</div>

Finally, saw the can0 line with ifconfig command after all those steps:

echo 2 \> /sys/class/gpio/export  
echo out \> /sys/class/gpio/gpio2/direction  
echo 1 \> /sys/class/gpio/gpio2/value

// Enable level shifter on the CAN IoT card  
echo 13 \> /sys/class/gpio/export  
echo out \> /sys/class/gpio/gpio13/direction  
echo 1 \> /sys/class/gpio/gpio13/value

modprobe can  
modprobe can-dev  
modprobe can-raw  
modprobe mcp251x  
ip link set can0 type can bitrate 125000 triple-sampling on  
ifconfig can0 up

**And after restarting MangOH red board I can not get it up and running again, why?**  
cannot find device “can0”

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [March 21, 2018, 9:08pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/31 "2018-03-21T21:08:01Z")

</div>

Hello

You must start a script who intialize the CAN.  
You can do that at startup or include this script into your application.

In your project create a folder “script” , inside create a file “can\_init.sh”

copy/paste this code into “can\_init.sh”

#!/bin/sh

export PATH=$PATH:/sbin

ifconfig can0 down

echo 2 \> /sys/class/gpio/export

echo out \> /sys/class/gpio/gpio2/direction

echo 1 \> /sys/class/gpio/gpio2/value

echo 13 \> /sys/class/gpio/export &&

echo out \> /sys/class/gpio/gpio13/direction &&

echo 1 \> /sys/class/gpio/gpio13/value &&

modprobe can &&

sleep 1 &&

modprobe can-dev &&

sleep 1 &&

modprobe can-raw &&

sleep 1 &&

modprobe mcp251x &&

sleep 1

CAN=`ifconfig | cut -c -4 | sed 's/^\s\+//' | sort | uniq | grep can0`

if [-z “$CAN”]; then

```
ip link set can0 type can bitrate 500000 triple-sampling on &&

sleep 1 &&

ifconfig can0 up

```

fi

In your app create this function :

#include \<net/if.h\>  
#include \<sys/ioctl.h\>  
#include \<linux/can.h\>  
#include \<linux/can/raw.h\>  
#include \<sys/types.h\>  
#include \<sys/socket.h\>  
#include \<stdio.h\>  
#include \<string.h\>  
#include \<unistd.h\>  
#include \<fcntl.h\>

#define INTERFACE\_NAME\_SIZE 15

#define SOCKET\_ERROR 1  
#define SOCK\_IOCTL\_ERROR 2  
#define SOCK\_BINDING\_ERROR 3  
#define SOCK\_WRITING\_ERROR 4  
#define SOCK\_READING\_ERROR 5  
#define WRONG\_DLC 6  
#define WRONG\_ID 7

int bitrate;  
int sdw; // socket descriptor write  
int sdr; // socket descriptor read  
int nbytesReceive;  
bool canwrite;  
char name[INTERFACE\_NAME\_SIZE];  
struct can\_frame frameRead;  
struct can\_frame frameWrite;

int can\_connect(const char \*interface)  
{  
struct sockaddr\_can addr;  
struct ifreq ifr;  
int sock\_buf\_size;

```
sock_buf_size = 1000;

// Run the can-init.sh before advertising the service to ensure that the CAN device is
// available before we allow it to be used.
char line[256];
FILE* fp = popen("can_init.sh 2>&1", "r");
LE_ASSERT(fp != NULL);
while (fgets(line, sizeof(line), fp) != NULL)
{
	LE_INFO("can_init.sh output: %s", line);
}
int canInitResult = pclose(fp);
LE_FATAL_IF(!WIFEXITED(canInitResult), "Could not run can_init.sh");
const int canInitExitCode = WEXITSTATUS(canInitResult);
LE_FATAL_IF(canInitExitCode != 0, "can_init.sh failed with exit code %d", canInitExitCode);

// if interface string is long we will cut it
if(strlen(interface)>INTERFACE_NAME_SIZE)
{
	memcpy(name,interface,INTERFACE_NAME_SIZE);
	name[INTERFACE_NAME_SIZE-1]='\0';
}
else
{
	strcpy(name,interface);
}

// SOCKET FOR READING
if ((sdr = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
{
	return SOCKET_ERROR;
}

addr.can_family = PF_CAN;

strcpy(ifr.ifr_name, name);
if (ioctl(sdr, SIOCGIFINDEX, &ifr) < 0)
{
	return SOCK_IOCTL_ERROR;
}

addr.can_ifindex = ifr.ifr_ifindex;

if (bind(sdr, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
	return SOCK_BINDING_ERROR;
}

// Filter
struct can_filter rfilter[7]; int i = 0;
rfilter[i].can_id = CHG_TX_STS;
rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
rfilter[i].can_id = CHG_TX_ACT;
rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
rfilter[i].can_id = BMS_CHRGR_CTL;
rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
rfilter[i].can_id = BMS_SOC_DATA;
rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
rfilter[i].can_id = BMS_BATTP_DATA;
rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
rfilter[i].can_id = BMS_BATP_DATA;
rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
rfilter[i].can_id = BMS_BATU_DATA;
rfilter[i].can_mask = CAN_SFF_MASK | CAN_EFF_FLAG | CAN_RTR_FLAG; i++;
setsockopt(sdr, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter));

// SET SOCKET BUFFER SIZE
setsockopt(sdr, SOL_CAN_RAW, SO_RCVBUF, (char *)&sock_buf_size, sizeof(sock_buf_size));

// SOCKET FOR WRITING
if ((sdw = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
{
	return SOCKET_ERROR;
}
addr.can_family = PF_CAN;

strcpy(ifr.ifr_name, name);
if (ioctl(sdw, SIOCGIFINDEX, &ifr) < 0)
{
	return SOCK_IOCTL_ERROR;
}
addr.can_ifindex = ifr.ifr_ifindex;

if (bind(sdw, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
	return SOCK_BINDING_ERROR;
}

// SET SOCKET BUFFER SIZE
setsockopt(sdw, SOL_CAN_RAW, SO_SNDBUF, (char *)&sock_buf_size, sizeof(sock_buf_size));

//READ SOCKET NON-BLOCKING
int flags;
if (-1 == (flags = fcntl(sdr, F_GETFL, 0))) flags = 0;
fcntl(sdr, F_SETFL, flags | O_NONBLOCK);

return 0;

```

}

int can\_disconnect(void)  
{  
close(sdw);  
close(sdr);  
return 0;  
}

int can\_send(void)  
{  
int nbytesWrite;

```
// USE SEND STANDARD FRAME
frameWrite.can_id = 0x600;
frameWrite.can_id &= CAN_SFF_MASK;

frameWrite.can_dlc = 8;
strcpy((char *)frameWrite.data, "MangoH ");

if ((nbytesWrite = write(sdw, &frameWrite, sizeof(frameWrite))) != sizeof(frameWrite))
{
    canwrite = false;
    LE_INFO ("Writing error");
	return SOCK_WRITING_ERROR;
}

canwrite = true;
return 0;

```

}

int can\_receive(void)  
{  
nbytesReceive = 1;  
while (nbytesReceive != 0)  
{  
if ((nbytesReceive = read(sdr, &frameRead, sizeof(frameRead))) != sizeof(frameRead))  
{  
return SOCK\_READING\_ERROR;  
}  
/_else  
LE\_INFO(“Receive message %X %X %X %X %X %X %X %X %X”, frameRead.can\_id, frameRead.data[0], frameRead.data[1], frameRead.data[2], frameRead.data[3]  
, frameRead.data[4], frameRead.data[5], frameRead.data[6], frameRead.data[7]);_/  
}

```
return 0;

```

}

You just now to call it in your main :

COMPONENT\_INIT  
{  
can\_connect(“can0”);  
can\_send();  
can\_receive();

}

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [March 21, 2018, 9:15pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/32 "2018-03-21T21:15:18Z")

</div>

Dont forget :

to run can\_init.sh as a Linux script :

chmod 755 can\_init.sh

---

<div class="post-metadata">

### Author: ![raxsix](https://avatars.discourse-cdn.com/v4/letter/r/dbc845/32.png) [@raxsix](https://mangoh.discourse.group/u/raxsix)
#### Post date: [March 22, 2018, 8:01am UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/33 "2018-03-22T08:01:06Z")

</div>

I almost figured that one out, I can use some of your suggested code, very helpful, thanks a lot.  
There’s one thing that I need to understand, what is the  
**triple-sampling on**?

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [March 22, 2018, 8:35am UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/34 "2018-03-22T08:35:10Z")

</div>

I think it’s around the CAN SPI driver setting to sure that not lost can frame when you use SocketCAN.

---

<div class="post-metadata">

### Author: ![abrothman](https://avatars.discourse-cdn.com/v4/letter/a/8797f3/32.png) [@abrothman](https://mangoh.discourse.group/u/abrothman)
#### Post date: [March 28, 2018, 8:13pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/35 "2018-03-28T20:13:50Z")

</div>

@asyal,

Isn’t it about time to make a single, accurate, up-to-date guide to using CAN on a MangOH Red, with ALL of the CORRECT steps in ONE place, as well as a sample app?

---

<div class="post-metadata">

### Author: ![asyal](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/asyal/32/78_2.png) [@asyal](https://mangoh.discourse.group/u/asyal)
#### Post date: [March 29, 2018, 12:20am UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/36 "2018-03-29T00:20:19Z")

</div>

@abrothman yes, it is.  
We will look at providing a much more simpler way for this. Will work with our linux experts and find a more easier way on this .

---

<div class="post-metadata">

### Author: ![sreesyratron](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@sreesyratron](https://mangoh.discourse.group/u/sreesyratron)
#### Post date: [May 18, 2018, 4:55am UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/37 "2018-05-18T04:55:32Z")

</div>

HI Francis ,

Thanks for your source code.  
I was trying Talon CAN interface over MangoH red + WP7502 combo, it is giving me some error.  
Running legato version 16.10.4 . Source compile over Developer studio.

| Description | Resource | Path | Location | Type |
| --- | --- | --- | --- | --- |
| Symbol ‘BMS\_BATP\_DATA’ could not be resolved | sreetestcanComponent.c | /sreetestcan/sreetestcanComponent | line 101 | Semantic Error |
| Symbol ‘BMS\_BATTP\_DATA’ could not be resolved | sreetestcanComponent.c | /sreetestcan/sreetestcanComponent | line 99 | Semantic Error |
| Symbol ‘BMS\_BATU\_DATA’ could not be resolved | sreetestcanComponent.c | /sreetestcan/sreetestcanComponent | line 103 | Semantic Error |
| Symbol ‘BMS\_CHRGR\_CTL’ could not be resolved | sreetestcanComponent.c | /sreetestcan/sreetestcanComponent | line 95 | Semantic Error |
| Symbol ‘BMS\_SOC\_DATA’ could not be resolved | sreetestcanComponent.c | /sreetestcan/sreetestcanComponent | line 97 | Semantic Error |
| Symbol ‘CHG\_TX\_ACT’ could not be resolved | sreetestcanComponent.c | /sreetestcan/sreetestcanComponent | line 93 | Semantic Error |
| Symbol ‘CHG\_TX\_STS’ could not be resolved | sreetestcanComponent.c | /sreetestcan/sreetestcanComponent | line 91 | Semantic Error |

Also attaching the screenshot of my developer studio compiler error.

Please let me know which header file contain CHG parameters ??  
Can you share your whole project directory ( developer studio ) ?

Thanks and Regards,

 ![CAN%20error](https://us1.discourse-cdn.com/flex016/uploads/mangoh/original/1X/8b5742fb050968a86879c4d9fc8e396faea5a753.png)

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [May 21, 2018, 9:43am UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/38 "2018-05-21T09:43:43Z")

</div>

Hello

Ah this is define for message ID. It’s just a example. You can replace this message by your own message ID.

Regards,

---

<div class="post-metadata">

### Author: ![asyal](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/asyal/32/78_2.png) [@asyal](https://mangoh.discourse.group/u/asyal)
#### Post date: [June 22, 2018, 12:34pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/39 "2018-06-22T12:34:35Z")

</div>

hi @abrothman , we have simplified the experience  
[https://github.com/mangOH/mangOH/wiki/Running-the-MCP2515-CAN-driver-for-mangOH-for-mangOH-Red](https://github.com/mangOH/mangOH/wiki/Running-the-MCP2515-CAN-driver-for-mangOH-for-mangOH-Red)

We will also hold a Google hangout on July 18th to walk you through the process on how we built it ground up

---

<div class="post-metadata">

### Author: ![Francis.duhaut](https://sea2.discourse-cdn.com/flex016/user_avatar/mangoh.discourse.group/francis.duhaut/32/350_2.png) [@Francis.duhaut](https://mangoh.discourse.group/u/Francis.duhaut)
#### Post date: [June 22, 2018, 1:02pm UTC](https://mangoh.discourse.group/t/can-iot-card-driver/997/40 "2018-06-22T13:02:44Z")

</div>

Thanks @asyal.

I try it but I not have the mcp251x.ko into the build.

I follow instruction to update the git into the VM

cd ~/mangOH git pull && git submodule update --init  
$ make red\_wp76xx

The build is complete but the driver is not in the image…

[Previous page](https://mangoh.discourse.group/t/can-iot-card-driver/997.md?page=1)

[Next page](https://mangoh.discourse.group/t/can-iot-card-driver/997.md?page=3)
