diff --git a/bscripts/build_package.sh b/bscripts/build_package.sh index 39117fc6..2edb683a 100755 --- a/bscripts/build_package.sh +++ b/bscripts/build_package.sh @@ -24,6 +24,7 @@ mv src/Makefile.static src/Makefile mv tools/srtank/Makefile.static tools/srtank/Makefile mv tools/espmsim/Makefile.static tools/espmsim/Makefile mv tools/PinViewer/Makefile.static tools/PinViewer/Makefile +mv tools/picsimlab_tool/Makefile.static tools/picsimlab_tool/Makefile PWD=`pwd` echo -e "\noverride_dh_auto_build:\n\tmake -j$(nproc) PKG=\"DEB\" $1\n\n" >> debian/rules echo -e "\noverride_dh_auto_install:\n\tdh_auto_install\n\tinstall -d ${PWD}/debian/picsimlab/usr/lib/picsimlab/" >> debian/rules diff --git a/bscripts/build_package_NOGUI.sh b/bscripts/build_package_NOGUI.sh index 65a56200..e5df67ba 100755 --- a/bscripts/build_package_NOGUI.sh +++ b/bscripts/build_package_NOGUI.sh @@ -37,6 +37,7 @@ sed -i 's/picsimlab /picsimlab_NOGUI /g' src/Makefile mv tools/srtank/Makefile.static tools/srtank/Makefile mv tools/espmsim/Makefile.static tools/espmsim/Makefile mv tools/PinViewer/Makefile.static tools/PinViewer/Makefile +mv tools/picsimlab_tool/Makefile.static tools/picsimlab_tool/Makefile rm -rf share/*/*/*.png PWD=`pwd` echo -e "\noverride_dh_auto_build:\n\tmake -j$(nproc) PKG=\"DEB\" $1\n\n" >> debian/rules diff --git a/src/boards/board_Blue_Pill.cc b/src/boards/board_Blue_Pill.cc index 91dd20a0..b1b6ec74 100644 --- a/src/boards/board_Blue_Pill.cc +++ b/src/boards/board_Blue_Pill.cc @@ -778,14 +778,14 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim2 ch1 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 0) { - pwm_out.pins[(1 << 2) + 0] = iopin(A, 0); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 0, iopin(A, 0)); } break; case 1: // tim2 ch2 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 0) { - pwm_out.pins[(1 << 2) + 1] = iopin(A, 1); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 1, iopin(A, 1)); } break; case 2: @@ -800,7 +800,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim2 ch3 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 0) { - pwm_out.pins[(1 << 2) + 2] = iopin(A, 2); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 2, iopin(A, 2)); } break; case 3: @@ -815,7 +815,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim2 ch4 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 0) { - pwm_out.pins[(1 << 2) + 3] = iopin(A, 3); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 3, iopin(A, 3)); } break; case 4: @@ -843,7 +843,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim3 ch1 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 0) { - pwm_out.pins[(2 << 2) + 0] = iopin(A, 6); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 0, iopin(A, 6)); } break; case 7: @@ -859,20 +859,20 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim1_ch1n (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 1) { - pwm_out.pins[(0 << 2) + 0] = iopin(A, 7); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 0, iopin(A, 7)); break; } // tim3 ch2 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 0) { - pwm_out.pins[(2 << 2) + 1] = iopin(A, 7); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 1, iopin(A, 7)); } break; case 8: // tim1 ch1 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 0) { - pwm_out.pins[(0 << 2) + 0] = iopin(A, 8); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 0, iopin(A, 8)); } break; case 9: @@ -887,7 +887,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim1 ch2 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 0) { - pwm_out.pins[(0 << 2) + 1] = iopin(A, 9); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 1, iopin(A, 9)); } break; case 10: @@ -902,14 +902,14 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim1 ch3 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 0) { - pwm_out.pins[(0 << 2) + 2] = iopin(A, 10); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 2, iopin(A, 10)); } break; case 11: // tim1 ch4 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 0) { - pwm_out.pins[(0 << 2) + 3] = iopin(A, 11); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 3, iopin(A, 11)); } break; case 12: @@ -928,26 +928,26 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim1_ch2n (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 1) { - pwm_out.pins[(0 << 2) + 1] = iopin(B, 0); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 1, iopin(B, 0)); break; } // tim3_ch3 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 0) { - pwm_out.pins[(2 << 2) + 2] = iopin(B, 0); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 2, iopin(B, 0)); } break; case 1: // tim1_ch3n (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 1) { - pwm_out.pins[(0 << 2) + 2] = iopin(B, 1); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 2, iopin(B, 1)); break; } // tim3_ch4 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 0) { - pwm_out.pins[(2 << 2) + 3] = iopin(B, 1); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 3, iopin(B, 1)); } break; @@ -957,14 +957,14 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim2_ch2n (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 1) { - pwm_out.pins[(1 << 2) + 1] = iopin(B, 3); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 1, iopin(B, 3)); break; } case 4: // tim3_ch1n (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 2) { - pwm_out.pins[(2 << 2) + 0] = iopin(B, 4); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 0, iopin(B, 4)); break; } case 5: @@ -980,7 +980,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim3_ch2n (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 2) { - pwm_out.pins[(2 << 2) + 1] = iopin(B, 5); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 1, iopin(B, 5)); break; } break; @@ -1004,7 +1004,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim4 ch1 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM4); if (*afio == 0) { - pwm_out.pins[(3 << 2) + 0] = iopin(B, 6); + bitbang_pwm_set_pin(&pwm_out, (3 << 2) + 0, iopin(B, 6)); } break; case 7: @@ -1027,7 +1027,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim4 ch2 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM4); if (*afio == 0) { - pwm_out.pins[(3 << 2) + 1] = iopin(B, 7); + bitbang_pwm_set_pin(&pwm_out, (3 << 2) + 1, iopin(B, 7)); } break; case 8: @@ -1042,7 +1042,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim4 ch3 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM4); if (*afio == 0) { - pwm_out.pins[(3 << 2) + 2] = iopin(B, 8); + bitbang_pwm_set_pin(&pwm_out, (3 << 2) + 2, iopin(B, 8)); } case 9: // i2c0 (alt) @@ -1056,14 +1056,14 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim4 ch4 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM4); if (*afio == 0) { - pwm_out.pins[(3 << 2) + 3] = iopin(B, 9); + bitbang_pwm_set_pin(&pwm_out, (3 << 2) + 3, iopin(B, 9)); } break; case 10: // tim2_ch3n (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 1) { - pwm_out.pins[(1 << 2) + 2] = iopin(B, 10); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 2, iopin(B, 10)); break; } /* @@ -1090,7 +1090,7 @@ void cboard_Blue_Pill::PinsExtraConfig(int cfg) { // tim2_ch4n (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 1) { - pwm_out.pins[(1 << 2) + 3] = iopin(B, 10); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 3, iopin(B, 10)); break; } /* diff --git a/src/boards/board_C3_DevKitC.cc b/src/boards/board_C3_DevKitC.cc index 2b4c7807..c1f963e2 100644 --- a/src/boards/board_C3_DevKitC.cc +++ b/src/boards/board_C3_DevKitC.cc @@ -934,7 +934,7 @@ void cboard_C3_DevKitC::PinsExtraConfig(int cfg) { case 49: // ledc_ls_sig_out4 case 50: // ledc_ls_sig_out5 // printf("LEDC channel %i in GPIO %i\n", function - 45, gpio); - pwm_out.pins[function - 45] = io2pin(gpio); + bitbang_pwm_set_pin(&pwm_out, function - 45, io2pin(gpio)); break; case 51: // rmt_sig_out0 case 52: // rmt_sig_out1 @@ -1046,9 +1046,17 @@ void cboard_C3_DevKitC::PinsExtraConfig(int cfg) { } } break; - case QEMU_EXTRA_PIN_LEDC_CFG: - bitbang_pwm_set_duty(&pwm_out, (cfg & 0x0F00) >> 8, cfg & 0xFF); - break; + case QEMU_EXTRA_PIN_LEDC_CFG: { + uint32_t* channel_conf0_reg = qemu_picsimlab_get_internals(QEMU_INTERNAL_LEDC_CHANNEL_CONF); // 16 + uint32_t* timer_freq = qemu_picsimlab_get_internals(QEMU_INTERNAL_LEDC_TIMER_FREQ); // 8 + float* channel_duty = (float*)qemu_picsimlab_get_internals(QEMU_INTERNAL_LEDC_CHANNEL_DUTY); // 16 + int channel = (cfg & 0x0F00) >> 8; + int timern = channel_conf0_reg[channel] & 0x03; + + bitbang_pwm_set_freq(&pwm_out, channel, timer_freq[timern]); + // bitbang_pwm_set_duty(&pwm_out, channel, cfg & 0xFF); + bitbang_pwm_set_duty_f(&pwm_out, channel, channel_duty[channel]); + } break; } } diff --git a/src/boards/board_DevKitC.cc b/src/boards/board_DevKitC.cc index 7a1ad3f6..cf512508 100644 --- a/src/boards/board_DevKitC.cc +++ b/src/boards/board_DevKitC.cc @@ -992,7 +992,7 @@ void cboard_DevKitC::PinsExtraConfig(int cfg) { case 85: // ledc_ls_sig_out6 case 86: // ledc_ls_sig_out7 // printf("LEDC channel %i in GPIO %i\n",function - 71, gpio); - pwm_out.pins[function - 71] = io2pin(gpio); + bitbang_pwm_set_pin(&pwm_out, function - 71, io2pin(gpio)); break; case 87: // rmt_sig_out0 // case 88: // rmt_sig_out1 //FIXME only channel 0 enabled @@ -1108,7 +1108,18 @@ void cboard_DevKitC::PinsExtraConfig(int cfg) { } break; case QEMU_EXTRA_PIN_LEDC_CFG: - bitbang_pwm_set_duty(&pwm_out, (cfg & 0x0F00) >> 8, cfg & 0xFF); + uint32_t* channel_conf0_reg = qemu_picsimlab_get_internals(QEMU_INTERNAL_LEDC_CHANNEL_CONF); // 16 + uint32_t* timer_freq = qemu_picsimlab_get_internals(QEMU_INTERNAL_LEDC_TIMER_FREQ); // 8 + float* channel_duty = (float*)qemu_picsimlab_get_internals(QEMU_INTERNAL_LEDC_CHANNEL_DUTY); // 16 + int channel = (cfg & 0x0F00) >> 8; + int timern = channel_conf0_reg[channel] & 0x03; + if (channel >= 8) { + timern += 4; // use LSTimers + } + + bitbang_pwm_set_freq(&pwm_out, channel, timer_freq[timern]); + // bitbang_pwm_set_duty(&pwm_out, channel, cfg & 0xFF); + bitbang_pwm_set_duty_f(&pwm_out, channel, channel_duty[channel]); break; } } diff --git a/src/boards/board_STM32_H103.cc b/src/boards/board_STM32_H103.cc index 447dc265..35317895 100644 --- a/src/boards/board_STM32_H103.cc +++ b/src/boards/board_STM32_H103.cc @@ -921,7 +921,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch1 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 0) { - pwm_out.pins[(1 << 2) + 0] = iopin(A, 0); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 0, iopin(A, 0)); } break; case 1: @@ -929,7 +929,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch2 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 0) { - pwm_out.pins[(1 << 2) + 1] = iopin(A, 1); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 1, iopin(A, 1)); } break; case 2: @@ -945,7 +945,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch3 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 0) { - pwm_out.pins[(1 << 2) + 2] = iopin(A, 2); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 2, iopin(A, 2)); } break; case 3: @@ -961,7 +961,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch4 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 0) { - pwm_out.pins[(1 << 2) + 3] = iopin(A, 3); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 3, iopin(A, 3)); } break; case 4: @@ -990,7 +990,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch1 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 0) { - pwm_out.pins[(2 << 2) + 0] = iopin(A, 6); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 0, iopin(A, 6)); } break; case 7: @@ -1007,14 +1007,14 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 1) { - pwm_out.pins[(0 << 2) + 0] = iopin(A, 7); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 0, iopin(A, 7)); break; } // tim3 // ch2 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 0) { - pwm_out.pins[(2 << 2) + 1] = iopin(A, 7); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 1, iopin(A, 7)); } break; case 8: @@ -1022,7 +1022,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch1 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 0) { - pwm_out.pins[(0 << 2) + 0] = iopin(A, 8); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 0, iopin(A, 8)); } break; case 9: @@ -1038,7 +1038,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch2 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 0) { - pwm_out.pins[(0 << 2) + 1] = iopin(A, 9); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 1, iopin(A, 9)); } break; case 10: @@ -1054,7 +1054,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch3 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 0) { - pwm_out.pins[(0 << 2) + 2] = iopin(A, 10); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 2, iopin(A, 10)); } break; case 11: @@ -1062,7 +1062,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch4 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 0) { - pwm_out.pins[(0 << 2) + 3] = iopin(A, 11); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 3, iopin(A, 11)); } break; case 12: @@ -1082,13 +1082,13 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 1) { - pwm_out.pins[(0 << 2) + 1] = iopin(B, 0); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 1, iopin(B, 0)); break; } // tim3_ch3 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 0) { - pwm_out.pins[(2 << 2) + 2] = iopin(B, 0); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 2, iopin(B, 0)); } break; case 1: @@ -1096,13 +1096,13 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM1); if (*afio == 1) { - pwm_out.pins[(0 << 2) + 2] = iopin(B, 1); + bitbang_pwm_set_pin(&pwm_out, (0 << 2) + 2, iopin(B, 1)); break; } // tim3_ch4 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 0) { - pwm_out.pins[(2 << 2) + 3] = iopin(B, 1); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 3, iopin(B, 1)); } break; @@ -1113,7 +1113,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 1) { - pwm_out.pins[(1 << 2) + 1] = iopin(B, 3); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 1, iopin(B, 3)); break; } case 4: @@ -1121,7 +1121,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 2) { - pwm_out.pins[(2 << 2) + 0] = iopin(B, 4); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 0, iopin(B, 4)); break; } case 5: @@ -1139,7 +1139,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM3); if (*afio == 2) { - pwm_out.pins[(2 << 2) + 1] = iopin(B, 5); + bitbang_pwm_set_pin(&pwm_out, (2 << 2) + 1, iopin(B, 5)); break; } break; @@ -1165,7 +1165,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch1 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM4); if (*afio == 0) { - pwm_out.pins[(3 << 2) + 0] = iopin(B, 6); + bitbang_pwm_set_pin(&pwm_out, (3 << 2) + 0, iopin(B, 6)); } break; case 7: @@ -1190,7 +1190,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch2 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM4); if (*afio == 0) { - pwm_out.pins[(3 << 2) + 1] = iopin(B, 7); + bitbang_pwm_set_pin(&pwm_out, (3 << 2) + 1, iopin(B, 7)); } break; case 8: @@ -1207,7 +1207,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch3 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM4); if (*afio == 0) { - pwm_out.pins[(3 << 2) + 2] = iopin(B, 8); + bitbang_pwm_set_pin(&pwm_out, (3 << 2) + 2, iopin(B, 8)); } case 9: // i2c0 @@ -1223,7 +1223,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // ch4 afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM4); if (*afio == 0) { - pwm_out.pins[(3 << 2) + 3] = iopin(B, 9); + bitbang_pwm_set_pin(&pwm_out, (3 << 2) + 3, iopin(B, 9)); } break; case 10: @@ -1231,7 +1231,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 1) { - pwm_out.pins[(1 << 2) + 2] = iopin(B, 10); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 2, iopin(B, 10)); break; } /* @@ -1271,7 +1271,7 @@ void cboard_STM32_H103::PinsExtraConfig(int cfg) { // (alt) afio = qemu_picsimlab_get_internals(0x1000 | STM32_TIM2); if (*afio == 1) { - pwm_out.pins[(1 << 2) + 3] = iopin(B, 10); + bitbang_pwm_set_pin(&pwm_out, (1 << 2) + 3, iopin(B, 10)); break; } /* diff --git a/src/boards/bsim_qemu.cc b/src/boards/bsim_qemu.cc index db640c13..dd19a500 100644 --- a/src/boards/bsim_qemu.cc +++ b/src/boards/bsim_qemu.cc @@ -1230,13 +1230,13 @@ void bsim_qemu::MStep(void) { } } } - for (int i = 0; i < pwm_out.channels; i++) { - if (pwm_out.pins[i]) { - pins[pwm_out.pins[i] - 1].dir = PD_OUT; - pins[pwm_out.pins[i] - 1].value = pwm_out.out[i]; + for (int i = 0; i < pwm_out.channels_count; i++) { + if (pwm_out.channels[i].pin) { + pins[pwm_out.channels[i].pin - 1].dir = PD_OUT; + pins[pwm_out.channels[i].pin - 1].value = pwm_out.channels[i].out; } } - for (int i = 0; i < rmt_out.channels; i++) { + for (int i = 0; i < rmt_out.channels_count; i++) { if (rmt_out.pins[i]) { pins[rmt_out.pins[i] - 1].dir = PD_OUT; pins[rmt_out.pins[i] - 1].value = rmt_out.out[i]; diff --git a/src/boards/qemu.h b/src/boards/qemu.h index 8affeb7c..0109183e 100644 --- a/src/boards/qemu.h +++ b/src/boards/qemu.h @@ -114,6 +114,12 @@ enum i2c_event { #define QEMU_INTERNAL_GPIO_IN_SEL 1 #define QEMU_INTERNAL_GPIO_OUT_SEL 2 #define QEMU_INTERNAL_IOMUX_GPIOS 3 +#define QEMU_INTERNAL_LEDC_CHANNEL_CONF 4 +#define QEMU_INTERNAL_LEDC_TIMER_FREQ 5 +#define QEMU_INTERNAL_LEDC_CHANNEL_DUTY 6 +#define QEMU_INTERNAL_UART0_BAUD 7 +#define QEMU_INTERNAL_UART1_BAUD 8 +#define QEMU_INTERNAL_UART2_BAUD 9 #define QEMU_EXTRA_PIN_IN_CFG 1 #define QEMU_EXTRA_PIN_OUT_CFG 2 diff --git a/src/devices/bitbang_out.cc b/src/devices/bitbang_out.cc index 2a1963f0..4d8cd840 100644 --- a/src/devices/bitbang_out.cc +++ b/src/devices/bitbang_out.cc @@ -37,14 +37,14 @@ void bitbang_out_rst(bitbang_out_t* out) { dprintf("bitbang_out rst\n"); - for (int i = 0; i < out->channels; i++) { + for (int i = 0; i < out->channels_count; i++) { out->out[i] = 0; } } -void bitbang_out_init(bitbang_out_t* out, board* pboard, const unsigned char channels) { +void bitbang_out_init(bitbang_out_t* out, board* pboard, const unsigned char channels_count) { dprintf("bitbang_out init\n"); - out->channels = channels; + out->channels_count = channels_count; bitbang_out_rst(out); out->pboard = pboard; memset(out->pins, 0, OUT_MAX); diff --git a/src/devices/bitbang_out.h b/src/devices/bitbang_out.h index 9c2b8e3c..088571ea 100644 --- a/src/devices/bitbang_out.h +++ b/src/devices/bitbang_out.h @@ -31,7 +31,7 @@ class board; #define OUT_MAX 32 typedef struct { - unsigned char channels; + unsigned char channels_count; board* pboard; unsigned char pins[OUT_MAX]; unsigned char out[OUT_MAX]; diff --git a/src/devices/bitbang_pwm.cc b/src/devices/bitbang_pwm.cc index 25bf562b..981fd163 100644 --- a/src/devices/bitbang_pwm.cc +++ b/src/devices/bitbang_pwm.cc @@ -37,57 +37,95 @@ void bitbang_pwm_rst(bitbang_pwm_t* pwm) { dprintf("bitbang_pwm rst\n"); - pwm->counter = 0; - for (int i = 0; i < pwm->channels; i++) { - pwm->duty[i] = 0; - pwm->out[i] = 0; + for (int i = 0; i < pwm->channels_count; i++) { + pwm->channels[i].counter = 0; + pwm->channels[i].duty = 0; + pwm->channels[i].out = 0; + pwm->channels[i].freq = 0; } } static void bitbang_pwm_ctrl_callback(void* arg) { - bitbang_pwm_t* pwm = (bitbang_pwm_t*)arg; + channel_pwm_t* channel = (channel_pwm_t*)arg; unsigned char out; - for (int i = 0; i < pwm->channels; i++) { - if (pwm->pins[i]) { - if (pwm->counter < pwm->duty[i]) { - out = 1; - } else { - out = 0; - } - if (pwm->out[i] != out) { - pwm->out[i] = out; - ioupdated = 1; - } + if (channel->pin) { + if (channel->counter < channel->duty) { + out = 1; + } else { + out = 0; } - } + if (channel->out != out) { + channel->out = out; + ioupdated = 1; + } + + channel->counter++; - pwm->counter++; - if (pwm->counter > 99) { - pwm->counter = 0; + if (channel->counter > ((100 * channel->res) - 1)) { + channel->counter = 0; + } } } void bitbang_pwm_init(bitbang_pwm_t* pwm, board* pboard, const unsigned char channels) { dprintf("bitbang_pwm init\n"); - pwm->channels = channels; - bitbang_pwm_rst(pwm); + pwm->channels_count = channels; pwm->pboard = pboard; - pwm->TimerID = pwm->pboard->TimerRegister_us(2, bitbang_pwm_ctrl_callback, pwm); - pwm->pboard->TimerChange_us(pwm->TimerID, 2); // FIXME only 5kHz frequency - pwm->pboard->TimerSetState(pwm->TimerID, 0); // disabled - memset(pwm->pins, 0, LEDC_MAX); - memset(pwm->out, 0, LEDC_MAX); - memset(pwm->duty, 0, LEDC_MAX); + memset(pwm->channels, 0, sizeof(channel_pwm_t) * PWM_CHANNEL_MAX); } void bitbang_pwm_end(bitbang_pwm_t* pwm) { dprintf("bitbang_pwm end\n"); - pwm->pboard->TimerUnregister(pwm->TimerID); + for (int i = 0; i < pwm->channels_count; i++) { + if (pwm->channels[i].pin) { + pwm->pboard->TimerUnregister(pwm->channels[i].TimerID); + } + } +} + +void bitbang_pwm_set_pin(bitbang_pwm_t* pwm, const unsigned char channel, const char pin) { + pwm->channels[channel].pin = pin; + if (!pwm->channels[channel].TimerID) { + pwm->channels[channel].TimerID = + pwm->pboard->TimerRegister_us(2, bitbang_pwm_ctrl_callback, &pwm->channels[channel]); + } + + if (!pwm->channels[channel].freq) { + bitbang_pwm_set_freq(pwm, channel, 5000); + } else { + bitbang_pwm_set_freq(pwm, channel, pwm->channels[channel].freq); + } + pwm->pboard->TimerSetState(pwm->channels[channel].TimerID, 1); } void bitbang_pwm_set_duty(bitbang_pwm_t* pwm, const unsigned char channel, const char duty) { - dprintf("bitbang_pwm chanell %i (pin %i) set duty to %i%% \n", channel, pwm->pins[channel], duty); - pwm->pboard->TimerSetState(pwm->TimerID, 1); - pwm->duty[channel] = duty; + dprintf("bitbang_pwm channel %i (pin %i) set duty to %i%% \n", channel, pwm->channels[channel].pin, duty); + pwm->channels[channel].duty = duty * pwm->channels[channel].res; } + +void bitbang_pwm_set_duty_f(bitbang_pwm_t* pwm, const unsigned char channel, const float duty) { + dprintf("bitbang_pwm channel %i (pin %i) set duty to %f%% \n", channel, pwm->channels[channel].pin, duty); + pwm->channels[channel].duty = duty * pwm->channels[channel].res; +} + +void bitbang_pwm_set_freq(bitbang_pwm_t* pwm, const unsigned char channel, unsigned int freq) { + if (freq > 10000) { + freq = 10000; + } + if (freq <= 0) { + freq = 1; + } + if ((pwm->channels[channel].freq != freq) && (pwm->channels[channel].TimerID)) { + dprintf("bitbang_pwm channel %i (pin %i) set freq to %i%% \n", channel, pwm->channels[channel].pin, freq); + if (freq > 1000) { // 0-100 + pwm->channels[channel].res = 1; + } else if (freq > 200) { // 0-200 + pwm->channels[channel].res = 2; + } else { // 0-400 + pwm->channels[channel].res = 4; + } + pwm->pboard->TimerChange_us(pwm->channels[channel].TimerID, 10000.0 / (freq * pwm->channels[channel].res)); + pwm->channels[channel].freq = freq; + } +} \ No newline at end of file diff --git a/src/devices/bitbang_pwm.h b/src/devices/bitbang_pwm.h index 8c020087..8a82ca84 100644 --- a/src/devices/bitbang_pwm.h +++ b/src/devices/bitbang_pwm.h @@ -28,21 +28,30 @@ class board; -#define LEDC_MAX 32 +#define PWM_CHANNEL_MAX 32 typedef struct { - unsigned char channels; - board* pboard; int TimerID; unsigned int counter; - unsigned char pins[LEDC_MAX]; - unsigned char out[LEDC_MAX]; - unsigned char duty[LEDC_MAX]; + unsigned char pin; + unsigned char out; + unsigned char duty; + unsigned int freq; + unsigned int res; +} channel_pwm_t; + +typedef struct { + unsigned char channels_count; + board* pboard; + channel_pwm_t channels[PWM_CHANNEL_MAX]; } bitbang_pwm_t; void bitbang_pwm_init(bitbang_pwm_t* pwm, board* pboard, const unsigned char channels = 8); void bitbang_pwm_rst(bitbang_pwm_t* pwm); void bitbang_pwm_end(bitbang_pwm_t* pwm); void bitbang_pwm_set_duty(bitbang_pwm_t* pwm, const unsigned char channel, const char duty); +void bitbang_pwm_set_duty_f(bitbang_pwm_t* pwm, const unsigned char channel, const float duty); +void bitbang_pwm_set_pin(bitbang_pwm_t* pwm, const unsigned char channel, const char pin); +void bitbang_pwm_set_freq(bitbang_pwm_t* pwm, const unsigned char channel, unsigned int freq); #endif // BITBANG_PWM \ No newline at end of file