Functions
pwm.c File Reference

PWM Sample More...

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iothinx/iothinxio.h>

Go to the source code of this file.

Functions

int main (int argc, char **const argv)
 

Detailed Description

PWM Sample

Date
2018-12-25
Author
Eddy Chiu
Version
V1.0
Introduction:
This is PWM sample code.
Example:
1. Using default: ./pwm
2. Setting DO slot and PWM channel: ./pwm -s3 -c3 -n1
Default:
Slot of DO module = 2
PWM start channel = 0
PWM channel count = 2
Help:
moxa@Moxa:~$ sudo ./pwm -h
PWM sample program.

Usage: ./pwm [OPTIONS]

Options:
        -s       Slot of DO module. Default slot = 2
        -c       PWM start channel. Default channel = 0
        -n       PWM channel count. Default count = 2

Library:
DO APIs

Definition in file pwm.c.

Function Documentation

◆ main()

int main ( int  argc,
char **const  argv 
)
/*******************************************************************************
* Copyright (C) 2019 Moxa Inc. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* PWM Sample Application
*
* Date Author Comment
* 2018-12-25 Eddy Chiu Created it.
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iothinx/iothinxio.h>
int main(int argc, char **const argv)
{
int32_t rc;
uint32_t do_slot = 2;
uint8_t do_modes[16];
uint8_t pwm_channel_start = 0;
uint8_t pwm_channel_count = 2;
uint32_t pwm_starts = 0;
uint16_t pwm_freqs[4];
uint16_t pwm_dutys[4];
uint32_t pwm_counts[4];
int32_t i;
char ch;
while (-1 != (rc = getopt(argc, argv, "c:hn:s:")))
{
switch (rc)
{
case 'c':
pwm_channel_start = atoi(optarg);
break;
case 'n':
pwm_channel_count = atoi(optarg);
break;
case 's':
do_slot = atoi(optarg);
break;
case 'h':
default:
printf("PWM sample program.\n\n");
printf("Usage: ./pwm [OPTIONS]\n\n");
printf("Options:\n");
printf("\t%-8s Slot of DO module. Default slot = %d\n", "-s", do_slot);
printf("\t%-8s PWM start channel. Default channel = %d\n", "-c", pwm_channel_start);
printf("\t%-8s PWM channel count. Default count = %d\n", "-n", pwm_channel_count);
printf("\n");
return 0;
}
}
printf("DO slot = %lu\n", do_slot);
printf("PWM start channel = %u\n", pwm_channel_start);
printf("PWM channel count = %u\n", pwm_channel_count);
// initialize ioThinx I/O
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Client_Init() = %d\n", rc);
return -1;
}
// temporarily set config
for (i = 0; i < pwm_channel_count; i++)
{
do_modes[i] = DO_MODE_PWM;
}
rc = ioThinx_DO_Config_SetModes(do_slot, pwm_channel_start, pwm_channel_count, do_modes);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_Config_SetModes() = %d\n", rc);
}
for (i = 0; i < pwm_channel_count; i++)
{
pwm_counts[i] = 0; // infinite
}
rc = ioThinx_DO_Config_SetPwmCounts(do_slot, pwm_channel_start, pwm_channel_count, pwm_counts);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_Config_SetPwmCounts() = %d\n", rc);
}
for (i = 0; i < pwm_channel_count; i++)
{
pwm_freqs[i] = 5;
pwm_dutys[i] = 50;
}
rc = ioThinx_DO_Config_SetPwmConfigures(do_slot, pwm_channel_start, pwm_channel_count, pwm_freqs, pwm_dutys);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_Config_SetPwmConfigures() = %d\n", rc);
}
// reload config
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_IO_Config_Reload() = %d\n", rc);
}
// start pwm
pwm_starts = 0;
for (i = 0; i < pwm_channel_count; i++)
{
pwm_starts |= 0x1 << (pwm_channel_start + i);
}
rc = ioThinx_DO_SetPwmStarts(do_slot, pwm_starts);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_SetPwmStarts() = %d\n", rc);
}
// set value
do
{
pwm_dutys[0] = (pwm_dutys[0] >= 100) ? 0 : pwm_dutys[0] + 10; // runtime adjust the duty cycle
rc = ioThinx_DO_SetPwmConfigures(do_slot, pwm_channel_start, 1, pwm_freqs, pwm_dutys);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_SetPwmConfigures() = %d\n", rc);
}
for (i = 0; i < pwm_channel_count; i++)
{
printf("[%lu:%u] frequency = %d, duty cycle = %d\n", do_slot, pwm_channel_start+i, pwm_freqs[i], pwm_dutys[i]);
}
ch = getchar();
}
while (ch != 'q');
rc = ioThinx_DO_SetPwmStops(do_slot, pwm_starts);
if (rc != IOTHINX_ERR_OK)
{
printf("ioThinx_DO_SetPwmStops() = %d\n", rc);
}
return 0;
}

Definition at line 52 of file pwm.c.

53 {
54  int32_t rc;
55  uint32_t do_slot = 2;
56  uint8_t do_modes[16];
57  uint8_t pwm_channel_start = 0;
58  uint8_t pwm_channel_count = 2;
59  uint32_t pwm_starts = 0;
60  uint16_t pwm_freqs[4];
61  uint16_t pwm_dutys[4];
62  uint32_t pwm_counts[4];
63  int32_t i;
64  char ch;
65 
66  while (-1 != (rc = getopt(argc, argv, "c:hn:s:")))
67  {
68  switch (rc)
69  {
70  case 'c':
71  pwm_channel_start = atoi(optarg);
72  break;
73  case 'n':
74  pwm_channel_count = atoi(optarg);
75  break;
76  case 's':
77  do_slot = atoi(optarg);
78  break;
79  case 'h':
80  default:
81  printf("PWM sample program.\n\n");
82  printf("Usage: ./pwm [OPTIONS]\n\n");
83  printf("Options:\n");
84  printf("\t%-8s Slot of DO module. Default slot = %d\n", "-s", do_slot);
85  printf("\t%-8s PWM start channel. Default channel = %d\n", "-c", pwm_channel_start);
86  printf("\t%-8s PWM channel count. Default count = %d\n", "-n", pwm_channel_count);
87  printf("\n");
88  return 0;
89  }
90  }
91 
92  printf("DO slot = %lu\n", do_slot);
93  printf("PWM start channel = %u\n", pwm_channel_start);
94  printf("PWM channel count = %u\n", pwm_channel_count);
95 
96  // initialize ioThinx I/O
98  if (rc != IOTHINX_ERR_OK)
99  {
100  printf("ioThinx_IO_Client_Init() = %d\n", rc);
101  return -1;
102  }
103 
104  // temporarily set config
105  for (i = 0; i < pwm_channel_count; i++)
106  {
107  do_modes[i] = DO_MODE_PWM;
108  }
109  rc = ioThinx_DO_Config_SetModes(do_slot, pwm_channel_start, pwm_channel_count, do_modes);
110  if (rc != IOTHINX_ERR_OK)
111  {
112  printf("ioThinx_DO_Config_SetModes() = %d\n", rc);
113  }
114 
115  for (i = 0; i < pwm_channel_count; i++)
116  {
117  pwm_counts[i] = 0; // infinite
118  }
119  rc = ioThinx_DO_Config_SetPwmCounts(do_slot, pwm_channel_start, pwm_channel_count, pwm_counts);
120  if (rc != IOTHINX_ERR_OK)
121  {
122  printf("ioThinx_DO_Config_SetPwmCounts() = %d\n", rc);
123  }
124 
125  for (i = 0; i < pwm_channel_count; i++)
126  {
127  pwm_freqs[i] = 5;
128  pwm_dutys[i] = 50;
129  }
130  rc = ioThinx_DO_Config_SetPwmConfigures(do_slot, pwm_channel_start, pwm_channel_count, pwm_freqs, pwm_dutys);
131  if (rc != IOTHINX_ERR_OK)
132  {
133  printf("ioThinx_DO_Config_SetPwmConfigures() = %d\n", rc);
134  }
135 
136  // reload config
138  if (rc != IOTHINX_ERR_OK)
139  {
140  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
141  }
142 
143  // start pwm
144  pwm_starts = 0;
145  for (i = 0; i < pwm_channel_count; i++)
146  {
147  pwm_starts |= 0x1 << (pwm_channel_start + i);
148  }
149  rc = ioThinx_DO_SetPwmStarts(do_slot, pwm_starts);
150  if (rc != IOTHINX_ERR_OK)
151  {
152  printf("ioThinx_DO_SetPwmStarts() = %d\n", rc);
153  }
154 
155  // set value
156  do
157  {
158  pwm_dutys[0] = (pwm_dutys[0] >= 100) ? 0 : pwm_dutys[0] + 10; // runtime adjust the duty cycle
159  rc = ioThinx_DO_SetPwmConfigures(do_slot, pwm_channel_start, 1, pwm_freqs, pwm_dutys);
160  if (rc != IOTHINX_ERR_OK)
161  {
162  printf("ioThinx_DO_SetPwmConfigures() = %d\n", rc);
163  }
164  for (i = 0; i < pwm_channel_count; i++)
165  {
166  printf("[%lu:%u] frequency = %d, duty cycle = %d\n", do_slot, pwm_channel_start+i, pwm_freqs[i], pwm_dutys[i]);
167  }
168  ch = getchar();
169  }
170  while (ch != 'q');
171 
172  rc = ioThinx_DO_SetPwmStops(do_slot, pwm_starts);
173  if (rc != IOTHINX_ERR_OK)
174  {
175  printf("ioThinx_DO_SetPwmStops() = %d\n", rc);
176  }
177 
178  return 0;
179 }
IOTHINX_ERR ioThinx_DO_SetPwmStops(uint32_t slot, uint32_t stops)
#define DO_MODE_PWM
Definition: iothinxio.h:292
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
IOTHINX_ERR ioThinx_DO_Config_SetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_DO_Config_SetPwmConfigures(uint32_t slot, uint8_t start, uint8_t count, uint16_t frequencies[], uint16_t duty_cycles[])
IOTHINX_ERR ioThinx_IO_Client_Init(void)
IOTHINX_ERR ioThinx_DO_SetPwmConfigures(uint32_t slot, uint8_t start, uint8_t count, uint16_t frequencies[], uint16_t duty_cycles[])
IOTHINX_ERR ioThinx_DO_SetPwmStarts(uint32_t slot, uint32_t starts)
IOTHINX_ERR ioThinx_DO_Config_SetPwmCounts(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])