pwm.c
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (C) 2019 Moxa Inc. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * PWM Sample Application
6  *
7  * Date Author Comment
8  * 2018-12-25 Eddy Chiu Created it.
9  ******************************************************************************/
10 
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <iothinx/iothinxio.h>
50 
52 int main(int argc, char **const argv)
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)
int main(int argc, char **const argv)
Definition: pwm.c:52
IOTHINX_ERR ioThinx_DO_Config_SetPwmCounts(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])