aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/platforms/LPC8xx/gpt_lld.c
blob: 44601272b3ae736c708d7db87dfa2e7317b59daa (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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
    ChibiOS/RT - Copyright (C) 2006-2013 Giovanni Di Sirio

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

/**
 * @file    LPC8xx/gpt_lld.c
 * @brief   LPC8xx GPT subsystem low level driver source.
 *
 * @addtogroup GPT
 * @{
 */

#include "ch.h"
#include "hal.h"

#if HAL_USE_GPT || defined(__DOXYGEN__)

/*===========================================================================*/
/* Driver exported variables.                                                */
/*===========================================================================*/

/**
 * @brief   GPT1 driver identifier.
 * @note    The driver GPT1 allocates MRT channel0 when enabled.
 */
#if LPC8xx_GPT_USE_MRT0 || defined(__DOXYGEN__)
GPTDriver GPTD1;
#endif

/**
 * @brief   GPT2 driver identifier.
 * @note    The driver GPT1 allocates MRT channel1 when enabled.
 */
#if LPC8xx_GPT_USE_MRT1 || defined(__DOXYGEN__)
GPTDriver GPTD2;
#endif

/**
 * @brief   GPT3 driver identifier.
 * @note    The driver GPT1 allocates MRT channel2 when enabled.
 */
#if LPC8xx_GPT_USE_MRT2 || defined(__DOXYGEN__)
GPTDriver GPTD3;
#endif

/**
 * @brief   GPT4 driver identifier.
 * @note    The driver GPT1 allocates MRT channel3 when enabled.
 */
#if LPC8xx_GPT_USE_MRT3 || defined(__DOXYGEN__)
GPTDriver GPTD4;
#endif

/*===========================================================================*/
/* Driver local variables.                                                   */
/*===========================================================================*/
static uint32_t clk_enabled;

/*===========================================================================*/
/* Driver local functions.                                                   */
/*===========================================================================*/
/**
 * @brief   Shared IRQ handler.
 *
 * @param[in] gptp      pointer to a @p GPTDriver object
 * @param[in] irq_flag  irq flag bit
 */

static void gpt_lld_serve_interrupt( GPTDriver *gptp ) {

  if (gptp->tmr->STAT & 0x01) {
    gptp->tmr->STAT |= 0x01;

    if (gptp->state == GPT_ONESHOT) {
      gptp->state = GPT_READY;                /* Back in GPT_READY state.     */
    }
    gptp->config->callback(gptp);
  }
  return;
}

/*===========================================================================*/
/* Driver interrupt handlers.                                                */
/*===========================================================================*/

/**

 * @brief   MRT IRQ handler.
 *
 */
CH_IRQ_HANDLER(Vector68) {
  CH_IRQ_PROLOGUE();
  
#if LPC8xx_GPT_USE_MRT0
  gpt_lld_serve_interrupt( &GPTD1 );
#endif
  
#if LPC8xx_GPT_USE_MRT1
  gpt_lld_serve_interrupt( &GPTD2 );
#endif

#if LPC8xx_GPT_USE_MRT2
  gpt_lld_serve_interrupt( &GPTD3 );
#endif

#if LPC8xx_GPT_USE_MRT3
  gpt_lld_serve_interrupt( &GPTD4 );
#endif

  CH_IRQ_EPILOGUE();
}

/*===========================================================================*/
/* Driver exported functions.                                                */
/*===========================================================================*/

/**
 * @brief   Low level GPT driver initialization.
 *
 * @notapi
 */
void gpt_lld_init(void) {

#if LPC8xx_GPT_USE_MRT0
  GPTD1.tmr = &(LPC_MRT->Channel[0]);
  gptObjectInit(&GPTD1);
  GPTD1.mask = (1<<0);
#endif

#if LPC8xx_GPT_USE_MRT1
  GPTD2.tmr = &(LPC_MRT->Channel[1]);
  gptObjectInit(&GPTD2);
  GPTD1.mask = (1<<1);
#endif

#if LPC8xx_GPT_USE_MRT2
  GPTD3.tmr = &(LPC_MRT->Channel[2]);
  gptObjectInit(&GPTD3);
  GPTD1.mask = (1<<2);
#endif

#if LPC8xx_GPT_USE_MRT3
  GPTD4.tmr = &(LPC_MRT->Channel[3]);
  gptObjectInit(&GPTD4);
  GPTD1.mask = (1<<3);
#endif

  clk_enabled = FALSE;
  return;
}

/**
 * @brief   Configures and activates a GPT channel.
 *
 * @param[in] gptp      pointer to the @p GPTDriver object
 *
 * @notapi
 */
void gpt_lld_start(GPTDriver *gptp) {

  if( !clk_enabled ) {

    /* Enable clock & reset MRT */
    LPC_SYSCON->SYSAHBCLKCTRL |=  (1<<10);
    LPC_SYSCON->PRESETCTRL    &= ~(1<<7);
    LPC_SYSCON->PRESETCTRL    |=  (1<<7);

    nvicEnableVector(MRT_IRQn,
                     CORTEX_PRIORITY_MASK(LPC8xx_GPT_MRT_IRQ_PRIORITY));

    clk_enabled |= gptp->mask;
  }

  /* Prescaler value calculation.*/
  gptp->pr = (LPC8xx_SYSCLK / gptp->config->frequency);
  chDbgAssert((gptp->pr * gptp->config->frequency) == LPC8xx_SYSCLK,
              "gpt_lld_start(), #1", "invalid frequency");

  /* MRT Channel configuration.*/
  gptp->tmr->CTRL  = 0;
  gptp->tmr->STAT |= 1;

}

/**
 * @brief   Deactivates a GPT channel.
 *
 * @param[in] gptp      pointer to the @p GPTDriver object
 *
 * @notapi
 */
void gpt_lld_stop(GPTDriver *gptp) {

  /* Shared peripheral - 
     mark this channel as disabled */
  clk_enabled &= ~gptp->mask;

  /* All channels disabled? */
  if( !clk_enabled )
  {
    /* Disable periheral */
    nvicDisableVector(MRT_IRQn);
    LPC_SYSCON->SYSAHBCLKCTRL &=  ~(1<<10);
  }
    
  return;
}

/**
 * @brief   Starts the timer in continuous/One shot mode.
 *
 * @param[in] gptp      pointer to the @p GPTDriver object
 * @param[in] interval  period in ticks
 *
 * @notapi
 */
void gpt_lld_start_timer(GPTDriver *gptp, gptcnt_t interval) {

  gptp->tmr->INTVAL = (1<<31)|((interval*gptp->pr) - 1);

  if (gptp->state == GPT_ONESHOT)
    gptp->tmr->CTRL = (1<<1)|1;
  else
    gptp->tmr->CTRL = 1;
}

/**
 * @brief   Stops the timer.
 *
 * @param[in] gptp      pointer to the @p GPTDriver object
 *
 * @notapi
 */
void gpt_lld_stop_timer(GPTDriver *gptp) {

  gptp->tmr->INTVAL = (1<<31);
  gptp->tmr->CTRL   = 0;
  gptp->tmr->STAT  |= 1;
}

/**
 * @brief   Starts the timer in one shot mode and waits for completion.
 * @details This function specifically polls the timer waiting for completion
 *          in order to not have extra delays caused by interrupt servicing,
 *          this function is only recommended for short delays.
 *
 * @param[in] gptp      pointer to the @p GPTDriver object
 * @param[in] interval  time interval in ticks
 *
 * @notapi
 */
void gpt_lld_polled_delay(GPTDriver *gptp, gptcnt_t interval) {

  gptp->tmr->INTVAL = (1<<31)|((interval*gptp->pr) - 1);
  gptp->tmr->CTRL = (1<<1);

  while (gptp->tmr->STAT & (1<<1))
    ;

  gptp->tmr->CTRL   = 0;
  gptp->tmr->STAT  |= 1;
}

#endif /* HAL_USE_GPT */

/** @} */