use two buffer to save spi data

This commit is contained in:
Roy
2021-07-28 09:46:20 +08:00
parent 2d851c2f7c
commit b0aee65916
2 changed files with 42 additions and 28 deletions
@@ -37,14 +37,14 @@ static uint8_t notify_value[250] = {0};
p += len; \
} while (0)
#define central_mem_init() \
do { \
master_tx_buffer[0] = MSM_REG_WRITE; \
master_tx_buffer[1] = 0b01000011; \
central_mem_select(1); \
central_spi_send(2); \
central_mem_select(0); \
central_spi_send(2); \
#define central_mem_init() \
do { \
master_tx_buffer[0] = MSM_REG_WRITE; \
master_tx_buffer[1] = 0b01000011; \
central_mem_select(1); \
central_spi_send(master_tx_buffer, 2); \
central_mem_select(0); \
central_spi_send(master_tx_buffer, 2); \
} while (0)
@@ -72,25 +72,39 @@ void master_switch_memory();
//static void central_handle_notify(uint8_t notify_length, uint8_t *notify_value){
static uint8_t notify_counter = 0;
uint8_t spi_buffer0[256];
uint8_t spi_buffer1[256];
static void central_handle_notify(){
uint8_t counter = not_counter ++;
static uint8_t spi_buffer_index = 0;
uint8_t *p;
// localize current buffer
uint8_t *p = master_tx_buffer;
if(spi_buffer_index){
spi_buffer_index = ~spi_buffer_index;
p = spi_buffer0;
}
else{
spi_buffer_index = ~spi_buffer_index;
p = spi_buffer1;
}
uint8_t counter = not_counter ++;
// update offset
uint32_t cnt_offset = not_offset;
not_offset = cnt_offset + 3 + notify_length;
*p++ = MEM_INS_WRITE; // instruction
*p++ = (uint8_t)((cnt_offset >> 8) & 0xFF); // address
*p++ = (uint8_t)(cnt_offset & 0xFF); // address
*p++ = 0xFF; // data header
*p++ = counter; // data counter
*p++ = notify_length; // data content length
unsafe_memcpy(p, notify_value, notify_length); // data content
p[0] = MEM_INS_WRITE; // instruction
p[1] = (uint8_t)((cnt_offset >> 8) & 0xFF); // address
p[2] = (uint8_t)(cnt_offset & 0xFF); // address
p[3] = 0xFF; // data header
p[4] = counter; // data counter
p[5] = notify_length; // data content length
memcpy(p + 6, notify_value, notify_length); // data content
central_spi_send(p - master_tx_buffer);
central_spi_send(p, notify_length + 6);
notify_handle_done = true;
if (ram_sw){
@@ -110,15 +124,15 @@ void master_switch_memory() {
// localize current buffer
uint8_t *p = master_tx_buffer;
*p++ = MEM_INS_WRITE; // instruction
*p++ = 0; // address
*p++ = 0; // address
*p++ = (uint8_t)((cnt_offset >> 8) & 0xFF); // data: notify data length
*p++ = (uint8_t)(cnt_offset & 0xFF); // data: notify data length
*p++ = 0;
*p++ = 0;
p[0] = MEM_INS_WRITE; // instruction
p[1] = 0; // address
p[2] = 0; // address
p[3] = (uint8_t)((cnt_offset >> 8) & 0xFF); // data: notify data length
p[4] = (uint8_t)(cnt_offset & 0xFF); // data: notify data length
p[5] = 0;
p[6] = 0;
central_spi_send(p - master_tx_buffer);
central_spi_send(p, 7);
// switch memory
mem_sel++;
@@ -45,9 +45,9 @@ static void SPI_reopen(){
central_spi_handle = SPI_open(Board_SPI0, &spi_parameter);
}
#define central_spi_send(len) \
#define central_spi_send(data, len) \
do { \
central_spi_transaction.txBuf = master_tx_buffer; \
central_spi_transaction.txBuf = data; \
central_spi_transaction.rxBuf = NULL; \
central_spi_transaction.count = (len); \
central_pin_output(Board_SPI_CS, 0); \