summaryrefslogtreecommitdiff
path: root/digital/zigbit/bitcloud/stack/Components/HAL/avr/atmega1281/common/include/halPwm.h
blob: 6c026ce06bd70d8057f896919be6768dd1ca4a59 (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
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
/**************************************************************************//**
  \file  halPwm.h

  \brief Declaration of hardware depended 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.                                *
 ******************************************************************************/

#ifndef _HALPWM_H
#define _HALPWM_H

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

/******************************************************************************
                   Definitions section
******************************************************************************/
/* Waveform Generation Mode bits position. PWM module independent. */
#define WGMn0 0
#define WGMn1 1
#define WGMn2 3
#define WGMn3 4

/* Force Output Compare bits position. PWM module independent. */
#define FOCnA 7
#define FOCnB 6
#define FOCnC 5

/* Clock Select bits position. PWM module independent. */
#define CSn0 0
#define CSn1 1
#define CSn2 2

/* Compare Output Mode bits position. PWM module independent. */
#define COMnA0 6
#define COMnA1 7
#define COMnB0 4
#define COMnB1 5
#define COMnC0 2
#define COMnC1 3

/* PWN unit base channel pins position. PWM module dependent. */
#define PWM_UNIT_1_BASE_CHANNEL_PIN PB5
#define PWM_UNIT_3_BASE_CHANNEL_PIN PE3

/* Force Output Compare base bit. PWM module independent. */
#define FOCNX_BASE_BIT FOCnA
/* Compare Output Mode base bit number. PWM module independent. */
#define COMNX0_BASE_BIT COMnA0
/* Compare Output Mode bitfield size. PWM module and channel independent. */
#define COMNX_BITFIELD_SIZE 2
/* Compare Output Mode low bit number. PWM module independent. */
#define COMnx0(descriptor) ((descriptor)->service.COMnx0)
/* Compare Output Mode high bit number. PWM module independent. */
#define COMnx1(descriptor) ((descriptor)->service.COMnx0 + 1)
/* Output Compare Register. PWM module and channel dependent. */
#define OCRnx(descriptor) (*((descriptor)->service.OCRnx))
/* Data Direction Rregister. PWM module dependent. */
#define DDRn(descriptor) (*((descriptor)->service.DDRn))

/******************************************************************************
                   Types section
******************************************************************************/

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

\param [in] pwmUnit - PWM unit number.
                      Equal to ID of Timer/Counter witch serves PWM module.
******************************************************************************/
void halOpenPwm(HAL_PwmUnit_t pwmUnit);

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

\param [in] descriptor - PWM channel descriptor.
******************************************************************************/
void halStartPwm(HAL_PwmDescriptor_t *descriptor);

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

\param [in] descriptor - PWM channel descriptor.
******************************************************************************/
void halStopPwm(HAL_PwmDescriptor_t *descriptor);

/**************************************************************************//**
\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.
******************************************************************************/
void halSetPwmFrequency(HAL_PwmUnit_t pwmUnit, uint16_t top, HAL_PwmPrescaler_t prescaler);

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

\param [in] descriptor - PWM channel descriptor.
******************************************************************************/
void halSetPwmCompareValue(HAL_PwmDescriptor_t *descriptor, uint16_t cmpValue);

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

\param [in] pwmUnit - PWM unit number.
                      Equal to ID of Timer/Counter witch serves PWM module.
******************************************************************************/
void halClosePwm(HAL_PwmUnit_t pwmUnit);

/**************************************************************************//**
\brief Prepare PWM channel access. Determine control registers, ports, pins etc.

\param [in] descriptor - PWM channel descriptor.
******************************************************************************/
void halPreparePwmChannelAccess(HAL_PwmDescriptor_t *descriptor);

/**************************************************************************//**
\brief Configure corresponding pin as PWM out.

\param [in] descriptor - PWM channel descriptor.
******************************************************************************/
static inline void halMakeOutPwmPin(HAL_PwmDescriptor_t *descriptor)
{
  DDRn(descriptor) |=
    (1 << (descriptor->service.pwmBaseChannelPin + descriptor->channel));
}

/**************************************************************************//**
\brief Configure corresponding PWM output pin as in.

\param [in] descriptor - PWM channel descriptor.
******************************************************************************/
static inline void halMakeInPwmPin(HAL_PwmDescriptor_t *descriptor)
{
  DDRn(descriptor) &=
    ~(1 << (descriptor->service.pwmBaseChannelPin + descriptor->channel));
}

/**************************************************************************//**
\brief Perform two-step writing to 16-bit registers with special access rule:
       TCNTn, OCRnA/B/C, ICRn.

\param [in] reg - register address.
\param [in] word - word to move.
******************************************************************************/
static inline void halMoveWordToRegister(volatile uint16_t *reg, uint16_t word)
{
ATOMIC_SECTION_ENTER
  /* High byte writing */
  *((volatile uint8_t*)(reg) + 1) = (uint8_t)(word >> 8);
  /* Low byte writing */
  *(volatile uint8_t*)(reg) = (uint8_t)(word);
ATOMIC_SECTION_LEAVE
}

/**************************************************************************//**
\brief Perform two-step reading of 16-bit registers with special access rule:
       TCNTn, OCRnA/B/C, ICRn.

\param [in] reg - register address.

\return register value
******************************************************************************/
static inline uint16_t halReadWordFromRegister(volatile uint16_t *reg)
{
  uint16_t word;
ATOMIC_SECTION_ENTER
  /* Low byte reading */
  word = *(volatile uint8_t*)(reg);
  /* High byte reading */
  word |= ((uint16_t)(*((volatile uint8_t*)(reg) + 1)) << 8);
ATOMIC_SECTION_LEAVE
  return word;
}

#endif /* _HALPWM_H */

// eof halPwm.h