pac.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /**
  2. * \file
  3. *
  4. * \brief SAM Peripheral Access Controller Driver
  5. *
  6. * Copyright (C) 2012-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 PAC_H_INCLUDED
  47. #define PAC_H_INCLUDED
  48. /**
  49. * \defgroup asfdoc_sam0_pac_group SAM Peripheral Access Controller (PAC) Driver
  50. *
  51. * This driver for Atmel&reg; | SMART ARM&reg;-based microcontroller provides
  52. * an interface for the locking and unlocking of peripheral registers within
  53. * the device. When a peripheral is locked, accidental writes to the peripheral
  54. * will be blocked and a CPU exception will be raised.
  55. *
  56. * The following peripherals are used by this module:
  57. * - PAC (Peripheral Access Controller)
  58. *
  59. * The following devices can use this module:
  60. * - Atmel | SMART SAM D20/D21
  61. * - Atmel | SMART SAM R21
  62. * - Atmel | SMART SAM D09/D10/D11
  63. * - Atmel | SMART SAM L21/L22
  64. * - Atmel | SMART SAM DA1
  65. * - Atmel | SMART SAM C20/C21
  66. * - Atmel | SMART SAM HA1
  67. *
  68. * The outline of this documentation is as follows:
  69. * - \ref asfdoc_sam0_pac_prerequisites
  70. * - \ref asfdoc_sam0_pac_module_overview
  71. * - \ref asfdoc_sam0_pac_special_considerations
  72. * - \ref asfdoc_sam0_pac_extra_info
  73. * - \ref asfdoc_sam0_pac_examples
  74. * - \ref asfdoc_sam0_pac_api_overview
  75. *
  76. *
  77. * \section asfdoc_sam0_pac_prerequisites Prerequisites
  78. *
  79. * There are no prerequisites for this module.
  80. *
  81. *
  82. * \section asfdoc_sam0_pac_module_overview Module Overview
  83. *
  84. * The SAM devices are fitted with a Peripheral Access Controller (PAC)
  85. * that can be used to lock and unlock write access to a peripheral's
  86. * registers (see \ref asfdoc_sam0_pac_non_write_protected). Locking a
  87. * peripheral minimizes the risk of unintended configuration changes to a
  88. * peripheral as a consequence of \ref asfdoc_sam0_pac_code_run_away
  89. * or use of a \ref asfdoc_sam0_pac_module_pointer.
  90. *
  91. * Physically, the PAC restricts write access through the AHB bus to registers
  92. * used by the peripheral, making the register non-writable. PAC locking of
  93. * modules should be implemented in configuration critical applications where
  94. * avoiding unintended peripheral configuration changes are to be regarded in
  95. * the highest of priorities.
  96. *
  97. * All interrupt must be disabled while a peripheral is unlocked to make sure
  98. * correct lock/unlock scheme is upheld.
  99. *
  100. * \subsection asfdoc_sam0_pac_locking_scheme Locking Scheme
  101. * The module has a built in safety feature requiring that an already locked
  102. * peripheral is not relocked, and that already unlocked peripherals are not
  103. * unlocked again. Attempting to unlock and already unlocked peripheral, or
  104. * attempting to lock a peripheral that is currently locked will generate a
  105. * CPU exception. This implies that the implementer must keep
  106. * strict control over the peripheral's lock-state before modifying them. With
  107. * this added safety, the probability of stopping runaway code increases as
  108. * the program pointer can be caught inside the exception handler, and necessary
  109. * countermeasures can be initiated. The implementer should also consider using
  110. * sanity checks after an unlock has been performed to further increase the
  111. * security.
  112. *
  113. * \subsection asfdoc_sam0_pac_correct_implementation Recommended Implementation
  114. * A recommended implementation of the PAC can be seen in
  115. * \ref asfdoc_sam0_pac_rec_imp_diagram "the figure below".
  116. *
  117. * \anchor asfdoc_sam0_pac_rec_imp_diagram
  118. * \dot
  119. * digraph correct {
  120. * subgraph cluster_a {
  121. * style="filled, dotted";
  122. * coler=lightgray;
  123. * init [label="Initialize Peripheral", shape=box];
  124. * lock [label="Lock peripheral", shape=box];
  125. * label="Initialization and code";
  126. * init -> lock;
  127. * }
  128. * subgraph cluster_b {
  129. * cli [label="Disable global interrupts", shape=box,
  130. * style=dotted];
  131. * unlock [label="Unlock peripheral", shape=box];
  132. * sanity [label="Sanity Check", shape=box, style=dotted];
  133. * modify [label="Modify peripheral", shape=box];
  134. * lock2 [label="Lock peripheral", shape=box];
  135. * sei [label="Enable global interrupts", shape=box
  136. * style=dotted];
  137. *
  138. * label="Peripheral Modification";
  139. * cli -> unlock;
  140. * unlock -> sanity
  141. * sanity -> modify;
  142. * modify -> lock2;
  143. * lock2 -> sei;
  144. * }
  145. * lock -> cli [label=
  146. * "Other initialization\n and enable interrupts if applicable"
  147. * , style=dotted];
  148. * }
  149. * \enddot
  150. *
  151. * \subsection asfdoc_sam0_pac_enabled_interrupt Why Disable Interrupts
  152. * Global interrupts must be disabled while a peripheral is unlocked as an
  153. * interrupt handler would not know the current state of the peripheral lock. If
  154. * the interrupt tries to alter the lock state, it can cause an exception as it
  155. * potentially tries to unlock an already unlocked peripheral. Reading current
  156. * lock state is to be avoided as it removes the security provided by the PAC
  157. * (\ref asfdoc_sam0_pac_check_lock).
  158. *
  159. * \note Global interrupts should also be disabled when a peripheral is unlocked
  160. * inside an interrupt handler.
  161. *
  162. * An example to illustrate the potential hazard of not disabling interrupts is
  163. * shown in \ref asfdoc_sam0_pac_int_hazard_diagram "the diagram below".
  164. *
  165. * \anchor asfdoc_sam0_pac_int_hazard_diagram
  166. * \dot
  167. * digraph enabled_interrupt {
  168. * subgraph cluster_0{
  169. * label="Main routine";
  170. * {node [style="filled", color=black, fillcolor=white]
  171. * init [label="Initialize and lock peripherals", shape=box];
  172. * main_unlock [label="Unlock peripheral", shape=box,
  173. * fillcolor=green];
  174. * main_modify [label="Modify peripheral", shape=box];}
  175. * main_lock [label="Lock peripheral", shape=box];
  176. * init -> main_unlock [label="User code"];
  177. * main_unlock -> main_modify;
  178. * main_modify -> main_lock [style=dotted];
  179. * }
  180. * subgraph cluster_1 {
  181. * label="Interrupt handler";
  182. * int_unlock [label="Unlock peripheral", shape=box,
  183. * style=filled, fillcolor=red];
  184. * int_modify [label="Modify peripheral", shape=box];
  185. * int_lock [label="Lock peripheral", shape=box];
  186. * int_unlock -> int_modify [style=dotted];
  187. * int_modify -> int_lock [style=dotted];
  188. * }
  189. * exception [label="Exception", shape=box, style=filled, fillcolor=red];
  190. * main_modify -> int_unlock [label=" Interrupt"];
  191. * int_unlock -> exception;
  192. * exception -> exception;
  193. * }
  194. * \enddot
  195. *
  196. * \subsection asfdoc_sam0_pac_code_run_away Run-away Code
  197. * Run-away code can be caused by the MCU being operated outside its
  198. * specification, faulty code, or EMI issues. If a runaway code occurs, it is
  199. * favorable to catch the issue as soon as possible. With a correct
  200. * implementation of the PAC, the runaway code can potentially be stopped.
  201. *
  202. * A graphical example showing how a PAC implementation will behave for
  203. * different circumstances of runaway code in shown in
  204. * \ref asfdoc_sam0_pac_code_runaway_diagram "the first" and
  205. * \ref asfdoc_sam0_pac_code_runaway_diagram2 "second figures below".
  206. *
  207. * \anchor asfdoc_sam0_pac_code_runaway_diagram
  208. * \dot
  209. * digraph run_away {
  210. * subgraph cluster_away1{
  211. * rankdir=TB;
  212. * color=white;
  213. * runaway1 [label="Run-away code", shape=box];
  214. * node [shape=plaintext];
  215. * program1 [label=<
  216. * <table>
  217. * <tr>
  218. * <td>PC#</td>
  219. * <td>Code</td>
  220. * </tr>
  221. * <tr>
  222. * <td>0x0020</td>
  223. * <td>initialize peripheral</td>
  224. * </tr>
  225. * <tr>
  226. * <td>0x0025</td>
  227. * <td>lock peripheral</td>
  228. * </tr>
  229. * <tr>
  230. * <td>...</td>
  231. * <td>...</td>
  232. * </tr>
  233. * <tr>
  234. * <td>0x0080</td>
  235. * <td>set sanity argument</td>
  236. * </tr>
  237. * <tr>
  238. * <td port="f0" BGCOLOR="green">...</td>
  239. * <td BGCOLOR="green">...</td>
  240. * </tr>
  241. * <tr>
  242. * <td BGCOLOR="green">0x0115</td>
  243. * <td BGCOLOR="green">disable interrupts</td>
  244. * </tr>
  245. * <tr>
  246. * <td BGCOLOR="green">0x0120</td>
  247. * <td BGCOLOR="green">unlock peripheral</td>
  248. * </tr>
  249. * <tr>
  250. * <td BGCOLOR="red">0x0125</td>
  251. * <td BGCOLOR="red">check sanity argument</td>
  252. * </tr>
  253. * <tr>
  254. * <td>0x0130</td>
  255. * <td>modify peripheral</td>
  256. * </tr>
  257. * <tr>
  258. * <td>0x0140</td>
  259. * <td>lock peripheral</td>
  260. * </tr>
  261. * <tr>
  262. * <td>0x0145</td>
  263. * <td>disable interrupts</td>
  264. * </tr>
  265. * </table>
  266. * >]
  267. * runaway1 -> program1:f0;
  268. * label="1. Run-away code is caught in sanity check.\nA CPU exception is executed."
  269. * }
  270. * subgraph cluster_away2{
  271. * rankdir=TB;
  272. * runaway2 [label="Run-away code", shape=box];
  273. * color=white;
  274. * node [shape=plaintext];
  275. * program2 [label=<
  276. * <table>
  277. * <tr>
  278. * <td>PC#</td>
  279. * <td>Code</td>
  280. * </tr>
  281. * <tr>
  282. * <td>0x0020</td>
  283. * <td>initialize peripheral</td>
  284. * </tr>
  285. * <tr>
  286. * <td>0x0025</td>
  287. * <td>lock peripheral</td>
  288. * </tr>
  289. * <tr>
  290. * <td>...</td>
  291. * <td>...</td>
  292. * </tr>
  293. * <tr>
  294. * <td>0x0080</td>
  295. * <td>set sanity argument</td>
  296. * </tr>
  297. * <tr>
  298. * <td >...</td>
  299. * <td >...</td>
  300. * </tr>
  301. * <tr>
  302. * <td >0x0115</td>
  303. * <td >disable interrupts</td>
  304. * </tr>
  305. * <tr>
  306. * <td >0x0120</td>
  307. * <td >unlock peripheral</td>
  308. * </tr>
  309. * <tr>
  310. * <td >0x0125</td>
  311. * <td >check sanity argument</td>
  312. * </tr>
  313. * <tr>
  314. * <td port="f0" BGCOLOR="red">0x0130</td>
  315. * <td BGCOLOR="red">modify peripheral</td>
  316. * </tr>
  317. * <tr>
  318. * <td>0x0140</td>
  319. * <td>lock peripheral</td>
  320. * </tr>
  321. * <tr>
  322. * <td>0x0145</td>
  323. * <td>disable interrupts</td>
  324. * </tr>
  325. * </table>
  326. * >]
  327. * runaway2 -> program2:f0;
  328. * label="2. Run-away code is caught when modifying\nlocked peripheral. A CPU exception is executed."
  329. * }
  330. * }
  331. * \enddot
  332. *
  333. * \anchor asfdoc_sam0_pac_code_runaway_diagram2
  334. * \dot
  335. * digraph run_away2 {
  336. * subgraph cluster_away3{
  337. * rankdir=TB;
  338. * runaway3 [label="Run-away code", shape=box];
  339. * color=white;
  340. * node [shape=plaintext];
  341. * program3 [label=<
  342. * <table>
  343. * <tr>
  344. * <td>PC#</td>
  345. * <td>Code</td>
  346. * </tr>
  347. * <tr>
  348. * <td>0x0020</td>
  349. * <td>initialize peripheral</td>
  350. * </tr>
  351. * <tr>
  352. * <td>0x0025</td>
  353. * <td>lock peripheral</td>
  354. * </tr>
  355. * <tr>
  356. * <td>...</td>
  357. * <td>...</td>
  358. * </tr>
  359. * <tr>
  360. * <td>0x0080</td>
  361. * <td>set sanity argument</td>
  362. * </tr>
  363. * <tr>
  364. * <td >...</td>
  365. * <td >...</td>
  366. * </tr>
  367. * <tr>
  368. * <td >0x0115</td>
  369. * <td >disable interrupts</td>
  370. * </tr>
  371. * <tr>
  372. * <td >0x0120</td>
  373. * <td >unlock peripheral</td>
  374. * </tr>
  375. * <tr>
  376. * <td >0x0125</td>
  377. * <td >check sanity argument</td>
  378. * </tr>
  379. * <tr>
  380. * <td >0x0130</td>
  381. * <td >modify peripheral</td>
  382. * </tr>
  383. * <tr>
  384. * <td port="f0" BGCOLOR="red">0x0140</td>
  385. * <td BGCOLOR="red">lock peripheral</td>
  386. * </tr>
  387. * <tr>
  388. * <td>0x0145</td>
  389. * <td>disable interrupts</td>
  390. * </tr>
  391. * </table>
  392. * >]
  393. * runaway3 -> program3:f0;
  394. * label="3. Run-away code is caught when locking\nlocked peripheral. A CPU exception is executed."
  395. * }
  396. * subgraph cluster_away4 {
  397. * rankdir=TB;
  398. * runaway4 [label="Run-away code", shape=box];
  399. * color=white;
  400. * node [shape=plaintext];
  401. * program4 [label=<
  402. * <table>
  403. * <tr>
  404. * <td>PC#</td>
  405. * <td>Code</td>
  406. * </tr>
  407. * <tr>
  408. * <td>0x0020</td>
  409. * <td>initialize peripheral</td>
  410. * </tr>
  411. * <tr>
  412. * <td>0x0025</td>
  413. * <td>lock peripheral</td>
  414. * </tr>
  415. * <tr>
  416. * <td port="f0" BGCOLOR="green">...</td>
  417. * <td BGCOLOR="green">...</td>
  418. * </tr>
  419. * <tr>
  420. * <td BGCOLOR="green">0x0080</td>
  421. * <td BGCOLOR="green">set sanity argument</td>
  422. * </tr>
  423. * <tr>
  424. * <td BGCOLOR="green">...</td>
  425. * <td BGCOLOR="green">...</td>
  426. * </tr>
  427. * <tr>
  428. * <td BGCOLOR="green">0x0115</td>
  429. * <td BGCOLOR="green">disable interrupts</td>
  430. * </tr>
  431. * <tr>
  432. * <td BGCOLOR="green">0x0120</td>
  433. * <td BGCOLOR="green">unlock peripheral</td>
  434. * </tr>
  435. * <tr>
  436. * <td BGCOLOR="green">0x0125</td>
  437. * <td BGCOLOR="green">check sanity argument</td>
  438. * </tr>
  439. * <tr>
  440. * <td BGCOLOR="green">0x0130</td>
  441. * <td BGCOLOR="green">modify peripheral</td>
  442. * </tr>
  443. * <tr>
  444. * <td BGCOLOR="green">0x0140</td>
  445. * <td BGCOLOR="green">lock peripheral</td>
  446. * </tr>
  447. * <tr>
  448. * <td BGCOLOR="green">0x0145</td>
  449. * <td BGCOLOR="green">disable interrupts</td>
  450. * </tr>
  451. * </table>
  452. * >]
  453. * runaway4 -> program4:f0;
  454. * label="4. Run-away code is not caught.\n "
  455. * }
  456. * }
  457. * \enddot
  458. *
  459. * In the example, green indicates that the command is allowed, red indicates
  460. * where the runaway code will be caught, and the arrow where the runaway
  461. * code enters the application. In special circumstances, like example 4
  462. * above, the runaway code will not be caught. However, the protection scheme
  463. * will greatly enhance peripheral configuration security from being affected by
  464. * runaway code.
  465. *
  466. * \subsubsection asfdoc_sam0_pac_bitwise_code Key-Argument
  467. * To protect the module functions against runaway code themselves, a key
  468. * is required as one of the input arguments. The key-argument will make sure
  469. * that runaway code entering the function without a function call will be
  470. * rejected before inflicting any damage. The argument is simply set to be
  471. * the bitwise inverse of the module flag, i.e.
  472. *
  473. * \code
  474. system_peripheral_<lock_state>(SYSTEM_PERIPHERAL_<module>,
  475. ~SYSTEM_PERIPHERAL_<module>);
  476. \endcode
  477. *
  478. * Where the lock state can be either lock or unlock, and module refer to the
  479. * peripheral that is to be locked/unlocked.
  480. *
  481. * \subsection asfdoc_sam0_pac_module_pointer Faulty Module Pointer
  482. * The PAC also protects the application from user errors such as the use of
  483. * incorrect module pointers in function arguments, given that the module is
  484. * locked. It is therefore recommended that any unused peripheral is locked
  485. * during application initialization.
  486. *
  487. * \subsection asfdoc_sam0_pac_no_inline Use of __no_inline
  488. * Using the function attribute \c __no_inline will ensure that there will only be
  489. * one copy of each functions in the PAC driver API in the application. This will
  490. * lower the likelihood that runaway code will hit any of these functions.
  491. *
  492. * \subsection asfdoc_sam0_pac_module_overview_physical Physical Connection
  493. *
  494. * \ref asfdoc_sam0_pac_int_connections "The diagram below" shows how this
  495. * module is interconnected within the device.
  496. *
  497. * \anchor asfdoc_sam0_pac_int_connections
  498. * \dot
  499. * digraph overview {
  500. * nodesep = .05;
  501. * rankdir=LR;
  502. *
  503. * ahb [label="Peripheral bus", shape=ellipse, style=filled, fillcolor=lightgray];
  504. * pac [label="<f0>PAC|<f1>Lock|<f2>Open|<f3>Open",
  505. * height=2.5, shape=record, width=.1];
  506. * per1 [label="Peripheral1", shape=ellipse, style=filled, fillcolor=lightgray];
  507. * per2 [label="Peripheral2", shape=ellipse, style=filled, fillcolor=lightgray];
  508. * per3 [label="Peripheral3", shape=ellipse, style=filled, fillcolor=lightgray];
  509. * edge [dir="both"];
  510. * ahb -> pac:f1 [label="Read/Write"];
  511. * ahb -> pac:f2 [label="Read/Write"];
  512. * ahb -> pac:f3 [label="Read/Write"];
  513. * edge [dir="back"];
  514. * pac:f1 -> per1 [label="Read"];
  515. * edge [dir="both"];
  516. * pac:f2 -> per2 [label="Read/Write"];
  517. * pac:f3 -> per3 [label="Read/Write"];
  518. * {rank=same; per1 per2 per3 }
  519. * }
  520. * \enddot
  521. *
  522. *
  523. * \section asfdoc_sam0_pac_special_considerations Special Considerations
  524. *
  525. * \subsection asfdoc_sam0_pac_non_write_protected Non-Writable Registers
  526. * Not all registers in a given peripheral can be set non-writable. Which
  527. * registers this applies to is showed in \ref asfdoc_sam0_pac_non_write_list
  528. * and the peripheral's subsection "Register Access Protection" in the device
  529. * datasheet.
  530. *
  531. * \subsection asfdoc_sam0_pac_check_lock Reading Lock State
  532. * Reading the state of the peripheral lock is to be avoided as it greatly
  533. * compromises the protection initially provided by the PAC. If a lock/unlock
  534. * is implemented conditionally, there is a risk that eventual errors are not
  535. * caught in the protection scheme. Examples indicating the issue are shown
  536. * in \ref asfdoc_sam0_pac_lock_errors_diagram "the diagram below".
  537. *
  538. * \anchor asfdoc_sam0_pac_lock_errors_diagram
  539. * \dot
  540. * digraph read_lock {
  541. * subgraph cluster_read1{
  542. * rankdir=TB;
  543. * color=white;
  544. * runaway1 [label="Run-away code\nwith peripheral unlocked", shape=box];
  545. * node [shape=plaintext];
  546. * program1 [label=<
  547. * <table>
  548. * <tr>
  549. * <td>PC#</td>
  550. * <td>Code</td>
  551. * </tr>
  552. * <tr>
  553. * <td port="f0" BGCOLOR="green">...</td>
  554. * <td BGCOLOR="green">...</td>
  555. * </tr>
  556. * <tr>
  557. * <td BGCOLOR="green">0x0100</td>
  558. * <td BGCOLOR="green">check if locked</td>
  559. * </tr>
  560. * <tr>
  561. * <td BGCOLOR="green">0x0102</td>
  562. * <td BGCOLOR="green">disable interrupts</td>
  563. * </tr>
  564. * <tr>
  565. * <td BGCOLOR="green">0x0105</td>
  566. * <td BGCOLOR="green">unlock if locked</td>
  567. * </tr>
  568. * <tr>
  569. * <td BGCOLOR="green">0x0110</td>
  570. * <td BGCOLOR="green">check sanity</td>
  571. * </tr>
  572. * <tr>
  573. * <td BGCOLOR="green">0x0115</td>
  574. * <td BGCOLOR="green">modify peripheral</td>
  575. * </tr>
  576. * <tr>
  577. * <td BGCOLOR="green">0x0120</td>
  578. * <td BGCOLOR="green">lock if previously locked</td>
  579. * </tr>
  580. * <tr>
  581. * <td BGCOLOR="green">0x0125</td>
  582. * <td BGCOLOR="green">enable interrupts</td>
  583. * </tr>
  584. * </table>
  585. * >]
  586. * runaway1 -> program1:f0;
  587. * label="1. Wrong implementation.\n "
  588. * }
  589. * subgraph cluster_read2{
  590. * rankdir=TB;
  591. * color=white;
  592. * runaway2 [label="Run-away code\nwith peripheral unlocked", shape=box];
  593. * node [shape=plaintext];
  594. * program2 [label=<
  595. * <table>
  596. * <tr>
  597. * <td>PC#</td>
  598. * <td>Code</td>
  599. * </tr>
  600. * <tr>
  601. * <td port="f0" BGCOLOR="green">...</td>
  602. * <td BGCOLOR="green">...</td>
  603. * </tr>
  604. * <tr>
  605. * <td BGCOLOR="green">0x0100</td>
  606. * <td BGCOLOR="green">disable interrupts</td>
  607. * </tr>
  608. * <tr>
  609. * <td BGCOLOR="red">0x0120</td>
  610. * <td BGCOLOR="red">unlock peripheral</td>
  611. * </tr>
  612. * <tr>
  613. * <td>0x0125</td>
  614. * <td>check sanity argument</td>
  615. * </tr>
  616. * <tr>
  617. * <td>0x0130</td>
  618. * <td>modify peripheral</td>
  619. * </tr>
  620. * <tr>
  621. * <td>0x0140</td>
  622. * <td>lock peripheral</td>
  623. * </tr>
  624. * <tr>
  625. * <td>0x0145</td>
  626. * <td>disable interrupts</td>
  627. * </tr>
  628. * </table>
  629. * >]
  630. * runaway2 -> program2:f0;
  631. *
  632. * label="2. Correct implementation.\n "
  633. * }
  634. * }
  635. * \enddot
  636. *
  637. * In the left figure above, one can see the runaway code continues as all
  638. * illegal operations are conditional. On the right side figure, the runaway
  639. * code is caught as it tries to unlock the peripheral.
  640. *
  641. * \section asfdoc_sam0_pac_extra_info Extra Information
  642. *
  643. * For extra information, see \ref asfdoc_sam0_pac_extra. This includes:
  644. * - \ref asfdoc_sam0_pac_extra_acronyms
  645. * - \ref asfdoc_sam0_pac_extra_dependencies
  646. * - \ref asfdoc_sam0_pac_extra_errata
  647. * - \ref asfdoc_sam0_pac_extra_history
  648. *
  649. *
  650. * \section asfdoc_sam0_pac_examples Examples
  651. *
  652. * For a list of examples related to this driver, see
  653. * \ref asfdoc_sam0_pac_exqsg.
  654. *
  655. *
  656. * \section asfdoc_sam0_pac_api_overview API Overview
  657. * @{
  658. */
  659. #include <compiler.h>
  660. #include <system.h>
  661. #ifdef __cplusplus
  662. extern "C" {
  663. #endif
  664. /**
  665. * Retrieves the ID of a specified peripheral name, giving its peripheral bus
  666. * location.
  667. *
  668. * \param[in] peripheral Name of the peripheral instance
  669. *
  670. * \returns Bus ID of the specified peripheral instance.
  671. */
  672. #define SYSTEM_PERIPHERAL_ID(peripheral) ID_##peripheral
  673. /** \name Peripheral Lock and Unlock
  674. * @{
  675. */
  676. __no_inline enum status_code system_peripheral_lock(
  677. const uint32_t peripheral_id,
  678. const uint32_t key);
  679. __no_inline enum status_code system_peripheral_unlock(
  680. const uint32_t peripheral_id,
  681. const uint32_t key);
  682. /** @} */
  683. #if (SAML21) || (SAML22) || (SAMC20) || (SAMC21) || (SAMR30) || defined(__DOXYGEN__)
  684. /** \name APIs available for SAM L21/L22/C20/C21.
  685. * @{
  686. */
  687. __no_inline enum status_code system_peripheral_lock_always(
  688. const uint32_t peripheral_id,
  689. const uint32_t key);
  690. /**
  691. * \brief Enable PAC interrupt.
  692. *
  693. * Enable PAC interrupt so can trigger execution on peripheral access error,
  694. * see \ref SYSTEM_Handler().
  695. *
  696. */
  697. static inline void system_pac_enable_interrupt(void)
  698. {
  699. PAC->INTENSET.reg = PAC_INTENSET_ERR;
  700. }
  701. /**
  702. * \brief Disable PAC interrupt.
  703. *
  704. * Disable PAC interrupt on peripheral access error.
  705. *
  706. */
  707. static inline void system_pac_disable_interrupt(void)
  708. {
  709. PAC->INTENCLR.reg = PAC_INTENCLR_ERR;
  710. }
  711. /**
  712. * \brief Enable PAC event output.
  713. *
  714. * Enable PAC event output on peripheral access error.
  715. *
  716. */
  717. static inline void system_pac_enable_event(void)
  718. {
  719. PAC->EVCTRL.reg = PAC_EVCTRL_ERREO;
  720. }
  721. /**
  722. * \brief Disable PAC event output.
  723. *
  724. * Disable PAC event output on peripheral access error.
  725. *
  726. */
  727. static inline void system_pac_disable_event(void)
  728. {
  729. PAC->EVCTRL.reg &= (~PAC_EVCTRL_ERREO);
  730. }
  731. /** @} */
  732. #endif
  733. #ifdef __cplusplus
  734. }
  735. #endif
  736. /** @} */
  737. /**
  738. * \page asfdoc_sam0_pac_extra Extra Information for PAC Driver
  739. *
  740. * \section asfdoc_sam0_pac_extra_acronyms Acronyms
  741. * Below is a table listing the acronyms used in this module, along with their
  742. * intended meanings.
  743. *
  744. * <table>
  745. * <tr>
  746. * <th>Acronym</td>
  747. * <th>Description</td>
  748. * </tr>
  749. * <tr>
  750. * <td>AC</td>
  751. * <td>Analog Comparator</td>
  752. * </tr>
  753. * <tr>
  754. * <td>ADC</td>
  755. * <td>Analog-to-Digital Converter</td>
  756. * </tr>
  757. * <tr>
  758. * <td>EVSYS</td>
  759. * <td>Event System</td>
  760. * </tr>
  761. * <tr>
  762. * <td>NMI</td>
  763. * <td>Non-Maskable Interrupt</td>
  764. * </tr>
  765. * <tr>
  766. * <td>NVMCTRL</td>
  767. * <td>Non-Volatile Memory Controller</td>
  768. * </tr>
  769. * <tr>
  770. * <td>PAC</td>
  771. * <td>Peripheral Access Controller</td>
  772. * </tr>
  773. * <tr>
  774. * <td>PM</td>
  775. * <td>Power Manager</td>
  776. * </tr>
  777. * <tr>
  778. * <td>RTC</td>
  779. * <td>Real-Time Counter</td>
  780. * </tr>
  781. * <tr>
  782. * <td>SERCOM</td>
  783. * <td>Serial Communication Interface</td>
  784. * </tr>
  785. * <tr>
  786. * <td>SYSCTRL</td>
  787. * <td>System Controller</td>
  788. * </tr>
  789. * <tr>
  790. * <td>TC</td>
  791. * <td>Timer/Counter</td>
  792. * </tr>
  793. * <tr>
  794. * <td>WDT</td>
  795. * <td>Watch Dog Timer</td>
  796. * </tr>
  797. * </table>
  798. *
  799. *
  800. * \section asfdoc_sam0_pac_extra_dependencies Dependencies
  801. * This driver has the following dependencies:
  802. *
  803. * - None
  804. *
  805. *
  806. * \section asfdoc_sam0_pac_extra_errata Errata
  807. * There are no errata related to this driver.
  808. *
  809. *
  810. * \section asfdoc_sam0_pac_extra_history Module History
  811. * An overview of the module history is presented in the table below, with
  812. * details on the enhancements and fixes made to the module since its first
  813. * release. The current version of this corresponds to the newest version in
  814. * the table.
  815. *
  816. * <table>
  817. * <tr>
  818. * <th>Changelog</th>
  819. * </tr>
  820. * <tr>
  821. * <td>Initial Release</td>
  822. * </tr>
  823. * </table>
  824. */
  825. /**
  826. * \page asfdoc_sam0_pac_exqsg Examples for PAC Driver
  827. *
  828. * This is a list of the available Quick Start guides (QSGs) and example
  829. * applications for \ref asfdoc_sam0_pac_group. QSGs are simple examples with
  830. * step-by-step instructions to configure and use this driver in a selection of
  831. * use cases. Note that a QSG can be compiled as a standalone application or be
  832. * added to the user application.
  833. *
  834. * - \subpage asfdoc_sam0_pac_basic_use_case
  835. */
  836. /**
  837. * \page asfdoc_sam0_pac_non_write_list List of Non-Write Protected Registers
  838. *
  839. * Look in device datasheet peripheral's subsection "Register Access
  840. * Protection" to see which is actually available for your device.
  841. * <table>
  842. * <tr>
  843. * <th>Module</th>
  844. * <th>Non-write protected register</th>
  845. * </tr>
  846. * <tr>
  847. * <td>AC</td>
  848. * <td>INTFLAG</td>
  849. * </tr>
  850. * <tr>
  851. * <td></td>
  852. * <td>STATUSA</td>
  853. * </tr>
  854. * <tr>
  855. * <td></td>
  856. * <td>STATUSB</td>
  857. * </tr>
  858. * <tr>
  859. * <td></td>
  860. * <td>STATUSC</td>
  861. * </tr>
  862. * <tr><td colspan="2"/></tr>
  863. * <tr>
  864. * <td>ADC</td>
  865. * <td>INTFLAG</td>
  866. * </tr>
  867. * <tr>
  868. * <td></td>
  869. * <td>STATUS</td>
  870. * </tr>
  871. * <tr>
  872. * <td></td>
  873. * <td>RESULT</td>
  874. * </tr>
  875. * <tr><td colspan="2"/></tr>
  876. * <tr>
  877. * <td>EVSYS</td>
  878. * <td>INTFLAG</td>
  879. * </tr>
  880. * <tr>
  881. * <td></td>
  882. * <td>CHSTATUS</td>
  883. * </tr>
  884. * <tr><td colspan="2"/></tr>
  885. * <tr>
  886. * <td>NVMCTRL</td>
  887. * <td>INTFLAG</td>
  888. * </tr>
  889. * <tr>
  890. * <td></td>
  891. * <td>STATUS</td>
  892. * </tr>
  893. * <tr><td colspan="2"/></tr>
  894. * <tr>
  895. * <td>PM</td>
  896. * <td>INTFLAG</td>
  897. * </tr>
  898. * <tr><td colspan="2"/></tr>
  899. * <tr>
  900. * <td>PORT</td>
  901. * <td>N/A</td>
  902. * </tr>
  903. * <tr><td colspan="2"/></tr>
  904. * <tr>
  905. * <td>RTC</td>
  906. * <td>INTFLAG</td>
  907. * </tr>
  908. * <tr>
  909. * <td></td>
  910. * <td>READREQ</td>
  911. * </tr>
  912. * <tr>
  913. * <td></td>
  914. * <td>STATUS</td>
  915. * </tr>
  916. * <tr><td colspan="2"/></tr>
  917. * <tr>
  918. * <td>SYSCTRL</td>
  919. * <td>INTFLAG</td>
  920. * </tr>
  921. * <tr><td colspan="2"/></tr>
  922. * <tr>
  923. * <td>SERCOM</td>
  924. * <td>INTFALG</td>
  925. * </tr>
  926. * <tr>
  927. * <td></td>
  928. * <td>STATUS</td>
  929. * </tr>
  930. * <tr>
  931. * <td></td>
  932. * <td>DATA</td>
  933. * </tr>
  934. * <tr><td colspan="2"/></tr>
  935. * <tr>
  936. * <td>TC</td>
  937. * <td>INTFLAG</td>
  938. * </tr>
  939. * <tr>
  940. * <td></td>
  941. * <td>STATUS</td>
  942. * </tr>
  943. * <tr><td colspan="2"/></tr>
  944. * <tr>
  945. * <td>WDT</td>
  946. * <td>INTFLAG</td>
  947. * </tr>
  948. * <tr>
  949. * <td></td>
  950. * <td>STATUS</td>
  951. * </tr>
  952. * <tr>
  953. * <td></td>
  954. * <td>(CLEAR)</td>
  955. * </tr>
  956. * </table>
  957. *
  958. * \page asfdoc_sam0_pac_document_revision_history Document Revision History
  959. *
  960. * <table>
  961. * <tr>
  962. * <th>Doc. Rev.</td>
  963. * <th>Date</td>
  964. * <th>Comments</td>
  965. * </tr>
  966. * <tr>
  967. * <td>42107F</td>
  968. * <td>12/2015</td>
  969. * <td>Added support for SAM L21/L22, SAM C20/C21, SAM D09, and SAM DA1</td>
  970. * </tr>
  971. * <tr>
  972. * <td>42107E</td>
  973. * <td>12/2014</td>
  974. * <td>Added support for SAM R21 and SAM D10/D11</td>
  975. * </tr>
  976. * <tr>
  977. * <td>42107D</td>
  978. * <td>01/2014</td>
  979. * <td>Added support for SAM D21</td>
  980. * </tr>
  981. * <tr>
  982. * <td>42107C</td>
  983. * <td>10/2013</td>
  984. * <td>Extended acronyms list</td>
  985. * </tr>
  986. * <tr>
  987. * <td>42107B</td>
  988. * <td>06/2013</td>
  989. * <td>Corrected documentation typos</td>
  990. * </tr>
  991. * <tr>
  992. * <td>42107A</td>
  993. * <td>06/2013</td>
  994. * <td>Initial document release</td>
  995. * </tr>
  996. * </table>
  997. */
  998. #endif /* PAC_H_INCLUDED */