Skip to content

Commit fa805b3

Browse files
committed
steami: Add config zone commands (WRITE/READ/CLEAR_CONFIG).
1 parent 270b2f1 commit fa805b3

5 files changed

Lines changed: 184 additions & 5 deletions

File tree

source/board/steami/steami32.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ typedef enum {
112112

113113
TASK_READ_SECTOR,
114114

115+
TASK_WRITE_CONFIG,
116+
TASK_READ_CONFIG,
117+
TASK_CLEAR_CONFIG,
118+
115119
TASK_WAIT_FLASH_BUSY,
116120
} steami_task;
117121

@@ -205,6 +209,40 @@ static void on_I2C_receive_command(steami_i2c_command cmd, uint8_t* rx, uint16_t
205209
break;
206210
}
207211

212+
case WRITE_CONFIG:{
213+
if(is_busy()){
214+
steami_uart_write_string("ERROR I2C busy.\n");
215+
break;
216+
}
217+
if(rx_len < 3){
218+
error_status_bad_parameter(&status_error);
219+
break;
220+
}
221+
current_task = TASK_WRITE_CONFIG;
222+
memcpy(task_rx, rx, rx_len);
223+
task_rx_len = rx_len;
224+
break;
225+
}
226+
227+
case READ_CONFIG:{
228+
if(is_busy()){
229+
steami_uart_write_string("ERROR I2C busy.\n");
230+
break;
231+
}
232+
current_task = TASK_READ_CONFIG;
233+
memcpy(task_rx, rx, rx_len);
234+
task_rx_len = rx_len;
235+
break;
236+
}
237+
238+
case CLEAR_CONFIG:
239+
if(is_busy()){
240+
steami_uart_write_string("ERROR I2C busy.\n");
241+
break;
242+
}
243+
current_task = TASK_CLEAR_CONFIG;
244+
break;
245+
208246
case STATUS:{
209247
uint8_t status = 0x00;
210248

@@ -394,6 +432,65 @@ void process_task()
394432
}
395433

396434

435+
case TASK_WRITE_CONFIG:{
436+
if( steami_flash_is_busy() ){
437+
break;
438+
}
439+
// task_rx: [offset_hi, offset_lo, len, data...]
440+
if( task_rx_len >= 3 ){
441+
uint16_t offset = ((uint16_t)task_rx[0] << 8) | task_rx[1];
442+
uint16_t len = task_rx[2];
443+
if( len <= task_rx_len - 3 && steami_flash_write_config(offset, task_rx + 3, len) ){
444+
steami_uart_write_string("Config written.\n");
445+
}
446+
else{
447+
error_status_set_last_command_fail(&status_error);
448+
steami_uart_write_string("ERROR Unable to write config.\n");
449+
}
450+
}
451+
else{
452+
error_status_bad_parameter(&status_error);
453+
}
454+
current_task = TASK_WAIT_FLASH_BUSY;
455+
break;
456+
}
457+
458+
case TASK_READ_CONFIG:{
459+
if( steami_flash_is_busy() ){
460+
break;
461+
}
462+
if( task_rx_len == 2 ){
463+
uint16_t offset = ((uint16_t)task_rx[0] << 8) | task_rx[1];
464+
if( steami_flash_read_config(offset, buffer_sector, STEAMI_FLASH_SECTOR) ){
465+
steami_i2c_set_tx_data(buffer_sector, STEAMI_FLASH_SECTOR);
466+
}
467+
else{
468+
error_status_set_last_command_fail(&status_error);
469+
steami_uart_write_string("ERROR Unable to read config.\n");
470+
}
471+
}
472+
else{
473+
error_status_bad_parameter(&status_error);
474+
}
475+
current_task = TASK_WAIT_FLASH_BUSY;
476+
break;
477+
}
478+
479+
case TASK_CLEAR_CONFIG:{
480+
if( steami_flash_is_busy() ){
481+
break;
482+
}
483+
if( steami_flash_erase_config() ){
484+
steami_uart_write_string("Config erased.\n");
485+
}
486+
else{
487+
error_status_set_last_command_fail(&status_error);
488+
steami_uart_write_string("ERROR Unable to erase config.\n");
489+
}
490+
current_task = TASK_WAIT_FLASH_BUSY;
491+
break;
492+
}
493+
397494
case TASK_WAIT_FLASH_BUSY:
398495
if( !steami_flash_is_busy() ){
399496
current_task = TASK_NONE;

source/board/steami/steami_flash.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,41 @@ uint16_t steami_flash_read_file(uint8_t* data, uint16_t data_len, uint32_t offse
219219

220220
bool steami_flash_is_busy(){
221221
return w25q64_is_busy();
222+
}
223+
224+
bool steami_flash_erase_config(){
225+
steami_led_turn_on_blue();
226+
227+
w25q64_write_enable();
228+
wait_w25q64_wel();
229+
wait_w25q64_busy();
230+
bool result = w25q64_sector_erase(STEAMI_FLASH_CONFIG_ADDR);
231+
232+
steami_led_turn_off_blue();
233+
return result;
234+
}
235+
236+
bool steami_flash_write_config(uint16_t offset, uint8_t* data, uint16_t len){
237+
if( offset + len > STEAMI_FLASH_CONFIG_SIZE ){
238+
return false;
239+
}
240+
241+
steami_led_turn_on_blue();
242+
243+
w25q64_write_enable();
244+
wait_w25q64_wel();
245+
wait_w25q64_busy();
246+
bool result = w25q64_page_program(data, STEAMI_FLASH_CONFIG_ADDR + offset, len);
247+
248+
steami_led_turn_off_blue();
249+
return result;
250+
}
251+
252+
bool steami_flash_read_config(uint16_t offset, uint8_t* data, uint16_t len){
253+
if( offset + len > STEAMI_FLASH_CONFIG_SIZE ){
254+
return false;
255+
}
256+
257+
wait_w25q64_busy();
258+
return w25q64_read_data(data, STEAMI_FLASH_CONFIG_ADDR + offset, len);
222259
}

source/board/steami/steami_flash.h

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
#include <stdint.h>
44
#include <stdbool.h>
55

6-
#define STEAMI_FLASH_FILE_ADDR (uint32_t)0x00000000
7-
#define STEAMI_FLASH_NAME_ADDR (uint32_t)0x007FF000
6+
#define STEAMI_FLASH_FILE_ADDR (uint32_t)0x00000000
7+
#define STEAMI_FLASH_CONFIG_ADDR (uint32_t)0x007FE000
8+
#define STEAMI_FLASH_NAME_ADDR (uint32_t)0x007FF000
89

9-
#define STEAMI_FLASH_FILE_SIZE 8384512
10+
#define STEAMI_FLASH_FILE_SIZE 8380416 /* 0x7FE000 - 0x000000 */
11+
#define STEAMI_FLASH_CONFIG_SIZE 4096
1012

1113
#define STEAMI_FLASH_SECTOR 256
1214
#define STEAMI_FLASH_4K 4096
@@ -108,4 +110,31 @@ uint16_t steami_flash_read_file(uint8_t* data, uint16_t data_len, uint32_t offse
108110
*
109111
* @return TRUE if the flash is buzy flag is high, FALSE otherwise
110112
*/
111-
bool steami_flash_is_busy();
113+
bool steami_flash_is_busy();
114+
115+
/**
116+
* @brief Erase the 4K config zone at STEAMI_FLASH_CONFIG_ADDR.
117+
*
118+
* @return TRUE if successful, FALSE otherwise
119+
*/
120+
bool steami_flash_erase_config();
121+
122+
/**
123+
* @brief Write data to the config zone.
124+
*
125+
* @param offset byte offset within the config zone (0-4095)
126+
* @param data data array
127+
* @param len number of bytes to write (max 256 per call, must stay within one page)
128+
* @return TRUE if successful, FALSE otherwise
129+
*/
130+
bool steami_flash_write_config(uint16_t offset, uint8_t* data, uint16_t len);
131+
132+
/**
133+
* @brief Read data from the config zone.
134+
*
135+
* @param offset byte offset within the config zone (0-4095)
136+
* @param data buffer for read data
137+
* @param len number of bytes to read
138+
* @return TRUE if successful, FALSE otherwise
139+
*/
140+
bool steami_flash_read_config(uint16_t offset, uint8_t* data, uint16_t len);

source/board/steami/steami_i2c.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ static bool is_command_valid(uint8_t cmd){
3535
case GET_FILENAME:
3636
case WRITE_DATA:
3737
case READ_SECTOR:
38+
case WRITE_CONFIG:
39+
case READ_CONFIG:
40+
case CLEAR_CONFIG:
3841
case STATUS:
3942
case ERROR_STATUS:
4043
return true;
@@ -65,6 +68,15 @@ static uint16_t get_argument_byte_number(uint8_t cmd){
6568
case READ_SECTOR:
6669
return 2;
6770

71+
case WRITE_CONFIG:
72+
return 31;
73+
74+
case READ_CONFIG:
75+
return 2;
76+
77+
case CLEAR_CONFIG:
78+
return 0;
79+
6880
case STATUS:
6981
return 0;
7082

source/board/steami/steami_i2c.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ typedef enum {
2626
WRITE_DATA = 0x11,
2727

2828
READ_SECTOR = 0x20,
29-
29+
30+
WRITE_CONFIG = 0x30,
31+
READ_CONFIG = 0x31,
32+
CLEAR_CONFIG = 0x32,
33+
3034
STATUS = 0x80,
3135
ERROR_STATUS = 0x81,
3236
} steami_i2c_command;

0 commit comments

Comments
 (0)