Repository: wagiminator/CH32X035-USB-PD-Tester Branch: main Commit: d835a923228e Files: 26 Total size: 434.9 KB Directory structure: gitextract_9q4k3lku/ ├── 3dprint/ │ ├── USB_PD_Tester_case.FCStd │ ├── USB_PD_Tester_case_bottom.stl │ ├── USB_PD_Tester_case_button.stl │ └── USB_PD_Tester_case_top.stl ├── LICENSE ├── README.md ├── hardware/ │ └── USB_PD_Tester_BOM.tsv └── software/ └── pd_tester/ ├── bin/ │ └── pd_tester.hex ├── config.h ├── ld/ │ └── ch32x035.ld ├── makefile ├── platformio.ini └── src/ ├── ch32x035.h ├── gpio.h ├── i2c_soft.c ├── i2c_soft.h ├── main.c ├── print.c ├── print.h ├── ssd1306_txt.c ├── ssd1306_txt.h ├── system.c ├── system.h ├── usbpd.h ├── usbpd_sink.c └── usbpd_sink.h ================================================ FILE CONTENTS ================================================ ================================================ FILE: LICENSE ================================================ This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA. ================================================ FILE: README.md ================================================ # USB PD Tester The USB PD Tester is a monitoring and triggering device designed for USB Power Delivery. It allows you to test a wide range of USB Type-C PD power supplies and their corresponding cables. This tool not only displays the capabilities of the power supply on an OLED but also enables you to select one of the available fixed or programmable voltages for output on the screw terminal. With this, it can also be used to power your projects with various selectable voltages and high currents, serving as a versatile variable power supply. The USB PD Tester is built around the inexpensive (30 cents at the time of writing) and user-friendly CH32X035 RISC-V microcontroller, which comes with integrated USB 2.0, USB PD 2.0/3.0, and USB Type-C hardware support. - Project video (Youtube): https://youtu.be/wqLiRnbcISo ![USB_PD_Tester_pic1.jpg](https://raw.githubusercontent.com/wagiminator/CH32X035-USB-PD-Tester/main/documentation/USB_PD_Tester_pic1.jpg) # USB Power Delivery USB Power Delivery (USB PD) is a protocol that extends the capabilities of standard USB connections, allowing for faster charging and power delivery between devices. USB PD operates through negotiation between the source and sink devices. They exchange information through PDOs, determining the optimal power level for charging or powering the sink device. PPS enhances flexibility by allowing dynamic adjustments, and CC1 and CC2 are the communication channels involved in the negotiation process: - Source: The source is a device that provides power, such as a charger or power bank. It can negotiate with the connected device to determine the optimal power level. Here the USB PD power supply to be tested acts as the source. - Sink: The sink is a device that consumes power, like a smartphone or laptop. It communicates with the source to negotiate the power it needs for charging or other operations. Here the USB PD Tester acts as the sink. - PDO (Power Delivery Object): A PDO is a data structure exchanged between the source and sink during negotiation. It contains information about the available voltage and current levels. The devices negotiate and agree on a mutually supported PDO for power delivery. - PPS (Programmable Power Supply): PPS is a feature in USB PD that allows dynamic adjustment of the voltage and current levels during operation. It enables more flexible power delivery based on the specific needs of the connected device. - CC1 and CC2 (Configuration Channel 1 and 2): These are the communication channels used by USB Type-C connectors for negotiating power delivery. CC1 and CC2 lines carry information about the capabilities of the devices and facilitate negotiation. # Hardware ## Schematic ![USB_PD_Tester_wiring.png](https://raw.githubusercontent.com/wagiminator/CH32X035-USB-PD-Tester/main/documentation/USB_PD_Tester_wiring.png) ## CH32X035 F7P6 32-bit RISC-V USB Microcontroller CH32X035F7P6 is a low-cost microcontroller that utilizes the QingKe 32-bit RISC-V4C core, supporting the RV32IMAC instruction set along with self-extending instructions. This microcontroller comes with a built-in USB PHY, supporting USB2.0 full-speed device functions and a USB PD PHY with source and sink capabilities. It features a programmable protocol I/O controller (PIOC), an operational amplifier (OPA) with programmable gain (PGA), an analog comparator (CMP), a 12-bit analog-to-digital converter (ADC), an 11-channel touch-key controller, 3 groups of USART, I2C, SPI, multiple timers, and various other peripheral resources. The device can operate at clock frequencies of up to 48MHz and is compatible with a supply voltage range of 2.0V to 5.5V. The CH32X035F7P6 includes 48KB of flash, 20KB of SRAM, and an embedded USB bootloader. ## 78L05 Voltage Regulator The 78L05 is a simple and inexpensive voltage regulator that can convert input voltages up to 30V to an output voltage of 5V with an output current of up to 100mA and a dropout voltage of 1.7V. The 78L05 supplies all elements of the circuit with up to 5V. ## SSD1306 OLED Display Module A low-cost SSD1306 4-pin I2C 128x64 pixels 0.96-inch OLED module is used as the display device. Make sure to acquire one with the correct pinout! ![USB_PD_Tester_pic2.jpg](https://raw.githubusercontent.com/wagiminator/CH32X035-USB-PD-Tester/main/documentation/USB_PD_Tester_pic2.jpg) # Software ## USB Bootloader ### Installing Drivers for the Bootloader On Linux you do not need to install a driver. However, by default Linux will not expose enough permission to upload your code with the USB bootloader. In order to fix this, open a terminal and run the following commands: ``` echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="4348", ATTR{idProduct}=="55e0", MODE="666"' | sudo tee /etc/udev/rules.d/99-ch55x.rules echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="1a86", ATTR{idProduct}=="55e0", MODE="666"' | sudo tee -a /etc/udev/rules.d/99-ch55x.rules sudo udevadm ``` For Windows you can use the [Zadig](https://zadig.akeo.ie/) tool to install the correct driver. Here, click "Options" -> "List All Devices" and select the USB module. Then install the libusb-win32 driver. To do this, the board must be connected and the microcontroller must be in bootloader mode. ### Entering Bootloader Mode The bootloader must be started manually for new uploads. To do this, the board must first be disconnected from the USB port. Now press the BOOT button and keep it pressed while reconnecting the board to the USB port of your PC. The chip now starts in bootloader mode, the BOOT button can be released and new firmware can be uploaded via USB within the next couple of seconds. ## Compiling and Uploading Firmware using the Makefile ### Linux Install the toolchain (GCC compiler, Python3, and chprog): ``` sudo apt install build-essential libnewlib-dev gcc-riscv64-unknown-elf sudo apt install python3 python3-pip pip install chprog ``` Open a terminal and navigate to the folder with the *makefile*. Press the BOOT button and keep it pressed while connecting the board to the USB port of your PC. Run the following command to compile and upload: ``` make flash ``` ### Other Operating Systems Follow the instructions on [CNLohr's ch32v003fun page](https://github.com/cnlohr/ch32v003fun/wiki/Installation) to set up the toolchain on your respective operating system (for Windows, use WSL). Also, install [Python3](https://www.pythontutorial.net/getting-started/install-python/) and [chprog](https://pypi.org/project/chprog/). Compile and upload with "make flash". Note that I only have Debian-based Linux and have not tested it on other operating systems. ## Compiling and Uploading Firmware using PlatformIO - Install [PlatformIO](https://platformio.org) and [platform-ch32v](https://github.com/Community-PIO-CH32V/platform-ch32v). Follow [these instructions](https://pio-ch32v.readthedocs.io/en/latest/installation.html) to do so. Linux/Mac users may also need to install [pyenv](https://realpython.com/intro-to-pyenv). - Click on "Open Project" and select the firmware folder with the *platformio.ini* file. - Press the BOOT button and keep it pressed while connecting the board to the USB port of your PC. Then click "Upload". ## Uploading pre-compiled Firmware Binary WCH offers the free but closed-source software [WCHISPTool](https://www.wch.cn/downloads/WCHISPTool_Setup_exe.html) to upload firmware with Windows via the USB bootloader. Press the BOOT button and keep it pressed while connecting the board to the USB port of your PC. Release the BOOT button, open the *pd_tester.hex* file in the *bin* folder with WCHISPTool and upload it to the microcontroller. If [Python3](https://www.pythontutorial.net/getting-started/install-python/) is installed, you can also use the platform-independent open-source command-line tool [chprog](https://pypi.org/project/chprog/) for uploading: ``` chprog bin/pd_tester.bin ``` # Building Instructions 1. Take the Gerber files (the *zip* file inside the *hardware* folder) and upload them to a PCB (printed circuit board) manufacturer of your choice (e.g., [JLCPCB](https://jlcpcb.com/)). They will use these files to create the circuit board for your device and send it to you. 2. Once you have the PCB, you can start soldering the components onto it. Use the BOM (bill of materials) and schematic as a guide to make sure everything is connected correctly. You can find the corresponding files in the *hardware* folder. Remove the plastic part from the pin header of the OLED, trim the pins, and solder the OLED module flush onto the PCB. Do not solder the screw terminal yet. 3. Print the casing with your 3D printer. You can find the corresponding *stl* files in the *3dprint* folder. 4. Insert the button extensions into the corresponding holes on the top part of the casing. Insert the circuit board and secure it with 4 self-tightening M2x5mm screws. 5. Now solder the screw terminal onto the circuit board. 6. Close the casing with the back panel. 7. Upload the firmware by following the instructions in the previous section (see above). ![USB_PD_Tester_pic3.jpg](https://raw.githubusercontent.com/wagiminator/CH32X035-USB-PD-Tester/main/documentation/USB_PD_Tester_pic3.jpg) # Operating Instructions 1. Connect the USB PD Tester to a USB Type-C PD power supply using a USB-C cable. The available PDOs and their corresponding capabilities are displayed on the OLED. 2. Utilize the UP and DOWN buttons to choose the desired PDO. A left-angle bracket on the right side serves as the selection indicator. Programmable PDOs, indicated by numbers within square brackets, allow precise adjustments of the output voltage in 20mV steps within the specified range. 3. Press the SLCT button to activate the chosen PDO. An asterisk on the left side of the PDO confirms the activation. 4. The selected voltage is now accessible on the screw terminal for further use. ![USB_PD_Tester_pic4.jpg](https://raw.githubusercontent.com/wagiminator/CH32X035-USB-PD-Tester/main/documentation/USB_PD_Tester_pic4.jpg) ![USB_PD_Tester_pic5.jpg](https://raw.githubusercontent.com/wagiminator/CH32X035-USB-PD-Tester/main/documentation/USB_PD_Tester_pic5.jpg) # References, Links and Notes - [EasyEDA Design Files](https://oshwlab.com/wagiminator) - [MCU Templates](https://github.com/wagiminator/MCU-Templates) - [MCU Flash Tools](https://github.com/wagiminator/MCU-Flash-Tools) - [CH32X035 Datasheets](http://www.wch-ic.com/products/CH32X035.html) - [SSD1306 Datasheet](https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf) - [78L05 Datasheet](https://datasheet.lcsc.com/lcsc/2209271730_HX-hengjiaxing-78L05_C5181466.pdf) - [ATtiny814 USB PD Adapter](https://github.com/wagiminator/ATtiny814-USB-PD-Adapter) - [ATtiny412 USB PD Inverter](https://github.com/wagiminator/ATtiny412-USB-PD-Inverter) - [TI Primer on USB PD](https://www.ti.com/lit/wp/slyy109b/slyy109b.pdf) - [CH32X035 F7P6 on Aliexpress](https://aliexpress.com/item/1005006199310724.html) - [128x64 OLED on Aliexpress](https://aliexpress.com/wholesale?SearchText=128+64+0.96+oled) ![USB_PD_Tester_pic6.jpg](https://raw.githubusercontent.com/wagiminator/CH32X035-USB-PD-Tester/main/documentation/USB_PD_Tester_pic6.jpg) # License ![license.png](https://i.creativecommons.org/l/by-sa/3.0/88x31.png) This work is licensed under Creative Commons Attribution-ShareAlike 3.0 Unported License. (http://creativecommons.org/licenses/by-sa/3.0/) ================================================ FILE: software/pd_tester/bin/pd_tester.hex ================================================ :040000006F108024D9 :10000400000000000000000036020000360200007C :1000140000000000360200000000000000000000A4 :10002400360200003602000000000000000000005C :10003400360200000000000036020000000000004C :100044003602000036020000360200000000000004 :1000540036020000360200003602000036020000BC :1000640036020000360200003602000036020000AC :10007400360200003602000036020000360200009C :10008400360200003602000036020000360200008C :10009400360200003602000036020000360200007C :1000A400360200003602000036020000360200006C :1000B400360200003602000036020000360200005C :1000C400360200001E08000036020000360200005E :0C00D40036020000360200003602000078 :1000E000B7F700E09C4737F700E03E951C47898F43 :1000F000E3CE07FE828079714ECE5EC6B70910FF4F :10010000B70B00F122D426D252CC56CA5AC862C4C8 :1001100066C206D64AD0AA84A14A37140140FD1B04 :10012000370C0003B70C0004FD19370B4000370AE9 :1001300030000327048093978401E18733777701A8 :1001400063DA0708336797012320E48003270480DC :100150000945FD1A33773701336767012320E480AF :10016000413703270480860493FAFA0F3377370167 :10017000336747012320E48093F4F40FE39B0AFAEA :1001800003270480B70700F1FD177D8FB707000430 :100190005D8F2320E480832704800945B3F737016E :1001A000B3E767012320F480253F83270480B25002 :1001B0009254B3F93701B3E9490123203481225421 :1001C0000259F249624AD24A424BB24B224C924CFB :1001D000456182803367870185BF411122C437148E :1001E000014003270480B70700F126C29384F7FF7C :1001F000658FB707000306C65D8F2320E48009459D :10020000C53503270480B70710FFFD177D8FB7079B :1002100040005D8F2320E4800945D9358327048081 :1002200037070004B240E58FD98F2320F4802244A1 :1002300092444101828001A09387C18603C7470190 :1002400083C65701158F1377F70F6368A700DC4744 :100250000A053E950355E5FF8280198D1947330540 :10026000E5029C4B3E950355A5FF82809387C1868E :1002700003C7470183C65701158F1377F70F6368CC :10028000A700DC470A053E950355E5FF8280198DDE :1002900019473305E5029C4B3E950355C5FF828007 :1002A0009387C18603C7470183C65701158F13770C :1002B000F70F6368A700DC470A053E950355C5FFA5 :1002C0008280198D19473305E5029C4B3E950355F5 :1002D000E5FF8280B77702401307200A2396E700E4 :1002E0002397E7009387C186A3840700238507002F :1002F00023840700238A0700A38A070023A007009E :1003000023A207001387418323810702D8C7A38153 :1003100007021387C1802382070298CB0547238BEE :10032000E70023800702238DE700A38DE700056720 :1003300013078738239EE700239FE7008280B77763 :10034000024003D70700118B21C303D7E700420700 :100350004183136707012397E700B777024013072C :10036000F0042391E7004205A38207004181239313 :10037000A700A384070003C747001377F70F13678D :1003800037002382E700828003D7C70042074183FA :10039000136707012396E700C9B7797122D426D2E3 :1003A0004AD04ECE52CC56CA9387C18606D65AC87A :1003B0005EC6239F07000569238007029304F00FA0 :1003C0001384C1861309892D93894183138A8181FE :1003D000834B040293FBFB0F63960B00FD1493F415 :1003E000F40F91ECB25022540259F249624AD24AB7 :1003F000424BB24B3335900092544561828037B501 :10040000030013050598E939B777024003D7C70001 :10041000420741831377E7F1420741832396E700C0 :1004200003D7C70042074183136707042396E700F9 :1004300083D6C70003D7E700C2064207418313777C :10044000E7F1420741832397E70003D7E700C18222 :1004500042074183136707042397E70083D7E70028 :100460000947858B99E313F7160083578401854666 :100470008507C207C183231CF4001C406393D70483 :100480006318F70823050400834794009546850701 :1004900093F7F70FA304F4008347940093F7F70F43 :1004A00063F1F602A304040089471CC0B77602403A :1004B00083D70600C207C183ED9BC207C183239087 :1004C000F6001440854763FBD70249E78347840061 :1004D0001547850793F7F70F2304F400834784003B :1004E00093F7F70F637CF700230404002320040034 :1004F000B7E700E03707020023A2E718032B040048 :100500001C402547636FF7088A07CA979C43828778 :100510008947A3040400631CF7028347A40095469F :10052000850793F7F70F2305F4008347A40093F79B :10053000F70FE3F8F6F82305040018C0B776024079 :1005400083D70600C207C18393E747008DBF230509 :10055000040085BF2304040055B7B7E700E0370760 :10056000020023A2E718BD3385471CC01DA85840D0 :100570001C406308F7023777024093077007231186 :10058000F7008347470093F7C70F93E7270023023D :10059000F700B7E700E0A38807423707020023A26D :1005A000E710232264012DB58347340293F7F70F38 :1005B000EDDBA3010402B7E700E0370702000328E0 :1005C000C400144823A2E718A30A0400B70800F0E7 :1005D000370300C0294E9305200319461305400632 :1005E0008347440193F7F70F63EAFB00B7E700E0A6 :1005F0003707020023A2E710914785BF13972B000E :10060000B307EA009C43B3FE170163986E060347E5 :10061000540193DE170193FEFE0F1377F70F330794 :10062000C702B38EAE0236972311D7010347540198 :1006300093DE870093FEFE0F1377F70F3307C70291 :1006400093F7F707B38EAE0236972310D70103470F :100650005401B387B7021377F70F3307C7023697F2 :100660002312F70083475401850793F7F70FA30A76 :10067000F400850B93FBFB0FA5B793FEF73FA9830F :1006800093F7F73FB38ECE034297B387B702231099 :10069000D7012311F700F1BF58401C406300F70C4D :1006A0008347A401834624020565130725009D8A1C :1006B000A606D98E21671307F7E1F98E0347640177 :1006C00093F6F6F313F6F70F0D8B1A07D98E03473F :1006D0004401034854019395C7011377F70F13782A :1006E000F80F330707416355C7088357C4015147C3 :1006F000C207C183B3D7E7021307F5FFF98FA60737 :1007000003475401CD8F834544011377F70F329788 :1007100093F5F50F0D8F19467D173307C702138622 :10072000C180329703574700130620033357C7028F :10073000370600031377F707518F370600FD1306BE :1007400006F8F18FD98F138781842311F700C183B5 :100750002310D7002312F7001945D53683474402EA :1007600093F7F70FE38F07E2230204029547F5BBE7 :100770001307F6FF0A074E9703570700A9473706E6 :1007800000033357F7021377F73F9317A700518FF2 :100790005D8FB367B70045BF8347340293F7F70F08 :1007A000E38107E0A3010402A5471CC0231C040049 :1007B000CDBB85472300F4020347A4018347B4015E :1007C0001377F70F93F7F70F631CF7000357C40174 :1007D0008357E4014207C2074183C183E303F7DC87 :1007E0008347A401094593F7F70FA30DF40083573E :1007F000C401C207C183231FF400230004028347FE :100800002402034764019D8BA6070D8B1A0793E70B :100810007700D98F2394F1841D36A947B9B35D7150 :100820003ED0B77702403AD203C7970086C696C437 :100830009AC29EC022DE26DC2ADA2ED832D636D4E0 :1008400042CE46CC4ACA72C876C67AC47EC21377F4 :10085000070235CB03C7970085460D8B631BD70472 :1008600083D7A7009546C207C18363F4F60403D477 :1008700081841389818493160401C1869384C1867F :1008800063CE060E1D66618E9376F4016310061228 :100890008D476383F60E99476388F6106390E60EE2 :1008A0002382D40283C72402850793F7F70F23819D :1008B000F402377702408347970093F7F70F93E7E7 :1008C0000702A304F700B777024003C79700620747 :1008D0006187635D070403D7C700854642074183EC :1008E0003D9B420741832396E70003D7E700420779 :1008F00041833D9B420741832397E700130770071D :100900002391E70003C747001377C70F136727003A :100910002382E7001387C186A301D70203C797008C :10092000136707F81377F70FA384E70037770240C0 :100930008347970093F7070491CB8347970093F77A :10094000F70F93E70704A304F70069327254B64027 :10095000A64216438643E2545255C2553256A25619 :10096000125782577248E2485249424EB24E224FC5 :10097000924F6161730020309D479CC01305005A5F :10098000EFF00FF6A381040203C7640193579400AC :100990009D8BA6070D8B1A0793E71700D98F094582 :1009A0002310F900693A31B7A147C1BFE398E6FCCB :1009B0000D4798C01357C4001D8B238AE4001357BA :1009C00064000D8B238BE40093878184014771467B :1009D000938681818507E303C7FA03C51700B385B2 :1009E000E60005072380A500F5B7411122C437149E :1009F000014083270480370700F17D17F98F3707FF :100A0000000306C6D98F2320F4800945EFF04FED8F :100A100083270480370710FF7D17F98F37073000D1 :100A2000D98F2320F4802244B2401305800741016E :100A30006FF06FEC411122C426C206C69D47AA84FE :100A4000014463E3B7002E8423849180A3848180D2 :100A5000693F0145EFF02FEA130404FB1375F40F0F :100A6000EFF06FE913F5F400EFF0EFE813D5440071 :100A700013650501EFF02FE82244B2409244410192 :100A80006FF0AFF5411122C44AC006C626C2214408 :100A90001309F00F7D141374F40F63182401B2408E :100AA00022449244024941018280A285014559377E :100AB0002D3F13050004EFF00FE493040008FD142C :100AC000014593F4F40FEFF00FE3F5F8EFF0EFF0DA :100AD000A285014585377DBF011501114205418180 :100AE0004EC603C7818022CC131425002A944204E9 :100AF00006CE26CA4AC89307A00741809389818001 :100B000063F9E70083C591800145850593F5F50FED :100B10001537E13D13050004EFF0EFDD93045400B9 :100B20000145C2040569EFF00FDDC1801309093AE0 :100B3000A287CA97050403C5070042044180EFF06D :100B40008FDBE39784FEEFF04FE983C70900F240A3 :100B5000624499072380F900D2444249B2490561B1 :100B60008280AA8793F707061375F50791C3ADB77F :100B7000A9476319F50083C59180850593F5F50FA5 :100B800001454DBDB5476315F50083C59180CDBFC7 :100B90008280014863DC0500B305B040E205E185D1 :100BA000635605003305A0401308D00293080002E5 :100BB000635806003306C04062066186930800034E :100BC000D1478146254E63D3C70A8280BE86B3775C :100BD000B50213F7F70F6370FE0813077703335559 :100BE000B50200101377F70F3303D4009387160074 :100BF0002306E3FE93F7F70F71F9630C0800130760 :100C000000036388E800A2978906238607FF93F70D :100C1000F60F3E8763C4C704630D08009306000304 :100C20006399D80014108507369793F7F70F2306BA :100C300007FF1384F7FF64001374F40F2694034531 :100C400004000537A2877D14E39BF4FEB2502254C2 :100C50009254456182801307070351B7141085072A :100C6000369793F7F70F230617FF65B7797106D606 :100C700022D426D2A9BF411122C4370400208345C3 :100C8000140026C2AA84FD1593F5F50F13058007FD :100C900006C64D331305000281351387C18683468E :100CA00047019307140093F6F60F63F49602054785 :100CB0002380E70083C5070013058007FD1593F522 :100CC000F50F8D3B2244B24092441305C00341010D :100CD00021B581E403474701E1BF23809700D9BFD5 :100CE000411126C2B70400209384040083C5040088 :100CF00022C42A84FD1593F5F50F014506C61D3B58 :100D000013050002D13B9305F4FF014593F5F50F60 :100D10002380840005332244B24092441305A0028C :100D200041015DBB1D7126DAA40022DC4ED652D4EF :100D300056D25AD05ECE62CC06DE4AD82E84B2C4D9 :100D4000B6C6BAC8BECAC2CCC6CE26C69309500221 :100D5000130A0003A54A294B930B3007130C3006E6 :100D60000345040009EDF2506254D2544259B2597D :100D7000225A925A025BF24B624C256182806306D2 :100D800035010504F93B2689A1A883471400638C2B :100D9000370109040147639647018347040005476B :100DA0000504014639A8130550023D33834724004A :100DB0000D04CDB7B30766030504B6973E96834787 :100DC000F4FF938607FDE3F7DAFE19C33306C0404C :100DD000639D770113894400844003C5040019E131 :100DE000CA84BDBF8504B53BCDBF6398870103C5E9 :100DF000040013894400B533E5B7B5D7130750078E :100E00006389E70213074006638DE7001307800735 :100E10006387E702130720068945638DE7007D1489 :100E20009DB76206138944006186D9558840953381 :100E300045BFA9456206138944006186C5BFC14507 :100E4000D5BF4111B70700204AC003C9170026C209 :100E5000AA844A8522C406C6EFF00FBE13842180FF :100E6000636895024A85EFF02FBD2310A4009945D1 :100E70007945C936035604002244B24092440249DF :100E80008565056593850530130525B6410159BD76 :100E90004A85EFF0AFBD63E6A4004A85EFF00FBDD1 :100EA000E9B723109400E1B7797126D256CAB70486 :100EB000004022D44AD04ECE52CC06D60544D93674 :100EC000938AC18605698569FD14056A83C74A014D :100ED00093F7F70F63F48702B707002003C51700E5 :100EE000593BB70700202254B25092540259F2499C :100EF000624AD24A03C507004561DDB383C74A0190 :100F000003C75A01998F93F7F70F63EA870203A784 :100F1000CA00B30794008A07BA9783D627002285B0 :100F200036C6EFF0EFB7B2462A8722869305CA305D :100F3000130529B60504FD331374F40F41BF228550 :100F4000EFF08FAF2AC62285EFF04FB2B2462A8764 :100F5000228693850932E9BFB717014003A70780AE :100F6000B70610FFFD167971758FB706400006D6DB :100F700022D426D24AD04ECE52CC56CA5AC85EC6C9 :100F800062C466C26AC0558F23A0E78003A70780AA :100F9000B70600F1FD16758FB7060004558F13864E :100FA000078023A0E780930700025CCA37A52400CE :100FB000930700045CCA130505F0EFF06F92353417 :100FC000014505690144EFF00F9381441309095865 :100FD000AD49B307990003C507008504EFF0AF9151 :100FE000E39934FFEFF06F9F713C05698565938548 :100FF0004533130529B63D33B7170240984FB7065E :101000000200B71401401367170198CFD84BCA8A62 :10101000558FD8CB83A744C0B7060044370701409B :10102000A207A183D58F23A2F4C01C4F93E70730FA :101030001CCF93878184C20737770240C18323186E :10104000F700F977A1072310F700F157A304F70081 :10105000EFF0AFB401E9856593854534130529B6F2 :10106000D13101A0913583A70480B706F1FFFD16A9 :10107000C19B93E7870023A0F480854723A8F480D1 :1010800083A70480B70B002093F7F7F093E70708D6 :1010900023A0F480894723A8F48083A70480938940 :1010A000C186856CF58FB7060800D58F23A0F48024 :1010B000C14723A8F480056DB714014083A784803D :1010C000858B85CF83A78480898B99EB03C51B0013 :1010D00005051375F50F453683A78480898BEDDFF1 :1010E00083A78480C18B8DC7EFF02FAB37550700E6 :1010F00013050530EFE0DFFED1B703C51B007D15FA :101100001375F50F8D3E83A78480858BEDDF5DBF62 :1011100083C7490103C7590103C91B00998F93F77E :10112000F70F63E7270303A7C90093172900238D4F :101130002901BA9783D7E7FF239EF900EFF0EFA5C7 :1011400019C14A85713E83A78480C18BEDDF69BFD9 :10115000EFF05F934A869385CC3413852AB6D9364F :101160004A85EFF06F8D2A8693054D3613852AB692 :10117000553E4A85EFF08F8F85652A8693858537A2 :1011800013852AB645364A85EFF08F9185652A8604 :101190009385C53813852AB6713603D52180138B04 :1011A0002180453183A78480C18BEDDF375A07004A :1011B00013050A30EFE0DFF2130CF00F130A0A30C8 :1011C00083A784801387048003550B00C18B91EFA4 :1011D000238D2901239EA900EFF02F9C09C5B70795 :1011E000002023802701C931B9BF1C47858B95E7B3 :1011F000510542054181B1317D141374F40F63161A :1012000084010944EFF06F9965BF83A78480858BC3 :10121000EDFB5285EFE0DFECC5B71C47898B95E30A :1012200031154205418131397D141374F40FE30AFD :1012300084FD83A78480898BE9F75285EFE05FEA1C :10124000E5B7130420037DBF8567938787F597F182 :10125000FF1F9381215B174100201301A1DA7D4517 :10126000731005BC13058008732005308D457390FD :101270004580130540004D8D731055307390173421 :10128000B70700200567938707001307C7589386A1 :10129000818063E8D702938781801387418963E85F :1012A000E702B7270240094798C3B717024023A2B5 :1012B000070037F700E0954614C3714798CF7300D5 :1012C000203010431107910723AEC7FED9B791070D :1012D00023AE07FEE9B700005A050000A205000092 :1012E0006E050000A805000098060000A205000099 :1012F000A2050000A205000098070000B207000048 :101300003E2536646D56203C0000000020282564F0 :10131000292536646D56202535646D412000000076 :10132000205B25645D2536646D562D2535646D562C :1013300020000000436F6E6E656374696E672E2E29 :101340002E0000004641494C4544000053656C6541 :10135000637420766F6C74616765206F66205B250F :10136000645D0A006D696E20766F6C746167653A22 :101370002537646D560000006D617820766F6C74BF :101380006167653A2537646D560000006D6178200D :1013900063757272656E743A2537646D41000000A2 :1013A000000000000000005F0000000700070014BC :1013B0007F147F14242A7F2A12231308646236497B :1013C0005522500004030000001C2241000041226D :1013D0001C0014083E081408083E0808008060003D :1013E00000080808080800606000002010080402D7 :1013F0003E5149453E44427F404042615149462268 :10140000414949361814127F102F494949313E4944 :101410004949320301710907364949493626494984 :10142000493E0036360000008068000000081422A3 :1014300000141414141400221408000201510906A7 :101440003E415D555E7C1211127C7F494949363E12 :10145000414141227F4141221C7F494949417F0945 :101460000909013E4149493A7F0808087F41417F07 :1014700041412040413F017F081422417F404040CC :10148000407F020C027F7F0408107F3E4141413EB5 :101490007F090909063E4141C1BE7F091929462637 :1014A0004949493201017F01013F4040403F1F202F :1014B00040201F3F4038403F631408146307087002 :1014C00008076151494543007F414100020408106B :1014D000200041417F000804020408404040404091 :1014E000000609090620545454787F44444438388F :1014F00044444428384444447F385454541808FE23 :1015000009010218A4A4A4787F0404047800447D8F :1015100040000080847D00417F10284400417F40CE :10152000007C047C04787C04040478384444443807 :10153000FC2424241818242424FC7C08040408080F :1015400054545420043F4440203C4040403C1C2024 :1015500040201C3C4030403C44281028441CA0A0A3 :10156000A07C4464544C4408083641410000FF000C :101570000041413608080804081008FFFFFFFFFF7C :0C158000A83F8D142000DA12A1C8AF00B3 :08158C000101881300000000BA :00000001FF ================================================ FILE: software/pd_tester/config.h ================================================ // =================================================================================== // User Configurations // =================================================================================== #pragma once // Pin definitions #define PIN_KEY_UP PA0 // pin connected to UP button (active low) #define PIN_KEY_DOWN PA1 // pin connected to DOWN button (active low) #define PIN_KEY_SLCT PA4 // pin connected to SELECT button (active low) #define PIN_SCL PA5 // I2C SCL connected to OLED #define PIN_SDA PA6 // I2C SDA connected to OLED #define PIN_LED PB1 // pin connected to LED (active low) // If using PC18 and/or PC19 for OLED, disable SWJ #define DISABLE_SWJ 0 // 0: normal SWJ pins, 1: OLED pins // MCU supply voltage #define USB_VDD 0 // 0: 3.3V, 1: 5V ================================================ FILE: software/pd_tester/ld/ch32x035.ld ================================================ ENTRY( jump_reset ) MEMORY { FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 62K RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K } SECTIONS { .init : { _sinit = .; . = ALIGN(4); KEEP(*(SORT_NONE(.init.jump))) KEEP(*(SORT_NONE(.init.data))) . = ALIGN(4); _einit = .; } >FLASH AT>FLASH .vector : { . = ALIGN(4); *(.vector); . = ALIGN(4); } >FLASH AT>FLASH .text : { . = ALIGN(4); *(.text) *(.text.*) *(.rodata) *(.rodata*) *(.glue_7) *(.glue_7t) *(.gnu.linkonce.t.*) . = ALIGN(4); } >FLASH AT>FLASH .fini : { KEEP(*(SORT_NONE(.fini))) . = ALIGN(4); } >FLASH AT>FLASH PROVIDE(_etext = .); PROVIDE(_eitcm = .); .preinit_array : { PROVIDE_HIDDEN(__preinit_array_start = .); KEEP(*(.preinit_array)) PROVIDE_HIDDEN(__preinit_array_end = .); } >FLASH AT>FLASH .init_array : { PROVIDE_HIDDEN(__init_array_start = .); KEEP(*(SORT_BY_INIT_PRIORITY(.init_array.*)SORT_BY_INIT_PRIORITY(.ctors.*))) KEEP(*(.init_array EXCLUDE_FILE(*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o) .ctors)) PROVIDE_HIDDEN(__init_array_end = .); } >FLASH AT>FLASH .fini_array : { PROVIDE_HIDDEN(__fini_array_start = .); KEEP(*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*))) KEEP(*(.fini_array EXCLUDE_FILE(*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o) .dtors)) PROVIDE_HIDDEN(__fini_array_end = .); } >FLASH AT>FLASH .ctors : { KEEP(*crtbegin.o(.ctors)) KEEP(*crtbegin?.o(.ctors)) KEEP(*(EXCLUDE_FILE(*crtend.o *crtend?.o) .ctors)) KEEP(*(SORT(.ctors.*))) KEEP(*(.ctors)) } >FLASH AT>FLASH .dtors : { KEEP(*crtbegin.o(.dtors)) KEEP(*crtbegin?.o(.dtors)) KEEP(*(EXCLUDE_FILE(*crtend.o *crtend?.o) .dtors)) KEEP(*(SORT(.dtors.*))) KEEP(*(.dtors)) } >FLASH AT>FLASH .dalign : { . = ALIGN(4); PROVIDE(_data_vma = .); } >RAM AT>FLASH .dlalign : { . = ALIGN(4); PROVIDE(_data_lma = .); } >FLASH AT>FLASH .data : { . = ALIGN(4); *(.gnu.linkonce.r.*) *(.data .data.*) *(.gnu.linkonce.d.*) . = ALIGN(8); PROVIDE(__global_pointer$ = . + 0x800); *(.sdata .sdata.*) *(.sdata2*) *(.gnu.linkonce.s.*) . = ALIGN(8); *(.srodata.cst16) *(.srodata.cst8) *(.srodata.cst4) *(.srodata.cst2) *(.srodata .srodata.*) . = ALIGN(4); PROVIDE(_edata = .); } >RAM AT>FLASH .bss : { . = ALIGN(4); PROVIDE(_sbss = .); *(.sbss*) *(.gnu.linkonce.sb.*) *(.bss*) *(.gnu.linkonce.b.*) *(COMMON*) . = ALIGN(4); PROVIDE(_ebss = .); } >RAM AT>FLASH PROVIDE(_end = _ebss); PROVIDE(end = . ); PROVIDE(_eusrstack = ORIGIN(RAM) + LENGTH(RAM)); } ================================================ FILE: software/pd_tester/makefile ================================================ # =================================================================================== # Project Makefile # =================================================================================== # Project: USB PD Tester for CH32X035 # Author: Stefan Wagner # Year: 2024 # URL: https://github.com/wagiminator # =================================================================================== # Install toolchain: # sudo apt install build-essential libnewlib-dev gcc-riscv64-unknown-elf # sudo apt install python3 python3-pip # pip install chprog # # Provide access permission to USB bootloader: # echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="4348", ATTR{idProduct}=="55e0", MODE="666"' | sudo tee /etc/udev/rules.d/99-ch55x.rules # echo 'SUBSYSTEM=="usb", ATTR{idVendor}=="1a86", ATTR{idProduct}=="55e0", MODE="666"' | sudo tee -a /etc/udev/rules.d/99-ch55x.rules # sudo udevadm control --reload-rules # # Set the device to bootloader mode and type "make flash" in the command line. # =================================================================================== # Files and Folders TARGET = pd_tester INCLUDE = include SOURCE = src BIN = bin # Microcontroller Settings F_CPU = 48000000 LDSCRIPT = ld/ch32x035.ld CPUARCH = -march=rv32imac -mabi=ilp32 # Toolchain PREFIX = riscv64-unknown-elf CC = $(PREFIX)-gcc OBJCOPY = $(PREFIX)-objcopy OBJDUMP = $(PREFIX)-objdump OBJSIZE = $(PREFIX)-size NEWLIB = /usr/include/newlib ISPTOOL = chprog $(BIN)/$(TARGET).bin CLEAN = rm -f *.lst *.obj *.cof *.list *.map *.eep.hex *.o *.d # Compiler Flags CFLAGS = -g -Os -flto -ffunction-sections -fdata-sections -fno-builtin -nostdlib CFLAGS += $(CPUARCH) -DF_CPU=$(F_CPU) -I$(NEWLIB) -I$(INCLUDE) -I$(SOURCE) -I. -Wall LDFLAGS = -T$(LDSCRIPT) -lgcc -Wl,--gc-sections,--build-id=none CFILES = $(wildcard ./*.c) $(wildcard $(SOURCE)/*.c) $(wildcard $(SOURCE)/*.S) # Symbolic Targets help: @echo "Use the following commands:" @echo "make all compile and build $(TARGET).elf/.bin/.hex/.asm" @echo "make hex compile and build $(TARGET).hex" @echo "make asm compile and disassemble to $(TARGET).asm" @echo "make bin compile and build $(TARGET).bin" @echo "make flash compile and upload to MCU" @echo "make clean remove all build files" $(BIN)/$(TARGET).elf: $(CFILES) @echo "Building $(BIN)/$(TARGET).elf ..." @mkdir -p $(BIN) @$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS) $(BIN)/$(TARGET).lst: $(BIN)/$(TARGET).elf @echo "Building $(BIN)/$(TARGET).lst ..." @$(OBJDUMP) -S $^ > $(BIN)/$(TARGET).lst $(BIN)/$(TARGET).map: $(BIN)/$(TARGET).elf @echo "Building $(BIN)/$(TARGET).map ..." @$(OBJDUMP) -t $^ > $(BIN)/$(TARGET).map $(BIN)/$(TARGET).bin: $(BIN)/$(TARGET).elf @echo "Building $(BIN)/$(TARGET).bin ..." @$(OBJCOPY) -O binary $< $(BIN)/$(TARGET).bin $(BIN)/$(TARGET).hex: $(BIN)/$(TARGET).elf @echo "Building $(BIN)/$(TARGET).hex ..." @$(OBJCOPY) -O ihex $< $(BIN)/$(TARGET).hex $(BIN)/$(TARGET).asm: $(BIN)/$(TARGET).elf @echo "Disassembling to $(BIN)/$(TARGET).asm ..." @$(OBJDUMP) -d $(BIN)/$(TARGET).elf > $(BIN)/$(TARGET).asm all: $(BIN)/$(TARGET).lst $(BIN)/$(TARGET).map $(BIN)/$(TARGET).bin $(BIN)/$(TARGET).hex $(BIN)/$(TARGET).asm size elf: $(BIN)/$(TARGET).elf removetemp size bin: $(BIN)/$(TARGET).bin removetemp size removeelf hex: $(BIN)/$(TARGET).hex removetemp size removeelf asm: $(BIN)/$(TARGET).asm removetemp size removeelf flash: $(BIN)/$(TARGET).bin size removeelf @echo "Uploading to MCU ..." @$(ISPTOOL) clean: @echo "Cleaning all up ..." @$(CLEAN) @rm -f $(BIN)/$(TARGET).elf $(BIN)/$(TARGET).lst $(BIN)/$(TARGET).map $(BIN)/$(TARGET).bin $(BIN)/$(TARGET).hex $(BIN)/$(TARGET).asm size: @echo "------------------" @echo "FLASH: $(shell $(OBJSIZE) -d $(BIN)/$(TARGET).elf | awk '/[0-9]/ {print $$1 + $$2}') bytes" @echo "SRAM: $(shell $(OBJSIZE) -d $(BIN)/$(TARGET).elf | awk '/[0-9]/ {print $$2 + $$3}') bytes" @echo "------------------" removetemp: @echo "Removing temporary files ..." @$(CLEAN) removeelf: @echo "Removing $(BIN)/$(TARGET).elf ..." @rm -f $(BIN)/$(TARGET).elf ================================================ FILE: software/pd_tester/platformio.ini ================================================ ; =================================================================================== ; PlatformIO Project Configuration File ; =================================================================================== ; Project: USB PD Tester for CH32X035 ; Author: Stefan Wagner ; Year: 2024 ; URL: https://github.com/wagiminator ; =================================================================================== ; Install PlatformIO and CH32V: ; https://pio-ch32v.readthedocs.io/en/latest/ ; https://github.com/Community-PIO-CH32V/platform-ch32v ; =================================================================================== [env:CH32X035] platform = https://github.com/Community-PIO-CH32V/platform-ch32v.git board = genericCH32X035F7P6 build_flags = -I. -D F_CPU=48000000 board_build.ldscript = $PROJECT_DIR/ld/ch32x035.ld board_build.use_lto = yes upload_protocol = minichlink upload_command = pip install chprog && chprog $SOURCE ================================================ FILE: software/pd_tester/src/ch32x035.h ================================================ // =================================================================================== // Header file for CH32X035/X034/X033 * v0.6 * // =================================================================================== // This contains a copy of ch32x035.h and core_riscv.h and other misc functions. // NOTE: This file includes modifications by Stefan Wagner. /********************************** (C) COPYRIGHT ******************************* * File Name : ch32x035.h * Author : WCH * Version : V1.0.0 * Date : 2023/04/06 * Description : CH32X035 Device Peripheral Access Layer Header File. ********************************************************************************* * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd. * Attention: This software (modified or not) and binary are used for * microcontroller manufactured by Nanjing Qinheng Microelectronics. *******************************************************************************/ #pragma once /* MCU definitions */ #define __MPU_PRESENT 0 /* Other CH32 devices does not provide an MPU */ #define __Vendor_SysTickConfig 0 /* Set to 1 if different SysTick Config is used */ #ifdef __ASSEMBLER__ #define HSI_VALUE (48000000) /* Value of the internal oscillator in Hz */ #else #define HSI_VALUE ((uint32_t)48000000) /* Value of the internal oscillator in Hz */ #ifdef __cplusplus extern "C" { #endif #include /* Interrupt Number Definition, according to the selected device */ typedef enum IRQn { /****** RISC-V Processor Exceptions Numbers ***************************************/ NonMaskableInt_IRQn = 2, /* 2 Non Maskable Interrupt */ EXC_IRQn = 3, /* 3 Exception Interrupt */ Ecall_M_Mode_IRQn = 5, /* 5 Ecall M Mode Interrupt */ Ecall_U_Mode_IRQn = 8, /* 8 Ecall U Mode Interrupt */ Break_Point_IRQn = 9, /* 9 Break Point Interrupt */ SysTicK_IRQn = 12, /* 12 System timer Interrupt */ Software_IRQn = 14, /* 14 software Interrupt */ /****** RISC-V specific Interrupt Numbers *****************************************/ WWDG_IRQn = 16, /* Window WatchDog Interrupt */ PVD_IRQn = 17, /* PVD through EXTI Line detection Interrupt */ FLASH_IRQn = 18, /* FLASH global Interrupt */ EXTI7_0_IRQn = 20, /* External Line[7:0] Interrupts */ AWU_IRQn = 21, /* AWU global Interrupt */ DMA1_Channel1_IRQn = 22, /* DMA1 Channel 1 global Interrupt */ DMA1_Channel2_IRQn = 23, /* DMA1 Channel 2 global Interrupt */ DMA1_Channel3_IRQn = 24, /* DMA1 Channel 3 global Interrupt */ DMA1_Channel4_IRQn = 25, /* DMA1 Channel 4 global Interrupt */ DMA1_Channel5_IRQn = 26, /* DMA1 Channel 5 global Interrupt */ DMA1_Channel6_IRQn = 27, /* DMA1 Channel 6 global Interrupt */ DMA1_Channel7_IRQn = 28, /* DMA1 Channel 7 global Interrupt */ ADC1_IRQn = 29, /* ADC1 global Interrupt */ I2C1_EV_IRQn = 30, /* I2C1 Event Interrupt */ I2C1_ER_IRQn = 31, /* I2C1 Error Interrupt */ USART1_IRQn = 32, /* USART1 global Interrupt */ SPI1_IRQn = 33, /* SPI1 global Interrupt */ TIM1_BRK_IRQn = 34, /* TIM1 Break Interrupt */ TIM1_UP_IRQn = 35, /* TIM1 Update Interrupt */ TIM1_TRG_COM_IRQn = 36, /* TIM1 Trigger and Commutation Interrupt */ TIM1_CC_IRQn = 37, /* TIM1 Capture Compare Interrupt */ TIM2_UP_IRQn = 38, /* TIM2 Update Interrupt */ USART2_IRQn = 39, /* USART2 global Interrupt */ EXTI15_8_IRQn = 40, /* External Line[15:8] Interrupts */ EXTI25_16_IRQn = 41, /* External Line[25:16] Interrupts */ USART3_IRQn = 42, /* USART3 global Interrupt */ USART4_IRQn = 43, /* USART4 global Interrupt */ DMA1_Channel8_IRQn = 44, /* DMA1 Channel 8 global Interrupt */ USBFS_IRQn = 45, /* USBFS Host/Device global Interrupt */ USBFSWakeUp_IRQn = 46, /* USBFS Host/Device WakeUp Interrupt */ PIOC_IRQn = 47, /* PIOC global Interrupt */ OPA_IRQn = 48, /* OPA global Interrupt */ USBPD_IRQn = 49, /* USBPD global Interrupt */ USBPDWakeUp_IRQn = 50, /* USBPD WakeUp Interrupt */ TIM2_CC_IRQn = 51, /* TIM2 Capture Compare Interrupt */ TIM2_TRG_COM_IRQn = 52, /* TIM2 Trigger and Commutation Interrupt */ TIM2_BRK_IRQn = 53, /* TIM2 Break Interrupt */ TIM3_IRQn = 54, /* TIM3 global Interrupt */ } IRQn_Type; #define HardFault_IRQn EXC_IRQn /* IO definitions */ #ifdef __cplusplus #define __I volatile /*!< defines 'read only' permissions */ #else #define __I volatile const /*!< defines 'read only' permissions */ #endif #define __O volatile /*!< defines 'write only' permissions */ #define __IO volatile /*!< defines 'read / write' permissions */ /* define compiler specific symbols */ #define __ASM __asm /*!< asm keyword for GNU Compiler */ #define __INLINE inline /*!< inline keyword for GNU Compiler */ #define RV_STATIC_INLINE static inline typedef enum {NoREADY = 0, READY = !NoREADY} ErrorStatus; typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus; /* Analog to Digital Converter */ typedef struct { __IO uint32_t STATR; __IO uint32_t CTLR1; __IO uint32_t CTLR2; __IO uint32_t SAMPTR1; __IO uint32_t SAMPTR2; __IO uint32_t IOFR1; __IO uint32_t IOFR2; __IO uint32_t IOFR3; __IO uint32_t IOFR4; __IO uint32_t WDHTR; __IO uint32_t WDLTR; __IO uint32_t RSQR1; __IO uint32_t RSQR2; __IO uint32_t RSQR3; __IO uint32_t ISQR; __IO uint32_t IDATAR1; __IO uint32_t IDATAR2; __IO uint32_t IDATAR3; __IO uint32_t IDATAR4; __IO uint32_t RDATAR; __IO uint32_t CTLR3; __IO uint32_t WDTR1; __IO uint32_t WDTR2; __IO uint32_t WDTR3; } ADC_TypeDef; /* DMA Channel Controller */ typedef struct { __IO uint32_t CFGR; __IO uint32_t CNTR; __IO uint32_t PADDR; __IO uint32_t MADDR; } DMA_Channel_TypeDef; /* DMA Controller */ typedef struct { __IO uint32_t INTFR; __IO uint32_t INTFCR; } DMA_TypeDef; /* External Interrupt/Event Controller */ typedef struct { __IO uint32_t INTENR; __IO uint32_t EVENR; __IO uint32_t RTENR; __IO uint32_t FTENR; __IO uint32_t SWIEVR; __IO uint32_t INTFR; } EXTI_TypeDef; /* FLASH Registers */ typedef struct { __IO uint32_t ACTLR; __IO uint32_t KEYR; __IO uint32_t OBKEYR; __IO uint32_t STATR; __IO uint32_t CTLR; __IO uint32_t ADDR; uint32_t RESERVED; __IO uint32_t OBR; __IO uint32_t WPR; __IO uint32_t MODEKEYR; __IO uint32_t BOOT_MODEKEYR; } FLASH_TypeDef; /* Option Bytes Registers */ typedef struct { __IO uint16_t RDPR; __IO uint16_t USER; __IO uint16_t Data0; __IO uint16_t Data1; __IO uint16_t WRPR0; __IO uint16_t WRPR1; __IO uint16_t WRPR2; __IO uint16_t WRPR3; } OB_TypeDef; /* General Purpose I/O */ typedef struct { __IO uint32_t CFGLR; __IO uint32_t CFGHR; __IO uint32_t INDR; __IO uint32_t OUTDR; __IO uint32_t BSHR; __IO uint32_t BCR; __IO uint32_t LCKR; __IO uint32_t CFGXR; __IO uint32_t BSXR; } GPIO_TypeDef; /* Alternate Function I/O */ typedef struct { uint32_t RESERVED0; __IO uint32_t PCFR1; __IO uint32_t EXTICR[2]; uint32_t RESERVED1; uint32_t RESERVED2; __IO uint32_t CTLR; } AFIO_TypeDef; /* Inter Integrated Circuit Interface */ typedef struct { __IO uint16_t CTLR1; uint16_t RESERVED0; __IO uint16_t CTLR2; uint16_t RESERVED1; __IO uint16_t OADDR1; uint16_t RESERVED2; __IO uint16_t OADDR2; uint16_t RESERVED3; __IO uint16_t DATAR; uint16_t RESERVED4; __IO uint16_t STAR1; uint16_t RESERVED5; __IO uint16_t STAR2; uint16_t RESERVED6; __IO uint16_t CKCFGR; uint16_t RESERVED7; } I2C_TypeDef; /* Independent WatchDog */ typedef struct { __IO uint32_t CTLR; __IO uint32_t PSCR; __IO uint32_t RLDR; __IO uint32_t STATR; } IWDG_TypeDef; /* Power Control */ typedef struct { __IO uint32_t CTLR; __IO uint32_t CSR; } PWR_TypeDef; /* Reset and Clock Control */ typedef struct { __IO uint32_t CTLR; __IO uint32_t CFGR0; __IO uint32_t RESERVED0; __IO uint32_t APB2PRSTR; __IO uint32_t APB1PRSTR; __IO uint32_t AHBPCENR; __IO uint32_t APB2PCENR; __IO uint32_t APB1PCENR; __IO uint32_t RESERVED1; __IO uint32_t RSTSCKR; __IO uint32_t AHBRSTR; } RCC_TypeDef; /* Serial Peripheral Interface */ typedef struct { __IO uint16_t CTLR1; uint16_t RESERVED0; __IO uint16_t CTLR2; uint16_t RESERVED1; __IO uint16_t STATR; uint16_t RESERVED2; __IO uint16_t DATAR; uint16_t RESERVED3; __IO uint16_t CRCR; uint16_t RESERVED4; __IO uint16_t RCRCR; uint16_t RESERVED5; __IO uint16_t TCRCR; uint16_t RESERVED6; uint32_t RESERVED7; uint32_t RESERVED8; __IO uint16_t HSCR; uint16_t RESERVED9; } SPI_TypeDef; /* TIM */ typedef struct { __IO uint16_t CTLR1; uint16_t RESERVED0; __IO uint16_t CTLR2; uint16_t RESERVED1; __IO uint16_t SMCFGR; uint16_t RESERVED2; __IO uint16_t DMAINTENR; uint16_t RESERVED3; __IO uint16_t INTFR; uint16_t RESERVED4; __IO uint16_t SWEVGR; uint16_t RESERVED5; __IO uint16_t CHCTLR1; uint16_t RESERVED6; __IO uint16_t CHCTLR2; uint16_t RESERVED7; __IO uint16_t CCER; uint16_t RESERVED8; __IO uint16_t CNT; uint16_t RESERVED9; __IO uint16_t PSC; uint16_t RESERVED10; __IO uint16_t ATRLR; uint16_t RESERVED11; __IO uint16_t RPTCR; uint16_t RESERVED12; __IO uint16_t CH1CVR; uint16_t RESERVED13; __IO uint16_t CH2CVR; uint16_t RESERVED14; __IO uint16_t CH3CVR; uint16_t RESERVED15; __IO uint16_t CH4CVR; uint16_t RESERVED16; __IO uint16_t BDTR; uint16_t RESERVED17; __IO uint16_t DMACFGR; uint16_t RESERVED18; __IO uint16_t DMAADR; uint16_t RESERVED19; __IO uint16_t SPEC; uint16_t RESERVED20; } TIM_TypeDef; /* Universal Synchronous Asynchronous Receiver Transmitter */ typedef struct { __IO uint16_t STATR; uint16_t RESERVED0; __IO uint16_t DATAR; uint16_t RESERVED1; __IO uint16_t BRR; uint16_t RESERVED2; __IO uint16_t CTLR1; uint16_t RESERVED3; __IO uint16_t CTLR2; uint16_t RESERVED4; __IO uint16_t CTLR3; uint16_t RESERVED5; __IO uint16_t GPR; uint16_t RESERVED6; } USART_TypeDef; /* Window WatchDog */ typedef struct { __IO uint32_t CTLR; __IO uint32_t CFGR; __IO uint32_t STATR; } WWDG_TypeDef; /* OPA Registers */ typedef struct { __IO uint16_t CFGR1; __IO uint16_t CFGR2; __IO uint32_t CTLR1; __IO uint32_t CTLR2; __IO uint32_t OPAKEY; __IO uint32_t CMPKEY; __IO uint32_t POLLKEY; } OPA_TypeDef; /* AWU Registers */ typedef struct { __IO uint32_t CSR; __IO uint32_t WR; __IO uint32_t PSC; } AWU_TypeDef; /* USBPD Registers */ typedef struct { union { __IO uint32_t USBPD_CONFIG; struct { __IO uint16_t CONFIG; __IO uint16_t BMC_CLK_CNT; }; }; union { __IO uint32_t USBPD_CONTROL; struct { union { __IO uint16_t R16_CONTROL; struct { __IO uint8_t CONTROL; __IO uint8_t TX_SEL; }; }; __IO uint16_t BMC_TX_SZ; }; }; union { __IO uint32_t USBPD_STATUS; struct { union { __IO uint16_t R16_STATUS; struct { __IO uint8_t DATA_BUF; __IO uint8_t STATUS; }; }; __IO uint16_t BMC_BYTE_CNT; }; }; union { __IO uint32_t USBPD_PORT; struct { __IO uint16_t PORT_CC1; __IO uint16_t PORT_CC2; }; }; union { __IO uint32_t USBPD_DMA; struct { __IO uint16_t DMA; __IO uint16_t RESERVED; }; }; } USBPD_TypeDef; /* USBFS Registers */ typedef struct { __IO uint8_t BASE_CTRL; __IO uint8_t UDEV_CTRL; __IO uint8_t INT_EN; __IO uint8_t DEV_ADDR; uint8_t RESERVED0; __IO uint8_t MIS_ST; __IO uint8_t INT_FG; __IO uint8_t INT_ST; __IO uint16_t RX_LEN; uint16_t RESERVED1; __IO uint8_t UEP4_1_MOD; __IO uint8_t UEP2_3_MOD; __IO uint8_t UEP567_MOD; uint8_t RESERVED2; __IO uint32_t UEP0_DMA; __IO uint32_t UEP1_DMA; __IO uint32_t UEP2_DMA; __IO uint32_t UEP3_DMA; union { __IO uint32_t UEP0_CTRL; struct { __IO uint16_t UEP0_TX_LEN; __IO uint16_t UEP0_CTRL_H; }; }; union { __IO uint32_t UEP1_CTRL; struct { __IO uint16_t UEP1_TX_LEN; __IO uint16_t UEP1_CTRL_H; }; }; union { __IO uint32_t UEP2_CTRL; struct { __IO uint16_t UEP2_TX_LEN; __IO uint16_t UEP2_CTRL_H; }; }; union { __IO uint32_t UEP3_CTRL; struct { __IO uint16_t UEP3_TX_LEN; __IO uint16_t UEP3_CTRL_H; }; }; union { __IO uint32_t UEP4_CTRL; struct { __IO uint16_t UEP4_TX_LEN; __IO uint16_t UEP4_CTRL_H; }; }; uint32_t RESERVED3; uint32_t RESERVED4; uint32_t RESERVED5; uint32_t RESERVED6; uint32_t RESERVED7; uint32_t RESERVED8; uint32_t RESERVED9; uint32_t RESERVED10; __IO uint32_t UEP5_DMA; __IO uint32_t UEP6_DMA; __IO uint32_t UEP7_DMA; uint32_t RESERVED11; union { __IO uint32_t UEP5_CTRL; struct { __IO uint16_t UEP5_TX_LEN; __IO uint16_t UEP5_CTRL_H; }; }; union { __IO uint32_t UEP6_CTRL; struct { __IO uint16_t UEP6_TX_LEN; __IO uint16_t UEP6_CTRL_H; }; }; union { __IO uint32_t UEP7_CTRL; struct { __IO uint16_t UEP7_TX_LEN; __IO uint16_t UEP7_CTRL_H; }; }; __IO uint32_t UEPX_MOD; } USBFSD_TypeDef; typedef struct { __IO uint8_t BASE_CTRL; __IO uint8_t HOST_CTRL; __IO uint8_t INT_EN; __IO uint8_t DEV_ADDR; uint8_t RESERVED0; __IO uint8_t MIS_ST; __IO uint8_t INT_FG; __IO uint8_t INT_ST; __IO uint16_t RX_LEN; uint16_t RESERVED1; uint8_t RESERVED2; __IO uint8_t HOST_EP_MOD; uint16_t RESERVED3; uint32_t RESERVED4; uint32_t RESERVED5; __IO uint16_t HOST_RX_DMA; uint16_t RESERVED6; __IO uint16_t HOST_TX_DMA; uint16_t RESERVED7; uint32_t RESERVED8; uint16_t RESERVED9; __IO uint8_t HOST_SETUP; uint8_t RESERVED10; __IO uint8_t HOST_EP_PID; uint8_t RESERVED11; __IO uint8_t HOST_RX_CTRL; uint8_t RESERVED12; __IO uint8_t HOST_TX_LEN; uint8_t RESERVED13; __IO uint8_t HOST_TX_CTRL; uint8_t RESERVED14; } USBFSH_TypeDef; /* memory mapped structure for Program Fast Interrupt Controller (PFIC) */ typedef struct{ __I uint32_t ISR[8]; __I uint32_t IPR[8]; __IO uint32_t ITHRESDR; __IO uint32_t RESERVED; __IO uint32_t CFGR; __I uint32_t GISR; __IO uint8_t VTFIDR[4]; uint8_t RESERVED0[12]; __IO uint32_t VTFADDR[4]; uint8_t RESERVED1[0x90]; __O uint32_t IENR[8]; uint8_t RESERVED2[0x60]; __O uint32_t IRER[8]; uint8_t RESERVED3[0x60]; __O uint32_t IPSR[8]; uint8_t RESERVED4[0x60]; __O uint32_t IPRR[8]; uint8_t RESERVED5[0x60]; __IO uint32_t IACTR[8]; uint8_t RESERVED6[0xE0]; __IO uint8_t IPRIOR[256]; uint8_t RESERVED7[0x810]; __IO uint32_t SCTLR; } PFIC_Type; /* memory mapped structure for SysTick */ typedef struct { __IO uint32_t CTLR; __IO uint32_t SR; union { __IO uint64_t CNT; struct { __IO uint32_t CNTL; __IO uint32_t CNTH; }; }; union { __IO uint64_t CMP; struct { __IO uint32_t CMPL; __IO uint32_t CMPH; }; }; } SysTick_Type; /* PIOC Registers */ typedef struct { uint32_t RESERVED00; union { __IO uint32_t PIOC_SFR; struct { __IO uint8_t INDIR_ADDR; __IO uint8_t TMR0_COUNT; __IO uint8_t TMR0_CTRL; __IO uint8_t TMR0_INIT; }; }; union { __IO uint32_t PORT_CFG; struct { __IO uint8_t BIT_CYCLE; __IO uint8_t INDIR_ADDR2; __IO uint8_t PORT_DIR; __IO uint8_t PORT_IO; }; }; uint32_t RESERVED0C; uint32_t RESERVED10; uint32_t RESERVED14; uint32_t RESERVED18; union { __IO uint32_t DATA_CTRL; struct { __IO uint8_t SYS_CFG; __IO uint8_t CTRL_RD; __IO uint8_t CTRL_WR; __IO uint8_t DATA_EXCH; }; }; union { __IO uint32_t DATA_REG0_3; struct { __IO uint8_t DATA_REG0; __IO uint8_t DATA_REG1; __IO uint8_t DATA_REG2; __IO uint8_t DATA_REG3; }; __IO uint16_t DATA_REG0_1; }; union { __IO uint32_t DATA_REG4_7; struct { __IO uint8_t DATA_REG4; __IO uint8_t DATA_REG5; __IO uint8_t DATA_REG6; __IO uint8_t DATA_REG7; }; }; union { __IO uint32_t DATA_REG8_11; struct { __IO uint8_t DATA_REG8; __IO uint8_t DATA_REG9; __IO uint8_t DATA_REG10; __IO uint8_t DATA_REG11; }; }; union { __IO uint32_t DATA_REG12_15; struct { __IO uint8_t DATA_REG12; __IO uint8_t DATA_REG13; __IO uint8_t DATA_REG14; __IO uint8_t DATA_REG15; }; }; union { __IO uint32_t DATA_REG16_19; struct { __IO uint8_t DATA_REG16; __IO uint8_t DATA_REG17; __IO uint8_t DATA_REG18; __IO uint8_t DATA_REG19; }; }; union { __IO uint32_t DATA_REG20_23; struct { __IO uint8_t DATA_REG20; __IO uint8_t DATA_REG21; __IO uint8_t DATA_REG22; __IO uint8_t DATA_REG23; }; }; union { __IO uint32_t DATA_REG24_27; struct { __IO uint8_t DATA_REG24; __IO uint8_t DATA_REG25; __IO uint8_t DATA_REG26; __IO uint8_t DATA_REG27; }; }; union { __IO uint32_t DATA_REG28_31; struct { __IO uint8_t DATA_REG28; __IO uint8_t DATA_REG29; __IO uint8_t DATA_REG30; __IO uint8_t DATA_REG31; }; }; } PIOC_TypeDef; #endif #ifdef __ASSEMBLER__ #define FLASH_BASE (0x08000000) /* FLASH base address in the alias region */ #define SRAM_BASE (0x20000000) /* SRAM base address in the alias region */ #define PERIPH_BASE (0x40000000) /* Peripheral base address in the alias region */ #define OB_BASE (0x1FFFF800) #define PFIC_BASE (0xE000E000) #define STK_BASE (0xE000F000) #else #define FLASH_BASE ((uint32_t)0x08000000) /* FLASH base address in the alias region */ #define SRAM_BASE ((uint32_t)0x20000000) /* SRAM base address in the alias region */ #define PERIPH_BASE ((uint32_t)0x40000000) /* Peripheral base address in the alias region */ #define OB_BASE ((uint32_t)0x1FFFF800) #define PFIC_BASE ((uint32_t)0xE000E000) #define STK_BASE ((uint32_t)0xE000F000) #endif #define APB1PERIPH_BASE (PERIPH_BASE) #define APB2PERIPH_BASE (PERIPH_BASE + 0x10000) #define AHBPERIPH_BASE (PERIPH_BASE + 0x20000) #define TIM2_BASE (APB1PERIPH_BASE + 0x0000) #define TIM3_BASE (APB1PERIPH_BASE + 0x0400) #define WWDG_BASE (APB1PERIPH_BASE + 0x2C00) #define IWDG_BASE (APB1PERIPH_BASE + 0x3000) #define USART2_BASE (APB1PERIPH_BASE + 0x4400) #define USART3_BASE (APB1PERIPH_BASE + 0x4800) #define USART4_BASE (APB1PERIPH_BASE + 0x4C00) #define I2C1_BASE (APB1PERIPH_BASE + 0x5400) #define PWR_BASE (APB1PERIPH_BASE + 0x7000) #define AFIO_BASE (APB2PERIPH_BASE + 0x0000) #define EXTI_BASE (APB2PERIPH_BASE + 0x0400) #define GPIOA_BASE (APB2PERIPH_BASE + 0x0800) #define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00) #define GPIOC_BASE (APB2PERIPH_BASE + 0x1000) #define ADC1_BASE (APB2PERIPH_BASE + 0x2400) #define TIM1_BASE (APB2PERIPH_BASE + 0x2C00) #define SPI1_BASE (APB2PERIPH_BASE + 0x3000) #define USART1_BASE (APB2PERIPH_BASE + 0x3800) #define DMA1_BASE (AHBPERIPH_BASE + 0x0000) #define DMA1_Channel1_BASE (AHBPERIPH_BASE + 0x0008) #define DMA1_Channel2_BASE (AHBPERIPH_BASE + 0x001C) #define DMA1_Channel3_BASE (AHBPERIPH_BASE + 0x0030) #define DMA1_Channel4_BASE (AHBPERIPH_BASE + 0x0044) #define DMA1_Channel5_BASE (AHBPERIPH_BASE + 0x0058) #define DMA1_Channel6_BASE (AHBPERIPH_BASE + 0x006C) #define DMA1_Channel7_BASE (AHBPERIPH_BASE + 0x0080) #define DMA1_Channel8_BASE (AHBPERIPH_BASE + 0x0094) #define RCC_BASE (AHBPERIPH_BASE + 0x1000) #define FLASH_R_BASE (AHBPERIPH_BASE + 0x2000) #define USBFS_BASE (AHBPERIPH_BASE + 0x3400) #define OPA_BASE (AHBPERIPH_BASE + 0x6000) #define AWU_BASE (AHBPERIPH_BASE + 0x6400) #define PIOC_BASE (AHBPERIPH_BASE + 0x6C00) #define USBPD_BASE (AHBPERIPH_BASE + 0x7000) #define PIOC_SRAM_BASE (SRAM_BASE+0x4000) #define PIOC_SFR_BASE PIOC_BASE /* Peripheral declaration */ #define TIM2 ((TIM_TypeDef *)TIM2_BASE) #define TIM3 ((TIM_TypeDef *)TIM3_BASE) #define WWDG ((WWDG_TypeDef *)WWDG_BASE) #define IWDG ((IWDG_TypeDef *)IWDG_BASE) #define USART2 ((USART_TypeDef *)USART2_BASE) #define USART3 ((USART_TypeDef *)USART3_BASE) #define USART4 ((USART_TypeDef *)USART4_BASE) #define I2C1 ((I2C_TypeDef *)I2C1_BASE) #define PWR ((PWR_TypeDef *)PWR_BASE) #define AFIO ((AFIO_TypeDef *)AFIO_BASE) #define EXTI ((EXTI_TypeDef *)EXTI_BASE) #define GPIOA ((GPIO_TypeDef *)GPIOA_BASE) #define GPIOB ((GPIO_TypeDef *)GPIOB_BASE) #define GPIOC ((GPIO_TypeDef *)GPIOC_BASE) #define ADC1 ((ADC_TypeDef *)ADC1_BASE) #define TKey1 ((ADC_TypeDef *)ADC1_BASE) #define TIM1 ((TIM_TypeDef *)TIM1_BASE) #define SPI1 ((SPI_TypeDef *)SPI1_BASE) #define USART1 ((USART_TypeDef *)USART1_BASE) #define DMA1 ((DMA_TypeDef *)DMA1_BASE) #define DMA1_Channel1 ((DMA_Channel_TypeDef *)DMA1_Channel1_BASE) #define DMA1_Channel2 ((DMA_Channel_TypeDef *)DMA1_Channel2_BASE) #define DMA1_Channel3 ((DMA_Channel_TypeDef *)DMA1_Channel3_BASE) #define DMA1_Channel4 ((DMA_Channel_TypeDef *)DMA1_Channel4_BASE) #define DMA1_Channel5 ((DMA_Channel_TypeDef *)DMA1_Channel5_BASE) #define DMA1_Channel6 ((DMA_Channel_TypeDef *)DMA1_Channel6_BASE) #define DMA1_Channel7 ((DMA_Channel_TypeDef *)DMA1_Channel7_BASE) #define DMA1_Channel8 ((DMA_Channel_TypeDef *)DMA1_Channel8_BASE) #define RCC ((RCC_TypeDef *)RCC_BASE) #define FLASH ((FLASH_TypeDef *)FLASH_R_BASE) #define USBFSD ((USBFSD_TypeDef *)USBFS_BASE) #define USBFSH ((USBFSH_TypeDef *)USBFS_BASE) #define OPA ((OPA_TypeDef *)OPA_BASE) #define AWU ((AWU_TypeDef *)AWU_BASE) #define PIOC ((PIOC_TypeDef *)PIOC_BASE) #define USBPD ((USBPD_TypeDef *)USBPD_BASE) #define OB ((OB_TypeDef *)OB_BASE) /* Core declaration */ #define PFIC ((PFIC_Type *)PFIC_BASE) #define NVIC PFIC #define SysTick ((SysTick_Type *)STK_BASE) #define STK SysTick /* PIOC declaration */ #define PIOC ((PIOC_TypeDef *)PIOC_BASE) /******************************************************************************/ /* Peripheral Registers Bits Definition */ /******************************************************************************/ /******************************************************************************/ /* Analog to Digital Converter */ /******************************************************************************/ /******************** Bit definition for ADC_STATR register ********************/ #define ADC_AWD ((uint8_t)0x01) /* Analog watchdog flag */ #define ADC_EOC ((uint8_t)0x02) /* End of conversion */ #define ADC_JEOC ((uint8_t)0x04) /* Injected channel end of conversion */ #define ADC_JSTRT ((uint8_t)0x08) /* Injected channel Start flag */ #define ADC_STRT ((uint8_t)0x10) /* Regular channel Start flag */ /******************* Bit definition for ADC_CTLR1 register ********************/ #define ADC_AWDCH ((uint32_t)0x0000000F) /* AWDCH[3:0] bits (Analog watchdog channel select bits) */ #define ADC_AWDCH_0 ((uint32_t)0x00000001) /* Bit 0 */ #define ADC_AWDCH_1 ((uint32_t)0x00000002) /* Bit 1 */ #define ADC_AWDCH_2 ((uint32_t)0x00000004) /* Bit 2 */ #define ADC_AWDCH_3 ((uint32_t)0x00000008) /* Bit 3 */ #define ADC_EOCIE ((uint32_t)0x00000020) /* Interrupt enable for EOC */ #define ADC_AWDIE ((uint32_t)0x00000040) /* Analog Watchdog interrupt enable */ #define ADC_JEOCIE ((uint32_t)0x00000080) /* Interrupt enable for injected channels */ #define ADC_SCAN ((uint32_t)0x00000100) /* Scan mode */ #define ADC_AWDSGL ((uint32_t)0x00000200) /* Enable the watchdog on a single channel in scan mode */ #define ADC_JAUTO ((uint32_t)0x00000400) /* Automatic injected group conversion */ #define ADC_DISCEN ((uint32_t)0x00000800) /* Discontinuous mode on regular channels */ #define ADC_JDISCEN ((uint32_t)0x00001000) /* Discontinuous mode on injected channels */ #define ADC_DISCNUM ((uint32_t)0x0000E000) /* DISCNUM[2:0] bits (Discontinuous mode channel count) */ #define ADC_DISCNUM_0 ((uint32_t)0x00002000) /* Bit 0 */ #define ADC_DISCNUM_1 ((uint32_t)0x00004000) /* Bit 1 */ #define ADC_DISCNUM_2 ((uint32_t)0x00008000) /* Bit 2 */ #define ADC_JAWDEN ((uint32_t)0x00400000) /* Analog watchdog enable on injected channels */ #define ADC_AWDEN ((uint32_t)0x00800000) /* Analog watchdog enable on regular channels */ #define ADC_TKENABLE ((uint32_t)0x01000000) /* TKEN mode enable */ /******************* Bit definition for ADC_CTLR2 register ********************/ #define ADC_ADON ((uint32_t)0x00000001) /* A/D Converter ON / OFF */ #define ADC_CONT ((uint32_t)0x00000002) /* Continuous Conversion */ #define ADC_DMA ((uint32_t)0x00000100) /* Direct Memory access mode */ #define ADC_ALIGN ((uint32_t)0x00000800) /* Data Alignment */ #define ADC_JEXTSEL ((uint32_t)0x00007000) /* JEXTSEL[2:0] bits (External event select for injected group) */ #define ADC_JEXTSEL_0 ((uint32_t)0x00001000) /* Bit 0 */ #define ADC_JEXTSEL_1 ((uint32_t)0x00002000) /* Bit 1 */ #define ADC_JEXTSEL_2 ((uint32_t)0x00004000) /* Bit 2 */ #define ADC_JEXTTRIG ((uint32_t)0x00008000) /* External Trigger Conversion mode for injected channels */ #define ADC_EXTSEL ((uint32_t)0x000E0000) /* EXTSEL[2:0] bits (External Event Select for regular group) */ #define ADC_EXTSEL_0 ((uint32_t)0x00020000) /* Bit 0 */ #define ADC_EXTSEL_1 ((uint32_t)0x00040000) /* Bit 1 */ #define ADC_EXTSEL_2 ((uint32_t)0x00080000) /* Bit 2 */ #define ADC_EXTTRIG ((uint32_t)0x00100000) /* External Trigger Conversion mode for regular channels */ #define ADC_JSWSTART ((uint32_t)0x00200000) /* Start Conversion of injected channels */ #define ADC_SWSTART ((uint32_t)0x00400000) /* Start Conversion of regular channels */ /****************** Bit definition for ADC_SAMPTR1 register *******************/ #define ADC_SMP10 ((uint32_t)0x00000007) /* SMP10[2:0] bits (Channel 10 Sample time selection) */ #define ADC_SMP10_0 ((uint32_t)0x00000001) /* Bit 0 */ #define ADC_SMP10_1 ((uint32_t)0x00000002) /* Bit 1 */ #define ADC_SMP10_2 ((uint32_t)0x00000004) /* Bit 2 */ #define ADC_SMP11 ((uint32_t)0x00000038) /* SMP11[2:0] bits (Channel 11 Sample time selection) */ #define ADC_SMP11_0 ((uint32_t)0x00000008) /* Bit 0 */ #define ADC_SMP11_1 ((uint32_t)0x00000010) /* Bit 1 */ #define ADC_SMP11_2 ((uint32_t)0x00000020) /* Bit 2 */ #define ADC_SMP12 ((uint32_t)0x000001C0) /* SMP12[2:0] bits (Channel 12 Sample time selection) */ #define ADC_SMP12_0 ((uint32_t)0x00000040) /* Bit 0 */ #define ADC_SMP12_1 ((uint32_t)0x00000080) /* Bit 1 */ #define ADC_SMP12_2 ((uint32_t)0x00000100) /* Bit 2 */ #define ADC_SMP13 ((uint32_t)0x00000E00) /* SMP13[2:0] bits (Channel 13 Sample time selection) */ #define ADC_SMP13_0 ((uint32_t)0x00000200) /* Bit 0 */ #define ADC_SMP13_1 ((uint32_t)0x00000400) /* Bit 1 */ #define ADC_SMP13_2 ((uint32_t)0x00000800) /* Bit 2 */ #define ADC_SMP14 ((uint32_t)0x00007000) /* SMP14[2:0] bits (Channel 14 Sample time selection) */ #define ADC_SMP14_0 ((uint32_t)0x00001000) /* Bit 0 */ #define ADC_SMP14_1 ((uint32_t)0x00002000) /* Bit 1 */ #define ADC_SMP14_2 ((uint32_t)0x00004000) /* Bit 2 */ #define ADC_SMP15 ((uint32_t)0x00038000) /* SMP15[2:0] bits (Channel 15 Sample time selection) */ #define ADC_SMP15_0 ((uint32_t)0x00008000) /* Bit 0 */ #define ADC_SMP15_1 ((uint32_t)0x00010000) /* Bit 1 */ #define ADC_SMP15_2 ((uint32_t)0x00020000) /* Bit 2 */ #define ADC_SMP16 ((uint32_t)0x001C0000) /* SMP16[2:0] bits (Channel 16 Sample time selection) */ #define ADC_SMP16_0 ((uint32_t)0x00040000) /* Bit 0 */ #define ADC_SMP16_1 ((uint32_t)0x00080000) /* Bit 1 */ #define ADC_SMP16_2 ((uint32_t)0x00100000) /* Bit 2 */ #define ADC_SMP17 ((uint32_t)0x00E00000) /* SMP17[2:0] bits (Channel 17 Sample time selection) */ #define ADC_SMP17_0 ((uint32_t)0x00200000) /* Bit 0 */ #define ADC_SMP17_1 ((uint32_t)0x00400000) /* Bit 1 */ #define ADC_SMP17_2 ((uint32_t)0x00800000) /* Bit 2 */ /****************** Bit definition for ADC_SAMPTR2 register *******************/ #define ADC_SMP0 ((uint32_t)0x00000007) /* SMP0[2:0] bits (Channel 0 Sample time selection) */ #define ADC_SMP0_0 ((uint32_t)0x00000001) /* Bit 0 */ #define ADC_SMP0_1 ((uint32_t)0x00000002) /* Bit 1 */ #define ADC_SMP0_2 ((uint32_t)0x00000004) /* Bit 2 */ #define ADC_SMP1 ((uint32_t)0x00000038) /* SMP1[2:0] bits (Channel 1 Sample time selection) */ #define ADC_SMP1_0 ((uint32_t)0x00000008) /* Bit 0 */ #define ADC_SMP1_1 ((uint32_t)0x00000010) /* Bit 1 */ #define ADC_SMP1_2 ((uint32_t)0x00000020) /* Bit 2 */ #define ADC_SMP2 ((uint32_t)0x000001C0) /* SMP2[2:0] bits (Channel 2 Sample time selection) */ #define ADC_SMP2_0 ((uint32_t)0x00000040) /* Bit 0 */ #define ADC_SMP2_1 ((uint32_t)0x00000080) /* Bit 1 */ #define ADC_SMP2_2 ((uint32_t)0x00000100) /* Bit 2 */ #define ADC_SMP3 ((uint32_t)0x00000E00) /* SMP3[2:0] bits (Channel 3 Sample time selection) */ #define ADC_SMP3_0 ((uint32_t)0x00000200) /* Bit 0 */ #define ADC_SMP3_1 ((uint32_t)0x00000400) /* Bit 1 */ #define ADC_SMP3_2 ((uint32_t)0x00000800) /* Bit 2 */ #define ADC_SMP4 ((uint32_t)0x00007000) /* SMP4[2:0] bits (Channel 4 Sample time selection) */ #define ADC_SMP4_0 ((uint32_t)0x00001000) /* Bit 0 */ #define ADC_SMP4_1 ((uint32_t)0x00002000) /* Bit 1 */ #define ADC_SMP4_2 ((uint32_t)0x00004000) /* Bit 2 */ #define ADC_SMP5 ((uint32_t)0x00038000) /* SMP5[2:0] bits (Channel 5 Sample time selection) */ #define ADC_SMP5_0 ((uint32_t)0x00008000) /* Bit 0 */ #define ADC_SMP5_1 ((uint32_t)0x00010000) /* Bit 1 */ #define ADC_SMP5_2 ((uint32_t)0x00020000) /* Bit 2 */ #define ADC_SMP6 ((uint32_t)0x001C0000) /* SMP6[2:0] bits (Channel 6 Sample time selection) */ #define ADC_SMP6_0 ((uint32_t)0x00040000) /* Bit 0 */ #define ADC_SMP6_1 ((uint32_t)0x00080000) /* Bit 1 */ #define ADC_SMP6_2 ((uint32_t)0x00100000) /* Bit 2 */ #define ADC_SMP7 ((uint32_t)0x00E00000) /* SMP7[2:0] bits (Channel 7 Sample time selection) */ #define ADC_SMP7_0 ((uint32_t)0x00200000) /* Bit 0 */ #define ADC_SMP7_1 ((uint32_t)0x00400000) /* Bit 1 */ #define ADC_SMP7_2 ((uint32_t)0x00800000) /* Bit 2 */ #define ADC_SMP8 ((uint32_t)0x07000000) /* SMP8[2:0] bits (Channel 8 Sample time selection) */ #define ADC_SMP8_0 ((uint32_t)0x01000000) /* Bit 0 */ #define ADC_SMP8_1 ((uint32_t)0x02000000) /* Bit 1 */ #define ADC_SMP8_2 ((uint32_t)0x04000000) /* Bit 2 */ #define ADC_SMP9 ((uint32_t)0x38000000) /* SMP9[2:0] bits (Channel 9 Sample time selection) */ #define ADC_SMP9_0 ((uint32_t)0x08000000) /* Bit 0 */ #define ADC_SMP9_1 ((uint32_t)0x10000000) /* Bit 1 */ #define ADC_SMP9_2 ((uint32_t)0x20000000) /* Bit 2 */ /****************** Bit definition for ADC_IOFR1 register *******************/ #define ADC_JOFFSET1 ((uint16_t)0x0FFF) /* Data offset for injected channel 1 */ /****************** Bit definition for ADC_IOFR2 register *******************/ #define ADC_JOFFSET2 ((uint16_t)0x0FFF) /* Data offset for injected channel 2 */ /****************** Bit definition for ADC_IOFR3 register *******************/ #define ADC_JOFFSET3 ((uint16_t)0x0FFF) /* Data offset for injected channel 3 */ /****************** Bit definition for ADC_IOFR4 register *******************/ #define ADC_JOFFSET4 ((uint16_t)0x0FFF) /* Data offset for injected channel 4 */ /******************* Bit definition for ADC_WDHTR register ********************/ #define ADC_HT ((uint16_t)0x0FFF) /* Analog watchdog high threshold */ /******************* Bit definition for ADC_WDLTR register ********************/ #define ADC_LT ((uint16_t)0x0FFF) /* Analog watchdog low threshold */ /******************* Bit definition for ADC_RSQR1 register *******************/ #define ADC_SQ13 ((uint32_t)0x0000001F) /* SQ13[4:0] bits (13th conversion in regular sequence) */ #define ADC_SQ13_0 ((uint32_t)0x00000001) /* Bit 0 */ #define ADC_SQ13_1 ((uint32_t)0x00000002) /* Bit 1 */ #define ADC_SQ13_2 ((uint32_t)0x00000004) /* Bit 2 */ #define ADC_SQ13_3 ((uint32_t)0x00000008) /* Bit 3 */ #define ADC_SQ13_4 ((uint32_t)0x00000010) /* Bit 4 */ #define ADC_SQ14 ((uint32_t)0x000003E0) /* SQ14[4:0] bits (14th conversion in regular sequence) */ #define ADC_SQ14_0 ((uint32_t)0x00000020) /* Bit 0 */ #define ADC_SQ14_1 ((uint32_t)0x00000040) /* Bit 1 */ #define ADC_SQ14_2 ((uint32_t)0x00000080) /* Bit 2 */ #define ADC_SQ14_3 ((uint32_t)0x00000100) /* Bit 3 */ #define ADC_SQ14_4 ((uint32_t)0x00000200) /* Bit 4 */ #define ADC_SQ15 ((uint32_t)0x00007C00) /* SQ15[4:0] bits (15th conversion in regular sequence) */ #define ADC_SQ15_0 ((uint32_t)0x00000400) /* Bit 0 */ #define ADC_SQ15_1 ((uint32_t)0x00000800) /* Bit 1 */ #define ADC_SQ15_2 ((uint32_t)0x00001000) /* Bit 2 */ #define ADC_SQ15_3 ((uint32_t)0x00002000) /* Bit 3 */ #define ADC_SQ15_4 ((uint32_t)0x00004000) /* Bit 4 */ #define ADC_SQ16 ((uint32_t)0x000F8000) /* SQ16[4:0] bits (16th conversion in regular sequence) */ #define ADC_SQ16_0 ((uint32_t)0x00008000) /* Bit 0 */ #define ADC_SQ16_1 ((uint32_t)0x00010000) /* Bit 1 */ #define ADC_SQ16_2 ((uint32_t)0x00020000) /* Bit 2 */ #define ADC_SQ16_3 ((uint32_t)0x00040000) /* Bit 3 */ #define ADC_SQ16_4 ((uint32_t)0x00080000) /* Bit 4 */ #define ADC_L ((uint32_t)0x00F00000) /* L[3:0] bits (Regular channel sequence length) */ #define ADC_L_0 ((uint32_t)0x00100000) /* Bit 0 */ #define ADC_L_1 ((uint32_t)0x00200000) /* Bit 1 */ #define ADC_L_2 ((uint32_t)0x00400000) /* Bit 2 */ #define ADC_L_3 ((uint32_t)0x00800000) /* Bit 3 */ /******************* Bit definition for ADC_RSQR2 register *******************/ #define ADC_SQ7 ((uint32_t)0x0000001F) /* SQ7[4:0] bits (7th conversion in regular sequence) */ #define ADC_SQ7_0 ((uint32_t)0x00000001) /* Bit 0 */ #define ADC_SQ7_1 ((uint32_t)0x00000002) /* Bit 1 */ #define ADC_SQ7_2 ((uint32_t)0x00000004) /* Bit 2 */ #define ADC_SQ7_3 ((uint32_t)0x00000008) /* Bit 3 */ #define ADC_SQ7_4 ((uint32_t)0x00000010) /* Bit 4 */ #define ADC_SQ8 ((uint32_t)0x000003E0) /* SQ8[4:0] bits (8th conversion in regular sequence) */ #define ADC_SQ8_0 ((uint32_t)0x00000020) /* Bit 0 */ #define ADC_SQ8_1 ((uint32_t)0x00000040) /* Bit 1 */ #define ADC_SQ8_2 ((uint32_t)0x00000080) /* Bit 2 */ #define ADC_SQ8_3 ((uint32_t)0x00000100) /* Bit 3 */ #define ADC_SQ8_4 ((uint32_t)0x00000200) /* Bit 4 */ #define ADC_SQ9 ((uint32_t)0x00007C00) /* SQ9[4:0] bits (9th conversion in regular sequence) */ #define ADC_SQ9_0 ((uint32_t)0x00000400) /* Bit 0 */ #define ADC_SQ9_1 ((uint32_t)0x00000800) /* Bit 1 */ #define ADC_SQ9_2 ((uint32_t)0x00001000) /* Bit 2 */ #define ADC_SQ9_3 ((uint32_t)0x00002000) /* Bit 3 */ #define ADC_SQ9_4 ((uint32_t)0x00004000) /* Bit 4 */ #define ADC_SQ10 ((uint32_t)0x000F8000) /* SQ10[4:0] bits (10th conversion in regular sequence) */ #define ADC_SQ10_0 ((uint32_t)0x00008000) /* Bit 0 */ #define ADC_SQ10_1 ((uint32_t)0x00010000) /* Bit 1 */ #define ADC_SQ10_2 ((uint32_t)0x00020000) /* Bit 2 */ #define ADC_SQ10_3 ((uint32_t)0x00040000) /* Bit 3 */ #define ADC_SQ10_4 ((uint32_t)0x00080000) /* Bit 4 */ #define ADC_SQ11 ((uint32_t)0x01F00000) /* SQ11[4:0] bits (11th conversion in regular sequence) */ #define ADC_SQ11_0 ((uint32_t)0x00100000) /* Bit 0 */ #define ADC_SQ11_1 ((uint32_t)0x00200000) /* Bit 1 */ #define ADC_SQ11_2 ((uint32_t)0x00400000) /* Bit 2 */ #define ADC_SQ11_3 ((uint32_t)0x00800000) /* Bit 3 */ #define ADC_SQ11_4 ((uint32_t)0x01000000) /* Bit 4 */ #define ADC_SQ12 ((uint32_t)0x3E000000) /* SQ12[4:0] bits (12th conversion in regular sequence) */ #define ADC_SQ12_0 ((uint32_t)0x02000000) /* Bit 0 */ #define ADC_SQ12_1 ((uint32_t)0x04000000) /* Bit 1 */ #define ADC_SQ12_2 ((uint32_t)0x08000000) /* Bit 2 */ #define ADC_SQ12_3 ((uint32_t)0x10000000) /* Bit 3 */ #define ADC_SQ12_4 ((uint32_t)0x20000000) /* Bit 4 */ /******************* Bit definition for ADC_RSQR3 register *******************/ #define ADC_SQ1 ((uint32_t)0x0000001F) /* SQ1[4:0] bits (1st conversion in regular sequence) */ #define ADC_SQ1_0 ((uint32_t)0x00000001) /* Bit 0 */ #define ADC_SQ1_1 ((uint32_t)0x00000002) /* Bit 1 */ #define ADC_SQ1_2 ((uint32_t)0x00000004) /* Bit 2 */ #define ADC_SQ1_3 ((uint32_t)0x00000008) /* Bit 3 */ #define ADC_SQ1_4 ((uint32_t)0x00000010) /* Bit 4 */ #define ADC_SQ2 ((uint32_t)0x000003E0) /* SQ2[4:0] bits (2nd conversion in regular sequence) */ #define ADC_SQ2_0 ((uint32_t)0x00000020) /* Bit 0 */ #define ADC_SQ2_1 ((uint32_t)0x00000040) /* Bit 1 */ #define ADC_SQ2_2 ((uint32_t)0x00000080) /* Bit 2 */ #define ADC_SQ2_3 ((uint32_t)0x00000100) /* Bit 3 */ #define ADC_SQ2_4 ((uint32_t)0x00000200) /* Bit 4 */ #define ADC_SQ3 ((uint32_t)0x00007C00) /* SQ3[4:0] bits (3rd conversion in regular sequence) */ #define ADC_SQ3_0 ((uint32_t)0x00000400) /* Bit 0 */ #define ADC_SQ3_1 ((uint32_t)0x00000800) /* Bit 1 */ #define ADC_SQ3_2 ((uint32_t)0x00001000) /* Bit 2 */ #define ADC_SQ3_3 ((uint32_t)0x00002000) /* Bit 3 */ #define ADC_SQ3_4 ((uint32_t)0x00004000) /* Bit 4 */ #define ADC_SQ4 ((uint32_t)0x000F8000) /* SQ4[4:0] bits (4th conversion in regular sequence) */ #define ADC_SQ4_0 ((uint32_t)0x00008000) /* Bit 0 */ #define ADC_SQ4_1 ((uint32_t)0x00010000) /* Bit 1 */ #define ADC_SQ4_2 ((uint32_t)0x00020000) /* Bit 2 */ #define ADC_SQ4_3 ((uint32_t)0x00040000) /* Bit 3 */ #define ADC_SQ4_4 ((uint32_t)0x00080000) /* Bit 4 */ #define ADC_SQ5 ((uint32_t)0x01F00000) /* SQ5[4:0] bits (5th conversion in regular sequence) */ #define ADC_SQ5_0 ((uint32_t)0x00100000) /* Bit 0 */ #define ADC_SQ5_1 ((uint32_t)0x00200000) /* Bit 1 */ #define ADC_SQ5_2 ((uint32_t)0x00400000) /* Bit 2 */ #define ADC_SQ5_3 ((uint32_t)0x00800000) /* Bit 3 */ #define ADC_SQ5_4 ((uint32_t)0x01000000) /* Bit 4 */ #define ADC_SQ6 ((uint32_t)0x3E000000) /* SQ6[4:0] bits (6th conversion in regular sequence) */ #define ADC_SQ6_0 ((uint32_t)0x02000000) /* Bit 0 */ #define ADC_SQ6_1 ((uint32_t)0x04000000) /* Bit 1 */ #define ADC_SQ6_2 ((uint32_t)0x08000000) /* Bit 2 */ #define ADC_SQ6_3 ((uint32_t)0x10000000) /* Bit 3 */ #define ADC_SQ6_4 ((uint32_t)0x20000000) /* Bit 4 */ /******************* Bit definition for ADC_ISQR register *******************/ #define ADC_JSQ1 ((uint32_t)0x0000001F) /* JSQ1[4:0] bits (1st conversion in injected sequence) */ #define ADC_JSQ1_0 ((uint32_t)0x00000001) /* Bit 0 */ #define ADC_JSQ1_1 ((uint32_t)0x00000002) /* Bit 1 */ #define ADC_JSQ1_2 ((uint32_t)0x00000004) /* Bit 2 */ #define ADC_JSQ1_3 ((uint32_t)0x00000008) /* Bit 3 */ #define ADC_JSQ1_4 ((uint32_t)0x00000010) /* Bit 4 */ #define ADC_JSQ2 ((uint32_t)0x000003E0) /* JSQ2[4:0] bits (2nd conversion in injected sequence) */ #define ADC_JSQ2_0 ((uint32_t)0x00000020) /* Bit 0 */ #define ADC_JSQ2_1 ((uint32_t)0x00000040) /* Bit 1 */ #define ADC_JSQ2_2 ((uint32_t)0x00000080) /* Bit 2 */ #define ADC_JSQ2_3 ((uint32_t)0x00000100) /* Bit 3 */ #define ADC_JSQ2_4 ((uint32_t)0x00000200) /* Bit 4 */ #define ADC_JSQ3 ((uint32_t)0x00007C00) /* JSQ3[4:0] bits (3rd conversion in injected sequence) */ #define ADC_JSQ3_0 ((uint32_t)0x00000400) /* Bit 0 */ #define ADC_JSQ3_1 ((uint32_t)0x00000800) /* Bit 1 */ #define ADC_JSQ3_2 ((uint32_t)0x00001000) /* Bit 2 */ #define ADC_JSQ3_3 ((uint32_t)0x00002000) /* Bit 3 */ #define ADC_JSQ3_4 ((uint32_t)0x00004000) /* Bit 4 */ #define ADC_JSQ4 ((uint32_t)0x000F8000) /* JSQ4[4:0] bits (4th conversion in injected sequence) */ #define ADC_JSQ4_0 ((uint32_t)0x00008000) /* Bit 0 */ #define ADC_JSQ4_1 ((uint32_t)0x00010000) /* Bit 1 */ #define ADC_JSQ4_2 ((uint32_t)0x00020000) /* Bit 2 */ #define ADC_JSQ4_3 ((uint32_t)0x00040000) /* Bit 3 */ #define ADC_JSQ4_4 ((uint32_t)0x00080000) /* Bit 4 */ #define ADC_JL ((uint32_t)0x00300000) /* JL[1:0] bits (Injected Sequence length) */ #define ADC_JL_0 ((uint32_t)0x00100000) /* Bit 0 */ #define ADC_JL_1 ((uint32_t)0x00200000) /* Bit 1 */ /******************* Bit definition for ADC_IDATAR1 register *******************/ #define ADC_IDATAR1_JDATA ((uint16_t)0xFFFF) /* Injected data */ /******************* Bit definition for ADC_IDATAR2 register *******************/ #define ADC_IDATAR2_JDATA ((uint16_t)0xFFFF) /* Injected data */ /******************* Bit definition for ADC_IDATAR3 register *******************/ #define ADC_IDATAR3_JDATA ((uint16_t)0xFFFF) /* Injected data */ /******************* Bit definition for ADC_IDATAR4 register *******************/ #define ADC_IDATAR4_JDATA ((uint16_t)0xFFFF) /* Injected data */ /******************** Bit definition for ADC_RDATAR register ********************/ #define ADC_RDATAR_DATA ((uint32_t)0x0000FFFF) /* Regular data */ /******************** Bit definition for ADC_CTLR3 register ********************/ #define ADC_CTLR3_CLK_DIV ((uint32_t)0x0000000F) /* CLK_DIVL[3:0] bits */ #define ADC_CTLR3_CLK_DIV_0 ((uint32_t)0x00000001) /* Bit 0 */ #define ADC_CTLR3_CLK_DIV_1 ((uint32_t)0x00000002) /* Bit 1 */ #define ADC_CTLR3_CLK_DIV_2 ((uint32_t)0x00000004) /* Bit 2 */ #define ADC_CTLR3_CLK_DIV_3 ((uint32_t)0x00000008) /* Bit 3 */ #define ADC_CTLR3_AWD_SCAN ((uint32_t)0x00000200) /* Analog watchdog Scan enable */ #define ADC_CTLR3_AWD0_RST_EN ((uint32_t)0x00001000) /* Watchdog0 Reset Enable */ #define ADC_CTLR3_AWD1_RST_EN ((uint32_t)0x00002000) /* Watchdog1 Reset Enable */ #define ADC_CTLR3_AWD2_RST_EN ((uint32_t)0x00004000) /* Watchdog2 Reset Enable */ #define ADC_CTLR3_AWD3_RST_EN ((uint32_t)0x00008000) /* Watchdog3 Reset Enable */ /******************** Bit definition for ADC_WDTR1 register ********************/ #define ADC_WDTR1_LTR1 ((uint32_t)0x00000FFF) /* Analog watchdog1 low threshold */ #define ADC_WDTR1_HTR1 ((uint32_t)0x00FFF000) /* Analog watchdog1 high threshold */ /******************** Bit definition for ADC_WDTR2 register ********************/ #define ADC_WDTR2_LTR2 ((uint32_t)0x00000FFF) /* Analog watchdog2 low threshold */ #define ADC_WDTR2_HTR2 ((uint32_t)0x00FFF000) /* Analog watchdog2 high threshold */ /******************** Bit definition for ADC_WDTR3 register ********************/ #define ADC_WDTR3_LTR3 ((uint32_t)0x00000FFF) /* Analog watchdog3 low threshold */ #define ADC_WDTR3_HTR3 ((uint32_t)0x00FFF000) /* Analog watchdog3 high threshold */ /******************************************************************************/ /* DMA Controller */ /******************************************************************************/ /******************* Bit definition for DMA_INTFR register ********************/ #define DMA_GIF1 ((uint32_t)0x00000001) /* Channel 1 Global interrupt flag */ #define DMA_TCIF1 ((uint32_t)0x00000002) /* Channel 1 Transfer Complete flag */ #define DMA_HTIF1 ((uint32_t)0x00000004) /* Channel 1 Half Transfer flag */ #define DMA_TEIF1 ((uint32_t)0x00000008) /* Channel 1 Transfer Error flag */ #define DMA_GIF2 ((uint32_t)0x00000010) /* Channel 2 Global interrupt flag */ #define DMA_TCIF2 ((uint32_t)0x00000020) /* Channel 2 Transfer Complete flag */ #define DMA_HTIF2 ((uint32_t)0x00000040) /* Channel 2 Half Transfer flag */ #define DMA_TEIF2 ((uint32_t)0x00000080) /* Channel 2 Transfer Error flag */ #define DMA_GIF3 ((uint32_t)0x00000100) /* Channel 3 Global interrupt flag */ #define DMA_TCIF3 ((uint32_t)0x00000200) /* Channel 3 Transfer Complete flag */ #define DMA_HTIF3 ((uint32_t)0x00000400) /* Channel 3 Half Transfer flag */ #define DMA_TEIF3 ((uint32_t)0x00000800) /* Channel 3 Transfer Error flag */ #define DMA_GIF4 ((uint32_t)0x00001000) /* Channel 4 Global interrupt flag */ #define DMA_TCIF4 ((uint32_t)0x00002000) /* Channel 4 Transfer Complete flag */ #define DMA_HTIF4 ((uint32_t)0x00004000) /* Channel 4 Half Transfer flag */ #define DMA_TEIF4 ((uint32_t)0x00008000) /* Channel 4 Transfer Error flag */ #define DMA_GIF5 ((uint32_t)0x00010000) /* Channel 5 Global interrupt flag */ #define DMA_TCIF5 ((uint32_t)0x00020000) /* Channel 5 Transfer Complete flag */ #define DMA_HTIF5 ((uint32_t)0x00040000) /* Channel 5 Half Transfer flag */ #define DMA_TEIF5 ((uint32_t)0x00080000) /* Channel 5 Transfer Error flag */ #define DMA_GIF6 ((uint32_t)0x00100000) /* Channel 6 Global interrupt flag */ #define DMA_TCIF6 ((uint32_t)0x00200000) /* Channel 6 Transfer Complete flag */ #define DMA_HTIF6 ((uint32_t)0x00400000) /* Channel 6 Half Transfer flag */ #define DMA_TEIF6 ((uint32_t)0x00800000) /* Channel 6 Transfer Error flag */ #define DMA_GIF7 ((uint32_t)0x01000000) /* Channel 7 Global interrupt flag */ #define DMA_TCIF7 ((uint32_t)0x02000000) /* Channel 7 Transfer Complete flag */ #define DMA_HTIF7 ((uint32_t)0x04000000) /* Channel 7 Half Transfer flag */ #define DMA_TEIF7 ((uint32_t)0x08000000) /* Channel 7 Transfer Error flag */ #define DMA_GIF8 ((uint32_t)0x10000000) /* Channel 8 Global interrupt flag */ #define DMA_TCIF8 ((uint32_t)0x20000000) /* Channel 8 Transfer Complete flag */ #define DMA_HTIF8 ((uint32_t)0x40000000) /* Channel 8 Half Transfer flag */ #define DMA_TEIF8 ((uint32_t)0x80000000) /* Channel 8 Transfer Error flag */ /******************* Bit definition for DMA_INTFCR register *******************/ #define DMA_CGIF1 ((uint32_t)0x00000001) /* Channel 1 Global interrupt clear */ #define DMA_CTCIF1 ((uint32_t)0x00000002) /* Channel 1 Transfer Complete clear */ #define DMA_CHTIF1 ((uint32_t)0x00000004) /* Channel 1 Half Transfer clear */ #define DMA_CTEIF1 ((uint32_t)0x00000008) /* Channel 1 Transfer Error clear */ #define DMA_CGIF2 ((uint32_t)0x00000010) /* Channel 2 Global interrupt clear */ #define DMA_CTCIF2 ((uint32_t)0x00000020) /* Channel 2 Transfer Complete clear */ #define DMA_CHTIF2 ((uint32_t)0x00000040) /* Channel 2 Half Transfer clear */ #define DMA_CTEIF2 ((uint32_t)0x00000080) /* Channel 2 Transfer Error clear */ #define DMA_CGIF3 ((uint32_t)0x00000100) /* Channel 3 Global interrupt clear */ #define DMA_CTCIF3 ((uint32_t)0x00000200) /* Channel 3 Transfer Complete clear */ #define DMA_CHTIF3 ((uint32_t)0x00000400) /* Channel 3 Half Transfer clear */ #define DMA_CTEIF3 ((uint32_t)0x00000800) /* Channel 3 Transfer Error clear */ #define DMA_CGIF4 ((uint32_t)0x00001000) /* Channel 4 Global interrupt clear */ #define DMA_CTCIF4 ((uint32_t)0x00002000) /* Channel 4 Transfer Complete clear */ #define DMA_CHTIF4 ((uint32_t)0x00004000) /* Channel 4 Half Transfer clear */ #define DMA_CTEIF4 ((uint32_t)0x00008000) /* Channel 4 Transfer Error clear */ #define DMA_CGIF5 ((uint32_t)0x00010000) /* Channel 5 Global interrupt clear */ #define DMA_CTCIF5 ((uint32_t)0x00020000) /* Channel 5 Transfer Complete clear */ #define DMA_CHTIF5 ((uint32_t)0x00040000) /* Channel 5 Half Transfer clear */ #define DMA_CTEIF5 ((uint32_t)0x00080000) /* Channel 5 Transfer Error clear */ #define DMA_CGIF6 ((uint32_t)0x00100000) /* Channel 6 Global interrupt clear */ #define DMA_CTCIF6 ((uint32_t)0x00200000) /* Channel 6 Transfer Complete clear */ #define DMA_CHTIF6 ((uint32_t)0x00400000) /* Channel 6 Half Transfer clear */ #define DMA_CTEIF6 ((uint32_t)0x00800000) /* Channel 6 Transfer Error clear */ #define DMA_CGIF7 ((uint32_t)0x01000000) /* Channel 7 Global interrupt clear */ #define DMA_CTCIF7 ((uint32_t)0x02000000) /* Channel 7 Transfer Complete clear */ #define DMA_CHTIF7 ((uint32_t)0x04000000) /* Channel 7 Half Transfer clear */ #define DMA_CTEIF7 ((uint32_t)0x08000000) /* Channel 7 Transfer Error clear */ #define DMA_CGIF8 ((uint32_t)0x10000000) /* Channel 8 Global interrupt clear */ #define DMA_CTCIF8 ((uint32_t)0x20000000) /* Channel 8 Transfer Complete clear */ #define DMA_CHTIF8 ((uint32_t)0x40000000) /* Channel 8 Half Transfer clear */ #define DMA_CTEIF8 ((uint32_t)0x80000000) /* Channel 8 Transfer Error clear */ /******************* Bit definition for DMA_CFGR1 register *******************/ #define DMA_CFGR1_EN ((uint16_t)0x0001) /* Channel enable*/ #define DMA_CFGR1_TCIE ((uint16_t)0x0002) /* Transfer complete interrupt enable */ #define DMA_CFGR1_HTIE ((uint16_t)0x0004) /* Half Transfer interrupt enable */ #define DMA_CFGR1_TEIE ((uint16_t)0x0008) /* Transfer error interrupt enable */ #define DMA_CFGR1_DIR ((uint16_t)0x0010) /* Data transfer direction */ #define DMA_CFGR1_CIRC ((uint16_t)0x0020) /* Circular mode */ #define DMA_CFGR1_PINC ((uint16_t)0x0040) /* Peripheral increment mode */ #define DMA_CFGR1_MINC ((uint16_t)0x0080) /* Memory increment mode */ #define DMA_CFGR1_PSIZE ((uint16_t)0x0300) /* PSIZE[1:0] bits (Peripheral size) */ #define DMA_CFGR1_PSIZE_0 ((uint16_t)0x0100) /* Bit 0 */ #define DMA_CFGR1_PSIZE_1 ((uint16_t)0x0200) /* Bit 1 */ #define DMA_CFGR1_MSIZE ((uint16_t)0x0C00) /* MSIZE[1:0] bits (Memory size) */ #define DMA_CFGR1_MSIZE_0 ((uint16_t)0x0400) /* Bit 0 */ #define DMA_CFGR1_MSIZE_1 ((uint16_t)0x0800) /* Bit 1 */ #define DMA_CFGR1_PL ((uint16_t)0x3000) /* PL[1:0] bits(Channel Priority level) */ #define DMA_CFGR1_PL_0 ((uint16_t)0x1000) /* Bit 0 */ #define DMA_CFGR1_PL_1 ((uint16_t)0x2000) /* Bit 1 */ #define DMA_CFGR1_MEM2MEM ((uint16_t)0x4000) /* Memory to memory mode */ /******************* Bit definition for DMA_CFGR2 register *******************/ #define DMA_CFGR2_EN ((uint16_t)0x0001) /* Channel enable */ #define DMA_CFGR2_TCIE ((uint16_t)0x0002) /* Transfer complete interrupt enable */ #define DMA_CFGR2_HTIE ((uint16_t)0x0004) /* Half Transfer interrupt enable */ #define DMA_CFGR2_TEIE ((uint16_t)0x0008) /* Transfer error interrupt enable */ #define DMA_CFGR2_DIR ((uint16_t)0x0010) /* Data transfer direction */ #define DMA_CFGR2_CIRC ((uint16_t)0x0020) /* Circular mode */ #define DMA_CFGR2_PINC ((uint16_t)0x0040) /* Peripheral increment mode */ #define DMA_CFGR2_MINC ((uint16_t)0x0080) /* Memory increment mode */ #define DMA_CFGR2_PSIZE ((uint16_t)0x0300) /* PSIZE[1:0] bits (Peripheral size) */ #define DMA_CFGR2_PSIZE_0 ((uint16_t)0x0100) /* Bit 0 */ #define DMA_CFGR2_PSIZE_1 ((uint16_t)0x0200) /* Bit 1 */ #define DMA_CFGR2_MSIZE ((uint16_t)0x0C00) /* MSIZE[1:0] bits (Memory size) */ #define DMA_CFGR2_MSIZE_0 ((uint16_t)0x0400) /* Bit 0 */ #define DMA_CFGR2_MSIZE_1 ((uint16_t)0x0800) /* Bit 1 */ #define DMA_CFGR2_PL ((uint16_t)0x3000) /* PL[1:0] bits (Channel Priority level) */ #define DMA_CFGR2_PL_0 ((uint16_t)0x1000) /* Bit 0 */ #define DMA_CFGR2_PL_1 ((uint16_t)0x2000) /* Bit 1 */ #define DMA_CFGR2_MEM2MEM ((uint16_t)0x4000) /* Memory to memory mode */ /******************* Bit definition for DMA_CFGR3 register *******************/ #define DMA_CFGR3_EN ((uint16_t)0x0001) /* Channel enable */ #define DMA_CFGR3_TCIE ((uint16_t)0x0002) /* Transfer complete interrupt enable */ #define DMA_CFGR3_HTIE ((uint16_t)0x0004) /* Half Transfer interrupt enable */ #define DMA_CFGR3_TEIE ((uint16_t)0x0008) /* Transfer error interrupt enable */ #define DMA_CFGR3_DIR ((uint16_t)0x0010) /* Data transfer direction */ #define DMA_CFGR3_CIRC ((uint16_t)0x0020) /* Circular mode */ #define DMA_CFGR3_PINC ((uint16_t)0x0040) /* Peripheral increment mode */ #define DMA_CFGR3_MINC ((uint16_t)0x0080) /* Memory increment mode */ #define DMA_CFGR3_PSIZE ((uint16_t)0x0300) /* PSIZE[1:0] bits (Peripheral size) */ #define DMA_CFGR3_PSIZE_0 ((uint16_t)0x0100) /* Bit 0 */ #define DMA_CFGR3_PSIZE_1 ((uint16_t)0x0200) /* Bit 1 */ #define DMA_CFGR3_MSIZE ((uint16_t)0x0C00) /* MSIZE[1:0] bits (Memory size) */ #define DMA_CFGR3_MSIZE_0 ((uint16_t)0x0400) /* Bit 0 */ #define DMA_CFGR3_MSIZE_1 ((uint16_t)0x0800) /* Bit 1 */ #define DMA_CFGR3_PL ((uint16_t)0x3000) /* PL[1:0] bits (Channel Priority level) */ #define DMA_CFGR3_PL_0 ((uint16_t)0x1000) /* Bit 0 */ #define DMA_CFGR3_PL_1 ((uint16_t)0x2000) /* Bit 1 */ #define DMA_CFGR3_MEM2MEM ((uint16_t)0x4000) /* Memory to memory mode */ /******************* Bit definition for DMA_CFG4 register *******************/ #define DMA_CFG4_EN ((uint16_t)0x0001) /* Channel enable */ #define DMA_CFG4_TCIE ((uint16_t)0x0002) /* Transfer complete interrupt enable */ #define DMA_CFG4_HTIE ((uint16_t)0x0004) /* Half Transfer interrupt enable */ #define DMA_CFG4_TEIE ((uint16_t)0x0008) /* Transfer error interrupt enable */ #define DMA_CFG4_DIR ((uint16_t)0x0010) /* Data transfer direction */ #define DMA_CFG4_CIRC ((uint16_t)0x0020) /* Circular mode */ #define DMA_CFG4_PINC ((uint16_t)0x0040) /* Peripheral increment mode */ #define DMA_CFG4_MINC ((uint16_t)0x0080) /* Memory increment mode */ #define DMA_CFG4_PSIZE ((uint16_t)0x0300) /* PSIZE[1:0] bits (Peripheral size) */ #define DMA_CFG4_PSIZE_0 ((uint16_t)0x0100) /* Bit 0 */ #define DMA_CFG4_PSIZE_1 ((uint16_t)0x0200) /* Bit 1 */ #define DMA_CFG4_MSIZE ((uint16_t)0x0C00) /* MSIZE[1:0] bits (Memory size) */ #define DMA_CFG4_MSIZE_0 ((uint16_t)0x0400) /* Bit 0 */ #define DMA_CFG4_MSIZE_1 ((uint16_t)0x0800) /* Bit 1 */ #define DMA_CFG4_PL ((uint16_t)0x3000) /* PL[1:0] bits (Channel Priority level) */ #define DMA_CFG4_PL_0 ((uint16_t)0x1000) /* Bit 0 */ #define DMA_CFG4_PL_1 ((uint16_t)0x2000) /* Bit 1 */ #define DMA_CFG4_MEM2MEM ((uint16_t)0x4000) /* Memory to memory mode */ /****************** Bit definition for DMA_CFG5 register *******************/ #define DMA_CFG5_EN ((uint16_t)0x0001) /* Channel enable */ #define DMA_CFG5_TCIE ((uint16_t)0x0002) /* Transfer complete interrupt enable */ #define DMA_CFG5_HTIE ((uint16_t)0x0004) /* Half Transfer interrupt enable */ #define DMA_CFG5_TEIE ((uint16_t)0x0008) /* Transfer error interrupt enable */ #define DMA_CFG5_DIR ((uint16_t)0x0010) /* Data transfer direction */ #define DMA_CFG5_CIRC ((uint16_t)0x0020) /* Circular mode */ #define DMA_CFG5_PINC ((uint16_t)0x0040) /* Peripheral increment mode */ #define DMA_CFG5_MINC ((uint16_t)0x0080) /* Memory increment mode */ #define DMA_CFG5_PSIZE ((uint16_t)0x0300) /* PSIZE[1:0] bits (Peripheral size) */ #define DMA_CFG5_PSIZE_0 ((uint16_t)0x0100) /* Bit 0 */ #define DMA_CFG5_PSIZE_1 ((uint16_t)0x0200) /* Bit 1 */ #define DMA_CFG5_MSIZE ((uint16_t)0x0C00) /* MSIZE[1:0] bits (Memory size) */ #define DMA_CFG5_MSIZE_0 ((uint16_t)0x0400) /* Bit 0 */ #define DMA_CFG5_MSIZE_1 ((uint16_t)0x0800) /* Bit 1 */ #define DMA_CFG5_PL ((uint16_t)0x3000) /* PL[1:0] bits (Channel Priority level) */ #define DMA_CFG5_PL_0 ((uint16_t)0x1000) /* Bit 0 */ #define DMA_CFG5_PL_1 ((uint16_t)0x2000) /* Bit 1 */ #define DMA_CFG5_MEM2MEM ((uint16_t)0x4000) /* Memory to memory mode enable */ /******************* Bit definition for DMA_CFG6 register *******************/ #define DMA_CFG6_EN ((uint16_t)0x0001) /* Channel enable */ #define DMA_CFG6_TCIE ((uint16_t)0x0002) /* Transfer complete interrupt enable */ #define DMA_CFG6_HTIE ((uint16_t)0x0004) /* Half Transfer interrupt enable */ #define DMA_CFG6_TEIE ((uint16_t)0x0008) /* Transfer error interrupt enable */ #define DMA_CFG6_DIR ((uint16_t)0x0010) /* Data transfer direction */ #define DMA_CFG6_CIRC ((uint16_t)0x0020) /* Circular mode */ #define DMA_CFG6_PINC ((uint16_t)0x0040) /* Peripheral increment mode */ #define DMA_CFG6_MINC ((uint16_t)0x0080) /* Memory increment mode */ #define DMA_CFG6_PSIZE ((uint16_t)0x0300) /* PSIZE[1:0] bits (Peripheral size) */ #define DMA_CFG6_PSIZE_0 ((uint16_t)0x0100) /* Bit 0 */ #define DMA_CFG6_PSIZE_1 ((uint16_t)0x0200) /* Bit 1 */ #define DMA_CFG6_MSIZE ((uint16_t)0x0C00) /* MSIZE[1:0] bits (Memory size) */ #define DMA_CFG6_MSIZE_0 ((uint16_t)0x0400) /* Bit 0 */ #define DMA_CFG6_MSIZE_1 ((uint16_t)0x0800) /* Bit 1 */ #define DMA_CFG6_PL ((uint16_t)0x3000) /* PL[1:0] bits (Channel Priority level) */ #define DMA_CFG6_PL_0 ((uint16_t)0x1000) /* Bit 0 */ #define DMA_CFG6_PL_1 ((uint16_t)0x2000) /* Bit 1 */ #define DMA_CFG6_MEM2MEM ((uint16_t)0x4000) /* Memory to memory mode */ /******************* Bit definition for DMA_CFG7 register *******************/ #define DMA_CFG7_EN ((uint16_t)0x0001) /* Channel enable */ #define DMA_CFG7_TCIE ((uint16_t)0x0002) /* Transfer complete interrupt enable */ #define DMA_CFG7_HTIE ((uint16_t)0x0004) /* Half Transfer interrupt enable */ #define DMA_CFG7_TEIE ((uint16_t)0x0008) /* Transfer error interrupt enable */ #define DMA_CFG7_DIR ((uint16_t)0x0010) /* Data transfer direction */ #define DMA_CFG7_CIRC ((uint16_t)0x0020) /* Circular mode */ #define DMA_CFG7_PINC ((uint16_t)0x0040) /* Peripheral increment mode */ #define DMA_CFG7_MINC ((uint16_t)0x0080) /* Memory increment mode */ #define DMA_CFG7_PSIZE ((uint16_t)0x0300) /* PSIZE[1:0] bits (Peripheral size) */ #define DMA_CFG7_PSIZE_0 ((uint16_t)0x0100) /* Bit 0 */ #define DMA_CFG7_PSIZE_1 ((uint16_t)0x0200) /* Bit 1 */ #define DMA_CFG7_MSIZE ((uint16_t)0x0C00) /* MSIZE[1:0] bits (Memory size) */ #define DMA_CFG7_MSIZE_0 ((uint16_t)0x0400) /* Bit 0 */ #define DMA_CFG7_MSIZE_1 ((uint16_t)0x0800) /* Bit 1 */ #define DMA_CFG7_PL ((uint16_t)0x3000) /* PL[1:0] bits (Channel Priority level) */ #define DMA_CFG7_PL_0 ((uint16_t)0x1000) /* Bit 0 */ #define DMA_CFG7_PL_1 ((uint16_t)0x2000) /* Bit 1 */ #define DMA_CFG7_MEM2MEM ((uint16_t)0x4000) /* Memory to memory mode enable */ /******************* Bit definition for DMA_CFG8 register *******************/ #define DMA_CFG8_EN ((uint16_t)0x0001) /* Channel enable */ #define DMA_CFG8_TCIE ((uint16_t)0x0002) /* Transfer complete interrupt enable */ #define DMA_CFG8_HTIE ((uint16_t)0x0004) /* Half Transfer interrupt enable */ #define DMA_CFG8_TEIE ((uint16_t)0x0008) /* Transfer error interrupt enable */ #define DMA_CFG8_DIR ((uint16_t)0x0010) /* Data transfer direction */ #define DMA_CFG8_CIRC ((uint16_t)0x0020) /* Circular mode */ #define DMA_CFG8_PINC ((uint16_t)0x0040) /* Peripheral increment mode */ #define DMA_CFG8_MINC ((uint16_t)0x0080) /* Memory increment mode */ #define DMA_CFG8_PSIZE ((uint16_t)0x0300) /* PSIZE[1:0] bits (Peripheral size) */ #define DMA_CFG8_PSIZE_0 ((uint16_t)0x0100) /* Bit 0 */ #define DMA_CFG8_PSIZE_1 ((uint16_t)0x0200) /* Bit 1 */ #define DMA_CFG8_MSIZE ((uint16_t)0x0C00) /* MSIZE[1:0] bits (Memory size) */ #define DMA_CFG8_MSIZE_0 ((uint16_t)0x0400) /* Bit 0 */ #define DMA_CFG8_MSIZE_1 ((uint16_t)0x0800) /* Bit 1 */ #define DMA_CFG8_PL ((uint16_t)0x3000) /* PL[1:0] bits (Channel Priority level) */ #define DMA_CFG8_PL_0 ((uint16_t)0x1000) /* Bit 0 */ #define DMA_CFG8_PL_1 ((uint16_t)0x2000) /* Bit 1 */ #define DMA_CFG8_MEM2MEM ((uint16_t)0x4000) /* Memory to memory mode enable */ /****************** Bit definition for DMA_CNTR1 register ******************/ #define DMA_CNTR1_NDT ((uint16_t)0xFFFF) /* Number of data to Transfer */ /****************** Bit definition for DMA_CNTR2 register ******************/ #define DMA_CNTR2_NDT ((uint16_t)0xFFFF) /* Number of data to Transfer */ /****************** Bit definition for DMA_CNTR3 register ******************/ #define DMA_CNTR3_NDT ((uint16_t)0xFFFF) /* Number of data to Transfer */ /****************** Bit definition for DMA_CNTR4 register ******************/ #define DMA_CNTR4_NDT ((uint16_t)0xFFFF) /* Number of data to Transfer */ /****************** Bit definition for DMA_CNTR5 register ******************/ #define DMA_CNTR5_NDT ((uint16_t)0xFFFF) /* Number of data to Transfer */ /****************** Bit definition for DMA_CNTR6 register ******************/ #define DMA_CNTR6_NDT ((uint16_t)0xFFFF) /* Number of data to Transfer */ /****************** Bit definition for DMA_CNTR7 register ******************/ #define DMA_CNTR7_NDT ((uint16_t)0xFFFF) /* Number of data to Transfer */ /****************** Bit definition for DMA_CNTR8 register ******************/ #define DMA_CNTR8_NDT ((uint16_t)0xFFFF) /* Number of data to Transfer */ /****************** Bit definition for DMA_PADDR1 register *******************/ #define DMA_PADDR1_PA ((uint32_t)0xFFFFFFFF) /* Peripheral Address */ /****************** Bit definition for DMA_PADDR2 register *******************/ #define DMA_PADDR2_PA ((uint32_t)0xFFFFFFFF) /* Peripheral Address */ /****************** Bit definition for DMA_PADDR3 register *******************/ #define DMA_PADDR3_PA ((uint32_t)0xFFFFFFFF) /* Peripheral Address */ /****************** Bit definition for DMA_PADDR4 register *******************/ #define DMA_PADDR4_PA ((uint32_t)0xFFFFFFFF) /* Peripheral Address */ /****************** Bit definition for DMA_PADDR5 register *******************/ #define DMA_PADDR5_PA ((uint32_t)0xFFFFFFFF) /* Peripheral Address */ /****************** Bit definition for DMA_PADDR6 register *******************/ #define DMA_PADDR6_PA ((uint32_t)0xFFFFFFFF) /* Peripheral Address */ /****************** Bit definition for DMA_PADDR7 register *******************/ #define DMA_PADDR7_PA ((uint32_t)0xFFFFFFFF) /* Peripheral Address */ /****************** Bit definition for DMA_PADDR8 register *******************/ #define DMA_PADDR8_PA ((uint32_t)0xFFFFFFFF) /* Peripheral Address */ /****************** Bit definition for DMA_MADDR1 register *******************/ #define DMA_MADDR1_MA ((uint32_t)0xFFFFFFFF) /* Memory Address */ /****************** Bit definition for DMA_MADDR2 register *******************/ #define DMA_MADDR2_MA ((uint32_t)0xFFFFFFFF) /* Memory Address */ /****************** Bit definition for DMA_MADDR3 register *******************/ #define DMA_MADDR3_MA ((uint32_t)0xFFFFFFFF) /* Memory Address */ /****************** Bit definition for DMA_MADDR4 register *******************/ #define DMA_MADDR4_MA ((uint32_t)0xFFFFFFFF) /* Memory Address */ /****************** Bit definition for DMA_MADDR5 register *******************/ #define DMA_MADDR5_MA ((uint32_t)0xFFFFFFFF) /* Memory Address */ /****************** Bit definition for DMA_MADDR6 register *******************/ #define DMA_MADDR6_MA ((uint32_t)0xFFFFFFFF) /* Memory Address */ /****************** Bit definition for DMA_MADDR7 register *******************/ #define DMA_MADDR7_MA ((uint32_t)0xFFFFFFFF) /* Memory Address */ /****************** Bit definition for DMA_MADDR8 register *******************/ #define DMA_MADDR8_MA ((uint32_t)0xFFFFFFFF) /* Memory Address */ /******************************************************************************/ /* External Interrupt/Event Controller */ /******************************************************************************/ /******************* Bit definition for EXTI_INTENR register *******************/ #define EXTI_INTENR_MR0 ((uint32_t)0x00000001) /* Interrupt Mask on line 0 */ #define EXTI_INTENR_MR1 ((uint32_t)0x00000002) /* Interrupt Mask on line 1 */ #define EXTI_INTENR_MR2 ((uint32_t)0x00000004) /* Interrupt Mask on line 2 */ #define EXTI_INTENR_MR3 ((uint32_t)0x00000008) /* Interrupt Mask on line 3 */ #define EXTI_INTENR_MR4 ((uint32_t)0x00000010) /* Interrupt Mask on line 4 */ #define EXTI_INTENR_MR5 ((uint32_t)0x00000020) /* Interrupt Mask on line 5 */ #define EXTI_INTENR_MR6 ((uint32_t)0x00000040) /* Interrupt Mask on line 6 */ #define EXTI_INTENR_MR7 ((uint32_t)0x00000080) /* Interrupt Mask on line 7 */ #define EXTI_INTENR_MR8 ((uint32_t)0x00000100) /* Interrupt Mask on line 8 */ #define EXTI_INTENR_MR9 ((uint32_t)0x00000200) /* Interrupt Mask on line 9 */ #define EXTI_INTENR_MR10 ((uint32_t)0x00000400) /* Interrupt Mask on line 10 */ #define EXTI_INTENR_MR11 ((uint32_t)0x00000800) /* Interrupt Mask on line 11 */ #define EXTI_INTENR_MR12 ((uint32_t)0x00001000) /* Interrupt Mask on line 12 */ #define EXTI_INTENR_MR13 ((uint32_t)0x00002000) /* Interrupt Mask on line 13 */ #define EXTI_INTENR_MR14 ((uint32_t)0x00004000) /* Interrupt Mask on line 14 */ #define EXTI_INTENR_MR15 ((uint32_t)0x00008000) /* Interrupt Mask on line 15 */ #define EXTI_INTENR_MR16 ((uint32_t)0x00010000) /* Interrupt Mask on line 16 */ #define EXTI_INTENR_MR17 ((uint32_t)0x00020000) /* Interrupt Mask on line 17 */ #define EXTI_INTENR_MR18 ((uint32_t)0x00040000) /* Interrupt Mask on line 18 */ #define EXTI_INTENR_MR19 ((uint32_t)0x00080000) /* Interrupt Mask on line 19 */ #define EXTI_INTENR_MR20 ((uint32_t)0x00100000) /* Interrupt Mask on line 20 */ #define EXTI_INTENR_MR21 ((uint32_t)0x00200000) /* Interrupt Mask on line 21 */ #define EXTI_INTENR_MR22 ((uint32_t)0x00400000) /* Interrupt Mask on line 22 */ #define EXTI_INTENR_MR23 ((uint32_t)0x00800000) /* Interrupt Mask on line 23 */ #define EXTI_INTENR_MR24 ((uint32_t)0x01000000) /* Interrupt Mask on line 24 */ #define EXTI_INTENR_MR25 ((uint32_t)0x02000000) /* Interrupt Mask on line 25 */ #define EXTI_INTENR_MR26 ((uint32_t)0x04000000) /* Interrupt Mask on line 26 */ #define EXTI_INTENR_MR27 ((uint32_t)0x08000000) /* Interrupt Mask on line 27 */ #define EXTI_INTENR_MR28 ((uint32_t)0x10000000) /* Interrupt Mask on line 28 */ #define EXTI_INTENR_MR29 ((uint32_t)0x20000000) /* Interrupt Mask on line 29 */ /******************* Bit definition for EXTI_EVENR register *******************/ #define EXTI_EVENR_MR0 ((uint32_t)0x00000001) /* Event Mask on line 0 */ #define EXTI_EVENR_MR1 ((uint32_t)0x00000002) /* Event Mask on line 1 */ #define EXTI_EVENR_MR2 ((uint32_t)0x00000004) /* Event Mask on line 2 */ #define EXTI_EVENR_MR3 ((uint32_t)0x00000008) /* Event Mask on line 3 */ #define EXTI_EVENR_MR4 ((uint32_t)0x00000010) /* Event Mask on line 4 */ #define EXTI_EVENR_MR5 ((uint32_t)0x00000020) /* Event Mask on line 5 */ #define EXTI_EVENR_MR6 ((uint32_t)0x00000040) /* Event Mask on line 6 */ #define EXTI_EVENR_MR7 ((uint32_t)0x00000080) /* Event Mask on line 7 */ #define EXTI_EVENR_MR8 ((uint32_t)0x00000100) /* Event Mask on line 8 */ #define EXTI_EVENR_MR9 ((uint32_t)0x00000200) /* Event Mask on line 9 */ #define EXTI_EVENR_MR10 ((uint32_t)0x00000400) /* Event Mask on line 10 */ #define EXTI_EVENR_MR11 ((uint32_t)0x00000800) /* Event Mask on line 11 */ #define EXTI_EVENR_MR12 ((uint32_t)0x00001000) /* Event Mask on line 12 */ #define EXTI_EVENR_MR13 ((uint32_t)0x00002000) /* Event Mask on line 13 */ #define EXTI_EVENR_MR14 ((uint32_t)0x00004000) /* Event Mask on line 14 */ #define EXTI_EVENR_MR15 ((uint32_t)0x00008000) /* Event Mask on line 15 */ #define EXTI_EVENR_MR16 ((uint32_t)0x00010000) /* Event Mask on line 16 */ #define EXTI_EVENR_MR17 ((uint32_t)0x00020000) /* Event Mask on line 17 */ #define EXTI_EVENR_MR18 ((uint32_t)0x00040000) /* Event Mask on line 18 */ #define EXTI_EVENR_MR19 ((uint32_t)0x00080000) /* Event Mask on line 19 */ #define EXTI_EVENR_MR20 ((uint32_t)0x00100000) /* Event Mask on line 20 */ #define EXTI_EVENR_MR21 ((uint32_t)0x00200000) /* Event Mask on line 21 */ #define EXTI_EVENR_MR22 ((uint32_t)0x00400000) /* Event Mask on line 22 */ #define EXTI_EVENR_MR23 ((uint32_t)0x00800000) /* Event Mask on line 23 */ #define EXTI_EVENR_MR24 ((uint32_t)0x01000000) /* Event Mask on line 24 */ #define EXTI_EVENR_MR25 ((uint32_t)0x02000000) /* Event Mask on line 25 */ #define EXTI_EVENR_MR26 ((uint32_t)0x04000000) /* Event Mask on line 26 */ #define EXTI_EVENR_MR27 ((uint32_t)0x08000000) /* Event Mask on line 27 */ #define EXTI_EVENR_MR28 ((uint32_t)0x10000000) /* Event Mask on line 28 */ #define EXTI_EVENR_MR29 ((uint32_t)0x20000000) /* Event Mask on line 29 */ /****************** Bit definition for EXTI_RTENR register *******************/ #define EXTI_RTENR_TR0 ((uint32_t)0x00000001) /* Rising trigger event configuration bit of line 0 */ #define EXTI_RTENR_TR1 ((uint32_t)0x00000002) /* Rising trigger event configuration bit of line 1 */ #define EXTI_RTENR_TR2 ((uint32_t)0x00000004) /* Rising trigger event configuration bit of line 2 */ #define EXTI_RTENR_TR3 ((uint32_t)0x00000008) /* Rising trigger event configuration bit of line 3 */ #define EXTI_RTENR_TR4 ((uint32_t)0x00000010) /* Rising trigger event configuration bit of line 4 */ #define EXTI_RTENR_TR5 ((uint32_t)0x00000020) /* Rising trigger event configuration bit of line 5 */ #define EXTI_RTENR_TR6 ((uint32_t)0x00000040) /* Rising trigger event configuration bit of line 6 */ #define EXTI_RTENR_TR7 ((uint32_t)0x00000080) /* Rising trigger event configuration bit of line 7 */ #define EXTI_RTENR_TR8 ((uint32_t)0x00000100) /* Rising trigger event configuration bit of line 8 */ #define EXTI_RTENR_TR9 ((uint32_t)0x00000200) /* Rising trigger event configuration bit of line 9 */ #define EXTI_RTENR_TR10 ((uint32_t)0x00000400) /* Rising trigger event configuration bit of line 10 */ #define EXTI_RTENR_TR11 ((uint32_t)0x00000800) /* Rising trigger event configuration bit of line 11 */ #define EXTI_RTENR_TR12 ((uint32_t)0x00001000) /* Rising trigger event configuration bit of line 12 */ #define EXTI_RTENR_TR13 ((uint32_t)0x00002000) /* Rising trigger event configuration bit of line 13 */ #define EXTI_RTENR_TR14 ((uint32_t)0x00004000) /* Rising trigger event configuration bit of line 14 */ #define EXTI_RTENR_TR15 ((uint32_t)0x00008000) /* Rising trigger event configuration bit of line 15 */ #define EXTI_RTENR_TR16 ((uint32_t)0x00010000) /* Rising trigger event configuration bit of line 16 */ #define EXTI_RTENR_TR17 ((uint32_t)0x00020000) /* Rising trigger event configuration bit of line 17 */ #define EXTI_RTENR_TR18 ((uint32_t)0x00040000) /* Rising trigger event configuration bit of line 18 */ #define EXTI_RTENR_TR19 ((uint32_t)0x00080000) /* Rising trigger event configuration bit of line 19 */ #define EXTI_RTENR_TR20 ((uint32_t)0x00100000) /* Rising trigger event configuration bit of line 20 */ #define EXTI_RTENR_TR21 ((uint32_t)0x00200000) /* Rising trigger event configuration bit of line 21 */ #define EXTI_RTENR_TR22 ((uint32_t)0x00400000) /* Rising trigger event configuration bit of line 22 */ #define EXTI_RTENR_TR23 ((uint32_t)0x00800000) /* Rising trigger event configuration bit of line 23 */ #define EXTI_RTENR_TR24 ((uint32_t)0x01000000) /* Rising trigger event configuration bit of line 24 */ #define EXTI_RTENR_TR25 ((uint32_t)0x02000000) /* Rising trigger event configuration bit of line 25 */ #define EXTI_RTENR_TR26 ((uint32_t)0x04000000) /* Rising trigger event configuration bit of line 26 */ #define EXTI_RTENR_TR27 ((uint32_t)0x08000000) /* Rising trigger event configuration bit of line 27 */ #define EXTI_RTENR_TR28 ((uint32_t)0x10000000) /* Rising trigger event configuration bit of line 28 */ #define EXTI_RTENR_TR29 ((uint32_t)0x20000000) /* Rising trigger event configuration bit of line 29 */ /****************** Bit definition for EXTI_FTENR register *******************/ #define EXTI_FTENR_TR0 ((uint32_t)0x00000001) /* Falling trigger event configuration bit of line 0 */ #define EXTI_FTENR_TR1 ((uint32_t)0x00000002) /* Falling trigger event configuration bit of line 1 */ #define EXTI_FTENR_TR2 ((uint32_t)0x00000004) /* Falling trigger event configuration bit of line 2 */ #define EXTI_FTENR_TR3 ((uint32_t)0x00000008) /* Falling trigger event configuration bit of line 3 */ #define EXTI_FTENR_TR4 ((uint32_t)0x00000010) /* Falling trigger event configuration bit of line 4 */ #define EXTI_FTENR_TR5 ((uint32_t)0x00000020) /* Falling trigger event configuration bit of line 5 */ #define EXTI_FTENR_TR6 ((uint32_t)0x00000040) /* Falling trigger event configuration bit of line 6 */ #define EXTI_FTENR_TR7 ((uint32_t)0x00000080) /* Falling trigger event configuration bit of line 7 */ #define EXTI_FTENR_TR8 ((uint32_t)0x00000100) /* Falling trigger event configuration bit of line 8 */ #define EXTI_FTENR_TR9 ((uint32_t)0x00000200) /* Falling trigger event configuration bit of line 9 */ #define EXTI_FTENR_TR10 ((uint32_t)0x00000400) /* Falling trigger event configuration bit of line 10 */ #define EXTI_FTENR_TR11 ((uint32_t)0x00000800) /* Falling trigger event configuration bit of line 11 */ #define EXTI_FTENR_TR12 ((uint32_t)0x00001000) /* Falling trigger event configuration bit of line 12 */ #define EXTI_FTENR_TR13 ((uint32_t)0x00002000) /* Falling trigger event configuration bit of line 13 */ #define EXTI_FTENR_TR14 ((uint32_t)0x00004000) /* Falling trigger event configuration bit of line 14 */ #define EXTI_FTENR_TR15 ((uint32_t)0x00008000) /* Falling trigger event configuration bit of line 15 */ #define EXTI_FTENR_TR16 ((uint32_t)0x00010000) /* Falling trigger event configuration bit of line 16 */ #define EXTI_FTENR_TR17 ((uint32_t)0x00020000) /* Falling trigger event configuration bit of line 17 */ #define EXTI_FTENR_TR18 ((uint32_t)0x00040000) /* Falling trigger event configuration bit of line 18 */ #define EXTI_FTENR_TR19 ((uint32_t)0x00080000) /* Falling trigger event configuration bit of line 19 */ #define EXTI_FTENR_TR20 ((uint32_t)0x00100000) /* Falling trigger event configuration bit of line 20 */ #define EXTI_FTENR_TR21 ((uint32_t)0x00200000) /* Falling trigger event configuration bit of line 21 */ #define EXTI_FTENR_TR22 ((uint32_t)0x00400000) /* Falling trigger event configuration bit of line 22 */ #define EXTI_FTENR_TR23 ((uint32_t)0x00800000) /* Falling trigger event configuration bit of line 23 */ #define EXTI_FTENR_TR24 ((uint32_t)0x01000000) /* Falling trigger event configuration bit of line 24 */ #define EXTI_FTENR_TR25 ((uint32_t)0x02000000) /* Falling trigger event configuration bit of line 25 */ #define EXTI_FTENR_TR26 ((uint32_t)0x04000000) /* Falling trigger event configuration bit of line 26 */ #define EXTI_FTENR_TR27 ((uint32_t)0x08000000) /* Falling trigger event configuration bit of line 27 */ #define EXTI_FTENR_TR28 ((uint32_t)0x10000000) /* Falling trigger event configuration bit of line 28 */ #define EXTI_FTENR_TR29 ((uint32_t)0x20000000) /* Falling trigger event configuration bit of line 29 */ /****************** Bit definition for EXTI_SWIEVR register ******************/ #define EXTI_SWIEVR_SWIEVR0 ((uint32_t)0x00000001) /* Software Interrupt on line 0 */ #define EXTI_SWIEVR_SWIEVR1 ((uint32_t)0x00000002) /* Software Interrupt on line 1 */ #define EXTI_SWIEVR_SWIEVR2 ((uint32_t)0x00000004) /* Software Interrupt on line 2 */ #define EXTI_SWIEVR_SWIEVR3 ((uint32_t)0x00000008) /* Software Interrupt on line 3 */ #define EXTI_SWIEVR_SWIEVR4 ((uint32_t)0x00000010) /* Software Interrupt on line 4 */ #define EXTI_SWIEVR_SWIEVR5 ((uint32_t)0x00000020) /* Software Interrupt on line 5 */ #define EXTI_SWIEVR_SWIEVR6 ((uint32_t)0x00000040) /* Software Interrupt on line 6 */ #define EXTI_SWIEVR_SWIEVR7 ((uint32_t)0x00000080) /* Software Interrupt on line 7 */ #define EXTI_SWIEVR_SWIEVR8 ((uint32_t)0x00000100) /* Software Interrupt on line 8 */ #define EXTI_SWIEVR_SWIEVR9 ((uint32_t)0x00000200) /* Software Interrupt on line 9 */ #define EXTI_SWIEVR_SWIEVR10 ((uint32_t)0x00000400) /* Software Interrupt on line 10 */ #define EXTI_SWIEVR_SWIEVR11 ((uint32_t)0x00000800) /* Software Interrupt on line 11 */ #define EXTI_SWIEVR_SWIEVR12 ((uint32_t)0x00001000) /* Software Interrupt on line 12 */ #define EXTI_SWIEVR_SWIEVR13 ((uint32_t)0x00002000) /* Software Interrupt on line 13 */ #define EXTI_SWIEVR_SWIEVR14 ((uint32_t)0x00004000) /* Software Interrupt on line 14 */ #define EXTI_SWIEVR_SWIEVR15 ((uint32_t)0x00008000) /* Software Interrupt on line 15 */ #define EXTI_SWIEVR_SWIEVR16 ((uint32_t)0x00010000) /* Software Interrupt on line 16 */ #define EXTI_SWIEVR_SWIEVR17 ((uint32_t)0x00020000) /* Software Interrupt on line 17 */ #define EXTI_SWIEVR_SWIEVR18 ((uint32_t)0x00040000) /* Software Interrupt on line 18 */ #define EXTI_SWIEVR_SWIEVR19 ((uint32_t)0x00080000) /* Software Interrupt on line 19 */ #define EXTI_SWIEVR_SWIEVR20 ((uint32_t)0x00100000) /* Software Interrupt on line 20 */ #define EXTI_SWIEVR_SWIEVR21 ((uint32_t)0x00200000) /* Software Interrupt on line 21 */ #define EXTI_SWIEVR_SWIEVR22 ((uint32_t)0x00400000) /* Software Interrupt on line 22 */ #define EXTI_SWIEVR_SWIEVR23 ((uint32_t)0x00800000) /* Software Interrupt on line 23 */ #define EXTI_SWIEVR_SWIEVR24 ((uint32_t)0x01000000) /* Software Interrupt on line 24 */ #define EXTI_SWIEVR_SWIEVR25 ((uint32_t)0x02000000) /* Software Interrupt on line 25 */ #define EXTI_SWIEVR_SWIEVR26 ((uint32_t)0x04000000) /* Software Interrupt on line 26 */ #define EXTI_SWIEVR_SWIEVR27 ((uint32_t)0x08000000) /* Software Interrupt on line 27 */ #define EXTI_SWIEVR_SWIEVR28 ((uint32_t)0x10000000) /* Software Interrupt on line 28 */ #define EXTI_SWIEVR_SWIEVR29 ((uint32_t)0x20000000) /* Software Interrupt on line 29 */ /******************* Bit definition for EXTI_INTFR register ********************/ #define EXTI_INTF_INTF0 ((uint32_t)0x00000001) /* Pending bit for line 0 */ #define EXTI_INTF_INTF1 ((uint32_t)0x00000002) /* Pending bit for line 1 */ #define EXTI_INTF_INTF2 ((uint32_t)0x00000004) /* Pending bit for line 2 */ #define EXTI_INTF_INTF3 ((uint32_t)0x00000008) /* Pending bit for line 3 */ #define EXTI_INTF_INTF4 ((uint32_t)0x00000010) /* Pending bit for line 4 */ #define EXTI_INTF_INTF5 ((uint32_t)0x00000020) /* Pending bit for line 5 */ #define EXTI_INTF_INTF6 ((uint32_t)0x00000040) /* Pending bit for line 6 */ #define EXTI_INTF_INTF7 ((uint32_t)0x00000080) /* Pending bit for line 7 */ #define EXTI_INTF_INTF8 ((uint32_t)0x00000100) /* Pending bit for line 8 */ #define EXTI_INTF_INTF9 ((uint32_t)0x00000200) /* Pending bit for line 9 */ #define EXTI_INTF_INTF10 ((uint32_t)0x00000400) /* Pending bit for line 10 */ #define EXTI_INTF_INTF11 ((uint32_t)0x00000800) /* Pending bit for line 11 */ #define EXTI_INTF_INTF12 ((uint32_t)0x00001000) /* Pending bit for line 12 */ #define EXTI_INTF_INTF13 ((uint32_t)0x00002000) /* Pending bit for line 13 */ #define EXTI_INTF_INTF14 ((uint32_t)0x00004000) /* Pending bit for line 14 */ #define EXTI_INTF_INTF15 ((uint32_t)0x00008000) /* Pending bit for line 15 */ #define EXTI_INTF_INTF16 ((uint32_t)0x00010000) /* Pending bit for line 16 */ #define EXTI_INTF_INTF17 ((uint32_t)0x00020000) /* Pending bit for line 17 */ #define EXTI_INTF_INTF18 ((uint32_t)0x00040000) /* Pending bit for line 18 */ #define EXTI_INTF_INTF19 ((uint32_t)0x00080000) /* Pending bit for line 19 */ #define EXTI_INTF_INTF20 ((uint32_t)0x00100000) /* Pending bit for line 20 */ #define EXTI_INTF_INTF21 ((uint32_t)0x00200000) /* Pending bit for line 21 */ #define EXTI_INTF_INTF22 ((uint32_t)0x00400000) /* Pending bit for line 22 */ #define EXTI_INTF_INTF23 ((uint32_t)0x00800000) /* Pending bit for line 23 */ #define EXTI_INTF_INTF24 ((uint32_t)0x01000000) /* Pending bit for line 24 */ #define EXTI_INTF_INTF25 ((uint32_t)0x02000000) /* Pending bit for line 25 */ #define EXTI_INTF_INTF26 ((uint32_t)0x04000000) /* Pending bit for line 26 */ #define EXTI_INTF_INTF27 ((uint32_t)0x08000000) /* Pending bit for line 27 */ #define EXTI_INTF_INTF28 ((uint32_t)0x10000000) /* Pending bit for line 28 */ #define EXTI_INTF_INTF29 ((uint32_t)0x20000000) /* Pending bit for line 29 */ /******************************************************************************/ /* FLASH and Option Bytes Registers */ /******************************************************************************/ /******************* Bit definition for FLASH_ACTLR register ******************/ #define FLASH_ACTLR_LATENCY ((uint8_t)0x03) /* LATENCY[2:0] bits (Latency) */ #define FLASH_ACTLR_LATENCY_0 ((uint8_t)0x00) /* Bit 0 */ #define FLASH_ACTLR_LATENCY_1 ((uint8_t)0x01) /* Bit 0 */ #define FLASH_ACTLR_LATENCY_2 ((uint8_t)0x02) /* Bit 1 */ /****************** Bit definition for FLASH_KEYR register ******************/ #define FLASH_KEYR_FKEYR ((uint32_t)0xFFFFFFFF) /* FPEC Key */ /***************** Bit definition for FLASH_OBKEYR register ****************/ #define FLASH_OBKEYR_OBKEYR ((uint32_t)0xFFFFFFFF) /* Option Byte Key */ /****************** Bit definition for FLASH_STATR register *******************/ #define FLASH_STATR_BSY ((uint8_t)0x01) /* Busy */ #define FLASH_STATR_PGERR ((uint8_t)0x04) /* Programming Error */ #define FLASH_STATR_WRPRTERR ((uint8_t)0x10) /* Write Protection Error */ #define FLASH_STATR_EOP ((uint8_t)0x20) /* End of operation */ #define FLASH_STATR_FWAKE_FLAG ((uint8_t)0x40) /* Flag of wake */ #define FLASH_STATR_TURBO ((uint8_t)0x80) /* The state of TURBO Enable */ #define FLASH_STATR_BOOT_AVA ((uint16_t)0x100) /* The state of Init Config */ #define FLASH_STATR_BOOT_STATUS ((uint16_t)0x200) /* The source of Execute Program */ #define FLASH_STATR_BOOT_MODE ((uint16_t)0x400) /* The switch of user section or boot section*/ #define FLASH_STATR_BOOT_LOCK ((uint16_t)0x800) /* Lock boot area*/ /******************* Bit definition for FLASH_CTLR register *******************/ #define FLASH_CTLR_PG ((uint32_t)0x00000001) /* Programming */ #define FLASH_CTLR_PER ((uint32_t)0x00000002) /* Sector Erase 4K */ #define FLASH_CTLR_MER ((uint32_t)0x00000004) /* Mass Erase */ #define FLASH_CTLR_OPTPG ((uint32_t)0x00000010) /* Option Byte Programming */ #define FLASH_CTLR_OPTER ((uint32_t)0x00000020) /* Option Byte Erase */ #define FLASH_CTLR_STRT ((uint32_t)0x00000040) /* Start */ #define FLASH_CTLR_LOCK ((uint32_t)0x00000080) /* Lock */ #define FLASH_CTLR_OBWRE ((uint32_t)0x00000200) /* Option Bytes Write Enable */ #define FLASH_CTLR_ERRIE ((uint32_t)0x00000400) /* Error Interrupt Enable */ #define FLASH_CTLR_EOPIE ((uint32_t)0x00001000) /* End of operation interrupt enable */ #define FLASH_CTLR_FWAKEIE ((uint32_t)0x00002000) /* Wake inter Enable */ #define FLASH_CTLR_FLOCK ((uint32_t)0x00008000) /* Fast Lock */ #define FLASH_CTLR_FTPG ((uint32_t)0x00010000) /* Fast Program */ #define FLASH_CTLR_FTER ((uint32_t)0x00020000) /* Fast Erase */ #define FLASH_CTLR_BUFLOAD ((uint32_t)0x00040000) /* BUF Load */ #define FLASH_CTLR_BUFRST ((uint32_t)0x00080000) /* BUF Reset */ #define FLASH_CTLR_BER32 ((uint32_t)0x00800000) /* Block Erase 32K */ /******************* Bit definition for FLASH_ADDR register *******************/ #define FLASH_ADDR_FAR ((uint32_t)0xFFFFFFFF) /* Flash Address */ /****************** Bit definition for FLASH_OBR register *******************/ #define FLASH_OBR_OPTERR ((uint16_t)0x0001) /* Option Byte Error */ #define FLASH_OBR_RDPRT ((uint16_t)0x0002) /* Read protection */ #define FLASH_OBR_USER ((uint16_t)0x007C) /* User Option Bytes */ #define FLASH_OBR_WDG_SW ((uint16_t)0x0004) /* WDG_SW */ #define FLASH_OBR_nRST_STOP ((uint16_t)0x0008) /* nRST_STOP */ #define FLASH_OBR_nRST_STDBY ((uint16_t)0x0010) /* nRST_STDBY */ #define FLASH_OBR_CFGRSTT ((uint16_t)0x0060) /* Config Reset delay time */ #define FLASH_OBR_FIX_11 ((uint16_t)0x0300) /* fix 11 */ #define FLASH_OBR_DATA0 ((uint32_t)0x3FC00) /* Data byte0 */ #define FLASH_OBR_DATA1 ((uint32_t)0x3FC0000) /* Data byte1 */ /****************** Bit definition for FLASH_WPR register ******************/ #define FLASH_WPR_WRP ((uint32_t)0xFFFFFFFF) /* Write Protect */ /****************** Bit definition for FLASH_MODEKEYR register ******************/ #define FLASH_MODEKEYR_MODEKEYR ((uint32_t)0xFFFFFFFF) /* Open fast program /erase */ #define FLASH_MODEKEYR_MODEKEYR1 ((uint32_t)0x45670123) #define FLASH_MODEKEYR_MODEKEYR2 ((uint32_t)0xCDEF89AB) /****************** Bit definition for BOOT_MODEKEYP register ******************/ #define BOOT_MODEKEYP_MODEKEYR ((uint32_t)0xFFFFFFFF) /* Open Boot section */ #define BOOT_MODEKEYP_MODEKEYR1 ((uint32_t)0x45670123) #define BOOT_MODEKEYP_MODEKEYR2 ((uint32_t)0xCDEF89AB) /****************** Bit definition for FLASH_RDPR register *******************/ #define FLASH_RDPR_RDPR ((uint32_t)0x000000FF) /* Read protection option byte */ #define FLASH_RDPR_nRDPR ((uint32_t)0x0000FF00) /* Read protection complemented option byte */ /****************** Bit definition for FLASH_USER register ******************/ #define FLASH_USER_USER ((uint32_t)0x00FF0000) /* User option byte */ #define FLASH_USER_nUSER ((uint32_t)0xFF000000) /* User complemented option byte */ /****************** Bit definition for FLASH_Data0 register *****************/ #define FLASH_Data0_Data0 ((uint32_t)0x000000FF) /* User data storage option byte */ #define FLASH_Data0_nData0 ((uint32_t)0x0000FF00) /* User data storage complemented option byte */ /****************** Bit definition for FLASH_Data1 register *****************/ #define FLASH_Data1_Data1 ((uint32_t)0x00FF0000) /* User data storage option byte */ #define FLASH_Data1_nData1 ((uint32_t)0xFF000000) /* User data storage complemented option byte */ /****************** Bit definition for FLASH_WRPR0 register ******************/ #define FLASH_WRPR0_WRPR0 ((uint32_t)0x000000FF) /* Flash memory write protection option bytes */ #define FLASH_WRPR0_nWRPR0 ((uint32_t)0x0000FF00) /* Flash memory write protection complemented option bytes */ /****************** Bit definition for FLASH_WRPR1 register ******************/ #define FLASH_WRPR1_WRPR1 ((uint32_t)0x00FF0000) /* Flash memory write protection option bytes */ #define FLASH_WRPR1_nWRPR1 ((uint32_t)0xFF000000) /* Flash memory write protection complemented option bytes */ /****************** Bit definition for FLASH_WRPR2 register ******************/ #define FLASH_WRPR2_WRPR2 ((uint32_t)0x000000FF) /* Flash memory write protection option bytes */ #define FLASH_WRPR2_nWRPR2 ((uint32_t)0x0000FF00) /* Flash memory write protection complemented option bytes */ /****************** Bit definition for FLASH_WRPR3 register ******************/ #define FLASH_WRPR3_WRPR3 ((uint32_t)0x00FF0000) /* Flash memory write protection option bytes */ #define FLASH_WRPR3_nWRPR3 ((uint32_t)0xFF000000) /* Flash memory write protection complemented option bytes */ /******************************* FLASH keys *********************************/ #define FLASH_KEY1 ((uint32_t)0x45670123) #define FLASH_KEY2 ((uint32_t)0xCDEF89AB) /******************************************************************************/ /* General Purpose and Alternate Function I/O */ /******************************************************************************/ /******************* Bit definition for GPIO_CFGLR register *******************/ #define GPIO_CFGLR_MODE ((uint32_t)0x33333333) /* Port x mode bits */ #define GPIO_CFGLR_MODE0 ((uint32_t)0x00000003) /* MODE0[1:0] bits (Port x mode bits, pin 0) */ #define GPIO_CFGLR_MODE0_0 ((uint32_t)0x00000001) /* Bit 0 */ #define GPIO_CFGLR_MODE0_1 ((uint32_t)0x00000002) /* Bit 1 */ #define GPIO_CFGLR_MODE1 ((uint32_t)0x00000030) /* MODE1[1:0] bits (Port x mode bits, pin 1) */ #define GPIO_CFGLR_MODE1_0 ((uint32_t)0x00000010) /* Bit 0 */ #define GPIO_CFGLR_MODE1_1 ((uint32_t)0x00000020) /* Bit 1 */ #define GPIO_CFGLR_MODE2 ((uint32_t)0x00000300) /* MODE2[1:0] bits (Port x mode bits, pin 2) */ #define GPIO_CFGLR_MODE2_0 ((uint32_t)0x00000100) /* Bit 0 */ #define GPIO_CFGLR_MODE2_1 ((uint32_t)0x00000200) /* Bit 1 */ #define GPIO_CFGLR_MODE3 ((uint32_t)0x00003000) /* MODE3[1:0] bits (Port x mode bits, pin 3) */ #define GPIO_CFGLR_MODE3_0 ((uint32_t)0x00001000) /* Bit 0 */ #define GPIO_CFGLR_MODE3_1 ((uint32_t)0x00002000) /* Bit 1 */ #define GPIO_CFGLR_MODE4 ((uint32_t)0x00030000) /* MODE4[1:0] bits (Port x mode bits, pin 4) */ #define GPIO_CFGLR_MODE4_0 ((uint32_t)0x00010000) /* Bit 0 */ #define GPIO_CFGLR_MODE4_1 ((uint32_t)0x00020000) /* Bit 1 */ #define GPIO_CFGLR_MODE5 ((uint32_t)0x00300000) /* MODE5[1:0] bits (Port x mode bits, pin 5) */ #define GPIO_CFGLR_MODE5_0 ((uint32_t)0x00100000) /* Bit 0 */ #define GPIO_CFGLR_MODE5_1 ((uint32_t)0x00200000) /* Bit 1 */ #define GPIO_CFGLR_MODE6 ((uint32_t)0x03000000) /* MODE6[1:0] bits (Port x mode bits, pin 6) */ #define GPIO_CFGLR_MODE6_0 ((uint32_t)0x01000000) /* Bit 0 */ #define GPIO_CFGLR_MODE6_1 ((uint32_t)0x02000000) /* Bit 1 */ #define GPIO_CFGLR_MODE7 ((uint32_t)0x30000000) /* MODE7[1:0] bits (Port x mode bits, pin 7) */ #define GPIO_CFGLR_MODE7_0 ((uint32_t)0x10000000) /* Bit 0 */ #define GPIO_CFGLR_MODE7_1 ((uint32_t)0x20000000) /* Bit 1 */ #define GPIO_CFGLR_CNF ((uint32_t)0xCCCCCCCC) /* Port x configuration bits */ #define GPIO_CFGLR_CNF0 ((uint32_t)0x0000000C) /* CNF0[1:0] bits (Port x configuration bits, pin 0) */ #define GPIO_CFGLR_CNF0_0 ((uint32_t)0x00000004) /* Bit 0 */ #define GPIO_CFGLR_CNF0_1 ((uint32_t)0x00000008) /* Bit 1 */ #define GPIO_CFGLR_CNF1 ((uint32_t)0x000000C0) /* CNF1[1:0] bits (Port x configuration bits, pin 1) */ #define GPIO_CFGLR_CNF1_0 ((uint32_t)0x00000040) /* Bit 0 */ #define GPIO_CFGLR_CNF1_1 ((uint32_t)0x00000080) /* Bit 1 */ #define GPIO_CFGLR_CNF2 ((uint32_t)0x00000C00) /* CNF2[1:0] bits (Port x configuration bits, pin 2) */ #define GPIO_CFGLR_CNF2_0 ((uint32_t)0x00000400) /* Bit 0 */ #define GPIO_CFGLR_CNF2_1 ((uint32_t)0x00000800) /* Bit 1 */ #define GPIO_CFGLR_CNF3 ((uint32_t)0x0000C000) /* CNF3[1:0] bits (Port x configuration bits, pin 3) */ #define GPIO_CFGLR_CNF3_0 ((uint32_t)0x00004000) /* Bit 0 */ #define GPIO_CFGLR_CNF3_1 ((uint32_t)0x00008000) /* Bit 1 */ #define GPIO_CFGLR_CNF4 ((uint32_t)0x000C0000) /* CNF4[1:0] bits (Port x configuration bits, pin 4) */ #define GPIO_CFGLR_CNF4_0 ((uint32_t)0x00040000) /* Bit 0 */ #define GPIO_CFGLR_CNF4_1 ((uint32_t)0x00080000) /* Bit 1 */ #define GPIO_CFGLR_CNF5 ((uint32_t)0x00C00000) /* CNF5[1:0] bits (Port x configuration bits, pin 5) */ #define GPIO_CFGLR_CNF5_0 ((uint32_t)0x00400000) /* Bit 0 */ #define GPIO_CFGLR_CNF5_1 ((uint32_t)0x00800000) /* Bit 1 */ #define GPIO_CFGLR_CNF6 ((uint32_t)0x0C000000) /* CNF6[1:0] bits (Port x configuration bits, pin 6) */ #define GPIO_CFGLR_CNF6_0 ((uint32_t)0x04000000) /* Bit 0 */ #define GPIO_CFGLR_CNF6_1 ((uint32_t)0x08000000) /* Bit 1 */ #define GPIO_CFGLR_CNF7 ((uint32_t)0xC0000000) /* CNF7[1:0] bits (Port x configuration bits, pin 7) */ #define GPIO_CFGLR_CNF7_0 ((uint32_t)0x40000000) /* Bit 0 */ #define GPIO_CFGLR_CNF7_1 ((uint32_t)0x80000000) /* Bit 1 */ /******************* Bit definition for GPIO_CFGHR register *******************/ #define GPIO_CFGHR_MODE ((uint32_t)0x33333333) /* Port x mode bits */ #define GPIO_CFGHR_MODE8 ((uint32_t)0x00000003) /* MODE8[1:0] bits (Port x mode bits, pin 8) */ #define GPIO_CFGHR_MODE8_0 ((uint32_t)0x00000001) /* Bit 0 */ #define GPIO_CFGHR_MODE8_1 ((uint32_t)0x00000002) /* Bit 1 */ #define GPIO_CFGHR_MODE9 ((uint32_t)0x00000030) /* MODE9[1:0] bits (Port x mode bits, pin 9) */ #define GPIO_CFGHR_MODE9_0 ((uint32_t)0x00000010) /* Bit 0 */ #define GPIO_CFGHR_MODE9_1 ((uint32_t)0x00000020) /* Bit 1 */ #define GPIO_CFGHR_MODE10 ((uint32_t)0x00000300) /* MODE10[1:0] bits (Port x mode bits, pin 10) */ #define GPIO_CFGHR_MODE10_0 ((uint32_t)0x00000100) /* Bit 0 */ #define GPIO_CFGHR_MODE10_1 ((uint32_t)0x00000200) /* Bit 1 */ #define GPIO_CFGHR_MODE11 ((uint32_t)0x00003000) /* MODE11[1:0] bits (Port x mode bits, pin 11) */ #define GPIO_CFGHR_MODE11_0 ((uint32_t)0x00001000) /* Bit 0 */ #define GPIO_CFGHR_MODE11_1 ((uint32_t)0x00002000) /* Bit 1 */ #define GPIO_CFGHR_MODE12 ((uint32_t)0x00030000) /* MODE12[1:0] bits (Port x mode bits, pin 12) */ #define GPIO_CFGHR_MODE12_0 ((uint32_t)0x00010000) /* Bit 0 */ #define GPIO_CFGHR_MODE12_1 ((uint32_t)0x00020000) /* Bit 1 */ #define GPIO_CFGHR_MODE13 ((uint32_t)0x00300000) /* MODE13[1:0] bits (Port x mode bits, pin 13) */ #define GPIO_CFGHR_MODE13_0 ((uint32_t)0x00100000) /* Bit 0 */ #define GPIO_CFGHR_MODE13_1 ((uint32_t)0x00200000) /* Bit 1 */ #define GPIO_CFGHR_MODE14 ((uint32_t)0x03000000) /* MODE14[1:0] bits (Port x mode bits, pin 14) */ #define GPIO_CFGHR_MODE14_0 ((uint32_t)0x01000000) /* Bit 0 */ #define GPIO_CFGHR_MODE14_1 ((uint32_t)0x02000000) /* Bit 1 */ #define GPIO_CFGHR_MODE15 ((uint32_t)0x30000000) /* MODE15[1:0] bits (Port x mode bits, pin 15) */ #define GPIO_CFGHR_MODE15_0 ((uint32_t)0x10000000) /* Bit 0 */ #define GPIO_CFGHR_MODE15_1 ((uint32_t)0x20000000) /* Bit 1 */ #define GPIO_CFGHR_CNF ((uint32_t)0xCCCCCCCC) /* Port x configuration bits */ #define GPIO_CFGHR_CNF8 ((uint32_t)0x0000000C) /* CNF8[1:0] bits (Port x configuration bits, pin 8) */ #define GPIO_CFGHR_CNF8_0 ((uint32_t)0x00000004) /* Bit 0 */ #define GPIO_CFGHR_CNF8_1 ((uint32_t)0x00000008) /* Bit 1 */ #define GPIO_CFGHR_CNF9 ((uint32_t)0x000000C0) /* CNF9[1:0] bits (Port x configuration bits, pin 9) */ #define GPIO_CFGHR_CNF9_0 ((uint32_t)0x00000040) /* Bit 0 */ #define GPIO_CFGHR_CNF9_1 ((uint32_t)0x00000080) /* Bit 1 */ #define GPIO_CFGHR_CNF10 ((uint32_t)0x00000C00) /* CNF10[1:0] bits (Port x configuration bits, pin 10) */ #define GPIO_CFGHR_CNF10_0 ((uint32_t)0x00000400) /* Bit 0 */ #define GPIO_CFGHR_CNF10_1 ((uint32_t)0x00000800) /* Bit 1 */ #define GPIO_CFGHR_CNF11 ((uint32_t)0x0000C000) /* CNF11[1:0] bits (Port x configuration bits, pin 11) */ #define GPIO_CFGHR_CNF11_0 ((uint32_t)0x00004000) /* Bit 0 */ #define GPIO_CFGHR_CNF11_1 ((uint32_t)0x00008000) /* Bit 1 */ #define GPIO_CFGHR_CNF12 ((uint32_t)0x000C0000) /* CNF12[1:0] bits (Port x configuration bits, pin 12) */ #define GPIO_CFGHR_CNF12_0 ((uint32_t)0x00040000) /* Bit 0 */ #define GPIO_CFGHR_CNF12_1 ((uint32_t)0x00080000) /* Bit 1 */ #define GPIO_CFGHR_CNF13 ((uint32_t)0x00C00000) /* CNF13[1:0] bits (Port x configuration bits, pin 13) */ #define GPIO_CFGHR_CNF13_0 ((uint32_t)0x00400000) /* Bit 0 */ #define GPIO_CFGHR_CNF13_1 ((uint32_t)0x00800000) /* Bit 1 */ #define GPIO_CFGHR_CNF14 ((uint32_t)0x0C000000) /* CNF14[1:0] bits (Port x configuration bits, pin 14) */ #define GPIO_CFGHR_CNF14_0 ((uint32_t)0x04000000) /* Bit 0 */ #define GPIO_CFGHR_CNF14_1 ((uint32_t)0x08000000) /* Bit 1 */ #define GPIO_CFGHR_CNF15 ((uint32_t)0xC0000000) /* CNF15[1:0] bits (Port x configuration bits, pin 15) */ #define GPIO_CFGHR_CNF15_0 ((uint32_t)0x40000000) /* Bit 0 */ #define GPIO_CFGHR_CNF15_1 ((uint32_t)0x80000000) /* Bit 1 */ /******************* Bit definition for GPIO_INDR register *******************/ #define GPIO_INDR_IDR0 ((uint16_t)0x0001) /* Port input data, bit 0 */ #define GPIO_INDR_IDR1 ((uint16_t)0x0002) /* Port input data, bit 1 */ #define GPIO_INDR_IDR2 ((uint16_t)0x0004) /* Port input data, bit 2 */ #define GPIO_INDR_IDR3 ((uint16_t)0x0008) /* Port input data, bit 3 */ #define GPIO_INDR_IDR4 ((uint16_t)0x0010) /* Port input data, bit 4 */ #define GPIO_INDR_IDR5 ((uint16_t)0x0020) /* Port input data, bit 5 */ #define GPIO_INDR_IDR6 ((uint16_t)0x0040) /* Port input data, bit 6 */ #define GPIO_INDR_IDR7 ((uint16_t)0x0080) /* Port input data, bit 7 */ #define GPIO_INDR_IDR8 ((uint16_t)0x0100) /* Port input data, bit 8 */ #define GPIO_INDR_IDR9 ((uint16_t)0x0200) /* Port input data, bit 9 */ #define GPIO_INDR_IDR10 ((uint16_t)0x0400) /* Port input data, bit 10 */ #define GPIO_INDR_IDR11 ((uint16_t)0x0800) /* Port input data, bit 11 */ #define GPIO_INDR_IDR12 ((uint16_t)0x1000) /* Port input data, bit 12 */ #define GPIO_INDR_IDR13 ((uint16_t)0x2000) /* Port input data, bit 13 */ #define GPIO_INDR_IDR14 ((uint16_t)0x4000) /* Port input data, bit 14 */ #define GPIO_INDR_IDR15 ((uint16_t)0x8000) /* Port input data, bit 15 */ #define GPIO_INDR_IDR16 ((uint32_t)0x10000) /* Port input data, bit 16 */ #define GPIO_INDR_IDR17 ((uint32_t)0x20000) /* Port input data, bit 17 */ #define GPIO_INDR_IDR18 ((uint32_t)0x40000) /* Port input data, bit 18 */ #define GPIO_INDR_IDR19 ((uint32_t)0x80000) /* Port input data, bit 19 */ #define GPIO_INDR_IDR20 ((uint32_t)0x100000) /* Port input data, bit 20 */ #define GPIO_INDR_IDR21 ((uint32_t)0x200000) /* Port input data, bit 21 */ #define GPIO_INDR_IDR22 ((uint32_t)0x400000) /* Port input data, bit 22 */ #define GPIO_INDR_IDR23 ((uint32_t)0x800000) /* Port input data, bit 23 */ /******************* Bit definition for GPIO_OUTDR register *******************/ #define GPIO_OUTDR_ODR0 ((uint16_t)0x0001) /* Port output data, bit 0 */ #define GPIO_OUTDR_ODR1 ((uint16_t)0x0002) /* Port output data, bit 1 */ #define GPIO_OUTDR_ODR2 ((uint16_t)0x0004) /* Port output data, bit 2 */ #define GPIO_OUTDR_ODR3 ((uint16_t)0x0008) /* Port output data, bit 3 */ #define GPIO_OUTDR_ODR4 ((uint16_t)0x0010) /* Port output data, bit 4 */ #define GPIO_OUTDR_ODR5 ((uint16_t)0x0020) /* Port output data, bit 5 */ #define GPIO_OUTDR_ODR6 ((uint16_t)0x0040) /* Port output data, bit 6 */ #define GPIO_OUTDR_ODR7 ((uint16_t)0x0080) /* Port output data, bit 7 */ #define GPIO_OUTDR_ODR8 ((uint16_t)0x0100) /* Port output data, bit 8 */ #define GPIO_OUTDR_ODR9 ((uint16_t)0x0200) /* Port output data, bit 9 */ #define GPIO_OUTDR_ODR10 ((uint16_t)0x0400) /* Port output data, bit 10 */ #define GPIO_OUTDR_ODR11 ((uint16_t)0x0800) /* Port output data, bit 11 */ #define GPIO_OUTDR_ODR12 ((uint16_t)0x1000) /* Port output data, bit 12 */ #define GPIO_OUTDR_ODR13 ((uint16_t)0x2000) /* Port output data, bit 13 */ #define GPIO_OUTDR_ODR14 ((uint16_t)0x4000) /* Port output data, bit 14 */ #define GPIO_OUTDR_ODR15 ((uint16_t)0x8000) /* Port output data, bit 15 */ #define GPIO_OUTDR_ODR16 ((uint32_t)0x10000) /* Port output data, bit 16 */ #define GPIO_OUTDR_ODR17 ((uint32_t)0x20000) /* Port output data, bit 17 */ #define GPIO_OUTDR_ODR18 ((uint32_t)0x40000) /* Port output data, bit 18 */ #define GPIO_OUTDR_ODR19 ((uint32_t)0x80000) /* Port output data, bit 19 */ #define GPIO_OUTDR_ODR20 ((uint32_t)0x100000) /* Port output data, bit 20 */ #define GPIO_OUTDR_ODR21 ((uint32_t)0x200000) /* Port output data, bit 21 */ #define GPIO_OUTDR_ODR22 ((uint32_t)0x400000) /* Port output data, bit 22 */ #define GPIO_OUTDR_ODR23 ((uint32_t)0x800000) /* Port output data, bit 23 */ /****************** Bit definition for GPIO_BSHR register *******************/ #define GPIO_BSHR_BS0 ((uint32_t)0x00000001) /* Port x Set bit 0 */ #define GPIO_BSHR_BS1 ((uint32_t)0x00000002) /* Port x Set bit 1 */ #define GPIO_BSHR_BS2 ((uint32_t)0x00000004) /* Port x Set bit 2 */ #define GPIO_BSHR_BS3 ((uint32_t)0x00000008) /* Port x Set bit 3 */ #define GPIO_BSHR_BS4 ((uint32_t)0x00000010) /* Port x Set bit 4 */ #define GPIO_BSHR_BS5 ((uint32_t)0x00000020) /* Port x Set bit 5 */ #define GPIO_BSHR_BS6 ((uint32_t)0x00000040) /* Port x Set bit 6 */ #define GPIO_BSHR_BS7 ((uint32_t)0x00000080) /* Port x Set bit 7 */ #define GPIO_BSHR_BS8 ((uint32_t)0x00000100) /* Port x Set bit 8 */ #define GPIO_BSHR_BS9 ((uint32_t)0x00000200) /* Port x Set bit 9 */ #define GPIO_BSHR_BS10 ((uint32_t)0x00000400) /* Port x Set bit 10 */ #define GPIO_BSHR_BS11 ((uint32_t)0x00000800) /* Port x Set bit 11 */ #define GPIO_BSHR_BS12 ((uint32_t)0x00001000) /* Port x Set bit 12 */ #define GPIO_BSHR_BS13 ((uint32_t)0x00002000) /* Port x Set bit 13 */ #define GPIO_BSHR_BS14 ((uint32_t)0x00004000) /* Port x Set bit 14 */ #define GPIO_BSHR_BS15 ((uint32_t)0x00008000) /* Port x Set bit 15 */ #define GPIO_BSHR_BR0 ((uint32_t)0x00010000) /* Port x Reset bit 0 */ #define GPIO_BSHR_BR1 ((uint32_t)0x00020000) /* Port x Reset bit 1 */ #define GPIO_BSHR_BR2 ((uint32_t)0x00040000) /* Port x Reset bit 2 */ #define GPIO_BSHR_BR3 ((uint32_t)0x00080000) /* Port x Reset bit 3 */ #define GPIO_BSHR_BR4 ((uint32_t)0x00100000) /* Port x Reset bit 4 */ #define GPIO_BSHR_BR5 ((uint32_t)0x00200000) /* Port x Reset bit 5 */ #define GPIO_BSHR_BR6 ((uint32_t)0x00400000) /* Port x Reset bit 6 */ #define GPIO_BSHR_BR7 ((uint32_t)0x00800000) /* Port x Reset bit 7 */ #define GPIO_BSHR_BR8 ((uint32_t)0x01000000) /* Port x Reset bit 8 */ #define GPIO_BSHR_BR9 ((uint32_t)0x02000000) /* Port x Reset bit 9 */ #define GPIO_BSHR_BR10 ((uint32_t)0x04000000) /* Port x Reset bit 10 */ #define GPIO_BSHR_BR11 ((uint32_t)0x08000000) /* Port x Reset bit 11 */ #define GPIO_BSHR_BR12 ((uint32_t)0x10000000) /* Port x Reset bit 12 */ #define GPIO_BSHR_BR13 ((uint32_t)0x20000000) /* Port x Reset bit 13 */ #define GPIO_BSHR_BR14 ((uint32_t)0x40000000) /* Port x Reset bit 14 */ #define GPIO_BSHR_BR15 ((uint32_t)0x80000000) /* Port x Reset bit 15 */ /******************* Bit definition for GPIO_BCR register *******************/ #define GPIO_BCR_BR0 ((uint16_t)0x0001) /* Port x Reset bit 0 */ #define GPIO_BCR_BR1 ((uint16_t)0x0002) /* Port x Reset bit 1 */ #define GPIO_BCR_BR2 ((uint16_t)0x0004) /* Port x Reset bit 2 */ #define GPIO_BCR_BR3 ((uint16_t)0x0008) /* Port x Reset bit 3 */ #define GPIO_BCR_BR4 ((uint16_t)0x0010) /* Port x Reset bit 4 */ #define GPIO_BCR_BR5 ((uint16_t)0x0020) /* Port x Reset bit 5 */ #define GPIO_BCR_BR6 ((uint16_t)0x0040) /* Port x Reset bit 6 */ #define GPIO_BCR_BR7 ((uint16_t)0x0080) /* Port x Reset bit 7 */ #define GPIO_BCR_BR8 ((uint16_t)0x0100) /* Port x Reset bit 8 */ #define GPIO_BCR_BR9 ((uint16_t)0x0200) /* Port x Reset bit 9 */ #define GPIO_BCR_BR10 ((uint16_t)0x0400) /* Port x Reset bit 10 */ #define GPIO_BCR_BR11 ((uint16_t)0x0800) /* Port x Reset bit 11 */ #define GPIO_BCR_BR12 ((uint16_t)0x1000) /* Port x Reset bit 12 */ #define GPIO_BCR_BR13 ((uint16_t)0x2000) /* Port x Reset bit 13 */ #define GPIO_BCR_BR14 ((uint16_t)0x4000) /* Port x Reset bit 14 */ #define GPIO_BCR_BR15 ((uint16_t)0x8000) /* Port x Reset bit 15 */ #define GPIO_BCR_BR16 ((uint32_t)0x10000) /* Port x Reset bit 16 */ #define GPIO_BCR_BR17 ((uint32_t)0x20000) /* Port x Reset bit 17 */ #define GPIO_BCR_BR18 ((uint32_t)0x40000) /* Port x Reset bit 18 */ #define GPIO_BCR_BR19 ((uint32_t)0x80000) /* Port x Reset bit 19 */ #define GPIO_BCR_BR20 ((uint32_t)0x100000) /* Port x Reset bit 20 */ #define GPIO_BCR_BR21 ((uint32_t)0x200000) /* Port x Reset bit 21 */ #define GPIO_BCR_BR22 ((uint32_t)0x400000) /* Port x Reset bit 22 */ #define GPIO_BCR_BR23 ((uint32_t)0x800000) /* Port x Reset bit 23 */ /****************** Bit definition for GPIO_LCKR register *******************/ #define GPIO_LCK0 ((uint32_t)0x00000001) /* Port x Lock bit 0 */ #define GPIO_LCK1 ((uint32_t)0x00000002) /* Port x Lock bit 1 */ #define GPIO_LCK2 ((uint32_t)0x00000004) /* Port x Lock bit 2 */ #define GPIO_LCK3 ((uint32_t)0x00000008) /* Port x Lock bit 3 */ #define GPIO_LCK4 ((uint32_t)0x00000010) /* Port x Lock bit 4 */ #define GPIO_LCK5 ((uint32_t)0x00000020) /* Port x Lock bit 5 */ #define GPIO_LCK6 ((uint32_t)0x00000040) /* Port x Lock bit 6 */ #define GPIO_LCK7 ((uint32_t)0x00000080) /* Port x Lock bit 7 */ #define GPIO_LCK8 ((uint32_t)0x00000100) /* Port x Lock bit 8 */ #define GPIO_LCK9 ((uint32_t)0x00000200) /* Port x Lock bit 9 */ #define GPIO_LCK10 ((uint32_t)0x00000400) /* Port x Lock bit 10 */ #define GPIO_LCK11 ((uint32_t)0x00000800) /* Port x Lock bit 11 */ #define GPIO_LCK12 ((uint32_t)0x00001000) /* Port x Lock bit 12 */ #define GPIO_LCK13 ((uint32_t)0x00002000) /* Port x Lock bit 13 */ #define GPIO_LCK14 ((uint32_t)0x00004000) /* Port x Lock bit 14 */ #define GPIO_LCK15 ((uint32_t)0x00008000) /* Port x Lock bit 15 */ #define GPIO_LCK16 ((uint32_t)0x00010000) /* Port x Lock bit 16 */ #define GPIO_LCK17 ((uint32_t)0x00020000) /* Port x Lock bit 17 */ #define GPIO_LCK18 ((uint32_t)0x00040000) /* Port x Lock bit 18 */ #define GPIO_LCK19 ((uint32_t)0x00080000) /* Port x Lock bit 19 */ #define GPIO_LCK20 ((uint32_t)0x00100000) /* Port x Lock bit 20 */ #define GPIO_LCK21 ((uint32_t)0x00200000) /* Port x Lock bit 21 */ #define GPIO_LCK22 ((uint32_t)0x00400000) /* Port x Lock bit 22 */ #define GPIO_LCK23 ((uint32_t)0x00800000) /* Port x Lock bit 23 */ #define GPIO_LCKK ((uint32_t)0x01000000) /* Lock key */ /******************* Bit definition for GPIO_CFGXR register *******************/ #define GPIO_CFGXR_MODE ((uint32_t)0x33333333) /* Port x mode bits */ #define GPIO_CFGXR_MODE16 ((uint32_t)0x00000003) /* MODE16[1:0] bits (Port x mode bits, pin 0) */ #define GPIO_CFGXR_MODE16_0 ((uint32_t)0x00000001) /* Bit 0 */ #define GPIO_CFGXR_MODE16_1 ((uint32_t)0x00000002) /* Bit 1 */ #define GPIO_CFGXR_MODE17 ((uint32_t)0x00000030) /* MODE17[1:0] bits (Port x mode bits, pin 1) */ #define GPIO_CFGXR_MODE17_0 ((uint32_t)0x00000010) /* Bit 0 */ #define GPIO_CFGXR_MODE17_1 ((uint32_t)0x00000020) /* Bit 1 */ #define GPIO_CFGXR_MODE18 ((uint32_t)0x00000300) /* MODE18[1:0] bits (Port x mode bits, pin 2) */ #define GPIO_CFGXR_MODE18_0 ((uint32_t)0x00000100) /* Bit 0 */ #define GPIO_CFGXR_MODE18_1 ((uint32_t)0x00000200) /* Bit 1 */ #define GPIO_CFGXR_MODE19 ((uint32_t)0x00003000) /* MODE19[1:0] bits (Port x mode bits, pin 3) */ #define GPIO_CFGXR_MODE19_0 ((uint32_t)0x00001000) /* Bit 0 */ #define GPIO_CFGXR_MODE19_1 ((uint32_t)0x00002000) /* Bit 1 */ #define GPIO_CFGXR_MODE20 ((uint32_t)0x00030000) /* MODE20[1:0] bits (Port x mode bits, pin 4) */ #define GPIO_CFGXR_MODE20_0 ((uint32_t)0x00010000) /* Bit 0 */ #define GPIO_CFGXR_MODE20_1 ((uint32_t)0x00020000) /* Bit 1 */ #define GPIO_CFGXR_MODE21 ((uint32_t)0x00300000) /* MODE21[1:0] bits (Port x mode bits, pin 5) */ #define GPIO_CFGXR_MODE21_0 ((uint32_t)0x00100000) /* Bit 0 */ #define GPIO_CFGXR_MODE21_1 ((uint32_t)0x00200000) /* Bit 1 */ #define GPIO_CFGXR_MODE22 ((uint32_t)0x03000000) /* MODE22[1:0] bits (Port x mode bits, pin 6) */ #define GPIO_CFGXR_MODE22_0 ((uint32_t)0x01000000) /* Bit 0 */ #define GPIO_CFGXR_MODE22_1 ((uint32_t)0x02000000) /* Bit 1 */ #define GPIO_CFGXR_MODE23 ((uint32_t)0x30000000) /* MODE23[1:0] bits (Port x mode bits, pin 7) */ #define GPIO_CFGXR_MODE23_0 ((uint32_t)0x10000000) /* Bit 0 */ #define GPIO_CFGXR_MODE23_1 ((uint32_t)0x20000000) /* Bit 1 */ #define GPIO_CFGXR_CNF ((uint32_t)0xCCCCCCCC) /* Port x configuration bits */ #define GPIO_CFGXR_CNF16 ((uint32_t)0x0000000C) /* CNF16[1:0] bits (Port x configuration bits, pin 0) */ #define GPIO_CFGXR_CNF16_0 ((uint32_t)0x00000004) /* Bit 0 */ #define GPIO_CFGXR_CNF16_1 ((uint32_t)0x00000008) /* Bit 1 */ #define GPIO_CFGXR_CNF17 ((uint32_t)0x000000C0) /* CNF17[1:0] bits (Port x configuration bits, pin 1) */ #define GPIO_CFGXR_CNF17_0 ((uint32_t)0x00000040) /* Bit 0 */ #define GPIO_CFGXR_CNF17_1 ((uint32_t)0x00000080) /* Bit 1 */ #define GPIO_CFGXR_CNF18 ((uint32_t)0x00000C00) /* CNF18[1:0] bits (Port x configuration bits, pin 2) */ #define GPIO_CFGXR_CNF18_0 ((uint32_t)0x00000400) /* Bit 0 */ #define GPIO_CFGXR_CNF18_1 ((uint32_t)0x00000800) /* Bit 1 */ #define GPIO_CFGXR_CNF19 ((uint32_t)0x0000C000) /* CNF19[1:0] bits (Port x configuration bits, pin 3) */ #define GPIO_CFGXR_CNF19_0 ((uint32_t)0x00004000) /* Bit 0 */ #define GPIO_CFGXR_CNF19_1 ((uint32_t)0x00008000) /* Bit 1 */ #define GPIO_CFGXR_CNF20 ((uint32_t)0x000C0000) /* CNF20[1:0] bits (Port x configuration bits, pin 4) */ #define GPIO_CFGXR_CNF20_0 ((uint32_t)0x00040000) /* Bit 0 */ #define GPIO_CFGXR_CNF20_1 ((uint32_t)0x00080000) /* Bit 1 */ #define GPIO_CFGXR_CNF21 ((uint32_t)0x00C00000) /* CNF21[1:0] bits (Port x configuration bits, pin 5) */ #define GPIO_CFGXR_CNF21_0 ((uint32_t)0x00400000) /* Bit 0 */ #define GPIO_CFGXR_CNF21_1 ((uint32_t)0x00800000) /* Bit 1 */ #define GPIO_CFGXR_CNF22 ((uint32_t)0x0C000000) /* CNF22[1:0] bits (Port x configuration bits, pin 6) */ #define GPIO_CFGXR_CNF22_0 ((uint32_t)0x04000000) /* Bit 0 */ #define GPIO_CFGXR_CNF22_1 ((uint32_t)0x08000000) /* Bit 1 */ #define GPIO_CFGXR_CNF23 ((uint32_t)0xC0000000) /* CNF23[1:0] bits (Port x configuration bits, pin 7) */ #define GPIO_CFGXR_CNF23_0 ((uint32_t)0x40000000) /* Bit 0 */ #define GPIO_CFGXR_CNF23_1 ((uint32_t)0x80000000) /* Bit 1 */ /****************** Bit definition for GPIO_BSXR register *******************/ #define GPIO_BSXR_BS16 ((uint32_t)0x00000001) /* Port x Set bit 0 */ #define GPIO_BSXR_BS17 ((uint32_t)0x00000002) /* Port x Set bit 1 */ #define GPIO_BSXR_BS18 ((uint32_t)0x00000004) /* Port x Set bit 2 */ #define GPIO_BSXR_BS19 ((uint32_t)0x00000008) /* Port x Set bit 3 */ #define GPIO_BSXR_BS20 ((uint32_t)0x00000010) /* Port x Set bit 4 */ #define GPIO_BSXR_BS21 ((uint32_t)0x00000020) /* Port x Set bit 5 */ #define GPIO_BSXR_BS22 ((uint32_t)0x00000040) /* Port x Set bit 6 */ #define GPIO_BSXR_BS23 ((uint32_t)0x00000080) /* Port x Set bit 7 */ #define GPIO_BSXR_BR16 ((uint32_t)0x00010000) /* Port x Reset bit 0 */ #define GPIO_BSXR_BR17 ((uint32_t)0x00020000) /* Port x Reset bit 1 */ #define GPIO_BSXR_BR18 ((uint32_t)0x00040000) /* Port x Reset bit 2 */ #define GPIO_BSXR_BR19 ((uint32_t)0x00080000) /* Port x Reset bit 3 */ #define GPIO_BSXR_BR20 ((uint32_t)0x00100000) /* Port x Reset bit 4 */ #define GPIO_BSXR_BR21 ((uint32_t)0x00200000) /* Port x Reset bit 5 */ #define GPIO_BSXR_BR22 ((uint32_t)0x00400000) /* Port x Reset bit 6 */ #define GPIO_BSXR_BR23 ((uint32_t)0x00800000) /* Port x Reset bit 7 */ /****************** Bit definition for AFIO_PCFR1register *******************/ #define AFIO_PCFR1_SPI1_REMAP ((uint32_t)0x00000003) /* SPI1_REMAP[1:0] bits (SPI1 remapping) */ #define AFIO_PCFR1_SPI1_REMAP_0 ((uint32_t)0x00000001) /* Bit 0 */ #define AFIO_PCFR1_SPI1_REMAP_1 ((uint32_t)0x00000002) /* Bit 1 */ #define AFIO_PCFR1_I2C1_REMAP ((uint32_t)0x0000001C) /* I2C1_REMAP[4:2] bits (I2C1 remapping) */ #define AFIO_PCFR1_I2C1_REMAP_0 ((uint32_t)0x00000004) /* Bit 0 */ #define AFIO_PCFR1_I2C1_REMAP_1 ((uint32_t)0x00000008) /* Bit 1 */ #define AFIO_PCFR1_I2C1_REMAP_2 ((uint32_t)0x00000010) /* Bit 2 */ #define AFIO_PCFR1_USART1_REMAP ((uint32_t)0x00000060) /* USART1_REMAP[6:5] bits (USART1 remapping) */ #define AFIO_PCFR1_USART1_REMAP_0 ((uint32_t)0x00000020) /* Bit 0 */ #define AFIO_PCFR1_USART1_REMAP_1 ((uint32_t)0x00000040) /* Bit 1 */ #define AFIO_PCFR1_USART2_REMAP ((uint32_t)0x00000380) /* USART2_REMAP[9:7] bits (USART2 remapping) */ #define AFIO_PCFR1_USART2_REMAP_0 ((uint32_t)0x00000080) /* Bit 0 */ #define AFIO_PCFR1_USART2_REMAP_1 ((uint32_t)0x00000100) /* Bit 1 */ #define AFIO_PCFR1_USART2_REMAP_2 ((uint32_t)0x00000200) /* Bit 2 */ #define AFIO_PCFR1_USART3_REMAP ((uint32_t)0x00000C00) /* USART3_REMAP[11:10] bits (USART3 remapping) */ #define AFIO_PCFR1_USART3_REMAP_0 ((uint32_t)0x00000400) /* Bit 0 */ #define AFIO_PCFR1_USART3_REMAP_1 ((uint32_t)0x00000800) /* Bit 1 */ #define AFIO_PCFR1_USART4_REMAP ((uint32_t)0x00000700) /* USART4_REMAP[14:12] bits (USART4 remapping) */ #define AFIO_PCFR1_USART4_REMAP_0 ((uint32_t)0x00000100) /* Bit 0 */ #define AFIO_PCFR1_USART4_REMAP_1 ((uint32_t)0x00000200) /* Bit 1 */ #define AFIO_PCFR1_USART4_REMAP_2 ((uint32_t)0x00000400) /* Bit 2 */ #define AFIO_PCFR1_TIM1_REMAP ((uint32_t)0x00003800) /* TIM1_REMAP[17:15] bits (TIM1 remapping) */ #define AFIO_PCFR1_TIM1_REMAP_0 ((uint32_t)0x00000800) /* Bit 0 */ #define AFIO_PCFR1_TIM1_REMAP_1 ((uint32_t)0x00001000) /* Bit 1 */ #define AFIO_PCFR1_TIM1_REMAP_2 ((uint32_t)0x00002000) /* Bit 2 */ #define AFIO_PCFR1_TIM2_REMAP ((uint32_t)0x0001C000) /* TIM2_REMAP[20:18] bits (TIM2 remapping) */ #define AFIO_PCFR1_TIM2_REMAP_0 ((uint32_t)0x00004000) /* Bit 0 */ #define AFIO_PCFR1_TIM2_REMAP_1 ((uint32_t)0x00008000) /* Bit 1 */ #define AFIO_PCFR1_TIM2_REMAP_2 ((uint32_t)0x00010000) /* Bit 2 */ #define AFIO_PCFR1_TIM3_REMAP ((uint32_t)0x00060000) /* TIM3_REMAP[22:21] bits (TIM3 remapping) */ #define AFIO_PCFR1_TIM3_REMAP_0 ((uint32_t)0x00020000) /* Bit 0 */ #define AFIO_PCFR1_TIM3_REMAP_1 ((uint32_t)0x00040000) /* Bit 1 */ #define AFIO_PCFR1_PIOC_REMAP ((uint32_t)0x00080000) /* PIOC[23] bits (PIOC remapping) */ #define AFIO_PCFR1_SWJ_CFG ((uint32_t)0x07000000) /* SWJ_CFG[2:0] bits (Serial Wire JTAG configuration) */ #define AFIO_PCFR1_SWJ_CFG_0 ((uint32_t)0x01000000) /* Bit 0 */ #define AFIO_PCFR1_SWJ_CFG_1 ((uint32_t)0x02000000) /* Bit 1 */ #define AFIO_PCFR1_SWJ_CFG_2 ((uint32_t)0x04000000) /* Bit 2 */ /***************** Bit definition for AFIO_EXTICR1 register *****************/ #define AFIO_EXTICR1_EXTI0 ((uint32_t)0x00000003) /* EXTI 0 configuration */ #define AFIO_EXTICR1_EXTI1 ((uint32_t)0x0000000C) /* EXTI 1 configuration */ #define AFIO_EXTICR1_EXTI2 ((uint32_t)0x00000030) /* EXTI 2 configuration */ #define AFIO_EXTICR1_EXTI3 ((uint32_t)0x000000C0) /* EXTI 3 configuration */ #define AFIO_EXTICR1_EXTI4 ((uint32_t)0x00000300) /* EXTI 4 configuration */ #define AFIO_EXTICR1_EXTI5 ((uint32_t)0x00000C00) /* EXTI 5 configuration */ #define AFIO_EXTICR1_EXTI6 ((uint32_t)0x00003000) /* EXTI 6 configuration */ #define AFIO_EXTICR1_EXTI7 ((uint32_t)0x0000C000) /* EXTI 7 configuration */ #define AFIO_EXTICR1_EXTI8 ((uint32_t)0x00030000) /* EXTI 8 configuration */ #define AFIO_EXTICR1_EXTI9 ((uint32_t)0x000C0000) /* EXTI 9 configuration */ #define AFIO_EXTICR1_EXTI10 ((uint32_t)0x00300000) /* EXTI 10 configuration */ #define AFIO_EXTICR1_EXTI11 ((uint32_t)0x00C00000) /* EXTI 11 configuration */ #define AFIO_EXTICR1_EXTI12 ((uint32_t)0x03000000) /* EXTI 12 configuration */ #define AFIO_EXTICR1_EXTI13 ((uint32_t)0x0C000000) /* EXTI 13 configuration */ #define AFIO_EXTICR1_EXTI14 ((uint32_t)0x30000000) /* EXTI 14 configuration */ #define AFIO_EXTICR1_EXTI15 ((uint32_t)0xC0000000) /* EXTI 15 configuration */ #define AFIO_EXTICR1_EXTI0_PA ((uint32_t)0x00000000) /* PA[0] pin */ #define AFIO_EXTICR1_EXTI0_PB ((uint32_t)0x00000001) /* PB[0] pin */ #define AFIO_EXTICR1_EXTI0_PC ((uint32_t)0x00000002) /* PC[0] pin */ #define AFIO_EXTICR1_EXTI1_PA ((uint32_t)0x00000000) /* PA[1] pin */ #define AFIO_EXTICR1_EXTI1_PB ((uint32_t)0x00000004) /* PB[1] pin */ #define AFIO_EXTICR1_EXTI1_PC ((uint32_t)0x00000008) /* PC[1] pin */ #define AFIO_EXTICR1_EXTI2_PA ((uint32_t)0x00000000) /* PA[2] pin */ #define AFIO_EXTICR1_EXTI2_PB ((uint32_t)0x00000010) /* PB[2] pin */ #define AFIO_EXTICR1_EXTI2_PC ((uint32_t)0x00000020) /* PC[2] pin */ #define AFIO_EXTICR1_EXTI3_PA ((uint32_t)0x00000000) /* PA[3] pin */ #define AFIO_EXTICR1_EXTI3_PB ((uint32_t)0x00000040) /* PB[3] pin */ #define AFIO_EXTICR1_EXTI3_PC ((uint32_t)0x00000080) /* PC[3] pin */ #define AFIO_EXTICR1_EXTI4_PA ((uint32_t)0x00000000) /* PA[4] pin */ #define AFIO_EXTICR1_EXTI4_PB ((uint32_t)0x00000100) /* PB[4] pin */ #define AFIO_EXTICR1_EXTI4_PC ((uint32_t)0x00000200) /* PC[4] pin */ #define AFIO_EXTICR1_EXTI5_PA ((uint32_t)0x00000000) /* PA[5] pin */ #define AFIO_EXTICR1_EXTI5_PB ((uint32_t)0x00000400) /* PB[5] pin */ #define AFIO_EXTICR1_EXTI5_PC ((uint32_t)0x00000800) /* PC[5] pin */ #define AFIO_EXTICR1_EXTI6_PA ((uint32_t)0x00000000) /* PA[6] pin */ #define AFIO_EXTICR1_EXTI6_PB ((uint32_t)0x00001000) /* PB[6] pin */ #define AFIO_EXTICR1_EXTI6_PC ((uint32_t)0x00002000) /* PC[6] pin */ #define AFIO_EXTICR1_EXTI7_PA ((uint32_t)0x00000000) /* PA[7] pin */ #define AFIO_EXTICR1_EXTI7_PB ((uint32_t)0x00004000) /* PB[7] pin */ #define AFIO_EXTICR1_EXTI7_PC ((uint32_t)0x00008000) /* PC[7] pin */ #define AFIO_EXTICR1_EXTI8_PA ((uint32_t)0x00000000) /* PA[8] pin */ #define AFIO_EXTICR1_EXTI8_PB ((uint32_t)0x00010000) /* PB[8] pin */ #define AFIO_EXTICR1_EXTI8_PC ((uint32_t)0x00020000) /* PC[8] pin */ #define AFIO_EXTICR1_EXTI9_PA ((uint32_t)0x00000000) /* PA[9] pin */ #define AFIO_EXTICR1_EXTI9_PB ((uint32_t)0x00040000) /* PB[9] pin */ #define AFIO_EXTICR1_EXTI9_PC ((uint32_t)0x00080000) /* PC[9] pin */ #define AFIO_EXTICR1_EXTI10_PA ((uint32_t)0x00000000) /* PA[10] pin */ #define AFIO_EXTICR1_EXTI10_PB ((uint32_t)0x00100000) /* PB[10] pin */ #define AFIO_EXTICR1_EXTI10_PC ((uint32_t)0x00200000) /* PC[10] pin */ #define AFIO_EXTICR1_EXTI11_PA ((uint32_t)0x00000000) /* PA[11] pin */ #define AFIO_EXTICR1_EXTI11_PB ((uint32_t)0x00400000) /* PB[11] pin */ #define AFIO_EXTICR1_EXTI11_PC ((uint32_t)0x00800000) /* PC[11] pin */ #define AFIO_EXTICR1_EXTI12_PA ((uint32_t)0x00000000) /* PA[12] pin */ #define AFIO_EXTICR1_EXTI12_PB ((uint32_t)0x01000000) /* PB[12] pin */ #define AFIO_EXTICR1_EXTI12_PC ((uint32_t)0x02000000) /* PC[12] pin */ #define AFIO_EXTICR1_EXTI13_PA ((uint32_t)0x00000000) /* PA[13] pin */ #define AFIO_EXTICR1_EXTI13_PB ((uint32_t)0x04000000) /* PB[13] pin */ #define AFIO_EXTICR1_EXTI13_PC ((uint32_t)0x08000000) /* PC[13] pin */ #define AFIO_EXTICR1_EXTI14_PA ((uint32_t)0x00000000) /* PA[14] pin */ #define AFIO_EXTICR1_EXTI14_PB ((uint32_t)0x10000000) /* PB[14] pin */ #define AFIO_EXTICR1_EXTI14_PC ((uint32_t)0x20000000) /* PC[14] pin */ #define AFIO_EXTICR1_EXTI15_PA ((uint32_t)0x00000000) /* PA[15] pin */ #define AFIO_EXTICR1_EXTI15_PB ((uint32_t)0x40000000) /* PB[15] pin */ #define AFIO_EXTICR1_EXTI15_PC ((uint32_t)0x80000000) /* PC[15] pin */ /***************** Bit definition for AFIO_EXTICR2 register *****************/ #define AFIO_EXTICR2_EXTI16 ((uint32_t)0x00000003) /* EXTI 16 configuration */ #define AFIO_EXTICR2_EXTI17 ((uint32_t)0x0000000C) /* EXTI 17 configuration */ #define AFIO_EXTICR2_EXTI18 ((uint32_t)0x00000030) /* EXTI 18 configuration */ #define AFIO_EXTICR2_EXTI19 ((uint32_t)0x000000C0) /* EXTI 19 configuration */ #define AFIO_EXTICR2_EXTI20 ((uint32_t)0x00000300) /* EXTI 20 configuration */ #define AFIO_EXTICR2_EXTI21 ((uint32_t)0x00000C00) /* EXTI 21 configuration */ #define AFIO_EXTICR2_EXTI22 ((uint32_t)0x00003000) /* EXTI 22 configuration */ #define AFIO_EXTICR2_EXTI23 ((uint32_t)0x0000C000) /* EXTI 23 configuration */ #define AFIO_EXTICR2_EXTI16_PA ((uint32_t)0x00000000) /* PA[16] pin */ #define AFIO_EXTICR2_EXTI16_PB ((uint32_t)0x00000001) /* PB[16] pin */ #define AFIO_EXTICR2_EXTI16_PC ((uint32_t)0x00000002) /* PC[16] pin */ #define AFIO_EXTICR2_EXTI17_PA ((uint32_t)0x00000000) /* PA[17] pin */ #define AFIO_EXTICR2_EXTI17_PB ((uint32_t)0x00000004) /* PB[17] pin */ #define AFIO_EXTICR2_EXTI17_PC ((uint32_t)0x00000008) /* PC[17] pin */ #define AFIO_EXTICR2_EXTI18_PA ((uint32_t)0x00000000) /* PA[18] pin */ #define AFIO_EXTICR2_EXTI18_PB ((uint32_t)0x00000010) /* PB[18] pin */ #define AFIO_EXTICR2_EXTI18_PC ((uint32_t)0x00000020) /* PC[18] pin */ #define AFIO_EXTICR2_EXTI19_PA ((uint32_t)0x00000000) /* PA[19] pin */ #define AFIO_EXTICR2_EXTI19_PB ((uint32_t)0x00000040) /* PB[19] pin */ #define AFIO_EXTICR2_EXTI19_PC ((uint32_t)0x00000080) /* PC[19] pin */ #define AFIO_EXTICR2_EXTI20_PA ((uint32_t)0x00000000) /* PA[20] pin */ #define AFIO_EXTICR2_EXTI20_PB ((uint32_t)0x00000100) /* PB[20] pin */ #define AFIO_EXTICR2_EXTI20_PC ((uint32_t)0x00000200) /* PC[20] pin */ #define AFIO_EXTICR2_EXTI21_PA ((uint32_t)0x00000000) /* PA[21] pin */ #define AFIO_EXTICR2_EXTI21_PB ((uint32_t)0x00000400) /* PB[21] pin */ #define AFIO_EXTICR2_EXTI21_PC ((uint32_t)0x00000800) /* PC[21] pin */ #define AFIO_EXTICR2_EXTI22_PA ((uint32_t)0x00000000) /* PA[22] pin */ #define AFIO_EXTICR2_EXTI22_PB ((uint32_t)0x00001000) /* PB[22] pin */ #define AFIO_EXTICR2_EXTI22_PC ((uint32_t)0x00002000) /* PC[22] pin */ #define AFIO_EXTICR2_EXTI23_PA ((uint32_t)0x00000000) /* PA[23] pin */ #define AFIO_EXTICR2_EXTI23_PB ((uint32_t)0x00004000) /* PB[23] pin */ #define AFIO_EXTICR2_EXTI23_PC ((uint32_t)0x00008000) /* PC[23] pin */ /******************* Bit definition for AFIO_CTLR register ********************/ #define AFIO_CTLR_UDM_PUE ((uint32_t)0x00000003) /* PC16/UDM Pin pull-up Mode*/ #define AFIO_CTLR_UDM_PUE_0 ((uint32_t)0x00000001) /* bit[0] */ #define AFIO_CTLR_UDM_PUE_1 ((uint32_t)0x00000002) /* bit[1] */ #define AFIO_CTLR_UDM_PUE_1K5 ((uint32_t)0x00000003) /* pull-up 1.5KΩ */ #define AFIO_CTLR_UDM_PUE_10K ((uint32_t)0x00000002) /* pull-up 10KΩ */ #define AFIO_CTLR_UDP_PUE ((uint32_t)0x0000000c) /* PC17/UDP Pin pull-up Mode*/ #define AFIO_CTLR_UDP_PUE_0 ((uint32_t)0x00000004) /* bit[2] */ #define AFIO_CTLR_UDP_PUE_1 ((uint32_t)0x00000008) /* bit[3] */ #define AFIO_CTLR_UDP_PUE_1K5 ((uint32_t)0x0000000c) /* pull-up 1.5KΩ */ #define AFIO_CTLR_UDP_PUE_10K ((uint32_t)0x00000008) /* pull-up 10KΩ */ #define AFIO_CTLR_USB_PHY_V33 ((uint32_t)0x00000040) /* USB transceiver PHY output and pull-up limiter configuration */ #define AFIO_CTLR_USB_IOEN ((uint32_t)0x00000080) /* USB Remap pin enable */ #define AFIO_CTLR_USBPD_PHY_V33 ((uint32_t)0x00000100) /* USBPD transceiver PHY output and pull-up limiter configuration */ #define AFIO_CTLR_USBPD_IN_HVT ((uint32_t)0x00000200) /* PD pin PC14/PC15 high threshold input mode */ #define AFIO_CTLR_UDP_BC_VSRC ((uint32_t)0x00010000) /* PC17/UDP pin BC protocol source voltage enable */ #define AFIO_CTLR_UDM_BC_VSRC ((uint32_t)0x00020000) /* PC16/UDM pin BC protocol source voltage enable */ #define AFIO_CTLR_UDP_BC_CMPO ((uint32_t)0x00040000) /* PC17/UDP pin BC protocol comparator status */ #define AFIO_CTLR_UDM_BC_CMPO ((uint32_t)0x00080000) /* PC16/UDM pin BC protocol comparator status */ #define AFIO_CTLR_PA3_FILT_EN ((uint32_t)0x01000000) /* Controls the input filtering of the PA3 pin */ #define AFIO_CTLR_PA4_FILT_EN ((uint32_t)0x02000000) /* Controls the input filtering of the PA4 pin */ #define AFIO_CTLR_PB5_FILT_EN ((uint32_t)0x04000000) /* Controls the input filtering of the PB5 pin */ #define AFIO_CTLR_PB6_FILT_EN ((uint32_t)0x08000000) /* Controls the input filtering of the PB6 pin */ /******************************************************************************/ /* Independent WATCHDOG */ /******************************************************************************/ /******************* Bit definition for IWDG_CTLR register ********************/ #define IWDG_KEY ((uint16_t)0xFFFF) /* Key value (write only, read 0000h) */ /******************* Bit definition for IWDG_PSCR register ********************/ #define IWDG_PR ((uint8_t)0x07) /* PR[2:0] (Prescaler divider) */ #define IWDG_PR_0 ((uint8_t)0x01) /* Bit 0 */ #define IWDG_PR_1 ((uint8_t)0x02) /* Bit 1 */ #define IWDG_PR_2 ((uint8_t)0x04) /* Bit 2 */ /******************* Bit definition for IWDG_RLDR register *******************/ #define IWDG_RL ((uint16_t)0x0FFF) /* Watchdog counter reload value */ /******************* Bit definition for IWDG_STATR register ********************/ #define IWDG_PVU ((uint8_t)0x01) /* Watchdog prescaler value update */ #define IWDG_RVU ((uint8_t)0x02) /* Watchdog counter reload value update */ /******************************************************************************/ /* Inter-integrated Circuit Interface */ /******************************************************************************/ /******************* Bit definition for I2C_CTLR1 register ********************/ #define I2C_CTLR1_PE ((uint16_t)0x0001) /* Peripheral Enable */ #define I2C_CTLR1_ENARP ((uint16_t)0x0010) /* ARP Enable */ #define I2C_CTLR1_ENPEC ((uint16_t)0x0020) /* PEC Enable */ #define I2C_CTLR1_ENGC ((uint16_t)0x0040) /* General Call Enable */ #define I2C_CTLR1_NOSTRETCH ((uint16_t)0x0080) /* Clock Stretching Disable (Slave mode) */ #define I2C_CTLR1_START ((uint16_t)0x0100) /* Start Generation */ #define I2C_CTLR1_STOP ((uint16_t)0x0200) /* Stop Generation */ #define I2C_CTLR1_ACK ((uint16_t)0x0400) /* Acknowledge Enable */ #define I2C_CTLR1_POS ((uint16_t)0x0800) /* Acknowledge/PEC Position (for data reception) */ #define I2C_CTLR1_PEC ((uint16_t)0x1000) /* Packet Error Checking */ #define I2C_CTLR1_ALERT ((uint16_t)0x2000) /* SMBus Alert */ #define I2C_CTLR1_SWRST ((uint16_t)0x8000) /* Software Reset */ /******************* Bit definition for I2C_CTLR2 register ********************/ #define I2C_CTLR2_FREQ ((uint16_t)0x003F) /* FREQ[5:0] bits (Peripheral Clock Frequency) */ #define I2C_CTLR2_FREQ_0 ((uint16_t)0x0001) /* Bit 0 */ #define I2C_CTLR2_FREQ_1 ((uint16_t)0x0002) /* Bit 1 */ #define I2C_CTLR2_FREQ_2 ((uint16_t)0x0004) /* Bit 2 */ #define I2C_CTLR2_FREQ_3 ((uint16_t)0x0008) /* Bit 3 */ #define I2C_CTLR2_FREQ_4 ((uint16_t)0x0010) /* Bit 4 */ #define I2C_CTLR2_FREQ_5 ((uint16_t)0x0020) /* Bit 5 */ #define I2C_CTLR2_ITERREN ((uint16_t)0x0100) /* Error Interrupt Enable */ #define I2C_CTLR2_ITEVTEN ((uint16_t)0x0200) /* Event Interrupt Enable */ #define I2C_CTLR2_ITBUFEN ((uint16_t)0x0400) /* Buffer Interrupt Enable */ #define I2C_CTLR2_DMAEN ((uint16_t)0x0800) /* DMA Requests Enable */ #define I2C_CTLR2_LAST ((uint16_t)0x1000) /* DMA Last Transfer */ /******************* Bit definition for I2C_OADDR1 register *******************/ #define I2C_OADDR1_ADD1_7 ((uint16_t)0x00FE) /* Interface Address */ #define I2C_OADDR1_ADD8_9 ((uint16_t)0x0300) /* Interface Address */ #define I2C_OADDR1_ADD0 ((uint16_t)0x0001) /* Bit 0 */ #define I2C_OADDR1_ADD1 ((uint16_t)0x0002) /* Bit 1 */ #define I2C_OADDR1_ADD2 ((uint16_t)0x0004) /* Bit 2 */ #define I2C_OADDR1_ADD3 ((uint16_t)0x0008) /* Bit 3 */ #define I2C_OADDR1_ADD4 ((uint16_t)0x0010) /* Bit 4 */ #define I2C_OADDR1_ADD5 ((uint16_t)0x0020) /* Bit 5 */ #define I2C_OADDR1_ADD6 ((uint16_t)0x0040) /* Bit 6 */ #define I2C_OADDR1_ADD7 ((uint16_t)0x0080) /* Bit 7 */ #define I2C_OADDR1_ADD8 ((uint16_t)0x0100) /* Bit 8 */ #define I2C_OADDR1_ADD9 ((uint16_t)0x0200) /* Bit 9 */ #define I2C_OADDR1_ADDMODE ((uint16_t)0x8000) /* Addressing Mode (Slave mode) */ /******************* Bit definition for I2C_OADDR2 register *******************/ #define I2C_OADDR2_ENDUAL ((uint8_t)0x01) /* Dual addressing mode enable */ #define I2C_OADDR2_ADD2 ((uint8_t)0xFE) /* Interface address */ /******************** Bit definition for I2C_DATAR register ********************/ #define I2C_DR_DATAR ((uint8_t)0xFF) /* 8-bit Data Register */ /******************* Bit definition for I2C_STAR1 register ********************/ #define I2C_STAR1_SB ((uint16_t)0x0001) /* Start Bit (Master mode) */ #define I2C_STAR1_ADDR ((uint16_t)0x0002) /* Address sent (master mode)/matched (slave mode) */ #define I2C_STAR1_BTF ((uint16_t)0x0004) /* Byte Transfer Finished */ #define I2C_STAR1_ADD10 ((uint16_t)0x0008) /* 10-bit header sent (Master mode) */ #define I2C_STAR1_STOPF ((uint16_t)0x0010) /* Stop detection (Slave mode) */ #define I2C_STAR1_RXNE ((uint16_t)0x0040) /* Data Register not Empty (receivers) */ #define I2C_STAR1_TXE ((uint16_t)0x0080) /* Data Register Empty (transmitters) */ #define I2C_STAR1_BERR ((uint16_t)0x0100) /* Bus Error */ #define I2C_STAR1_ARLO ((uint16_t)0x0200) /* Arbitration Lost (master mode) */ #define I2C_STAR1_AF ((uint16_t)0x0400) /* Acknowledge Failure */ #define I2C_STAR1_OVR ((uint16_t)0x0800) /* Overrun/Underrun */ #define I2C_STAR1_PECERR ((uint16_t)0x1000) /* PEC Error in reception */ /******************* Bit definition for I2C_STAR2 register ********************/ #define I2C_STAR2_MSL ((uint16_t)0x0001) /* Master/Slave */ #define I2C_STAR2_BUSY ((uint16_t)0x0002) /* Bus Busy */ #define I2C_STAR2_TRA ((uint16_t)0x0004) /* Transmitter/Receiver */ #define I2C_STAR2_GENCALL ((uint16_t)0x0010) /* General Call Address (Slave mode) */ #define I2C_STAR2_DUALF ((uint16_t)0x0080) /* Dual Flag (Slave mode) */ #define I2C_STAR2_PEC ((uint16_t)0xFF00) /* Packet Error Checking Register */ /******************* Bit definition for I2C_CKCFGR register ********************/ #define I2C_CKCFGR_CCR ((uint16_t)0x0FFF) /* Clock Control Register in Fast/Standard mode (Master mode) */ #define I2C_CKCFGR_DUTY ((uint16_t)0x4000) /* Fast Mode Duty Cycle */ #define I2C_CKCFGR_FS ((uint16_t)0x8000) /* I2C Master Mode Selection */ /******************************************************************************/ /* Operational Amplifiers and Comparators */ /******************************************************************************/ /****************** Bit definition for OPA_CFGR1 register *******************/ #define OPA_CFGR1_POLL_EN1 ((uint16_t)0x0001) /* OPA1 positive polling enable */ #define OPA_CFGR1_POLL_EN2 ((uint16_t)0x0002) /* OPA2 positive polling enable */ #define OPA_CFGR1_BKIN_EN1 ((uint16_t)0x0004) /* Timer's brake input source OPA1 enable */ #define OPA_CFGR1_BKIN_EN2 ((uint16_t)0x0008) /* Timer's brake input source OPA2 enable */ #define OPA_CFGR1_RST_EN1 ((uint16_t)0x0010) /* OPA1 reset system enable */ #define OPA_CFGR1_RST_EN2 ((uint16_t)0x0020) /* OPA2 reset system enable */ #define OPA_CFGR1_BKIN_SEL ((uint16_t)0x0040) /* Timer selection for brake input connection */ #define OPA_CFGR1_POLL_LOCK ((uint16_t)0x0080) /* POLL lock */ #define OPA_CFGR1_IE_OUT1 ((uint16_t)0x0100) /* OPA1 interrupt enable */ #define OPA_CFGR1_IE_OUT2 ((uint16_t)0x0200) /* OPA2 interrupt enable */ #define OPA_CFGR1_IE_CNT ((uint16_t)0x0400) /* OPA end-of-polling-interval interrupt enable */ #define OPA_CFGR1_NMI_EN ((uint16_t)0x0800) /* OPA connection NMI interrupt enable */ #define OPA_CFGR1_IF_OUT1 ((uint16_t)0x1000) /* Interrupt flag for polling to an OPA1 output high */ #define OPA_CFGR1_IF_OUT2 ((uint16_t)0x2000) /* Interrupt flag for polling to an OPA2 output high */ #define OPA_CFGR1_IF_CNT ((uint16_t)0x4000) /* Interrupt flag for the end of the OPA polling interval */ /****************** Bit definition for OPA_CFGR2 register *******************/ #define OPA_CFGR2_POLL_VLU ((uint16_t)0x01ff) /* OPA positive end polling interval */ #define OPA_CFGR2_POLL1_NUM ((uint16_t)0x0600) /* Configure the number of positive ends polled by OPA1 */ #define OPA_CFGR2_POLL1_NUM_1 ((uint16_t)0x0000) /* 1, O1P0 */ #define OPA_CFGR2_POLL1_NUM_2 ((uint16_t)0x0200) /* 2, O1P0 + O1P1 */ #define OPA_CFGR2_POLL1_NUM_3 ((uint16_t)0x0400) /* 3, O1P0 + O1P1 + O1P2*/ #define OPA_CFGR2_POLL2_NUM ((uint16_t)0x1800) /* Configure the number of positive ends polled by OPA2 */ #define OPA_CFGR2_POLL2_NUM_1 ((uint16_t)0x0000) /* 1, O2P0 */ #define OPA_CFGR2_POLL2_NUM_2 ((uint16_t)0x0800) /* 2, O2P0 + O2P1 */ #define OPA_CFGR2_POLL2_NUM_3 ((uint16_t)0x1000) /* 3, O2P0 + O2P1 + O2P2*/ /****************** Bit definition for OPA_CTLR1 register *******************/ #define OPA_CTLR1_EN1 ((uint32_t)0x00000001) /* OPA1 enable */ #define OPA_CTLR1_MODE1 ((uint32_t)0x00000002) /* OPA1 output channel selection */ #define OPA_CTLR1_MODE1_PA3 ((uint32_t)0x00000000) /* OPA1 output channel is PA3 */ #define OPA_CTLR1_MODE1_PB5 ((uint32_t)0x00000002) /* OPA1 output channel is PB5 */ #define OPA_CTLR1_PSEL1 ((uint32_t)0x00000018) /* OPA1 positive input selection */ #define OPA_CTLR1_PSEL1_PB0 ((uint32_t)0x00000000) /* OPA1 positive input is PB0 */ #define OPA_CTLR1_PSEL1_PB8 ((uint32_t)0x00000008) /* OPA1 positive input is PB8 */ #define OPA_CTLR1_PSEL1_PB4 ((uint32_t)0x00000010) /* OPA1 positive input is PB4 */ #define OPA_CTLR1_FB_EN1 ((uint32_t)0x00000020) /* OPA1 internal feedback resistor enable */ #define OPA_CTLR1_NSEL1 ((uint32_t)0x000001c0) /* OPA1 negative input selection and gain selection */ #define OPA_CTLR1_NSEL1_PA6 ((uint32_t)0x00000000) /* OPA1 negative input is PA6 */ #define OPA_CTLR1_NSEL1_PB6 ((uint32_t)0x00000040) /* OPA1 negative input is PB6 */ #define OPA_CTLR1_NSEL1_PA1 ((uint32_t)0x00000080) /* OPA1 negative input is PA1, PGA 16x */ #define OPA_CTLR1_NSEL1_PGA_4X ((uint32_t)0x000000c0) /* OPA1 PGA 4x amplification */ #define OPA_CTLR1_NSEL1_PGA_8X ((uint32_t)0x00000100) /* OPA1 PGA 8x amplification */ #define OPA_CTLR1_NSEL1_PGA_16X ((uint32_t)0x00000140) /* OPA1 PGA 16x amplification */ #define OPA_CTLR1_NSEL1_PGA_32X ((uint32_t)0x00000180) /* OPA1 PGA 32x amplification */ #define OPA_CTLR1_NSEL1_OFF ((uint32_t)0x000001c0) /* OPA1 negative input off */ #define OPA_CTLR1_EN2 ((uint32_t)0x00010000) /* OPA2 enable */ #define OPA_CTLR1_MODE2 ((uint32_t)0x00020000) /* OPA2 output channel selection */ #define OPA_CTLR1_MODE2_PA4 ((uint32_t)0x00000000) /* OPA2 output channel is PA4 */ #define OPA_CTLR1_MODE2_PA2 ((uint32_t)0x00020000) /* OPA2 output channel is PA2 */ #define OPA_CTLR1_PSEL2 ((uint32_t)0x00180000) /* OPA2 positive input selection */ #define OPA_CTLR1_PSEL2_PA7 ((uint32_t)0x00000000) /* OPA2 positive input is PA7 */ #define OPA_CTLR1_PSEL2_PB3 ((uint32_t)0x00080000) /* OPA2 positive input is PB3 */ #define OPA_CTLR1_PSEL2_PB7 ((uint32_t)0x00100000) /* OPA2 positive input is PB7 */ #define OPA_CTLR1_FB_EN2 ((uint32_t)0x00200000) /* OPA2 internal feedback resistor enable */ #define OPA_CTLR1_NSEL2 ((uint32_t)0x01c00000) /* OPA2 negative input selection and gain selection */ #define OPA_CTLR1_NSEL2_PA5 ((uint32_t)0x00000000) /* OPA2 negative input is PA5 */ #define OPA_CTLR1_NSEL2_PB1 ((uint32_t)0x00400000) /* OPA2 negative input is PB1 */ #define OPA_CTLR1_NSEL2_PA1 ((uint32_t)0x00800000) /* OPA2 negative input is PA1, PGA 16x */ #define OPA_CTLR1_NSEL2_PGA_4X ((uint32_t)0x00c00000) /* OPA2 PGA 4x amplification */ #define OPA_CTLR1_NSEL2_PGA_8X ((uint32_t)0x01000000) /* OPA2 PGA 8x amplification */ #define OPA_CTLR1_NSEL2_PGA_16X ((uint32_t)0x01400000) /* OPA2 PGA 16x amplification */ #define OPA_CTLR1_NSEL2_PGA_32X ((uint32_t)0x01800000) /* OPA2 PGA 32x amplification */ #define OPA_CTLR1_NSEL2_OFF ((uint32_t)0x01c00000) /* OPA2 negative input off */ #define OPA_CTLR1_OPA_LOCK ((uint32_t)0x80000000) /* OPA lock */ /****************** Bit definition for OPA_CTLR2 register *******************/ #define OPA_CTLR2_EN1 ((uint32_t)0x00000001) /* CMP1 enable */ #define OPA_CTLR2_MODE1 ((uint32_t)0x00000002) /* CMP1 output channel selection */ #define OPA_CTLR2_MODE1_T2C1 ((uint32_t)0x00000000) /* CMP1 output channel is TIM2_CH1 */ #define OPA_CTLR2_MODE1_PA1 ((uint32_t)0x00000002) /* CMP1 output channel is PA1 */ #define OPA_CTLR2_NSEL1 ((uint32_t)0x00000004) /* CMP1 negative input channel selection */ #define OPA_CTLR2_NSEL1_PC3 ((uint32_t)0x00000000) /* CMP1 negative input is PC3 */ #define OPA_CTLR2_NSEL1_PA23 ((uint32_t)0x00000004) /* CMP1 negative input is PA23 */ #define OPA_CTLR2_PSEL1 ((uint32_t)0x00000008) /* CMP1 negative input channel selection */ #define OPA_CTLR2_PSEL1_PC19 ((uint32_t)0x00000000) /* CMP1 negative input is PC3 */ #define OPA_CTLR2_PSEL1_PA0 ((uint32_t)0x00000008) /* CMP1 negative input is PA23 */ #define OPA_CTLR2_HYEN1 ((uint32_t)0x00000010) /* CMP1 comparator hysteresis enable */ #define OPA_CTLR2_EN2 ((uint32_t)0x00000020) /* CMP2 enable */ #define OPA_CTLR2_MODE2 ((uint32_t)0x00000040) /* CMP2 output channel selection */ #define OPA_CTLR2_MODE2_T2C2 ((uint32_t)0x00000000) /* CMP2 output channel is TIM2_CH2 */ #define OPA_CTLR2_MODE2_PB2 ((uint32_t)0x00000040) /* CMP2 output channel is PB2 */ #define OPA_CTLR2_NSEL2 ((uint32_t)0x00000080) /* CMP2 negative input channel selection */ #define OPA_CTLR2_NSEL2_PA22 ((uint32_t)0x00000000) /* CMP2 negative input is PA22 */ #define OPA_CTLR2_NSEL2_PC3 ((uint32_t)0x00000080) /* CMP2 negative input is PC3 */ #define OPA_CTLR2_PSEL2 ((uint32_t)0x00000100) /* CMP2 negative input channel selection */ #define OPA_CTLR2_PSEL2_PA12 ((uint32_t)0x00000000) /* CMP2 negative input is PA12 */ #define OPA_CTLR2_PSEL2_PA11 ((uint32_t)0x00000100) /* CMP2 negative input is PA11 */ #define OPA_CTLR2_HYEN2 ((uint32_t)0x00000200) /* CMP2 comparator hysteresis enable */ #define OPA_CTLR2_EN3 ((uint32_t)0x00000400) /* CMP3 enable */ #define OPA_CTLR2_MODE3 ((uint32_t)0x00000800) /* CMP3 output channel selection */ #define OPA_CTLR2_MODE3_T2C3 ((uint32_t)0x00000000) /* CMP3 output channel is TIM2_CH3 */ #define OPA_CTLR2_MODE3_PB3 ((uint32_t)0x00000800) /* CMP3 output channel is PB3 */ #define OPA_CTLR2_NSEL3 ((uint32_t)0x00001000) /* CMP3 negative input channel selection */ #define OPA_CTLR2_NSEL3_PA2 ((uint32_t)0x00000000) /* CMP3 negative input is PA2 */ #define OPA_CTLR2_NSEL3_PC3 ((uint32_t)0x00001000) /* CMP3 negative input is PC3 */ #define OPA_CTLR2_PSEL3 ((uint32_t)0x00002000) /* CMP3 negative input channel selection */ #define OPA_CTLR2_PSEL3_PA13 ((uint32_t)0x00000000) /* CMP3 negative input is PA13 */ #define OPA_CTLR2_PSEL3_PA14 ((uint32_t)0x00002000) /* CMP3 negative input is PA14 */ #define OPA_CTLR2_HYEN3 ((uint32_t)0x00004000) /* CMP3 comparator hysteresis enable */ #define OPA_CTLR2_CMP_LOCK ((uint32_t)0x80000000) /* CMP lock */ /******************* Bit definition for OPA_KEY register ********************/ #define OPA_KEY1 ((uint32_t)0x45670123) /* OPA unlock keys */ #define OPA_KEY2 ((uint32_t)0xCDEF89AB) /* OPA unlock keys */ /******************* Bit definition for CMP_KEY register ********************/ #define CMP_KEY1 ((uint32_t)0x45670123) /* CMP unlock keys */ #define CMP_KEY2 ((uint32_t)0xCDEF89AB) /* CMP unlock keys */ /******************* Bit definition for POLL_KEY register *******************/ #define POLL_KEY1 ((uint32_t)0x45670123) /* POLL unlock keys */ #define POLL_KEY2 ((uint32_t)0xCDEF89AB) /* POLL unlock keys */ /******************************************************************************/ /* Power Control */ /******************************************************************************/ /******************** Bit definition for PWR_CTLR register ********************/ #define PWR_CTLR_PDDS ((uint16_t)0x0002) /* Power Down Deepsleep */ #define PWR_CTLR_PLS ((uint16_t)0x0060) /* PLS[2:0] bits (PVD Level Selection) */ #define PWR_CTLR_PLS_0 ((uint16_t)0x0020) /* Bit 0 */ #define PWR_CTLR_PLS_1 ((uint16_t)0x0040) /* Bit 1 */ #define PWR_CTLR_PLS_2V1 ((uint16_t)0x0000) #define PWR_CTLR_PLS_2V3 ((uint16_t)0x0020) #define PWR_CTLR_PLS_3V0 ((uint16_t)0x0040) #define PWR_CTLR_PLS_4V0 ((uint16_t)0x0060) #define PWR_CTLR_LP_REG ((uint16_t)0x0200) /* Software configure flash into lower energy mode */ #define PWR_CTLR_LP ((uint16_t)0x0C00) /* Software configure flash Status */ #define PWR_CTLR_LP_0 ((uint16_t)0x0400) #define PWR_CTLR_LP_1 ((uint16_t)0x0800) /******************* Bit definition for PWR_CSR register ********************/ #define PWR_CSR_PVDO ((uint16_t)0x0004) /* PVD Output */ #define PWR_CSR_Flash_ack ((uint16_t)0x0200) /* Flash Status */ /******************************************************************************/ /* Reset and Clock Control */ /******************************************************************************/ /******************** Bit definition for RCC_CTLR register ********************/ #define RCC_HSION ((uint32_t)0x00000001) /* Internal High Speed clock enable */ #define RCC_HSIRDY ((uint32_t)0x00000002) /* Internal High Speed clock ready flag */ #define RCC_HSITRIM ((uint32_t)0x000000F8) /* Internal High Speed clock trimming */ #define RCC_HSICAL ((uint32_t)0x0000FF00) /* Internal High Speed clock Calibration */ /******************* Bit definition for RCC_CFGR0 register *******************/ #define RCC_HPRE ((uint32_t)0x000000F0) /* HPRE[3:0] bits (AHB prescaler) */ #define RCC_HPRE_0 ((uint32_t)0x00000010) /* Bit 0 */ #define RCC_HPRE_1 ((uint32_t)0x00000020) /* Bit 1 */ #define RCC_HPRE_2 ((uint32_t)0x00000040) /* Bit 2 */ #define RCC_HPRE_3 ((uint32_t)0x00000080) /* Bit 3 */ #define RCC_HPRE_DIV1 ((uint32_t)0x00000000) /* SYSCLK not divided */ #define RCC_HPRE_DIV2 ((uint32_t)0x00000010) /* SYSCLK divided by 2 */ #define RCC_HPRE_DIV3 ((uint32_t)0x00000020) /* SYSCLK divided by 3 */ #define RCC_HPRE_DIV4 ((uint32_t)0x00000030) /* SYSCLK divided by 4 */ #define RCC_HPRE_DIV5 ((uint32_t)0x00000040) /* SYSCLK divided by 5 */ #define RCC_HPRE_DIV6 ((uint32_t)0x00000050) /* SYSCLK divided by 6 */ #define RCC_HPRE_DIV7 ((uint32_t)0x00000060) /* SYSCLK divided by 7 */ #define RCC_HPRE_DIV8 ((uint32_t)0x00000070) /* SYSCLK divided by 8 */ #define RCC_HPRE_DIV16 ((uint32_t)0x000000B0) /* SYSCLK divided by 16 */ #define RCC_HPRE_DIV32 ((uint32_t)0x000000C0) /* SYSCLK divided by 32 */ #define RCC_HPRE_DIV64 ((uint32_t)0x000000D0) /* SYSCLK divided by 64 */ #define RCC_HPRE_DIV128 ((uint32_t)0x000000E0) /* SYSCLK divided by 128 */ #define RCC_HPRE_DIV256 ((uint32_t)0x000000F0) /* SYSCLK divided by 256 */ #define RCC_CFGR0_MCO ((uint32_t)0x07000000) /* MCO[2:0] bits (Microcontroller Clock Output) */ #define RCC_MCO_0 ((uint32_t)0x01000000) /* Bit 0 */ #define RCC_MCO_1 ((uint32_t)0x02000000) /* Bit 1 */ #define RCC_MCO_2 ((uint32_t)0x04000000) /* Bit 2 */ #define RCC_MCO_NOCLOCK ((uint32_t)0x00000000) /* No clock */ #define RCC_CFGR0_MCO_SYSCLK ((uint32_t)0x04000000) /* System clock selected as MCO source */ #define RCC_CFGR0_MCO_HSI ((uint32_t)0x05000000) /* HSI clock selected as MCO source */ /***************** Bit definition for RCC_APB2PRSTR register *****************/ #define RCC_AFIORST ((uint32_t)0x00000001) /* Alternate Function I/O reset */ #define RCC_IOPARST ((uint32_t)0x00000004) /* I/O port A reset */ #define RCC_IOPBRST ((uint32_t)0x00000008) /* I/O port B reset */ #define RCC_IOPCRST ((uint32_t)0x00000010) /* I/O port C reset */ #define RCC_ADC1RST ((uint32_t)0x00000200) /* ADC 1 interface reset */ #define RCC_TIM1RST ((uint32_t)0x00000800) /* TIM1 Timer reset */ #define RCC_SPI1RST ((uint32_t)0x00001000) /* SPI 1 reset */ #define RCC_USART1RST ((uint32_t)0x00004000) /* USART1 reset */ /***************** Bit definition for RCC_APB1PRSTR register *****************/ #define RCC_TIM2RST ((uint32_t)0x00000001) /* Timer 2 reset */ #define RCC_TIM3RST ((uint32_t)0x00000002) /* Timer 3 reset */ #define RCC_WWDGRST ((uint32_t)0x00000800) /* Window Watchdog reset */ #define RCC_USART2RST ((uint32_t)0x00020000) /* USART 2 reset */ #define RCC_USART3RST ((uint32_t)0x00040000) /* USART 3 reset */ #define RCC_USART4RST ((uint32_t)0x00080000) /* USART 4 reset */ #define RCC_I2C1RST ((uint32_t)0x00200000) /* I2C 1 reset */ #define RCC_PWRRST ((uint32_t)0x10000000) /* Power interface reset */ /****************** Bit definition for RCC_AHBPCENR register ******************/ #define RCC_DMA1EN ((uint32_t)0x00000001) /* DMA1 clock enable */ #define RCC_SRAMEN ((uint32_t)0x00000004) /* SRAM interface clock enable */ #define RCC_USBFS ((uint32_t)0x00001000) /* USBFS clock enable */ #define RCC_USBPD ((uint32_t)0x00020000) /* USBPD clock enable */ /****************** Bit definition for RCC_APB2PCENR register *****************/ #define RCC_AFIOEN ((uint32_t)0x00000001) /* Alternate Function I/O clock enable */ #define RCC_IOPAEN ((uint32_t)0x00000004) /* I/O port A clock enable */ #define RCC_IOPBEN ((uint32_t)0x00000008) /* I/O port B clock enable */ #define RCC_IOPCEN ((uint32_t)0x00000010) /* I/O port C clock enable */ #define RCC_ADC1EN ((uint32_t)0x00000200) /* ADC 1 interface clock enable */ #define RCC_TIM1EN ((uint32_t)0x00000800) /* TIM1 Timer clock enable */ #define RCC_SPI1EN ((uint32_t)0x00001000) /* SPI 1 clock enable */ #define RCC_USART1EN ((uint32_t)0x00004000) /* USART1 clock enable */ /***************** Bit definition for RCC_APB1PCENR register ******************/ #define RCC_TIM2EN ((uint32_t)0x00000001) /* Timer 2 clock enabled*/ #define RCC_TIM3EN ((uint32_t)0x00000002) /* Timer 3 clock enable */ #define RCC_WWDGEN ((uint32_t)0x00000800) /* Window Watchdog clock enable */ #define RCC_USART2EN ((uint32_t)0x00020000) /* USART 2 clock enable */ #define RCC_USART3EN ((uint32_t)0x00040000) /* USART 3 clock enable */ #define RCC_USART4EN ((uint32_t)0x00080000) /* USART 4 clock enable */ #define RCC_I2C1EN ((uint32_t)0x00200000) /* I2C 1 clock enable */ #define RCC_PWREN ((uint32_t)0x10000000) /* Power interface clock enable */ /******************* Bit definition for RCC_RSTSCKR register ********************/ #define RCC_RMVF ((uint32_t)0x01000000) /* Remove reset flag */ #define RCC_OPARSTF ((uint32_t)0x02000000) /* OPA reset flag */ #define RCC_PINRSTF ((uint32_t)0x04000000) /* PIN reset flag */ #define RCC_PORRSTF ((uint32_t)0x08000000) /* POR/PDR reset flag */ #define RCC_SFTRSTF ((uint32_t)0x10000000) /* Software Reset flag */ #define RCC_IWDGRSTF ((uint32_t)0x20000000) /* Independent Watchdog reset flag */ #define RCC_WWDGRSTF ((uint32_t)0x40000000) /* Window watchdog reset flag */ #define RCC_LPWRRSTF ((uint32_t)0x80000000) /* Low-Power reset flag */ /******************* Bit definition for RCC_AHBRSTR register ********************/ #define RCC_USBFSRST ((uint32_t)0x00001000) /* USBFS reset */ #define RCC_PIOCRST ((uint32_t)0x00002000) /* PIOC RST */ #define RCC_USBPDRST ((uint32_t)0x00020000) /* USBPD reset */ /******************************************************************************/ /* Serial Peripheral Interface */ /******************************************************************************/ /******************* Bit definition for SPI_CTLR1 register ********************/ #define SPI_CTLR1_CPHA ((uint16_t)0x0001) /* Clock Phase */ #define SPI_CTLR1_CPOL ((uint16_t)0x0002) /* Clock Polarity */ #define SPI_CTLR1_MSTR ((uint16_t)0x0004) /* Master Selection */ #define SPI_CTLR1_BR ((uint16_t)0x0038) /* BR[2:0] bits (Baud Rate Control) */ #define SPI_CTLR1_BR_0 ((uint16_t)0x0008) /* Bit 0 */ #define SPI_CTLR1_BR_1 ((uint16_t)0x0010) /* Bit 1 */ #define SPI_CTLR1_BR_2 ((uint16_t)0x0020) /* Bit 2 */ #define SPI_CTLR1_SPE ((uint16_t)0x0040) /* SPI Enable */ #define SPI_CTLR1_LSBFIRST ((uint16_t)0x0080) /* Frame Format */ #define SPI_CTLR1_SSI ((uint16_t)0x0100) /* Internal slave select */ #define SPI_CTLR1_SSM ((uint16_t)0x0200) /* Software slave management */ #define SPI_CTLR1_RXONLY ((uint16_t)0x0400) /* Receive only */ #define SPI_CTLR1_DFF ((uint16_t)0x0800) /* Data Frame Format */ #define SPI_CTLR1_CRCNEXT ((uint16_t)0x1000) /* Transmit CRC next */ #define SPI_CTLR1_CRCEN ((uint16_t)0x2000) /* Hardware CRC calculation enable */ #define SPI_CTLR1_BIDIOE ((uint16_t)0x4000) /* Output enable in bidirectional mode */ #define SPI_CTLR1_BIDIMODE ((uint16_t)0x8000) /* Bidirectional data mode enable */ /******************* Bit definition for SPI_CTLR2 register ********************/ #define SPI_CTLR2_RXDMAEN ((uint8_t)0x01) /* Rx Buffer DMA Enable */ #define SPI_CTLR2_TXDMAEN ((uint8_t)0x02) /* Tx Buffer DMA Enable */ #define SPI_CTLR2_SSOE ((uint8_t)0x04) /* SS Output Enable */ #define SPI_CTLR2_ERRIE ((uint8_t)0x20) /* Error Interrupt Enable */ #define SPI_CTLR2_RXNEIE ((uint8_t)0x40) /* RX buffer Not Empty Interrupt Enable */ #define SPI_CTLR2_TXEIE ((uint8_t)0x80) /* Tx buffer Empty Interrupt Enable */ #define SPI_CTLR2_ODEN ((uint16_t)0x8000) /* SPI OD output Enable */ /******************** Bit definition for SPI_STATR register ********************/ #define SPI_STATR_RXNE ((uint8_t)0x01) /* Receive buffer Not Empty */ #define SPI_STATR_TXE ((uint8_t)0x02) /* Transmit buffer Empty */ #define SPI_STATR_CHSIDE ((uint8_t)0x04) /* Channel side */ #define SPI_STATR_UDR ((uint8_t)0x08) /* Underrun flag */ #define SPI_STATR_CRCERR ((uint8_t)0x10) /* CRC Error flag */ #define SPI_STATR_MODF ((uint8_t)0x20) /* Mode fault */ #define SPI_STATR_OVR ((uint8_t)0x40) /* Overrun flag */ #define SPI_STATR_BSY ((uint8_t)0x80) /* Busy flag */ /******************** Bit definition for SPI_DATAR register ********************/ #define SPI_DATAR_DR ((uint16_t)0xFFFF) /* Data Register */ /******************* Bit definition for SPI_CRCR register ******************/ #define SPI_CRCR_CRCPOLY ((uint16_t)0xFFFF) /* CRC polynomial register */ /****************** Bit definition for SPI_RCRCR register ******************/ #define SPI_RCRCR_RXCRC ((uint16_t)0xFFFF) /* Rx CRC Register */ /****************** Bit definition for SPI_TCRCR register ******************/ #define SPI_TCRCR_TXCRC ((uint16_t)0xFFFF) /* Tx CRC Register */ /****************** Bit definition for SPI_HSCR register *****************/ #define SPI_HSCR_HSRXEN ((uint16_t)0x0001) /* Read Enable under SPI High speed mode */ /******************************************************************************/ /* TIM */ /******************************************************************************/ /******************* Bit definition for TIM_CTLR1 register ********************/ #define TIM_CEN ((uint16_t)0x0001) /* Counter enable */ #define TIM_UDIS ((uint16_t)0x0002) /* Update disable */ #define TIM_URS ((uint16_t)0x0004) /* Update request source */ #define TIM_OPM ((uint16_t)0x0008) /* One pulse mode */ #define TIM_DIR ((uint16_t)0x0010) /* Direction */ #define TIM_CMS ((uint16_t)0x0060) /* CMS[1:0] bits (Center-aligned mode selection) */ #define TIM_CMS_0 ((uint16_t)0x0020) /* Bit 0 */ #define TIM_CMS_1 ((uint16_t)0x0040) /* Bit 1 */ #define TIM_ARPE ((uint16_t)0x0080) /* Auto-reload preload enable */ #define TIM_CTLR1_CKD ((uint16_t)0x0300) /* CKD[1:0] bits (clock division) */ #define TIM_CKD_0 ((uint16_t)0x0100) /* Bit 0 */ #define TIM_CKD_1 ((uint16_t)0x0200) /* Bit 1 */ #define TIM_CMP_BK ((uint16_t)0x1000) /* voltage comparator break enable, TIM1 only */ #define TIM_CAPOV ((uint16_t)0x4000) /* Cfg mode of capture value */ #define TIM_CAPLVL ((uint16_t)0x8000) /******************* Bit definition for TIM_CTLR2 register ********************/ #define TIM_CCPC ((uint16_t)0x0001) /* Capture/Compare Preloaded Control */ #define TIM_CCUS ((uint16_t)0x0004) /* Capture/Compare Control Update Selection */ #define TIM_CCDS ((uint16_t)0x0008) /* Capture/Compare DMA Selection */ #define TIM_MMS ((uint16_t)0x0070) /* MMS[2:0] bits (Master Mode Selection) */ #define TIM_MMS_0 ((uint16_t)0x0010) /* Bit 0 */ #define TIM_MMS_1 ((uint16_t)0x0020) /* Bit 1 */ #define TIM_MMS_2 ((uint16_t)0x0040) /* Bit 2 */ #define TIM_TI1S ((uint16_t)0x0080) /* TI1 Selection */ #define TIM_OIS1 ((uint16_t)0x0100) /* Output Idle state 1 (OC1 output) */ #define TIM_OIS1N ((uint16_t)0x0200) /* Output Idle state 1 (OC1N output) */ #define TIM_OIS2 ((uint16_t)0x0400) /* Output Idle state 2 (OC2 output) */ #define TIM_OIS2N ((uint16_t)0x0800) /* Output Idle state 2 (OC2N output) */ #define TIM_OIS3 ((uint16_t)0x1000) /* Output Idle state 3 (OC3 output) */ #define TIM_OIS3N ((uint16_t)0x2000) /* Output Idle state 3 (OC3N output) */ #define TIM_OIS4 ((uint16_t)0x4000) /* Output Idle state 4 (OC4 output) */ /******************* Bit definition for TIM_SMCFGR register *******************/ #define TIM_SMS ((uint16_t)0x0007) /* SMS[2:0] bits (Slave mode selection) */ #define TIM_SMS_0 ((uint16_t)0x0001) /* Bit 0 */ #define TIM_SMS_1 ((uint16_t)0x0002) /* Bit 1 */ #define TIM_SMS_2 ((uint16_t)0x0004) /* Bit 2 */ #define TIM_TS ((uint16_t)0x0070) /* TS[2:0] bits (Trigger selection) */ #define TIM_TS_0 ((uint16_t)0x0010) /* Bit 0 */ #define TIM_TS_1 ((uint16_t)0x0020) /* Bit 1 */ #define TIM_TS_2 ((uint16_t)0x0040) /* Bit 2 */ #define TIM_MSM ((uint16_t)0x0080) /* Master/slave mode */ #define TIM_ETF ((uint16_t)0x0F00) /* ETF[3:0] bits (External trigger filter) */ #define TIM_ETF_0 ((uint16_t)0x0100) /* Bit 0 */ #define TIM_ETF_1 ((uint16_t)0x0200) /* Bit 1 */ #define TIM_ETF_2 ((uint16_t)0x0400) /* Bit 2 */ #define TIM_ETF_3 ((uint16_t)0x0800) /* Bit 3 */ #define TIM_ETPS ((uint16_t)0x3000) /* ETPS[1:0] bits (External trigger prescaler) */ #define TIM_ETPS_0 ((uint16_t)0x1000) /* Bit 0 */ #define TIM_ETPS_1 ((uint16_t)0x2000) /* Bit 1 */ #define TIM_ECE ((uint16_t)0x4000) /* External clock enable */ #define TIM_ETP ((uint16_t)0x8000) /* External trigger polarity */ /******************* Bit definition for TIM_DMAINTENR register *******************/ #define TIM_UIE ((uint16_t)0x0001) /* Update interrupt enable */ #define TIM_CC1IE ((uint16_t)0x0002) /* Capture/Compare 1 interrupt enable */ #define TIM_CC2IE ((uint16_t)0x0004) /* Capture/Compare 2 interrupt enable */ #define TIM_CC3IE ((uint16_t)0x0008) /* Capture/Compare 3 interrupt enable */ #define TIM_CC4IE ((uint16_t)0x0010) /* Capture/Compare 4 interrupt enable */ #define TIM_COMIE ((uint16_t)0x0020) /* COM interrupt enable */ #define TIM_TIE ((uint16_t)0x0040) /* Trigger interrupt enable */ #define TIM_BIE ((uint16_t)0x0080) /* Break interrupt enable */ #define TIM_UDE ((uint16_t)0x0100) /* Update DMA request enable */ #define TIM_CC1DE ((uint16_t)0x0200) /* Capture/Compare 1 DMA request enable */ #define TIM_CC2DE ((uint16_t)0x0400) /* Capture/Compare 2 DMA request enable */ #define TIM_CC3DE ((uint16_t)0x0800) /* Capture/Compare 3 DMA request enable */ #define TIM_CC4DE ((uint16_t)0x1000) /* Capture/Compare 4 DMA request enable */ #define TIM_COMDE ((uint16_t)0x2000) /* COM DMA request enable */ #define TIM_TDE ((uint16_t)0x4000) /* Trigger DMA request enable */ /******************** Bit definition for TIM_INTFR register ********************/ #define TIM_UIF ((uint16_t)0x0001) /* Update interrupt Flag */ #define TIM_CC1IF ((uint16_t)0x0002) /* Capture/Compare 1 interrupt Flag */ #define TIM_CC2IF ((uint16_t)0x0004) /* Capture/Compare 2 interrupt Flag */ #define TIM_CC3IF ((uint16_t)0x0008) /* Capture/Compare 3 interrupt Flag */ #define TIM_CC4IF ((uint16_t)0x0010) /* Capture/Compare 4 interrupt Flag */ #define TIM_COMIF ((uint16_t)0x0020) /* COM interrupt Flag */ #define TIM_TIF ((uint16_t)0x0040) /* Trigger interrupt Flag */ #define TIM_BIF ((uint16_t)0x0080) /* Break interrupt Flag */ #define TIM_CC1OF ((uint16_t)0x0200) /* Capture/Compare 1 Overcapture Flag */ #define TIM_CC2OF ((uint16_t)0x0400) /* Capture/Compare 2 Overcapture Flag */ #define TIM_CC3OF ((uint16_t)0x0800) /* Capture/Compare 3 Overcapture Flag */ #define TIM_CC4OF ((uint16_t)0x1000) /* Capture/Compare 4 Overcapture Flag */ /******************* Bit definition for TIM_SWEVGR register ********************/ #define TIM_UG ((uint8_t)0x01) /* Update Generation */ #define TIM_CC1G ((uint8_t)0x02) /* Capture/Compare 1 Generation */ #define TIM_CC2G ((uint8_t)0x04) /* Capture/Compare 2 Generation */ #define TIM_CC3G ((uint8_t)0x08) /* Capture/Compare 3 Generation */ #define TIM_CC4G ((uint8_t)0x10) /* Capture/Compare 4 Generation */ #define TIM_COMG ((uint8_t)0x20) /* Capture/Compare Control Update Generation */ #define TIM_TG ((uint8_t)0x40) /* Trigger Generation */ #define TIM_BG ((uint8_t)0x80) /* Break Generation */ /****************** Bit definition for TIM_CHCTLR1 register *******************/ #define TIM_CC1S ((uint16_t)0x0003) /* CC1S[1:0] bits (Capture/Compare 1 Selection) */ #define TIM_CC1S_0 ((uint16_t)0x0001) /* Bit 0 */ #define TIM_CC1S_1 ((uint16_t)0x0002) /* Bit 1 */ #define TIM_OC1FE ((uint16_t)0x0004) /* Output Compare 1 Fast enable */ #define TIM_OC1PE ((uint16_t)0x0008) /* Output Compare 1 Preload enable */ #define TIM_OC1M ((uint16_t)0x0070) /* OC1M[2:0] bits (Output Compare 1 Mode) */ #define TIM_OC1M_0 ((uint16_t)0x0010) /* Bit 0 */ #define TIM_OC1M_1 ((uint16_t)0x0020) /* Bit 1 */ #define TIM_OC1M_2 ((uint16_t)0x0040) /* Bit 2 */ #define TIM_OC1CE ((uint16_t)0x0080) /* Output Compare 1Clear Enable */ #define TIM_CC2S ((uint16_t)0x0300) /* CC2S[1:0] bits (Capture/Compare 2 Selection) */ #define TIM_CC2S_0 ((uint16_t)0x0100) /* Bit 0 */ #define TIM_CC2S_1 ((uint16_t)0x0200) /* Bit 1 */ #define TIM_OC2FE ((uint16_t)0x0400) /* Output Compare 2 Fast enable */ #define TIM_OC2PE ((uint16_t)0x0800) /* Output Compare 2 Preload enable */ #define TIM_OC2M ((uint16_t)0x7000) /* OC2M[2:0] bits (Output Compare 2 Mode) */ #define TIM_OC2M_0 ((uint16_t)0x1000) /* Bit 0 */ #define TIM_OC2M_1 ((uint16_t)0x2000) /* Bit 1 */ #define TIM_OC2M_2 ((uint16_t)0x4000) /* Bit 2 */ #define TIM_OC2CE ((uint16_t)0x8000) /* Output Compare 2 Clear Enable */ #define TIM_IC1PSC ((uint16_t)0x000C) /* IC1PSC[1:0] bits (Input Capture 1 Prescaler) */ #define TIM_IC1PSC_0 ((uint16_t)0x0004) /* Bit 0 */ #define TIM_IC1PSC_1 ((uint16_t)0x0008) /* Bit 1 */ #define TIM_IC1F ((uint16_t)0x00F0) /* IC1F[3:0] bits (Input Capture 1 Filter) */ #define TIM_IC1F_0 ((uint16_t)0x0010) /* Bit 0 */ #define TIM_IC1F_1 ((uint16_t)0x0020) /* Bit 1 */ #define TIM_IC1F_2 ((uint16_t)0x0040) /* Bit 2 */ #define TIM_IC1F_3 ((uint16_t)0x0080) /* Bit 3 */ #define TIM_IC2PSC ((uint16_t)0x0C00) /* IC2PSC[1:0] bits (Input Capture 2 Prescaler) */ #define TIM_IC2PSC_0 ((uint16_t)0x0400) /* Bit 0 */ #define TIM_IC2PSC_1 ((uint16_t)0x0800) /* Bit 1 */ #define TIM_IC2F ((uint16_t)0xF000) /* IC2F[3:0] bits (Input Capture 2 Filter) */ #define TIM_IC2F_0 ((uint16_t)0x1000) /* Bit 0 */ #define TIM_IC2F_1 ((uint16_t)0x2000) /* Bit 1 */ #define TIM_IC2F_2 ((uint16_t)0x4000) /* Bit 2 */ #define TIM_IC2F_3 ((uint16_t)0x8000) /* Bit 3 */ /****************** Bit definition for TIM_CHCTLR2 register *******************/ #define TIM_CC3S ((uint16_t)0x0003) /* CC3S[1:0] bits (Capture/Compare 3 Selection) */ #define TIM_CC3S_0 ((uint16_t)0x0001) /* Bit 0 */ #define TIM_CC3S_1 ((uint16_t)0x0002) /* Bit 1 */ #define TIM_OC3FE ((uint16_t)0x0004) /* Output Compare 3 Fast enable */ #define TIM_OC3PE ((uint16_t)0x0008) /* Output Compare 3 Preload enable */ #define TIM_OC3M ((uint16_t)0x0070) /* OC3M[2:0] bits (Output Compare 3 Mode) */ #define TIM_OC3M_0 ((uint16_t)0x0010) /* Bit 0 */ #define TIM_OC3M_1 ((uint16_t)0x0020) /* Bit 1 */ #define TIM_OC3M_2 ((uint16_t)0x0040) /* Bit 2 */ #define TIM_OC3CE ((uint16_t)0x0080) /* Output Compare 3 Clear Enable */ #define TIM_CC4S ((uint16_t)0x0300) /* CC4S[1:0] bits (Capture/Compare 4 Selection) */ #define TIM_CC4S_0 ((uint16_t)0x0100) /* Bit 0 */ #define TIM_CC4S_1 ((uint16_t)0x0200) /* Bit 1 */ #define TIM_OC4FE ((uint16_t)0x0400) /* Output Compare 4 Fast enable */ #define TIM_OC4PE ((uint16_t)0x0800) /* Output Compare 4 Preload enable */ #define TIM_OC4M ((uint16_t)0x7000) /* OC4M[2:0] bits (Output Compare 4 Mode) */ #define TIM_OC4M_0 ((uint16_t)0x1000) /* Bit 0 */ #define TIM_OC4M_1 ((uint16_t)0x2000) /* Bit 1 */ #define TIM_OC4M_2 ((uint16_t)0x4000) /* Bit 2 */ #define TIM_OC4CE ((uint16_t)0x8000) /* Output Compare 4 Clear Enable */ #define TIM_IC3PSC ((uint16_t)0x000C) /* IC3PSC[1:0] bits (Input Capture 3 Prescaler) */ #define TIM_IC3PSC_0 ((uint16_t)0x0004) /* Bit 0 */ #define TIM_IC3PSC_1 ((uint16_t)0x0008) /* Bit 1 */ #define TIM_IC3F ((uint16_t)0x00F0) /* IC3F[3:0] bits (Input Capture 3 Filter) */ #define TIM_IC3F_0 ((uint16_t)0x0010) /* Bit 0 */ #define TIM_IC3F_1 ((uint16_t)0x0020) /* Bit 1 */ #define TIM_IC3F_2 ((uint16_t)0x0040) /* Bit 2 */ #define TIM_IC3F_3 ((uint16_t)0x0080) /* Bit 3 */ #define TIM_IC4PSC ((uint16_t)0x0C00) /* IC4PSC[1:0] bits (Input Capture 4 Prescaler) */ #define TIM_IC4PSC_0 ((uint16_t)0x0400) /* Bit 0 */ #define TIM_IC4PSC_1 ((uint16_t)0x0800) /* Bit 1 */ #define TIM_IC4F ((uint16_t)0xF000) /* IC4F[3:0] bits (Input Capture 4 Filter) */ #define TIM_IC4F_0 ((uint16_t)0x1000) /* Bit 0 */ #define TIM_IC4F_1 ((uint16_t)0x2000) /* Bit 1 */ #define TIM_IC4F_2 ((uint16_t)0x4000) /* Bit 2 */ #define TIM_IC4F_3 ((uint16_t)0x8000) /* Bit 3 */ /******************* Bit definition for TIM_CCER register *******************/ #define TIM_CC1E ((uint16_t)0x0001) /* Capture/Compare 1 output enable */ #define TIM_CC1P ((uint16_t)0x0002) /* Capture/Compare 1 output Polarity */ #define TIM_CC1NE ((uint16_t)0x0004) /* Capture/Compare 1 Complementary output enable */ #define TIM_CC1NP ((uint16_t)0x0008) /* Capture/Compare 1 Complementary output Polarity */ #define TIM_CC2E ((uint16_t)0x0010) /* Capture/Compare 2 output enable */ #define TIM_CC2P ((uint16_t)0x0020) /* Capture/Compare 2 output Polarity */ #define TIM_CC2NE ((uint16_t)0x0040) /* Capture/Compare 2 Complementary output enable */ #define TIM_CC2NP ((uint16_t)0x0080) /* Capture/Compare 2 Complementary output Polarity */ #define TIM_CC3E ((uint16_t)0x0100) /* Capture/Compare 3 output enable */ #define TIM_CC3P ((uint16_t)0x0200) /* Capture/Compare 3 output Polarity */ #define TIM_CC3NE ((uint16_t)0x0400) /* Capture/Compare 3 Complementary output enable */ #define TIM_CC3NP ((uint16_t)0x0800) /* Capture/Compare 3 Complementary output Polarity */ #define TIM_CC4E ((uint16_t)0x1000) /* Capture/Compare 4 output enable */ #define TIM_CC4P ((uint16_t)0x2000) /* Capture/Compare 4 output Polarity */ /******************* Bit definition for TIM_CNT register ********************/ #define TIM_CNT ((uint16_t)0xFFFF) /* Counter Value */ /******************* Bit definition for TIM_PSC register ********************/ #define TIM_PSC ((uint16_t)0xFFFF) /* Prescaler Value */ /******************* Bit definition for TIM_ATRLR register ********************/ #define TIM_ARR ((uint16_t)0xFFFF) /* actual auto-reload Value */ /******************* Bit definition for TIM_RPTCR register ********************/ #define TIM_REP ((uint8_t)0xFF) /* Repetition Counter Value */ /******************* Bit definition for TIM_CH1CVR register *******************/ #define TIM_CCR1 ((uint16_t)0xFFFF) /* Capture/Compare 1 Value */ /******************* Bit definition for TIM_CH2CVR register *******************/ #define TIM_CCR2 ((uint16_t)0xFFFF) /* Capture/Compare 2 Value */ /******************* Bit definition for TIM_CH3CVR register *******************/ #define TIM_CCR3 ((uint16_t)0xFFFF) /* Capture/Compare 3 Value */ /******************* Bit definition for TIM_CH4CVR register *******************/ #define TIM_CCR4 ((uint16_t)0xFFFF) /* Capture/Compare 4 Value */ /******************* Bit definition for TIM_BDTR register *******************/ #define TIM_DTG ((uint16_t)0x00FF) /* DTG[0:7] bits (Dead-Time Generator set-up) */ #define TIM_DTG_0 ((uint16_t)0x0001) /* Bit 0 */ #define TIM_DTG_1 ((uint16_t)0x0002) /* Bit 1 */ #define TIM_DTG_2 ((uint16_t)0x0004) /* Bit 2 */ #define TIM_DTG_3 ((uint16_t)0x0008) /* Bit 3 */ #define TIM_DTG_4 ((uint16_t)0x0010) /* Bit 4 */ #define TIM_DTG_5 ((uint16_t)0x0020) /* Bit 5 */ #define TIM_DTG_6 ((uint16_t)0x0040) /* Bit 6 */ #define TIM_DTG_7 ((uint16_t)0x0080) /* Bit 7 */ #define TIM_LOCK ((uint16_t)0x0300) /* LOCK[1:0] bits (Lock Configuration) */ #define TIM_LOCK_0 ((uint16_t)0x0100) /* Bit 0 */ #define TIM_LOCK_1 ((uint16_t)0x0200) /* Bit 1 */ #define TIM_OSSI ((uint16_t)0x0400) /* Off-State Selection for Idle mode */ #define TIM_OSSR ((uint16_t)0x0800) /* Off-State Selection for Run mode */ #define TIM_BKE ((uint16_t)0x1000) /* Break enable */ #define TIM_BKP ((uint16_t)0x2000) /* Break Polarity */ #define TIM_AOE ((uint16_t)0x4000) /* Automatic Output enable */ #define TIM_MOE ((uint16_t)0x8000) /* Main Output enable */ /******************* Bit definition for TIM_DMACFGR register ********************/ #define TIM_DBA ((uint16_t)0x001F) /* DBA[4:0] bits (DMA Base Address) */ #define TIM_DBA_0 ((uint16_t)0x0001) /* Bit 0 */ #define TIM_DBA_1 ((uint16_t)0x0002) /* Bit 1 */ #define TIM_DBA_2 ((uint16_t)0x0004) /* Bit 2 */ #define TIM_DBA_3 ((uint16_t)0x0008) /* Bit 3 */ #define TIM_DBA_4 ((uint16_t)0x0010) /* Bit 4 */ #define TIM_DBL ((uint16_t)0x1F00) /* DBL[4:0] bits (DMA Burst Length) */ #define TIM_DBL_0 ((uint16_t)0x0100) /* Bit 0 */ #define TIM_DBL_1 ((uint16_t)0x0200) /* Bit 1 */ #define TIM_DBL_2 ((uint16_t)0x0400) /* Bit 2 */ #define TIM_DBL_3 ((uint16_t)0x0800) /* Bit 3 */ #define TIM_DBL_4 ((uint16_t)0x1000) /* Bit 4 */ /******************* Bit definition for TIM_DMAADR register *******************/ #define TIM_DMAR_DMAB ((uint16_t)0xFFFF) /* DMA register for burst accesses */ /******************* Bit definition for TIM_SPEC register *******************/ #define TIM_SPEC_PWM_EN_1_2 ((uint16_t)0x0001) /* Channel 1 and Channel 2 alternate */ #define TIM_SPEC_PWM_EN_3_4 ((uint16_t)0x0002) /* Channel 3 and Channel 4 alternate */ #define TIM_SPEC_PWM_OC1 ((uint16_t)0x0010) /* Channel 1 invalid level under alternate mode */ #define TIM_SPEC_PWM_OC2 ((uint16_t)0x0020) /* Channel 2 invalid level under alternate mode */ #define TIM_SPEC_PWM_OC3 ((uint16_t)0x0040) /* Channel 3 invalid level under alternate mode */ #define TIM_SPEC_PWM_OC4 ((uint16_t)0x0080) /* Channel 4 invalid level under alternate mode */ #define TIM_SPEC_TOGGLE ((uint16_t)0x8000) /* valid channel indicator */ /******************************************************************************/ /* Universal Synchronous Asynchronous Receiver Transmitter */ /******************************************************************************/ /******************* Bit definition for USART_STATR register *******************/ #define USART_STATR_PE ((uint16_t)0x0001) /* Parity Error */ #define USART_STATR_FE ((uint16_t)0x0002) /* Framing Error */ #define USART_STATR_NE ((uint16_t)0x0004) /* Noise Error Flag */ #define USART_STATR_ORE ((uint16_t)0x0008) /* OverRun Error */ #define USART_STATR_IDLE ((uint16_t)0x0010) /* IDLE line detected */ #define USART_STATR_RXNE ((uint16_t)0x0020) /* Read Data Register Not Empty */ #define USART_STATR_TC ((uint16_t)0x0040) /* Transmission Complete */ #define USART_STATR_TXE ((uint16_t)0x0080) /* Transmit Data Register Empty */ #define USART_STATR_LBD ((uint16_t)0x0100) /* LIN Break Detection Flag */ #define USART_STATR_CTS ((uint16_t)0x0200) /* CTS Flag */ /******************* Bit definition for USART_DATAR register *******************/ #define USART_DATAR_DR ((uint16_t)0x01FF) /* Data value */ /****************** Bit definition for USART_BRR register *******************/ #define USART_BRR_DIV_Fraction ((uint16_t)0x000F) /* Fraction of USARTDIV */ #define USART_BRR_DIV_Mantissa ((uint16_t)0xFFF0) /* Mantissa of USARTDIV */ /****************** Bit definition for USART_CTLR1 register *******************/ #define USART_CTLR1_SBK ((uint16_t)0x0001) /* Send Break */ #define USART_CTLR1_RWU ((uint16_t)0x0002) /* Receiver wakeup */ #define USART_CTLR1_RE ((uint16_t)0x0004) /* Receiver Enable */ #define USART_CTLR1_TE ((uint16_t)0x0008) /* Transmitter Enable */ #define USART_CTLR1_IDLEIE ((uint16_t)0x0010) /* IDLE Interrupt Enable */ #define USART_CTLR1_RXNEIE ((uint16_t)0x0020) /* RXNE Interrupt Enable */ #define USART_CTLR1_TCIE ((uint16_t)0x0040) /* Transmission Complete Interrupt Enable */ #define USART_CTLR1_TXEIE ((uint16_t)0x0080) /* PE Interrupt Enable */ #define USART_CTLR1_PEIE ((uint16_t)0x0100) /* PE Interrupt Enable */ #define USART_CTLR1_PS ((uint16_t)0x0200) /* Parity Selection */ #define USART_CTLR1_PCE ((uint16_t)0x0400) /* Parity Control Enable */ #define USART_CTLR1_WAKE ((uint16_t)0x0800) /* Wakeup method */ #define USART_CTLR1_M ((uint16_t)0x1000) /* Word length */ #define USART_CTLR1_UE ((uint16_t)0x2000) /* USART Enable */ /****************** Bit definition for USART_CTLR2 register *******************/ #define USART_CTLR2_ADD ((uint16_t)0x000F) /* Address of the USART node */ #define USART_CTLR2_LBDL ((uint16_t)0x0020) /* LIN Break Detection Length */ #define USART_CTLR2_LBDIE ((uint16_t)0x0040) /* LIN Break Detection Interrupt Enable */ #define USART_CTLR2_LBCL ((uint16_t)0x0100) /* Last Bit Clock pulse */ #define USART_CTLR2_CPHA ((uint16_t)0x0200) /* Clock Phase */ #define USART_CTLR2_CPOL ((uint16_t)0x0400) /* Clock Polarity */ #define USART_CTLR2_CLKEN ((uint16_t)0x0800) /* Clock Enable */ #define USART_CTLR2_STOP ((uint16_t)0x3000) /* STOP[1:0] bits (STOP bits) */ #define USART_CTLR2_STOP_0 ((uint16_t)0x1000) /* Bit 0 */ #define USART_CTLR2_STOP_1 ((uint16_t)0x2000) /* Bit 1 */ #define USART_CTLR2_LINEN ((uint16_t)0x4000) /* LIN mode enable */ /****************** Bit definition for USART_CTLR3 register *******************/ #define USART_CTLR3_EIE ((uint16_t)0x0001) /* Error Interrupt Enable */ #define USART_CTLR3_IREN ((uint16_t)0x0002) /* IrDA mode Enable */ #define USART_CTLR3_IRLP ((uint16_t)0x0004) /* IrDA Low-Power */ #define USART_CTLR3_HDSEL ((uint16_t)0x0008) /* Half-Duplex Selection */ #define USART_CTLR3_NACK ((uint16_t)0x0010) /* Smartcard NACK enable */ #define USART_CTLR3_SCEN ((uint16_t)0x0020) /* Smartcard mode enable */ #define USART_CTLR3_DMAR ((uint16_t)0x0040) /* DMA Enable Receiver */ #define USART_CTLR3_DMAT ((uint16_t)0x0080) /* DMA Enable Transmitter */ #define USART_CTLR3_RTSE ((uint16_t)0x0100) /* RTS Enable */ #define USART_CTLR3_CTSE ((uint16_t)0x0200) /* CTS Enable */ #define USART_CTLR3_CTSIE ((uint16_t)0x0400) /* CTS Interrupt Enable */ /****************** Bit definition for USART_GPR register ******************/ #define USART_GPR_PSC ((uint16_t)0x00FF) /* PSC[7:0] bits (Prescaler value) */ #define USART_GPR_PSC_0 ((uint16_t)0x0001) /* Bit 0 */ #define USART_GPR_PSC_1 ((uint16_t)0x0002) /* Bit 1 */ #define USART_GPR_PSC_2 ((uint16_t)0x0004) /* Bit 2 */ #define USART_GPR_PSC_3 ((uint16_t)0x0008) /* Bit 3 */ #define USART_GPR_PSC_4 ((uint16_t)0x0010) /* Bit 4 */ #define USART_GPR_PSC_5 ((uint16_t)0x0020) /* Bit 5 */ #define USART_GPR_PSC_6 ((uint16_t)0x0040) /* Bit 6 */ #define USART_GPR_PSC_7 ((uint16_t)0x0080) /* Bit 7 */ #define USART_GPR_GT ((uint16_t)0xFF00) /* Guard time value */ /******************************************************************************/ /* Window WATCHDOG */ /******************************************************************************/ /******************* Bit definition for WWDG_CTLR register ********************/ #define WWDG_CTLR_T ((uint8_t)0x7F) /* T[6:0] bits (7-Bit counter (MSB to LSB)) */ #define WWDG_CTLR_T0 ((uint8_t)0x01) /* Bit 0 */ #define WWDG_CTLR_T1 ((uint8_t)0x02) /* Bit 1 */ #define WWDG_CTLR_T2 ((uint8_t)0x04) /* Bit 2 */ #define WWDG_CTLR_T3 ((uint8_t)0x08) /* Bit 3 */ #define WWDG_CTLR_T4 ((uint8_t)0x10) /* Bit 4 */ #define WWDG_CTLR_T5 ((uint8_t)0x20) /* Bit 5 */ #define WWDG_CTLR_T6 ((uint8_t)0x40) /* Bit 6 */ #define WWDG_CTLR_WDGA ((uint8_t)0x80) /* Activation bit */ /******************* Bit definition for WWDG_CFGR register *******************/ #define WWDG_CFGR_W ((uint16_t)0x007F) /* W[6:0] bits (7-bit window value) */ #define WWDG_CFGR_W0 ((uint16_t)0x0001) /* Bit 0 */ #define WWDG_CFGR_W1 ((uint16_t)0x0002) /* Bit 1 */ #define WWDG_CFGR_W2 ((uint16_t)0x0004) /* Bit 2 */ #define WWDG_CFGR_W3 ((uint16_t)0x0008) /* Bit 3 */ #define WWDG_CFGR_W4 ((uint16_t)0x0010) /* Bit 4 */ #define WWDG_CFGR_W5 ((uint16_t)0x0020) /* Bit 5 */ #define WWDG_CFGR_W6 ((uint16_t)0x0040) /* Bit 6 */ #define WWDG_CFGR_WDGTB ((uint16_t)0x0180) /* WDGTB[1:0] bits (Timer Base) */ #define WWDG_CFGR_WDGTB0 ((uint16_t)0x0080) /* Bit 0 */ #define WWDG_CFGR_WDGTB1 ((uint16_t)0x0100) /* Bit 1 */ #define WWDG_CFGR_EWI ((uint16_t)0x0200) /* Early Wakeup Interrupt */ /******************* Bit definition for WWDG_STATR register ********************/ #define WWDG_STATR_EWIF ((uint8_t)0x01) /* Early Wakeup Interrupt Flag */ /******************************************************************************/ /* PIOC */ /******************************************************************************/ /****************** Bit definition for TMR0_CTRL register *******************/ #define PIOC_EN_LEVEL1 ((uint8_t)0x80) // RO/RW, enable IO1 level change to wakeup & action interrupt flag #define PIOC_EN_LEVEL0 ((uint8_t)0x40) // RO/RW, enable IO0 level change to wakeup & action interrupt flag #define PIOC_GP_BIT_Y ((uint8_t)0x20) // RO/RW, general-purpose bit 1, reset by power on, no effect if system reset or RB_MST_RESET reset #define PIOC_GP_BIT_X ((uint8_t)0x10) // RO/RW, general-purpose bit 0, reset by power on, no effect if system reset or RB_MST_RESET reset #define PIOC_TMR0_MODE ((uint8_t)0x08) // RO/RW, timer mode: 0-timer, 1-PWM #define PIOC_TMR0_FREQ2 ((uint8_t)0x04) // RO/RW, timer clock frequency selection 2 #define PIOC_TMR0_FREQ1 ((uint8_t)0x02) // RO/RW, timer clock frequency selection 1 #define PIOC_TMR0_FREQ0 ((uint8_t)0x01) // RO/RW, timer clock frequency selection 0 /****************** Bit definition for BIT_CYCLE register *******************/ #define PIOC_BIT_TX_IO0 ((uint8_t)0x80) // RO/RW, bit data for IO0 port encode output #define PIOC_BIT_CYCLE ((uint8_t)0x7F) // RO/RW, IO0 port bit data cycle -1 /****************** Bit definition for PORT_DIR register ********************/ #define PIOC_PORT_MOD3 ((uint8_t)0x80) // RO/RW, IO port mode 3 #define PIOC_PORT_MOD2 ((uint8_t)0x40) // RO/RW, IO port mode 2 #define PIOC_PORT_MOD1 ((uint8_t)0x20) // RO/RW, IO port mode 1 #define PIOC_PORT_MOD0 ((uint8_t)0x10) // RO/RW, IO port mode 0 #define PIOC_PORT_PU1 ((uint8_t)0x08) // RO/RW, IO1 port pullup enable #define PIOC_PORT_PU0 ((uint8_t)0x04) // RO/RW, IO0 port pullup enable #define PIOC_PORT_DIR1 ((uint8_t)0x02) // RO/RW, IO1 port direction #define PIOC_PORT_DIR0 ((uint8_t)0x01) // RO/RW, IO0 port direction /******************* Bit definition for PORT_IO register ********************/ #define PIOC_PORT_IN_XOR ((uint8_t)0x80) // RO/RO, IO0 XOR IO1 port input #define PIOC_BIT_RX_I0 ((uint8_t)0x40) // RO/RO, decoced bit data for IO0 port received #define PIOC_PORT_IN1 ((uint8_t)0x20) // RO/RO, IO1 port input #define PIOC_PORT_IN0 ((uint8_t)0x10) // RO/RO, IO0 port input #define PIOC_PORT_XOR1 ((uint8_t)0x08) // RO/RO, IO1 port output XOR input #define PIOC_PORT_XOR0 ((uint8_t)0x04) // RO/RO, IO0 port output XOR input #define PIOC_PORT_OUT1 ((uint8_t)0x02) // RO/RW, IO1 port output #define PIOC_PORT_OUT0 ((uint8_t)0x01) // RO/RW, IO0 port output /******************* Bit definition for SYS_CFG register ********************/ #define PIOC_INT_REQ ((uint8_t)0x80) // RO/RW, PIOC interrupt request action, set 1/0 by PIOC, clear 0 by master write R8_CTRL_RD (no effect) #define PIOC_DATA_SW_MR ((uint8_t)0x40) // RO/RO, R8_CTRL_RD wait for read status, set 1 by PIOC write R8_CTRL_RD, clear 0 by master read R8_CTRL_RD #define PIOC_DATA_MW_SR ((uint8_t)0x20) // RO/RO, R8_CTRL_WR wait for read status, set 1 by master write R8_CTRL_WR, clear 0 by PIOC read R8_CTRL_WR #define PIOC_MST_CFG_B4 ((uint8_t)0x10) // RW/RO, config inform bit, default 0 #define PIOC_MST_IO_EN1 ((uint8_t)0x08) // RW/RO, IO1 switch enable, default 0 #define PIOC_MST_IO_EN0 ((uint8_t)0x04) // RW/RO, IO0 switch enable, default 0 #define PIOC_MST_RESET ((uint8_t)0x02) // RW/RO, force PIOC reset, high action, default 0 #define PIOC_MST_CLK_GATE ((uint8_t)0x01) // RW/RO, PIOC global clock enable, high action, default 0 /******************************************************************************/ /* Programmable Fast Interrupt Controller (PFIC) */ /******************************************************************************/ /****************** Bit definition for PFIC_CFGR register *******************/ #define PFIC_RESETSYS ((uint32_t)0x00000080) /* System reset */ #define PFIC_KEY1 ((uint32_t)0xFA050000) #define PFIC_KEY2 ((uint32_t)0xBCAF0000) #define PFIC_KEY3 ((uint32_t)0xBEEF0000) #define NVIC_RESETSYS ((uint32_t)0x00000080) #define NVIC_KEY1 ((uint32_t)0xFA050000) #define NVIC_KEY2 ((uint32_t)0xBCAF0000) #define NVIC_KEY3 ((uint32_t)0xBEEF0000) /****************** Bit definition for PFIC_SCTLR register ******************/ #define PFIC_SYSRESET ((uint32_t)0x80000000) /* System reset */ #define PFIC_SETEVENT ((uint32_t)0x00000020) /* Set event to wake up WFE case */ #define PFIC_SEVONPEND ((uint32_t)0x00000010) /* All events and IRQ can wake up */ #define PFIC_WFITOWFE ((uint32_t)0x00000008) /* Treat WFI as WFE */ #define PFIC_SLEEPDEEP ((uint32_t)0x00000004) /* 1:deep sleep; 0:sleep */ #define PFIC_SLEEPONEXIT ((uint32_t)0x00000002) /* 1:sleep after ISR */ #define NVIC_SYSRESET ((uint32_t)0x80000000) #define NVIC_SETEVENT ((uint32_t)0x00000020) #define NVIC_SEVONPEND ((uint32_t)0x00000010) #define NVIC_WFITOWFE ((uint32_t)0x00000008) #define NVIC_SLEEPDEEP ((uint32_t)0x00000004) #define NVIC_SLEEPONEXIT ((uint32_t)0x00000002) /******************************************************************************/ /* System Counter (STK / SysTick) */ /******************************************************************************/ /******************* Bit definition for STK_CTLR register *******************/ #define STK_CTLR_STE ((uint32_t)0x00000001) /* System counter enable */ #define STK_CTLR_STIE ((uint32_t)0x00000002) /* Counter interrupt enable */ #define STK_CTLR_STCLK ((uint32_t)0x00000004) /* 1: use HCLK; 0: use HCLK/8 */ #define STK_CTLR_STRE ((uint32_t)0x00000008) /* Auto-reload count enable */ #define STK_CTLR_MODE ((uint32_t)0x00000010) /* 0: upcount, 1: down count */ #define STK_CTLR_INIT ((uint32_t)0x00000020) /* counter init value update */ #define STK_CTLR_SWIE ((uint32_t)0x80000000) /* Software interrupt enable */ /******************** Bit definition for STK_SR register ********************/ #define STK_SR_CNTIF ((uint8_t)0x01) /* Count value comparison flag */ /******************************************************************************/ /* USBFS FUNCTION */ /******************************************************************************/ /****************************** CTRL register *******************************/ #define USBFS_UC_DMA_EN ((uint8_t)0x01) /* Enable DMA mode */ #define USBFS_UC_CLR_ALL ((uint8_t)0x02) /* Clear USB FIFO and interrupt flag */ #define USBFS_UC_RESET_SIE ((uint8_t)0x04) /* Reset USB protocol processor */ #define USBFS_UC_INT_BUSY ((uint8_t)0x08) /* Auto-suspend enable */ #define USBFS_UC_SYS_CTRL_MASK ((uint8_t)0x30) /* USB system configuration */ #define USBFS_UC_DEV_PU_EN ((uint8_t)0x20) /* USB device and pull-up enable */ #define USBFS_UC_LOW_SPEED ((uint8_t)0x40) /* 0: 12Mbps, 1: 1.5Mbps */ #define USBFS_UC_HOST_MODE ((uint8_t)0x80) /* 0: Host mode, 1: Device mode */ /***************************** INT_EN register *****************************/ #define USBFS_UIE_DETECT ((uint8_t)0x01) /* USB host: device (dis)connect interrupt */ #define USBFS_UIE_BUS_RST ((uint8_t)0x01) /* USB device: bus reset interrupt */ #define USBFS_UIE_TRANSFER ((uint8_t)0x02) /* USB transfer completion interrupt */ #define USBFS_UIE_SUSPEND ((uint8_t)0x04) /* USB bus suspend/wake-up interrupt */ #define USBFS_UIE_HST_SOF ((uint8_t)0x08) /* USB host: SOF timing interrupt */ #define USBFS_UIE_FIFO_OV ((uint8_t)0x10) /* FIFO overrun interrupt */ #define USBFS_UIE_DEV_NAK ((uint8_t)0x40) /* USB device: receive NAK interrupt */ #define USBFS_UIE_DEV_SOF ((uint8_t)0x80) /* USB device: receive SOF packet interrupt */ /***************************** DEV_AD register *****************************/ #define USBFS_USB_ADDR_MASK ((uint8_t)0x7f) /* USB device address */ #define USBFS_UDA_GP_BIT ((uint8_t)0x80) /* USB general flag, user self-defined */ /***************************** MIS_ST register *****************************/ #define USBFS_UMS_DEV_ATTACH ((uint8_t)0x01) /* USB host: USB device connected to port */ #define USBFS_UMS_DM_LEVEL ((uint8_t)0x02) /* USB host: 0: full speed, 1: low speed */ #define USBFS_UMS_SUSPEND ((uint8_t)0x04) /* USB suspension status */ #define USBFS_UMS_BUS_RESET ((uint8_t)0x08) /* USB bus reset status */ #define USBFS_UMS_R_FIFO_RDY ((uint8_t)0x10) /* USB receiver FIFO data ready status */ #define USBFS_UMS_SIE_FREE ((uint8_t)0x20) /* USB protocol processor idle status */ #define USBFS_UMS_SOF_ACT ((uint8_t)0x40) /* USB host: SOF packet transmission status */ #define USBFS_UMS_SOF_PRES ((uint8_t)0x80) /* USB host: SOF packet indicator status */ /***************************** INT_FG register *****************************/ #define USBFS_UIF_DETECT ((uint8_t)0x01) /* USB host: device (dis)connect interrupt */ #define USBFS_UIF_BUS_RST ((uint8_t)0x01) /* USB device: bus reset interrupt */ #define USBFS_UIF_TRANSFER ((uint8_t)0x02) /* USB transfer completion interrupt */ #define USBFS_UIF_SUSPEND ((uint8_t)0x04) /* USB bus suspend/wake-up interrupt */ #define USBFS_UIF_HST_SOF ((uint8_t)0x08) /* USB host: SOF timing interrupt */ #define USBFS_UIF_FIFO_OV ((uint8_t)0x10) /* FIFO overrun interrupt */ #define USBFS_U_SIE_FREE ((uint8_t)0x20) /* USB protocol processor idle status */ #define USBFS_U_TOG_OK ((uint8_t)0x40) /* Current synchronous flag match status */ #define USBFS_U_IS_NAK ((uint8_t)0x80) /* USB device: NAK acknowledge status */ /***************************** INT_ST register *****************************/ #define USBFS_UIS_H_RES_MASK ((uint8_t)0x0f) /* USB host: response PID of current transfer */ #define USBFS_UIS_ENDP_MASK ((uint8_t)0x0f) /* USB device: endpoint number of current transfer */ #define USBFS_UIS_TOKEN_MASK ((uint8_t)0x30) /* USB device: token PID of current transfer */ #define USBFS_UIS_TOKEN_OUT ((uint8_t)0x00) #define USBFS_UIS_TOKEN_SOF ((uint8_t)0x10) #define USBFS_UIS_TOKEN_IN ((uint8_t)0x20) #define USBFS_UIS_TOKEN_SETUP ((uint8_t)0x30) #define USBFS_UIS_TOG_OK ((uint8_t)0x40) /* Current synchronous flag match status */ #define USBFS_UIS_SETUP_ACT ((uint8_t)0x80) /* SETUP transaction complete */ /***************************** RX_LEN register *****************************/ #define USBFS_RX_LEN_MASK ((uint16_t)0x007f) /* Number of bytes received by current endpoint */ /******************************** UEP4_1_MOD *******************************/ #define USBFS_UEP4_BUF_MOD ((uint8_t)0x01) /* Endpoint 4 data buffer mode */ #define USBFS_UEP4_TX_EN ((uint8_t)0x04) /* Enable endpoint 4 transmission (IN) */ #define USBFS_UEP4_RX_EN ((uint8_t)0x08) /* Enable endpoint 4 reception (OUT) */ #define USBFS_UEP1_BUF_MOD ((uint8_t)0x10) /* Endpoint 1 data buffer mode */ #define USBFS_UEP1_TX_EN ((uint8_t)0x40) /* Enable endpoint 1 transmission (IN) */ #define USBFS_UEP1_RX_EN ((uint8_t)0x80) /* Enable endpoint 1 reception (OUT) */ /******************************** UEP2_3_MOD *******************************/ #define USBFS_UEP2_BUF_MOD ((uint8_t)0x01) /* Endpoint 2 data buffer mode */ #define USBFS_UEP2_TX_EN ((uint8_t)0x04) /* Enable endpoint 2 transmission (IN) */ #define USBFS_UEP2_RX_EN ((uint8_t)0x08) /* Enable endpoint 2 reception (OUT) */ #define USBFS_UEP3_BUF_MOD ((uint8_t)0x10) /* Endpoint data buffer mode */ #define USBFS_UEP3_TX_EN ((uint8_t)0x40) /* Enable endpoint transmission (IN) */ #define USBFS_UEP3_RX_EN ((uint8_t)0x80) /* Enable endpoint 3 reception (OUT) */ /******************************** UEP567_MOD ********************************/ #define USBFS_UEP5_TX_EN ((uint8_t)0x01) /* Enable endpoint 5 transmission (IN) */ #define USBFS_UEP5_RX_EN ((uint8_t)0x02) /* Enable endpoint 5 reception (OUT) */ #define USBFS_UEP6_TX_EN ((uint8_t)0x04) /* Enable endpoint 6 transmission (IN) */ #define USBFS_UEP6_RX_EN ((uint8_t)0x08) /* Enable endpoint 6 reception (OUT) */ #define USBFS_UEP7_TX_EN ((uint8_t)0x10) /* Enable endpoint 7 transmission (IN) */ #define USBFS_UEP7_RX_EN ((uint8_t)0x20) /* Enable endpoint 7 reception (OUT) */ /******************************** UDEV_CTRL ********************************/ #define USBFS_UD_PORT_EN ((uint8_t)0x01) /* USB device physical port enable */ #define USBFS_UD_GP_BIT ((uint8_t)0x02) /* USB device mode general flag, user-defined */ #define USBFS_UD_LOW_SPEED ((uint8_t)0x04) /* USB device physical port low-speed enable */ #define USBFS_UD_DM_PIN ((uint8_t)0x10) /* Current UDM pin status */ #define USBFS_UD_DP_PIN ((uint8_t)0x20) /* Current UDP pin status */ #define USBFS_UD_PD_DIS ((uint8_t)0x80) /* UDP/UDM pull-down disable */ /******************************** UEPn_CTRL_H ******************************/ #define USBFS_UEP_T_RES_MASK ((uint16_t)0x0003) /* Response control by transmitter to IN services */ #define USBFS_UEP_T_RES_ACK ((uint16_t)0x0000) /* Rensponse ACK (ready) to IN services */ #define USBFS_UEP_T_RES_TOUT ((uint16_t)0x0001) /* No response to IN services */ #define USBFS_UEP_T_RES_NAK ((uint16_t)0x0002) /* Rensponse NAK (busy) to IN services */ #define USBFS_UEP_T_RES_STALL ((uint16_t)0x0003) /* Rensponse STALL (error) to IN services */ #define USBFS_UEP_R_RES_MASK ((uint16_t)0x000c) /* Response control by transmitter to OUT services */ #define USBFS_UEP_R_RES_ACK ((uint16_t)0x0000) /* Rensponse ACK (ready) to OUT services */ #define USBFS_UEP_R_RES_TOUT ((uint16_t)0x0004) /* No response to OUT services */ #define USBFS_UEP_R_RES_NAK ((uint16_t)0x0008) /* Rensponse NAK (busy) to OUT services */ #define USBFS_UEP_R_RES_STALL ((uint16_t)0x000c) /* Rensponse STALL (error) to OUT services */ #define USBFS_UEP_AUTO_TOG ((uint16_t)0x0010) /* Synchronous trigger bit auto flip enable */ #define USBFS_UEP_T_TOG ((uint16_t)0x0040) /* Synchronization trigger bit for IN services */ #define USBFS_UEP_R_TOG ((uint16_t)0x0080) /* Synchronization trigger bit for OUT services */ #define USBFS_UEP_RES_MASK (USBFS_UEP_T_RES_MASK | USBFS_UEP_R_RES_MASK) /********************************* UEPX_MOD *********************************/ #define USBFS_UEP8_TX_EN ((uint32_t)0x00000001) /* Enable endpoint 8 transmission (IN) */ #define USBFS_UEP9_TX_EN ((uint32_t)0x00000002) /* Enable endpoint 9 transmission (IN) */ #define USBFS_UEP10_TX_EN ((uint32_t)0x00000004) /* Enable endpoint 10 transmission (IN) */ #define USBFS_UEP11_TX_EN ((uint32_t)0x00000008) /* Enable endpoint 11 transmission (IN) */ #define USBFS_UEP12_TX_EN ((uint32_t)0x00000010) /* Enable endpoint 12 transmission (IN) */ #define USBFS_UEP13_TX_EN ((uint32_t)0x00000020) /* Enable endpoint 13 transmission (IN) */ #define USBFS_UEP14_TX_EN ((uint32_t)0x00000040) /* Enable endpoint 14 transmission (IN) */ #define USBFS_UEP15_TX_EN ((uint32_t)0x00000080) /* Enable endpoint 15 transmission (IN) */ #define USBFS_UEP8_RX_EN ((uint32_t)0x00000100) /* Enable endpoint 8 reception (OUT) */ #define USBFS_UEP9_RX_EN ((uint32_t)0x00000200) /* Enable endpoint 9 reception (OUT) */ #define USBFS_UEP10_RX_EN ((uint32_t)0x00000400) /* Enable endpoint 10 reception (OUT) */ #define USBFS_UEP11_RX_EN ((uint32_t)0x00000800) /* Enable endpoint 11 reception (OUT) */ #define USBFS_UEP12_RX_EN ((uint32_t)0x00001000) /* Enable endpoint 12 reception (OUT) */ #define USBFS_UEP13_RX_EN ((uint32_t)0x00002000) /* Enable endpoint 13 reception (OUT) */ #define USBFS_UEP14_RX_EN ((uint32_t)0x00004000) /* Enable endpoint 14 reception (OUT) */ #define USBFS_UEP15_RX_EN ((uint32_t)0x00008000) /* Enable endpoint 15 reception (OUT) */ #define USBFS_UEP8_TX_AF ((uint32_t)0x00010000) /* Enable endpoint 8 alternate (IN) */ #define USBFS_UEP9_TX_AF ((uint32_t)0x00020000) /* Enable endpoint 9 alternate (IN) */ #define USBFS_UEP10_TX_AF ((uint32_t)0x00040000) /* Enable endpoint 10 alternate (IN) */ #define USBFS_UEP11_TX_AF ((uint32_t)0x00080000) /* Enable endpoint 11 alternate (IN) */ #define USBFS_UEP12_TX_AF ((uint32_t)0x00100000) /* Enable endpoint 12 alternate (IN) */ #define USBFS_UEP13_TX_AF ((uint32_t)0x00200000) /* Enable endpoint 13 alternate (IN) */ #define USBFS_UEP14_TX_AF ((uint32_t)0x00400000) /* Enable endpoint 14 alternate (IN) */ #define USBFS_UEP15_TX_AF ((uint32_t)0x00800000) /* Enable endpoint 15 alternate (IN) */ /******************************** UHOST_CTRL *******************************/ #define USBFS_UH_PORT_EN ((uint8_t)0x01) /* USB host port enable */ #define USBFS_UH_BUS_RESET ((uint8_t)0x02) /* USB host mode bus reset */ #define USBFS_UH_LOW_SPEED ((uint8_t)0x04) /* USB host port low-speed mode enable */ #define USBFS_UH_DM_PIN ((uint8_t)0x10) /* Current UDM pin status */ #define USBFS_UH_DP_PIN ((uint8_t)0x20) /* Current UDP pin status */ #define USBFS_UH_PD_DIS ((uint8_t)0x80) /* USB host port UDP/UDM pull-down disable */ /********************************* UH_EP_MOD *******************************/ #define USBFS_UH_EP_RBUF_MOD ((uint8_t)0x01) /* USB host reception endpoint data buffer mode */ #define USBFS_UH_EP_RX_EN ((uint8_t)0x08) /* Host reception endpoint receive (IN) enable */ #define USBFS_UH_EP_TBUF_MOD ((uint8_t)0x10) /* Host transmission endpoint data buffer mode */ #define USBFS_UH_EP_TX_EN ((uint8_t)0x40) /* Host transmission endpoint transmit (SETUP/OUT) enable */ /********************************* UH_SETUP ********************************/ #define USBFS_UH_SOF_EN ((uint8_t)0x40) /* Automatically generate SOF packet enable */ #define USBFS_UH_PRE_PID_EN ((uint8_t)0x80) /* Low-speed preamble packet PRE PID enable */ /********************************* UH_EP_PID *******************************/ #define USBFS_UH_ENDP_MASK ((uint8_t)0x0f) /* Set endpoint number of current target device */ #define USBFS_UH_TOKEN_MASK ((uint8_t)0xf0) /* Set token PID identification of this USB transmission */ /******************************** UH_RX_CTRL *******************************/ #define USBFS_UH_R_RES ((uint8_t)0x04) /* Control on response to IN transactions */ #define USBFS_UH_R_AUTO_TOG ((uint8_t)0x10) /* Synchronous trigger bit auto flip enable */ #define USBFS_UH_R_TOG ((uint8_t)0x80) /* Synchronous trigger bit expected by receiver */ /******************************** UH_TX_CTRL *******************************/ #define USBFS_UH_T_RES ((uint8_t)0x01) /* Response control to SETUP/OUT transaction */ #define USBFS_UH_T_AUTO_TOG ((uint8_t)0x10) /* Synchronous trigger bit auto flip enable */ #define USBFS_UH_T_TOG ((uint8_t)0x40) /* Synchronous trigger bit prepared by transmitter */ /*************************** USB host mode aliases *************************/ #define USBFS_UHOST_CTRL USBFS_UDEV_CTRL #define USBFS_UH_EP_MOD USBFS_UEP2_3_MOD #define USBFS_UH_RX_DMA USBFS_UEP2_DMA #define USBFS_UH_TX_DMA USBFS_UEP3_DMA #define USBFS_UH_SETUP USBFS_UEP1_CTRL #define USBFS_UH_EP_PID USBFS_UEP2_T_LEN #define USBFS_UH_RX_CTRL USBFS_UEP2_CTRL #define USBFS_UH_TX_LEN USBFS_UEP3_T_LEN #define USBFS_UH_TX_CTRL USBFS_UEP3_CTRL /************************ AFIO->CTLR register aliases **********************/ #define USBFS_UDM_PUE AFIO_CTLR_UDM_PUE /* PC16/UDM Pin pull-up Mode*/ #define USBFS_UDM_PUE_1K5 AFIO_CTLR_UDM_PUE_1K5 /* pull-up 1.5KΩ */ #define USBFS_UDM_PUE_10K AFIO_CTLR_UDM_PUE_10K /* pull-up 10KΩ */ #define USBFS_UDP_PUE AFIO_CTLR_UDP_PUE /* PC17/UDP Pin pull-up Mode*/ #define USBFS_UDP_PUE_1K5 AFIO_CTLR_UDP_PUE_1K5 /* pull-up 1.5KΩ */ #define USBFS_UDP_PUE_10K AFIO_CTLR_UDP_PUE_10K /* pull-up 10KΩ */ #define USBFS_USB_PHY_V33 AFIO_CTLR_USB_PHY_V33 /* USB transceiver PHY output and pull-up limiter configuration */ #define USBFS_USB_IOEN AFIO_CTLR_USB_IOEN /* USB Remap pin enable */ /******************************************************************************/ /* USBPD FUNCTION */ /******************************************************************************/ /***************************** CONFIG register ******************************/ #define USBPD_PD_FILT_EN ((uint16_t)0x0001) // input filter enable on the PD pin #define USBPD_PD_ALL_CLR ((uint16_t)0x0002) // clear all interrupt flags #define USBPD_CC_SEL ((uint16_t)0x0004) // select current PD port (0:CC1, 1:CC2) #define USBPD_PD_DMA_EN ((uint16_t)0x0008) // enable DMA function and DMA interrupt #define USBPD_PD_RST_EN ((uint16_t)0x0010) // PD mode reset command enable #define USBPD_WAKE_POLAR ((uint16_t)0x0020) // PD port wake-up level (0:active low, 1: active high) #define USBPD_IE_PD_IO ((uint16_t)0x0400) // PD IO interrupt enable #define USBPD_IE_RX_BIT ((uint16_t)0x0800) // receive bit interrupt enable #define USBPD_IE_RX_BYTE ((uint16_t)0x1000) // receive byte interrupt enable #define USBPD_IE_RX_ACT ((uint16_t)0x2000) // receive complete interrupt enable #define USBPD_IE_RX_RESET ((uint16_t)0x4000) // receive reset interrupt enable #define USBPD_IE_TX_END ((uint16_t)0x8000) // transmit end interrupt enable /***************************** CONTROL register *****************************/ #define USBPD_PD_TX_EN ((uint8_t)0x01) // PD mode (0: RX enable, 1: TX enable) #define USBPD_BMC_START ((uint8_t)0x02) // BMC transmit start signal #define USBPD_RX_STATE ((uint8_t)0x1c) // PD receive status identification #define USBPD_DATA_FLAG ((uint8_t)0x20) // cache data valid flag bit #define USBPD_TX_BIT_BACK ((uint8_t)0x40) // current bit status of the BMC (0: idle, 1: BMC byte is being transmitted) #define USBPD_BMC_BYTE_HI ((uint8_t)0x80) // current half-byte status PD data (0: low nibble, 1: high nibble) #define USBPD_RX_STATE_INIT ((uint8_t)0x00) // receive initial status #define USBPD_RX_STATE_START ((uint8_t)0x04) // start receiving SOP #define USBPD_RX_STATE_RESET ((uint8_t)0x08) // receive reset #define USBPD_RX_STATE_SOP ((uint8_t)0x0c) // receive SOP #define USBPD_RX_STATE_END ((uint8_t)0x10) // receive end #define USBPD_RX_STATE_UNUSED ((uint8_t)0x14) // receive unused #define USBPD_RX_STATE_EOP ((uint8_t)0x18) // receive EOP #define USBPD_RX_STATE_BYTE ((uint8_t)0x1c) // receive byte /****************************** TX_SEL register ******************************/ #define USBPD_TX_SEL1 ((uint8_t)0x01) // K-CODE1 type selection in PD transmit mode #define USBPD_TX_SEL2 ((uint8_t)0x0c) // K-CODE2 type selection in PD transmit mode #define USBPD_TX_SEL3 ((uint8_t)0x30) // K-CODE3 type selection in PD transmit mode #define USBPD_TX_SEL4 ((uint8_t)0xc0) // K-CODE4 type selection in PD transmit mode #define USBPD_TX_SEL1_SYNC1 ((uint8_t)0x00) // SYNC1 #define USBPD_TX_SEL1_RST1 ((uint8_t)0x01) // RST1 #define USBPD_TX_SEL2_SYNC1 ((uint8_t)0x00) // SYNC1 #define USBPD_TX_SEL2_SYNC3 ((uint8_t)0x04) // SYNC3 #define USBPD_TX_SEL2_RST1 ((uint8_t)0x08) // RST1 #define USBPD_TX_SEL3_SYNC1 ((uint8_t)0x00) // SYNC1 #define USBPD_TX_SEL3_SYNC3 ((uint8_t)0x10) // SYNC3 #define USBPD_TX_SEL3_RST1 ((uint8_t)0x20) // RST1 #define USBPD_TX_SEL4_SYNC2 ((uint8_t)0x00) // SYNC2 #define USBPD_TX_SEL4_SYNC3 ((uint8_t)0x40) // SYNC3 #define USBPD_TX_SEL4_RST1 ((uint8_t)0x80) // RST2 #define USBPD_TX_SOP0 (USBPD_TX_SEL1_SYNC1 | USBPD_TX_SEL2_SYNC1 | USBPD_TX_SEL3_SYNC1 | USBPD_TX_SEL4_SYNC2) // Start of Packet Sequence #define USBPD_TX_SOP1 (USBPD_TX_SEL1_SYNC1 | USBPD_TX_SEL2_SYNC1 | USBPD_TX_SEL3_SYNC3 | USBPD_TX_SEL4_SYNC3) // Start of Packet Sequence Prime #define USBPD_TX_SOP2 (USBPD_TX_SEL1_SYNC1 | USBPD_TX_SEL2_SYNC3 | USBPD_TX_SEL3_SYNC1 | USBPD_TX_SEL4_SYNC3) // Start of Packet Sequence Double Prime #define USBPD_TX_HARD_RESET (USBPD_TX_SEL1_RST1 | USBPD_TX_SEL2_RST1 | USBPD_TX_SEL3_RST1 | USBPD_TX_SEL4_RST2 ) // Hard Reset #define USBPD_TX_CABLE_RESET (USBPD_TX_SEL1_RST1 | USBPD_TX_SEL2_SYNC1 | USBPD_TX_SEL3_RST1 | USBPD_TX_SEL4_SYNC3) // Cable Reset /****************************** STATUS register ******************************/ #define USBPD_BMC_AUX ((uint8_t)0x03) // current PD status #define USBPD_BUF_ERR ((uint8_t)0x04) // BUFFER or DMA error interrupt flag, write 1 to clear #define USBPD_IF_RX_BIT ((uint8_t)0x08) // receive bit or 5bit interrupt flag, write 1 to clear #define USBPD_IF_RX_BYTE ((uint8_t)0x10) // receive byte or SOP interrupt flag, write 1 to clear #define USBPD_IF_RX_ACT ((uint8_t)0x20) // receive complete interrupt flag, write 1 to clear #define USBPD_IF_RX_RESET ((uint8_t)0x40) // receive reset interrupt flag, write 1 to clear #define USBPD_IF_TX_END ((uint8_t)0x80) // transmit complete interrupt flag, write 1 to clear #define USBPD_BMC_AUX_INVALID ((uint8_t)0x00) // reception idle or no valid packet received #define USBPD_BMC_AUX_SOP0 ((uint8_t)0x01) // SOP received i.e. SOP0 #define USBPD_BMC_AUX_SOP1_HRST ((uint8_t)0x02) // SOP received i.e. SOP1 or hard reset #define USBPD_BMC_AUX_SOP2_CRST ((uint8_t)0x03) // SOP received i.e. SOP2 or cable reset #define USBPD_BMC_AUX_CRC0 ((uint8_t)0x00) // CRC32[ 7: 0] is being transmitted #define USBPD_BMC_AUX_CRC1 ((uint8_t)0x01) // CRC32[15: 8] is being transmitted #define USBPD_BMC_AUX_CRC2 ((uint8_t)0x02) // CRC32[23:16] is being transmitted #define USBPD_BMC_AUX_CRC3 ((uint8_t)0x03) // CRC32[31:24] is being transmitted /***************************** PORT_CCx register *****************************/ #define USBPD_PA_CC_AI ((uint16_t)0x0001) // CCx port comparator analog input #define USBPD_CC_PD ((uint16_t)0x0002) // CCx port pull-down enable #define USBPD_CC_PU ((uint16_t)0x000c) // CCx port pull-up current selection #define USBPD_CC_LVE ((uint16_t)0x0010) // CCx port output low voltage enable #define USBPD_CC_CE ((uint16_t)0x00e0) // CCx voltage comparator enable #define USBPD_CC_PU_CLR ((uint16_t)0x000c) // CCx port pull-up current selection #define USBPD_CC_PU_NO ((uint16_t)0x0000) // pull-up current disable #define USBPD_CC_PU_330 ((uint16_t)0x0004) // 330uA #define USBPD_CC_PU_180 ((uint16_t)0x0008) // 180uA #define USBPD_CC_PU_80 ((uint16_t)0x000c) // 80uA #define USBPD_CC_CMP_NO ((uint16_t)0x0000) // CCx voltage comparator disable #define USBPD_CC_CMP_22 ((uint16_t)0x0040) // 0.22V #define USBPD_CC_CMP_43 ((uint16_t)0x0060) // 0.43V #define USBPD_CC_CMP_55 ((uint16_t)0x0080) // 0.55V #define USBPD_CC_CMP_66 ((uint16_t)0x00a0) // 0.66V #define USBPD_CC_CMP_96 ((uint16_t)0x00c0) // 0.96V #define USBPD_CC_CMP_123 ((uint16_t)0x00e0) // 1.23V /************************* AFIO->CTLR register aliases ***********************/ #define USBPD_IN_HVT AFIO_CTLR_USBPD_IN_HVT // PD pin PC14/PC15 high threshold input mode #define USBPD_PHY_V33 AFIO_CTLR_USBPD_PHY_V33 // USBPD transceiver PHY output and pull-up limiter configuration #ifdef __cplusplus } #endif ================================================ FILE: software/pd_tester/src/gpio.h ================================================ // =================================================================================== // Basic GPIO Functions for CH32X035/X034/X033 * v0.4 * // =================================================================================== // // Pins must be defined as PA0, PA1, .., PB0, PB1, .. - e.g.: // #define PIN_LED PC0 // LED on pin PC0 // // PIN functions available: // ------------------------ // PIN_input(PIN) set PIN as INPUT (floating, no pullup/pulldown) // PIN_input_PU(PIN) set PIN as INPUT with internal PULLUP resistor // PIN_input_PD(PIN) set PIN as INPUT with internal PULLDOWN resistor // PIN_input_AN(PIN) set PIN as INPUT for analog peripherals (e.g. ADC) (*) // PIN_output(PIN) set PIN as OUTPUT (push-pull) // PIN_alternate(PIN) set PIN as alternate output mode // // PIN_low(PIN) set PIN output value to LOW (*) // PIN_high(PIN) set PIN output value to HIGH // PIN_toggle(PIN) TOGGLE PIN output value // PIN_read(PIN) read PIN input value // PIN_write(PIN, val) write PIN output value (0 = LOW / 1 = HIGH) // // PIN interrupt and event functions available: // -------------------------------------------- // PIN_EVT_set(PIN,TYPE) Setup PIN event TYPE: // PIN_EVT_OFF, PIN_EVT_RISING, PIN_EVT_FALLING, PIN_EVT_BOTH // PIN_INT_set(PIN,TYPE) Setup PIN interrupt TYPE: // PIN_INT_OFF, PIN_INT_RISING, PIN_INT_FALLING, PIN_INT_BOTH // PIN_INT_enable() Enable PIN interrupts // PIN_INT_disable() Disable PIN interrupts // PIN_INTFLAG_read(PIN) Read interrupt flag of PIN // PIN_INTFLAG_clear(PIN) Clear interrupt flag of PIN // PIN_INT_ISR { } Pin interrupt service routine // // PORT functions available: // ------------------------- // PORT_enable(PIN) enable GPIO PORT of PIN // PORTA_enable() enable GPIO PORT A // PORTB_enable() enable GPIO PORT B // PORTC_enable() enable GPIO PORT C // PORTS_enable() enable all GPIO PORTS // // PORT_disable(PIN) disable GPIO PORT of PIN // PORTA_disable() disable GPIO PORT A // PORTB_disable() disable GPIO PORT B // PORTC_disable() disable GPIO PORT C // PORTS_disable() disable all GPIO PORTS // // Analog-to-Digital Converter (ADC) functions available: // ------------------------------------------------------ // ADC_init() init and enable ADC (must be called first) // ADC_enable() enable ADC (power-up) // ADC_disable() disable ADC (power-down) (*) // ADC_fast() set fast mode (fast speed, least accurate) // ADC_slow() set slow mode (slow speed, most accurate) (*) // ADC_medium() set medium mode (medium speed, medium accurate) // // ADC_input(PIN) set PIN as ADC input // ADC_input_VREF() set internal voltage referece (Vref) as ADC input // // ADC_read() sample and read 12-bit ADC value (0..4095) // ADC_read_VDD() sample and read supply voltage (VDD) in millivolts (mV) // // Analog Comparator (CMP) functions available: // -------------------------------------------- // CMP_lock() lock comparators (*) // CMP_unlock() unlock comparators // // CMPx_enable() enable CMPx (x = 1..3) // CMPx_disable() disable CMPx (*) // CMPx_HYS_enable() enable CMPx hysteresis // CMPx_HYS_disable() disable CMPx hysteresis (*) // // CMP1_OUT_PA1() set CMP1 output to pin PA1 // CMP1_OUT_T2C1() set CMP1 output to timer2, channel1 (*) // CMP1_NEG_PA23() set CMP1 negative input to pin PA23 // CMP1_NEG_PC3() set CMP1 negative input to pin PC3 (*) // CMP1_POS_PA0() set CMP1 positive input to pin PA0 // CMP1_POS_PC19() set CMP1 positive input to pin PC19 (*) // // CMP2_OUT_PB2() set CMP2 output to pin PB2 // CMP2_OUT_T2C2() set CMP2 output to timer2, channel2 (*) // CMP2_NEG_PC3() set CMP2 negative input to pin PC3 // CMP2_NEG_PA22() set CMP2 negative input to pin PA22 (*) // CMP2_POS_PA11() set CMP2 positive input to pin PA11 // CMP2_POS_PA12() set CMP2 positive input to pin PA12 (*) // // CMP3_OUT_PB3() set CMP3 output to pin PB3 // CMP3_OUT_T2C3() set CMP3 output to timer2, channel3 (*) // CMP3_NEG_PC3() set CMP3 negative input to pin PC3 // CMP3_NEG_PA2() set CMP3 negative input to pin PA2 (*) // CMP3_POS_PA14() set CMP3 positive input to pin PA14 // CMP3_POS_PA13() set CMP3 positive input to pin PA13 (*) // // Operational Amplifier (OPA) functions available: // ------------------------------------------------ // not yet implemented // // Touch Key (TK) functions available: // ----------------------------------- // TK_init() init and enable touch key functions (must be called first) // TK_input(PIN) set PIN as touch key input // TK_read() returns TRUE if touch key is pressed // // Notes: // ------ // - (*) default state // - For interrupts and events: Each PIN number can only be used once simultaneously. // (For example, PA1 and PC1 cannot be used simultaneously, but PA1 and PC2). // - Pins used for ADC must be set with PIN_input_AN beforehand. ADC input pins are: // PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PB0, PB1, PC0, PC1, PC2, PC3. // // 2023 by Stefan Wagner: https://github.com/wagiminator #pragma once #ifdef __cplusplus extern "C" { #endif #include "system.h" // =================================================================================== // Enumerate PIN Designators (use these designators to define pins) // =================================================================================== enum{ PA0, PA1, PA2, PA3, PA4, PA5, PA6, PA7, PA8, PA9, PA10, PA11, PA12, PA13, PA14, PA15, PA16, PA17, PA18, PA19, PA20, PA21, PA22, PA23, PA24, PA25, PA26, PA27, PA28, PA29, PA30, PA31, PB0, PB1, PB2, PB3, PB4, PB5, PB6, PB7, PB8, PB9, PB10, PB11, PB12, PB13, PB14, PB15, PB16, PB17, PB18, PB19, PB20, PB21, PB22, PB23, PB24, PB25, PB26, PB27, PB28, PB29, PB30, PB31, PC0, PC1, PC2, PC3, PC4, PC5, PC6, PC7, PC8, PC9, PC10, PC11, PC12, PC13, PC14, PC15, PC16, PC17, PC18, PC19, PC20, PC21, PC22, PC23 }; // =================================================================================== // Set PIN as INPUT (high impedance, no pullup/pulldown) // =================================================================================== #define PIN_input(PIN) \ ((PIN>=PA0)&&(PIN<=PA7) ? ( GPIOA->CFGLR = (GPIOA->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ ((PIN>=PA8)&&(PIN<=PA15) ? ( GPIOA->CFGHR = (GPIOA->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ( GPIOA->CFGXR = (GPIOA->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ ((PIN>=PB0)&&(PIN<=PB7) ? ( GPIOB->CFGLR = (GPIOB->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ ((PIN>=PB8)&&(PIN<=PB15) ? ( GPIOB->CFGHR = (GPIOB->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ( GPIOB->CFGXR = (GPIOB->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ ((PIN>=PC0)&&(PIN<=PC7) ? ( GPIOC->CFGLR = (GPIOC->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ ((PIN>=PC8)&&(PIN<=PC15) ? ( GPIOC->CFGHR = (GPIOC->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ( GPIOC->CFGXR = (GPIOC->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0100<<(((PIN)&7)<<2))) : \ (0)))))))))) #define PIN_input_HI PIN_input #define PIN_input_FL PIN_input // =================================================================================== // Set PIN as INPUT with internal PULLUP resistor // =================================================================================== #define PIN_input_PU(PIN) \ ((PIN>=PA0)&&(PIN<=PA7) ? ({GPIOA->CFGLR = (GPIOA->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOA->BSHR = ((uint32_t)1<<((PIN)&15)); }) : \ ((PIN>=PA8)&&(PIN<=PA15) ? ({GPIOA->CFGHR = (GPIOA->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOA->BSHR = ((uint32_t)1<<((PIN)&15)); }) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ({GPIOA->CFGXR = (GPIOA->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOA->BSXR = ((uint32_t)1<<((PIN)&15)); }) : \ ((PIN>=PB0)&&(PIN<=PB7) ? ({GPIOB->CFGLR = (GPIOB->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOB->BSHR = ((uint32_t)1<<((PIN)&15)); }) : \ ((PIN>=PB8)&&(PIN<=PB15) ? ({GPIOB->CFGHR = (GPIOB->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOB->BSHR = ((uint32_t)1<<((PIN)&15)); }) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ({GPIOB->CFGXR = (GPIOB->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOB->BSXR = ((uint32_t)1<<((PIN)&15)); }) : \ ((PIN>=PC0)&&(PIN<=PC7) ? ({GPIOC->CFGLR = (GPIOC->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOC->BSHR = ((uint32_t)1<<((PIN)&15)); }) : \ ((PIN>=PC8)&&(PIN<=PC15) ? ({GPIOC->CFGHR = (GPIOC->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOC->BSHR = ((uint32_t)1<<((PIN)&15)); }) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ({GPIOC->CFGXR = (GPIOC->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOC->BSXR = ((uint32_t)1<<((PIN)&15)); }) : \ (0)))))))))) // =================================================================================== // Set PIN as INPUT with internal PULLDOWN resistor // =================================================================================== #define PIN_input_PD(PIN) \ ((PIN>=PA0)&&(PIN<=PA7) ? ({GPIOA->CFGLR = (GPIOA->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOA->BSHR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ ((PIN>=PA8)&&(PIN<=PA15) ? ({GPIOA->CFGHR = (GPIOA->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOA->BSHR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ({GPIOA->CFGXR = (GPIOA->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOA->BSXR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ ((PIN>=PB0)&&(PIN<=PB7) ? ({GPIOB->CFGLR = (GPIOB->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOB->BSHR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ ((PIN>=PB8)&&(PIN<=PB15) ? ({GPIOB->CFGHR = (GPIOB->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOB->BSHR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ({GPIOB->CFGXR = (GPIOB->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOB->BSXR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ ((PIN>=PC0)&&(PIN<=PC7) ? ({GPIOC->CFGLR = (GPIOC->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOC->BSHR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ ((PIN>=PC8)&&(PIN<=PC15) ? ({GPIOC->CFGHR = (GPIOC->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOC->BSHR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ({GPIOC->CFGXR = (GPIOC->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1000<<(((PIN)&7)<<2)); \ GPIOC->BSXR = (((uint32_t)1<<16)<<((PIN)&15));}) : \ (0)))))))))) // =================================================================================== // Set PIN as INPUT for analog peripherals (e.g. ADC) // =================================================================================== #define PIN_input_AN(PIN) \ ((PIN>=PA0)&&(PIN<=PA7) ? ( GPIOA->CFGLR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ ((PIN>=PA8)&&(PIN<=PA15) ? ( GPIOA->CFGHR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ( GPIOA->CFGXR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ ((PIN>=PB0)&&(PIN<=PB7) ? ( GPIOB->CFGLR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ ((PIN>=PB8)&&(PIN<=PB15) ? ( GPIOB->CFGHR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ( GPIOB->CFGXR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ ((PIN>=PC0)&&(PIN<=PC7) ? ( GPIOC->CFGLR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ ((PIN>=PC8)&&(PIN<=PC15) ? ( GPIOC->CFGHR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ( GPIOC->CFGXR &= ~((uint32_t)0b1111<<(((PIN)&7)<<2)) ) : \ (0)))))))))) #define PIN_input_AD PIN_input_AN #define PIN_input_ADC PIN_input_AN // =================================================================================== // Set PIN as OUTPUT (push-pull) // =================================================================================== #define PIN_output(PIN) \ ((PIN>=PA0)&&(PIN<=PA7) ? ( GPIOA->CFGLR = (GPIOA->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ ((PIN>=PA8)&&(PIN<=PA15) ? ( GPIOA->CFGHR = (GPIOA->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ( GPIOA->CFGXR = (GPIOA->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ ((PIN>=PB0)&&(PIN<=PB7) ? ( GPIOB->CFGLR = (GPIOB->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ ((PIN>=PB8)&&(PIN<=PB15) ? ( GPIOB->CFGHR = (GPIOB->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ( GPIOB->CFGXR = (GPIOB->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ ((PIN>=PC0)&&(PIN<=PC7) ? ( GPIOC->CFGLR = (GPIOC->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ ((PIN>=PC8)&&(PIN<=PC15) ? ( GPIOC->CFGHR = (GPIOC->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ( GPIOC->CFGXR = (GPIOC->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b0011<<(((PIN)&7)<<2))) : \ (0)))))))))) #define PIN_output_PP PIN_output // =================================================================================== // Set PIN as alternate output mode // =================================================================================== #define PIN_alternate(PIN) \ ((PIN>=PA0)&&(PIN<=PA7) ? ( GPIOA->CFGLR = (GPIOA->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ ((PIN>=PA8)&&(PIN<=PA15) ? ( GPIOA->CFGHR = (GPIOA->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ( GPIOA->CFGXR = (GPIOA->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ ((PIN>=PB0)&&(PIN<=PB7) ? ( GPIOB->CFGLR = (GPIOB->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ ((PIN>=PB8)&&(PIN<=PB15) ? ( GPIOB->CFGHR = (GPIOB->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ( GPIOB->CFGXR = (GPIOB->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ ((PIN>=PC0)&&(PIN<=PC7) ? ( GPIOC->CFGLR = (GPIOC->CFGLR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ ((PIN>=PC8)&&(PIN<=PC15) ? ( GPIOC->CFGHR = (GPIOC->CFGHR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ( GPIOC->CFGXR = (GPIOC->CFGXR \ & ~((uint32_t)0b1111<<(((PIN)&7)<<2))) \ | ((uint32_t)0b1011<<(((PIN)&7)<<2))) : \ (0)))))))))) // =================================================================================== // Set PIN output value to LOW // =================================================================================== #define PIN_low(PIN) \ ((PIN>=PA0 )&&(PIN<=PA15) ? ( GPIOA->BCR = 1<<((PIN)&15) ) : \ ((PIN>=PB0 )&&(PIN<=PB15) ? ( GPIOB->BCR = 1<<((PIN)&15) ) : \ ((PIN>=PC0 )&&(PIN<=PC15) ? ( GPIOC->BCR = 1<<((PIN)&15) ) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ( GPIOA->BSXR = ((1<<16)<<((PIN)&15)) ) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ( GPIOB->BSXR = ((1<<16)<<((PIN)&15)) ) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ( GPIOC->BSXR = ((1<<16)<<((PIN)&15)) ) : \ (0))))))) // =================================================================================== // Set PIN output value to HIGH // =================================================================================== #define PIN_high(PIN) \ ((PIN>=PA0 )&&(PIN<=PA15) ? ( GPIOA->BSHR = 1<<((PIN)&15) ) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ( GPIOA->BSXR = 1<<((PIN)&15) ) : \ ((PIN>=PB0 )&&(PIN<=PB15) ? ( GPIOB->BSHR = 1<<((PIN)&15) ) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ( GPIOB->BSXR = 1<<((PIN)&15) ) : \ ((PIN>=PC0 )&&(PIN<=PC15) ? ( GPIOC->BSHR = 1<<((PIN)&15) ) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ( GPIOC->BSXR = 1<<((PIN)&15) ) : \ (0))))))) // =================================================================================== // Toggle PIN output value // =================================================================================== #define PIN_toggle(PIN) \ ((PIN>=PA0)&&(PIN<=PA23) ? ( GPIOA->OUTDR ^= 1<<((PIN)&31) ) : \ ((PIN>=PB0)&&(PIN<=PB23) ? ( GPIOB->OUTDR ^= 1<<((PIN)&31) ) : \ ((PIN>=PC0)&&(PIN<=PC23) ? ( GPIOC->OUTDR ^= 1<<((PIN)&31) ) : \ (0)))) // =================================================================================== // Read PIN input value (returns 0 for LOW, 1 for HIGH) // =================================================================================== #define PIN_read(PIN) \ ((PIN>=PA0)&&(PIN<=PA23) ? ( (GPIOA->INDR>>((PIN)&31))&1 ) : \ ((PIN>=PB0)&&(PIN<=PB23) ? ( (GPIOB->INDR>>((PIN)&31))&1 ) : \ ((PIN>=PC0)&&(PIN<=PC23) ? ( (GPIOC->INDR>>((PIN)&31))&1 ) : \ (0)))) // =================================================================================== // Write PIN output value (0 = LOW / 1 = HIGH) // =================================================================================== #define PIN_write(PIN, val) (val)?(PIN_high(PIN)):(PIN_low(PIN)) // =================================================================================== // Setup PIN interrupt // =================================================================================== enum{PIN_INT_OFF, PIN_INT_RISING, PIN_INT_FALLING, PIN_INT_BOTH}; #define EXTICR1 EXTICR[0] #define EXTICR2 EXTICR[1] #define PIN_INT_set(PIN, TYPE) { \ ((PIN>=PA0 )&&(PIN<=PA15) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPAEN; \ AFIO->EXTICR1 &= ~((uint32_t)3<<(((PIN)&15)<<1)); }) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPAEN; \ AFIO->EXTICR2 &= ~((uint32_t)3<<(((PIN)&15)<<1)); }) : \ ((PIN>=PB0 )&&(PIN<=PB15) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPBEN; \ AFIO->EXTICR1 = (AFIO->EXTICR1 \ & ~((uint32_t)3<<(((PIN)&15)<<1))) \ | ((uint32_t)2<<(((PIN)&15)<<1)); }) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPBEN; \ AFIO->EXTICR2 = (AFIO->EXTICR2 \ & ~((uint32_t)3<<(((PIN)&15)<<1))) \ | ((uint32_t)2<<(((PIN)&15)<<1)); }) : \ ((PIN>=PC0 )&&(PIN<=PC15) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPCEN; \ AFIO->EXTICR1 |= ((uint32_t)3<<(((PIN)&15)<<1)); }) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPCEN; \ AFIO->EXTICR2 |= ((uint32_t)3<<(((PIN)&15)<<1)); }) : \ (0))))))); \ (TYPE & 3) ? (EXTI->INTENR |= (uint32_t)1<<((PIN)&31)) : \ (EXTI->INTENR &= ~((uint32_t)1<<((PIN)&31))); \ (TYPE & 1) ? (EXTI->RTENR |= (uint32_t)1<<((PIN)&31)) : \ (EXTI->RTENR &= ~((uint32_t)1<<((PIN)&31))); \ (TYPE & 2) ? (EXTI->FTENR |= (uint32_t)1<<((PIN)&31)) : \ (EXTI->FTENR &= ~((uint32_t)1<<((PIN)&31))); \ } #define PIN_INT_enable() {NVIC_EnableIRQ(EXTI7_0_IRQn); \ NVIC_EnableIRQ(EXTI15_8_IRQn); \ NVIC_EnableIRQ(EXTI25_16_IRQn); } #define PIN_INT_disable() {NVIC_DisableIRQ(EXTI7_0_IRQn); \ NVIC_DisableIRQ(EXTI15_8_IRQn); \ NVIC_DisableIRQ(EXTI25_16_IRQn);} #define PIN_INTFLAG_read(PIN) (EXTI->INTFR & ((uint32_t)1 << ((PIN) & 31))) #define PIN_INTFLAG_clear(PIN) EXTI->INTFR = ((uint32_t)1 << ((PIN) & 31)) #define PIN_INT_ISR \ void PIN_INT_IRQHandler(void) __attribute__((interrupt)); \ void EXTI7_0_IRQHandler(void) __attribute__((alias("PIN_INT_IRQHandler"))); \ void EXTI15_8_IRQHandler(void) __attribute__((alias("PIN_INT_IRQHandler"))); \ void EXTI25_16_IRQHandler(void) __attribute__((alias("PIN_INT_IRQHandler"))); \ void PIN_INT_IRQHandler(void) // =================================================================================== // Setup PIN event // =================================================================================== enum{PIN_EVT_OFF, PIN_EVT_RISING, PIN_EVT_FALLING, PIN_EVT_BOTH}; #define PIN_EVT_set(PIN, TYPE) { \ ((PIN>=PA0 )&&(PIN<=PA15) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPAEN; \ AFIO->EXTICR1 &= ~((uint32_t)3<<(((PIN)&15)<<1)); }) : \ ((PIN>=PA16)&&(PIN<=PA23) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPAEN; \ AFIO->EXTICR2 &= ~((uint32_t)3<<(((PIN)&15)<<1)); }) : \ ((PIN>=PB0 )&&(PIN<=PB15) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPBEN; \ AFIO->EXTICR1 = (AFIO->EXTICR1 \ & ~((uint32_t)3<<(((PIN)&15)<<1))) \ | ((uint32_t)2<<(((PIN)&15)<<1)); }) : \ ((PIN>=PB16)&&(PIN<=PB23) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPBEN; \ AFIO->EXTICR2 = (AFIO->EXTICR2 \ & ~((uint32_t)3<<(((PIN)&15)<<1))) \ | ((uint32_t)2<<(((PIN)&15)<<1)); }) : \ ((PIN>=PC0 )&&(PIN<=PC15) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPCEN; \ AFIO->EXTICR1 |= ((uint32_t)3<<(((PIN)&15)<<1)); }) : \ ((PIN>=PC16)&&(PIN<=PC23) ? ({RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPCEN; \ AFIO->EXTICR2 |= ((uint32_t)3<<(((PIN)&15)<<1)); }) : \ (0))))))); \ (TYPE & 3) ? (EXTI->EVENR |= (uint32_t)1<<((PIN)&31)) : \ (EXTI->EVENR &= ~((uint32_t)1<<((PIN)&31))); \ (TYPE & 1) ? (EXTI->RTENR |= (uint32_t)1<<((PIN)&31)) : \ (EXTI->RTENR &= ~((uint32_t)1<<((PIN)&31))); \ (TYPE & 2) ? (EXTI->FTENR |= (uint32_t)1<<((PIN)&31)) : \ (EXTI->FTENR &= ~((uint32_t)1<<((PIN)&31))); \ } // =================================================================================== // Enable GPIO PORTS // =================================================================================== #define PORTA_enable() RCC->APB2PCENR |= RCC_IOPAEN; #define PORTB_enable() RCC->APB2PCENR |= RCC_IOPBEN; #define PORTC_enable() RCC->APB2PCENR |= RCC_IOPCEN; #define PORTS_enable() RCC->APB2PCENR |= RCC_IOPAEN | RCC_IOPBEN | RCC_IOPCEN #define PORT_enable(PIN) \ ((PIN>=PA0)&&(PIN<=PA15) ? ( RCC->APB2PCENR |= RCC_IOPAEN ) : \ ((PIN>=PB0)&&(PIN<=PB15) ? ( RCC->APB2PCENR |= RCC_IOPBEN ) : \ ((PIN>=PC0)&&(PIN<=PC15) ? ( RCC->APB2PCENR |= RCC_IOPCEN ) : \ (0)))) // =================================================================================== // Disable GPIO PORTS // =================================================================================== #define PORTA_disable() RCC->APB2PCENR &= ~RCC_IOPAEN #define PORTB_disable() RCC->APB2PCENR &= ~RCC_IOPBEN #define PORTC_disable() RCC->APB2PCENR &= ~RCC_IOPCEN #define PORTS_disable() RCC->APB2PCENR &= ~(RCC_IOPAEN | RCC_IOPBEN | RCC_IOPCEN) #define PORT_disable(PIN) \ ((PIN>=PA0)&&(PIN<=PA15) ? ( RCC->APB2PCENR &= ~RCC_IOPAEN ) : \ ((PIN>=PB0)&&(PIN<=PB15) ? ( RCC->APB2PCENR &= ~RCC_IOPBEN ) : \ ((PIN>=PC0)&&(PIN<=PC15) ? ( RCC->APB2PCENR &= ~RCC_IOPCEN ) : \ (0)))) // =================================================================================== // Analog-to-Digital Converter (ADC) Functions // =================================================================================== #define ADC_enable() ADC1->CTLR2 |= ADC_ADON #define ADC_disable() ADC1->CTLR2 &= ~ADC_ADON #define ADC_fast() { ADC1->CTLR3 = 0b00000000000000000000000000000101; \ ADC1->SAMPTR1 = 0b00000000000000000000000000000000; \ ADC1->SAMPTR2 = 0b00000000000000000000000000000000; } #define ADC_slow() { ADC1->CTLR3 = 0b00000000000000000000000000001011; \ ADC1->SAMPTR1 = 0b00111111111111111111111111111111; \ ADC1->SAMPTR2 = 0b00111111111111111111111111111111; } #define ADC_medium() { ADC1->CTLR3 = 0b00000000000000000000000000001000; \ ADC1->SAMPTR1 = 0b00011011011011011011011011011011; \ ADC1->SAMPTR2 = 0b00011011011011011011011011011011; } #define ADC_input_VREF() ADC1->RSQR3 = 15 #define ADC_input(PIN) \ ((PIN>=PA0)&&(PIN<=PA7) ? (ADC1->RSQR3 = (PIN)&7) : \ ((PIN>=PB0)&&(PIN<=PB1) ? (ADC1->RSQR3 = ((PIN)&7)+8) : \ ((PIN>=PC0)&&(PIN<=PC3) ? (ADC1->RSQR3 = ((PIN)&7)+10) : \ (0)))) static inline void ADC_init(void) { RCC->APB2PCENR |= RCC_ADC1EN | RCC_AFIOEN; // enable ADC and AFIO ADC_slow(); // set slow speed as default ADC1->CTLR2 = ADC_ADON // turn on ADC | ADC_EXTSEL; // software triggering } static inline uint16_t ADC_read(void) { ADC_enable(); // make sure ADC is enabled ADC1->CTLR2 |= ADC_SWSTART; // start conversion while(!(ADC1->STATR & ADC_EOC)); // wait until finished return ADC1->RDATAR; // return result } static inline uint16_t ADC_read_VDD(void) { ADC_input_VREF(); // set VREF as ADC input return((uint32_t)1200 * 4095 / ADC_read()); // return VDD in mV } // =================================================================================== // Analog Comparator (CMP) Functions // =================================================================================== #define CMP_lock() OPA->CTLR2 |= OPA_CTLR2_CMP_LOCK #define CMP_unlock() {CMP->KEY = CMP_KEY1; CMP->KEY = CMP_KEY2;} #define CMP1_enable() OPA->CTLR2 |= OPA_CTLR2_EN1 #define CMP1_disable() OPA->CTLR2 &= ~OPA_CTLR2_EN1 #define CMP1_HYS_enable() OPA->CTLR2 |= OPA_CTLR2_HYEN1 #define CMP1_HYS_disable() OPA->CTLR2 &= ~OPA_CTLR2_HYEN1 #define CMP1_OUT_PA1() OPA->CTLR2 |= OPA_CTLR2_MODE1 #define CMP1_OUT_T2C1() OPA->CTLR2 &= ~OPA_CTLR2_MODE1 #define CMP1_NEG_PA23() OPA->CTLR2 |= OPA_CTLR2_NSEL1 #define CMP1_NEG_PC3() OPA->CTLR2 &= ~OPA_CTLR2_NSEL1 #define CMP1_POS_PA0() OPA->CTLR2 |= OPA_CTLR2_PSEL1 #define CMP1_POS_PC19() OPA->CTLR2 &= ~OPA_CTLR2_PSEL1 #define CMP2_enable() OPA->CTLR2 |= OPA_CTLR2_EN2 #define CMP2_disable() OPA->CTLR2 &= ~OPA_CTLR2_EN2 #define CMP2_HYS_enable() OPA->CTLR2 |= OPA_CTLR2_HYEN2 #define CMP2_HYS_disable() OPA->CTLR2 &= ~OPA_CTLR2_HYEN2 #define CMP2_OUT_PB2() OPA->CTLR2 |= OPA_CTLR2_MODE2 #define CMP2_OUT_T2C2() OPA->CTLR2 &= ~OPA_CTLR2_MODE2 #define CMP2_NEG_PC3() OPA->CTLR2 |= OPA_CTLR2_NSEL2 #define CMP2_NEG_PA22() OPA->CTLR2 &= ~OPA_CTLR2_NSEL2 #define CMP2_POS_PA11() OPA->CTLR2 |= OPA_CTLR2_PSEL2 #define CMP2_POS_PA12() OPA->CTLR2 &= ~OPA_CTLR2_PSEL2 #define CMP3_enable() OPA->CTLR2 |= OPA_CTLR2_EN3 #define CMP3_disable() OPA->CTLR2 &= ~OPA_CTLR2_EN3 #define CMP3_HYS_enable() OPA->CTLR2 |= OPA_CTLR2_HYEN3 #define CMP3_HYS_disable() OPA->CTLR2 &= ~OPA_CTLR2_HYEN3 #define CMP3_OUT_PB3() OPA->CTLR2 |= OPA_CTLR2_MODE3 #define CMP3_OUT_T2C3() OPA->CTLR2 &= ~OPA_CTLR2_MODE3 #define CMP3_NEG_PC3() OPA->CTLR2 |= OPA_CTLR2_NSEL3 #define CMP3_NEG_PA2() OPA->CTLR2 &= ~OPA_CTLR2_NSEL3 #define CMP3_POS_PA14() OPA->CTLR2 |= OPA_CTLR2_PSEL3 #define CMP3_POS_PA13() OPA->CTLR2 &= ~OPA_CTLR2_PSEL3 // =================================================================================== // Operational Amplifier (OPA) Functions // =================================================================================== // not yet implemented // =================================================================================== // Touch Key (TK) Functions // =================================================================================== #define TK_input(PIN) ADC_input(PIN) static inline void TK_init(void) { ADC_init(); // init ADC ADC1->CTLR1 |= ADC_TKENABLE; // enable touch key ADC1->IDATAR1 = 0x80; // TKEY1->CHGOFFSET = 0x80; } static inline uint8_t TK_read(void) { uint8_t result; uint16_t value; ADC_enable(); // (re-)enable ADC ADC1->RDATAR = 0x08; // (TKEY1->ACT_DCG) set discharge time and start while(!(ADC1->STATR & ADC_EOC)); // wait until sampling completed value = ADC1->RDATAR; // read sampling value result = (value == 2047); // 2047 if pressed ADC1->RDATAR = 0x08; // second sampling (blind) while(!(ADC1->STATR & ADC_EOC)); value = ADC1->RDATAR; return result; } #ifdef __cplusplus }; #endif ================================================ FILE: software/pd_tester/src/i2c_soft.c ================================================ // =================================================================================== // Software I2C Master Functions for CH32X035/X034/X033 * v1.1 * // =================================================================================== // // Simple I2C bitbanging. ACK bit of the slave is ignored. Clock stretching by the // slave is not allowed. External pull-up resistors (4k7 - 10k) are mandatory! // // Further information: https://github.com/wagiminator/ATtiny13-TinyOLEDdemo // 2023 by Stefan Wagner: https://github.com/wagiminator #include "i2c_soft.h" // =================================================================================== // I2C Delay // =================================================================================== #define I2C_DLY_TICKS_H (((F_CPU * 9) / (I2C_CLKRATE * 25)) - 41) #define I2C_DLY_TICKS_L (((F_CPU * 16) / (I2C_CLKRATE * 25)) - 76) #if I2C_DLY_TICKS_H >= 1 #define I2C_DELAY_H() DLY_ticks(I2C_DLY_TICKS_H) #else #define I2C_DELAY_H() #endif #if I2C_DLY_TICKS_L >= 1 #define I2C_DELAY_L() DLY_ticks(I2C_DLY_TICKS_L) #else #define I2C_DELAY_L() #endif // =================================================================================== // I2C Pin Macros // =================================================================================== #define I2C_SDA_HIGH() PIN_input(PIN_SDA) // release SDA -> pulled HIGH by resistor #define I2C_SDA_LOW() PIN_output(PIN_SDA) // SDA LOW -> pulled LOW by MCU #define I2C_SCL_HIGH() PIN_input(PIN_SCL) // release SCL -> pulled HIGH by resistor #define I2C_SCL_LOW() PIN_output(PIN_SCL) // SCL LOW -> pulled LOW by MCU #define I2C_SDA_READ() PIN_read(PIN_SDA) // read SDA pin #define I2C_CLOCKOUT() I2C_DELAY_L();I2C_SCL_HIGH();I2C_DELAY_H();I2C_SCL_LOW() // =================================================================================== // I2C Functions // =================================================================================== // I2C init function void I2C_init(void) { PIN_input(PIN_SCL); // release SCL PIN_input(PIN_SDA); // release SDA PIN_low(PIN_SCL); // preset for SCL low PIN_low(PIN_SDA); // preset for SDA low } // I2C transmit one data byte to the slave, ignore ACK bit, no clock stretching allowed void I2C_write(uint8_t data) { uint8_t i; for(i=8; i; i--, data<<=1) { // transmit 8 bits, MSB first (data & 0x80) ? (I2C_SDA_HIGH()) : (I2C_SDA_LOW()); // SDA HIGH if bit is 1 I2C_CLOCKOUT(); // clock out -> slave reads the bit } I2C_SDA_HIGH(); // release SDA for ACK bit of slave I2C_CLOCKOUT(); // 9th clock pulse is for the ignored ACK bit } // I2C start transmission void I2C_start(uint8_t addr) { I2C_SDA_LOW(); // start condition: SDA goes LOW first I2C_DELAY_H(); // delay I2C_SCL_LOW(); // start condition: SCL goes LOW second I2C_write(addr); // send slave address } // I2C restart transmission void I2C_restart(uint8_t addr) { I2C_SDA_HIGH(); // prepare SDA for HIGH to LOW transition I2C_DELAY_H(); // delay I2C_SCL_HIGH(); // restart condition: clock HIGH I2C_start(addr); // start again } // I2C stop transmission void I2C_stop(void) { I2C_SDA_LOW(); // prepare SDA for LOW to HIGH transition I2C_DELAY_H(); // delay I2C_SCL_HIGH(); // stop condition: SCL goes HIGH first I2C_DELAY_H(); // delay I2C_SDA_HIGH(); // stop condition: SDA goes HIGH second } // I2C receive one data byte from the slave (ack=0 for last byte, ack>0 if more bytes to follow) uint8_t I2C_read(uint8_t ack) { uint8_t i; uint8_t data = 0; // variable for the received byte I2C_SDA_HIGH(); // release SDA -> will be toggled by slave for(i=8; i; i--) { // receive 8 bits data <<= 1; // bits shifted in right (MSB first) I2C_DELAY_L(); // delay I2C_SCL_HIGH(); // clock HIGH I2C_DELAY_H(); // delay if(I2C_SDA_READ()) data |= 1; // read bit I2C_SCL_LOW(); // clock LOW -> slave prepares next bit } if(ack) I2C_SDA_LOW(); // pull SDA LOW to acknowledge (ACK) I2C_CLOCKOUT(); // clock out -> slave reads ACK bit return data; // return the received byte } // Send data buffer via I2C bus and stop void I2C_writeBuffer(uint8_t* buf, uint16_t len) { while(len--) I2C_write(*buf++); // write buffer I2C_stop(); // stop transmission } // Read data via I2C bus to buffer and stop void I2C_readBuffer(uint8_t* buf, uint16_t len) { while(len--) *buf++ = I2C_read(len > 0); I2C_stop(); } ================================================ FILE: software/pd_tester/src/i2c_soft.h ================================================ // =================================================================================== // Software I2C Master Functions for CH32X035/X034/X033 * v1.1 * // =================================================================================== // // Simple I2C bitbanging. ACK bit of the slave is ignored. Clock stretching by the // slave is not allowed. External pull-up resistors (4k7 - 10k) are mandatory! // // Functions available: // -------------------- // I2C_init() I2C init function // I2C_start(addr) I2C start transmission, address must contain R/W bit // I2C_restart(addr) I2C restart transmission, address must contain R/W bit // I2C_stop() I2C stop transmission // I2C_write(data) I2C transmit one data byte to the slave // I2C_read(ack) I2C receive one data byte (set ack=0 for last byte) // // I2C_writeBuffer(buf,len) Send buffer (*buf) with length (len) via I2C and stop // I2C_readBuffer(buf,len) Read buffer (*buf) with length (len) via I2C and stop // // Define SDA/SCL pin and clock rate below! // // Further information: https://github.com/wagiminator/ATtiny13-TinyOLEDdemo // 2023 by Stefan Wagner: https://github.com/wagiminator #pragma once #ifdef __cplusplus extern "C" { #endif #include "config.h" #include "system.h" #include "gpio.h" // I2C parameters #ifndef PIN_SDA #define PIN_SDA PA11 // pin connected to serial data of the I2C bus #define PIN_SCL PA10 // pin connected to serial clock of the I2C bus #endif #define I2C_CLKRATE 400000 // I2C bus clock rate in Hz // I2C Functions void I2C_init(void); // I2C init function void I2C_start(uint8_t addr); // I2C start transmission void I2C_restart(uint8_t addr); // I2C restart transmission void I2C_stop(void); // I2C stop transmission void I2C_write(uint8_t data); // I2C transmit one data byte to the slave uint8_t I2C_read(uint8_t ack); // I2C receive one data byte from the slave void I2C_writeBuffer(uint8_t* buf, uint16_t len); void I2C_readBuffer(uint8_t* buf, uint16_t len); #ifdef __cplusplus }; #endif ================================================ FILE: software/pd_tester/src/main.c ================================================ // =================================================================================== // Project: USB PD Tester for CH32X035 // Version: v1.3 // Year: 2024 // Author: Stefan Wagner // Github: https://github.com/wagiminator // EasyEDA: https://easyeda.com/wagiminator // License: http://creativecommons.org/licenses/by-sa/3.0/ // =================================================================================== // // Description: // ------------ // The USB PD Tester allows users to retrieve and test the capabilities of a connected // USB Power Delivery Adapter. // // References: // ----------- // - WCH Nanjing Qinheng Microelectronics: http://wch.cn // // Compilation Instructions: // ------------------------- // - Make sure GCC toolchain (gcc-riscv64-unknown-elf, newlib) and Python3 with chprog // are installed. In addition, Linux requires access rights to the USB bootloader. // - Press the BOOT button on the MCU board and keep it pressed while connecting it // via USB to your PC. // - Run 'make flash'. // =================================================================================== // Libraries, Definitions and Macros // =================================================================================== #include // user configurations #include // OLED text functions #include // USB PD sink functions // Global variables uint8_t select = 1; // selected PDO uint8_t active = 1; // active PDO uint16_t voltage = 5000; // selected voltage // =================================================================================== // Functions // =================================================================================== // Set selected PDO marker void setSelect(uint8_t pdo) { OLED_cursor(120, select - 1); OLED_write(' '); if(pdo > PD_getPDONum()) select = 1; else if(pdo < 1) select = PD_getPDONum(); else select = pdo; OLED_cursor(120, select - 1); OLED_write('<'); } // Set active PDO marker void setActive(uint8_t pdo) { OLED_cursor( 0, active - 1); OLED_write(' '); active = pdo; OLED_cursor( 0, active - 1); OLED_write('*'); } // Set selected voltage void setVoltage(uint16_t v) { if (v <= PD_getPDOMinVoltage(select)) voltage = PD_getPDOMinVoltage(select); else if(v >= PD_getPDOMaxVoltage(select)) voltage = PD_getPDOMaxVoltage(select); else voltage = v; OLED_cursor(30, 6); OLED_printf(">%6dmV <", voltage); } // Print source capabilities void printSourceCap(void) { uint8_t i; OLED_clear(); for(i = 1; i <= PD_getPDONum(); i++) { if(i <= PD_getFixedNum()) OLED_printf(" (%d)%6dmV %5dmA ", i, PD_getPDOVoltage(i), PD_getPDOMaxCurrent(i)); else OLED_printf(" [%d]%6dmV-%5dmV ", i, PD_getPDOMinVoltage(i), PD_getPDOMaxVoltage(i)); } setSelect(select); setActive(active); } // Print selected programmable power PDO infos void printPPS(void) { OLED_clear(); OLED_printf("Select voltage of [%d]\n", select); OLED_printf("min voltage:%7dmV", PD_getPDOMinVoltage(select)); OLED_printf("max voltage:%7dmV", PD_getPDOMaxVoltage(select)); OLED_printf("max current:%7dmA", PD_getPDOMaxCurrent(select)); setVoltage(voltage); } // =================================================================================== // Main Function // =================================================================================== int main(void) { // Variables uint8_t i; uint8_t keydelay; // Setup OLED and USB-PD #if DISABLE_SWJ RCC->APB2PCENR |= RCC_AFIOEN; // enable AFIO clock AFIO->PCFR1 |= AFIO_PCFR1_SWJ_CFG_2; // disable SWJ on pins PC18 and PC19 #endif OLED_init(); OLED_clear(); OLED_printf("Connecting..."); if(!PD_connect()) { OLED_printf("FAILED"); while(1); } // Print source capabilities printSourceCap(); // Setup button pins PIN_input_PU(PIN_KEY_UP); PIN_input_PU(PIN_KEY_DOWN); PIN_input_PU(PIN_KEY_SLCT); // Loop while(1) { if(!PIN_read(PIN_KEY_UP)) { setSelect(select - 1); while(!PIN_read(PIN_KEY_UP)); } if(!PIN_read(PIN_KEY_DOWN)) { setSelect(select + 1); while(!PIN_read(PIN_KEY_DOWN)); } if(!PIN_read(PIN_KEY_SLCT)) { if(select <= PD_getFixedNum()) { if(PD_setPDO(select, PD_getPDOVoltage(select))) setActive(select); } else { printPPS(); while(!PIN_read(PIN_KEY_SLCT)); DLY_ms(10); while(PIN_read(PIN_KEY_SLCT)) { if(!PIN_read(PIN_KEY_UP)) { setVoltage(voltage + 20); i = keydelay; while((i--) && (!PIN_read(PIN_KEY_UP))) DLY_ms(10); keydelay = 2; } else if(!PIN_read(PIN_KEY_DOWN)) { setVoltage(voltage - 20); i = keydelay; while((i--) && (!PIN_read(PIN_KEY_DOWN))) DLY_ms(10); keydelay = 2; } else keydelay = 50; PD_negotiate(); } if(PD_setPDO(select, voltage)) active = select; printSourceCap(); } while(!PIN_read(PIN_KEY_SLCT)); } PD_negotiate(); DLY_ms(10); } } ================================================ FILE: software/pd_tester/src/print.c ================================================ // =================================================================================== // Basic PRINT Functions * v1.1 * // =================================================================================== // 2023 by Stefan Wagner: https://github.com/wagiminator #include #include "print.h" // Print decimal value void printD(void (*putchar) (char c), uint32_t value) { uint8_t digitval; // current digit value uint8_t leadflag = 0; // flag for leading spaces uint32_t divider = 1000000000; // current divider while(divider) { // for all digits digitval = value / divider; // calculate digit value value = value % divider; // set value to division remainder divider /= 10; // calculate next divider if(digitval) leadflag++; // end of leading spaces if(!divider) leadflag++; // least digit has to be printed if(leadflag) putchar(digitval + '0'); // print the digit } } // Convert 4-bit byte nibble into hex character and print it via putchar void printN(void (*putchar) (char c), uint8_t nibble) { putchar((nibble <= 9) ? ('0' + nibble) : ('A' - 10 + nibble)); } // Convert 8-bit byte into hex characters and print it via putchar void printB(void (*putchar) (char c), uint8_t value) { printN(putchar, value >> 4); printN(putchar, value & 0x0f); } // Convert 16-bit half-word into hex characters and print it via putchar void printH(void (*putchar) (char c), uint16_t value) { printB(putchar, value >> 8); printB(putchar, value); } // Convert 32-bit word into hex characters and print it via putchar void printW(void (*putchar) (char c), uint32_t value) { printH(putchar, value >> 16); printH(putchar, value); } // Print string via putchar void printS(void (*putchar) (char c), const char* str) { while(*str) putchar(*str++); } // Print string with newline via putchar void println(void (*putchar) (char c), const char* str) { while(*str) putchar(*str++); putchar('\n'); } // printf, supports %s, %c, %d, %u, %x, %b, %02d, %% void _itoa(void (*putchar) (char c), int32_t, int8_t, int8_t); static void _vfprintf(void (*putchar) (char c), const char *format, va_list arg); void printF(void (*putchar) (char c), const char *format, ...) { va_list arg; va_start(arg, format); _vfprintf(putchar, format, arg); va_end(arg); } static void _vfprintf(void (*putchar) (char c), const char* str, va_list arp) { int32_t d, r, w, s; char *c; while((d = *str++) != 0) { if(d != '%') { putchar(d); continue; } d = *str++; w = r = s = 0; if(d == '%') { putchar(d); d = *str++; } if(d == '0') { d = *str++; s = 1; } while((d >= '0') && (d <= '9')) { w += w * 10 + (d - '0'); d = *str++; } if(s) w = -w; if(d == 's') { c = va_arg(arp, char*); while(*c) putchar(*(c++)); continue; } if(d == 'c') { putchar((char)va_arg(arp, int)); continue; } if(d =='\0') break; else if(d == 'u') r = 10; else if(d == 'd') r = -10; else if(d == 'x') r = 16; else if(d == 'b') r = 2; else str--; if(r == 0) continue; if(r > 0) _itoa(putchar, (uint32_t)va_arg(arp, int32_t), r, w); else _itoa(putchar, (int32_t)va_arg(arp, int32_t), r, w); } } void _itoa(void (*putchar) (char c), int32_t val, int8_t rad, int8_t len) { char c, sgn = 0, pad = ' '; char s[20]; uint8_t i = 0; if(rad < 0) { rad = -rad; if(val < 0) { val = -val; sgn = '-'; } } if(len < 0) { len = -len; pad = '0'; } if(len > 20) return; do { c = (char)((uint32_t)val % rad); if (c >= 10) c += ('A' - 10); else c += '0'; s[i++] = c; val = (uint32_t)val / rad; } while(val); if((sgn != 0) && (pad != '0')) s[i++] = sgn; while(i < len) s[i++] = pad; if((sgn != 0) && (pad == '0')) s[i++] = sgn; do putchar(s[--i]); while(i); } ================================================ FILE: software/pd_tester/src/print.h ================================================ // =================================================================================== // Basic PRINT Functions * v1.1 * // =================================================================================== // // Functions available: // -------------------- // printF(putchar, f, ...) Uses printf (supports %s, %c, %d, %u, %x, %b, %02d, %%) // printD(putchar, n) Print decimal value as string via putchar function // printW(putchar, n) Print 32-bit hex word value as string via putchar function // printH(putchar, n) Print 16-bit hex half-word value as string via putchar function // printB(putchar, n) Print 8-bit hex byte value as string via putchar function // printS(putchar, s) Print string via putchar function // println(putchar, s) Print string with newline via putchar function // // 2023 by Stefan Wagner: https://github.com/wagiminator #pragma once #ifdef __cplusplus extern "C" { #endif #include void printD(void (*putchar) (char c), uint32_t value); void printB(void (*putchar) (char c), uint8_t value); void printH(void (*putchar) (char c), uint16_t value); void printW(void (*putchar) (char c), uint32_t value); void printS(void (*putchar) (char c), const char* str); void println(void (*putchar) (char c), const char* str); void printF(void (*putchar) (char c), const char *format, ...); #ifdef __cplusplus }; #endif ================================================ FILE: software/pd_tester/src/ssd1306_txt.c ================================================ // =================================================================================== // SSD1306/SH1106/SH1107 I2C OLED Text Functions * v1.3 * // =================================================================================== // // Collection of the most necessary functions for controlling an SSD1306/SH1106 I2C // OLED for the display of simple text. // // References: // ----------- // - Neven Boyanov: https://github.com/tinusaur/ssd1306xled // - Stephen Denne: https://github.com/datacute/Tiny4kOLED // - David Johnson-Davies: http://www.technoblogy.com/show?TV4 // - TinyOLEDdemo: https://github.com/wagiminator/attiny13-tinyoleddemo // - TinyTerminal: https://github.com/wagiminator/ATtiny85-TinyTerminal // - OLED Font Editor: http://sourpuss.net/projects/fontedit/ // // 2022 by Stefan Wagner: https://github.com/wagiminator #include "ssd1306_txt.h" // =================================================================================== // Standard ASCII 5x8 Font (chars 32 - 127) // =================================================================================== const uint8_t OLED_FONT[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x5F, 0x00, 0x00, 0x00, 0x07, 0x00, 0x07, 0x00, 0x14, 0x7F, 0x14, 0x7F, 0x14, 0x24, 0x2A, 0x7F, 0x2A, 0x12, 0x23, 0x13, 0x08, 0x64, 0x62, 0x36, 0x49, 0x55, 0x22, 0x50, 0x00, 0x04, 0x03, 0x00, 0x00, 0x00, 0x1C, 0x22, 0x41, 0x00, 0x00, 0x41, 0x22, 0x1C, 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14, 0x08, 0x08, 0x3E, 0x08, 0x08, 0x00, 0x80, 0x60, 0x00, 0x00, 0x08, 0x08, 0x08, 0x08, 0x08, 0x00, 0x60, 0x60, 0x00, 0x00, 0x20, 0x10, 0x08, 0x04, 0x02, 0x3E, 0x51, 0x49, 0x45, 0x3E, 0x44, 0x42, 0x7F, 0x40, 0x40, 0x42, 0x61, 0x51, 0x49, 0x46, 0x22, 0x41, 0x49, 0x49, 0x36, 0x18, 0x14, 0x12, 0x7F, 0x10, 0x2F, 0x49, 0x49, 0x49, 0x31, 0x3E, 0x49, 0x49, 0x49, 0x32, 0x03, 0x01, 0x71, 0x09, 0x07, 0x36, 0x49, 0x49, 0x49, 0x36, 0x26, 0x49, 0x49, 0x49, 0x3E, 0x00, 0x36, 0x36, 0x00, 0x00, 0x00, 0x80, 0x68, 0x00, 0x00, 0x00, 0x08, 0x14, 0x22, 0x00, 0x14, 0x14, 0x14, 0x14, 0x14, 0x00, 0x22, 0x14, 0x08, 0x00, 0x02, 0x01, 0x51, 0x09, 0x06, 0x3E, 0x41, 0x5D, 0x55, 0x5E, 0x7C, 0x12, 0x11, 0x12, 0x7C, 0x7F, 0x49, 0x49, 0x49, 0x36, 0x3E, 0x41, 0x41, 0x41, 0x22, 0x7F, 0x41, 0x41, 0x22, 0x1C, 0x7F, 0x49, 0x49, 0x49, 0x41, 0x7F, 0x09, 0x09, 0x09, 0x01, 0x3E, 0x41, 0x49, 0x49, 0x3A, 0x7F, 0x08, 0x08, 0x08, 0x7F, 0x41, 0x41, 0x7F, 0x41, 0x41, 0x20, 0x40, 0x41, 0x3F, 0x01, 0x7F, 0x08, 0x14, 0x22, 0x41, 0x7F, 0x40, 0x40, 0x40, 0x40, 0x7F, 0x02, 0x0C, 0x02, 0x7F, 0x7F, 0x04, 0x08, 0x10, 0x7F, 0x3E, 0x41, 0x41, 0x41, 0x3E, 0x7F, 0x09, 0x09, 0x09, 0x06, 0x3E, 0x41, 0x41, 0xC1, 0xBE, 0x7F, 0x09, 0x19, 0x29, 0x46, 0x26, 0x49, 0x49, 0x49, 0x32, 0x01, 0x01, 0x7F, 0x01, 0x01, 0x3F, 0x40, 0x40, 0x40, 0x3F, 0x1F, 0x20, 0x40, 0x20, 0x1F, 0x3F, 0x40, 0x38, 0x40, 0x3F, 0x63, 0x14, 0x08, 0x14, 0x63, 0x07, 0x08, 0x70, 0x08, 0x07, 0x61, 0x51, 0x49, 0x45, 0x43, 0x00, 0x7F, 0x41, 0x41, 0x00, 0x02, 0x04, 0x08, 0x10, 0x20, 0x00, 0x41, 0x41, 0x7F, 0x00, 0x08, 0x04, 0x02, 0x04, 0x08, 0x40, 0x40, 0x40, 0x40, 0x40, 0x00, 0x06, 0x09, 0x09, 0x06, 0x20, 0x54, 0x54, 0x54, 0x78, 0x7F, 0x44, 0x44, 0x44, 0x38, 0x38, 0x44, 0x44, 0x44, 0x28, 0x38, 0x44, 0x44, 0x44, 0x7F, 0x38, 0x54, 0x54, 0x54, 0x18, 0x08, 0xFE, 0x09, 0x01, 0x02, 0x18, 0xA4, 0xA4, 0xA4, 0x78, 0x7F, 0x04, 0x04, 0x04, 0x78, 0x00, 0x44, 0x7D, 0x40, 0x00, 0x00, 0x80, 0x84, 0x7D, 0x00, 0x41, 0x7F, 0x10, 0x28, 0x44, 0x00, 0x41, 0x7F, 0x40, 0x00, 0x7C, 0x04, 0x7C, 0x04, 0x78, 0x7C, 0x04, 0x04, 0x04, 0x78, 0x38, 0x44, 0x44, 0x44, 0x38, 0xFC, 0x24, 0x24, 0x24, 0x18, 0x18, 0x24, 0x24, 0x24, 0xFC, 0x7C, 0x08, 0x04, 0x04, 0x08, 0x08, 0x54, 0x54, 0x54, 0x20, 0x04, 0x3F, 0x44, 0x40, 0x20, 0x3C, 0x40, 0x40, 0x40, 0x3C, 0x1C, 0x20, 0x40, 0x20, 0x1C, 0x3C, 0x40, 0x30, 0x40, 0x3C, 0x44, 0x28, 0x10, 0x28, 0x44, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C, 0x44, 0x64, 0x54, 0x4C, 0x44, 0x08, 0x08, 0x36, 0x41, 0x41, 0x00, 0x00, 0xFF, 0x00, 0x00, 0x41, 0x41, 0x36, 0x08, 0x08, 0x08, 0x04, 0x08, 0x10, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // =================================================================================== // 13x32 7-Segment Font (0 - 9) // =================================================================================== #if OLED_SEG_FONT == 1 const uint8_t OLED_FONT_SEG[] = { 0xFC, 0xF9, 0xF3, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0xF3, 0xF9, 0xFC, // 0 0x7F, 0x3F, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x3F, 0x7F, 0xFF, 0xFE, 0xFC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFE, 0xFF, 0x1F, 0x4F, 0x67, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x67, 0x4F, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xF8, 0xFC, // 1 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x3F, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F, 0x1F, 0x00, 0x01, 0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0xF3, 0xF9, 0xFC, // 2 0x00, 0x00, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x9F, 0x3F, 0x7F, 0xFF, 0xFE, 0xFC, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x1F, 0x4F, 0x67, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x60, 0x40, 0x00, 0x00, 0x01, 0x03, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0xF3, 0xF9, 0xFC, // 3 0x00, 0x00, 0x80, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x9F, 0x3F, 0x7F, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFC, 0xFE, 0xFF, 0x00, 0x40, 0x60, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x67, 0x4F, 0x1F, 0xFC, 0xF8, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0xF8, 0xFC, // 4 0x7F, 0x3F, 0x9F, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x9F, 0x3F, 0x7F, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFC, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F, 0x1F, 0xFC, 0xF9, 0xF3, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x03, 0x01, 0x00, // 5 0x7F, 0x3F, 0x9F, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFC, 0xFE, 0xFF, 0x00, 0x40, 0x60, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x67, 0x4F, 0x1F, 0xFC, 0xF9, 0xF3, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x03, 0x01, 0x00, // 6 0x7F, 0x3F, 0x9F, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x80, 0x00, 0x00, 0xFF, 0xFE, 0xFC, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFC, 0xFE, 0xFF, 0x1F, 0x4F, 0x67, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x67, 0x4F, 0x1F, 0xFC, 0xF9, 0xF3, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0xF3, 0xF9, 0xFC, // 7 0x7F, 0x3F, 0x1F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x3F, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFC, 0xFE, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0x0F, 0x1F, 0xFC, 0xF9, 0xF3, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0xF3, 0xF9, 0xFC, // 8 0x7F, 0x3F, 0x9F, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x9F, 0x3F, 0x7F, 0xFF, 0xFE, 0xFC, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFC, 0xFE, 0xFF, 0x1F, 0x4F, 0x67, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x67, 0x4F, 0x1F, 0xFC, 0xF9, 0xF3, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0xF3, 0xF9, 0xFC, // 9 0x7F, 0x3F, 0x9F, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0xC0, 0x9F, 0x3F, 0x7F, 0x00, 0x00, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFC, 0xFE, 0xFF, 0x00, 0x40, 0x60, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x70, 0x67, 0x4F, 0x1F }; const uint8_t OLED_FONT_POINT[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x70, 0x70, 0x70 }; #endif // =================================================================================== // 5x16 7-Segment Font (0 - 9) // =================================================================================== #if OLED_SEG_FONT == 2 const uint8_t OLED_FONT_SEG[] = { 0x7C, 0x02, 0x02, 0x02, 0x7C, 0x1F, 0x20, 0x20, 0x20, 0x1F, // 0 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x1F, // 1 0x00, 0x82, 0x82, 0x82, 0x7C, 0x1F, 0x20, 0x20, 0x20, 0x00, // 2 0x00, 0x82, 0x82, 0x82, 0x7C, 0x00, 0x20, 0x20, 0x20, 0x1F, // 3 0x7C, 0x80, 0x80, 0x80, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x1F, // 4 0x7C, 0x82, 0x82, 0x82, 0x00, 0x00, 0x20, 0x20, 0x20, 0x1F, // 5 0x7C, 0x82, 0x82, 0x82, 0x00, 0x1F, 0x20, 0x20, 0x20, 0x1F, // 6 0x7C, 0x02, 0x02, 0x02, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x1F, // 7 0x7C, 0x82, 0x82, 0x82, 0x7C, 0x1F, 0x20, 0x20, 0x20, 0x1F, // 8 0x7C, 0x82, 0x82, 0x82, 0x7C, 0x00, 0x20, 0x20, 0x20, 0x1F // 9 }; const uint8_t OLED_FONT_POINT[] = { 0x00, 0x00, 0x30, 0x30 }; #endif // =================================================================================== // OLED Control Functions // =================================================================================== // Screen offsets #if OLED_SH1106 == 1 #define OLED_XOFF ((128 - OLED_WIDTH) / 2) + 2 #else #define OLED_XOFF ((128 - OLED_WIDTH) / 2) #endif // OLED initialisation sequence const uint8_t OLED_INIT_CMD[] = { OLED_MULTIPLEX, OLED_HEIGHT - 1, // set multiplex ratio OLED_CHARGEPUMP, 0x14, // set DC-DC enable OLED_MEMORYMODE, 0x00, // set horizontal addressing mode #if OLED_WIDTH == 128 && OLED_HEIGHT == 32 OLED_COMPINS, 0x02, // set com pins #else OLED_COMPINS, 0x12, // set com pins #endif #if OLED_XFLIP > 0 OLED_XFLIP_ON, // flip screen in X-direction #endif #if OLED_YFLIP > 0 OLED_YFLIP_ON, // flip screen in Y-direction #endif #if OLED_INVERT > 0 OLED_INVERT_ON, // invert screen #endif OLED_DISPLAY_ON // display on }; // OLED init function void OLED_init(void) { #if OLED_INIT_I2C > 0 I2C_init(); // initialize I2C first #endif #if OLED_BOOT_TIME > 0 DLY_ms(OLED_BOOT_TIME); // time for the OLED to boot up #endif I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_CMD_MODE); // set command mode I2C_writeBuffer((uint8_t*)OLED_INIT_CMD, sizeof(OLED_INIT_CMD)); // send the command bytes } // Switch display on/off (0: display off, 1: display on) void OLED_display(uint8_t val) { I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_CMD_MODE); // set command mode I2C_write(val ? OLED_DISPLAY_ON : OLED_DISPLAY_OFF); // set display power I2C_stop(); // stop transmission } // Set display contrast (0-255) void OLED_contrast(uint8_t val) { I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_CMD_MODE); // set command mode I2C_write(OLED_CONTRAST); // contrast command I2C_write(val); // set contrast value I2C_stop(); // stop transmission } // Invert display (0: inverse off, 1: inverse on) void OLED_invert(uint8_t val) { I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_CMD_MODE); // set command mode I2C_write(val ? OLED_INVERT_ON : OLED_INVERT_OFF); // set invert mode I2C_stop(); // stop transmission } // Flip display (0: flip off, 1: flip on) void OLED_flip(uint8_t xflip, uint8_t yflip) { I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_CMD_MODE); // set command mode I2C_write(xflip ? OLED_XFLIP_ON : OLED_XFLIP_OFF); // set x-flip I2C_write(yflip ? OLED_YFLIP_ON : OLED_YFLIP_OFF); // set y-flip I2C_stop(); // stop transmission } // Scroll display vertically void OLED_vscroll(uint8_t y) { I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_CMD_MODE); // set command mode I2C_write(OLED_OFFSET); // offset command I2C_write(y); // set y-scroll I2C_stop(); // stop transmission } // =================================================================================== // OLED Text Functions // =================================================================================== // OLED global variables uint8_t OLED_x, OLED_y, OLED_i; // OLED clear line void OLED_clearLine(uint8_t y) { uint8_t i; OLED_cursor(0, y); // set cursor to line start I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_DAT_MODE); // set data mode for(i=OLED_WIDTH; i; i--) I2C_write(0x00); // clear line I2C_stop(); // stop transmission OLED_cursor(0, y); // re-set cursor to line start } // OLED clear screen void OLED_clear(void) { uint8_t y = OLED_HEIGHT / 8; while(y--) OLED_clearLine(y); // clear all lines } // OLED set cursor to specified position void OLED_cursor(uint8_t x, uint8_t y) { if(y >= OLED_HEIGHT / 8) y = 0; // limit y OLED_x = x; OLED_y = y; // set cursor variables x += OLED_XOFF; // add offset I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_CMD_MODE); // set command mode I2C_write(OLED_PAGE + y); // set line I2C_write(x & 0xf); // set column I2C_write((x >> 4) | 0x10); I2C_stop(); // stop transmission } // OLED set text invert void OLED_textinvert(uint8_t yes) { OLED_i = yes; } #if OLED_BIGCHARS > 0 // Character buffer uint8_t OLED_buf[2*10]; uint8_t OLED_sz; // Converts bit pattern abcdefgh into aabbccddeeffgghh uint16_t OLED_stretch(uint16_t x) { x = (x & 0xF0)<<4 | (x & 0x0F); x = (x<<2 | x) & 0x3333; x = (x<<1 | x) & 0x5555; return x | x<<1; } // Set character size void OLED_textsize(uint8_t size) { OLED_sz = size; } #endif // OLED_BIGCHARS > 0 // OLED plot a single character void OLED_plotChar(char c) { uint16_t ptr = c - 32; // character pointer ptr += ptr << 2; // -> ptr = (ch - 32) * 5; #if OLED_BIGCHARS > 0 if(OLED_sz == 0) { // normal character (5x8) #endif if(OLED_x > OLED_WIDTH - 6) OLED_cursor(0, OLED_y + 1); I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_DAT_MODE); // set data mode I2C_write(OLED_i ? 0xff : 0x00); // write space between characters for(uint8_t i=5; i; i--) I2C_write(OLED_i ? ~OLED_FONT[ptr++] : OLED_FONT[ptr++]); I2C_stop(); OLED_x += 6; // move cursor #if OLED_BIGCHARS > 0 } else if(OLED_sz == 1) { // v-stretched character (5x16) if(OLED_x > OLED_WIDTH - 6) OLED_cursor(0, OLED_y + 2); for(uint8_t i=0; i<5; i++) { uint16_t ch = OLED_stretch(OLED_FONT[ptr++]); OLED_buf[i] = ch; OLED_buf[i+5] = ch >> 8; } OLED_drawBitmap(OLED_buf, 5, 2); OLED_clearRect(1, 2); } else { // double-sized smoothed character (10x16) uint16_t col0L, col0R, col1L, col1R; // David Johnson-Davies' Smooth Big Text algorithm uint8_t col0 = OLED_FONT[ptr++]; if(OLED_x > OLED_WIDTH - 12) OLED_cursor(0, OLED_y + 2); col0L = OLED_stretch(col0); col0R = col0L; for(uint8_t col=0; col<10; col+=2) { uint8_t col1 = OLED_FONT[ptr++]; if(col == 8) col1 = 0; col1L = OLED_stretch(col1); col1R = col1L; for(int8_t i=6; i>=0; i--) { for(int8_t j=1; j<3; j++) { if(((col0>>i & 0b11) == (3 - j)) && ((col1>>i & 0b11) == j)) { col0R = col0R | 1<<((i << 1) + j); col1L = col1L | 1<<((i << 1) + 3 - j); } } } OLED_buf[col] = col0L; OLED_buf[col + 1] = col0R; OLED_buf[col + 10] = col0L >> 8; OLED_buf[col + 11] = col0R >> 8; col0 = col1; col0L = col1L; col0R = col1R; } OLED_drawBitmap(OLED_buf, 10, 2); OLED_clearRect(2, 2); } #endif } // OLED write a character or handle control characters void OLED_write(char c) { c &= 0x7f; // ignore top bit if(c >= 32) OLED_plotChar(c); // normal character #if OLED_BIGCHARS > 0 else if(c == '\n') OLED_cursor(0, OLED_y + (OLED_sz ? 2 : 1)); #else else if(c == '\n') OLED_cursor(0, OLED_y + 1); // new line #endif else if(c == '\r') OLED_cursor(0, OLED_y); // carriage return } // OLED print a string void OLED_print(char* str) { while(*str) OLED_write(*str++); } // =================================================================================== // OLED Bitmap Functions // =================================================================================== // Draw bitmap (pointer *bmp) at cursor position width (w) in pixels, hight (h) in 8-pixel lines void OLED_drawBitmap(const uint8_t* bmp, uint8_t w, uint8_t h) { uint8_t y = OLED_y; while(h--) { I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_DAT_MODE); // set data mode for(uint8_t i=w; i; i--) I2C_write(OLED_i ? ~(*bmp++) : *bmp++); I2C_stop(); OLED_cursor(OLED_x, OLED_y + 1); // set next line } OLED_cursor(OLED_x + w, y); // move cursor } // =================================================================================== // OLED 7-Segment Functions // =================================================================================== // Clear a rectangle starting from cursor position void OLED_clearRect(uint8_t w, uint8_t h) { uint8_t y = OLED_y; while(h--) { I2C_start(OLED_ADDR << 1); // start transmission to OLED I2C_write(OLED_DAT_MODE); // set data mode for(uint8_t i=w; i; i--) I2C_write(OLED_i ? 0xff : 0x00); // clear line I2C_stop(); // stop transmission OLED_cursor(OLED_x, OLED_y + 1); // set next line } OLED_cursor(OLED_x + w, y); // move cursor } // Print value as 7-segment digits (BCD conversion by substraction method) void OLED_printSegment(uint16_t value, uint8_t digits, uint8_t lead, uint8_t decimal) { static const uint16_t DIVIDER[] = {1, 10, 100, 1000, 10000}; uint8_t leadflag = 0; // flag for leading spaces while(digits--) { // for all digits digits uint8_t digitval = 0; // start with digit value 0 uint16_t divider = DIVIDER[digits]; // read current divider while(value >= divider) { // if current divider fits into the value leadflag = 1; // end of leading spaces digitval++; // increase digit value value -= divider; // decrease value by divider } if(digits == decimal) leadflag++; // end leading characters before decimal if(leadflag || !lead) { #if OLED_SEG_FONT == 0 OLED_write(digitval + '0'); #elif OLED_SEG_FONT == 1 uint16_t ptr = (uint16_t)digitval; // character pointer ptr = (ptr << 5) + (ptr << 4) + (ptr << 2); // -> ptr = c * 13 * 4; OLED_drawBitmap((uint8_t*)&OLED_FONT_SEG[ptr], 13, 4); #elif OLED_SEG_FONT == 2 uint16_t ptr = (uint16_t)digitval; // character pointer ptr = (ptr << 3) + (ptr << 1); // -> ptr = c * 5 * 2; OLED_drawBitmap((uint8_t*)&OLED_FONT_SEG[ptr], 5, 2); #endif } else { #if OLED_SEG_FONT == 0 OLED_write(' '); #elif OLED_SEG_FONT == 1 OLED_clearRect(13, 4); #elif OLED_SEG_FONT == 2 OLED_clearRect( 5, 2); #endif } #if OLED_SEG_FONT == 1 OLED_clearRect(OLED_SEG_SPACE, 4); #elif OLED_SEG_FONT == 2 OLED_clearRect(OLED_SEG_SPACE, 2); #endif if(decimal && (digits == decimal)) { #if OLED_SEG_FONT == 0 OLED_write('.'); #elif OLED_SEG_FONT == 1 OLED_drawBitmap(OLED_FONT_POINT, 3, 4); OLED_clearRect(OLED_SEG_SPACE, 4); #elif OLED_SEG_FONT == 2 OLED_drawBitmap(OLED_FONT_POINT, 2, 2); OLED_clearRect(OLED_SEG_SPACE, 2); #endif } } } ================================================ FILE: software/pd_tester/src/ssd1306_txt.h ================================================ // =================================================================================== // SSD1306/SH1106/SH1107 I2C OLED Text Functions * v1.3 * // =================================================================================== // // Collection of the most necessary functions for controlling an SSD1306/SH1106 I2C // OLED for the display of simple text, working without a screen buffer. // // Functions available: // -------------------- // OLED_init() Init OLED display // OLED_display(v) Switch display on/off (0: display off, 1: display on) // OLED_contrast(v) Set OLED contrast (0-255) // OLED_invert(v) Invert display (0: inverse off, 1: inverse on) // OLED_flip(xflip,yflip) Flip display (0: flip off, 1: flip on) // OLED_vscroll(y) Scroll display vertically (0-64) // OLED_clear() Clear screen of OLED display // OLED_clearLine(y) Clear line y // // OLED_cursor(x,y) Set text cursor at position (x,y) // OLED_textsize(sz) Set text size (0: 5x8, 1: 5x16, 2: 10x16), enable OLED_BIGCHARS // OLED_textinvert(v) Invert text (0: inverse off, 1: inverse on) // OLED_write(c) Write character at cursor position or handle control characters // OLED_print(str) Print string (*str) at cursor position // OLED_printSegment(v,d,l,dp) Print value (v) at cursor position using defined segment font // with (d) number of digits, (l) leading (0: '0', 1: space) and // decimal point at position (dp) counted from the right // OLED_drawBitmap(bmp,w,h) Draw bitmap (pointer *bmp) at cursor position // width (w) in pixels, hight (h) in 8-pixel lines // // If print functions are activated (see below, print.h must be included): // ----------------------------------------------------------------------- // OLED_printf(f, ...) printf (supports %s, %c, %d, %u, %x, %b, %02d, %%) // OLED_printD(n) Print decimal value // OLED_printW(n) Print 32-bit hex word value // OLED_printH(n) Print 16-bit hex half-word value // OLED_printB(n) Print 8-bit hex byte value // OLED_printS(s) Print string // OLED_println(s) Print string with newline // OLED_newline() Send newline // // Tested devices: // --------------- // - 1.5" 128x128 SH1107 // - 1.3" 128x64 SH1106 // - 0.96" 128x64 SSD1306 // - 0.91" 128x32 SSD1306 // - 0.49" 64x32 SSD1306 // - 0.42" 72x40 SSD1306 // // References: // ----------- // - Neven Boyanov: https://github.com/tinusaur/ssd1306xled // - Stephen Denne: https://github.com/datacute/Tiny4kOLED // - David Johnson-Davies: http://www.technoblogy.com/show?TV4 // - TinyOLEDdemo: https://github.com/wagiminator/attiny13-tinyoleddemo // - TinyTerminal: https://github.com/wagiminator/ATtiny85-TinyTerminal // - OLED Font Editor: http://sourpuss.net/projects/fontedit/ // // 2022 by Stefan Wagner: https://github.com/wagiminator #pragma once #ifdef __cplusplus extern "C" { #endif #include "i2c_soft.h" // choose your I2C library #include "system.h" // OLED Parameters #define OLED_ADDR 0x3C // OLED I2C device address #define OLED_WIDTH 128 // OLED width in pixels #define OLED_HEIGHT 64 // OLED height in pixels #define OLED_SH1106 0 // OLED driver - 0: SSD1306/SH1107, 1: SH1106 #define OLED_BOOT_TIME 50 // OLED boot up time in milliseconds #define OLED_INIT_I2C 1 // 1: init I2C with OLED_init() #define OLED_XFLIP 1 // 1: flip screen in X-direction with OLED_init() #define OLED_YFLIP 1 // 1: flip screen in Y-direction with OLED_init() #define OLED_INVERT 0 // 1: invert screen with OLED_init() // OLED Text Settings #define OLED_PRINT 1 // 1: include print functions (needs print.h) #define OLED_BIGCHARS 0 // 1: use big fonts (OLED_textsize()) #define OLED_SEG_FONT 0 // 0: standard font, 1: 13x32 digits, 2: 5x16 digits #define OLED_SEG_SPACE 3 // width of space between segment digits in pixels // OLED Modes #define OLED_CMD_MODE 0x00 // set command mode #define OLED_DAT_MODE 0x40 // set data mode #define OLED_CMD_ONCE 0x80 // send one command byte #define OLED_DAT_ONCE 0xC0 // send one data byte // OLED Commands #define OLED_COLUMN_LOW 0x00 // set lower 4 bits of start column (0x00 - 0x0F) #define OLED_COLUMN_HIGH 0x10 // set higher 4 bits of start column (0x10 - 0x1F) #define OLED_MEMORYMODE 0x20 // set memory addressing mode (following byte) #define OLED_COLUMNS 0x21 // set start and end column (following 2 bytes) #define OLED_PAGES 0x22 // set start and end page (following 2 bytes) #define OLED_STARTLINE 0x40 // set display start line (0x40-0x7F = 0-63) #define OLED_CONTRAST 0x81 // set display contrast (following byte, 0-255) #define OLED_CHARGEPUMP 0x8D // (following byte - 0x14:enable, 0x10: disable) #define OLED_XFLIP_OFF 0xA0 // don't flip display horizontally #define OLED_XFLIP_ON 0xA1 // flip display horizontally #define OLED_RESUME 0xA4 // display all on resume #define OLED_ALL_ON 0xA5 // display all on #define OLED_INVERT_OFF 0xA6 // set non-inverted display #define OLED_INVERT_ON 0xA7 // set inverse display #define OLED_MULTIPLEX 0xA8 // set multiplex ratio (following byte) #define OLED_DISPLAY_OFF 0xAE // set display off (sleep mode) #define OLED_DISPLAY_ON 0xAF // set display on #define OLED_PAGE 0xB0 // set start page (0xB0-0xB7 = 0-7) #define OLED_YFLIP_OFF 0xC0 // don't flip display vertically #define OLED_YFLIP_ON 0xC8 // flip display vertically #define OLED_OFFSET 0xD3 // set display offset (y-scroll: following byte) #define OLED_CLOCK 0xD5 // set frequency (bits 7-4) and divider (bits 3-0) #define OLED_PRECHARGE 0xD9 // set pre-charge period (following byte) #define OLED_COMPINS 0xDA // set COM pin config (following byte) #define OLED_VCOM_DETECT 0xDB // set VCOM detect (following byte) // OLED Control Functions void OLED_init(void); // OLED init function void OLED_display(uint8_t val); // Switch display on/off (0: display off, 1: display on) void OLED_contrast(uint8_t val); // Set display contrast (0-255) void OLED_invert(uint8_t val); // Invert display (0: inverse off, 1: inverse on) void OLED_flip(uint8_t xflip, uint8_t yflip); // Flip display (0: flip off, 1: flip on) void OLED_vscroll(uint8_t y); // Scroll display vertically (0-64) // OLED Text Functions void OLED_clear(void); // Clear screen void OLED_clearLine(uint8_t y); // Clear line y void OLED_write(char c); // Write a character or handle control characters void OLED_print(char* str); // Print a string void OLED_cursor(uint8_t x, uint8_t y); // Set cursor void OLED_textinvert(uint8_t yes); // Invert text #if OLED_BIGCHARS > 0 void OLED_textsize(uint8_t size); // Set text size (0: 5x8, 1: 5x16, 2: 10x16) #endif // OLED Special Functions void OLED_drawBitmap(const uint8_t* bmp, uint8_t w, uint8_t h); void OLED_clearRect(uint8_t w, uint8_t h); void OLED_printSegment(uint16_t value, uint8_t digits, uint8_t lead, uint8_t decimal); #define OLED_textcolor(c) OLED_textinvert(!(c)) // OLED Cursor Position extern uint8_t OLED_x, OLED_y; // Additional print functions (if activated, see above) #if OLED_PRINT == 1 #include "print.h" #define OLED_printD(n) printD(OLED_write, n) // print decimal as string #define OLED_printW(n) printW(OLED_write, n) // print word as string #define OLED_printH(n) printH(OLED_write, n) // print half-word as string #define OLED_printB(n) printB(OLED_write, n) // print byte as string #define OLED_printS(s) printS(OLED_write, s) // print string #define OLED_println(s) println(OLED_write, s) // print string with newline #define OLED_newline() OLED_write('\n') // send newline #define OLED_printf(f, ...) printF(OLED_write, f, ##__VA_ARGS__) #endif #ifdef __cplusplus }; #endif ================================================ FILE: software/pd_tester/src/system.c ================================================ // =================================================================================== // Basic System Functions for CH32X035/X034/X033 * v1.1 * // =================================================================================== // // This file must be included!!!! // // References: // ----------- // - WCH Nanjing Qinheng Microelectronics: http://wch.cn // // 2023 by Stefan Wagner: https://github.com/wagiminator #include "system.h" // =================================================================================== // Setup Microcontroller (this function is called automatically at startup) // =================================================================================== void SYS_init(void) { // Init system clock #if SYS_CLK_INIT > 0 #if F_CPU <= 12000000 //FLASH->ACTLR = FLASH_ACTLR_LATENCY_0; #elif F_CPU <= 24000000 FLASH->ACTLR = FLASH_ACTLR_LATENCY_1; #else FLASH->ACTLR = FLASH_ACTLR_LATENCY_2; #endif RCC->CFGR0 = SYS_CLK_DIV; #endif // Init SYSTICK #if SYS_TICK_INIT > 0 STK_init(); #endif // Enable GPIO #if SYS_GPIO_EN > 0 RCC->APB2PCENR = RCC_IOPAEN | RCC_IOPBEN | RCC_IOPCEN; #endif } // =================================================================================== // System Clock Functions // =================================================================================== // Reset system clock to default state void CLK_reset(void) { RCC->CTLR |= (uint32_t)0x00000001; RCC->CFGR0 = (uint32_t)0x00000050; FLASH->ACTLR = (uint32_t)0x00000000; } // Setup pin PB9 for MCO (output, push-pull, alternate) void MCO_init(void) { RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPBEN; GPIOB->CFGHR = (GPIOB->CFGHR & ~((uint32_t)0b1111<<(1<<2))) | ((uint32_t)0b1011<<(1<<2)); RCC->CFGR0 |= RCC_CFGR0_MCO_SYSCLK; } // =================================================================================== // Delay Functions // =================================================================================== // Wait n counts of SysTick void DLY_ticks(uint32_t n) { uint32_t end = STK->CNTL + n; while(((int32_t)(STK->CNTL - end)) < 0); } // =================================================================================== // Bootloader (BOOT) Functions // =================================================================================== // Perform software reset and jump to bootloader void BOOT_now(void) { FLASH->KEYR = FLASH_KEY1; FLASH->KEYR = FLASH_KEY2; FLASH->BOOT_MODEKEYR = FLASH_KEY1; FLASH->BOOT_MODEKEYR = FLASH_KEY2; // unlock flash FLASH->STATR |= FLASH_STATR_BOOT_MODE; // start bootloader after software reset FLASH->CTLR |= FLASH_CTLR_LOCK; // lock flash RCC->RSTSCKR |= RCC_RMVF; // clear reset flags NVIC->CFGR = NVIC_RESETSYS | NVIC_KEY3; // perform software reset } // =================================================================================== // Independent Watchdog Timer (IWDG) Functions // =================================================================================== // Start independent watchdog timer (IWDG) with given amount of WDG clock cycles // (ticks). The IWDG clock is HSI/1024/prescaler. One tick is 64*1024*1000/48000000 ms // long, max ticks is 4095 = 5591ms. // Once the IWDG has been started, it cannot be disabled, only reloaded (feed). void IWDG_start_t(uint16_t ticks) { IWDG->CTLR = 0x5555; // allow register modification while(IWDG->STATR & IWDG_PVU); // wait for clock register to be ready IWDG->PSCR = 0b100; // set clock prescaler 64 while(IWDG->STATR & IWDG_RVU); // wait for reload register to be ready IWDG->RLDR = ticks; // set watchdog counter reload value IWDG->CTLR = 0xAAAA; // load reload value into watchdog counter IWDG->CTLR = 0xCCCC; // enable IWDG } // Reload watchdog counter with n ticks, n<=4095 void IWDG_reload_t(uint16_t ticks) { IWDG->CTLR = 0x5555; // allow register modification while(IWDG->STATR & IWDG_RVU); // wait for reload register to be ready IWDG->RLDR = ticks; // set watchdog counter reload value IWDG->CTLR = 0xAAAA; // load reload value into watchdog counter } // =================================================================================== // Automatic Wake-up Timer (AWU) Functions // =================================================================================== // Init and start automatic wake-up timer void AWU_init(void) { AWU->CSR = 0x02; // enable automatic wake-up timer EXTI->EVENR |= ((uint32_t)1<<27); // enable AWU event EXTI->RTENR |= ((uint32_t)1<<27); // enable AWU rising edge triggering } // Stop automatic wake-up timer void AWU_stop(void) { AWU->CSR = 0x00; // disable automatic wake-up timer EXTI->EVENR &= ~((uint32_t)1<<27); // disable AWU event EXTI->RTENR &= ~((uint32_t)1<<27); // disable AWU rising edge triggering } // =================================================================================== // Sleep Functions // =================================================================================== // Put device into sleep, wake up by interrupt void SLEEP_WFI_now(void) { PFIC->SCTLR &= ~PFIC_SLEEPDEEP; // set power-down mode to SLEEP __WFI(); // wait for interrupt } // Put device into sleep, wake up by event void SLEEP_WFE_now(void) { PFIC->SCTLR &= ~PFIC_SLEEPDEEP; // set power-down mode to SLEEP __WFE(); // wait for event } // Put device into stop, wake up interrupt void STOP_WFI_now(void) { PFIC->SCTLR |= PFIC_SLEEPDEEP; // set power-down mode to STOP __WFI(); // wait for interrupt } // Put device into stop, wake up event void STOP_WFE_now(void) { PFIC->SCTLR |= PFIC_SLEEPDEEP; // set power-down mode to STOP __WFE(); // wait for event } // Put device into standby (deep sleep), wake up interrupt void STDBY_WFI_now(void) { RCC->APB1PCENR |= RCC_PWREN; // enable power module PWR->CTLR |= PWR_CTLR_PDDS; // set power-down mode to STANDBY PFIC->SCTLR |= PFIC_SLEEPDEEP; __WFI(); // wait for interrupt PWR->CTLR &= ~PWR_CTLR_PDDS; // reset power-down mode } // Put device into standby (deep sleep), wake up event void STDBY_WFE_now(void) { RCC->APB1PCENR |= RCC_PWREN; // enable power module PWR->CTLR |= PWR_CTLR_PDDS; // set power-down mode to STANDBY PFIC->SCTLR |= PFIC_SLEEPDEEP; __WFE(); // wait for event PWR->CTLR &= ~PWR_CTLR_PDDS; // reset power-down mode } // =================================================================================== // C++ Support // =================================================================================== #ifdef __cplusplus extern void __cxa_pure_virtual() { while (1); } extern void (*__preinit_array_start[]) (void) __attribute__((weak)); extern void (*__preinit_array_end[]) (void) __attribute__((weak)); extern void (*__init_array_start[]) (void) __attribute__((weak)); extern void (*__init_array_end[]) (void) __attribute__((weak)); void __libc_init_array(void) { uint32_t count, i; count = __preinit_array_end - __preinit_array_start; for (i = 0; i < count; i++) __preinit_array_start[i](); count = __init_array_end - __init_array_start; for (i = 0; i < count; i++) __init_array_start[i](); } #endif // =================================================================================== // C version of CH32X035 Startup .s file from WCH // =================================================================================== extern uint32_t _sbss; extern uint32_t _ebss; extern uint32_t _data_lma; extern uint32_t _data_vma; extern uint32_t _edata; // Prototypes int main(void) __attribute__((section(".text.main"), used)); void jump_reset(void) __attribute__((section(".init.jump"), naked, used)); void (*const vectors[])(void) __attribute__((section(".vector"), used)); void reset_handler(void) __attribute__((section(".text.reset_handler"), naked, used)); #if SYS_USE_VECTORS > 0 // Unless a specific handler is overridden, it just spins forever void default_handler(void) __attribute__((section(".text.vector_handler"), naked, used)); void default_handler(void) { while(1); } // All interrupt handlers are aliased to default_handler unless overridden individually #define DUMMY_HANDLER __attribute__((section(".text.vector_handler"), weak, alias("default_handler"), used)) DUMMY_HANDLER void NMI_Handler(void); DUMMY_HANDLER void HardFault_Handler(void); DUMMY_HANDLER void Ecall_M_Mode_Handler(void); DUMMY_HANDLER void Ecall_U_Mode_Handler(void); DUMMY_HANDLER void Break_Point_Handler(void); DUMMY_HANDLER void SysTick_Handler(void); DUMMY_HANDLER void SW_Handler(void); DUMMY_HANDLER void WWDG_IRQHandler(void); DUMMY_HANDLER void PVD_IRQHandler(void); DUMMY_HANDLER void FLASH_IRQHandler(void); DUMMY_HANDLER void EXTI7_0_IRQHandler(void); DUMMY_HANDLER void AWU_IRQHandler(void); DUMMY_HANDLER void DMA1_Channel1_IRQHandler(void); DUMMY_HANDLER void DMA1_Channel2_IRQHandler(void); DUMMY_HANDLER void DMA1_Channel3_IRQHandler(void); DUMMY_HANDLER void DMA1_Channel4_IRQHandler(void); DUMMY_HANDLER void DMA1_Channel5_IRQHandler(void); DUMMY_HANDLER void DMA1_Channel6_IRQHandler(void); DUMMY_HANDLER void DMA1_Channel7_IRQHandler(void); DUMMY_HANDLER void ADC1_IRQHandler(void); DUMMY_HANDLER void I2C1_EV_IRQHandler(void); DUMMY_HANDLER void I2C1_ER_IRQHandler(void); DUMMY_HANDLER void USART1_IRQHandler(void); DUMMY_HANDLER void SPI1_IRQHandler(void); DUMMY_HANDLER void TIM1_BRK_IRQHandler(void); DUMMY_HANDLER void TIM1_UP_IRQHandler(void); DUMMY_HANDLER void TIM1_TRG_COM_IRQHandler(void); DUMMY_HANDLER void TIM1_CC_IRQHandler(void); DUMMY_HANDLER void TIM2_UP_IRQHandler(void); DUMMY_HANDLER void USART2_IRQHandler(void); DUMMY_HANDLER void EXTI15_8_IRQHandler(void); DUMMY_HANDLER void EXTI25_16_IRQHandler(void); DUMMY_HANDLER void USART3_IRQHandler(void); DUMMY_HANDLER void USART4_IRQHandler(void); DUMMY_HANDLER void DMA1_Channel8_IRQHandler(void); DUMMY_HANDLER void USBFS_IRQHandler(void); DUMMY_HANDLER void USBFSWakeUp_IRQHandler(void); DUMMY_HANDLER void PIOC_IRQHandler(void); DUMMY_HANDLER void OPA_IRQHandler(void); DUMMY_HANDLER void USBPD_IRQHandler(void); DUMMY_HANDLER void USBPDWakeUp_IRQHandler(void); DUMMY_HANDLER void TIM2_CC_IRQHandler(void); DUMMY_HANDLER void TIM2_TRG_COM_IRQHandler(void); DUMMY_HANDLER void TIM2_BRK_IRQHandler(void); DUMMY_HANDLER void TIM3_IRQHandler(void); #endif // SYS_USE_VECTORS > 0 // FLASH starts with a jump to the reset handler void jump_reset(void) { asm volatile("j reset_handler"); } // Interrupt vector table void (* const vectors[])(void) = { // RISC-V handlers jump_reset, // 0 - Reset #if SYS_USE_VECTORS > 0 0, // 1 - Reserved NMI_Handler, // 2 - NMI Handler HardFault_Handler, // 3 - Hard Fault Handler 0, // 4 - Reserved Ecall_M_Mode_Handler, // 5 - Ecall M Mode Handler 0, // 6 - Reserved 0, // 7 - Reserved Ecall_U_Mode_Handler, // 8 - Ecall U Mode Handler Break_Point_Handler, // 9 - Break Point Handler 0, // 10 - Reserved 0, // 11 - Reserved SysTick_Handler, // 12 - SysTick Handler 0, // 13 - Reserved SW_Handler, // 14 - SW Handler 0, // 15 - Reserved // Peripheral handlers WWDG_IRQHandler, // 16 - Window Watchdog PVD_IRQHandler, // 17 - PVD through EXTI Line detect FLASH_IRQHandler, // 18 - Flash 0, // 19 - Reserved EXTI7_0_IRQHandler, // 20 - EXTI Line 7..0 AWU_IRQHandler, // 21 - Auto Wake-up DMA1_Channel1_IRQHandler, // 22 - DMA1 Channel 1 DMA1_Channel2_IRQHandler, // 23 - DMA1 Channel 2 DMA1_Channel3_IRQHandler, // 24 - DMA1 Channel 3 DMA1_Channel4_IRQHandler, // 25 - DMA1 Channel 4 DMA1_Channel5_IRQHandler, // 26 - DMA1 Channel 5 DMA1_Channel6_IRQHandler, // 27 - DMA1 Channel 6 DMA1_Channel7_IRQHandler, // 28 - DMA1 Channel 7 ADC1_IRQHandler, // 29 - ADC1 I2C1_EV_IRQHandler, // 30 - I2C1 Event I2C1_ER_IRQHandler, // 31 - I2C1 Error USART1_IRQHandler, // 32 - USART1 SPI1_IRQHandler, // 33 - SPI1 TIM1_BRK_IRQHandler, // 34 - TIM1 Break TIM1_UP_IRQHandler, // 35 - TIM1 Update TIM1_TRG_COM_IRQHandler, // 36 - TIM1 Trigger and Commutation TIM1_CC_IRQHandler, // 37 - TIM1 Capture Compare TIM2_UP_IRQHandler, // 38 - TIM2 Update USART2_IRQHandler, // 39 - USART2 EXTI15_8_IRQHandler, // 40 - EXTI Line 15..8 EXTI25_16_IRQHandler, // 41 - EXTI Line 25..16 USART3_IRQHandler, // 42 - USART3 USART4_IRQHandler, // 43 - USART4 DMA1_Channel8_IRQHandler, // 44 - DMA1 Channel8 USBFS_IRQHandler, // 45 - USBFS Break USBFSWakeUp_IRQHandler, // 46 - USBFS Wake up from suspend PIOC_IRQHandler, // 47 - PIOC OPA_IRQHandler, // 48 - OPA USBPD_IRQHandler, // 49 - USBPD USBPDWakeUp_IRQHandler, // 50 - USBPD Wake-up TIM2_CC_IRQHandler, // 51 - TIM2 Capture Compare TIM2_TRG_COM_IRQHandler, // 52 - TIM2 Trigger and Commutation TIM2_BRK_IRQHandler, // 53 - TIM2 Break TIM3_IRQHandler // 54 - TIM3 #endif // SYS_USE_VECTORS > 0 }; // Reset handler void reset_handler(void) { uint32_t *src, *dst; // Set pointers, vectors, processor status, and interrupts asm volatile( " .option push \n\ .option norelax \n\ la gp, __global_pointer$ \n\ .option pop \n\ la sp, _eusrstack \n" #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif " li a0, 0x1f \n\ csrw 0xbc0, a0 \n\ li a0, 0x88 \n\ csrs mstatus, a0 \n\ li a1, 0x3 \n\ csrw 0x804, a1 \n\ la a0, vectors \n\ or a0, a0, a1 \n\ csrw mtvec, a0 \n\ csrw mepc, %[main] \n" : : [main] "r" (main) : "a0", "a1" , "memory" ); // Copy data from FLASH to RAM src = &_data_lma; dst = &_data_vma; while(dst < &_edata) *dst++ = *src++; // Clear uninitialized variables #if SYS_CLEAR_BSS > 0 dst = &_sbss; while(dst < &_ebss) *dst++ = 0; #endif // C++ Support #ifdef __cplusplus __libc_init_array(); #endif // Init system SYS_init(); // Return asm volatile("mret"); } ================================================ FILE: software/pd_tester/src/system.h ================================================ // =================================================================================== // Basic System Functions for CH32X035/X034/X033 * v1.1 * // =================================================================================== // // This file must be included!!! The system configuration and the system clock are // set up automatically on system start. // // System clock functions available: // --------------------------------- // CLK_init() init system clock according to F_CPU // CLK_reset() reset system clock to default state // // HSI_enable() enable internal 48 MHz high-speed clock (HSI) // HSI_disable() disable HSI // HSI_ready() check if HSI is stable // // MCO_init() init clock output to pin PB9 // MCO_setSYS() output SYS_CLK on pin PB9 // MCO_setHSI() output internal oscillator on pin PB9 // MCO_stop() stop clock output // // Delay (DLY) functions available: // -------------------------------- // DLY_ticks(n) delay n system clock cycles // DLY_us(n) delay n microseconds // DLY_ms(n) delay n milliseconds // // Reset (RST) and Bootloader (BOOT) functions available: // ------------------------------------------------------ // BOOT_now() conduct software reset and jump to bootloader // RST_now() conduct software reset // RST_clearFlags() clear all reset flags // RST_wasLowPower() check if last reset was caused by low power // RST_wasWWDG() check if last reset was caused by window watchdog // RST_wasIWDG() check if last reset was caused by independent watchdog // RST_wasSoftware() check if last reset was caused by software // RST_wasPower() check if last reset was caused by power up // RST_wasPin() check if last reset was caused by RST pin low // RST_wasOPA() check if last reset was caused by OPA // // Independent Watchdog Timer (IWDG) functions available: // ------------------------------------------------------ // IWDG_start(n) start independent watchdog timer, n milliseconds, n<=5591 // IWDG_reload(n) reload watchdog counter with n milliseconds, n<=5591 // IWDG_feed() feed the dog (reload last time) // // Automatic Wake-up Timer (AWU) functions available: // -------------------------------------------------- // AWU_start(n) start AWU with n milliseconds period and event trigger // AWU_stop() stop AWU and event trigger // AWU_set(n) set AWU period to n milliseconds // // AWU_enable() enable AWU // AWU_disable() disable AWU // AWU_RT_enable() enable AWU rising edge trigger // AWU_RT_disable() disable AWU rising edge trigger // AWU_EV_enable() enable AWU event // AWU_EV_disable() disable AWU event // AWU_INT_enable() enable AWU interrupt (without NVIC) // AWU_INT_disable() disable AWU interrupt (without NVIC) // // Sleep functions available: // -------------------------- // SLEEP_WFI_now() put device into SLEEP, wake up by interrupt // SLEEP_WFE_now() put device into SLEEP, wake up by event // STOP_WFI_now() put device into STOP, wake up by interrupt // STOP_WFE_now() put device into STOP, wake up by event // STDBY_WFI_now() put device into STANDBY (deep sleep), wake by interrupt // STDBY_WFE_now() put device into STANDBY (deep sleep), wake by event // // SLEEP_ms(n) put device into SLEEP for n milliseconds (uses AWU) // STOP_ms(n) put device into STOP for n milliseconds (uses AWU) // STDBY_ms(n) put device into STANDBY for n milliseconds (uses AWU) // // Programmable Voltage Detector (PVD) functions available: // -------------------------------------------------------- // PVD_enable() enable PVD (power module clock) // PVD_set_2V1() set detection level to 2.1V // PVD_set_2V3() set detection level to 2.3V // PVD_set_3V0() set detection level to 3.0V // PVD_set_4V0() set detection level to 4.0V // PVD_isLow() check if VDD is below detection level // // Interrupt (INT) functions available: // ------------------------------------ // INT_enable() global interrupt enable // INT_disable() global interrupt disable // INT_ATOMIC_BLOCK { } execute block without being interrupted // // References: // ----------- // - WCH Nanjing Qinheng Microelectronics: http://wch.cn // // 2023 by Stefan Wagner: https://github.com/wagiminator #pragma once #ifdef __cplusplus extern "C" { #endif #include "ch32x035.h" // =================================================================================== // System Options (set "1" to activate) // =================================================================================== #define SYS_CLK_INIT 1 // 1: init system clock on startup #define SYS_TICK_INIT 1 // 1: init and start SYSTICK on startup #define SYS_GPIO_EN 1 // 1: enable GPIO ports on startup #define SYS_CLEAR_BSS 1 // 1: clear uninitialized variables #define SYS_USE_VECTORS 1 // 1: create interrupt vector table // =================================================================================== // Sytem Clock Defines // =================================================================================== // Set system clock frequency #ifndef F_CPU #define F_CPU 8000000 // 8 Mhz if not otherwise defined #endif // Calculate system clock settings #if F_CPU == 48000000 #define SYS_CLK_DIV RCC_HPRE_DIV1 #elif F_CPU == 24000000 #define SYS_CLK_DIV RCC_HPRE_DIV2 #elif F_CPU == 16000000 #define SYS_CLK_DIV RCC_HPRE_DIV3 #elif F_CPU == 12000000 #define SYS_CLK_DIV RCC_HPRE_DIV4 #elif F_CPU == 9600000 #define SYS_CLK_DIV RCC_HPRE_DIV5 #elif F_CPU == 8000000 #define SYS_CLK_DIV RCC_HPRE_DIV6 #elif F_CPU == 6000000 #define SYS_CLK_DIV RCC_HPRE_DIV8 #elif F_CPU == 3000000 #define SYS_CLK_DIV RCC_HPRE_DIV16 #elif F_CPU == 1500000 #define SYS_CLK_DIV RCC_HPRE_DIV32 #elif F_CPU == 750000 #define SYS_CLK_DIV RCC_HPRE_DIV64 #elif F_CPU == 375000 #define SYS_CLK_DIV RCC_HPRE_DIV128 #elif F_CPU == 187500 #define SYS_CLK_DIV RCC_HPRE_DIV256 #else #warning Unsupported system clock frequency, using internal 8 MHz #define SYS_CLK_DIV RCC_HPRE_DIV6 #undef F_CPU #define F_CPU 8000000 #endif // =================================================================================== // System Clock Functions // =================================================================================== void CLK_reset(void); // reset system clock to default state void MCO_init(void); // init clock output to pin PB9 // Internal 48MHz high-speed clock (HSI) functions #define CLK_init() RCC->CFGR0 = SYS_CLK_DIV // init system clock #define HSI_enable() RCC->CTLR |= RCC_HSION // enable HSI #define HSI_disable() RCC->CTLR &= ~RCC_HSION // disable HSI #define HSI_ready() (RCC->CTLR & RCC_HSIRDY) // check if HSI is stable // Clock output functions (pin PA8) #define MCO_setSYS() RCC->CFGR0 = (RCC->CFGR0 & ~RCC_CFGR0_MCO) | RCC_CFGR0_MCO_SYSCLK #define MCO_setHSI() RCC->CFGR0 = (RCC->CFGR0 & ~RCC_CFGR0_MCO) | RCC_CFGR0_MCO_HSI #define MCO_stop() RCC->CFGR0 &= ~RCC_CFGR0_MCO // stop clock output to pin PB9 // =================================================================================== // Delay (DLY) Functions // =================================================================================== void DLY_ticks(uint32_t n); // delay n system ticks #define STK_init() STK->CTLR = STK_CTLR_STE | STK_CTLR_STCLK // init SYSTICK @ F_CPU #define DLY_US_TIME (F_CPU / 1000000) // system ticks per us #define DLY_MS_TIME (F_CPU / 1000) // system ticks per ms #define DLY_us(n) DLY_ticks((n) * DLY_US_TIME) // delay n microseconds #define DLY_ms(n) DLY_ticks((n) * DLY_MS_TIME) // delay n milliseconds #define DLY_cycles DLY_ticks // alias // =================================================================================== // Reset (RST) Functions // =================================================================================== #define RST_now() PFIC->CFGR = PFIC_RESETSYS | PFIC_KEY3 #define RST_clearFlags() RCC->RSTSCKR |= RCC_RMVF #define RST_wasLowPower() (RCC->RSTSCKR & RCC_LPWRRSTF) #define RST_wasWWDG() (RCC->RSTSCKR & RCC_WWDGRSTF) #define RST_wasIWDG() (RCC->RSTSCKR & RCC_IWDGRSTF) #define RST_wasSoftware() (RCC->RSTSCKR & RCC_SFTRSTF) #define RST_wasPower() (RCC->RSTSCKR & RCC_PORRSTF) #define RST_wasPin() (RCC->RSTSCKR & RCC_PINRSTF) #define RST_wasOPA() (RCC->RSTSCKR & RCC_OPARSTF) // =================================================================================== // Bootloader (BOOT) Functions // =================================================================================== void BOOT_now(void); // perform software reset and jump to bootloader // =================================================================================== // Independent Watchdog Timer (IWDG) Functions // =================================================================================== void IWDG_start_t(uint16_t ticks); // start IWDG with n ticks void IWDG_reload_t(uint16_t ticks); // reload IWDG with n ticks #define IWDG_start(n) IWDG_start_t((HSI_VALUE/1000)*(n)/65536) #define IWDG_reload(n) IWDG_reload_t((HSI_VALUE/1000)*(n)/65536) #define IWDG_feed() IWDG->CTLR = 0xAAAA #define IWDG_reset() IWDG->CTLR = 0xAAAA // alias // =================================================================================== // Automatic Wake-up Timer (AWU) Functions // =================================================================================== void AWU_init(void); void AWU_stop(void); #define AWU_start(n) {AWU_init(); AWU_set(n);} #define AWU_enable() AWU->CSR = 0x02 #define AWU_disable() AWU->CSR = 0x00 #define AWU_RT_enable() EXTI->RTENR |= ((uint32_t)1 << 27) #define AWU_RT_disable() EXTI->RTENR &= ~((uint32_t)1 << 27) #define AWU_EV_enable() EXTI->EVENR |= ((uint32_t)1 << 27) #define AWU_EV_disable() EXTI->EVENR &= ~((uint32_t)1 << 27) #define AWU_INT_enable() EXTI->INTENR |= ((uint32_t)1 << 27) #define AWU_INT_disable() EXTI->INTENR &= ~((uint32_t)1 << 27) #define AWU_WRVAL(n) ((HSI_VALUE/1000)*(n)/1024) #define AWU_LIMIT(n) (63*(n)*1024/(HSI_VALUE/1000)) #define AWU_set(n) \ (n <= AWU_LIMIT( 32) ? ({AWU->PSC = 0b0110; AWU->WR = AWU_WRVAL(n) / 32;}) : \ (n <= AWU_LIMIT( 64) ? ({AWU->PSC = 0b0111; AWU->WR = AWU_WRVAL(n) / 64;}) : \ (n <= AWU_LIMIT( 128) ? ({AWU->PSC = 0b1000; AWU->WR = AWU_WRVAL(n) / 128;}) : \ (n <= AWU_LIMIT( 256) ? ({AWU->PSC = 0b1001; AWU->WR = AWU_WRVAL(n) / 256;}) : \ (n <= AWU_LIMIT( 512) ? ({AWU->PSC = 0b1010; AWU->WR = AWU_WRVAL(n) / 512;}) : \ (n <= AWU_LIMIT( 1024) ? ({AWU->PSC = 0b1011; AWU->WR = AWU_WRVAL(n) / 1024;}) : \ (n <= AWU_LIMIT( 2048) ? ({AWU->PSC = 0b1100; AWU->WR = AWU_WRVAL(n) / 2048;}) : \ (n <= AWU_LIMIT( 4096) ? ({AWU->PSC = 0b1101; AWU->WR = AWU_WRVAL(n) / 4096;}) : \ (n <= AWU_LIMIT(10240) ? ({AWU->PSC = 0b1110; AWU->WR = AWU_WRVAL(n) / 10240;}) : \ (n <= AWU_LIMIT(61440) ? ({AWU->PSC = 0b1111; AWU->WR = AWU_WRVAL(n) / 61440;}) : \ (0))))))))))) #define AWU_SLEEP(n) {AWU_set(n); SLEEP_WFE_now();} #define AWU_STOP(n) {AWU_set(n); STOP_WFE_now();} #define AWU_STDBY(n) {AWU_set(n); STDBY_WFE_now();} // =================================================================================== // Sleep Functions // =================================================================================== void SLEEP_WFI_now(void); // put device into sleep, wake up by interrupt void SLEEP_WFE_now(void); // put device into sleep, wake up by event void STOP_WFI_now(void); // put device into stop, wake up by interrupt void STOP_WFE_now(void); // put device into stop, wake up by event void STDBY_WFI_now(void); // put device into standby (deep sleep), wake up interrupt void STDBY_WFE_now(void); // put device into standby (deep sleep), wake up event #define SLEEP_ms(n) {AWU_start(n); SLEEP_WFE_now(); AWU_stop();} #define STOP_ms(n) {AWU_start(n); STOP_WFE_now(); AWU_stop();} #define STDBY_ms(n) {AWU_start(n); STDBY_WFE_now(); AWU_stop();} // =================================================================================== // Programmable Voltage Detector (PVD) Functions // =================================================================================== #define PVD_enable() RCC->APB1PCENR |= RCC_PWREN #define PVD_set_2V1() PWR->CTLR &= ~PWR_CTLR_PLS #define PVD_set_2V3() PWR->CTLR = (PWR->CTLR & ~PWR_CTLR_PLS) | PWR_CTLR_PLS_2V3 #define PVD_set_3V0() PWR->CTLR = (PWR->CTLR & ~PWR_CTLR_PLS) | PWR_CTLR_PLS_3V0 #define PVD_set_4V0() PWR->CTLR |= PWR_CTLR_PLS #define PVD_isLow() (PWR->CSR & PWR_CSR_PVDO) #define PVD_RT_enable() EXTI->RTENR |= ((uint32_t)1 << 26) #define PVD_RT_disable() EXTI->RTENR &= ~((uint32_t)1 << 26) #define PVD_FT_enable() EXTI->FTENR |= ((uint32_t)1 << 26) #define PVD_FT_disable() EXTI->FTENR &= ~((uint32_t)1 << 26) #define PVD_EV_enable() EXTI->EVENR |= ((uint32_t)1 << 26) #define PVD_EV_disable() EXTI->EVENR &= ~((uint32_t)1 << 26) #define PVD_INT_enable() EXTI->INTENR |= ((uint32_t)1 << 26) #define PVD_INT_disable() EXTI->INTENR &= ~((uint32_t)1 << 26) // =================================================================================== // Interrupt (INT) Functions // =================================================================================== #define INT_enable() __enable_irq() #define INT_disable() __disable_irq() #define INT_ATOMIC_BLOCK for(INT_ATOMIC_RESTORE, __ToDo = 1; __ToDo; __ToDo = 0) #define INT_ATOMIC_RESTORE uint32_t __reg_save __attribute__((__cleanup__(__iRestore))) = __iSave() // Save interrupt status and disable interrupts static inline uint32_t __iSave(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, 0x800 \n" "csrw 0x800, %1 \n" : "=&r" (result) : "r" (0x6000) ); return result; } // Restore interrupt status static inline void __iRestore(const uint32_t *__s) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw 0x800, %0" : : "r" (*__s) ); } // =================================================================================== // Device Electronic Signature (ESIG) // =================================================================================== #define ESIG_FLASHSIZE (*(__I uint16_t*)(0x1FFFF7E0)) #define ESIG_UID1 (*(__I uint32_t*)(0x1FFFF7E8)) #define ESIG_UID2 (*(__I uint32_t*)(0x1FFFF7EC)) #define ESIG_UID3 (*(__I uint32_t*)(0x1FFFF7F0)) // =================================================================================== // Imported System Functions // =================================================================================== // Enable Global Interrupt __attribute__((always_inline)) static inline void __enable_irq(void) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw 0x800, %0" : : "r" (0x6088) ); } // Disable Global Interrupt __attribute__((always_inline)) static inline void __disable_irq(void) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw 0x800, %0" : : "r" (0x6000) ); } // Return the Machine Status Register __attribute__((always_inline)) static inline uint32_t __get_MSTATUS(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, mstatus" : "=r"(result) ); return (result); } // Set the Machine Status Register __attribute__((always_inline)) static inline void __set_MSTATUS(uint32_t value) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw mstatus, %0" : : "r"(value) ); } // Return the Machine ISA Register __attribute__((always_inline)) static inline uint32_t __get_MISA(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, misa" : "=r"(result) ); return (result); } // Set the Machine ISA Register __attribute__((always_inline)) static inline void __set_MISA(uint32_t value) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw misa, %0" : : "r"(value) ); } // Return the Machine Trap-Vector Base-Address Register __attribute__((always_inline)) static inline uint32_t __get_MTVEC(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, mtvec": "=r"(result) ); return (result); } // Set the Machine Trap-Vector Base-Address Register __attribute__((always_inline)) static inline void __set_MTVEC(uint32_t value) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw mtvec, %0" : : "r"(value) ); } // Return the Machine Seratch Register __attribute__((always_inline)) static inline uint32_t __get_MSCRATCH(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, mscratch" : "=r"(result) ); return (result); } // Set the Machine Seratch Register __attribute__((always_inline)) static inline void __set_MSCRATCH(uint32_t value) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw mscratch, %0" : : "r"(value) ); } // Return the Machine Exception Program Register __attribute__((always_inline)) static inline uint32_t __get_MEPC(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, mepc" : "=r"(result) ); return (result); } // Set the Machine Exception Program Register __attribute__((always_inline)) static inline void __set_MEPC(uint32_t value) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw mepc, %0" : : "r"(value) ); } // Return the Machine Cause Register __attribute__((always_inline)) static inline uint32_t __get_MCAUSE(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, mcause": "=r"(result) ); return (result); } // Set the Machine Cause Register __attribute__((always_inline)) static inline void __set_MCAUSE(uint32_t value) { __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrw mcause, %0" : : "r"(value) ); } // Return Vendor ID Register __attribute__((always_inline)) static inline uint32_t __get_MVENDORID(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, mvendorid" : "=r"(result) ); return (result); } // Return Machine Architecture ID Register __attribute__((always_inline)) static inline uint32_t __get_MARCHID(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, marchid" : "=r"(result) ); return (result); } // Return Machine Implementation ID Register __attribute__((always_inline)) static inline uint32_t __get_MIMPID(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, mimpid" : "=r"(result) ); return (result); } // Return Hart ID Register __attribute__((always_inline)) static inline uint32_t __get_MHARTID(void) { uint32_t result; __asm volatile( #if __GNUC__ > 10 ".option arch, +zicsr \n" #endif "csrr %0, mhartid" : "=r"(result) ); return (result); } // Return SP Register __attribute__((always_inline)) static inline uint32_t __get_SP(void) { uint32_t result; __asm volatile("mv %0, sp" : "=r"(result):); return (result); } // No OPeration __attribute__((always_inline)) static inline void __NOP(void) { __asm volatile("nop"); } // Enable NVIC interrupt (interrupt numbers) __attribute__((always_inline)) static inline void NVIC_EnableIRQ(IRQn_Type IRQn) { NVIC->IENR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); } // Disable NVIC interrupt (interrupt numbers) __attribute__((always_inline)) static inline void NVIC_DisableIRQ(IRQn_Type IRQn) { NVIC->IRER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); } // Get Interrupt Enable State __attribute__((always_inline)) static inline uint32_t NVIC_GetStatusIRQ(IRQn_Type IRQn) { return((uint32_t) ((NVIC->ISR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); } // Get Interrupt Pending State __attribute__((always_inline)) static inline uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn) { return((uint32_t) ((NVIC->IPR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); } // Set Interrupt Pending __attribute__((always_inline)) static inline void NVIC_SetPendingIRQ(IRQn_Type IRQn) { NVIC->IPSR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); } // Clear Interrupt Pending __attribute__((always_inline)) static inline void NVIC_ClearPendingIRQ(IRQn_Type IRQn) { NVIC->IPRR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); } // Get Interrupt Active State __attribute__((always_inline)) static inline uint32_t NVIC_GetActive(IRQn_Type IRQn) { return((uint32_t)((NVIC->IACTR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); } // Set Interrupt Priority __attribute__((always_inline)) static inline void NVIC_SetPriority(IRQn_Type IRQn, uint8_t priority) { NVIC->IPRIOR[(uint32_t)(IRQn)] = priority; } // Wait for Interrupt __attribute__((always_inline)) static inline void __WFI(void) { NVIC->SCTLR &= ~(1<<3); // wfi asm volatile ("wfi"); } // Set Event __attribute__((always_inline)) static inline void _SEV(void) { uint32_t t; t = NVIC->SCTLR; NVIC->SCTLR |= (1<<3)|(1<<5); NVIC->SCTLR = (NVIC->SCTLR & ~(1<<5)) | ( t & (1<<5)); } // Wait for Events __attribute__((always_inline)) static inline void _WFE(void) { NVIC->SCTLR |= (1<<3); __asm volatile ("wfi"); } // Wait for Events __attribute__((always_inline)) static inline void __WFE(void) { _SEV(); _WFE(); _WFE(); } // Set VTF Interrupt __attribute__((always_inline)) static inline void SetVTFIRQ(uint32_t addr, IRQn_Type IRQn, uint8_t num, FunctionalState NewState) { if(num > 3) return; if(NewState != DISABLE) { NVIC->VTFIDR[num] = IRQn; NVIC->VTFADDR[num] = ((addr&0xFFFFFFFE)|0x1); } else { NVIC->VTFIDR[num] = IRQn; NVIC->VTFADDR[num] = ((addr&0xFFFFFFFE)&(~0x1)); } } // Initiate a system reset request __attribute__((always_inline)) static inline void NVIC_SystemReset(void) { NVIC->CFGR = NVIC_KEY3|(1<<7); } // Atomic Add with 32bit value __attribute__((always_inline)) static inline int32_t __AMOADD_W(volatile int32_t *addr, int32_t value) { int32_t result; __asm volatile ("amoadd.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(value) : "memory"); return *addr; } // Atomic And with 32bit value __attribute__((always_inline)) static inline int32_t __AMOAND_W(volatile int32_t *addr, int32_t value) { int32_t result; __asm volatile ("amoand.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(value) : "memory"); return *addr; } // Atomic signed MAX with 32bit value __attribute__((always_inline)) static inline int32_t __AMOMAX_W(volatile int32_t *addr, int32_t value) { int32_t result; __asm volatile ("amomax.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(value) : "memory"); return *addr; } // Atomic unsigned MAX with 32bit value __attribute__((always_inline)) static inline uint32_t __AMOMAXU_W(volatile uint32_t *addr, uint32_t value) { uint32_t result; __asm volatile ("amomaxu.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(value) : "memory"); return *addr; } // Atomic signed MIN with 32bit value __attribute__((always_inline)) static inline int32_t __AMOMIN_W(volatile int32_t *addr, int32_t value) { int32_t result; __asm volatile ("amomin.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(value) : "memory"); return *addr; } // Atomic unsigned MIN with 32bit value __attribute__((always_inline)) static inline uint32_t __AMOMINU_W(volatile uint32_t *addr, uint32_t value) { uint32_t result; __asm volatile ("amominu.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(value) : "memory"); return *addr; } // Atomic OR with 32bit value __attribute__((always_inline)) static inline int32_t __AMOOR_W(volatile int32_t *addr, int32_t value) { int32_t result; __asm volatile ("amoor.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(value) : "memory"); return *addr; } // Atomically swap new 32bit value into memory using amoswap.d __attribute__((always_inline)) static inline uint32_t __AMOSWAP_W(volatile uint32_t *addr, uint32_t newval) { uint32_t result; __asm volatile ("amoswap.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(newval) : "memory"); return result; } // Atomic XOR with 32bit value __attribute__((always_inline)) static inline int32_t __AMOXOR_W(volatile int32_t *addr, int32_t value) { int32_t result; __asm volatile ("amoxor.w %0, %2, %1" : "=r"(result), "+A"(*addr) : "r"(value) : "memory"); return *addr; } #ifdef __cplusplus }; #endif ================================================ FILE: software/pd_tester/src/usbpd.h ================================================ // =================================================================================== // USB PD Constant and Structure Defines * v1.1 * // =================================================================================== #pragma once #ifdef __cplusplus extern "C" { #endif #include // Get values from PDO representation #define POWER_DECODE_50MV(value) ((uint16_t)(((value) * 50))) // From 50mV multiples to mV #define POWER_DECODE_100MV(value) ((uint16_t)(((value) * 100))) // From 100mV multiples to mV #define POWER_DECODE_10MA(value) ((uint16_t)(((value) * 10))) // From 10mA multiples to mA #define POWER_DECODE_50MA(value) ((uint16_t)(((value) * 50))) // From 50mA multiples to mA // PD PHY Channel #define USBPD_CCNONE 0x00u #define USBPD_CC1 0x01u #define USBPD_CC2 0x02u // USB PD Revision #define USBPD_SPECIFICATION_REV1 0x00u // Revision 1.0 #define USBPD_SPECIFICATION_REV2 0x01u // Revision 2.0 #define USBPD_SPECIFICATION_REV3 0x02u // Revision 3.0 // USB PD Revision aliases #define USBPD_REVISION_10 0x00u // Revision 1.0 #define USBPD_REVISION_20 0x01u // Revision 2.0 #define USBPD_REVISION_30 0x02u // Revision 3.0 typedef struct { uint32_t MaxCurrentIn10mAunits : 10u; uint32_t VoltageIn50mVunits : 10u; uint32_t PeakCurrent : 2u; uint32_t Reserved_22bit : 1u; uint32_t EPRModeCapable : 1u; uint32_t UnchunkedExtendedMessage : 1u; uint32_t DualRoleData : 1u; uint32_t USBCommunicationsCapable : 1u; uint32_t UnconstrainedPower : 1u; uint32_t USBSuspendSupported : 1u; uint32_t DualRolePower : 1u; uint32_t FixedSupply : 2u; } USBPD_SourceFixedSupplyPDO_t; typedef struct { uint32_t MaxCurrentIn50mAincrements : 7u; uint32_t Reserved_7bit : 1u; // shall be set to zero uint32_t MinVoltageIn100mVincrements: 8u; uint32_t Reserved_16bit : 1u; // shall be set to zero uint32_t MaxVoltageIn100mVincrements: 8u; uint32_t Reserved_25_26bit : 2u; // shall be set to zero uint32_t PPSpowerLimited : 1u; uint32_t SPRprogrammablePowerSupply : 2u; // 00b uint32_t AugmentedPowerDataObject : 2u; // 11b } USBPD_SourcePPSSupplyPDO_t; typedef struct { uint32_t MaxPowerIn1Wincrements : 8u; uint32_t MinVoltageIn100mVincrements: 8u; uint32_t Reserved_16bit : 1u; // shall be set to zero uint32_t MaxVoltageIn100mVincrements: 9u; uint32_t Reserved_26_27bit : 2u; // shall be set to zero uint32_t EPRprogrammablePowerSupply : 2u; // 01b uint32_t AugmentedPowerDataObject : 2u; // 11b } USBPD_SourceEPRSupplyPDO_t; typedef union { uint32_t d32; USBPD_SourceFixedSupplyPDO_t SourceFixedPDO; USBPD_SourcePPSSupplyPDO_t SourcePPSPDO; USBPD_SourceEPRSupplyPDO_t SourceEPRPDO; } USBPD_PDO_t; typedef enum { USBPD_CONTROL_MSG_GOODCRC = 0x01u, USBPD_CONTROL_MSG_GOTOMIN = 0x02u, USBPD_CONTROL_MSG_ACCEPT = 0x03u, USBPD_CONTROL_MSG_REJECT = 0x04u, USBPD_CONTROL_MSG_PING = 0x05u, USBPD_CONTROL_MSG_PS_RDY = 0x06u, USBPD_CONTROL_MSG_GET_SRC_CAP = 0x07u, USBPD_CONTROL_MSG_GET_SNK_CAP = 0x08u, USBPD_CONTROL_MSG_DR_SWAP = 0x09u, USBPD_CONTROL_MSG_PR_SWAP = 0x0Au, USBPD_CONTROL_MSG_VCONN_SWAP = 0x0Bu, USBPD_CONTROL_MSG_WAIT = 0x0Cu, USBPD_CONTROL_MSG_SOFT_RESET = 0x0Du, USBPD_CONTROL_MSG_DATA_RESET = 0x0Eu, USBPD_CONTROL_MSG_DATA_RESET_COMPLETE = 0x0Fu, USBPD_CONTROL_MSG_NOT_SUPPORTED = 0x10u, USBPD_CONTROL_MSG_GET_SRC_CAPEXT = 0x11u, USBPD_CONTROL_MSG_GET_STATUS = 0x12u, USBPD_CONTROL_MSG_FR_SWAP = 0x13u, USBPD_CONTROL_MSG_GET_PPS_STATUS = 0x14u, USBPD_CONTROL_MSG_GET_COUNTRY_CODES = 0x15u, USBPD_CONTROL_MSG_GET_SNK_CAPEXT = 0x16u, USBPD_CONTROL_MSG_GET_SRC_INFO = 0x17u, USBPD_CONTROL_MSG_GET_REVISION = 0x18u, } USBPD_ControlMessage_t; typedef enum { USBPD_DATA_MSG_SRC_CAP = 0x01u, USBPD_DATA_MSG_REQUEST = 0x02u, USBPD_DATA_MSG_BIST = 0x03u, USBPD_DATA_MSG_SNK_CAP = 0x04u, USBPD_DATA_MSG_BATTERY_STATUS = 0x05u, USBPD_DATA_MSG_ALERT = 0x06u, USBPD_DATA_MSG_GET_COUNTRY_INFO = 0x07u, USBPD_DATA_MSG_ENTER_USB = 0x08u, USBPD_DATA_MSG_EPR_REUEST = 0x09u, USBPD_DATA_MSG_EPR_MODE = 0x0Au, USBPD_DATA_MSG_SRC_INFO = 0x0Bu, USBPD_DATA_MSG_REVISION = 0x0Cu, USBPD_DATA_MSG_VENDOR_DEFINED = 0x0Fu } USBPD_DataMessage_t; typedef union { uint16_t d16; USBPD_DataMessage_t DataMessage; USBPD_ControlMessage_t ControlMessage; } USBPD_MessageType_t; typedef struct { uint16_t MessageType : 5u; uint16_t PortDataRole : 1u; // 0b->UFP, 1b->DFP uint16_t SpecificationRevision : 2u; // 00b->v1.0, 01b->v2.0, 10b->v3.0 uint16_t PortPowerRole : 1u; // 0b->sink, 1b->source uint16_t MessageID : 3u; uint16_t NumberOfDataObjects : 3u; uint16_t Extended : 1u; } USBPD_MessageHeader_tt; typedef union { uint16_t d16; USBPD_MessageHeader_tt MessageHeader; } USBPD_MessageHeader_t; typedef struct { uint32_t MaxOperatingCurrent10mAunits : 10u; uint32_t OperatingCurrentIn10mAunits : 10u; uint32_t Reserved20_21 : 2u; // 00b uint32_t EPRModeCapable : 1u; uint32_t UnchunkedExtendedMessage : 1u; uint32_t NoUSBSuspend : 1u; uint32_t USBCommunicationsCapable : 1u; uint32_t CapabilityMismatch : 1u; uint32_t GiveBackFlag : 1u; uint32_t ObjectPosition : 4u; } USBPD_SinkFixedVariableRDO_t; typedef struct { uint32_t OperatingCurrentIn50mAunits : 7u; uint32_t Reserved7_8 : 2u; // shall be set to zero uint32_t OutputVoltageIn20mVunits : 12u; uint32_t Reserved21 : 1u; // shall be set to zero uint32_t EPRModeCapable : 1u; uint32_t UnchunkedExtendedMessage : 1u; uint32_t NoUSBSuspend : 1u; uint32_t USBCommunicationsCapable : 1u; uint32_t CapabilityMismatch : 1u; uint32_t Rserved27 : 1u; // shall be set to zero uint32_t ObjectPosition : 4u; } USBPD_SinkPPSRDO_t; typedef union { uint32_t d32; USBPD_SinkFixedVariableRDO_t SinkFixedVariableRDO; USBPD_SinkPPSRDO_t SinkPPSRDO; } USBPD_SINKRDO_t; #ifdef __cplusplus } #endif ================================================ FILE: software/pd_tester/src/usbpd_sink.c ================================================ // =================================================================================== // USB PD SINK Handler for CH32X035 * v1.5 * // =================================================================================== // // Reference: https://github.com/openwch/ch32x035 // 2023 by Stefan Wagner: https://github.com/wagiminator #include "usbpd_sink.h" // Variables static pd_control_t PD_control = { .CC_State = CC_IDLE, .CC1_ConnectTimes = 0, .CC2_ConnectTimes = 0, }; FixedSourceCap_t PD_SC_fixed[5]; PPSSourceCap_t PD_SC_PPS[2]; // Buffers __attribute__ ((aligned(4))) uint8_t PD_TR_buffer[34]; // PD transmit/receive buffer __attribute__ ((aligned(4))) uint8_t PD_SC_buffer[28]; // PD Source Cap buffer // =================================================================================== // USB PD SINK Front End Functions // =================================================================================== // Prototype void PD_update(void); // Negotiate current settings and wait until finished (return 1) or timeout (return 0) uint8_t PD_negotiate(void) { uint8_t counter = 255; PD_control.LastSetVoltage = 0; PD_control.USBPD_READY = 0; while((!PD_control.USBPD_READY) && (--counter)) { DLY_ms(5); PD_update(); } return(counter > 0); } // Get total number of PDOs (fixed and programmable) uint8_t PD_getPDONum(void) { return PD_control.SourcePDONum; } // Get number of fixed power PDOs uint8_t PD_getFixedNum(void) { return PD_control.SourcePDONum - PD_control.SourcePPSNum; } // Get number of programmable power PDOs uint8_t PD_getPPSNum(void) { return PD_control.SourcePPSNum; } // Get voltage of specified fixed power PDO uint16_t PD_getPDOVoltage(uint8_t pdonum) { return PD_control.FixedSourceCap[pdonum - 1].Voltage; } // Get minimum voltage of specified PDO (fixed and programmable) uint16_t PD_getPDOMinVoltage(uint8_t pdonum) { uint8_t ppspos = PD_control.SourcePDONum - PD_control.SourcePPSNum; if(pdonum <= ppspos) return PD_control.FixedSourceCap[pdonum - 1].Voltage; else return PD_control.PPSSourceCap[pdonum - ppspos - 1].MinVoltage; } // Get maximum voltage of specified PDO (fixed and programmable) uint16_t PD_getPDOMaxVoltage(uint8_t pdonum) { uint8_t ppspos = PD_control.SourcePDONum - PD_control.SourcePPSNum; if(pdonum <= ppspos) return PD_control.FixedSourceCap[pdonum - 1].Voltage; else return PD_control.PPSSourceCap[pdonum - ppspos - 1].MaxVoltage; } // Get max current of specified PDO (fixed and programmable) uint16_t PD_getPDOMaxCurrent(uint8_t pdonum) { uint8_t ppspos = PD_control.SourcePDONum - PD_control.SourcePPSNum; if(pdonum <= ppspos) return PD_control.FixedSourceCap[pdonum - 1].Current; else return PD_control.PPSSourceCap[pdonum - ppspos - 1].Current; } // Set specified PDO and voltage; returns 0:failed, 1:success uint8_t PD_setPDO(uint8_t pdonum, uint16_t voltage) { PD_control.SetPDONum = pdonum; PD_control.SetVoltage = voltage; return PD_negotiate(); } // Set specified voltage (in millivolts) if available; returns 0:failed, 1:success uint8_t PD_setVoltage(uint16_t voltage) { uint8_t i; uint8_t ppspos = PD_control.SourcePDONum - PD_control.SourcePPSNum; for(i=0; i= voltage)) { return PD_setPDO(i + 1, voltage); } } } return 0; } // Get active PDO uint8_t PD_getPDO(void) { return PD_control.SetPDONum; } // Get active voltage uint16_t PD_getVoltage(void) { return PD_control.SetVoltage; } // Get active max current uint16_t PD_getCurrent(void) { uint8_t pdonum = PD_control.SetPDONum; uint8_t ppspos = PD_control.SourcePDONum - PD_control.SourcePPSNum; if(pdonum <= ppspos) return PD_control.FixedSourceCap[pdonum - 1].Current; else return PD_control.PPSSourceCap[pdonum - ppspos - 1].Current; } // Initialize PD registers and states, then connect uint8_t PD_connect(void) { RCC->APB2PCENR |= RCC_AFIOEN | RCC_IOPCEN; RCC->AHBPCENR |= RCC_USBPD; GPIOB->CFGHR = (GPIOB->CFGHR & ~( (uint32_t)0b1111<<(((14)&7)<<2) | (uint32_t)0b1111<<(((15)&7)<<2))) | ( (uint32_t)0b0100<<(((14)&7)<<2) | (uint32_t)0b0100<<(((15)&7)<<2)); #ifdef USB_VDD #if USB_VDD > 0 AFIO->CTLR |= USBPD_IN_HVT; #else AFIO->CTLR |= USBPD_IN_HVT | USBPD_PHY_V33; #endif #else RCC->APB1PCENR |= RCC_PWREN; PWR->CTLR |= PWR_CTLR_PLS; if(PWR->CSR & PWR_CSR_PVDO) AFIO->CTLR |= USBPD_IN_HVT | USBPD_PHY_V33; else AFIO->CTLR |= USBPD_IN_HVT; #endif USBPD->DMA = (uint32_t)PD_TR_buffer; USBPD->CONFIG = USBPD_IE_RX_ACT | USBPD_IE_RX_RESET | USBPD_IE_TX_END | USBPD_PD_DMA_EN; USBPD->STATUS = USBPD_BUF_ERR | USBPD_IF_RX_BIT | USBPD_IF_RX_BYTE | USBPD_IF_RX_ACT | USBPD_IF_RX_RESET | USBPD_IF_TX_END; return PD_negotiate(); } // =================================================================================== // USB PD SINK Back End Functions // =================================================================================== // Enter reception mode void PD_RX_mode(void) { USBPD->BMC_CLK_CNT = USBPD_TMR_RX; USBPD->CONTROL = (USBPD->CONTROL & ~USBPD_PD_TX_EN) | USBPD_BMC_START; } // Reset PD void PD_reset(void) { USBPD->PORT_CC1 = USBPD_CC_CMP_66 | USBPD_CC_PD; USBPD->PORT_CC2 = USBPD_CC_CMP_66 | USBPD_CC_PD; PD_control.CC1_ConnectTimes = 0; PD_control.CC2_ConnectTimes = 0; PD_control.CC_NoneTimes = 0; PD_control.SourcePDONum = 0; PD_control.SourcePPSNum = 0; PD_control.FixedSourceCap = PD_SC_fixed; PD_control.PPSSourceCap = PD_SC_PPS; PD_control.CC_State = CC_IDLE; PD_control.CC_LastState = CC_IDLE; PD_control.SinkMessageID = 0; PD_control.SinkGoodCRCOver = 0; PD_control.SourceGoodCRCOver = 0; PD_control.PD_Version = USBPD_REVISION_20; PD_control.USBPD_READY = 0; PD_control.SetPDONum = 1; PD_control.LastSetPDONum = 1; PD_control.SetVoltage = 5000; PD_control.LastSetVoltage = 5000; } // Copy buffers void PD_memcpy(uint8_t* dest, const uint8_t* src, uint8_t n) { while(n--) *dest++ = *src++; } // Send PD data void PD_sendData(uint8_t length) { if((USBPD->CONFIG & USBPD_CC_SEL) == USBPD_CC_SEL) USBPD->PORT_CC2 |= USBPD_CC_LVE; else USBPD->PORT_CC1 |= USBPD_CC_LVE; USBPD->BMC_CLK_CNT = USBPD_TMR_TX; USBPD->TX_SEL = USBPD_TX_SOP0; USBPD->BMC_TX_SZ = length; USBPD->STATUS = 0; USBPD->CONTROL |= USBPD_BMC_START | USBPD_PD_TX_EN; } // Detect CC connection; returns 0:No connection, 1:CC1 connection, 2:CC2 connection uint8_t PD_checkCC(void) { uint8_t ccLine = USBPD_CCNONE; USBPD->PORT_CC1 &= ~(USBPD_CC_CE | USBPD_PA_CC_AI); USBPD->PORT_CC1 |= USBPD_CC_CMP_22; if(USBPD->PORT_CC1 & USBPD_PA_CC_AI) ccLine = USBPD_CC1; USBPD->PORT_CC2 &= ~(USBPD_CC_CE | USBPD_PA_CC_AI); USBPD->PORT_CC2 |= USBPD_CC_CMP_22; if(USBPD->PORT_CC2 & USBPD_PA_CC_AI) ccLine = USBPD_CC2; return ccLine; } void PD_PDO_analyze(void) { USBPD_PDO_t test; PD_control.SourcePPSNum = 0; for(uint8_t i=0; i (PD_control.SourcePDONum - PD_control.SourcePPSNum)) { pdo.SinkPPSRDO.ObjectPosition = pdoNum; pdo.SinkPPSRDO.OutputVoltageIn20mVunits = PD_control.SetVoltage / 20; pdo.SinkPPSRDO.OperatingCurrentIn50mAunits = PD_SC_PPS[pdoNum+PD_control.SourcePPSNum-PD_control.SourcePDONum-1].Current/50; pdo.SinkPPSRDO.NoUSBSuspend = 1u; pdo.SinkPPSRDO.USBCommunicationsCapable = 1u; } else { pdo.SinkFixedVariableRDO.ObjectPosition = pdoNum; pdo.SinkFixedVariableRDO.MaxOperatingCurrent10mAunits = PD_SC_fixed[pdoNum-1].Current/10; pdo.SinkFixedVariableRDO.OperatingCurrentIn10mAunits = PD_SC_fixed[pdoNum-1].Current/10; pdo.SinkFixedVariableRDO.USBCommunicationsCapable = 1u; pdo.SinkFixedVariableRDO.NoUSBSuspend = 1u; } *(uint16_t*)&PD_TR_buffer[0] = mh.d16; *(uint32_t*)&PD_TR_buffer[2] = pdo.d32; PD_sendData(6); } void PD_process(void) { cc_state_t temp = PD_control.CC_State; USBPD_MessageHeader_t mh; switch (PD_control.CC_State) { case CC_IDLE: NVIC_DisableIRQ(USBPD_IRQn); PD_reset(); PD_control.CC_State = CC_CHECK_CONNECT; break; case CC_CHECK_CONNECT: break; case CC_CONNECT: if(PD_control.CC_LastState != PD_control.CC_State) { PD_RX_mode(); NVIC_SetPriority(USBPD_IRQn, 0x00); NVIC_EnableIRQ(USBPD_IRQn); } break; case CC_SOURCE_CAP: if(PD_control.SinkGoodCRCOver) { PD_control.SinkGoodCRCOver = 0; NVIC_DisableIRQ(USBPD_IRQn); PD_PDO_analyze(); NVIC_EnableIRQ(USBPD_IRQn); PD_control.CC_State = CC_SEND_REQUEST; } break; case CC_SEND_REQUEST: if(PD_control.CC_LastState != PD_control.CC_State) { PD_PDO_request(); } if(PD_control.SourceGoodCRCOver) { PD_control.SourceGoodCRCOver = 0; PD_control.CC_State = CC_WAIT_ACCEPT; } break; case CC_WAIT_PS_RDY: break; case CC_PS_RDY: if(PD_control.SinkGoodCRCOver) { PD_control.SinkGoodCRCOver = 0; PD_control.CC_State = CC_GET_SOURCE_CAP; PD_control.WaitTime = 0; } break; case CC_GET_SOURCE_CAP: PD_control.USBPD_READY = 1; if((PD_control.SetPDONum != PD_control.LastSetPDONum) || (PD_control.SetVoltage != PD_control.LastSetVoltage)) { PD_control.LastSetPDONum = PD_control.SetPDONum; PD_control.LastSetVoltage = PD_control.SetVoltage; PD_control.USBPD_READY = 0; mh.d16 = 0u; mh.MessageHeader.MessageID = PD_control.SinkMessageID; mh.MessageHeader.MessageType = USBPD_CONTROL_MSG_GET_SRC_CAP; mh.MessageHeader.NumberOfDataObjects = 0u; mh.MessageHeader.SpecificationRevision = PD_control.PD_Version; *(uint16_t*)&PD_TR_buffer[0] = mh.d16; PD_sendData(2); PD_control.CC_State = CC_GET_SOURCE_CAP+1; } break; default: break; } PD_control.CC_LastState = temp; } void PD_update(void) { uint8_t ccLine = PD_checkCC(); PD_control.WaitTime++; if(PD_control.CC_State == CC_CHECK_CONNECT) { if(ccLine == USBPD_CC1) { PD_control.CC2_ConnectTimes = 0; PD_control.CC1_ConnectTimes++; if(PD_control.CC1_ConnectTimes > 5) { PD_control.CC1_ConnectTimes = 0; PD_control.CC_State = CC_CONNECT; USBPD->CONFIG &= ~USBPD_CC_SEL; } } else if(ccLine == USBPD_CC2) { PD_control.CC1_ConnectTimes = 0; PD_control.CC2_ConnectTimes++; if(PD_control.CC2_ConnectTimes > 5) { PD_control.CC2_ConnectTimes = 0; PD_control.CC_State = CC_CONNECT; USBPD->CONFIG |= USBPD_CC_SEL; } } else { PD_control.CC1_ConnectTimes = 0; PD_control.CC2_ConnectTimes = 0; } } if(PD_control.CC_State > CC_CHECK_CONNECT) { if(ccLine == USBPD_CCNONE) { PD_control.CC_NoneTimes++; if(PD_control.CC_NoneTimes > 5) { PD_control.CC_NoneTimes = 0; PD_control.CC_State = CC_IDLE; NVIC_DisableIRQ(USBPD_IRQn); } } else PD_control.CC_NoneTimes = 0; } PD_process(); } // Analyze received data void PD_RX_analyze(void) { uint8_t sendGoodCRCFlag = 1; USBPD_MessageHeader_t mh; mh.d16 = *(uint16_t*)PD_TR_buffer; if(mh.MessageHeader.Extended == 0u) { if(mh.MessageHeader.NumberOfDataObjects == 0u) { switch(mh.MessageHeader.MessageType) { case USBPD_CONTROL_MSG_GOODCRC: sendGoodCRCFlag = 0; PD_control.SourceGoodCRCOver = 1; PD_control.SinkMessageID++; break; case USBPD_CONTROL_MSG_ACCEPT: PD_control.CC_State = CC_WAIT_PS_RDY; break; case USBPD_CONTROL_MSG_PS_RDY: PD_control.CC_State = CC_PS_RDY; break; default: break; } } else { switch(mh.MessageHeader.MessageType) { case USBPD_DATA_MSG_SRC_CAP: PD_control.CC_State = CC_SOURCE_CAP; PD_control.SourcePDONum = mh.MessageHeader.NumberOfDataObjects; PD_control.PD_Version = mh.MessageHeader.SpecificationRevision; PD_memcpy(PD_SC_buffer, &PD_TR_buffer[2], 28); break; default: break; } } } if(sendGoodCRCFlag) { DLY_us(30); PD_control.SinkGoodCRCOver = 0; USBPD_MessageHeader_t my_mh; my_mh.d16 = 0u; my_mh.MessageHeader.MessageID = mh.MessageHeader.MessageID; my_mh.MessageHeader.MessageType = USBPD_CONTROL_MSG_GOODCRC; my_mh.MessageHeader.SpecificationRevision = PD_control.PD_Version; *(uint16_t*)&PD_TR_buffer[0] = my_mh.d16; PD_sendData(2); } } // =================================================================================== // USB PD Interrupt Service Routine // =================================================================================== void USBPD_IRQHandler(void) __attribute__((interrupt)); void USBPD_IRQHandler(void) { // Receive complete interrupt if(USBPD->STATUS & USBPD_IF_RX_ACT) { if((USBPD->STATUS & USBPD_BMC_AUX) == USBPD_BMC_AUX_SOP0) { if(USBPD->BMC_BYTE_CNT >= 6) { PD_RX_analyze(); } } USBPD->STATUS |= USBPD_IF_RX_ACT; } // Transmit complete interrupt (GoodCRC only) if(USBPD->STATUS & USBPD_IF_TX_END) { USBPD->PORT_CC1 &= ~USBPD_CC_LVE; USBPD->PORT_CC2 &= ~USBPD_CC_LVE; PD_RX_mode(); PD_control.SinkGoodCRCOver = 1; USBPD->STATUS |= USBPD_IF_TX_END; } // Reset interrupt if(USBPD->STATUS & USBPD_IF_RX_RESET) { USBPD->STATUS |= USBPD_IF_RX_RESET; PD_reset(); } } ================================================ FILE: software/pd_tester/src/usbpd_sink.h ================================================ // =================================================================================== // USB PD SINK Handler for CH32X035 * v1.5 * // =================================================================================== // // Functions available: // -------------------- // PD_connect() Initialize USB-PD and connect, returns 0 if failed // PD_negotiate() Negotiate current settings, returns 0 if failed // PD_setVoltage(mV) Request specified voltage in millivolts, returns 0 if failed // // PD_getPDONum() Get total number of PDOs // PD_getFixedNum() Get number of fixed power PDOs // PD_getPPSNum() Get number of programmable power PDOs // // PD_getPDOVoltage(p) Get voltage of specified fixed power PDO (1..PD_getFixedNum()) // PD_getPDOMinVoltage(p) Get min voltage of specified PDO (p = 1..PD_getPDONum()) // PD_getPDOMaxVoltage(p) Get max voltage of specified PDO (p = 1..PD_getPDONum()) // PD_getPDOMaxCurrent(p) Get max current of specified PDO (p = 1..PD_getPDONum()) // // PD_getPDO() Get active PDO // PD_getVoltage() Get active voltage // PD_getCurrent() Get active max current // // Reference: https://github.com/openwch/ch32x035 // 2023 by Stefan Wagner: https://github.com/wagiminator #pragma once #ifdef __cplusplus extern "C" { #endif #include "config.h" #include "system.h" #include "usbpd.h" // =================================================================================== // Parameters and Checks // =================================================================================== #if SYS_USE_VECTORS == 0 #error Interrupt vector table must be enabled (SYS_USE_VECTORS in system.h)! #endif #if F_CPU == 48000000 #define USBPD_TMR_TX (80-1) // timer value for USB PD BMC TX @ F_CPU=48MHz #define USBPD_TMR_RX (120-1) // timer value for USB PD BMC RX @ F_CPU=48MHz #elif F_CPU == 24000000 #define USBPD_TMR_TX (40-1) // timer value for USB PD BMC TX @ F_CPU=24MHz #define USBPD_TMR_RX (60-1) // timer value for USB PD BMC RX @ F_CPU=24MHz #elif F_CPU == 12000000 #define USBPD_TMR_TX (20-1) // timer value for USB PD BMC TX @ F_CPU=12MHz #define USBPD_TMR_RX (30-1) // timer value for USB PD BMC RX @ F_CPU=12MHz #elif F_CPU == 6000000 #define USBPD_TMR_TX (10-1) // timer value for USB PD BMC TX @ F_CPU=6MHz #define USBPD_TMR_RX (15-1) // timer value for USB PD BMC RX @ F_CPU=6MHz #else #error Unsupported system frequency for USBPD! #endif // =================================================================================== // Type defines // =================================================================================== typedef struct { uint16_t Current; uint16_t Voltage; } FixedSourceCap_t; typedef struct { uint16_t MinVoltage; uint16_t MaxVoltage; uint16_t Current; } PPSSourceCap_t; typedef enum { CC_IDLE = 0u, CC_CHECK_CONNECT, CC_CONNECT, CC_SOURCE_CAP, CC_SEND_REQUEST, CC_WAIT_ACCEPT, CC_ACCEPT, CC_WAIT_PS_RDY, CC_PS_RDY, CC_GET_SOURCE_CAP, } cc_state_t; typedef struct { volatile cc_state_t CC_State; volatile cc_state_t CC_LastState; volatile uint8_t CC_NoneTimes; volatile uint8_t CC1_ConnectTimes; volatile uint8_t CC2_ConnectTimes; FixedSourceCap_t* FixedSourceCap; PPSSourceCap_t* PPSSourceCap; volatile uint8_t SourcePDONum; volatile uint8_t SourcePPSNum; volatile uint8_t PD_Version; volatile uint16_t WaitTime; volatile uint8_t SetPDONum; volatile uint8_t LastSetPDONum; volatile uint16_t SetVoltage; volatile uint16_t LastSetVoltage; volatile uint8_t USBPD_READY; volatile uint8_t SourceMessageID; volatile uint8_t SinkMessageID; volatile uint8_t SinkGoodCRCOver; volatile uint8_t SourceGoodCRCOver; } pd_control_t; // =================================================================================== // Functions // =================================================================================== uint8_t PD_connect(void); // Initialize PD and connect uint8_t PD_negotiate(void); // Negotiate current settings uint8_t PD_setVoltage(uint16_t voltage); // Set specified voltage (in millivolts) uint8_t PD_getPDONum(void); // Get total number of PDOs uint8_t PD_getFixedNum(void); // Get number of fixed power PDOs uint8_t PD_getPPSNum(void); // Get number of programmable power PDOs uint16_t PD_getPDOVoltage(uint8_t pdonum); // Get voltage of specified fixed power PDO uint16_t PD_getPDOMinVoltage(uint8_t pdonum); // Get minimum voltage of specified PDO uint16_t PD_getPDOMaxVoltage(uint8_t pdonum); // Get maximum voltage of specified PDO uint16_t PD_getPDOMaxCurrent(uint8_t pdonum); // Get max current of specified PDO uint8_t PD_getPDO(void); // Get active PDO uint16_t PD_getVoltage(void); // Get active voltage uint16_t PD_getCurrent(void); // Get active max current uint8_t PD_setPDO(uint8_t pdonum, uint16_t voltage); // Set specified PDO and voltage #ifdef __cplusplus } #endif