[Atmel] 用ASF跑SAMD21程序(12) EEPROM

[复制链接]
5897|4
 楼主| ddllxxrr 发表于 2015-1-1 12:05 | 显示全部楼层 |阅读模式
SAMD21板子上有一片SPI接口的EEPROM,不错,今天驱动它。

首先在前一个工程基础上添加EEPROM模块。



再打开ASF EXPLOER 找到快速开始说明文档。



编译时提示少了管脚定义,这时可以增加一个.h文件。


  1. #ifndef CONF_AT25DFX_H_INCLUDED
  2. #define CONF_AT25DFX_H_INCLUDED

  3. #include <board.h>
  4. #include "at25dfx.h"

  5. //! Select the SPI module AT25DFx is connected to
  6. #define AT25DFX_SPI                 SERIALFLASH_SPI_MODULE

  7. /** AT25DFx device type */
  8. #define AT25DFX_MEM_TYPE            AT25DFX_081A

  9. #define AT25DFX_SPI_PINMUX_SETTING  SERIALFLASH_SPI_MUX_SETTING
  10. #define AT25DFX_SPI_PINMUX_PAD0     SERIALFLASH_SPI_PINMUX_PAD0
  11. #define AT25DFX_SPI_PINMUX_PAD1     SERIALFLASH_SPI_PINMUX_PAD1
  12. #define AT25DFX_SPI_PINMUX_PAD2     SERIALFLASH_SPI_PINMUX_PAD2
  13. #define AT25DFX_SPI_PINMUX_PAD3     SERIALFLASH_SPI_PINMUX_PAD3

  14. #define AT25DFX_CS                  SERIALFLASH_SPI_CS

  15. //! SPI master speed in Hz.
  16. #define AT25DFX_CLOCK_SPEED         120000

  17. #endif  /* CONF_AT25DFX_H_INCLUDED */
这时再编译则编译通过。时钟配置不用改变。但写EEPROM顺序注意一下。有时写保护打开时才可写入,读出是在保护加上时才可读出。
结果我用两种方式,一个是灯亮一下,写完灯灭,再有一种是通过串口输出信息结果如下:



程序清单列出:


  1. /**
  2. * \file
  3. *
  4. * \brief Empty user application template
  5. *
  6. */

  7. /**
  8. * \mainpage User Application template doxygen documentation
  9. *
  10. * \par Empty user application template
  11. *
  12. * This is a bare minimum user application template.
  13. *
  14. * For documentation of the board, go \ref group_common_boards "here" for a link
  15. * to the board-specific documentation.
  16. *
  17. * \par Content
  18. *
  19. * -# Include the ASF header files (through asf.h)
  20. * -# Minimal main function that starts with a call to system_init()
  21. * -# Basic usage of on-board LED and button
  22. * -# "Insert application code here" comment
  23. *
  24. */

  25. /*
  26. * Include header files for all drivers that have been imported from
  27. * Atmel Software Framework (ASF).
  28. */
  29. #include <asf.h>
  30. #include <stdio_serial.h>
  31. #include "conf_at25dfx.h"

  32. bool volatile callback_status = false;

  33. void configure_usart(void);



  34. struct usart_module usart_instance;

  35. void configure_usart(void)
  36. {   
  37.          struct usart_config config_usart;
  38.          usart_get_config_defaults(&config_usart);
  39.          config_usart.baudrate    = 9600;
  40.          config_usart.mux_setting = EXT3_UART_SERCOM_MUX_SETTING;
  41.          config_usart.pinmux_pad0 = EXT3_UART_SERCOM_PINMUX_PAD0;
  42.          config_usart.pinmux_pad1 = EXT3_UART_SERCOM_PINMUX_PAD1;
  43.          config_usart.pinmux_pad2 = EXT3_UART_SERCOM_PINMUX_PAD2;
  44.          config_usart.pinmux_pad3 = EXT3_UART_SERCOM_PINMUX_PAD3;
  45.          
  46.          while (usart_init(&usart_instance, EXT3_UART_MODULE, &config_usart) != STATUS_OK)
  47.           {    }
  48.           stdio_serial_init(&usart_instance, EXT3_UART_MODULE, &config_usart);
  49.           usart_enable(&usart_instance);
  50. }

  51. #define AT25DFX_BUFFER_SIZE  (10)
  52. static uint8_t read_buffer[AT25DFX_BUFFER_SIZE];
  53. static uint8_t write_buffer[AT25DFX_BUFFER_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  54. struct spi_module at25dfx_spi;
  55. struct at25dfx_chip_module at25dfx_chip;

  56. static void at25dfx_init(void)
  57. {
  58.             struct at25dfx_chip_config at25dfx_chip_config;
  59.                 struct spi_config at25dfx_spi_config;
  60.                 at25dfx_spi_get_config_defaults(&at25dfx_spi_config);
  61.                 at25dfx_spi_config.mode_specific.master.baudrate = AT25DFX_CLOCK_SPEED;
  62.                 at25dfx_spi_config.mux_setting = AT25DFX_SPI_PINMUX_SETTING;
  63.                 at25dfx_spi_config.pinmux_pad0 = AT25DFX_SPI_PINMUX_PAD0;
  64.                 at25dfx_spi_config.pinmux_pad1 = AT25DFX_SPI_PINMUX_PAD1;
  65.                 at25dfx_spi_config.pinmux_pad2 = AT25DFX_SPI_PINMUX_PAD2;
  66.                 at25dfx_spi_config.pinmux_pad3 = AT25DFX_SPI_PINMUX_PAD3;
  67.                 spi_init(&at25dfx_spi, AT25DFX_SPI, &at25dfx_spi_config);
  68.                 spi_enable(&at25dfx_spi);
  69.                 at25dfx_chip_config.type = AT25DFX_MEM_TYPE;
  70.                 at25dfx_chip_config.cs_pin = AT25DFX_CS;
  71.                 at25dfx_chip_init(&at25dfx_chip, &at25dfx_spi, &at25dfx_chip_config);
  72. }
  73. uint8_t i;       
  74. int main (void)
  75. {
  76.        
  77.         system_init();
  78.         configure_usart();
  79.         at25dfx_init();
  80.         uint8_t string[] = "This is AT25DFX test!\r\n";
  81.         uint8_t string2[] = "This is AT25DFX test Good,Very Happy!\r\n";
  82.        
  83.         usart_write_buffer_wait(&usart_instance, string, sizeof(string));
  84.        
  85.        
  86.                    system_interrupt_enable_global();
  87.          at25dfx_chip_wake(&at25dfx_chip);
  88.           port_pin_set_output_level(LED_0_PIN, false);
  89.          if (at25dfx_chip_check_presence(&at25dfx_chip) != STATUS_OK)
  90.          {        // Handle missing or non-responsive device
  91.          }   
  92.          at25dfx_chip_read_buffer(&at25dfx_chip, 0x0000, read_buffer, AT25DFX_BUFFER_SIZE);
  93.          at25dfx_chip_set_sector_protect(&at25dfx_chip, 0x10000, false);
  94.          at25dfx_chip_erase_block(&at25dfx_chip, 0x10000, AT25DFX_BLOCK_SIZE_4KB);
  95.          at25dfx_chip_write_buffer(&at25dfx_chip, 0x10000, write_buffer, AT25DFX_BUFFER_SIZE);
  96.           
  97.           
  98.           
  99.          at25dfx_chip_set_global_sector_protect(&at25dfx_chip, true);
  100.           at25dfx_chip_read_buffer(&at25dfx_chip, 0x10000, read_buffer, AT25DFX_BUFFER_SIZE);
  101.           for(i=0;i<=11;i++)
  102.           {
  103.                   if(read_buffer[i]!= write_buffer[i])
  104.                   {
  105.                           break;
  106.                   }
  107.                   
  108.                   
  109.           }
  110.           if (i<= 11)
  111.           {
  112.                   printf("the test is good!!!");
  113.           }
  114.          at25dfx_chip_sleep(&at25dfx_chip);
  115.           
  116.            port_pin_set_output_level(LED_0_PIN, true);
  117.           while (true)
  118.           {
  119.                         ;
  120.      }
  121. }



运行时截图:




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
老鱼探戈 发表于 2015-1-1 21:52 | 显示全部楼层
抢沙发,点赞!
lvyunhua 发表于 2015-1-1 22:05 | 显示全部楼层
楼主加油!支持!
 楼主| ddllxxrr 发表于 2015-1-2 15:49 | 显示全部楼层
老鱼探戈 发表于 2015-1-1 21:52
抢沙发,点赞!

 楼主| ddllxxrr 发表于 2015-1-2 15:49 | 显示全部楼层
lvyunhua 发表于 2015-1-1 22:05
楼主加油!支持!

您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部