Browse Source

blmc setpwm communication

master
Olaf Rempel 12 years ago
parent
commit
c4227fbabb
5 changed files with 93 additions and 38 deletions
  1. +30
    -2
      include/at91_twi.h
  2. +7
    -3
      main.c
  3. +3
    -3
      src/at91_adc.c
  4. +1
    -1
      src/at91_pitc.c
  5. +52
    -29
      src/at91_twi.c

+ 30
- 2
include/at91_twi.h View File

@@ -25,10 +25,38 @@
//#define CMD_GET_INFO 0x10
#define CMD_SET_PWM 0x21
#define CMD_GET_STATUS 0x22
//#define CMD_SET_PARAM 0x23
//#define CMD_GET_PARAM 0x24
#define CMD_SET_PARAM 0x23
#define CMD_GET_PARAM 0x24
#define CMD_BOOT_LOADER 0x2F

struct blmc_status {
uint8_t pwm_ist;
uint8_t pwm_soll;

uint16_t rpm;
uint16_t current;
uint16_t voltage;
} __attribute__ ((packed));

struct blmc_param {
uint16_t spinup_ticks;

uint8_t spinup_tick;
uint8_t spinup_step;

uint8_t spinup_wait;
uint8_t spinup_pwm;

uint8_t pwm_min;
uint8_t pwm_max;

uint16_t current_limit;
uint16_t current_max;

uint16_t voltage_min;

uint16_t crc16;
} __attribute__ ((packed));

struct blmc_cmd {
uint32_t cmd; /* cmd byte(s) */


+ 7
- 3
main.c View File

@@ -49,6 +49,10 @@ static uint32_t pitc_test(struct pitc_timer *timer)
printf("\r");
*/
static uint8_t data[4] = { 0x0f, 0x0f, 0x0f, 0x0f };
twi_setpwm(data);
return PITC_RESTART_TIMER;
}
@@ -77,9 +81,6 @@ int main(void)
at91_rttc_test_init();
at91_tc1_init();
/* adc, need timer */
at91_adc_test_init();
/* twi */
at91_twi_init();
at91_twi_test();
@@ -89,6 +90,9 @@ int main(void)
printf("static alloc: %5ld bytes\n\r", static_alloc_used());
/* adc, need timer */
// at91_adc_test_init();
pitc_schedule_timer(&pitc_test_timer);
while (1);


+ 3
- 3
src/at91_adc.c View File

@@ -42,7 +42,7 @@ static uint32_t adc_trigger(struct pitc_timer *timer)
{
uint32_t i;
for (i = 0; i < ARRAY_SIZE(adc_result); i++)
printf("0x%03x ", adc_result[i]);
printf("%+4d ", 0x200 - adc_result[i]);

printf("\n\r");

@@ -51,7 +51,7 @@ static uint32_t adc_trigger(struct pitc_timer *timer)
}

static struct pitc_timer adc_timer = {
.interval = 10,
.interval = 100,
.func = &adc_trigger,
};

@@ -94,5 +94,5 @@ void at91_adc_test_init(void)
aic->AIC_SVR[AT91C_ID_ADC] = (uint32_t)at91_adc_isr;
aic->AIC_IECR = (1<<AT91C_ID_ADC);

// pitc_schedule_timer(&adc_timer);
pitc_schedule_timer(&adc_timer);
}

+ 1
- 1
src/at91_pitc.c View File

@@ -73,7 +73,7 @@ void at91_pitc_init(void)
{
sysc_register_isr(AT91_SYSIRQ_PIT, &pitc_isr);

*AT91C_PITC_PIMR = (AT91C_PITC_PIV & HZ_TO_PIV(100)) |
*AT91C_PITC_PIMR = (AT91C_PITC_PIV & HZ_TO_PIV(1000)) |
AT91C_PITC_PITEN |
AT91C_PITC_PITIEN;
}

+ 52
- 29
src/at91_twi.c View File

@@ -95,19 +95,15 @@ static void twi_isr(void)

/* transfer really complete? */
if (status & AT91C_TWI_TXCOMP) {
uint32_t addr = (*AT91C_TWI_MMR >> 16) & 0x7F;

/* are we doing a blmc update? */
if (twi_state == TWI_BLMC_UPDATE) {
uint32_t addr = (*AT91C_TWI_MMR >> 16) & 0x7F;
if (addr != TWI_ADDR_BL4) {
/* increase address */
*AT91C_TWI_MMR += (1<<16);

/* send next value to next blmc */
*AT91C_TWI_THR = *twi_data++;
} else {
// TODO:
}
if (twi_state == TWI_BLMC_UPDATE && addr != TWI_ADDR_BL4) {
/* increase address */
*AT91C_TWI_MMR += (1<<16);

/* send next value to next blmc */
*AT91C_TWI_THR = *twi_data++;

} else {
*AT91C_TWI_IDR = AT91C_TWI_TXCOMP | AT91C_TWI_RXRDY | AT91C_TWI_TXRDY | AT91C_TWI_NACK;
@@ -118,11 +114,14 @@ static void twi_isr(void)

uint32_t twi_setpwm(uint8_t *values)
{
if (twi_state == TWI_ERROR)
twi_state = TWI_IDLE;

if (twi_state != TWI_IDLE)
return 1;

twi_state = TWI_BLMC_UPDATE;
twi_data = values; /* data is not copied! */
twi_data = values;
twi_size = 0;
twi_count = 0;

@@ -137,6 +136,9 @@ uint32_t twi_setpwm(uint8_t *values)

uint32_t twi_cmd(uint8_t addr, struct blmc_cmd *cmd)
{
if (twi_state == TWI_ERROR)
twi_state = TWI_IDLE;

if (twi_state != TWI_IDLE)
return 1;

@@ -189,42 +191,63 @@ void at91_twi_test(void)
{
uint32_t i;
for (i = TWI_ADDR_BL1; i <= TWI_ADDR_BL4; i++) {
printf("twi[0x%02lx] ", i);

struct blmc_cmd cmd = {
.cmd = CMD_BOOT_LOADER,
.mode = BLMC_CMD_WRITE | BLMC_CMD_0_ARG,
};
uint32_t ret = twi_cmd(i, &cmd);
printf("twi[0x%02lx](%ld) ", i, ret);
twi_cmd(i, &cmd);

/* TODO: sleep */
volatile uint32_t x;
for (x = 0; x < 200000; x++);

uint8_t buf[16];
cmd.cmd = CMD_GET_INFO,
cmd.mode = BLMC_CMD_READ | BLMC_CMD_0_ARG,
cmd.size = sizeof(buf),
cmd.data = buf,
ret = twi_cmd(i, &cmd);
printf("boot(%ld):'%s' ", ret, buf);
buf[0] = '\0';
cmd.cmd = CMD_GET_INFO;
cmd.mode = BLMC_CMD_READ | BLMC_CMD_0_ARG;
cmd.size = sizeof(buf);
cmd.data = buf;
twi_cmd(i, &cmd);
printf("boot:'%s' ", buf);

/* TODO: single 32bit write */
buf[0] = 0xFF;
buf[1] = 0xFF;
buf[2] = 0xFF;

cmd.cmd = CMD_GET_SIGNATURE;
cmd.size = 4;
ret = twi_cmd(i, &cmd);
printf("sig(%ld):0x%02x%02x%02x ", ret, buf[0], buf[1], buf[2]);
twi_cmd(i, &cmd);
printf("sig:0x%02x%02x%02x\n\r", buf[0], buf[1], buf[2]);

cmd.cmd = CMD_BOOT_APPLICATION;
cmd.mode = BLMC_CMD_WRITE | BLMC_CMD_0_ARG;
cmd.size = 0;
ret = twi_cmd(i, &cmd);
twi_cmd(i, &cmd);

/* TODO: sleep */
for (x = 0; x < 200000; x++);

cmd.cmd = CMD_GET_INFO,
cmd.mode = BLMC_CMD_READ | BLMC_CMD_0_ARG,
cmd.size = sizeof(buf),
cmd.data = buf,
ret = twi_cmd(i, &cmd);
printf("app(%ld):'%s'\n\r", ret, buf);
buf[0] = '\0';
cmd.cmd = CMD_GET_INFO;
cmd.mode = BLMC_CMD_READ | BLMC_CMD_0_ARG;
cmd.size = sizeof(buf);
cmd.data = buf;
twi_cmd(i, &cmd);
printf(" app :'%s' ", buf);

struct blmc_param param;
cmd.cmd = CMD_GET_PARAM;
cmd.mode = BLMC_CMD_READ | BLMC_CMD_0_ARG;
cmd.size = sizeof(param);
cmd.data = (uint8_t *)&param;
twi_cmd(i, &cmd);

printf("pwm:0x%02x-0x%02x Ilimit:0x%03x Imax:0x%03x\n\r",
param.pwm_min, param.pwm_max,
param.current_limit, param.current_max);
}
}



Loading…
Cancel
Save