Drive the LED_PWM line of the left half via TPM which doesn't work yet for some reason. Add protocol command for setting the LED_PWM brightness of the left half. Expose UpdateUsbReports()

This commit is contained in:
László Monda
2017-01-13 21:43:47 +01:00
parent 0ded50b43a
commit 9ef9e5f734
10 changed files with 89 additions and 8 deletions

View File

@@ -96,6 +96,16 @@
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_smc.h</locationURI>
</link>
<link>
<name>drivers/fsl_tpm.c</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_tpm.c</locationURI>
</link>
<link>
<name>drivers/fsl_tpm.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_tpm.h</locationURI>
</link>
<link>
<name>startup/startup_MKL03Z4.S</name>
<type>1</type>

View File

@@ -3,6 +3,7 @@
#include "main.h"
#include "i2c_addresses.h"
#include "i2c.h"
#include "led_pwm.h"
void SetError(uint8_t error);
void SetGenericError();
@@ -31,10 +32,16 @@ void BridgeProtocolHandler()
BridgeTxSize = KEYBOARD_MATRIX_COLS_NUM*KEYBOARD_MATRIX_ROWS_NUM;
memcpy(BridgeTxBuffer, keyMatrix.keyStates, BridgeTxSize);
break;
case BRIDGE_COMMAND_SET_LED:
case BRIDGE_COMMAND_SET_TEST_LED:
TEST_LED_OFF();
BridgeTxSize = 0;
TEST_LED_SET(BridgeRxBuffer[1]);
break;
case BRIDGE_COMMAND_SET_LED_PWM:
BridgeTxSize = 0;
uint8_t brightnessPercent = BridgeRxBuffer[1];
LedPwm_SetBrightness(brightnessPercent);
TEST_LED_SET(brightnessPercent == 0);
break;
}
}

View File

@@ -14,7 +14,8 @@
#define PROTOCOL_RESPONSE_GENERIC_ERROR 1
#define BRIDGE_COMMAND_GET_KEY_STATES 0
#define BRIDGE_COMMAND_SET_LED 1
#define BRIDGE_COMMAND_SET_TEST_LED 1
#define BRIDGE_COMMAND_SET_LED_PWM 2
// Variables:

View File

@@ -6,6 +6,7 @@
#include "fsl_i2c.h"
#include "fsl_clock.h"
#include "i2c.h"
#include "led_pwm.h"
void InitI2c() {
port_pin_config_t pinConfig = {
@@ -41,5 +42,6 @@ void InitPeripherials(void)
{
InitLedDriver();
InitTestLed();
LedPwm_Init();
InitI2c();
}

31
left/src/led_pwm.c Normal file
View File

@@ -0,0 +1,31 @@
#include "led_pwm.h"
#include "fsl_port.h"
void LedPwm_Init() {
tpm_config_t tpmInfo;
tpm_chnl_pwm_signal_param_t tpmParam[1];/* = {{
.chnlNumber = LED_PWM_TPM_CHANNEL,
.level = kTPM_LowTrue,
.dutyCyclePercent = 00,
}};*/
CLOCK_EnableClock(LED_PWM_CLOCK);
PORT_SetPinMux(LED_PWM_PORT, LED_PWM_PIN, kPORT_MuxAlt2);
tpmParam[0].chnlNumber = LED_PWM_TPM_CHANNEL;
tpmParam[0].level = kTPM_LowTrue;
tpmParam[0].dutyCyclePercent = 100 - INITIAL_DUTY_CYCLE_PERCENT;
TPM_GetDefaultConfig(&tpmInfo);
TPM_Init(LED_PWM_TPM_BASEADDR, &tpmInfo);
TPM_SetupPwm(LED_PWM_TPM_BASEADDR, tpmParam, sizeof(tpmParam),
kTPM_EdgeAlignedPwm, TPM_PWM_FREQUENCY, TPM_SOURCE_CLOCK);
TPM_StartTimer(LED_PWM_TPM_BASEADDR, kTPM_SystemClock);
// LedPwm_SetBrightness(INITIAL_DUTY_CYCLE_PERCENT);
}
void LedPwm_SetBrightness(uint8_t brightnessPercent)
{
TPM_UpdatePwmDutycycle(LED_PWM_TPM_BASEADDR, LED_PWM_TPM_CHANNEL,
kTPM_EdgeAlignedPwm, 100-brightnessPercent);
}

27
left/src/led_pwm.h Normal file
View File

@@ -0,0 +1,27 @@
#ifndef __LED_PWM_H__
#define __LED_PWM_H__
// Includes:
#include "fsl_tpm.h"
// Macros:
#define LED_PWM_PORT PORTB
#define LED_PWM_CLOCK kCLOCK_PortB
#define LED_PWM_PIN 5
#define LED_PWM_TPM_BASEADDR TPM1
#define LED_PWM_TPM_CHANNEL kTPM_Chnl_1
#define TPM_SOURCE_CLOCK CLOCK_GetFreq(kCLOCK_BusClk)
#define TPM_PWM_FREQUENCY 24000U
#define INITIAL_DUTY_CYCLE_PERCENT 100U
// Functions:
extern void LedPwm_Init();
void LedPwm_SetBrightness(uint8_t brightnessPercent);
#endif

View File

@@ -15,7 +15,7 @@ void LedPwm_Init() {
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].dutyCyclePercent = 100 - INITIAL_DUTY_CYCLE_PERCENT;
ftmParam[0].firstEdgeDelayPercent = 0;
FTM_GetDefaultConfig(&ftmInfo);
@@ -28,6 +28,6 @@ void LedPwm_Init() {
void LedPwm_SetBrightness(uint8_t brightnessPercent)
{
FTM_UpdatePwmDutycycle(LED_PWM_FTM_BASEADDR, LED_PWM_FTM_CHANNEL, kFTM_EdgeAlignedPwm, brightnessPercent-100);
FTM_UpdatePwmDutycycle(LED_PWM_FTM_BASEADDR, LED_PWM_FTM_CHANNEL, kFTM_EdgeAlignedPwm, 100 - brightnessPercent);
FTM_SetSoftwareTrigger(LED_PWM_FTM_BASEADDR, true); // Triggers register update.
}

View File

@@ -59,7 +59,7 @@ static const uint8_t testData[] =
0x0a, 0x02, 0x2d, 0x02, 0x1d, 0x02, 0x1b, 0x02, 0x06, 0x02, 0x19, 0x02, 0x05, 0x02, 0x04, 0x02, 0x05, 0x02, 0x06,
0x02, 0x07, 0x02, 0x08, 0x02, 0x09, 0x02, 0x02, 0x00 };
void updateUsbReports()
void UpdateUsbReports()
{
ResetActiveUsbKeyboardReport();
@@ -82,13 +82,13 @@ void main() {
InitClock();
LedDriver_InitAllLeds(1);
KeyMatrix_Init(&KeyMatrix);
updateUsbReports();
UpdateUsbReports();
InitUsb();
// deserialize_Layer(testData, 0);
while (1) {
updateUsbReports();
UpdateUsbReports();
asm("wfi");
}
}

View File

@@ -19,5 +19,6 @@
extern key_matrix_t KeyMatrix;
extern uint8_t PreviousKeyStates[SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE];
extern uint8_t CurrentKeyStates[SLOT_COUNT][MAX_KEY_COUNT_PER_MODULE];
extern void UpdateUsbReports();
#endif

View File

@@ -207,8 +207,10 @@ void applyConfig()
void setLedPwm()
{
uint8_t brightnessPercent = GenericHidInBuffer[1];
#if UHK_PCB_MAJOR_VERSION == 7
uint8_t brightnessPercent = GenericHidInBuffer[1];
LedPwm_SetBrightness(brightnessPercent);
uint8_t data[] = {2, brightnessPercent};
I2cWrite(I2C_MAIN_BUS_BASEADDR, I2C_ADDRESS_LEFT_KEYBOARD_HALF, data, sizeof(data));
#endif
}