Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement serprog protocol SPI speed for flashrom #124

Merged
merged 2 commits into from
Oct 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions src/hydrabus/hydrabus_mode_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ static const char* str_bsp_init_err= { "bsp_spi_init() error %d\r\n" };

#define MODE_DEV_NB_ARGC ((int)ARRAY_SIZE(mode_dev_arg)) /* Number of arguments/parameters for this mode */

#define SPEED_NB (8)
static uint32_t speeds[2][SPEED_NB] = {
const uint32_t spi_speeds[2][SPI_SPEED_NB] = {
/* SPI1 */
{
320000,
Expand Down Expand Up @@ -91,14 +90,14 @@ static void show_params(t_hydra_console *con)
"floating",
proto->config.spi.dev_mode == DEV_MASTER ? "master" : "slave");

print_freq(con, speeds[proto->dev_num][proto->config.spi.dev_speed]);
print_freq(con, spi_speeds[proto->dev_num][proto->config.spi.dev_speed]);
cprintf(con, " (");
for (i = 0, cnt = 0; i < SPEED_NB; i++) {
for (i = 0, cnt = 0; i < SPI_SPEED_NB; i++) {
if (proto->config.spi.dev_speed == i)
continue;
if (cnt++)
cprintf(con, ", ");
print_freq(con, speeds[proto->dev_num][i]);
print_freq(con, spi_speeds[proto->dev_num][i]);
}
cprintf(con, ")\r\n");
cprintf(con, "Polarity: %d\r\nPhase: %d\r\nBit order: %s first\r\n",
Expand Down Expand Up @@ -187,8 +186,8 @@ static int exec(t_hydra_console *con, t_tokenline_parsed *p, int token_pos)
case T_FREQUENCY:
t += 2;
memcpy(&arg_float, p->buf + p->tokens[t], sizeof(float));
for (i = 0; i < SPEED_NB; i++) {
if (arg_float == speeds[proto->dev_num][i]) {
for (i = 0; i < SPI_SPEED_NB; i++) {
if (arg_float == spi_speeds[proto->dev_num][i]) {
proto->config.spi.dev_speed = i;
break;
}
Expand Down
4 changes: 3 additions & 1 deletion src/hydrabus/hydrabus_mode_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ enum {
SPI_LSB_FIRST,
};

#endif /* _HYDRABUS_MODE_SPI_H_ */
#define SPI_SPEED_NB (8)
extern const uint32_t spi_speeds[2][SPI_SPEED_NB];

#endif /* _HYDRABUS_MODE_SPI_H_ */
30 changes: 29 additions & 1 deletion src/hydrabus/hydrabus_serprog.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "hydrabus_bbio.h"
#include "hydrabus_serprog.h"
#include "hydrabus_mode_spi.h"
#include "bsp_spi.h"

void bbio_serprog_init_proto_default(t_hydra_console *con)
Expand Down Expand Up @@ -148,8 +149,35 @@ void bbio_mode_serprog(t_hydra_console *con)
break;
case S_CMD_S_SPI_FREQ:
chnRead(con->sdu, rx_data, 4);
//TODO: define variable speed.
/* requested speed */
to_rx = ((uint32_t)rx_data[3] << 24) | \
((uint32_t)rx_data[2] << 16) | \
((uint32_t)rx_data[1] << 8) | rx_data[0];
/* value 0 is reserved and should not be requested as speed */
if (to_rx == 0) {
cprint(con, S_NAK, 1);
break;
}
/* find available SPI speed <= request speed */
i = 0;
while (i < SPI_SPEED_NB && spi_speeds[proto->dev_num][i] <= to_rx) {
i++;
}
i = i == 0 ? 0 : i - 1;
/* configure selected speed */
proto->config.spi.dev_speed = i;
if (bsp_spi_init(proto->dev_num, proto) != BSP_OK) {
cprint(con, S_NAK, 1);
break;
}
/* send back selected speed */
to_tx = spi_speeds[proto->dev_num][i];
tx_data[0] = (uint8_t)((to_tx >> 0 ) & 0xff);
tx_data[1] = (uint8_t)((to_tx >> 8 ) & 0xff);
tx_data[2] = (uint8_t)((to_tx >> 16) & 0xff);
tx_data[3] = (uint8_t)((to_tx >> 24) & 0xff);
cprint(con, S_ACK, 1);
cprint(con, (char *)tx_data, 4);
break;
case S_CMD_S_PIN_STATE:
chnRead(con->sdu, rx_data, 1);
Expand Down