LED Controller via Ethernet with KSDK 2.0 and FreeRTOS

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LED Controller via Ethernet with KSDK 2.0 and FreeRTOS

LED Controller via Ethernet with KSDK 2.0 and FreeRTOS

What is needed:

SW:

  • KDS 3.2
  • KSDK 2.0
  • Hercules
  • (Visual Studio 2015)

 

HW:

  • FRDM-K64F
  • Ethernet Cable

 

Install KSDK 2.0

Be sure, that you have downloaded correct package KSDK 2.0 for FRDM-K64F, for all procedure please follow instructions mentioned at How to: install KSDK 2.0

 

Install KDS 3.2

Be sure, that you will work with the newest Kinetis Design Studio v.3.2, please see New Kinetis Design Studio v3.2.0 available for more details.

149416_149416.pngKDS.png

 

Import demo example

For start with this example we will build on existing demo project, located under

C:\Freescale\<ksdk2.0_package>\boards\frdmk64f\demo_apps\lwip\lwip_tcpecho\freertos\kds

Please, import this example according to the procedure described at How to: import example in KSDK

 

Start with programming

Let´s start with programming example for LED RGB controlling via ethernet

 

Checking and parsing incoming packets

This packet is divided into header and data. The header represents first two bytes and the remaining three bytes are occupied by data. The zero byte is 0xFF and the first byte must be 0x00. The second byte represents red color, the third byte green color and the last fourth byte presents blue color.

149436_149436.pngfram_final.png

lwip_tcpecho_freertos.c

Server is listening on port 7 and waiting for a connection from the client. If the client sends 5B, it find out according to header whether it is correct 5B. If so, each RGB parts will be parsed individually and set the LED accordingly.

 

    while (1)     {         /* Grab new connection. */         err = netconn_accept(conn, &newconn);         /* Process the new connection. */         if (err == ERR_OK)         {             struct netbuf *buf;             u8_t *data;             u16_t len;               while ((err = netconn_recv(newconn, &buf)) == ERR_OK)             {                 do                 {                     netbuf_data(buf, &data, &len);                     if(len==5){                         if(data[0]==0xFF && data[1]==0x00){                             if(data[2]>0){                                 LED_RED_ON();                             }else {                                 LED_RED_OFF();                             }                             if(data[3]>0){                                 LED_GREEN_ON();                             }else {                                 LED_GREEN_OFF();                             }                             if(data[4]>0){                                 LED_BLUE_ON();                             }else {                                 LED_BLUE_OFF();                             }                             //err = netconn_write(newconn, "ok", 2, NETCONN_COPY);                         }                     }                 } while (netbuf_next(buf) >= 0);                 netbuf_delete(buf);             }             /* Close connection and discard connection identifier. */             netconn_close(newconn);             netconn_delete(newconn);         }     } 

 

Initializing LEDs

 

It is needed to set all LEDs in pin_mux.c in BOARD_InitPins() function and initialize in lwip_tcpecho_freertos.c in main() function.

 

pin_mux.c

Go to BOARD_InitPins() and at the end of the function add these lines:

149445_149445.pnginit_leds_0.png

Copy and paste to your project

    CLOCK_EnableClock(kCLOCK_PortB);     CLOCK_EnableClock(kCLOCK_PortE);     PORT_SetPinMux(PORTB, 21U, kPORT_MuxAsGpio);     PORT_SetPinMux(PORTB, 22U, kPORT_MuxAsGpio);     PORT_SetPinMux(PORTE, 26U, kPORT_MuxAsGpio);

 

lwip_tcpecho_freertos.c

Go to main() and initialize LEDs

149446_149446.pnginit_leds.png

Copy and paste to your project

LED_RED_INIT(LOGIC_LED_OFF); LED_GREEN_INIT(LOGIC_LED_OFF); LED_BLUE_INIT(LOGIC_LED_OFF);

Set up connection on PC site

Set PC on 192.168.1.100

149449_149449.pngip_address.png

 

Controlling the application

Hercules

For test connection you can use Hercules. After testing don´t forget disconnect Hercules, server can handle only one TCP connection.

IP Address of the board is set on 192.168.1.102

149451_149451.pnghercules.png

It works - the board is green lighting:

149452_149452.jpgFullSizeRender.jpg

 

Visualization in Visual Studio 2015

For better controlling we will create application in Visual Studio 2015.

Start with new project and create new form according this:

149453_149453.pngLED CONTROLLER.png

 

And set functionality for all items.

Client connects to the IP Address on port 7 and sends our packet according selected colour.

For red color are data set on { 0xFF, 0x00, 1, 0, 0 };, for yellow { 0xFF, 0x00, 1, 1, 0 }; etc.

 

Form1.cs

public partial class Form1 : Form     {         Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);          public Form1()         {             InitializeComponent();                      }          private void button1_Click(object sender, EventArgs e)         {             try              {                 s.Connect(IPAddress.Parse(textBox1.Text), 7);                 byte[] data = { 0xFF, 0x00, 0, 0, 0 };                 groupBox1.Enabled = true;                 button1.Enabled = false;                 s.Send(data);                  textBox1.Enabled = false;             }             catch              {                 MessageBox.Show("Connection failed");             }         }          private void button_red_Click(object sender, EventArgs e)         {             if (s.Connected) {                 byte[] data = { 0xFF, 0x00, 1, 0, 0 };                 s.Send(data);             }         }          private void button_green_Click(object sender, EventArgs e)         {             if (s.Connected)             {                 byte[] data = { 0xFF, 0x00, 0, 1, 0 };                 s.Send(data);             }         }          private void button_blue_Click(object sender, EventArgs e)         {             if (s.Connected)             {                 byte[] data = { 0xFF, 0x00, 0, 0, 1 };                 s.Send(data);             }         }          private void button_black_Click(object sender, EventArgs e)         {             if (s.Connected)             {                 byte[] data = { 0xFF, 0x00, 0, 0, 0 };                 s.Send(data);             }         }          private void button_white_Click(object sender, EventArgs e)         {             if (s.Connected)             {                 byte[] data = { 0xFF, 0x00, 1, 1, 1 };                 s.Send(data);             }         }          private void button_cyan_Click(object sender, EventArgs e)         {             if (s.Connected)             {                 byte[] data = { 0xFF, 0x00, 0, 1, 1 };                 s.Send(data);             }         }          private void button_magenta_Click(object sender, EventArgs e)         {             if (s.Connected)             {                 byte[] data = { 0xFF, 0x00, 1, 0, 1 };                 s.Send(data);             }         }          private void button_yellow_Click(object sender, EventArgs e)         {             if (s.Connected)             {                 byte[] data = { 0xFF, 0x00, 1, 1, 0 };                 s.Send(data);             }         }     }

 

 

Enjoy!

 

Iva

Labels (1)
Attachments
No ratings
Version history
Last update:
‎09-10-2020 02:55 AM
Updated by: