[分享] FRDM-KW41Z入门——SPI驱动微雪电子纸屏

dql2016   2017-5-26 12:08 楼主
基于例程\SDK_2.2_FRDM-KW41Z\boards\frdmkw41z\driver_examples\dspi\polling_transfer修改,SPI可以使用轮询或者DMA模式。 使用的是微雪1.54英寸电子纸屏幕,6根线驱动。 接线如下: 电子纸屏 FRDM-KW41Z的Arduino UNO接口名 管脚名 信号名 BUSY D2 PTC19 忙检测,低电平有效 RST D3 PTC16 复位,低电平有效 DC D4 PTC4 数据/指令 CS D10 PTA19 SPI1片选 CLK D13 PTA18 SPI1时钟 DIN D11 PTA16 SPI1主机输出 首先配置管脚: 在pin_mux.c文件添加代码:
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /*
  31. * TEXT BELOW IS USED AS SETTING FOR THE PINS TOOL *****************************
  32. PinsProfile:
  33. - !!product 'Pins v2.0'
  34. - !!processor 'MKW41Z512xxx4'
  35. - !!package 'MKW41Z512VHT4'
  36. - !!mcu_data 'ksdk2_0'
  37. - !!processor_version '0.0.0'
  38. * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE PINS TOOL ***
  39. */
  40. #include "fsl_common.h"
  41. #include "fsl_port.h"
  42. #include "pin_mux.h"
  43. #define PIN6_IDX 6u /*!< Pin number for pin 6 in a port */
  44. #define PIN7_IDX 7u /*!< Pin number for pin 7 in a port */
  45. #define PIN16_IDX 16u /*!< Pin number for pin 16 in a port */
  46. #define PIN17_IDX 17u /*!< Pin number for pin 17 in a port */
  47. #define PIN18_IDX 18u /*!< Pin number for pin 18 in a port */
  48. #define PIN19_IDX 19u /*!< Pin number for pin 19 in a port */
  49. #define SOPT5_LPUART0RXSRC_LPUART_RX 0x00u /*!< LPUART0 Receive Data Source Select: LPUART_RX pin */
  50. /*
  51. * TEXT BELOW IS USED AS SETTING FOR THE PINS TOOL *****************************
  52. BOARD_InitPins:
  53. - options: {coreID: singlecore, enableClock: 'true'}
  54. - pin_list:
  55. - {pin_num: '42', peripheral: LPUART0, signal: RX, pin_signal: TSI0_CH2/PTC6/LLWU_P14/XTAL_OUT_EN/I2C1_SCL/UART0_RX/TPM2_CH0/BSM_FRAME}
  56. - {pin_num: '43', peripheral: LPUART0, signal: TX, pin_signal: TSI0_CH3/PTC7/LLWU_P15/SPI0_PCS2/I2C1_SDA/UART0_TX/TPM2_CH1/BSM_DATA}
  57. - {pin_num: '45', peripheral: SPI0, signal: SCK, pin_signal: TSI0_CH4/PTC16/LLWU_P0/SPI0_SCK/I2C0_SDA/UART0_RTS_b/TPM0_CH3}
  58. - {pin_num: '46', peripheral: SPI0, signal: SOUT, pin_signal: TSI0_CH5/PTC17/LLWU_P1/SPI0_SOUT/I2C1_SCL/UART0_RX/BSM_FRAME/DTM_RX}
  59. - {pin_num: '47', peripheral: SPI0, signal: SIN, pin_signal: TSI0_CH6/PTC18/LLWU_P2/SPI0_SIN/I2C1_SDA/UART0_TX/BSM_DATA/DTM_TX}
  60. - {pin_num: '48', peripheral: SPI0, signal: PCS0_SS, pin_signal: TSI0_CH7/PTC19/LLWU_P3/SPI0_PCS0/I2C0_SCL/UART0_CTS_b/BSM_CLK/BLE_RF_ACTIVE}
  61. - {pin_num: '4', peripheral: SPI1, signal: SOUT, pin_signal: TSI0_CH10/PTA16/LLWU_P4/SPI1_SOUT/TPM0_CH0}
  62. - {pin_num: '5', peripheral: SPI1, signal: SIN, pin_signal: TSI0_CH11/PTA17/LLWU_P5/RF_RESET/SPI1_SIN/TPM_CLKIN1}
  63. - {pin_num: '6', peripheral: SPI1, signal: SCK, pin_signal: TSI0_CH12/PTA18/LLWU_P6/SPI1_SCK/TPM2_CH0}
  64. - {pin_num: '7', peripheral: SPI1, signal: PCS0_SS, pin_signal: TSI0_CH13/ADC0_SE5/PTA19/LLWU_P7/SPI1_PCS0/TPM2_CH1}
  65. * BE CAREFUL MODIFYING THIS COMMENT - IT IS YAML SETTINGS FOR THE PINS TOOL ***
  66. */
  67. /*FUNCTION**********************************************************************
  68. *
  69. * Function Name : BOARD_InitPins
  70. * Description : Configures pin routing and optionally pin electrical features.
  71. *
  72. *END**************************************************************************/
  73. void BOARD_InitPins(void) {
  74. CLOCK_EnableClock(kCLOCK_PortA); /* Port A Clock Gate Control: Clock enabled */
  75. CLOCK_EnableClock(kCLOCK_PortC); /* Port C Clock Gate Control: Clock enabled */
  76. [color=#ff0000] PORT_SetPinMux(PORTA, PIN16_IDX, kPORT_MuxAlt2); /* PORTA16 (pin 4) is configured as SPI1_SOUT */
  77. PORT_SetPinMux(PORTA, PIN17_IDX, kPORT_MuxAlt2); /* PORTA17 (pin 5) is configured as SPI1_SIN */
  78. PORT_SetPinMux(PORTA, PIN18_IDX, kPORT_MuxAlt2); /* PORTA18 (pin 6) is configured as SPI1_SCK */
  79. PORT_SetPinMux(PORTA, PIN19_IDX, kPORT_MuxAlt2); /* PORTA19 (pin 7) is configured as SPI1_PCS0 */
  80. PORT_SetPinMux(PORTC, 4u, kPORT_MuxAsGpio); /* PORTC4 (pin 40) is configured as GPIOC4 */
  81. PORT_SetPinMux(PORTC, 16u, kPORT_MuxAsGpio); /* PORTC16 (pin 45) is configured as GPIOC16 */[/color]
  82. PORT_SetPinMux(PORTC, PIN6_IDX, kPORT_MuxAlt4); /* PORTC6 (pin 42) is configured as UART0_RX */
  83. PORT_SetPinMux(PORTC, PIN7_IDX, kPORT_MuxAlt4); /* PORTC7 (pin 43) is configured as UART0_TX */
  84. SIM->SOPT5 = ((SIM->SOPT5 &
  85. (~(SIM_SOPT5_LPUART0RXSRC_MASK))) /* Mask bits to zero which are setting */
  86. | SIM_SOPT5_LPUART0RXSRC(SOPT5_LPUART0RXSRC_LPUART_RX) /* LPUART0 Receive Data Source Select: LPUART_RX pin */
  87. );
  88. }
  89. /*******************************************************************************
  90. * EOF
  91. ******************************************************************************/
主函数:
  1. /*
  2. * Copyright (c) 2013 - 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_device_registers.h"
  31. #include "fsl_debug_console.h"
  32. #include "fsl_dspi.h"
  33. #include "fsl_port.h"
  34. #include "board.h"
  35. #include "pin_mux.h"
  36. #include "clock_config.h"
  37. #include "EPD_drive.h"
  38. #include "EPD_drive_gpio.h"
  39. #include "Display_Lib.h"
  40. /*******************************************************************************
  41. * Definitions
  42. ******************************************************************************/
  43. #define DSPI_MASTER_CLK_SRC DSPI1_CLK_SRC
  44. #define DSPI_MASTER_CLK_FREQ CLOCK_GetFreq(DSPI1_CLK_SRC)
  45. dspi_master_handle_t g_m_handle; //global variable
  46. /*******************************************************************************
  47. * Prototypes
  48. ******************************************************************************/
  49. /*******************************************************************************
  50. * Variables
  51. ******************************************************************************/
  52. /*******************************************************************************
  53. * Code
  54. ******************************************************************************/
  55. void delay_ms(unsigned long xms)
  56. {
  57. volatile uint32_t i = 0;
  58. while(xms--)
  59. {
  60. for (i = 0; i < 4000; ++i)
  61. {
  62. __asm("NOP"); /* delay */
  63. }
  64. }
  65. }
  66. /*!
  67. * [url=home.php?mod=space&uid=159083]@brief[/url] Main function
  68. */
  69. int main(void)
  70. {
  71. int tt=0;
  72. [color=#ff0000] //Define a digital output pin configuration
  73. gpio_pin_config_t out_config =
  74. {
  75. kGPIO_DigitalOutput,
  76. 0,
  77. };
  78. //Define a digital intput pin configuration
  79. gpio_pin_config_t in_config =
  80. {
  81. kGPIO_DigitalInput,
  82. 0,
  83. };
  84. /* Input pin PORT configuration */
  85. port_pin_config_t config = {
  86. kPORT_PullUp,
  87. kPORT_FastSlewRate,
  88. kPORT_PassiveFilterDisable,
  89. kPORT_LowDriveStrength,
  90. kPORT_MuxAsGpio,
  91. };[/color]
  92. BOARD_InitPins();
  93. BOARD_BootClockRUN();
  94. BOARD_InitDebugConsole();
  95. /* Sets the configuration */
  96. [color=#ff0000] PORT_SetPinConfig(PORTC, 19, &config);
  97. FGPIO_PinInit(FGPIOC, 19, &in_config);//D2
  98. GPIO_PinInit(RST_GPIO_Port, RST_Pin, &out_config);//D3
  99. GPIO_PinInit(DC_GPIO_Port, DC_Pin, &out_config);//D4[/color]
  100. PRINTF("DSPI polling to drive the WaveShare 1.54 inch e-Paper example start.\r\n");
  101. uint32_t srcClock_Hz;
  102. [color=#ff0000] dspi_master_config_t masterConfig;
  103. /* Master config */
  104. masterConfig.whichCtar = kDSPI_Ctar0;
  105. masterConfig.ctarConfig.baudRate = 16000000U;
  106. masterConfig.ctarConfig.bitsPerFrame = 8;
  107. masterConfig.ctarConfig.cpol = kDSPI_ClockPolarityActiveLow;
  108. masterConfig.ctarConfig.cpha = kDSPI_ClockPhaseFirstEdge;
  109. masterConfig.ctarConfig.direction = kDSPI_MsbFirst;
  110. masterConfig.ctarConfig.pcsToSckDelayInNanoSec =1;//1000000000U / masterConfig.ctarConfig.baudRate
  111. masterConfig.ctarConfig.lastSckToPcsDelayInNanoSec = 1;
  112. masterConfig.ctarConfig.betweenTransferDelayInNanoSec = 1;
  113. masterConfig.whichPcs = kDSPI_Pcs0;
  114. masterConfig.pcsActiveHighOrLow = kDSPI_PcsActiveLow;
  115. masterConfig.enableContinuousSCK = false;
  116. masterConfig.enableRxFifoOverWrite = false;
  117. masterConfig.enableModifiedTimingFormat = false;
  118. masterConfig.samplePoint = kDSPI_SckToSin0Clock;
  119. srcClock_Hz = DSPI_MASTER_CLK_FREQ;
  120. DSPI_MasterInit(SPI1, &masterConfig, srcClock_Hz);
  121. DSPI_MasterTransferCreateHandle(SPI1, &g_m_handle, NULL, NULL);[/color]
  122. PRINTF("srcClock=%d MHz\r\n",srcClock_Hz/1000000);
  123. PRINTF("sysClock=%d MHz\r\n",CLOCK_GetFreq(kCLOCK_CoreSysClk)/1000000);
  124. /* Start master transfer */
  125. Dis_Clear_full();
  126. delay_ms(500);
  127. Dis_Clear_full();
  128. delay_ms(500);
  129. Dis_Clear_part();
  130. delay_ms(500);
  131. Dis_Clear_part();
  132. Dis_showNum64128(32,48,00);
  133. EPD_Dis_Part(0,0+1*24-1,0,0+48-1,Font2448[13],1);//p
  134. EPD_Dis_Part(0+1*24,0+2*24-1,0,0+48-1,Font2448[12],1);//x
  135. EPD_Dis_Part(0+2*24,0+3*24-1,0,0+48-1,Font2448[11],1);//n
  136. EPD_Dis_Part(0+3*24,0+4*24-1,0,0+48-1,Font2448[16],1);//z
  137. EPD_Dis_Part(0+4*24,0+5*24-1,0,0+48-1,Font2448[1],1);//1
  138. EPD_Dis_Part(0+5*24,0+6*24-1,0,0+48-1,Font2448[4],1);//4
  139. EPD_Dis_Part(0+6*24,0+7*24-1,0,0+48-1,Font2448[15],1);//w
  140. EPD_Dis_Part(0+7*24,0+8*24-1,0,0+48-1,Font2448[14],1);//k
  141. while (1)
  142. {
  143. tt++;
  144. if(tt>99)
  145. tt=0;
  146. Dis_showNum64128(32,48,tt);
  147. delay_ms(500);
  148. }
  149. }
若使用DMA则定义句柄dspi_master_handle_t,在上面主函数里绑定SPI1,然后在接口函数调用DSPI_MasterTransferNonBlocking发送数据;不使用DMA的话调用DSPI_MasterTransferBlocking即可。 EPD_drive_gpio.c接口文件:
  1. dspi_transfer_t masterXfer;
  2. extern dspi_master_handle_t g_m_handle; //global variable
  3. void SPI_Write(unsigned char value)
  4. {
  5. // status_t ret=kStatus_Fail;
  6. uint8_t TxData=value;
  7. [color=#ff0000] masterXfer.txData=&TxData;
  8. masterXfer.dataSize=1;
  9. masterXfer.configFlags = kDSPI_MasterPcs0;//kDSPI_MasterCtar0 | kDSPI_MasterPcs0 | kDSPI_MasterPcsContinuous[/color]
  10. //DSPI_MasterTransferBlocking(SPI1, &masterXfer);
  11. DSPI_MasterTransferNonBlocking(SPI1, &g_m_handle,&masterXfer);
  12. // ret=DSPI_MasterTransferBlocking(SPI1, &masterXfer);
  13. // if(ret!=kStatus_Success)
  14. // {
  15. // PRINTF("ret=%d \r\n",ret);
  16. // }
  17. }
把2个驱动文件添加到工程: 搜狗截图20170526120350.png 字模使用zimo221软件:
zimo_gr.zip (256.91 KB)
(下载次数: 4, 2017-5-26 12:07 上传)
需要左右转换和反白。 搜狗截图20170526120559.png 驱动文件附件:
Display_Lib.h (51.47 KB)
(下载次数: 4, 2017-5-26 12:08 上传)
EPD_drive.c (25.62 KB)
(下载次数: 4, 2017-5-26 12:08 上传)
EPD_drive.h (105.94 KB)
(下载次数: 5, 2017-5-26 12:08 上传)
EPD_drive_gpio.c (1.26 KB)
(下载次数: 3, 2017-5-26 12:08 上传)
EPD_drive_gpio.h (1.39 KB)
(下载次数: 3, 2017-5-26 12:08 上传)
工程文件:采用config工具生成
Project.zip (11.26 MB)
(下载次数: 10, 2017-5-27 15:03 上传)
本帖最后由 dql2016 于 2017-5-27 15:03 编辑

回复评论 (6)

楼主的头像棒棒的,是不是跑题了
点赞  2017-5-26 13:47
引用: machinnneee 发表于 2017-5-26 13:47
楼主的头像棒棒的,是不是跑题了

大胆刁民,见了本公举还不下跪
点赞  2017-5-26 14:05
引用: dql2016 发表于 2017-5-26 14:05
大胆刁民,见了本公举还不下跪

你可拉倒吧,女生做想你这么玩电子的,简直凤毛麟角。
点赞  2017-5-26 14:17
这屏幕不错,效果很喜欢
HELLO_WATER
点赞  2017-5-26 14:44
引用: shinykongcn 发表于 2017-5-26 14:44
这屏幕不错,效果很喜欢

跟我想说的一样
点赞  2017-5-26 15:58
视频里面,29到后面该30的时候,显示变成39, 再变成30的。
这是电子纸屏刷新慢的原因吗?
点赞  2017-5-26 23:47
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复