Root/
| 1 | #pypp 0 |
| 2 | // Iris: micro-kernel for a capability-based operating system. |
| 3 | // boot-programs/buzzer.ccp: Piezo buzzer driver. |
| 4 | // Copyright 2009 Bas Wijnen <wijnen@debian.org> |
| 5 | // |
| 6 | // This program is free software: you can redistribute it and/or modify |
| 7 | // it under the terms of the GNU General Public License as published by |
| 8 | // the Free Software Foundation, either version 3 of the License, or |
| 9 | // (at your option) any later version. |
| 10 | // |
| 11 | // This program is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | // |
| 16 | // You should have received a copy of the GNU General Public License |
| 17 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | |
| 19 | #include "devices.hh" |
| 20 | #define ARCH |
| 21 | #include "arch.hh" |
| 22 | |
| 23 | class DevBuzzer: |
| 24 | static unsigned const pwm = 4 |
| 25 | Iris::Cap event |
| 26 | bool is_beeping |
| 27 | public: |
| 28 | DevBuzzer (): |
| 29 | is_beeping = false |
| 30 | tcu_stop_counter (pwm) |
| 31 | tcu_select_extalclk (pwm) |
| 32 | tcu_select_clk_div64 (pwm) |
| 33 | tcu_enable_pwm_output (pwm) |
| 34 | void stop (): |
| 35 | if !is_beeping: |
| 36 | return |
| 37 | tcu_stop_counter (pwm) |
| 38 | event.invoke () |
| 39 | Iris::free_cap (event) |
| 40 | is_beeping = false |
| 41 | void beep (unsigned freq, unsigned ms, Iris::Cap cb): |
| 42 | stop () |
| 43 | event = cb |
| 44 | unsigned full = JZ_EXTAL / 64 / freq |
| 45 | tcu_set_full_data (pwm, full) |
| 46 | tcu_set_half_data (pwm, full / 2) |
| 47 | tcu_set_count (pwm, 0) |
| 48 | tcu_start_counter (pwm) |
| 49 | Iris::my_receiver.set_alarm (ms * HZ / 1000) |
| 50 | is_beeping = true |
| 51 | |
| 52 | enum codes: |
| 53 | BUZZER = 32 |
| 54 | |
| 55 | Iris::Num start (): |
| 56 | map_tcu () |
| 57 | |
| 58 | DevBuzzer buzzer |
| 59 | |
| 60 | Iris::Buzzer dev = Iris::my_receiver.create_capability (BUZZER) |
| 61 | Iris::my_parent.provide_capability <Iris::Buzzer> (dev.copy ()) |
| 62 | Iris::free_cap (dev) |
| 63 | Iris::my_parent.init_done () |
| 64 | while true: |
| 65 | Iris::wait () |
| 66 | if Iris::recv.protected_data.h == ~0: |
| 67 | // Alarm. |
| 68 | buzzer.stop () |
| 69 | continue |
| 70 | switch Iris::recv.protected_data.l: |
| 71 | case BUZZER: |
| 72 | // Buzzer device user request. |
| 73 | switch Iris::recv.data[0].l: |
| 74 | case Iris::Device::RESET: |
| 75 | buzzer.stop () |
| 76 | Iris::recv.reply.invoke () |
| 77 | break |
| 78 | case Iris::Buzzer::BEEP: |
| 79 | // Volume is not used by this buzzer. |
| 80 | Iris::Cap arg = Iris::get_arg () |
| 81 | Iris::Cap reply = Iris::get_reply () |
| 82 | buzzer.beep (Iris::recv.data[1].l, Iris::recv.data[1].h, arg) |
| 83 | reply.invoke () |
| 84 | Iris::free_cap (reply) |
| 85 | break |
| 86 | case Iris::Buzzer::STOP: |
| 87 | buzzer.stop () |
| 88 | Iris::recv.reply.invoke () |
| 89 | break |
| 90 | default: |
| 91 | kdebug ("Buzzer: other\n") |
| 92 | break |
| 93 | break |
| 94 | default: |
| 95 | kdebug ("Buzzer: unknown num: ") |
| 96 | kdebug_num (Iris::recv.protected_data.l) |
| 97 | kdebug ("\n") |
| 98 |
Branches:
master
