├── .github ├── ISSUE_TEMPLATE.md └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── 25.mjpeg ├── 25.txt ├── CHANGELOG.md ├── CMakeLists.txt ├── LICENSE ├── README.md ├── cmake ├── CMakeLists.txt ├── README.md ├── common.cmake ├── compile-flags.cmake ├── dump-config.cmake ├── executable.cmake ├── fix-9985.cmake ├── macros.cmake ├── macros.internal.cmake └── toolchain.cmake ├── demo └── IMG_20191104_125231.jpg ├── kendryte-package.json ├── lds └── kendryte.ld ├── lib ├── CMakeLists.txt ├── bsp │ ├── crt.S │ ├── entry.c │ ├── entry_user.c │ ├── include │ │ ├── atomic.h │ │ ├── bsp.h │ │ ├── dump.h │ │ ├── encoding.h │ │ ├── entry.h │ │ ├── interrupt.h │ │ ├── platform.h │ │ ├── printf.h │ │ ├── sleep.h │ │ ├── syscalls.h │ │ └── util.h │ ├── interrupt.c │ ├── locks.c │ ├── printf.c │ ├── sleep.c │ └── syscalls.c ├── drivers │ ├── aes.c │ ├── apu.c │ ├── clint.c │ ├── dmac.c │ ├── dvp.c │ ├── fft.c │ ├── fpioa.c │ ├── gpio.c │ ├── gpiohs.c │ ├── i2c.c │ ├── i2s.c │ ├── include │ │ ├── aes.h │ │ ├── apu.h │ │ ├── clint.h │ │ ├── dmac.h │ │ ├── dvp.h │ │ ├── fft.h │ │ ├── fpioa.h │ │ ├── gpio.h │ │ ├── gpio_common.h │ │ ├── gpiohs.h │ │ ├── i2c.h │ │ ├── i2s.h │ │ ├── io.h │ │ ├── kpu.h │ │ ├── plic.h │ │ ├── pwm.h │ │ ├── rtc.h │ │ ├── sha256.h │ │ ├── spi.h │ │ ├── sysctl.h │ │ ├── timer.h │ │ ├── uart.h │ │ ├── uarths.h │ │ ├── utils.h │ │ └── wdt.h │ ├── kpu.c │ ├── plic.c │ ├── pwm.c │ ├── rtc.c │ ├── sha256.c │ ├── spi.c │ ├── sysctl.c │ ├── timer.c │ ├── uart.c │ ├── uarths.c │ ├── utils.c │ └── wdt.c └── utils │ └── include │ └── syslog.h ├── playback_gui.py ├── read_record.py └── src ├── .gitignore └── yolo3_frame_test_public_maixpy ├── README.md ├── aiimg.h ├── bitstr.c ├── bitstr.h ├── bmp.c ├── bmp.h ├── board_config.h ├── ccsbcs.c ├── color.c ├── color.h ├── dct.c ├── dct.h ├── diskio.c ├── diskio.h ├── ff.c ├── ff.h ├── ffconf.h ├── font.h ├── huffman.c ├── huffman.h ├── image_process.c ├── image_process.h ├── incbin.h ├── integer.h ├── jfif.c ├── jfif.h ├── kfpkg ├── dog.jpg ├── flash-list.json ├── kpu_yolov3.kfpkg └── mkaiimg.py ├── lcd.c ├── lcd.h ├── main.c ├── nt35310.c ├── nt35310.h ├── ov2640.c ├── ov2640.h ├── ov5640.c ├── ov5640.h ├── ov5640cfg.h ├── quant.c ├── quant.h ├── region_layer.c ├── region_layer.h ├── rgb2bmp.c ├── rgb2bmp.h ├── sdcard.c ├── sdcard.h ├── stdefine.h ├── w25qxx.c ├── w25qxx.h ├── yolo3_frame_test_public_maixpy ├── zigzag.c └── zigzag.h /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Make sure you read and understand this document. 2 | Use one of the two templates below and delete the rest. 3 | 4 | - [ ] BUG REPORT 5 | - [ ] FEATURE REQUEST 6 | 7 | ------------------------------------------------------------------------------- 8 | > BUG REPORT 9 | 10 | ### Expected behavior 11 | 12 | ### Actual behavior 13 | 14 | ### Test code 15 | 16 | Provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) which will reproduce the problem. 17 | 18 | ```bash 19 | # Add code here 20 | ``` 21 | 22 | ### Document version 23 | 24 | Which branch are you on? If you know the Git revision then add it here as well. 25 | 26 | ### Hardware 27 | 28 | Which hardware do you use? 29 | 30 | ### System 31 | 32 | What system do you use? 33 | 34 | ------------------------------------------------------------------------------- 35 | > FEATURE REQUEST 36 | 37 | ### Missing feature 38 | 39 | ### Justification 40 | 41 | Tell us why you would like to see this feature added. 42 | 43 | ### Workarounds 44 | 45 | Are there any workarounds you currently have in place because the feature is missing? 46 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | Fixes #\. 2 | 3 | Make sure all boxes are checked (add x inside the brackets) when you submit your contribution, remove this sentence before doing so. 4 | 5 | - [ ] I have thoroughly tested my contribution. 6 | - [ ] The code I submitted has no copyright issues. 7 | 8 | \ -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: cpp 3 | git: 4 | submodules: false 5 | addons: 6 | apt: 7 | packages: 8 | - python 9 | - python-pip 10 | - git 11 | - wget 12 | - make 13 | - cmake 14 | - libncurses-dev 15 | - flex 16 | - bison 17 | - gperf 18 | - aria2 19 | cache: 20 | - pip 21 | - directories: 22 | - cache 23 | before_install: 24 | - export TOOLCHAIN_URL=https://github.com/kendryte/kendryte-gnu-toolchain/releases/download/v8.2.0-20190213/kendryte-toolchain-ubuntu-amd64-8.2.0-20190213.tar.gz 25 | - export TOOLCHAIN=${TOOLCHAIN_URL##*/} 26 | - | 27 | if [ ! -f $TRAVIS_BUILD_DIR/cache/$TOOLCHAIN ]; then 28 | echo "Download toolchain ..." 29 | aria2c $TOOLCHAIN_URL -d $TRAVIS_BUILD_DIR/cache -o $TOOLCHAIN 30 | else 31 | echo "Toolchain is ready download in cache" 32 | fi 33 | - | 34 | if [ ! -d $TRAVIS_BUILD_DIR/cache/kendryte-toolchain ]; then 35 | echo "Extract toolchain ..." 36 | tar -C $TRAVIS_BUILD_DIR/cache -zxvf $TRAVIS_BUILD_DIR/cache/$TOOLCHAIN 37 | else 38 | echo "Toolchain is ready extract in cache" 39 | fi 40 | install: 41 | - export PATH=$TRAVIS_BUILD_DIR/cache/kendryte-toolchain/bin:$PATH 42 | - export LD_LIBRARY_PATH=$TRAVIS_BUILD_DIR/cache/kendryte-toolchain/bin:$LD_LIBRARY_PATH 43 | - | 44 | cd $TRAVIS_BUILD_DIR 45 | export BRANCH=$(if [ "$TRAVIS_BRANCH" == "master" ]; then echo $TRAVIS_BRANCH; else echo "develop"; fi) 46 | git clone --single-branch --branch $BRANCH https://github.com/kendryte/kendryte-standalone-demo.git 47 | echo "Build branch $BRANCH" 48 | 49 | script: 50 | - export BUILD_DATE=$(date +%Y%m%d) 51 | ## Copy all example code 52 | - | 53 | cd $TRAVIS_BUILD_DIR/src 54 | cp -rf $TRAVIS_BUILD_DIR/kendryte-standalone-demo/* . 55 | mv $TRAVIS_BUILD_DIR/kendryte-standalone-demo ~/demo 56 | ## Check example code 57 | - | 58 | for DIR in $(ls $TRAVIS_BUILD_DIR/src) 59 | do 60 | echo "[MAKE]" $TRAVIS_BUILD_DIR/src/$DIR 61 | cd $TRAVIS_BUILD_DIR/src/$DIR 62 | if [ ! -f "Makefile" ]; then 63 | COUNT=$(expr $(ls -1 *.c 2>/dev/null | wc -l) + $(ls -1 *.cpp 2>/dev/null | wc -l) + $(ls -1 *.S 2>/dev/null | wc -l)) 64 | if [ $COUNT -gt 0 ]; then 65 | mkdir build && cd build 66 | cmake -DPROJ=$DIR -DTOOLCHAIN=$TRAVIS_BUILD_DIR/cache/kendryte-toolchain/bin ../../../ || exit -1 67 | make || exit -1 68 | else 69 | echo "[IGNORE]" $TRAVIS_BUILD_DIR/src/$DIR 70 | fi 71 | else 72 | make || exit -1 73 | fi 74 | done 75 | -------------------------------------------------------------------------------- /25.mjpeg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/25.mjpeg -------------------------------------------------------------------------------- /25.txt: -------------------------------------------------------------------------------- 1 | T 1624 0 2 | 108 38 130 80 0.792225 3 | 133 5 154 40 0.998932 4 | 158 1 174 47 0.999492 5 | 64 50 84 90 0.990146 6 | 107 44 139 90 0.766683 7 | T 1983 5000 8 | 139 0 155 45 0.993670 9 | 145 0 164 42 0.889135 10 | 138 2 159 32 0.996981 11 | 143 5 170 35 0.815642 12 | 159 4 183 39 0.998332 13 | 116 35 133 81 0.982280 14 | 70 42 88 88 0.996498 15 | T 2125 9865 16 | 120 0 142 20 0.766683 17 | 127 1 153 21 0.609718 18 | 93 14 117 60 0.836964 19 | 47 17 68 71 0.963442 20 | T 2259 14689 21 | 121 0 140 41 0.766683 22 | 127 0 152 42 0.856254 23 | 143 0 164 39 0.677771 24 | 118 3 146 32 0.856254 25 | 132 5 164 31 0.998332 26 | 51 41 75 81 0.968334 27 | 93 30 126 83 0.926086 28 | T 2392 19475 29 | 133 4 148 51 0.999410 30 | 138 5 155 51 0.709356 31 | 150 9 171 55 0.995939 32 | 64 52 82 106 0.998932 33 | 109 51 127 97 0.944038 34 | 111 61 149 107 0.873606 35 | T 2525 24110 36 | 152 26 168 83 0.999720 37 | 33 4 71 30 0.968334 38 | 133 28 150 67 0.999562 39 | 108 70 135 116 0.915240 40 | 67 73 85 119 0.994540 41 | 107 79 151 125 0.766683 42 | T 2685 28956 43 | 46 8 84 37 0.986778 44 | 136 36 154 75 0.999945 45 | 157 39 174 79 0.999937 46 | 116 76 133 122 0.873606 47 | 73 82 89 128 0.996498 48 | 118 82 146 129 0.677771 49 | T 2825 33817 50 | 165 55 184 112 0.999915 51 | 59 30 91 59 0.988585 52 | 145 60 163 99 0.999410 53 | 81 102 99 148 0.999792 54 | 124 97 141 143 0.988585 55 | T 2959 38726 56 | 124 5 134 32 0.951395 57 | 150 64 167 121 0.999885 58 | 169 71 183 120 0.999410 59 | 83 110 102 159 0.998562 60 | 127 101 143 158 0.986778 61 | 69 39 93 73 0.972590 62 | T 3141 43924 63 | 130 17 139 48 0.997756 64 | 67 53 86 95 0.766683 65 | 154 78 170 135 0.999988 66 | 172 82 191 139 0.999758 67 | 89 122 103 178 0.992663 68 | 72 55 96 89 0.986778 69 | 132 122 149 168 0.976287 70 | T 3270 49074 71 | 126 30 136 61 0.990146 72 | 42 58 59 94 0.709356 73 | 66 58 85 107 0.739037 74 | 150 94 169 143 0.999675 75 | 171 99 185 141 0.999206 76 | 71 66 95 96 0.998932 77 | 86 139 104 178 0.998562 78 | 127 136 148 176 0.935642 79 | T 3400 54428 80 | 123 35 136 66 0.766683 81 | 59 72 81 108 0.836964 82 | 149 95 168 152 0.999953 83 | 169 98 185 155 0.999492 84 | 126 137 142 179 0.986778 85 | 85 139 101 188 0.990146 86 | 68 71 95 105 0.957828 87 | 151 106 171 141 0.915240 88 | 164 109 185 143 0.644474 89 | T 3566 59769 90 | 126 44 138 71 0.957828 91 | 41 73 60 110 0.766683 92 | 62 80 87 117 0.766683 93 | 151 106 170 155 0.999953 94 | 170 110 189 159 0.999792 95 | 127 144 146 186 0.915240 96 | 69 81 97 110 0.992663 97 | 90 148 105 195 0.915240 98 | T 3695 65198 99 | 125 50 134 77 0.935642 100 | 40 77 56 108 0.836964 101 | 61 84 80 116 0.677771 102 | 70 79 92 122 0.991496 103 | 148 110 169 159 0.999885 104 | 169 112 185 161 0.999885 105 | 84 152 103 195 0.984691 106 | 125 152 149 192 0.991496 107 | T 3827 70712 108 | 126 54 135 85 0.963442 109 | 62 88 78 124 0.815642 110 | 67 86 84 128 0.815642 111 | 150 113 169 170 0.999821 112 | 170 123 186 165 0.999410 113 | 84 157 103 206 0.996498 114 | 70 89 94 123 0.984691 115 | 128 160 148 200 0.990146 116 | T 3965 76115 117 | 130 68 139 95 0.997756 118 | 67 100 86 136 0.677771 119 | 153 123 169 180 0.999492 120 | 173 126 192 183 0.999937 121 | 129 158 145 215 0.926086 122 | 70 104 97 134 0.991496 123 | 174 140 194 170 0.856254 124 | 90 167 105 221 0.968334 125 | T 4128 81586 126 | 154 79 163 106 0.766683 127 | 133 80 142 111 0.972590 128 | 49 103 65 145 0.944038 129 | 69 113 87 149 0.677771 130 | 157 139 176 188 0.999991 131 | 177 147 193 189 0.999410 132 | 134 179 150 215 0.986778 133 | 94 181 108 224 0.999079 134 | 77 115 101 145 0.994540 135 | T 4327 87330 136 | 158 97 167 124 0.677771 137 | 138 99 146 126 0.996498 138 | 54 126 68 153 0.915240 139 | 75 130 91 166 0.902968 140 | 83 128 102 164 0.998562 141 | 160 156 178 205 0.999974 142 | 179 160 198 209 0.999821 143 | 91 193 112 224 0.972590 144 | 133 195 158 222 0.984691 145 | T 4468 93203 146 | 140 107 147 134 0.994540 147 | 159 105 167 136 0.792225 148 | 55 132 69 175 0.815642 149 | 74 139 90 176 0.979497 150 | 85 138 101 174 0.998065 151 | 162 166 178 215 0.999953 152 | 181 168 200 217 0.999959 153 | 134 207 156 222 0.996498 154 | T 4600 99253 155 | 157 112 166 139 0.836964 156 | 138 114 147 141 0.984691 157 | 50 135 69 177 0.972590 158 | 71 144 93 180 0.968334 159 | 82 143 101 179 0.984691 160 | 160 171 179 220 0.999937 161 | 180 174 199 223 0.999622 162 | 89 212 111 220 0.677771 163 | 133 210 158 221 0.957828 164 | T 4768 105494 165 | 140 119 147 146 0.951395 166 | 160 117 169 144 0.766683 167 | 57 143 69 179 0.856254 168 | 75 152 89 179 0.935642 169 | 86 148 102 184 0.996498 170 | 163 178 180 220 0.999079 171 | 180 179 202 228 0.999316 172 | T 4904 111798 173 | 139 128 148 155 0.915240 174 | 150 129 160 152 0.609718 175 | 161 126 170 153 0.836964 176 | 57 149 69 191 0.739037 177 | 77 160 91 187 0.926086 178 | 86 155 102 192 0.972590 179 | 164 185 180 227 0.999867 180 | 178 192 204 223 0.997397 181 | T 5036 118149 182 | 137 134 146 157 0.992663 183 | 157 131 166 158 0.889135 184 | 51 160 65 183 0.609718 185 | 74 162 93 198 0.815642 186 | 81 159 100 202 0.996981 187 | 161 192 177 224 0.999562 188 | 179 196 198 223 0.999792 189 | T 5181 124573 190 | 134 138 142 165 0.976287 191 | 73 170 83 190 0.792225 192 | 83 166 97 203 0.968334 193 | 158 196 177 223 0.999206 194 | 175 202 196 225 0.999410 195 | T 5358 130982 196 | 134 142 143 166 0.995939 197 | 51 166 63 193 0.739037 198 | 72 173 84 200 0.644474 199 | 78 168 90 205 0.644474 200 | 82 169 96 206 0.998932 201 | 156 197 178 224 0.999845 202 | 175 205 196 225 0.999562 203 | T 5561 137525 204 | 140 149 147 172 0.976287 205 | 74 177 91 213 0.915240 206 | 86 172 102 214 0.995939 207 | 160 208 181 223 0.995291 208 | 179 210 201 221 0.988585 209 | 52 174 80 220 0.984691 210 | T 5700 144104 211 | 141 153 150 176 0.992663 212 | 162 150 171 174 0.856254 213 | 76 179 95 215 0.739037 214 | 85 176 107 218 0.976287 215 | 161 210 186 221 0.972590 216 | 178 211 203 222 0.935642 217 | 56 175 80 221 0.951395 218 | T 5838 150840 219 | 142 151 151 174 0.993670 220 | 87 178 106 214 0.963442 221 | 159 211 189 221 0.951395 222 | 180 211 210 221 0.976287 223 | 54 174 78 220 0.889135 224 | T 6016 157631 225 | 147 151 156 174 0.609718 226 | 91 179 110 216 0.979497 227 | 168 210 193 221 0.951395 228 | 185 212 210 221 0.709356 229 | 55 174 83 221 0.739037 230 | T 6154 164180 231 | 158 162 166 189 0.986778 232 | 69 183 83 226 0.963442 233 | 101 189 118 225 0.982280 234 | T 6294 170971 235 | 163 173 171 200 0.766683 236 | 67 196 89 223 0.951395 237 | 99 203 121 223 0.951395 238 | T 6434 177806 239 | T 6629 184849 240 | 171 196 183 223 0.677771 241 | T 6825 192068 242 | 165 200 178 223 0.644474 243 | T 6967 199262 244 | T 7142 206198 245 | T 7284 213040 246 | T 7429 220114 247 | T 7609 227206 248 | T 7744 232983 249 | T 7863 236943 250 | T 8027 240031 251 | T 8161 242704 252 | T 8294 245491 253 | T 8449 248472 254 | T 8578 251413 255 | T 8695 254205 256 | T 8828 257281 257 | T 8961 260538 258 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | Changelog for Kendryte K210 2 | ====== 3 | 4 | ## 0.1.0 5 | 6 | Kendryte K210 first SDK with FreeRTOS, have fun. 7 | 8 | ## 0.2.0 9 | 10 | - Major changes 11 | - Rework trap handling 12 | - New functions to enable spi0 and dvp pin 13 | - New functions to select IO power mode 14 | - Breaking changes 15 | - Modify struct enum union format 16 | - Non-breaking bug fixes 17 | - Fix spi lcd unwork issues 18 | - Fix dual core startup issues 19 | - Use "__global_pointer$" instead of "_gp" 20 | 21 | ## 0.3.0 22 | 23 | - Major change 24 | - Modify AES、FFT、SHA、I2C、SPI、WDT、SPI driver 25 | - Breaking changes 26 | - Modify struct enum union format 27 | - Non-breaking bug fixes 28 | - Fix out of memory issues 29 | - Fix lcd unused issues 30 | 31 | ## 0.4.1 32 | 33 | - Major change 34 | - Add dma support for aes driver 35 | - Add uarths driver 36 | - Add dma interrupt handler 37 | 38 | - Non-breaking bug fixes 39 | - Fix the procedure of setting pll 40 | - Fix wdt interrupt bug 41 | - Fix serveral bugs in i2s drivers 42 | 43 | ## 0.5.0 44 | 45 | - Major change 46 | - Add KPU driver 47 | - Find toolchain automatically 48 | 49 | - Non-breaking bug fixes 50 | - Fix aes gcm bug 51 | - Fix dmac interrupt bug 52 | - Fix i2s transfer bug 53 | 54 | ## 0.5.1 55 | 56 | - Major changes 57 | - Add i2c slave driver 58 | 59 | - Non-breaking bug fixes 60 | - Fix pll init issues 61 | - Fix spi receive mode issues 62 | - Fix redefine function does not report error issues 63 | - Reduce stack size 64 | 65 | ## 0.5.2 66 | 67 | - Major change 68 | - Add KPU driver for latest model compiler 69 | - Automatic set PROJ if user not set it 70 | - Update timer driver to support better interrupt 71 | - Add uart dma and interrupt function 72 | - Non-breaking bug fixes 73 | - Fix rtc issues 74 | - Fix sccb issues 75 | 76 | - Breaking change 77 | - Fix timer interrupt lost problem 78 | - Add new timer interrupt API 79 | 80 | ## 0.5.3 81 | 82 | - Major change 83 | - Modify KPU driver for latest model compiler 84 | - Add freertos 85 | - Add new gpiohs and wdt interrupt function 86 | - Add dvp xclk setting 87 | - Add sysctl reset status 88 | 89 | - Non-breaking bug fixes 90 | - Fix i2c issues 91 | - Fix spi issues 92 | 93 | - Breaking change 94 | - Fix uarths stopbit problem 95 | - Fix core1 stack problem 96 | - Fix core1 interrupt problem 97 | 98 | ## 0.5.4 99 | 100 | - Major change 101 | - Modify KPU driver for NNCASE 102 | - Add APU driver 103 | - Add support for new toolchain 104 | - UART use shadow regs 105 | - Add spi slave driver 106 | - Add travis CI script 107 | 108 | - Non-breaking bug fixes 109 | - Fix float issues 110 | 111 | - Breaking change 112 | - Fix bus reset problem 113 | 114 | ## 0.5.5 115 | 116 | - Major change 117 | - Add SPI I2C I2S UART DMA callback 118 | - Add malloc lock 119 | - Update WIN32 cmake program auto-set 120 | - Update KPU driver for lastest NNCASE 121 | 122 | - Non-breaking bug fixes 123 | - Fix double issues 124 | - Fix GPIO issues 125 | 126 | - Breaking change 127 | - Fix device reset problem 128 | 129 | ## 0.5.6 130 | 131 | - Major change 132 | 133 | - Add irda rs485 134 | - Add rtc tick interrupt handler 135 | - Add rtc alarm interrupt handler 136 | - Modify system default print uart 137 | - Update KPU driver for lastest NNCASE 138 | - Delete freertos 139 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ### This file is used for build example projects. 2 | 3 | # set this will supress some warnings 4 | set(BUILDING_SDK "yes" CACHE INTERNAL "") 5 | 6 | # basic config 7 | if (NOT PROJ) 8 | get_filename_component(PROJ ${CMAKE_CURRENT_BINARY_DIR} DIRECTORY) 9 | get_filename_component(PROJ ${PROJ} NAME) 10 | string(REPLACE " " "_" PROJ ${PROJ}) 11 | message(STATUS "PROJ not set, use ${PROJ} as PROJ. Also, you can set it manually. e.g. -DPROJ=hello_world") 12 | else() 13 | message("PROJ = ${PROJ}") 14 | endif () 15 | cmake_minimum_required(VERSION 3.0) 16 | include(./cmake/common.cmake) 17 | project(${PROJ} C CXX ASM) 18 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3") 19 | set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3") 20 | 21 | # config self use headers 22 | include(./cmake/macros.internal.cmake) 23 | header_directories(${SDK_ROOT}/lib) 24 | header_directories(src/${PROJ}) 25 | header_directories(kendryte-standalone-demo/${PROJ}) 26 | # build library first 27 | add_subdirectory(lib) 28 | 29 | # compile project 30 | add_source_files(src/${PROJ}/*.c src/${PROJ}/*.s src/${PROJ}/*.S src/${PROJ}/*.cpp) 31 | add_source_files(kendryte-standalone-demo/${PROJ}/*.c kendryte-standalone-demo/${PROJ}/*.s kendryte-standalone-demo/${PROJ}/*.S kendryte-standalone-demo/${PROJ}/*.cpp) 32 | include(./cmake/executable.cmake) 33 | 34 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Kendryte K210 Animal Tracking 2 | 3 | Kendryte Standalone SDK 4 | 5 | ## Demo 6 | 7 | ![demo](demo/IMG_20191104_125231.jpg) 8 | 9 | ## Analysis tool 10 | 11 | Now available [TrackingAnalyze](https://github.com/zyayoung/TrackingAnalyze) 12 | ![](https://raw.githubusercontent.com/zyayoung/TrackingAnalyze/master/demo/demo2.png) 13 | 14 | ## Train 15 | 16 | Train yolo model using [darknet](https://pjreddie.com/darknet/yolo/) or this [repo](https://gitee.com/zyayoung/keras-yolo3/tree/rat/). 17 | 18 | To conver your keras model to kmodel, [MaixPy_scripts](https://github.com/sipeed/MaixPy_scripts) or [nncase](https://github.com/kendryte/nncase/tree/v0.1.0-rc5) can be used. 19 | 20 | First prepare your `yolo.h5` file, then 21 | ```bash 22 | tflite_convert --keras_model_file=yolo.h5 --output_file=yolo.tflite 23 | ./tflite2kmodel.sh yolo.tflite 24 | ``` 25 | 26 | ## Usage 27 | 28 | ### Compile 29 | 30 | - Ubuntu 31 | 32 | Download `RISC-V 64bit toolchain for Kendryte K210_ubuntu_amd64` from https://kendryte.com/downloads/. 33 | 34 | Extract it to /opt/riscv-toolchain, and add `export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/riscv-toolchain/bin/` to `bashrc`. 35 | 36 | ```bash 37 | mkdir build && cd build 38 | cmake .. -DPROJ=yolo3_frame_test_public_maixpy -DTOOLCHAIN=/opt/riscv-toolchain/bin && make 39 | ``` 40 | 41 | - Windows 42 | 43 | Download and install CMake and the latest toolchain. 44 | 45 | ```powershell 46 | mkdir build && cd build 47 | cmake -G "MinGW Makefiles" .. -DPROJ=yolo3_frame_test_public_maixpy -DTOOLCHAIN=/path/to/toolchain/bin 48 | make 49 | ``` 50 | 51 | ### flash 52 | 53 | You will get `yolo3_frame_test_public_maixpy.bin`. 54 | 55 | If you want to flash it in UOG, using `yolo3_frame_test_public_maixpy.bin`, then using flash-tool(s) burn it to your flash. 56 | 57 | ```bash 58 | sudo kflash yolo3_frame_test_public_maixpy.bin -B dockE -p /dev/ttyUSB0 -b 3000000 -t 59 | ``` 60 | 61 | You may want to flash your bin and model toghther with kfpkg 62 | 63 | ```bash 64 | cp ../src/yolo3_frame_test_public_maixpy/kfpkg/kpu_yolov3.kfpkg . 65 | zip kpu_yolov3.kfpkg yolo.kmodel yolo3_frame_test_public_maixpy.bin 66 | sudo kflash kpu_yolov3.kfpkg -B dockE -p /dev/ttyUSB0 -b 3000000 -t 67 | ``` 68 | 69 | ## Credit 70 | 71 | - [Yolov3](https://pjreddie.com/darknet/yolo/) 72 | - [kendryte-standalone-sdk](https://github.com/kendryte/kendryte-standalone-sdk) 73 | - [keras-yolo3](https://github.com/qqwweee/keras-yolo3) 74 | - [K210_Yolo_framework](https://github.com/zhen8838/K210_Yolo_framework) 75 | - M. Lorbach, E. I. Kyriakou, R. Poppe, E. A. van Dam, L. P. J. J. Noldus, and R. C. Veltkamp, “Learning to Recognize Rat Social Behavior: Novel Dataset and Cross-Dataset Application,” Journal of Neuroscience Methods, 2017. [data](https://www.noldus.com/projects/phenorat/datasets/ratsi) 76 | - [MaixPy_scripts](https://github.com/sipeed/MaixPy_scripts) 77 | -------------------------------------------------------------------------------- /cmake/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | ### This file is used for build library standalone. 2 | 3 | # set this will supress some warnings 4 | set(BUILDING_SDK "yes" CACHE INTERNAL "") 5 | 6 | # basic config 7 | cmake_minimum_required(VERSION 3.0) 8 | include(./common.cmake) 9 | project(kendryte) 10 | 11 | # config self use headers 12 | include(./macros.internal.cmake) 13 | header_directories(${SDK_ROOT}/lib) 14 | 15 | # include lib make file 16 | include(../lib/CMakeLists.txt) 17 | 18 | # find headers files to INSTALL 19 | file(GLOB_RECURSE LIB_HEADERS 20 | "../lib/*.h" 21 | "../lib/*.hpp" 22 | ) 23 | set_target_properties(kendryte PROPERTIES PUBLIC_HEADER "${LIB_HEADERS}") 24 | 25 | # copy .a file and headers 26 | install(TARGETS kendryte 27 | EXPORT kendryte 28 | ARCHIVE 29 | DESTINATION ${CMAKE_BINARY_DIR}/archive 30 | PUBLIC_HEADER DESTINATION ${CMAKE_BINARY_DIR}/archive/include 31 | ) 32 | 33 | # copy utils files 34 | install(DIRECTORY 35 | ../lds 36 | ../utils 37 | ../cmake 38 | DESTINATION ${CMAKE_BINARY_DIR}/archive 39 | PATTERN "*internal*" EXCLUDE 40 | PATTERN "CMakeLists.txt" EXCLUDE 41 | ) 42 | 43 | # show information 44 | include(./dump-config.cmake) 45 | -------------------------------------------------------------------------------- /cmake/README.md: -------------------------------------------------------------------------------- 1 | prepend `common.cmake` before 2 | 3 | append `executable.cmake` after 4 | -------------------------------------------------------------------------------- /cmake/common.cmake: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.0) 2 | 3 | include(${CMAKE_CURRENT_LIST_DIR}/macros.cmake) 4 | 5 | global_set(CMAKE_C_COMPILER_WORKS 1) 6 | global_set(CMAKE_CXX_COMPILER_WORKS 1) 7 | 8 | global_set(CMAKE_SYSTEM_NAME "Generic") 9 | if (NOT CMAKE_BUILD_TYPE) 10 | global_set(CMAKE_BUILD_TYPE Debug) 11 | else () 12 | if ((NOT CMAKE_BUILD_TYPE STREQUAL "Debug") AND (NOT CMAKE_BUILD_TYPE STREQUAL "Release")) 13 | message(FATAL_ERROR "CMAKE_BUILD_TYPE must either be Debug or Release instead of ${CMAKE_BUILD_TYPE}") 14 | endif () 15 | endif () 16 | 17 | # - Debug & Release 18 | IF (CMAKE_BUILD_TYPE STREQUAL Debug) 19 | add_definitions(-DDEBUG=1) 20 | ENDIF () 21 | 22 | # definitions in macros 23 | add_definitions(-DCONFIG_LOG_LEVEL=LOG_VERBOSE -DCONFIG_LOG_ENABLE -DCONFIG_LOG_COLORS -DLOG_KERNEL -D__riscv64 -DLV_CONF_INCLUDE_SIMPLE) 24 | 25 | if (NOT SDK_ROOT) 26 | get_filename_component(_SDK_ROOT ${CMAKE_CURRENT_LIST_DIR} DIRECTORY) 27 | global_set(SDK_ROOT ${_SDK_ROOT}) 28 | endif () 29 | 30 | include(${CMAKE_CURRENT_LIST_DIR}/toolchain.cmake) 31 | 32 | include(${CMAKE_CURRENT_LIST_DIR}/compile-flags.cmake) 33 | 34 | include(${CMAKE_CURRENT_LIST_DIR}/fix-9985.cmake) 35 | -------------------------------------------------------------------------------- /cmake/compile-flags.cmake: -------------------------------------------------------------------------------- 1 | add_compile_flags(LD 2 | -nostartfiles 3 | -static 4 | -Wl,--gc-sections 5 | -Wl,-static 6 | -Wl,--start-group 7 | -Wl,--whole-archive 8 | -Wl,--no-whole-archive 9 | -Wl,--end-group 10 | -Wl,-EL 11 | -Wl,--no-relax 12 | -T ${SDK_ROOT}/lds/kendryte.ld 13 | ) 14 | 15 | # C Flags Settings 16 | add_compile_flags(BOTH 17 | -mcmodel=medany 18 | -mabi=lp64f 19 | -march=rv64imafc 20 | -fno-common 21 | -ffunction-sections 22 | -fdata-sections 23 | -fstrict-volatile-bitfields 24 | -fno-zero-initialized-in-bss 25 | -ffast-math 26 | -fno-math-errno 27 | -fsingle-precision-constant 28 | -Os 29 | -ggdb 30 | ) 31 | 32 | add_compile_flags(C -std=gnu11 -Wno-pointer-to-int-cast) 33 | add_compile_flags(CXX -std=gnu++17) 34 | 35 | if (BUILDING_SDK) 36 | add_compile_flags(BOTH 37 | -Wall 38 | -Werror=all 39 | -Wno-error=unused-function 40 | -Wno-error=unused-but-set-variable 41 | -Wno-error=unused-variable 42 | -Wno-error=deprecated-declarations 43 | -Wextra 44 | -Werror=frame-larger-than=32768 45 | -Wno-unused-parameter 46 | -Wno-sign-compare 47 | -Wno-error=missing-braces 48 | -Wno-error=return-type 49 | -Wno-error=pointer-sign 50 | -Wno-missing-braces 51 | -Wno-strict-aliasing 52 | -Wno-implicit-fallthrough 53 | -Wno-missing-field-initializers 54 | -Wno-int-to-pointer-cast 55 | -Wno-error=comment 56 | -Wno-error=logical-not-parentheses 57 | -Wno-error=duplicate-decl-specifier 58 | -Wno-error=parentheses 59 | ) 60 | 61 | add_compile_flags(C -Wno-old-style-declaration) 62 | else () 63 | add_compile_flags(BOTH -L${SDK_ROOT}/include/) 64 | endif () 65 | 66 | -------------------------------------------------------------------------------- /cmake/dump-config.cmake: -------------------------------------------------------------------------------- 1 | message("") 2 | message("Project: ${PROJECT_NAME}") 3 | message(" LIST_FILE=${CMAKE_PARENT_LIST_FILE}") 4 | message(" TOOLCHAIN=${TOOLCHAIN}") 5 | message(" KENDRYTE_IDE=${KENDRYTE_IDE}") 6 | message(" BUILDING_SDK=${BUILDING_SDK}") 7 | message("") 8 | message(" CMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}") 9 | message(" CMAKE_C_COMPILER=${CMAKE_C_COMPILER}") 10 | message(" CMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}") 11 | message(" CMAKE_LINKER=${CMAKE_LINKER}") 12 | message(" CMAKE_OBJCOPY=${CMAKE_OBJCOPY}") 13 | message(" CMAKE_OBJDUMP=${CMAKE_OBJDUMP}") 14 | message(" CMAKE_MAKE_PROGRAM=${CMAKE_MAKE_PROGRAM}") 15 | message("") 16 | message(" CMAKE_C_FLAGS=${CMAKE_C_FLAGS}") 17 | message(" CMAKE_CXX_FLAGS=${CMAKE_CXX_FLAGS}") 18 | message(" LDFLAGS=${LDFLAGS}") 19 | message(" CMAKE_BINARY_DIR=${CMAKE_BINARY_DIR}") 20 | message("Makefile created.") 21 | message("") 22 | message("") 23 | -------------------------------------------------------------------------------- /cmake/executable.cmake: -------------------------------------------------------------------------------- 1 | if (NOT BUILDING_SDK) 2 | if(EXISTS ${SDK_ROOT}/libkendryte.a) 3 | add_library(kendryte STATIC IMPORTED) 4 | set_property(TARGET kendryte PROPERTY IMPORTED_LOCATION ${SDK_ROOT}/libkendryte.a) 5 | include_directories(${SDK_ROOT}/include/) 6 | else() 7 | header_directories(${SDK_ROOT}/lib) 8 | add_subdirectory(${SDK_ROOT}/lib) 9 | endif() 10 | endif () 11 | 12 | removeDuplicateSubstring(${CMAKE_C_FLAGS} CMAKE_C_FLAGS) 13 | removeDuplicateSubstring(${CMAKE_CXX_FLAGS} CMAKE_CXX_FLAGS) 14 | 15 | message("SOURCE_FILES=${SOURCE_FILES}") 16 | add_executable(${PROJECT_NAME} ${SOURCE_FILES}) 17 | 18 | 19 | set_target_properties(${PROJECT_NAME} PROPERTIES LINKER_LANGUAGE C) 20 | 21 | target_link_libraries(${PROJECT_NAME} 22 | -Wl,--start-group 23 | gcc m c 24 | -Wl,--whole-archive 25 | kendryte 26 | -Wl,--no-whole-archive 27 | -Wl,--end-group 28 | ) 29 | 30 | if (EXISTS ${SDK_ROOT}/src/${PROJ}/project.cmake) 31 | include(${SDK_ROOT}/src/${PROJ}/project.cmake) 32 | endif () 33 | 34 | IF(SUFFIX) 35 | SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SUFFIX ${SUFFIX}) 36 | ENDIF() 37 | 38 | # Build target 39 | add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD 40 | COMMAND ${CMAKE_OBJCOPY} --output-format=binary ${CMAKE_BINARY_DIR}/${PROJECT_NAME}${SUFFIX} ${CMAKE_BINARY_DIR}/${PROJECT_NAME}.bin 41 | DEPENDS ${PROJECT_NAME} 42 | COMMENT "Generating .bin file ...") 43 | 44 | # show information 45 | include(${CMAKE_CURRENT_LIST_DIR}/dump-config.cmake) 46 | -------------------------------------------------------------------------------- /cmake/fix-9985.cmake: -------------------------------------------------------------------------------- 1 | ### http://www.cmake.org/Bug/view.php?id=9985 2 | string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_C_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_C_FLAGS}") 3 | string(REPLACE "-rdynamic" "" CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS "${CMAKE_SHARED_LIBRARY_LINK_CXX_FLAGS}") 4 | -------------------------------------------------------------------------------- /cmake/macros.cmake: -------------------------------------------------------------------------------- 1 | macro(global_set Name Value) 2 | # message("set ${Name} to " ${ARGN}) 3 | set(${Name} "${Value}" CACHE STRING "NoDesc" FORCE) 4 | endmacro() 5 | 6 | macro(condition_set Name Value) 7 | if (NOT ${Name}) 8 | global_set(${Name} ${Value}) 9 | else () 10 | # message("exists ${Name} is " ${ARGN}) 11 | endif () 12 | endmacro() 13 | 14 | 15 | set(SOURCE_FILES "" CACHE STRING "Source Files" FORCE) 16 | macro(add_source_files) 17 | # message(" + add_source_files ${ARGN}") 18 | file(GLOB_RECURSE newlist ${ARGN}) 19 | 20 | foreach (filepath ${newlist}) 21 | string(FIND ${filepath} ${CMAKE_BINARY_DIR} found) 22 | if (NOT found EQUAL 0) 23 | set(SOURCE_FILES ${SOURCE_FILES} ${filepath} CACHE STRING "Source Files" FORCE) 24 | endif () 25 | endforeach () 26 | endmacro() 27 | 28 | function(JOIN VALUES GLUE OUTPUT) 29 | string(REGEX REPLACE "([^\\]|^);" "\\1${GLUE}" _TMP_STR "${VALUES}") 30 | string(REGEX REPLACE "[\\](.)" "\\1" _TMP_STR "${_TMP_STR}") #fixes escaping 31 | set(${OUTPUT} "${_TMP_STR}" PARENT_SCOPE) 32 | endfunction() 33 | 34 | global_set(LDFLAGS "") 35 | global_set(CMAKE_EXE_LINKER_FLAGS "") 36 | global_set(CMAKE_SHARED_LINKER_FLAGS "") 37 | global_set(CMAKE_MODULE_LINKER_FLAGS "") 38 | 39 | function(removeDuplicateSubstring stringIn stringOut) 40 | separate_arguments(stringIn) 41 | list(REMOVE_DUPLICATES stringIn) 42 | string(REPLACE ";" " " stringIn "${stringIn}") 43 | set(${stringOut} "${stringIn}" PARENT_SCOPE) 44 | endfunction() 45 | 46 | macro(add_compile_flags WHERE) 47 | JOIN("${ARGN}" " " STRING_ARGS) 48 | if (${WHERE} STREQUAL C) 49 | global_set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${STRING_ARGS}") 50 | 51 | elseif (${WHERE} STREQUAL CXX) 52 | global_set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${STRING_ARGS}") 53 | 54 | elseif (${WHERE} STREQUAL LD) 55 | global_set(LDFLAGS "${LDFLAGS} ${STRING_ARGS}") 56 | global_set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${STRING_ARGS}") 57 | global_set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${STRING_ARGS}") 58 | global_set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${STRING_ARGS}") 59 | 60 | elseif (${WHERE} STREQUAL BOTH) 61 | add_compile_flags(C ${ARGN}) 62 | add_compile_flags(CXX ${ARGN}) 63 | 64 | elseif (${WHERE} STREQUAL ALL) 65 | add_compile_flags(C ${ARGN}) 66 | add_compile_flags(CXX ${ARGN}) 67 | add_compile_flags(LD ${ARGN}) 68 | 69 | else () 70 | message(FATAL_ERROR "add_compile_flags - only support: C, CXX, BOTH, LD, ALL") 71 | endif () 72 | endmacro() 73 | -------------------------------------------------------------------------------- /cmake/macros.internal.cmake: -------------------------------------------------------------------------------- 1 | # Add lib headers 2 | macro(header_directories parent) 3 | file(GLOB_RECURSE newList ${parent}/*.h) 4 | set(dir_list "") 5 | foreach (file_path ${newList}) 6 | get_filename_component(dir_path ${file_path} DIRECTORY) 7 | set(dir_list ${dir_list} ${dir_path}) 8 | endforeach () 9 | list(REMOVE_DUPLICATES dir_list) 10 | 11 | include_directories(${dir_list}) 12 | endmacro() -------------------------------------------------------------------------------- /cmake/toolchain.cmake: -------------------------------------------------------------------------------- 1 | if (WIN32) 2 | set(EXT ".exe") 3 | else () 4 | set(EXT "") 5 | endif () 6 | 7 | message(STATUS "Check for RISCV toolchain ...") 8 | if(NOT TOOLCHAIN) 9 | find_path(_TOOLCHAIN riscv64-unknown-elf-gcc${EXT}) 10 | global_set(TOOLCHAIN "${_TOOLCHAIN}") 11 | elseif(NOT "${TOOLCHAIN}" MATCHES "/$") 12 | global_set(TOOLCHAIN "${TOOLCHAIN}") 13 | endif() 14 | 15 | if (NOT TOOLCHAIN) 16 | message(FATAL_ERROR "TOOLCHAIN must be set, to absolute path of kendryte-toolchain dist/bin folder.") 17 | endif () 18 | 19 | message(STATUS "Using ${TOOLCHAIN} RISCV toolchain") 20 | 21 | global_set(CMAKE_C_COMPILER "${TOOLCHAIN}/riscv64-unknown-elf-gcc${EXT}") 22 | global_set(CMAKE_CXX_COMPILER "${TOOLCHAIN}/riscv64-unknown-elf-g++${EXT}") 23 | global_set(CMAKE_LINKER "${TOOLCHAIN}/riscv64-unknown-elf-ld${EXT}") 24 | global_set(CMAKE_AR "${TOOLCHAIN}/riscv64-unknown-elf-ar${EXT}") 25 | global_set(CMAKE_OBJCOPY "${TOOLCHAIN}/riscv64-unknown-elf-objcopy${EXT}") 26 | global_set(CMAKE_SIZE "${TOOLCHAIN}/riscv64-unknown-elf-size${EXT}") 27 | global_set(CMAKE_OBJDUMP "${TOOLCHAIN}/riscv64-unknown-elf-objdump${EXT}") 28 | if (WIN32) 29 | if(EXISTS "${TOOLCHAIN}/make${EXT}") 30 | global_set(CMAKE_MAKE_PROGRAM "${TOOLCHAIN}/make${EXT}") 31 | else() 32 | global_set(CMAKE_MAKE_PROGRAM "${TOOLCHAIN}/mingw32-make${EXT}") 33 | endif() 34 | endif () 35 | 36 | execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crt0.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRT0_OBJ) 37 | execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtbegin.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTBEGIN_OBJ) 38 | execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtend.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTEND_OBJ) 39 | execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crti.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTI_OBJ) 40 | execute_process(COMMAND ${CMAKE_C_COMPILER} -print-file-name=crtn.o OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE CRTN_OBJ) 41 | 42 | global_set(CMAKE_C_LINK_EXECUTABLE 43 | " \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o ") 44 | 45 | global_set(CMAKE_CXX_LINK_EXECUTABLE 46 | " \"${CRTI_OBJ}\" \"${CRTBEGIN_OBJ}\" \"${CRTEND_OBJ}\" \"${CRTN_OBJ}\" -o ") 47 | 48 | get_filename_component(_BIN_DIR "${CMAKE_C_COMPILER}" DIRECTORY) 49 | if (NOT "${TOOLCHAIN}" STREQUAL "${_BIN_DIR}") 50 | message(FATAL_ERROR "CMAKE_C_COMPILER is not in kendryte-toolchain dist/bin folder.") 51 | endif () 52 | -------------------------------------------------------------------------------- /demo/IMG_20191104_125231.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/demo/IMG_20191104_125231.jpg -------------------------------------------------------------------------------- /kendryte-package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "framework-kendryte210-standalone-sdk", 3 | "description": "This SDK is for Kendryte K210 without OS support.", 4 | "version": "0.5.4", 5 | "url": "https://github.com/kendryte/kendryte-standalone-sdk" 6 | } 7 | -------------------------------------------------------------------------------- /lib/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #project(maix_drivers) 2 | 3 | # create driver library 4 | 5 | FILE(GLOB_RECURSE LIB_SRC 6 | "${CMAKE_CURRENT_LIST_DIR}/*.h" 7 | "${CMAKE_CURRENT_LIST_DIR}/*.hpp" 8 | "${CMAKE_CURRENT_LIST_DIR}/*.cpp" 9 | "${CMAKE_CURRENT_LIST_DIR}/*.c" 10 | "${CMAKE_CURRENT_LIST_DIR}/*.s" 11 | "${CMAKE_CURRENT_LIST_DIR}/*.S" 12 | ) 13 | 14 | FILE(GLOB_RECURSE ASSEMBLY_FILES 15 | "${CMAKE_CURRENT_LIST_DIR}/*.s" 16 | "${CMAKE_CURRENT_LIST_DIR}/*.S" 17 | ) 18 | 19 | include_directories(${CMAKE_CURRENT_LIST_DIR}/drivers/include ${CMAKE_CURRENT_LIST_DIR}/bsp/include) 20 | 21 | SET_PROPERTY(SOURCE ${ASSEMBLY_FILES} PROPERTY LANGUAGE C) 22 | SET_SOURCE_FILES_PROPERTIES(${ASSEMBLY_FILES} PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp -D __riscv64") 23 | 24 | ADD_LIBRARY(kendryte 25 | ${LIB_SRC} 26 | ) 27 | SET_TARGET_PROPERTIES(kendryte PROPERTIES LINKER_LANGUAGE C) 28 | -------------------------------------------------------------------------------- /lib/bsp/crt.S: -------------------------------------------------------------------------------- 1 | # Copyright 2018 Canaan Inc. 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | #include "encoding.h" 16 | 17 | # define LREG ld 18 | # define SREG sd 19 | # define LFREG flw 20 | # define SFREG fsw 21 | # define REGBYTES 8 22 | # define STKSHIFT 15 23 | 24 | 25 | .section .text.start, "ax", @progbits 26 | .globl _start 27 | _start: 28 | j 1f 29 | .word 0xdeadbeef 30 | .align 3 31 | .global g_wake_up 32 | g_wake_up: 33 | .dword 1 34 | .dword 0 35 | 1: 36 | csrw mideleg, 0 37 | csrw medeleg, 0 38 | csrw mie, 0 39 | csrw mip, 0 40 | la t0, trap_entry 41 | csrw mtvec, t0 42 | 43 | li x1, 0 44 | li x2, 0 45 | li x3, 0 46 | li x4, 0 47 | li x5, 0 48 | li x6, 0 49 | li x7, 0 50 | li x8, 0 51 | li x9, 0 52 | li x10,0 53 | li x11,0 54 | li x12,0 55 | li x13,0 56 | li x14,0 57 | li x15,0 58 | li x16,0 59 | li x17,0 60 | li x18,0 61 | li x19,0 62 | li x20,0 63 | li x21,0 64 | li x22,0 65 | li x23,0 66 | li x24,0 67 | li x25,0 68 | li x26,0 69 | li x27,0 70 | li x28,0 71 | li x29,0 72 | li x30,0 73 | li x31,0 74 | 75 | li t0, MSTATUS_FS 76 | csrs mstatus, t0 77 | 78 | fssr x0 79 | fmv.w.x f0, x0 80 | fmv.w.x f1, x0 81 | fmv.w.x f2, x0 82 | fmv.w.x f3, x0 83 | fmv.w.x f4, x0 84 | fmv.w.x f5, x0 85 | fmv.w.x f6, x0 86 | fmv.w.x f7, x0 87 | fmv.w.x f8, x0 88 | fmv.w.x f9, x0 89 | fmv.w.x f10,x0 90 | fmv.w.x f11,x0 91 | fmv.w.x f12,x0 92 | fmv.w.x f13,x0 93 | fmv.w.x f14,x0 94 | fmv.w.x f15,x0 95 | fmv.w.x f16,x0 96 | fmv.w.x f17,x0 97 | fmv.w.x f18,x0 98 | fmv.w.x f19,x0 99 | fmv.w.x f20,x0 100 | fmv.w.x f21,x0 101 | fmv.w.x f22,x0 102 | fmv.w.x f23,x0 103 | fmv.w.x f24,x0 104 | fmv.w.x f25,x0 105 | fmv.w.x f26,x0 106 | fmv.w.x f27,x0 107 | fmv.w.x f28,x0 108 | fmv.w.x f29,x0 109 | fmv.w.x f30,x0 110 | fmv.w.x f31,x0 111 | 112 | .option push 113 | .option norelax 114 | la gp, __global_pointer$ 115 | .option pop 116 | la tp, _end + 63 117 | and tp, tp, -64 118 | csrr a0, mhartid 119 | 120 | add sp, a0, 1 121 | sll sp, sp, STKSHIFT 122 | add sp, sp, tp 123 | 124 | j _init_bsp 125 | 126 | .globl trap_entry 127 | .type trap_entry, @function 128 | .align 2 129 | trap_entry: 130 | addi sp, sp, -64*REGBYTES 131 | 132 | SREG x1, 1*REGBYTES(sp) 133 | SREG x2, 2*REGBYTES(sp) 134 | SREG x3, 3*REGBYTES(sp) 135 | SREG x4, 4*REGBYTES(sp) 136 | SREG x5, 5*REGBYTES(sp) 137 | SREG x6, 6*REGBYTES(sp) 138 | SREG x7, 7*REGBYTES(sp) 139 | SREG x8, 8*REGBYTES(sp) 140 | SREG x9, 9*REGBYTES(sp) 141 | SREG x10, 10*REGBYTES(sp) 142 | SREG x11, 11*REGBYTES(sp) 143 | SREG x12, 12*REGBYTES(sp) 144 | SREG x13, 13*REGBYTES(sp) 145 | SREG x14, 14*REGBYTES(sp) 146 | SREG x15, 15*REGBYTES(sp) 147 | SREG x16, 16*REGBYTES(sp) 148 | SREG x17, 17*REGBYTES(sp) 149 | SREG x18, 18*REGBYTES(sp) 150 | SREG x19, 19*REGBYTES(sp) 151 | SREG x20, 20*REGBYTES(sp) 152 | SREG x21, 21*REGBYTES(sp) 153 | SREG x22, 22*REGBYTES(sp) 154 | SREG x23, 23*REGBYTES(sp) 155 | SREG x24, 24*REGBYTES(sp) 156 | SREG x25, 25*REGBYTES(sp) 157 | SREG x26, 26*REGBYTES(sp) 158 | SREG x27, 27*REGBYTES(sp) 159 | SREG x28, 28*REGBYTES(sp) 160 | SREG x29, 29*REGBYTES(sp) 161 | SREG x30, 30*REGBYTES(sp) 162 | SREG x31, 31*REGBYTES(sp) 163 | 164 | SFREG f0, ( 0 + 32)*REGBYTES(sp) 165 | SFREG f1, ( 1 + 32)*REGBYTES(sp) 166 | SFREG f2, ( 2 + 32)*REGBYTES(sp) 167 | SFREG f3, ( 3 + 32)*REGBYTES(sp) 168 | SFREG f4, ( 4 + 32)*REGBYTES(sp) 169 | SFREG f5, ( 5 + 32)*REGBYTES(sp) 170 | SFREG f6, ( 6 + 32)*REGBYTES(sp) 171 | SFREG f7, ( 7 + 32)*REGBYTES(sp) 172 | SFREG f8, ( 8 + 32)*REGBYTES(sp) 173 | SFREG f9, ( 9 + 32)*REGBYTES(sp) 174 | SFREG f10,( 10 + 32)*REGBYTES(sp) 175 | SFREG f11,( 11 + 32)*REGBYTES(sp) 176 | SFREG f12,( 12 + 32)*REGBYTES(sp) 177 | SFREG f13,( 13 + 32)*REGBYTES(sp) 178 | SFREG f14,( 14 + 32)*REGBYTES(sp) 179 | SFREG f15,( 15 + 32)*REGBYTES(sp) 180 | SFREG f16,( 16 + 32)*REGBYTES(sp) 181 | SFREG f17,( 17 + 32)*REGBYTES(sp) 182 | SFREG f18,( 18 + 32)*REGBYTES(sp) 183 | SFREG f19,( 19 + 32)*REGBYTES(sp) 184 | SFREG f20,( 20 + 32)*REGBYTES(sp) 185 | SFREG f21,( 21 + 32)*REGBYTES(sp) 186 | SFREG f22,( 22 + 32)*REGBYTES(sp) 187 | SFREG f23,( 23 + 32)*REGBYTES(sp) 188 | SFREG f24,( 24 + 32)*REGBYTES(sp) 189 | SFREG f25,( 25 + 32)*REGBYTES(sp) 190 | SFREG f26,( 26 + 32)*REGBYTES(sp) 191 | SFREG f27,( 27 + 32)*REGBYTES(sp) 192 | SFREG f28,( 28 + 32)*REGBYTES(sp) 193 | SFREG f29,( 29 + 32)*REGBYTES(sp) 194 | SFREG f30,( 30 + 32)*REGBYTES(sp) 195 | SFREG f31,( 31 + 32)*REGBYTES(sp) 196 | 197 | csrr a0, mcause 198 | csrr a1, mepc 199 | mv a2, sp 200 | add a3, sp, 32*REGBYTES 201 | bgez a0, .handle_syscall 202 | .handle_irq: 203 | jal handle_irq 204 | j .restore 205 | .handle_syscall: 206 | jal handle_syscall 207 | .restore: 208 | csrw mepc, a0 209 | LREG x1, 1*REGBYTES(sp) 210 | LREG x2, 2*REGBYTES(sp) 211 | LREG x3, 3*REGBYTES(sp) 212 | LREG x4, 4*REGBYTES(sp) 213 | LREG x5, 5*REGBYTES(sp) 214 | LREG x6, 6*REGBYTES(sp) 215 | LREG x7, 7*REGBYTES(sp) 216 | LREG x8, 8*REGBYTES(sp) 217 | LREG x9, 9*REGBYTES(sp) 218 | LREG x10, 10*REGBYTES(sp) 219 | LREG x11, 11*REGBYTES(sp) 220 | LREG x12, 12*REGBYTES(sp) 221 | LREG x13, 13*REGBYTES(sp) 222 | LREG x14, 14*REGBYTES(sp) 223 | LREG x15, 15*REGBYTES(sp) 224 | LREG x16, 16*REGBYTES(sp) 225 | LREG x17, 17*REGBYTES(sp) 226 | LREG x18, 18*REGBYTES(sp) 227 | LREG x19, 19*REGBYTES(sp) 228 | LREG x20, 20*REGBYTES(sp) 229 | LREG x21, 21*REGBYTES(sp) 230 | LREG x22, 22*REGBYTES(sp) 231 | LREG x23, 23*REGBYTES(sp) 232 | LREG x24, 24*REGBYTES(sp) 233 | LREG x25, 25*REGBYTES(sp) 234 | LREG x26, 26*REGBYTES(sp) 235 | LREG x27, 27*REGBYTES(sp) 236 | LREG x28, 28*REGBYTES(sp) 237 | LREG x29, 29*REGBYTES(sp) 238 | LREG x30, 30*REGBYTES(sp) 239 | LREG x31, 31*REGBYTES(sp) 240 | 241 | LFREG f0, ( 0 + 32)*REGBYTES(sp) 242 | LFREG f1, ( 1 + 32)*REGBYTES(sp) 243 | LFREG f2, ( 2 + 32)*REGBYTES(sp) 244 | LFREG f3, ( 3 + 32)*REGBYTES(sp) 245 | LFREG f4, ( 4 + 32)*REGBYTES(sp) 246 | LFREG f5, ( 5 + 32)*REGBYTES(sp) 247 | LFREG f6, ( 6 + 32)*REGBYTES(sp) 248 | LFREG f7, ( 7 + 32)*REGBYTES(sp) 249 | LFREG f8, ( 8 + 32)*REGBYTES(sp) 250 | LFREG f9, ( 9 + 32)*REGBYTES(sp) 251 | LFREG f10,( 10 + 32)*REGBYTES(sp) 252 | LFREG f11,( 11 + 32)*REGBYTES(sp) 253 | LFREG f12,( 12 + 32)*REGBYTES(sp) 254 | LFREG f13,( 13 + 32)*REGBYTES(sp) 255 | LFREG f14,( 14 + 32)*REGBYTES(sp) 256 | LFREG f15,( 15 + 32)*REGBYTES(sp) 257 | LFREG f16,( 16 + 32)*REGBYTES(sp) 258 | LFREG f17,( 17 + 32)*REGBYTES(sp) 259 | LFREG f18,( 18 + 32)*REGBYTES(sp) 260 | LFREG f19,( 19 + 32)*REGBYTES(sp) 261 | LFREG f20,( 20 + 32)*REGBYTES(sp) 262 | LFREG f21,( 21 + 32)*REGBYTES(sp) 263 | LFREG f22,( 22 + 32)*REGBYTES(sp) 264 | LFREG f23,( 23 + 32)*REGBYTES(sp) 265 | LFREG f24,( 24 + 32)*REGBYTES(sp) 266 | LFREG f25,( 25 + 32)*REGBYTES(sp) 267 | LFREG f26,( 26 + 32)*REGBYTES(sp) 268 | LFREG f27,( 27 + 32)*REGBYTES(sp) 269 | LFREG f28,( 28 + 32)*REGBYTES(sp) 270 | LFREG f29,( 29 + 32)*REGBYTES(sp) 271 | LFREG f30,( 30 + 32)*REGBYTES(sp) 272 | LFREG f31,( 31 + 32)*REGBYTES(sp) 273 | 274 | addi sp, sp, 64*REGBYTES 275 | mret 276 | 277 | .section ".tdata.begin" 278 | .globl _tdata_begin 279 | _tdata_begin: 280 | 281 | .section ".tdata.end" 282 | .globl _tdata_end 283 | _tdata_end: 284 | 285 | .section ".tbss.end" 286 | .globl _tbss_end 287 | _tbss_end: 288 | 289 | -------------------------------------------------------------------------------- /lib/bsp/entry.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include "entry.h" 17 | 18 | /** 19 | * @brief Dummy function for __libc_init_array called 20 | */ 21 | void __attribute__((weak)) _init(void) 22 | { 23 | /** 24 | * These don't have to do anything since we use init_array/fini_array. 25 | */ 26 | } 27 | 28 | /** 29 | * @brief Dummy function for __libc_fini_array called 30 | */ 31 | void __attribute__((weak)) _fini(void) 32 | { 33 | /** 34 | * These don't have to do anything since we use init_array/fini_array. 35 | */ 36 | } 37 | 38 | -------------------------------------------------------------------------------- /lib/bsp/entry_user.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "atomic.h" 18 | #include "clint.h" 19 | #include "dmac.h" 20 | #include "entry.h" 21 | #include "fpioa.h" 22 | #include "platform.h" 23 | #include "plic.h" 24 | #include "sysctl.h" 25 | #include "syslog.h" 26 | #include "uart.h" 27 | #include "syscalls.h" 28 | 29 | extern volatile uint64_t g_wake_up[2]; 30 | 31 | core_instance_t core1_instance; 32 | 33 | volatile char * const ram = (volatile char*)RAM_BASE_ADDR; 34 | 35 | extern char _heap_start[]; 36 | extern char _heap_end[]; 37 | 38 | void thread_entry(int core_id) 39 | { 40 | while (!atomic_read(&g_wake_up[core_id])); 41 | } 42 | 43 | void core_enable(int core_id) 44 | { 45 | clint_ipi_send(core_id); 46 | atomic_set(&g_wake_up[core_id], 1); 47 | } 48 | 49 | int register_core1(core_function func, void *ctx) 50 | { 51 | if(func == NULL) 52 | return -1; 53 | core1_instance.callback = func; 54 | core1_instance.ctx = ctx; 55 | core_enable(1); 56 | return 0; 57 | } 58 | 59 | int __attribute__((weak)) os_entry(int core_id, int number_of_cores, int (*user_main)(int, char**)) 60 | { 61 | /* Call main if there is no OS */ 62 | return user_main(0, 0); 63 | } 64 | 65 | void _init_bsp(int core_id, int number_of_cores) 66 | { 67 | extern int main(int argc, char* argv[]); 68 | extern void __libc_init_array(void); 69 | extern void __libc_fini_array(void); 70 | 71 | if (core_id == 0) 72 | { 73 | /* Initialize bss data to 0 */ 74 | init_bss(); 75 | /* Init UART */ 76 | fpioa_set_function(4, FUNC_UART3_RX); 77 | fpioa_set_function(5, FUNC_UART3_TX); 78 | uart_debug_init(UART_DEVICE_3); 79 | /* Init FPIOA */ 80 | fpioa_init(); 81 | /* Register finalization function */ 82 | atexit(__libc_fini_array); 83 | /* Init libc array for C++ */ 84 | __libc_init_array(); 85 | /* Get reset status */ 86 | sysctl_get_reset_status(); 87 | /* Init plic */ 88 | plic_init(); 89 | /* Enable global interrupt */ 90 | sysctl_enable_irq(); 91 | } 92 | 93 | int ret = 0; 94 | if (core_id == 0) 95 | { 96 | core1_instance.callback = NULL; 97 | core1_instance.ctx = NULL; 98 | ret = os_entry(core_id, number_of_cores, main); 99 | } 100 | else 101 | { 102 | plic_init(); 103 | sysctl_enable_irq(); 104 | thread_entry(core_id); 105 | if(core1_instance.callback == NULL) 106 | asm volatile ("wfi"); 107 | else 108 | ret = core1_instance.callback(core1_instance.ctx); 109 | } 110 | exit(ret); 111 | } 112 | 113 | int pthread_setcancelstate(int __state, int *__oldstate) 114 | { 115 | return 0; 116 | } 117 | 118 | -------------------------------------------------------------------------------- /lib/bsp/include/atomic.h: -------------------------------------------------------------------------------- 1 | 2 | /* Copyright 2018 Canaan Inc. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | #ifndef _BSP_ATOMIC_H 17 | #define _BSP_ATOMIC_H 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | 24 | #define SPINLOCK_INIT \ 25 | { \ 26 | 0 \ 27 | } 28 | 29 | #define CORELOCK_INIT \ 30 | { \ 31 | .lock = SPINLOCK_INIT, \ 32 | .count = 0, \ 33 | .core = -1 \ 34 | } 35 | 36 | 37 | /* Defination of memory barrier macro */ 38 | #define mb() \ 39 | { \ 40 | asm volatile("fence" :: \ 41 | : "memory"); \ 42 | } 43 | 44 | #define atomic_set(ptr, val) (*(volatile typeof(*(ptr))*)(ptr) = val) 45 | #define atomic_read(ptr) (*(volatile typeof(*(ptr))*)(ptr)) 46 | 47 | #ifndef __riscv_atomic 48 | #error "atomic extension is required." 49 | #endif 50 | #define atomic_add(ptr, inc) __sync_fetch_and_add(ptr, inc) 51 | #define atomic_or(ptr, inc) __sync_fetch_and_or(ptr, inc) 52 | #define atomic_swap(ptr, swp) __sync_lock_test_and_set(ptr, swp) 53 | #define atomic_cas(ptr, cmp, swp) __sync_val_compare_and_swap(ptr, cmp, swp) 54 | 55 | typedef struct _spinlock 56 | { 57 | int lock; 58 | } spinlock_t; 59 | 60 | typedef struct _semaphore 61 | { 62 | spinlock_t lock; 63 | int count; 64 | int waiting; 65 | } semaphore_t; 66 | 67 | 68 | typedef struct _corelock 69 | { 70 | spinlock_t lock; 71 | int count; 72 | int core; 73 | } corelock_t; 74 | 75 | static inline int spinlock_trylock(spinlock_t *lock) 76 | { 77 | int res = atomic_swap(&lock->lock, -1); 78 | /* Use memory barrier to keep coherency */ 79 | mb(); 80 | return res; 81 | } 82 | 83 | static inline void spinlock_lock(spinlock_t *lock) 84 | { 85 | while (spinlock_trylock(lock)); 86 | } 87 | 88 | static inline void spinlock_unlock(spinlock_t *lock) 89 | { 90 | /* Use memory barrier to keep coherency */ 91 | mb(); 92 | atomic_set(&lock->lock, 0); 93 | asm volatile ("nop"); 94 | } 95 | 96 | static inline void semaphore_signal(semaphore_t *semaphore, int i) 97 | { 98 | spinlock_lock(&(semaphore->lock)); 99 | semaphore->count += i; 100 | spinlock_unlock(&(semaphore->lock)); 101 | } 102 | 103 | static inline void semaphore_wait(semaphore_t *semaphore, int i) 104 | { 105 | atomic_add(&(semaphore->waiting), 1); 106 | while (1) 107 | { 108 | spinlock_lock(&(semaphore->lock)); 109 | if (semaphore->count >= i) 110 | { 111 | semaphore->count -= i; 112 | atomic_add(&(semaphore->waiting), -1); 113 | spinlock_unlock(&(semaphore->lock)); 114 | break; 115 | } 116 | spinlock_unlock(&(semaphore->lock)); 117 | } 118 | } 119 | 120 | static inline int semaphore_count(semaphore_t *semaphore) 121 | { 122 | int res = 0; 123 | 124 | spinlock_lock(&(semaphore->lock)); 125 | res = semaphore->count; 126 | spinlock_unlock(&(semaphore->lock)); 127 | return res; 128 | } 129 | 130 | static inline int semaphore_waiting(semaphore_t *semaphore) 131 | { 132 | return atomic_read(&(semaphore->waiting)); 133 | } 134 | 135 | static inline int corelock_trylock(corelock_t *lock) 136 | { 137 | int res = 0; 138 | unsigned long core; 139 | 140 | asm volatile("csrr %0, mhartid;" 141 | : "=r"(core)); 142 | if(spinlock_trylock(&lock->lock)) 143 | { 144 | return -1; 145 | } 146 | 147 | if (lock->count == 0) 148 | { 149 | /* First time get lock */ 150 | lock->count++; 151 | lock->core = core; 152 | res = 0; 153 | } 154 | else if (lock->core == core) 155 | { 156 | /* Same core get lock */ 157 | lock->count++; 158 | res = 0; 159 | } 160 | else 161 | { 162 | /* Different core get lock */ 163 | res = -1; 164 | } 165 | spinlock_unlock(&lock->lock); 166 | 167 | return res; 168 | } 169 | 170 | static inline void corelock_lock(corelock_t *lock) 171 | { 172 | unsigned long core; 173 | 174 | asm volatile("csrr %0, mhartid;" 175 | : "=r"(core)); 176 | spinlock_lock(&lock->lock); 177 | 178 | if (lock->count == 0) 179 | { 180 | /* First time get lock */ 181 | lock->count++; 182 | lock->core = core; 183 | } 184 | else if (lock->core == core) 185 | { 186 | /* Same core get lock */ 187 | lock->count++; 188 | } 189 | else 190 | { 191 | /* Different core get lock */ 192 | spinlock_unlock(&lock->lock); 193 | 194 | do 195 | { 196 | while (atomic_read(&lock->count)) 197 | ; 198 | } while (corelock_trylock(lock)); 199 | return; 200 | } 201 | spinlock_unlock(&lock->lock); 202 | } 203 | 204 | static inline void corelock_unlock(corelock_t *lock) 205 | { 206 | unsigned long core; 207 | 208 | asm volatile("csrr %0, mhartid;" 209 | : "=r"(core)); 210 | spinlock_lock(&lock->lock); 211 | 212 | if (lock->core == core) 213 | { 214 | /* Same core release lock */ 215 | lock->count--; 216 | if (lock->count <= 0) 217 | { 218 | lock->core = -1; 219 | lock->count = 0; 220 | } 221 | } 222 | else 223 | { 224 | /* Different core release lock */ 225 | spinlock_unlock(&lock->lock); 226 | 227 | register unsigned long a7 asm("a7") = 93; 228 | register unsigned long a0 asm("a0") = 0; 229 | register unsigned long a1 asm("a1") = 0; 230 | register unsigned long a2 asm("a2") = 0; 231 | 232 | asm volatile("scall" 233 | : "+r"(a0) 234 | : "r"(a1), "r"(a2), "r"(a7)); 235 | } 236 | spinlock_unlock(&lock->lock); 237 | } 238 | 239 | #ifdef __cplusplus 240 | } 241 | #endif 242 | 243 | #endif /* _BSP_ATOMIC_H */ 244 | 245 | -------------------------------------------------------------------------------- /lib/bsp/include/bsp.h: -------------------------------------------------------------------------------- 1 | #ifndef _KENDRYTE_BSP_H 2 | #define _KENDRYTE_BSP_H 3 | #include "atomic.h" 4 | #include "entry.h" 5 | #include "sleep.h" 6 | #include "encoding.h" 7 | #include "syscalls.h" 8 | #include "printf.h" 9 | #endif -------------------------------------------------------------------------------- /lib/bsp/include/dump.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef _BSP_DUMP_H 17 | #define _BSP_DUMP_H 18 | 19 | #include 20 | #include 21 | #include "syslog.h" 22 | #include "uarths.h" 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | #define DUMP_PRINTF printk 29 | 30 | static inline void 31 | dump_core(const char *reason, uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32]) 32 | { 33 | static const char *const reg_usage[][2] = 34 | { 35 | {"zero ", "Hard-wired zero"}, 36 | {"ra ", "Return address"}, 37 | {"sp ", "Stack pointer"}, 38 | {"gp ", "Global pointer"}, 39 | {"tp ", "Thread pointer"}, 40 | {"t0 ", "Temporaries Caller"}, 41 | {"t1 ", "Temporaries Caller"}, 42 | {"t2 ", "Temporaries Caller"}, 43 | {"s0/fp", "Saved register/frame pointer"}, 44 | {"s1 ", "Saved register"}, 45 | {"a0 ", "Function arguments/return values"}, 46 | {"a1 ", "Function arguments/return values"}, 47 | {"a2 ", "Function arguments values"}, 48 | {"a3 ", "Function arguments values"}, 49 | {"a4 ", "Function arguments values"}, 50 | {"a5 ", "Function arguments values"}, 51 | {"a6 ", "Function arguments values"}, 52 | {"a7 ", "Function arguments values"}, 53 | {"s2 ", "Saved registers"}, 54 | {"s3 ", "Saved registers"}, 55 | {"s4 ", "Saved registers"}, 56 | {"s5 ", "Saved registers"}, 57 | {"s6 ", "Saved registers"}, 58 | {"s7 ", "Saved registers"}, 59 | {"s8 ", "Saved registers"}, 60 | {"s9 ", "Saved registers"}, 61 | {"s10 ", "Saved registers"}, 62 | {"s11 ", "Saved registers"}, 63 | {"t3 ", "Temporaries Caller"}, 64 | {"t4 ", "Temporaries Caller"}, 65 | {"t5 ", "Temporaries Caller"}, 66 | {"t6 ", "Temporaries Caller"}, 67 | }; 68 | 69 | static const char *const regf_usage[][2] = 70 | { 71 | {"ft0 ", "FP temporaries"}, 72 | {"ft1 ", "FP temporaries"}, 73 | {"ft2 ", "FP temporaries"}, 74 | {"ft3 ", "FP temporaries"}, 75 | {"ft4 ", "FP temporaries"}, 76 | {"ft5 ", "FP temporaries"}, 77 | {"ft6 ", "FP temporaries"}, 78 | {"ft7 ", "FP temporaries"}, 79 | {"fs0 ", "FP saved registers"}, 80 | {"fs1 ", "FP saved registers"}, 81 | {"fa0 ", "FP arguments/return values"}, 82 | {"fa1 ", "FP arguments/return values"}, 83 | {"fa2 ", "FP arguments values"}, 84 | {"fa3 ", "FP arguments values"}, 85 | {"fa4 ", "FP arguments values"}, 86 | {"fa5 ", "FP arguments values"}, 87 | {"fa6 ", "FP arguments values"}, 88 | {"fa7 ", "FP arguments values"}, 89 | {"fs2 ", "FP Saved registers"}, 90 | {"fs3 ", "FP Saved registers"}, 91 | {"fs4 ", "FP Saved registers"}, 92 | {"fs5 ", "FP Saved registers"}, 93 | {"fs6 ", "FP Saved registers"}, 94 | {"fs7 ", "FP Saved registers"}, 95 | {"fs8 ", "FP Saved registers"}, 96 | {"fs9 ", "FP Saved registers"}, 97 | {"fs10", "FP Saved registers"}, 98 | {"fs11", "FP Saved registers"}, 99 | {"ft8 ", "FP Temporaries Caller"}, 100 | {"ft9 ", "FP Temporaries Caller"}, 101 | {"ft10", "FP Temporaries Caller"}, 102 | {"ft11", "FP Temporaries Caller"}, 103 | }; 104 | 105 | if (CONFIG_LOG_LEVEL >= LOG_ERROR) 106 | { 107 | const char unknown_reason[] = "unknown"; 108 | 109 | if (!reason) 110 | reason = unknown_reason; 111 | 112 | DUMP_PRINTF("core dump: %s\r\n", reason); 113 | DUMP_PRINTF("Cause 0x%016lx, EPC 0x%016lx\r\n", cause, epc); 114 | 115 | int i = 0; 116 | for (i = 0; i < 32 / 2; i++) 117 | { 118 | DUMP_PRINTF( 119 | "reg[%02d](%s) = 0x%016lx, reg[%02d](%s) = 0x%016lx\r\n", 120 | i * 2, reg_usage[i * 2][0], regs[i * 2], 121 | i * 2 + 1, reg_usage[i * 2 + 1][0], regs[i * 2 + 1]); 122 | } 123 | 124 | for (i = 0; i < 32 / 2; i++) 125 | { 126 | DUMP_PRINTF( 127 | "freg[%02d](%s) = 0x%016lx(%f), freg[%02d](%s) = 0x%016lx(%f)\r\n", 128 | i * 2, regf_usage[i * 2][0], fregs[i * 2], (float)fregs[i * 2], 129 | i * 2 + 1, regf_usage[i * 2 + 1][0], fregs[i * 2 + 1], (float)fregs[i * 2 + 1]); 130 | } 131 | } 132 | } 133 | 134 | #undef DUMP_PRINTF 135 | 136 | #ifdef __cplusplus 137 | } 138 | #endif 139 | 140 | #endif /* _BSP_DUMP_H */ 141 | 142 | -------------------------------------------------------------------------------- /lib/bsp/include/entry.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef _BSP_ENTRY_H 17 | #define _BSP_ENTRY_H 18 | 19 | #include 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | typedef int (*core_function)(void *ctx); 27 | 28 | typedef struct _core_instance_t 29 | { 30 | core_function callback; 31 | void *ctx; 32 | } core_instance_t; 33 | 34 | int register_core1(core_function func, void *ctx); 35 | 36 | static inline void init_lma(void) 37 | { 38 | extern unsigned int _data_lma; 39 | extern unsigned int _data; 40 | extern unsigned int _edata; 41 | unsigned int *src, *dst; 42 | 43 | src = &_data_lma; 44 | dst = &_data; 45 | while (dst < &_edata) 46 | *dst++ = *src++; 47 | } 48 | 49 | static inline void init_bss(void) 50 | { 51 | extern unsigned int _bss; 52 | extern unsigned int _ebss; 53 | unsigned int *dst; 54 | 55 | dst = &_bss; 56 | while (dst < &_ebss) 57 | *dst++ = 0; 58 | } 59 | 60 | static inline void init_tls(void) 61 | { 62 | register void *thread_pointer asm("tp"); 63 | extern char _tls_data; 64 | 65 | extern __thread char _tdata_begin, _tdata_end, _tbss_end; 66 | 67 | size_t tdata_size = &_tdata_end - &_tdata_begin; 68 | 69 | memcpy(thread_pointer, &_tls_data, tdata_size); 70 | 71 | size_t tbss_size = &_tbss_end - &_tdata_end; 72 | 73 | memset(thread_pointer + tdata_size, 0, tbss_size); 74 | } 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif /* _BSP_ENTRY_H */ 81 | 82 | -------------------------------------------------------------------------------- /lib/bsp/include/interrupt.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef _BSP_INTERRUPT_H 17 | #define _BSP_INTERRUPT_H 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | /* clang-format off */ 23 | /* Machine interrupt mask for 64 bit system, 0x8000 0000 0000 0000 */ 24 | #define CAUSE_MACHINE_IRQ_MASK (0x1ULL << 63) 25 | 26 | /* Machine interrupt reason mask for 64 bit system, 0x7FFF FFFF FFFF FFFF */ 27 | #define CAUSE_MACHINE_IRQ_REASON_MASK (CAUSE_MACHINE_IRQ_MASK - 1) 28 | 29 | /* Hypervisor interrupt mask for 64 bit system, 0x8000 0000 0000 0000 */ 30 | #define CAUSE_HYPERVISOR_IRQ_MASK (0x1ULL << 63) 31 | 32 | /* Hypervisor interrupt reason mask for 64 bit system, 0x7FFF FFFF FFFF FFFF */ 33 | #define CAUSE_HYPERVISOR_IRQ_REASON_MASK (CAUSE_HYPERVISOR_IRQ_MASK - 1) 34 | 35 | /* Supervisor interrupt mask for 64 bit system, 0x8000 0000 0000 0000 */ 36 | #define CAUSE_SUPERVISOR_IRQ_MASK (0x1ULL << 63) 37 | 38 | /* Supervisor interrupt reason mask for 64 bit system, 0x7FFF FFFF FFFF FFFF */ 39 | #define CAUSE_SUPERVISOR_IRQ_REASON_MASK (CAUSE_SUPERVISOR_IRQ_MASK - 1) 40 | /* clang-format on */ 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | 46 | #endif /* _BSP_INTERRUPT_H */ 47 | 48 | -------------------------------------------------------------------------------- /lib/bsp/include/platform.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef _BSP_PLATFORM_H 17 | #define _BSP_PLATFORM_H 18 | 19 | #ifdef __cplusplus 20 | extern "C" { 21 | #endif 22 | 23 | /* clang-format off */ 24 | /* Register base address */ 25 | 26 | /* Under Coreplex */ 27 | #define CLINT_BASE_ADDR (0x02000000U) 28 | #define PLIC_BASE_ADDR (0x0C000000U) 29 | 30 | /* Under TileLink */ 31 | #define UARTHS_BASE_ADDR (0x38000000U) 32 | #define GPIOHS_BASE_ADDR (0x38001000U) 33 | 34 | /* Under AXI 64 bit */ 35 | #define RAM_BASE_ADDR (0x80000000U) 36 | #define RAM_SIZE (6 * 1024 * 1024U) 37 | 38 | #define IO_BASE_ADDR (0x40000000U) 39 | #define IO_SIZE (6 * 1024 * 1024U) 40 | 41 | #define AI_RAM_BASE_ADDR (0x80600000U) 42 | #define AI_RAM_SIZE (2 * 1024 * 1024U) 43 | 44 | #define AI_IO_BASE_ADDR (0x40600000U) 45 | #define AI_IO_SIZE (2 * 1024 * 1024U) 46 | 47 | #define AI_BASE_ADDR (0x40800000U) 48 | #define AI_SIZE (12 * 1024 * 1024U) 49 | 50 | #define FFT_BASE_ADDR (0x42000000U) 51 | #define FFT_SIZE (4 * 1024 * 1024U) 52 | 53 | #define ROM_BASE_ADDR (0x88000000U) 54 | #define ROM_SIZE (128 * 1024U) 55 | 56 | /* Under AHB 32 bit */ 57 | #define DMAC_BASE_ADDR (0x50000000U) 58 | 59 | /* Under APB1 32 bit */ 60 | #define GPIO_BASE_ADDR (0x50200000U) 61 | #define UART1_BASE_ADDR (0x50210000U) 62 | #define UART2_BASE_ADDR (0x50220000U) 63 | #define UART3_BASE_ADDR (0x50230000U) 64 | #define SPI_SLAVE_BASE_ADDR (0x50240000U) 65 | #define I2S0_BASE_ADDR (0x50250000U) 66 | #define I2S1_BASE_ADDR (0x50260000U) 67 | #define I2S2_BASE_ADDR (0x50270000U) 68 | #define I2C0_BASE_ADDR (0x50280000U) 69 | #define I2C1_BASE_ADDR (0x50290000U) 70 | #define I2C2_BASE_ADDR (0x502A0000U) 71 | #define FPIOA_BASE_ADDR (0x502B0000U) 72 | #define SHA256_BASE_ADDR (0x502C0000U) 73 | #define TIMER0_BASE_ADDR (0x502D0000U) 74 | #define TIMER1_BASE_ADDR (0x502E0000U) 75 | #define TIMER2_BASE_ADDR (0x502F0000U) 76 | 77 | /* Under APB2 32 bit */ 78 | #define WDT0_BASE_ADDR (0x50400000U) 79 | #define WDT1_BASE_ADDR (0x50410000U) 80 | #define OTP_BASE_ADDR (0x50420000U) 81 | #define DVP_BASE_ADDR (0x50430000U) 82 | #define SYSCTL_BASE_ADDR (0x50440000U) 83 | #define AES_BASE_ADDR (0x50450000U) 84 | #define RTC_BASE_ADDR (0x50460000U) 85 | 86 | 87 | /* Under APB3 32 bit */ 88 | #define SPI0_BASE_ADDR (0x52000000U) 89 | #define SPI1_BASE_ADDR (0x53000000U) 90 | #define SPI3_BASE_ADDR (0x54000000U) 91 | 92 | /* clang-format on */ 93 | 94 | #ifdef __cplusplus 95 | } 96 | #endif 97 | 98 | #endif /* _BSP_PLATFORM_H */ 99 | 100 | -------------------------------------------------------------------------------- /lib/bsp/include/sleep.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef _BSP_SLEEP_H 17 | #define _BSP_SLEEP_H 18 | 19 | #include "encoding.h" 20 | #include "clint.h" 21 | #include "syscalls.h" 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | int usleep(uint64_t usec); 28 | int msleep(uint64_t msec); 29 | unsigned int sleep(unsigned int seconds); 30 | 31 | #ifdef __cplusplus 32 | } 33 | #endif 34 | 35 | #endif /* _BSP_SLEEP_H */ 36 | 37 | -------------------------------------------------------------------------------- /lib/bsp/include/syscalls.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef _BSP_SYSCALLS_H 17 | #define _BSP_SYSCALLS_H 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /** 30 | * @brief Definitions for syscall putchar function 31 | * 32 | * @param[in] c The char to put 33 | * 34 | * @return result 35 | * - Byte On success, returns the written character. 36 | * - EOF On failure, returns EOF and sets the error indicator (see ferror()) on stdout. 37 | */ 38 | typedef int (*sys_putchar_t)(char c); 39 | 40 | /** 41 | * @brief Definitions for syscall getchar function 42 | * 43 | * @return byte as int type to get 44 | * - Byte The character read as an unsigned char cast to an int 45 | * - EOF EOF on end of file or error, no enough byte to read 46 | */ 47 | typedef int (*sys_getchar_t)(void); 48 | 49 | extern sys_putchar_t sys_putchar; 50 | extern sys_getchar_t sys_getchar; 51 | 52 | /** 53 | * @brief Register putchar function when perform write syscall 54 | * 55 | * @param[in] putchar The user-defined putchar function 56 | * 57 | * @return None 58 | */ 59 | void sys_register_putchar(sys_putchar_t putchar); 60 | 61 | /** 62 | * @brief Register getchar function when perform read syscall 63 | * 64 | * @param[in] getchar The user-defined getchar function 65 | * 66 | * @return None 67 | */ 68 | void sys_register_getchar(sys_getchar_t getchar); 69 | 70 | /** 71 | * @brief Flush stdin buffer 72 | * 73 | * @return None 74 | */ 75 | void sys_stdin_flush(void); 76 | 77 | void __attribute__((noreturn)) sys_exit(int code); 78 | 79 | /** 80 | * @brief Get free memory 81 | * 82 | * @return The size of free memory 83 | */ 84 | size_t get_free_heap_size(void); 85 | 86 | #ifdef __cplusplus 87 | } 88 | #endif 89 | 90 | #endif /* _BSP_SYSCALLS_H */ 91 | 92 | -------------------------------------------------------------------------------- /lib/bsp/include/util.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #ifndef _BSP_UTIL_H 17 | #define _BSP_UTIL_H 18 | 19 | 20 | #include 21 | #if defined(__riscv) 22 | #include "encoding.h" 23 | #endif 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | #if defined(__GNUC__) 30 | #pragma GCC diagnostic ignored "-Wunused-parameter" 31 | #pragma GCC diagnostic ignored "-Wunused-function" 32 | #endif 33 | 34 | /** 35 | * -------------------------------------------------------------------------- 36 | * Macros 37 | 38 | * Set HOST_DEBUG to 1 if you are going to compile this for a host 39 | * machine (ie Athena/Linux) for debug purposes and set HOST_DEBUG 40 | * to 0 if you are compiling with the smips-gcc toolchain. 41 | */ 42 | 43 | #ifndef HOST_DEBUG 44 | #define HOST_DEBUG 0 45 | #endif 46 | 47 | /** 48 | * Set PREALLOCATE to 1 if you want to preallocate the benchmark 49 | * function before starting stats. If you have instruction/data 50 | * caches and you don't want to count the overhead of misses, then 51 | * you will need to use preallocation. 52 | */ 53 | 54 | #ifndef PREALLOCATE 55 | #define PREALLOCATE 0 56 | #endif 57 | 58 | 59 | #define static_assert(cond) \ 60 | { \ 61 | switch (0) \ 62 | { \ 63 | case 0: \ 64 | case !!(long)(cond):; \ 65 | } \ 66 | } 67 | 68 | #define stringify_1(s) #s 69 | #define stringify(s) stringify_1(s) 70 | #define stats(code, iter) \ 71 | do \ 72 | { \ 73 | unsigned long _c = -read_cycle(), _i = -read_csr(minstret); \ 74 | code; \ 75 | _c += read_cycle(), _i += read_csr(minstret); \ 76 | if (cid == 0) \ 77 | printf("\r\n%s: %ld cycles, %ld.%ld cycles/iter, %ld.%ld CPI\r\n", \ 78 | stringify(code), _c, _c / iter, 10 * _c / iter % 10, _c / _i, 10 * _c / _i % 10); \ 79 | } while (0) 80 | 81 | 82 | /** 83 | * Set SET_STATS to 1 if you want to carve out the piece that actually 84 | * does the computation. 85 | */ 86 | 87 | #if HOST_DEBUG 88 | #include 89 | static void setStats(int enable) {} 90 | #else 91 | extern void setStats(int enable); 92 | #endif 93 | 94 | 95 | static void printArray(const char name[], int n, const int arr[]) 96 | { 97 | #if HOST_DEBUG 98 | int i; 99 | 100 | printf(" %10s :", name); 101 | for (i = 0; i < n; i++) 102 | printf(" %3d ", arr[i]); 103 | printf("\r\n"); 104 | #endif 105 | } 106 | 107 | static void printDoubleArray(const char name[], int n, const double arr[]) 108 | { 109 | #if HOST_DEBUG 110 | int i; 111 | 112 | printf(" %10s :", name); 113 | for (i = 0; i < n; i++) 114 | printf(" %g ", arr[i]); 115 | printf("\r\n"); 116 | #endif 117 | } 118 | 119 | static int verify(int n, const volatile int *test, const int *verify) 120 | { 121 | int i; 122 | /* Unrolled for faster verification */ 123 | for (i = 0; i < n / 2 * 2; i += 2) 124 | { 125 | int t0 = test[i], t1 = test[i + 1]; 126 | int v0 = verify[i], v1 = verify[i + 1]; 127 | 128 | if (t0 != v0) 129 | return i + 1; 130 | if (t1 != v1) 131 | return i + 2; 132 | } 133 | if (n % 2 != 0 && test[n - 1] != verify[n - 1]) 134 | return n; 135 | return 0; 136 | } 137 | 138 | static int verifyDouble(int n, const volatile double *test, const double *verify) 139 | { 140 | int i; 141 | /* Unrolled for faster verification */ 142 | for (i = 0; i < n / 2 * 2; i += 2) 143 | { 144 | double t0 = test[i], t1 = test[i + 1]; 145 | double v0 = verify[i], v1 = verify[i + 1]; 146 | int eq1 = t0 == v0, eq2 = t1 == v1; 147 | 148 | if (!(eq1 & eq2)) 149 | return i + 1 + eq1; 150 | } 151 | if (n % 2 != 0 && test[n - 1] != verify[n - 1]) 152 | return n; 153 | return 0; 154 | } 155 | 156 | static void __attribute__((noinline)) barrier(int ncores) 157 | { 158 | static volatile int sense = 0; 159 | static volatile int count = 0; 160 | 161 | static __thread int threadsense; 162 | 163 | __sync_synchronize(); 164 | 165 | threadsense = !threadsense; 166 | if (__sync_fetch_and_add(&count, 1) == ncores - 1) 167 | { 168 | count = 0; 169 | sense = threadsense; 170 | } 171 | else 172 | { 173 | while (sense != threadsense) 174 | ; 175 | } 176 | 177 | __sync_synchronize(); 178 | } 179 | 180 | static uint64_t lfsr(uint64_t x) 181 | { 182 | uint64_t bit = (x ^ (x >> 1)) & 1; 183 | 184 | return (x >> 1) | (bit << 62); 185 | } 186 | 187 | 188 | #if defined(__GNUC__) 189 | #pragma GCC diagnostic warning "-Wunused-parameter" 190 | #pragma GCC diagnostic warning "-Wunused-function" 191 | #endif 192 | 193 | #ifdef __cplusplus 194 | } 195 | #endif 196 | 197 | #endif /* _BSP_UTIL_H */ 198 | 199 | -------------------------------------------------------------------------------- /lib/bsp/interrupt.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | /* Enable kernel-mode log API */ 17 | 18 | #include 19 | #include 20 | #include "interrupt.h" 21 | #include "dump.h" 22 | #include "syscalls.h" 23 | #include "syslog.h" 24 | 25 | uintptr_t __attribute__((weak)) 26 | handle_irq_dummy(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32]) 27 | { 28 | dump_core("unhandled interrupt", cause, epc, regs, fregs); 29 | sys_exit(1337); 30 | return epc; 31 | } 32 | 33 | uintptr_t __attribute__((weak, alias("handle_irq_dummy"))) 34 | handle_irq_m_soft(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32]); 35 | 36 | uintptr_t __attribute__((weak, alias("handle_irq_dummy"))) 37 | handle_irq_m_timer(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32]); 38 | 39 | extern uintptr_t 40 | handle_irq_m_ext(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32]); 41 | 42 | uintptr_t __attribute__((weak)) 43 | handle_irq(uintptr_t cause, uintptr_t epc, uintptr_t regs[32], uintptr_t fregs[32]) 44 | { 45 | #if defined(__GNUC__) 46 | #pragma GCC diagnostic ignored "-Woverride-init" 47 | #endif 48 | /* clang-format off */ 49 | static uintptr_t (* const irq_table[])( 50 | uintptr_t cause, 51 | uintptr_t epc, 52 | uintptr_t regs[32], 53 | uintptr_t fregs[32]) = 54 | { 55 | [0 ... 14] = handle_irq_dummy, 56 | [IRQ_M_SOFT] = handle_irq_m_soft, 57 | [IRQ_M_TIMER] = handle_irq_m_timer, 58 | [IRQ_M_EXT] = handle_irq_m_ext, 59 | }; 60 | /* clang-format on */ 61 | #if defined(__GNUC__) 62 | #pragma GCC diagnostic warning "-Woverride-init" 63 | #endif 64 | return irq_table[cause & CAUSE_MACHINE_IRQ_REASON_MASK](cause, epc, regs, fregs); 65 | } 66 | 67 | -------------------------------------------------------------------------------- /lib/bsp/locks.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "bsp.h" 4 | 5 | #define LOCK_MAX_NUM (1024) 6 | 7 | typedef long _lock_t; 8 | 9 | typedef struct 10 | { 11 | _lock_t *lock; 12 | long counter; 13 | unsigned long core; 14 | } reculock_t; 15 | 16 | reculock_t reculock[LOCK_MAX_NUM]; 17 | 18 | void show_error(void) 19 | { 20 | register unsigned long a7 asm("a7") = 93; 21 | register unsigned long a0 asm("a0") = 0; 22 | register unsigned long a1 asm("a1") = 0; 23 | register unsigned long a2 asm("a2") = 0; 24 | 25 | asm volatile("scall" 26 | : "+r"(a0) 27 | : "r"(a1), "r"(a2), "r"(a7)); 28 | } 29 | 30 | static inline long lock_trylock(_lock_t *lock) 31 | { 32 | long res = atomic_swap(lock, -1); 33 | /* Use memory barrier to keep coherency */ 34 | mb(); 35 | return res; 36 | } 37 | 38 | static inline void lock_lock(_lock_t *lock) 39 | { 40 | while (lock_trylock(lock)); 41 | } 42 | 43 | static inline void lock_unlock(_lock_t *lock) 44 | { 45 | /* Use memory barrier to keep coherency */ 46 | mb(); 47 | atomic_swap(lock, 0); 48 | asm volatile ("nop"); 49 | } 50 | 51 | static reculock_t *get_reculock(_lock_t *lock) 52 | { 53 | for(uint32_t i = 0; i < LOCK_MAX_NUM; i++) 54 | { 55 | if(reculock[i].lock == lock) 56 | return &reculock[i]; 57 | } 58 | return NULL; 59 | } 60 | 61 | static reculock_t *get_free_reculock(void) 62 | { 63 | for(uint32_t i = 0; i < LOCK_MAX_NUM; i++) 64 | { 65 | if(reculock[i].lock == NULL) 66 | { 67 | return &reculock[i]; 68 | } 69 | } 70 | return NULL; 71 | } 72 | 73 | static reculock_t *reculock_init(_lock_t *lock) 74 | { 75 | reculock_t *v_reculock = get_free_reculock(); 76 | if(v_reculock == NULL) 77 | { 78 | return NULL; 79 | } 80 | *v_reculock = (reculock_t) 81 | { 82 | .lock = lock, 83 | .counter = 0, 84 | .core = 0, 85 | }; 86 | return v_reculock; 87 | } 88 | 89 | static void reculock_deinit(_lock_t *lock) 90 | { 91 | lock_lock(lock); 92 | reculock_t *v_reculock = get_reculock(lock); 93 | if(v_reculock) 94 | { 95 | *v_reculock = (reculock_t) 96 | { 97 | .lock = NULL, 98 | .counter = 0, 99 | .core = 0, 100 | }; 101 | } 102 | lock_unlock(lock); 103 | } 104 | 105 | static inline int reculock_trylock(_lock_t *lock) 106 | { 107 | int res = 0; 108 | unsigned long core; 109 | 110 | asm volatile("csrr %0, mhartid;" 111 | : "=r"(core)); 112 | if(lock_trylock(lock)) 113 | { 114 | return -1; 115 | } 116 | 117 | reculock_t *v_reculock = get_reculock(lock); 118 | if(v_reculock == NULL) 119 | { 120 | v_reculock = reculock_init(lock); 121 | if(v_reculock == NULL) 122 | { 123 | lock_unlock(lock); 124 | show_error(); 125 | } 126 | } 127 | 128 | if (v_reculock->counter == 0) 129 | { 130 | /* First time get lock */ 131 | v_reculock->counter++; 132 | v_reculock->core = core; 133 | res = 0; 134 | } 135 | else if (v_reculock->core == core) 136 | { 137 | /* Same core get lock */ 138 | v_reculock->counter++; 139 | res = 0; 140 | } 141 | else 142 | { 143 | /* Different core get lock */ 144 | res = -1; 145 | } 146 | lock_unlock(lock); 147 | 148 | return res; 149 | } 150 | 151 | static inline void reculock_lock(_lock_t *lock) 152 | { 153 | unsigned long core; 154 | 155 | asm volatile("csrr %0, mhartid;" 156 | : "=r"(core)); 157 | lock_lock(lock); 158 | 159 | reculock_t *v_reculock = get_reculock(lock); 160 | if(v_reculock == NULL) 161 | { 162 | v_reculock = reculock_init(lock); 163 | if(v_reculock == NULL) 164 | { 165 | lock_unlock(lock); 166 | show_error(); 167 | } 168 | } 169 | 170 | if (v_reculock->counter == 0) 171 | { 172 | /* First time get lock */ 173 | v_reculock->counter++; 174 | v_reculock->core = core; 175 | } 176 | else if (v_reculock->core == core) 177 | { 178 | /* Same core get lock */ 179 | v_reculock->counter++; 180 | } 181 | else 182 | { 183 | /* Different core get lock */ 184 | lock_unlock(lock); 185 | do 186 | { 187 | while (atomic_read(&reculock->counter)) 188 | ; 189 | } while (reculock_trylock(lock)); 190 | return; 191 | } 192 | lock_unlock(lock); 193 | } 194 | 195 | static inline void reculock_unlock(_lock_t *lock) 196 | { 197 | unsigned long core; 198 | 199 | asm volatile("csrr %0, mhartid;" 200 | : "=r"(core)); 201 | lock_lock(lock); 202 | 203 | reculock_t *v_reculock = get_reculock(lock); 204 | if(v_reculock == NULL) 205 | { 206 | lock_unlock(lock); 207 | show_error(); 208 | } 209 | 210 | if (v_reculock->core == core) 211 | { 212 | /* Same core release lock */ 213 | v_reculock->counter--; 214 | if (v_reculock->counter <= 0) 215 | { 216 | v_reculock->core = 0; 217 | v_reculock->counter = 0; 218 | v_reculock->lock = NULL; 219 | } 220 | } 221 | else 222 | { 223 | /* Different core release lock */ 224 | lock_unlock(lock); 225 | show_error(); 226 | } 227 | lock_unlock(lock); 228 | } 229 | 230 | void _lock_init(_lock_t *lock) 231 | { 232 | *lock = 0; 233 | } 234 | 235 | void _lock_init_recursive(_lock_t *lock) 236 | { 237 | reculock_init(lock); 238 | } 239 | 240 | void _lock_close(_lock_t *lock) 241 | { 242 | lock_unlock(lock); 243 | } 244 | 245 | void _lock_close_recursive(_lock_t *lock) 246 | { 247 | reculock_deinit(lock); 248 | } 249 | 250 | void _lock_acquire(_lock_t *lock) 251 | { 252 | lock_lock(lock); 253 | } 254 | 255 | void _lock_acquire_recursive(_lock_t *lock) 256 | { 257 | reculock_lock(lock); 258 | } 259 | 260 | int _lock_try_acquire(_lock_t *lock) 261 | { 262 | return lock_trylock(lock); 263 | } 264 | 265 | int _lock_try_acquire_recursive(_lock_t *lock) 266 | { 267 | return reculock_trylock(lock); 268 | } 269 | 270 | void _lock_release(_lock_t *lock) 271 | { 272 | lock_unlock(lock); 273 | } 274 | 275 | void _lock_release_recursive(_lock_t *lock) 276 | { 277 | reculock_unlock(lock); 278 | } 279 | 280 | -------------------------------------------------------------------------------- /lib/bsp/sleep.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include "sleep.h" 16 | #include "sysctl.h" 17 | 18 | int usleep(uint64_t usec) 19 | { 20 | uint64_t cycle = read_cycle(); 21 | uint64_t nop_all = usec * sysctl_clock_get_freq(SYSCTL_CLOCK_CPU) / 1000000UL; 22 | while (1) 23 | { 24 | if(read_cycle() - cycle >= nop_all) 25 | break; 26 | } 27 | return 0; 28 | } 29 | 30 | int msleep(uint64_t msec) 31 | { 32 | return (unsigned int)usleep(msec * 1000); 33 | } 34 | 35 | unsigned int sleep(unsigned int seconds) 36 | { 37 | return (unsigned int)msleep(seconds * 1000); 38 | } 39 | 40 | -------------------------------------------------------------------------------- /lib/drivers/fft.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include 16 | #include "dmac.h" 17 | #include "utils.h" 18 | #include "sysctl.h" 19 | #include "fft.h" 20 | 21 | static volatile fft_t *const fft = (volatile fft_t *)FFT_BASE_ADDR; 22 | 23 | static void fft_init(uint8_t point, uint8_t mode, uint16_t shift, uint8_t is_dma, uint8_t input_mode, uint8_t data_mode) 24 | { 25 | fft->fft_ctrl.fft_point = point; 26 | fft->fft_ctrl.fft_mode = mode; 27 | fft->fft_ctrl.fft_shift = shift; 28 | fft->fft_ctrl.dma_send = is_dma; 29 | fft->fft_ctrl.fft_enable = 1; 30 | fft->fft_ctrl.fft_input_mode = input_mode; 31 | fft->fft_ctrl.fft_data_mode = data_mode; 32 | } 33 | 34 | void fft_complex_uint16_dma(dmac_channel_number_t dma_send_channel_num, dmac_channel_number_t dma_receive_channel_num, 35 | uint16_t shift, fft_direction_t direction, const uint64_t *input, size_t point_num, uint64_t *output) 36 | { 37 | fft_point_t point = FFT_512; 38 | switch(point_num) 39 | { 40 | case 512: 41 | point = FFT_512; 42 | break; 43 | case 256: 44 | point = FFT_256; 45 | break; 46 | case 128: 47 | point = FFT_128; 48 | break; 49 | case 64: 50 | point = FFT_64; 51 | break; 52 | default: 53 | configASSERT(!"fft point error"); 54 | break; 55 | } 56 | sysctl_clock_enable(SYSCTL_CLOCK_FFT); 57 | sysctl_reset(SYSCTL_RESET_FFT); 58 | fft_init(point, direction, shift, 1, 0, 0); 59 | sysctl_dma_select(dma_receive_channel_num, SYSCTL_DMA_SELECT_FFT_RX_REQ); 60 | sysctl_dma_select(dma_send_channel_num, SYSCTL_DMA_SELECT_FFT_TX_REQ); 61 | dmac_set_single_mode(dma_receive_channel_num, (void *)(&fft->fft_output_fifo), output, DMAC_ADDR_NOCHANGE, DMAC_ADDR_INCREMENT, 62 | DMAC_MSIZE_4, DMAC_TRANS_WIDTH_64, point_num>>1); 63 | dmac_set_single_mode(dma_send_channel_num, input, (void *)(&fft->fft_input_fifo), DMAC_ADDR_INCREMENT, DMAC_ADDR_NOCHANGE, 64 | DMAC_MSIZE_4, DMAC_TRANS_WIDTH_64, point_num>>1); 65 | dmac_wait_done(dma_receive_channel_num); 66 | } 67 | 68 | 69 | -------------------------------------------------------------------------------- /lib/drivers/gpio.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include "gpio.h" 16 | #include "utils.h" 17 | #include "fpioa.h" 18 | #include "sysctl.h" 19 | #define GPIO_MAX_PINNO 8 20 | 21 | volatile gpio_t* const gpio = (volatile gpio_t*)GPIO_BASE_ADDR; 22 | 23 | int gpio_init(void) 24 | { 25 | return sysctl_clock_enable(SYSCTL_CLOCK_GPIO); 26 | } 27 | 28 | void gpio_set_drive_mode(uint8_t pin, gpio_drive_mode_t mode) 29 | { 30 | configASSERT(pin < GPIO_MAX_PINNO); 31 | int io_number = fpioa_get_io_by_function(FUNC_GPIO0 + pin); 32 | configASSERT(io_number >= 0); 33 | 34 | fpioa_pull_t pull; 35 | uint32_t dir; 36 | 37 | switch (mode) 38 | { 39 | case GPIO_DM_INPUT: 40 | pull = FPIOA_PULL_NONE; 41 | dir = 0; 42 | break; 43 | case GPIO_DM_INPUT_PULL_DOWN: 44 | pull = FPIOA_PULL_DOWN; 45 | dir = 0; 46 | break; 47 | case GPIO_DM_INPUT_PULL_UP: 48 | pull = FPIOA_PULL_UP; 49 | dir = 0; 50 | break; 51 | case GPIO_DM_OUTPUT: 52 | pull = FPIOA_PULL_DOWN; 53 | dir = 1; 54 | break; 55 | default: 56 | configASSERT(!"GPIO drive mode is not supported.") break; 57 | } 58 | 59 | fpioa_set_io_pull(io_number, pull); 60 | set_gpio_bit(gpio->direction.u32, pin, dir); 61 | } 62 | 63 | gpio_pin_value_t gpio_get_pin(uint8_t pin) 64 | { 65 | configASSERT(pin < GPIO_MAX_PINNO); 66 | uint32_t dir = get_gpio_bit(gpio->direction.u32, pin); 67 | volatile uint32_t *reg = dir ? gpio->data_output.u32 : gpio->data_input.u32; 68 | return get_gpio_bit(reg, pin); 69 | } 70 | 71 | void gpio_set_pin(uint8_t pin, gpio_pin_value_t value) 72 | { 73 | configASSERT(pin < GPIO_MAX_PINNO); 74 | uint32_t dir = get_gpio_bit(gpio->direction.u32, pin); 75 | volatile uint32_t *reg = dir ? gpio->data_output.u32 : gpio->data_input.u32; 76 | configASSERT(dir == 1); 77 | set_gpio_bit(reg, pin, value); 78 | } 79 | 80 | -------------------------------------------------------------------------------- /lib/drivers/gpiohs.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include "gpiohs.h" 16 | #include "utils.h" 17 | #include "fpioa.h" 18 | #include "sysctl.h" 19 | #define GPIOHS_MAX_PINNO 32 20 | 21 | volatile gpiohs_t* const gpiohs = (volatile gpiohs_t*)GPIOHS_BASE_ADDR; 22 | 23 | typedef struct _gpiohs_pin_instance 24 | { 25 | size_t pin; 26 | gpio_pin_edge_t edge; 27 | void (*callback)(); 28 | plic_irq_callback_t gpiohs_callback; 29 | void *context; 30 | } gpiohs_pin_instance_t; 31 | 32 | static gpiohs_pin_instance_t pin_instance[32]; 33 | 34 | void gpiohs_set_drive_mode(uint8_t pin, gpio_drive_mode_t mode) 35 | { 36 | configASSERT(pin < GPIOHS_MAX_PINNO); 37 | int io_number = fpioa_get_io_by_function(FUNC_GPIOHS0 + pin); 38 | configASSERT(io_number >= 0); 39 | 40 | fpioa_pull_t pull; 41 | uint32_t dir; 42 | 43 | switch (mode) 44 | { 45 | case GPIO_DM_INPUT: 46 | pull = FPIOA_PULL_NONE; 47 | dir = 0; 48 | break; 49 | case GPIO_DM_INPUT_PULL_DOWN: 50 | pull = FPIOA_PULL_DOWN; 51 | dir = 0; 52 | break; 53 | case GPIO_DM_INPUT_PULL_UP: 54 | pull = FPIOA_PULL_UP; 55 | dir = 0; 56 | break; 57 | case GPIO_DM_OUTPUT: 58 | pull = FPIOA_PULL_DOWN; 59 | dir = 1; 60 | break; 61 | default: 62 | configASSERT(!"GPIO drive mode is not supported.") break; 63 | } 64 | 65 | fpioa_set_io_pull(io_number, pull); 66 | volatile uint32_t *reg = dir ? gpiohs->output_en.u32 : gpiohs->input_en.u32; 67 | volatile uint32_t *reg_d = !dir ? gpiohs->output_en.u32 : gpiohs->input_en.u32; 68 | set_gpio_bit(reg_d, pin, 0); 69 | set_gpio_bit(reg, pin, 1); 70 | } 71 | 72 | gpio_pin_value_t gpiohs_get_pin(uint8_t pin) 73 | { 74 | configASSERT(pin < GPIOHS_MAX_PINNO); 75 | return get_gpio_bit(gpiohs->input_val.u32, pin); 76 | } 77 | 78 | void gpiohs_set_pin(uint8_t pin, gpio_pin_value_t value) 79 | { 80 | configASSERT(pin < GPIOHS_MAX_PINNO); 81 | set_gpio_bit(gpiohs->output_val.u32, pin, value); 82 | } 83 | 84 | void gpiohs_set_pin_edge(uint8_t pin, gpio_pin_edge_t edge) 85 | { 86 | set_gpio_bit(gpiohs->rise_ie.u32, pin, 0); 87 | set_gpio_bit(gpiohs->rise_ip.u32, pin, 1); 88 | 89 | set_gpio_bit(gpiohs->fall_ie.u32, pin, 0); 90 | set_gpio_bit(gpiohs->fall_ip.u32, pin, 1); 91 | 92 | set_gpio_bit(gpiohs->low_ie.u32, pin, 0); 93 | set_gpio_bit(gpiohs->low_ip.u32, pin, 1); 94 | 95 | set_gpio_bit(gpiohs->high_ie.u32, pin, 0); 96 | set_gpio_bit(gpiohs->high_ip.u32, pin, 1); 97 | 98 | if(edge & GPIO_PE_FALLING) 99 | { 100 | set_gpio_bit(gpiohs->fall_ie.u32, pin, 1); 101 | } 102 | else 103 | { 104 | set_gpio_bit(gpiohs->fall_ie.u32, pin, 0); 105 | } 106 | 107 | if(edge & GPIO_PE_RISING) 108 | { 109 | set_gpio_bit(gpiohs->rise_ie.u32, pin, 1); 110 | } 111 | else 112 | { 113 | set_gpio_bit(gpiohs->rise_ie.u32, pin, 0); 114 | } 115 | 116 | if(edge & GPIO_PE_LOW) 117 | { 118 | set_gpio_bit(gpiohs->low_ie.u32, pin, 1); 119 | } 120 | else 121 | { 122 | set_gpio_bit(gpiohs->low_ie.u32, pin, 0); 123 | } 124 | 125 | if(edge & GPIO_PE_HIGH) 126 | { 127 | set_gpio_bit(gpiohs->high_ie.u32, pin, 1); 128 | } 129 | else 130 | { 131 | set_gpio_bit(gpiohs->high_ie.u32, pin, 0); 132 | } 133 | 134 | pin_instance[pin].edge = edge; 135 | } 136 | 137 | int gpiohs_pin_onchange_isr(void *userdata) 138 | { 139 | gpiohs_pin_instance_t *ctx = (gpiohs_pin_instance_t *)userdata; 140 | size_t pin = ctx->pin; 141 | 142 | if(ctx->edge & GPIO_PE_FALLING) 143 | { 144 | set_gpio_bit(gpiohs->fall_ie.u32, pin, 0); 145 | set_gpio_bit(gpiohs->fall_ip.u32, pin, 1); 146 | set_gpio_bit(gpiohs->fall_ie.u32, pin, 1); 147 | } 148 | 149 | if(ctx->edge & GPIO_PE_RISING) 150 | { 151 | set_gpio_bit(gpiohs->rise_ie.u32, pin, 0); 152 | set_gpio_bit(gpiohs->rise_ip.u32, pin, 1); 153 | set_gpio_bit(gpiohs->rise_ie.u32, pin, 1); 154 | } 155 | 156 | if(ctx->edge & GPIO_PE_LOW) 157 | { 158 | set_gpio_bit(gpiohs->low_ie.u32, pin, 0); 159 | set_gpio_bit(gpiohs->low_ip.u32, pin, 1); 160 | set_gpio_bit(gpiohs->low_ie.u32, pin, 1); 161 | } 162 | 163 | if(ctx->edge & GPIO_PE_HIGH) 164 | { 165 | set_gpio_bit(gpiohs->high_ie.u32, pin, 0); 166 | set_gpio_bit(gpiohs->high_ip.u32, pin, 1); 167 | set_gpio_bit(gpiohs->high_ie.u32, pin, 1); 168 | } 169 | 170 | if (ctx->callback) 171 | ctx->callback(); 172 | if(ctx->gpiohs_callback) 173 | ctx->gpiohs_callback(ctx->context); 174 | 175 | return 0; 176 | } 177 | 178 | void gpiohs_set_irq(uint8_t pin, uint32_t priority, void (*func)()) 179 | { 180 | 181 | pin_instance[pin].pin = pin; 182 | pin_instance[pin].callback = func; 183 | 184 | plic_set_priority(IRQN_GPIOHS0_INTERRUPT + pin, priority); 185 | plic_irq_register(IRQN_GPIOHS0_INTERRUPT + pin, gpiohs_pin_onchange_isr, &(pin_instance[pin])); 186 | plic_irq_enable(IRQN_GPIOHS0_INTERRUPT + pin); 187 | } 188 | 189 | void gpiohs_irq_register(uint8_t pin, uint32_t priority, plic_irq_callback_t callback, void *ctx) 190 | { 191 | pin_instance[pin].pin = pin; 192 | pin_instance[pin].gpiohs_callback = callback; 193 | pin_instance[pin].context = ctx; 194 | 195 | plic_set_priority(IRQN_GPIOHS0_INTERRUPT + pin, priority); 196 | plic_irq_register(IRQN_GPIOHS0_INTERRUPT + pin, gpiohs_pin_onchange_isr, &(pin_instance[pin])); 197 | plic_irq_enable(IRQN_GPIOHS0_INTERRUPT + pin); 198 | } 199 | 200 | void gpiohs_irq_unregister(uint8_t pin) 201 | { 202 | pin_instance[pin] = (gpiohs_pin_instance_t){ 203 | .callback = NULL, 204 | .gpiohs_callback = NULL, 205 | .context = NULL, 206 | }; 207 | set_gpio_bit(gpiohs->rise_ie.u32, pin, 0); 208 | set_gpio_bit(gpiohs->fall_ie.u32, pin, 0); 209 | set_gpio_bit(gpiohs->low_ie.u32, pin, 0); 210 | set_gpio_bit(gpiohs->high_ie.u32, pin, 0); 211 | plic_irq_unregister(IRQN_GPIOHS0_INTERRUPT + pin); 212 | } 213 | 214 | void gpiohs_irq_disable(size_t pin) 215 | { 216 | plic_irq_disable(IRQN_GPIOHS0_INTERRUPT + pin); 217 | } 218 | 219 | -------------------------------------------------------------------------------- /lib/drivers/include/gpio.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _DRIVER_GPIO_H 16 | #define _DRIVER_GPIO_H 17 | 18 | #include "platform.h" 19 | #include 20 | #include 21 | #include "gpio_common.h" 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | /** 27 | * @brief Structure for accessing GPIO registers by individual bit 28 | */ 29 | typedef struct _gpio_bits 30 | { 31 | uint32_t b0 : 1; 32 | uint32_t b1 : 1; 33 | uint32_t b2 : 1; 34 | uint32_t b3 : 1; 35 | uint32_t b4 : 1; 36 | uint32_t b5 : 1; 37 | uint32_t b6 : 1; 38 | uint32_t b7 : 1; 39 | uint32_t b8 : 1; 40 | uint32_t b9 : 1; 41 | uint32_t b10 : 1; 42 | uint32_t b11 : 1; 43 | uint32_t b12 : 1; 44 | uint32_t b13 : 1; 45 | uint32_t b14 : 1; 46 | uint32_t b15 : 1; 47 | uint32_t b16 : 1; 48 | uint32_t b17 : 1; 49 | uint32_t b18 : 1; 50 | uint32_t b19 : 1; 51 | uint32_t b20 : 1; 52 | uint32_t b21 : 1; 53 | uint32_t b22 : 1; 54 | uint32_t b23 : 1; 55 | uint32_t b24 : 1; 56 | uint32_t b25 : 1; 57 | uint32_t b26 : 1; 58 | uint32_t b27 : 1; 59 | uint32_t b28 : 1; 60 | uint32_t b29 : 1; 61 | uint32_t b30 : 1; 62 | uint32_t b31 : 1; 63 | } __attribute__((packed, aligned(4))) gpio_bits_t; 64 | 65 | /** 66 | * @brief Structure of templates for accessing GPIO registers 67 | */ 68 | typedef union _gpio_access_tp 69 | { 70 | /* 32x1 bit mode */ 71 | uint32_t u32[1]; 72 | /* 16x2 bit mode */ 73 | uint16_t u16[2]; 74 | /* 8x4 bit mode */ 75 | uint8_t u8[4]; 76 | /* 1 bit mode */ 77 | gpio_bits_t bits; 78 | } __attribute__((packed, aligned(4))) gpio_access_tp_t; 79 | 80 | /** 81 | * @brief The GPIO address map 82 | */ 83 | typedef struct _gpio 84 | { 85 | /* Offset 0x00: Data (output) registers */ 86 | gpio_access_tp_t data_output; 87 | /* Offset 0x04: Data direction registers */ 88 | gpio_access_tp_t direction; 89 | /* Offset 0x08: Data source registers */ 90 | gpio_access_tp_t source; 91 | /* Offset 0x10 - 0x2f: Unused registers, 9x4 bytes */ 92 | uint32_t unused_0[9]; 93 | /* Offset 0x30: Interrupt enable/disable registers */ 94 | gpio_access_tp_t interrupt_enable; 95 | /* Offset 0x34: Interrupt mask registers */ 96 | gpio_access_tp_t interrupt_mask; 97 | /* Offset 0x38: Interrupt level registers */ 98 | gpio_access_tp_t interrupt_level; 99 | /* Offset 0x3c: Interrupt polarity registers */ 100 | gpio_access_tp_t interrupt_polarity; 101 | /* Offset 0x40: Interrupt status registers */ 102 | gpio_access_tp_t interrupt_status; 103 | /* Offset 0x44: Raw interrupt status registers */ 104 | gpio_access_tp_t interrupt_status_raw; 105 | /* Offset 0x48: Interrupt debounce registers */ 106 | gpio_access_tp_t interrupt_debounce; 107 | /* Offset 0x4c: Registers for clearing interrupts */ 108 | gpio_access_tp_t interrupt_clear; 109 | /* Offset 0x50: External port (data input) registers */ 110 | gpio_access_tp_t data_input; 111 | /* Offset 0x54 - 0x5f: Unused registers, 3x4 bytes */ 112 | uint32_t unused_1[3]; 113 | /* Offset 0x60: Sync level registers */ 114 | gpio_access_tp_t sync_level; 115 | /* Offset 0x64: ID code */ 116 | gpio_access_tp_t id_code; 117 | /* Offset 0x68: Interrupt both edge type */ 118 | gpio_access_tp_t interrupt_bothedge; 119 | 120 | } __attribute__((packed, aligned(4))) gpio_t; 121 | 122 | /** 123 | * @brief Bus GPIO object instance 124 | */ 125 | extern volatile gpio_t *const gpio; 126 | 127 | /** 128 | * @brief Gpio initialize 129 | * 130 | * @return Result 131 | * - 0 Success 132 | * - Other Fail 133 | */ 134 | int gpio_init(void); 135 | 136 | /** 137 | * @brief Set Gpio drive mode 138 | * 139 | * @param[in] pin Gpio pin 140 | * @param[in] mode Gpio pin drive mode 141 | */ 142 | void gpio_set_drive_mode(uint8_t pin, gpio_drive_mode_t mode); 143 | 144 | /** 145 | * @brief Get Gpio pin value 146 | * 147 | * @param[in] pin Gpio pin 148 | * @return Pin value 149 | * 150 | * - GPIO_PV_Low Gpio pin low 151 | * - GPIO_PV_High Gpio pin high 152 | */ 153 | gpio_pin_value_t gpio_get_pin(uint8_t pin); 154 | 155 | /** 156 | * @brief Set Gpio pin value 157 | * 158 | * @param[in] pin Gpio pin 159 | * @param[in] value Gpio pin value 160 | */ 161 | void gpio_set_pin(uint8_t pin, gpio_pin_value_t value); 162 | 163 | #ifdef __cplusplus 164 | } 165 | #endif 166 | 167 | #endif /* _DRIVER_GPIO_H */ 168 | 169 | -------------------------------------------------------------------------------- /lib/drivers/include/gpio_common.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _GPIO_COMMON_H 16 | #define _GPIO_COMMON_H 17 | 18 | #ifdef __cplusplus 19 | extern "C" { 20 | #endif 21 | 22 | typedef enum _gpio_drive_mode 23 | { 24 | GPIO_DM_INPUT, 25 | GPIO_DM_INPUT_PULL_DOWN, 26 | GPIO_DM_INPUT_PULL_UP, 27 | GPIO_DM_OUTPUT, 28 | } gpio_drive_mode_t; 29 | 30 | typedef enum _gpio_pin_edge 31 | { 32 | GPIO_PE_NONE, 33 | GPIO_PE_FALLING, 34 | GPIO_PE_RISING, 35 | GPIO_PE_BOTH, 36 | GPIO_PE_LOW, 37 | GPIO_PE_HIGH = 8, 38 | } gpio_pin_edge_t; 39 | 40 | typedef enum _gpio_pin_value 41 | { 42 | GPIO_PV_LOW, 43 | GPIO_PV_HIGH 44 | } gpio_pin_value_t; 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif /* _GPIO_COMMON_H */ 51 | 52 | -------------------------------------------------------------------------------- /lib/drivers/include/io.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _DRIVER_IO_H 16 | #define _DRIVER_IO_H 17 | 18 | #include 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #define readb(addr) (*(volatile uint8_t *)(addr)) 25 | #define readw(addr) (*(volatile uint16_t *)(addr)) 26 | #define readl(addr) (*(volatile uint32_t *)(addr)) 27 | #define readq(addr) (*(volatile uint64_t *)(addr)) 28 | 29 | #define writeb(v, addr) \ 30 | { \ 31 | (*(volatile uint8_t *)(addr)) = (v); \ 32 | } 33 | #define writew(v, addr) \ 34 | { \ 35 | (*(volatile uint16_t *)(addr)) = (v); \ 36 | } 37 | #define writel(v, addr) \ 38 | { \ 39 | (*(volatile uint32_t *)(addr)) = (v); \ 40 | } 41 | #define writeq(v, addr) \ 42 | { \ 43 | (*(volatile uint64_t *)(addr)) = (v); \ 44 | } 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif /* _DRIVER_IO_H */ 51 | -------------------------------------------------------------------------------- /lib/drivers/include/pwm.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _DRIVER_PWM_H 16 | #define _DRIVER_PWM_H 17 | 18 | #include 19 | #include 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | typedef enum _pwm_device_number 25 | { 26 | PWM_DEVICE_0, 27 | PWM_DEVICE_1, 28 | PWM_DEVICE_2, 29 | PWM_DEVICE_MAX, 30 | } pwm_device_number_t; 31 | 32 | typedef enum _pwm_channel_number 33 | { 34 | PWM_CHANNEL_0, 35 | PWM_CHANNEL_1, 36 | PWM_CHANNEL_2, 37 | PWM_CHANNEL_3, 38 | PWM_CHANNEL_MAX, 39 | } pwm_channel_number_t; 40 | 41 | /** 42 | * @brief Init pwm timer 43 | * 44 | * @param[in] timer timer 45 | */ 46 | void pwm_init(pwm_device_number_t pwm_number); 47 | 48 | /** 49 | * @brief Enable timer 50 | * 51 | * @param[in] timer timer 52 | * @param[in] channel channel 53 | * @param[in] enable Enable or disable 54 | * 55 | */ 56 | void pwm_set_enable(pwm_device_number_t pwm_number, pwm_channel_number_t channel, int enable); 57 | 58 | /** 59 | * @brief Set pwm duty 60 | * 61 | * @param[in] timer timer 62 | * @param[in] channel channel 63 | * @param[in] frequency pwm frequency 64 | * @param[in] duty duty 65 | * 66 | */ 67 | double pwm_set_frequency(pwm_device_number_t pwm_number, pwm_channel_number_t channel, double frequency, double duty); 68 | 69 | 70 | #ifdef __cplusplus 71 | } 72 | #endif 73 | 74 | #endif /* _DRIVER_PWM_H */ 75 | -------------------------------------------------------------------------------- /lib/drivers/include/sha256.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _SHA256_H 16 | #define _SHA256_H 17 | #include 18 | #include 19 | 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | #define ENABLE_SHA (0x1) 25 | #define SHA256_BIG_ENDIAN (0x1) 26 | 27 | #define SHA256_HASH_LEN 32 28 | #define SHA256_HASH_WORDS 8 29 | #define SHA256_BLOCK_LEN 64L 30 | 31 | typedef struct _sha_num_reg 32 | { 33 | /* The total amount of data calculated by SHA256 is set by this register, and the smallest unit is 512bit. */ 34 | uint32_t sha_data_cnt : 16; 35 | /* currently calculated block number. 512bit=1block*/ 36 | uint32_t sha_data_num : 16; 37 | } __attribute__((packed, aligned(4))) sha_num_reg_t; 38 | 39 | typedef struct _sha_function_reg_0 40 | { 41 | /* write:SHA256 enable register. read:Calculation completed flag */ 42 | uint32_t sha_en : 1; 43 | uint32_t reserved00 : 7; 44 | /* SHA256 calculation overflow flag */ 45 | uint32_t sha_overflow : 1; 46 | uint32_t reserved01 : 7; 47 | /* Endian setting; b'0:little endian b'1:big endian */ 48 | uint32_t sha_endian : 1; 49 | uint32_t reserved02 : 15; 50 | } __attribute__((packed, aligned(4))) sha_function_reg_0_t; 51 | 52 | typedef struct _sha_function_reg_1 53 | { 54 | /* Sha and DMA handshake signals enable.b'1:enable;b'0:disable */ 55 | uint32_t dma_en : 1; 56 | uint32_t reserved10 : 7; 57 | /* b'1:sha256 fifo is full; b'0:not full */ 58 | uint32_t fifo_in_full : 1; 59 | uint32_t reserved11 : 23; 60 | } __attribute__((packed, aligned(4))) sha_function_reg_1_t; 61 | 62 | typedef struct _sha256 63 | { 64 | /* Calculated sha256 return value. */ 65 | uint32_t sha_result[8]; 66 | /* SHA256 input data from this register. */ 67 | uint32_t sha_data_in1; 68 | uint32_t reselved0; 69 | sha_num_reg_t sha_num_reg; 70 | sha_function_reg_0_t sha_function_reg_0; 71 | uint32_t reserved1; 72 | sha_function_reg_1_t sha_function_reg_1; 73 | } __attribute__((packed, aligned(4))) sha256_t; 74 | 75 | typedef struct _sha256_context 76 | { 77 | size_t total_len; 78 | size_t buffer_len; 79 | union 80 | { 81 | uint32_t words[16]; 82 | uint8_t bytes[64]; 83 | } buffer; 84 | } sha256_context_t; 85 | 86 | /** 87 | * @brief Init SHA256 calculation context 88 | * 89 | * @param[in] context SHA256 context object 90 | * 91 | */ 92 | void sha256_init(sha256_context_t *context, size_t input_len); 93 | 94 | /** 95 | * @brief Called repeatedly with chunks of the message to be hashed 96 | * 97 | * @param[in] context SHA256 context object 98 | * @param[in] data_buf data chunk to be hashed 99 | * @param[in] buf_len length of data chunk 100 | * 101 | */ 102 | void sha256_update(sha256_context_t *context, const void *input, size_t input_len); 103 | 104 | /** 105 | * @brief Finish SHA256 hash process, output the result. 106 | * 107 | * @param[in] context SHA256 context object 108 | * @param[out] output The buffer where SHA256 hash will be output 109 | * 110 | */ 111 | void sha256_final(sha256_context_t *context, uint8_t *output); 112 | 113 | /** 114 | * @brief Simple SHA256 hash once. 115 | * 116 | * @param[in] data Data will be hashed 117 | * @param[in] data_len Data length 118 | * @param[out] output Output buffer 119 | * 120 | */ 121 | void sha256_hard_calculate(const uint8_t *input, size_t input_len, uint8_t *output); 122 | 123 | #ifdef __cplusplus 124 | } 125 | #endif 126 | 127 | #endif 128 | 129 | -------------------------------------------------------------------------------- /lib/drivers/include/timer.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _DRIVER_TIMER_H 16 | #define _DRIVER_TIMER_H 17 | 18 | #include 19 | #include 20 | #ifdef __cplusplus 21 | extern "C" { 22 | #endif 23 | 24 | /* clang-format off */ 25 | typedef struct _timer_channel 26 | { 27 | /* TIMER_N Load Count Register (0x00+(N-1)*0x14) */ 28 | volatile uint32_t load_count; 29 | /* TIMER_N Current Value Register (0x04+(N-1)*0x14) */ 30 | volatile uint32_t current_value; 31 | /* TIMER_N Control Register (0x08+(N-1)*0x14) */ 32 | volatile uint32_t control; 33 | /* TIMER_N Interrupt Clear Register (0x0c+(N-1)*0x14) */ 34 | volatile uint32_t eoi; 35 | /* TIMER_N Interrupt Status Register (0x10+(N-1)*0x14) */ 36 | volatile uint32_t intr_stat; 37 | } __attribute__((packed, aligned(4))) timer_channel_t; 38 | 39 | typedef struct _kendryte_timer 40 | { 41 | /* TIMER_N Register (0x00-0x4c) */ 42 | volatile timer_channel_t channel[4]; 43 | /* reserverd (0x50-0x9c) */ 44 | volatile uint32_t resv1[20]; 45 | /* TIMER Interrupt Status Register (0xa0) */ 46 | volatile uint32_t intr_stat; 47 | /* TIMER Interrupt Clear Register (0xa4) */ 48 | volatile uint32_t eoi; 49 | /* TIMER Raw Interrupt Status Register (0xa8) */ 50 | volatile uint32_t raw_intr_stat; 51 | /* TIMER Component Version Register (0xac) */ 52 | volatile uint32_t comp_version; 53 | /* TIMER_N Load Count2 Register (0xb0-0xbc) */ 54 | volatile uint32_t load_count2[4]; 55 | } __attribute__((packed, aligned(4))) kendryte_timer_t; 56 | 57 | typedef enum _timer_deivce_number 58 | { 59 | TIMER_DEVICE_0, 60 | TIMER_DEVICE_1, 61 | TIMER_DEVICE_2, 62 | TIMER_DEVICE_MAX, 63 | } timer_device_number_t; 64 | 65 | typedef enum _timer_channel_number 66 | { 67 | TIMER_CHANNEL_0, 68 | TIMER_CHANNEL_1, 69 | TIMER_CHANNEL_2, 70 | TIMER_CHANNEL_3, 71 | TIMER_CHANNEL_MAX, 72 | } timer_channel_number_t; 73 | 74 | /* TIMER Control Register */ 75 | #define TIMER_CR_ENABLE 0x00000001 76 | #define TIMER_CR_MODE_MASK 0x00000002 77 | #define TIMER_CR_FREE_MODE 0x00000000 78 | #define TIMER_CR_USER_MODE 0x00000002 79 | #define TIMER_CR_INTERRUPT_MASK 0x00000004 80 | #define TIMER_CR_PWM_ENABLE 0x00000008 81 | /* clang-format on */ 82 | 83 | extern volatile kendryte_timer_t *const timer[3]; 84 | 85 | /** 86 | * @brief Definitions for the timer callbacks 87 | */ 88 | typedef int (*timer_callback_t)(void *ctx); 89 | 90 | /** 91 | * @brief Set timer timeout 92 | * 93 | * @param[in] timer timer 94 | * @param[in] channel channel 95 | * @param[in] nanoseconds timeout 96 | * 97 | * @return the real timeout 98 | */ 99 | size_t timer_set_interval(timer_device_number_t timer_number, timer_channel_number_t channel, size_t nanoseconds); 100 | 101 | /** 102 | * @brief Init timer 103 | * 104 | * @param[in] timer timer 105 | */ 106 | void timer_init(timer_device_number_t timer_number); 107 | 108 | /** 109 | * @brief [DEPRECATED] Set timer timeout function 110 | * 111 | * @param[in] timer timer 112 | * @param[in] channel channel 113 | * @param[in] func timeout function 114 | * @param[in] priority interrupt priority 115 | * 116 | */ 117 | void timer_set_irq(timer_device_number_t timer_number, timer_channel_number_t channel, void(*func)(), uint32_t priority); 118 | 119 | /** 120 | * @brief Register timer interrupt user callback function 121 | * 122 | * @param[in] device The timer device number 123 | * @param[in] channel The channel 124 | * @param[in] is_one_shot Indicates if single shot 125 | * @param[in] priority The priority 126 | * @param[in] callback The callback function 127 | * @param[in] ctx The context 128 | * 129 | * @return result 130 | * - 0 Success 131 | * - Other Fail 132 | */ 133 | int timer_irq_register(timer_device_number_t device, timer_channel_number_t channel, int is_single_shot, uint32_t priority, timer_callback_t callback, void *ctx); 134 | 135 | /** 136 | * @brief Deregister timer interrupt user callback function 137 | * 138 | * @param[in] device The timer device number 139 | * @param[in] channel The channel 140 | * 141 | * @return result 142 | * - 0 Success 143 | * - Other Fail 144 | */ 145 | int timer_irq_unregister(timer_device_number_t device, timer_channel_number_t channel); 146 | 147 | /** 148 | * @brief Enable timer 149 | * 150 | * @param[in] timer timer 151 | * @param[in] channel channel 152 | * @param[in] enable Enable or disable 153 | * 154 | */ 155 | void timer_set_enable(timer_device_number_t timer_number, timer_channel_number_t channel, uint32_t enable); 156 | 157 | #ifdef __cplusplus 158 | } 159 | #endif 160 | 161 | #endif /* _DRIVER_TIMER_H */ 162 | -------------------------------------------------------------------------------- /lib/drivers/include/wdt.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _DRIVER_WDT_H 16 | #define _DRIVER_WDT_H 17 | 18 | #include 19 | #include 20 | #include 21 | 22 | #ifdef __cplusplus 23 | extern "C" { 24 | #endif 25 | 26 | /* clang-format off */ 27 | typedef struct _wdt 28 | { 29 | /* WDT Control Register (0x00) */ 30 | volatile uint32_t cr; 31 | /* WDT Timeout Range Register (0x04) */ 32 | volatile uint32_t torr; 33 | /* WDT Current Counter Value Register (0x08) */ 34 | volatile uint32_t ccvr; 35 | /* WDT Counter Restart Register (0x0c) */ 36 | volatile uint32_t crr; 37 | /* WDT Interrupt Status Register (0x10) */ 38 | volatile uint32_t stat; 39 | /* WDT Interrupt Clear Register (0x14) */ 40 | volatile uint32_t eoi; 41 | /* reserverd (0x18) */ 42 | volatile uint32_t resv1; 43 | /* WDT Protection level Register (0x1c) */ 44 | volatile uint32_t prot_level; 45 | /* reserved (0x20-0xe0) */ 46 | volatile uint32_t resv4[49]; 47 | /* WDT Component Parameters Register 5 (0xe4) */ 48 | volatile uint32_t comp_param_5; 49 | /* WDT Component Parameters Register 4 (0xe8) */ 50 | volatile uint32_t comp_param_4; 51 | /* WDT Component Parameters Register 3 (0xec) */ 52 | volatile uint32_t comp_param_3; 53 | /* WDT Component Parameters Register 2 (0xf0) */ 54 | volatile uint32_t comp_param_2; 55 | /* WDT Component Parameters Register 1 (0xf4) */ 56 | volatile uint32_t comp_param_1; 57 | /* WDT Component Version Register (0xf8) */ 58 | volatile uint32_t comp_version; 59 | /* WDT Component Type Register (0xfc) */ 60 | volatile uint32_t comp_type; 61 | } __attribute__((packed, aligned(4))) wdt_t; 62 | 63 | typedef enum _wdt_device_number 64 | { 65 | WDT_DEVICE_0, 66 | WDT_DEVICE_1, 67 | WDT_DEVICE_MAX, 68 | } wdt_device_number_t; 69 | 70 | 71 | #define WDT_RESET_ALL 0x00000000U 72 | #define WDT_RESET_CPU 0x00000001U 73 | 74 | /* WDT Control Register */ 75 | #define WDT_CR_ENABLE 0x00000001U 76 | #define WDT_CR_RMOD_MASK 0x00000002U 77 | #define WDT_CR_RMOD_RESET 0x00000000U 78 | #define WDT_CR_RMOD_INTERRUPT 0x00000002U 79 | #define WDT_CR_RPL_MASK 0x0000001CU 80 | #define WDT_CR_RPL(x) ((x) << 2) 81 | /* WDT Timeout Range Register */ 82 | #define WDT_TORR_TOP_MASK 0x000000FFU 83 | #define WDT_TORR_TOP(x) ((x) << 4 | (x) << 0) 84 | /* WDT Current Counter Value Register */ 85 | #define WDT_CCVR_MASK 0xFFFFFFFFU 86 | /* WDT Counter Restart Register */ 87 | #define WDT_CRR_MASK 0x00000076U 88 | /* WDT Interrupt Status Register */ 89 | #define WDT_STAT_MASK 0x00000001U 90 | /* WDT Interrupt Clear Register */ 91 | #define WDT_EOI_MASK 0x00000001U 92 | /* WDT Protection level Register */ 93 | #define WDT_PROT_LEVEL_MASK 0x00000007U 94 | /* WDT Component Parameter Register 5 */ 95 | #define WDT_COMP_PARAM_5_CP_WDT_USER_TOP_MAX_MASK 0xFFFFFFFFU 96 | /* WDT Component Parameter Register 4 */ 97 | #define WDT_COMP_PARAM_4_CP_WDT_USER_TOP_INIT_MAX_MASK 0xFFFFFFFFU 98 | /* WDT Component Parameter Register 3 */ 99 | #define WDT_COMP_PARAM_3_CD_WDT_TOP_RST_MASK 0xFFFFFFFFU 100 | /* WDT Component Parameter Register 2 */ 101 | #define WDT_COMP_PARAM_3_CP_WDT_CNT_RST_MASK 0xFFFFFFFFU 102 | /* WDT Component Parameter Register 1 */ 103 | #define WDT_COMP_PARAM_1_WDT_ALWAYS_EN_MASK 0x00000001U 104 | #define WDT_COMP_PARAM_1_WDT_DFLT_RMOD_MASK 0x00000002U 105 | #define WDT_COMP_PARAM_1_WDT_DUAL_TOP_MASK 0x00000004U 106 | #define WDT_COMP_PARAM_1_WDT_HC_RMOD_MASK 0x00000008U 107 | #define WDT_COMP_PARAM_1_WDT_HC_RPL_MASK 0x00000010U 108 | #define WDT_COMP_PARAM_1_WDT_HC_TOP_MASK 0x00000020U 109 | #define WDT_COMP_PARAM_1_WDT_USE_FIX_TOP_MASK 0x00000040U 110 | #define WDT_COMP_PARAM_1_WDT_PAUSE_MASK 0x00000080U 111 | #define WDT_COMP_PARAM_1_APB_DATA_WIDTH_MASK 0x00000300U 112 | #define WDT_COMP_PARAM_1_WDT_DFLT_RPL_MASK 0x00001C00U 113 | #define WDT_COMP_PARAM_1_WDT_DFLT_TOP_MASK 0x000F0000U 114 | #define WDT_COMP_PARAM_1_WDT_DFLT_TOP_INIT_MASK 0x00F00000U 115 | #define WDT_COMP_PARAM_1_WDT_CNT_WIDTH_MASK 0x1F000000U 116 | /* WDT Component Version Register */ 117 | #define WDT_COMP_VERSION_MASK 0xFFFFFFFFU 118 | /* WDT Component Type Register */ 119 | #define WDT_COMP_TYPE_MASK 0xFFFFFFFFU 120 | /* clang-format on */ 121 | 122 | /** 123 | * @brief Feed wdt 124 | */ 125 | void wdt_feed(wdt_device_number_t id); 126 | 127 | /** 128 | * @brief Start wdt 129 | * 130 | * @param[in] id Wdt id 0 or 1 131 | * @param[in] time_out_ms Wdt trigger time 132 | * @param[in] on_irq Wdt interrupt callback 133 | * 134 | */ 135 | void wdt_start(wdt_device_number_t id, uint64_t time_out_ms, plic_irq_callback_t on_irq); 136 | 137 | /** 138 | * @brief Start wdt 139 | * 140 | * @param[in] id Wdt id 0 or 1 141 | * @param[in] time_out_ms Wdt trigger time 142 | * @param[in] on_irq Wdt interrupt callback 143 | * @param[in] ctx Param of callback 144 | * 145 | * @return Wdt time 146 | * 147 | */ 148 | uint32_t wdt_init(wdt_device_number_t id, uint64_t time_out_ms, plic_irq_callback_t on_irq, void *ctx); 149 | 150 | /** 151 | * @brief Stop wdt 152 | * 153 | * @param[in] id Wdt id 0 or 1 154 | * 155 | */ 156 | void wdt_stop(wdt_device_number_t id); 157 | 158 | /** 159 | * @brief Clear wdt interrupt 160 | * 161 | * @param[in] id Wdt id 0 or 1 162 | * 163 | */ 164 | void wdt_clear_interrupt(wdt_device_number_t id); 165 | 166 | #ifdef __cplusplus 167 | } 168 | #endif 169 | 170 | #endif /* _DRIVER_WDT_H */ 171 | -------------------------------------------------------------------------------- /lib/drivers/plic.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include 16 | #include 17 | #include "encoding.h" 18 | #include "plic.h" 19 | #include "syscalls.h" 20 | #include "syslog.h" 21 | 22 | volatile plic_t* const plic = (volatile plic_t*)PLIC_BASE_ADDR; 23 | 24 | static plic_instance_t plic_instance[PLIC_NUM_CORES][IRQN_MAX]; 25 | 26 | void plic_init(void) 27 | { 28 | int i = 0; 29 | 30 | /* Get current core id */ 31 | unsigned long core_id = current_coreid(); 32 | 33 | /* Disable all interrupts for the current core. */ 34 | for (i = 0; i < ((PLIC_NUM_SOURCES + 32u) / 32u); i++) 35 | plic->target_enables.target[core_id].enable[i] = 0; 36 | 37 | static uint8_t s_plic_priorities_init_flag = 0; 38 | /* Set priorities to zero. */ 39 | if(s_plic_priorities_init_flag == 0) 40 | { 41 | for (i = 0; i < PLIC_NUM_SOURCES; i++) 42 | plic->source_priorities.priority[i] = 0; 43 | s_plic_priorities_init_flag = 1; 44 | } 45 | 46 | /* Set the threshold to zero. */ 47 | plic->targets.target[core_id].priority_threshold = 0; 48 | 49 | /* Clear PLIC instance for every cores */ 50 | for (i = 0; i < IRQN_MAX; i++) 51 | { 52 | /* clang-format off */ 53 | plic_instance[core_id][i] = (const plic_instance_t){ 54 | .callback = NULL, 55 | .ctx = NULL, 56 | }; 57 | /* clang-format on */ 58 | } 59 | 60 | /* 61 | * A successful claim will also atomically clear the corresponding 62 | * pending bit on the interrupt source. A target can perform a claim 63 | * at any time, even if the EIP is not set. 64 | */ 65 | i = 0; 66 | while (plic->targets.target[core_id].claim_complete > 0 && i < 100) 67 | { 68 | /* This loop will clear pending bit on the interrupt source */ 69 | i++; 70 | } 71 | 72 | /* Enable machine external interrupts. */ 73 | set_csr(mie, MIP_MEIP); 74 | } 75 | 76 | int plic_irq_enable(plic_irq_t irq_number) 77 | { 78 | /* Check parameters */ 79 | if (PLIC_NUM_SOURCES < irq_number || 0 > irq_number) 80 | return -1; 81 | unsigned long core_id = current_coreid(); 82 | /* Get current enable bit array by IRQ number */ 83 | uint32_t current = plic->target_enables.target[core_id].enable[irq_number / 32]; 84 | /* Set enable bit in enable bit array */ 85 | current |= (uint32_t)1 << (irq_number % 32); 86 | /* Write back the enable bit array */ 87 | plic->target_enables.target[core_id].enable[irq_number / 32] = current; 88 | return 0; 89 | } 90 | 91 | int plic_irq_disable(plic_irq_t irq_number) 92 | { 93 | /* Check parameters */ 94 | if (PLIC_NUM_SOURCES < irq_number || 0 > irq_number) 95 | return -1; 96 | unsigned long core_id = current_coreid(); 97 | /* Get current enable bit array by IRQ number */ 98 | uint32_t current = plic->target_enables.target[core_id].enable[irq_number / 32]; 99 | /* Clear enable bit in enable bit array */ 100 | current &= ~((uint32_t)1 << (irq_number % 32)); 101 | /* Write back the enable bit array */ 102 | plic->target_enables.target[core_id].enable[irq_number / 32] = current; 103 | return 0; 104 | } 105 | 106 | int plic_set_priority(plic_irq_t irq_number, uint32_t priority) 107 | { 108 | /* Check parameters */ 109 | if (PLIC_NUM_SOURCES < irq_number || 0 > irq_number) 110 | return -1; 111 | /* Set interrupt priority by IRQ number */ 112 | plic->source_priorities.priority[irq_number] = priority; 113 | return 0; 114 | } 115 | 116 | uint32_t plic_get_priority(plic_irq_t irq_number) 117 | { 118 | /* Check parameters */ 119 | if (PLIC_NUM_SOURCES < irq_number || 0 > irq_number) 120 | return 0; 121 | /* Get interrupt priority by IRQ number */ 122 | return plic->source_priorities.priority[irq_number]; 123 | } 124 | 125 | uint32_t plic_irq_claim(void) 126 | { 127 | unsigned long core_id = current_coreid(); 128 | /* Perform IRQ claim */ 129 | return plic->targets.target[core_id].claim_complete; 130 | } 131 | 132 | int plic_irq_complete(uint32_t source) 133 | { 134 | unsigned long core_id = current_coreid(); 135 | /* Perform IRQ complete */ 136 | plic->targets.target[core_id].claim_complete = source; 137 | return 0; 138 | } 139 | 140 | void plic_irq_register(plic_irq_t irq, plic_irq_callback_t callback, void *ctx) 141 | { 142 | /* Read core id */ 143 | unsigned long core_id = current_coreid(); 144 | /* Set user callback function */ 145 | plic_instance[core_id][irq].callback = callback; 146 | /* Assign user context */ 147 | plic_instance[core_id][irq].ctx = ctx; 148 | } 149 | 150 | void plic_irq_unregister(plic_irq_t irq) 151 | { 152 | /* Just assign NULL to user callback function and context */ 153 | plic_irq_register(irq, NULL, NULL); 154 | } 155 | 156 | void __attribute__((weak, alias("plic_irq_unregister"))) plic_irq_deregister(plic_irq_t irq); 157 | 158 | plic_instance_t (*plic_get_instance(void))[IRQN_MAX] 159 | { 160 | return plic_instance; 161 | } 162 | 163 | /*Entry Point for PLIC Interrupt Handler*/ 164 | uintptr_t __attribute__((weak)) 165 | handle_irq_m_ext(uintptr_t cause, uintptr_t epc) 166 | { 167 | /* 168 | * After the highest-priority pending interrupt is claimed by a target 169 | * and the corresponding IP bit is cleared, other lower-priority 170 | * pending interrupts might then become visible to the target, and so 171 | * the PLIC EIP bit might not be cleared after a claim. The interrupt 172 | * handler can check the local meip/heip/seip/ueip bits before exiting 173 | * the handler, to allow more efficient service of other interrupts 174 | * without first restoring the interrupted context and taking another 175 | * interrupt trap. 176 | */ 177 | if (read_csr(mip) & MIP_MEIP) 178 | { 179 | /* Get current core id */ 180 | uint64_t core_id = current_coreid(); 181 | /* Get primitive interrupt enable flag */ 182 | uint64_t ie_flag = read_csr(mie); 183 | /* Get current IRQ num */ 184 | uint32_t int_num = plic->targets.target[core_id].claim_complete; 185 | /* Get primitive IRQ threshold */ 186 | uint32_t int_threshold = plic->targets.target[core_id].priority_threshold; 187 | /* Set new IRQ threshold = current IRQ threshold */ 188 | plic->targets.target[core_id].priority_threshold = plic->source_priorities.priority[int_num]; 189 | /* Disable software interrupt and timer interrupt */ 190 | clear_csr(mie, MIP_MTIP | MIP_MSIP); 191 | /* Enable global interrupt */ 192 | set_csr(mstatus, MSTATUS_MIE); 193 | if (plic_instance[core_id][int_num].callback) 194 | plic_instance[core_id][int_num].callback( 195 | plic_instance[core_id][int_num].ctx); 196 | /* Perform IRQ complete */ 197 | plic->targets.target[core_id].claim_complete = int_num; 198 | /* Disable global interrupt */ 199 | clear_csr(mstatus, MSTATUS_MIE); 200 | /* Set MPIE and MPP flag used to MRET instructions restore MIE flag */ 201 | set_csr(mstatus, MSTATUS_MPIE | MSTATUS_MPP); 202 | /* Restore primitive interrupt enable flag */ 203 | write_csr(mie, ie_flag); 204 | /* Restore primitive IRQ threshold */ 205 | plic->targets.target[core_id].priority_threshold = int_threshold; 206 | } 207 | 208 | return epc; 209 | } 210 | -------------------------------------------------------------------------------- /lib/drivers/pwm.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include "timer.h" 16 | #include "pwm.h" 17 | #include "sysctl.h" 18 | #include 19 | #include "utils.h" 20 | #include "plic.h" 21 | #include "io.h" 22 | 23 | void pwm_init(pwm_device_number_t pwm_number) 24 | { 25 | sysctl_clock_enable(SYSCTL_CLOCK_TIMER0 + pwm_number); 26 | } 27 | 28 | void pwm_set_enable(pwm_device_number_t pwm_number, pwm_channel_number_t channel, int enable) 29 | { 30 | if (enable) 31 | { 32 | if (timer[pwm_number]->channel[channel].load_count == 0) 33 | timer[pwm_number]->channel[channel].load_count = 1; 34 | if (timer[pwm_number]->load_count2[channel] == 0) 35 | timer[pwm_number]->load_count2[channel] = 1; 36 | timer[pwm_number]->channel[channel].control = TIMER_CR_INTERRUPT_MASK | TIMER_CR_PWM_ENABLE | TIMER_CR_USER_MODE | TIMER_CR_ENABLE; 37 | } 38 | else 39 | { 40 | timer[pwm_number]->channel[channel].control = TIMER_CR_INTERRUPT_MASK; 41 | } 42 | } 43 | 44 | double pwm_set_frequency(pwm_device_number_t pwm_number, pwm_channel_number_t channel, double frequency, double duty) 45 | { 46 | uint32_t clk_freq = sysctl_clock_get_freq(SYSCTL_CLOCK_TIMER0 + pwm_number); 47 | 48 | int32_t periods = (int32_t)(clk_freq / frequency); 49 | configASSERT(periods > 0 && periods <= INT32_MAX); 50 | frequency = clk_freq / (double)periods; 51 | 52 | uint32_t percent = (uint32_t)(duty * periods); 53 | timer[pwm_number]->channel[channel].load_count = periods - percent; 54 | timer[pwm_number]->load_count2[channel] = percent; 55 | 56 | return frequency; 57 | } 58 | 59 | -------------------------------------------------------------------------------- /lib/drivers/sha256.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include 16 | #include "sysctl.h" 17 | #include "sha256.h" 18 | #include "utils.h" 19 | 20 | #define ROTL(x, n) (((x) << (n)) | ((x) >> (32 - (n)))) 21 | #define ROTR(x, n) (((x) >> (n)) | ((x) << (32 - (n)))) 22 | #define BYTESWAP(x) ((ROTR((x), 8) & 0xff00ff00L) | (ROTL((x), 8) & 0x00ff00ffL)) 23 | #define BYTESWAP64(x) byteswap64(x) 24 | 25 | volatile sha256_t* const sha256 = (volatile sha256_t*)SHA256_BASE_ADDR; 26 | static const uint8_t padding[64] = 27 | { 28 | 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 29 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 30 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 31 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 32 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 33 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 34 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 35 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 36 | }; 37 | 38 | static inline uint64_t byteswap64(uint64_t x) 39 | { 40 | uint32_t a = (uint32_t)(x >> 32); 41 | uint32_t b = (uint32_t)x; 42 | return ((uint64_t)BYTESWAP(b) << 32) | (uint64_t)BYTESWAP(a); 43 | } 44 | 45 | void sha256_init(sha256_context_t *context, size_t input_len) 46 | { 47 | sysctl_clock_enable(SYSCTL_CLOCK_SHA); 48 | sysctl_reset(SYSCTL_RESET_SHA); 49 | 50 | sha256->sha_num_reg.sha_data_cnt = (uint32_t)((input_len + SHA256_BLOCK_LEN + 8) / SHA256_BLOCK_LEN); 51 | sha256->sha_function_reg_1.dma_en = 0x0; 52 | sha256->sha_function_reg_0.sha_endian = SHA256_BIG_ENDIAN; 53 | sha256->sha_function_reg_0.sha_en = ENABLE_SHA; 54 | context->total_len = 0L; 55 | context->buffer_len = 0L; 56 | } 57 | 58 | void sha256_update(sha256_context_t *context, const void *input, size_t input_len) 59 | { 60 | const uint8_t *data = input; 61 | size_t buffer_bytes_left; 62 | size_t bytes_to_copy; 63 | uint32_t i; 64 | 65 | while (input_len) 66 | { 67 | buffer_bytes_left = SHA256_BLOCK_LEN - context->buffer_len; 68 | bytes_to_copy = buffer_bytes_left; 69 | if (bytes_to_copy > input_len) 70 | bytes_to_copy = input_len; 71 | memcpy(&context->buffer.bytes[context->buffer_len], data, bytes_to_copy); 72 | context->total_len += bytes_to_copy * 8L; 73 | context->buffer_len += bytes_to_copy; 74 | data += bytes_to_copy; 75 | input_len -= bytes_to_copy; 76 | if (context->buffer_len == SHA256_BLOCK_LEN) 77 | { 78 | for (i = 0; i < 16; i++) 79 | { 80 | while (sha256->sha_function_reg_1.fifo_in_full) 81 | ; 82 | sha256->sha_data_in1 = context->buffer.words[i]; 83 | } 84 | context->buffer_len = 0L; 85 | } 86 | } 87 | } 88 | 89 | void sha256_final(sha256_context_t *context, uint8_t *output) 90 | { 91 | size_t bytes_to_pad; 92 | size_t length_pad; 93 | uint32_t i; 94 | 95 | bytes_to_pad = 120L - context->buffer_len; 96 | if (bytes_to_pad > 64L) 97 | bytes_to_pad -= 64L; 98 | length_pad = BYTESWAP64(context->total_len); 99 | sha256_update(context, padding, bytes_to_pad); 100 | sha256_update(context, &length_pad, 8L); 101 | while (!(sha256->sha_function_reg_0.sha_en)) 102 | ; 103 | if (output) 104 | { 105 | for (i = 0; i < SHA256_HASH_WORDS; i++) 106 | { 107 | *((uint32_t *)output) = sha256->sha_result[SHA256_HASH_WORDS - i - 1]; 108 | output += 4; 109 | } 110 | } 111 | } 112 | 113 | void sha256_hard_calculate(const uint8_t *input, size_t input_len, uint8_t *output) 114 | { 115 | sha256_context_t sha; 116 | sha256_init(&sha, input_len); 117 | sha256_update(&sha, input, input_len); 118 | sha256_final(&sha, output); 119 | } 120 | 121 | -------------------------------------------------------------------------------- /lib/drivers/uarths.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include 18 | #include "uarths.h" 19 | #include "sysctl.h" 20 | #include "encoding.h" 21 | 22 | volatile uarths_t *const uarths = (volatile uarths_t *)UARTHS_BASE_ADDR; 23 | 24 | typedef struct _uarths_instance 25 | { 26 | plic_irq_callback_t callback; 27 | void *ctx; 28 | uarths_interrupt_mode_t uarths_interrupt_mode; 29 | } uarths_instance_t; 30 | 31 | uarths_instance_t g_uarths_instance; 32 | 33 | uarths_interrupt_mode_t uarths_get_interrupt_mode(void) 34 | { 35 | uint32_t v_rx_interrupt = uarths->ip.rxwm; 36 | uint32_t v_tx_interrupt = uarths->ip.txwm; 37 | return (v_rx_interrupt << 1) | v_tx_interrupt; 38 | } 39 | 40 | int uarths_irq_callback(void *ctx) 41 | { 42 | uarths_instance_t *uart_context = (uarths_instance_t *)ctx; 43 | 44 | if(uart_context->callback) 45 | uart_context->callback(uart_context->ctx); 46 | return 0; 47 | } 48 | 49 | void uarths_set_interrupt_cnt(uarths_interrupt_mode_t interrupt_mode, uint8_t cnt) 50 | { 51 | switch(interrupt_mode) 52 | { 53 | case UARTHS_SEND: 54 | uarths->txctrl.txcnt = cnt; 55 | break; 56 | case UARTHS_RECEIVE: 57 | uarths->rxctrl.rxcnt = cnt; 58 | break; 59 | case UARTHS_SEND_RECEIVE: 60 | default: 61 | uarths->txctrl.txcnt = cnt; 62 | uarths->rxctrl.rxcnt = cnt; 63 | break; 64 | } 65 | } 66 | 67 | void uarths_set_irq(uarths_interrupt_mode_t interrupt_mode, plic_irq_callback_t uarths_callback, void *ctx, uint32_t priority) 68 | { 69 | g_uarths_instance.callback = uarths_callback; 70 | g_uarths_instance.ctx = ctx; 71 | 72 | switch(interrupt_mode) 73 | { 74 | case UARTHS_SEND: 75 | uarths->ie.txwm = 1; 76 | uarths->ie.rxwm = 0; 77 | break; 78 | case UARTHS_RECEIVE: 79 | uarths->ie.txwm = 0; 80 | uarths->ie.rxwm = 1; 81 | break; 82 | default: 83 | uarths->ie.txwm = 1; 84 | uarths->ie.rxwm = 1; 85 | break; 86 | } 87 | g_uarths_instance.uarths_interrupt_mode = interrupt_mode; 88 | 89 | plic_set_priority(IRQN_UARTHS_INTERRUPT, priority); 90 | plic_irq_register(IRQN_UARTHS_INTERRUPT, uarths_irq_callback, &g_uarths_instance); 91 | plic_irq_enable(IRQN_UARTHS_INTERRUPT); 92 | } 93 | 94 | int uarths_putchar(char c) 95 | { 96 | while (uarths->txdata.full) 97 | continue; 98 | uarths->txdata.data = (uint8_t)c; 99 | 100 | return (c & 0xff); 101 | } 102 | 103 | int uarths_getchar(void) 104 | { 105 | /* while not empty */ 106 | uarths_rxdata_t recv = uarths->rxdata; 107 | 108 | if (recv.empty) 109 | return EOF; 110 | else 111 | return (recv.data & 0xff); 112 | } 113 | 114 | /* [Deprecated] this function will remove in future */ 115 | int uarths_getc(void) __attribute__ ((weak, alias ("uarths_getchar"))); 116 | 117 | size_t uarths_receive_data(uint8_t *buf, size_t buf_len) 118 | { 119 | size_t i; 120 | for(i = 0; i < buf_len; i++) 121 | { 122 | uarths_rxdata_t recv = uarths->rxdata; 123 | if(recv.empty) 124 | break; 125 | else 126 | buf[i] = (recv.data & 0xFF); 127 | } 128 | return i; 129 | } 130 | 131 | size_t uarths_send_data(const uint8_t *buf, size_t buf_len) 132 | { 133 | size_t write = 0; 134 | while (write < buf_len) 135 | { 136 | uarths_putchar(*buf++); 137 | write++; 138 | } 139 | return write; 140 | } 141 | 142 | int uarths_puts(const char *s) 143 | { 144 | while (*s) 145 | if (uarths_putchar(*s++) != 0) 146 | return -1; 147 | return 0; 148 | } 149 | 150 | void uarths_init(void) 151 | { 152 | uint32_t freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU); 153 | uint16_t div = freq / 115200 - 1; 154 | 155 | /* Set UART registers */ 156 | uarths->div.div = div; 157 | uarths->txctrl.txen = 1; 158 | uarths->rxctrl.rxen = 1; 159 | uarths->txctrl.txcnt = 0; 160 | uarths->rxctrl.rxcnt = 0; 161 | uarths->ip.txwm = 1; 162 | uarths->ip.rxwm = 1; 163 | uarths->ie.txwm = 0; 164 | uarths->ie.rxwm = 1; 165 | } 166 | 167 | void uarths_config(uint32_t baud_rate, uarths_stopbit_t stopbit) 168 | { 169 | uint32_t freq = sysctl_clock_get_freq(SYSCTL_CLOCK_CPU); 170 | uint16_t div = freq / baud_rate - 1; 171 | uarths->div.div = div; 172 | uarths->txctrl.nstop = stopbit; 173 | } 174 | 175 | -------------------------------------------------------------------------------- /lib/drivers/utils.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include 16 | #include "encoding.h" 17 | #include "utils.h" 18 | 19 | void set_bit(volatile uint32_t *bits, uint32_t mask, uint32_t value) 20 | { 21 | uint32_t org = (*bits) & ~mask; 22 | *bits = org | (value & mask); 23 | } 24 | 25 | void set_bit_offset(volatile uint32_t *bits, uint32_t mask, size_t offset, uint32_t value) 26 | { 27 | set_bit(bits, mask << offset, value << offset); 28 | } 29 | 30 | void set_gpio_bit(volatile uint32_t *bits, size_t offset, uint32_t value) 31 | { 32 | set_bit_offset(bits, 1, offset, value); 33 | } 34 | 35 | uint32_t get_bit(volatile uint32_t *bits, uint32_t mask, size_t offset) 36 | { 37 | return ((*bits) & (mask << offset)) >> offset; 38 | } 39 | 40 | uint32_t get_gpio_bit(volatile uint32_t *bits, size_t offset) 41 | { 42 | return get_bit(bits, 1, offset); 43 | } 44 | 45 | -------------------------------------------------------------------------------- /lib/drivers/wdt.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include "wdt.h" 16 | #include "platform.h" 17 | #include "stddef.h" 18 | #include "utils.h" 19 | #include "sysctl.h" 20 | #include "plic.h" 21 | #include "math.h" 22 | 23 | volatile wdt_t *const wdt[2] = 24 | { 25 | (volatile wdt_t *)WDT0_BASE_ADDR, 26 | (volatile wdt_t *)WDT1_BASE_ADDR 27 | }; 28 | 29 | static void wdt_enable(wdt_device_number_t id) 30 | { 31 | wdt[id]->crr = WDT_CRR_MASK; 32 | wdt[id]->cr |= WDT_CR_ENABLE; 33 | } 34 | 35 | static void wdt_disable(wdt_device_number_t id) 36 | { 37 | wdt[id]->crr = WDT_CRR_MASK; 38 | wdt[id]->cr &= (~WDT_CR_ENABLE); 39 | } 40 | 41 | static void wdt_set_timeout(wdt_device_number_t id, uint8_t timeout) 42 | { 43 | wdt[id]->torr = WDT_TORR_TOP(timeout); 44 | } 45 | 46 | static void wdt_response_mode(wdt_device_number_t id, uint8_t mode) 47 | { 48 | wdt[id]->cr &= (~WDT_CR_RMOD_MASK); 49 | wdt[id]->cr |= mode; 50 | } 51 | 52 | static uint64_t wdt_get_pclk(wdt_device_number_t id) 53 | { 54 | return id ? sysctl_clock_get_freq(SYSCTL_CLOCK_WDT1) : sysctl_clock_get_freq(SYSCTL_CLOCK_WDT0); 55 | } 56 | 57 | static uint8_t wdt_get_top(wdt_device_number_t id, uint64_t timeout_ms) 58 | { 59 | uint64_t wdt_clk = wdt_get_pclk(id); 60 | uint64_t ret = (timeout_ms * wdt_clk / 1000) >> 16; 61 | if (ret) 62 | ret = (uint32_t)log2(ret); 63 | if (ret > 0xf) 64 | ret = 0xf; 65 | return (uint8_t)ret; 66 | } 67 | 68 | void wdt_feed(wdt_device_number_t id) 69 | { 70 | wdt[id]->crr = WDT_CRR_MASK; 71 | } 72 | 73 | void wdt_clear_interrupt(wdt_device_number_t id) 74 | { 75 | wdt[id]->eoi = wdt[id]->eoi; 76 | } 77 | 78 | void wdt_start(wdt_device_number_t id, uint64_t time_out_ms, plic_irq_callback_t on_irq) 79 | { 80 | sysctl_reset(id ? SYSCTL_RESET_WDT1 : SYSCTL_RESET_WDT0); 81 | sysctl_clock_set_threshold(id ? SYSCTL_THRESHOLD_WDT1 : SYSCTL_THRESHOLD_WDT0, 0); 82 | sysctl_clock_enable(id ? SYSCTL_CLOCK_WDT1 : SYSCTL_CLOCK_WDT0); 83 | 84 | plic_set_priority(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT, 1); 85 | plic_irq_enable(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT); 86 | plic_irq_register(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT, on_irq, NULL); 87 | 88 | wdt_response_mode(id, WDT_CR_RMOD_INTERRUPT); 89 | uint8_t m_top = wdt_get_top(id, time_out_ms); 90 | wdt_set_timeout(id, m_top); 91 | wdt_enable(id); 92 | } 93 | 94 | uint32_t wdt_init(wdt_device_number_t id, uint64_t time_out_ms, plic_irq_callback_t on_irq, void *ctx) 95 | { 96 | sysctl_reset(id ? SYSCTL_RESET_WDT1 : SYSCTL_RESET_WDT0); 97 | sysctl_clock_set_threshold(id ? SYSCTL_THRESHOLD_WDT1 : SYSCTL_THRESHOLD_WDT0, 0); 98 | sysctl_clock_enable(id ? SYSCTL_CLOCK_WDT1 : SYSCTL_CLOCK_WDT0); 99 | 100 | plic_set_priority(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT, 1); 101 | plic_irq_enable(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT); 102 | plic_irq_register(id ? IRQN_WDT1_INTERRUPT : IRQN_WDT0_INTERRUPT, on_irq, ctx); 103 | 104 | wdt_response_mode(id, WDT_CR_RMOD_INTERRUPT); 105 | uint8_t m_top = wdt_get_top(id, time_out_ms); 106 | wdt_set_timeout(id, m_top); 107 | wdt_enable(id); 108 | return (1UL << (m_top + 16 + 1)) * 1000UL / wdt_get_pclk(id); 109 | } 110 | 111 | void wdt_stop(wdt_device_number_t id) 112 | { 113 | wdt_disable(id); 114 | } 115 | 116 | -------------------------------------------------------------------------------- /lib/utils/include/syslog.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _SYSLOG_H 16 | #define _SYSLOG_H 17 | 18 | #include 19 | #include 20 | #include "printf.h" 21 | #include "encoding.h" 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /** 28 | * @brief Logging library 29 | * 30 | * Log library has two ways of managing log verbosity: compile time, set via 31 | * menuconfig 32 | * 33 | * At compile time, filtering is done using CONFIG_LOG_DEFAULT_LEVEL macro, set via 34 | * menuconfig. All logging statments for levels higher than CONFIG_LOG_DEFAULT_LEVEL 35 | * will be removed by the preprocessor. 36 | * 37 | * 38 | * How to use this library: 39 | * 40 | * In each C file which uses logging functionality, define TAG variable like this: 41 | * 42 | * static const char *TAG = "MODULE_NAME"; 43 | * 44 | * then use one of logging macros to produce output, e.g: 45 | * 46 | * LOGW(TAG, "Interrupt error %d", error); 47 | * 48 | * Several macros are available for different verbosity levels: 49 | * 50 | * LOGE - error 51 | * LOGW - warning 52 | * LOGI - info 53 | * LOGD - debug 54 | * LOGV - verbose 55 | * 56 | * To override default verbosity level at file or component scope, define LOG_LEVEL macro. 57 | * At file scope, define it before including esp_log.h, e.g.: 58 | * 59 | * #define LOG_LEVEL LOG_VERBOSE 60 | * #include "dxx_log.h" 61 | * 62 | * At component scope, define it in component makefile: 63 | * 64 | * CFLAGS += -D LOG_LEVEL=LOG_DEBUG 65 | * 66 | * 67 | */ 68 | 69 | /* clang-format off */ 70 | typedef enum _kendryte_log_level 71 | { 72 | LOG_NONE, /*!< No log output */ 73 | LOG_ERROR, /*!< Critical errors, software module can not recover on its own */ 74 | LOG_WARN, /*!< Error conditions from which recovery measures have been taken */ 75 | LOG_INFO, /*!< Information messages which describe normal flow of events */ 76 | LOG_DEBUG, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */ 77 | LOG_VERBOSE /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */ 78 | } kendryte_log_level_t ; 79 | /* clang-format on */ 80 | 81 | /* clang-format off */ 82 | #if CONFIG_LOG_COLORS 83 | #define LOG_COLOR_BLACK "30" 84 | #define LOG_COLOR_RED "31" 85 | #define LOG_COLOR_GREEN "32" 86 | #define LOG_COLOR_BROWN "33" 87 | #define LOG_COLOR_BLUE "34" 88 | #define LOG_COLOR_PURPLE "35" 89 | #define LOG_COLOR_CYAN "36" 90 | #define LOG_COLOR(COLOR) "\033[0;" COLOR "m" 91 | #define LOG_BOLD(COLOR) "\033[1;" COLOR "m" 92 | #define LOG_RESET_COLOR "\033[0m" 93 | #define LOG_COLOR_E LOG_COLOR(LOG_COLOR_RED) 94 | #define LOG_COLOR_W LOG_COLOR(LOG_COLOR_BROWN) 95 | #define LOG_COLOR_I LOG_COLOR(LOG_COLOR_GREEN) 96 | #define LOG_COLOR_D 97 | #define LOG_COLOR_V 98 | #else /* CONFIG_LOG_COLORS */ 99 | #define LOG_COLOR_E 100 | #define LOG_COLOR_W 101 | #define LOG_COLOR_I 102 | #define LOG_COLOR_D 103 | #define LOG_COLOR_V 104 | #define LOG_RESET_COLOR 105 | #endif /* CONFIG_LOG_COLORS */ 106 | /* clang-format on */ 107 | 108 | #define LOG_FORMAT(letter, format) LOG_COLOR_ ## letter #letter " (%lu) %s: " format LOG_RESET_COLOR "\n" 109 | 110 | #ifdef LOG_LEVEL 111 | #undef CONFIG_LOG_LEVEL 112 | #define CONFIG_LOG_LEVEL LOG_LEVEL 113 | #endif 114 | 115 | #ifdef LOG_KERNEL 116 | #define LOG_PRINTF printk 117 | #else 118 | #define LOG_PRINTF printf 119 | #endif 120 | 121 | #ifdef CONFIG_LOG_ENABLE 122 | #define LOGE(tag, format, ...) do {if (CONFIG_LOG_LEVEL >= LOG_ERROR) LOG_PRINTF(LOG_FORMAT(E, format), read_cycle(), tag, ##__VA_ARGS__); } while (0) 123 | #define LOGW(tag, format, ...) do {if (CONFIG_LOG_LEVEL >= LOG_WARN) LOG_PRINTF(LOG_FORMAT(W, format), read_cycle(), tag, ##__VA_ARGS__); } while (0) 124 | #define LOGI(tag, format, ...) do {if (CONFIG_LOG_LEVEL >= LOG_INFO) LOG_PRINTF(LOG_FORMAT(I, format), read_cycle(), tag, ##__VA_ARGS__); } while (0) 125 | #define LOGD(tag, format, ...) do {if (CONFIG_LOG_LEVEL >= LOG_DEBUG) LOG_PRINTF(LOG_FORMAT(D, format), read_cycle(), tag, ##__VA_ARGS__); } while (0) 126 | #define LOGV(tag, format, ...) do {if (CONFIG_LOG_LEVEL >= LOG_VERBOSE) LOG_PRINTF(LOG_FORMAT(V, format), read_cycle(), tag, ##__VA_ARGS__); } while (0) 127 | #else 128 | #define LOGE(tag, format, ...) 129 | #define LOGW(tag, format, ...) 130 | #define LOGI(tag, format, ...) 131 | #define LOGD(tag, format, ...) 132 | #define LOGV(tag, format, ...) 133 | #endif /* LOG_ENABLE */ 134 | 135 | #ifdef __cplusplus 136 | } 137 | #endif 138 | 139 | 140 | #endif /* _SYSLOG_H */ 141 | 142 | -------------------------------------------------------------------------------- /read_record.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | from PIL import Image 4 | import io 5 | 6 | class Record: 7 | def __init__(self, ann_filename): 8 | ann_filename = ann_filename.replace(".mjpeg", ".txt") 9 | ann_raw = open(ann_filename).read().split("T") 10 | self.ann = [] 11 | self.p = [] 12 | self.times = [] 13 | self.det_counts = [] 14 | self.dets = {} 15 | for i in range(len(ann_raw)-1): 16 | a = ann_raw[i].split() 17 | if a: 18 | time = int(a[0]) 19 | if len(a)>2: 20 | ann_tmp = np.array(a[2:], dtype=float).reshape(-1, 5) 21 | ann_tmp[..., 4] *= 100 22 | self.ann.append((time, int(a[1]), int(ann_raw[i+1].split()[1]), np.array(np.uint16(ann_tmp)))) 23 | else: 24 | self.ann.append((time, int(a[1]), int(ann_raw[i+1].split()[1]), [])) 25 | self.times.append(time) 26 | self.det_counts.append(self.ann[-1][-1][..., 4].sum() if len(self.ann[-1][-1]) else 0) 27 | self.frame_count = len(self.ann) 28 | self.vid_file = open(ann_filename[:-4]+".mjpeg", "rb") 29 | 30 | self.cur_pos = 0 31 | self.threshold = 0.8 32 | 33 | def get_frame(self, index=None): 34 | if index is None: 35 | # self.cur_pos += 1 36 | index = self.cur_pos 37 | time, p, pend, dets = self.ann[index] 38 | self.vid_file.seek(p) 39 | im_b = self.vid_file.read(pend-p+4) 40 | im = cv2.imdecode(np.asarray(bytearray(im_b), dtype="uint8"), cv2.IMREAD_COLOR) 41 | return im 42 | 43 | def get_dets(self, index=None): 44 | if index is None: 45 | index = self.cur_pos 46 | time, p, pend, dets = self.ann[index] 47 | if len(dets) == 0: 48 | return [] 49 | keep = dets[..., 4]>self.threshold * 100 50 | return dets[keep] 51 | 52 | def get_time(self, index=None): 53 | if index is None: 54 | index = self.cur_pos 55 | return self.times[index] 56 | 57 | def get_interval(self, index=None): 58 | if index is None: 59 | index = self.cur_pos 60 | return (self.times[index+1] - self.times[index]) if (0 < index + 1 < self.frame_count) else (1000 / self.get_mean_fps()) 61 | 62 | def get_mean_fps(self): 63 | return len(self.times) * 1000 / (self.times[-1] - self.times[0]) 64 | 65 | 66 | if __name__ == "__main__": 67 | rec = Record("25.txt") 68 | ann = rec.ann 69 | for i in range(rec.frame_count): 70 | im = rec.get_frame(i) 71 | dets = rec.get_dets(i) 72 | time = rec.get_time(i) 73 | 74 | cv2.putText(im, f"{(time//60000)%60}:{(time//1000)%60}", (20, 20), cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, 0.6, (0,255,0)) 75 | for det in dets: 76 | x_min, y_min, x_max, y_max = np.int32(det[:-1]) 77 | cv2.rectangle(im, (x_min, y_min), (x_max, y_max), (255, 0, 0)) 78 | cv2.imshow("im", im) 79 | cv2.waitKey(1) 80 | -------------------------------------------------------------------------------- /src/.gitignore: -------------------------------------------------------------------------------- 1 | .release/ -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/README.md: -------------------------------------------------------------------------------- 1 | # YOLO v3 test 2 | 3 | use modilenet yolov3 alpha=0.75 -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/bitstr.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/bitstr.c -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/bitstr.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/bitstr.h -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/bmp.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/bmp.c -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/bmp.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/bmp.h -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/board_config.h: -------------------------------------------------------------------------------- 1 | #ifndef _BOARD_CONFIG_ 2 | #define _BOARD_CONFIG_ 3 | 4 | #define OV5640 0 5 | #define OV2640 1 6 | 7 | #define BOARD_KD233 0 8 | #define BOARD_LICHEEDAN 1 9 | #define BOARD_K61 0 10 | 11 | #if (OV5640 && OV2640) || (!OV5640 && !OV2640) 12 | #error ov sensor only choose one 13 | #endif 14 | 15 | #if (BOARD_LICHEEDAN && BOARD_KD233) || (BOARD_LICHEEDAN && BOARD_K61) || (BOARD_K61 && BOARD_KD233) || (!BOARD_LICHEEDAN && !BOARD_KD233 && !BOARD_K61) 16 | #error board only choose one 17 | #endif 18 | 19 | #endif 20 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/color.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/color.c -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/color.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/color.h -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/dct.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/dct.c -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/dct.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/dct.h -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/diskio.c: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------*/ 2 | /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */ 3 | /*-----------------------------------------------------------------------*/ 4 | /* If a working storage control module is available, it should be */ 5 | /* attached to the FatFs via a glue function rather than modifying it. */ 6 | /* This is an example of glue functions to attach various exsisting */ 7 | /* storage control modules to the FatFs module with a defined API. */ 8 | /*-----------------------------------------------------------------------*/ 9 | 10 | #include "diskio.h" /* FatFs lower layer API */ 11 | #include "sdcard.h" 12 | 13 | /* Definitions of physical drive number for each drive */ 14 | #define M0 0 /* Example: Map MMC/SD card to physical drive 0 */ 15 | 16 | 17 | /*-----------------------------------------------------------------------*/ 18 | /* Get Drive Status */ 19 | /*-----------------------------------------------------------------------*/ 20 | 21 | DSTATUS disk_status(BYTE pdrv) 22 | { 23 | return 0; 24 | } 25 | 26 | 27 | 28 | /*-----------------------------------------------------------------------*/ 29 | /* Inidialize a Drive */ 30 | /*-----------------------------------------------------------------------*/ 31 | 32 | DSTATUS disk_initialize(BYTE pdrv) 33 | { 34 | if (sd_init() == 0) 35 | return 0; 36 | return STA_NOINIT; 37 | } 38 | 39 | 40 | 41 | /*-----------------------------------------------------------------------*/ 42 | /* Read Sector(s) */ 43 | /*-----------------------------------------------------------------------*/ 44 | 45 | DRESULT disk_read(BYTE pdrv, BYTE *buff, DWORD sector, UINT count) 46 | { 47 | if (sd_read_sector_dma(buff, sector, count) == 0) 48 | return RES_OK; 49 | return RES_ERROR; 50 | } 51 | 52 | 53 | 54 | /*-----------------------------------------------------------------------*/ 55 | /* Write Sector(s) */ 56 | /*-----------------------------------------------------------------------*/ 57 | 58 | DRESULT disk_write(BYTE pdrv, const BYTE *buff, DWORD sector, UINT count) 59 | { 60 | if (sd_write_sector_dma((BYTE *)buff, sector, count) == 0) 61 | return RES_OK; 62 | return RES_ERROR; 63 | } 64 | 65 | 66 | 67 | /*-----------------------------------------------------------------------*/ 68 | /* Miscellaneous Functions */ 69 | /*-----------------------------------------------------------------------*/ 70 | 71 | DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) 72 | { 73 | DRESULT res = RES_ERROR; 74 | 75 | switch (cmd) { 76 | /* Make sure that no pending write process */ 77 | case CTRL_SYNC: 78 | res = RES_OK; 79 | break; 80 | /* Get number of sectors on the disk (DWORD) */ 81 | case GET_SECTOR_COUNT: 82 | *(DWORD *)buff = (cardinfo.SD_csd.DeviceSize + 1) << 10; 83 | res = RES_OK; 84 | break; 85 | /* Get R/W sector size (WORD) */ 86 | case GET_SECTOR_SIZE: 87 | *(WORD *)buff = cardinfo.CardBlockSize; 88 | res = RES_OK; 89 | break; 90 | /* Get erase block size in unit of sector (DWORD) */ 91 | case GET_BLOCK_SIZE: 92 | *(DWORD *)buff = cardinfo.CardBlockSize; 93 | res = RES_OK; 94 | break; 95 | default: 96 | res = RES_PARERR; 97 | } 98 | return res; 99 | } 100 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/diskio.h: -------------------------------------------------------------------------------- 1 | /*-----------------------------------------------------------------------/ 2 | / Low level disk interface modlue include file (C)ChaN, 2014 / 3 | /-----------------------------------------------------------------------*/ 4 | 5 | #ifndef _DISKIO_DEFINED 6 | #define _DISKIO_DEFINED 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | #include "integer.h" 13 | 14 | 15 | /* Status of Disk Functions */ 16 | typedef BYTE DSTATUS; 17 | 18 | /* Results of Disk Functions */ 19 | typedef enum { 20 | RES_OK = 0, /* 0: Successful */ 21 | RES_ERROR, /* 1: R/W Error */ 22 | RES_WRPRT, /* 2: Write Protected */ 23 | RES_NOTRDY, /* 3: Not Ready */ 24 | RES_PARERR /* 4: Invalid Parameter */ 25 | } DRESULT; 26 | 27 | 28 | /*---------------------------------------*/ 29 | /* Prototypes for disk control functions */ 30 | 31 | 32 | DSTATUS disk_initialize (BYTE pdrv); 33 | DSTATUS disk_status (BYTE pdrv); 34 | DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count); 35 | DRESULT disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count); 36 | DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff); 37 | 38 | 39 | /* Disk Status Bits (DSTATUS) */ 40 | 41 | #define STA_NOINIT 0x01 /* Drive not initialized */ 42 | #define STA_NODISK 0x02 /* No medium in the drive */ 43 | #define STA_PROTECT 0x04 /* Write protected */ 44 | 45 | 46 | /* Command code for disk_ioctrl fucntion */ 47 | 48 | /* Generic command (Used by FatFs) */ 49 | #define CTRL_SYNC 0 /* Complete pending write process (needed at _FS_READONLY == 0) */ 50 | #define GET_SECTOR_COUNT 1 /* Get media size (needed at _USE_MKFS == 1) */ 51 | #define GET_SECTOR_SIZE 2 /* Get sector size (needed at _MAX_SS != _MIN_SS) */ 52 | #define GET_BLOCK_SIZE 3 /* Get erase block size (needed at _USE_MKFS == 1) */ 53 | #define CTRL_TRIM 4 /* Inform device that the data on the block of sectors is no longer used (needed at _USE_TRIM == 1) */ 54 | 55 | /* Generic command (Not used by FatFs) */ 56 | #define CTRL_POWER 5 /* Get/Set power status */ 57 | #define CTRL_LOCK 6 /* Lock/Unlock media removal */ 58 | #define CTRL_EJECT 7 /* Eject media */ 59 | #define CTRL_FORMAT 8 /* Create physical format on the media */ 60 | 61 | /* MMC/SDC specific ioctl command */ 62 | #define MMC_GET_TYPE 10 /* Get card type */ 63 | #define MMC_GET_CSD 11 /* Get CSD */ 64 | #define MMC_GET_CID 12 /* Get CID */ 65 | #define MMC_GET_OCR 13 /* Get OCR */ 66 | #define MMC_GET_SDSTAT 14 /* Get SD status */ 67 | #define ISDIO_READ 55 /* Read data form SD iSDIO register */ 68 | #define ISDIO_WRITE 56 /* Write data to SD iSDIO register */ 69 | #define ISDIO_MRITE 57 /* Masked write data to SD iSDIO register */ 70 | 71 | /* ATA/CF specific ioctl command */ 72 | #define ATA_GET_REV 20 /* Get F/W revision */ 73 | #define ATA_GET_MODEL 21 /* Get model name */ 74 | #define ATA_GET_SN 22 /* Get serial number */ 75 | 76 | #ifdef __cplusplus 77 | } 78 | #endif 79 | 80 | #endif 81 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/huffman.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/huffman.c -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/huffman.h: -------------------------------------------------------------------------------- 1 | #ifndef __FFJPEG_HUFFMAN_H__ 2 | #define __FFJPEG_HUFFMAN_H__ 3 | 4 | /* 包含头文件 */ 5 | #include "stdefine.h" 6 | 7 | #ifdef __cplusplus 8 | extern "C" { 9 | #endif 10 | 11 | /* 常量定义 */ 12 | #define MAX_HUFFMAN_CODE_LEN 16 13 | 14 | extern const BYTE STD_HUFTAB_LUMIN_AC[]; 15 | extern const BYTE STD_HUFTAB_LUMIN_DC[]; 16 | extern const BYTE STD_HUFTAB_CHROM_AC[]; 17 | extern const BYTE STD_HUFTAB_CHROM_DC[]; 18 | 19 | /* 类型定义 */ 20 | /* 编码表项类型定义 */ 21 | typedef struct 22 | { 23 | int symbol; /* 符号 */ 24 | int freq; /* 频率 */ 25 | int group; /* 分组 */ 26 | int depth; /* 码长 */ 27 | int code; /* 码字 */ 28 | } HUFCODEITEM; 29 | 30 | /* 编码器类型定义 */ 31 | typedef struct 32 | { 33 | BYTE huftab[MAX_HUFFMAN_CODE_LEN + 256]; /* 哈夫曼表 */ 34 | int first [MAX_HUFFMAN_CODE_LEN]; /* first */ 35 | int index [MAX_HUFFMAN_CODE_LEN]; /* index */ 36 | HUFCODEITEM codelist[256];/* 编码表 */ 37 | void *input; /* input bit stream */ 38 | void *output; /* output bit stream */ 39 | } HUFCODEC; 40 | 41 | 42 | /* 函数声明 */ 43 | /* 标准的符号频率统计器 */ 44 | void huffman_stat_freq(HUFCODEITEM codelist[256], void *stream); 45 | 46 | 47 | /* 编码 */ 48 | /* 49 | 编码方法: 50 | 1. 初始化 HUFCODEC 中的 freqlist 成员,可以采用 51 | huffman_stat_freq 进行频率统计,也可以自己编 52 | 码实现频率统计 53 | 54 | 2. 调用 huffman_encode_init 函数将会生成 huftab, 55 | codelist 分别对应了哈夫曼表 (JPEG format) 和 56 | 编码表 57 | 58 | 3. 指定 input 和 output 分别代表编码过程中的输入 59 | 数据流和输出数据流 60 | 61 | 4. 调用 huffman_encode_run 可以完成自动的编码操 62 | 作编码的结束条件是 input 或 output 到达了尾部 63 | 64 | 5. 调用 huffman_encode_done 结束哈夫曼编码 65 | 66 | 6. 也可以通过 huffman_encode_step 进行单步的哈夫 67 | 曼编码操作 68 | */ 69 | // flag == 0, init from code freq list 70 | // flag == 1, init from huffman table 71 | void huffman_encode_init(HUFCODEC *phc, int flag); 72 | void huffman_encode_done(HUFCODEC *phc); 73 | BOOL huffman_encode_run (HUFCODEC *phc); 74 | 75 | /* 一次只编码一个符号 */ 76 | BOOL huffman_encode_step(HUFCODEC *phc, int symbol); 77 | 78 | 79 | /* 解码 */ 80 | /* 81 | 解码方法: 82 | 1. 初始化 HUFCODEC 中的 huftab 成员,huftab 83 | 是 JPEG format 的范式哈夫曼表 84 | 85 | 2. 调用 huffman_decode_init 将会根据 huftab 86 | 生成 first 和 index 两个表 87 | 88 | 3. 指定 input 和 output 分别代表解码过程中的输 89 | 入数据流和输出数据流 90 | 91 | 4. 调用 huffman_decode_run 可以完成自动的解码 92 | 操作解码的结束条件是 input 或 output 到达了 93 | 尾部 94 | 95 | 5. 调用 huffman_decode_done 结束哈夫曼解码 96 | 97 | 6. 也可以通过 huffman_decode_step 进行单步 98 | 的哈夫曼解码操作,该函数每次调用只会从数 99 | 据流中解码出一个符号 100 | */ 101 | void huffman_decode_init(HUFCODEC *phc); 102 | void huffman_decode_done(HUFCODEC *phc); 103 | BOOL huffman_decode_run (HUFCODEC *phc); 104 | 105 | /* 一次只解码一个符号 */ 106 | int huffman_decode_step(HUFCODEC *phc); 107 | 108 | #ifdef __cplusplus 109 | } 110 | #endif 111 | 112 | #endif 113 | 114 | 115 | 116 | 117 | 118 | 119 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/image_process.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | 16 | #include 17 | #include "image_process.h" 18 | 19 | int min(int a, int b){return aaddr = calloc(image->width * image->height * image->pixel, 1); 24 | if (image->addr == NULL) 25 | return -1; 26 | return 0; 27 | } 28 | 29 | void image_deinit(image_t *image) 30 | { 31 | free(image->addr); 32 | } 33 | 34 | int image_crop(image_t *src, image_t *dst, int x, int y, int w, int h, int stride){ 35 | int wh_dst = dst->width * dst->height; 36 | int wh_src = src->width * src->height; 37 | int channel = min(src->pixel, dst->pixel); 38 | if(channel==2){ 39 | for(int j=y, dst_j=0; dst_jaddr)[dst_j * dst->width + dst_i] = ((uint16_t*)src->addr)[j * src->width + i]; 42 | return 0; 43 | } 44 | for(int c=0; c < channel; c++) 45 | for(int j=y, dst_j=0; dst_jaddr[c * wh_dst + dst_j * dst->width + dst_i] = src->addr[c * wh_src + j * src->width + i]; 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/image_process.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _IMAGE_PROCESS_H 16 | #define _IMAGE_PROCESS_H 17 | 18 | #include 19 | 20 | typedef struct 21 | { 22 | uint8_t *addr; 23 | uint16_t width; 24 | uint16_t height; 25 | uint16_t pixel; 26 | uint16_t format; 27 | } image_t; 28 | 29 | 30 | int image_init(image_t *image); 31 | void image_deinit(image_t *image); 32 | 33 | int image_crop(image_t *src, image_t *dst, int x, int y, int w, int h, int stride); 34 | 35 | 36 | #endif /* _IMAGE_PROCESS_H */ 37 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/integer.h: -------------------------------------------------------------------------------- 1 | /*-------------------------------------------*/ 2 | /* Integer type definitions for FatFs module */ 3 | /*-------------------------------------------*/ 4 | 5 | #ifndef _FF_INTEGER 6 | #define _FF_INTEGER 7 | 8 | #ifdef _WIN32 /* FatFs development platform */ 9 | 10 | #include 11 | #include 12 | typedef unsigned __int64 QWORD; 13 | 14 | 15 | #else /* Embedded platform */ 16 | #include 17 | 18 | /* These types MUST be 16-bit or 32-bit */ 19 | typedef int INT; 20 | typedef unsigned int UINT; 21 | 22 | /* This type MUST be 8-bit */ 23 | typedef unsigned char BYTE; 24 | 25 | /* These types MUST be 16-bit */ 26 | typedef short SHORT; 27 | typedef unsigned short WORD; 28 | typedef unsigned short WCHAR; 29 | 30 | /* These types MUST be 32-bit */ 31 | typedef long LONG; 32 | typedef uint32_t DWORD; 33 | 34 | /* This type MUST be 64-bit (Remove this for C89 compatibility) */ 35 | typedef uint64_t QWORD; 36 | 37 | #endif 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/jfif.h: -------------------------------------------------------------------------------- 1 | #ifndef __FFJPEG_JFIF_H__ 2 | #define __FFJPEG_JFIF_H__ 3 | 4 | // ����ͷ�ļ� 5 | #include "bmp.h" 6 | #include "ff.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | /* �������� */ 13 | void* jfif_load(char *file); 14 | int jfif_save(void *ctxt, char *file); 15 | int jfif_write(void *ctxt, FIL* fp); 16 | void jfif_free(void *ctxt); 17 | 18 | int jfif_decode(void *ctxt, BMP *pb); 19 | void* jfif_encode(BMP *pb); 20 | 21 | #ifdef __cplusplus 22 | } 23 | #endif 24 | 25 | #endif 26 | 27 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/kfpkg/dog.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/kfpkg/dog.jpg -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/kfpkg/flash-list.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.1.0", 3 | "files": [ 4 | { 5 | "address": 0, 6 | "bin": "yolo3_frame_test_public_maixpy.bin", 7 | "sha256Prefix": true 8 | }, 9 | { 10 | "address": 0x00A00000, 11 | "bin": "yolo.kmodel", 12 | "sha256Prefix": false 13 | } 14 | ] 15 | } -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/kfpkg/kpu_yolov3.kfpkg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/kfpkg/kpu_yolov3.kfpkg -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/kfpkg/mkaiimg.py: -------------------------------------------------------------------------------- 1 | from skimage.io import imread, imsave 2 | from skimage.transform import resize 3 | import numpy as np 4 | import sys 5 | 6 | if __name__ == '__main__': 7 | img = imread(sys.argv[1]) 8 | print(sys.argv[1]) 9 | img = resize(img, (224, 320), preserve_range=True).astype('uint8') 10 | imsave('test.jpg', img) 11 | img = np.transpose(img, [2, 0, 1]) 12 | with open('aiimg.h', 'w') as f: 13 | f.write('#ifndef _AIIMG_H_\n#define _AIIMG_H_\n#include \nconst unsigned char ai_image[] __attribute__((aligned(128))) = {') 14 | f.write(', '.join([str(i) for i in img.flatten()])) 15 | f.write('};\n#endif') 16 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/lcd.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include 16 | #include 17 | #include "lcd.h" 18 | #include "nt35310.h" 19 | #include "font.h" 20 | #include "board_config.h" 21 | 22 | static lcd_ctl_t lcd_ctl; 23 | 24 | void lcd_polling_enable(void) 25 | { 26 | lcd_ctl.mode = 0; 27 | } 28 | 29 | void lcd_interrupt_enable(void) 30 | { 31 | lcd_ctl.mode = 1; 32 | } 33 | 34 | void lcd_init(void) 35 | { 36 | uint8_t data = 0; 37 | 38 | tft_hard_init(); 39 | /*soft reset*/ 40 | tft_write_command(SOFTWARE_RESET); 41 | usleep(100000); 42 | /*exit sleep*/ 43 | tft_write_command(SLEEP_OFF); 44 | usleep(100000); 45 | /*pixel format*/ 46 | tft_write_command(PIXEL_FORMAT_SET); 47 | data = 0x55; 48 | tft_write_byte(&data, 1); 49 | lcd_set_direction(DIR_XY_LRUD); 50 | 51 | /*display on*/ 52 | tft_write_command(DISPALY_ON); 53 | lcd_polling_enable(); 54 | } 55 | 56 | void lcd_set_direction(lcd_dir_t dir) 57 | { 58 | #if BOARD_LICHEEDAN 59 | #else 60 | dir |= 0x08; 61 | #endif 62 | lcd_ctl.dir = dir; 63 | if (dir & DIR_XY_MASK) 64 | { 65 | lcd_ctl.width = LCD_Y_MAX - 1; 66 | lcd_ctl.height = LCD_X_MAX - 1; 67 | } 68 | else 69 | { 70 | lcd_ctl.width = LCD_X_MAX - 1; 71 | lcd_ctl.height = LCD_Y_MAX - 1; 72 | } 73 | 74 | tft_write_command(MEMORY_ACCESS_CTL); 75 | tft_write_byte((uint8_t *)&dir, 1); 76 | } 77 | 78 | void lcd_set_area(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) 79 | { 80 | uint8_t data[4] = {0}; 81 | 82 | data[0] = (uint8_t)(x1 >> 8); 83 | data[1] = (uint8_t)(x1); 84 | data[2] = (uint8_t)(x2 >> 8); 85 | data[3] = (uint8_t)(x2); 86 | tft_write_command(HORIZONTAL_ADDRESS_SET); 87 | tft_write_byte(data, 4); 88 | 89 | data[0] = (uint8_t)(y1 >> 8); 90 | data[1] = (uint8_t)(y1); 91 | data[2] = (uint8_t)(y2 >> 8); 92 | data[3] = (uint8_t)(y2); 93 | tft_write_command(VERTICAL_ADDRESS_SET); 94 | tft_write_byte(data, 4); 95 | 96 | tft_write_command(MEMORY_WRITE); 97 | } 98 | 99 | void lcd_draw_point(uint16_t x, uint16_t y, uint16_t color) 100 | { 101 | lcd_set_area(x, y, x, y); 102 | tft_write_half(&color, 1); 103 | } 104 | 105 | void lcd_draw_char(uint16_t x, uint16_t y, char c, uint16_t color) 106 | { 107 | uint8_t i = 0; 108 | uint8_t j = 0; 109 | uint8_t data = 0; 110 | 111 | for (i = 0; i < 16; i++) 112 | { 113 | data = ascii0816[c * 16 + i]; 114 | for (j = 0; j < 8; j++) 115 | { 116 | if (data & 0x80) 117 | lcd_draw_point(x + j, y, color); 118 | data <<= 1; 119 | } 120 | y++; 121 | } 122 | } 123 | 124 | void lcd_draw_string(uint16_t x, uint16_t y, char *str, uint16_t color) 125 | { 126 | while (*str) 127 | { 128 | lcd_draw_char(x, y, *str, color); 129 | str++; 130 | x += 8; 131 | } 132 | } 133 | 134 | void lcd_ram_draw_string(char *str, uint32_t *ptr, uint16_t font_color, uint16_t bg_color) 135 | { 136 | uint8_t i = 0; 137 | uint8_t j = 0; 138 | uint8_t data = 0; 139 | uint8_t *pdata = NULL; 140 | uint16_t width = 0; 141 | uint32_t *pixel = NULL; 142 | 143 | width = 4 * strlen(str); 144 | while (*str) 145 | { 146 | pdata = (uint8_t *)&ascii0816[(*str) * 16]; 147 | for (i = 0; i < 16; i++) 148 | { 149 | data = *pdata++; 150 | pixel = ptr + i * width; 151 | for (j = 0; j < 4; j++) 152 | { 153 | switch (data >> 6) 154 | { 155 | case 0: 156 | *pixel = ((uint32_t)bg_color << 16) | bg_color; 157 | break; 158 | case 1: 159 | *pixel = ((uint32_t)bg_color << 16) | font_color; 160 | break; 161 | case 2: 162 | *pixel = ((uint32_t)font_color << 16) | bg_color; 163 | break; 164 | case 3: 165 | *pixel = ((uint32_t)font_color << 16) | font_color; 166 | break; 167 | default: 168 | *pixel = 0; 169 | break; 170 | } 171 | data <<= 2; 172 | pixel++; 173 | } 174 | } 175 | str++; 176 | ptr += 4; 177 | } 178 | } 179 | 180 | void lcd_clear(uint16_t color) 181 | { 182 | uint32_t data = ((uint32_t)color << 16) | (uint32_t)color; 183 | 184 | lcd_set_area(0, 0, lcd_ctl.width, lcd_ctl.height); 185 | tft_fill_data(&data, LCD_X_MAX * LCD_Y_MAX / 2); 186 | } 187 | 188 | void lcd_clear_area(uint16_t color, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) 189 | { 190 | uint32_t data = ((uint32_t)color << 16) | (uint32_t)color; 191 | 192 | lcd_set_area(x1, y1, x2, y2); 193 | tft_fill_data(&data, (x2-x1)*(y2-y1)); 194 | } 195 | 196 | void lcd_draw_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t width, uint16_t color) 197 | { 198 | uint32_t data_buf[640] = {0}; 199 | uint32_t *p = data_buf; 200 | uint32_t data = color; 201 | uint32_t index = 0; 202 | 203 | data = (data << 16) | data; 204 | for (index = 0; index < 160 * width; index++) 205 | *p++ = data; 206 | 207 | lcd_set_area(x1, y1, x2, y1 + width - 1); 208 | tft_write_word(data_buf, ((x2 - x1 + 1) * width + 1) / 2, 0); 209 | lcd_set_area(x1, y2 - width + 1, x2, y2); 210 | tft_write_word(data_buf, ((x2 - x1 + 1) * width + 1) / 2, 0); 211 | lcd_set_area(x1, y1, x1 + width - 1, y2); 212 | tft_write_word(data_buf, ((y2 - y1 + 1) * width + 1) / 2, 0); 213 | lcd_set_area(x2 - width + 1, y1, x2, y2); 214 | tft_write_word(data_buf, ((y2 - y1 + 1) * width + 1) / 2, 0); 215 | } 216 | 217 | void lcd_draw_picture(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint32_t *ptr) 218 | { 219 | lcd_set_area(x1, y1, x1 + width - 1, y1 + height - 1); 220 | tft_write_word(ptr, width * height / 2, lcd_ctl.mode ? 2 : 0); 221 | } 222 | 223 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/lcd.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _LCD_H_ 16 | #define _LCD_H_ 17 | 18 | #include 19 | 20 | /* clang-format off */ 21 | #define LCD_X_MAX (240) 22 | #define LCD_Y_MAX (320) 23 | 24 | #define BLACK 0x0000 25 | #define NAVY 0x000F 26 | #define DARKGREEN 0x03E0 27 | #define DARKCYAN 0x03EF 28 | #define MAROON 0x7800 29 | #define PURPLE 0x780F 30 | #define OLIVE 0x7BE0 31 | #define LIGHTGREY 0xC618 32 | #define DARKGREY 0x7BEF 33 | #define BLUE 0x001F 34 | #define GREEN 0x07E0 35 | #define CYAN 0x07FF 36 | #define RED 0xF800 37 | #define MAGENTA 0xF81F 38 | #define YELLOW 0xFFE0 39 | #define WHITE 0xFFFF 40 | #define ORANGE 0xFD20 41 | #define GREENYELLOW 0xAFE5 42 | #define PINK 0xF81F 43 | #define USER_COLOR 0xAA55 44 | /* clang-format on */ 45 | 46 | typedef enum _lcd_dir 47 | { 48 | DIR_XY_RLUD = 0x00, 49 | DIR_YX_RLUD = 0x20, 50 | DIR_XY_LRUD = 0x40, 51 | DIR_YX_LRUD = 0x60, 52 | DIR_XY_RLDU = 0x80, 53 | DIR_YX_RLDU = 0xA0, 54 | DIR_XY_LRDU = 0xC0, 55 | DIR_YX_LRDU = 0xE0, 56 | DIR_XY_MASK = 0x20, 57 | DIR_MASK = 0xE0, 58 | } lcd_dir_t; 59 | 60 | typedef struct _lcd_ctl 61 | { 62 | uint8_t mode; 63 | uint8_t dir; 64 | uint16_t width; 65 | uint16_t height; 66 | } lcd_ctl_t; 67 | 68 | void lcd_polling_enable(void); 69 | void lcd_interrupt_enable(void); 70 | void lcd_init(void); 71 | void lcd_clear(uint16_t color); 72 | void lcd_set_direction(lcd_dir_t dir); 73 | void lcd_set_area(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); 74 | void lcd_clear_area(uint16_t color, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2); 75 | void lcd_draw_point(uint16_t x, uint16_t y, uint16_t color); 76 | void lcd_draw_string(uint16_t x, uint16_t y, char *str, uint16_t color); 77 | void lcd_draw_picture(uint16_t x1, uint16_t y1, uint16_t width, uint16_t height, uint32_t *ptr); 78 | void lcd_draw_rectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t width, uint16_t color); 79 | void lcd_ram_draw_string(char *str, uint32_t *ptr, uint16_t font_color, uint16_t bg_color); 80 | 81 | #endif 82 | 83 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/nt35310.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include "nt35310.h" 16 | #include "gpiohs.h" 17 | #include "spi.h" 18 | #include "unistd.h" 19 | #include "board_config.h" 20 | 21 | static void init_dcx(void) 22 | { 23 | gpiohs_set_drive_mode(DCX_GPIONUM, GPIO_DM_OUTPUT); 24 | gpiohs_set_pin(DCX_GPIONUM, GPIO_PV_HIGH); 25 | } 26 | 27 | static void set_dcx_control(void) 28 | { 29 | gpiohs_set_pin(DCX_GPIONUM, GPIO_PV_LOW); 30 | } 31 | 32 | static void set_dcx_data(void) 33 | { 34 | gpiohs_set_pin(DCX_GPIONUM, GPIO_PV_HIGH); 35 | } 36 | 37 | #if BOARD_LICHEEDAN 38 | static void init_rst(void) 39 | { 40 | gpiohs_set_drive_mode(RST_GPIONUM, GPIO_DM_OUTPUT); 41 | gpiohs_set_pin(RST_GPIONUM, GPIO_PV_LOW); 42 | usleep(100000); 43 | gpiohs_set_pin(RST_GPIONUM, GPIO_PV_HIGH); 44 | usleep(100000); 45 | } 46 | #endif 47 | 48 | void tft_hard_init(void) 49 | { 50 | init_dcx(); 51 | spi_init(SPI_CHANNEL, SPI_WORK_MODE_0, SPI_FF_OCTAL, 8, 0); 52 | #if BOARD_LICHEEDAN 53 | init_rst(); 54 | spi_set_clk_rate(SPI_CHANNEL, 20000000); 55 | #else 56 | spi_set_clk_rate(SPI_CHANNEL, 25000000); 57 | #endif 58 | } 59 | 60 | void tft_write_command(uint8_t cmd) 61 | { 62 | set_dcx_control(); 63 | spi_init(SPI_CHANNEL, SPI_WORK_MODE_0, SPI_FF_OCTAL, 8, 0); 64 | spi_init_non_standard(SPI_CHANNEL, 8/*instrction length*/, 0/*address length*/, 0/*wait cycles*/, 65 | SPI_AITM_AS_FRAME_FORMAT/*spi address trans mode*/); 66 | spi_send_data_normal_dma(DMAC_CHANNEL0, SPI_CHANNEL, SPI_SLAVE_SELECT, (uint8_t *)(&cmd), 1,SPI_TRANS_CHAR); 67 | } 68 | 69 | void tft_write_byte(uint8_t *data_buf, uint32_t length) 70 | { 71 | set_dcx_data(); 72 | spi_init(SPI_CHANNEL, SPI_WORK_MODE_0, SPI_FF_OCTAL, 8, 0); 73 | spi_init_non_standard(SPI_CHANNEL, 8/*instrction length*/, 0/*address length*/, 0/*wait cycles*/, 74 | SPI_AITM_AS_FRAME_FORMAT/*spi address trans mode*/); 75 | spi_send_data_normal_dma(DMAC_CHANNEL0, SPI_CHANNEL, SPI_SLAVE_SELECT, data_buf, length, SPI_TRANS_CHAR); 76 | } 77 | 78 | void tft_write_half(uint16_t *data_buf, uint32_t length) 79 | { 80 | set_dcx_data(); 81 | spi_init(SPI_CHANNEL, SPI_WORK_MODE_0, SPI_FF_OCTAL, 16, 0); 82 | spi_init_non_standard(SPI_CHANNEL, 16/*instrction length*/, 0/*address length*/, 0/*wait cycles*/, 83 | SPI_AITM_AS_FRAME_FORMAT/*spi address trans mode*/); 84 | spi_send_data_normal_dma(DMAC_CHANNEL0, SPI_CHANNEL, SPI_SLAVE_SELECT,data_buf, length, SPI_TRANS_SHORT); 85 | } 86 | 87 | void tft_write_word(uint32_t *data_buf, uint32_t length, uint32_t flag) 88 | { 89 | set_dcx_data(); 90 | spi_init(SPI_CHANNEL, SPI_WORK_MODE_0, SPI_FF_OCTAL, 32, 0); 91 | 92 | spi_init_non_standard(SPI_CHANNEL, 0/*instrction length*/, 32/*address length*/, 0/*wait cycles*/, 93 | SPI_AITM_AS_FRAME_FORMAT/*spi address trans mode*/); 94 | spi_send_data_normal_dma(DMAC_CHANNEL0, SPI_CHANNEL, SPI_SLAVE_SELECT,data_buf, length, SPI_TRANS_INT); 95 | } 96 | 97 | void tft_fill_data(uint32_t *data_buf, uint32_t length) 98 | { 99 | set_dcx_data(); 100 | spi_init(SPI_CHANNEL, SPI_WORK_MODE_0, SPI_FF_OCTAL, 32, 0); 101 | spi_init_non_standard(SPI_CHANNEL, 0/*instrction length*/, 32/*address length*/, 0/*wait cycles*/, 102 | SPI_AITM_AS_FRAME_FORMAT/*spi address trans mode*/); 103 | spi_fill_data_dma(DMAC_CHANNEL0, SPI_CHANNEL, SPI_SLAVE_SELECT,data_buf, length); 104 | } 105 | 106 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/nt35310.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _NT35310_H_ 16 | #define _NT35310_H_ 17 | 18 | #include 19 | 20 | /* clang-format off */ 21 | #define NO_OPERATION 0x00 22 | #define SOFTWARE_RESET 0x01 23 | #define READ_ID 0x04 24 | #define READ_STATUS 0x09 25 | #define READ_POWER_MODE 0x0A 26 | #define READ_MADCTL 0x0B 27 | #define READ_PIXEL_FORMAT 0x0C 28 | #define READ_IMAGE_FORMAT 0x0D 29 | #define READ_SIGNAL_MODE 0x0E 30 | #define READ_SELT_DIAG_RESULT 0x0F 31 | #define SLEEP_ON 0x10 32 | #define SLEEP_OFF 0x11 33 | #define PARTIAL_DISPALY_ON 0x12 34 | #define NORMAL_DISPALY_ON 0x13 35 | #define INVERSION_DISPALY_OFF 0x20 36 | #define INVERSION_DISPALY_ON 0x21 37 | #define GAMMA_SET 0x26 38 | #define DISPALY_OFF 0x28 39 | #define DISPALY_ON 0x29 40 | #define HORIZONTAL_ADDRESS_SET 0x2A 41 | #define VERTICAL_ADDRESS_SET 0x2B 42 | #define MEMORY_WRITE 0x2C 43 | #define COLOR_SET 0x2D 44 | #define MEMORY_READ 0x2E 45 | #define PARTIAL_AREA 0x30 46 | #define VERTICAL_SCROL_DEFINE 0x33 47 | #define TEAR_EFFECT_LINE_OFF 0x34 48 | #define TEAR_EFFECT_LINE_ON 0x35 49 | #define MEMORY_ACCESS_CTL 0x36 50 | #define VERTICAL_SCROL_S_ADD 0x37 51 | #define IDLE_MODE_OFF 0x38 52 | #define IDLE_MODE_ON 0x39 53 | #define PIXEL_FORMAT_SET 0x3A 54 | #define WRITE_MEMORY_CONTINUE 0x3C 55 | #define READ_MEMORY_CONTINUE 0x3E 56 | #define SET_TEAR_SCANLINE 0x44 57 | #define GET_SCANLINE 0x45 58 | #define WRITE_BRIGHTNESS 0x51 59 | #define READ_BRIGHTNESS 0x52 60 | #define WRITE_CTRL_DISPALY 0x53 61 | #define READ_CTRL_DISPALY 0x54 62 | #define WRITE_BRIGHTNESS_CTL 0x55 63 | #define READ_BRIGHTNESS_CTL 0x56 64 | #define WRITE_MIN_BRIGHTNESS 0x5E 65 | #define READ_MIN_BRIGHTNESS 0x5F 66 | #define READ_ID1 0xDA 67 | #define READ_ID2 0xDB 68 | #define READ_ID3 0xDC 69 | #define RGB_IF_SIGNAL_CTL 0xB0 70 | #define NORMAL_FRAME_CTL 0xB1 71 | #define IDLE_FRAME_CTL 0xB2 72 | #define PARTIAL_FRAME_CTL 0xB3 73 | #define INVERSION_CTL 0xB4 74 | #define BLANK_PORCH_CTL 0xB5 75 | #define DISPALY_FUNCTION_CTL 0xB6 76 | #define ENTRY_MODE_SET 0xB7 77 | #define BACKLIGHT_CTL1 0xB8 78 | #define BACKLIGHT_CTL2 0xB9 79 | #define BACKLIGHT_CTL3 0xBA 80 | #define BACKLIGHT_CTL4 0xBB 81 | #define BACKLIGHT_CTL5 0xBC 82 | #define BACKLIGHT_CTL7 0xBE 83 | #define BACKLIGHT_CTL8 0xBF 84 | #define POWER_CTL1 0xC0 85 | #define POWER_CTL2 0xC1 86 | #define VCOM_CTL1 0xC5 87 | #define VCOM_CTL2 0xC7 88 | #define NV_MEMORY_WRITE 0xD0 89 | #define NV_MEMORY_PROTECT_KEY 0xD1 90 | #define NV_MEMORY_STATUS_READ 0xD2 91 | #define READ_ID4 0xD3 92 | #define POSITIVE_GAMMA_CORRECT 0xE0 93 | #define NEGATIVE_GAMMA_CORRECT 0xE1 94 | #define DIGITAL_GAMMA_CTL1 0xE2 95 | #define DIGITAL_GAMMA_CTL2 0xE3 96 | #define INTERFACE_CTL 0xF6 97 | 98 | #define DCX_GPIONUM (2) 99 | #define RST_GPIONUM (3) 100 | 101 | #define SPI_CHANNEL 0 102 | #define SPI_SLAVE_SELECT 3 103 | /* clang-format on */ 104 | 105 | void tft_hard_init(void); 106 | void tft_write_command(uint8_t cmd); 107 | void tft_write_byte(uint8_t *data_buf, uint32_t length); 108 | void tft_write_half(uint16_t *data_buf, uint32_t length); 109 | void tft_write_word(uint32_t *data_buf, uint32_t length, uint32_t flag); 110 | void tft_fill_data(uint32_t *data_buf, uint32_t length); 111 | 112 | #endif 113 | 114 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/ov2640.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include 16 | #include "ov2640.h" 17 | #include "dvp.h" 18 | #include "plic.h" 19 | #include "board_config.h" 20 | 21 | const uint8_t ov2640_config[][2]= 22 | { 23 | {0xff, 0x01}, 24 | {0x12, 0x80}, 25 | {0xff, 0x00}, 26 | {0x2c, 0xff}, 27 | {0x2e, 0xdf}, 28 | {0xff, 0x01}, 29 | {0x3c, 0x32}, 30 | {0x11, 0x00}, 31 | {0x09, 0x02}, 32 | #if BOARD_LICHEEDAN 33 | {0x04, 0x08}, 34 | #else 35 | {0x04, 0x58}, 36 | #endif 37 | {0x13, 0xe5}, 38 | {0x14, 0x48}, 39 | {0x2c, 0x0c}, 40 | {0x33, 0x78}, 41 | {0x3a, 0x33}, 42 | {0x3b, 0xfb}, 43 | {0x3e, 0x00}, 44 | {0x43, 0x11}, 45 | {0x16, 0x10}, 46 | {0x39, 0x92}, 47 | {0x35, 0xda}, 48 | {0x22, 0x1a}, 49 | {0x37, 0xc3}, 50 | {0x23, 0x00}, 51 | {0x34, 0xc0}, 52 | {0x36, 0x1a}, 53 | {0x06, 0x88}, 54 | {0x07, 0xc0}, 55 | {0x0d, 0x87}, 56 | {0x0e, 0x41}, 57 | {0x4c, 0x00}, 58 | {0x48, 0x00}, 59 | {0x5b, 0x00}, 60 | {0x42, 0x03}, 61 | {0x4a, 0x81}, 62 | {0x21, 0x99}, 63 | {0x24, 0x40}, 64 | {0x25, 0x38}, 65 | {0x26, 0x82}, 66 | {0x5c, 0x00}, 67 | {0x63, 0x00}, 68 | {0x46, 0x22}, 69 | {0x0c, 0x3c}, 70 | {0x61, 0x70}, 71 | {0x62, 0x80}, 72 | {0x7c, 0x05}, 73 | {0x20, 0x80}, 74 | {0x28, 0x30}, 75 | {0x6c, 0x00}, 76 | {0x6d, 0x80}, 77 | {0x6e, 0x00}, 78 | {0x70, 0x02}, 79 | {0x71, 0x94}, 80 | {0x73, 0xc1}, 81 | {0x3d, 0x34}, 82 | {0x5a, 0x57}, 83 | {0x12, 0x40}, 84 | {0x17, 0x11}, 85 | {0x18, 0x43}, 86 | {0x19, 0x00}, 87 | {0x1a, 0x4b}, 88 | {0x32, 0x09}, 89 | {0x37, 0xc0}, 90 | {0x4f, 0xca}, 91 | {0x50, 0xa8}, 92 | {0x5a, 0x23}, 93 | {0x6d, 0x00}, 94 | {0x3d, 0x38}, 95 | {0xff, 0x00}, 96 | {0xe5, 0x7f}, 97 | {0xf9, 0xc0}, 98 | {0x41, 0x24}, 99 | {0xe0, 0x14}, 100 | {0x76, 0xff}, 101 | {0x33, 0xa0}, 102 | {0x42, 0x20}, 103 | {0x43, 0x18}, 104 | {0x4c, 0x00}, 105 | {0x87, 0xd5}, 106 | {0x88, 0x3f}, 107 | {0xd7, 0x03}, 108 | {0xd9, 0x10}, 109 | {0xd3, 0x82}, 110 | {0xc8, 0x08}, 111 | {0xc9, 0x80}, 112 | {0x7c, 0x00}, 113 | {0x7d, 0x00}, 114 | {0x7c, 0x03}, 115 | {0x7d, 0x48}, 116 | {0x7d, 0x48}, 117 | {0x7c, 0x08}, 118 | {0x7d, 0x20}, 119 | {0x7d, 0x10}, 120 | {0x7d, 0x0e}, 121 | {0x90, 0x00}, 122 | {0x91, 0x0e}, 123 | {0x91, 0x1a}, 124 | {0x91, 0x31}, 125 | {0x91, 0x5a}, 126 | {0x91, 0x69}, 127 | {0x91, 0x75}, 128 | {0x91, 0x7e}, 129 | {0x91, 0x88}, 130 | {0x91, 0x8f}, 131 | {0x91, 0x96}, 132 | {0x91, 0xa3}, 133 | {0x91, 0xaf}, 134 | {0x91, 0xc4}, 135 | {0x91, 0xd7}, 136 | {0x91, 0xe8}, 137 | {0x91, 0x20}, 138 | {0x92, 0x00}, 139 | {0x93, 0x06}, 140 | {0x93, 0xe3}, 141 | {0x93, 0x05}, 142 | {0x93, 0x05}, 143 | {0x93, 0x00}, 144 | {0x93, 0x04}, 145 | {0x93, 0x00}, 146 | {0x93, 0x00}, 147 | {0x93, 0x00}, 148 | {0x93, 0x00}, 149 | {0x93, 0x00}, 150 | {0x93, 0x00}, 151 | {0x93, 0x00}, 152 | {0x96, 0x00}, 153 | {0x97, 0x08}, 154 | {0x97, 0x19}, 155 | {0x97, 0x02}, 156 | {0x97, 0x0c}, 157 | {0x97, 0x24}, 158 | {0x97, 0x30}, 159 | {0x97, 0x28}, 160 | {0x97, 0x26}, 161 | {0x97, 0x02}, 162 | {0x97, 0x98}, 163 | {0x97, 0x80}, 164 | {0x97, 0x00}, 165 | {0x97, 0x00}, 166 | {0xc3, 0xed}, 167 | {0xa4, 0x00}, 168 | {0xa8, 0x00}, 169 | {0xc5, 0x11}, 170 | {0xc6, 0x51}, 171 | {0xbf, 0x80}, 172 | {0xc7, 0x10}, 173 | {0xb6, 0x66}, 174 | {0xb8, 0xa5}, 175 | {0xb7, 0x64}, 176 | {0xb9, 0x7c}, 177 | {0xb3, 0xaf}, 178 | {0xb4, 0x97}, 179 | {0xb5, 0xff}, 180 | {0xb0, 0xc5}, 181 | {0xb1, 0x94}, 182 | {0xb2, 0x0f}, 183 | {0xc4, 0x5c}, 184 | {0xc0, 0x64}, 185 | {0xc1, 0x4b}, 186 | {0x8c, 0x00}, 187 | {0x86, 0x3d}, 188 | {0x50, 0x00}, 189 | {0x51, 0xc8}, 190 | {0x52, 0x96}, 191 | {0x53, 0x00}, 192 | {0x54, 0x00}, 193 | {0x55, 0x00}, 194 | {0x5a, 0xc8}, 195 | {0x5b, 0x96}, 196 | {0x5c, 0x00}, 197 | {0xd3, 0x02}, 198 | {0xc3, 0xed}, 199 | {0x7f, 0x00}, 200 | {0xda, 0x08}, 201 | {0xe5, 0x1f}, 202 | {0xe1, 0x67}, 203 | {0xe0, 0x00}, 204 | {0xdd, 0x7f}, 205 | {0x05, 0x00}, 206 | {0xff, 0x00}, 207 | {0xe0, 0x04}, 208 | {0x5a, 0x50}, 209 | {0x5b, 0x3c}, 210 | {0x5c, 0x00}, 211 | {0xe0, 0x00}, 212 | {0x00, 0x00} 213 | }; 214 | 215 | int ov2640_init(void) 216 | { 217 | uint16_t v_manuf_id; 218 | uint16_t v_device_id; 219 | ov2640_read_id(&v_manuf_id, &v_device_id); 220 | printf("manuf_id:0x%04x,device_id:0x%04x\n", v_manuf_id, v_device_id); 221 | uint16_t index = 0; 222 | for (index = 0; ov2640_config[index][0]; index++) 223 | dvp_sccb_send_data(OV2640_ADDR, ov2640_config[index][0], ov2640_config[index][1]); 224 | return 0; 225 | } 226 | 227 | int ov2640_read_id(uint16_t *manuf_id, uint16_t *device_id) 228 | { 229 | dvp_sccb_send_data(OV2640_ADDR, 0xFF, 0x01); 230 | *manuf_id = (dvp_sccb_receive_data(OV2640_ADDR, 0x1C) << 8) | dvp_sccb_receive_data(OV2640_ADDR, 0x1D); 231 | *device_id = (dvp_sccb_receive_data(OV2640_ADDR, 0x0A) << 8) | dvp_sccb_receive_data(OV2640_ADDR, 0x0B); 232 | return 0; 233 | } 234 | 235 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/ov2640.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _OV2640_H 16 | #define _OV2640_H 17 | 18 | #include 19 | 20 | #define OV2640_ADDR 0x60 21 | 22 | int ov2640_init(void); 23 | int ov2640_read_id(uint16_t *manuf_id, uint16_t *device_id); 24 | 25 | #endif /* _OV2640_H */ 26 | 27 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/ov5640.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #include 16 | #include 17 | #include "ov5640.h" 18 | #include "ov5640cfg.h" 19 | #include "dvp.h" 20 | 21 | static void hal_delay(uint32_t delay) 22 | { 23 | usleep(delay * 1000); 24 | } 25 | 26 | static void ov5640_wr_reg(uint16_t reg,uint8_t data) 27 | { 28 | dvp_sccb_send_data(OV5640_ADDR, reg, data); 29 | } 30 | 31 | static uint8_t ov5640_rd_reg(uint16_t reg) 32 | { 33 | return dvp_sccb_receive_data(OV5640_ADDR, reg); 34 | } 35 | 36 | uint8_t ov5640_init(void) 37 | { 38 | uint16_t i = 0; 39 | uint16_t reg = 0; 40 | 41 | reg = ov5640_rd_reg(OV5640_CHIPIDH); 42 | reg <<= 8; 43 | reg |= ov5640_rd_reg(OV5640_CHIPIDL); 44 | printf("ID: %X \r\n", reg); 45 | if(reg != OV5640_ID) 46 | { 47 | printf("ID: %d \r\n", reg); 48 | return 1; 49 | } 50 | 51 | ov5640_wr_reg(0x3103,0X11); /*system clock from pad, bit[1]*/ 52 | ov5640_wr_reg(0X3008,0X82); 53 | hal_delay(10); 54 | 55 | for(i = 0; i 19 | 20 | #define OV5640_ID 0X5640 21 | #define OV5640_ADDR 0X78 22 | #define OV5640_CHIPIDH 0X300A 23 | #define OV5640_CHIPIDL 0X300B 24 | 25 | #define XSIZE 320 26 | #define YSIZE 240 27 | #define LCD_GRAM_ADDRESS 0x60020000 28 | 29 | #define QQVGA_160_120 0 30 | #define QCIF_176_144 1 31 | #define QVGA_320_240 2 32 | #define WQVGA_400_240 3 33 | #define CIF_352_288 4 34 | 35 | #define jpeg_buf_size (30*1024) 36 | 37 | uint8_t ov5640_init(void); 38 | void ov5640_flash_lamp(uint8_t sw); 39 | 40 | #endif 41 | 42 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/quant.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/quant.c -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/quant.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/quant.h -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/region_layer.h: -------------------------------------------------------------------------------- 1 | #ifndef _REGION_LAYER 2 | #define _REGION_LAYER 3 | 4 | #include "kpu.h" 5 | #include "ff.h" 6 | #include 7 | 8 | typedef struct { 9 | uint32_t obj_number; 10 | struct { 11 | uint32_t x1; 12 | uint32_t y1; 13 | uint32_t x2; 14 | uint32_t y2; 15 | uint32_t class_id; 16 | float prob; 17 | } obj[64]; 18 | } obj_info_t; 19 | 20 | typedef struct { 21 | float threshold; 22 | float nms_value; 23 | uint32_t coords; 24 | uint32_t anchor_number; 25 | float *anchor; 26 | uint32_t image_width; 27 | uint32_t image_height; 28 | uint32_t classes; 29 | uint32_t net_width; 30 | uint32_t net_height; 31 | uint32_t layer_width; 32 | uint32_t layer_height; 33 | uint32_t boxes_number; 34 | uint32_t output_number; 35 | void *boxes; 36 | float *input; 37 | float *output; 38 | float *probs_buf; 39 | float **probs; 40 | FIL *logfile; 41 | float scale_x_y; 42 | } region_layer_t; 43 | 44 | typedef void (*callback_draw_box)(uint32_t x1, uint32_t y1, uint32_t x2, uint32_t y2, 45 | uint32_t class, float prob); 46 | 47 | int region_layer_init(region_layer_t *rl, int width, int height, int channels, int origin_width, 48 | int origin_height); 49 | void region_layer_deinit(region_layer_t *rl); 50 | void region_layer_run(region_layer_t *rl, obj_info_t *obj_info); 51 | void region_layer_draw_boxes(region_layer_t *rl, callback_draw_box callback); 52 | void do_more_nms_sort(region_layer_t *rl1, region_layer_t *rl2, float score_threshold); 53 | #endif // _REGION_LAYER 54 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/rgb2bmp.c: -------------------------------------------------------------------------------- 1 | #include "rgb2bmp.h" 2 | #include "ff.h" 3 | 4 | int rgb565tobmp(uint8_t *buf,int width,int height, const char *filename) 5 | { 6 | FIL file; 7 | FRESULT ret = FR_OK; 8 | uint32_t ret_len = 0; 9 | uint32_t i; 10 | uint16_t temp; 11 | uint16_t *ptr; 12 | 13 | BitMapFileHeader bmfHdr; /* file header */ 14 | BitMapInfoHeader bmiHdr; /* information header */ 15 | RgbQuad bmiClr[3]; /* color palette */ 16 | 17 | bmiHdr.biSize = sizeof(BitMapInfoHeader); 18 | bmiHdr.biWidth = width; 19 | bmiHdr.biHeight = height; 20 | bmiHdr.biPlanes = 1; 21 | bmiHdr.biBitCount = 16; 22 | bmiHdr.biCompression = BI_BITFIELDS; 23 | bmiHdr.biSizeImage = (width * height * 2); 24 | bmiHdr.biXPelsPerMeter = 0; 25 | bmiHdr.biYPelsPerMeter = 0; 26 | bmiHdr.biClrUsed = 0; 27 | bmiHdr.biClrImportant = 0; 28 | 29 | /* rgb565 mask */ 30 | bmiClr[0].rgbBlue = 0; 31 | bmiClr[0].rgbGreen = 0xF8; 32 | bmiClr[0].rgbRed = 0; 33 | bmiClr[0].rgbReserved = 0; 34 | 35 | bmiClr[1].rgbBlue = 0xE0; 36 | bmiClr[1].rgbGreen = 0x07; 37 | bmiClr[1].rgbRed = 0; 38 | bmiClr[1].rgbReserved = 0; 39 | 40 | bmiClr[2].rgbBlue = 0x1F; 41 | bmiClr[2].rgbGreen = 0; 42 | bmiClr[2].rgbRed = 0; 43 | bmiClr[2].rgbReserved = 0; 44 | 45 | bmfHdr.bfType = 0x4D42; 46 | bmfHdr.bfSize = sizeof(BitMapFileHeader) + sizeof(BitMapInfoHeader) + sizeof(RgbQuad) * 3 + bmiHdr.biSizeImage; 47 | bmfHdr.bfReserved1 = 0; 48 | bmfHdr.bfReserved2 = 0; 49 | bmfHdr.bfOffBits = sizeof(BitMapFileHeader) + sizeof(BitMapInfoHeader)+ sizeof(RgbQuad) * 3; 50 | 51 | ptr = (uint16_t*)buf; 52 | 53 | for (i = 0; i < width * height; i += 2) 54 | { 55 | temp = ptr[i]; 56 | ptr[i] = ptr[i + 1]; 57 | ptr[i + 1] = temp; 58 | } 59 | 60 | if ((ret = f_open(&file, filename, FA_CREATE_ALWAYS | FA_WRITE)) != FR_OK) 61 | { 62 | printf("create file %s err[%d]\n", filename, ret); 63 | return ret; 64 | } 65 | 66 | f_write(&file, &bmfHdr, sizeof(BitMapFileHeader), &ret_len); 67 | f_write(&file, &bmiHdr, sizeof(BitMapInfoHeader), &ret_len); 68 | f_write(&file, &bmiClr, 3 * sizeof(RgbQuad), &ret_len); 69 | 70 | f_write(&file, buf, bmiHdr.biSizeImage, &ret_len); 71 | 72 | f_close(&file); 73 | 74 | return 0; 75 | } 76 | 77 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/rgb2bmp.h: -------------------------------------------------------------------------------- 1 | #ifndef _RGB2BMP_H 2 | #define _RGB2BMP_H 3 | 4 | #include 5 | 6 | #define BI_BITFIELDS 0x3 7 | 8 | typedef struct tagBITMAPFILEHEADER 9 | { 10 | uint16_t bfType; /* file type, must be 0x4d42, ie. BM */ 11 | uint32_t bfSize; /* file size */ 12 | uint16_t bfReserved1; /* reserved, must be 0 */ 13 | uint16_t bfReserved2; /* reserved, must be 0 */ 14 | uint32_t bfOffBits; /* bmp data offset */ 15 | } __attribute__((packed)) BitMapFileHeader; 16 | 17 | typedef struct tagBITMAPINFOHEADER{ 18 | uint32_t biSize; /* size of the struct, must be 40 */ 19 | uint32_t biWidth; /* width in pixels */ 20 | uint32_t biHeight; /* height in pixels */ 21 | uint16_t biPlanes; /* must be 0 */ 22 | uint16_t biBitCount;/* bits of one pixel */ 23 | uint32_t biCompression; /* whether compressed or not */ 24 | uint32_t biSizeImage; /* bmp data size */ 25 | uint32_t biXPelsPerMeter; /* width resolution in pixels */ 26 | uint32_t biYPelsPerMeter; /* height resolution in pixels */ 27 | uint32_t biClrUsed; /* color number used */ 28 | uint32_t biClrImportant;/* important color number used */ 29 | }__attribute__((packed)) BitMapInfoHeader; 30 | 31 | typedef struct tagRGBQUAD{ 32 | uint8_t rgbBlue; /* blue component */ 33 | uint8_t rgbGreen; /* green component */ 34 | uint8_t rgbRed; /* red component */ 35 | uint8_t rgbReserved;/* reserved */ 36 | }__attribute__((packed)) RgbQuad; 37 | 38 | int rgb565tobmp(uint8_t *buf,int width,int height, const char *filename); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/sdcard.h: -------------------------------------------------------------------------------- 1 | #ifndef _SDCARD_H 2 | #define _SDCARD_H 3 | 4 | #ifdef __cplusplus 5 | extern "C" { 6 | #endif 7 | 8 | #include "stdint.h" 9 | 10 | /** 11 | * @brief Card Specific Data: CSD Register 12 | */ 13 | typedef struct { 14 | uint8_t CSDStruct; /*!< CSD structure */ 15 | uint8_t SysSpecVersion; /*!< System specification version */ 16 | uint8_t Reserved1; /*!< Reserved */ 17 | uint8_t TAAC; /*!< Data read access-time 1 */ 18 | uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */ 19 | uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */ 20 | uint16_t CardComdClasses; /*!< Card command classes */ 21 | uint8_t RdBlockLen; /*!< Max. read data block length */ 22 | uint8_t PartBlockRead; /*!< Partial blocks for read allowed */ 23 | uint8_t WrBlockMisalign; /*!< Write block misalignment */ 24 | uint8_t RdBlockMisalign; /*!< Read block misalignment */ 25 | uint8_t DSRImpl; /*!< DSR implemented */ 26 | uint8_t Reserved2; /*!< Reserved */ 27 | uint32_t DeviceSize; /*!< Device Size */ 28 | uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */ 29 | uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */ 30 | uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */ 31 | uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */ 32 | uint8_t DeviceSizeMul; /*!< Device size multiplier */ 33 | uint8_t EraseGrSize; /*!< Erase group size */ 34 | uint8_t EraseGrMul; /*!< Erase group size multiplier */ 35 | uint8_t WrProtectGrSize; /*!< Write protect group size */ 36 | uint8_t WrProtectGrEnable; /*!< Write protect group enable */ 37 | uint8_t ManDeflECC; /*!< Manufacturer default ECC */ 38 | uint8_t WrSpeedFact; /*!< Write speed factor */ 39 | uint8_t MaxWrBlockLen; /*!< Max. write data block length */ 40 | uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */ 41 | uint8_t Reserved3; /*!< Reserded */ 42 | uint8_t ContentProtectAppli; /*!< Content protection application */ 43 | uint8_t FileFormatGrouop; /*!< File format group */ 44 | uint8_t CopyFlag; /*!< Copy flag (OTP) */ 45 | uint8_t PermWrProtect; /*!< Permanent write protection */ 46 | uint8_t TempWrProtect; /*!< Temporary write protection */ 47 | uint8_t FileFormat; /*!< File Format */ 48 | uint8_t ECC; /*!< ECC code */ 49 | uint8_t CSD_CRC; /*!< CSD CRC */ 50 | uint8_t Reserved4; /*!< always 1*/ 51 | } SD_CSD; 52 | 53 | /** 54 | * @brief Card Identification Data: CID Register 55 | */ 56 | typedef struct { 57 | uint8_t ManufacturerID; /*!< ManufacturerID */ 58 | uint16_t OEM_AppliID; /*!< OEM/Application ID */ 59 | uint32_t ProdName1; /*!< Product Name part1 */ 60 | uint8_t ProdName2; /*!< Product Name part2*/ 61 | uint8_t ProdRev; /*!< Product Revision */ 62 | uint32_t ProdSN; /*!< Product Serial Number */ 63 | uint8_t Reserved1; /*!< Reserved1 */ 64 | uint16_t ManufactDate; /*!< Manufacturing Date */ 65 | uint8_t CID_CRC; /*!< CID CRC */ 66 | uint8_t Reserved2; /*!< always 1 */ 67 | } SD_CID; 68 | 69 | /** 70 | * @brief SD Card information 71 | */ 72 | typedef struct { 73 | SD_CSD SD_csd; 74 | SD_CID SD_cid; 75 | uint64_t CardCapacity; /*!< Card Capacity */ 76 | uint32_t CardBlockSize; /*!< Card Block Size */ 77 | } SD_CardInfo; 78 | 79 | extern SD_CardInfo cardinfo; 80 | 81 | uint8_t sd_init(void); 82 | uint8_t sd_read_sector(uint8_t *data_buff, uint32_t sector, uint32_t count); 83 | uint8_t sd_write_sector(uint8_t *data_buff, uint32_t sector, uint32_t count); 84 | uint8_t sd_read_sector_dma(uint8_t *data_buff, uint32_t sector, uint32_t count); 85 | uint8_t sd_write_sector_dma(uint8_t *data_buff, uint32_t sector, uint32_t count); 86 | 87 | #ifdef __cplusplus 88 | } 89 | #endif 90 | 91 | #endif 92 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/stdefine.h: -------------------------------------------------------------------------------- 1 | /* ��׼ͷ�ļ� */ 2 | #ifndef __STDEFINE_H__ 3 | #define __STDEFINE_H__ 4 | 5 | #if defined(WIN32) 6 | #include 7 | #else 8 | #include 9 | #define TRUE 1 10 | #define FALSE 0 11 | typedef int BOOL ; 12 | typedef uint8_t BYTE ; 13 | typedef uint16_t WORD ; 14 | typedef uint32_t DWORD; 15 | #endif 16 | 17 | //++ for fixed point 18 | #define FIXQ 11 19 | #define FLOAT2FIX(f) ((int)((f) * (1 << 11))) 20 | //-- for fixed point 21 | 22 | #endif 23 | 24 | 25 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/w25qxx.h: -------------------------------------------------------------------------------- 1 | /* Copyright 2018 Canaan Inc. 2 | * 3 | * Licensed under the Apache License, Version 2.0 (the "License"); 4 | * you may not use this file except in compliance with the License. 5 | * You may obtain a copy of the License at 6 | * 7 | * http://www.apache.org/licenses/LICENSE-2.0 8 | * 9 | * Unless required by applicable law or agreed to in writing, software 10 | * distributed under the License is distributed on an "AS IS" BASIS, 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | * See the License for the specific language governing permissions and 13 | * limitations under the License. 14 | */ 15 | #ifndef _W25QXX_H 16 | #define _W25QXX_H 17 | 18 | #include 19 | 20 | /* clang-format off */ 21 | #define DATALENGTH 8 22 | 23 | #define w25qxx_FLASH_PAGE_SIZE 256 24 | #define w25qxx_FLASH_SECTOR_SIZE 4096 25 | #define w25qxx_FLASH_PAGE_NUM_PER_SECTOR 16 26 | #define w25qxx_FLASH_CHIP_SIZE (16777216 UL) 27 | 28 | #define WRITE_ENABLE 0x06 29 | #define WRITE_DISABLE 0x04 30 | #define READ_REG1 0x05 31 | #define READ_REG2 0x35 32 | #define READ_REG3 0x15 33 | #define WRITE_REG1 0x01 34 | #define WRITE_REG2 0x31 35 | #define WRITE_REG3 0x11 36 | #define READ_DATA 0x03 37 | #define FAST_READ 0x0B 38 | #define FAST_READ_DUAL_OUTPUT 0x3B 39 | #define FAST_READ_QUAL_OUTPUT 0x6B 40 | #define FAST_READ_DUAL_IO 0xBB 41 | #define FAST_READ_QUAL_IO 0xEB 42 | #define DUAL_READ_RESET 0xFFFF 43 | #define QUAL_READ_RESET 0xFF 44 | #define PAGE_PROGRAM 0x02 45 | #define QUAD_PAGE_PROGRAM 0x32 46 | #define SECTOR_ERASE 0x20 47 | #define BLOCK_32K_ERASE 0x52 48 | #define BLOCK_64K_ERASE 0xD8 49 | #define CHIP_ERASE 0x60 50 | #define READ_ID 0x90 51 | #define ENABLE_QPI 0x38 52 | #define EXIT_QPI 0xFF 53 | #define ENABLE_RESET 0x66 54 | #define RESET_DEVICE 0x99 55 | 56 | #define REG1_BUSY_MASK 0x01 57 | #define REG2_QUAL_MASK 0x02 58 | 59 | #define LETOBE(x) ((x >> 24) | ((x & 0x00FF0000) >> 8) | ((x & 0x0000FF00) << 8) | (x << 24)) 60 | /* clang-format on */ 61 | 62 | /** 63 | * @brief w25qxx operating status enumerate 64 | */ 65 | typedef enum _w25qxx_status 66 | { 67 | W25QXX_OK = 0, 68 | W25QXX_BUSY, 69 | W25QXX_ERROR, 70 | } w25qxx_status_t; 71 | 72 | /** 73 | * @brief w25qxx read operating enumerate 74 | */ 75 | typedef enum _w25qxx_read 76 | { 77 | W25QXX_STANDARD = 0, 78 | W25QXX_STANDARD_FAST, 79 | W25QXX_DUAL, 80 | W25QXX_DUAL_FAST, 81 | W25QXX_QUAD, 82 | W25QXX_QUAD_FAST, 83 | } w25qxx_read_t; 84 | 85 | w25qxx_status_t w25qxx_init(uint8_t spi_index, uint8_t spi_ss); 86 | w25qxx_status_t w25qxx_is_busy(void); 87 | w25qxx_status_t w25qxx_chip_erase(void); 88 | w25qxx_status_t w25qxx_enable_quad_mode(void); 89 | w25qxx_status_t w25qxx_disable_quad_mode(void); 90 | w25qxx_status_t w25qxx_sector_erase(uint32_t addr); 91 | w25qxx_status_t w25qxx_32k_block_erase(uint32_t addr); 92 | w25qxx_status_t w25qxx_64k_block_erase(uint32_t addr); 93 | w25qxx_status_t w25qxx_read_status_reg1(uint8_t *reg_data); 94 | w25qxx_status_t w25qxx_read_status_reg2(uint8_t *reg_data); 95 | w25qxx_status_t w25qxx_write_status_reg(uint8_t reg1_data, uint8_t reg2_data); 96 | w25qxx_status_t w25qxx_read_id(uint8_t *manuf_id, uint8_t *device_id); 97 | w25qxx_status_t w25qxx_write_data(uint32_t addr, uint8_t *data_buf, uint32_t length); 98 | w25qxx_status_t w25qxx_write_data_direct(uint32_t addr, uint8_t *data_buf, uint32_t length); 99 | w25qxx_status_t w25qxx_read_data(uint32_t addr, uint8_t *data_buf, uint32_t length, w25qxx_read_t mode); 100 | w25qxx_status_t w25qxx_enable_xip_mode(void); 101 | w25qxx_status_t w25qxx_disable_xip_mode(void); 102 | w25qxx_status_t w25qxx_read_id_dma(uint8_t *manuf_id, uint8_t *device_id); 103 | w25qxx_status_t w25qxx_sector_erase_dma(uint32_t addr); 104 | w25qxx_status_t w25qxx_init_dma(uint8_t spi_index, uint8_t spi_ss); 105 | w25qxx_status_t w25qxx_write_data_dma(uint32_t addr, uint8_t *data_buf, uint32_t length); 106 | w25qxx_status_t w25qxx_write_data_direct_dma(uint32_t addr, uint8_t *data_buf, uint32_t length); 107 | w25qxx_status_t w25qxx_read_data_dma(uint32_t addr, uint8_t *data_buf, uint32_t length, w25qxx_read_t mode); 108 | w25qxx_status_t w25qxx_is_busy_dma(void); 109 | w25qxx_status_t w25qxx_enable_quad_mode_dma(void); 110 | 111 | #endif 112 | 113 | -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/yolo3_frame_test_public_maixpy: -------------------------------------------------------------------------------- 1 | c:/home/zqh/workspace/K210_C_CODE/yolo3_frame_test_public_maixpy -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/zigzag.c: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/zigzag.c -------------------------------------------------------------------------------- /src/yolo3_frame_test_public_maixpy/zigzag.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zyayoung/K210-Tracking/d1641d84d40d05eabf8b152f5d0e1627bfc3fdd8/src/yolo3_frame_test_public_maixpy/zigzag.h --------------------------------------------------------------------------------