pwm.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /**
  2. * \file
  3. *
  4. * \brief SAM PWM Driver for SAMB11
  5. *
  6. * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #ifndef PWM_H_INCLUDED
  47. #define PWM_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_samb_pwm_group SAM Pulse Width Modulation Driver (PWM)
  50. *
  51. * This driver for Atmel&reg; | SMART SAM devices provides an interface for the
  52. * configuration and management of the device's Pulse Width Modulation Module
  53. * (PWM) functionality for waveform generation operations.
  54. *
  55. * The following peripherals are used by this module:
  56. * - PWM (Pulse Width Modulation)
  57. *
  58. * The following devices can use this module:
  59. * - Atmel | SMART SAM B11
  60. *
  61. * The outline of this documentation is as follows:
  62. * - \ref asfdoc_samb_pwm_prerequisites
  63. * - \ref asfdoc_samb_pwm_module_overview
  64. * - \ref asfdoc_samb_pwm_special_considerations
  65. * - \ref asfdoc_samb_pwm_extra_info
  66. * - \ref asfdoc_samb_pwm_examples
  67. * - \ref asfdoc_samb_pwm_api_overview
  68. *
  69. *
  70. * \section asfdoc_samb_pwm_prerequisites Prerequisites
  71. *
  72. * There are no prerequisites for this module.
  73. *
  74. *
  75. * \section asfdoc_samb_pwm_module_overview Module Overview
  76. *
  77. * PWM is a flexible fully configurable module which has the following features:
  78. * - 4 independent configurable pulse width modulator (PWM) blocks.
  79. * - It can be connected to an external on board low pass filter to produce
  80. * different analog control voltages depending on the configured duty cycle.
  81. * - The base frequency of PWM block is the XO clock (26MHz) or the RC oscillator.
  82. * - Different duty cycles could be obtained
  83. * - The frequency of the PWM pulse is programmable in steps according
  84. * to the following relation:
  85. * \f[
  86. * f_{PWM} = \frac{f_{PWM_{base}}} {64 \times 2^i}
  87. * \f]
  88. * \f[
  89. * i = 0 - 8
  90. * \f]
  91. *
  92. * f<SUB>PWM</SUB><SUB>base</SUB> can be selected to have different values
  93. * according to the following table. Minimum and maximum fre-quencies supported
  94. * for each clock selection is listed in the table as well.
  95. *
  96. * <table>
  97. * <caption>Frequency support</caption>
  98. * <tr>
  99. * <th>f<SUB>PWM</SUB><SUB>base</SUB></th>
  100. * <th>Maximum frequency</th>
  101. * <th>Minimum frequency</th>
  102. * </tr>
  103. * <tr>
  104. * <td>26 MHz</td>
  105. * <td>406.25 KHz</td>
  106. * <td>1.586 KHz</td>
  107. * </tr>
  108. * <tr>
  109. * <td>13 MHz</td>
  110. * <td>203.125 KHz</td>
  111. * <td>793.45 Hz</td>
  112. * </tr>
  113. * <tr>
  114. * <td>6.5 MHz</td>
  115. * <td>101.562 KHz</td>
  116. * <td>396.72 Hz</td>
  117. * </tr>
  118. * <tr>
  119. * <td>3.25 MHz</td>
  120. * <td>50.781 KHz</td>
  121. * <td>198.36 Hz</td>
  122. * </tr>
  123. * </table>
  124. *
  125. * \section asfdoc_samb_pwm_special_considerations Special Considerations
  126. *
  127. * There are no special considerations for this module.
  128. *
  129. * \section asfdoc_samb_pwm_extra_info Extra Information
  130. *
  131. * For extra information, see \ref asfdoc_samb_pwm_extra. This includes:
  132. * - \ref asfdoc_samb_pwm_extra_acronyms
  133. * - \ref asfdoc_samb_pwm_extra_dependencies
  134. * - \ref asfdoc_samb_pwm_extra_errata
  135. * - \ref asfdoc_samb_pwm_extra_history
  136. *
  137. *
  138. * \section asfdoc_samb_pwm_examples Examples
  139. *
  140. * For a list of examples related to this driver, see
  141. * \ref asfdoc_samb_pwm_exqsg.
  142. *
  143. *
  144. * \section asfdoc_samb_pwm_api_overview API Overview
  145. * @{
  146. */
  147. #include <compiler.h>
  148. #include "gpio.h"
  149. #ifdef __cplusplus
  150. extern "C" {
  151. #endif
  152. /**
  153. * \brief PWM device configuration enum.
  154. *
  155. * Enum for the possible settings of pwm device configuration.
  156. */
  157. enum pwm_device_select {
  158. /** PWM module 0 */
  159. PWM0 = 0,
  160. /** PWM module 1 */
  161. PWM1,
  162. /** PWM module 2 */
  163. PWM2,
  164. /** PWM module 3 */
  165. PWM3,
  166. };
  167. /**
  168. * \brief PWM sample method configuration enum.
  169. *
  170. * Enum for the possible settings of pwm sample method configuration.
  171. */
  172. enum pwm_sample_method {
  173. /** Samples agcdata at >= 1024 cycles and does not lose precision */
  174. PWM_SAMPLE_METHOD_0 = 0,
  175. /** Samples at PWM period but will lose LSBs if less than 1024 */
  176. PWM_SAMPLE_METHOD_1,
  177. };
  178. /**
  179. * \brief PWM period configuration enum.
  180. *
  181. * Enum for the possible settings of pwm period configuration.
  182. */
  183. enum pwm_period {
  184. /** PWM perid 0 */
  185. PWM_PERIOD_0 = 0,
  186. /** PWM perid 1 */
  187. PWM_PERIOD_1,
  188. /** PWM perid 2 */
  189. PWM_PERIOD_2,
  190. /** PWM perid 3 */
  191. PWM_PERIOD_3,
  192. /** PWM perid 4 */
  193. PWM_PERIOD_4,
  194. /** PWM perid 5 */
  195. PWM_PERIOD_5,
  196. /** PWM perid 6 */
  197. PWM_PERIOD_6,
  198. /** PWM perid 7 */
  199. PWM_PERIOD_7,
  200. /** PWM perid 8 */
  201. PWM_PERIOD_8,
  202. };
  203. /**
  204. * \brief PWM clock select configuration enum.
  205. *
  206. * Enum for the possible settings of pwm clock select configuration.
  207. */
  208. enum pwm_clock_select {
  209. /** Clock 26MHz */
  210. PWM_CLOCK_SELECT_26_0 = 0,
  211. /** Clock 13MHz */
  212. PWM_CLOCK_SELECT_13_0,
  213. /** Clock 6.5MHz */
  214. PWM_CLOCK_SELECT_6_5,
  215. /** Clock 3.25MHz */
  216. PWM_CLOCK_SELECT_3_25,
  217. };
  218. /**
  219. * \brief PWM configuration structure.
  220. *
  221. * Configuration structure for a PWM instance. This structure should be
  222. * initialized by the \ref pwm_get_config_defaults() function before being
  223. * modified by the user application.
  224. */
  225. struct pwm_config {
  226. /** 1 to inverse the polarity */
  227. bool output_polarity;
  228. /** AGC data format */
  229. bool agcdata_format;
  230. /** Sample method */
  231. enum pwm_sample_method sample_method;
  232. /** Programmable PWM update period */
  233. enum pwm_period period;
  234. /** This value specifies the duty cycle(%) */
  235. uint8_t duty_cycle;
  236. /** PWM Source Clock Frequency Select */
  237. enum pwm_clock_select clock_select;
  238. /** PWM PAD pin number */
  239. uint32_t pin_number_pad;
  240. /** PWM PAD pinmux selection */
  241. uint32_t pinmux_sel_pad;
  242. };
  243. /** \name Configuration and initialization
  244. * @{
  245. */
  246. void pwm_get_config_defaults(struct pwm_config *const config);
  247. void pwm_set_duty_cycle(enum pwm_device_select device_select, \
  248. uint8_t duty_cycle);
  249. void pwm_set_period(enum pwm_device_select device_select, \
  250. enum pwm_period period);
  251. enum status_code pwm_init(enum pwm_device_select device_select, \
  252. const struct pwm_config *const config);
  253. /** @} */
  254. /**
  255. * \name Enable/Disable and Reset
  256. * @{
  257. */
  258. void pwm_enable(enum pwm_device_select device_select);
  259. void pwm_disable(enum pwm_device_select device_select);
  260. void pwm_reset(enum pwm_device_select device_select);
  261. /** @} */
  262. /** @}*/
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. /**
  267. * \page asfdoc_samb_pwm_extra Extra Information for PWM Driver
  268. *
  269. * \section asfdoc_samb_pwm_extra_acronyms Acronyms
  270. * Below is a table listing the acronyms used in this module, along with their
  271. * intended meanings.
  272. *
  273. * <table>
  274. * <tr>
  275. * <th>Acronym</th>
  276. * <th>Description</th>
  277. * </tr>
  278. * <tr>
  279. * <td>PWM</td>
  280. * <td>Pulse Width Modulation</td>
  281. * </tr>
  282. * </table>
  283. *
  284. *
  285. * \section asfdoc_samb_pwm_extra_dependencies Dependencies
  286. * There are no dependencies related to this driver.
  287. *
  288. *
  289. * \section asfdoc_samb_pwm_extra_errata Errata
  290. * There are no errata related to this driver.
  291. *
  292. *
  293. * \section asfdoc_samb_pwm_extra_history Module History
  294. * An overview of the module history is presented in the table below, with
  295. * details on the enhancements and fixes made to the module since its first
  296. * release. The current version of this corresponds to the newest version in
  297. * the table.
  298. *
  299. * <table>
  300. * <tr>
  301. * <th>Changelog</th>
  302. * </tr>
  303. * <tr>
  304. * <td>Initial Release</td>
  305. * </tr>
  306. * </table>
  307. */
  308. /**
  309. * \page asfdoc_samb_pwm_exqsg Examples for PWM Driver
  310. *
  311. * This is a list of the available Quick Start guides (QSGs) and example
  312. * applications for \ref asfdoc_samb_pwm_group. QSGs are simple examples with
  313. * step-by-step instructions to configure and use this driver in a selection of
  314. * use cases. Note that QSGs can be compiled as a standalone application or be
  315. * added to the user application.
  316. *
  317. * - \subpage asfdoc_samb_pwm_basic_use_case
  318. *
  319. * \page asfdoc_samb_pwm_document_revision_history Document Revision History
  320. *
  321. * <table>
  322. * <tr>
  323. * <th>Doc. Rev.</td>
  324. * <th>Date</td>
  325. * <th>Comments</td>
  326. * </tr>
  327. * <tr>
  328. * <td>A</td>
  329. * <td>09/2015</td>
  330. * <td>Initial release</td>
  331. * </tr>
  332. * </table>
  333. */
  334. #endif