summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/common/src/pwm.c
blob: ad7b18a258655f3f579e6c58c11cf3eec1f0ca0d (plain)
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
/**************************************************************************//**
  \file  pwm.c

  \brief Implementation of PWM interface.

  \author
      Atmel Corporation: http://www.atmel.com \n
      Support email: avr@atmel.com

    Copyright (c) 2008-2011, Atmel Corporation. All rights reserved.
    Licensed under Atmel's Limited License Agreement (BitCloudTM).

  \internal
    History:
     10/11/08 A. Taradov - Created
     5/04/11 A.Razinkov - Refactored
*******************************************************************************/
/******************************************************************************
 *   WARNING: CHANGING THIS FILE MAY AFFECT CORE FUNCTIONALITY OF THE STACK.  *
 *   EXPERT USERS SHOULD PROCEED WITH CAUTION.                                *
 ******************************************************************************/

/******************************************************************************
                   Includes section
******************************************************************************/
#include <halPwm.h>
#include <pwm.h>

/******************************************************************************
                   Defines section
******************************************************************************/

/******************************************************************************
                   Constants section
******************************************************************************/

/******************************************************************************
                   Global variables section
******************************************************************************/

/******************************************************************************
                   Implementations section
******************************************************************************/

/**************************************************************************//**
\brief Initializes the PWM.

\param [in] pwmUnit - PWM unit number.
                      Equal to ID of Timer/Counter witch serves PWM module.

\return operation status
******************************************************************************/
int HAL_OpenPwm(HAL_PwmUnit_t pwmUnit)
{
  /* Check PWM unit */
  if ((PWM_UNIT_1 == pwmUnit) || (PWM_UNIT_3 == pwmUnit))
    halOpenPwm(pwmUnit);
  else
    return PWM_INVALID_UNIT_STATUS;

  return PWM_SUCCESS_STATUS;
}

/**************************************************************************//**
\brief Starts PWM on specified channel.

\param [in] descriptor - PWM channel descriptor.

\return operation status
******************************************************************************/
int HAL_StartPwm(HAL_PwmDescriptor_t *descriptor)
{
  /* Invalid PWM channel specified */
  if (PWM_INVALID_CHANNEL <= descriptor->channel)
    return PWM_INVALID_CHANNEL_STATUS;
  /* Check PWM unit */
  if ((PWM_UNIT_1 == descriptor->unit) || (PWM_UNIT_3 == descriptor->unit))
  {
    halPreparePwmChannelAccess(descriptor);
    halStartPwm(descriptor);
  }
  else
    return PWM_INVALID_UNIT_STATUS;

  return PWM_SUCCESS_STATUS;
}

/**************************************************************************//**
\brief Stops PWM on specified channel.

\param [in] descriptor - PWM channel descriptor.

\return operation status
******************************************************************************/
int HAL_StopPwm(HAL_PwmDescriptor_t *descriptor)
{
  /* Invalid PWM channel specified */
  if (PWM_INVALID_CHANNEL <= descriptor->channel)
    return PWM_INVALID_CHANNEL_STATUS;
  else
    halStopPwm(descriptor);

  return PWM_SUCCESS_STATUS;
}

/**************************************************************************//**
\brief Sets base frequency of module. Common for all module channels.

\param [in] pwmUnit - PWM unit number. Equal to corresponding Timer/Counter ID.
\param [in] top - value for the TOP register.
\param [in] prescaler - clock prescaler.

\return operation status
******************************************************************************/
int HAL_SetPwmFrequency(HAL_PwmUnit_t pwmUnit, uint16_t top, HAL_PwmPrescaler_t prescaler)
{
  /* Check prescaler value */
  if (PWM_PRESCALER_INVALID <= prescaler)
    return PWM_INVALID_PRESCALER_STATUS;
  else halSetPwmFrequency(pwmUnit, top, prescaler);

  return PWM_SUCCESS_STATUS;
}

/**************************************************************************//**
\brief Sets compare value for the PWM channel.

\param [in] descriptor - PWM channel descriptor.

\return operation status
******************************************************************************/
int HAL_SetPwmCompareValue(HAL_PwmDescriptor_t *descriptor, uint16_t cmpValue)
{
  /* Invalid PWM channel specified */
  if (PWM_INVALID_CHANNEL <= descriptor->channel)
    return PWM_INVALID_CHANNEL_STATUS;
  /* Check PWM unit */
  if ((PWM_UNIT_1 == descriptor->unit) || (PWM_UNIT_3 == descriptor->unit))
    halSetPwmCompareValue(descriptor, cmpValue);
  else
    return PWM_INVALID_UNIT_STATUS;

  return PWM_SUCCESS_STATUS;
}

/**************************************************************************//**
\brief Closes the PWM.

\param [in] pwmUnit - PWM unit number.
                      Equal to ID of Timer/Counter witch serves PWM module.

\return operation status
******************************************************************************/
int HAL_ClosePwm(HAL_PwmUnit_t pwmUnit)
{
  /* Check PWM unit */
  if ((PWM_UNIT_1 == pwmUnit) || (PWM_UNIT_3 == pwmUnit))
    halClosePwm(pwmUnit);
  else
    return PWM_INVALID_UNIT_STATUS;

  return PWM_SUCCESS_STATUS;
}

// eof pwm.c