├── .githooks
└── pre-commit
├── .gitignore
├── .gitmodules
├── LICENSE.txt
├── README.md
├── TODO.txt
├── build.zig
├── fe310_g000.ld
├── flash
├── openocd.cfg
├── slipmux
├── coap.go
├── dispatcher.go
├── go.mod
├── go.sum
├── main.go
├── serial.go
└── slow_writer.go
└── src
├── clock.c
├── console.zig
├── crc.zig
├── gpio.zig
├── init.zig
├── irq.S
├── main.zig
├── periph.zig
├── plic.zig
├── slipmux.zig
├── start.S
└── uart.zig
/.githooks/pre-commit:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 |
3 | # XXX: Unfourtunatly, it's difficult to differentiate between a
4 | # `zig fmt` usage error and an "found unformated file"-error.
5 | # To workaround this, we only check the output not the exit code.
6 | files=$(git diff-index --name-only --cached HEAD -- '*.zig' | \
7 | xargs zig fmt --check 2>&1 | \
8 | grep -v "expected at least one source file argument")
9 |
10 | if [ -n "$files" ]; then
11 | printf "The following files need to be formated with 'zig fmt':\n\n" 1>&2
12 | printf "%s\n" "$files" | sed 's/^/\t/g' 1>&2
13 | exit 1
14 | fi
15 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | zig-cache
2 | zig-out
3 | slipmux/slipmux
4 |
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "zoap"]
2 | path = zoap
3 | url = https://git.8pit.net/zoap.git
4 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | GNU AFFERO GENERAL PUBLIC LICENSE
2 | Version 3, 19 November 2007
3 |
4 | Copyright (C) 2007 Free Software Foundation, Inc.
5 | Everyone is permitted to copy and distribute verbatim copies
6 | of this license document, but changing it is not allowed.
7 |
8 | Preamble
9 |
10 | The GNU Affero General Public License is a free, copyleft license for
11 | software and other kinds of works, specifically designed to ensure
12 | cooperation with the community in the case of network server software.
13 |
14 | The licenses for most software and other practical works are designed
15 | to take away your freedom to share and change the works. By contrast,
16 | our General Public Licenses are intended to guarantee your freedom to
17 | share and change all versions of a program--to make sure it remains free
18 | software for all its users.
19 |
20 | When we speak of free software, we are referring to freedom, not
21 | price. Our General Public Licenses are designed to make sure that you
22 | have the freedom to distribute copies of free software (and charge for
23 | them if you wish), that you receive source code or can get it if you
24 | want it, that you can change the software or use pieces of it in new
25 | free programs, and that you know you can do these things.
26 |
27 | Developers that use our General Public Licenses protect your rights
28 | with two steps: (1) assert copyright on the software, and (2) offer
29 | you this License which gives you legal permission to copy, distribute
30 | and/or modify the software.
31 |
32 | A secondary benefit of defending all users' freedom is that
33 | improvements made in alternate versions of the program, if they
34 | receive widespread use, become available for other developers to
35 | incorporate. Many developers of free software are heartened and
36 | encouraged by the resulting cooperation. However, in the case of
37 | software used on network servers, this result may fail to come about.
38 | The GNU General Public License permits making a modified version and
39 | letting the public access it on a server without ever releasing its
40 | source code to the public.
41 |
42 | The GNU Affero General Public License is designed specifically to
43 | ensure that, in such cases, the modified source code becomes available
44 | to the community. It requires the operator of a network server to
45 | provide the source code of the modified version running there to the
46 | users of that server. Therefore, public use of a modified version, on
47 | a publicly accessible server, gives the public access to the source
48 | code of the modified version.
49 |
50 | An older license, called the Affero General Public License and
51 | published by Affero, was designed to accomplish similar goals. This is
52 | a different license, not a version of the Affero GPL, but Affero has
53 | released a new version of the Affero GPL which permits relicensing under
54 | this license.
55 |
56 | The precise terms and conditions for copying, distribution and
57 | modification follow.
58 |
59 | TERMS AND CONDITIONS
60 |
61 | 0. Definitions.
62 |
63 | "This License" refers to version 3 of the GNU Affero General Public License.
64 |
65 | "Copyright" also means copyright-like laws that apply to other kinds of
66 | works, such as semiconductor masks.
67 |
68 | "The Program" refers to any copyrightable work licensed under this
69 | License. Each licensee is addressed as "you". "Licensees" and
70 | "recipients" may be individuals or organizations.
71 |
72 | To "modify" a work means to copy from or adapt all or part of the work
73 | in a fashion requiring copyright permission, other than the making of an
74 | exact copy. The resulting work is called a "modified version" of the
75 | earlier work or a work "based on" the earlier work.
76 |
77 | A "covered work" means either the unmodified Program or a work based
78 | on the Program.
79 |
80 | To "propagate" a work means to do anything with it that, without
81 | permission, would make you directly or secondarily liable for
82 | infringement under applicable copyright law, except executing it on a
83 | computer or modifying a private copy. Propagation includes copying,
84 | distribution (with or without modification), making available to the
85 | public, and in some countries other activities as well.
86 |
87 | To "convey" a work means any kind of propagation that enables other
88 | parties to make or receive copies. Mere interaction with a user through
89 | a computer network, with no transfer of a copy, is not conveying.
90 |
91 | An interactive user interface displays "Appropriate Legal Notices"
92 | to the extent that it includes a convenient and prominently visible
93 | feature that (1) displays an appropriate copyright notice, and (2)
94 | tells the user that there is no warranty for the work (except to the
95 | extent that warranties are provided), that licensees may convey the
96 | work under this License, and how to view a copy of this License. If
97 | the interface presents a list of user commands or options, such as a
98 | menu, a prominent item in the list meets this criterion.
99 |
100 | 1. Source Code.
101 |
102 | The "source code" for a work means the preferred form of the work
103 | for making modifications to it. "Object code" means any non-source
104 | form of a work.
105 |
106 | A "Standard Interface" means an interface that either is an official
107 | standard defined by a recognized standards body, or, in the case of
108 | interfaces specified for a particular programming language, one that
109 | is widely used among developers working in that language.
110 |
111 | The "System Libraries" of an executable work include anything, other
112 | than the work as a whole, that (a) is included in the normal form of
113 | packaging a Major Component, but which is not part of that Major
114 | Component, and (b) serves only to enable use of the work with that
115 | Major Component, or to implement a Standard Interface for which an
116 | implementation is available to the public in source code form. A
117 | "Major Component", in this context, means a major essential component
118 | (kernel, window system, and so on) of the specific operating system
119 | (if any) on which the executable work runs, or a compiler used to
120 | produce the work, or an object code interpreter used to run it.
121 |
122 | The "Corresponding Source" for a work in object code form means all
123 | the source code needed to generate, install, and (for an executable
124 | work) run the object code and to modify the work, including scripts to
125 | control those activities. However, it does not include the work's
126 | System Libraries, or general-purpose tools or generally available free
127 | programs which are used unmodified in performing those activities but
128 | which are not part of the work. For example, Corresponding Source
129 | includes interface definition files associated with source files for
130 | the work, and the source code for shared libraries and dynamically
131 | linked subprograms that the work is specifically designed to require,
132 | such as by intimate data communication or control flow between those
133 | subprograms and other parts of the work.
134 |
135 | The Corresponding Source need not include anything that users
136 | can regenerate automatically from other parts of the Corresponding
137 | Source.
138 |
139 | The Corresponding Source for a work in source code form is that
140 | same work.
141 |
142 | 2. Basic Permissions.
143 |
144 | All rights granted under this License are granted for the term of
145 | copyright on the Program, and are irrevocable provided the stated
146 | conditions are met. This License explicitly affirms your unlimited
147 | permission to run the unmodified Program. The output from running a
148 | covered work is covered by this License only if the output, given its
149 | content, constitutes a covered work. This License acknowledges your
150 | rights of fair use or other equivalent, as provided by copyright law.
151 |
152 | You may make, run and propagate covered works that you do not
153 | convey, without conditions so long as your license otherwise remains
154 | in force. You may convey covered works to others for the sole purpose
155 | of having them make modifications exclusively for you, or provide you
156 | with facilities for running those works, provided that you comply with
157 | the terms of this License in conveying all material for which you do
158 | not control copyright. Those thus making or running the covered works
159 | for you must do so exclusively on your behalf, under your direction
160 | and control, on terms that prohibit them from making any copies of
161 | your copyrighted material outside their relationship with you.
162 |
163 | Conveying under any other circumstances is permitted solely under
164 | the conditions stated below. Sublicensing is not allowed; section 10
165 | makes it unnecessary.
166 |
167 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
168 |
169 | No covered work shall be deemed part of an effective technological
170 | measure under any applicable law fulfilling obligations under article
171 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or
172 | similar laws prohibiting or restricting circumvention of such
173 | measures.
174 |
175 | When you convey a covered work, you waive any legal power to forbid
176 | circumvention of technological measures to the extent such circumvention
177 | is effected by exercising rights under this License with respect to
178 | the covered work, and you disclaim any intention to limit operation or
179 | modification of the work as a means of enforcing, against the work's
180 | users, your or third parties' legal rights to forbid circumvention of
181 | technological measures.
182 |
183 | 4. Conveying Verbatim Copies.
184 |
185 | You may convey verbatim copies of the Program's source code as you
186 | receive it, in any medium, provided that you conspicuously and
187 | appropriately publish on each copy an appropriate copyright notice;
188 | keep intact all notices stating that this License and any
189 | non-permissive terms added in accord with section 7 apply to the code;
190 | keep intact all notices of the absence of any warranty; and give all
191 | recipients a copy of this License along with the Program.
192 |
193 | You may charge any price or no price for each copy that you convey,
194 | and you may offer support or warranty protection for a fee.
195 |
196 | 5. Conveying Modified Source Versions.
197 |
198 | You may convey a work based on the Program, or the modifications to
199 | produce it from the Program, in the form of source code under the
200 | terms of section 4, provided that you also meet all of these conditions:
201 |
202 | a) The work must carry prominent notices stating that you modified
203 | it, and giving a relevant date.
204 |
205 | b) The work must carry prominent notices stating that it is
206 | released under this License and any conditions added under section
207 | 7. This requirement modifies the requirement in section 4 to
208 | "keep intact all notices".
209 |
210 | c) You must license the entire work, as a whole, under this
211 | License to anyone who comes into possession of a copy. This
212 | License will therefore apply, along with any applicable section 7
213 | additional terms, to the whole of the work, and all its parts,
214 | regardless of how they are packaged. This License gives no
215 | permission to license the work in any other way, but it does not
216 | invalidate such permission if you have separately received it.
217 |
218 | d) If the work has interactive user interfaces, each must display
219 | Appropriate Legal Notices; however, if the Program has interactive
220 | interfaces that do not display Appropriate Legal Notices, your
221 | work need not make them do so.
222 |
223 | A compilation of a covered work with other separate and independent
224 | works, which are not by their nature extensions of the covered work,
225 | and which are not combined with it such as to form a larger program,
226 | in or on a volume of a storage or distribution medium, is called an
227 | "aggregate" if the compilation and its resulting copyright are not
228 | used to limit the access or legal rights of the compilation's users
229 | beyond what the individual works permit. Inclusion of a covered work
230 | in an aggregate does not cause this License to apply to the other
231 | parts of the aggregate.
232 |
233 | 6. Conveying Non-Source Forms.
234 |
235 | You may convey a covered work in object code form under the terms
236 | of sections 4 and 5, provided that you also convey the
237 | machine-readable Corresponding Source under the terms of this License,
238 | in one of these ways:
239 |
240 | a) Convey the object code in, or embodied in, a physical product
241 | (including a physical distribution medium), accompanied by the
242 | Corresponding Source fixed on a durable physical medium
243 | customarily used for software interchange.
244 |
245 | b) Convey the object code in, or embodied in, a physical product
246 | (including a physical distribution medium), accompanied by a
247 | written offer, valid for at least three years and valid for as
248 | long as you offer spare parts or customer support for that product
249 | model, to give anyone who possesses the object code either (1) a
250 | copy of the Corresponding Source for all the software in the
251 | product that is covered by this License, on a durable physical
252 | medium customarily used for software interchange, for a price no
253 | more than your reasonable cost of physically performing this
254 | conveying of source, or (2) access to copy the
255 | Corresponding Source from a network server at no charge.
256 |
257 | c) Convey individual copies of the object code with a copy of the
258 | written offer to provide the Corresponding Source. This
259 | alternative is allowed only occasionally and noncommercially, and
260 | only if you received the object code with such an offer, in accord
261 | with subsection 6b.
262 |
263 | d) Convey the object code by offering access from a designated
264 | place (gratis or for a charge), and offer equivalent access to the
265 | Corresponding Source in the same way through the same place at no
266 | further charge. You need not require recipients to copy the
267 | Corresponding Source along with the object code. If the place to
268 | copy the object code is a network server, the Corresponding Source
269 | may be on a different server (operated by you or a third party)
270 | that supports equivalent copying facilities, provided you maintain
271 | clear directions next to the object code saying where to find the
272 | Corresponding Source. Regardless of what server hosts the
273 | Corresponding Source, you remain obligated to ensure that it is
274 | available for as long as needed to satisfy these requirements.
275 |
276 | e) Convey the object code using peer-to-peer transmission, provided
277 | you inform other peers where the object code and Corresponding
278 | Source of the work are being offered to the general public at no
279 | charge under subsection 6d.
280 |
281 | A separable portion of the object code, whose source code is excluded
282 | from the Corresponding Source as a System Library, need not be
283 | included in conveying the object code work.
284 |
285 | A "User Product" is either (1) a "consumer product", which means any
286 | tangible personal property which is normally used for personal, family,
287 | or household purposes, or (2) anything designed or sold for incorporation
288 | into a dwelling. In determining whether a product is a consumer product,
289 | doubtful cases shall be resolved in favor of coverage. For a particular
290 | product received by a particular user, "normally used" refers to a
291 | typical or common use of that class of product, regardless of the status
292 | of the particular user or of the way in which the particular user
293 | actually uses, or expects or is expected to use, the product. A product
294 | is a consumer product regardless of whether the product has substantial
295 | commercial, industrial or non-consumer uses, unless such uses represent
296 | the only significant mode of use of the product.
297 |
298 | "Installation Information" for a User Product means any methods,
299 | procedures, authorization keys, or other information required to install
300 | and execute modified versions of a covered work in that User Product from
301 | a modified version of its Corresponding Source. The information must
302 | suffice to ensure that the continued functioning of the modified object
303 | code is in no case prevented or interfered with solely because
304 | modification has been made.
305 |
306 | If you convey an object code work under this section in, or with, or
307 | specifically for use in, a User Product, and the conveying occurs as
308 | part of a transaction in which the right of possession and use of the
309 | User Product is transferred to the recipient in perpetuity or for a
310 | fixed term (regardless of how the transaction is characterized), the
311 | Corresponding Source conveyed under this section must be accompanied
312 | by the Installation Information. But this requirement does not apply
313 | if neither you nor any third party retains the ability to install
314 | modified object code on the User Product (for example, the work has
315 | been installed in ROM).
316 |
317 | The requirement to provide Installation Information does not include a
318 | requirement to continue to provide support service, warranty, or updates
319 | for a work that has been modified or installed by the recipient, or for
320 | the User Product in which it has been modified or installed. Access to a
321 | network may be denied when the modification itself materially and
322 | adversely affects the operation of the network or violates the rules and
323 | protocols for communication across the network.
324 |
325 | Corresponding Source conveyed, and Installation Information provided,
326 | in accord with this section must be in a format that is publicly
327 | documented (and with an implementation available to the public in
328 | source code form), and must require no special password or key for
329 | unpacking, reading or copying.
330 |
331 | 7. Additional Terms.
332 |
333 | "Additional permissions" are terms that supplement the terms of this
334 | License by making exceptions from one or more of its conditions.
335 | Additional permissions that are applicable to the entire Program shall
336 | be treated as though they were included in this License, to the extent
337 | that they are valid under applicable law. If additional permissions
338 | apply only to part of the Program, that part may be used separately
339 | under those permissions, but the entire Program remains governed by
340 | this License without regard to the additional permissions.
341 |
342 | When you convey a copy of a covered work, you may at your option
343 | remove any additional permissions from that copy, or from any part of
344 | it. (Additional permissions may be written to require their own
345 | removal in certain cases when you modify the work.) You may place
346 | additional permissions on material, added by you to a covered work,
347 | for which you have or can give appropriate copyright permission.
348 |
349 | Notwithstanding any other provision of this License, for material you
350 | add to a covered work, you may (if authorized by the copyright holders of
351 | that material) supplement the terms of this License with terms:
352 |
353 | a) Disclaiming warranty or limiting liability differently from the
354 | terms of sections 15 and 16 of this License; or
355 |
356 | b) Requiring preservation of specified reasonable legal notices or
357 | author attributions in that material or in the Appropriate Legal
358 | Notices displayed by works containing it; or
359 |
360 | c) Prohibiting misrepresentation of the origin of that material, or
361 | requiring that modified versions of such material be marked in
362 | reasonable ways as different from the original version; or
363 |
364 | d) Limiting the use for publicity purposes of names of licensors or
365 | authors of the material; or
366 |
367 | e) Declining to grant rights under trademark law for use of some
368 | trade names, trademarks, or service marks; or
369 |
370 | f) Requiring indemnification of licensors and authors of that
371 | material by anyone who conveys the material (or modified versions of
372 | it) with contractual assumptions of liability to the recipient, for
373 | any liability that these contractual assumptions directly impose on
374 | those licensors and authors.
375 |
376 | All other non-permissive additional terms are considered "further
377 | restrictions" within the meaning of section 10. If the Program as you
378 | received it, or any part of it, contains a notice stating that it is
379 | governed by this License along with a term that is a further
380 | restriction, you may remove that term. If a license document contains
381 | a further restriction but permits relicensing or conveying under this
382 | License, you may add to a covered work material governed by the terms
383 | of that license document, provided that the further restriction does
384 | not survive such relicensing or conveying.
385 |
386 | If you add terms to a covered work in accord with this section, you
387 | must place, in the relevant source files, a statement of the
388 | additional terms that apply to those files, or a notice indicating
389 | where to find the applicable terms.
390 |
391 | Additional terms, permissive or non-permissive, may be stated in the
392 | form of a separately written license, or stated as exceptions;
393 | the above requirements apply either way.
394 |
395 | 8. Termination.
396 |
397 | You may not propagate or modify a covered work except as expressly
398 | provided under this License. Any attempt otherwise to propagate or
399 | modify it is void, and will automatically terminate your rights under
400 | this License (including any patent licenses granted under the third
401 | paragraph of section 11).
402 |
403 | However, if you cease all violation of this License, then your
404 | license from a particular copyright holder is reinstated (a)
405 | provisionally, unless and until the copyright holder explicitly and
406 | finally terminates your license, and (b) permanently, if the copyright
407 | holder fails to notify you of the violation by some reasonable means
408 | prior to 60 days after the cessation.
409 |
410 | Moreover, your license from a particular copyright holder is
411 | reinstated permanently if the copyright holder notifies you of the
412 | violation by some reasonable means, this is the first time you have
413 | received notice of violation of this License (for any work) from that
414 | copyright holder, and you cure the violation prior to 30 days after
415 | your receipt of the notice.
416 |
417 | Termination of your rights under this section does not terminate the
418 | licenses of parties who have received copies or rights from you under
419 | this License. If your rights have been terminated and not permanently
420 | reinstated, you do not qualify to receive new licenses for the same
421 | material under section 10.
422 |
423 | 9. Acceptance Not Required for Having Copies.
424 |
425 | You are not required to accept this License in order to receive or
426 | run a copy of the Program. Ancillary propagation of a covered work
427 | occurring solely as a consequence of using peer-to-peer transmission
428 | to receive a copy likewise does not require acceptance. However,
429 | nothing other than this License grants you permission to propagate or
430 | modify any covered work. These actions infringe copyright if you do
431 | not accept this License. Therefore, by modifying or propagating a
432 | covered work, you indicate your acceptance of this License to do so.
433 |
434 | 10. Automatic Licensing of Downstream Recipients.
435 |
436 | Each time you convey a covered work, the recipient automatically
437 | receives a license from the original licensors, to run, modify and
438 | propagate that work, subject to this License. You are not responsible
439 | for enforcing compliance by third parties with this License.
440 |
441 | An "entity transaction" is a transaction transferring control of an
442 | organization, or substantially all assets of one, or subdividing an
443 | organization, or merging organizations. If propagation of a covered
444 | work results from an entity transaction, each party to that
445 | transaction who receives a copy of the work also receives whatever
446 | licenses to the work the party's predecessor in interest had or could
447 | give under the previous paragraph, plus a right to possession of the
448 | Corresponding Source of the work from the predecessor in interest, if
449 | the predecessor has it or can get it with reasonable efforts.
450 |
451 | You may not impose any further restrictions on the exercise of the
452 | rights granted or affirmed under this License. For example, you may
453 | not impose a license fee, royalty, or other charge for exercise of
454 | rights granted under this License, and you may not initiate litigation
455 | (including a cross-claim or counterclaim in a lawsuit) alleging that
456 | any patent claim is infringed by making, using, selling, offering for
457 | sale, or importing the Program or any portion of it.
458 |
459 | 11. Patents.
460 |
461 | A "contributor" is a copyright holder who authorizes use under this
462 | License of the Program or a work on which the Program is based. The
463 | work thus licensed is called the contributor's "contributor version".
464 |
465 | A contributor's "essential patent claims" are all patent claims
466 | owned or controlled by the contributor, whether already acquired or
467 | hereafter acquired, that would be infringed by some manner, permitted
468 | by this License, of making, using, or selling its contributor version,
469 | but do not include claims that would be infringed only as a
470 | consequence of further modification of the contributor version. For
471 | purposes of this definition, "control" includes the right to grant
472 | patent sublicenses in a manner consistent with the requirements of
473 | this License.
474 |
475 | Each contributor grants you a non-exclusive, worldwide, royalty-free
476 | patent license under the contributor's essential patent claims, to
477 | make, use, sell, offer for sale, import and otherwise run, modify and
478 | propagate the contents of its contributor version.
479 |
480 | In the following three paragraphs, a "patent license" is any express
481 | agreement or commitment, however denominated, not to enforce a patent
482 | (such as an express permission to practice a patent or covenant not to
483 | sue for patent infringement). To "grant" such a patent license to a
484 | party means to make such an agreement or commitment not to enforce a
485 | patent against the party.
486 |
487 | If you convey a covered work, knowingly relying on a patent license,
488 | and the Corresponding Source of the work is not available for anyone
489 | to copy, free of charge and under the terms of this License, through a
490 | publicly available network server or other readily accessible means,
491 | then you must either (1) cause the Corresponding Source to be so
492 | available, or (2) arrange to deprive yourself of the benefit of the
493 | patent license for this particular work, or (3) arrange, in a manner
494 | consistent with the requirements of this License, to extend the patent
495 | license to downstream recipients. "Knowingly relying" means you have
496 | actual knowledge that, but for the patent license, your conveying the
497 | covered work in a country, or your recipient's use of the covered work
498 | in a country, would infringe one or more identifiable patents in that
499 | country that you have reason to believe are valid.
500 |
501 | If, pursuant to or in connection with a single transaction or
502 | arrangement, you convey, or propagate by procuring conveyance of, a
503 | covered work, and grant a patent license to some of the parties
504 | receiving the covered work authorizing them to use, propagate, modify
505 | or convey a specific copy of the covered work, then the patent license
506 | you grant is automatically extended to all recipients of the covered
507 | work and works based on it.
508 |
509 | A patent license is "discriminatory" if it does not include within
510 | the scope of its coverage, prohibits the exercise of, or is
511 | conditioned on the non-exercise of one or more of the rights that are
512 | specifically granted under this License. You may not convey a covered
513 | work if you are a party to an arrangement with a third party that is
514 | in the business of distributing software, under which you make payment
515 | to the third party based on the extent of your activity of conveying
516 | the work, and under which the third party grants, to any of the
517 | parties who would receive the covered work from you, a discriminatory
518 | patent license (a) in connection with copies of the covered work
519 | conveyed by you (or copies made from those copies), or (b) primarily
520 | for and in connection with specific products or compilations that
521 | contain the covered work, unless you entered into that arrangement,
522 | or that patent license was granted, prior to 28 March 2007.
523 |
524 | Nothing in this License shall be construed as excluding or limiting
525 | any implied license or other defenses to infringement that may
526 | otherwise be available to you under applicable patent law.
527 |
528 | 12. No Surrender of Others' Freedom.
529 |
530 | If conditions are imposed on you (whether by court order, agreement or
531 | otherwise) that contradict the conditions of this License, they do not
532 | excuse you from the conditions of this License. If you cannot convey a
533 | covered work so as to satisfy simultaneously your obligations under this
534 | License and any other pertinent obligations, then as a consequence you may
535 | not convey it at all. For example, if you agree to terms that obligate you
536 | to collect a royalty for further conveying from those to whom you convey
537 | the Program, the only way you could satisfy both those terms and this
538 | License would be to refrain entirely from conveying the Program.
539 |
540 | 13. Remote Network Interaction; Use with the GNU General Public License.
541 |
542 | Notwithstanding any other provision of this License, if you modify the
543 | Program, your modified version must prominently offer all users
544 | interacting with it remotely through a computer network (if your version
545 | supports such interaction) an opportunity to receive the Corresponding
546 | Source of your version by providing access to the Corresponding Source
547 | from a network server at no charge, through some standard or customary
548 | means of facilitating copying of software. This Corresponding Source
549 | shall include the Corresponding Source for any work covered by version 3
550 | of the GNU General Public License that is incorporated pursuant to the
551 | following paragraph.
552 |
553 | Notwithstanding any other provision of this License, you have
554 | permission to link or combine any covered work with a work licensed
555 | under version 3 of the GNU General Public License into a single
556 | combined work, and to convey the resulting work. The terms of this
557 | License will continue to apply to the part which is the covered work,
558 | but the work with which it is combined will remain governed by version
559 | 3 of the GNU General Public License.
560 |
561 | 14. Revised Versions of this License.
562 |
563 | The Free Software Foundation may publish revised and/or new versions of
564 | the GNU Affero General Public License from time to time. Such new versions
565 | will be similar in spirit to the present version, but may differ in detail to
566 | address new problems or concerns.
567 |
568 | Each version is given a distinguishing version number. If the
569 | Program specifies that a certain numbered version of the GNU Affero General
570 | Public License "or any later version" applies to it, you have the
571 | option of following the terms and conditions either of that numbered
572 | version or of any later version published by the Free Software
573 | Foundation. If the Program does not specify a version number of the
574 | GNU Affero General Public License, you may choose any version ever published
575 | by the Free Software Foundation.
576 |
577 | If the Program specifies that a proxy can decide which future
578 | versions of the GNU Affero General Public License can be used, that proxy's
579 | public statement of acceptance of a version permanently authorizes you
580 | to choose that version for the Program.
581 |
582 | Later license versions may give you additional or different
583 | permissions. However, no additional obligations are imposed on any
584 | author or copyright holder as a result of your choosing to follow a
585 | later version.
586 |
587 | 15. Disclaimer of Warranty.
588 |
589 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
590 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
591 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
592 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
593 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
594 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
595 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
596 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
597 |
598 | 16. Limitation of Liability.
599 |
600 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
601 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
602 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
603 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
604 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
605 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
606 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
607 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
608 | SUCH DAMAGES.
609 |
610 | 17. Interpretation of Sections 15 and 16.
611 |
612 | If the disclaimer of warranty and limitation of liability provided
613 | above cannot be given local legal effect according to their terms,
614 | reviewing courts shall apply local law that most closely approximates
615 | an absolute waiver of all civil liability in connection with the
616 | Program, unless a warranty or assumption of liability accompanies a
617 | copy of the Program in return for a fee.
618 |
619 | END OF TERMS AND CONDITIONS
620 |
621 | How to Apply These Terms to Your New Programs
622 |
623 | If you develop a new program, and you want it to be of the greatest
624 | possible use to the public, the best way to achieve this is to make it
625 | free software which everyone can redistribute and change under these terms.
626 |
627 | To do so, attach the following notices to the program. It is safest
628 | to attach them to the start of each source file to most effectively
629 | state the exclusion of warranty; and each file should have at least
630 | the "copyright" line and a pointer to where the full notice is found.
631 |
632 |
633 | Copyright (C)
634 |
635 | This program is free software: you can redistribute it and/or modify
636 | it under the terms of the GNU Affero General Public License as published by
637 | the Free Software Foundation, either version 3 of the License, or
638 | (at your option) any later version.
639 |
640 | This program is distributed in the hope that it will be useful,
641 | but WITHOUT ANY WARRANTY; without even the implied warranty of
642 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
643 | GNU Affero General Public License for more details.
644 |
645 | You should have received a copy of the GNU Affero General Public License
646 | along with this program. If not, see .
647 |
648 | Also add information on how to contact you by electronic and paper mail.
649 |
650 | If your software can interact with users remotely through a computer
651 | network, you should also make sure that it provides a way for users to
652 | get its source. For example, if your program is a web application, its
653 | interface could display a "Source" link that leads users to an archive
654 | of the code. There are many ways you could offer source, and different
655 | solutions will be better for different programs; see section 13 for the
656 | specific requirements.
657 |
658 | You should also get your employer (if you work as a programmer) or school,
659 | if any, to sign a "copyright disclaimer" for the program, if necessary.
660 | For more information on this, and how to apply and follow the GNU AGPL, see
661 | .
662 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # zig-riscv-embedded
2 |
3 | Experimental [Zig][zig website]-based [CoAP][rfc7252] node for the [HiFive1][hifive1 website] RISC-V board.
4 |
5 | 
6 |
7 | ## Status
8 |
9 | This repository is intended to provide a simple sample application for
10 | experimenting with the Zig programming language on freestanding RISC-V.
11 | The application targets the [SiFive FE310-G000][fe310 manual] or more
12 | specifically the [HiFive 1][hifive1 website]. While possible to run the
13 | application on "real hardware", it can also be run using QEMU. In both
14 | cases it is possible to toggle an LED using [CoAP][rfc7252] over
15 | [SLIP][rfc1055].
16 |
17 | ## CoAP over SLIP
18 |
19 | To experiment with external dependencies in Zig, this application
20 | provides a very bare bone implementation of [CoAP][rfc7252] using
21 | [zoap][zoap github]. Since implementing an entire UDP/IP stack from
22 | scratch is out-of-scope, this repository transports CoAP packets
23 | directly over [SLIP][rfc1055].
24 |
25 | Unfortunately, the QFN48 package of the FE310-G000 (as used by the
26 | HiFive1) does not support the UART1. For this reason, the application
27 | multiplexes diagnostic messages and CoAP frames over the same UART
28 | (UART0) using [Slipmux][slipmux]. For this purpose, a Go-based
29 | multiplexer for the development system is available in the `./slipmux`
30 | subdirectory.
31 |
32 | ## Dependencies
33 |
34 | For building the software and the associated Slipmux tooling, the
35 | following software is required:
36 |
37 | * Zig `0.9.1`
38 | * [Go][golang web] for compiling the `./slipmux` tool
39 | * A CoAP client, e.g. `coap-client(1)` from [libcoap][libcoap github]
40 | * QEMU (`qemu-system-riscv32`) for emulating a HiFive1 (optional)
41 |
42 | For flashing to real hardware, the following software is required:
43 |
44 | * [riscv-openocd][riscv-openocd]
45 | * [GDB][gdb web] with 32-bit RISC-V support
46 |
47 | ## Building
48 |
49 | The Zig build system is used for building the application, the
50 | configuration is available in `build.zig`. To build the application run:
51 |
52 | $ zig build
53 |
54 | This will create a freestanding RISC-V ELF binary `zig-out/bin/main`.
55 | If the image should be booted on real hardware, building in the
56 | `ReleaseSmall` [build mode][zig build modes] may be desirable:
57 |
58 | $ zig build -Drelease-small
59 |
60 | Furthermore, the Slipmux multiplexer needs to be compiled using the
61 | following commands in order to receive diagnostic messages from the
62 | device and send CoAP messages to the device:
63 |
64 | $ cd slipmux && go build -trimpath
65 |
66 | ## Booting in QEMU
67 |
68 | In order to simulate a serial device, which can be used with the
69 | `./slipmux` tool, QEMU must be started as follows:
70 |
71 | $ qemu-system-riscv32 -M sifive_e -nographic -kernel zig-out/bin/main -serial pty
72 |
73 | QEMU will print the allocated PTY path to standard output. In a separate
74 | terminal the `./slipmux` tool can then be started as follows:
75 |
76 | $ ./slipmux/slipmux :2342
77 |
78 | This will create a UDP Socket on `localhost:2342`, CoAP packets send to
79 | this socket are converted into Slipmux CoAP frames and forwarded to the
80 | emulated HiFive1 over the allocated PTY. CoAP packets can be send using
81 | any CoAP client, e.g. using `coap-client(1)` from [libcoap][libcoap github]:
82 |
83 | $ coap-client -N -m put coap://[::1]:2342/on
84 | $ coap-client -N -m put coap://[::1]:2342/off
85 |
86 | In QEMU, this will cause debug messages to appear in the terminal window
87 | were `./slipmux` is running. On real hardware, it will also cause the
88 | red LED to be toggled.
89 |
90 | ## Booting on real hardware
91 |
92 | The binary can be flashed to real hardware using OpenOCD and gdb. For
93 | this purpose, a shell script is provided. In order to flash a compiled
94 | binary run the following command:
95 |
96 | $ ./flash
97 |
98 | After flashing the device, interactions through CoAP are possible using
99 | the instructions given for QEMU above. However, with real hardware
100 | `./slipmux` needs to be passed the TTY device for the HiFive1 (i.e.
101 | `/dev/ttyUSB0`).
102 |
103 | To debug errors on real hardware start OpenOCD using `openocd -f
104 | openocd.cfg`. In a separate terminal start a gdb version with RISC-V
105 | support (e.g. [gdb-multiarch][gdb-multiarch alpine]) as follows:
106 |
107 | $ gdb-multiarch -ex 'target extended-remote :3333' zig-out/bin/main
108 |
109 | ## Development
110 |
111 | A pre-commit git hook for checking if files are properly formated is
112 | provided in `.githooks`. It can be activated using:
113 |
114 | $ git config --local core.hooksPath .githooks
115 |
116 | ## License
117 |
118 | The application uses slightly modified linker scripts and assembler
119 | startup code copied from the [RIOT][riot fe310] operating system. Unless
120 | otherwise noted code written by myself is licensed under
121 | `AGPL-3.0-or-later`. Refer to the license headers of the different files
122 | for more information.
123 |
124 | [zig website]: https://ziglang.org/
125 | [zig build modes]: https://ziglang.org/documentation/master/#Build-Mode
126 | [qemu website]: https://www.qemu.org/
127 | [fe310 manual]: https://static.dev.sifive.com/FE310-G000.pdf
128 | [hifive1 website]: https://www.sifive.com/boards/hifive1
129 | [riot fe310]: https://github.com/RIOT-OS/RIOT/tree/master/cpu/fe310
130 | [slipmux]: https://datatracker.ietf.org/doc/html/draft-bormann-t2trg-slipmux-03
131 | [rfc7252]: https://datatracker.ietf.org/doc/html/rfc7252
132 | [rfc1055]: https://datatracker.ietf.org/doc/html/rfc1055
133 | [libcoap github]: https://github.com/obgm/libcoap
134 | [golang web]: https://golang.org
135 | [zoap github]: https://github.com/nmeum/zoap
136 | [riscv-openocd]: https://github.com/riscv/riscv-openocd
137 | [gdb web]: https://www.gnu.org/software/gdb/
138 | [gdb-multiarch alpine]: https://pkgs.alpinelinux.org/package/edge/main/x86_64/gdb-multiarch
139 |
--------------------------------------------------------------------------------
/TODO.txt:
--------------------------------------------------------------------------------
1 | * Add abstraction for PRCI
2 | * Rewrite src/clock.c in Zig
3 | * Make LED handler read payload to determine desired LED status
4 | * Allow setting all three LEDs
5 | * Will likely require some sort of subdispatcher code in zoap
6 | * That is, a subdispatcher for /led
7 | * Debug occasional "bad checksum" Slipmux errors
8 | * Buffer incoming UART data in a linear FIFO
9 | * To reduce time spend in interrupt handler
10 | * Use RISC-V atomic instructions for MMIO
11 | * For example, atomic_or etc. (see RIOT code)
12 | * To-Do: Figure out how Zig abstracts these instructions
13 |
--------------------------------------------------------------------------------
/build.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2020-2021 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public License
14 | // along with this program. If not, see .
15 |
16 | const std = @import("std");
17 | const Target = std.Target;
18 | const Zig = std.zig;
19 | const FileSource = std.build.FileSource;
20 | const Builder = std.build.Builder;
21 | const FeatureSet = std.Target.Cpu.Feature.Set;
22 |
23 | pub fn build(b: *Builder) void {
24 | var fe310_cpu_feat = FeatureSet.empty;
25 | const m: std.Target.riscv.Feature = .m;
26 | const a: std.Target.riscv.Feature = .a;
27 | const c: std.Target.riscv.Feature = .c;
28 | fe310_cpu_feat.addFeature(@enumToInt(a));
29 | fe310_cpu_feat.addFeature(@enumToInt(m));
30 | fe310_cpu_feat.addFeature(@enumToInt(c));
31 |
32 | const target = Zig.CrossTarget{
33 | .cpu_arch = Target.Cpu.Arch.riscv32,
34 | .os_tag = Target.Os.Tag.freestanding,
35 | .abi = Target.Abi.none,
36 | .cpu_features_sub = std.Target.riscv.cpu.baseline_rv32.features,
37 | .cpu_features_add = fe310_cpu_feat,
38 | };
39 |
40 | const mode = b.standardReleaseOptions();
41 |
42 | const exe = b.addExecutable("main", "src/init.zig");
43 | exe.setLinkerScriptPath(FileSource{ .path = "fe310_g000.ld" });
44 | exe.setTarget(target);
45 | exe.setBuildMode(mode);
46 |
47 | exe.addCSourceFile("src/start.S", &[_][]const u8{});
48 | exe.addCSourceFile("src/irq.S", &[_][]const u8{});
49 | exe.addCSourceFile("src/clock.c", &[_][]const u8{});
50 |
51 | exe.addPackage(std.build.Pkg{
52 | .name = "zoap",
53 | .path = FileSource{ .path = "./zoap/src/zoap.zig" },
54 | });
55 |
56 | b.default_step.dependOn(&exe.step);
57 | b.installArtifact(exe);
58 | }
59 |
--------------------------------------------------------------------------------
/fe310_g000.ld:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017, 2019 Ken Rabold
3 | *
4 | * This file is subject to the terms and conditions of the GNU Lesser
5 | * General Public License v2.1. See the file LICENSE in the top level
6 | * directory for more details.
7 | */
8 |
9 | OUTPUT_ARCH( "riscv" )
10 |
11 | ENTRY( _start )
12 |
13 | PHDRS
14 | {
15 | flash PT_LOAD;
16 | ram_init PT_LOAD;
17 | ram PT_NULL;
18 | }
19 |
20 | MEMORY
21 | {
22 | flash (rx) : ORIGIN = 0x20400000, LENGTH = 0x1fc00000
23 | ram (rwx) : ORIGIN = 0x80000000, LENGTH = 0x00004000
24 | itim (rwx) : ORIGIN = 0x08000000, LENGTH = 0x00002000
25 | }
26 |
27 | SECTIONS
28 | {
29 | __stack_size = DEFINED(__stack_size) ? __stack_size : 256;
30 |
31 | .init :
32 | {
33 | KEEP (*(SORT_NONE(.init)))
34 | } >flash AT>flash :flash
35 |
36 | .text :
37 | {
38 | *(.text.unlikely .text.unlikely.*)
39 | *(.text.startup .text.startup.*)
40 | *(.text .text.*)
41 | *(.gnu.linkonce.t.*)
42 |
43 | KEEP(*(.eh_frame*))
44 | } >flash AT>flash :flash
45 |
46 | .fini :
47 | {
48 | KEEP (*(SORT_NONE(.fini)))
49 | } >flash AT>flash :flash
50 |
51 | PROVIDE (__etext = .);
52 | PROVIDE (_etext = .);
53 | PROVIDE (etext = .);
54 |
55 | .rodata :
56 | {
57 | *(.rdata)
58 | *(.rodata .rodata.*)
59 | *(.gnu.linkonce.r.*)
60 | } >flash AT>flash :flash
61 |
62 | . = ALIGN(4);
63 |
64 | .preinit_array :
65 | {
66 | PROVIDE_HIDDEN (__preinit_array_start = .);
67 | KEEP (*(.preinit_array))
68 | PROVIDE_HIDDEN (__preinit_array_end = .);
69 | } >flash AT>flash :flash
70 |
71 | .init_array :
72 | {
73 | PROVIDE_HIDDEN (__init_array_start = .);
74 | KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
75 | KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
76 | PROVIDE_HIDDEN (__init_array_end = .);
77 | } >flash AT>flash :flash
78 |
79 | .fini_array :
80 | {
81 | PROVIDE_HIDDEN (__fini_array_start = .);
82 | KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
83 | KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
84 | PROVIDE_HIDDEN (__fini_array_end = .);
85 | } >flash AT>flash :flash
86 |
87 | .ctors :
88 | {
89 | /* gcc uses crtbegin.o to find the start of
90 | the constructors, so we make sure it is
91 | first. Because this is a wildcard, it
92 | doesn't matter if the user does not
93 | actually link against crtbegin.o; the
94 | linker won't look for a file to match a
95 | wildcard. The wildcard also means that it
96 | doesn't matter which directory crtbegin.o
97 | is in. */
98 | KEEP (*crtbegin.o(.ctors))
99 | KEEP (*crtbegin?.o(.ctors))
100 | /* We don't want to include the .ctor section from
101 | the crtend.o file until after the sorted ctors.
102 | The .ctor section from the crtend file contains the
103 | end of ctors marker and it must be last */
104 | KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
105 | KEEP (*(SORT(.ctors.*)))
106 | KEEP (*(.ctors))
107 | } >flash AT>flash :flash
108 |
109 | .dtors :
110 | {
111 | KEEP (*crtbegin.o(.dtors))
112 | KEEP (*crtbegin?.o(.dtors))
113 | KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
114 | KEEP (*(SORT(.dtors.*)))
115 | KEEP (*(.dtors))
116 | } >flash AT>flash :flash
117 |
118 | .lalign :
119 | {
120 | . = ALIGN(4);
121 | PROVIDE( _data_lma = . );
122 | } >flash AT>flash :flash
123 |
124 | .dalign :
125 | {
126 | . = ALIGN(4);
127 | PROVIDE( _data = . );
128 | } >ram AT>flash :ram_init
129 |
130 | .data :
131 | {
132 | *(.ramfunc .ramfunc.*)
133 | *(.data .data.*)
134 | *(.gnu.linkonce.d.*)
135 | . = ALIGN(8);
136 | PROVIDE( __global_pointer$ = . + 0x800 );
137 | *(.sdata .sdata.*)
138 | *(.gnu.linkonce.s.*)
139 | . = ALIGN(8);
140 | *(.srodata.cst16)
141 | *(.srodata.cst8)
142 | *(.srodata.cst4)
143 | *(.srodata.cst2)
144 | *(.srodata .srodata.*)
145 | } >ram AT>flash :ram_init
146 |
147 | . = ALIGN(4);
148 | PROVIDE( _edata = . );
149 | PROVIDE( edata = . );
150 |
151 | PROVIDE( _fbss = . );
152 | PROVIDE( __bss_start = . );
153 | .bss :
154 | {
155 | *(.sbss*)
156 | *(.gnu.linkonce.sb.*)
157 | *(.bss .bss.*)
158 | *(.gnu.linkonce.b.*)
159 | *(COMMON)
160 | . = ALIGN(4);
161 | } >ram AT>ram :ram
162 |
163 | . = ALIGN(8);
164 | PROVIDE( _end = . );
165 | PROVIDE( end = . );
166 | PROVIDE( _heap_start = . );
167 |
168 | __StackTop = ORIGIN(ram) + LENGTH(ram);
169 | __StackLimit = __StackTop - __stack_size;
170 | PROVIDE(__stack = __StackTop);
171 | }
172 |
--------------------------------------------------------------------------------
/flash:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # From https://github.com/sifive/freedom-e-sdk/blob/c07ac84cbdd678963c4dc3ea34a68c47a8b53651/scripts/upload
3 | #
4 | # Copyright (c) 2019 SiFive, Inc.
5 | # Licensed under Apache2 and MIT licenses.
6 |
7 | set -e
8 |
9 | ELF_FILE=./zig-out/bin/main
10 | export GDB_PORT=3333
11 |
12 | openocd -f openocd.cfg &
13 |
14 | gdb-multiarch $ELF_FILE --batch -ex "set remotetimeout 240" \
15 | -ex "target extended-remote localhost:${GDB_PORT}" \
16 | -ex "monitor reset halt" \
17 | -ex "monitor flash protect 0 64 last off" \
18 | -ex "thread apply all set \$pc=_enter" \
19 | -ex "load" \
20 | -ex "monitor resume" \
21 | -ex "monitor shutdown" -ex "quit"
22 |
--------------------------------------------------------------------------------
/openocd.cfg:
--------------------------------------------------------------------------------
1 | # From https://github.com/sifive/freedom-e-sdk/blob/c07ac84cbdd678963c4dc3ea34a68c47a8b53651/bsp/sifive-hifive1/openocd.cfg
2 | #
3 | # Copyright (c) 2019 SiFive, Inc.
4 | # Licensed under Apache2 and MIT licenses.
5 |
6 | # JTAG adapter setup
7 | adapter_khz 10000
8 |
9 | set chain_length 5
10 |
11 | interface ftdi
12 | ftdi_device_desc "Dual RS232-HS"
13 | ftdi_vid_pid 0x0403 0x6010
14 |
15 | ftdi_layout_init 0x0008 0x001b
16 | ftdi_layout_signal nSRST -oe 0x0020
17 | ftdi_layout_signal LED -data 0x0020
18 |
19 | set _CHIPNAME riscv
20 | jtag newtap $_CHIPNAME cpu -irlen $chain_length
21 |
22 | set _TARGETNAME_0 $_CHIPNAME.cpu
23 |
24 | target create $_TARGETNAME_0 riscv -chain-position $_TARGETNAME_0
25 |
26 | $_TARGETNAME_0 configure -work-area-phys 0x80000000 -work-area-size 0x2710 -work-area-backup 1
27 |
28 | if { $chain_length == 6 } {
29 | riscv use_bscan_tunnel 5
30 | }
31 |
32 | flash bank spi0 fespi 0x20000000 0 0 0 $_TARGETNAME_0 0x10014000
33 |
34 | init
35 | if { [info exists authkey] } {
36 | riscv authdata_write $authkey
37 | }
38 |
39 | if {[ info exists pulse_srst]} {
40 | ftdi_set_signal nSRST 0
41 | ftdi_set_signal nSRST z
42 | sleep 1500
43 | }
44 | halt
45 |
46 | flash protect 0 64 last off
47 |
48 | echo "Ready for Remote Connections"
49 |
--------------------------------------------------------------------------------
/slipmux/coap.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "net"
5 | )
6 |
7 | type CoapEndpoint struct {
8 | conn net.PacketConn
9 | lastAddr *net.Addr
10 | // XXX: mutex?
11 |
12 | RX <-chan []byte
13 | TX chan<- []byte
14 | }
15 |
16 | func NewCoapEP(addr string) (*CoapEndpoint, error) {
17 | conn, err := net.ListenPacket("udp", addr)
18 | if err != nil {
19 | return nil, err
20 | }
21 |
22 | rx := make(chan []byte)
23 | tx := make(chan []byte)
24 |
25 | ep := &CoapEndpoint{
26 | conn: conn,
27 | lastAddr: nil,
28 | RX: rx,
29 | TX: tx,
30 | }
31 |
32 | go ep.sndLoop(tx)
33 | go ep.rcvLoop(rx)
34 |
35 | return ep, nil
36 | }
37 |
38 | func (c *CoapEndpoint) sndLoop(ch <-chan []byte) {
39 | for {
40 | data := <-ch
41 |
42 | if c.lastAddr == nil {
43 | logger.Println("[CoapEndpoint] Unexpected CoAP send")
44 | continue
45 | }
46 |
47 | _, err := c.conn.WriteTo(data, *c.lastAddr)
48 | if err != nil {
49 | logger.Println("[CoapEndpoint] WriteTo failed", err)
50 | continue
51 | }
52 | }
53 | }
54 |
55 | func (c *CoapEndpoint) rcvLoop(ch chan<- []byte) {
56 | buf := make([]byte, bufSize)
57 |
58 | for {
59 | n, addr, err := c.conn.ReadFrom(buf)
60 | if err != nil {
61 | logger.Println("[CoapEndpoint] ReadFrom failed", err)
62 | continue
63 | }
64 |
65 | c.lastAddr = &addr
66 | ch <- buf[0:n]
67 | }
68 | }
69 |
70 | func (c *CoapEndpoint) Close() {
71 | // close(c.Chan)
72 | c.conn.Close()
73 | }
74 |
--------------------------------------------------------------------------------
/slipmux/dispatcher.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "os"
5 |
6 | "github.com/Lobaro/slip"
7 | )
8 |
9 | type Dispatcher struct {
10 | coap *CoapEndpoint
11 | serial *SerialEndpoint
12 | }
13 |
14 | func (d *Dispatcher) handleCoap(data []byte) {
15 | d.serial.TX <- data
16 | }
17 |
18 | func (d *Dispatcher) handleSerial(pkt Packet) {
19 | switch pkt.FrameType {
20 | case slip.FRAME_DIAGNOSTIC:
21 | _, err := os.Stdout.WriteString(string(pkt.Data))
22 | if err != nil {
23 | logger.Println("handleSerial:", err)
24 | }
25 | case slip.FRAME_COAP:
26 | d.coap.TX <- pkt.Data
27 | default:
28 | logger.Printf("handleSerial: Unsupported frame type: 0x%x\n", pkt.FrameType)
29 | }
30 | }
31 |
32 | func (d *Dispatcher) Run() {
33 | for {
34 | select {
35 | case data := <-d.coap.RX:
36 | d.handleCoap(data)
37 | case pkt := <-d.serial.RX:
38 | d.handleSerial(pkt)
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/slipmux/go.mod:
--------------------------------------------------------------------------------
1 | module github.com/nmeum/zig-riscv-embedded/slipmux
2 |
3 | go 1.17
4 |
5 | require (
6 | github.com/Lobaro/slip v0.0.0-20170904164317-88dcb78414ea
7 | go.bug.st/serial v1.3.3
8 | )
9 |
10 | require (
11 | github.com/creack/goselect v0.1.2 // indirect
12 | golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef // indirect
13 | )
14 |
--------------------------------------------------------------------------------
/slipmux/go.sum:
--------------------------------------------------------------------------------
1 | github.com/Lobaro/slip v0.0.0-20170904164317-88dcb78414ea h1:lpHJkBxATbHlFRPGlD7/gU1LTyMNMkhpYKxCUT7Ynx0=
2 | github.com/Lobaro/slip v0.0.0-20170904164317-88dcb78414ea/go.mod h1:r5Qh0dZbKfQ+1KRK78WRCeStYKOIGr/cF/QrkjtH88s=
3 | github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
4 | github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
5 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
6 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
7 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
10 | github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
11 | github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
12 | go.bug.st/serial v1.3.3 h1:lOSLGmZSB7qU6pSOaZqlRholjC8SmmFTGv4ib9oPwYo=
13 | go.bug.st/serial v1.3.3/go.mod h1:jDkjqASf/qSjmaOxHSHljwUQ6eHo/ZX/bxJLQqSlvZg=
14 | golang.org/x/sys v0.0.0-20210823070655-63515b42dcdf/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
15 | golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef h1:fPxZ3Umkct3LZ8gK9nbk+DWDJ9fstZa2grBn+lWVKPs=
16 | golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
17 | gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
18 | gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
19 | gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
20 |
--------------------------------------------------------------------------------
/slipmux/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | "log"
6 | "os"
7 | "path/filepath"
8 | )
9 |
10 | const (
11 | bufSize = 1024
12 | baudRate = 115200
13 | )
14 |
15 | var (
16 | out = os.Stdout
17 | err = os.Stderr
18 | )
19 |
20 | var logger = log.New(err, "", log.Lshortfile)
21 |
22 | func main() {
23 | if len(os.Args) < 3 {
24 | fmt.Fprintf(os.Stderr, "USAGE: %s ADDR PATH\n", filepath.Base(os.Args[0]))
25 | os.Exit(1)
26 | }
27 |
28 | addr := os.Args[1]
29 | path := os.Args[2]
30 |
31 | cep, err := NewCoapEP(addr)
32 | if err != nil {
33 | logger.Fatal(err)
34 | }
35 | defer cep.Close()
36 |
37 | sep, err := NewSerialEP(path)
38 | if err != nil {
39 | logger.Fatal(err)
40 | }
41 | defer sep.Close()
42 |
43 | dispatcher := &Dispatcher{cep, sep}
44 | dispatcher.Run()
45 | }
46 |
--------------------------------------------------------------------------------
/slipmux/serial.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "bytes"
5 | "errors"
6 |
7 | "github.com/Lobaro/slip"
8 | "go.bug.st/serial"
9 | )
10 |
11 | type Packet struct {
12 | FrameType byte
13 | Data []byte
14 | }
15 |
16 | type SerialEndpoint struct {
17 | port serial.Port
18 |
19 | RX <-chan Packet
20 | TX chan<- []byte
21 | }
22 |
23 | func NewSerialEP(path string) (*SerialEndpoint, error) {
24 | mode := &serial.Mode{
25 | BaudRate: baudRate,
26 | DataBits: 8,
27 | Parity: serial.NoParity,
28 | StopBits: serial.OneStopBit,
29 | }
30 |
31 | port, err := serial.Open(path, mode)
32 | if err != nil {
33 | return nil, err
34 | }
35 |
36 | rx := make(chan Packet)
37 | tx := make(chan []byte)
38 |
39 | ep := &SerialEndpoint{
40 | port: port,
41 | RX: rx,
42 | TX: tx,
43 | }
44 |
45 | go ep.rcvLoop(rx)
46 | go ep.sndLoop(tx)
47 |
48 | return ep, nil
49 | }
50 |
51 | func (s *SerialEndpoint) sndLoop(ch <-chan []byte) {
52 | writer := slip.NewSlipMuxWriter(NewSlowWriter(s.port))
53 |
54 | for {
55 | data := <-ch
56 |
57 | err := writer.WritePacket(slip.FRAME_COAP, data)
58 | if err != nil {
59 | logger.Println("handleCoap:", err)
60 | continue
61 | }
62 | }
63 | }
64 |
65 | func (s *SerialEndpoint) rcvLoop(ch chan<- Packet) {
66 | reader := slip.NewSlipMuxReader(s.port)
67 |
68 | for {
69 | packet, frame, err := reader.ReadPacket()
70 |
71 | var perr *serial.PortError
72 | if errors.As(err, &perr) && perr.Code() == serial.PortClosed {
73 | logger.Fatal("[SerialEndpoint]", err)
74 | } else if err != nil {
75 | logger.Println("[SerialEndpoint]", err)
76 | continue
77 | }
78 |
79 | packet = bytes.TrimPrefix(packet, []byte{0})
80 | ch <- Packet{frame, packet}
81 | }
82 | }
83 |
84 | func (s *SerialEndpoint) Close() {
85 | // close(s.Chan)
86 | // TODO: Close TTYIO
87 | }
88 |
--------------------------------------------------------------------------------
/slipmux/slow_writer.go:
--------------------------------------------------------------------------------
1 | // This is a hacky workaround for the fact that the HiFive1 FTDI chip
2 | // doesn't do hardware flow control and the UART0 interrupt handler
3 | // takes an eternity to complete since it does CoAP message handling.
4 | package main
5 |
6 | import (
7 | "io"
8 | "time"
9 | )
10 |
11 | const (
12 | pause = 1 * time.Second
13 | fifoDepth = 8
14 | )
15 |
16 | type SlowWriter struct {
17 | w io.Writer
18 | }
19 |
20 | func NewSlowWriter(w io.Writer) SlowWriter {
21 | return SlowWriter{w: w}
22 | }
23 |
24 | func (w SlowWriter) Write(p []byte) (int, error) {
25 | var n int
26 | for i, c := range p {
27 | if i != 0 && i%fifoDepth == 0 {
28 | time.Sleep(pause)
29 | }
30 |
31 | written, err := w.w.Write([]byte{c})
32 | if err != nil {
33 | return n, err
34 | }
35 |
36 | n += written
37 | }
38 |
39 | return n, nil
40 | }
41 |
--------------------------------------------------------------------------------
/src/clock.c:
--------------------------------------------------------------------------------
1 | /* Copied from https://wiki.osdev.org/HiFive-1_Bare_Bones#The_kernel_source_code */
2 | /* Should eventually be rewritten in Zig with proper abstraction for the PRCI. */
3 |
4 | #include
5 |
6 | #define PRCI_CTRL_ADDR 0x10008000UL
7 | #define PRCI_HFROSCCFG (0x0000)
8 | #define PRCI_PLLCFG (0x0008)
9 | #define ROSC_EN(x) (((x) & 0x1) << 30)
10 | #define PLL_REFSEL(x) (((x) & 0x1) << 17)
11 | #define PLL_BYPASS(x) (((x) & 0x1) << 18)
12 | #define PLL_SEL(x) (((x) & 0x1) << 16)
13 |
14 | static inline uint32_t
15 | mmio_read_u32(unsigned long reg, unsigned int offset)
16 | {
17 | return (*(volatile uint32_t *) ((reg) + (offset)));
18 | }
19 |
20 | static inline void
21 | mmio_write_u32(unsigned long reg, unsigned int offset, uint32_t val)
22 | {
23 | (*(volatile uint32_t *) ((reg) + (offset))) = val;
24 | }
25 |
26 | void
27 | clock_init(void)
28 | {
29 | /* Make sure the HFROSC is on */
30 | mmio_write_u32(PRCI_CTRL_ADDR, PRCI_HFROSCCFG,
31 | mmio_read_u32(PRCI_CTRL_ADDR, PRCI_HFROSCCFG)
32 | | ROSC_EN(1));
33 |
34 | /* Run off 16 MHz Crystal for accuracy */
35 | mmio_write_u32(PRCI_CTRL_ADDR, PRCI_PLLCFG,
36 | mmio_read_u32(PRCI_CTRL_ADDR, PRCI_PLLCFG)
37 | | (PLL_REFSEL(1) | PLL_BYPASS(1)));
38 | mmio_write_u32(PRCI_CTRL_ADDR, PRCI_PLLCFG,
39 | mmio_read_u32(PRCI_CTRL_ADDR, PRCI_PLLCFG)
40 | | (PLL_SEL(1)));
41 |
42 | /* Turn off HFROSC to save power */
43 | mmio_write_u32(PRCI_CTRL_ADDR, PRCI_HFROSCCFG,
44 | mmio_read_u32(PRCI_CTRL_ADDR, PRCI_HFROSCCFG)
45 | & ~(ROSC_EN(1)));
46 | }
47 |
--------------------------------------------------------------------------------
/src/console.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public License
14 | // along with this program. If not, see .
15 |
16 | const periph = @import("periph.zig");
17 | const slipmux = @import("slipmux.zig");
18 |
19 | pub fn getStdDbg() slipmux.Frame {
20 | const ftype = slipmux.FrameType.diagnostic;
21 | return periph.slipmux.newFrame(ftype);
22 | }
23 |
24 | // Write a Slipmux diagnostic message, unbuffered, to the UART.
25 | pub fn print(comptime fmt: []const u8, args: anytype) void {
26 | var stddbg = getStdDbg();
27 | defer stddbg.close();
28 |
29 | const w = stddbg.writer();
30 | nosuspend w.print(fmt, args) catch return;
31 | }
32 |
--------------------------------------------------------------------------------
/src/crc.zig:
--------------------------------------------------------------------------------
1 | // From https://github.com/lobaro/slip/blob/master/fcs.go
2 |
3 | const FCS_INIT: u16 = 0xffff;
4 | const FCS_GOOD: u16 = 0xf0b8;
5 |
6 | const fcstab = [256]u16{
7 | 0x0000, 0x1189, 0x2312, 0x329b, 0x4624, 0x57ad, 0x6536, 0x74bf,
8 | 0x8c48, 0x9dc1, 0xaf5a, 0xbed3, 0xca6c, 0xdbe5, 0xe97e, 0xf8f7,
9 | 0x1081, 0x0108, 0x3393, 0x221a, 0x56a5, 0x472c, 0x75b7, 0x643e,
10 | 0x9cc9, 0x8d40, 0xbfdb, 0xae52, 0xdaed, 0xcb64, 0xf9ff, 0xe876,
11 | 0x2102, 0x308b, 0x0210, 0x1399, 0x6726, 0x76af, 0x4434, 0x55bd,
12 | 0xad4a, 0xbcc3, 0x8e58, 0x9fd1, 0xeb6e, 0xfae7, 0xc87c, 0xd9f5,
13 | 0x3183, 0x200a, 0x1291, 0x0318, 0x77a7, 0x662e, 0x54b5, 0x453c,
14 | 0xbdcb, 0xac42, 0x9ed9, 0x8f50, 0xfbef, 0xea66, 0xd8fd, 0xc974,
15 | 0x4204, 0x538d, 0x6116, 0x709f, 0x0420, 0x15a9, 0x2732, 0x36bb,
16 | 0xce4c, 0xdfc5, 0xed5e, 0xfcd7, 0x8868, 0x99e1, 0xab7a, 0xbaf3,
17 | 0x5285, 0x430c, 0x7197, 0x601e, 0x14a1, 0x0528, 0x37b3, 0x263a,
18 | 0xdecd, 0xcf44, 0xfddf, 0xec56, 0x98e9, 0x8960, 0xbbfb, 0xaa72,
19 | 0x6306, 0x728f, 0x4014, 0x519d, 0x2522, 0x34ab, 0x0630, 0x17b9,
20 | 0xef4e, 0xfec7, 0xcc5c, 0xddd5, 0xa96a, 0xb8e3, 0x8a78, 0x9bf1,
21 | 0x7387, 0x620e, 0x5095, 0x411c, 0x35a3, 0x242a, 0x16b1, 0x0738,
22 | 0xffcf, 0xee46, 0xdcdd, 0xcd54, 0xb9eb, 0xa862, 0x9af9, 0x8b70,
23 | 0x8408, 0x9581, 0xa71a, 0xb693, 0xc22c, 0xd3a5, 0xe13e, 0xf0b7,
24 | 0x0840, 0x19c9, 0x2b52, 0x3adb, 0x4e64, 0x5fed, 0x6d76, 0x7cff,
25 | 0x9489, 0x8500, 0xb79b, 0xa612, 0xd2ad, 0xc324, 0xf1bf, 0xe036,
26 | 0x18c1, 0x0948, 0x3bd3, 0x2a5a, 0x5ee5, 0x4f6c, 0x7df7, 0x6c7e,
27 | 0xa50a, 0xb483, 0x8618, 0x9791, 0xe32e, 0xf2a7, 0xc03c, 0xd1b5,
28 | 0x2942, 0x38cb, 0x0a50, 0x1bd9, 0x6f66, 0x7eef, 0x4c74, 0x5dfd,
29 | 0xb58b, 0xa402, 0x9699, 0x8710, 0xf3af, 0xe226, 0xd0bd, 0xc134,
30 | 0x39c3, 0x284a, 0x1ad1, 0x0b58, 0x7fe7, 0x6e6e, 0x5cf5, 0x4d7c,
31 | 0xc60c, 0xd785, 0xe51e, 0xf497, 0x8028, 0x91a1, 0xa33a, 0xb2b3,
32 | 0x4a44, 0x5bcd, 0x6956, 0x78df, 0x0c60, 0x1de9, 0x2f72, 0x3efb,
33 | 0xd68d, 0xc704, 0xf59f, 0xe416, 0x90a9, 0x8120, 0xb3bb, 0xa232,
34 | 0x5ac5, 0x4b4c, 0x79d7, 0x685e, 0x1ce1, 0x0d68, 0x3ff3, 0x2e7a,
35 | 0xe70e, 0xf687, 0xc41c, 0xd595, 0xa12a, 0xb0a3, 0x8238, 0x93b1,
36 | 0x6b46, 0x7acf, 0x4854, 0x59dd, 0x2d62, 0x3ceb, 0x0e70, 0x1ff9,
37 | 0xf78f, 0xe606, 0xd49d, 0xc514, 0xb1ab, 0xa022, 0x92b9, 0x8330,
38 | 0x7bc7, 0x6a4e, 0x58d5, 0x495c, 0x3de3, 0x2c6a, 0x1ef1, 0x0f78,
39 | };
40 |
41 | pub const Incremental = struct {
42 | fcs: u16 = FCS_INIT,
43 |
44 | pub fn add(self: *Incremental, data: u8) void {
45 | self.fcs = (self.fcs >> 8) ^ fcstab[(self.fcs ^ @as(u16, data)) & 0xff];
46 | }
47 |
48 | pub fn csum(self: *const Incremental) u16 {
49 | return self.fcs;
50 | }
51 | };
52 |
53 | pub fn calcCsum(data: []const u8) u16 {
54 | var inc: Incremental = .{};
55 | for (data) |b|
56 | inc.add(b);
57 |
58 | return inc.csum();
59 | }
60 |
61 | pub fn validCsum(data: []const u8) bool {
62 | const trialfcs = calcCsum(data);
63 | return trialfcs == FCS_GOOD;
64 | }
65 |
--------------------------------------------------------------------------------
/src/gpio.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public License
14 | // along with this program. If not, see .
15 |
16 | // Maximum of 32 pins -> 2**5 = 32.
17 | pub const Pin = u5;
18 |
19 | pub const Mode = enum {
20 | IN,
21 | OUT,
22 | };
23 |
24 | pub fn pin(x: Pin, y: Pin) Pin {
25 | return x | y;
26 | }
27 |
28 | pub const Gpio = struct {
29 | base_addr: usize,
30 |
31 | const Reg = enum(usize) {
32 | input = 0x04,
33 | output_en = 0x08,
34 | output_val = 0x0c,
35 | pue = 0x10,
36 | iof_en = 0x38,
37 | iof_sel = 0x3c,
38 | };
39 |
40 | fn readWord(self: Gpio, reg: Reg) u32 {
41 | const ptr = @intToPtr(*volatile u32, self.base_addr + @enumToInt(reg));
42 | return ptr.*;
43 | }
44 |
45 | fn writeWord(self: Gpio, reg: Reg, value: u32) void {
46 | const ptr = @intToPtr(*volatile u32, self.base_addr + @enumToInt(reg));
47 | ptr.* = value;
48 | }
49 |
50 | pub fn setRegister(self: Gpio, reg: Reg, x: Pin, val: u1) void {
51 | const regVal = self.readWord(reg);
52 |
53 | const mask = @as(u32, 1) << x;
54 | if (val == 0) {
55 | self.writeWord(reg, regVal & ~mask);
56 | } else {
57 | self.writeWord(reg, regVal | mask);
58 | }
59 | }
60 |
61 | // Configure a GPIO pin as IOF controlled (instead of software controlled).
62 | pub fn setIOFCtrl(self: Gpio, x: Pin, select: u1) void {
63 | // Select one of the two HW-Driven functions.
64 | self.setRegister(Reg.iof_sel, x, select);
65 |
66 | // Enable selected HW-Driven function.
67 | self.setRegister(Reg.iof_en, x, 1);
68 | }
69 |
70 | pub fn set(self: Gpio, x: Pin, v: u1) void {
71 | self.setRegister(Reg.output_val, x, v);
72 | }
73 |
74 | pub fn init(self: Gpio, x: Pin, mode: Mode) void {
75 | switch (mode) {
76 | Mode.IN => {
77 | self.setRegister(Reg.input, x, 1);
78 | self.setRegister(Reg.output_en, x, 0);
79 | self.setRegister(Reg.pue, x, 0);
80 | },
81 | Mode.OUT => {
82 | self.setRegister(Reg.input, x, 0);
83 | self.setRegister(Reg.output_en, x, 1);
84 | self.setRegister(Reg.pue, x, 0);
85 | },
86 | }
87 |
88 | // Disable HW-driven functions for Pin
89 | self.setRegister(Reg.iof_en, x, 0);
90 | self.setRegister(Reg.iof_sel, x, 0);
91 | }
92 | };
93 |
--------------------------------------------------------------------------------
/src/init.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2020-2021 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public License
14 | // along with this program. If not, see .
15 |
16 | const console = @import("console.zig");
17 | const periph = @import("periph.zig");
18 | const main = @import("main.zig");
19 | const StackTrace = @import("std").builtin.StackTrace;
20 |
21 | // Bitmasks for modifying mcause CSR
22 | const MCAUSE_EXPCODE = 0x0fff;
23 | const MCAUSE_INT = 0x80000000;
24 |
25 | // Exception codes
26 | const EXP_BREAKPOINT = 3;
27 |
28 | pub fn panic(msg: []const u8, error_return_trace: ?*StackTrace) noreturn {
29 | _ = error_return_trace; // unused
30 |
31 | // Copied from the default_panic implementation
32 | @setCold(true);
33 |
34 | // Write panic message, unbuffered to standard out
35 | console.print("PANIC: {s}\n", .{msg});
36 |
37 | @breakpoint();
38 | while (true) {}
39 | }
40 |
41 | export fn level1IRQHandler() void {
42 | const mcause = asm ("csrr %[ret], mcause"
43 | : [ret] "=r" (-> u32)
44 | );
45 |
46 | const expcode: u32 = mcause & MCAUSE_EXPCODE;
47 | if ((mcause & MCAUSE_INT) == MCAUSE_INT) {
48 | periph.plic0.invokeHandler();
49 | } else {
50 | if (expcode == EXP_BREAKPOINT) {
51 | while (true) {}
52 | }
53 | @panic("unexpected trap");
54 | }
55 | }
56 |
57 | export fn init() void {
58 | periph.init();
59 | console.print("Booting zig-riscv-embedded...\n", .{});
60 |
61 | main.main() catch |err| {
62 | @panic(@errorName(err));
63 | };
64 | }
65 |
--------------------------------------------------------------------------------
/src/irq.S:
--------------------------------------------------------------------------------
1 | // Based on https://github.com/agra-uni-bremen/riscv-vp/blob/f6e95b370cb6eab7024f1be26a2b8a8ae2c916be/sw/simple-sensor/bootstrap.S
2 | //
3 | // Copyright © 2017-2018, Group of Computer Architecture, University of Bremen
4 | //
5 | // Permission is hereby granted, free of charge, to any person obtaining a
6 | // copy of this software and associated documentation files (the
7 | // "Software"), to deal in the Software without restriction, including
8 | // without limitation the rights to use, copy, modify, merge, publish,
9 | // distribute, sublicense, and/or sell copies of the Software, and to
10 | // permit persons to whom the Software is furnished to do so, subject to
11 | // the following conditions:
12 | //
13 | // The above copyright notice and this permission notice shall be included
14 | // in all copies or substantial portions of the Software.
15 | //
16 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 | // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 | // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 | // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 | // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 | // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 | // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 |
24 | .globl register_handler
25 | .globl lvl0_handler
26 |
27 | lvl0_handler:
28 | # Store all register values on the stack
29 | addi sp, sp, -4 * 32
30 | sw x1, 0x0(sp)
31 | sw x4, 3 * 4(sp)
32 | sw x5, 4 * 4(sp)
33 | sw x6, 5 * 4(sp)
34 | sw x7, 6 * 4(sp)
35 | sw x10, 9 * 4(sp)
36 | sw x11, 10 * 4(sp)
37 | sw x12, 11 * 4(sp)
38 | sw x13, 12 * 4(sp)
39 | sw x14, 13 * 4(sp)
40 | sw x15, 14 * 4(sp)
41 | sw x16, 15 * 4(sp)
42 | sw x17, 16 * 4(sp)
43 | sw x28, 27 * 4(sp)
44 | sw x29, 28 * 4(sp)
45 | sw x30, 29 * 4(sp)
46 | sw x31, 30 * 4(sp)
47 |
48 | # Jump to the level 1 trap handler written in Zig
49 | jal level1IRQHandler
50 |
51 | # Load all register values from the stack and return
52 | lw x1, 0x0(sp)
53 | lw x4, 3 * 4(sp)
54 | lw x5, 4 * 4(sp)
55 | lw x6, 5 * 4(sp)
56 | lw x7, 6 * 4(sp)
57 | lw x10, 9 * 4(sp)
58 | lw x11, 10 * 4(sp)
59 | lw x12, 11 * 4(sp)
60 | lw x13, 12 * 4(sp)
61 | lw x14, 13 * 4(sp)
62 | lw x15, 14 * 4(sp)
63 | lw x16, 15 * 4(sp)
64 | lw x17, 16 * 4(sp)
65 | lw x28, 27 * 4(sp)
66 | lw x29, 28 * 4(sp)
67 | lw x30, 29 * 4(sp)
68 | lw x31, 30 * 4(sp)
69 | addi sp, sp, 4 * 32
70 | mret
71 |
72 | register_handler:
73 | # Use lvl0_handler as the trap handler
74 | la t0, lvl0_handler
75 | csrw mtvec, t0
76 |
77 | # Enable machine external interrupts (MIE bit)
78 | li t1, 0x800
79 | csrw mie, t1
80 |
81 | # Globally enable machine mode interrupts (MIE bit)
82 | li t1, 0x8
83 | csrw mstatus, t1
84 |
85 | # Return
86 | ret
87 |
--------------------------------------------------------------------------------
/src/main.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public License
14 | // along with this program. If not, see .
15 |
16 | const console = @import("console.zig");
17 | const slipmux = @import("slipmux.zig");
18 | const periph = @import("periph.zig");
19 | const zoap = @import("zoap");
20 | const codes = zoap.codes;
21 |
22 | const resources = &[_]zoap.Resource{
23 | .{ .path = "about", .handler = about },
24 | .{ .path = "on", .handler = ledOn },
25 | .{ .path = "off", .handler = ledOff },
26 | };
27 | var dispatcher = zoap.Dispatcher{
28 | .resources = resources,
29 | };
30 |
31 | pub fn about(resp: *zoap.Response, req: *zoap.Request) codes.Code {
32 | if (!req.header.code.equal(codes.GET))
33 | return codes.BAD_METHOD;
34 |
35 | console.print("[coap] Received /about request\n", .{});
36 |
37 | const w = resp.payloadWriter();
38 | w.writeAll("zig-riscv-embedded licensed under AGPL3+") catch |err| {
39 | console.print("[coap] error in /about: {s}\n", .{@errorName(err)});
40 | return codes.INTERNAL_ERR;
41 | };
42 |
43 | return codes.CONTENT;
44 | }
45 |
46 | pub fn ledOn(resp: *zoap.Response, req: *zoap.Request) codes.Code {
47 | _ = resp; // Don't change response abort from code
48 |
49 | if (!req.header.code.equal(codes.PUT))
50 | return codes.BAD_METHOD;
51 |
52 | console.print("[coap] Turning LED on\n", .{});
53 | periph.gpio0.set(periph.led0, 0);
54 |
55 | return codes.CHANGED;
56 | }
57 |
58 | pub fn ledOff(resp: *zoap.Response, req: *zoap.Request) codes.Code {
59 | _ = resp; // Don't change response abort from code
60 |
61 | if (!req.header.code.equal(codes.PUT))
62 | return codes.BAD_METHOD;
63 |
64 | console.print("[coap] Turning LED off\n", .{});
65 | periph.gpio0.set(periph.led0, 1);
66 |
67 | return codes.CHANGED;
68 | }
69 |
70 | pub fn coapHandler(req: *zoap.Request) void {
71 | console.print("[coap] Incoming request\n", .{});
72 | var resp = dispatcher.dispatch(req) catch |err| {
73 | console.print("[coap] Dispatch failed: {s}\n", .{@errorName(err)});
74 | return;
75 | };
76 |
77 | const ftype = slipmux.FrameType.coap;
78 | var frame = periph.slipmux.newFrame(ftype);
79 |
80 | try frame.writer().writeAll(resp.marshal());
81 | frame.close();
82 | }
83 |
84 | pub fn main() !void {
85 | try periph.slipmux.registerHandler(coapHandler);
86 | console.print("Waiting for incoming CoAP packets over UART0...\n", .{});
87 | }
88 |
--------------------------------------------------------------------------------
/src/periph.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public License
14 | // along with this program. If not, see .
15 |
16 | const gpio = @import("gpio.zig");
17 | const plic = @import("plic.zig");
18 | const uart = @import("uart.zig");
19 | const smux = @import("slipmux.zig");
20 |
21 | // Addresses of FE310 peripherals.
22 | const UART0_CTRL_ADDR: usize = 0x10013000;
23 | const UART1_CTRL_ADDR: usize = 0x10023000;
24 | const PLIC_CTRL_ADDR: usize = 0x0C000000;
25 | const GPIO_CTRL_ADDR: usize = 0x10012000;
26 |
27 | // LEDs.
28 | pub const led0 = gpio.pin(0, 22);
29 | pub const led1 = gpio.pin(0, 19);
30 | pub const led2 = gpio.pin(0, 21);
31 |
32 | pub const gpio0 = gpio.Gpio{
33 | .base_addr = GPIO_CTRL_ADDR,
34 | };
35 | pub const plic0 = plic.Plic{
36 | .base_addr = PLIC_CTRL_ADDR,
37 | };
38 |
39 | const uart0 = uart.Uart{
40 | .base_addr = UART0_CTRL_ADDR,
41 | .rx_pin = gpio.pin(0, 16),
42 | .tx_pin = gpio.pin(0, 17),
43 | .irq = 3,
44 | };
45 | var slip0 = smux.Slip{
46 | .uart = &uart0,
47 | .plic = &plic0,
48 | };
49 | pub var slipmux = smux.SlipMux{
50 | .slip = &slip0,
51 | };
52 |
53 | pub fn init() void {
54 | plic0.init();
55 | uart0.init(gpio0, .{ .tx = true, .rx = true });
56 |
57 | gpio0.init(led0, gpio.Mode.OUT);
58 | gpio0.init(led1, gpio.Mode.OUT);
59 | gpio0.init(led2, gpio.Mode.OUT);
60 |
61 | gpio0.set(led0, 1);
62 | gpio0.set(led1, 1);
63 | gpio0.set(led2, 1);
64 | }
65 |
--------------------------------------------------------------------------------
/src/plic.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2020 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public License
14 | // along with this program. If not, see .
15 |
16 | const console = @import("console.zig");
17 |
18 | // Type alias for IRQ values, largest possible IRQ on the FE310 is
19 | // 52 (see INTERRUPT_SOURCES below), thus representable by a u6.
20 | pub const Irq = u6;
21 |
22 | // Type alias for PLIC interrupt handler functions.
23 | pub const Handler = fn (args: ?*anyopaque) void;
24 |
25 | pub const Plic = struct {
26 | base_addr: usize,
27 |
28 | // Offsets for memory mapped PLIC control registers.
29 | const PLIC_PRIO_OFF: usize = 0x0000;
30 | const PLIC_PENDING_OFF: usize = 0x1000;
31 | const PLIC_ENABLE_OFF: usize = 0x2000;
32 | const PLIC_CONTEXT_OFF: usize = 0x200000;
33 |
34 | // Amount of interrupt sources supported by plic.
35 | const INTERRUPT_SOURCES: Irq = 52;
36 |
37 | // TODO: Get rid of anyopaque in the long run.
38 | var irq_handlers = [_]?Handler{null} ** INTERRUPT_SOURCES;
39 | var irq_contexts = [_]?*anyopaque{null} ** INTERRUPT_SOURCES;
40 |
41 | pub fn setThreshold(self: Plic, threshold: u3) void {
42 | const plic_thres = @intToPtr(*volatile u32, self.base_addr + PLIC_CONTEXT_OFF);
43 | plic_thres.* = threshold;
44 | }
45 |
46 | fn setPriority(self: Plic, irq: Irq, prio: u3) void {
47 | // Set PLIC priority for IRQ
48 | const plic_prio = @intToPtr(*volatile u32, self.base_addr +
49 | PLIC_PRIO_OFF + (@as(u32, irq) * @sizeOf(u32)));
50 | plic_prio.* = @as(u32, prio);
51 | }
52 |
53 | fn setEnable(self: Plic, irq: Irq, enable: bool) void {
54 | const idx = irq / 32;
55 |
56 | const enable_addr: usize = self.base_addr + PLIC_ENABLE_OFF;
57 | const plic_enable = @intToPtr(*volatile u32, enable_addr + (idx * @sizeOf(u32)));
58 |
59 | const off = @intCast(u5, irq % 32);
60 | if (enable) {
61 | plic_enable.* |= @intCast(u32, 1) << off;
62 | } else {
63 | plic_enable.* &= ~(@intCast(u32, 1) << off);
64 | }
65 | }
66 |
67 | pub fn registerHandler(self: Plic, irq: Irq, func: Handler, ctx: ?*anyopaque) !void {
68 | if (irq >= irq_handlers.len)
69 | return error.OutOfBounds;
70 |
71 | irq_handlers[irq] = func;
72 | irq_contexts[irq] = ctx;
73 |
74 | self.setPriority(irq, 1);
75 | self.setEnable(irq, true);
76 | }
77 |
78 | pub fn invokeHandler(self: Plic) void {
79 | const claim_reg = @intToPtr(*volatile u32, self.base_addr + PLIC_CONTEXT_OFF + @sizeOf(u32));
80 | const irq = @intCast(Irq, claim_reg.*);
81 |
82 | if (irq_handlers[irq]) |handler|
83 | handler(irq_contexts[irq]);
84 |
85 | // Mark interrupt as completed
86 | claim_reg.* = irq;
87 | }
88 |
89 | pub fn init(self: Plic) void {
90 | // Threshold is uninitialized by default.
91 | self.setThreshold(0);
92 |
93 | var i: Irq = 1;
94 | while (i <= INTERRUPT_SOURCES) : (i += 1) {
95 | self.setEnable(i, false);
96 | self.setPriority(i, 0);
97 | }
98 | }
99 | };
100 |
--------------------------------------------------------------------------------
/src/slipmux.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2021 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public License
14 | // along with this program. If not, see .
15 |
16 | const zoap = @import("zoap");
17 | const crc = @import("crc.zig");
18 | const std = @import("std");
19 | const console = @import("console.zig");
20 |
21 | const Plic = @import("plic.zig").Plic;
22 | const Uart = @import("uart.zig").Uart;
23 |
24 | const FrameHandler = fn (ctx: ?*anyopaque, buf: []const u8) void;
25 | const CoapHandler = fn (req: *zoap.Request) void;
26 |
27 | pub const Slip = struct {
28 | uart: *const Uart,
29 | plic: *const Plic,
30 | handler: ?FrameHandler = null,
31 | context: ?*anyopaque = null,
32 | rcvbuf: [MTU]u8 = undefined,
33 | rcvpos: usize = 0,
34 | prev_esc: bool = false,
35 |
36 | // SLIP control bytes from RFC 1055.
37 | const END: u8 = 0o300;
38 | const ESC: u8 = 0o333;
39 | const ESC_END: u8 = 0o334;
40 | const ESC_ESC: u8 = 0o335;
41 |
42 | // SLIP (as defined in RFC 1055) doesn't specify an MTU.
43 | const MTU: u32 = 1500;
44 |
45 | fn writeByte(self: *Slip, byte: u8) void {
46 | self.rcvbuf[self.rcvpos] = byte;
47 | self.rcvpos += 1;
48 | }
49 |
50 | fn handleByte(self: *Slip, byte: u8) !void {
51 | if (self.rcvpos >= self.rcvbuf.len) {
52 | self.prev_esc = false;
53 | return error.FrameTooLarge;
54 | }
55 |
56 | switch (byte) {
57 | ESC => {
58 | self.prev_esc = true;
59 | return;
60 | },
61 | END => {
62 | if (self.handler != null)
63 | self.handler.?(self.context, self.rcvbuf[0..self.rcvpos]);
64 | self.rcvpos = 0;
65 | },
66 | ESC_END, ESC_ESC => {
67 | var c: u8 = undefined;
68 | if (self.prev_esc) {
69 | switch (byte) {
70 | ESC_END => c = END,
71 | ESC_ESC => c = ESC,
72 | else => return error.UnknownEscapeSequence,
73 | }
74 | } else {
75 | c = byte;
76 | }
77 |
78 | self.writeByte(c);
79 | },
80 | else => {
81 | self.writeByte(byte);
82 | },
83 | }
84 |
85 | self.prev_esc = false;
86 | }
87 |
88 | fn rxIrqHandler(self: *Slip) !void {
89 | while (self.uart.readByte()) |byte| {
90 | try self.handleByte(byte);
91 | }
92 | }
93 |
94 | fn irqHandler(ctx: ?*anyopaque) void {
95 | var self: *Slip = @ptrCast(*Slip, @alignCast(@alignOf(*Slip), ctx.?));
96 |
97 | const ip = self.uart.readIp();
98 | if (ip.rxwm) {
99 | rxIrqHandler(self) catch {
100 | @panic("rx handler failed");
101 | };
102 | }
103 | }
104 |
105 | pub fn registerHandler(self: *Slip, func: FrameHandler, ctx: ?*anyopaque) !void {
106 | // Enable RX interrupt, dissable TX interrupt.
107 | self.uart.writeIe(false, true);
108 |
109 | self.handler = func;
110 | self.context = ctx;
111 |
112 | try self.plic.registerHandler(self.uart.irq, irqHandler, self);
113 | }
114 | };
115 |
116 | pub const FrameType = enum(u8) {
117 | diagnostic = 0x0a,
118 | coap = 0xa9,
119 | };
120 |
121 | pub const Frame = struct {
122 | slip: *const Slip,
123 | ftype: FrameType,
124 | csum: crc.Incremental,
125 |
126 | const WriteError = error{};
127 | const FrameWriter = std.io.Writer(*Frame, WriteError, write);
128 |
129 | fn init(slip: *const Slip, ftype: FrameType) Frame {
130 | var frame = Frame{
131 | .slip = slip,
132 | .ftype = ftype,
133 | .csum = .{},
134 | };
135 |
136 | frame.pushByte(@enumToInt(ftype));
137 | return frame;
138 | }
139 |
140 | fn pushByteRaw(self: *Frame, byte: u8) void {
141 | const uart = self.slip.uart;
142 |
143 | // Busy wait for TX fifo to empty.
144 | while (uart.isTxFull()) {}
145 | uart.writeByte(byte);
146 | }
147 |
148 | fn pushByte(self: *Frame, byte: u8) void {
149 | self.pushByteRaw(byte);
150 | if (self.ftype == FrameType.coap)
151 | self.csum.add(byte);
152 | }
153 |
154 | fn write(self: *Frame, data: []const u8) WriteError!usize {
155 | for (data) |c| {
156 | switch (c) {
157 | Slip.END => {
158 | self.pushByte(Slip.ESC);
159 | self.pushByte(Slip.ESC_END);
160 | },
161 | Slip.ESC => {
162 | self.pushByte(Slip.ESC);
163 | self.pushByte(Slip.ESC_ESC);
164 | },
165 | else => {
166 | self.pushByte(c);
167 | },
168 | }
169 | }
170 |
171 | return data.len;
172 | }
173 |
174 | pub fn close(self: *Frame) void {
175 | if (self.ftype == FrameType.coap) {
176 | var fcs16 = self.csum.csum();
177 | fcs16 ^= 0xffff; // complement
178 |
179 | // XXX: Use @truncate instead?
180 | self.pushByteRaw(@intCast(u8, fcs16 & @as(u16, 0x00ff)));
181 | self.pushByteRaw(@intCast(u8, fcs16 >> 8 & @as(u16, 0x00ff)));
182 | }
183 |
184 | self.pushByteRaw(Slip.END);
185 | }
186 |
187 | pub fn writer(self: *Frame) FrameWriter {
188 | return .{ .context = self };
189 | }
190 | };
191 |
192 | pub const SlipMux = struct {
193 | slip: *Slip,
194 | handler: ?CoapHandler = null,
195 |
196 | fn handleCoAP(self: *SlipMux, buf: []const u8) !void {
197 | // 1 byte (frame type) + 4 byte (coap message) + 2 byte CRC
198 | if (buf.len <= 7)
199 | return error.CoAPFrameTooShort;
200 | if (!crc.validCsum(buf))
201 | return error.InvalidChecksum;
202 |
203 | // Strip frame identifier and 16-bit CRC FCS.
204 | const msgBuf = buf[1..(buf.len - @sizeOf(u16))];
205 |
206 | var req = try zoap.Request.init(msgBuf);
207 | self.handler.?(&req);
208 | }
209 |
210 | fn dispatchFrame(self: *SlipMux, buf: []const u8) !void {
211 | switch (buf[0]) {
212 | @enumToInt(FrameType.diagnostic) => {
213 | return error.NoDiagnosticSupport;
214 | },
215 | @enumToInt(FrameType.coap) => {
216 | try self.handleCoAP(buf);
217 | },
218 | else => {
219 | return error.UnsupportedFrameType;
220 | },
221 | }
222 | }
223 |
224 | fn handleFrame(ctx: ?*anyopaque, buf: []const u8) void {
225 | var self: *SlipMux = @ptrCast(*SlipMux, @alignCast(@alignOf(*SlipMux), ctx.?));
226 | if (buf.len == 0)
227 | return;
228 |
229 | self.dispatchFrame(buf) catch |err| {
230 | console.print("handleFrame failed: {s}\n", .{@errorName(err)});
231 | };
232 | }
233 |
234 | pub fn newFrame(self: *SlipMux, ftype: FrameType) Frame {
235 | return Frame.init(self.slip, ftype);
236 | }
237 |
238 | pub fn registerHandler(self: *SlipMux, handler: CoapHandler) !void {
239 | self.handler = handler;
240 | try self.slip.registerHandler(handleFrame, self);
241 | }
242 | };
243 |
--------------------------------------------------------------------------------
/src/start.S:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2017 JP Bonn, Ken Rabold
3 | *
4 | * This file is subject to the terms and conditions of the GNU Lesser
5 | * General Public License v2.1. See the file LICENSE in the top level
6 | * directory for more details.
7 | */
8 |
9 | .section .init
10 | .globl _start
11 | .type _start,@function
12 |
13 | _start:
14 | .cfi_startproc
15 | .cfi_undefined ra
16 | .option push
17 | .option norelax
18 | la gp, __global_pointer$
19 | .option pop
20 | la sp, __StackTop
21 |
22 |
23 | /* Load data section */
24 | la a0, _data_lma
25 | la a1, _data
26 | la a2, _edata
27 | bgeu a1, a2, 2f
28 | 1:
29 | lw t0, (a0)
30 | sw t0, (a1)
31 | addi a0, a0, 4
32 | addi a1, a1, 4
33 | bltu a1, a2, 1b
34 | 2:
35 |
36 | /* Clear bss section */
37 | la a0, __bss_start
38 | la a1, _end
39 | bgeu a0, a1, 2f
40 | 1:
41 | sw zero, (a0)
42 | addi a0, a0, 4
43 | bltu a0, a1, 1b
44 | 2:
45 | call register_handler
46 | call clock_init
47 | call init
48 |
49 | /* Loop forever */
50 | 1:
51 | wfi
52 | j 1b
53 |
54 | .cfi_endproc
55 |
--------------------------------------------------------------------------------
/src/uart.zig:
--------------------------------------------------------------------------------
1 | // Copyright © 2020-2021 Sören Tempel
2 | //
3 | // This program is free software: you can redistribute it and/or modify
4 | // it under the terms of the GNU Affero General Public License as
5 | // published by the Free Software Foundation, either version 3 of the
6 | // License, or (at your option) any later version.
7 | //
8 | // This program is distributed in the hope that it will be useful, but
9 | // WITHOUT ANY WARRANTY; without even the implied warranty of
10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 | // Affero General Public License for more details.
12 | //
13 | // You should have received a copy of the GNU Affero General Public
14 | // License along with this program. If not, see .
15 |
16 | const gpio = @import("gpio.zig");
17 | const plic = @import("plic.zig");
18 |
19 | // TODO: Extract this value using the PRCI.
20 | const CLK_FREQ = 16 * 1000 * 1000; // 16 MHZ
21 |
22 | pub const ConfFlags = struct {
23 | tx: bool,
24 | rx: bool,
25 | cnt: u3 = 0,
26 | baud: u32 = 115200,
27 | };
28 |
29 | fn ctrlCount(watermark: u3) u32 {
30 | return (@as(u32, watermark) & 0x07) << 16;
31 | }
32 |
33 | pub const Uart = struct {
34 | base_addr: usize,
35 | rx_pin: gpio.Pin,
36 | tx_pin: gpio.Pin,
37 | irq: plic.Irq,
38 |
39 | const Reg = enum(usize) {
40 | txfifo = 0x00,
41 | rxfifo = 0x04,
42 | txctrl = 0x08,
43 | rxctrl = 0x0c,
44 | ie = 0x10,
45 | ip = 0x14,
46 | div = 0x18,
47 | };
48 |
49 | pub const ie = struct {
50 | txwm: bool,
51 | rxwm: bool,
52 | };
53 |
54 | pub const FIFO_DEPTH: usize = 8;
55 | pub const TXCTRL_ENABLE: u32 = 1;
56 | pub const RXCTRL_ENABLE: u32 = 1;
57 | pub const EMPTY_MASK: u32 = 1 << 31; // for txdata/rxdata
58 | pub const IE_TXWM: u32 = 1 << 0;
59 | pub const IE_RXWM: u32 = 1 << 1;
60 |
61 | fn writeWord(self: Uart, reg: Reg, value: u32) void {
62 | const ptr = @intToPtr(*volatile u32, self.base_addr + @enumToInt(reg));
63 | ptr.* = value;
64 | }
65 |
66 | fn readWord(self: Uart, reg: Reg) u32 {
67 | const ptr = @intToPtr(*volatile u32, self.base_addr + @enumToInt(reg));
68 | return ptr.*;
69 | }
70 |
71 | pub fn confTx(self: Uart, watermark: u3) void {
72 | self.writeWord(Reg.txctrl, TXCTRL_ENABLE | ctrlCount(watermark));
73 | }
74 |
75 | pub fn confRx(self: Uart, watermark: u3) void {
76 | self.writeWord(Reg.rxctrl, RXCTRL_ENABLE | ctrlCount(watermark));
77 | }
78 |
79 | pub fn readIp(self: Uart) ie {
80 | const r = self.readWord(Reg.ip);
81 | return .{
82 | .txwm = (r & IE_TXWM) != 0,
83 | .rxwm = (r & IE_RXWM) != 0,
84 | };
85 | }
86 |
87 | pub fn writeIe(self: Uart, txwm: bool, rxwm: bool) void {
88 | var r: u32 = 0;
89 | if (txwm)
90 | r |= IE_TXWM;
91 | if (rxwm)
92 | r |= IE_RXWM;
93 |
94 | self.writeWord(Reg.ie, r);
95 | }
96 |
97 | pub fn writeByte(self: Uart, value: u8) void {
98 | self.writeWord(Reg.txfifo, value);
99 | }
100 |
101 | fn drainInput(self: Uart) void {
102 | // Read until self.readByte() returns null
103 | while (self.readByte()) |_| {}
104 | }
105 |
106 | pub fn readByte(self: Uart) ?u8 {
107 | const rxdata = self.readWord(Reg.rxfifo);
108 | if ((rxdata & EMPTY_MASK) != 0)
109 | return null;
110 | return @truncate(u8, rxdata);
111 | }
112 |
113 | pub fn isTxFull(self: Uart) bool {
114 | const txdata = self.readWord(Reg.txfifo);
115 | return (txdata & EMPTY_MASK) != 0;
116 | }
117 |
118 | pub fn init(self: Uart, ugpio: gpio.Gpio, conf: ConfFlags) void {
119 | // Enable the UART at the given baud rate
120 | self.writeWord(Reg.div, CLK_FREQ / conf.baud);
121 |
122 | if (conf.tx)
123 | self.confTx(conf.cnt);
124 | if (conf.rx)
125 | self.confRx(conf.cnt);
126 |
127 | if (conf.tx)
128 | ugpio.setIOFCtrl(self.tx_pin, 0);
129 | if (conf.rx)
130 | ugpio.setIOFCtrl(self.rx_pin, 0);
131 |
132 | if (conf.rx)
133 | self.drainInput();
134 | }
135 | };
136 |
--------------------------------------------------------------------------------