Skip to content

Commit 9fcd430

Browse files
author
Wayne Ren
authored
Merge pull request #55 from IRISZZW/feature/iotdk_fix
device: flash: bug fixes in spi_flash_w25qxx driver
2 parents 1a77919 + a4fadf2 commit 9fcd430

File tree

11 files changed

+1014
-16
lines changed

11 files changed

+1014
-16
lines changed

board/iotdk/common/iotdk_init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161

6262
static void iotdk_mux_init(void)
6363
{
64-
pmode_mux_config(PMA_GPIO | PMB_SPI | PMC_GPIO);
64+
pmode_mux_config(PMA_GPIO | PMB_SPI | PMC_UART);
6565
arduino_pin_mux(ARDUINO_A4, ARDUINO_FUNC_2ND);
6666
}
6767

device/peripheral/flash/w25qxx/spi_flash_w25qxx.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "board.h"
3434
#include "dev_spi.h"
3535
#include "arc_exception.h"
36+
#include "string.h"
3637

3738
/**
3839
* \name W25QXX SPI Flash Commands
@@ -50,6 +51,7 @@
5051

5152
#define W25QXX_NOT_VALID (0xFFFFFFFF)
5253

54+
5355
/**
5456
* \brief spi flash spi send command to operate spi flash
5557
* \param[in] xfer spi transfer that need to transfer to spi device
@@ -132,6 +134,7 @@ int32_t w25qxx_write_enable(W25QXX_DEF_PTR dev)
132134
local_buf[1] = 0x00; // write status
133135
local_buf[2] = 0x00; // write status
134136

137+
135138
DEV_SPI_XFER_SET_TXBUF(&cmd_xfer, local_buf, 0, 3);
136139
DEV_SPI_XFER_SET_RXBUF(&cmd_xfer, NULL, 0, 0);
137140
DEV_SPI_XFER_SET_NEXT(&cmd_xfer, NULL);
@@ -312,10 +315,7 @@ int32_t w25qxx_erase(W25QXX_DEF_PTR dev, uint32_t address, uint32_t size)
312315
*/
313316
int32_t w25qxx_write(W25QXX_DEF_PTR dev, uint32_t address, uint32_t size, const void *data)
314317
{
315-
316-
uint8_t local_buf[4];
317318
DEV_SPI_TRANSFER cmd_xfer;
318-
DEV_SPI_TRANSFER data_xfer;
319319

320320
uint32_t first = 0;
321321
uint32_t size_orig = size;
@@ -337,18 +337,20 @@ int32_t w25qxx_write(W25QXX_DEF_PTR dev, uint32_t address, uint32_t size, const
337337

338338
first = first < size ? first : size;
339339

340-
local_buf[0] = PP;
341-
local_buf[1] = (address >> 16) & 0xff;
342-
local_buf[2] = (address >> 8) & 0xff;
343-
local_buf[3] = address & 0xff;
340+
dev->write_buf[0] = PP;
341+
dev->write_buf[1] = (address >> 16) & 0xff;
342+
dev->write_buf[2] = (address >> 8) & 0xff;
343+
dev->write_buf[3] = address & 0xff;
344344

345-
DEV_SPI_XFER_SET_TXBUF(&data_xfer, data, 0, first);
346-
DEV_SPI_XFER_SET_RXBUF(&data_xfer, NULL, 0, 0);
347-
DEV_SPI_XFER_SET_NEXT(&data_xfer, NULL);
345+
memcpy(&(dev->write_buf[4]), data, first);
348346

349-
DEV_SPI_XFER_SET_TXBUF(&cmd_xfer, local_buf, 0, 4);
347+
// DEV_SPI_XFER_SET_TXBUF(&data_xfer, data, 0, first);
348+
// DEV_SPI_XFER_SET_RXBUF(&data_xfer, NULL, 0, 0);
349+
// DEV_SPI_XFER_SET_NEXT(&data_xfer, NULL);
350+
351+
DEV_SPI_XFER_SET_TXBUF(&cmd_xfer, dev->write_buf, 0, 4+first);
350352
DEV_SPI_XFER_SET_RXBUF(&cmd_xfer, NULL, 0, 0);
351-
DEV_SPI_XFER_SET_NEXT(&cmd_xfer, &data_xfer);
353+
DEV_SPI_XFER_SET_NEXT(&cmd_xfer, NULL);
352354

353355
if (spi_send_cmd(dev, &cmd_xfer) != 0) {
354356
return -1;

device/peripheral/flash/w25qxx/spi_flash_w25qxx.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef struct w25qxx_t {
5555

5656
uint32_t page_sz;
5757
uint32_t sector_sz;
58+
uint8_t write_buf[4+FLASH_PAGE_SIZE];
5859
} W25QXX_DEF, * W25QXX_DEF_PTR;
5960

6061
#define W25QXX_DEF(name, spi_master_id, cs_line, page, sector) \

example/baremetal/test/iotdk/spiflash_test/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ int main(void)
4747
int32_t data_len;
4848
int32_t i;
4949

50-
w25qxx_init(w25q16dv, 1000000);
50+
w25qxx_init(w25q16dv, 100000);
5151

5252
id = w25qxx_read_id(w25q16dv);
5353
EMBARC_PRINTF("Device ID = %x\r\n",id);
5454

5555
for (i = 0; i < (DATA_BUF_SIZE / 4); i++) {
56-
data_buf[i] = i;
56+
data_buf[i] = i + (i<<8) + (i<<16) + (i<<24);
5757
}
5858

5959
if (w25qxx_erase(w25q16dv, SPI_FLASH_ADDRESS, 1024) < 0) {
@@ -73,7 +73,7 @@ int main(void)
7373
EMBARC_PRINTF("read %d bytes\r\n", data_len);
7474

7575
for (i = 0; i < (DATA_BUF_SIZE / 4); i += 4) {
76-
EMBARC_PRINTF("%x, %x, %x, %x\r\n",
76+
EMBARC_PRINTF("%08X, %08X, %08X, %08X\r\n",
7777
data_buf[i + 0], data_buf[i + 1],
7878
data_buf[i + 2], data_buf[i + 3]);
7979

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
FreeRTOS V8.0.0 - Copyright (C) 2014 Real Time Engineers Ltd.
3+
All rights reserved
4+
5+
VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6+
7+
***************************************************************************
8+
* *
9+
* FreeRTOS provides completely free yet professionally developed, *
10+
* robust, strictly quality controlled, supported, and cross *
11+
* platform software that has become a de facto standard. *
12+
* *
13+
* Help yourself get started quickly and support the FreeRTOS *
14+
* project by purchasing a FreeRTOS tutorial book, reference *
15+
* manual, or both from: http://www.FreeRTOS.org/Documentation *
16+
* *
17+
* Thank you! *
18+
* *
19+
***************************************************************************
20+
21+
This file is part of the FreeRTOS distribution.
22+
23+
FreeRTOS is free software; you can redistribute it and/or modify it under
24+
the terms of the GNU General Public License (version 2) as published by the
25+
Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
26+
27+
>>! NOTE: The modification to the GPL is included to allow you to distribute
28+
>>! a combined work that includes FreeRTOS without being obliged to provide
29+
>>! the source code for proprietary components outside of the FreeRTOS
30+
>>! kernel.
31+
32+
FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
33+
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
34+
FOR A PARTICULAR PURPOSE. Full license text is available from the following
35+
link: http://www.freertos.org/a00114.html
36+
37+
1 tab == 4 spaces!
38+
39+
***************************************************************************
40+
* *
41+
* Having a problem? Start by reading the FAQ "My application does *
42+
* not run, what could be wrong?" *
43+
* *
44+
* http://www.FreeRTOS.org/FAQHelp.html *
45+
* *
46+
***************************************************************************
47+
48+
http://www.FreeRTOS.org - Documentation, books, training, latest versions,
49+
license and Real Time Engineers Ltd. contact details.
50+
51+
http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
52+
including FreeRTOS+Trace - an indispensable productivity tool, a DOS
53+
compatible FAT file system, and our tiny thread aware UDP/IP stack.
54+
55+
http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
56+
Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
57+
licenses offer ticketed support, indemnification and middleware.
58+
59+
http://www.SafeRTOS.com - High Integrity Systems also provide a safety
60+
engineered and independently SIL3 certified version for use in safety and
61+
mission critical applications that require provable dependability.
62+
63+
1 tab == 4 spaces!
64+
*/
65+
66+
#ifndef FREERTOS_CONFIG_H
67+
#define FREERTOS_CONFIG_H
68+
69+
#include <stdio.h>
70+
/*-----------------------------------------------------------
71+
* Application specific definitions.
72+
*
73+
* These definitions should be adjusted for your particular hardware and
74+
* application requirements.
75+
*
76+
* THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE
77+
* FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE.
78+
*
79+
* See http://www.freertos.org/a00110.html.
80+
*----------------------------------------------------------*/
81+
82+
#define configUSE_PREEMPTION 1
83+
#define configUSE_IDLE_HOOK 0
84+
#define configUSE_TICK_HOOK 0
85+
#define configCPU_CLOCK_HZ ( ( unsigned long ) BOARD_CPU_CLOCK )
86+
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
87+
#define configMAX_PRIORITIES ( 10 )
88+
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 104 )
89+
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 16 * 1024 ) )
90+
#define configMAX_TASK_NAME_LEN ( 10 )
91+
#define configUSE_TRACE_FACILITY 0
92+
#define configUSE_16_BIT_TICKS 0
93+
#define configIDLE_SHOULD_YIELD 1
94+
#define configUSE_MUTEXES 1
95+
96+
/* Co-routine definitions. */
97+
#define configUSE_CO_ROUTINES 0
98+
#define configMAX_CO_ROUTINE_PRIORITIES ( 2 )
99+
100+
101+
/* Set the following definitions to 1 to include the API function, or zero
102+
to exclude the API function. */
103+
104+
#define INCLUDE_vTaskPrioritySet 1
105+
#define INCLUDE_uxTaskPriorityGet 1
106+
#define INCLUDE_vTaskDelete 1
107+
#define INCLUDE_vTaskCleanUpResources 0
108+
#define INCLUDE_vTaskSuspend 1
109+
#define INCLUDE_vTaskDelayUntil 1
110+
#define INCLUDE_vTaskDelay 1
111+
#define INCLUDE_xTaskGetCurrentTaskHandle 1
112+
113+
#endif /* FREERTOS_CONFIG_H */
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* ------------------------------------------
2+
* Copyright (c) 2018, Synopsys, Inc. All rights reserved.
3+
4+
* Redistribution and use in source and binary forms, with or without modification,
5+
* are permitted provided that the following conditions are met:
6+
7+
* 1) Redistributions of source code must retain the above copyright notice, this
8+
* list of conditions and the following disclaimer.
9+
10+
* 2) Redistributions in binary form must reproduce the above copyright notice,
11+
* this list of conditions and the following disclaimer in the documentation and/or
12+
* other materials provided with the distribution.
13+
14+
* 3) Neither the name of the Synopsys, Inc., nor the names of its contributors may
15+
* be used to endorse or promote products derived from this software without
16+
* specific prior written permission.
17+
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20+
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22+
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23+
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24+
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25+
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28+
*
29+
--------------------------------------------- */
30+
31+
#include "embARC_error.h"
32+
#include "string.h"
33+
#include "board.h"
34+
#include "at_parser.h"
35+
//#include "embARC_error.h"
36+
37+
#define DBG_MORE
38+
#include "embARC_debug.h"
39+
40+
/* if \r\n is needed to be attached at the end of AT comand, define AT_ADD_POSTFIX
41+
* otherwise comment out this line
42+
*/
43+
#define AT_ADD_POSTFIX
44+
45+
#define AT_PREFIX "AT"
46+
#define AT_POSTFIX "\r\n"
47+
48+
#define AT_TIMENOW() OSP_GET_CUR_MS()
49+
50+
#define AT_MAX_LEN 128
51+
#define AT_MAX_PARAMETER 8
52+
53+
54+
int32_t at_parser_init(AT_PARSER_DEF_PTR obj, uint32_t baudrate){
55+
obj->psio = ez_sio_open(obj->uart_id, baudrate, AT_TX_BUFSIZE, AT_RX_BUFSIZE);
56+
dbg_printf(DBG_MORE_INFO, "[%s]%d: obj->psio 0x%x -> 0x%x\r\n", __FUNCTION__, __LINE__, obj->psio, *obj->psio);
57+
return (obj->psio != NULL) ? AT_OK : AT_ERROR;
58+
}
59+
60+
void at_parser_deinit(AT_PARSER_DEF_PTR obj){
61+
ez_sio_close(obj->psio);
62+
return;
63+
}
64+
65+
int32_t at_read(AT_PARSER_DEF_PTR obj, char *buf, uint32_t cnt)
66+
{
67+
return ez_sio_read(obj->psio, buf, cnt);
68+
}
69+
70+
int32_t at_write(AT_PARSER_DEF_PTR obj, char *buf, uint32_t cnt)
71+
{
72+
return ez_sio_write(obj->psio, buf, cnt);
73+
}
74+
75+
/*
76+
* please use NULL as last parameter
77+
*/
78+
int32_t at_send_cmd(AT_PARSER_DEF_PTR obj, AT_MODE mode, AT_STRING command, ...){
79+
va_list vl;
80+
char at_out[AT_MAX_LEN] = AT_PREFIX;
81+
char * str = command;
82+
if(str == NULL){
83+
dbg_printf(DBG_MORE_INFO, "[%s]%d: command is NULL, send AT test command\r\n", __FUNCTION__, __LINE__);
84+
} else {
85+
strcat(at_out,"+");
86+
strcat(at_out, command);
87+
switch(mode){
88+
case AT_LIST:
89+
strcat(at_out, "=?");
90+
break;
91+
case AT_READ:
92+
strcat(at_out, "?");
93+
break;
94+
case AT_WRITE:
95+
strcat(at_out, "=");
96+
va_start(vl, command);
97+
for(int i = 0; i < AT_MAX_PARAMETER; i++){
98+
str = va_arg(vl, AT_STRING);
99+
if(str == NULL){
100+
break;
101+
}
102+
if(i != 0){
103+
strcat(at_out, ",");
104+
}
105+
strcat(at_out, str);
106+
}
107+
va_end(vl);
108+
break;
109+
case AT_EXECUTE:
110+
default:
111+
break;
112+
}
113+
}
114+
#ifdef AT_ADD_POSTFIX
115+
strcat(at_out, AT_POSTFIX);
116+
#endif /*AT_ADD_POSTFIX*/
117+
dbg_printf(DBG_LESS_INFO, "[%s]%d: at_out: \"%s\" (%d)\r\n", __FUNCTION__, __LINE__, at_out, strlen(at_out));
118+
return at_write(obj, at_out, strlen(at_out));
119+
}
120+
121+
/* make sure the buf is large enough*/
122+
int32_t at_get_reply(AT_PARSER_DEF_PTR obj, char *buf, uint32_t timeout)
123+
{
124+
uint32_t cur_ofs = 0;
125+
uint32_t read_cnt;
126+
uint32_t cur_time;
127+
cur_time = AT_TIMENOW();
128+
do {
129+
read_cnt = at_read(obj, &buf[cur_ofs], 1);
130+
cur_ofs += read_cnt;
131+
buf[cur_ofs] = '\0';
132+
if ((strstr(buf, AT_OK_STR)!= NULL) || (strstr(buf, AT_ERROR_STR)!= NULL)){
133+
break;
134+
}
135+
} while((AT_TIMENOW()-cur_time) < timeout);
136+
buf[cur_ofs] = '\0';
137+
dbg_printf(DBG_LESS_INFO, "[%s]%d: \"%s\" (%d)\r\n", __FUNCTION__, __LINE__, buf, strlen(buf));
138+
if (strstr(buf, AT_OK_STR)!= NULL){
139+
return AT_OK;
140+
}
141+
return AT_ERROR;
142+
}
143+
144+
int32_t at_test(AT_PARSER_DEF_PTR obj){
145+
char rcv_buf[64];
146+
at_send_cmd(obj, AT_LIST, NULL);
147+
return at_get_reply(obj, rcv_buf, AT_NORMAL_TIMEOUT);
148+
}
149+

0 commit comments

Comments
 (0)