Add debug_over_spi.[ch]

This commit is contained in:
László Monda
2017-10-08 03:38:43 +02:00
parent 302a750621
commit 6b0ebb1385
6 changed files with 127 additions and 3 deletions

View File

@@ -106,6 +106,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_spi.c</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_spi.c</locationURI>
</link>
<link>
<name>drivers/fsl_spi.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/lib/KSDK_2.0_MKL03Z8xxx4/devices/MKL03Z4/drivers/fsl_spi.h</locationURI>
</link>
<link>
<name>drivers/fsl_tpm.c</name>
<type>1</type>

8
left/src/config.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef __CONFIG_H__
#define __CONFIG_H__
// Macros:
#define DEBUG_OVER_SPI
#endif

61
left/src/debug_over_spi.c Normal file
View File

@@ -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
}

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

@@ -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

View File

@@ -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();
}

View File

@@ -3,6 +3,7 @@
#include "init_peripherals.h"
#include "bootloader.h"
#include <stdio.h>
#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}
}
};