{"id":527,"date":"2017-11-25T15:06:18","date_gmt":"2017-11-25T14:06:18","guid":{"rendered":"http:\/\/mikrocontroller.bplaced.net\/wordpress\/?page_id=527"},"modified":"2017-12-30T19:41:08","modified_gmt":"2017-12-30T18:41:08","slug":"91-spi-dma-library-stm32f4","status":"publish","type":"page","link":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/?page_id=527","title":{"rendered":"91-SPI-DMA-Library (STM32F4)"},"content":{"rendered":"<p><div id=\"nav-below\" class=\"navigation\"><div class=\"nav-previous\"><a href=\"https:\/\/mikrocontroller.bplaced.net\/wordpress\/?page_id=525\" title=\"90-I2C_GY271-Library (STM32F4)\"><span class=\"meta-nav\">\u2190<\/span> 90-I2C_GY271-Library (STM32F4)<\/a><\/div><\/div><!-- #nav-below --><div id=\"nav-below\" class=\"navigation\"><div class=\"nav-next\"><a href=\"https:\/\/mikrocontroller.bplaced.net\/wordpress\/?page_id=529\" title=\"92-ID_Check-Library (STM32F4)\">92-ID_Check-Library (STM32F4) <span class=\"meta-nav\">&rarr;<\/span><\/a><\/div><\/div><!-- #nav-below --><\/p>\n<p>Hier eine Library um die SPI-Schnittstelle im Master-Mode und per DMA-Betrieb nutzen zu k\u00f6nnnen.<\/p>\n<p>ich habe wieder 3 identische Libs f\u00fcr SPI1, SPI2, SPI3 erstellt.<\/p>\n<p>die SPI-Pins die benutzt werden sollen, m\u00fcssen im C-File eingetragen werden<br \/>\n(im H-File kann der Clock-Vorteiler gew\u00e4hlt werden)<\/p>\n<p>Das setzen der ChipSelect-Leitung muss von der \u00dcbergeordneten Funktion gemacht werden (event. muss das disable Signal in die ISR gepackt werden)<\/p>\n<p>Beim initialisieren kann der Mode (Mode-0 bis Mode-3) und LSB oder MSB gew\u00e4hlt werden. Die Schnittstelle darf nur einmal initialisiert werden.<\/p>\n<p>Zum senden und empfangen gibt es je ein Byte-Array dessen max. Gr\u00f6\u00dfe im H-File eingestellt werden kann.<\/p>\n<p>Vor dem senden m\u00fcssen die Daten in das TX-Array kopiert werden. Beim senden muss die Anzahl der Daten zum senden mit \u00fcbergeben werden. (1 bis Arraysize)<\/p>\n<p>Das eigentliche senden\/empfangen passiert dann per DMA und \u00fcber eine Funktion kann gepr\u00fcft werden ob das senden\/empfangen fertig ist. Falls es fertig ist, wird die Anzahl der empfangenen Bytes zur\u00fcckgegeben und die eigentlichen Daten stehen dann im RX-Array.<\/p>\n<p>Falls die Senderoutine aufgerufen wird, aber das letzte senden noch gar nicht fertig ist, wird solange gewartet und erst im Anschluss daran gesendet.<\/p>\n<p>im Beispiel wurde SPI2 benutzt mit dieser Pinbelegung :<\/p>\n<pre lang=\"c\" line=\"1\">SCK an PB13\r\nMOSI an PB15\r\nMISO an PB14<\/pre>\n<p><strong>Voraussetzungen :<\/strong><\/p>\n<pre lang=\"c\" line=\"1\">Benutzte Module der CooCox-IDE : GPIO, SPI, DMA, MISC\r\nBenutzte Librarys : keine<\/pre>\n<p><strong>Enumerationen (f\u00fcr SPI1) :<\/strong><\/p>\n<pre lang=\"c\" line=\"1\">typedef enum {\r\n  SPI_MODE_0_MSB = 0,  \/\/ CPOL=0, CPHA=0 (MSB-First)\r\n  SPI_MODE_1_MSB,      \/\/ CPOL=0, CPHA=1 (MSB-First)\r\n  SPI_MODE_2_MSB,      \/\/ CPOL=1, CPHA=0 (MSB-First)\r\n  SPI_MODE_3_MSB,      \/\/ CPOL=1, CPHA=1 (MSB-First)\r\n  SPI_MODE_0_LSB,      \/\/ CPOL=0, CPHA=0 (LSB-First)\r\n  SPI_MODE_1_LSB,      \/\/ CPOL=0, CPHA=1 (LSB-First)\r\n  SPI_MODE_2_LSB,      \/\/ CPOL=1, CPHA=0 (LSB-First)\r\n  SPI_MODE_3_LSB       \/\/ CPOL=1, CPHA=1 (LSB-First) \r\n}SPI1_Mode_t;<\/pre>\n<p><strong>Funktionen (f\u00fcr SPI1) :<\/strong><\/p>\n<pre lang=\"c\" line=\"1\">ErrorStatus UB_SPI1_DMA_Init(SPI1_Mode_t mode);\r\nErrorStatus UB_SPI1_DMA_SendBuffer(uint32_t cnt);\r\nuint32_t UB_SPI1_DMA_GetReceivedBytes(void);<\/pre>\n<p><strong>Beispiel :<\/strong><\/p>\n<pre lang=\"c\" line=\"1\">\/\/--------------------------------------------------------------\r\n\/\/ File     : main.c\r\n\/\/ Datum    : 30.12.2014\r\n\/\/ Version  : 1.0\r\n\/\/ Autor    : UB\r\n\/\/ EMail    : mc-4u(@)t-online.de\r\n\/\/ Web      : www.mikrocontroller-4u.de\r\n\/\/ CPU      : STM32F4\r\n\/\/ IDE      : CooCox CoIDE 1.7.4\r\n\/\/ GCC      : 4.7 2012q4\r\n\/\/ Module   : CMSIS_BOOT, M4_CMSIS_CORE\r\n\/\/ Funktion : Demo der SPI_DMA Library\r\n\/\/ Hinweis  : Diese zwei Files muessen auf 8MHz stehen\r\n\/\/              \"cmsis_boot\/stm32f4xx.h\"\r\n\/\/              \"cmsis_boot\/system_stm32f4xx.c\"\r\n\/\/--------------------------------------------------------------\r\n\r\n#include \"main.h\"\r\n#include \"stm32_ub_spi2_dma.h\"\r\n\r\nint main(void)\r\n{\r\n  SystemInit(); \/\/ Quarz Einstellungen aktivieren\r\n\r\n  \/\/ SPI2 im Mode0 initialisieren\r\n  UB_SPI2_DMA_Init(SPI_MODE_0_MSB);\r\n\r\n  \/\/ TX-Puffer mit 3 Bytes Daten fuellen\r\n  SPI2_DMA.tx_buffer[0]=0x55;\r\n  SPI2_DMA.tx_buffer[1]=0xAA;\r\n  SPI2_DMA.tx_buffer[2]=0x01;\r\n\r\n  \/\/ 3Bytes per DMA senden\r\n  UB_SPI2_DMA_SendBuffer(3);\r\n\r\n  while(1)\r\n  {\r\n    if(UB_SPI2_DMA_GetReceivedBytes()&gt;0) {\r\n      \/\/ wenn senden fertig\r\n      \/\/ hier die 3 Bytes vom RX-Buffer auswerten\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>Hier die Library zum\u00a0<strong>Download :<\/strong><\/p>\n<p><a href=\"http:\/\/mikrocontroller.bplaced.net\/wordpress\/wp-content\/uploads\/2014\/12\/ub_stm32f4_spi_dma_v100.zip\">ub_stm32f4_spi_dma_v100<\/a><\/p>\n<p>Hier der komplette CooCox-Projektordner zum\u00a0<strong>Download :<\/strong><\/p>\n<p><a href=\"http:\/\/mikrocontroller.bplaced.net\/wordpress\/wp-content\/uploads\/2014\/12\/Demo_91_SPI_DMA.zip\">Demo_91_SPI_DMA<\/a><\/p>\n<hr \/>\n<h3 id=\"comments-title\">3 Antworten auf <em>91-SPI-DMA-Library (STM32F4)<\/em><\/h3>\n<ol class=\"commentlist\">\n<li id=\"li-comment-2912\" class=\"comment even thread-even depth-1\">\n<div id=\"comment-2912\">\n<div class=\"comment-author vcard\"><img loading=\"lazy\" decoding=\"async\" class=\"avatar avatar-40 photo\" src=\"http:\/\/1.gravatar.com\/avatar\/d76f7a3ade81cb8ceef101af99c148b3?s=40&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D40&amp;r=G\" alt=\"\" width=\"40\" height=\"40\" \/><cite class=\"fn\">Lucas<\/cite> <span class=\"says\">sagt:<\/span><\/div>\n<p><!-- .comment-author .vcard --><\/p>\n<div class=\"comment-meta commentmetadata\">25. Februar 2015 um 17:36<\/div>\n<p><!-- .comment-meta .commentmetadata --><\/p>\n<div class=\"comment-body\">\n<p>Hey Uwe!<\/p>\n<p>Wollte gerade mein EADOG-Display 1701 auf SPI-DMA umstellen, da ich f\u00fcr ein Programm eine Refreshzeit von 500ms des kompletten Displays brauche!<\/p>\n<p>Leider bin ich auf das Problem gestossen, dass die Funktion in der EADOG-Lib -&gt; P_EADOG_SetCursor(\u2026) den COMMAND\/DATA-Pin umschaltet.<\/p>\n<p>Meine einziger \u201cL\u00f6sungsansatz\u201d ist, der aber nicht viel bringen wird, jeweils commands und data getrennt an den DMA zu \u00fcbergeben\u2026.<\/p>\n<p>Hast du eine Idee, ob man das noch irgendwie anders machen kann. Eventuell hat der Display einen internen \u201cBurst-mode\u201d bei dem man keine Koordinaten angeben muss, sondern intern einfach pro empfangenen Byte automatisch die n\u00e4chste Koordinate angesprungen wird? Oder kann man mit dem DMA vielleicht sogar einen normalen GPIOPin mit dem RICHTIGEN Timing zum SPI Transfer schalten(-&gt; glaub ich nicht)?<\/p>\n<p>W\u00fcrde mich freuen wenn du da helfen k\u00f6nntest!<\/p>\n<\/div>\n<\/div>\n<p><!-- #comment-## --><\/p>\n<ul class=\"children\">\n<li id=\"li-comment-2913\" class=\"comment odd alt depth-2\">\n<div id=\"comment-2913\">\n<div class=\"comment-author vcard\"><img loading=\"lazy\" decoding=\"async\" class=\"avatar avatar-40 photo\" src=\"http:\/\/1.gravatar.com\/avatar\/d76f7a3ade81cb8ceef101af99c148b3?s=40&amp;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D40&amp;r=G\" alt=\"\" width=\"40\" height=\"40\" \/><cite class=\"fn\">Lucas<\/cite> <span class=\"says\">sagt:<\/span><\/div>\n<p><!-- .comment-author .vcard --><\/p>\n<div class=\"comment-meta commentmetadata\">25. Februar 2015 um 18:06<\/div>\n<p><!-- .comment-meta .commentmetadata --><\/p>\n<div class=\"comment-body\">\n<p>Im Datenblatt vom Controller<br \/>\n<a href=\"http:\/\/www.lcd-module.de\/deu\/pdf\/grafik\/dogs102-6.pdf\" rel=\"nofollow\">http:\/\/www.lcd-module.de\/deu\/pdf\/grafik\/dogs102-6.pdf<\/a><br \/>\nhab ich gerade etwas gefunden, was sich sehr nach den vom mir betitelten \u201cBurst-Mode\u201d anh\u00f6rt.<\/p>\n<p>Seite 5:<br \/>\n(25) Set Adv. Program Control 0<\/p>\n<p>Jetzt muss ich nur mal rausfinden wie ich die Lib dementsprechend umschreibe\u2026 Sollte aber nocht so heftig sein\u2026<\/p>\n<\/div>\n<\/div>\n<p><!-- #comment-## --><\/p>\n<ul class=\"children\">\n<li id=\"li-comment-2915\" class=\"comment byuser comment-author-admin_ub bypostauthor even depth-3\">\n<div id=\"comment-2915\">\n<div class=\"comment-author vcard\"><img loading=\"lazy\" decoding=\"async\" class=\"avatar avatar-40 photo\" src=\"http:\/\/0.gravatar.com\/avatar\/67426419ead44d5afa132e92685bb460?s=40&amp;d=http%3A%2F%2F0.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D40&amp;r=G\" alt=\"\" width=\"40\" height=\"40\" \/><cite class=\"fn\">admin_ub<\/cite> <span class=\"says\">sagt:<\/span><\/div>\n<p><!-- .comment-author .vcard --><\/p>\n<div class=\"comment-meta commentmetadata\">26. Februar 2015 um 12:08<\/div>\n<p><!-- .comment-meta .commentmetadata --><\/p>\n<div class=\"comment-body\">\n<p>ich hab f\u00fcr den ATMega eine Library f\u00fcr das EA-Dog Display geschrieben. Ein Byte setzt ja 8 Pixel vom LCD gleichzeitig<br \/>\n(und das Display incrementiert die Adresse selbst)<br \/>\nalso kannst du nach jeder \u201cZeile\u201d von 102 Pixel den Cursor<br \/>\num eine Page weiterschalten (das sind dann 8Pixel)<br \/>\nsomit brauchst du zum l\u00f6schen vom kompletten LCD nur 8mal<br \/>\ndie Adresse neu setzen<\/p>\n<\/div>\n<\/div>\n<p><!-- #comment-## --><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Hier eine Library um die SPI-Schnittstelle im Master-Mode und per DMA-Betrieb nutzen zu k\u00f6nnnen. ich habe wieder 3 identische Libs f\u00fcr SPI1, SPI2, SPI3 erstellt. die SPI-Pins die benutzt werden sollen, m\u00fcssen im C-File eingetragen werden (im H-File kann der &hellip; <a href=\"https:\/\/mikrocontroller.bplaced.net\/wordpress\/?page_id=527\">Weiterlesen <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":144,"menu_order":91,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[128],"tags":[149,9,49,7],"class_list":["post-527","page","type-page","status-publish","hentry","category-stm32f4","tag-dma","tag-library","tag-spi","tag-stm32f4"],"_links":{"self":[{"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=\/wp\/v2\/pages\/527","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=527"}],"version-history":[{"count":3,"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=\/wp\/v2\/pages\/527\/revisions"}],"predecessor-version":[{"id":1634,"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=\/wp\/v2\/pages\/527\/revisions\/1634"}],"up":[{"embeddable":true,"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=\/wp\/v2\/pages\/144"}],"wp:attachment":[{"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=527"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=527"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mikrocontroller.bplaced.net\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=527"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}