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

Replace long switch structures with lookup tables #43

Open
dave2 opened this issue Aug 26, 2014 · 1 comment
Open

Replace long switch structures with lookup tables #43

dave2 opened this issue Aug 26, 2014 · 1 comment

Comments

@dave2
Copy link
Owner

dave2 commented Aug 26, 2014

At the moment we have a separate case block in a long switch for each hardware instance (eg, every usart). This results in a lot of code being generated when the pattern for each of these instances is the same. A lookup table containing pointers to the appropriate hardware struct should be both less flash footprint (eg, 16 bytes for 8 usarts vs may switch case blocks) and should be faster (less if branches).

For a USART, we'd do this:

#if defined (_xmega_type_A1U) || defined (_xmega_type_A1)

#define MAX_PORTS 8
#define USART_PORT_INIT {0,0,0,0,0,0,0}
typedef enum {
    usart_c0 = 0,
    usart_c1,
    usart_d0,
    usart_d1,
    usart_e0,
    usart_e1,
    usart_f0,
    usart_f1,
} usart_portname_t;

const USART_t PROGMEM *USARTTOPORT[8] = {
  &USARTC0, &USARTC1, &USARTD0, etc etc };
const uint8_t PROGMEM USARTTOTXPIN = {
 PIN2_bm, PIN6_bm, PIN2_bm, etc etc };

Then in the functions inside usart.c we would only need to call pgm_read_byte() on the lookup table index.

This would allow certain pin-polling specific needs in SPI API as well.

@dave2
Copy link
Owner Author

dave2 commented Aug 27, 2014

So, no you can't do the simple lookup table given above. gcc only applies PROGMEM to the array, not the elements of the array.

Lookup tables may have to be an offset from a base register address, and assume that the sequence of hardware addresses for types of HW is consistent.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant