将数据存储在程序存储器(PROGMEM)中,并通过USB串行通信将其发送到PuTTY屏幕

时间:2018-10-25 07:28:54

标签: c gcc arduino avr-gcc progmem

我正在尝试将数据存储在PROGMEM中并在以后检索。然后通过USB串行通讯将它们发送到屏幕。

int8_t serial_comm_write(const uint8_t *buffer, uint16_t size){
    //Here contains the code from the lib which I don't understand.
    //Basically, it's sending data (char *data) thru to screen. 
}

//This char *data could simply be:
// char *line = "This is stored in RAM"
//usb_send_info(line); would send the "line" to the screen.
void usb_send_info(char *data){
    serial_comm_write((uint8_t *)data, strlen(data));
}

//This doesn't work. I got a squiggly line saying "unknown register name 
//'r0'
//have no idea what it means. 
void usb_send_info_P(const char *data){
    while(pgm_read_byte(data) != 0x00){
        usb_send_info((pgm_read_byte(data++))); 
    }
}

const static char line1[] PROGMEM = "This is stored in flash mem";

usb_send_info_P(line1);

它根本不起作用。有什么技巧或选择吗? 干杯。

2 个答案:

答案 0 :(得分:0)

usb_send_info期望一个char *指向SRAM,而不是FLASH(PROGMEM)。

usb_send_info((pgm_read_byte(data++))); 

pgm_read_byte从给定的PROGMEM地址中读取一个字节/字符。它不返回指针。因此,此函数调用没有意义。

如果您这样更改usb_send_info,它应该可以工作:

void usb_send_info(char data) {
    serial_comm_write((uint8_t *)&data, 1);
}

答案 1 :(得分:0)

得到了队友的一些帮助。对于任何想知道的人,这就是问题的答案。特别感谢乔纳森。

int8_t serial_comm_transmit(uint8_t c)
{
    uint8_t timeout, intr_state;

    if (!usb_configuration) return -1;

    intr_state = SREG;
    cli();
    UENUM = CDC_TX_ENDPOINT;

    if (transmit_previous_timeout) {
        if (!(UEINTX & (1<<RWAL))) {
            SREG = intr_state;
            return -1;
        }
        transmit_previous_timeout = 0;
    }

    timeout = UDFNUML + TRANSMIT_TIMEOUT;
    while (1) {

        if (UEINTX & (1<<RWAL)) break;
        SREG = intr_state;

        if (UDFNUML == timeout) {
            transmit_previous_timeout = 1;
            return -1;
        }

        if (!usb_configuration) return -1;

        intr_state = SREG;
        cli();
        UENUM = CDC_TX_ENDPOINT;
    }

    UEDATX = c;

    if (!(UEINTX & (1<<RWAL))) UEINTX = 0x3A;
    transmit_flush_timer = TRANSMIT_FLUSH_TIMEOUT;
    SREG = intr_state;
    return 0;
}

void usb_send_info(const char *data){
    for (int i = 0; i < strlen_P(data); i ++){
        serial_comm_transmit(pgm_read_byte(&data[i]));
    }
}

static char line1[] PROGMEM = "This is stored in flash mem";

usb_send_info(line1);