ostimer_at91.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (C) 2001-2007 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * $Log$
  35. * Revision 1.23 2009/01/17 11:26:37 haraldkipp
  36. * Getting rid of two remaining BSD types in favor of stdint.
  37. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  38. *
  39. * Revision 1.22 2008/08/22 09:25:33 haraldkipp
  40. * Clock value caching and new functions NutArchClockGet, NutClockGet and
  41. * NutClockSet added.
  42. *
  43. * Revision 1.21 2008/08/11 06:59:12 haraldkipp
  44. * BSD types replaced by stdint types (feature request #1282721).
  45. *
  46. * Revision 1.20 2008/08/06 12:51:01 haraldkipp
  47. * Added support for Ethernut 5 (AT91SAM9XE reference design).
  48. *
  49. * Revision 1.19 2008/07/08 08:25:04 haraldkipp
  50. * NutDelay is no more architecture specific.
  51. * Number of loops per millisecond is configurable or will be automatically
  52. * determined.
  53. * A new function NutMicroDelay provides shorter delays.
  54. *
  55. * Revision 1.18 2008/02/15 16:58:41 haraldkipp
  56. * Spport for AT91SAM7SE512 added.
  57. *
  58. * Revision 1.17 2007/10/04 19:59:47 olereinhardt
  59. * Support for SAM7S256 added
  60. *
  61. * Revision 1.16 2007/08/17 10:44:37 haraldkipp
  62. * Timer enable/disable macro replaces previous global interrupt
  63. * enable/disable or function calling.
  64. *
  65. * Revision 1.15 2007/04/12 09:03:48 haraldkipp
  66. * Miserable delay routine will now honor milliseconds on a 73 MHz ARM.
  67. *
  68. * Revision 1.14 2007/02/15 16:14:39 haraldkipp
  69. * Periodic interrupt timer can be used as a system clock.
  70. *
  71. * Revision 1.13 2006/10/08 16:48:07 haraldkipp
  72. * Documentation fixed
  73. *
  74. * Revision 1.12 2006/09/29 12:37:36 haraldkipp
  75. * Now working correctly, if the CPU is running on the second PLL.
  76. *
  77. * Revision 1.11 2006/09/05 12:27:25 haraldkipp
  78. * PLL clock calculation re-arranged to prevent 32-bit overflow.
  79. * NutTimerMillisToTicks() returned wrong result. Shane Buckham reported
  80. * this long time ago. Many thanks. Needs to be fixed for other platforms too.
  81. *
  82. * Revision 1.10 2006/08/31 18:59:50 haraldkipp
  83. * Added support for the AT91SAM9260. We now determine between processor and
  84. * master clock. A new API function At91GetMasterClock() had been added to
  85. * query the latter.
  86. *
  87. * Revision 1.9 2006/08/05 12:00:01 haraldkipp
  88. * NUT_CPU_FREQ did not override AT91_PLL_MAINCK or NUT_PLL_CPUCLK. Fixed.
  89. *
  90. * Revision 1.8 2006/07/26 11:17:16 haraldkipp
  91. * Defining AT91_PLL_MAINCK will automatically determine SAM7X clock by
  92. * reading PLL settings.
  93. *
  94. * Revision 1.7 2006/07/05 07:59:41 haraldkipp
  95. * Daidai's support for AT91SAM7X added.
  96. *
  97. * Revision 1.6 2006/06/28 17:10:35 haraldkipp
  98. * Include more general header file for ARM.
  99. *
  100. * Revision 1.5 2006/03/02 19:53:01 haraldkipp
  101. * Bugfix. The system timer configuration was based on a fixed MCU clock
  102. * of 66.6 MHz. Now it uses the actual frequency.
  103. *
  104. * Revision 1.4 2006/01/05 16:46:25 haraldkipp
  105. * Added support for CY22393 programmable clock chip.
  106. *
  107. * Revision 1.3 2005/10/24 08:34:13 haraldkipp
  108. * Moved AT91 family specific header files to sbudir arm.
  109. * Use new IRQ API.
  110. *
  111. * Revision 1.2 2005/08/02 17:46:45 haraldkipp
  112. * Major API documentation update.
  113. *
  114. * Revision 1.1 2005/07/26 18:02:26 haraldkipp
  115. * Moved from dev.
  116. *
  117. * Revision 1.2 2005/07/20 09:17:26 haraldkipp
  118. * Default NUT_CPU_FREQ and NUT_TICK_FREQ added.
  119. * NutTimerIntr() removed, because we can use the hardware independent code.
  120. *
  121. * Revision 1.1 2005/05/27 17:16:40 drsung
  122. * Moved the file.
  123. *
  124. * Revision 1.5 2005/04/05 17:50:46 haraldkipp
  125. * Use register names in gba.h.
  126. *
  127. * Revision 1.4 2004/11/08 19:16:37 haraldkipp
  128. * Hacked in Gameboy timer support
  129. *
  130. * Revision 1.3 2004/10/03 18:42:21 haraldkipp
  131. * No GBA support yet, but let the compiler run through
  132. *
  133. * Revision 1.2 2004/09/08 10:19:39 haraldkipp
  134. * Running on AT91 and S3C, thanks to James Tyou
  135. *
  136. */
  137. #define NUT_DEPRECATED
  138. #include <cfg/os.h>
  139. #include <cfg/clock.h>
  140. #include <arch/arm.h>
  141. #include <dev/irqreg.h>
  142. #include <sys/timer.h>
  143. #ifndef NUT_CPU_FREQ
  144. #ifdef NUT_PLL_CPUCLK
  145. #include <dev/cy2239x.h>
  146. #elif !defined(AT91_PLL_MAINCK)
  147. #define NUT_CPU_FREQ 73728000UL
  148. #endif /* !AT91_PLL_MAINCK */
  149. #endif /* !NUT_CPU_FREQ */
  150. /*!
  151. * \addtogroup xgNutArchArmOsTimerAt91
  152. */
  153. /*@{*/
  154. #ifndef NUT_TICK_FREQ
  155. #define NUT_TICK_FREQ 1000UL
  156. #endif
  157. /*!
  158. * \brief Initialize system timer.
  159. *
  160. * This function is automatically called by Nut/OS
  161. * during system initialization.
  162. *
  163. * Nut/OS uses on-chip timer 0 for its timer services.
  164. * Applications should not modify any registers of this
  165. * timer, but make use of the Nut/OS timer API. Timer 1
  166. * and timer 2 are available to applications.
  167. */
  168. void NutRegisterTimer(void (*handler) (void *))
  169. {
  170. #if defined(NUT_TICK_AT91PIT)
  171. /* Set compare value for the specified tick frequency. */
  172. #if defined(AT91_PLL_MAINCK)
  173. outr(PIT_MR, (At91GetMasterClock() / (16 * NUT_TICK_FREQ) - 1) << PIT_PIV_LSB);
  174. #else
  175. outr(PIT_MR, (NutGetCpuClock() / (16 * NUT_TICK_FREQ) - 1) << PIT_PIV_LSB);
  176. #endif
  177. /* Register system interrupt handler. */
  178. NutRegisterSysIrqHandler(&syssig_PIT, handler, NULL);
  179. /* Enable interval timer and interval timer interrupts */
  180. outr(PIT_MR, inr(PIT_MR) | PIT_PITEN | PIT_PITIEN);
  181. NutSysIrqEnable(&syssig_PIT);
  182. inr(PIT_PIVR);
  183. #else /* NUT_TICK_AT91PIT */
  184. #if defined(MCU_AT91SAM7X) || defined(MCU_AT91SAM7S256) || defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9G45) || defined (MCU_AT91SAM7SE512) || defined(MCU_AT91SAM9XE512)
  185. /* Enable TC0 clock. */
  186. outr(PMC_PCER, _BV(TC0_ID));
  187. #endif
  188. /* Disable the Clock Counter */
  189. outr(TC0_CCR, TC_CLKDIS);
  190. /* Disable all interrupts */
  191. outr(TC0_IDR, 0xFFFFFFFF);
  192. /* Clear the status register. */
  193. inr(TC0_SR);
  194. /* Select divider and compare trigger */
  195. outr(TC0_CMR, TC_CLKS_MCK32 | TC_CPCTRG);
  196. /* Enable the Clock counter */
  197. outr(TC0_CCR, TC_CLKEN);
  198. /* Validate the RC compare interrupt */
  199. outr(TC0_IER, TC_CPCS);
  200. /* Register timer interrupt handler. */
  201. NutRegisterIrqHandler(&sig_TC0, handler, 0);
  202. /* Set to lowest priority. */
  203. NutIrqSetPriority(&sig_TC0, 0);
  204. /* Enable timer 0 interrupts */
  205. NutIrqEnable(&sig_TC0);
  206. //outr(AIC_IECR, _BV(TC0_ID));
  207. /* Set compare value for 1 ms. */
  208. #if defined(AT91_PLL_MAINCK)
  209. outr(TC0_RC, At91GetMasterClock() / (32 * NUT_TICK_FREQ));
  210. #else
  211. outr(TC0_RC, NutGetCpuClock() / (32 * NUT_TICK_FREQ));
  212. #endif
  213. /* Software trigger starts the clock. */
  214. outr(TC0_CCR, TC_SWTRG);
  215. #endif /* NUT_TICK_AT91PIT */
  216. }
  217. #if defined(AT91_PLL_MAINCK)
  218. #if !defined(AT91_SLOW_CLOCK)
  219. /* This is just a guess and may be completely wrong. */
  220. #define AT91_SLOW_CLOCK 32000
  221. #endif
  222. /*!
  223. * \brief Determine the PLL output clock frequency.
  224. *
  225. * \param plla Specifies the PLL, 0 for default, 1 for alternate.
  226. *
  227. * \return Frequency of the selected PLL in Hertz.
  228. */
  229. static unsigned int At91GetPllClock(int plla)
  230. {
  231. unsigned int rc;
  232. unsigned int pllr;
  233. unsigned int divider;
  234. /*
  235. * The main oscillator clock frequency is specified by the
  236. * configuration. It's usually equal to the on-board crystal.
  237. */
  238. rc = AT91_PLL_MAINCK;
  239. /* Retrieve the clock generator register of the selected PLL. */
  240. #if defined(CKGR_PLLAR) && defined(CKGR_PLLBR)
  241. pllr = plla ? inr(CKGR_PLLAR) : inr(CKGR_PLLBR);
  242. #else
  243. pllr = inr(CKGR_PLLR);
  244. #endif
  245. /* Extract the divider value. */
  246. divider = (pllr & CKGR_DIV) >> CKGR_DIV_LSB;
  247. if (divider) {
  248. rc /= divider;
  249. rc *= ((pllr & CKGR_MUL) >> CKGR_MUL_LSB) + 1;
  250. }
  251. return rc;
  252. }
  253. /*!
  254. * \brief Determine the processor clock frequency.
  255. *
  256. * \return CPU clock frequency in Hertz.
  257. */
  258. static uint32_t At91GetProcessorClock(void)
  259. {
  260. unsigned int rc = 0;
  261. unsigned int mckr = inr(PMC_MCKR);
  262. /* Determine the clock source. */
  263. switch(mckr & PMC_CSS) {
  264. case PMC_CSS_SLOW_CLK:
  265. /* Slow clock selected. */
  266. rc = AT91_SLOW_CLOCK;
  267. break;
  268. case PMC_CSS_MAIN_CLK:
  269. /* Main clock selected. */
  270. rc = AT91_PLL_MAINCK;
  271. break;
  272. #if defined(PMC_CSS_PLLA_CLK)
  273. case PMC_CSS_PLLA_CLK:
  274. /* PLL A clock selected. */
  275. rc = At91GetPllClock(1);
  276. break;
  277. #endif
  278. #if defined(PMC_CSS_PLLB_CLK)
  279. case PMC_CSS_PLLB_CLK:
  280. /* PLL (B) clock selected. */
  281. rc = At91GetPllClock(0);
  282. break;
  283. #elif defined(PMC_CSS_PLL_CLK)
  284. case PMC_CSS_PLL_CLK:
  285. /* PLL (B) clock selected. */
  286. rc = At91GetPllClock(0);
  287. break;
  288. #endif
  289. }
  290. /* Handle pre-scaling. */
  291. mckr &= PMC_PRES;
  292. mckr >>= PMC_PRES_LSB;
  293. if (mckr < 7) {
  294. rc /= _BV(mckr);
  295. }
  296. else {
  297. rc = 0;
  298. }
  299. return rc;
  300. }
  301. /*!
  302. * \brief Determine the master clock frequency.
  303. *
  304. * \deprecated Use NutArchClockGet(NUT_HWCLK_PERIPHERAL)
  305. *
  306. * \return Master clock frequency in Hertz.
  307. */
  308. uint32_t At91GetMasterClock(void)
  309. {
  310. return NutArchClockGet(NUT_HWCLK_PERIPHERAL);
  311. }
  312. #endif /* AT91_PLL_MAINCK */
  313. #ifndef NUT_CPU_FREQ
  314. /*!
  315. * \brief Return the CPU clock in Hertz.
  316. *
  317. * On several AT91 CPUs the processor clock may differ from the clock
  318. * driving the peripherals. In this case At91GetMasterClock() will
  319. * provide the correct master clock.
  320. *
  321. * \return CPU clock frequency in Hertz.
  322. */
  323. uint32_t NutArchClockGet(int idx)
  324. {
  325. uint32_t rc = 0;
  326. if (idx == NUT_HWCLK_CPU) {
  327. #if defined(AT91_PLL_MAINCK)
  328. rc = At91GetProcessorClock();
  329. #elif defined(NUT_PLL_CPUCLK)
  330. rc = Cy2239xGetFreq(NUT_PLL_CPUCLK, 7);
  331. #else
  332. #warning "No CPU Clock defined"
  333. #endif
  334. }
  335. #if defined(AT91_PLL_MAINCK)
  336. if (idx == NUT_HWCLK_PERIPHERAL) {
  337. rc = At91GetProcessorClock();
  338. #if defined(PMC_MDIV)
  339. switch(inr(PMC_MCKR) & PMC_MDIV) {
  340. case PMC_MDIV_2:
  341. rc /= 2;
  342. break;
  343. case PMC_MDIV_4:
  344. rc /= 4;
  345. break;
  346. case PMC_MDIV_3:
  347. rc /= 3;
  348. break;
  349. }
  350. #endif
  351. }
  352. #ifdef PMC_PLLADIV2
  353. if (inr(PMC_MCKR) & PMC_PLLADIV2)
  354. {
  355. rc /= 2;
  356. }
  357. #endif
  358. #endif
  359. return rc;
  360. }
  361. #endif
  362. /*!
  363. * \brief Return the number of system ticks per second.
  364. *
  365. * \return System tick frequency in Hertz.
  366. */
  367. uint32_t NutGetTickClock(void)
  368. {
  369. unsigned int rc;
  370. #if defined(NUT_TICK_AT91PIT)
  371. rc = ((inr(PIT_MR) & PIT_PIV) + 1) * 16;
  372. #else
  373. rc = inr(TC0_RC) * 32;
  374. #endif
  375. if (rc) {
  376. #if defined(AT91_PLL_MAINCK)
  377. return At91GetMasterClock() / rc;
  378. #else
  379. return NutGetCpuClock() / rc;
  380. #endif
  381. }
  382. return NUT_TICK_FREQ;
  383. }
  384. /*!
  385. * \brief Calculate system ticks for a given number of milliseconds.
  386. */
  387. uint32_t NutTimerMillisToTicks(uint32_t ms)
  388. {
  389. return (ms * NutGetTickClock()) / 1000;
  390. }
  391. /*@}*/