Skip to content

Commit 8f39aa4

Browse files
committed
Refactored rx & tx buffers
1 parent 0172402 commit 8f39aa4

File tree

1 file changed

+19
-31
lines changed

1 file changed

+19
-31
lines changed

firmware/main.c

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#include <stddef.h>
3131
#include <stdbool.h>
3232

33-
#define NP_USB_BUF_SIZE 64
33+
#define NP_PACKET_BUF_SIZE 64
3434
#define NP_MAX_PAGE_SIZE 0x0800
3535
#define NP_WRITE_ACK_BYTES 1984
3636
#define NP_NAND_TIMEOUT 0x1000000
@@ -169,14 +169,6 @@ typedef struct
169169
typedef struct
170170
{
171171
uint8_t *rx_buf;
172-
uint8_t rx_buf_size;
173-
uint8_t *tx_buf;
174-
uint8_t tx_buf_size;
175-
} np_usb_t;
176-
177-
typedef struct
178-
{
179-
np_usb_t usb;
180172
uint32_t addr;
181173
uint32_t len;
182174
int addr_is_valid;
@@ -196,19 +188,15 @@ typedef struct
196188

197189
static np_comm_cb_t *np_comm_cb;
198190

199-
uint8_t np_usb_send_buf[NP_USB_BUF_SIZE];
191+
uint8_t np_packet_send_buf[NP_PACKET_BUF_SIZE];
200192

201-
static void np_usb_init(np_usb_t *usb)
193+
static void np_usb_init()
202194
{
203195
Set_System();
204196
Set_USBClock();
205197
USB_Interrupts_Config();
206198
USB_Init();
207-
while (!USB_IsDeviceConfigured());
208-
209-
usb->tx_buf = (uint8_t *)np_usb_send_buf;
210-
usb->tx_buf_size = NP_USB_BUF_SIZE;
211-
usb->rx_buf_size = NP_USB_BUF_SIZE;
199+
while (!USB_IsDeviceConfigured());
212200
}
213201

214202
static int np_send_ok_status()
@@ -254,7 +242,7 @@ static int np_cmd_nand_read_id(np_prog_t *prog)
254242

255243
DEBUG_PRINT("Read ID command\r\n");
256244

257-
if (prog->usb.tx_buf_size < resp_len)
245+
if (NP_PACKET_BUF_SIZE < resp_len)
258246
{
259247
ERROR_PRINT("Response size is more then TX buffer size\r\n");
260248
return np_send_error(NP_ERR_BUF_OVERFLOW);
@@ -301,7 +289,7 @@ static int np_nand_erase(uint32_t page, uint32_t addr)
301289
static int np_cmd_nand_erase(np_prog_t *prog)
302290
{
303291
uint32_t addr, page, pages_in_block;
304-
np_erase_cmd_t *erase_cmd = (np_erase_cmd_t *)prog->usb.rx_buf;
292+
np_erase_cmd_t *erase_cmd = (np_erase_cmd_t *)prog->rx_buf;
305293

306294
led_wr_set(true);
307295

@@ -353,7 +341,7 @@ static int np_send_write_ack(uint32_t bytes_ack)
353341
static int np_cmd_nand_write_start(np_prog_t *prog)
354342
{
355343
np_write_start_cmd_t *write_start_cmd =
356-
(np_write_start_cmd_t *)prog->usb.rx_buf;
344+
(np_write_start_cmd_t *)prog->rx_buf;
357345

358346
DEBUG_PRINT("Write at 0x%lx command\r\n", write_start_cmd->addr);
359347

@@ -436,10 +424,10 @@ static int np_cmd_nand_write_data(np_prog_t *prog)
436424
{
437425
uint32_t write_len, bytes_left;
438426
np_write_data_cmd_t *write_data_cmd =
439-
(np_write_data_cmd_t *)prog->usb.rx_buf;
427+
(np_write_data_cmd_t *)prog->rx_buf;
440428

441429
if (write_data_cmd->len + offsetof(np_write_data_cmd_t, data) >
442-
prog->usb.rx_buf_size)
430+
NP_PACKET_BUF_SIZE)
443431
{
444432
ERROR_PRINT("Data size is wrong %d\r\n", write_data_cmd->len);
445433
return np_send_error(NP_ERR_CMD_DATA_SIZE);
@@ -514,7 +502,7 @@ static int np_cmd_nand_write_end(np_prog_t *prog)
514502

515503
static int np_cmd_nand_write(np_prog_t *prog)
516504
{
517-
np_cmd_t *cmd = (np_cmd_t *)prog->usb.rx_buf;
505+
np_cmd_t *cmd = (np_cmd_t *)prog->rx_buf;
518506
int ret = 0;
519507

520508
switch (cmd->code)
@@ -570,9 +558,9 @@ static int np_cmd_nand_read(np_prog_t *prog)
570558
static np_page_t page;
571559
uint32_t write_len;
572560
uint32_t resp_header_size = offsetof(np_resp_t, data);
573-
uint32_t tx_data_len = prog->usb.tx_buf_size - resp_header_size;
574-
np_read_cmd_t *read_cmd = (np_read_cmd_t *)prog->usb.rx_buf;
575-
np_resp_t *resp = (np_resp_t *)prog->usb.tx_buf;
561+
uint32_t tx_data_len = sizeof(np_packet_send_buf) - resp_header_size;
562+
np_read_cmd_t *read_cmd = (np_read_cmd_t *)prog->rx_buf;
563+
np_resp_t *resp = (np_resp_t *)np_packet_send_buf;
576564

577565
led_rd_set(true);
578566

@@ -612,7 +600,7 @@ static int np_cmd_nand_read(np_prog_t *prog)
612600
while (!np_comm_cb->send_ready());
613601

614602
resp->info = write_len;
615-
if (np_comm_cb->send(prog->usb.tx_buf,
603+
if (np_comm_cb->send(np_packet_send_buf,
616604
resp_header_size + write_len))
617605
{
618606
return -1;
@@ -643,7 +631,7 @@ static int np_cmd_nand_read(np_prog_t *prog)
643631

644632
static int np_cmd_nand_select(np_prog_t *prog)
645633
{
646-
np_select_cmd_t *select_cmd = (np_select_cmd_t *)prog->usb.rx_buf;
634+
np_select_cmd_t *select_cmd = (np_select_cmd_t *)prog->rx_buf;
647635

648636
DEBUG_PRINT("Chip select ID %lu command\r\n", select_cmd->chip_num);
649637

@@ -751,7 +739,7 @@ static bool np_cmd_is_valid(int cmd)
751739

752740
static int np_cmd_handler(np_prog_t *prog)
753741
{
754-
np_cmd_t *cmd = (np_cmd_t *)prog->usb.rx_buf;
742+
np_cmd_t *cmd = (np_cmd_t *)prog->rx_buf;
755743

756744
if (!prog->chip_info && cmd->code != NP_CMD_NAND_SELECT)
757745
{
@@ -775,9 +763,9 @@ static void np_packet_handler(np_prog_t *prog)
775763
{
776764
do
777765
{
778-
np_comm_cb->peek(&prog->usb.rx_buf);
766+
np_comm_cb->peek(&prog->rx_buf);
779767

780-
if (!prog->usb.rx_buf)
768+
if (!prog->rx_buf)
781769
break;
782770

783771
np_cmd_handler(prog);
@@ -825,7 +813,7 @@ int main()
825813
printf("done.\r\n");
826814

827815
printf("USB init...");
828-
np_usb_init(&prog.usb);
816+
np_usb_init();
829817
printf("done.\r\n");
830818

831819
printf("CDC init...");

0 commit comments

Comments
 (0)