diff --git a/right/build/kds/.cproject b/right/build/kds/.cproject index 940f763..3eb3f5f 100644 --- a/right/build/kds/.cproject +++ b/right/build/kds/.cproject @@ -595,13 +595,17 @@ - + + + + + diff --git a/right/build/kds/.project b/right/build/kds/.project index 7712e82..4844b0b 100644 --- a/right/build/kds/.project +++ b/right/build/kds/.project @@ -70,6 +70,16 @@ 1 $%7BPARENT-3-PROJECT_LOC%7D/lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_clock.h + + drivers/fsl_ftm.c + 1 + $%7BPARENT-3-PROJECT_LOC%7D/lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_ftm.c + + + drivers/fsl_ftm.h + 1 + $%7BPARENT-3-PROJECT_LOC%7D/lib/KSDK_2.0_MK22FN512xxx12/devices/MK22F51212/drivers/fsl_ftm.h + drivers/fsl_gpio.c 1 diff --git a/right/src/init_peripherials.c b/right/src/init_peripherials.c index bb995d4..18e4d33 100644 --- a/right/src/init_peripherials.c +++ b/right/src/init_peripherials.c @@ -5,6 +5,7 @@ #include "i2c.h" #include "led_driver.h" #include "merge_sensor.h" +#include "led_pwm.h" void InitI2c() { port_pin_config_t pinConfig = { @@ -49,4 +50,7 @@ void InitPeripherials(void) InitMergeSensor(); InitTestLed(); InitI2c(); +#if UHK_PCB_MAJOR_VERSION == 7 + LedPwm_Init(); +#endif } diff --git a/right/src/led_driver.c b/right/src/led_driver.c index f9f4403..a7159f1 100644 --- a/right/src/led_driver.c +++ b/right/src/led_driver.c @@ -14,13 +14,6 @@ void LedDriver_InitAllLeds(char isEnabled) GPIO_PinInit(LED_DRIVER_SDB_GPIO, LED_DRIVER_SDB_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 0}); GPIO_WritePinOutput(LED_DRIVER_SDB_GPIO, LED_DRIVER_SDB_PIN, 1); -#if UHK_PCB_MAJOR_VERSION == 7 - CLOCK_EnableClock(LED_DRIVER_PWM_CLOCK); - PORT_SetPinMux(LED_DRIVER_PWM_PORT, LED_DRIVER_PWM_PIN, kPORT_MuxAsGpio); - GPIO_PinInit(LED_DRIVER_PWM_GPIO, LED_DRIVER_PWM_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 0}); - GPIO_WritePinOutput(LED_DRIVER_PWM_GPIO, LED_DRIVER_PWM_PIN, 1); -#endif - LedDriver_SetAllLedsTo(isEnabled ? 0xFF : 0x00); } @@ -87,7 +80,6 @@ void LedDriver_SetAllLedsTo(uint8_t val) I2cWrite(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LED_DRIVER_LEFT, ledControlBufferLeft, sizeof(ledControlBufferLeft)); - for (i=FRAME_REGISTER_LED_CONTROL_FIRST; i<=FRAME_REGISTER_LED_CONTROL_LAST; i++) { LedDriver_WriteRegister(address, i, 0xff); } diff --git a/right/src/led_driver.h b/right/src/led_driver.h index aab8869..84e9db5 100644 --- a/right/src/led_driver.h +++ b/right/src/led_driver.h @@ -16,11 +16,6 @@ #define LED_DRIVER_SDB_CLOCK kCLOCK_PortA #define LED_DRIVER_SDB_PIN 2 - #define LED_DRIVER_PWM_PORT PORTD - #define LED_DRIVER_PWM_GPIO GPIOD - #define LED_DRIVER_PWM_CLOCK kCLOCK_PortD - #define LED_DRIVER_PWM_PIN 6 - #define LED_DRIVER_REGISTER_SHUTDOWN 0x0A #define LED_DRIVER_REGISTER_FRAME 0xFD diff --git a/right/src/led_pwm.c b/right/src/led_pwm.c new file mode 100644 index 0000000..d2d4c2e --- /dev/null +++ b/right/src/led_pwm.c @@ -0,0 +1,33 @@ +#include "led_pwm.h" +#include "fsl_port.h" + +void LedPwm_Init() { + ftm_config_t ftmInfo; + ftm_chnl_pwm_signal_param_t ftmParam[] = {{ + .chnlNumber = LED_PWM_FTM_CHANNEL, + .level = kFTM_LowTrue, + .dutyCyclePercent = 00, + .firstEdgeDelayPercent = 0 + }}; + + CLOCK_EnableClock(LED_PWM_CLOCK); + PORT_SetPinMux(LED_PWM_PORT, LED_PWM_PIN, kPORT_MuxAlt4); + + ftmParam[0].chnlNumber = (ftm_chnl_t)LED_PWM_FTM_CHANNEL; + ftmParam[0].level = kFTM_LowTrue; + ftmParam[0].dutyCyclePercent = INITIAL_DUTY_CYCLE_PERCENT - 100; + ftmParam[0].firstEdgeDelayPercent = 0; + FTM_GetDefaultConfig(&ftmInfo); + + // Initializes the FTM module. + FTM_Init(LED_PWM_FTM_BASEADDR, &ftmInfo); + FTM_SetupPwm(LED_PWM_FTM_BASEADDR, ftmParam, sizeof(ftmParam), + kFTM_EdgeAlignedPwm, FTM_PWM_FREQUENCY, FTM_SOURCE_CLOCK); + FTM_StartTimer(LED_PWM_FTM_BASEADDR, kFTM_SystemClock); +} + +void LedPwm_SetBrightness(uint8_t brightnessPercent) +{ + FTM_UpdatePwmDutycycle(LED_PWM_FTM_BASEADDR, LED_PWM_FTM_CHANNEL, kFTM_EdgeAlignedPwm, brightnessPercent-100); + FTM_SetSoftwareTrigger(LED_PWM_FTM_BASEADDR, true); // Triggers register update. +} diff --git a/right/src/led_pwm.h b/right/src/led_pwm.h new file mode 100644 index 0000000..838b782 --- /dev/null +++ b/right/src/led_pwm.h @@ -0,0 +1,27 @@ +#ifndef __LED_PWM_H__ +#define __LED_PWM_H__ + +// Includes: + + #include "fsl_ftm.h" + +// Macros: + + #define LED_PWM_PORT PORTD + #define LED_PWM_CLOCK kCLOCK_PortD + #define LED_PWM_PIN 6 + + #define LED_PWM_FTM_BASEADDR FTM0 + #define LED_PWM_FTM_CHANNEL kFTM_Chnl_6 + + #define FTM_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_BusClk) + #define FTM_PWM_FREQUENCY 24000 + + #define INITIAL_DUTY_CYCLE_PERCENT 100 + +// Functions: + + extern void LedPwm_Init(); + void LedPwm_SetBrightness(uint8_t brightnessPercent); + +#endif diff --git a/right/src/usb_protocol_handler.c b/right/src/usb_protocol_handler.c index fe4ea34..374ffba 100644 --- a/right/src/usb_protocol_handler.c +++ b/right/src/usb_protocol_handler.c @@ -6,6 +6,7 @@ #include "merge_sensor.h" #include "deserialize.h" #include "config_buffer.h" +#include "led_pwm.h" void setError(uint8_t error); void setGenericError(); @@ -20,6 +21,7 @@ void readEeprom(); void readMergeSensor(); void uploadConfig(); void applyConfig(); +void setLedPwm(); // Functions for setting error statuses @@ -75,6 +77,9 @@ void usbProtocolHandler() case USB_COMMAND_APPLY_CONFIG: applyConfig(); break; + case USB_COMMAND_SET_LED_PWM: + setLedPwm(); + break; default: break; } @@ -199,3 +204,11 @@ void applyConfig() { deserialize_Layer(ConfigBuffer, 0); } + +void setLedPwm() +{ + uint8_t brightnessPercent = GenericHidInBuffer[1]; +#if UHK_PCB_MAJOR_VERSION == 7 + LedPwm_SetBrightness(brightnessPercent); +#endif +} diff --git a/right/src/usb_protocol_handler.h b/right/src/usb_protocol_handler.h index 0b82deb..75829f1 100644 --- a/right/src/usb_protocol_handler.h +++ b/right/src/usb_protocol_handler.h @@ -28,6 +28,7 @@ #define USB_COMMAND_UPLOAD_CONFIG 8 #define UPLOAD_CONFIG_INVALID_PAYLOAD_SIZE 1 #define USB_COMMAND_APPLY_CONFIG 9 + #define USB_COMMAND_SET_LED_PWM 10 // Functions: