diff --git a/left/build/kds/.project b/left/build/kds/.project index dbebd5c..ad31601 100644 --- a/left/build/kds/.project +++ b/left/build/kds/.project @@ -106,6 +106,16 @@ 1 PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_smc.h + + drivers/fsl_spi.c + 1 + PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_spi.c + + + drivers/fsl_spi.h + 1 + PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_spi.h + drivers/fsl_tpm.c 1 diff --git a/left/src/config.h b/left/src/config.h new file mode 100644 index 0000000..fb65ff0 --- /dev/null +++ b/left/src/config.h @@ -0,0 +1,8 @@ +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +// Macros: + + #define DEBUG_OVER_SPI + +#endif diff --git a/left/src/debug_over_spi.c b/left/src/debug_over_spi.c new file mode 100644 index 0000000..b009a22 --- /dev/null +++ b/left/src/debug_over_spi.c @@ -0,0 +1,61 @@ +#include "debug_over_spi.h" +#include "config.h" +#include "fsl_gpio.h" + +#ifdef DEBUG_OVER_SPI + +#define EXAMPLE_SPI_MASTER (SPI0) +#define EXAMPLE_SPI_MASTER_SOURCE_CLOCK (kCLOCK_BusClk) + +#define BUFFER_SIZE (64) +static uint8_t srcBuff[BUFFER_SIZE]; + +static spi_transfer_t xfer = {0}; +static spi_master_config_t userConfig; +spi_master_handle_t handle; + +static volatile bool masterFinished = false; + +#endif + +static void masterCallback(SPI_Type *base, spi_master_handle_t *masterHandle, status_t status, void *userData) +{ + masterFinished = true; +} + +void DebugOverSpi_Init(void) +{ +#ifdef DEBUG_OVER_SPI + + CLOCK_EnableClock(DEBUG_OVER_SPI_MOSI_CLOCK); + CLOCK_EnableClock(DEBUG_OVER_SPI_SCK_CLOCK); + + PORT_SetPinMux(DEBUG_OVER_SPI_MOSI_PORT, DEBUG_OVER_SPI_MOSI_PIN, kPORT_MuxAlt3); + PORT_SetPinMux(DEBUG_OVER_SPI_SCK_PORT, DEBUG_OVER_SPI_SCK_PIN, kPORT_MuxAlt3); + + GPIO_PinInit(DEBUG_OVER_SPI_MOSI_GPIO, DEBUG_OVER_SPI_MOSI_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 0}); + GPIO_PinInit(DEBUG_OVER_SPI_SCK_GPIO, DEBUG_OVER_SPI_SCK_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 0}); + + GPIO_SetPinsOutput(DEBUG_OVER_SPI_MOSI_GPIO, 1U << DEBUG_OVER_SPI_MOSI_PIN); + GPIO_SetPinsOutput(DEBUG_OVER_SPI_SCK_GPIO, 1U << DEBUG_OVER_SPI_SCK_PIN); + + SPI_MasterGetDefaultConfig(&userConfig); + uint32_t srcFreq = CLOCK_GetFreq(EXAMPLE_SPI_MASTER_SOURCE_CLOCK); + SPI_MasterInit(EXAMPLE_SPI_MASTER, &userConfig, srcFreq); + + SPI_MasterTransferCreateHandle(EXAMPLE_SPI_MASTER, &handle, masterCallback, NULL); +#endif +} + +void DebugOverSpi_Send(uint8_t *tx, uint8_t len) +{ +#ifdef DEBUG_OVER_SPI +// if (masterFinished) { + masterFinished = false; + memcpy(srcBuff, tx, MIN(BUFFER_SIZE, len)); + xfer.txData = srcBuff; + xfer.dataSize = len; + SPI_MasterTransferNonBlocking(EXAMPLE_SPI_MASTER, &handle, &xfer); +// } +#endif +} diff --git a/left/src/debug_over_spi.h b/left/src/debug_over_spi.h new file mode 100644 index 0000000..a97d8ae --- /dev/null +++ b/left/src/debug_over_spi.h @@ -0,0 +1,27 @@ +#ifndef __DEBUG_OVER_SPI_H__ +#define __DEBUG_OVER_SPI_H__ + +// Includes: + + #include "fsl_common.h" + #include "fsl_port.h" + #include "fsl_spi.h" + +// Macros: + + #define DEBUG_OVER_SPI_MOSI_PORT PORTA + #define DEBUG_OVER_SPI_MOSI_GPIO GPIOA + #define DEBUG_OVER_SPI_MOSI_CLOCK kCLOCK_PortA + #define DEBUG_OVER_SPI_MOSI_PIN 7 + + #define DEBUG_OVER_SPI_SCK_PORT PORTB + #define DEBUG_OVER_SPI_SCK_GPIO GPIOB + #define DEBUG_OVER_SPI_SCK_CLOCK kCLOCK_PortB + #define DEBUG_OVER_SPI_SCK_PIN 0 + +// Functions: + + void DebugOverSpi_Init(void); + void DebugOverSpi_Send(uint8_t *tx, uint8_t len); + +#endif diff --git a/left/src/init_peripherals.c b/left/src/init_peripherals.c index f4026b3..302bb55 100644 --- a/left/src/init_peripherals.c +++ b/left/src/init_peripherals.c @@ -9,6 +9,7 @@ #include "led_pwm.h" #include "slave_protocol_handler.h" #include "i2c_watchdog.h" +#include "debug_over_spi.h" i2c_slave_config_t slaveConfig; i2c_slave_handle_t slaveHandle; @@ -17,8 +18,12 @@ uint8_t byteIn; uint8_t rxMessagePos; i2c_slave_transfer_event_t prevEvent; +uint8_t dosBuffer[60]; + static void i2cSlaveCallback(I2C_Type *base, i2c_slave_transfer_t *xfer, void *userData) { + dosBuffer[0] = xfer->event; + DebugOverSpi_Send(dosBuffer, 1); switch (xfer->event) { case kI2C_SlaveTransmitEvent: SlaveTxHandler(); @@ -54,9 +59,11 @@ void InitInterruptPriorities(void) { NVIC_SetPriority(I2C0_IRQn, 1); NVIC_SetPriority(TPM1_IRQn, 1); + NVIC_SetPriority(SPI0_IRQn, 1); } -void InitI2c(void) { +void InitI2c(void) +{ port_pin_config_t pinConfig = { .pullSelect = kPORT_PullUp, }; @@ -76,7 +83,8 @@ void InitI2c(void) { I2C_SlaveTransferNonBlocking(I2C_BUS_BASEADDR, &slaveHandle, kI2C_SlaveCompletionEvent); } -void InitLedDriver(void) { +void InitLedDriver(void) +{ CLOCK_EnableClock(LED_DRIVER_SDB_CLOCK); PORT_SetPinMux(LED_DRIVER_SDB_PORT, LED_DRIVER_SDB_PIN, kPORT_MuxAsGpio); GPIO_PinInit(LED_DRIVER_SDB_GPIO, LED_DRIVER_SDB_PIN, &(gpio_pin_config_t){kGPIO_DigitalOutput, 0}); @@ -89,6 +97,7 @@ void InitPeripherals(void) InitLedDriver(); InitTestLed(); LedPwm_Init(); - InitI2c(); // InitI2cWatchdog(); + DebugOverSpi_Init(); + InitI2c(); } diff --git a/left/src/main.c b/left/src/main.c index 05ca250..623793f 100644 --- a/left/src/main.c +++ b/left/src/main.c @@ -3,6 +3,7 @@ #include "init_peripherals.h" #include "bootloader.h" #include +#include "config.h" DEFINE_BOOTLOADER_CONFIG_AREA(I2C_ADDRESS_LEFT_KEYBOARD_HALF_BOOTLOADER) @@ -13,7 +14,11 @@ key_matrix_t keyMatrix = { {PORTB, GPIOB, kCLOCK_PortB, 11}, {PORTA, GPIOA, kCLOCK_PortA, 6}, {PORTA, GPIOA, kCLOCK_PortA, 8}, +#ifdef DEBUG_OVER_SPI + {PORTA, GPIOA, kCLOCK_PortA, 8}, +#else {PORTB, GPIOB, kCLOCK_PortB, 0}, +#endif {PORTB, GPIOB, kCLOCK_PortB, 6}, {PORTA, GPIOA, kCLOCK_PortA, 3}, {PORTA, GPIOA, kCLOCK_PortA, 12} @@ -22,7 +27,11 @@ key_matrix_t keyMatrix = { {PORTB, GPIOB, kCLOCK_PortB, 7}, {PORTB, GPIOB, kCLOCK_PortB, 10}, {PORTA, GPIOA, kCLOCK_PortA, 5}, +#ifdef DEBUG_OVER_SPI + {PORTA, GPIOA, kCLOCK_PortA, 5}, +#else {PORTA, GPIOA, kCLOCK_PortA, 7}, +#endif {PORTA, GPIOA, kCLOCK_PortA, 4} } };