Permalink
Browse files

- added 2 SPI sniffers

- added SPI macro's for setting cpol and cpha
  • Loading branch information...
smdprutser committed May 7, 2018
1 parent af3094f commit 67fce21635cc7b0fd7cb0247bbc927457f783fb6
Showing with 183 additions and 2 deletions.
  1. +51 −1 source/HWSPI.c
  2. +1 −1 source/Makefile
  3. +128 −0 source/sniffer.c
  4. +3 −0 source/sniffer.h
View
@@ -8,6 +8,7 @@
#include "cdcacm.h"
#include "UI.h"
#include "LA.h"
#include "sniffer.h"
static uint32_t cpol, cpha, br, dff, lsbfirst, csidle, od;
@@ -110,7 +111,50 @@ void HWSPI_macro(uint32_t macro)
{
switch(macro)
{
case 0: cdcprintf("No macros available");
case 0: cdcprintf("Available macros\r\n");
cdcprintf(" 1. EXTI SPI sniffer\r\n";
cdcprintf(" 2. HW SPI sniffer\r\n";
cdcprintf(" 11. cpol=0\r\n";
cdcprintf(" 12. cpol=1\r\n";
cdcprintf(" 13. cpha=0\r\n";
cdcprintf(" 14. cpha=1\r\n";
break;
case 1: cdcprintf("Sniff settings cpol=%d, cpha=%d, csidle=%d\r\n", (cpol>>1), cpha, csidle);
cdcprintf("press any key to exit\r\n");
HWSPI_cleanup();
sniffSPI((cpol>>1), cpha, csidle);
HWSPI_setup_exc();
break;
case 2: cdcprintf("Sniff settings cpol=%d, cpha=%d, csidle=%d\r\n", (cpol>>1), cpha, csidle);
cdcprintf("press any key to exit\r\n");
HWSPI_cleanup();
rcc_periph_clock_enable(BP_SPI_CLK);
spi_reset(BP_SPI);
if(cpol) spi_set_clock_polarity_1(BP_SPI);
else spi_set_clock_polarity_0(BP_SPI);
if(cpha) spi_set_clock_phase_1(BP_SPI);
else spi_set_clock_phase_0(BP_SPI);
// spi_set_standard_mode(BP_SPI, 0);
spi_enable(BP_SPI);
spi_set_slave_mode(BP_SPI);
spi_enable_software_slave_management(BP_SPI);
while(!cdcbyteready())
{
if(SPI_SR(BP_SPI)&SPI_SR_RXNE)
{
cdcputc((gpio_get(BP_SPI_CS_PORT, BP_SPI_CS_PIN)?'-':'_'));
cdcprintf("0x%02X", spi_read(BP_SPI));
}
}
if(modeConfig.init) HWSPI_setup_exc();
break;
case 11: spi_set_clock_polarity_0(BP_SPI);
break;
case 12: spi_set_clock_polarity_1(BP_SPI);
break;
case 13: spi_set_clock_phase_0(BP_SPI);
break;
case 14: spi_set_clock_phase_1(BP_SPI);
break;
default: cdcprintf("Macro not defined");
modeConfig.error=1;
@@ -381,3 +425,9 @@ uint16_t HWSPI_xfer(uint16_t d)
return (uint16_t) spi_xfer(BP_SPI, (uint16_t)d);
}
View
@@ -23,7 +23,7 @@ CFLAGS +=-DFWVER=\"$(GIT_VERSION)\"
OBJS += debug.o cdcacm.o UI.o protocols.o dummy1.o dummy2.o HiZ.o HWSPI.o
OBJS += AUXpin.o ADC.o HWUSART.o bpflash.o HWI2C.o LA.o SW2W.o SW3W.o sump.o
OBJS += DIO.o LCDSPI.o HD44780.o ST7735.o 1WIRE.o
OBJS += DIO.o LCDSPI.o HD44780.o ST7735.o 1WIRE.o sniffer.o
BINARY = buspirateNG
View
@@ -0,0 +1,128 @@
#include <stdint.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/exti.h>
#include <libopencm3/cm3/nvic.h>
#include "buspirateNG.h"
#include "LA.h"
#include "cdcacm.h"
#include "UI.h"
#include "sniffer.h"
static volatile uint8_t armed;
static volatile uint8_t csidle;
static uint8_t miso, mosi, clock;
void sniffSPI(uint8_t cpol, uint8_t cpha, uint8_t cs)
{
if(cpol==0)
{
if(cpha==0)
exti_set_trigger(EXTI13, EXTI_TRIGGER_RISING);
else
exti_set_trigger(EXTI13, EXTI_TRIGGER_FALLING);
}
else
{
if(cpha==0)
exti_set_trigger(EXTI13, EXTI_TRIGGER_FALLING);
else
exti_set_trigger(EXTI13, EXTI_TRIGGER_RISING);
}
if(cs)
{
exti_set_trigger(EXTI12, EXTI_TRIGGER_FALLING);
}
else
{
exti_set_trigger(EXTI12, EXTI_TRIGGER_RISING);
}
csidle=cs;
miso=0;
mosi=0;
armed=0;
exti_select_source(EXTI12, GPIOB);
exti_select_source(EXTI13, GPIOB);
exti_enable_request(EXTI12);
exti_enable_request(EXTI13);
nvic_set_priority(NVIC_EXTI15_10_IRQ, 0);
nvic_enable_irq(NVIC_EXTI15_10_IRQ);
cdcgetc();
nvic_disable_irq(NVIC_EXTI15_10_IRQ);
exti_disable_request(EXTI12);
exti_disable_request(EXTI13);
}
//
void exti15_10_isr(void)
{
if(exti_get_flag_status(EXTI10)) // not used
{
exti_reset_request(EXTI10);
}
if(exti_get_flag_status(EXTI11)) // not used
{
exti_reset_request(EXTI11);
}
if(exti_get_flag_status(EXTI12)) // CS
{
if(armed)
{
if(csidle)
exti_set_trigger(EXTI12, EXTI_TRIGGER_FALLING);
else
exti_set_trigger(EXTI12, EXTI_TRIGGER_RISING);
armed=0;
cdcputc(']');
}
else
{
if(csidle)
exti_set_trigger(EXTI12, EXTI_TRIGGER_RISING);
else
exti_set_trigger(EXTI12, EXTI_TRIGGER_FALLING);
armed=1;
cdcputc('[');
}
cdcputc(' ');
exti_reset_request(EXTI12);
}
if(exti_get_flag_status(EXTI13)) // CLOCK/SCL
{
if(armed)
{
miso<<=1;
mosi<<=1;
if(gpio_get(GPIOB, GPIO14))
miso|=1;
if(gpio_get(GPIOB, GPIO15))
mosi|=1;
clock++;
if(clock==8)
{
clock=0;
cdcprintf(">0x%02X <0x%02X ", mosi, miso);
miso=0;
mosi=0;
}
}
exti_reset_request(EXTI13);
}
if(exti_get_flag_status(EXTI14)) // MISO/-
{
exti_reset_request(EXTI14);
}
if(exti_get_flag_status(EXTI15)) // MOSI/SDA
{
exti_reset_request(EXTI15);
}
}
View
@@ -0,0 +1,3 @@
void sniffSPI(uint8_t cpol, uint8_t cpha, uint8_t cs);

0 comments on commit 67fce21

Please sign in to comment.