fix bug in switch statement refactor, fix serial_write

master
Firmware Batman 2017-04-18 01:12:04 -07:00
parent efca3f7093
commit 1b3d3c9b3e
5 changed files with 125 additions and 115 deletions

View File

@ -527,6 +527,7 @@ int usb_cb_control_msg(USB_Setup_TypeDef *setup, uint8_t *resp) {
while (resp_len < min(setup->b.wLength.w, MAX_RESP_LEN) && getc(ur, &resp[resp_len])) {
++resp_len;
}
break;
case 0xe1: // uart set baud rate
ur = get_ring_by_number(setup->b.wValue.w);
uart_set_baud(ur->uart, setup->b.wIndex.w);
@ -587,26 +588,44 @@ void ADC_IRQHandler(void) {
#ifdef ENABLE_SPI
#define SPI_BUF_SIZE 128
#define SPI_BUF_SIZE 256
uint8_t spi_buf[SPI_BUF_SIZE];
int spi_buf_count = 0;
uint8_t spi_tx_buf[0x10];
int spi_total_count = 0;
uint8_t spi_tx_buf[0x40];
void handle_spi(uint8_t *data, int len) {
memset(spi_tx_buf, 0xaa, 0x10);
spi_tx_buf[0] = 1;
spi_tx_buf[1] = 2;
spi_tx_buf[2] = 3;
spi_tx_buf[3] = 4;
spi_tx_buf[4] = 5;
spi_tx_buf[5] = 6;
spi_tx_dma(spi_tx_buf, 0x10);
// last thing
hexdump(data, len);
}
void SPI1_IRQHandler(void) {
// status is 0x43
if (SPI1->SR & SPI_SR_RXNE) {
uint8_t dat = SPI1->DR;
spi_buf[spi_buf_count] = dat;
if (spi_buf_count < SPI_BUF_SIZE-1) {
spi_buf_count += 1;
if (spi_total_count == 0) {
spi_total_count = dat;
} else {
spi_buf[spi_buf_count] = dat;
if (spi_buf_count < SPI_BUF_SIZE-1) {
spi_buf_count += 1;
}
}
if (spi_buf_count == spi_total_count) {
handle_spi(spi_buf, spi_total_count);
}
}
/*if (SPI1->SR & SPI_SR_TXE) {
// all i send is U U U no matter what
//SPI1->DR = 'U';
}*/
int stat = SPI1->SR;
//if (stat & ((~SPI_SR_RXNE) & (~SPI_SR_TXE) & (~SPI_SR_BSY))) {
if (stat & ((~SPI_SR_RXNE) & (~SPI_SR_TXE) & (~SPI_SR_BSY))) {
@ -616,6 +635,22 @@ void SPI1_IRQHandler(void) {
}
}
// SPI RX
void DMA2_Stream2_IRQHandler(void) {
// ack
DMA2->LIFCR = DMA_LIFCR_CTCIF2;
handle_spi(spi_buf, 0x14);
/*if (spi_total_count == 0) {
spi_total_count = spi_buf[0];
spi_rx_dma(spi_buf, spi_total_count);
} else {
puts("dma rx\n");
hexdump(spi_buf, spi_total_count+1);
}*/
}
// SPI TX
void DMA2_Stream3_IRQHandler(void) {
// ack
DMA2->LIFCR = DMA_LIFCR_CTCIF3;
@ -629,22 +664,9 @@ void EXTI4_IRQHandler(void) {
int pr = EXTI->PR;
// SPI CS rising
if (pr & (1 << 4)) {
spi_total_count = 0;
spi_rx_dma(spi_buf, 0x14);
puts("exti4\n");
memset(spi_tx_buf, 0xaa, 0x10);
spi_tx_buf[0] = 1;
spi_tx_buf[1] = 2;
spi_tx_buf[2] = 3;
spi_tx_buf[3] = 4;
spi_tx_buf[4] = 5;
spi_tx_buf[5] = 6;
spi_tx_dma(spi_tx_buf, 0x10);
/*if (pop(&can_rx_q, spi_tx_buf)) {
spi_tx_dma(spi_tx_buf, 0x10);
} else {
memset(spi_tx_buf, 0, 0x10);
spi_tx_dma(spi_tx_buf, 0x10);
}*/
//EXTI->IMR &= ~(1 << 4);
}
EXTI->PR = pr;
}
@ -693,10 +715,6 @@ int main() {
#ifdef ENABLE_SPI
spi_init();
// set up DMA
//memset(spi_tx_buf, 0, 0x10);
//spi_tx_dma(spi_tx_buf, 0x10);
#endif
// timer for fan PWM
@ -743,8 +761,9 @@ int main() {
#endif
#ifdef ENABLE_SPI
NVIC_EnableIRQ(DMA2_Stream2_IRQn);
NVIC_EnableIRQ(DMA2_Stream3_IRQn);
NVIC_EnableIRQ(SPI1_IRQn);
//NVIC_EnableIRQ(SPI1_IRQn);
// setup interrupt on falling edge of SPI enable (on PA4)
SYSCFG->EXTICR[2] = SYSCFG_EXTICR2_EXTI4_PA;
@ -785,10 +804,10 @@ int main() {
set_led(LED_BLUE, 0);
#ifdef ENABLE_SPI
if (spi_buf_count > 0) {
/*if (spi_buf_count > 0) {
hexdump(spi_buf, spi_buf_count);
spi_buf_count = 0;
}
}*/
#endif
// started logic

View File

@ -24,3 +24,23 @@ void spi_tx_dma(void *addr, int len) {
SPI1->CR2 |= SPI_CR2_TXDMAEN;
}
void spi_rx_dma(void *addr, int len) {
// disable DMA
SPI1->CR2 &= ~SPI_CR2_RXDMAEN;
DMA2_Stream2->CR &= ~DMA_SxCR_EN;
// drain the bus
uint8_t dat = SPI1->DR;
// DMA2, stream 2, channel 3
DMA2_Stream2->M0AR = addr;
DMA2_Stream2->NDTR = len;
DMA2_Stream2->PAR = &(SPI1->DR);
// channel3, increment memory, periph -> memory, enable
DMA2_Stream2->CR = DMA_SxCR_CHSEL_1 | DMA_SxCR_CHSEL_0 | DMA_SxCR_MINC | DMA_SxCR_EN;
DMA2_Stream2->CR |= DMA_SxCR_TCIE;
SPI1->CR2 |= SPI_CR2_RXDMAEN;
}

View File

@ -26,58 +26,6 @@ struct espconn tcp_conn;
// TCP specific protocol structure.
esp_tcp tcp_proto;
uint8_t buf[0x40*0x10];
void ICACHE_FLASH_ATTR some_timerfunc(void *arg)
{
// uart testing
/*uart0_sendStr("hello uart0\n");
uart1_sendStr_no_wait("hello uart1\n");*/
//uart0_sendStr("hello uart0\n");
if (GPIO_REG_READ(GPIO_OUT_ADDRESS) & (1 << pin)) {
// set gpio low
gpio_output_set(0, (1 << pin), 0, 0);
} else {
// set gpio high
gpio_output_set((1 << pin), 0, 0, 0);
}
// *** SPI MODE ***
uint32_t value = 0xD3D4D5D6;
uint32_t sendData[8] = {0};
SpiData spiData;
int i = 0;
int first = 1;
while (i < 0x40) {
spiData.cmd = 2;
spiData.cmdLen = 0;
spiData.addr = NULL;
spiData.addrLen = 0;
spiData.data = sendData;
spiData.dataLen = 16;
// manual CS pin
gpio_output_set(0, (1 << 5), 0, 0);
SPIMasterRecvData(SpiNum_HSPI, &spiData);
gpio_output_set((1 << 5), 0, 0, 0);
if (sendData[0] != 0) {
memcpy(buf + i*0x10, sendData, 0x10);
i++;
} else {
if (first == 0) break;
}
first = 0;
}
if (i != 0) {
espconn_send(&tcp_conn, buf, i*0x10);
}
}
static void ICACHE_FLASH_ATTR tcp_rx_cb(void *arg, char *data, uint16_t len) {
if (GPIO_REG_READ(GPIO_OUT_ADDRESS) & (1 << pin)) {
// set gpio low
@ -87,8 +35,11 @@ static void ICACHE_FLASH_ATTR tcp_rx_cb(void *arg, char *data, uint16_t len) {
gpio_output_set((1 << pin), 0, 0, 0);
}
// nothing too big
if (len > 0x14) return;
uint32_t value = 0xD3D4D5D6;
uint32_t sendData[8] = {0};
uint32_t sendData[0x40] = {0};
SpiData spiData;
@ -99,36 +50,22 @@ static void ICACHE_FLASH_ATTR tcp_rx_cb(void *arg, char *data, uint16_t len) {
// manual CS pin
gpio_output_set(0, (1 << 5), 0, 0);
memcpy(sendData, data, len);
memcpy(((void*)sendData), data, len);
spiData.data = sendData;
spiData.dataLen = len;
spiData.dataLen = 0x14;
SPIMasterSendData(SpiNum_HSPI, &spiData);
spiData.data = sendData;
spiData.dataLen = 16;
spiData.dataLen = 0x44;
SPIMasterRecvData(SpiNum_HSPI, &spiData);
gpio_output_set((1 << 5), 0, 0, 0);
espconn_send(&tcp_conn, sendData, 0x10);
espconn_send(&tcp_conn, sendData, 0x40);
}
int did_start_timer = 0;
void ICACHE_FLASH_ATTR tcp_connect_cb(void *arg) {
struct espconn *conn = (struct espconn *)arg;
espconn_set_opt(&tcp_conn, ESPCONN_NODELAY);
/*char message[] = "hello\r\n";
uint16_t len = strlen(message);
espconn_send (&tcp_conn, message, len);*/
/*if (!did_start_timer) {
// not atomic!
did_start_timer = 1;
// setup timer (100ms, repeating)
os_timer_setfn(&some_timer, (os_timer_func_t *)some_timerfunc, NULL);
os_timer_arm(&some_timer, 50, 1);
}*/
espconn_regist_recvcb(conn, tcp_rx_cb);
}

View File

@ -1,22 +1,54 @@
# python library to interface with panda
import struct
import socket
import usb1
from usb1 import USBErrorIO, USBErrorOverflow
# stupid tunneling of USB over wifi and SPI
class WifiHandle(object):
def __init__(self, ip="192.168.0.10", port=1337):
self.sock = socket.create_connection((ip, port))
def __recv(self):
ret = self.sock.recv(0x44)
from hexdump import hexdump
hexdump(ret)
return ret
def controlWrite(self, request_type, request, value, index, data, timeout=0):
# ignore data, panda doesn't use it
return self.controlRead(self, request_type, request, value, index, 0, timeout)
def controlRead(self, request_type, request, value, index, length, timeout=0):
self.sock.send(struct.pack("IBBHHH", 0, request_type, request, value, index, length))
return self.__recv()
def bulkWrite(self, endpoint, data, timeout=0):
assert len(data) <= 0x10
self.sock.send(struct.pack("I", endpoint)+data)
self.__recv() # to /dev/null
def bulkRead(self, endpoint, length, timeout=0):
self.sock.send(struct.pack("I", endpoint))
return self.__recv()
class Panda(object):
def __init__(self, serial=None, claim=True):
context = usb1.USBContext()
if serial == "WIFI":
self.handle = WifiHandle()
else:
context = usb1.USBContext()
self.handle = None
for device in context.getDeviceList(skip_on_error=True):
if device.getVendorID() == 0xbbaa and device.getProductID() == 0xddcc:
if serial is None or device.getSerialNumber() == serial:
print "opening device", device.getSerialNumber()
self.handle = device.open()
if claim:
self.handle.claimInterface(0)
break
self.handle = None
for device in context.getDeviceList(skip_on_error=True):
if device.getVendorID() == 0xbbaa and device.getProductID() == 0xddcc:
if serial is None or device.getSerialNumber() == serial:
print "opening device", device.getSerialNumber()
self.handle = device.open()
if claim:
self.handle.claimInterface(0)
break
assert self.handle != None
@ -27,6 +59,8 @@ class Panda(object):
for device in context.getDeviceList(skip_on_error=True):
if device.getVendorID() == 0xbbaa and device.getProductID() == 0xddcc:
ret.append(device.getSerialNumber())
# TODO: detect if this is real
#ret += ["WIFI"]
return ret
# ******************* health *******************
@ -87,7 +121,7 @@ class Panda(object):
def serial_read(self, port_number):
return self.handle.controlRead(usb1.TYPE_VENDOR | usb1.RECIPIENT_DEVICE, 0xe0, port_number, 0, 0x100)
def serial_write(self, port_number):
def serial_write(self, port_number, ln):
return self.handle.bulkWrite(2, chr(port_number) + ln)
# ******************* kline *******************

View File

@ -22,6 +22,6 @@ if __name__ == "__main__":
sys.stdout.flush()
if select.select([sys.stdin], [], [], 0) == ([sys.stdin], [], []):
ln = sys.stdin.readline()
panda.serial_write(port_number)
panda.serial_write(port_number, ln)
time.sleep(0.05)