<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>CoR &#187; socketcan</title>
	<atom:link href="http://blog.cor-net.org/tag/socketcan/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cor-net.org</link>
	<description>...</description>
	<lastBuildDate>Sat, 03 Jul 2010 19:52:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>CAN Interface for the Mini2440 Dev. Board</title>
		<link>http://blog.cor-net.org/embedded/mini2440/can-interface-for-the-mini2440-dev-board/</link>
		<comments>http://blog.cor-net.org/embedded/mini2440/can-interface-for-the-mini2440-dev-board/#comments</comments>
		<pubDate>Tue, 08 Sep 2009 01:21:34 +0000</pubDate>
		<dc:creator>cor</dc:creator>
				<category><![CDATA[Mini2440]]></category>
		<category><![CDATA[can]]></category>
		<category><![CDATA[diy]]></category>
		<category><![CDATA[hardware]]></category>
		<category><![CDATA[interface]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[s3c2440]]></category>
		<category><![CDATA[socketcan]]></category>
		<category><![CDATA[spi]]></category>

		<guid isPermaLink="false">http://blog.cor-net.org/?p=9</guid>
		<description><![CDATA[CAN Interface for Mini2440 using SocketCAN]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><em>As part of the student project where I was using the Mini2440 board, I was also taking care of implementing a CAN network between a few PIC devices and that board.<br />
Here is a very cheap yet powerful CAN interface for that board.</em></p>
<p><img class="aligncenter size-medium wp-image-32" title="Logo-v10-black" src="http://blog.cor-net.org/wp-content/uploads/2009/07/Logo-v10-black-300x164.png" alt="Logo-v10-black" width="150" height="82" /></p>
<p>While being a very cheap board, the mini2440 has also great features, but since the MCU used (Samsung S3C2440) is made for consumer products like phones or GPS, is does not implement any CAN driver.</p>
<p>As I needed one, I built this small card. I guess the design is really easy to understand cause, well &#8230; it&#8217;s really simple, but it might be useful to some people. Feel free to comment or modify the card, just don&#8217;t sell it.</p>
<p><span style="color: #ff0000;">This article <strong>is not finished</strong>, I publish it now cause I&#8217;ve been writing it for few months (since march 2009 &#8230;) and I guess there is already some interesting informations for advanced users, but this is a WIP. I will resume it and add stuff if people are interested and when I have time, and I&#8217;ll also try to answer comments the best I CAN <img src='http://blog.cor-net.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </span></p>
<h1>Hardware</h1>
<p>The board has a IO extension port of 34 pins, which make it possible to access to:</p>
<ul>
<li>+5V and GND : we can power something</li>
<li>an SPI interface</li>
<li>some GPIOs</li>
</ul>
<p>The S3C2440 feature an SPI interface, which is a slave-master bus that allow the MCU to drive peripherals.</p>
<p>Since I didn&#8217;t had much time to develop this, the main purpose of this card is to re-use what&#8217;s available. On linux, the socket-CAN project intend to provide a socket way to access CAN controllers. Few drivers are available, one is for Microchip MCP251x CAN drivers, which provide an SPI interface. As SPI drivers for linux are available in the kernel, and as MCP2515 drivers are provided by the socket-CAN project, I decided to use it.</p>
<p>The Mini2440 provide few IO ports in order to add peripherals, but they&#8217;re not standardized. On the Mini2440 schematic we can see that an SPI port is available on the 34 pins port, that&#8217;s the one I used.</p>
<p>The 34 pins connector is quite hard to find since it is not really a common one. I didn&#8217;t manage to find one on Farnell/RS/&#8230; : the only one I found on Farnell was a Harwin model, but really had too tiny holes, don&#8217;t order it, it won&#8217;t work. However, I did find another connector on Harwin, the M22-7141742, it&#8217;s a 2mm 34pins header which fit great with the board connector. I asked for samples on the harwin website and received it two days latter, they provide a really great service and quality products.</p>
<p>The CAN connector is not a standard one, in our bot we had another norm using a 10 pin header instead of an expansive DB-9.</p>
<p>I made the schematic and PCB under Protel DXP, cause it&#8217;s a standard in my school (hopefully not for long, we are switching to Eagle this year). I can eventually make the schematic and PCB under Eagle with a DB-9 connector if there is demand for it. I&#8217;m not yet comfortable with Eagle though, if someone can do that I would be happy to edit and add his content (and credit them of course).</p>
<p>Schematic:<br />
<a href="http://blog.cor-net.org/wp-content/uploads/2009/07/Can2440-Schem.jpg" rel="wp-prettyPhoto[g9]"><img class="aligncenter size-medium wp-image-27" title="Can2440-Schematic" src="http://blog.cor-net.org/wp-content/uploads/2009/07/Can2440-Schem-299x193.jpg" alt="Can2440-Schematic" width="299" height="193" /></a></p>
<p>PCB:<br />
<a href="http://blog.cor-net.org/wp-content/uploads/2009/07/Can2440-PCB.jpg" rel="wp-prettyPhoto[g9]"><img class="aligncenter size-medium wp-image-26" title="Can2440-PCB" src="http://blog.cor-net.org/wp-content/uploads/2009/07/Can2440-PCB-238x300.jpg" alt="Can2440-PCB" width="238" height="300" /></a></p>
<h1>Software</h1>
<p>I used Embdebian and Angstrom on my board, I guess you should not have any problem using the board with another distro as long as it has a linux kernel <img src='http://blog.cor-net.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The software part relies on 2 things:</p>
<ul>
<li><a href="http://developer.berlios.de/projects/socketcan/">SocketCAN</a></li>
<li><a href="http://kernel.org/">Linux Kernel</a></li>
<p>(<a href="http://repo.or.cz/w/linux-2.6/mini2440.git">Mini2440 Kernel Repository</a> &amp; <a href="http://bliterness.blogspot.com/">news</a>)</ul>
<p>I just heard that the mini2440 repository for the kernel has been merged into the official repository, but in order to work you just need kernel sources with Mini2440 support.</p>
<p>Then you will have to modify the machine definition to configure the SPI interface. As you will see, this is not very clean as we need the driver&#8217;s header to be included, and as the SocketCAN project is not part of the kernel &#8230; But well, it works.</p>
<p>Here is some kind of patch I made, you should be really careful and understand what happens. As the kernel is in active developmental, I can&#8217;t say that it won&#8217;t broke in a few days.<br />
NB: All that somehow explain why I didn&#8217;t do a real patch, cause if you don&#8217;t understand why you&#8217;re copy/pasting this code, you&#8217;re going down.</p>
<p><a href="http://repo.or.cz/w/linux-2.6/mini2440.git?a=blob;f=arch/arm/mach-s3c2440/mach-mini2440.c"><strong><em>arch/arm/mach-s3c2440/mach-mini2440.c</em></strong></a></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// This is ugly, but I didn't find any other clean way as socketcan is external to the kernel</span>
<span style="color: #339933;">#include &quot;/path/to/socketcan/kernel/2.6/include/linux/can/platform/mcp251x.h&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* SPI / CAN MCP251x support */</span>
<span style="color: #993333;">static</span> <span style="color: #993333;">void</span> mini2440_set_cs<span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> s3c2410_spi_info <span style="color: #339933;">*</span>spi<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> cs<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> pol<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #993333;">int</span> pin<span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">switch</span><span style="color: #009900;">&#40;</span>cs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
<span style="color: #b1b100;">case</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">:</span>
pin <span style="color: #339933;">=</span> S3C2410_GPG2<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
printk<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;BUG in %s, wrong CS %d<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> __FUNCTION__<span style="color: #339933;">,</span> cs<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
s3c2410_gpio_setpin<span style="color: #009900;">&#40;</span>pin<span style="color: #339933;">,</span> pol<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
s3c2410_gpio_cfgpin<span style="color: #009900;">&#40;</span>pin<span style="color: #339933;">,</span> S3C2410_GPIO_OUTPUT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">struct</span> s3c2410_spi_info mini2440_spi_pd <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
.<span style="color: #202020;">num_cs</span>         <span style="color: #339933;">=</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">,</span>
.<span style="color: #202020;">set_cs</span>         <span style="color: #339933;">=</span> mini2440_set_cs<span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">int</span> mcp251x_setup<span style="color: #009900;">&#40;</span><span style="color: #993333;">struct</span> spi_device <span style="color: #339933;">*</span>spi<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
printk<span style="color: #009900;">&#40;</span>KERN_INFO <span style="color: #ff0000;">&quot;MINI2440: Loading MCP251x setup<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
s3c2410_gpio_pullup<span style="color: #009900;">&#40;</span>S3C2410_GPG9<span style="color: #339933;">,</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">struct</span> mcp251x_platform_data mcp251x_info __initdata <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
.<span style="color: #202020;">oscillator_frequency</span> <span style="color: #339933;">=</span> <span style="color: #0000dd;">8000000</span><span style="color: #339933;">,</span>
.<span style="color: #202020;">board_specific_setup</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>mcp251x_setup<span style="color: #339933;">,</span>
.<span style="color: #202020;">model</span> <span style="color: #339933;">=</span> CAN_MCP251X_MCP2515<span style="color: #339933;">,</span>
.<span style="color: #202020;">power_enable</span> <span style="color: #339933;">=</span> NULL<span style="color: #339933;">,</span>
.<span style="color: #202020;">transceiver_enable</span> <span style="color: #339933;">=</span> NULL<span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">struct</span> spi_board_info mini2440_spi_board_info<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> __initdata <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
.<span style="color: #202020;">modalias</span>      <span style="color: #339933;">=</span> <span style="color: #ff0000;">&quot;mcp251x&quot;</span><span style="color: #339933;">,</span>
.<span style="color: #202020;">platform_data</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>mcp251x_info<span style="color: #339933;">,</span>
.<span style="color: #202020;">irq</span>           <span style="color: #339933;">=</span> IRQ_EINT17<span style="color: #339933;">,</span> <span style="color: #808080; font-style: italic;">/* GPG9 */</span>
.<span style="color: #202020;">max_speed_hz</span>  <span style="color: #339933;">=</span> <span style="color: #0000dd;">2</span><span style="color: #339933;">*</span><span style="color: #0000dd;">1000</span><span style="color: #339933;">*</span><span style="color: #0000dd;">1000</span><span style="color: #339933;">,</span>
.<span style="color: #202020;">chip_select</span>   <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>
.<span style="color: #202020;">bus_num</span>       <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">,</span>
.<span style="color: #202020;">mode</span>          <span style="color: #339933;">=</span> <span style="color: #0000dd;">0</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
...
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">struct</span> platform_device <span style="color: #339933;">*</span>mini2440_devices<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> __initdata <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
...<span style="color: #339933;">,</span>
<span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>s3c_device_iis<span style="color: #339933;">,</span>
<span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>mini2440_audio<span style="color: #339933;">,</span>
<span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>s3c_device_spi0
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">static</span> <span style="color: #993333;">void</span> __init mini2440_init<span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
...
&nbsp;
<span style="color: #808080; font-style: italic;">/* turn LCD on */</span>
s3c2410_gpio_cfgpin<span style="color: #009900;">&#40;</span>S3C2410_GPC0<span style="color: #339933;">,</span> S3C2410_GPC0_LEND<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">/* Init SPI */</span>
s3c_device_spi0.<span style="color: #202020;">dev</span>.<span style="color: #202020;">platform_data</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span>mini2440_spi_pd<span style="color: #339933;">;</span>
i <span style="color: #339933;">=</span> spi_register_board_info<span style="color: #009900;">&#40;</span>mini2440_spi_board_info<span style="color: #339933;">,</span> ARRAY_SIZE<span style="color: #009900;">&#40;</span>mini2440_spi_board_info<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
printk<span style="color: #009900;">&#40;</span>KERN_INFO <span style="color: #ff0000;">&quot;MINI2440: SPI p_d set %i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
...
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In order to compile the kernel, you must have a SocketCAN repo somewhere on your host computer. You must change the path of the include line to a valid mcp251x header.<br />
Then after preparing your kernel for your board as usual, you should install SocketCAN.</p>
<p>I&#8217;m not gonna enter in the details to make this piece of board, there is plenty of information on the Internet that explain how to make PCBs if you don&#8217;t already know.<br />
Check carefully that there is no short circuit or anything though, it won&#8217;t do any good to your mini2440.</p>
<p>Ok so now you should connect the board to the Mini2440. Be careful to do that before powering the board, I&#8217;m not sure hot plugging it would be a good thing =)</p>
<p>Then you have to load the mcp251x module.</p>
<p>In order to configure the can connection, you have to push your can network bitrate into the proc filesystem.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.cor-net.org/embedded/mini2440/can-interface-for-the-mini2440-dev-board/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
