aboutsummaryrefslogtreecommitdiff
path: root/src/drivers/kinetis/drv_pwm_servo.c
blob: 8a3d583ac2b5cfdb6475998dfbd58ed21c04e4b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/****************************************************************************
 *
 *   Copyright (C) 2015 PX4 Development Team. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

/*
 * @file drv_pwm_servo.c
 *
 * Servo driver supporting PWM servos connected to kinetis flex timer modules.
 *
 */
#include <nuttx/config.h>
#include <stdio.h>
#include <errno.h>
#include <chip.h>
#include <kinetis_ftm.h>
#include <kinetis_internal.h>
#include <up_arch.h>
#include <kinetis_sim.h>

#include "drv_pwm_servo.h"


/**
 * Intialise the PWM servo outputs using the specified configuration.
 *
 * @param channel_mask	Bitmask of channels (LSB = channel 0) to enable.
 *			This allows some of the channels to remain configured
 *			as GPIOs or as another function.
 * @return		OK on success.
 */
 int
 up_pwm_servo_init(uint32_t channel_mask)
 {
   
   for (size_t i = 0; i < PWM_SERVO_MAX_TIMERS; ++i) {
     
     const struct pwm_servo_timer* timer = &pwm_servo_timers[i];
     uint32_t regval;
     
     regval = getreg32(timer->scgc);
     putreg32(regval | timer->scgc_enable, timer->scgc); // enable clock signal
     up_pwm_servo_set_rate_group_update(i, 50); // 50Hz default
       
   }

   for (size_t i = 0; i < PWM_SERVO_MAX_CHANNELS; ++i) {
     const struct pwm_servo_channel* channel = &pwm_servo_channels[i];

     if ((1 << i) & channel_mask) {
       // enable edge aligned pwm
       putreg32(0x28, channel->timer->ftm_base + KINETIS_FTM_CSC_OFFSET(channel->ftm_channel));
       // configure pin
       kinetis_pinconfig(channel->pinconfig); 
     }

      
   }

   return 0;
 }

/**
 * De-initialise the PWM servo outputs.
 */
void
up_pwm_servo_deinit(void)
{
  
}

/**
 * Arm or disarm servo outputs.
 *
 * When disarmed, servos output no pulse.
 *
 * @bug This function should, but does not, guarantee that any pulse
 *      currently in progress is cleanly completed.
 *
 * @param armed		If true, outputs are armed; if false they
 *			are disarmed.
 */
void up_pwm_servo_arm(bool armed)
{
  for (size_t i = 0; i < PWM_SERVO_MAX_TIMERS; ++i) {
    const struct pwm_servo_timer* timer = &pwm_servo_timers[i];
    
    if (armed) {
      putreg32(FTM_SC_CLKS_SYSCLK, timer->ftm_base + KINETIS_FTM_SC_OFFSET);
    } else {
      putreg32(FTM_SC_CLKS_NONE, timer->ftm_base + KINETIS_FTM_SC_OFFSET);
    }
    
  }
}


/**
 * Set the servo update rate for all rate groups.
 *
 * @param rate		The update rate in Hz to set.
 * @return		OK on success, -ERANGE if an unsupported update rate is set.
 */
int
up_pwm_servo_set_rate(unsigned rate)
{
  
  return 0;
}

/**
 * Get a bitmap of output channels assigned to a given rate group.
 *
 * @param group		The rate group to query. Rate groups are assigned contiguously
 *			starting from zero.
 * @return		A bitmap of channels assigned to the rate group, or zero if
 *			the group number has no channels.
 */
uint32_t
up_pwm_servo_get_rate_group(unsigned group)
{
  return 0;
}

/**
 * Set the update rate for a given rate group.
 *
 * @param group		The rate group whose update rate will be changed.
 * @param rate		The update rate in Hz.
 * @return		OK if the group was adjusted, -ERANGE if an unsupported update rate is set.
 */
int
up_pwm_servo_set_rate_group_update(unsigned group, unsigned rate)
{
  const struct pwm_servo_timer* timer = &pwm_servo_timers[group];

  putreg32(FTM_SC_PS_4, timer->ftm_base + KINETIS_FTM_SC_OFFSET);
  putreg32(0, timer->ftm_base + KINETIS_FTM_CNT_OFFSET);
  putreg32(48000, timer->ftm_base + KINETIS_FTM_MOD_OFFSET);
  
  return 0;
}

/**
 * Set the current output value for a channel.
 *
 * @param channel	The channel to set.
 * @param value		The output pulse width in microseconds.
 */
int
up_pwm_servo_set(unsigned channel, servo_position_t value)
{
  const struct pwm_servo_channel* ch = &pwm_servo_channels[channel];
  
  putreg32(value * 48000 / 2000, ch->timer->ftm_base + KINETIS_FTM_CV_OFFSET(ch->ftm_channel));
  return 0;
}

/**
 * Get the current output value for a channel.
 *
 * @param channel	The channel to read.
 * @return		The output pulse width in microseconds, or zero if
 *			outputs are not armed or not configured.
 */
servo_position_t
up_pwm_servo_get(unsigned channel)
{
  return 0;
}


/*
int helloworld_main(int args, char *argv[]) {
  printf("Setting timer\n");

  uint8_t ftm_channel = 5;
  struct pwm_timer* timer = &pwm0;
  

  uint32_t regval;
  regval = getreg32(timer->scgc);
  putreg32(regval | timer->scgc_enable, timer->scgc); // enable clock signal to FTM0

  putreg32(0, timer->ftm_base + KINETIS_FTM_CNT_OFFSET);
  putreg32(36864, timer->ftm_base + KINETIS_FTM_MOD_OFFSET);
  
  putreg32(0x28, timer->ftm_base + KINETIS_FTM_CSC_OFFSET(ftm_channel)); // channle, enable edge aligned pwm

  putreg32(FTM_SC_CLKS_SYSCLK | FTM_SC_PS_1, timer->ftm_base + KINETIS_FTM_SC_OFFSET); // set clock source of FTM module
  putreg32(atoi(argv[1]), timer->ftm_base + KINETIS_FTM_CV_OFFSET(ftm_channel));

  kinetis_pinconfig(PIN_PORTD | PIN5 |  PIN_ALT4_OUTPUT | PIN_ALT4_HIGHDRIVE ) ;
  
  printf("Done\n");
  return 0;
}
*/